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