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