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