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