Home | History | Annotate | Line # | Download | only in nfs
nfs_subs.c revision 1.202.2.3
      1 /*	$NetBSD: nfs_subs.c,v 1.202.2.3 2008/10/10 22:35:43 skrll 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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)nfs_subs.c	8.8 (Berkeley) 5/22/95
     35  */
     36 
     37 /*
     38  * Copyright 2000 Wasabi Systems, Inc.
     39  * All rights reserved.
     40  *
     41  * Written by Frank van der Linden for Wasabi Systems, Inc.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *      This product includes software developed for the NetBSD Project by
     54  *      Wasabi Systems, Inc.
     55  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     56  *    or promote products derived from this software without specific prior
     57  *    written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     61  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     62  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     63  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     64  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     65  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     66  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     67  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     68  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     69  * POSSIBILITY OF SUCH DAMAGE.
     70  */
     71 
     72 #include <sys/cdefs.h>
     73 __KERNEL_RCSID(0, "$NetBSD: nfs_subs.c,v 1.202.2.3 2008/10/10 22:35:43 skrll Exp $");
     74 
     75 #include "fs_nfs.h"
     76 #include "opt_nfs.h"
     77 #include "opt_nfsserver.h"
     78 #include "opt_iso.h"
     79 #include "opt_inet.h"
     80 
     81 /*
     82  * These functions support the macros and help fiddle mbuf chains for
     83  * the nfs op functions. They do things like create the rpc header and
     84  * copy data between mbuf chains and uio lists.
     85  */
     86 #include <sys/param.h>
     87 #include <sys/proc.h>
     88 #include <sys/systm.h>
     89 #include <sys/kernel.h>
     90 #include <sys/kmem.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/filedesc.h>
     98 #include <sys/time.h>
     99 #include <sys/dirent.h>
    100 #include <sys/once.h>
    101 #include <sys/kauth.h>
    102 #include <sys/atomic.h>
    103 
    104 #include <uvm/uvm_extern.h>
    105 
    106 #include <nfs/rpcv2.h>
    107 #include <nfs/nfsproto.h>
    108 #include <nfs/nfsnode.h>
    109 #include <nfs/nfs.h>
    110 #include <nfs/xdr_subs.h>
    111 #include <nfs/nfsm_subs.h>
    112 #include <nfs/nfsmount.h>
    113 #include <nfs/nfsrtt.h>
    114 #include <nfs/nfs_var.h>
    115 
    116 #include <miscfs/specfs/specdev.h>
    117 
    118 #include <netinet/in.h>
    119 #ifdef ISO
    120 #include <netiso/iso.h>
    121 #endif
    122 
    123 static u_int32_t nfs_xid;
    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, nfs_true, nfs_false;
    134 
    135 /* And other global data */
    136 const nfstype nfsv2_type[9] =
    137 	{ NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON, NFCHR, NFNON };
    138 const nfstype nfsv3_type[9] =
    139 	{ NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK, NFFIFO, NFNON };
    140 const enum vtype nv2tov_type[8] =
    141 	{ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON };
    142 const enum vtype nv3tov_type[8] =
    143 	{ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO };
    144 int nfs_ticks;
    145 int nfs_commitsize;
    146 
    147 MALLOC_DEFINE(M_NFSDIROFF, "NFS diroff", "NFS directory cookies");
    148 
    149 /* NFS client/server stats. */
    150 struct nfsstats nfsstats;
    151 
    152 /*
    153  * Mapping of old NFS Version 2 RPC numbers to generic numbers.
    154  */
    155 const int nfsv3_procid[NFS_NPROCS] = {
    156 	NFSPROC_NULL,
    157 	NFSPROC_GETATTR,
    158 	NFSPROC_SETATTR,
    159 	NFSPROC_NOOP,
    160 	NFSPROC_LOOKUP,
    161 	NFSPROC_READLINK,
    162 	NFSPROC_READ,
    163 	NFSPROC_NOOP,
    164 	NFSPROC_WRITE,
    165 	NFSPROC_CREATE,
    166 	NFSPROC_REMOVE,
    167 	NFSPROC_RENAME,
    168 	NFSPROC_LINK,
    169 	NFSPROC_SYMLINK,
    170 	NFSPROC_MKDIR,
    171 	NFSPROC_RMDIR,
    172 	NFSPROC_READDIR,
    173 	NFSPROC_FSSTAT,
    174 	NFSPROC_NOOP,
    175 	NFSPROC_NOOP,
    176 	NFSPROC_NOOP,
    177 	NFSPROC_NOOP,
    178 	NFSPROC_NOOP
    179 };
    180 
    181 /*
    182  * and the reverse mapping from generic to Version 2 procedure numbers
    183  */
    184 const int nfsv2_procid[NFS_NPROCS] = {
    185 	NFSV2PROC_NULL,
    186 	NFSV2PROC_GETATTR,
    187 	NFSV2PROC_SETATTR,
    188 	NFSV2PROC_LOOKUP,
    189 	NFSV2PROC_NOOP,
    190 	NFSV2PROC_READLINK,
    191 	NFSV2PROC_READ,
    192 	NFSV2PROC_WRITE,
    193 	NFSV2PROC_CREATE,
    194 	NFSV2PROC_MKDIR,
    195 	NFSV2PROC_SYMLINK,
    196 	NFSV2PROC_CREATE,
    197 	NFSV2PROC_REMOVE,
    198 	NFSV2PROC_RMDIR,
    199 	NFSV2PROC_RENAME,
    200 	NFSV2PROC_LINK,
    201 	NFSV2PROC_READDIR,
    202 	NFSV2PROC_NOOP,
    203 	NFSV2PROC_STATFS,
    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 vfs_hooks nfs_export_hooks;
    570 extern struct nfsrtt nfsrtt;
    571 extern struct nfsnodehashhead *nfsnodehashtbl;
    572 extern u_long nfsnodehash;
    573 
    574 u_long nfsdirhashmask;
    575 
    576 int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *));
    577 
    578 /*
    579  * Create the header for an rpc request packet
    580  * The hsiz is the size of the rest of the nfs request header.
    581  * (just used to decide if a cluster is a good idea)
    582  */
    583 struct mbuf *
    584 nfsm_reqh(struct nfsnode *np, u_long procid, int hsiz, char **bposp)
    585 {
    586 	struct mbuf *mb;
    587 	char *bpos;
    588 
    589 	mb = m_get(M_WAIT, MT_DATA);
    590 	MCLAIM(mb, &nfs_mowner);
    591 	if (hsiz >= MINCLSIZE)
    592 		m_clget(mb, M_WAIT);
    593 	mb->m_len = 0;
    594 	bpos = mtod(mb, void *);
    595 
    596 	/* Finally, return values */
    597 	*bposp = bpos;
    598 	return (mb);
    599 }
    600 
    601 /*
    602  * Build the RPC header and fill in the authorization info.
    603  * The authorization string argument is only used when the credentials
    604  * come from outside of the kernel.
    605  * Returns the head of the mbuf list.
    606  */
    607 struct mbuf *
    608 nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
    609 	verf_str, mrest, mrest_len, mbp, xidp)
    610 	kauth_cred_t cr;
    611 	int nmflag;
    612 	int procid;
    613 	int auth_type;
    614 	int auth_len;
    615 	char *auth_str;
    616 	int verf_len;
    617 	char *verf_str;
    618 	struct mbuf *mrest;
    619 	int mrest_len;
    620 	struct mbuf **mbp;
    621 	u_int32_t *xidp;
    622 {
    623 	struct mbuf *mb;
    624 	u_int32_t *tl;
    625 	char *bpos;
    626 	int i;
    627 	struct mbuf *mreq;
    628 	int siz, grpsiz, authsiz;
    629 
    630 	authsiz = nfsm_rndup(auth_len);
    631 	mb = m_gethdr(M_WAIT, MT_DATA);
    632 	MCLAIM(mb, &nfs_mowner);
    633 	if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
    634 		m_clget(mb, M_WAIT);
    635 	} else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
    636 		MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
    637 	} else {
    638 		MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
    639 	}
    640 	mb->m_len = 0;
    641 	mreq = mb;
    642 	bpos = mtod(mb, void *);
    643 
    644 	/*
    645 	 * First the RPC header.
    646 	 */
    647 	nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
    648 
    649 	*tl++ = *xidp = nfs_getxid();
    650 	*tl++ = rpc_call;
    651 	*tl++ = rpc_vers;
    652 	*tl++ = txdr_unsigned(NFS_PROG);
    653 	if (nmflag & NFSMNT_NFSV3)
    654 		*tl++ = txdr_unsigned(NFS_VER3);
    655 	else
    656 		*tl++ = txdr_unsigned(NFS_VER2);
    657 	if (nmflag & NFSMNT_NFSV3)
    658 		*tl++ = txdr_unsigned(procid);
    659 	else
    660 		*tl++ = txdr_unsigned(nfsv2_procid[procid]);
    661 
    662 	/*
    663 	 * And then the authorization cred.
    664 	 */
    665 	*tl++ = txdr_unsigned(auth_type);
    666 	*tl = txdr_unsigned(authsiz);
    667 	switch (auth_type) {
    668 	case RPCAUTH_UNIX:
    669 		nfsm_build(tl, u_int32_t *, auth_len);
    670 		*tl++ = 0;		/* stamp ?? */
    671 		*tl++ = 0;		/* NULL hostname */
    672 		*tl++ = txdr_unsigned(kauth_cred_geteuid(cr));
    673 		*tl++ = txdr_unsigned(kauth_cred_getegid(cr));
    674 		grpsiz = (auth_len >> 2) - 5;
    675 		*tl++ = txdr_unsigned(grpsiz);
    676 		for (i = 0; i < grpsiz; i++)
    677 			*tl++ = txdr_unsigned(kauth_cred_group(cr, i)); /* XXX elad review */
    678 		break;
    679 	case RPCAUTH_KERB4:
    680 		siz = auth_len;
    681 		while (siz > 0) {
    682 			if (M_TRAILINGSPACE(mb) == 0) {
    683 				struct mbuf *mb2;
    684 				mb2 = m_get(M_WAIT, MT_DATA);
    685 				MCLAIM(mb2, &nfs_mowner);
    686 				if (siz >= MINCLSIZE)
    687 					m_clget(mb2, M_WAIT);
    688 				mb->m_next = mb2;
    689 				mb = mb2;
    690 				mb->m_len = 0;
    691 				bpos = mtod(mb, void *);
    692 			}
    693 			i = min(siz, M_TRAILINGSPACE(mb));
    694 			memcpy(bpos, auth_str, i);
    695 			mb->m_len += i;
    696 			auth_str += i;
    697 			bpos += i;
    698 			siz -= i;
    699 		}
    700 		if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
    701 			for (i = 0; i < siz; i++)
    702 				*bpos++ = '\0';
    703 			mb->m_len += siz;
    704 		}
    705 		break;
    706 	};
    707 
    708 	/*
    709 	 * And the verifier...
    710 	 */
    711 	nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
    712 	if (verf_str) {
    713 		*tl++ = txdr_unsigned(RPCAUTH_KERB4);
    714 		*tl = txdr_unsigned(verf_len);
    715 		siz = verf_len;
    716 		while (siz > 0) {
    717 			if (M_TRAILINGSPACE(mb) == 0) {
    718 				struct mbuf *mb2;
    719 				mb2 = m_get(M_WAIT, MT_DATA);
    720 				MCLAIM(mb2, &nfs_mowner);
    721 				if (siz >= MINCLSIZE)
    722 					m_clget(mb2, M_WAIT);
    723 				mb->m_next = mb2;
    724 				mb = mb2;
    725 				mb->m_len = 0;
    726 				bpos = mtod(mb, void *);
    727 			}
    728 			i = min(siz, M_TRAILINGSPACE(mb));
    729 			memcpy(bpos, verf_str, i);
    730 			mb->m_len += i;
    731 			verf_str += i;
    732 			bpos += i;
    733 			siz -= i;
    734 		}
    735 		if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
    736 			for (i = 0; i < siz; i++)
    737 				*bpos++ = '\0';
    738 			mb->m_len += siz;
    739 		}
    740 	} else {
    741 		*tl++ = txdr_unsigned(RPCAUTH_NULL);
    742 		*tl = 0;
    743 	}
    744 	mb->m_next = mrest;
    745 	mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
    746 	mreq->m_pkthdr.rcvif = (struct ifnet *)0;
    747 	*mbp = mb;
    748 	return (mreq);
    749 }
    750 
    751 /*
    752  * copies mbuf chain to the uio scatter/gather list
    753  */
    754 int
    755 nfsm_mbuftouio(mrep, uiop, siz, dpos)
    756 	struct mbuf **mrep;
    757 	struct uio *uiop;
    758 	int siz;
    759 	char **dpos;
    760 {
    761 	char *mbufcp, *uiocp;
    762 	int xfer, left, len;
    763 	struct mbuf *mp;
    764 	long uiosiz, rem;
    765 	int error = 0;
    766 
    767 	mp = *mrep;
    768 	mbufcp = *dpos;
    769 	len = mtod(mp, char *) + mp->m_len - mbufcp;
    770 	rem = nfsm_rndup(siz)-siz;
    771 	while (siz > 0) {
    772 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
    773 			return (EFBIG);
    774 		left = uiop->uio_iov->iov_len;
    775 		uiocp = uiop->uio_iov->iov_base;
    776 		if (left > siz)
    777 			left = siz;
    778 		uiosiz = left;
    779 		while (left > 0) {
    780 			while (len == 0) {
    781 				mp = mp->m_next;
    782 				if (mp == NULL)
    783 					return (EBADRPC);
    784 				mbufcp = mtod(mp, void *);
    785 				len = mp->m_len;
    786 			}
    787 			xfer = (left > len) ? len : left;
    788 			error = copyout_vmspace(uiop->uio_vmspace, mbufcp,
    789 			    uiocp, xfer);
    790 			if (error) {
    791 				return error;
    792 			}
    793 			left -= xfer;
    794 			len -= xfer;
    795 			mbufcp += xfer;
    796 			uiocp += xfer;
    797 			uiop->uio_offset += xfer;
    798 			uiop->uio_resid -= xfer;
    799 		}
    800 		if (uiop->uio_iov->iov_len <= siz) {
    801 			uiop->uio_iovcnt--;
    802 			uiop->uio_iov++;
    803 		} else {
    804 			uiop->uio_iov->iov_base =
    805 			    (char *)uiop->uio_iov->iov_base + uiosiz;
    806 			uiop->uio_iov->iov_len -= uiosiz;
    807 		}
    808 		siz -= uiosiz;
    809 	}
    810 	*dpos = mbufcp;
    811 	*mrep = mp;
    812 	if (rem > 0) {
    813 		if (len < rem)
    814 			error = nfs_adv(mrep, dpos, rem, len);
    815 		else
    816 			*dpos += rem;
    817 	}
    818 	return (error);
    819 }
    820 
    821 /*
    822  * copies a uio scatter/gather list to an mbuf chain.
    823  * NOTE: can ony handle iovcnt == 1
    824  */
    825 int
    826 nfsm_uiotombuf(uiop, mq, siz, bpos)
    827 	struct uio *uiop;
    828 	struct mbuf **mq;
    829 	int siz;
    830 	char **bpos;
    831 {
    832 	char *uiocp;
    833 	struct mbuf *mp, *mp2;
    834 	int xfer, left, mlen;
    835 	int uiosiz, clflg, rem;
    836 	char *cp;
    837 	int error;
    838 
    839 #ifdef DIAGNOSTIC
    840 	if (uiop->uio_iovcnt != 1)
    841 		panic("nfsm_uiotombuf: iovcnt != 1");
    842 #endif
    843 
    844 	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
    845 		clflg = 1;
    846 	else
    847 		clflg = 0;
    848 	rem = nfsm_rndup(siz)-siz;
    849 	mp = mp2 = *mq;
    850 	while (siz > 0) {
    851 		left = uiop->uio_iov->iov_len;
    852 		uiocp = uiop->uio_iov->iov_base;
    853 		if (left > siz)
    854 			left = siz;
    855 		uiosiz = left;
    856 		while (left > 0) {
    857 			mlen = M_TRAILINGSPACE(mp);
    858 			if (mlen == 0) {
    859 				mp = m_get(M_WAIT, MT_DATA);
    860 				MCLAIM(mp, &nfs_mowner);
    861 				if (clflg)
    862 					m_clget(mp, M_WAIT);
    863 				mp->m_len = 0;
    864 				mp2->m_next = mp;
    865 				mp2 = mp;
    866 				mlen = M_TRAILINGSPACE(mp);
    867 			}
    868 			xfer = (left > mlen) ? mlen : left;
    869 			cp = mtod(mp, char *) + mp->m_len;
    870 			error = copyin_vmspace(uiop->uio_vmspace, uiocp, cp,
    871 			    xfer);
    872 			if (error) {
    873 				/* XXX */
    874 			}
    875 			mp->m_len += xfer;
    876 			left -= xfer;
    877 			uiocp += xfer;
    878 			uiop->uio_offset += xfer;
    879 			uiop->uio_resid -= xfer;
    880 		}
    881 		uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
    882 		    uiosiz;
    883 		uiop->uio_iov->iov_len -= uiosiz;
    884 		siz -= uiosiz;
    885 	}
    886 	if (rem > 0) {
    887 		if (rem > M_TRAILINGSPACE(mp)) {
    888 			mp = m_get(M_WAIT, MT_DATA);
    889 			MCLAIM(mp, &nfs_mowner);
    890 			mp->m_len = 0;
    891 			mp2->m_next = mp;
    892 		}
    893 		cp = mtod(mp, char *) + mp->m_len;
    894 		for (left = 0; left < rem; left++)
    895 			*cp++ = '\0';
    896 		mp->m_len += rem;
    897 		*bpos = cp;
    898 	} else
    899 		*bpos = mtod(mp, char *) + mp->m_len;
    900 	*mq = mp;
    901 	return (0);
    902 }
    903 
    904 /*
    905  * Get at least "siz" bytes of correctly aligned data.
    906  * When called the mbuf pointers are not necessarily correct,
    907  * dsosp points to what ought to be in m_data and left contains
    908  * what ought to be in m_len.
    909  * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
    910  * cases. (The macros use the vars. dpos and dpos2)
    911  */
    912 int
    913 nfsm_disct(mdp, dposp, siz, left, cp2)
    914 	struct mbuf **mdp;
    915 	char **dposp;
    916 	int siz;
    917 	int left;
    918 	char **cp2;
    919 {
    920 	struct mbuf *m1, *m2;
    921 	struct mbuf *havebuf = NULL;
    922 	char *src = *dposp;
    923 	char *dst;
    924 	int len;
    925 
    926 #ifdef DEBUG
    927 	if (left < 0)
    928 		panic("nfsm_disct: left < 0");
    929 #endif
    930 	m1 = *mdp;
    931 	/*
    932 	 * Skip through the mbuf chain looking for an mbuf with
    933 	 * some data. If the first mbuf found has enough data
    934 	 * and it is correctly aligned return it.
    935 	 */
    936 	while (left == 0) {
    937 		havebuf = m1;
    938 		*mdp = m1 = m1->m_next;
    939 		if (m1 == NULL)
    940 			return (EBADRPC);
    941 		src = mtod(m1, void *);
    942 		left = m1->m_len;
    943 		/*
    944 		 * If we start a new mbuf and it is big enough
    945 		 * and correctly aligned just return it, don't
    946 		 * do any pull up.
    947 		 */
    948 		if (left >= siz && nfsm_aligned(src)) {
    949 			*cp2 = src;
    950 			*dposp = src + siz;
    951 			return (0);
    952 		}
    953 	}
    954 	if ((m1->m_flags & M_EXT) != 0) {
    955 		if (havebuf && M_TRAILINGSPACE(havebuf) >= siz &&
    956 		    nfsm_aligned(mtod(havebuf, char *) + havebuf->m_len)) {
    957 			/*
    958 			 * If the first mbuf with data has external data
    959 			 * and there is a previous mbuf with some trailing
    960 			 * space, use it to move the data into.
    961 			 */
    962 			m2 = m1;
    963 			*mdp = m1 = havebuf;
    964 			*cp2 = mtod(m1, char *) + m1->m_len;
    965 		} else if (havebuf) {
    966 			/*
    967 			 * If the first mbuf has a external data
    968 			 * and there is no previous empty mbuf
    969 			 * allocate a new mbuf and move the external
    970 			 * data to the new mbuf. Also make the first
    971 			 * mbuf look empty.
    972 			 */
    973 			m2 = m1;
    974 			*mdp = m1 = m_get(M_WAIT, MT_DATA);
    975 			MCLAIM(m1, m2->m_owner);
    976 			if ((m2->m_flags & M_PKTHDR) != 0) {
    977 				/* XXX MOVE */
    978 				M_COPY_PKTHDR(m1, m2);
    979 				m_tag_delete_chain(m2, NULL);
    980 				m2->m_flags &= ~M_PKTHDR;
    981 			}
    982 			if (havebuf) {
    983 				havebuf->m_next = m1;
    984 			}
    985 			m1->m_next = m2;
    986 			MRESETDATA(m1);
    987 			m1->m_len = 0;
    988 			m2->m_data = src;
    989 			m2->m_len = left;
    990 			*cp2 = mtod(m1, char *);
    991 		} else {
    992 			struct mbuf **nextp = &m1->m_next;
    993 
    994 			m1->m_len -= left;
    995 			do {
    996 				m2 = m_get(M_WAIT, MT_DATA);
    997 				MCLAIM(m2, m1->m_owner);
    998 				if (left >= MINCLSIZE) {
    999 					MCLGET(m2, M_WAIT);
   1000 				}
   1001 				m2->m_next = *nextp;
   1002 				*nextp = m2;
   1003 				nextp = &m2->m_next;
   1004 				len = (m2->m_flags & M_EXT) != 0 ?
   1005 				    MCLBYTES : MLEN;
   1006 				if (len > left) {
   1007 					len = left;
   1008 				}
   1009 				memcpy(mtod(m2, char *), src, len);
   1010 				m2->m_len = len;
   1011 				src += len;
   1012 				left -= len;
   1013 			} while (left > 0);
   1014 			*mdp = m1 = m1->m_next;
   1015 			m2 = m1->m_next;
   1016 			*cp2 = mtod(m1, char *);
   1017 		}
   1018 	} else {
   1019 		/*
   1020 		 * If the first mbuf has no external data
   1021 		 * move the data to the front of the mbuf.
   1022 		 */
   1023 		MRESETDATA(m1);
   1024 		dst = mtod(m1, char *);
   1025 		if (dst != src) {
   1026 			memmove(dst, src, left);
   1027 		}
   1028 		m1->m_len = left;
   1029 		m2 = m1->m_next;
   1030 		*cp2 = m1->m_data;
   1031 	}
   1032 	*dposp = *cp2 + siz;
   1033 	/*
   1034 	 * Loop through mbufs pulling data up into first mbuf until
   1035 	 * the first mbuf is full or there is no more data to
   1036 	 * pullup.
   1037 	 */
   1038 	dst = mtod(m1, char *) + m1->m_len;
   1039 	while ((len = M_TRAILINGSPACE(m1)) != 0 && m2) {
   1040 		if ((len = min(len, m2->m_len)) != 0) {
   1041 			memcpy(dst, mtod(m2, char *), len);
   1042 		}
   1043 		m1->m_len += len;
   1044 		dst += len;
   1045 		m2->m_data += len;
   1046 		m2->m_len -= len;
   1047 		m2 = m2->m_next;
   1048 	}
   1049 	if (m1->m_len < siz)
   1050 		return (EBADRPC);
   1051 	return (0);
   1052 }
   1053 
   1054 /*
   1055  * Advance the position in the mbuf chain.
   1056  */
   1057 int
   1058 nfs_adv(mdp, dposp, offs, left)
   1059 	struct mbuf **mdp;
   1060 	char **dposp;
   1061 	int offs;
   1062 	int left;
   1063 {
   1064 	struct mbuf *m;
   1065 	int s;
   1066 
   1067 	m = *mdp;
   1068 	s = left;
   1069 	while (s < offs) {
   1070 		offs -= s;
   1071 		m = m->m_next;
   1072 		if (m == NULL)
   1073 			return (EBADRPC);
   1074 		s = m->m_len;
   1075 	}
   1076 	*mdp = m;
   1077 	*dposp = mtod(m, char *) + offs;
   1078 	return (0);
   1079 }
   1080 
   1081 /*
   1082  * Copy a string into mbufs for the hard cases...
   1083  */
   1084 int
   1085 nfsm_strtmbuf(mb, bpos, cp, siz)
   1086 	struct mbuf **mb;
   1087 	char **bpos;
   1088 	const char *cp;
   1089 	long siz;
   1090 {
   1091 	struct mbuf *m1 = NULL, *m2;
   1092 	long left, xfer, len, tlen;
   1093 	u_int32_t *tl;
   1094 	int putsize;
   1095 
   1096 	putsize = 1;
   1097 	m2 = *mb;
   1098 	left = M_TRAILINGSPACE(m2);
   1099 	if (left > 0) {
   1100 		tl = ((u_int32_t *)(*bpos));
   1101 		*tl++ = txdr_unsigned(siz);
   1102 		putsize = 0;
   1103 		left -= NFSX_UNSIGNED;
   1104 		m2->m_len += NFSX_UNSIGNED;
   1105 		if (left > 0) {
   1106 			memcpy((void *) tl, cp, left);
   1107 			siz -= left;
   1108 			cp += left;
   1109 			m2->m_len += left;
   1110 			left = 0;
   1111 		}
   1112 	}
   1113 	/* Loop around adding mbufs */
   1114 	while (siz > 0) {
   1115 		m1 = m_get(M_WAIT, MT_DATA);
   1116 		MCLAIM(m1, &nfs_mowner);
   1117 		if (siz > MLEN)
   1118 			m_clget(m1, M_WAIT);
   1119 		m1->m_len = NFSMSIZ(m1);
   1120 		m2->m_next = m1;
   1121 		m2 = m1;
   1122 		tl = mtod(m1, u_int32_t *);
   1123 		tlen = 0;
   1124 		if (putsize) {
   1125 			*tl++ = txdr_unsigned(siz);
   1126 			m1->m_len -= NFSX_UNSIGNED;
   1127 			tlen = NFSX_UNSIGNED;
   1128 			putsize = 0;
   1129 		}
   1130 		if (siz < m1->m_len) {
   1131 			len = nfsm_rndup(siz);
   1132 			xfer = siz;
   1133 			if (xfer < len)
   1134 				*(tl+(xfer>>2)) = 0;
   1135 		} else {
   1136 			xfer = len = m1->m_len;
   1137 		}
   1138 		memcpy((void *) tl, cp, xfer);
   1139 		m1->m_len = len+tlen;
   1140 		siz -= xfer;
   1141 		cp += xfer;
   1142 	}
   1143 	*mb = m1;
   1144 	*bpos = mtod(m1, char *) + m1->m_len;
   1145 	return (0);
   1146 }
   1147 
   1148 /*
   1149  * Directory caching routines. They work as follows:
   1150  * - a cache is maintained per VDIR nfsnode.
   1151  * - for each offset cookie that is exported to userspace, and can
   1152  *   thus be thrown back at us as an offset to VOP_READDIR, store
   1153  *   information in the cache.
   1154  * - cached are:
   1155  *   - cookie itself
   1156  *   - blocknumber (essentially just a search key in the buffer cache)
   1157  *   - entry number in block.
   1158  *   - offset cookie of block in which this entry is stored
   1159  *   - 32 bit cookie if NFSMNT_XLATECOOKIE is used.
   1160  * - entries are looked up in a hash table
   1161  * - also maintained is an LRU list of entries, used to determine
   1162  *   which ones to delete if the cache grows too large.
   1163  * - if 32 <-> 64 translation mode is requested for a filesystem,
   1164  *   the cache also functions as a translation table
   1165  * - in the translation case, invalidating the cache does not mean
   1166  *   flushing it, but just marking entries as invalid, except for
   1167  *   the <64bit cookie, 32bitcookie> pair which is still valid, to
   1168  *   still be able to use the cache as a translation table.
   1169  * - 32 bit cookies are uniquely created by combining the hash table
   1170  *   entry value, and one generation count per hash table entry,
   1171  *   incremented each time an entry is appended to the chain.
   1172  * - the cache is invalidated each time a direcory is modified
   1173  * - sanity checks are also done; if an entry in a block turns
   1174  *   out not to have a matching cookie, the cache is invalidated
   1175  *   and a new block starting from the wanted offset is fetched from
   1176  *   the server.
   1177  * - directory entries as read from the server are extended to contain
   1178  *   the 64bit and, optionally, the 32bit cookies, for sanity checking
   1179  *   the cache and exporting them to userspace through the cookie
   1180  *   argument to VOP_READDIR.
   1181  */
   1182 
   1183 u_long
   1184 nfs_dirhash(off)
   1185 	off_t off;
   1186 {
   1187 	int i;
   1188 	char *cp = (char *)&off;
   1189 	u_long sum = 0L;
   1190 
   1191 	for (i = 0 ; i < sizeof (off); i++)
   1192 		sum += *cp++;
   1193 
   1194 	return sum;
   1195 }
   1196 
   1197 #define	_NFSDC_MTX(np)		(&NFSTOV(np)->v_interlock)
   1198 #define	NFSDC_LOCK(np)		mutex_enter(_NFSDC_MTX(np))
   1199 #define	NFSDC_UNLOCK(np)	mutex_exit(_NFSDC_MTX(np))
   1200 #define	NFSDC_ASSERT_LOCKED(np) KASSERT(mutex_owned(_NFSDC_MTX(np)))
   1201 
   1202 void
   1203 nfs_initdircache(vp)
   1204 	struct vnode *vp;
   1205 {
   1206 	struct nfsnode *np = VTONFS(vp);
   1207 	struct nfsdirhashhead *dircache;
   1208 
   1209 	dircache = hashinit(NFS_DIRHASHSIZ, HASH_LIST, true,
   1210 	    &nfsdirhashmask);
   1211 
   1212 	NFSDC_LOCK(np);
   1213 	if (np->n_dircache == NULL) {
   1214 		np->n_dircachesize = 0;
   1215 		np->n_dircache = dircache;
   1216 		dircache = NULL;
   1217 		TAILQ_INIT(&np->n_dirchain);
   1218 	}
   1219 	NFSDC_UNLOCK(np);
   1220 	if (dircache)
   1221 		hashdone(dircache, HASH_LIST, nfsdirhashmask);
   1222 }
   1223 
   1224 void
   1225 nfs_initdirxlatecookie(vp)
   1226 	struct vnode *vp;
   1227 {
   1228 	struct nfsnode *np = VTONFS(vp);
   1229 	unsigned *dirgens;
   1230 
   1231 	KASSERT(VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_XLATECOOKIE);
   1232 
   1233 	dirgens = kmem_zalloc(NFS_DIRHASHSIZ * sizeof(unsigned), KM_SLEEP);
   1234 	NFSDC_LOCK(np);
   1235 	if (np->n_dirgens == NULL) {
   1236 		np->n_dirgens = dirgens;
   1237 		dirgens = NULL;
   1238 	}
   1239 	NFSDC_UNLOCK(np);
   1240 	if (dirgens)
   1241 		kmem_free(dirgens, NFS_DIRHASHSIZ * sizeof(unsigned));
   1242 }
   1243 
   1244 static const struct nfsdircache dzero;
   1245 
   1246 static void nfs_unlinkdircache __P((struct nfsnode *np, struct nfsdircache *));
   1247 static void nfs_putdircache_unlocked __P((struct nfsnode *,
   1248     struct nfsdircache *));
   1249 
   1250 static void
   1251 nfs_unlinkdircache(np, ndp)
   1252 	struct nfsnode *np;
   1253 	struct nfsdircache *ndp;
   1254 {
   1255 
   1256 	NFSDC_ASSERT_LOCKED(np);
   1257 	KASSERT(ndp != &dzero);
   1258 
   1259 	if (LIST_NEXT(ndp, dc_hash) == (void *)-1)
   1260 		return;
   1261 
   1262 	TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain);
   1263 	LIST_REMOVE(ndp, dc_hash);
   1264 	LIST_NEXT(ndp, dc_hash) = (void *)-1; /* mark as unlinked */
   1265 
   1266 	nfs_putdircache_unlocked(np, ndp);
   1267 }
   1268 
   1269 void
   1270 nfs_putdircache(np, ndp)
   1271 	struct nfsnode *np;
   1272 	struct nfsdircache *ndp;
   1273 {
   1274 	int ref;
   1275 
   1276 	if (ndp == &dzero)
   1277 		return;
   1278 
   1279 	KASSERT(ndp->dc_refcnt > 0);
   1280 	NFSDC_LOCK(np);
   1281 	ref = --ndp->dc_refcnt;
   1282 	NFSDC_UNLOCK(np);
   1283 
   1284 	if (ref == 0)
   1285 		kmem_free(ndp, sizeof(*ndp));
   1286 }
   1287 
   1288 static void
   1289 nfs_putdircache_unlocked(struct nfsnode *np, struct nfsdircache *ndp)
   1290 {
   1291 	int ref;
   1292 
   1293 	NFSDC_ASSERT_LOCKED(np);
   1294 
   1295 	if (ndp == &dzero)
   1296 		return;
   1297 
   1298 	KASSERT(ndp->dc_refcnt > 0);
   1299 	ref = --ndp->dc_refcnt;
   1300 	if (ref == 0)
   1301 		kmem_free(ndp, sizeof(*ndp));
   1302 }
   1303 
   1304 struct nfsdircache *
   1305 nfs_searchdircache(vp, off, do32, hashent)
   1306 	struct vnode *vp;
   1307 	off_t off;
   1308 	int do32;
   1309 	int *hashent;
   1310 {
   1311 	struct nfsdirhashhead *ndhp;
   1312 	struct nfsdircache *ndp = NULL;
   1313 	struct nfsnode *np = VTONFS(vp);
   1314 	unsigned ent;
   1315 
   1316 	/*
   1317 	 * Zero is always a valid cookie.
   1318 	 */
   1319 	if (off == 0)
   1320 		/* XXXUNCONST */
   1321 		return (struct nfsdircache *)__UNCONST(&dzero);
   1322 
   1323 	if (!np->n_dircache)
   1324 		return NULL;
   1325 
   1326 	/*
   1327 	 * We use a 32bit cookie as search key, directly reconstruct
   1328 	 * the hashentry. Else use the hashfunction.
   1329 	 */
   1330 	if (do32) {
   1331 		ent = (u_int32_t)off >> 24;
   1332 		if (ent >= NFS_DIRHASHSIZ)
   1333 			return NULL;
   1334 		ndhp = &np->n_dircache[ent];
   1335 	} else {
   1336 		ndhp = NFSDIRHASH(np, off);
   1337 	}
   1338 
   1339 	if (hashent)
   1340 		*hashent = (int)(ndhp - np->n_dircache);
   1341 
   1342 	NFSDC_LOCK(np);
   1343 	if (do32) {
   1344 		LIST_FOREACH(ndp, ndhp, dc_hash) {
   1345 			if (ndp->dc_cookie32 == (u_int32_t)off) {
   1346 				/*
   1347 				 * An invalidated entry will become the
   1348 				 * start of a new block fetched from
   1349 				 * the server.
   1350 				 */
   1351 				if (ndp->dc_flags & NFSDC_INVALID) {
   1352 					ndp->dc_blkcookie = ndp->dc_cookie;
   1353 					ndp->dc_entry = 0;
   1354 					ndp->dc_flags &= ~NFSDC_INVALID;
   1355 				}
   1356 				break;
   1357 			}
   1358 		}
   1359 	} else {
   1360 		LIST_FOREACH(ndp, ndhp, dc_hash) {
   1361 			if (ndp->dc_cookie == off)
   1362 				break;
   1363 		}
   1364 	}
   1365 	if (ndp != NULL)
   1366 		ndp->dc_refcnt++;
   1367 	NFSDC_UNLOCK(np);
   1368 	return ndp;
   1369 }
   1370 
   1371 
   1372 struct nfsdircache *
   1373 nfs_enterdircache(struct vnode *vp, off_t off, off_t blkoff, int en,
   1374     daddr_t blkno)
   1375 {
   1376 	struct nfsnode *np = VTONFS(vp);
   1377 	struct nfsdirhashhead *ndhp;
   1378 	struct nfsdircache *ndp = NULL;
   1379 	struct nfsdircache *newndp = NULL;
   1380 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1381 	int hashent = 0, gen, overwrite;	/* XXX: GCC */
   1382 
   1383 	/*
   1384 	 * XXX refuse entries for offset 0. amd(8) erroneously sets
   1385 	 * cookie 0 for the '.' entry, making this necessary. This
   1386 	 * isn't so bad, as 0 is a special case anyway.
   1387 	 */
   1388 	if (off == 0)
   1389 		/* XXXUNCONST */
   1390 		return (struct nfsdircache *)__UNCONST(&dzero);
   1391 
   1392 	if (!np->n_dircache)
   1393 		/*
   1394 		 * XXX would like to do this in nfs_nget but vtype
   1395 		 * isn't known at that time.
   1396 		 */
   1397 		nfs_initdircache(vp);
   1398 
   1399 	if ((nmp->nm_flag & NFSMNT_XLATECOOKIE) && !np->n_dirgens)
   1400 		nfs_initdirxlatecookie(vp);
   1401 
   1402 retry:
   1403 	ndp = nfs_searchdircache(vp, off, 0, &hashent);
   1404 
   1405 	NFSDC_LOCK(np);
   1406 	if (ndp && (ndp->dc_flags & NFSDC_INVALID) == 0) {
   1407 		/*
   1408 		 * Overwriting an old entry. Check if it's the same.
   1409 		 * If so, just return. If not, remove the old entry.
   1410 		 */
   1411 		if (ndp->dc_blkcookie == blkoff && ndp->dc_entry == en)
   1412 			goto done;
   1413 		nfs_unlinkdircache(np, ndp);
   1414 		nfs_putdircache_unlocked(np, ndp);
   1415 		ndp = NULL;
   1416 	}
   1417 
   1418 	ndhp = &np->n_dircache[hashent];
   1419 
   1420 	if (!ndp) {
   1421 		if (newndp == NULL) {
   1422 			NFSDC_UNLOCK(np);
   1423 			newndp = kmem_alloc(sizeof(*newndp), KM_SLEEP);
   1424 			newndp->dc_refcnt = 1;
   1425 			LIST_NEXT(newndp, dc_hash) = (void *)-1;
   1426 			goto retry;
   1427 		}
   1428 		ndp = newndp;
   1429 		newndp = NULL;
   1430 		overwrite = 0;
   1431 		if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
   1432 			/*
   1433 			 * We're allocating a new entry, so bump the
   1434 			 * generation number.
   1435 			 */
   1436 			KASSERT(np->n_dirgens);
   1437 			gen = ++np->n_dirgens[hashent];
   1438 			if (gen == 0) {
   1439 				np->n_dirgens[hashent]++;
   1440 				gen++;
   1441 			}
   1442 			ndp->dc_cookie32 = (hashent << 24) | (gen & 0xffffff);
   1443 		}
   1444 	} else
   1445 		overwrite = 1;
   1446 
   1447 	ndp->dc_cookie = off;
   1448 	ndp->dc_blkcookie = blkoff;
   1449 	ndp->dc_entry = en;
   1450 	ndp->dc_flags = 0;
   1451 
   1452 	if (overwrite)
   1453 		goto done;
   1454 
   1455 	/*
   1456 	 * If the maximum directory cookie cache size has been reached
   1457 	 * for this node, take one off the front. The idea is that
   1458 	 * directories are typically read front-to-back once, so that
   1459 	 * the oldest entries can be thrown away without much performance
   1460 	 * loss.
   1461 	 */
   1462 	if (np->n_dircachesize == NFS_MAXDIRCACHE) {
   1463 		nfs_unlinkdircache(np, TAILQ_FIRST(&np->n_dirchain));
   1464 	} else
   1465 		np->n_dircachesize++;
   1466 
   1467 	KASSERT(ndp->dc_refcnt == 1);
   1468 	LIST_INSERT_HEAD(ndhp, ndp, dc_hash);
   1469 	TAILQ_INSERT_TAIL(&np->n_dirchain, ndp, dc_chain);
   1470 	ndp->dc_refcnt++;
   1471 done:
   1472 	KASSERT(ndp->dc_refcnt > 0);
   1473 	NFSDC_UNLOCK(np);
   1474 	if (newndp)
   1475 		nfs_putdircache(np, newndp);
   1476 	return ndp;
   1477 }
   1478 
   1479 void
   1480 nfs_invaldircache(vp, flags)
   1481 	struct vnode *vp;
   1482 	int flags;
   1483 {
   1484 	struct nfsnode *np = VTONFS(vp);
   1485 	struct nfsdircache *ndp = NULL;
   1486 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1487 	const bool forcefree = flags & NFS_INVALDIRCACHE_FORCE;
   1488 
   1489 #ifdef DIAGNOSTIC
   1490 	if (vp->v_type != VDIR)
   1491 		panic("nfs: invaldircache: not dir");
   1492 #endif
   1493 
   1494 	if ((flags & NFS_INVALDIRCACHE_KEEPEOF) == 0)
   1495 		np->n_flag &= ~NEOFVALID;
   1496 
   1497 	if (!np->n_dircache)
   1498 		return;
   1499 
   1500 	NFSDC_LOCK(np);
   1501 	if (!(nmp->nm_flag & NFSMNT_XLATECOOKIE) || forcefree) {
   1502 		while ((ndp = TAILQ_FIRST(&np->n_dirchain)) != NULL) {
   1503 			KASSERT(!forcefree || ndp->dc_refcnt == 1);
   1504 			nfs_unlinkdircache(np, ndp);
   1505 		}
   1506 		np->n_dircachesize = 0;
   1507 		if (forcefree && np->n_dirgens) {
   1508 			kmem_free(np->n_dirgens,
   1509 			    NFS_DIRHASHSIZ * sizeof(unsigned));
   1510 			np->n_dirgens = NULL;
   1511 		}
   1512 	} else {
   1513 		TAILQ_FOREACH(ndp, &np->n_dirchain, dc_chain)
   1514 			ndp->dc_flags |= NFSDC_INVALID;
   1515 	}
   1516 
   1517 	NFSDC_UNLOCK(np);
   1518 }
   1519 
   1520 /*
   1521  * Called once before VFS init to initialize shared and
   1522  * server-specific data structures.
   1523  */
   1524 static int
   1525 nfs_init0(void)
   1526 {
   1527 
   1528 	nfsrtt.pos = 0;
   1529 	rpc_vers = txdr_unsigned(RPC_VER2);
   1530 	rpc_call = txdr_unsigned(RPC_CALL);
   1531 	rpc_reply = txdr_unsigned(RPC_REPLY);
   1532 	rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
   1533 	rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
   1534 	rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
   1535 	rpc_autherr = txdr_unsigned(RPC_AUTHERR);
   1536 	rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
   1537 	rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
   1538 	nfs_prog = txdr_unsigned(NFS_PROG);
   1539 	nfs_true = txdr_unsigned(true);
   1540 	nfs_false = txdr_unsigned(false);
   1541 	nfs_xdrneg1 = txdr_unsigned(-1);
   1542 	nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
   1543 	if (nfs_ticks < 1)
   1544 		nfs_ticks = 1;
   1545 	nfs_xid = arc4random();
   1546 #ifdef NFSSERVER
   1547 	vfs_hooks_attach(&nfs_export_hooks);
   1548 	nfsrv_init(0);			/* Init server data structures */
   1549 	nfsrv_initcache();		/* Init the server request cache */
   1550 	{
   1551 		extern krwlock_t netexport_lock;	/* XXX */
   1552 		rw_init(&netexport_lock);
   1553 	}
   1554 #endif /* NFSSERVER */
   1555 
   1556 #if defined(NFSSERVER) || (defined(NFS) && !defined(NFS_V2_ONLY))
   1557 	nfsdreq_init();
   1558 #endif /* defined(NFSSERVER) || (defined(NFS) && !defined(NFS_V2_ONLY)) */
   1559 
   1560 	/*
   1561 	 * Initialize reply list and start timer
   1562 	 */
   1563 	TAILQ_INIT(&nfs_reqq);
   1564 	nfs_timer_init();
   1565 	MOWNER_ATTACH(&nfs_mowner);
   1566 
   1567 #ifdef NFS
   1568 	/* Initialize the kqueue structures */
   1569 	nfs_kqinit();
   1570 	/* Initialize the iod structures */
   1571 	nfs_iodinit();
   1572 #endif
   1573 
   1574 	return 0;
   1575 }
   1576 
   1577 void
   1578 nfs_init(void)
   1579 {
   1580 	static ONCE_DECL(nfs_init_once);
   1581 
   1582 	RUN_ONCE(&nfs_init_once, nfs_init0);
   1583 }
   1584 
   1585 #ifdef NFS
   1586 /*
   1587  * Called once at VFS init to initialize client-specific data structures.
   1588  */
   1589 void
   1590 nfs_vfs_init()
   1591 {
   1592 
   1593 	/* Initialize NFS server / client shared data. */
   1594 	nfs_init();
   1595 
   1596 	nfs_node_init();
   1597 	nfs_commitsize = uvmexp.npages << (PAGE_SHIFT - 4);
   1598 }
   1599 
   1600 void
   1601 nfs_vfs_reinit()
   1602 {
   1603 
   1604 	nfs_node_reinit();
   1605 }
   1606 
   1607 void
   1608 nfs_vfs_done()
   1609 {
   1610 
   1611 	nfs_node_done();
   1612 }
   1613 
   1614 /*
   1615  * Attribute cache routines.
   1616  * nfs_loadattrcache() - loads or updates the cache contents from attributes
   1617  *	that are on the mbuf list
   1618  * nfs_getattrcache() - returns valid attributes if found in cache, returns
   1619  *	error otherwise
   1620  */
   1621 
   1622 /*
   1623  * Load the attribute cache (that lives in the nfsnode entry) with
   1624  * the values on the mbuf list and
   1625  * Iff vap not NULL
   1626  *    copy the attributes to *vaper
   1627  */
   1628 int
   1629 nfsm_loadattrcache(vpp, mdp, dposp, vaper, flags)
   1630 	struct vnode **vpp;
   1631 	struct mbuf **mdp;
   1632 	char **dposp;
   1633 	struct vattr *vaper;
   1634 	int flags;
   1635 {
   1636 	int32_t t1;
   1637 	char *cp2;
   1638 	int error = 0;
   1639 	struct mbuf *md;
   1640 	int v3 = NFS_ISV3(*vpp);
   1641 
   1642 	md = *mdp;
   1643 	t1 = (mtod(md, char *) + md->m_len) - *dposp;
   1644 	error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2);
   1645 	if (error)
   1646 		return (error);
   1647 	return nfs_loadattrcache(vpp, (struct nfs_fattr *)cp2, vaper, flags);
   1648 }
   1649 
   1650 int
   1651 nfs_loadattrcache(vpp, fp, vaper, flags)
   1652 	struct vnode **vpp;
   1653 	struct nfs_fattr *fp;
   1654 	struct vattr *vaper;
   1655 	int flags;
   1656 {
   1657 	struct vnode *vp = *vpp;
   1658 	struct vattr *vap;
   1659 	int v3 = NFS_ISV3(vp);
   1660 	enum vtype vtyp;
   1661 	u_short vmode;
   1662 	struct timespec mtime;
   1663 	struct timespec ctime;
   1664 	int32_t rdev;
   1665 	struct nfsnode *np;
   1666 	extern int (**spec_nfsv2nodeop_p) __P((void *));
   1667 	uid_t uid;
   1668 	gid_t gid;
   1669 
   1670 	if (v3) {
   1671 		vtyp = nfsv3tov_type(fp->fa_type);
   1672 		vmode = fxdr_unsigned(u_short, fp->fa_mode);
   1673 		rdev = makedev(fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata1),
   1674 			fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata2));
   1675 		fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
   1676 		fxdr_nfsv3time(&fp->fa3_ctime, &ctime);
   1677 	} else {
   1678 		vtyp = nfsv2tov_type(fp->fa_type);
   1679 		vmode = fxdr_unsigned(u_short, fp->fa_mode);
   1680 		if (vtyp == VNON || vtyp == VREG)
   1681 			vtyp = IFTOVT(vmode);
   1682 		rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
   1683 		fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
   1684 		ctime.tv_sec = fxdr_unsigned(u_int32_t,
   1685 		    fp->fa2_ctime.nfsv2_sec);
   1686 		ctime.tv_nsec = 0;
   1687 
   1688 		/*
   1689 		 * Really ugly NFSv2 kludge.
   1690 		 */
   1691 		if (vtyp == VCHR && rdev == 0xffffffff)
   1692 			vtyp = VFIFO;
   1693 	}
   1694 
   1695 	vmode &= ALLPERMS;
   1696 
   1697 	/*
   1698 	 * If v_type == VNON it is a new node, so fill in the v_type,
   1699 	 * n_mtime fields. Check to see if it represents a special
   1700 	 * device, and if so, check for a possible alias. Once the
   1701 	 * correct vnode has been obtained, fill in the rest of the
   1702 	 * information.
   1703 	 */
   1704 	np = VTONFS(vp);
   1705 	if (vp->v_type == VNON) {
   1706 		vp->v_type = vtyp;
   1707 		if (vp->v_type == VFIFO) {
   1708 			extern int (**fifo_nfsv2nodeop_p) __P((void *));
   1709 			vp->v_op = fifo_nfsv2nodeop_p;
   1710 		} else if (vp->v_type == VREG) {
   1711 			mutex_init(&np->n_commitlock, MUTEX_DEFAULT, IPL_NONE);
   1712 		} else if (vp->v_type == VCHR || vp->v_type == VBLK) {
   1713 			vp->v_op = spec_nfsv2nodeop_p;
   1714 			spec_node_init(vp, (dev_t)rdev);
   1715 		}
   1716 		np->n_mtime = mtime;
   1717 	}
   1718 	uid = fxdr_unsigned(uid_t, fp->fa_uid);
   1719 	gid = fxdr_unsigned(gid_t, fp->fa_gid);
   1720 	vap = np->n_vattr;
   1721 
   1722 	/*
   1723 	 * Invalidate access cache if uid, gid, mode or ctime changed.
   1724 	 */
   1725 	if (np->n_accstamp != -1 &&
   1726 	    (gid != vap->va_gid || uid != vap->va_uid || vmode != vap->va_mode
   1727 	    || timespeccmp(&ctime, &vap->va_ctime, !=)))
   1728 		np->n_accstamp = -1;
   1729 
   1730 	vap->va_type = vtyp;
   1731 	vap->va_mode = vmode;
   1732 	vap->va_rdev = (dev_t)rdev;
   1733 	vap->va_mtime = mtime;
   1734 	vap->va_ctime = ctime;
   1735 	vap->va_birthtime.tv_sec = VNOVAL;
   1736 	vap->va_birthtime.tv_nsec = VNOVAL;
   1737 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
   1738 	switch (vtyp) {
   1739 	case VDIR:
   1740 		vap->va_blocksize = NFS_DIRFRAGSIZ;
   1741 		break;
   1742 	case VBLK:
   1743 		vap->va_blocksize = BLKDEV_IOSIZE;
   1744 		break;
   1745 	case VCHR:
   1746 		vap->va_blocksize = MAXBSIZE;
   1747 		break;
   1748 	default:
   1749 		vap->va_blocksize = v3 ? vp->v_mount->mnt_stat.f_iosize :
   1750 		    fxdr_unsigned(int32_t, fp->fa2_blocksize);
   1751 		break;
   1752 	}
   1753 	if (v3) {
   1754 		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
   1755 		vap->va_uid = uid;
   1756 		vap->va_gid = gid;
   1757 		vap->va_size = fxdr_hyper(&fp->fa3_size);
   1758 		vap->va_bytes = fxdr_hyper(&fp->fa3_used);
   1759 		vap->va_fileid = fxdr_hyper(&fp->fa3_fileid);
   1760 		fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
   1761 		vap->va_flags = 0;
   1762 		vap->va_filerev = 0;
   1763 	} else {
   1764 		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
   1765 		vap->va_uid = uid;
   1766 		vap->va_gid = gid;
   1767 		vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
   1768 		vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks)
   1769 		    * NFS_FABLKSIZE;
   1770 		vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
   1771 		fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
   1772 		vap->va_flags = 0;
   1773 		vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
   1774 		vap->va_filerev = 0;
   1775 	}
   1776 	if (vap->va_size > VFSTONFS(vp->v_mount)->nm_maxfilesize) {
   1777 		return EFBIG;
   1778 	}
   1779 	if (vap->va_size != np->n_size) {
   1780 		if ((np->n_flag & NMODIFIED) && vap->va_size < np->n_size) {
   1781 			vap->va_size = np->n_size;
   1782 		} else {
   1783 			np->n_size = vap->va_size;
   1784 			if (vap->va_type == VREG) {
   1785 				/*
   1786 				 * we can't free pages if NAC_NOTRUNC because
   1787 				 * the pages can be owned by ourselves.
   1788 				 */
   1789 				if (flags & NAC_NOTRUNC) {
   1790 					np->n_flag |= NTRUNCDELAYED;
   1791 				} else {
   1792 					genfs_node_wrlock(vp);
   1793 					mutex_enter(&vp->v_interlock);
   1794 					(void)VOP_PUTPAGES(vp, 0,
   1795 					    0, PGO_SYNCIO | PGO_CLEANIT |
   1796 					    PGO_FREE | PGO_ALLPAGES);
   1797 					uvm_vnp_setsize(vp, np->n_size);
   1798 					genfs_node_unlock(vp);
   1799 				}
   1800 			}
   1801 		}
   1802 	}
   1803 	np->n_attrstamp = time_second;
   1804 	if (vaper != NULL) {
   1805 		memcpy((void *)vaper, (void *)vap, sizeof(*vap));
   1806 		if (np->n_flag & NCHG) {
   1807 			if (np->n_flag & NACC)
   1808 				vaper->va_atime = np->n_atim;
   1809 			if (np->n_flag & NUPD)
   1810 				vaper->va_mtime = np->n_mtim;
   1811 		}
   1812 	}
   1813 	return (0);
   1814 }
   1815 
   1816 /*
   1817  * Check the time stamp
   1818  * If the cache is valid, copy contents to *vap and return 0
   1819  * otherwise return an error
   1820  */
   1821 int
   1822 nfs_getattrcache(vp, vaper)
   1823 	struct vnode *vp;
   1824 	struct vattr *vaper;
   1825 {
   1826 	struct nfsnode *np = VTONFS(vp);
   1827 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1828 	struct vattr *vap;
   1829 
   1830 	if (np->n_attrstamp == 0 ||
   1831 	    (time_second - np->n_attrstamp) >= nfs_attrtimeo(nmp, np)) {
   1832 		nfsstats.attrcache_misses++;
   1833 		return (ENOENT);
   1834 	}
   1835 	nfsstats.attrcache_hits++;
   1836 	vap = np->n_vattr;
   1837 	if (vap->va_size != np->n_size) {
   1838 		if (vap->va_type == VREG) {
   1839 			if ((np->n_flag & NMODIFIED) != 0 &&
   1840 			    vap->va_size < np->n_size) {
   1841 				vap->va_size = np->n_size;
   1842 			} else {
   1843 				np->n_size = vap->va_size;
   1844 			}
   1845 			genfs_node_wrlock(vp);
   1846 			uvm_vnp_setsize(vp, np->n_size);
   1847 			genfs_node_unlock(vp);
   1848 		} else
   1849 			np->n_size = vap->va_size;
   1850 	}
   1851 	memcpy((void *)vaper, (void *)vap, sizeof(struct vattr));
   1852 	if (np->n_flag & NCHG) {
   1853 		if (np->n_flag & NACC)
   1854 			vaper->va_atime = np->n_atim;
   1855 		if (np->n_flag & NUPD)
   1856 			vaper->va_mtime = np->n_mtim;
   1857 	}
   1858 	return (0);
   1859 }
   1860 
   1861 void
   1862 nfs_delayedtruncate(vp)
   1863 	struct vnode *vp;
   1864 {
   1865 	struct nfsnode *np = VTONFS(vp);
   1866 
   1867 	if (np->n_flag & NTRUNCDELAYED) {
   1868 		np->n_flag &= ~NTRUNCDELAYED;
   1869 		genfs_node_wrlock(vp);
   1870 		mutex_enter(&vp->v_interlock);
   1871 		(void)VOP_PUTPAGES(vp, 0,
   1872 		    0, PGO_SYNCIO | PGO_CLEANIT | PGO_FREE | PGO_ALLPAGES);
   1873 		uvm_vnp_setsize(vp, np->n_size);
   1874 		genfs_node_unlock(vp);
   1875 	}
   1876 }
   1877 
   1878 #define	NFS_WCCKLUDGE_TIMEOUT	(24 * 60 * 60)	/* 1 day */
   1879 #define	NFS_WCCKLUDGE(nmp, now) \
   1880 	(((nmp)->nm_iflag & NFSMNT_WCCKLUDGE) && \
   1881 	((now) - (nmp)->nm_wcckludgetime - NFS_WCCKLUDGE_TIMEOUT) < 0)
   1882 
   1883 /*
   1884  * nfs_check_wccdata: check inaccurate wcc_data
   1885  *
   1886  * => return non-zero if we shouldn't trust the wcc_data.
   1887  * => NFS_WCCKLUDGE_TIMEOUT is for the case that the server is "fixed".
   1888  */
   1889 
   1890 int
   1891 nfs_check_wccdata(struct nfsnode *np, const struct timespec *ctime,
   1892     struct timespec *mtime, bool docheck)
   1893 {
   1894 	int error = 0;
   1895 
   1896 #if !defined(NFS_V2_ONLY)
   1897 
   1898 	if (docheck) {
   1899 		struct vnode *vp = NFSTOV(np);
   1900 		struct nfsmount *nmp;
   1901 		long now = time_second;
   1902 		const struct timespec *omtime = &np->n_vattr->va_mtime;
   1903 		const struct timespec *octime = &np->n_vattr->va_ctime;
   1904 		const char *reason = NULL; /* XXX: gcc */
   1905 
   1906 		if (timespeccmp(omtime, mtime, <=)) {
   1907 			reason = "mtime";
   1908 			error = EINVAL;
   1909 		}
   1910 
   1911 		if (vp->v_type == VDIR && timespeccmp(octime, ctime, <=)) {
   1912 			reason = "ctime";
   1913 			error = EINVAL;
   1914 		}
   1915 
   1916 		nmp = VFSTONFS(vp->v_mount);
   1917 		if (error) {
   1918 
   1919 			/*
   1920 			 * despite of the fact that we've updated the file,
   1921 			 * timestamps of the file were not updated as we
   1922 			 * expected.
   1923 			 * it means that the server has incompatible
   1924 			 * semantics of timestamps or (more likely)
   1925 			 * the server time is not precise enough to
   1926 			 * track each modifications.
   1927 			 * in that case, we disable wcc processing.
   1928 			 *
   1929 			 * yes, strictly speaking, we should disable all
   1930 			 * caching.  it's a compromise.
   1931 			 */
   1932 
   1933 			mutex_enter(&nmp->nm_lock);
   1934 			if (!NFS_WCCKLUDGE(nmp, now)) {
   1935 				printf("%s: inaccurate wcc data (%s) detected,"
   1936 				    " disabling wcc"
   1937 				    " (ctime %u.%09u %u.%09u,"
   1938 				    " mtime %u.%09u %u.%09u)\n",
   1939 				    vp->v_mount->mnt_stat.f_mntfromname,
   1940 				    reason,
   1941 				    (unsigned int)octime->tv_sec,
   1942 				    (unsigned int)octime->tv_nsec,
   1943 				    (unsigned int)ctime->tv_sec,
   1944 				    (unsigned int)ctime->tv_nsec,
   1945 				    (unsigned int)omtime->tv_sec,
   1946 				    (unsigned int)omtime->tv_nsec,
   1947 				    (unsigned int)mtime->tv_sec,
   1948 				    (unsigned int)mtime->tv_nsec);
   1949 			}
   1950 			nmp->nm_iflag |= NFSMNT_WCCKLUDGE;
   1951 			nmp->nm_wcckludgetime = now;
   1952 			mutex_exit(&nmp->nm_lock);
   1953 		} else if (NFS_WCCKLUDGE(nmp, now)) {
   1954 			error = EPERM; /* XXX */
   1955 		} else if (nmp->nm_iflag & NFSMNT_WCCKLUDGE) {
   1956 			mutex_enter(&nmp->nm_lock);
   1957 			if (nmp->nm_iflag & NFSMNT_WCCKLUDGE) {
   1958 				printf("%s: re-enabling wcc\n",
   1959 				    vp->v_mount->mnt_stat.f_mntfromname);
   1960 				nmp->nm_iflag &= ~NFSMNT_WCCKLUDGE;
   1961 			}
   1962 			mutex_exit(&nmp->nm_lock);
   1963 		}
   1964 	}
   1965 
   1966 #endif /* !defined(NFS_V2_ONLY) */
   1967 
   1968 	return error;
   1969 }
   1970 
   1971 /*
   1972  * Heuristic to see if the server XDR encodes directory cookies or not.
   1973  * it is not supposed to, but a lot of servers may do this. Also, since
   1974  * most/all servers will implement V2 as well, it is expected that they
   1975  * may return just 32 bits worth of cookie information, so we need to
   1976  * find out in which 32 bits this information is available. We do this
   1977  * to avoid trouble with emulated binaries that can't handle 64 bit
   1978  * directory offsets.
   1979  */
   1980 
   1981 void
   1982 nfs_cookieheuristic(vp, flagp, l, cred)
   1983 	struct vnode *vp;
   1984 	int *flagp;
   1985 	struct lwp *l;
   1986 	kauth_cred_t cred;
   1987 {
   1988 	struct uio auio;
   1989 	struct iovec aiov;
   1990 	char *tbuf, *cp;
   1991 	struct dirent *dp;
   1992 	off_t *cookies = NULL, *cop;
   1993 	int error, eof, nc, len;
   1994 
   1995 	MALLOC(tbuf, void *, NFS_DIRFRAGSIZ, M_TEMP, M_WAITOK);
   1996 
   1997 	aiov.iov_base = tbuf;
   1998 	aiov.iov_len = NFS_DIRFRAGSIZ;
   1999 	auio.uio_iov = &aiov;
   2000 	auio.uio_iovcnt = 1;
   2001 	auio.uio_rw = UIO_READ;
   2002 	auio.uio_resid = NFS_DIRFRAGSIZ;
   2003 	auio.uio_offset = 0;
   2004 	UIO_SETUP_SYSSPACE(&auio);
   2005 
   2006 	error = VOP_READDIR(vp, &auio, cred, &eof, &cookies, &nc);
   2007 
   2008 	len = NFS_DIRFRAGSIZ - auio.uio_resid;
   2009 	if (error || len == 0) {
   2010 		FREE(tbuf, M_TEMP);
   2011 		if (cookies)
   2012 			free(cookies, M_TEMP);
   2013 		return;
   2014 	}
   2015 
   2016 	/*
   2017 	 * Find the first valid entry and look at its offset cookie.
   2018 	 */
   2019 
   2020 	cp = tbuf;
   2021 	for (cop = cookies; len > 0; len -= dp->d_reclen) {
   2022 		dp = (struct dirent *)cp;
   2023 		if (dp->d_fileno != 0 && len >= dp->d_reclen) {
   2024 			if ((*cop >> 32) != 0 && (*cop & 0xffffffffLL) == 0) {
   2025 				*flagp |= NFSMNT_SWAPCOOKIE;
   2026 				nfs_invaldircache(vp, 0);
   2027 				nfs_vinvalbuf(vp, 0, cred, l, 1);
   2028 			}
   2029 			break;
   2030 		}
   2031 		cop++;
   2032 		cp += dp->d_reclen;
   2033 	}
   2034 
   2035 	FREE(tbuf, M_TEMP);
   2036 	free(cookies, M_TEMP);
   2037 }
   2038 #endif /* NFS */
   2039 
   2040 #ifdef NFSSERVER
   2041 /*
   2042  * Set up nameidata for a lookup() call and do it.
   2043  *
   2044  * If pubflag is set, this call is done for a lookup operation on the
   2045  * public filehandle. In that case we allow crossing mountpoints and
   2046  * absolute pathnames. However, the caller is expected to check that
   2047  * the lookup result is within the public fs, and deny access if
   2048  * it is not.
   2049  */
   2050 int
   2051 nfs_namei(ndp, nsfh, len, slp, nam, mdp, dposp, retdirp, l, kerbflag, pubflag)
   2052 	struct nameidata *ndp;
   2053 	nfsrvfh_t *nsfh;
   2054 	uint32_t len;
   2055 	struct nfssvc_sock *slp;
   2056 	struct mbuf *nam;
   2057 	struct mbuf **mdp;
   2058 	char **dposp;
   2059 	struct vnode **retdirp;
   2060 	struct lwp *l;
   2061 	int kerbflag, pubflag;
   2062 {
   2063 	int i, rem;
   2064 	struct mbuf *md;
   2065 	char *fromcp, *tocp, *cp;
   2066 	struct iovec aiov;
   2067 	struct uio auio;
   2068 	struct vnode *dp;
   2069 	int error, rdonly, linklen;
   2070 	struct componentname *cnp = &ndp->ni_cnd;
   2071 
   2072 	*retdirp = NULL;
   2073 
   2074 	if ((len + 1) > MAXPATHLEN)
   2075 		return (ENAMETOOLONG);
   2076 	if (len == 0)
   2077 		return (EACCES);
   2078 	cnp->cn_pnbuf = PNBUF_GET();
   2079 
   2080 	/*
   2081 	 * Copy the name from the mbuf list to ndp->ni_pnbuf
   2082 	 * and set the various ndp fields appropriately.
   2083 	 */
   2084 	fromcp = *dposp;
   2085 	tocp = cnp->cn_pnbuf;
   2086 	md = *mdp;
   2087 	rem = mtod(md, char *) + md->m_len - fromcp;
   2088 	for (i = 0; i < len; i++) {
   2089 		while (rem == 0) {
   2090 			md = md->m_next;
   2091 			if (md == NULL) {
   2092 				error = EBADRPC;
   2093 				goto out;
   2094 			}
   2095 			fromcp = mtod(md, void *);
   2096 			rem = md->m_len;
   2097 		}
   2098 		if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
   2099 			error = EACCES;
   2100 			goto out;
   2101 		}
   2102 		*tocp++ = *fromcp++;
   2103 		rem--;
   2104 	}
   2105 	*tocp = '\0';
   2106 	*mdp = md;
   2107 	*dposp = fromcp;
   2108 	len = nfsm_rndup(len)-len;
   2109 	if (len > 0) {
   2110 		if (rem >= len)
   2111 			*dposp += len;
   2112 		else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
   2113 			goto out;
   2114 	}
   2115 
   2116 	/*
   2117 	 * Extract and set starting directory.
   2118 	 */
   2119 	error = nfsrv_fhtovp(nsfh, false, &dp, ndp->ni_cnd.cn_cred, slp,
   2120 	    nam, &rdonly, kerbflag, pubflag);
   2121 	if (error)
   2122 		goto out;
   2123 	if (dp->v_type != VDIR) {
   2124 		vrele(dp);
   2125 		error = ENOTDIR;
   2126 		goto out;
   2127 	}
   2128 
   2129 	if (rdonly)
   2130 		cnp->cn_flags |= RDONLY;
   2131 
   2132 	*retdirp = dp;
   2133 
   2134 	if (pubflag) {
   2135 		/*
   2136 		 * Oh joy. For WebNFS, handle those pesky '%' escapes,
   2137 		 * and the 'native path' indicator.
   2138 		 */
   2139 		cp = PNBUF_GET();
   2140 		fromcp = cnp->cn_pnbuf;
   2141 		tocp = cp;
   2142 		if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
   2143 			switch ((unsigned char)*fromcp) {
   2144 			case WEBNFS_NATIVE_CHAR:
   2145 				/*
   2146 				 * 'Native' path for us is the same
   2147 				 * as a path according to the NFS spec,
   2148 				 * just skip the escape char.
   2149 				 */
   2150 				fromcp++;
   2151 				break;
   2152 			/*
   2153 			 * More may be added in the future, range 0x80-0xff
   2154 			 */
   2155 			default:
   2156 				error = EIO;
   2157 				vrele(dp);
   2158 				PNBUF_PUT(cp);
   2159 				goto out;
   2160 			}
   2161 		}
   2162 		/*
   2163 		 * Translate the '%' escapes, URL-style.
   2164 		 */
   2165 		while (*fromcp != '\0') {
   2166 			if (*fromcp == WEBNFS_ESC_CHAR) {
   2167 				if (fromcp[1] != '\0' && fromcp[2] != '\0') {
   2168 					fromcp++;
   2169 					*tocp++ = HEXSTRTOI(fromcp);
   2170 					fromcp += 2;
   2171 					continue;
   2172 				} else {
   2173 					error = ENOENT;
   2174 					vrele(dp);
   2175 					PNBUF_PUT(cp);
   2176 					goto out;
   2177 				}
   2178 			} else
   2179 				*tocp++ = *fromcp++;
   2180 		}
   2181 		*tocp = '\0';
   2182 		PNBUF_PUT(cnp->cn_pnbuf);
   2183 		cnp->cn_pnbuf = cp;
   2184 	}
   2185 
   2186 	ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
   2187 	ndp->ni_segflg = UIO_SYSSPACE;
   2188 	ndp->ni_rootdir = rootvnode;
   2189 	ndp->ni_erootdir = NULL;
   2190 
   2191 	if (pubflag) {
   2192 		ndp->ni_loopcnt = 0;
   2193 		if (cnp->cn_pnbuf[0] == '/')
   2194 			dp = rootvnode;
   2195 	} else {
   2196 		cnp->cn_flags |= NOCROSSMOUNT;
   2197 	}
   2198 
   2199 	VREF(dp);
   2200 	vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
   2201 
   2202     for (;;) {
   2203 	cnp->cn_nameptr = cnp->cn_pnbuf;
   2204 	ndp->ni_startdir = dp;
   2205 
   2206 	/*
   2207 	 * And call lookup() to do the real work
   2208 	 */
   2209 	error = lookup(ndp);
   2210 	if (error) {
   2211 		if (ndp->ni_dvp) {
   2212 			vput(ndp->ni_dvp);
   2213 		}
   2214 		PNBUF_PUT(cnp->cn_pnbuf);
   2215 		return (error);
   2216 	}
   2217 
   2218 	/*
   2219 	 * Check for encountering a symbolic link
   2220 	 */
   2221 	if ((cnp->cn_flags & ISSYMLINK) == 0) {
   2222 		if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp) {
   2223 			if (ndp->ni_dvp == ndp->ni_vp) {
   2224 				vrele(ndp->ni_dvp);
   2225 			} else {
   2226 				vput(ndp->ni_dvp);
   2227 			}
   2228 		}
   2229 		if (cnp->cn_flags & (SAVENAME | SAVESTART))
   2230 			cnp->cn_flags |= HASBUF;
   2231 		else
   2232 			PNBUF_PUT(cnp->cn_pnbuf);
   2233 		return (0);
   2234 	} else {
   2235 		if (!pubflag) {
   2236 			error = EINVAL;
   2237 			break;
   2238 		}
   2239 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
   2240 			error = ELOOP;
   2241 			break;
   2242 		}
   2243 		if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
   2244 			error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred);
   2245 			if (error != 0)
   2246 				break;
   2247 		}
   2248 		if (ndp->ni_pathlen > 1)
   2249 			cp = PNBUF_GET();
   2250 		else
   2251 			cp = cnp->cn_pnbuf;
   2252 		aiov.iov_base = cp;
   2253 		aiov.iov_len = MAXPATHLEN;
   2254 		auio.uio_iov = &aiov;
   2255 		auio.uio_iovcnt = 1;
   2256 		auio.uio_offset = 0;
   2257 		auio.uio_rw = UIO_READ;
   2258 		auio.uio_resid = MAXPATHLEN;
   2259 		UIO_SETUP_SYSSPACE(&auio);
   2260 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
   2261 		if (error) {
   2262 badlink:
   2263 			if (ndp->ni_pathlen > 1)
   2264 				PNBUF_PUT(cp);
   2265 			break;
   2266 		}
   2267 		linklen = MAXPATHLEN - auio.uio_resid;
   2268 		if (linklen == 0) {
   2269 			error = ENOENT;
   2270 			goto badlink;
   2271 		}
   2272 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
   2273 			error = ENAMETOOLONG;
   2274 			goto badlink;
   2275 		}
   2276 		if (ndp->ni_pathlen > 1) {
   2277 			memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
   2278 			PNBUF_PUT(cnp->cn_pnbuf);
   2279 			cnp->cn_pnbuf = cp;
   2280 		} else
   2281 			cnp->cn_pnbuf[linklen] = '\0';
   2282 		ndp->ni_pathlen += linklen;
   2283 		vput(ndp->ni_vp);
   2284 		dp = ndp->ni_dvp;
   2285 
   2286 		/*
   2287 		 * Check if root directory should replace current directory.
   2288 		 */
   2289 		if (cnp->cn_pnbuf[0] == '/') {
   2290 			vput(dp);
   2291 			dp = ndp->ni_rootdir;
   2292 			VREF(dp);
   2293 			vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
   2294 		}
   2295 	}
   2296    }
   2297 	vput(ndp->ni_dvp);
   2298 	vput(ndp->ni_vp);
   2299 	ndp->ni_vp = NULL;
   2300 out:
   2301 	PNBUF_PUT(cnp->cn_pnbuf);
   2302 	return (error);
   2303 }
   2304 #endif /* NFSSERVER */
   2305 
   2306 /*
   2307  * A fiddled version of m_adj() that ensures null fill to a 32-bit
   2308  * boundary and only trims off the back end
   2309  *
   2310  * 1. trim off 'len' bytes as m_adj(mp, -len).
   2311  * 2. add zero-padding 'nul' bytes at the end of the mbuf chain.
   2312  */
   2313 void
   2314 nfs_zeropad(mp, len, nul)
   2315 	struct mbuf *mp;
   2316 	int len;
   2317 	int nul;
   2318 {
   2319 	struct mbuf *m;
   2320 	int count;
   2321 
   2322 	/*
   2323 	 * Trim from tail.  Scan the mbuf chain,
   2324 	 * calculating its length and finding the last mbuf.
   2325 	 * If the adjustment only affects this mbuf, then just
   2326 	 * adjust and return.  Otherwise, rescan and truncate
   2327 	 * after the remaining size.
   2328 	 */
   2329 	count = 0;
   2330 	m = mp;
   2331 	for (;;) {
   2332 		count += m->m_len;
   2333 		if (m->m_next == NULL)
   2334 			break;
   2335 		m = m->m_next;
   2336 	}
   2337 
   2338 	KDASSERT(count >= len);
   2339 
   2340 	if (m->m_len >= len) {
   2341 		m->m_len -= len;
   2342 	} else {
   2343 		count -= len;
   2344 		/*
   2345 		 * Correct length for chain is "count".
   2346 		 * Find the mbuf with last data, adjust its length,
   2347 		 * and toss data from remaining mbufs on chain.
   2348 		 */
   2349 		for (m = mp; m; m = m->m_next) {
   2350 			if (m->m_len >= count) {
   2351 				m->m_len = count;
   2352 				break;
   2353 			}
   2354 			count -= m->m_len;
   2355 		}
   2356 		KASSERT(m && m->m_next);
   2357 		m_freem(m->m_next);
   2358 		m->m_next = NULL;
   2359 	}
   2360 
   2361 	KDASSERT(m->m_next == NULL);
   2362 
   2363 	/*
   2364 	 * zero-padding.
   2365 	 */
   2366 	if (nul > 0) {
   2367 		char *cp;
   2368 		int i;
   2369 
   2370 		if (M_ROMAP(m) || M_TRAILINGSPACE(m) < nul) {
   2371 			struct mbuf *n;
   2372 
   2373 			KDASSERT(MLEN >= nul);
   2374 			n = m_get(M_WAIT, MT_DATA);
   2375 			MCLAIM(n, &nfs_mowner);
   2376 			n->m_len = nul;
   2377 			n->m_next = NULL;
   2378 			m->m_next = n;
   2379 			cp = mtod(n, void *);
   2380 		} else {
   2381 			cp = mtod(m, char *) + m->m_len;
   2382 			m->m_len += nul;
   2383 		}
   2384 		for (i = 0; i < nul; i++)
   2385 			*cp++ = '\0';
   2386 	}
   2387 	return;
   2388 }
   2389 
   2390 /*
   2391  * Make these functions instead of macros, so that the kernel text size
   2392  * doesn't get too big...
   2393  */
   2394 void
   2395 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
   2396 	struct nfsrv_descript *nfsd;
   2397 	int before_ret;
   2398 	struct vattr *before_vap;
   2399 	int after_ret;
   2400 	struct vattr *after_vap;
   2401 	struct mbuf **mbp;
   2402 	char **bposp;
   2403 {
   2404 	struct mbuf *mb = *mbp;
   2405 	char *bpos = *bposp;
   2406 	u_int32_t *tl;
   2407 
   2408 	if (before_ret) {
   2409 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   2410 		*tl = nfs_false;
   2411 	} else {
   2412 		nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
   2413 		*tl++ = nfs_true;
   2414 		txdr_hyper(before_vap->va_size, tl);
   2415 		tl += 2;
   2416 		txdr_nfsv3time(&(before_vap->va_mtime), tl);
   2417 		tl += 2;
   2418 		txdr_nfsv3time(&(before_vap->va_ctime), tl);
   2419 	}
   2420 	*bposp = bpos;
   2421 	*mbp = mb;
   2422 	nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
   2423 }
   2424 
   2425 void
   2426 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
   2427 	struct nfsrv_descript *nfsd;
   2428 	int after_ret;
   2429 	struct vattr *after_vap;
   2430 	struct mbuf **mbp;
   2431 	char **bposp;
   2432 {
   2433 	struct mbuf *mb = *mbp;
   2434 	char *bpos = *bposp;
   2435 	u_int32_t *tl;
   2436 	struct nfs_fattr *fp;
   2437 
   2438 	if (after_ret) {
   2439 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   2440 		*tl = nfs_false;
   2441 	} else {
   2442 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
   2443 		*tl++ = nfs_true;
   2444 		fp = (struct nfs_fattr *)tl;
   2445 		nfsm_srvfattr(nfsd, after_vap, fp);
   2446 	}
   2447 	*mbp = mb;
   2448 	*bposp = bpos;
   2449 }
   2450 
   2451 void
   2452 nfsm_srvfattr(nfsd, vap, fp)
   2453 	struct nfsrv_descript *nfsd;
   2454 	struct vattr *vap;
   2455 	struct nfs_fattr *fp;
   2456 {
   2457 
   2458 	fp->fa_nlink = txdr_unsigned(vap->va_nlink);
   2459 	fp->fa_uid = txdr_unsigned(vap->va_uid);
   2460 	fp->fa_gid = txdr_unsigned(vap->va_gid);
   2461 	if (nfsd->nd_flag & ND_NFSV3) {
   2462 		fp->fa_type = vtonfsv3_type(vap->va_type);
   2463 		fp->fa_mode = vtonfsv3_mode(vap->va_mode);
   2464 		txdr_hyper(vap->va_size, &fp->fa3_size);
   2465 		txdr_hyper(vap->va_bytes, &fp->fa3_used);
   2466 		fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
   2467 		fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
   2468 		fp->fa3_fsid.nfsuquad[0] = 0;
   2469 		fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
   2470 		txdr_hyper(vap->va_fileid, &fp->fa3_fileid);
   2471 		txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
   2472 		txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
   2473 		txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
   2474 	} else {
   2475 		fp->fa_type = vtonfsv2_type(vap->va_type);
   2476 		fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
   2477 		fp->fa2_size = txdr_unsigned(vap->va_size);
   2478 		fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
   2479 		if (vap->va_type == VFIFO)
   2480 			fp->fa2_rdev = 0xffffffff;
   2481 		else
   2482 			fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
   2483 		fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
   2484 		fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
   2485 		fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
   2486 		txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
   2487 		txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
   2488 		txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
   2489 	}
   2490 }
   2491 
   2492 #ifdef NFSSERVER
   2493 /*
   2494  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
   2495  * 	- look up fsid in mount list (if not found ret error)
   2496  *	- get vp and export rights by calling VFS_FHTOVP()
   2497  *	- if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
   2498  *	- if not lockflag unlock it with VOP_UNLOCK()
   2499  */
   2500 int
   2501 nfsrv_fhtovp(nfsrvfh_t *nsfh, int lockflag, struct vnode **vpp,
   2502     kauth_cred_t cred, struct nfssvc_sock *slp, struct mbuf *nam, int *rdonlyp,
   2503     int kerbflag, int pubflag)
   2504 {
   2505 	struct mount *mp;
   2506 	kauth_cred_t credanon;
   2507 	int error, exflags;
   2508 	struct sockaddr_in *saddr;
   2509 	fhandle_t *fhp;
   2510 
   2511 	fhp = NFSRVFH_FHANDLE(nsfh);
   2512 	*vpp = (struct vnode *)0;
   2513 
   2514 	if (nfs_ispublicfh(nsfh)) {
   2515 		if (!pubflag || !nfs_pub.np_valid)
   2516 			return (ESTALE);
   2517 		fhp = nfs_pub.np_handle;
   2518 	}
   2519 
   2520 	error = netexport_check(&fhp->fh_fsid, nam, &mp, &exflags, &credanon);
   2521 	if (error) {
   2522 		return error;
   2523 	}
   2524 
   2525 	error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
   2526 	if (error)
   2527 		return (error);
   2528 
   2529 	if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
   2530 		saddr = mtod(nam, struct sockaddr_in *);
   2531 		if ((saddr->sin_family == AF_INET) &&
   2532 		    ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
   2533 			vput(*vpp);
   2534 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
   2535 		}
   2536 #ifdef INET6
   2537 		if ((saddr->sin_family == AF_INET6) &&
   2538 		    ntohs(saddr->sin_port) >= IPV6PORT_RESERVED) {
   2539 			vput(*vpp);
   2540 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
   2541 		}
   2542 #endif
   2543 	}
   2544 	/*
   2545 	 * Check/setup credentials.
   2546 	 */
   2547 	if (exflags & MNT_EXKERB) {
   2548 		if (!kerbflag) {
   2549 			vput(*vpp);
   2550 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
   2551 		}
   2552 	} else if (kerbflag) {
   2553 		vput(*vpp);
   2554 		return (NFSERR_AUTHERR | AUTH_TOOWEAK);
   2555 	} else if (kauth_cred_geteuid(cred) == 0 || /* NFS maproot, see below */
   2556 	    (exflags & MNT_EXPORTANON)) {
   2557 		/*
   2558 		 * This is used by the NFS maproot option. While we can change
   2559 		 * the secmodel on our own host, we can't change it on the
   2560 		 * clients. As means of least surprise, we're doing the
   2561 		 * traditional thing here.
   2562 		 * Should look into adding a "mapprivileged" or similar where
   2563 		 * the users can be explicitly specified...
   2564 		 * [elad, yamt 2008-03-05]
   2565 		 */
   2566 		kauth_cred_clone(credanon, cred);
   2567 	}
   2568 	if (exflags & MNT_EXRDONLY)
   2569 		*rdonlyp = 1;
   2570 	else
   2571 		*rdonlyp = 0;
   2572 	if (!lockflag)
   2573 		VOP_UNLOCK(*vpp, 0);
   2574 	return (0);
   2575 }
   2576 
   2577 /*
   2578  * WebNFS: check if a filehandle is a public filehandle. For v3, this
   2579  * means a length of 0, for v2 it means all zeroes.
   2580  */
   2581 int
   2582 nfs_ispublicfh(const nfsrvfh_t *nsfh)
   2583 {
   2584 	const char *cp = (const void *)(NFSRVFH_DATA(nsfh));
   2585 	int i;
   2586 
   2587 	if (NFSRVFH_SIZE(nsfh) == 0) {
   2588 		return true;
   2589 	}
   2590 	if (NFSRVFH_SIZE(nsfh) != NFSX_V2FH) {
   2591 		return false;
   2592 	}
   2593 	for (i = 0; i < NFSX_V2FH; i++)
   2594 		if (*cp++ != 0)
   2595 			return false;
   2596 	return true;
   2597 }
   2598 #endif /* NFSSERVER */
   2599 
   2600 /*
   2601  * This function compares two net addresses by family and returns true
   2602  * if they are the same host.
   2603  * If there is any doubt, return false.
   2604  * The AF_INET family is handled as a special case so that address mbufs
   2605  * don't need to be saved to store "struct in_addr", which is only 4 bytes.
   2606  */
   2607 int
   2608 netaddr_match(family, haddr, nam)
   2609 	int family;
   2610 	union nethostaddr *haddr;
   2611 	struct mbuf *nam;
   2612 {
   2613 	struct sockaddr_in *inetaddr;
   2614 
   2615 	switch (family) {
   2616 	case AF_INET:
   2617 		inetaddr = mtod(nam, struct sockaddr_in *);
   2618 		if (inetaddr->sin_family == AF_INET &&
   2619 		    inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
   2620 			return (1);
   2621 		break;
   2622 #ifdef INET6
   2623 	case AF_INET6:
   2624 	    {
   2625 		struct sockaddr_in6 *sin6_1, *sin6_2;
   2626 
   2627 		sin6_1 = mtod(nam, struct sockaddr_in6 *);
   2628 		sin6_2 = mtod(haddr->had_nam, struct sockaddr_in6 *);
   2629 		if (sin6_1->sin6_family == AF_INET6 &&
   2630 		    IN6_ARE_ADDR_EQUAL(&sin6_1->sin6_addr, &sin6_2->sin6_addr))
   2631 			return 1;
   2632 	    }
   2633 #endif
   2634 #ifdef ISO
   2635 	case AF_ISO:
   2636 	    {
   2637 		struct sockaddr_iso *isoaddr1, *isoaddr2;
   2638 
   2639 		isoaddr1 = mtod(nam, struct sockaddr_iso *);
   2640 		isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
   2641 		if (isoaddr1->siso_family == AF_ISO &&
   2642 		    isoaddr1->siso_nlen > 0 &&
   2643 		    isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
   2644 		    SAME_ISOADDR(isoaddr1, isoaddr2))
   2645 			return (1);
   2646 		break;
   2647 	    }
   2648 #endif	/* ISO */
   2649 	default:
   2650 		break;
   2651 	};
   2652 	return (0);
   2653 }
   2654 
   2655 /*
   2656  * The write verifier has changed (probably due to a server reboot), so all
   2657  * PG_NEEDCOMMIT pages will have to be written again. Since they are marked
   2658  * as dirty or are being written out just now, all this takes is clearing
   2659  * the PG_NEEDCOMMIT flag. Once done the new write verifier can be set for
   2660  * the mount point.
   2661  */
   2662 void
   2663 nfs_clearcommit(mp)
   2664 	struct mount *mp;
   2665 {
   2666 	struct vnode *vp;
   2667 	struct nfsnode *np;
   2668 	struct vm_page *pg;
   2669 	struct nfsmount *nmp = VFSTONFS(mp);
   2670 
   2671 	rw_enter(&nmp->nm_writeverflock, RW_WRITER);
   2672 	mutex_enter(&mntvnode_lock);
   2673 	TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
   2674 		KASSERT(vp->v_mount == mp);
   2675 		if (vp->v_type != VREG)
   2676 			continue;
   2677 		np = VTONFS(vp);
   2678 		np->n_pushlo = np->n_pushhi = np->n_pushedlo =
   2679 		    np->n_pushedhi = 0;
   2680 		np->n_commitflags &=
   2681 		    ~(NFS_COMMIT_PUSH_VALID | NFS_COMMIT_PUSHED_VALID);
   2682 		mutex_enter(&vp->v_uobj.vmobjlock);
   2683 		TAILQ_FOREACH(pg, &vp->v_uobj.memq, listq.queue) {
   2684 			pg->flags &= ~PG_NEEDCOMMIT;
   2685 		}
   2686 		mutex_exit(&vp->v_uobj.vmobjlock);
   2687 	}
   2688 	mutex_exit(&mntvnode_lock);
   2689 	mutex_enter(&nmp->nm_lock);
   2690 	nmp->nm_iflag &= ~NFSMNT_STALEWRITEVERF;
   2691 	mutex_exit(&nmp->nm_lock);
   2692 	rw_exit(&nmp->nm_writeverflock);
   2693 }
   2694 
   2695 void
   2696 nfs_merge_commit_ranges(vp)
   2697 	struct vnode *vp;
   2698 {
   2699 	struct nfsnode *np = VTONFS(vp);
   2700 
   2701 	KASSERT(np->n_commitflags & NFS_COMMIT_PUSH_VALID);
   2702 
   2703 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
   2704 		np->n_pushedlo = np->n_pushlo;
   2705 		np->n_pushedhi = np->n_pushhi;
   2706 		np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
   2707 	} else {
   2708 		if (np->n_pushlo < np->n_pushedlo)
   2709 			np->n_pushedlo = np->n_pushlo;
   2710 		if (np->n_pushhi > np->n_pushedhi)
   2711 			np->n_pushedhi = np->n_pushhi;
   2712 	}
   2713 
   2714 	np->n_pushlo = np->n_pushhi = 0;
   2715 	np->n_commitflags &= ~NFS_COMMIT_PUSH_VALID;
   2716 
   2717 #ifdef NFS_DEBUG_COMMIT
   2718 	printf("merge: committed: %u - %u\n", (unsigned)np->n_pushedlo,
   2719 	    (unsigned)np->n_pushedhi);
   2720 #endif
   2721 }
   2722 
   2723 int
   2724 nfs_in_committed_range(vp, off, len)
   2725 	struct vnode *vp;
   2726 	off_t off, len;
   2727 {
   2728 	struct nfsnode *np = VTONFS(vp);
   2729 	off_t lo, hi;
   2730 
   2731 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
   2732 		return 0;
   2733 	lo = off;
   2734 	hi = lo + len;
   2735 
   2736 	return (lo >= np->n_pushedlo && hi <= np->n_pushedhi);
   2737 }
   2738 
   2739 int
   2740 nfs_in_tobecommitted_range(vp, off, len)
   2741 	struct vnode *vp;
   2742 	off_t off, len;
   2743 {
   2744 	struct nfsnode *np = VTONFS(vp);
   2745 	off_t lo, hi;
   2746 
   2747 	if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
   2748 		return 0;
   2749 	lo = off;
   2750 	hi = lo + len;
   2751 
   2752 	return (lo >= np->n_pushlo && hi <= np->n_pushhi);
   2753 }
   2754 
   2755 void
   2756 nfs_add_committed_range(vp, off, len)
   2757 	struct vnode *vp;
   2758 	off_t off, len;
   2759 {
   2760 	struct nfsnode *np = VTONFS(vp);
   2761 	off_t lo, hi;
   2762 
   2763 	lo = off;
   2764 	hi = lo + len;
   2765 
   2766 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
   2767 		np->n_pushedlo = lo;
   2768 		np->n_pushedhi = hi;
   2769 		np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
   2770 	} else {
   2771 		if (hi > np->n_pushedhi)
   2772 			np->n_pushedhi = hi;
   2773 		if (lo < np->n_pushedlo)
   2774 			np->n_pushedlo = lo;
   2775 	}
   2776 #ifdef NFS_DEBUG_COMMIT
   2777 	printf("add: committed: %u - %u\n", (unsigned)np->n_pushedlo,
   2778 	    (unsigned)np->n_pushedhi);
   2779 #endif
   2780 }
   2781 
   2782 void
   2783 nfs_del_committed_range(vp, off, len)
   2784 	struct vnode *vp;
   2785 	off_t off, len;
   2786 {
   2787 	struct nfsnode *np = VTONFS(vp);
   2788 	off_t lo, hi;
   2789 
   2790 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
   2791 		return;
   2792 
   2793 	lo = off;
   2794 	hi = lo + len;
   2795 
   2796 	if (lo > np->n_pushedhi || hi < np->n_pushedlo)
   2797 		return;
   2798 	if (lo <= np->n_pushedlo)
   2799 		np->n_pushedlo = hi;
   2800 	else if (hi >= np->n_pushedhi)
   2801 		np->n_pushedhi = lo;
   2802 	else {
   2803 		/*
   2804 		 * XXX There's only one range. If the deleted range
   2805 		 * is in the middle, pick the largest of the
   2806 		 * contiguous ranges that it leaves.
   2807 		 */
   2808 		if ((np->n_pushedlo - lo) > (hi - np->n_pushedhi))
   2809 			np->n_pushedhi = lo;
   2810 		else
   2811 			np->n_pushedlo = hi;
   2812 	}
   2813 #ifdef NFS_DEBUG_COMMIT
   2814 	printf("del: committed: %u - %u\n", (unsigned)np->n_pushedlo,
   2815 	    (unsigned)np->n_pushedhi);
   2816 #endif
   2817 }
   2818 
   2819 void
   2820 nfs_add_tobecommitted_range(vp, off, len)
   2821 	struct vnode *vp;
   2822 	off_t off, len;
   2823 {
   2824 	struct nfsnode *np = VTONFS(vp);
   2825 	off_t lo, hi;
   2826 
   2827 	lo = off;
   2828 	hi = lo + len;
   2829 
   2830 	if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID)) {
   2831 		np->n_pushlo = lo;
   2832 		np->n_pushhi = hi;
   2833 		np->n_commitflags |= NFS_COMMIT_PUSH_VALID;
   2834 	} else {
   2835 		if (lo < np->n_pushlo)
   2836 			np->n_pushlo = lo;
   2837 		if (hi > np->n_pushhi)
   2838 			np->n_pushhi = hi;
   2839 	}
   2840 #ifdef NFS_DEBUG_COMMIT
   2841 	printf("add: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
   2842 	    (unsigned)np->n_pushhi);
   2843 #endif
   2844 }
   2845 
   2846 void
   2847 nfs_del_tobecommitted_range(vp, off, len)
   2848 	struct vnode *vp;
   2849 	off_t off, len;
   2850 {
   2851 	struct nfsnode *np = VTONFS(vp);
   2852 	off_t lo, hi;
   2853 
   2854 	if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
   2855 		return;
   2856 
   2857 	lo = off;
   2858 	hi = lo + len;
   2859 
   2860 	if (lo > np->n_pushhi || hi < np->n_pushlo)
   2861 		return;
   2862 
   2863 	if (lo <= np->n_pushlo)
   2864 		np->n_pushlo = hi;
   2865 	else if (hi >= np->n_pushhi)
   2866 		np->n_pushhi = lo;
   2867 	else {
   2868 		/*
   2869 		 * XXX There's only one range. If the deleted range
   2870 		 * is in the middle, pick the largest of the
   2871 		 * contiguous ranges that it leaves.
   2872 		 */
   2873 		if ((np->n_pushlo - lo) > (hi - np->n_pushhi))
   2874 			np->n_pushhi = lo;
   2875 		else
   2876 			np->n_pushlo = hi;
   2877 	}
   2878 #ifdef NFS_DEBUG_COMMIT
   2879 	printf("del: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
   2880 	    (unsigned)np->n_pushhi);
   2881 #endif
   2882 }
   2883 
   2884 /*
   2885  * Map errnos to NFS error numbers. For Version 3 also filter out error
   2886  * numbers not specified for the associated procedure.
   2887  */
   2888 int
   2889 nfsrv_errmap(nd, err)
   2890 	struct nfsrv_descript *nd;
   2891 	int err;
   2892 {
   2893 	const short *defaulterrp, *errp;
   2894 
   2895 	if (nd->nd_flag & ND_NFSV3) {
   2896 	    if (nd->nd_procnum <= NFSPROC_COMMIT) {
   2897 		errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
   2898 		while (*++errp) {
   2899 			if (*errp == err)
   2900 				return (err);
   2901 			else if (*errp > err)
   2902 				break;
   2903 		}
   2904 		return ((int)*defaulterrp);
   2905 	    } else
   2906 		return (err & 0xffff);
   2907 	}
   2908 	if (err <= ELAST)
   2909 		return ((int)nfsrv_v2errmap[err - 1]);
   2910 	return (NFSERR_IO);
   2911 }
   2912 
   2913 u_int32_t
   2914 nfs_getxid()
   2915 {
   2916 	u_int32_t newxid;
   2917 
   2918 	/* get next xid.  skip 0 */
   2919 	do {
   2920 		newxid = atomic_inc_32_nv(&nfs_xid);
   2921 	} while (__predict_false(newxid == 0));
   2922 
   2923 	return txdr_unsigned(newxid);
   2924 }
   2925 
   2926 /*
   2927  * assign a new xid for existing request.
   2928  * used for NFSERR_JUKEBOX handling.
   2929  */
   2930 void
   2931 nfs_renewxid(struct nfsreq *req)
   2932 {
   2933 	u_int32_t xid;
   2934 	int off;
   2935 
   2936 	xid = nfs_getxid();
   2937 	if (req->r_nmp->nm_sotype == SOCK_STREAM)
   2938 		off = sizeof(u_int32_t); /* RPC record mark */
   2939 	else
   2940 		off = 0;
   2941 
   2942 	m_copyback(req->r_mreq, off, sizeof(xid), (void *)&xid);
   2943 	req->r_xid = xid;
   2944 }
   2945 
   2946 #if defined(NFSSERVER)
   2947 int
   2948 nfsrv_composefh(struct vnode *vp, nfsrvfh_t *nsfh, bool v3)
   2949 {
   2950 	int error;
   2951 	size_t fhsize;
   2952 
   2953 	fhsize = NFSD_MAXFHSIZE;
   2954 	error = vfs_composefh(vp, (void *)NFSRVFH_DATA(nsfh), &fhsize);
   2955 	if (NFSX_FHTOOBIG_P(fhsize, v3)) {
   2956 		error = EOPNOTSUPP;
   2957 	}
   2958 	if (error != 0) {
   2959 		return error;
   2960 	}
   2961 	if (!v3 && fhsize < NFSX_V2FH) {
   2962 		memset((char *)NFSRVFH_DATA(nsfh) + fhsize, 0,
   2963 		    NFSX_V2FH - fhsize);
   2964 		fhsize = NFSX_V2FH;
   2965 	}
   2966 	if ((fhsize % NFSX_UNSIGNED) != 0) {
   2967 		return EOPNOTSUPP;
   2968 	}
   2969 	nsfh->nsfh_size = fhsize;
   2970 	return 0;
   2971 }
   2972 
   2973 int
   2974 nfsrv_comparefh(const nfsrvfh_t *fh1, const nfsrvfh_t *fh2)
   2975 {
   2976 
   2977 	if (NFSRVFH_SIZE(fh1) != NFSRVFH_SIZE(fh2)) {
   2978 		return NFSRVFH_SIZE(fh2) - NFSRVFH_SIZE(fh1);
   2979 	}
   2980 	return memcmp(NFSRVFH_DATA(fh1), NFSRVFH_DATA(fh2), NFSRVFH_SIZE(fh1));
   2981 }
   2982 
   2983 void
   2984 nfsrv_copyfh(nfsrvfh_t *fh1, const nfsrvfh_t *fh2)
   2985 {
   2986 	size_t size;
   2987 
   2988 	fh1->nsfh_size = size = NFSRVFH_SIZE(fh2);
   2989 	memcpy(NFSRVFH_DATA(fh1), NFSRVFH_DATA(fh2), size);
   2990 }
   2991 #endif /* defined(NFSSERVER) */
   2992 
   2993 #if defined(NFS)
   2994 /*
   2995  * Set the attribute timeout based on how recently the file has been modified.
   2996  */
   2997 
   2998 time_t
   2999 nfs_attrtimeo(struct nfsmount *nmp, struct nfsnode *np)
   3000 {
   3001 	time_t timeo;
   3002 
   3003 	if ((nmp->nm_flag & NFSMNT_NOAC) != 0)
   3004 		return 0;
   3005 
   3006 	if (((np)->n_flag & NMODIFIED) != 0)
   3007 		return NFS_MINATTRTIMO;
   3008 
   3009 	timeo = (time_second - np->n_mtime.tv_sec) / 10;
   3010 	timeo = max(timeo, NFS_MINATTRTIMO);
   3011 	timeo = min(timeo, NFS_MAXATTRTIMO);
   3012 	return timeo;
   3013 }
   3014 #endif /* defined(NFS) */
   3015