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