Home | History | Annotate | Line # | Download | only in nfs
nfs_subs.c revision 1.26
      1 /*	$NetBSD: nfs_subs.c,v 1.26 1996/03/13 00:44:17 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)nfs_subs.c	8.8 (Berkeley) 5/22/95
     39  */
     40 
     41 
     42 /*
     43  * These functions support the macros and help fiddle mbuf chains for
     44  * the nfs op functions. They do things like create the rpc header and
     45  * copy data between mbuf chains and uio lists.
     46  */
     47 #include <sys/param.h>
     48 #include <sys/proc.h>
     49 #include <sys/systm.h>
     50 #include <sys/kernel.h>
     51 #include <sys/mount.h>
     52 #include <sys/vnode.h>
     53 #include <sys/namei.h>
     54 #include <sys/mbuf.h>
     55 #include <sys/socket.h>
     56 #include <sys/stat.h>
     57 #include <sys/malloc.h>
     58 
     59 #include <vm/vm.h>
     60 
     61 #include <nfs/rpcv2.h>
     62 #include <nfs/nfsproto.h>
     63 #include <nfs/nfsnode.h>
     64 #include <nfs/nfs.h>
     65 #include <nfs/xdr_subs.h>
     66 #include <nfs/nfsm_subs.h>
     67 #include <nfs/nfsmount.h>
     68 #include <nfs/nqnfs.h>
     69 #include <nfs/nfsrtt.h>
     70 #include <nfs/nfs_var.h>
     71 
     72 #include <miscfs/specfs/specdev.h>
     73 
     74 #include <vm/vm.h>
     75 
     76 #include <netinet/in.h>
     77 #ifdef ISO
     78 #include <netiso/iso.h>
     79 #endif
     80 
     81 /*
     82  * Data items converted to xdr at startup, since they are constant
     83  * This is kinda hokey, but may save a little time doing byte swaps
     84  */
     85 u_int32_t nfs_xdrneg1;
     86 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
     87 	rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
     88 	rpc_auth_kerb;
     89 u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
     90 
     91 /* And other global data */
     92 static u_int32_t nfs_xid = 0;
     93 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
     94 		      NFCHR, NFNON };
     95 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
     96 		      NFFIFO, NFNON };
     97 enum vtype nv2tov_type[8] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON };
     98 enum vtype nv3tov_type[8]={ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO };
     99 int nfs_ticks;
    100 
    101 /*
    102  * Mapping of old NFS Version 2 RPC numbers to generic numbers.
    103  */
    104 int nfsv3_procid[NFS_NPROCS] = {
    105 	NFSPROC_NULL,
    106 	NFSPROC_GETATTR,
    107 	NFSPROC_SETATTR,
    108 	NFSPROC_NOOP,
    109 	NFSPROC_LOOKUP,
    110 	NFSPROC_READLINK,
    111 	NFSPROC_READ,
    112 	NFSPROC_NOOP,
    113 	NFSPROC_WRITE,
    114 	NFSPROC_CREATE,
    115 	NFSPROC_REMOVE,
    116 	NFSPROC_RENAME,
    117 	NFSPROC_LINK,
    118 	NFSPROC_SYMLINK,
    119 	NFSPROC_MKDIR,
    120 	NFSPROC_RMDIR,
    121 	NFSPROC_READDIR,
    122 	NFSPROC_FSSTAT,
    123 	NFSPROC_NOOP,
    124 	NFSPROC_NOOP,
    125 	NFSPROC_NOOP,
    126 	NFSPROC_NOOP,
    127 	NFSPROC_NOOP,
    128 	NFSPROC_NOOP,
    129 	NFSPROC_NOOP,
    130 	NFSPROC_NOOP
    131 };
    132 
    133 /*
    134  * and the reverse mapping from generic to Version 2 procedure numbers
    135  */
    136 int nfsv2_procid[NFS_NPROCS] = {
    137 	NFSV2PROC_NULL,
    138 	NFSV2PROC_GETATTR,
    139 	NFSV2PROC_SETATTR,
    140 	NFSV2PROC_LOOKUP,
    141 	NFSV2PROC_NOOP,
    142 	NFSV2PROC_READLINK,
    143 	NFSV2PROC_READ,
    144 	NFSV2PROC_WRITE,
    145 	NFSV2PROC_CREATE,
    146 	NFSV2PROC_MKDIR,
    147 	NFSV2PROC_SYMLINK,
    148 	NFSV2PROC_CREATE,
    149 	NFSV2PROC_REMOVE,
    150 	NFSV2PROC_RMDIR,
    151 	NFSV2PROC_RENAME,
    152 	NFSV2PROC_LINK,
    153 	NFSV2PROC_READDIR,
    154 	NFSV2PROC_NOOP,
    155 	NFSV2PROC_STATFS,
    156 	NFSV2PROC_NOOP,
    157 	NFSV2PROC_NOOP,
    158 	NFSV2PROC_NOOP,
    159 	NFSV2PROC_NOOP,
    160 	NFSV2PROC_NOOP,
    161 	NFSV2PROC_NOOP,
    162 	NFSV2PROC_NOOP,
    163 };
    164 
    165 /*
    166  * Maps errno values to nfs error numbers.
    167  * Use NFSERR_IO as the catch all for ones not specifically defined in
    168  * RFC 1094.
    169  */
    170 static u_char nfsrv_v2errmap[ELAST] = {
    171   NFSERR_PERM,	NFSERR_NOENT,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    172   NFSERR_NXIO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    173   NFSERR_IO,	NFSERR_IO,	NFSERR_ACCES,	NFSERR_IO,	NFSERR_IO,
    174   NFSERR_IO,	NFSERR_EXIST,	NFSERR_IO,	NFSERR_NODEV,	NFSERR_NOTDIR,
    175   NFSERR_ISDIR,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    176   NFSERR_IO,	NFSERR_FBIG,	NFSERR_NOSPC,	NFSERR_IO,	NFSERR_ROFS,
    177   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    178   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    179   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    180   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    181   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    182   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    183   NFSERR_IO,	NFSERR_IO,	NFSERR_NAMETOL,	NFSERR_IO,	NFSERR_IO,
    184   NFSERR_NOTEMPTY, NFSERR_IO,	NFSERR_IO,	NFSERR_DQUOT,	NFSERR_STALE,
    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,
    188 };
    189 
    190 /*
    191  * Maps errno values to nfs error numbers.
    192  * Although it is not obvious whether or not NFS clients really care if
    193  * a returned error value is in the specified list for the procedure, the
    194  * safest thing to do is filter them appropriately. For Version 2, the
    195  * X/Open XNFS document is the only specification that defines error values
    196  * for each RPC (The RFC simply lists all possible error values for all RPCs),
    197  * so I have decided to not do this for Version 2.
    198  * The first entry is the default error return and the rest are the valid
    199  * errors for that RPC in increasing numeric order.
    200  */
    201 static short nfsv3err_null[] = {
    202 	0,
    203 	0,
    204 };
    205 
    206 static short nfsv3err_getattr[] = {
    207 	NFSERR_IO,
    208 	NFSERR_IO,
    209 	NFSERR_STALE,
    210 	NFSERR_BADHANDLE,
    211 	NFSERR_SERVERFAULT,
    212 	0,
    213 };
    214 
    215 static short nfsv3err_setattr[] = {
    216 	NFSERR_IO,
    217 	NFSERR_PERM,
    218 	NFSERR_IO,
    219 	NFSERR_ACCES,
    220 	NFSERR_INVAL,
    221 	NFSERR_NOSPC,
    222 	NFSERR_ROFS,
    223 	NFSERR_DQUOT,
    224 	NFSERR_STALE,
    225 	NFSERR_BADHANDLE,
    226 	NFSERR_NOT_SYNC,
    227 	NFSERR_SERVERFAULT,
    228 	0,
    229 };
    230 
    231 static short nfsv3err_lookup[] = {
    232 	NFSERR_IO,
    233 	NFSERR_NOENT,
    234 	NFSERR_IO,
    235 	NFSERR_ACCES,
    236 	NFSERR_NOTDIR,
    237 	NFSERR_NAMETOL,
    238 	NFSERR_STALE,
    239 	NFSERR_BADHANDLE,
    240 	NFSERR_SERVERFAULT,
    241 	0,
    242 };
    243 
    244 static short nfsv3err_access[] = {
    245 	NFSERR_IO,
    246 	NFSERR_IO,
    247 	NFSERR_STALE,
    248 	NFSERR_BADHANDLE,
    249 	NFSERR_SERVERFAULT,
    250 	0,
    251 };
    252 
    253 static short nfsv3err_readlink[] = {
    254 	NFSERR_IO,
    255 	NFSERR_IO,
    256 	NFSERR_ACCES,
    257 	NFSERR_INVAL,
    258 	NFSERR_STALE,
    259 	NFSERR_BADHANDLE,
    260 	NFSERR_NOTSUPP,
    261 	NFSERR_SERVERFAULT,
    262 	0,
    263 };
    264 
    265 static short nfsv3err_read[] = {
    266 	NFSERR_IO,
    267 	NFSERR_IO,
    268 	NFSERR_NXIO,
    269 	NFSERR_ACCES,
    270 	NFSERR_INVAL,
    271 	NFSERR_STALE,
    272 	NFSERR_BADHANDLE,
    273 	NFSERR_SERVERFAULT,
    274 	0,
    275 };
    276 
    277 static short nfsv3err_write[] = {
    278 	NFSERR_IO,
    279 	NFSERR_IO,
    280 	NFSERR_ACCES,
    281 	NFSERR_INVAL,
    282 	NFSERR_FBIG,
    283 	NFSERR_NOSPC,
    284 	NFSERR_ROFS,
    285 	NFSERR_DQUOT,
    286 	NFSERR_STALE,
    287 	NFSERR_BADHANDLE,
    288 	NFSERR_SERVERFAULT,
    289 	0,
    290 };
    291 
    292 static short nfsv3err_create[] = {
    293 	NFSERR_IO,
    294 	NFSERR_IO,
    295 	NFSERR_ACCES,
    296 	NFSERR_EXIST,
    297 	NFSERR_NOTDIR,
    298 	NFSERR_NOSPC,
    299 	NFSERR_ROFS,
    300 	NFSERR_NAMETOL,
    301 	NFSERR_DQUOT,
    302 	NFSERR_STALE,
    303 	NFSERR_BADHANDLE,
    304 	NFSERR_NOTSUPP,
    305 	NFSERR_SERVERFAULT,
    306 	0,
    307 };
    308 
    309 static short nfsv3err_mkdir[] = {
    310 	NFSERR_IO,
    311 	NFSERR_IO,
    312 	NFSERR_ACCES,
    313 	NFSERR_EXIST,
    314 	NFSERR_NOTDIR,
    315 	NFSERR_NOSPC,
    316 	NFSERR_ROFS,
    317 	NFSERR_NAMETOL,
    318 	NFSERR_DQUOT,
    319 	NFSERR_STALE,
    320 	NFSERR_BADHANDLE,
    321 	NFSERR_NOTSUPP,
    322 	NFSERR_SERVERFAULT,
    323 	0,
    324 };
    325 
    326 static short nfsv3err_symlink[] = {
    327 	NFSERR_IO,
    328 	NFSERR_IO,
    329 	NFSERR_ACCES,
    330 	NFSERR_EXIST,
    331 	NFSERR_NOTDIR,
    332 	NFSERR_NOSPC,
    333 	NFSERR_ROFS,
    334 	NFSERR_NAMETOL,
    335 	NFSERR_DQUOT,
    336 	NFSERR_STALE,
    337 	NFSERR_BADHANDLE,
    338 	NFSERR_NOTSUPP,
    339 	NFSERR_SERVERFAULT,
    340 	0,
    341 };
    342 
    343 static short nfsv3err_mknod[] = {
    344 	NFSERR_IO,
    345 	NFSERR_IO,
    346 	NFSERR_ACCES,
    347 	NFSERR_EXIST,
    348 	NFSERR_NOTDIR,
    349 	NFSERR_NOSPC,
    350 	NFSERR_ROFS,
    351 	NFSERR_NAMETOL,
    352 	NFSERR_DQUOT,
    353 	NFSERR_STALE,
    354 	NFSERR_BADHANDLE,
    355 	NFSERR_NOTSUPP,
    356 	NFSERR_SERVERFAULT,
    357 	NFSERR_BADTYPE,
    358 	0,
    359 };
    360 
    361 static short nfsv3err_remove[] = {
    362 	NFSERR_IO,
    363 	NFSERR_NOENT,
    364 	NFSERR_IO,
    365 	NFSERR_ACCES,
    366 	NFSERR_NOTDIR,
    367 	NFSERR_ROFS,
    368 	NFSERR_NAMETOL,
    369 	NFSERR_STALE,
    370 	NFSERR_BADHANDLE,
    371 	NFSERR_SERVERFAULT,
    372 	0,
    373 };
    374 
    375 static short nfsv3err_rmdir[] = {
    376 	NFSERR_IO,
    377 	NFSERR_NOENT,
    378 	NFSERR_IO,
    379 	NFSERR_ACCES,
    380 	NFSERR_EXIST,
    381 	NFSERR_NOTDIR,
    382 	NFSERR_INVAL,
    383 	NFSERR_ROFS,
    384 	NFSERR_NAMETOL,
    385 	NFSERR_NOTEMPTY,
    386 	NFSERR_STALE,
    387 	NFSERR_BADHANDLE,
    388 	NFSERR_NOTSUPP,
    389 	NFSERR_SERVERFAULT,
    390 	0,
    391 };
    392 
    393 static short nfsv3err_rename[] = {
    394 	NFSERR_IO,
    395 	NFSERR_NOENT,
    396 	NFSERR_IO,
    397 	NFSERR_ACCES,
    398 	NFSERR_EXIST,
    399 	NFSERR_XDEV,
    400 	NFSERR_NOTDIR,
    401 	NFSERR_ISDIR,
    402 	NFSERR_INVAL,
    403 	NFSERR_NOSPC,
    404 	NFSERR_ROFS,
    405 	NFSERR_MLINK,
    406 	NFSERR_NAMETOL,
    407 	NFSERR_NOTEMPTY,
    408 	NFSERR_DQUOT,
    409 	NFSERR_STALE,
    410 	NFSERR_BADHANDLE,
    411 	NFSERR_NOTSUPP,
    412 	NFSERR_SERVERFAULT,
    413 	0,
    414 };
    415 
    416 static short nfsv3err_link[] = {
    417 	NFSERR_IO,
    418 	NFSERR_IO,
    419 	NFSERR_ACCES,
    420 	NFSERR_EXIST,
    421 	NFSERR_XDEV,
    422 	NFSERR_NOTDIR,
    423 	NFSERR_INVAL,
    424 	NFSERR_NOSPC,
    425 	NFSERR_ROFS,
    426 	NFSERR_MLINK,
    427 	NFSERR_NAMETOL,
    428 	NFSERR_DQUOT,
    429 	NFSERR_STALE,
    430 	NFSERR_BADHANDLE,
    431 	NFSERR_NOTSUPP,
    432 	NFSERR_SERVERFAULT,
    433 	0,
    434 };
    435 
    436 static short nfsv3err_readdir[] = {
    437 	NFSERR_IO,
    438 	NFSERR_IO,
    439 	NFSERR_ACCES,
    440 	NFSERR_NOTDIR,
    441 	NFSERR_STALE,
    442 	NFSERR_BADHANDLE,
    443 	NFSERR_BAD_COOKIE,
    444 	NFSERR_TOOSMALL,
    445 	NFSERR_SERVERFAULT,
    446 	0,
    447 };
    448 
    449 static short nfsv3err_readdirplus[] = {
    450 	NFSERR_IO,
    451 	NFSERR_IO,
    452 	NFSERR_ACCES,
    453 	NFSERR_NOTDIR,
    454 	NFSERR_STALE,
    455 	NFSERR_BADHANDLE,
    456 	NFSERR_BAD_COOKIE,
    457 	NFSERR_NOTSUPP,
    458 	NFSERR_TOOSMALL,
    459 	NFSERR_SERVERFAULT,
    460 	0,
    461 };
    462 
    463 static short nfsv3err_fsstat[] = {
    464 	NFSERR_IO,
    465 	NFSERR_IO,
    466 	NFSERR_STALE,
    467 	NFSERR_BADHANDLE,
    468 	NFSERR_SERVERFAULT,
    469 	0,
    470 };
    471 
    472 static short nfsv3err_fsinfo[] = {
    473 	NFSERR_STALE,
    474 	NFSERR_STALE,
    475 	NFSERR_BADHANDLE,
    476 	NFSERR_SERVERFAULT,
    477 	0,
    478 };
    479 
    480 static short nfsv3err_pathconf[] = {
    481 	NFSERR_STALE,
    482 	NFSERR_STALE,
    483 	NFSERR_BADHANDLE,
    484 	NFSERR_SERVERFAULT,
    485 	0,
    486 };
    487 
    488 static short nfsv3err_commit[] = {
    489 	NFSERR_IO,
    490 	NFSERR_IO,
    491 	NFSERR_STALE,
    492 	NFSERR_BADHANDLE,
    493 	NFSERR_SERVERFAULT,
    494 	0,
    495 };
    496 
    497 static short *nfsrv_v3errmap[] = {
    498 	nfsv3err_null,
    499 	nfsv3err_getattr,
    500 	nfsv3err_setattr,
    501 	nfsv3err_lookup,
    502 	nfsv3err_access,
    503 	nfsv3err_readlink,
    504 	nfsv3err_read,
    505 	nfsv3err_write,
    506 	nfsv3err_create,
    507 	nfsv3err_mkdir,
    508 	nfsv3err_symlink,
    509 	nfsv3err_mknod,
    510 	nfsv3err_remove,
    511 	nfsv3err_rmdir,
    512 	nfsv3err_rename,
    513 	nfsv3err_link,
    514 	nfsv3err_readdir,
    515 	nfsv3err_readdirplus,
    516 	nfsv3err_fsstat,
    517 	nfsv3err_fsinfo,
    518 	nfsv3err_pathconf,
    519 	nfsv3err_commit,
    520 };
    521 
    522 extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
    523 extern struct nfsrtt nfsrtt;
    524 extern time_t nqnfsstarttime;
    525 extern int nqsrv_clockskew;
    526 extern int nqsrv_writeslack;
    527 extern int nqsrv_maxlease;
    528 extern struct nfsstats nfsstats;
    529 extern int nqnfs_piggy[NFS_NPROCS];
    530 extern nfstype nfsv2_type[9];
    531 extern nfstype nfsv3_type[9];
    532 extern struct nfsnodehashhead *nfsnodehashtbl;
    533 extern u_long nfsnodehash;
    534 
    535 LIST_HEAD(nfsnodehashhead, nfsnode);
    536 
    537 /*
    538  * Create the header for an rpc request packet
    539  * The hsiz is the size of the rest of the nfs request header.
    540  * (just used to decide if a cluster is a good idea)
    541  */
    542 struct mbuf *
    543 nfsm_reqh(vp, procid, hsiz, bposp)
    544 	struct vnode *vp;
    545 	u_long procid;
    546 	int hsiz;
    547 	caddr_t *bposp;
    548 {
    549 	register struct mbuf *mb;
    550 	register u_int32_t *tl;
    551 	register caddr_t bpos;
    552 	struct mbuf *mb2;
    553 	struct nfsmount *nmp;
    554 	int nqflag;
    555 
    556 	MGET(mb, M_WAIT, MT_DATA);
    557 	if (hsiz >= MINCLSIZE)
    558 		MCLGET(mb, M_WAIT);
    559 	mb->m_len = 0;
    560 	bpos = mtod(mb, caddr_t);
    561 
    562 	/*
    563 	 * For NQNFS, add lease request.
    564 	 */
    565 	if (vp) {
    566 		nmp = VFSTONFS(vp->v_mount);
    567 		if (nmp->nm_flag & NFSMNT_NQNFS) {
    568 			nqflag = NQNFS_NEEDLEASE(vp, procid);
    569 			if (nqflag) {
    570 				nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
    571 				*tl++ = txdr_unsigned(nqflag);
    572 				*tl = txdr_unsigned(nmp->nm_leaseterm);
    573 			} else {
    574 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    575 				*tl = 0;
    576 			}
    577 		}
    578 	}
    579 	/* Finally, return values */
    580 	*bposp = bpos;
    581 	return (mb);
    582 }
    583 
    584 /*
    585  * Build the RPC header and fill in the authorization info.
    586  * The authorization string argument is only used when the credentials
    587  * come from outside of the kernel.
    588  * Returns the head of the mbuf list.
    589  */
    590 struct mbuf *
    591 nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
    592 	verf_str, mrest, mrest_len, mbp, xidp)
    593 	register struct ucred *cr;
    594 	int nmflag;
    595 	int procid;
    596 	int auth_type;
    597 	int auth_len;
    598 	char *auth_str;
    599 	int verf_len;
    600 	char *verf_str;
    601 	struct mbuf *mrest;
    602 	int mrest_len;
    603 	struct mbuf **mbp;
    604 	u_int32_t *xidp;
    605 {
    606 	register struct mbuf *mb;
    607 	register u_int32_t *tl;
    608 	register caddr_t bpos;
    609 	register int i;
    610 	struct mbuf *mreq, *mb2;
    611 	int siz, grpsiz, authsiz;
    612 
    613 	authsiz = nfsm_rndup(auth_len);
    614 	MGETHDR(mb, M_WAIT, MT_DATA);
    615 	if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
    616 		MCLGET(mb, M_WAIT);
    617 	} else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
    618 		MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
    619 	} else {
    620 		MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
    621 	}
    622 	mb->m_len = 0;
    623 	mreq = mb;
    624 	bpos = mtod(mb, caddr_t);
    625 
    626 	/*
    627 	 * First the RPC header.
    628 	 */
    629 	nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
    630 	if (++nfs_xid == 0)
    631 		nfs_xid++;
    632 	*tl++ = *xidp = txdr_unsigned(nfs_xid);
    633 	*tl++ = rpc_call;
    634 	*tl++ = rpc_vers;
    635 	if (nmflag & NFSMNT_NQNFS) {
    636 		*tl++ = txdr_unsigned(NQNFS_PROG);
    637 		*tl++ = txdr_unsigned(NQNFS_VER3);
    638 	} else {
    639 		*tl++ = txdr_unsigned(NFS_PROG);
    640 		if (nmflag & NFSMNT_NFSV3)
    641 			*tl++ = txdr_unsigned(NFS_VER3);
    642 		else
    643 			*tl++ = txdr_unsigned(NFS_VER2);
    644 	}
    645 	if (nmflag & NFSMNT_NFSV3)
    646 		*tl++ = txdr_unsigned(procid);
    647 	else
    648 		*tl++ = txdr_unsigned(nfsv2_procid[procid]);
    649 
    650 	/*
    651 	 * And then the authorization cred.
    652 	 */
    653 	*tl++ = txdr_unsigned(auth_type);
    654 	*tl = txdr_unsigned(authsiz);
    655 	switch (auth_type) {
    656 	case RPCAUTH_UNIX:
    657 		nfsm_build(tl, u_int32_t *, auth_len);
    658 		*tl++ = 0;		/* stamp ?? */
    659 		*tl++ = 0;		/* NULL hostname */
    660 		*tl++ = txdr_unsigned(cr->cr_uid);
    661 		*tl++ = txdr_unsigned(cr->cr_gid);
    662 		grpsiz = (auth_len >> 2) - 5;
    663 		*tl++ = txdr_unsigned(grpsiz);
    664 		for (i = 0; i < grpsiz; i++)
    665 			*tl++ = txdr_unsigned(cr->cr_groups[i]);
    666 		break;
    667 	case RPCAUTH_KERB4:
    668 		siz = auth_len;
    669 		while (siz > 0) {
    670 			if (M_TRAILINGSPACE(mb) == 0) {
    671 				MGET(mb2, M_WAIT, MT_DATA);
    672 				if (siz >= MINCLSIZE)
    673 					MCLGET(mb2, M_WAIT);
    674 				mb->m_next = mb2;
    675 				mb = mb2;
    676 				mb->m_len = 0;
    677 				bpos = mtod(mb, caddr_t);
    678 			}
    679 			i = min(siz, M_TRAILINGSPACE(mb));
    680 			bcopy(auth_str, bpos, i);
    681 			mb->m_len += i;
    682 			auth_str += i;
    683 			bpos += i;
    684 			siz -= i;
    685 		}
    686 		if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
    687 			for (i = 0; i < siz; i++)
    688 				*bpos++ = '\0';
    689 			mb->m_len += siz;
    690 		}
    691 		break;
    692 	};
    693 
    694 	/*
    695 	 * And the verifier...
    696 	 */
    697 	nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
    698 	if (verf_str) {
    699 		*tl++ = txdr_unsigned(RPCAUTH_KERB4);
    700 		*tl = txdr_unsigned(verf_len);
    701 		siz = verf_len;
    702 		while (siz > 0) {
    703 			if (M_TRAILINGSPACE(mb) == 0) {
    704 				MGET(mb2, M_WAIT, MT_DATA);
    705 				if (siz >= MINCLSIZE)
    706 					MCLGET(mb2, M_WAIT);
    707 				mb->m_next = mb2;
    708 				mb = mb2;
    709 				mb->m_len = 0;
    710 				bpos = mtod(mb, caddr_t);
    711 			}
    712 			i = min(siz, M_TRAILINGSPACE(mb));
    713 			bcopy(verf_str, bpos, i);
    714 			mb->m_len += i;
    715 			verf_str += i;
    716 			bpos += i;
    717 			siz -= i;
    718 		}
    719 		if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
    720 			for (i = 0; i < siz; i++)
    721 				*bpos++ = '\0';
    722 			mb->m_len += siz;
    723 		}
    724 	} else {
    725 		*tl++ = txdr_unsigned(RPCAUTH_NULL);
    726 		*tl = 0;
    727 	}
    728 	mb->m_next = mrest;
    729 	mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
    730 	mreq->m_pkthdr.rcvif = (struct ifnet *)0;
    731 	*mbp = mb;
    732 	return (mreq);
    733 }
    734 
    735 /*
    736  * copies mbuf chain to the uio scatter/gather list
    737  */
    738 int
    739 nfsm_mbuftouio(mrep, uiop, siz, dpos)
    740 	struct mbuf **mrep;
    741 	register struct uio *uiop;
    742 	int siz;
    743 	caddr_t *dpos;
    744 {
    745 	register char *mbufcp, *uiocp;
    746 	register int xfer, left, len;
    747 	register struct mbuf *mp;
    748 	long uiosiz, rem;
    749 	int error = 0;
    750 
    751 	mp = *mrep;
    752 	mbufcp = *dpos;
    753 	len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
    754 	rem = nfsm_rndup(siz)-siz;
    755 	while (siz > 0) {
    756 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
    757 			return (EFBIG);
    758 		left = uiop->uio_iov->iov_len;
    759 		uiocp = uiop->uio_iov->iov_base;
    760 		if (left > siz)
    761 			left = siz;
    762 		uiosiz = left;
    763 		while (left > 0) {
    764 			while (len == 0) {
    765 				mp = mp->m_next;
    766 				if (mp == NULL)
    767 					return (EBADRPC);
    768 				mbufcp = mtod(mp, caddr_t);
    769 				len = mp->m_len;
    770 			}
    771 			xfer = (left > len) ? len : left;
    772 #ifdef notdef
    773 			/* Not Yet.. */
    774 			if (uiop->uio_iov->iov_op != NULL)
    775 				(*(uiop->uio_iov->iov_op))
    776 				(mbufcp, uiocp, xfer);
    777 			else
    778 #endif
    779 			if (uiop->uio_segflg == UIO_SYSSPACE)
    780 				bcopy(mbufcp, uiocp, xfer);
    781 			else
    782 				copyout(mbufcp, uiocp, xfer);
    783 			left -= xfer;
    784 			len -= xfer;
    785 			mbufcp += xfer;
    786 			uiocp += xfer;
    787 			uiop->uio_offset += xfer;
    788 			uiop->uio_resid -= xfer;
    789 		}
    790 		if (uiop->uio_iov->iov_len <= siz) {
    791 			uiop->uio_iovcnt--;
    792 			uiop->uio_iov++;
    793 		} else {
    794 			uiop->uio_iov->iov_base += uiosiz;
    795 			uiop->uio_iov->iov_len -= uiosiz;
    796 		}
    797 		siz -= uiosiz;
    798 	}
    799 	*dpos = mbufcp;
    800 	*mrep = mp;
    801 	if (rem > 0) {
    802 		if (len < rem)
    803 			error = nfs_adv(mrep, dpos, rem, len);
    804 		else
    805 			*dpos += rem;
    806 	}
    807 	return (error);
    808 }
    809 
    810 /*
    811  * copies a uio scatter/gather list to an mbuf chain...
    812  */
    813 int
    814 nfsm_uiotombuf(uiop, mq, siz, bpos)
    815 	register struct uio *uiop;
    816 	struct mbuf **mq;
    817 	int siz;
    818 	caddr_t *bpos;
    819 {
    820 	register char *uiocp;
    821 	register struct mbuf *mp, *mp2;
    822 	register int xfer, left, mlen;
    823 	int uiosiz, clflg, rem;
    824 	char *cp;
    825 
    826 	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
    827 		clflg = 1;
    828 	else
    829 		clflg = 0;
    830 	rem = nfsm_rndup(siz)-siz;
    831 	mp = mp2 = *mq;
    832 	while (siz > 0) {
    833 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
    834 			return (EINVAL);
    835 		left = uiop->uio_iov->iov_len;
    836 		uiocp = uiop->uio_iov->iov_base;
    837 		if (left > siz)
    838 			left = siz;
    839 		uiosiz = left;
    840 		while (left > 0) {
    841 			mlen = M_TRAILINGSPACE(mp);
    842 			if (mlen == 0) {
    843 				MGET(mp, M_WAIT, MT_DATA);
    844 				if (clflg)
    845 					MCLGET(mp, M_WAIT);
    846 				mp->m_len = 0;
    847 				mp2->m_next = mp;
    848 				mp2 = mp;
    849 				mlen = M_TRAILINGSPACE(mp);
    850 			}
    851 			xfer = (left > mlen) ? mlen : left;
    852 #ifdef notdef
    853 			/* Not Yet.. */
    854 			if (uiop->uio_iov->iov_op != NULL)
    855 				(*(uiop->uio_iov->iov_op))
    856 				(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
    857 			else
    858 #endif
    859 			if (uiop->uio_segflg == UIO_SYSSPACE)
    860 				bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
    861 			else
    862 				copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
    863 			mp->m_len += xfer;
    864 			left -= xfer;
    865 			uiocp += xfer;
    866 			uiop->uio_offset += xfer;
    867 			uiop->uio_resid -= xfer;
    868 		}
    869 		if (uiop->uio_iov->iov_len <= siz) {
    870 			uiop->uio_iovcnt--;
    871 			uiop->uio_iov++;
    872 		} else {
    873 			uiop->uio_iov->iov_base += uiosiz;
    874 			uiop->uio_iov->iov_len -= uiosiz;
    875 		}
    876 		siz -= uiosiz;
    877 	}
    878 	if (rem > 0) {
    879 		if (rem > M_TRAILINGSPACE(mp)) {
    880 			MGET(mp, M_WAIT, MT_DATA);
    881 			mp->m_len = 0;
    882 			mp2->m_next = mp;
    883 		}
    884 		cp = mtod(mp, caddr_t)+mp->m_len;
    885 		for (left = 0; left < rem; left++)
    886 			*cp++ = '\0';
    887 		mp->m_len += rem;
    888 		*bpos = cp;
    889 	} else
    890 		*bpos = mtod(mp, caddr_t)+mp->m_len;
    891 	*mq = mp;
    892 	return (0);
    893 }
    894 
    895 /*
    896  * Help break down an mbuf chain by setting the first siz bytes contiguous
    897  * pointed to by returned val.
    898  * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
    899  * cases. (The macros use the vars. dpos and dpos2)
    900  */
    901 int
    902 nfsm_disct(mdp, dposp, siz, left, cp2)
    903 	struct mbuf **mdp;
    904 	caddr_t *dposp;
    905 	int siz;
    906 	int left;
    907 	caddr_t *cp2;
    908 {
    909 	register struct mbuf *mp, *mp2;
    910 	register int siz2, xfer;
    911 	register caddr_t p;
    912 
    913 	mp = *mdp;
    914 	while (left == 0) {
    915 		*mdp = mp = mp->m_next;
    916 		if (mp == NULL)
    917 			return (EBADRPC);
    918 		left = mp->m_len;
    919 		*dposp = mtod(mp, caddr_t);
    920 	}
    921 	if (left >= siz) {
    922 		*cp2 = *dposp;
    923 		*dposp += siz;
    924 	} else if (mp->m_next == NULL) {
    925 		return (EBADRPC);
    926 	} else if (siz > MHLEN) {
    927 		panic("nfs S too big");
    928 	} else {
    929 		MGET(mp2, M_WAIT, MT_DATA);
    930 		mp2->m_next = mp->m_next;
    931 		mp->m_next = mp2;
    932 		mp->m_len -= left;
    933 		mp = mp2;
    934 		*cp2 = p = mtod(mp, caddr_t);
    935 		bcopy(*dposp, p, left);		/* Copy what was left */
    936 		siz2 = siz-left;
    937 		p += left;
    938 		mp2 = mp->m_next;
    939 		/* Loop around copying up the siz2 bytes */
    940 		while (siz2 > 0) {
    941 			if (mp2 == NULL)
    942 				return (EBADRPC);
    943 			xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
    944 			if (xfer > 0) {
    945 				bcopy(mtod(mp2, caddr_t), p, xfer);
    946 				NFSMADV(mp2, xfer);
    947 				mp2->m_len -= xfer;
    948 				p += xfer;
    949 				siz2 -= xfer;
    950 			}
    951 			if (siz2 > 0)
    952 				mp2 = mp2->m_next;
    953 		}
    954 		mp->m_len = siz;
    955 		*mdp = mp2;
    956 		*dposp = mtod(mp2, caddr_t);
    957 	}
    958 	return (0);
    959 }
    960 
    961 /*
    962  * Advance the position in the mbuf chain.
    963  */
    964 int
    965 nfs_adv(mdp, dposp, offs, left)
    966 	struct mbuf **mdp;
    967 	caddr_t *dposp;
    968 	int offs;
    969 	int left;
    970 {
    971 	register struct mbuf *m;
    972 	register int s;
    973 
    974 	m = *mdp;
    975 	s = left;
    976 	while (s < offs) {
    977 		offs -= s;
    978 		m = m->m_next;
    979 		if (m == NULL)
    980 			return (EBADRPC);
    981 		s = m->m_len;
    982 	}
    983 	*mdp = m;
    984 	*dposp = mtod(m, caddr_t)+offs;
    985 	return (0);
    986 }
    987 
    988 /*
    989  * Copy a string into mbufs for the hard cases...
    990  */
    991 int
    992 nfsm_strtmbuf(mb, bpos, cp, siz)
    993 	struct mbuf **mb;
    994 	char **bpos;
    995 	char *cp;
    996 	long siz;
    997 {
    998 	register struct mbuf *m1 = NULL, *m2;
    999 	long left, xfer, len, tlen;
   1000 	u_int32_t *tl;
   1001 	int putsize;
   1002 
   1003 	putsize = 1;
   1004 	m2 = *mb;
   1005 	left = M_TRAILINGSPACE(m2);
   1006 	if (left > 0) {
   1007 		tl = ((u_int32_t *)(*bpos));
   1008 		*tl++ = txdr_unsigned(siz);
   1009 		putsize = 0;
   1010 		left -= NFSX_UNSIGNED;
   1011 		m2->m_len += NFSX_UNSIGNED;
   1012 		if (left > 0) {
   1013 			bcopy(cp, (caddr_t) tl, left);
   1014 			siz -= left;
   1015 			cp += left;
   1016 			m2->m_len += left;
   1017 			left = 0;
   1018 		}
   1019 	}
   1020 	/* Loop around adding mbufs */
   1021 	while (siz > 0) {
   1022 		MGET(m1, M_WAIT, MT_DATA);
   1023 		if (siz > MLEN)
   1024 			MCLGET(m1, M_WAIT);
   1025 		m1->m_len = NFSMSIZ(m1);
   1026 		m2->m_next = m1;
   1027 		m2 = m1;
   1028 		tl = mtod(m1, u_int32_t *);
   1029 		tlen = 0;
   1030 		if (putsize) {
   1031 			*tl++ = txdr_unsigned(siz);
   1032 			m1->m_len -= NFSX_UNSIGNED;
   1033 			tlen = NFSX_UNSIGNED;
   1034 			putsize = 0;
   1035 		}
   1036 		if (siz < m1->m_len) {
   1037 			len = nfsm_rndup(siz);
   1038 			xfer = siz;
   1039 			if (xfer < len)
   1040 				*(tl+(xfer>>2)) = 0;
   1041 		} else {
   1042 			xfer = len = m1->m_len;
   1043 		}
   1044 		bcopy(cp, (caddr_t) tl, xfer);
   1045 		m1->m_len = len+tlen;
   1046 		siz -= xfer;
   1047 		cp += xfer;
   1048 	}
   1049 	*mb = m1;
   1050 	*bpos = mtod(m1, caddr_t)+m1->m_len;
   1051 	return (0);
   1052 }
   1053 
   1054 /*
   1055  * Called once to initialize data structures...
   1056  */
   1057 void
   1058 nfs_init()
   1059 {
   1060 	register int i;
   1061 
   1062 #if !defined(alpha) && defined(DIAGNOSTIC)
   1063 	/*
   1064 	 * Check to see if major data structures haven't bloated.
   1065 	 */
   1066 	if (sizeof (struct nfsnode) > NFS_NODEALLOC) {
   1067 		printf("struct nfsnode bloated (> %dbytes)\n", NFS_NODEALLOC);
   1068 		printf("Try reducing NFS_SMALLFH\n");
   1069 	}
   1070 	if (sizeof (struct nfsmount) > NFS_MNTALLOC) {
   1071 		printf("struct nfsmount bloated (> %dbytes)\n", NFS_MNTALLOC);
   1072 		printf("Try reducing NFS_MUIDHASHSIZ\n");
   1073 	}
   1074 	if (sizeof (struct nfssvc_sock) > NFS_SVCALLOC) {
   1075 		printf("struct nfssvc_sock bloated (> %dbytes)\n",NFS_SVCALLOC);
   1076 		printf("Try reducing NFS_UIDHASHSIZ\n");
   1077 	}
   1078 	if (sizeof (struct nfsuid) > NFS_UIDALLOC) {
   1079 		printf("struct nfsuid bloated (> %dbytes)\n",NFS_UIDALLOC);
   1080 		printf("Try unionizing the nu_nickname and nu_flag fields\n");
   1081 	}
   1082 #endif
   1083 
   1084 	nfsrtt.pos = 0;
   1085 	rpc_vers = txdr_unsigned(RPC_VER2);
   1086 	rpc_call = txdr_unsigned(RPC_CALL);
   1087 	rpc_reply = txdr_unsigned(RPC_REPLY);
   1088 	rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
   1089 	rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
   1090 	rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
   1091 	rpc_autherr = txdr_unsigned(RPC_AUTHERR);
   1092 	rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
   1093 	rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
   1094 	nfs_prog = txdr_unsigned(NFS_PROG);
   1095 	nqnfs_prog = txdr_unsigned(NQNFS_PROG);
   1096 	nfs_true = txdr_unsigned(TRUE);
   1097 	nfs_false = txdr_unsigned(FALSE);
   1098 	nfs_xdrneg1 = txdr_unsigned(-1);
   1099 	nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
   1100 	if (nfs_ticks < 1)
   1101 		nfs_ticks = 1;
   1102 #ifdef NFSCLIENT
   1103 	/* Ensure async daemons disabled */
   1104 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
   1105 		nfs_iodwant[i] = (struct proc *)0;
   1106 	TAILQ_INIT(&nfs_bufq);
   1107 	nfs_nhinit();			/* Init the nfsnode table */
   1108 #endif /* NFSCLIENT */
   1109 #ifdef NFSSERVER
   1110 	nfsrv_init(0);			/* Init server data structures */
   1111 	nfsrv_initcache();		/* Init the server request cache */
   1112 #endif /* NFSSERVER */
   1113 
   1114 	/*
   1115 	 * Initialize the nqnfs server stuff.
   1116 	 */
   1117 	if (nqnfsstarttime == 0) {
   1118 		nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
   1119 			+ nqsrv_clockskew + nqsrv_writeslack;
   1120 		NQLOADNOVRAM(nqnfsstarttime);
   1121 		CIRCLEQ_INIT(&nqtimerhead);
   1122 		nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash);
   1123 	}
   1124 
   1125 	/*
   1126 	 * Initialize reply list and start timer
   1127 	 */
   1128 	TAILQ_INIT(&nfs_reqq);
   1129 	nfs_timer(NULL);
   1130 }
   1131 
   1132 #ifdef NFSCLIENT
   1133 /*
   1134  * Attribute cache routines.
   1135  * nfs_loadattrcache() - loads or updates the cache contents from attributes
   1136  *	that are on the mbuf list
   1137  * nfs_getattrcache() - returns valid attributes if found in cache, returns
   1138  *	error otherwise
   1139  */
   1140 
   1141 /*
   1142  * Load the attribute cache (that lives in the nfsnode entry) with
   1143  * the values on the mbuf list and
   1144  * Iff vap not NULL
   1145  *    copy the attributes to *vaper
   1146  */
   1147 int
   1148 nfs_loadattrcache(vpp, mdp, dposp, vaper)
   1149 	struct vnode **vpp;
   1150 	struct mbuf **mdp;
   1151 	caddr_t *dposp;
   1152 	struct vattr *vaper;
   1153 {
   1154 	register struct vnode *vp = *vpp;
   1155 	register struct vattr *vap;
   1156 	register struct nfs_fattr *fp;
   1157 	extern int (**spec_nfsv2nodeop_p) __P((void *));
   1158 	register struct nfsnode *np;
   1159 	register int32_t t1;
   1160 	caddr_t cp2;
   1161 	int error = 0;
   1162 	int32_t rdev;
   1163 	struct mbuf *md;
   1164 	enum vtype vtyp;
   1165 	u_short vmode;
   1166 	struct timespec mtime;
   1167 	struct vnode *nvp;
   1168 	int v3 = NFS_ISV3(vp);
   1169 
   1170 	md = *mdp;
   1171 	t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
   1172 	error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2);
   1173 	if (error)
   1174 		return (error);
   1175 	fp = (struct nfs_fattr *)cp2;
   1176 	if (v3) {
   1177 		vtyp = nfsv3tov_type(fp->fa_type);
   1178 		vmode = fxdr_unsigned(u_short, fp->fa_mode);
   1179 		rdev = makedev(fxdr_unsigned(u_char, fp->fa3_rdev.specdata1),
   1180 			fxdr_unsigned(u_char, fp->fa3_rdev.specdata2));
   1181 		fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
   1182 	} else {
   1183 		vtyp = nfsv2tov_type(fp->fa_type);
   1184 		vmode = fxdr_unsigned(u_short, fp->fa_mode);
   1185 		if (vtyp == VNON || vtyp == VREG)
   1186 			vtyp = IFTOVT(vmode);
   1187 		rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
   1188 		fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
   1189 
   1190 		/*
   1191 		 * Really ugly NFSv2 kludge.
   1192 		 */
   1193 		if (vtyp == VCHR && rdev == 0xffffffff)
   1194 			vtyp = VFIFO;
   1195 	}
   1196 
   1197 	/*
   1198 	 * If v_type == VNON it is a new node, so fill in the v_type,
   1199 	 * n_mtime fields. Check to see if it represents a special
   1200 	 * device, and if so, check for a possible alias. Once the
   1201 	 * correct vnode has been obtained, fill in the rest of the
   1202 	 * information.
   1203 	 */
   1204 	np = VTONFS(vp);
   1205 	if (vp->v_type == VNON) {
   1206 		vp->v_type = vtyp;
   1207 		if (vp->v_type == VFIFO) {
   1208 			extern int (**fifo_nfsv2nodeop_p) __P((void *));
   1209 			vp->v_op = fifo_nfsv2nodeop_p;
   1210 		}
   1211 		if (vp->v_type == VCHR || vp->v_type == VBLK) {
   1212 			vp->v_op = spec_nfsv2nodeop_p;
   1213 			nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
   1214 			if (nvp) {
   1215 				/*
   1216 				 * Discard unneeded vnode, but save its nfsnode.
   1217 				 * Since the nfsnode does not have a lock, its
   1218 				 * vnode lock has to be carried over.
   1219 				 */
   1220 #ifdef Lite2_integrated
   1221 				nvp->v_vnlock = vp->v_vnlock;
   1222 				vp->v_vnlock = NULL;
   1223 #endif
   1224 				nvp->v_data = vp->v_data;
   1225 				vp->v_data = NULL;
   1226 				vp->v_op = spec_vnodeop_p;
   1227 				vrele(vp);
   1228 				vgone(vp);
   1229 				/*
   1230 				 * Reinitialize aliased node.
   1231 				 */
   1232 				np->n_vnode = nvp;
   1233 				*vpp = vp = nvp;
   1234 			}
   1235 		}
   1236 		np->n_mtime = mtime.tv_sec;
   1237 	}
   1238 	vap = &np->n_vattr;
   1239 	vap->va_type = vtyp;
   1240 	vap->va_mode = (vmode & 07777);
   1241 	vap->va_rdev = (dev_t)rdev;
   1242 	vap->va_mtime = mtime;
   1243 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
   1244 	if (v3) {
   1245 		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
   1246 		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
   1247 		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
   1248 		fxdr_hyper(&fp->fa3_size, &vap->va_size);
   1249 		vap->va_blocksize = NFS_FABLKSIZE;
   1250 		fxdr_hyper(&fp->fa3_used, &vap->va_bytes);
   1251 		vap->va_fileid = fxdr_unsigned(int32_t,
   1252 		    fp->fa3_fileid.nfsuquad[1]);
   1253 		fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
   1254 		fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
   1255 		vap->va_flags = 0;
   1256 		vap->va_filerev = 0;
   1257 	} else {
   1258 		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
   1259 		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
   1260 		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
   1261 		vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
   1262 		vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
   1263 		vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks)
   1264 		    * NFS_FABLKSIZE;
   1265 		vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
   1266 		fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
   1267 		vap->va_flags = 0;
   1268 		vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
   1269 		    fp->fa2_ctime.nfsv2_sec);
   1270 		vap->va_ctime.tv_nsec = 0;
   1271 		vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
   1272 		vap->va_filerev = 0;
   1273 	}
   1274 	if (vap->va_size != np->n_size) {
   1275 		if (vap->va_type == VREG) {
   1276 			if (np->n_flag & NMODIFIED) {
   1277 				if (vap->va_size < np->n_size)
   1278 					vap->va_size = np->n_size;
   1279 				else
   1280 					np->n_size = vap->va_size;
   1281 			} else
   1282 				np->n_size = vap->va_size;
   1283 			vnode_pager_setsize(vp, (u_long)np->n_size);
   1284 		} else
   1285 			np->n_size = vap->va_size;
   1286 	}
   1287 	np->n_attrstamp = time.tv_sec;
   1288 	if (vaper != NULL) {
   1289 		bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
   1290 		if (np->n_flag & NCHG) {
   1291 			if (np->n_flag & NACC)
   1292 				vaper->va_atime = np->n_atim;
   1293 			if (np->n_flag & NUPD)
   1294 				vaper->va_mtime = np->n_mtim;
   1295 		}
   1296 	}
   1297 	return (0);
   1298 }
   1299 
   1300 /*
   1301  * Check the time stamp
   1302  * If the cache is valid, copy contents to *vap and return 0
   1303  * otherwise return an error
   1304  */
   1305 int
   1306 nfs_getattrcache(vp, vaper)
   1307 	register struct vnode *vp;
   1308 	struct vattr *vaper;
   1309 {
   1310 	register struct nfsnode *np = VTONFS(vp);
   1311 	register struct vattr *vap;
   1312 
   1313 	if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(np)) {
   1314 		nfsstats.attrcache_misses++;
   1315 		return (ENOENT);
   1316 	}
   1317 	nfsstats.attrcache_hits++;
   1318 	vap = &np->n_vattr;
   1319 	if (vap->va_size != np->n_size) {
   1320 		if (vap->va_type == VREG) {
   1321 			if (np->n_flag & NMODIFIED) {
   1322 				if (vap->va_size < np->n_size)
   1323 					vap->va_size = np->n_size;
   1324 				else
   1325 					np->n_size = vap->va_size;
   1326 			} else
   1327 				np->n_size = vap->va_size;
   1328 			vnode_pager_setsize(vp, (u_long)np->n_size);
   1329 		} else
   1330 			np->n_size = vap->va_size;
   1331 	}
   1332 	bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
   1333 	if (np->n_flag & NCHG) {
   1334 		if (np->n_flag & NACC)
   1335 			vaper->va_atime = np->n_atim;
   1336 		if (np->n_flag & NUPD)
   1337 			vaper->va_mtime = np->n_mtim;
   1338 	}
   1339 	return (0);
   1340 }
   1341 #endif /* NFSCLIENT */
   1342 
   1343 /*
   1344  * Set up nameidata for a lookup() call and do it
   1345  */
   1346 int
   1347 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag)
   1348 	register struct nameidata *ndp;
   1349 	fhandle_t *fhp;
   1350 	int len;
   1351 	struct nfssvc_sock *slp;
   1352 	struct mbuf *nam;
   1353 	struct mbuf **mdp;
   1354 	caddr_t *dposp;
   1355 	struct vnode **retdirp;
   1356 	struct proc *p;
   1357 	int kerbflag;
   1358 {
   1359 	register int i, rem;
   1360 	register struct mbuf *md;
   1361 	register char *fromcp, *tocp;
   1362 	struct vnode *dp;
   1363 	int error, rdonly;
   1364 	struct componentname *cnp = &ndp->ni_cnd;
   1365 
   1366 	*retdirp = (struct vnode *)0;
   1367 	MALLOC(cnp->cn_pnbuf, char *, len + 1, M_NAMEI, M_WAITOK);
   1368 	/*
   1369 	 * Copy the name from the mbuf list to ndp->ni_pnbuf
   1370 	 * and set the various ndp fields appropriately.
   1371 	 */
   1372 	fromcp = *dposp;
   1373 	tocp = cnp->cn_pnbuf;
   1374 	md = *mdp;
   1375 	rem = mtod(md, caddr_t) + md->m_len - fromcp;
   1376 	for (i = 0; i < len; i++) {
   1377 		while (rem == 0) {
   1378 			md = md->m_next;
   1379 			if (md == NULL) {
   1380 				error = EBADRPC;
   1381 				goto out;
   1382 			}
   1383 			fromcp = mtod(md, caddr_t);
   1384 			rem = md->m_len;
   1385 		}
   1386 		if (*fromcp == '\0' || *fromcp == '/') {
   1387 			error = EACCES;
   1388 			goto out;
   1389 		}
   1390 		*tocp++ = *fromcp++;
   1391 		rem--;
   1392 	}
   1393 	*tocp = '\0';
   1394 	*mdp = md;
   1395 	*dposp = fromcp;
   1396 	len = nfsm_rndup(len)-len;
   1397 	if (len > 0) {
   1398 		if (rem >= len)
   1399 			*dposp += len;
   1400 		else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
   1401 			goto out;
   1402 	}
   1403 	ndp->ni_pathlen = tocp - cnp->cn_pnbuf;
   1404 	cnp->cn_nameptr = cnp->cn_pnbuf;
   1405 	/*
   1406 	 * Extract and set starting directory.
   1407 	 */
   1408 	error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
   1409 	    nam, &rdonly, kerbflag);
   1410 	if (error)
   1411 		goto out;
   1412 	if (dp->v_type != VDIR) {
   1413 		vrele(dp);
   1414 		error = ENOTDIR;
   1415 		goto out;
   1416 	}
   1417 	VREF(dp);
   1418 	*retdirp = dp;
   1419 	ndp->ni_startdir = dp;
   1420 	if (rdonly)
   1421 		cnp->cn_flags |= (NOCROSSMOUNT | RDONLY);
   1422 	else
   1423 		cnp->cn_flags |= NOCROSSMOUNT;
   1424 	/*
   1425 	 * And call lookup() to do the real work
   1426 	 */
   1427 	cnp->cn_proc = p;
   1428 	error = lookup(ndp);
   1429 	if (error)
   1430 		goto out;
   1431 	/*
   1432 	 * Check for encountering a symbolic link
   1433 	 */
   1434 	if (cnp->cn_flags & ISSYMLINK) {
   1435 		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
   1436 			vput(ndp->ni_dvp);
   1437 		else
   1438 			vrele(ndp->ni_dvp);
   1439 		vput(ndp->ni_vp);
   1440 		ndp->ni_vp = NULL;
   1441 		error = EINVAL;
   1442 		goto out;
   1443 	}
   1444 	/*
   1445 	 * Check for saved name request
   1446 	 */
   1447 	if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
   1448 		cnp->cn_flags |= HASBUF;
   1449 		return (0);
   1450 	}
   1451 out:
   1452 	FREE(cnp->cn_pnbuf, M_NAMEI);
   1453 	return (error);
   1454 }
   1455 
   1456 /*
   1457  * A fiddled version of m_adj() that ensures null fill to a long
   1458  * boundary and only trims off the back end
   1459  */
   1460 void
   1461 nfsm_adj(mp, len, nul)
   1462 	struct mbuf *mp;
   1463 	register int len;
   1464 	int nul;
   1465 {
   1466 	register struct mbuf *m;
   1467 	register int count, i;
   1468 	register char *cp;
   1469 
   1470 	/*
   1471 	 * Trim from tail.  Scan the mbuf chain,
   1472 	 * calculating its length and finding the last mbuf.
   1473 	 * If the adjustment only affects this mbuf, then just
   1474 	 * adjust and return.  Otherwise, rescan and truncate
   1475 	 * after the remaining size.
   1476 	 */
   1477 	count = 0;
   1478 	m = mp;
   1479 	for (;;) {
   1480 		count += m->m_len;
   1481 		if (m->m_next == (struct mbuf *)0)
   1482 			break;
   1483 		m = m->m_next;
   1484 	}
   1485 	if (m->m_len > len) {
   1486 		m->m_len -= len;
   1487 		if (nul > 0) {
   1488 			cp = mtod(m, caddr_t)+m->m_len-nul;
   1489 			for (i = 0; i < nul; i++)
   1490 				*cp++ = '\0';
   1491 		}
   1492 		return;
   1493 	}
   1494 	count -= len;
   1495 	if (count < 0)
   1496 		count = 0;
   1497 	/*
   1498 	 * Correct length for chain is "count".
   1499 	 * Find the mbuf with last data, adjust its length,
   1500 	 * and toss data from remaining mbufs on chain.
   1501 	 */
   1502 	for (m = mp; m; m = m->m_next) {
   1503 		if (m->m_len >= count) {
   1504 			m->m_len = count;
   1505 			if (nul > 0) {
   1506 				cp = mtod(m, caddr_t)+m->m_len-nul;
   1507 				for (i = 0; i < nul; i++)
   1508 					*cp++ = '\0';
   1509 			}
   1510 			break;
   1511 		}
   1512 		count -= m->m_len;
   1513 	}
   1514 	for (m = m->m_next;m;m = m->m_next)
   1515 		m->m_len = 0;
   1516 }
   1517 
   1518 /*
   1519  * Make these functions instead of macros, so that the kernel text size
   1520  * doesn't get too big...
   1521  */
   1522 void
   1523 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
   1524 	struct nfsrv_descript *nfsd;
   1525 	int before_ret;
   1526 	register struct vattr *before_vap;
   1527 	int after_ret;
   1528 	struct vattr *after_vap;
   1529 	struct mbuf **mbp;
   1530 	char **bposp;
   1531 {
   1532 	register struct mbuf *mb = *mbp, *mb2;
   1533 	register char *bpos = *bposp;
   1534 	register u_int32_t *tl;
   1535 
   1536 	if (before_ret) {
   1537 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1538 		*tl = nfs_false;
   1539 	} else {
   1540 		nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
   1541 		*tl++ = nfs_true;
   1542 		txdr_hyper(&(before_vap->va_size), tl);
   1543 		tl += 2;
   1544 		txdr_nfsv3time(&(before_vap->va_mtime), tl);
   1545 		tl += 2;
   1546 		txdr_nfsv3time(&(before_vap->va_ctime), tl);
   1547 	}
   1548 	*bposp = bpos;
   1549 	*mbp = mb;
   1550 	nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
   1551 }
   1552 
   1553 void
   1554 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
   1555 	struct nfsrv_descript *nfsd;
   1556 	int after_ret;
   1557 	struct vattr *after_vap;
   1558 	struct mbuf **mbp;
   1559 	char **bposp;
   1560 {
   1561 	register struct mbuf *mb = *mbp, *mb2;
   1562 	register char *bpos = *bposp;
   1563 	register u_int32_t *tl;
   1564 	register struct nfs_fattr *fp;
   1565 
   1566 	if (after_ret) {
   1567 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1568 		*tl = nfs_false;
   1569 	} else {
   1570 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
   1571 		*tl++ = nfs_true;
   1572 		fp = (struct nfs_fattr *)tl;
   1573 		nfsm_srvfattr(nfsd, after_vap, fp);
   1574 	}
   1575 	*mbp = mb;
   1576 	*bposp = bpos;
   1577 }
   1578 
   1579 void
   1580 nfsm_srvfattr(nfsd, vap, fp)
   1581 	register struct nfsrv_descript *nfsd;
   1582 	register struct vattr *vap;
   1583 	register struct nfs_fattr *fp;
   1584 {
   1585 
   1586 	fp->fa_nlink = txdr_unsigned(vap->va_nlink);
   1587 	fp->fa_uid = txdr_unsigned(vap->va_uid);
   1588 	fp->fa_gid = txdr_unsigned(vap->va_gid);
   1589 	if (nfsd->nd_flag & ND_NFSV3) {
   1590 		fp->fa_type = vtonfsv3_type(vap->va_type);
   1591 		fp->fa_mode = vtonfsv3_mode(vap->va_mode);
   1592 		txdr_hyper(&vap->va_size, &fp->fa3_size);
   1593 		txdr_hyper(&vap->va_bytes, &fp->fa3_used);
   1594 		fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
   1595 		fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
   1596 		fp->fa3_fsid.nfsuquad[0] = 0;
   1597 		fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
   1598 		fp->fa3_fileid.nfsuquad[0] = 0;
   1599 		fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
   1600 		txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
   1601 		txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
   1602 		txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
   1603 	} else {
   1604 		fp->fa_type = vtonfsv2_type(vap->va_type);
   1605 		fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
   1606 		fp->fa2_size = txdr_unsigned(vap->va_size);
   1607 		fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
   1608 		if (vap->va_type == VFIFO)
   1609 			fp->fa2_rdev = 0xffffffff;
   1610 		else
   1611 			fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
   1612 		fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
   1613 		fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
   1614 		fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
   1615 		txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
   1616 		txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
   1617 		txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
   1618 	}
   1619 }
   1620 
   1621 /*
   1622  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
   1623  * 	- look up fsid in mount list (if not found ret error)
   1624  *	- get vp and export rights by calling VFS_FHTOVP()
   1625  *	- if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
   1626  *	- if not lockflag unlock it with VOP_UNLOCK()
   1627  */
   1628 int
   1629 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag)
   1630 	fhandle_t *fhp;
   1631 	int lockflag;
   1632 	struct vnode **vpp;
   1633 	struct ucred *cred;
   1634 	struct nfssvc_sock *slp;
   1635 	struct mbuf *nam;
   1636 	int *rdonlyp;
   1637 	int kerbflag;
   1638 {
   1639 #ifdef Lite2_integrated
   1640 	struct proc *p = curproc;	/* XXX */
   1641 #endif
   1642 	register struct mount *mp;
   1643 	register int i;
   1644 	struct ucred *credanon;
   1645 	int error, exflags;
   1646 
   1647 	*vpp = (struct vnode *)0;
   1648 #ifdef Lite2_integrated
   1649 	mp = vfs_getvfs(&fhp->fh_fsid);
   1650 #else
   1651 	mp = getvfs(&fhp->fh_fsid);
   1652 #endif
   1653 	if (!mp)
   1654 		return (ESTALE);
   1655 	error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon);
   1656 	if (error)
   1657 		return (error);
   1658 	/*
   1659 	 * Check/setup credentials.
   1660 	 */
   1661 	if (exflags & MNT_EXKERB) {
   1662 		if (!kerbflag) {
   1663 			vput(*vpp);
   1664 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
   1665 		}
   1666 	} else if (kerbflag) {
   1667 		vput(*vpp);
   1668 		return (NFSERR_AUTHERR | AUTH_TOOWEAK);
   1669 	} else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
   1670 		cred->cr_uid = credanon->cr_uid;
   1671 		cred->cr_gid = credanon->cr_gid;
   1672 		for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
   1673 			cred->cr_groups[i] = credanon->cr_groups[i];
   1674 		cred->cr_ngroups = i;
   1675 	}
   1676 	if (exflags & MNT_EXRDONLY)
   1677 		*rdonlyp = 1;
   1678 	else
   1679 		*rdonlyp = 0;
   1680 	if (!lockflag)
   1681 #ifdef Lite2_integrated
   1682 		VOP_UNLOCK(*vpp, 0, p);
   1683 #else
   1684 		VOP_UNLOCK(*vpp);
   1685 #endif
   1686 	return (0);
   1687 }
   1688 
   1689 /*
   1690  * This function compares two net addresses by family and returns TRUE
   1691  * if they are the same host.
   1692  * If there is any doubt, return FALSE.
   1693  * The AF_INET family is handled as a special case so that address mbufs
   1694  * don't need to be saved to store "struct in_addr", which is only 4 bytes.
   1695  */
   1696 int
   1697 netaddr_match(family, haddr, nam)
   1698 	int family;
   1699 	union nethostaddr *haddr;
   1700 	struct mbuf *nam;
   1701 {
   1702 	register struct sockaddr_in *inetaddr;
   1703 
   1704 	switch (family) {
   1705 	case AF_INET:
   1706 		inetaddr = mtod(nam, struct sockaddr_in *);
   1707 		if (inetaddr->sin_family == AF_INET &&
   1708 		    inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
   1709 			return (1);
   1710 		break;
   1711 #ifdef ISO
   1712 	case AF_ISO:
   1713 	    {
   1714 		register struct sockaddr_iso *isoaddr1, *isoaddr2;
   1715 
   1716 		isoaddr1 = mtod(nam, struct sockaddr_iso *);
   1717 		isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
   1718 		if (isoaddr1->siso_family == AF_ISO &&
   1719 		    isoaddr1->siso_nlen > 0 &&
   1720 		    isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
   1721 		    SAME_ISOADDR(isoaddr1, isoaddr2))
   1722 			return (1);
   1723 		break;
   1724 	    }
   1725 #endif	/* ISO */
   1726 	default:
   1727 		break;
   1728 	};
   1729 	return (0);
   1730 }
   1731 
   1732 static nfsuint64 nfs_nullcookie = {{ 0, 0 }};
   1733 /*
   1734  * This function finds the directory cookie that corresponds to the
   1735  * logical byte offset given.
   1736  */
   1737 nfsuint64 *
   1738 nfs_getcookie(np, off, add)
   1739 	register struct nfsnode *np;
   1740 	off_t off;
   1741 	int add;
   1742 {
   1743 	register struct nfsdmap *dp, *dp2;
   1744 	register int pos;
   1745 
   1746 	pos = off / NFS_DIRBLKSIZ;
   1747 	if (pos == 0) {
   1748 #ifdef DIAGNOSTIC
   1749 		if (add)
   1750 			panic("nfs getcookie add at 0");
   1751 #endif
   1752 		return (&nfs_nullcookie);
   1753 	}
   1754 	pos--;
   1755 	dp = np->n_cookies.lh_first;
   1756 	if (!dp) {
   1757 		if (add) {
   1758 			MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
   1759 				M_NFSDIROFF, M_WAITOK);
   1760 			dp->ndm_eocookie = 0;
   1761 			LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
   1762 		} else
   1763 			return ((nfsuint64 *)0);
   1764 	}
   1765 	while (pos >= NFSNUMCOOKIES) {
   1766 		pos -= NFSNUMCOOKIES;
   1767 		if (dp->ndm_list.le_next) {
   1768 			if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
   1769 				pos >= dp->ndm_eocookie)
   1770 				return ((nfsuint64 *)0);
   1771 			dp = dp->ndm_list.le_next;
   1772 		} else if (add) {
   1773 			MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
   1774 				M_NFSDIROFF, M_WAITOK);
   1775 			dp2->ndm_eocookie = 0;
   1776 			LIST_INSERT_AFTER(dp, dp2, ndm_list);
   1777 			dp = dp2;
   1778 		} else
   1779 			return ((nfsuint64 *)0);
   1780 	}
   1781 	if (pos >= dp->ndm_eocookie) {
   1782 		if (add)
   1783 			dp->ndm_eocookie = pos + 1;
   1784 		else
   1785 			return ((nfsuint64 *)0);
   1786 	}
   1787 	return (&dp->ndm_cookies[pos]);
   1788 }
   1789 
   1790 /*
   1791  * Invalidate cached directory information, except for the actual directory
   1792  * blocks (which are invalidated separately).
   1793  * Done mainly to avoid the use of stale offset cookies.
   1794  */
   1795 void
   1796 nfs_invaldir(vp)
   1797 	register struct vnode *vp;
   1798 {
   1799 #ifdef notdef /* XXX */
   1800 	register struct nfsnode *np = VTONFS(vp);
   1801 
   1802 #ifdef DIAGNOSTIC
   1803 	if (vp->v_type != VDIR)
   1804 		panic("nfs: invaldir not dir");
   1805 #endif
   1806 	np->n_direofoffset = 0;
   1807 	np->n_cookieverf.nfsuquad[0] = 0;
   1808 	np->n_cookieverf.nfsuquad[1] = 0;
   1809 	if (np->n_cookies.lh_first)
   1810 		np->n_cookies.lh_first->ndm_eocookie = 0;
   1811 #endif
   1812 }
   1813 
   1814 /*
   1815  * The write verifier has changed (probably due to a server reboot), so all
   1816  * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
   1817  * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
   1818  * flag. Once done the new write verifier can be set for the mount point.
   1819  */
   1820 void
   1821 nfs_clearcommit(mp)
   1822 	struct mount *mp;
   1823 {
   1824 	register struct vnode *vp, *nvp;
   1825 	register struct buf *bp, *nbp;
   1826 	int s;
   1827 
   1828 	s = splbio();
   1829 loop:
   1830 	for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
   1831 		if (vp->v_mount != mp)	/* Paranoia */
   1832 			goto loop;
   1833 		nvp = vp->v_mntvnodes.le_next;
   1834 		for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
   1835 			nbp = bp->b_vnbufs.le_next;
   1836 			if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
   1837 				== (B_DELWRI | B_NEEDCOMMIT))
   1838 				bp->b_flags &= ~B_NEEDCOMMIT;
   1839 		}
   1840 	}
   1841 	splx(s);
   1842 }
   1843 
   1844 /*
   1845  * Map errnos to NFS error numbers. For Version 3 also filter out error
   1846  * numbers not specified for the associated procedure.
   1847  */
   1848 int
   1849 nfsrv_errmap(nd, err)
   1850 	struct nfsrv_descript *nd;
   1851 	register int err;
   1852 {
   1853 	register short *defaulterrp, *errp;
   1854 
   1855 	if (nd->nd_flag & ND_NFSV3) {
   1856 	    if (nd->nd_procnum <= NFSPROC_COMMIT) {
   1857 		errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
   1858 		while (*++errp) {
   1859 			if (*errp == err)
   1860 				return (err);
   1861 			else if (*errp > err)
   1862 				break;
   1863 		}
   1864 		return ((int)*defaulterrp);
   1865 	    } else
   1866 		return (err & 0xffff);
   1867 	}
   1868 	if (err <= ELAST)
   1869 		return ((int)nfsrv_v2errmap[err - 1]);
   1870 	return (NFSERR_IO);
   1871 }
   1872 
   1873 /*
   1874  * Sort the group list in increasing numerical order.
   1875  * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
   1876  *  that used to be here.)
   1877  */
   1878 void
   1879 nfsrvw_sort(list, num)
   1880         register gid_t *list;
   1881         register int num;
   1882 {
   1883 	register int i, j;
   1884 	gid_t v;
   1885 
   1886 	/* Insertion sort. */
   1887 	for (i = 1; i < num; i++) {
   1888 		v = list[i];
   1889 		/* find correct slot for value v, moving others up */
   1890 		for (j = i; --j >= 0 && v < list[j];)
   1891 			list[j + 1] = list[j];
   1892 		list[j + 1] = v;
   1893 	}
   1894 }
   1895 
   1896 /*
   1897  * copy credentials making sure that the result can be compared with bcmp().
   1898  */
   1899 void
   1900 nfsrv_setcred(incred, outcred)
   1901 	register struct ucred *incred, *outcred;
   1902 {
   1903 	register int i;
   1904 
   1905 	bzero((caddr_t)outcred, sizeof (struct ucred));
   1906 	outcred->cr_ref = 1;
   1907 	outcred->cr_uid = incred->cr_uid;
   1908 	outcred->cr_gid = incred->cr_gid;
   1909 	outcred->cr_ngroups = incred->cr_ngroups;
   1910 	for (i = 0; i < incred->cr_ngroups; i++)
   1911 		outcred->cr_groups[i] = incred->cr_groups[i];
   1912 	nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
   1913 }
   1914