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