Home | History | Annotate | Line # | Download | only in nfs
nfs_subs.c revision 1.96
      1 /*	$NetBSD: nfs_subs.c,v 1.96 2001/09/15 16:13:01 chs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)nfs_subs.c	8.8 (Berkeley) 5/22/95
     39  */
     40 
     41 /*
     42  * Copyright 2000 Wasabi Systems, Inc.
     43  * All rights reserved.
     44  *
     45  * Written by Frank van der Linden for Wasabi Systems, Inc.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. All advertising materials mentioning features or use of this software
     56  *    must display the following acknowledgement:
     57  *      This product includes software developed for the NetBSD Project by
     58  *      Wasabi Systems, Inc.
     59  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     60  *    or promote products derived from this software without specific prior
     61  *    written permission.
     62  *
     63  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     65  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     66  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     67  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     68  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     69  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     70  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     71  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     72  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     73  * POSSIBILITY OF SUCH DAMAGE.
     74  */
     75 
     76 #include "fs_nfs.h"
     77 #include "opt_nfs.h"
     78 #include "opt_nfsserver.h"
     79 #include "opt_iso.h"
     80 #include "opt_inet.h"
     81 
     82 /*
     83  * These functions support the macros and help fiddle mbuf chains for
     84  * the nfs op functions. They do things like create the rpc header and
     85  * copy data between mbuf chains and uio lists.
     86  */
     87 #include <sys/param.h>
     88 #include <sys/proc.h>
     89 #include <sys/systm.h>
     90 #include <sys/kernel.h>
     91 #include <sys/mount.h>
     92 #include <sys/vnode.h>
     93 #include <sys/namei.h>
     94 #include <sys/mbuf.h>
     95 #include <sys/socket.h>
     96 #include <sys/stat.h>
     97 #include <sys/malloc.h>
     98 #include <sys/time.h>
     99 #include <sys/dirent.h>
    100 
    101 #include <uvm/uvm_extern.h>
    102 
    103 #include <nfs/rpcv2.h>
    104 #include <nfs/nfsproto.h>
    105 #include <nfs/nfsnode.h>
    106 #include <nfs/nfs.h>
    107 #include <nfs/xdr_subs.h>
    108 #include <nfs/nfsm_subs.h>
    109 #include <nfs/nfsmount.h>
    110 #include <nfs/nqnfs.h>
    111 #include <nfs/nfsrtt.h>
    112 #include <nfs/nfs_var.h>
    113 
    114 #include <miscfs/specfs/specdev.h>
    115 
    116 #include <netinet/in.h>
    117 #ifdef ISO
    118 #include <netiso/iso.h>
    119 #endif
    120 
    121 /*
    122  * Data items converted to xdr at startup, since they are constant
    123  * This is kinda hokey, but may save a little time doing byte swaps
    124  */
    125 u_int32_t nfs_xdrneg1;
    126 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
    127 	rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
    128 	rpc_auth_kerb;
    129 u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
    130 
    131 /* And other global data */
    132 static u_int32_t nfs_xid = 0;
    133 const nfstype nfsv2_type[9] =
    134 	{ NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON, NFCHR, NFNON };
    135 const nfstype nfsv3_type[9] =
    136 	{ NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK, NFFIFO, NFNON };
    137 const enum vtype nv2tov_type[8] =
    138 	{ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON };
    139 const enum vtype nv3tov_type[8] =
    140 	{ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO };
    141 int nfs_ticks;
    142 
    143 /* NFS client/server stats. */
    144 struct nfsstats nfsstats;
    145 
    146 /*
    147  * Mapping of old NFS Version 2 RPC numbers to generic numbers.
    148  */
    149 const int nfsv3_procid[NFS_NPROCS] = {
    150 	NFSPROC_NULL,
    151 	NFSPROC_GETATTR,
    152 	NFSPROC_SETATTR,
    153 	NFSPROC_NOOP,
    154 	NFSPROC_LOOKUP,
    155 	NFSPROC_READLINK,
    156 	NFSPROC_READ,
    157 	NFSPROC_NOOP,
    158 	NFSPROC_WRITE,
    159 	NFSPROC_CREATE,
    160 	NFSPROC_REMOVE,
    161 	NFSPROC_RENAME,
    162 	NFSPROC_LINK,
    163 	NFSPROC_SYMLINK,
    164 	NFSPROC_MKDIR,
    165 	NFSPROC_RMDIR,
    166 	NFSPROC_READDIR,
    167 	NFSPROC_FSSTAT,
    168 	NFSPROC_NOOP,
    169 	NFSPROC_NOOP,
    170 	NFSPROC_NOOP,
    171 	NFSPROC_NOOP,
    172 	NFSPROC_NOOP,
    173 	NFSPROC_NOOP,
    174 	NFSPROC_NOOP,
    175 	NFSPROC_NOOP
    176 };
    177 
    178 /*
    179  * and the reverse mapping from generic to Version 2 procedure numbers
    180  */
    181 const int nfsv2_procid[NFS_NPROCS] = {
    182 	NFSV2PROC_NULL,
    183 	NFSV2PROC_GETATTR,
    184 	NFSV2PROC_SETATTR,
    185 	NFSV2PROC_LOOKUP,
    186 	NFSV2PROC_NOOP,
    187 	NFSV2PROC_READLINK,
    188 	NFSV2PROC_READ,
    189 	NFSV2PROC_WRITE,
    190 	NFSV2PROC_CREATE,
    191 	NFSV2PROC_MKDIR,
    192 	NFSV2PROC_SYMLINK,
    193 	NFSV2PROC_CREATE,
    194 	NFSV2PROC_REMOVE,
    195 	NFSV2PROC_RMDIR,
    196 	NFSV2PROC_RENAME,
    197 	NFSV2PROC_LINK,
    198 	NFSV2PROC_READDIR,
    199 	NFSV2PROC_NOOP,
    200 	NFSV2PROC_STATFS,
    201 	NFSV2PROC_NOOP,
    202 	NFSV2PROC_NOOP,
    203 	NFSV2PROC_NOOP,
    204 	NFSV2PROC_NOOP,
    205 	NFSV2PROC_NOOP,
    206 	NFSV2PROC_NOOP,
    207 	NFSV2PROC_NOOP,
    208 };
    209 
    210 /*
    211  * Maps errno values to nfs error numbers.
    212  * Use NFSERR_IO as the catch all for ones not specifically defined in
    213  * RFC 1094.
    214  */
    215 static const u_char nfsrv_v2errmap[ELAST] = {
    216   NFSERR_PERM,	NFSERR_NOENT,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    217   NFSERR_NXIO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    218   NFSERR_IO,	NFSERR_IO,	NFSERR_ACCES,	NFSERR_IO,	NFSERR_IO,
    219   NFSERR_IO,	NFSERR_EXIST,	NFSERR_IO,	NFSERR_NODEV,	NFSERR_NOTDIR,
    220   NFSERR_ISDIR,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    221   NFSERR_IO,	NFSERR_FBIG,	NFSERR_NOSPC,	NFSERR_IO,	NFSERR_ROFS,
    222   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    223   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    224   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    225   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    226   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    227   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    228   NFSERR_IO,	NFSERR_IO,	NFSERR_NAMETOL,	NFSERR_IO,	NFSERR_IO,
    229   NFSERR_NOTEMPTY, NFSERR_IO,	NFSERR_IO,	NFSERR_DQUOT,	NFSERR_STALE,
    230   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    231   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    232   NFSERR_IO,	NFSERR_IO,
    233 };
    234 
    235 /*
    236  * Maps errno values to nfs error numbers.
    237  * Although it is not obvious whether or not NFS clients really care if
    238  * a returned error value is in the specified list for the procedure, the
    239  * safest thing to do is filter them appropriately. For Version 2, the
    240  * X/Open XNFS document is the only specification that defines error values
    241  * for each RPC (The RFC simply lists all possible error values for all RPCs),
    242  * so I have decided to not do this for Version 2.
    243  * The first entry is the default error return and the rest are the valid
    244  * errors for that RPC in increasing numeric order.
    245  */
    246 static const short nfsv3err_null[] = {
    247 	0,
    248 	0,
    249 };
    250 
    251 static const short nfsv3err_getattr[] = {
    252 	NFSERR_IO,
    253 	NFSERR_IO,
    254 	NFSERR_STALE,
    255 	NFSERR_BADHANDLE,
    256 	NFSERR_SERVERFAULT,
    257 	0,
    258 };
    259 
    260 static const short nfsv3err_setattr[] = {
    261 	NFSERR_IO,
    262 	NFSERR_PERM,
    263 	NFSERR_IO,
    264 	NFSERR_ACCES,
    265 	NFSERR_INVAL,
    266 	NFSERR_NOSPC,
    267 	NFSERR_ROFS,
    268 	NFSERR_DQUOT,
    269 	NFSERR_STALE,
    270 	NFSERR_BADHANDLE,
    271 	NFSERR_NOT_SYNC,
    272 	NFSERR_SERVERFAULT,
    273 	0,
    274 };
    275 
    276 static const short nfsv3err_lookup[] = {
    277 	NFSERR_IO,
    278 	NFSERR_NOENT,
    279 	NFSERR_IO,
    280 	NFSERR_ACCES,
    281 	NFSERR_NOTDIR,
    282 	NFSERR_NAMETOL,
    283 	NFSERR_STALE,
    284 	NFSERR_BADHANDLE,
    285 	NFSERR_SERVERFAULT,
    286 	0,
    287 };
    288 
    289 static const short nfsv3err_access[] = {
    290 	NFSERR_IO,
    291 	NFSERR_IO,
    292 	NFSERR_STALE,
    293 	NFSERR_BADHANDLE,
    294 	NFSERR_SERVERFAULT,
    295 	0,
    296 };
    297 
    298 static const short nfsv3err_readlink[] = {
    299 	NFSERR_IO,
    300 	NFSERR_IO,
    301 	NFSERR_ACCES,
    302 	NFSERR_INVAL,
    303 	NFSERR_STALE,
    304 	NFSERR_BADHANDLE,
    305 	NFSERR_NOTSUPP,
    306 	NFSERR_SERVERFAULT,
    307 	0,
    308 };
    309 
    310 static const short nfsv3err_read[] = {
    311 	NFSERR_IO,
    312 	NFSERR_IO,
    313 	NFSERR_NXIO,
    314 	NFSERR_ACCES,
    315 	NFSERR_INVAL,
    316 	NFSERR_STALE,
    317 	NFSERR_BADHANDLE,
    318 	NFSERR_SERVERFAULT,
    319 	NFSERR_JUKEBOX,
    320 	0,
    321 };
    322 
    323 static const short nfsv3err_write[] = {
    324 	NFSERR_IO,
    325 	NFSERR_IO,
    326 	NFSERR_ACCES,
    327 	NFSERR_INVAL,
    328 	NFSERR_FBIG,
    329 	NFSERR_NOSPC,
    330 	NFSERR_ROFS,
    331 	NFSERR_DQUOT,
    332 	NFSERR_STALE,
    333 	NFSERR_BADHANDLE,
    334 	NFSERR_SERVERFAULT,
    335 	NFSERR_JUKEBOX,
    336 	0,
    337 };
    338 
    339 static const short nfsv3err_create[] = {
    340 	NFSERR_IO,
    341 	NFSERR_IO,
    342 	NFSERR_ACCES,
    343 	NFSERR_EXIST,
    344 	NFSERR_NOTDIR,
    345 	NFSERR_NOSPC,
    346 	NFSERR_ROFS,
    347 	NFSERR_NAMETOL,
    348 	NFSERR_DQUOT,
    349 	NFSERR_STALE,
    350 	NFSERR_BADHANDLE,
    351 	NFSERR_NOTSUPP,
    352 	NFSERR_SERVERFAULT,
    353 	0,
    354 };
    355 
    356 static const short nfsv3err_mkdir[] = {
    357 	NFSERR_IO,
    358 	NFSERR_IO,
    359 	NFSERR_ACCES,
    360 	NFSERR_EXIST,
    361 	NFSERR_NOTDIR,
    362 	NFSERR_NOSPC,
    363 	NFSERR_ROFS,
    364 	NFSERR_NAMETOL,
    365 	NFSERR_DQUOT,
    366 	NFSERR_STALE,
    367 	NFSERR_BADHANDLE,
    368 	NFSERR_NOTSUPP,
    369 	NFSERR_SERVERFAULT,
    370 	0,
    371 };
    372 
    373 static const short nfsv3err_symlink[] = {
    374 	NFSERR_IO,
    375 	NFSERR_IO,
    376 	NFSERR_ACCES,
    377 	NFSERR_EXIST,
    378 	NFSERR_NOTDIR,
    379 	NFSERR_NOSPC,
    380 	NFSERR_ROFS,
    381 	NFSERR_NAMETOL,
    382 	NFSERR_DQUOT,
    383 	NFSERR_STALE,
    384 	NFSERR_BADHANDLE,
    385 	NFSERR_NOTSUPP,
    386 	NFSERR_SERVERFAULT,
    387 	0,
    388 };
    389 
    390 static const short nfsv3err_mknod[] = {
    391 	NFSERR_IO,
    392 	NFSERR_IO,
    393 	NFSERR_ACCES,
    394 	NFSERR_EXIST,
    395 	NFSERR_NOTDIR,
    396 	NFSERR_NOSPC,
    397 	NFSERR_ROFS,
    398 	NFSERR_NAMETOL,
    399 	NFSERR_DQUOT,
    400 	NFSERR_STALE,
    401 	NFSERR_BADHANDLE,
    402 	NFSERR_NOTSUPP,
    403 	NFSERR_SERVERFAULT,
    404 	NFSERR_BADTYPE,
    405 	0,
    406 };
    407 
    408 static const short nfsv3err_remove[] = {
    409 	NFSERR_IO,
    410 	NFSERR_NOENT,
    411 	NFSERR_IO,
    412 	NFSERR_ACCES,
    413 	NFSERR_NOTDIR,
    414 	NFSERR_ROFS,
    415 	NFSERR_NAMETOL,
    416 	NFSERR_STALE,
    417 	NFSERR_BADHANDLE,
    418 	NFSERR_SERVERFAULT,
    419 	0,
    420 };
    421 
    422 static const short nfsv3err_rmdir[] = {
    423 	NFSERR_IO,
    424 	NFSERR_NOENT,
    425 	NFSERR_IO,
    426 	NFSERR_ACCES,
    427 	NFSERR_EXIST,
    428 	NFSERR_NOTDIR,
    429 	NFSERR_INVAL,
    430 	NFSERR_ROFS,
    431 	NFSERR_NAMETOL,
    432 	NFSERR_NOTEMPTY,
    433 	NFSERR_STALE,
    434 	NFSERR_BADHANDLE,
    435 	NFSERR_NOTSUPP,
    436 	NFSERR_SERVERFAULT,
    437 	0,
    438 };
    439 
    440 static const short nfsv3err_rename[] = {
    441 	NFSERR_IO,
    442 	NFSERR_NOENT,
    443 	NFSERR_IO,
    444 	NFSERR_ACCES,
    445 	NFSERR_EXIST,
    446 	NFSERR_XDEV,
    447 	NFSERR_NOTDIR,
    448 	NFSERR_ISDIR,
    449 	NFSERR_INVAL,
    450 	NFSERR_NOSPC,
    451 	NFSERR_ROFS,
    452 	NFSERR_MLINK,
    453 	NFSERR_NAMETOL,
    454 	NFSERR_NOTEMPTY,
    455 	NFSERR_DQUOT,
    456 	NFSERR_STALE,
    457 	NFSERR_BADHANDLE,
    458 	NFSERR_NOTSUPP,
    459 	NFSERR_SERVERFAULT,
    460 	0,
    461 };
    462 
    463 static const short nfsv3err_link[] = {
    464 	NFSERR_IO,
    465 	NFSERR_IO,
    466 	NFSERR_ACCES,
    467 	NFSERR_EXIST,
    468 	NFSERR_XDEV,
    469 	NFSERR_NOTDIR,
    470 	NFSERR_INVAL,
    471 	NFSERR_NOSPC,
    472 	NFSERR_ROFS,
    473 	NFSERR_MLINK,
    474 	NFSERR_NAMETOL,
    475 	NFSERR_DQUOT,
    476 	NFSERR_STALE,
    477 	NFSERR_BADHANDLE,
    478 	NFSERR_NOTSUPP,
    479 	NFSERR_SERVERFAULT,
    480 	0,
    481 };
    482 
    483 static const short nfsv3err_readdir[] = {
    484 	NFSERR_IO,
    485 	NFSERR_IO,
    486 	NFSERR_ACCES,
    487 	NFSERR_NOTDIR,
    488 	NFSERR_STALE,
    489 	NFSERR_BADHANDLE,
    490 	NFSERR_BAD_COOKIE,
    491 	NFSERR_TOOSMALL,
    492 	NFSERR_SERVERFAULT,
    493 	0,
    494 };
    495 
    496 static const short nfsv3err_readdirplus[] = {
    497 	NFSERR_IO,
    498 	NFSERR_IO,
    499 	NFSERR_ACCES,
    500 	NFSERR_NOTDIR,
    501 	NFSERR_STALE,
    502 	NFSERR_BADHANDLE,
    503 	NFSERR_BAD_COOKIE,
    504 	NFSERR_NOTSUPP,
    505 	NFSERR_TOOSMALL,
    506 	NFSERR_SERVERFAULT,
    507 	0,
    508 };
    509 
    510 static const short nfsv3err_fsstat[] = {
    511 	NFSERR_IO,
    512 	NFSERR_IO,
    513 	NFSERR_STALE,
    514 	NFSERR_BADHANDLE,
    515 	NFSERR_SERVERFAULT,
    516 	0,
    517 };
    518 
    519 static const short nfsv3err_fsinfo[] = {
    520 	NFSERR_STALE,
    521 	NFSERR_STALE,
    522 	NFSERR_BADHANDLE,
    523 	NFSERR_SERVERFAULT,
    524 	0,
    525 };
    526 
    527 static const short nfsv3err_pathconf[] = {
    528 	NFSERR_STALE,
    529 	NFSERR_STALE,
    530 	NFSERR_BADHANDLE,
    531 	NFSERR_SERVERFAULT,
    532 	0,
    533 };
    534 
    535 static const short nfsv3err_commit[] = {
    536 	NFSERR_IO,
    537 	NFSERR_IO,
    538 	NFSERR_STALE,
    539 	NFSERR_BADHANDLE,
    540 	NFSERR_SERVERFAULT,
    541 	0,
    542 };
    543 
    544 static const short * const nfsrv_v3errmap[] = {
    545 	nfsv3err_null,
    546 	nfsv3err_getattr,
    547 	nfsv3err_setattr,
    548 	nfsv3err_lookup,
    549 	nfsv3err_access,
    550 	nfsv3err_readlink,
    551 	nfsv3err_read,
    552 	nfsv3err_write,
    553 	nfsv3err_create,
    554 	nfsv3err_mkdir,
    555 	nfsv3err_symlink,
    556 	nfsv3err_mknod,
    557 	nfsv3err_remove,
    558 	nfsv3err_rmdir,
    559 	nfsv3err_rename,
    560 	nfsv3err_link,
    561 	nfsv3err_readdir,
    562 	nfsv3err_readdirplus,
    563 	nfsv3err_fsstat,
    564 	nfsv3err_fsinfo,
    565 	nfsv3err_pathconf,
    566 	nfsv3err_commit,
    567 };
    568 
    569 extern struct nfsrtt nfsrtt;
    570 extern time_t nqnfsstarttime;
    571 extern int nqsrv_clockskew;
    572 extern int nqsrv_writeslack;
    573 extern int nqsrv_maxlease;
    574 extern const int nqnfs_piggy[NFS_NPROCS];
    575 extern struct nfsnodehashhead *nfsnodehashtbl;
    576 extern u_long nfsnodehash;
    577 
    578 LIST_HEAD(nfsnodehashhead, nfsnode);
    579 u_long nfsdirhashmask;
    580 
    581 int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *));
    582 
    583 /*
    584  * Create the header for an rpc request packet
    585  * The hsiz is the size of the rest of the nfs request header.
    586  * (just used to decide if a cluster is a good idea)
    587  */
    588 struct mbuf *
    589 nfsm_reqh(vp, procid, hsiz, bposp)
    590 	struct vnode *vp;
    591 	u_long procid;
    592 	int hsiz;
    593 	caddr_t *bposp;
    594 {
    595 	struct mbuf *mb;
    596 	caddr_t bpos;
    597 	struct nfsmount *nmp;
    598 #ifndef NFS_V2_ONLY
    599 	u_int32_t *tl;
    600 	struct mbuf *mb2;
    601 	int nqflag;
    602 #endif
    603 
    604 	MGET(mb, M_WAIT, MT_DATA);
    605 	if (hsiz >= MINCLSIZE)
    606 		MCLGET(mb, M_WAIT);
    607 	mb->m_len = 0;
    608 	bpos = mtod(mb, caddr_t);
    609 
    610 	/*
    611 	 * For NQNFS, add lease request.
    612 	 */
    613 	if (vp) {
    614 		nmp = VFSTONFS(vp->v_mount);
    615 #ifndef NFS_V2_ONLY
    616 		if (nmp->nm_flag & NFSMNT_NQNFS) {
    617 			nqflag = NQNFS_NEEDLEASE(vp, procid);
    618 			if (nqflag) {
    619 				nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
    620 				*tl++ = txdr_unsigned(nqflag);
    621 				*tl = txdr_unsigned(nmp->nm_leaseterm);
    622 			} else {
    623 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    624 				*tl = 0;
    625 			}
    626 		}
    627 #endif
    628 	}
    629 	/* Finally, return values */
    630 	*bposp = bpos;
    631 	return (mb);
    632 }
    633 
    634 /*
    635  * Build the RPC header and fill in the authorization info.
    636  * The authorization string argument is only used when the credentials
    637  * come from outside of the kernel.
    638  * Returns the head of the mbuf list.
    639  */
    640 struct mbuf *
    641 nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
    642 	verf_str, mrest, mrest_len, mbp, xidp)
    643 	struct ucred *cr;
    644 	int nmflag;
    645 	int procid;
    646 	int auth_type;
    647 	int auth_len;
    648 	char *auth_str;
    649 	int verf_len;
    650 	char *verf_str;
    651 	struct mbuf *mrest;
    652 	int mrest_len;
    653 	struct mbuf **mbp;
    654 	u_int32_t *xidp;
    655 {
    656 	struct mbuf *mb;
    657 	u_int32_t *tl;
    658 	caddr_t bpos;
    659 	int i;
    660 	struct mbuf *mreq, *mb2;
    661 	int siz, grpsiz, authsiz;
    662 	struct timeval tv;
    663 	static u_int32_t base;
    664 
    665 	authsiz = nfsm_rndup(auth_len);
    666 	MGETHDR(mb, M_WAIT, MT_DATA);
    667 	if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
    668 		MCLGET(mb, M_WAIT);
    669 	} else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
    670 		MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
    671 	} else {
    672 		MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
    673 	}
    674 	mb->m_len = 0;
    675 	mreq = mb;
    676 	bpos = mtod(mb, caddr_t);
    677 
    678 	/*
    679 	 * First the RPC header.
    680 	 */
    681 	nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
    682 
    683 	/*
    684 	 * derive initial xid from system time
    685 	 * XXX time is invalid if root not yet mounted
    686 	 */
    687 	if (!base && (rootvp)) {
    688 		microtime(&tv);
    689 		base = tv.tv_sec << 12;
    690 		nfs_xid = base;
    691 	}
    692 	/*
    693 	 * Skip zero xid if it should ever happen.
    694 	 */
    695 	if (++nfs_xid == 0)
    696 		nfs_xid++;
    697 
    698 	*tl++ = *xidp = txdr_unsigned(nfs_xid);
    699 	*tl++ = rpc_call;
    700 	*tl++ = rpc_vers;
    701 	if (nmflag & NFSMNT_NQNFS) {
    702 		*tl++ = txdr_unsigned(NQNFS_PROG);
    703 		*tl++ = txdr_unsigned(NQNFS_VER3);
    704 	} else {
    705 		*tl++ = txdr_unsigned(NFS_PROG);
    706 		if (nmflag & NFSMNT_NFSV3)
    707 			*tl++ = txdr_unsigned(NFS_VER3);
    708 		else
    709 			*tl++ = txdr_unsigned(NFS_VER2);
    710 	}
    711 	if (nmflag & NFSMNT_NFSV3)
    712 		*tl++ = txdr_unsigned(procid);
    713 	else
    714 		*tl++ = txdr_unsigned(nfsv2_procid[procid]);
    715 
    716 	/*
    717 	 * And then the authorization cred.
    718 	 */
    719 	*tl++ = txdr_unsigned(auth_type);
    720 	*tl = txdr_unsigned(authsiz);
    721 	switch (auth_type) {
    722 	case RPCAUTH_UNIX:
    723 		nfsm_build(tl, u_int32_t *, auth_len);
    724 		*tl++ = 0;		/* stamp ?? */
    725 		*tl++ = 0;		/* NULL hostname */
    726 		*tl++ = txdr_unsigned(cr->cr_uid);
    727 		*tl++ = txdr_unsigned(cr->cr_gid);
    728 		grpsiz = (auth_len >> 2) - 5;
    729 		*tl++ = txdr_unsigned(grpsiz);
    730 		for (i = 0; i < grpsiz; i++)
    731 			*tl++ = txdr_unsigned(cr->cr_groups[i]);
    732 		break;
    733 	case RPCAUTH_KERB4:
    734 		siz = auth_len;
    735 		while (siz > 0) {
    736 			if (M_TRAILINGSPACE(mb) == 0) {
    737 				MGET(mb2, M_WAIT, MT_DATA);
    738 				if (siz >= MINCLSIZE)
    739 					MCLGET(mb2, M_WAIT);
    740 				mb->m_next = mb2;
    741 				mb = mb2;
    742 				mb->m_len = 0;
    743 				bpos = mtod(mb, caddr_t);
    744 			}
    745 			i = min(siz, M_TRAILINGSPACE(mb));
    746 			memcpy(bpos, auth_str, i);
    747 			mb->m_len += i;
    748 			auth_str += i;
    749 			bpos += i;
    750 			siz -= i;
    751 		}
    752 		if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
    753 			for (i = 0; i < siz; i++)
    754 				*bpos++ = '\0';
    755 			mb->m_len += siz;
    756 		}
    757 		break;
    758 	};
    759 
    760 	/*
    761 	 * And the verifier...
    762 	 */
    763 	nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
    764 	if (verf_str) {
    765 		*tl++ = txdr_unsigned(RPCAUTH_KERB4);
    766 		*tl = txdr_unsigned(verf_len);
    767 		siz = verf_len;
    768 		while (siz > 0) {
    769 			if (M_TRAILINGSPACE(mb) == 0) {
    770 				MGET(mb2, M_WAIT, MT_DATA);
    771 				if (siz >= MINCLSIZE)
    772 					MCLGET(mb2, M_WAIT);
    773 				mb->m_next = mb2;
    774 				mb = mb2;
    775 				mb->m_len = 0;
    776 				bpos = mtod(mb, caddr_t);
    777 			}
    778 			i = min(siz, M_TRAILINGSPACE(mb));
    779 			memcpy(bpos, verf_str, i);
    780 			mb->m_len += i;
    781 			verf_str += i;
    782 			bpos += i;
    783 			siz -= i;
    784 		}
    785 		if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
    786 			for (i = 0; i < siz; i++)
    787 				*bpos++ = '\0';
    788 			mb->m_len += siz;
    789 		}
    790 	} else {
    791 		*tl++ = txdr_unsigned(RPCAUTH_NULL);
    792 		*tl = 0;
    793 	}
    794 	mb->m_next = mrest;
    795 	mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
    796 	mreq->m_pkthdr.rcvif = (struct ifnet *)0;
    797 	*mbp = mb;
    798 	return (mreq);
    799 }
    800 
    801 /*
    802  * copies mbuf chain to the uio scatter/gather list
    803  */
    804 int
    805 nfsm_mbuftouio(mrep, uiop, siz, dpos)
    806 	struct mbuf **mrep;
    807 	struct uio *uiop;
    808 	int siz;
    809 	caddr_t *dpos;
    810 {
    811 	char *mbufcp, *uiocp;
    812 	int xfer, left, len;
    813 	struct mbuf *mp;
    814 	long uiosiz, rem;
    815 	int error = 0;
    816 
    817 	mp = *mrep;
    818 	mbufcp = *dpos;
    819 	len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
    820 	rem = nfsm_rndup(siz)-siz;
    821 	while (siz > 0) {
    822 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
    823 			return (EFBIG);
    824 		left = uiop->uio_iov->iov_len;
    825 		uiocp = uiop->uio_iov->iov_base;
    826 		if (left > siz)
    827 			left = siz;
    828 		uiosiz = left;
    829 		while (left > 0) {
    830 			while (len == 0) {
    831 				mp = mp->m_next;
    832 				if (mp == NULL)
    833 					return (EBADRPC);
    834 				mbufcp = mtod(mp, caddr_t);
    835 				len = mp->m_len;
    836 			}
    837 			xfer = (left > len) ? len : left;
    838 #ifdef notdef
    839 			/* Not Yet.. */
    840 			if (uiop->uio_iov->iov_op != NULL)
    841 				(*(uiop->uio_iov->iov_op))
    842 				(mbufcp, uiocp, xfer);
    843 			else
    844 #endif
    845 			if (uiop->uio_segflg == UIO_SYSSPACE)
    846 				memcpy(uiocp, mbufcp, xfer);
    847 			else
    848 				copyout(mbufcp, uiocp, xfer);
    849 			left -= xfer;
    850 			len -= xfer;
    851 			mbufcp += xfer;
    852 			uiocp += xfer;
    853 			uiop->uio_offset += xfer;
    854 			uiop->uio_resid -= xfer;
    855 		}
    856 		if (uiop->uio_iov->iov_len <= siz) {
    857 			uiop->uio_iovcnt--;
    858 			uiop->uio_iov++;
    859 		} else {
    860 			uiop->uio_iov->iov_base =
    861 			    (caddr_t)uiop->uio_iov->iov_base + uiosiz;
    862 			uiop->uio_iov->iov_len -= uiosiz;
    863 		}
    864 		siz -= uiosiz;
    865 	}
    866 	*dpos = mbufcp;
    867 	*mrep = mp;
    868 	if (rem > 0) {
    869 		if (len < rem)
    870 			error = nfs_adv(mrep, dpos, rem, len);
    871 		else
    872 			*dpos += rem;
    873 	}
    874 	return (error);
    875 }
    876 
    877 /*
    878  * copies a uio scatter/gather list to an mbuf chain.
    879  * NOTE: can ony handle iovcnt == 1
    880  */
    881 int
    882 nfsm_uiotombuf(uiop, mq, siz, bpos)
    883 	struct uio *uiop;
    884 	struct mbuf **mq;
    885 	int siz;
    886 	caddr_t *bpos;
    887 {
    888 	char *uiocp;
    889 	struct mbuf *mp, *mp2;
    890 	int xfer, left, mlen;
    891 	int uiosiz, clflg, rem;
    892 	char *cp;
    893 
    894 #ifdef DIAGNOSTIC
    895 	if (uiop->uio_iovcnt != 1)
    896 		panic("nfsm_uiotombuf: iovcnt != 1");
    897 #endif
    898 
    899 	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
    900 		clflg = 1;
    901 	else
    902 		clflg = 0;
    903 	rem = nfsm_rndup(siz)-siz;
    904 	mp = mp2 = *mq;
    905 	while (siz > 0) {
    906 		left = uiop->uio_iov->iov_len;
    907 		uiocp = uiop->uio_iov->iov_base;
    908 		if (left > siz)
    909 			left = siz;
    910 		uiosiz = left;
    911 		while (left > 0) {
    912 			mlen = M_TRAILINGSPACE(mp);
    913 			if (mlen == 0) {
    914 				MGET(mp, M_WAIT, MT_DATA);
    915 				if (clflg)
    916 					MCLGET(mp, M_WAIT);
    917 				mp->m_len = 0;
    918 				mp2->m_next = mp;
    919 				mp2 = mp;
    920 				mlen = M_TRAILINGSPACE(mp);
    921 			}
    922 			xfer = (left > mlen) ? mlen : left;
    923 #ifdef notdef
    924 			/* Not Yet.. */
    925 			if (uiop->uio_iov->iov_op != NULL)
    926 				(*(uiop->uio_iov->iov_op))
    927 				(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
    928 			else
    929 #endif
    930 			if (uiop->uio_segflg == UIO_SYSSPACE)
    931 				memcpy(mtod(mp, caddr_t)+mp->m_len, uiocp, xfer);
    932 			else
    933 				copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
    934 			mp->m_len += xfer;
    935 			left -= xfer;
    936 			uiocp += xfer;
    937 			uiop->uio_offset += xfer;
    938 			uiop->uio_resid -= xfer;
    939 		}
    940 		uiop->uio_iov->iov_base = (caddr_t)uiop->uio_iov->iov_base +
    941 		    uiosiz;
    942 		uiop->uio_iov->iov_len -= uiosiz;
    943 		siz -= uiosiz;
    944 	}
    945 	if (rem > 0) {
    946 		if (rem > M_TRAILINGSPACE(mp)) {
    947 			MGET(mp, M_WAIT, MT_DATA);
    948 			mp->m_len = 0;
    949 			mp2->m_next = mp;
    950 		}
    951 		cp = mtod(mp, caddr_t)+mp->m_len;
    952 		for (left = 0; left < rem; left++)
    953 			*cp++ = '\0';
    954 		mp->m_len += rem;
    955 		*bpos = cp;
    956 	} else
    957 		*bpos = mtod(mp, caddr_t)+mp->m_len;
    958 	*mq = mp;
    959 	return (0);
    960 }
    961 
    962 /*
    963  * Get at least "siz" bytes of correctly aligned data.
    964  * When called the mbuf pointers are not necessarily correct,
    965  * dsosp points to what ought to be in m_data and left contains
    966  * what ought to be in m_len.
    967  * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
    968  * cases. (The macros use the vars. dpos and dpos2)
    969  */
    970 int
    971 nfsm_disct(mdp, dposp, siz, left, cp2)
    972 	struct mbuf **mdp;
    973 	caddr_t *dposp;
    974 	int siz;
    975 	int left;
    976 	caddr_t *cp2;
    977 {
    978 	struct mbuf *m1, *m2;
    979 	struct mbuf *havebuf = NULL;
    980 	caddr_t src = *dposp;
    981 	caddr_t dst;
    982 	int len;
    983 
    984 #ifdef DEBUG
    985 	if (left < 0)
    986 		panic("nfsm_disct: left < 0");
    987 #endif
    988 	m1 = *mdp;
    989 	/*
    990 	 * Skip through the mbuf chain looking for an mbuf with
    991 	 * some data. If the first mbuf found has enough data
    992 	 * and it is correctly aligned return it.
    993 	 */
    994 	while (left == 0) {
    995 		havebuf = m1;
    996 		*mdp = m1 = m1->m_next;
    997 		if (m1 == NULL)
    998 			return (EBADRPC);
    999 		src = mtod(m1, caddr_t);
   1000 		left = m1->m_len;
   1001 		/*
   1002 		 * If we start a new mbuf and it is big enough
   1003 		 * and correctly aligned just return it, don't
   1004 		 * do any pull up.
   1005 		 */
   1006 		if (left >= siz && nfsm_aligned(src)) {
   1007 			*cp2 = src;
   1008 			*dposp = src + siz;
   1009 			return (0);
   1010 		}
   1011 	}
   1012 	if (m1->m_flags & M_EXT) {
   1013 		if (havebuf) {
   1014 			/* If the first mbuf with data has external data
   1015 			 * and there is a previous empty mbuf use it
   1016 			 * to move the data into.
   1017 			 */
   1018 			m2 = m1;
   1019 			*mdp = m1 = havebuf;
   1020 			if (m1->m_flags & M_EXT) {
   1021 				MEXTREMOVE(m1);
   1022 			}
   1023 		} else {
   1024 			/*
   1025 			 * If the first mbuf has a external data
   1026 			 * and there is no previous empty mbuf
   1027 			 * allocate a new mbuf and move the external
   1028 			 * data to the new mbuf. Also make the first
   1029 			 * mbuf look empty.
   1030 			 */
   1031 			m2 = m_get(M_WAIT, MT_DATA);
   1032 			m2->m_ext = m1->m_ext;
   1033 			m2->m_data = src;
   1034 			m2->m_len = left;
   1035 			MCLADDREFERENCE(m1, m2);
   1036 			MEXTREMOVE(m1);
   1037 			m2->m_next = m1->m_next;
   1038 			m1->m_next = m2;
   1039 		}
   1040 		m1->m_len = 0;
   1041 		dst = m1->m_dat;
   1042 	} else {
   1043 		/*
   1044 		 * If the first mbuf has no external data
   1045 		 * move the data to the front of the mbuf.
   1046 		 */
   1047 		if ((dst = m1->m_dat) != src)
   1048 			memmove(dst, src, left);
   1049 		dst += left;
   1050 		m1->m_len = left;
   1051 		m2 = m1->m_next;
   1052 	}
   1053 	m1->m_flags &= ~M_PKTHDR;
   1054 	*cp2 = m1->m_data = m1->m_dat;   /* data is at beginning of buffer */
   1055 	*dposp = mtod(m1, caddr_t) + siz;
   1056 	/*
   1057 	 * Loop through mbufs pulling data up into first mbuf until
   1058 	 * the first mbuf is full or there is no more data to
   1059 	 * pullup.
   1060 	 */
   1061 	while ((len = (MLEN - m1->m_len)) != 0 && m2) {
   1062 		if ((len = min(len, m2->m_len)) != 0)
   1063 			memcpy(dst, m2->m_data, len);
   1064 		m1->m_len += len;
   1065 		dst += len;
   1066 		m2->m_data += len;
   1067 		m2->m_len -= len;
   1068 		m2 = m2->m_next;
   1069 	}
   1070 	if (m1->m_len < siz)
   1071 		return (EBADRPC);
   1072 	return (0);
   1073 }
   1074 
   1075 /*
   1076  * Advance the position in the mbuf chain.
   1077  */
   1078 int
   1079 nfs_adv(mdp, dposp, offs, left)
   1080 	struct mbuf **mdp;
   1081 	caddr_t *dposp;
   1082 	int offs;
   1083 	int left;
   1084 {
   1085 	struct mbuf *m;
   1086 	int s;
   1087 
   1088 	m = *mdp;
   1089 	s = left;
   1090 	while (s < offs) {
   1091 		offs -= s;
   1092 		m = m->m_next;
   1093 		if (m == NULL)
   1094 			return (EBADRPC);
   1095 		s = m->m_len;
   1096 	}
   1097 	*mdp = m;
   1098 	*dposp = mtod(m, caddr_t)+offs;
   1099 	return (0);
   1100 }
   1101 
   1102 /*
   1103  * Copy a string into mbufs for the hard cases...
   1104  */
   1105 int
   1106 nfsm_strtmbuf(mb, bpos, cp, siz)
   1107 	struct mbuf **mb;
   1108 	char **bpos;
   1109 	const char *cp;
   1110 	long siz;
   1111 {
   1112 	struct mbuf *m1 = NULL, *m2;
   1113 	long left, xfer, len, tlen;
   1114 	u_int32_t *tl;
   1115 	int putsize;
   1116 
   1117 	putsize = 1;
   1118 	m2 = *mb;
   1119 	left = M_TRAILINGSPACE(m2);
   1120 	if (left > 0) {
   1121 		tl = ((u_int32_t *)(*bpos));
   1122 		*tl++ = txdr_unsigned(siz);
   1123 		putsize = 0;
   1124 		left -= NFSX_UNSIGNED;
   1125 		m2->m_len += NFSX_UNSIGNED;
   1126 		if (left > 0) {
   1127 			memcpy((caddr_t) tl, cp, left);
   1128 			siz -= left;
   1129 			cp += left;
   1130 			m2->m_len += left;
   1131 			left = 0;
   1132 		}
   1133 	}
   1134 	/* Loop around adding mbufs */
   1135 	while (siz > 0) {
   1136 		MGET(m1, M_WAIT, MT_DATA);
   1137 		if (siz > MLEN)
   1138 			MCLGET(m1, M_WAIT);
   1139 		m1->m_len = NFSMSIZ(m1);
   1140 		m2->m_next = m1;
   1141 		m2 = m1;
   1142 		tl = mtod(m1, u_int32_t *);
   1143 		tlen = 0;
   1144 		if (putsize) {
   1145 			*tl++ = txdr_unsigned(siz);
   1146 			m1->m_len -= NFSX_UNSIGNED;
   1147 			tlen = NFSX_UNSIGNED;
   1148 			putsize = 0;
   1149 		}
   1150 		if (siz < m1->m_len) {
   1151 			len = nfsm_rndup(siz);
   1152 			xfer = siz;
   1153 			if (xfer < len)
   1154 				*(tl+(xfer>>2)) = 0;
   1155 		} else {
   1156 			xfer = len = m1->m_len;
   1157 		}
   1158 		memcpy((caddr_t) tl, cp, xfer);
   1159 		m1->m_len = len+tlen;
   1160 		siz -= xfer;
   1161 		cp += xfer;
   1162 	}
   1163 	*mb = m1;
   1164 	*bpos = mtod(m1, caddr_t)+m1->m_len;
   1165 	return (0);
   1166 }
   1167 
   1168 /*
   1169  * Directory caching routines. They work as follows:
   1170  * - a cache is maintained per VDIR nfsnode.
   1171  * - for each offset cookie that is exported to userspace, and can
   1172  *   thus be thrown back at us as an offset to VOP_READDIR, store
   1173  *   information in the cache.
   1174  * - cached are:
   1175  *   - cookie itself
   1176  *   - blocknumber (essentially just a search key in the buffer cache)
   1177  *   - entry number in block.
   1178  *   - offset cookie of block in which this entry is stored
   1179  *   - 32 bit cookie if NFSMNT_XLATECOOKIE is used.
   1180  * - entries are looked up in a hash table
   1181  * - also maintained is an LRU list of entries, used to determine
   1182  *   which ones to delete if the cache grows too large.
   1183  * - if 32 <-> 64 translation mode is requested for a filesystem,
   1184  *   the cache also functions as a translation table
   1185  * - in the translation case, invalidating the cache does not mean
   1186  *   flushing it, but just marking entries as invalid, except for
   1187  *   the <64bit cookie, 32bitcookie> pair which is still valid, to
   1188  *   still be able to use the cache as a translation table.
   1189  * - 32 bit cookies are uniquely created by combining the hash table
   1190  *   entry value, and one generation count per hash table entry,
   1191  *   incremented each time an entry is appended to the chain.
   1192  * - the cache is invalidated each time a direcory is modified
   1193  * - sanity checks are also done; if an entry in a block turns
   1194  *   out not to have a matching cookie, the cache is invalidated
   1195  *   and a new block starting from the wanted offset is fetched from
   1196  *   the server.
   1197  * - directory entries as read from the server are extended to contain
   1198  *   the 64bit and, optionally, the 32bit cookies, for sanity checking
   1199  *   the cache and exporting them to userspace through the cookie
   1200  *   argument to VOP_READDIR.
   1201  */
   1202 
   1203 u_long
   1204 nfs_dirhash(off)
   1205 	off_t off;
   1206 {
   1207 	int i;
   1208 	char *cp = (char *)&off;
   1209 	u_long sum = 0L;
   1210 
   1211 	for (i = 0 ; i < sizeof (off); i++)
   1212 		sum += *cp++;
   1213 
   1214 	return sum;
   1215 }
   1216 
   1217 void
   1218 nfs_initdircache(vp)
   1219 	struct vnode *vp;
   1220 {
   1221 	struct nfsnode *np = VTONFS(vp);
   1222 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1223 
   1224 	np->n_dircachesize = 0;
   1225 	np->n_dblkno = 1;
   1226 	np->n_dircache = hashinit(NFS_DIRHASHSIZ, HASH_LIST, M_NFSDIROFF,
   1227 	    M_WAITOK, &nfsdirhashmask);
   1228 	TAILQ_INIT(&np->n_dirchain);
   1229 	if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
   1230 		MALLOC(np->n_dirgens, unsigned *,
   1231 		    NFS_DIRHASHSIZ * sizeof (unsigned), M_NFSDIROFF,
   1232 		    M_WAITOK);
   1233 		memset((caddr_t)np->n_dirgens, 0,
   1234 		    NFS_DIRHASHSIZ * sizeof (unsigned));
   1235 	}
   1236 }
   1237 
   1238 static struct nfsdircache dzero = {0, 0, {0, 0}, {0, 0}, 0, 0, 0};
   1239 
   1240 struct nfsdircache *
   1241 nfs_searchdircache(vp, off, do32, hashent)
   1242 	struct vnode *vp;
   1243 	off_t off;
   1244 	int do32;
   1245 	int *hashent;
   1246 {
   1247 	struct nfsdirhashhead *ndhp;
   1248 	struct nfsdircache *ndp = NULL;
   1249 	struct nfsnode *np = VTONFS(vp);
   1250 	unsigned ent;
   1251 
   1252 	/*
   1253 	 * Zero is always a valid cookie.
   1254 	 */
   1255 	if (off == 0)
   1256 		return &dzero;
   1257 
   1258 	/*
   1259 	 * We use a 32bit cookie as search key, directly reconstruct
   1260 	 * the hashentry. Else use the hashfunction.
   1261 	 */
   1262 	if (do32) {
   1263 		ent = (u_int32_t)off >> 24;
   1264 		if (ent >= NFS_DIRHASHSIZ)
   1265 			return NULL;
   1266 		ndhp = &np->n_dircache[ent];
   1267 	} else {
   1268 		ndhp = NFSDIRHASH(np, off);
   1269 	}
   1270 
   1271 	if (hashent)
   1272 		*hashent = (int)(ndhp - np->n_dircache);
   1273 	if (do32) {
   1274 		for (ndp = ndhp->lh_first; ndp; ndp = ndp->dc_hash.le_next) {
   1275 			if (ndp->dc_cookie32 == (u_int32_t)off) {
   1276 				/*
   1277 				 * An invalidated entry will become the
   1278 				 * start of a new block fetched from
   1279 				 * the server.
   1280 				 */
   1281 				if (ndp->dc_blkno == -1) {
   1282 					ndp->dc_blkcookie = ndp->dc_cookie;
   1283 					ndp->dc_blkno = np->n_dblkno++;
   1284 					ndp->dc_entry = 0;
   1285 				}
   1286 				break;
   1287 			}
   1288 		}
   1289 	} else {
   1290 		for (ndp = ndhp->lh_first; ndp; ndp = ndp->dc_hash.le_next)
   1291 			if (ndp->dc_cookie == off)
   1292 				break;
   1293 	}
   1294 	return ndp;
   1295 }
   1296 
   1297 
   1298 struct nfsdircache *
   1299 nfs_enterdircache(vp, off, blkoff, en, blkno)
   1300 	struct vnode *vp;
   1301 	off_t off, blkoff;
   1302 	daddr_t blkno;
   1303 	int en;
   1304 {
   1305 	struct nfsnode *np = VTONFS(vp);
   1306 	struct nfsdirhashhead *ndhp;
   1307 	struct nfsdircache *ndp = NULL, *first;
   1308 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1309 	int hashent, gen, overwrite;
   1310 
   1311 	if (!np->n_dircache)
   1312 		/*
   1313 		 * XXX would like to do this in nfs_nget but vtype
   1314 		 * isn't known at that time.
   1315 		 */
   1316 		nfs_initdircache(vp);
   1317 
   1318 	/*
   1319 	 * XXX refuse entries for offset 0. amd(8) erroneously sets
   1320 	 * cookie 0 for the '.' entry, making this necessary. This
   1321 	 * isn't so bad, as 0 is a special case anyway.
   1322 	 */
   1323 	if (off == 0)
   1324 		return &dzero;
   1325 
   1326 	ndp = nfs_searchdircache(vp, off, 0, &hashent);
   1327 
   1328 	if (ndp && ndp->dc_blkno != -1) {
   1329 		/*
   1330 		 * Overwriting an old entry. Check if it's the same.
   1331 		 * If so, just return. If not, remove the old entry.
   1332 		 */
   1333 		if (ndp->dc_blkcookie == blkoff && ndp->dc_entry == en)
   1334 			return ndp;
   1335 		TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain);
   1336 		LIST_REMOVE(ndp, dc_hash);
   1337 		FREE(ndp, M_NFSDIROFF);
   1338 		ndp = 0;
   1339 	}
   1340 
   1341 	ndhp = &np->n_dircache[hashent];
   1342 
   1343 	if (!ndp) {
   1344 		MALLOC(ndp, struct nfsdircache *, sizeof (*ndp), M_NFSDIROFF,
   1345 		    M_WAITOK);
   1346 		overwrite = 0;
   1347 		if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
   1348 			/*
   1349 			 * We're allocating a new entry, so bump the
   1350 			 * generation number.
   1351 			 */
   1352 			gen = ++np->n_dirgens[hashent];
   1353 			if (gen == 0) {
   1354 				np->n_dirgens[hashent]++;
   1355 				gen++;
   1356 			}
   1357 			ndp->dc_cookie32 = (hashent << 24) | (gen & 0xffffff);
   1358 		}
   1359 	} else
   1360 		overwrite = 1;
   1361 
   1362 	/*
   1363 	 * If the entry number is 0, we are at the start of a new block, so
   1364 	 * allocate a new blocknumber.
   1365 	 */
   1366 	if (en == 0)
   1367 		ndp->dc_blkno = np->n_dblkno++;
   1368 	else
   1369 		ndp->dc_blkno = blkno;
   1370 
   1371 	ndp->dc_cookie = off;
   1372 	ndp->dc_blkcookie = blkoff;
   1373 	ndp->dc_entry = en;
   1374 
   1375 	if (overwrite)
   1376 		return ndp;
   1377 
   1378 	/*
   1379 	 * If the maximum directory cookie cache size has been reached
   1380 	 * for this node, take one off the front. The idea is that
   1381 	 * directories are typically read front-to-back once, so that
   1382 	 * the oldest entries can be thrown away without much performance
   1383 	 * loss.
   1384 	 */
   1385 	if (np->n_dircachesize == NFS_MAXDIRCACHE) {
   1386 		first = np->n_dirchain.tqh_first;
   1387 		TAILQ_REMOVE(&np->n_dirchain, first, dc_chain);
   1388 		LIST_REMOVE(first, dc_hash);
   1389 		FREE(first, M_NFSDIROFF);
   1390 	} else
   1391 		np->n_dircachesize++;
   1392 
   1393 	LIST_INSERT_HEAD(ndhp, ndp, dc_hash);
   1394 	TAILQ_INSERT_TAIL(&np->n_dirchain, ndp, dc_chain);
   1395 	return ndp;
   1396 }
   1397 
   1398 void
   1399 nfs_invaldircache(vp, forcefree)
   1400 	struct vnode *vp;
   1401 	int forcefree;
   1402 {
   1403 	struct nfsnode *np = VTONFS(vp);
   1404 	struct nfsdircache *ndp = NULL;
   1405 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1406 
   1407 #ifdef DIAGNOSTIC
   1408 	if (vp->v_type != VDIR)
   1409 		panic("nfs: invaldircache: not dir");
   1410 #endif
   1411 
   1412 	if (!np->n_dircache)
   1413 		return;
   1414 
   1415 	if (!(nmp->nm_flag & NFSMNT_XLATECOOKIE) || forcefree) {
   1416 		while ((ndp = np->n_dirchain.tqh_first)) {
   1417 			TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain);
   1418 			LIST_REMOVE(ndp, dc_hash);
   1419 			FREE(ndp, M_NFSDIROFF);
   1420 		}
   1421 		np->n_dircachesize = 0;
   1422 		if (forcefree && np->n_dirgens) {
   1423 			FREE(np->n_dirgens, M_NFSDIROFF);
   1424 		}
   1425 	} else {
   1426 		for (ndp = np->n_dirchain.tqh_first; ndp;
   1427 		    ndp = ndp->dc_chain.tqe_next)
   1428 			ndp->dc_blkno = -1;
   1429 	}
   1430 
   1431 	np->n_dblkno = 1;
   1432 }
   1433 
   1434 /*
   1435  * Called once before VFS init to initialize shared and
   1436  * server-specific data structures.
   1437  */
   1438 void
   1439 nfs_init()
   1440 {
   1441 	nfsrtt.pos = 0;
   1442 	rpc_vers = txdr_unsigned(RPC_VER2);
   1443 	rpc_call = txdr_unsigned(RPC_CALL);
   1444 	rpc_reply = txdr_unsigned(RPC_REPLY);
   1445 	rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
   1446 	rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
   1447 	rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
   1448 	rpc_autherr = txdr_unsigned(RPC_AUTHERR);
   1449 	rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
   1450 	rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
   1451 	nfs_prog = txdr_unsigned(NFS_PROG);
   1452 	nqnfs_prog = txdr_unsigned(NQNFS_PROG);
   1453 	nfs_true = txdr_unsigned(TRUE);
   1454 	nfs_false = txdr_unsigned(FALSE);
   1455 	nfs_xdrneg1 = txdr_unsigned(-1);
   1456 	nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
   1457 	if (nfs_ticks < 1)
   1458 		nfs_ticks = 1;
   1459 #ifdef NFSSERVER
   1460 	nfsrv_init(0);			/* Init server data structures */
   1461 	nfsrv_initcache();		/* Init the server request cache */
   1462 #endif /* NFSSERVER */
   1463 
   1464 #if defined(NFSSERVER) || !defined(NFS_V2_ONLY)
   1465 	/*
   1466 	 * Initialize the nqnfs data structures.
   1467 	 */
   1468 	if (nqnfsstarttime == 0) {
   1469 		nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
   1470 			+ nqsrv_clockskew + nqsrv_writeslack;
   1471 		NQLOADNOVRAM(nqnfsstarttime);
   1472 		CIRCLEQ_INIT(&nqtimerhead);
   1473 		nqfhhashtbl = hashinit(NQLCHSZ, HASH_LIST, M_NQLEASE,
   1474 		    M_WAITOK, &nqfhhash);
   1475 	}
   1476 #endif
   1477 
   1478 	/*
   1479 	 * Initialize reply list and start timer
   1480 	 */
   1481 	TAILQ_INIT(&nfs_reqq);
   1482 	nfs_timer(NULL);
   1483 }
   1484 
   1485 #ifdef NFS
   1486 /*
   1487  * Called once at VFS init to initialize client-specific data structures.
   1488  */
   1489 void
   1490 nfs_vfs_init()
   1491 {
   1492 	nfs_nhinit();			/* Init the nfsnode table */
   1493 }
   1494 
   1495 void
   1496 nfs_vfs_reinit()
   1497 {
   1498 	nfs_nhreinit();
   1499 }
   1500 
   1501 void
   1502 nfs_vfs_done()
   1503 {
   1504 	nfs_nhdone();
   1505 }
   1506 
   1507 /*
   1508  * Attribute cache routines.
   1509  * nfs_loadattrcache() - loads or updates the cache contents from attributes
   1510  *	that are on the mbuf list
   1511  * nfs_getattrcache() - returns valid attributes if found in cache, returns
   1512  *	error otherwise
   1513  */
   1514 
   1515 /*
   1516  * Load the attribute cache (that lives in the nfsnode entry) with
   1517  * the values on the mbuf list and
   1518  * Iff vap not NULL
   1519  *    copy the attributes to *vaper
   1520  */
   1521 int
   1522 nfsm_loadattrcache(vpp, mdp, dposp, vaper)
   1523 	struct vnode **vpp;
   1524 	struct mbuf **mdp;
   1525 	caddr_t *dposp;
   1526 	struct vattr *vaper;
   1527 {
   1528 	int32_t t1;
   1529 	caddr_t cp2;
   1530 	int error = 0;
   1531 	struct mbuf *md;
   1532 	int v3 = NFS_ISV3(*vpp);
   1533 
   1534 	md = *mdp;
   1535 	t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
   1536 	error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2);
   1537 	if (error)
   1538 		return (error);
   1539 	return nfs_loadattrcache(vpp, (struct nfs_fattr *)cp2, vaper);
   1540 }
   1541 
   1542 int
   1543 nfs_loadattrcache(vpp, fp, vaper)
   1544 	struct vnode **vpp;
   1545 	struct nfs_fattr *fp;
   1546 	struct vattr *vaper;
   1547 {
   1548 	struct vnode *vp = *vpp;
   1549 	struct vattr *vap;
   1550 	int v3 = NFS_ISV3(vp);
   1551 	enum vtype vtyp;
   1552 	u_short vmode;
   1553 	struct timespec mtime;
   1554 	struct vnode *nvp;
   1555 	int32_t rdev;
   1556 	struct nfsnode *np;
   1557 	extern int (**spec_nfsv2nodeop_p) __P((void *));
   1558 
   1559 	if (v3) {
   1560 		vtyp = nfsv3tov_type(fp->fa_type);
   1561 		vmode = fxdr_unsigned(u_short, fp->fa_mode);
   1562 		rdev = makedev(fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata1),
   1563 			fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata2));
   1564 		fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
   1565 	} else {
   1566 		vtyp = nfsv2tov_type(fp->fa_type);
   1567 		vmode = fxdr_unsigned(u_short, fp->fa_mode);
   1568 		if (vtyp == VNON || vtyp == VREG)
   1569 			vtyp = IFTOVT(vmode);
   1570 		rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
   1571 		fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
   1572 
   1573 		/*
   1574 		 * Really ugly NFSv2 kludge.
   1575 		 */
   1576 		if (vtyp == VCHR && rdev == 0xffffffff)
   1577 			vtyp = VFIFO;
   1578 	}
   1579 
   1580 	/*
   1581 	 * If v_type == VNON it is a new node, so fill in the v_type,
   1582 	 * n_mtime fields. Check to see if it represents a special
   1583 	 * device, and if so, check for a possible alias. Once the
   1584 	 * correct vnode has been obtained, fill in the rest of the
   1585 	 * information.
   1586 	 */
   1587 	np = VTONFS(vp);
   1588 	if (vp->v_type == VNON) {
   1589 		vp->v_type = vtyp;
   1590 		if (vp->v_type == VFIFO) {
   1591 			extern int (**fifo_nfsv2nodeop_p) __P((void *));
   1592 			vp->v_op = fifo_nfsv2nodeop_p;
   1593 		}
   1594 		if (vp->v_type == VCHR || vp->v_type == VBLK) {
   1595 			vp->v_op = spec_nfsv2nodeop_p;
   1596 			nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
   1597 			if (nvp) {
   1598 				/*
   1599 				 * Discard unneeded vnode, but save its nfsnode.
   1600 				 * Since the nfsnode does not have a lock, its
   1601 				 * vnode lock has to be carried over.
   1602 				 */
   1603 				/*
   1604 				 * XXX is the old node sure to be locked here?
   1605 				 */
   1606 				KASSERT(lockstatus(&vp->v_lock) ==
   1607 				    LK_EXCLUSIVE);
   1608 				nvp->v_data = vp->v_data;
   1609 				vp->v_data = NULL;
   1610 				VOP_UNLOCK(vp, 0);
   1611 				vp->v_op = spec_vnodeop_p;
   1612 				vrele(vp);
   1613 				vgone(vp);
   1614 				lockmgr(&nvp->v_lock, LK_EXCLUSIVE,
   1615 				    &nvp->v_interlock);
   1616 				/*
   1617 				 * Reinitialize aliased node.
   1618 				 */
   1619 				np->n_vnode = nvp;
   1620 				*vpp = vp = nvp;
   1621 			}
   1622 		}
   1623 		np->n_mtime = mtime.tv_sec;
   1624 	}
   1625 	vap = np->n_vattr;
   1626 	vap->va_type = vtyp;
   1627 	vap->va_mode = vmode & ALLPERMS;
   1628 	vap->va_rdev = (dev_t)rdev;
   1629 	vap->va_mtime = mtime;
   1630 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
   1631 	switch (vtyp) {
   1632 	case VDIR:
   1633 		vap->va_blocksize = NFS_DIRFRAGSIZ;
   1634 		break;
   1635 	case VBLK:
   1636 		vap->va_blocksize = BLKDEV_IOSIZE;
   1637 		break;
   1638 	case VCHR:
   1639 		vap->va_blocksize = MAXBSIZE;
   1640 		break;
   1641 	default:
   1642 		vap->va_blocksize = v3 ? vp->v_mount->mnt_stat.f_iosize :
   1643 		    fxdr_unsigned(int32_t, fp->fa2_blocksize);
   1644 		break;
   1645 	}
   1646 	if (v3) {
   1647 		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
   1648 		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
   1649 		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
   1650 		vap->va_size = fxdr_hyper(&fp->fa3_size);
   1651 		vap->va_bytes = fxdr_hyper(&fp->fa3_used);
   1652 		vap->va_fileid = fxdr_unsigned(int32_t,
   1653 		    fp->fa3_fileid.nfsuquad[1]);
   1654 		fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
   1655 		fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
   1656 		vap->va_flags = 0;
   1657 		vap->va_filerev = 0;
   1658 	} else {
   1659 		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
   1660 		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
   1661 		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
   1662 		vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
   1663 		vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks)
   1664 		    * NFS_FABLKSIZE;
   1665 		vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
   1666 		fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
   1667 		vap->va_flags = 0;
   1668 		vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
   1669 		    fp->fa2_ctime.nfsv2_sec);
   1670 		vap->va_ctime.tv_nsec = 0;
   1671 		vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
   1672 		vap->va_filerev = 0;
   1673 	}
   1674 	if (vap->va_size != np->n_size) {
   1675 		if ((np->n_flag & NMODIFIED) && vap->va_size < np->n_size) {
   1676 			vap->va_size = np->n_size;
   1677 		} else {
   1678 			np->n_size = vap->va_size;
   1679 			if (vap->va_type == VREG) {
   1680 				uvm_vnp_setsize(vp, np->n_size);
   1681 			}
   1682 		}
   1683 	}
   1684 	np->n_attrstamp = time.tv_sec;
   1685 	if (vaper != NULL) {
   1686 		memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(*vap));
   1687 		if (np->n_flag & NCHG) {
   1688 			if (np->n_flag & NACC)
   1689 				vaper->va_atime = np->n_atim;
   1690 			if (np->n_flag & NUPD)
   1691 				vaper->va_mtime = np->n_mtim;
   1692 		}
   1693 	}
   1694 	return (0);
   1695 }
   1696 
   1697 /*
   1698  * Check the time stamp
   1699  * If the cache is valid, copy contents to *vap and return 0
   1700  * otherwise return an error
   1701  */
   1702 int
   1703 nfs_getattrcache(vp, vaper)
   1704 	struct vnode *vp;
   1705 	struct vattr *vaper;
   1706 {
   1707 	struct nfsnode *np = VTONFS(vp);
   1708 	struct vattr *vap;
   1709 
   1710 	if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(np)) {
   1711 		nfsstats.attrcache_misses++;
   1712 		return (ENOENT);
   1713 	}
   1714 	nfsstats.attrcache_hits++;
   1715 	vap = np->n_vattr;
   1716 	if (vap->va_size != np->n_size) {
   1717 		if (vap->va_type == VREG) {
   1718 			if (np->n_flag & NMODIFIED) {
   1719 				if (vap->va_size < np->n_size)
   1720 					vap->va_size = np->n_size;
   1721 				else
   1722 					np->n_size = vap->va_size;
   1723 			} else
   1724 				np->n_size = vap->va_size;
   1725 			uvm_vnp_setsize(vp, np->n_size);
   1726 		} else
   1727 			np->n_size = vap->va_size;
   1728 	}
   1729 	memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(struct vattr));
   1730 	if (np->n_flag & NCHG) {
   1731 		if (np->n_flag & NACC)
   1732 			vaper->va_atime = np->n_atim;
   1733 		if (np->n_flag & NUPD)
   1734 			vaper->va_mtime = np->n_mtim;
   1735 	}
   1736 	return (0);
   1737 }
   1738 
   1739 /*
   1740  * Heuristic to see if the server XDR encodes directory cookies or not.
   1741  * it is not supposed to, but a lot of servers may do this. Also, since
   1742  * most/all servers will implement V2 as well, it is expected that they
   1743  * may return just 32 bits worth of cookie information, so we need to
   1744  * find out in which 32 bits this information is available. We do this
   1745  * to avoid trouble with emulated binaries that can't handle 64 bit
   1746  * directory offsets.
   1747  */
   1748 
   1749 void
   1750 nfs_cookieheuristic(vp, flagp, p, cred)
   1751 	struct vnode *vp;
   1752 	int *flagp;
   1753 	struct proc *p;
   1754 	struct ucred *cred;
   1755 {
   1756 	struct uio auio;
   1757 	struct iovec aiov;
   1758 	caddr_t buf, cp;
   1759 	struct dirent *dp;
   1760 	off_t *cookies = NULL, *cop;
   1761 	int error, eof, nc, len;
   1762 
   1763 	MALLOC(buf, caddr_t, NFS_DIRFRAGSIZ, M_TEMP, M_WAITOK);
   1764 
   1765 	aiov.iov_base = buf;
   1766 	aiov.iov_len = NFS_DIRFRAGSIZ;
   1767 	auio.uio_iov = &aiov;
   1768 	auio.uio_iovcnt = 1;
   1769 	auio.uio_rw = UIO_READ;
   1770 	auio.uio_segflg = UIO_SYSSPACE;
   1771 	auio.uio_procp = p;
   1772 	auio.uio_resid = NFS_DIRFRAGSIZ;
   1773 	auio.uio_offset = 0;
   1774 
   1775 	error = VOP_READDIR(vp, &auio, cred, &eof, &cookies, &nc);
   1776 
   1777 	len = NFS_DIRFRAGSIZ - auio.uio_resid;
   1778 	if (error || len == 0) {
   1779 		FREE(buf, M_TEMP);
   1780 		if (cookies)
   1781 			free(cookies, M_TEMP);
   1782 		return;
   1783 	}
   1784 
   1785 	/*
   1786 	 * Find the first valid entry and look at its offset cookie.
   1787 	 */
   1788 
   1789 	cp = buf;
   1790 	for (cop = cookies; len > 0; len -= dp->d_reclen) {
   1791 		dp = (struct dirent *)cp;
   1792 		if (dp->d_fileno != 0 && len >= dp->d_reclen) {
   1793 			if ((*cop >> 32) != 0 && (*cop & 0xffffffffLL) == 0) {
   1794 				*flagp |= NFSMNT_SWAPCOOKIE;
   1795 				nfs_invaldircache(vp, 0);
   1796 				nfs_vinvalbuf(vp, 0, cred, p, 1);
   1797 			}
   1798 			break;
   1799 		}
   1800 		cop++;
   1801 		cp += dp->d_reclen;
   1802 	}
   1803 
   1804 	FREE(buf, M_TEMP);
   1805 	free(cookies, M_TEMP);
   1806 }
   1807 #endif /* NFS */
   1808 
   1809 /*
   1810  * Set up nameidata for a lookup() call and do it.
   1811  *
   1812  * If pubflag is set, this call is done for a lookup operation on the
   1813  * public filehandle. In that case we allow crossing mountpoints and
   1814  * absolute pathnames. However, the caller is expected to check that
   1815  * the lookup result is within the public fs, and deny access if
   1816  * it is not.
   1817  */
   1818 int
   1819 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
   1820 	struct nameidata *ndp;
   1821 	fhandle_t *fhp;
   1822 	int len;
   1823 	struct nfssvc_sock *slp;
   1824 	struct mbuf *nam;
   1825 	struct mbuf **mdp;
   1826 	caddr_t *dposp;
   1827 	struct vnode **retdirp;
   1828 	struct proc *p;
   1829 	int kerbflag, pubflag;
   1830 {
   1831 	int i, rem;
   1832 	struct mbuf *md;
   1833 	char *fromcp, *tocp, *cp;
   1834 	struct iovec aiov;
   1835 	struct uio auio;
   1836 	struct vnode *dp;
   1837 	int error, rdonly, linklen;
   1838 	struct componentname *cnp = &ndp->ni_cnd;
   1839 
   1840 	*retdirp = (struct vnode *)0;
   1841 
   1842 	if ((len + 1) > MAXPATHLEN)
   1843 		return (ENAMETOOLONG);
   1844 	cnp->cn_pnbuf = PNBUF_GET();
   1845 
   1846 	/*
   1847 	 * Copy the name from the mbuf list to ndp->ni_pnbuf
   1848 	 * and set the various ndp fields appropriately.
   1849 	 */
   1850 	fromcp = *dposp;
   1851 	tocp = cnp->cn_pnbuf;
   1852 	md = *mdp;
   1853 	rem = mtod(md, caddr_t) + md->m_len - fromcp;
   1854 	for (i = 0; i < len; i++) {
   1855 		while (rem == 0) {
   1856 			md = md->m_next;
   1857 			if (md == NULL) {
   1858 				error = EBADRPC;
   1859 				goto out;
   1860 			}
   1861 			fromcp = mtod(md, caddr_t);
   1862 			rem = md->m_len;
   1863 		}
   1864 		if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
   1865 			error = EACCES;
   1866 			goto out;
   1867 		}
   1868 		*tocp++ = *fromcp++;
   1869 		rem--;
   1870 	}
   1871 	*tocp = '\0';
   1872 	*mdp = md;
   1873 	*dposp = fromcp;
   1874 	len = nfsm_rndup(len)-len;
   1875 	if (len > 0) {
   1876 		if (rem >= len)
   1877 			*dposp += len;
   1878 		else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
   1879 			goto out;
   1880 	}
   1881 
   1882 	/*
   1883 	 * Extract and set starting directory.
   1884 	 */
   1885 	error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
   1886 	    nam, &rdonly, kerbflag, pubflag);
   1887 	if (error)
   1888 		goto out;
   1889 	if (dp->v_type != VDIR) {
   1890 		vrele(dp);
   1891 		error = ENOTDIR;
   1892 		goto out;
   1893 	}
   1894 
   1895 	if (rdonly)
   1896 		cnp->cn_flags |= RDONLY;
   1897 
   1898 	*retdirp = dp;
   1899 
   1900 	if (pubflag) {
   1901 		/*
   1902 		 * Oh joy. For WebNFS, handle those pesky '%' escapes,
   1903 		 * and the 'native path' indicator.
   1904 		 */
   1905 		cp = PNBUF_GET();
   1906 		fromcp = cnp->cn_pnbuf;
   1907 		tocp = cp;
   1908 		if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
   1909 			switch ((unsigned char)*fromcp) {
   1910 			case WEBNFS_NATIVE_CHAR:
   1911 				/*
   1912 				 * 'Native' path for us is the same
   1913 				 * as a path according to the NFS spec,
   1914 				 * just skip the escape char.
   1915 				 */
   1916 				fromcp++;
   1917 				break;
   1918 			/*
   1919 			 * More may be added in the future, range 0x80-0xff
   1920 			 */
   1921 			default:
   1922 				error = EIO;
   1923 				FREE(cp, M_NAMEI);
   1924 				goto out;
   1925 			}
   1926 		}
   1927 		/*
   1928 		 * Translate the '%' escapes, URL-style.
   1929 		 */
   1930 		while (*fromcp != '\0') {
   1931 			if (*fromcp == WEBNFS_ESC_CHAR) {
   1932 				if (fromcp[1] != '\0' && fromcp[2] != '\0') {
   1933 					fromcp++;
   1934 					*tocp++ = HEXSTRTOI(fromcp);
   1935 					fromcp += 2;
   1936 					continue;
   1937 				} else {
   1938 					error = ENOENT;
   1939 					FREE(cp, M_NAMEI);
   1940 					goto out;
   1941 				}
   1942 			} else
   1943 				*tocp++ = *fromcp++;
   1944 		}
   1945 		*tocp = '\0';
   1946 		PNBUF_PUT(cnp->cn_pnbuf);
   1947 		cnp->cn_pnbuf = cp;
   1948 	}
   1949 
   1950 	ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
   1951 	ndp->ni_segflg = UIO_SYSSPACE;
   1952 
   1953 	if (pubflag) {
   1954 		ndp->ni_rootdir = rootvnode;
   1955 		ndp->ni_loopcnt = 0;
   1956 		if (cnp->cn_pnbuf[0] == '/')
   1957 			dp = rootvnode;
   1958 	} else {
   1959 		cnp->cn_flags |= NOCROSSMOUNT;
   1960 	}
   1961 
   1962 	cnp->cn_proc = p;
   1963 	VREF(dp);
   1964 
   1965     for (;;) {
   1966 	cnp->cn_nameptr = cnp->cn_pnbuf;
   1967 	ndp->ni_startdir = dp;
   1968 	/*
   1969 	 * And call lookup() to do the real work
   1970 	 */
   1971 	error = lookup(ndp);
   1972 	if (error) {
   1973 		PNBUF_PUT(cnp->cn_pnbuf);
   1974 		return (error);
   1975 	}
   1976 	/*
   1977 	 * Check for encountering a symbolic link
   1978 	 */
   1979 	if ((cnp->cn_flags & ISSYMLINK) == 0) {
   1980 		if (cnp->cn_flags & (SAVENAME | SAVESTART))
   1981 			cnp->cn_flags |= HASBUF;
   1982 		else
   1983 			PNBUF_PUT(cnp->cn_pnbuf);
   1984 		return (0);
   1985 	} else {
   1986 		if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
   1987 			VOP_UNLOCK(ndp->ni_dvp, 0);
   1988 		if (!pubflag) {
   1989 			error = EINVAL;
   1990 			break;
   1991 		}
   1992 
   1993 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
   1994 			error = ELOOP;
   1995 			break;
   1996 		}
   1997 		if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
   1998 			error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred,
   1999 			    cnp->cn_proc);
   2000 			if (error != 0)
   2001 				break;
   2002 		}
   2003 		if (ndp->ni_pathlen > 1)
   2004 			cp = PNBUF_GET();
   2005 		else
   2006 			cp = cnp->cn_pnbuf;
   2007 		aiov.iov_base = cp;
   2008 		aiov.iov_len = MAXPATHLEN;
   2009 		auio.uio_iov = &aiov;
   2010 		auio.uio_iovcnt = 1;
   2011 		auio.uio_offset = 0;
   2012 		auio.uio_rw = UIO_READ;
   2013 		auio.uio_segflg = UIO_SYSSPACE;
   2014 		auio.uio_procp = (struct proc *)0;
   2015 		auio.uio_resid = MAXPATHLEN;
   2016 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
   2017 		if (error) {
   2018 		badlink:
   2019 			if (ndp->ni_pathlen > 1)
   2020 				PNBUF_PUT(cp);
   2021 			break;
   2022 		}
   2023 		linklen = MAXPATHLEN - auio.uio_resid;
   2024 		if (linklen == 0) {
   2025 			error = ENOENT;
   2026 			goto badlink;
   2027 		}
   2028 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
   2029 			error = ENAMETOOLONG;
   2030 			goto badlink;
   2031 		}
   2032 		if (ndp->ni_pathlen > 1) {
   2033 			memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
   2034 			PNBUF_PUT(cnp->cn_pnbuf);
   2035 			cnp->cn_pnbuf = cp;
   2036 		} else
   2037 			cnp->cn_pnbuf[linklen] = '\0';
   2038 		ndp->ni_pathlen += linklen;
   2039 		vput(ndp->ni_vp);
   2040 		dp = ndp->ni_dvp;
   2041 		/*
   2042 		 * Check if root directory should replace current directory.
   2043 		 */
   2044 		if (cnp->cn_pnbuf[0] == '/') {
   2045 			vrele(dp);
   2046 			dp = ndp->ni_rootdir;
   2047 			VREF(dp);
   2048 		}
   2049 	}
   2050    }
   2051 	vrele(ndp->ni_dvp);
   2052 	vput(ndp->ni_vp);
   2053 	ndp->ni_vp = NULL;
   2054 out:
   2055 	PNBUF_PUT(cnp->cn_pnbuf);
   2056 	return (error);
   2057 }
   2058 
   2059 /*
   2060  * A fiddled version of m_adj() that ensures null fill to a long
   2061  * boundary and only trims off the back end
   2062  */
   2063 void
   2064 nfsm_adj(mp, len, nul)
   2065 	struct mbuf *mp;
   2066 	int len;
   2067 	int nul;
   2068 {
   2069 	struct mbuf *m;
   2070 	int count, i;
   2071 	char *cp;
   2072 
   2073 	/*
   2074 	 * Trim from tail.  Scan the mbuf chain,
   2075 	 * calculating its length and finding the last mbuf.
   2076 	 * If the adjustment only affects this mbuf, then just
   2077 	 * adjust and return.  Otherwise, rescan and truncate
   2078 	 * after the remaining size.
   2079 	 */
   2080 	count = 0;
   2081 	m = mp;
   2082 	for (;;) {
   2083 		count += m->m_len;
   2084 		if (m->m_next == (struct mbuf *)0)
   2085 			break;
   2086 		m = m->m_next;
   2087 	}
   2088 	if (m->m_len > len) {
   2089 		m->m_len -= len;
   2090 		if (nul > 0) {
   2091 			cp = mtod(m, caddr_t)+m->m_len-nul;
   2092 			for (i = 0; i < nul; i++)
   2093 				*cp++ = '\0';
   2094 		}
   2095 		return;
   2096 	}
   2097 	count -= len;
   2098 	if (count < 0)
   2099 		count = 0;
   2100 	/*
   2101 	 * Correct length for chain is "count".
   2102 	 * Find the mbuf with last data, adjust its length,
   2103 	 * and toss data from remaining mbufs on chain.
   2104 	 */
   2105 	for (m = mp; m; m = m->m_next) {
   2106 		if (m->m_len >= count) {
   2107 			m->m_len = count;
   2108 			if (nul > 0) {
   2109 				cp = mtod(m, caddr_t)+m->m_len-nul;
   2110 				for (i = 0; i < nul; i++)
   2111 					*cp++ = '\0';
   2112 			}
   2113 			break;
   2114 		}
   2115 		count -= m->m_len;
   2116 	}
   2117 	for (m = m->m_next;m;m = m->m_next)
   2118 		m->m_len = 0;
   2119 }
   2120 
   2121 /*
   2122  * Make these functions instead of macros, so that the kernel text size
   2123  * doesn't get too big...
   2124  */
   2125 void
   2126 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
   2127 	struct nfsrv_descript *nfsd;
   2128 	int before_ret;
   2129 	struct vattr *before_vap;
   2130 	int after_ret;
   2131 	struct vattr *after_vap;
   2132 	struct mbuf **mbp;
   2133 	char **bposp;
   2134 {
   2135 	struct mbuf *mb = *mbp, *mb2;
   2136 	char *bpos = *bposp;
   2137 	u_int32_t *tl;
   2138 
   2139 	if (before_ret) {
   2140 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   2141 		*tl = nfs_false;
   2142 	} else {
   2143 		nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
   2144 		*tl++ = nfs_true;
   2145 		txdr_hyper(before_vap->va_size, tl);
   2146 		tl += 2;
   2147 		txdr_nfsv3time(&(before_vap->va_mtime), tl);
   2148 		tl += 2;
   2149 		txdr_nfsv3time(&(before_vap->va_ctime), tl);
   2150 	}
   2151 	*bposp = bpos;
   2152 	*mbp = mb;
   2153 	nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
   2154 }
   2155 
   2156 void
   2157 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
   2158 	struct nfsrv_descript *nfsd;
   2159 	int after_ret;
   2160 	struct vattr *after_vap;
   2161 	struct mbuf **mbp;
   2162 	char **bposp;
   2163 {
   2164 	struct mbuf *mb = *mbp, *mb2;
   2165 	char *bpos = *bposp;
   2166 	u_int32_t *tl;
   2167 	struct nfs_fattr *fp;
   2168 
   2169 	if (after_ret) {
   2170 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   2171 		*tl = nfs_false;
   2172 	} else {
   2173 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
   2174 		*tl++ = nfs_true;
   2175 		fp = (struct nfs_fattr *)tl;
   2176 		nfsm_srvfattr(nfsd, after_vap, fp);
   2177 	}
   2178 	*mbp = mb;
   2179 	*bposp = bpos;
   2180 }
   2181 
   2182 void
   2183 nfsm_srvfattr(nfsd, vap, fp)
   2184 	struct nfsrv_descript *nfsd;
   2185 	struct vattr *vap;
   2186 	struct nfs_fattr *fp;
   2187 {
   2188 
   2189 	fp->fa_nlink = txdr_unsigned(vap->va_nlink);
   2190 	fp->fa_uid = txdr_unsigned(vap->va_uid);
   2191 	fp->fa_gid = txdr_unsigned(vap->va_gid);
   2192 	if (nfsd->nd_flag & ND_NFSV3) {
   2193 		fp->fa_type = vtonfsv3_type(vap->va_type);
   2194 		fp->fa_mode = vtonfsv3_mode(vap->va_mode);
   2195 		txdr_hyper(vap->va_size, &fp->fa3_size);
   2196 		txdr_hyper(vap->va_bytes, &fp->fa3_used);
   2197 		fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
   2198 		fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
   2199 		fp->fa3_fsid.nfsuquad[0] = 0;
   2200 		fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
   2201 		fp->fa3_fileid.nfsuquad[0] = 0;
   2202 		fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
   2203 		txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
   2204 		txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
   2205 		txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
   2206 	} else {
   2207 		fp->fa_type = vtonfsv2_type(vap->va_type);
   2208 		fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
   2209 		fp->fa2_size = txdr_unsigned(vap->va_size);
   2210 		fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
   2211 		if (vap->va_type == VFIFO)
   2212 			fp->fa2_rdev = 0xffffffff;
   2213 		else
   2214 			fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
   2215 		fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
   2216 		fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
   2217 		fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
   2218 		txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
   2219 		txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
   2220 		txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
   2221 	}
   2222 }
   2223 
   2224 /*
   2225  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
   2226  * 	- look up fsid in mount list (if not found ret error)
   2227  *	- get vp and export rights by calling VFS_FHTOVP()
   2228  *	- if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
   2229  *	- if not lockflag unlock it with VOP_UNLOCK()
   2230  */
   2231 int
   2232 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
   2233 	fhandle_t *fhp;
   2234 	int lockflag;
   2235 	struct vnode **vpp;
   2236 	struct ucred *cred;
   2237 	struct nfssvc_sock *slp;
   2238 	struct mbuf *nam;
   2239 	int *rdonlyp;
   2240 	int kerbflag;
   2241 {
   2242 	struct mount *mp;
   2243 	int i;
   2244 	struct ucred *credanon;
   2245 	int error, exflags;
   2246 	struct sockaddr_in *saddr;
   2247 
   2248 	*vpp = (struct vnode *)0;
   2249 
   2250 	if (nfs_ispublicfh(fhp)) {
   2251 		if (!pubflag || !nfs_pub.np_valid)
   2252 			return (ESTALE);
   2253 		fhp = &nfs_pub.np_handle;
   2254 	}
   2255 
   2256 	mp = vfs_getvfs(&fhp->fh_fsid);
   2257 	if (!mp)
   2258 		return (ESTALE);
   2259 	error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
   2260 	if (error)
   2261 		return (error);
   2262 	error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
   2263 	if (error)
   2264 		return (error);
   2265 
   2266 	if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
   2267 		saddr = mtod(nam, struct sockaddr_in *);
   2268 		if ((saddr->sin_family == AF_INET) &&
   2269 		    ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
   2270 			vput(*vpp);
   2271 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
   2272 		}
   2273 #ifdef INET6
   2274 		if ((saddr->sin_family == AF_INET6) &&
   2275 		    ntohs(saddr->sin_port) >= IPV6PORT_RESERVED) {
   2276 			vput(*vpp);
   2277 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
   2278 		}
   2279 #endif
   2280 	}
   2281 	/*
   2282 	 * Check/setup credentials.
   2283 	 */
   2284 	if (exflags & MNT_EXKERB) {
   2285 		if (!kerbflag) {
   2286 			vput(*vpp);
   2287 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
   2288 		}
   2289 	} else if (kerbflag) {
   2290 		vput(*vpp);
   2291 		return (NFSERR_AUTHERR | AUTH_TOOWEAK);
   2292 	} else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
   2293 		cred->cr_uid = credanon->cr_uid;
   2294 		cred->cr_gid = credanon->cr_gid;
   2295 		for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
   2296 			cred->cr_groups[i] = credanon->cr_groups[i];
   2297 		cred->cr_ngroups = i;
   2298 	}
   2299 	if (exflags & MNT_EXRDONLY)
   2300 		*rdonlyp = 1;
   2301 	else
   2302 		*rdonlyp = 0;
   2303 	if (!lockflag)
   2304 		VOP_UNLOCK(*vpp, 0);
   2305 	return (0);
   2306 }
   2307 
   2308 /*
   2309  * WebNFS: check if a filehandle is a public filehandle. For v3, this
   2310  * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
   2311  * transformed this to all zeroes in both cases, so check for it.
   2312  */
   2313 int
   2314 nfs_ispublicfh(fhp)
   2315 	fhandle_t *fhp;
   2316 {
   2317 	char *cp = (char *)fhp;
   2318 	int i;
   2319 
   2320 	for (i = 0; i < NFSX_V3FH; i++)
   2321 		if (*cp++ != 0)
   2322 			return (FALSE);
   2323 	return (TRUE);
   2324 }
   2325 
   2326 /*
   2327  * This function compares two net addresses by family and returns TRUE
   2328  * if they are the same host.
   2329  * If there is any doubt, return FALSE.
   2330  * The AF_INET family is handled as a special case so that address mbufs
   2331  * don't need to be saved to store "struct in_addr", which is only 4 bytes.
   2332  */
   2333 int
   2334 netaddr_match(family, haddr, nam)
   2335 	int family;
   2336 	union nethostaddr *haddr;
   2337 	struct mbuf *nam;
   2338 {
   2339 	struct sockaddr_in *inetaddr;
   2340 
   2341 	switch (family) {
   2342 	case AF_INET:
   2343 		inetaddr = mtod(nam, struct sockaddr_in *);
   2344 		if (inetaddr->sin_family == AF_INET &&
   2345 		    inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
   2346 			return (1);
   2347 		break;
   2348 #ifdef INET6
   2349 	case AF_INET6:
   2350 	    {
   2351 		struct sockaddr_in6 *sin6_1, *sin6_2;
   2352 
   2353 		sin6_1 = mtod(nam, struct sockaddr_in6 *);
   2354 		sin6_2 = mtod(haddr->had_nam, struct sockaddr_in6 *);
   2355 		if (sin6_1->sin6_family == AF_INET6 &&
   2356 		    IN6_ARE_ADDR_EQUAL(&sin6_1->sin6_addr, &sin6_2->sin6_addr))
   2357 			return 1;
   2358 	    }
   2359 #endif
   2360 #ifdef ISO
   2361 	case AF_ISO:
   2362 	    {
   2363 		struct sockaddr_iso *isoaddr1, *isoaddr2;
   2364 
   2365 		isoaddr1 = mtod(nam, struct sockaddr_iso *);
   2366 		isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
   2367 		if (isoaddr1->siso_family == AF_ISO &&
   2368 		    isoaddr1->siso_nlen > 0 &&
   2369 		    isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
   2370 		    SAME_ISOADDR(isoaddr1, isoaddr2))
   2371 			return (1);
   2372 		break;
   2373 	    }
   2374 #endif	/* ISO */
   2375 	default:
   2376 		break;
   2377 	};
   2378 	return (0);
   2379 }
   2380 
   2381 /*
   2382  * The write verifier has changed (probably due to a server reboot), so all
   2383  * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
   2384  * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
   2385  * flag. Once done the new write verifier can be set for the mount point.
   2386  */
   2387 void
   2388 nfs_clearcommit(mp)
   2389 	struct mount *mp;
   2390 {
   2391 	struct vnode *vp;
   2392 	struct nfsnode *np;
   2393 	struct vm_page *pg;
   2394 	int s;
   2395 
   2396 	s = splbio();
   2397 	LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
   2398 		KASSERT(vp->v_mount == mp);
   2399 		if (vp->v_type == VNON)
   2400 			continue;
   2401 		np = VTONFS(vp);
   2402 		np->n_pushlo = np->n_pushhi = np->n_pushedlo =
   2403 		    np->n_pushedhi = 0;
   2404 		np->n_commitflags &=
   2405 		    ~(NFS_COMMIT_PUSH_VALID | NFS_COMMIT_PUSHED_VALID);
   2406 		simple_lock(&vp->v_uvm.u_obj.vmobjlock);
   2407 		TAILQ_FOREACH(pg, &vp->v_uvm.u_obj.memq, listq) {
   2408 			pg->flags &= ~PG_NEEDCOMMIT;
   2409 		}
   2410 		simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
   2411 	}
   2412 	splx(s);
   2413 }
   2414 
   2415 void
   2416 nfs_merge_commit_ranges(vp)
   2417 	struct vnode *vp;
   2418 {
   2419 	struct nfsnode *np = VTONFS(vp);
   2420 
   2421 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
   2422 		np->n_pushedlo = np->n_pushlo;
   2423 		np->n_pushedhi = np->n_pushhi;
   2424 		np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
   2425 	} else {
   2426 		if (np->n_pushlo < np->n_pushedlo)
   2427 			np->n_pushedlo = np->n_pushlo;
   2428 		if (np->n_pushhi > np->n_pushedhi)
   2429 			np->n_pushedhi = np->n_pushhi;
   2430 	}
   2431 
   2432 	np->n_pushlo = np->n_pushhi = 0;
   2433 	np->n_commitflags &= ~NFS_COMMIT_PUSH_VALID;
   2434 
   2435 #ifdef fvdl_debug
   2436 	printf("merge: committed: %u - %u\n", (unsigned)np->n_pushedlo,
   2437 	    (unsigned)np->n_pushedhi);
   2438 #endif
   2439 }
   2440 
   2441 int
   2442 nfs_in_committed_range(vp, off, len)
   2443 	struct vnode *vp;
   2444 	off_t off, len;
   2445 {
   2446 	struct nfsnode *np = VTONFS(vp);
   2447 	off_t lo, hi;
   2448 
   2449 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
   2450 		return 0;
   2451 	lo = off;
   2452 	hi = lo + len;
   2453 
   2454 	return (lo >= np->n_pushedlo && hi <= np->n_pushedhi);
   2455 }
   2456 
   2457 int
   2458 nfs_in_tobecommitted_range(vp, off, len)
   2459 	struct vnode *vp;
   2460 	off_t off, len;
   2461 {
   2462 	struct nfsnode *np = VTONFS(vp);
   2463 	off_t lo, hi;
   2464 
   2465 	if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
   2466 		return 0;
   2467 	lo = off;
   2468 	hi = lo + len;
   2469 
   2470 	return (lo >= np->n_pushlo && hi <= np->n_pushhi);
   2471 }
   2472 
   2473 void
   2474 nfs_add_committed_range(vp, off, len)
   2475 	struct vnode *vp;
   2476 	off_t off, len;
   2477 {
   2478 	struct nfsnode *np = VTONFS(vp);
   2479 	off_t lo, hi;
   2480 
   2481 	lo = off;
   2482 	hi = lo + len;
   2483 
   2484 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
   2485 		np->n_pushedlo = lo;
   2486 		np->n_pushedhi = hi;
   2487 		np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
   2488 	} else {
   2489 		if (hi > np->n_pushedhi)
   2490 			np->n_pushedhi = hi;
   2491 		if (lo < np->n_pushedlo)
   2492 			np->n_pushedlo = lo;
   2493 	}
   2494 #ifdef fvdl_debug
   2495 	printf("add: committed: %u - %u\n", (unsigned)np->n_pushedlo,
   2496 	    (unsigned)np->n_pushedhi);
   2497 #endif
   2498 }
   2499 
   2500 void
   2501 nfs_del_committed_range(vp, off, len)
   2502 	struct vnode *vp;
   2503 	off_t off, len;
   2504 {
   2505 	struct nfsnode *np = VTONFS(vp);
   2506 	off_t lo, hi;
   2507 
   2508 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
   2509 		return;
   2510 
   2511 	lo = off;
   2512 	hi = lo + len;
   2513 
   2514 	if (lo > np->n_pushedhi || hi < np->n_pushedlo)
   2515 		return;
   2516 	if (lo <= np->n_pushedlo)
   2517 		np->n_pushedlo = hi;
   2518 	else if (hi >= np->n_pushedhi)
   2519 		np->n_pushedhi = lo;
   2520 	else {
   2521 		/*
   2522 		 * XXX There's only one range. If the deleted range
   2523 		 * is in the middle, pick the largest of the
   2524 		 * contiguous ranges that it leaves.
   2525 		 */
   2526 		if ((np->n_pushedlo - lo) > (hi - np->n_pushedhi))
   2527 			np->n_pushedhi = lo;
   2528 		else
   2529 			np->n_pushedlo = hi;
   2530 	}
   2531 #ifdef fvdl_debug
   2532 	printf("del: committed: %u - %u\n", (unsigned)np->n_pushedlo,
   2533 	    (unsigned)np->n_pushedhi);
   2534 #endif
   2535 }
   2536 
   2537 void
   2538 nfs_add_tobecommitted_range(vp, off, len)
   2539 	struct vnode *vp;
   2540 	off_t off, len;
   2541 {
   2542 	struct nfsnode *np = VTONFS(vp);
   2543 	off_t lo, hi;
   2544 
   2545 	lo = off;
   2546 	hi = lo + len;
   2547 
   2548 	if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID)) {
   2549 		np->n_pushlo = lo;
   2550 		np->n_pushhi = hi;
   2551 		np->n_commitflags |= NFS_COMMIT_PUSH_VALID;
   2552 	} else {
   2553 		if (lo < np->n_pushlo)
   2554 			np->n_pushlo = lo;
   2555 		if (hi > np->n_pushhi)
   2556 			np->n_pushhi = hi;
   2557 	}
   2558 #ifdef fvdl_debug
   2559 	printf("add: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
   2560 	    (unsigned)np->n_pushhi);
   2561 #endif
   2562 }
   2563 
   2564 void
   2565 nfs_del_tobecommitted_range(vp, off, len)
   2566 	struct vnode *vp;
   2567 	off_t off, len;
   2568 {
   2569 	struct nfsnode *np = VTONFS(vp);
   2570 	off_t lo, hi;
   2571 
   2572 	if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
   2573 		return;
   2574 
   2575 	lo = off;
   2576 	hi = lo + len;
   2577 
   2578 	if (lo > np->n_pushhi || hi < np->n_pushlo)
   2579 		return;
   2580 
   2581 	if (lo <= np->n_pushlo)
   2582 		np->n_pushlo = hi;
   2583 	else if (hi >= np->n_pushhi)
   2584 		np->n_pushhi = lo;
   2585 	else {
   2586 		/*
   2587 		 * XXX There's only one range. If the deleted range
   2588 		 * is in the middle, pick the largest of the
   2589 		 * contiguous ranges that it leaves.
   2590 		 */
   2591 		if ((np->n_pushlo - lo) > (hi - np->n_pushhi))
   2592 			np->n_pushhi = lo;
   2593 		else
   2594 			np->n_pushlo = hi;
   2595 	}
   2596 #ifdef fvdl_debug
   2597 	printf("del: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
   2598 	    (unsigned)np->n_pushhi);
   2599 #endif
   2600 }
   2601 
   2602 /*
   2603  * Map errnos to NFS error numbers. For Version 3 also filter out error
   2604  * numbers not specified for the associated procedure.
   2605  */
   2606 int
   2607 nfsrv_errmap(nd, err)
   2608 	struct nfsrv_descript *nd;
   2609 	int err;
   2610 {
   2611 	const short *defaulterrp, *errp;
   2612 
   2613 	if (nd->nd_flag & ND_NFSV3) {
   2614 	    if (nd->nd_procnum <= NFSPROC_COMMIT) {
   2615 		errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
   2616 		while (*++errp) {
   2617 			if (*errp == err)
   2618 				return (err);
   2619 			else if (*errp > err)
   2620 				break;
   2621 		}
   2622 		return ((int)*defaulterrp);
   2623 	    } else
   2624 		return (err & 0xffff);
   2625 	}
   2626 	if (err <= ELAST)
   2627 		return ((int)nfsrv_v2errmap[err - 1]);
   2628 	return (NFSERR_IO);
   2629 }
   2630 
   2631 /*
   2632  * Sort the group list in increasing numerical order.
   2633  * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
   2634  *  that used to be here.)
   2635  */
   2636 void
   2637 nfsrvw_sort(list, num)
   2638         gid_t *list;
   2639         int num;
   2640 {
   2641 	int i, j;
   2642 	gid_t v;
   2643 
   2644 	/* Insertion sort. */
   2645 	for (i = 1; i < num; i++) {
   2646 		v = list[i];
   2647 		/* find correct slot for value v, moving others up */
   2648 		for (j = i; --j >= 0 && v < list[j];)
   2649 			list[j + 1] = list[j];
   2650 		list[j + 1] = v;
   2651 	}
   2652 }
   2653 
   2654 /*
   2655  * copy credentials making sure that the result can be compared with memcmp().
   2656  */
   2657 void
   2658 nfsrv_setcred(incred, outcred)
   2659 	struct ucred *incred, *outcred;
   2660 {
   2661 	int i;
   2662 
   2663 	memset((caddr_t)outcred, 0, sizeof (struct ucred));
   2664 	outcred->cr_ref = 1;
   2665 	outcred->cr_uid = incred->cr_uid;
   2666 	outcred->cr_gid = incred->cr_gid;
   2667 	outcred->cr_ngroups = incred->cr_ngroups;
   2668 	for (i = 0; i < incred->cr_ngroups; i++)
   2669 		outcred->cr_groups[i] = incred->cr_groups[i];
   2670 	nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
   2671 }
   2672