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