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