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