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