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