Home | History | Annotate | Line # | Download | only in nfs
nfs_subs.c revision 1.221.2.6
      1 /*	$NetBSD: nfs_subs.c,v 1.221.2.6 2014/05/22 19:11:37 yamt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)nfs_subs.c	8.8 (Berkeley) 5/22/95
     35  */
     36 
     37 /*
     38  * Copyright 2000 Wasabi Systems, Inc.
     39  * All rights reserved.
     40  *
     41  * Written by Frank van der Linden for Wasabi Systems, Inc.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *      This product includes software developed for the NetBSD Project by
     54  *      Wasabi Systems, Inc.
     55  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     56  *    or promote products derived from this software without specific prior
     57  *    written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     61  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     62  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     63  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     64  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     65  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     66  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     67  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     68  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     69  * POSSIBILITY OF SUCH DAMAGE.
     70  */
     71 
     72 #include <sys/cdefs.h>
     73 __KERNEL_RCSID(0, "$NetBSD: nfs_subs.c,v 1.221.2.6 2014/05/22 19:11:37 yamt Exp $");
     74 
     75 #ifdef _KERNEL_OPT
     76 #include "opt_nfs.h"
     77 #endif
     78 
     79 /*
     80  * These functions support the macros and help fiddle mbuf chains for
     81  * the nfs op functions. They do things like create the rpc header and
     82  * copy data between mbuf chains and uio lists.
     83  */
     84 #include <sys/param.h>
     85 #include <sys/proc.h>
     86 #include <sys/systm.h>
     87 #include <sys/kernel.h>
     88 #include <sys/kmem.h>
     89 #include <sys/mount.h>
     90 #include <sys/vnode.h>
     91 #include <sys/namei.h>
     92 #include <sys/mbuf.h>
     93 #include <sys/socket.h>
     94 #include <sys/stat.h>
     95 #include <sys/filedesc.h>
     96 #include <sys/time.h>
     97 #include <sys/dirent.h>
     98 #include <sys/once.h>
     99 #include <sys/kauth.h>
    100 #include <sys/atomic.h>
    101 #include <sys/cprng.h>
    102 
    103 #include <uvm/uvm.h>
    104 #include <uvm/uvm_page_array.h>
    105 
    106 #include <nfs/rpcv2.h>
    107 #include <nfs/nfsproto.h>
    108 #include <nfs/nfsnode.h>
    109 #include <nfs/nfs.h>
    110 #include <nfs/xdr_subs.h>
    111 #include <nfs/nfsm_subs.h>
    112 #include <nfs/nfsmount.h>
    113 #include <nfs/nfsrtt.h>
    114 #include <nfs/nfs_var.h>
    115 
    116 #include <miscfs/specfs/specdev.h>
    117 
    118 #include <netinet/in.h>
    119 
    120 static u_int32_t nfs_xid;
    121 
    122 int nuidhash_max = NFS_MAXUIDHASH;
    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, 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 
    144 /* NFS client/server stats. */
    145 struct nfsstats nfsstats;
    146 
    147 /*
    148  * Mapping of old NFS Version 2 RPC numbers to generic numbers.
    149  */
    150 const int nfsv3_procid[NFS_NPROCS] = {
    151 	NFSPROC_NULL,
    152 	NFSPROC_GETATTR,
    153 	NFSPROC_SETATTR,
    154 	NFSPROC_NOOP,
    155 	NFSPROC_LOOKUP,
    156 	NFSPROC_READLINK,
    157 	NFSPROC_READ,
    158 	NFSPROC_NOOP,
    159 	NFSPROC_WRITE,
    160 	NFSPROC_CREATE,
    161 	NFSPROC_REMOVE,
    162 	NFSPROC_RENAME,
    163 	NFSPROC_LINK,
    164 	NFSPROC_SYMLINK,
    165 	NFSPROC_MKDIR,
    166 	NFSPROC_RMDIR,
    167 	NFSPROC_READDIR,
    168 	NFSPROC_FSSTAT,
    169 	NFSPROC_NOOP,
    170 	NFSPROC_NOOP,
    171 	NFSPROC_NOOP,
    172 	NFSPROC_NOOP,
    173 	NFSPROC_NOOP
    174 };
    175 
    176 /*
    177  * and the reverse mapping from generic to Version 2 procedure numbers
    178  */
    179 const int nfsv2_procid[NFS_NPROCS] = {
    180 	NFSV2PROC_NULL,
    181 	NFSV2PROC_GETATTR,
    182 	NFSV2PROC_SETATTR,
    183 	NFSV2PROC_LOOKUP,
    184 	NFSV2PROC_NOOP,
    185 	NFSV2PROC_READLINK,
    186 	NFSV2PROC_READ,
    187 	NFSV2PROC_WRITE,
    188 	NFSV2PROC_CREATE,
    189 	NFSV2PROC_MKDIR,
    190 	NFSV2PROC_SYMLINK,
    191 	NFSV2PROC_CREATE,
    192 	NFSV2PROC_REMOVE,
    193 	NFSV2PROC_RMDIR,
    194 	NFSV2PROC_RENAME,
    195 	NFSV2PROC_LINK,
    196 	NFSV2PROC_READDIR,
    197 	NFSV2PROC_NOOP,
    198 	NFSV2PROC_STATFS,
    199 	NFSV2PROC_NOOP,
    200 	NFSV2PROC_NOOP,
    201 	NFSV2PROC_NOOP,
    202 	NFSV2PROC_NOOP,
    203 };
    204 
    205 /*
    206  * Maps errno values to nfs error numbers.
    207  * Use NFSERR_IO as the catch all for ones not specifically defined in
    208  * RFC 1094.
    209  */
    210 static const u_char nfsrv_v2errmap[ELAST] = {
    211   NFSERR_PERM,	NFSERR_NOENT,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    212   NFSERR_NXIO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    213   NFSERR_IO,	NFSERR_IO,	NFSERR_ACCES,	NFSERR_IO,	NFSERR_IO,
    214   NFSERR_IO,	NFSERR_EXIST,	NFSERR_IO,	NFSERR_NODEV,	NFSERR_NOTDIR,
    215   NFSERR_ISDIR,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    216   NFSERR_IO,	NFSERR_FBIG,	NFSERR_NOSPC,	NFSERR_IO,	NFSERR_ROFS,
    217   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    218   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    219   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    220   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    221   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    222   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    223   NFSERR_IO,	NFSERR_IO,	NFSERR_NAMETOL,	NFSERR_IO,	NFSERR_IO,
    224   NFSERR_NOTEMPTY, NFSERR_IO,	NFSERR_IO,	NFSERR_DQUOT,	NFSERR_STALE,
    225   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    226   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    227   NFSERR_IO,	NFSERR_IO,
    228 };
    229 
    230 /*
    231  * Maps errno values to nfs error numbers.
    232  * Although it is not obvious whether or not NFS clients really care if
    233  * a returned error value is in the specified list for the procedure, the
    234  * safest thing to do is filter them appropriately. For Version 2, the
    235  * X/Open XNFS document is the only specification that defines error values
    236  * for each RPC (The RFC simply lists all possible error values for all RPCs),
    237  * so I have decided to not do this for Version 2.
    238  * The first entry is the default error return and the rest are the valid
    239  * errors for that RPC in increasing numeric order.
    240  */
    241 static const short nfsv3err_null[] = {
    242 	0,
    243 	0,
    244 };
    245 
    246 static const short nfsv3err_getattr[] = {
    247 	NFSERR_IO,
    248 	NFSERR_IO,
    249 	NFSERR_STALE,
    250 	NFSERR_BADHANDLE,
    251 	NFSERR_SERVERFAULT,
    252 	0,
    253 };
    254 
    255 static const short nfsv3err_setattr[] = {
    256 	NFSERR_IO,
    257 	NFSERR_PERM,
    258 	NFSERR_IO,
    259 	NFSERR_ACCES,
    260 	NFSERR_INVAL,
    261 	NFSERR_NOSPC,
    262 	NFSERR_ROFS,
    263 	NFSERR_DQUOT,
    264 	NFSERR_STALE,
    265 	NFSERR_BADHANDLE,
    266 	NFSERR_NOT_SYNC,
    267 	NFSERR_SERVERFAULT,
    268 	0,
    269 };
    270 
    271 static const short nfsv3err_lookup[] = {
    272 	NFSERR_IO,
    273 	NFSERR_NOENT,
    274 	NFSERR_IO,
    275 	NFSERR_ACCES,
    276 	NFSERR_NOTDIR,
    277 	NFSERR_NAMETOL,
    278 	NFSERR_STALE,
    279 	NFSERR_BADHANDLE,
    280 	NFSERR_SERVERFAULT,
    281 	0,
    282 };
    283 
    284 static const short nfsv3err_access[] = {
    285 	NFSERR_IO,
    286 	NFSERR_IO,
    287 	NFSERR_STALE,
    288 	NFSERR_BADHANDLE,
    289 	NFSERR_SERVERFAULT,
    290 	0,
    291 };
    292 
    293 static const short nfsv3err_readlink[] = {
    294 	NFSERR_IO,
    295 	NFSERR_IO,
    296 	NFSERR_ACCES,
    297 	NFSERR_INVAL,
    298 	NFSERR_STALE,
    299 	NFSERR_BADHANDLE,
    300 	NFSERR_NOTSUPP,
    301 	NFSERR_SERVERFAULT,
    302 	0,
    303 };
    304 
    305 static const short nfsv3err_read[] = {
    306 	NFSERR_IO,
    307 	NFSERR_IO,
    308 	NFSERR_NXIO,
    309 	NFSERR_ACCES,
    310 	NFSERR_INVAL,
    311 	NFSERR_STALE,
    312 	NFSERR_BADHANDLE,
    313 	NFSERR_SERVERFAULT,
    314 	NFSERR_JUKEBOX,
    315 	0,
    316 };
    317 
    318 static const short nfsv3err_write[] = {
    319 	NFSERR_IO,
    320 	NFSERR_IO,
    321 	NFSERR_ACCES,
    322 	NFSERR_INVAL,
    323 	NFSERR_FBIG,
    324 	NFSERR_NOSPC,
    325 	NFSERR_ROFS,
    326 	NFSERR_DQUOT,
    327 	NFSERR_STALE,
    328 	NFSERR_BADHANDLE,
    329 	NFSERR_SERVERFAULT,
    330 	NFSERR_JUKEBOX,
    331 	0,
    332 };
    333 
    334 static const short nfsv3err_create[] = {
    335 	NFSERR_IO,
    336 	NFSERR_IO,
    337 	NFSERR_ACCES,
    338 	NFSERR_EXIST,
    339 	NFSERR_NOTDIR,
    340 	NFSERR_NOSPC,
    341 	NFSERR_ROFS,
    342 	NFSERR_NAMETOL,
    343 	NFSERR_DQUOT,
    344 	NFSERR_STALE,
    345 	NFSERR_BADHANDLE,
    346 	NFSERR_NOTSUPP,
    347 	NFSERR_SERVERFAULT,
    348 	0,
    349 };
    350 
    351 static const short nfsv3err_mkdir[] = {
    352 	NFSERR_IO,
    353 	NFSERR_IO,
    354 	NFSERR_ACCES,
    355 	NFSERR_EXIST,
    356 	NFSERR_NOTDIR,
    357 	NFSERR_NOSPC,
    358 	NFSERR_ROFS,
    359 	NFSERR_NAMETOL,
    360 	NFSERR_DQUOT,
    361 	NFSERR_STALE,
    362 	NFSERR_BADHANDLE,
    363 	NFSERR_NOTSUPP,
    364 	NFSERR_SERVERFAULT,
    365 	0,
    366 };
    367 
    368 static const short nfsv3err_symlink[] = {
    369 	NFSERR_IO,
    370 	NFSERR_IO,
    371 	NFSERR_ACCES,
    372 	NFSERR_EXIST,
    373 	NFSERR_NOTDIR,
    374 	NFSERR_NOSPC,
    375 	NFSERR_ROFS,
    376 	NFSERR_NAMETOL,
    377 	NFSERR_DQUOT,
    378 	NFSERR_STALE,
    379 	NFSERR_BADHANDLE,
    380 	NFSERR_NOTSUPP,
    381 	NFSERR_SERVERFAULT,
    382 	0,
    383 };
    384 
    385 static const short nfsv3err_mknod[] = {
    386 	NFSERR_IO,
    387 	NFSERR_IO,
    388 	NFSERR_ACCES,
    389 	NFSERR_EXIST,
    390 	NFSERR_NOTDIR,
    391 	NFSERR_NOSPC,
    392 	NFSERR_ROFS,
    393 	NFSERR_NAMETOL,
    394 	NFSERR_DQUOT,
    395 	NFSERR_STALE,
    396 	NFSERR_BADHANDLE,
    397 	NFSERR_NOTSUPP,
    398 	NFSERR_SERVERFAULT,
    399 	NFSERR_BADTYPE,
    400 	0,
    401 };
    402 
    403 static const short nfsv3err_remove[] = {
    404 	NFSERR_IO,
    405 	NFSERR_NOENT,
    406 	NFSERR_IO,
    407 	NFSERR_ACCES,
    408 	NFSERR_NOTDIR,
    409 	NFSERR_ROFS,
    410 	NFSERR_NAMETOL,
    411 	NFSERR_STALE,
    412 	NFSERR_BADHANDLE,
    413 	NFSERR_SERVERFAULT,
    414 	0,
    415 };
    416 
    417 static const short nfsv3err_rmdir[] = {
    418 	NFSERR_IO,
    419 	NFSERR_NOENT,
    420 	NFSERR_IO,
    421 	NFSERR_ACCES,
    422 	NFSERR_EXIST,
    423 	NFSERR_NOTDIR,
    424 	NFSERR_INVAL,
    425 	NFSERR_ROFS,
    426 	NFSERR_NAMETOL,
    427 	NFSERR_NOTEMPTY,
    428 	NFSERR_STALE,
    429 	NFSERR_BADHANDLE,
    430 	NFSERR_NOTSUPP,
    431 	NFSERR_SERVERFAULT,
    432 	0,
    433 };
    434 
    435 static const short nfsv3err_rename[] = {
    436 	NFSERR_IO,
    437 	NFSERR_NOENT,
    438 	NFSERR_IO,
    439 	NFSERR_ACCES,
    440 	NFSERR_EXIST,
    441 	NFSERR_XDEV,
    442 	NFSERR_NOTDIR,
    443 	NFSERR_ISDIR,
    444 	NFSERR_INVAL,
    445 	NFSERR_NOSPC,
    446 	NFSERR_ROFS,
    447 	NFSERR_MLINK,
    448 	NFSERR_NAMETOL,
    449 	NFSERR_NOTEMPTY,
    450 	NFSERR_DQUOT,
    451 	NFSERR_STALE,
    452 	NFSERR_BADHANDLE,
    453 	NFSERR_NOTSUPP,
    454 	NFSERR_SERVERFAULT,
    455 	0,
    456 };
    457 
    458 static const short nfsv3err_link[] = {
    459 	NFSERR_IO,
    460 	NFSERR_IO,
    461 	NFSERR_ACCES,
    462 	NFSERR_EXIST,
    463 	NFSERR_XDEV,
    464 	NFSERR_NOTDIR,
    465 	NFSERR_INVAL,
    466 	NFSERR_NOSPC,
    467 	NFSERR_ROFS,
    468 	NFSERR_MLINK,
    469 	NFSERR_NAMETOL,
    470 	NFSERR_DQUOT,
    471 	NFSERR_STALE,
    472 	NFSERR_BADHANDLE,
    473 	NFSERR_NOTSUPP,
    474 	NFSERR_SERVERFAULT,
    475 	0,
    476 };
    477 
    478 static const short nfsv3err_readdir[] = {
    479 	NFSERR_IO,
    480 	NFSERR_IO,
    481 	NFSERR_ACCES,
    482 	NFSERR_NOTDIR,
    483 	NFSERR_STALE,
    484 	NFSERR_BADHANDLE,
    485 	NFSERR_BAD_COOKIE,
    486 	NFSERR_TOOSMALL,
    487 	NFSERR_SERVERFAULT,
    488 	0,
    489 };
    490 
    491 static const short nfsv3err_readdirplus[] = {
    492 	NFSERR_IO,
    493 	NFSERR_IO,
    494 	NFSERR_ACCES,
    495 	NFSERR_NOTDIR,
    496 	NFSERR_STALE,
    497 	NFSERR_BADHANDLE,
    498 	NFSERR_BAD_COOKIE,
    499 	NFSERR_NOTSUPP,
    500 	NFSERR_TOOSMALL,
    501 	NFSERR_SERVERFAULT,
    502 	0,
    503 };
    504 
    505 static const short nfsv3err_fsstat[] = {
    506 	NFSERR_IO,
    507 	NFSERR_IO,
    508 	NFSERR_STALE,
    509 	NFSERR_BADHANDLE,
    510 	NFSERR_SERVERFAULT,
    511 	0,
    512 };
    513 
    514 static const short nfsv3err_fsinfo[] = {
    515 	NFSERR_STALE,
    516 	NFSERR_STALE,
    517 	NFSERR_BADHANDLE,
    518 	NFSERR_SERVERFAULT,
    519 	0,
    520 };
    521 
    522 static const short nfsv3err_pathconf[] = {
    523 	NFSERR_STALE,
    524 	NFSERR_STALE,
    525 	NFSERR_BADHANDLE,
    526 	NFSERR_SERVERFAULT,
    527 	0,
    528 };
    529 
    530 static const short nfsv3err_commit[] = {
    531 	NFSERR_IO,
    532 	NFSERR_IO,
    533 	NFSERR_STALE,
    534 	NFSERR_BADHANDLE,
    535 	NFSERR_SERVERFAULT,
    536 	0,
    537 };
    538 
    539 static const short * const nfsrv_v3errmap[] = {
    540 	nfsv3err_null,
    541 	nfsv3err_getattr,
    542 	nfsv3err_setattr,
    543 	nfsv3err_lookup,
    544 	nfsv3err_access,
    545 	nfsv3err_readlink,
    546 	nfsv3err_read,
    547 	nfsv3err_write,
    548 	nfsv3err_create,
    549 	nfsv3err_mkdir,
    550 	nfsv3err_symlink,
    551 	nfsv3err_mknod,
    552 	nfsv3err_remove,
    553 	nfsv3err_rmdir,
    554 	nfsv3err_rename,
    555 	nfsv3err_link,
    556 	nfsv3err_readdir,
    557 	nfsv3err_readdirplus,
    558 	nfsv3err_fsstat,
    559 	nfsv3err_fsinfo,
    560 	nfsv3err_pathconf,
    561 	nfsv3err_commit,
    562 };
    563 
    564 extern struct nfsrtt nfsrtt;
    565 
    566 u_long nfsdirhashmask;
    567 
    568 int nfs_webnamei(struct nameidata *, struct vnode *, struct proc *);
    569 
    570 /*
    571  * Create the header for an rpc request packet
    572  * The hsiz is the size of the rest of the nfs request header.
    573  * (just used to decide if a cluster is a good idea)
    574  */
    575 struct mbuf *
    576 nfsm_reqh(struct nfsnode *np, u_long procid, int hsiz, char **bposp)
    577 {
    578 	struct mbuf *mb;
    579 	char *bpos;
    580 
    581 	mb = m_get(M_WAIT, MT_DATA);
    582 	MCLAIM(mb, &nfs_mowner);
    583 	if (hsiz >= MINCLSIZE)
    584 		m_clget(mb, M_WAIT);
    585 	mb->m_len = 0;
    586 	bpos = mtod(mb, void *);
    587 
    588 	/* Finally, return values */
    589 	*bposp = bpos;
    590 	return (mb);
    591 }
    592 
    593 /*
    594  * Build the RPC header and fill in the authorization info.
    595  * The authorization string argument is only used when the credentials
    596  * come from outside of the kernel.
    597  * Returns the head of the mbuf list.
    598  */
    599 struct mbuf *
    600 nfsm_rpchead(kauth_cred_t cr, int nmflag, int procid,
    601 	int auth_type, int auth_len, char *auth_str, int verf_len,
    602 	char *verf_str, struct mbuf *mrest, int mrest_len,
    603 	struct mbuf **mbp, uint32_t *xidp)
    604 {
    605 	struct mbuf *mb;
    606 	u_int32_t *tl;
    607 	char *bpos;
    608 	int i;
    609 	struct mbuf *mreq;
    610 	int siz, grpsiz, authsiz;
    611 
    612 	authsiz = nfsm_rndup(auth_len);
    613 	mb = m_gethdr(M_WAIT, MT_DATA);
    614 	MCLAIM(mb, &nfs_mowner);
    615 	if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
    616 		m_clget(mb, M_WAIT);
    617 	} else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
    618 		MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
    619 	} else {
    620 		MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
    621 	}
    622 	mb->m_len = 0;
    623 	mreq = mb;
    624 	bpos = mtod(mb, void *);
    625 
    626 	/*
    627 	 * First the RPC header.
    628 	 */
    629 	nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
    630 
    631 	*tl++ = *xidp = nfs_getxid();
    632 	*tl++ = rpc_call;
    633 	*tl++ = rpc_vers;
    634 	*tl++ = txdr_unsigned(NFS_PROG);
    635 	if (nmflag & NFSMNT_NFSV3)
    636 		*tl++ = txdr_unsigned(NFS_VER3);
    637 	else
    638 		*tl++ = txdr_unsigned(NFS_VER2);
    639 	if (nmflag & NFSMNT_NFSV3)
    640 		*tl++ = txdr_unsigned(procid);
    641 	else
    642 		*tl++ = txdr_unsigned(nfsv2_procid[procid]);
    643 
    644 	/*
    645 	 * And then the authorization cred.
    646 	 */
    647 	*tl++ = txdr_unsigned(auth_type);
    648 	*tl = txdr_unsigned(authsiz);
    649 	switch (auth_type) {
    650 	case RPCAUTH_UNIX:
    651 		nfsm_build(tl, u_int32_t *, auth_len);
    652 		*tl++ = 0;		/* stamp ?? */
    653 		*tl++ = 0;		/* NULL hostname */
    654 		*tl++ = txdr_unsigned(kauth_cred_geteuid(cr));
    655 		*tl++ = txdr_unsigned(kauth_cred_getegid(cr));
    656 		grpsiz = (auth_len >> 2) - 5;
    657 		*tl++ = txdr_unsigned(grpsiz);
    658 		for (i = 0; i < grpsiz; i++)
    659 			*tl++ = txdr_unsigned(kauth_cred_group(cr, i)); /* XXX elad review */
    660 		break;
    661 	case RPCAUTH_KERB4:
    662 		siz = auth_len;
    663 		while (siz > 0) {
    664 			if (M_TRAILINGSPACE(mb) == 0) {
    665 				struct mbuf *mb2;
    666 				mb2 = m_get(M_WAIT, MT_DATA);
    667 				MCLAIM(mb2, &nfs_mowner);
    668 				if (siz >= MINCLSIZE)
    669 					m_clget(mb2, M_WAIT);
    670 				mb->m_next = mb2;
    671 				mb = mb2;
    672 				mb->m_len = 0;
    673 				bpos = mtod(mb, void *);
    674 			}
    675 			i = min(siz, M_TRAILINGSPACE(mb));
    676 			memcpy(bpos, auth_str, i);
    677 			mb->m_len += i;
    678 			auth_str += i;
    679 			bpos += i;
    680 			siz -= i;
    681 		}
    682 		if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
    683 			for (i = 0; i < siz; i++)
    684 				*bpos++ = '\0';
    685 			mb->m_len += siz;
    686 		}
    687 		break;
    688 	};
    689 
    690 	/*
    691 	 * And the verifier...
    692 	 */
    693 	nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
    694 	if (verf_str) {
    695 		*tl++ = txdr_unsigned(RPCAUTH_KERB4);
    696 		*tl = txdr_unsigned(verf_len);
    697 		siz = verf_len;
    698 		while (siz > 0) {
    699 			if (M_TRAILINGSPACE(mb) == 0) {
    700 				struct mbuf *mb2;
    701 				mb2 = m_get(M_WAIT, MT_DATA);
    702 				MCLAIM(mb2, &nfs_mowner);
    703 				if (siz >= MINCLSIZE)
    704 					m_clget(mb2, M_WAIT);
    705 				mb->m_next = mb2;
    706 				mb = mb2;
    707 				mb->m_len = 0;
    708 				bpos = mtod(mb, void *);
    709 			}
    710 			i = min(siz, M_TRAILINGSPACE(mb));
    711 			memcpy(bpos, verf_str, i);
    712 			mb->m_len += i;
    713 			verf_str += i;
    714 			bpos += i;
    715 			siz -= i;
    716 		}
    717 		if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
    718 			for (i = 0; i < siz; i++)
    719 				*bpos++ = '\0';
    720 			mb->m_len += siz;
    721 		}
    722 	} else {
    723 		*tl++ = txdr_unsigned(RPCAUTH_NULL);
    724 		*tl = 0;
    725 	}
    726 	mb->m_next = mrest;
    727 	mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
    728 	mreq->m_pkthdr.rcvif = (struct ifnet *)0;
    729 	*mbp = mb;
    730 	return (mreq);
    731 }
    732 
    733 /*
    734  * copies mbuf chain to the uio scatter/gather list
    735  */
    736 int
    737 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, char **dpos)
    738 {
    739 	char *mbufcp, *uiocp;
    740 	int xfer, left, len;
    741 	struct mbuf *mp;
    742 	long uiosiz, rem;
    743 	int error = 0;
    744 
    745 	mp = *mrep;
    746 	mbufcp = *dpos;
    747 	len = mtod(mp, char *) + mp->m_len - mbufcp;
    748 	rem = nfsm_rndup(siz)-siz;
    749 	while (siz > 0) {
    750 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
    751 			return (EFBIG);
    752 		left = uiop->uio_iov->iov_len;
    753 		uiocp = uiop->uio_iov->iov_base;
    754 		if (left > siz)
    755 			left = siz;
    756 		uiosiz = left;
    757 		while (left > 0) {
    758 			while (len == 0) {
    759 				mp = mp->m_next;
    760 				if (mp == NULL)
    761 					return (EBADRPC);
    762 				mbufcp = mtod(mp, void *);
    763 				len = mp->m_len;
    764 			}
    765 			xfer = (left > len) ? len : left;
    766 			error = copyout_vmspace(uiop->uio_vmspace, mbufcp,
    767 			    uiocp, xfer);
    768 			if (error) {
    769 				return error;
    770 			}
    771 			left -= xfer;
    772 			len -= xfer;
    773 			mbufcp += xfer;
    774 			uiocp += xfer;
    775 			uiop->uio_offset += xfer;
    776 			uiop->uio_resid -= xfer;
    777 		}
    778 		if (uiop->uio_iov->iov_len <= siz) {
    779 			uiop->uio_iovcnt--;
    780 			uiop->uio_iov++;
    781 		} else {
    782 			uiop->uio_iov->iov_base =
    783 			    (char *)uiop->uio_iov->iov_base + uiosiz;
    784 			uiop->uio_iov->iov_len -= uiosiz;
    785 		}
    786 		siz -= uiosiz;
    787 	}
    788 	*dpos = mbufcp;
    789 	*mrep = mp;
    790 	if (rem > 0) {
    791 		if (len < rem)
    792 			error = nfs_adv(mrep, dpos, rem, len);
    793 		else
    794 			*dpos += rem;
    795 	}
    796 	return (error);
    797 }
    798 
    799 /*
    800  * copies a uio scatter/gather list to an mbuf chain.
    801  * NOTE: can ony handle iovcnt == 1
    802  */
    803 int
    804 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, char **bpos)
    805 {
    806 	char *uiocp;
    807 	struct mbuf *mp, *mp2;
    808 	int xfer, left, mlen;
    809 	int uiosiz, clflg, rem;
    810 	char *cp;
    811 	int error;
    812 
    813 #ifdef DIAGNOSTIC
    814 	if (uiop->uio_iovcnt != 1)
    815 		panic("nfsm_uiotombuf: iovcnt != 1");
    816 #endif
    817 
    818 	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
    819 		clflg = 1;
    820 	else
    821 		clflg = 0;
    822 	rem = nfsm_rndup(siz)-siz;
    823 	mp = mp2 = *mq;
    824 	while (siz > 0) {
    825 		left = uiop->uio_iov->iov_len;
    826 		uiocp = uiop->uio_iov->iov_base;
    827 		if (left > siz)
    828 			left = siz;
    829 		uiosiz = left;
    830 		while (left > 0) {
    831 			mlen = M_TRAILINGSPACE(mp);
    832 			if (mlen == 0) {
    833 				mp = m_get(M_WAIT, MT_DATA);
    834 				MCLAIM(mp, &nfs_mowner);
    835 				if (clflg)
    836 					m_clget(mp, M_WAIT);
    837 				mp->m_len = 0;
    838 				mp2->m_next = mp;
    839 				mp2 = mp;
    840 				mlen = M_TRAILINGSPACE(mp);
    841 			}
    842 			xfer = (left > mlen) ? mlen : left;
    843 			cp = mtod(mp, char *) + mp->m_len;
    844 			error = copyin_vmspace(uiop->uio_vmspace, uiocp, cp,
    845 			    xfer);
    846 			if (error) {
    847 				/* XXX */
    848 			}
    849 			mp->m_len += xfer;
    850 			left -= xfer;
    851 			uiocp += xfer;
    852 			uiop->uio_offset += xfer;
    853 			uiop->uio_resid -= xfer;
    854 		}
    855 		uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base +
    856 		    uiosiz;
    857 		uiop->uio_iov->iov_len -= uiosiz;
    858 		siz -= uiosiz;
    859 	}
    860 	if (rem > 0) {
    861 		if (rem > M_TRAILINGSPACE(mp)) {
    862 			mp = m_get(M_WAIT, MT_DATA);
    863 			MCLAIM(mp, &nfs_mowner);
    864 			mp->m_len = 0;
    865 			mp2->m_next = mp;
    866 		}
    867 		cp = mtod(mp, char *) + mp->m_len;
    868 		for (left = 0; left < rem; left++)
    869 			*cp++ = '\0';
    870 		mp->m_len += rem;
    871 		*bpos = cp;
    872 	} else
    873 		*bpos = mtod(mp, char *) + mp->m_len;
    874 	*mq = mp;
    875 	return (0);
    876 }
    877 
    878 /*
    879  * Get at least "siz" bytes of correctly aligned data.
    880  * When called the mbuf pointers are not necessarily correct,
    881  * dsosp points to what ought to be in m_data and left contains
    882  * what ought to be in m_len.
    883  * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
    884  * cases. (The macros use the vars. dpos and dpos2)
    885  */
    886 int
    887 nfsm_disct(struct mbuf **mdp, char **dposp, int siz, int left, char **cp2)
    888 {
    889 	struct mbuf *m1, *m2;
    890 	struct mbuf *havebuf = NULL;
    891 	char *src = *dposp;
    892 	char *dst;
    893 	int len;
    894 
    895 #ifdef DEBUG
    896 	if (left < 0)
    897 		panic("nfsm_disct: left < 0");
    898 #endif
    899 	m1 = *mdp;
    900 	/*
    901 	 * Skip through the mbuf chain looking for an mbuf with
    902 	 * some data. If the first mbuf found has enough data
    903 	 * and it is correctly aligned return it.
    904 	 */
    905 	while (left == 0) {
    906 		havebuf = m1;
    907 		*mdp = m1 = m1->m_next;
    908 		if (m1 == NULL)
    909 			return (EBADRPC);
    910 		src = mtod(m1, void *);
    911 		left = m1->m_len;
    912 		/*
    913 		 * If we start a new mbuf and it is big enough
    914 		 * and correctly aligned just return it, don't
    915 		 * do any pull up.
    916 		 */
    917 		if (left >= siz && nfsm_aligned(src)) {
    918 			*cp2 = src;
    919 			*dposp = src + siz;
    920 			return (0);
    921 		}
    922 	}
    923 	if ((m1->m_flags & M_EXT) != 0) {
    924 		if (havebuf && M_TRAILINGSPACE(havebuf) >= siz &&
    925 		    nfsm_aligned(mtod(havebuf, char *) + havebuf->m_len)) {
    926 			/*
    927 			 * If the first mbuf with data has external data
    928 			 * and there is a previous mbuf with some trailing
    929 			 * space, use it to move the data into.
    930 			 */
    931 			m2 = m1;
    932 			*mdp = m1 = havebuf;
    933 			*cp2 = mtod(m1, char *) + m1->m_len;
    934 		} else if (havebuf) {
    935 			/*
    936 			 * If the first mbuf has a external data
    937 			 * and there is no previous empty mbuf
    938 			 * allocate a new mbuf and move the external
    939 			 * data to the new mbuf. Also make the first
    940 			 * mbuf look empty.
    941 			 */
    942 			m2 = m1;
    943 			*mdp = m1 = m_get(M_WAIT, MT_DATA);
    944 			MCLAIM(m1, m2->m_owner);
    945 			if ((m2->m_flags & M_PKTHDR) != 0) {
    946 				/* XXX MOVE */
    947 				M_COPY_PKTHDR(m1, m2);
    948 				m_tag_delete_chain(m2, NULL);
    949 				m2->m_flags &= ~M_PKTHDR;
    950 			}
    951 			if (havebuf) {
    952 				havebuf->m_next = m1;
    953 			}
    954 			m1->m_next = m2;
    955 			MRESETDATA(m1);
    956 			m1->m_len = 0;
    957 			m2->m_data = src;
    958 			m2->m_len = left;
    959 			*cp2 = mtod(m1, char *);
    960 		} else {
    961 			struct mbuf **nextp = &m1->m_next;
    962 
    963 			m1->m_len -= left;
    964 			do {
    965 				m2 = m_get(M_WAIT, MT_DATA);
    966 				MCLAIM(m2, m1->m_owner);
    967 				if (left >= MINCLSIZE) {
    968 					MCLGET(m2, M_WAIT);
    969 				}
    970 				m2->m_next = *nextp;
    971 				*nextp = m2;
    972 				nextp = &m2->m_next;
    973 				len = (m2->m_flags & M_EXT) != 0 ?
    974 				    MCLBYTES : MLEN;
    975 				if (len > left) {
    976 					len = left;
    977 				}
    978 				memcpy(mtod(m2, char *), src, len);
    979 				m2->m_len = len;
    980 				src += len;
    981 				left -= len;
    982 			} while (left > 0);
    983 			*mdp = m1 = m1->m_next;
    984 			m2 = m1->m_next;
    985 			*cp2 = mtod(m1, char *);
    986 		}
    987 	} else {
    988 		/*
    989 		 * If the first mbuf has no external data
    990 		 * move the data to the front of the mbuf.
    991 		 */
    992 		MRESETDATA(m1);
    993 		dst = mtod(m1, char *);
    994 		if (dst != src) {
    995 			memmove(dst, src, left);
    996 		}
    997 		m1->m_len = left;
    998 		m2 = m1->m_next;
    999 		*cp2 = m1->m_data;
   1000 	}
   1001 	*dposp = *cp2 + siz;
   1002 	/*
   1003 	 * Loop through mbufs pulling data up into first mbuf until
   1004 	 * the first mbuf is full or there is no more data to
   1005 	 * pullup.
   1006 	 */
   1007 	dst = mtod(m1, char *) + m1->m_len;
   1008 	while ((len = M_TRAILINGSPACE(m1)) != 0 && m2) {
   1009 		if ((len = min(len, m2->m_len)) != 0) {
   1010 			memcpy(dst, mtod(m2, char *), len);
   1011 		}
   1012 		m1->m_len += len;
   1013 		dst += len;
   1014 		m2->m_data += len;
   1015 		m2->m_len -= len;
   1016 		m2 = m2->m_next;
   1017 	}
   1018 	if (m1->m_len < siz)
   1019 		return (EBADRPC);
   1020 	return (0);
   1021 }
   1022 
   1023 /*
   1024  * Advance the position in the mbuf chain.
   1025  */
   1026 int
   1027 nfs_adv(struct mbuf **mdp, char **dposp, int offs, int left)
   1028 {
   1029 	struct mbuf *m;
   1030 	int s;
   1031 
   1032 	m = *mdp;
   1033 	s = left;
   1034 	while (s < offs) {
   1035 		offs -= s;
   1036 		m = m->m_next;
   1037 		if (m == NULL)
   1038 			return (EBADRPC);
   1039 		s = m->m_len;
   1040 	}
   1041 	*mdp = m;
   1042 	*dposp = mtod(m, char *) + offs;
   1043 	return (0);
   1044 }
   1045 
   1046 /*
   1047  * Copy a string into mbufs for the hard cases...
   1048  */
   1049 int
   1050 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
   1051 {
   1052 	struct mbuf *m1 = NULL, *m2;
   1053 	long left, xfer, len, tlen;
   1054 	u_int32_t *tl;
   1055 	int putsize;
   1056 
   1057 	putsize = 1;
   1058 	m2 = *mb;
   1059 	left = M_TRAILINGSPACE(m2);
   1060 	if (left > 0) {
   1061 		tl = ((u_int32_t *)(*bpos));
   1062 		*tl++ = txdr_unsigned(siz);
   1063 		putsize = 0;
   1064 		left -= NFSX_UNSIGNED;
   1065 		m2->m_len += NFSX_UNSIGNED;
   1066 		if (left > 0) {
   1067 			memcpy((void *) tl, cp, left);
   1068 			siz -= left;
   1069 			cp += left;
   1070 			m2->m_len += left;
   1071 			left = 0;
   1072 		}
   1073 	}
   1074 	/* Loop around adding mbufs */
   1075 	while (siz > 0) {
   1076 		m1 = m_get(M_WAIT, MT_DATA);
   1077 		MCLAIM(m1, &nfs_mowner);
   1078 		if (siz > MLEN)
   1079 			m_clget(m1, M_WAIT);
   1080 		m1->m_len = NFSMSIZ(m1);
   1081 		m2->m_next = m1;
   1082 		m2 = m1;
   1083 		tl = mtod(m1, u_int32_t *);
   1084 		tlen = 0;
   1085 		if (putsize) {
   1086 			*tl++ = txdr_unsigned(siz);
   1087 			m1->m_len -= NFSX_UNSIGNED;
   1088 			tlen = NFSX_UNSIGNED;
   1089 			putsize = 0;
   1090 		}
   1091 		if (siz < m1->m_len) {
   1092 			len = nfsm_rndup(siz);
   1093 			xfer = siz;
   1094 			if (xfer < len)
   1095 				*(tl+(xfer>>2)) = 0;
   1096 		} else {
   1097 			xfer = len = m1->m_len;
   1098 		}
   1099 		memcpy((void *) tl, cp, xfer);
   1100 		m1->m_len = len+tlen;
   1101 		siz -= xfer;
   1102 		cp += xfer;
   1103 	}
   1104 	*mb = m1;
   1105 	*bpos = mtod(m1, char *) + m1->m_len;
   1106 	return (0);
   1107 }
   1108 
   1109 /*
   1110  * Directory caching routines. They work as follows:
   1111  * - a cache is maintained per VDIR nfsnode.
   1112  * - for each offset cookie that is exported to userspace, and can
   1113  *   thus be thrown back at us as an offset to VOP_READDIR, store
   1114  *   information in the cache.
   1115  * - cached are:
   1116  *   - cookie itself
   1117  *   - blocknumber (essentially just a search key in the buffer cache)
   1118  *   - entry number in block.
   1119  *   - offset cookie of block in which this entry is stored
   1120  *   - 32 bit cookie if NFSMNT_XLATECOOKIE is used.
   1121  * - entries are looked up in a hash table
   1122  * - also maintained is an LRU list of entries, used to determine
   1123  *   which ones to delete if the cache grows too large.
   1124  * - if 32 <-> 64 translation mode is requested for a filesystem,
   1125  *   the cache also functions as a translation table
   1126  * - in the translation case, invalidating the cache does not mean
   1127  *   flushing it, but just marking entries as invalid, except for
   1128  *   the <64bit cookie, 32bitcookie> pair which is still valid, to
   1129  *   still be able to use the cache as a translation table.
   1130  * - 32 bit cookies are uniquely created by combining the hash table
   1131  *   entry value, and one generation count per hash table entry,
   1132  *   incremented each time an entry is appended to the chain.
   1133  * - the cache is invalidated each time a direcory is modified
   1134  * - sanity checks are also done; if an entry in a block turns
   1135  *   out not to have a matching cookie, the cache is invalidated
   1136  *   and a new block starting from the wanted offset is fetched from
   1137  *   the server.
   1138  * - directory entries as read from the server are extended to contain
   1139  *   the 64bit and, optionally, the 32bit cookies, for sanity checking
   1140  *   the cache and exporting them to userspace through the cookie
   1141  *   argument to VOP_READDIR.
   1142  */
   1143 
   1144 u_long
   1145 nfs_dirhash(off_t off)
   1146 {
   1147 	int i;
   1148 	char *cp = (char *)&off;
   1149 	u_long sum = 0L;
   1150 
   1151 	for (i = 0 ; i < sizeof (off); i++)
   1152 		sum += *cp++;
   1153 
   1154 	return sum;
   1155 }
   1156 
   1157 #define	_NFSDC_MTX(np)		(NFSTOV(np)->v_interlock)
   1158 #define	NFSDC_LOCK(np)		mutex_enter(_NFSDC_MTX(np))
   1159 #define	NFSDC_UNLOCK(np)	mutex_exit(_NFSDC_MTX(np))
   1160 #define	NFSDC_ASSERT_LOCKED(np) KASSERT(mutex_owned(_NFSDC_MTX(np)))
   1161 
   1162 void
   1163 nfs_initdircache(struct vnode *vp)
   1164 {
   1165 	struct nfsnode *np = VTONFS(vp);
   1166 	struct nfsdirhashhead *dircache;
   1167 
   1168 	dircache = hashinit(NFS_DIRHASHSIZ, HASH_LIST, true,
   1169 	    &nfsdirhashmask);
   1170 
   1171 	NFSDC_LOCK(np);
   1172 	if (np->n_dircache == NULL) {
   1173 		np->n_dircachesize = 0;
   1174 		np->n_dircache = dircache;
   1175 		dircache = NULL;
   1176 		TAILQ_INIT(&np->n_dirchain);
   1177 	}
   1178 	NFSDC_UNLOCK(np);
   1179 	if (dircache)
   1180 		hashdone(dircache, HASH_LIST, nfsdirhashmask);
   1181 }
   1182 
   1183 void
   1184 nfs_initdirxlatecookie(struct vnode *vp)
   1185 {
   1186 	struct nfsnode *np = VTONFS(vp);
   1187 	unsigned *dirgens;
   1188 
   1189 	KASSERT(VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_XLATECOOKIE);
   1190 
   1191 	dirgens = kmem_zalloc(NFS_DIRHASHSIZ * sizeof(unsigned), KM_SLEEP);
   1192 	NFSDC_LOCK(np);
   1193 	if (np->n_dirgens == NULL) {
   1194 		np->n_dirgens = dirgens;
   1195 		dirgens = NULL;
   1196 	}
   1197 	NFSDC_UNLOCK(np);
   1198 	if (dirgens)
   1199 		kmem_free(dirgens, NFS_DIRHASHSIZ * sizeof(unsigned));
   1200 }
   1201 
   1202 static const struct nfsdircache dzero;
   1203 
   1204 static void nfs_unlinkdircache(struct nfsnode *np, struct nfsdircache *);
   1205 static void nfs_putdircache_unlocked(struct nfsnode *,
   1206     struct nfsdircache *);
   1207 
   1208 static void
   1209 nfs_unlinkdircache(struct nfsnode *np, struct nfsdircache *ndp)
   1210 {
   1211 
   1212 	NFSDC_ASSERT_LOCKED(np);
   1213 	KASSERT(ndp != &dzero);
   1214 
   1215 	if (LIST_NEXT(ndp, dc_hash) == (void *)-1)
   1216 		return;
   1217 
   1218 	TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain);
   1219 	LIST_REMOVE(ndp, dc_hash);
   1220 	LIST_NEXT(ndp, dc_hash) = (void *)-1; /* mark as unlinked */
   1221 
   1222 	nfs_putdircache_unlocked(np, ndp);
   1223 }
   1224 
   1225 void
   1226 nfs_putdircache(struct nfsnode *np, struct nfsdircache *ndp)
   1227 {
   1228 	int ref;
   1229 
   1230 	if (ndp == &dzero)
   1231 		return;
   1232 
   1233 	KASSERT(ndp->dc_refcnt > 0);
   1234 	NFSDC_LOCK(np);
   1235 	ref = --ndp->dc_refcnt;
   1236 	NFSDC_UNLOCK(np);
   1237 
   1238 	if (ref == 0)
   1239 		kmem_free(ndp, sizeof(*ndp));
   1240 }
   1241 
   1242 static void
   1243 nfs_putdircache_unlocked(struct nfsnode *np, struct nfsdircache *ndp)
   1244 {
   1245 	int ref;
   1246 
   1247 	NFSDC_ASSERT_LOCKED(np);
   1248 
   1249 	if (ndp == &dzero)
   1250 		return;
   1251 
   1252 	KASSERT(ndp->dc_refcnt > 0);
   1253 	ref = --ndp->dc_refcnt;
   1254 	if (ref == 0)
   1255 		kmem_free(ndp, sizeof(*ndp));
   1256 }
   1257 
   1258 struct nfsdircache *
   1259 nfs_searchdircache(struct vnode *vp, off_t off, int do32, int *hashent)
   1260 {
   1261 	struct nfsdirhashhead *ndhp;
   1262 	struct nfsdircache *ndp = NULL;
   1263 	struct nfsnode *np = VTONFS(vp);
   1264 	unsigned ent;
   1265 
   1266 	/*
   1267 	 * Zero is always a valid cookie.
   1268 	 */
   1269 	if (off == 0)
   1270 		/* XXXUNCONST */
   1271 		return (struct nfsdircache *)__UNCONST(&dzero);
   1272 
   1273 	if (!np->n_dircache)
   1274 		return NULL;
   1275 
   1276 	/*
   1277 	 * We use a 32bit cookie as search key, directly reconstruct
   1278 	 * the hashentry. Else use the hashfunction.
   1279 	 */
   1280 	if (do32) {
   1281 		ent = (u_int32_t)off >> 24;
   1282 		if (ent >= NFS_DIRHASHSIZ)
   1283 			return NULL;
   1284 		ndhp = &np->n_dircache[ent];
   1285 	} else {
   1286 		ndhp = NFSDIRHASH(np, off);
   1287 	}
   1288 
   1289 	if (hashent)
   1290 		*hashent = (int)(ndhp - np->n_dircache);
   1291 
   1292 	NFSDC_LOCK(np);
   1293 	if (do32) {
   1294 		LIST_FOREACH(ndp, ndhp, dc_hash) {
   1295 			if (ndp->dc_cookie32 == (u_int32_t)off) {
   1296 				/*
   1297 				 * An invalidated entry will become the
   1298 				 * start of a new block fetched from
   1299 				 * the server.
   1300 				 */
   1301 				if (ndp->dc_flags & NFSDC_INVALID) {
   1302 					ndp->dc_blkcookie = ndp->dc_cookie;
   1303 					ndp->dc_entry = 0;
   1304 					ndp->dc_flags &= ~NFSDC_INVALID;
   1305 				}
   1306 				break;
   1307 			}
   1308 		}
   1309 	} else {
   1310 		LIST_FOREACH(ndp, ndhp, dc_hash) {
   1311 			if (ndp->dc_cookie == off)
   1312 				break;
   1313 		}
   1314 	}
   1315 	if (ndp != NULL)
   1316 		ndp->dc_refcnt++;
   1317 	NFSDC_UNLOCK(np);
   1318 	return ndp;
   1319 }
   1320 
   1321 
   1322 struct nfsdircache *
   1323 nfs_enterdircache(struct vnode *vp, off_t off, off_t blkoff, int en,
   1324     daddr_t blkno)
   1325 {
   1326 	struct nfsnode *np = VTONFS(vp);
   1327 	struct nfsdirhashhead *ndhp;
   1328 	struct nfsdircache *ndp = NULL;
   1329 	struct nfsdircache *newndp = NULL;
   1330 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1331 	int hashent = 0, gen, overwrite;	/* XXX: GCC */
   1332 
   1333 	/*
   1334 	 * XXX refuse entries for offset 0. amd(8) erroneously sets
   1335 	 * cookie 0 for the '.' entry, making this necessary. This
   1336 	 * isn't so bad, as 0 is a special case anyway.
   1337 	 */
   1338 	if (off == 0)
   1339 		/* XXXUNCONST */
   1340 		return (struct nfsdircache *)__UNCONST(&dzero);
   1341 
   1342 	if (!np->n_dircache)
   1343 		/*
   1344 		 * XXX would like to do this in nfs_nget but vtype
   1345 		 * isn't known at that time.
   1346 		 */
   1347 		nfs_initdircache(vp);
   1348 
   1349 	if ((nmp->nm_flag & NFSMNT_XLATECOOKIE) && !np->n_dirgens)
   1350 		nfs_initdirxlatecookie(vp);
   1351 
   1352 retry:
   1353 	ndp = nfs_searchdircache(vp, off, 0, &hashent);
   1354 
   1355 	NFSDC_LOCK(np);
   1356 	if (ndp && (ndp->dc_flags & NFSDC_INVALID) == 0) {
   1357 		/*
   1358 		 * Overwriting an old entry. Check if it's the same.
   1359 		 * If so, just return. If not, remove the old entry.
   1360 		 */
   1361 		if (ndp->dc_blkcookie == blkoff && ndp->dc_entry == en)
   1362 			goto done;
   1363 		nfs_unlinkdircache(np, ndp);
   1364 		nfs_putdircache_unlocked(np, ndp);
   1365 		ndp = NULL;
   1366 	}
   1367 
   1368 	ndhp = &np->n_dircache[hashent];
   1369 
   1370 	if (!ndp) {
   1371 		if (newndp == NULL) {
   1372 			NFSDC_UNLOCK(np);
   1373 			newndp = kmem_alloc(sizeof(*newndp), KM_SLEEP);
   1374 			newndp->dc_refcnt = 1;
   1375 			LIST_NEXT(newndp, dc_hash) = (void *)-1;
   1376 			goto retry;
   1377 		}
   1378 		ndp = newndp;
   1379 		newndp = NULL;
   1380 		overwrite = 0;
   1381 		if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
   1382 			/*
   1383 			 * We're allocating a new entry, so bump the
   1384 			 * generation number.
   1385 			 */
   1386 			KASSERT(np->n_dirgens);
   1387 			gen = ++np->n_dirgens[hashent];
   1388 			if (gen == 0) {
   1389 				np->n_dirgens[hashent]++;
   1390 				gen++;
   1391 			}
   1392 			ndp->dc_cookie32 = (hashent << 24) | (gen & 0xffffff);
   1393 		}
   1394 	} else
   1395 		overwrite = 1;
   1396 
   1397 	ndp->dc_cookie = off;
   1398 	ndp->dc_blkcookie = blkoff;
   1399 	ndp->dc_entry = en;
   1400 	ndp->dc_flags = 0;
   1401 
   1402 	if (overwrite)
   1403 		goto done;
   1404 
   1405 	/*
   1406 	 * If the maximum directory cookie cache size has been reached
   1407 	 * for this node, take one off the front. The idea is that
   1408 	 * directories are typically read front-to-back once, so that
   1409 	 * the oldest entries can be thrown away without much performance
   1410 	 * loss.
   1411 	 */
   1412 	if (np->n_dircachesize == NFS_MAXDIRCACHE) {
   1413 		nfs_unlinkdircache(np, TAILQ_FIRST(&np->n_dirchain));
   1414 	} else
   1415 		np->n_dircachesize++;
   1416 
   1417 	KASSERT(ndp->dc_refcnt == 1);
   1418 	LIST_INSERT_HEAD(ndhp, ndp, dc_hash);
   1419 	TAILQ_INSERT_TAIL(&np->n_dirchain, ndp, dc_chain);
   1420 	ndp->dc_refcnt++;
   1421 done:
   1422 	KASSERT(ndp->dc_refcnt > 0);
   1423 	NFSDC_UNLOCK(np);
   1424 	if (newndp)
   1425 		nfs_putdircache(np, newndp);
   1426 	return ndp;
   1427 }
   1428 
   1429 void
   1430 nfs_invaldircache(struct vnode *vp, int flags)
   1431 {
   1432 	struct nfsnode *np = VTONFS(vp);
   1433 	struct nfsdircache *ndp = NULL;
   1434 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1435 	const bool forcefree = flags & NFS_INVALDIRCACHE_FORCE;
   1436 
   1437 #ifdef DIAGNOSTIC
   1438 	if (vp->v_type != VDIR)
   1439 		panic("nfs: invaldircache: not dir");
   1440 #endif
   1441 
   1442 	if ((flags & NFS_INVALDIRCACHE_KEEPEOF) == 0)
   1443 		np->n_flag &= ~NEOFVALID;
   1444 
   1445 	if (!np->n_dircache)
   1446 		return;
   1447 
   1448 	NFSDC_LOCK(np);
   1449 	if (!(nmp->nm_flag & NFSMNT_XLATECOOKIE) || forcefree) {
   1450 		while ((ndp = TAILQ_FIRST(&np->n_dirchain)) != NULL) {
   1451 			KASSERT(!forcefree || ndp->dc_refcnt == 1);
   1452 			nfs_unlinkdircache(np, ndp);
   1453 		}
   1454 		np->n_dircachesize = 0;
   1455 		if (forcefree && np->n_dirgens) {
   1456 			kmem_free(np->n_dirgens,
   1457 			    NFS_DIRHASHSIZ * sizeof(unsigned));
   1458 			np->n_dirgens = NULL;
   1459 		}
   1460 	} else {
   1461 		TAILQ_FOREACH(ndp, &np->n_dirchain, dc_chain)
   1462 			ndp->dc_flags |= NFSDC_INVALID;
   1463 	}
   1464 
   1465 	NFSDC_UNLOCK(np);
   1466 }
   1467 
   1468 /*
   1469  * Called once before VFS init to initialize shared and
   1470  * server-specific data structures.
   1471  */
   1472 static int
   1473 nfs_init0(void)
   1474 {
   1475 
   1476 	nfsrtt.pos = 0;
   1477 	rpc_vers = txdr_unsigned(RPC_VER2);
   1478 	rpc_call = txdr_unsigned(RPC_CALL);
   1479 	rpc_reply = txdr_unsigned(RPC_REPLY);
   1480 	rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
   1481 	rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
   1482 	rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
   1483 	rpc_autherr = txdr_unsigned(RPC_AUTHERR);
   1484 	rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
   1485 	rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
   1486 	nfs_prog = txdr_unsigned(NFS_PROG);
   1487 	nfs_true = txdr_unsigned(true);
   1488 	nfs_false = txdr_unsigned(false);
   1489 	nfs_xdrneg1 = txdr_unsigned(-1);
   1490 	nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
   1491 	if (nfs_ticks < 1)
   1492 		nfs_ticks = 1;
   1493 	nfs_xid = cprng_fast32();
   1494 	nfsdreq_init();
   1495 
   1496 	/*
   1497 	 * Initialize reply list and start timer
   1498 	 */
   1499 	TAILQ_INIT(&nfs_reqq);
   1500 	nfs_timer_init();
   1501 	MOWNER_ATTACH(&nfs_mowner);
   1502 
   1503 	return 0;
   1504 }
   1505 
   1506 static volatile uint32_t nfs_mutex;
   1507 static uint32_t nfs_refcount;
   1508 
   1509 #define nfs_p()	while (atomic_cas_32(&nfs_mutex, 0, 1) == 0) continue;
   1510 #define nfs_v()	while (atomic_cas_32(&nfs_mutex, 1, 0) == 1) continue;
   1511 
   1512 /*
   1513  * This is disgusting, but it must support both modular and monolothic
   1514  * configurations, plus the code is shared between server and client.
   1515  * For monolithic builds NFSSERVER may not imply NFS. Unfortunately we
   1516  * can't use regular mutexes here that would require static initialization
   1517  * and we can get initialized from multiple places, so we improvise.
   1518  *
   1519  * Yuck.
   1520  */
   1521 void
   1522 nfs_init(void)
   1523 {
   1524 
   1525 	nfs_p();
   1526 	if (nfs_refcount++ == 0)
   1527 		nfs_init0();
   1528 	nfs_v();
   1529 }
   1530 
   1531 void
   1532 nfs_fini(void)
   1533 {
   1534 
   1535 	nfs_p();
   1536 	if (--nfs_refcount == 0) {
   1537 		MOWNER_DETACH(&nfs_mowner);
   1538 		nfs_timer_fini();
   1539 		nfsdreq_fini();
   1540 	}
   1541 	nfs_v();
   1542 }
   1543 
   1544 /*
   1545  * A fiddled version of m_adj() that ensures null fill to a 32-bit
   1546  * boundary and only trims off the back end
   1547  *
   1548  * 1. trim off 'len' bytes as m_adj(mp, -len).
   1549  * 2. add zero-padding 'nul' bytes at the end of the mbuf chain.
   1550  */
   1551 void
   1552 nfs_zeropad(struct mbuf *mp, int len, int nul)
   1553 {
   1554 	struct mbuf *m;
   1555 	int count;
   1556 
   1557 	/*
   1558 	 * Trim from tail.  Scan the mbuf chain,
   1559 	 * calculating its length and finding the last mbuf.
   1560 	 * If the adjustment only affects this mbuf, then just
   1561 	 * adjust and return.  Otherwise, rescan and truncate
   1562 	 * after the remaining size.
   1563 	 */
   1564 	count = 0;
   1565 	m = mp;
   1566 	for (;;) {
   1567 		count += m->m_len;
   1568 		if (m->m_next == NULL)
   1569 			break;
   1570 		m = m->m_next;
   1571 	}
   1572 
   1573 	KDASSERT(count >= len);
   1574 
   1575 	if (m->m_len >= len) {
   1576 		m->m_len -= len;
   1577 	} else {
   1578 		count -= len;
   1579 		/*
   1580 		 * Correct length for chain is "count".
   1581 		 * Find the mbuf with last data, adjust its length,
   1582 		 * and toss data from remaining mbufs on chain.
   1583 		 */
   1584 		for (m = mp; m; m = m->m_next) {
   1585 			if (m->m_len >= count) {
   1586 				m->m_len = count;
   1587 				break;
   1588 			}
   1589 			count -= m->m_len;
   1590 		}
   1591 		KASSERT(m && m->m_next);
   1592 		m_freem(m->m_next);
   1593 		m->m_next = NULL;
   1594 	}
   1595 
   1596 	KDASSERT(m->m_next == NULL);
   1597 
   1598 	/*
   1599 	 * zero-padding.
   1600 	 */
   1601 	if (nul > 0) {
   1602 		char *cp;
   1603 		int i;
   1604 
   1605 		if (M_ROMAP(m) || M_TRAILINGSPACE(m) < nul) {
   1606 			struct mbuf *n;
   1607 
   1608 			KDASSERT(MLEN >= nul);
   1609 			n = m_get(M_WAIT, MT_DATA);
   1610 			MCLAIM(n, &nfs_mowner);
   1611 			n->m_len = nul;
   1612 			n->m_next = NULL;
   1613 			m->m_next = n;
   1614 			cp = mtod(n, void *);
   1615 		} else {
   1616 			cp = mtod(m, char *) + m->m_len;
   1617 			m->m_len += nul;
   1618 		}
   1619 		for (i = 0; i < nul; i++)
   1620 			*cp++ = '\0';
   1621 	}
   1622 	return;
   1623 }
   1624 
   1625 /*
   1626  * Make these functions instead of macros, so that the kernel text size
   1627  * doesn't get too big...
   1628  */
   1629 void
   1630 nfsm_srvwcc(struct nfsrv_descript *nfsd, int before_ret, struct vattr *before_vap, int after_ret, struct vattr *after_vap, struct mbuf **mbp, char **bposp)
   1631 {
   1632 	struct mbuf *mb = *mbp;
   1633 	char *bpos = *bposp;
   1634 	u_int32_t *tl;
   1635 
   1636 	if (before_ret) {
   1637 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1638 		*tl = nfs_false;
   1639 	} else {
   1640 		nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
   1641 		*tl++ = nfs_true;
   1642 		txdr_hyper(before_vap->va_size, tl);
   1643 		tl += 2;
   1644 		txdr_nfsv3time(&(before_vap->va_mtime), tl);
   1645 		tl += 2;
   1646 		txdr_nfsv3time(&(before_vap->va_ctime), tl);
   1647 	}
   1648 	*bposp = bpos;
   1649 	*mbp = mb;
   1650 	nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
   1651 }
   1652 
   1653 void
   1654 nfsm_srvpostopattr(struct nfsrv_descript *nfsd, int after_ret, struct vattr *after_vap, struct mbuf **mbp, char **bposp)
   1655 {
   1656 	struct mbuf *mb = *mbp;
   1657 	char *bpos = *bposp;
   1658 	u_int32_t *tl;
   1659 	struct nfs_fattr *fp;
   1660 
   1661 	if (after_ret) {
   1662 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1663 		*tl = nfs_false;
   1664 	} else {
   1665 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
   1666 		*tl++ = nfs_true;
   1667 		fp = (struct nfs_fattr *)tl;
   1668 		nfsm_srvfattr(nfsd, after_vap, fp);
   1669 	}
   1670 	*mbp = mb;
   1671 	*bposp = bpos;
   1672 }
   1673 
   1674 void
   1675 nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap, struct nfs_fattr *fp)
   1676 {
   1677 
   1678 	fp->fa_nlink = txdr_unsigned(vap->va_nlink);
   1679 	fp->fa_uid = txdr_unsigned(vap->va_uid);
   1680 	fp->fa_gid = txdr_unsigned(vap->va_gid);
   1681 	if (nfsd->nd_flag & ND_NFSV3) {
   1682 		fp->fa_type = vtonfsv3_type(vap->va_type);
   1683 		fp->fa_mode = vtonfsv3_mode(vap->va_mode);
   1684 		txdr_hyper(vap->va_size, &fp->fa3_size);
   1685 		txdr_hyper(vap->va_bytes, &fp->fa3_used);
   1686 		fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
   1687 		fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
   1688 		fp->fa3_fsid.nfsuquad[0] = 0;
   1689 		fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
   1690 		txdr_hyper(vap->va_fileid, &fp->fa3_fileid);
   1691 		txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
   1692 		txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
   1693 		txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
   1694 	} else {
   1695 		fp->fa_type = vtonfsv2_type(vap->va_type);
   1696 		fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
   1697 		fp->fa2_size = txdr_unsigned(vap->va_size);
   1698 		fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
   1699 		if (vap->va_type == VFIFO)
   1700 			fp->fa2_rdev = 0xffffffff;
   1701 		else
   1702 			fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
   1703 		fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
   1704 		fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
   1705 		fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
   1706 		txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
   1707 		txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
   1708 		txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
   1709 	}
   1710 }
   1711 
   1712 /*
   1713  * This function compares two net addresses by family and returns true
   1714  * if they are the same host.
   1715  * If there is any doubt, return false.
   1716  * The AF_INET family is handled as a special case so that address mbufs
   1717  * don't need to be saved to store "struct in_addr", which is only 4 bytes.
   1718  */
   1719 int
   1720 netaddr_match(int family, union nethostaddr *haddr, struct mbuf *nam)
   1721 {
   1722 	struct sockaddr_in *inetaddr;
   1723 
   1724 	switch (family) {
   1725 	case AF_INET:
   1726 		inetaddr = mtod(nam, struct sockaddr_in *);
   1727 		if (inetaddr->sin_family == AF_INET &&
   1728 		    inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
   1729 			return (1);
   1730 		break;
   1731 	case AF_INET6:
   1732 	    {
   1733 		struct sockaddr_in6 *sin6_1, *sin6_2;
   1734 
   1735 		sin6_1 = mtod(nam, struct sockaddr_in6 *);
   1736 		sin6_2 = mtod(haddr->had_nam, struct sockaddr_in6 *);
   1737 		if (sin6_1->sin6_family == AF_INET6 &&
   1738 		    IN6_ARE_ADDR_EQUAL(&sin6_1->sin6_addr, &sin6_2->sin6_addr))
   1739 			return 1;
   1740 	    }
   1741 	default:
   1742 		break;
   1743 	};
   1744 	return (0);
   1745 }
   1746 
   1747 /*
   1748  * The write verifier has changed (probably due to a server reboot), so all
   1749  * PG_NEEDCOMMIT pages will have to be written again. Since they are marked
   1750  * as dirty or are being written out just now, all this takes is clearing
   1751  * the PG_NEEDCOMMIT flag. Once done the new write verifier can be set for
   1752  * the mount point.
   1753  */
   1754 void
   1755 nfs_clearcommit(struct mount *mp)
   1756 {
   1757 	struct vnode *vp;
   1758 	struct vnode_iterator *marker;
   1759 	struct nfsnode *np;
   1760 	struct vm_page *pg;
   1761 	struct nfsmount *nmp = VFSTONFS(mp);
   1762 
   1763 	rw_enter(&nmp->nm_writeverflock, RW_WRITER);
   1764 	vfs_vnode_iterator_init(mp, &marker);
   1765 	while (vfs_vnode_iterator_next(marker, &vp)) {
   1766 		struct uvm_page_array a;
   1767 		voff_t off;
   1768 
   1769 		mutex_enter(vp->v_interlock);
   1770 		np = VTONFS(vp);
   1771 		if (vp->v_type != VREG || vp->v_mount != mp || np == NULL) {
   1772 			mutex_exit(vp->v_interlock);
   1773 			vrele(vp);
   1774 			continue;
   1775 		}
   1776 		np->n_pushlo = np->n_pushhi = np->n_pushedlo =
   1777 		    np->n_pushedhi = 0;
   1778 		np->n_commitflags &=
   1779 		    ~(NFS_COMMIT_PUSH_VALID | NFS_COMMIT_PUSHED_VALID);
   1780 		uvm_page_array_init(&a);
   1781 		off = 0;
   1782 		while ((pg = uvm_page_array_fill_and_peek(&a, &vp->v_uobj, off,
   1783 		    0, 0)) != NULL) {
   1784 			pg->flags &= ~PG_NEEDCOMMIT;
   1785 			uvm_page_array_advance(&a);
   1786 			off = pg->offset + PAGE_SIZE;
   1787 		}
   1788 		uvm_page_array_fini(&a);
   1789 		mutex_exit(vp->v_interlock);
   1790 		vrele(vp);
   1791 	}
   1792 	vfs_vnode_iterator_destroy(marker);
   1793 	mutex_enter(&nmp->nm_lock);
   1794 	nmp->nm_iflag &= ~NFSMNT_STALEWRITEVERF;
   1795 	mutex_exit(&nmp->nm_lock);
   1796 	rw_exit(&nmp->nm_writeverflock);
   1797 }
   1798 
   1799 void
   1800 nfs_merge_commit_ranges(struct vnode *vp)
   1801 {
   1802 	struct nfsnode *np = VTONFS(vp);
   1803 
   1804 	KASSERT(np->n_commitflags & NFS_COMMIT_PUSH_VALID);
   1805 	nfs_add_committed_range(vp, np->n_pushlo, np->n_pushhi - np->n_pushlo);
   1806 	np->n_commitflags &= ~NFS_COMMIT_PUSH_VALID;
   1807 
   1808 #ifdef NFS_DEBUG_COMMIT
   1809 	printf("merge: committed: %u - %u\n", (unsigned)np->n_pushedlo,
   1810 	    (unsigned)np->n_pushedhi);
   1811 #endif
   1812 }
   1813 
   1814 int
   1815 nfs_in_committed_range(struct vnode *vp, off_t off, off_t len)
   1816 {
   1817 	struct nfsnode *np = VTONFS(vp);
   1818 	off_t lo, hi;
   1819 
   1820 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
   1821 		return 0;
   1822 	lo = off;
   1823 	hi = lo + len;
   1824 
   1825 	return (lo >= np->n_pushedlo && hi <= np->n_pushedhi);
   1826 }
   1827 
   1828 int
   1829 nfs_in_tobecommitted_range(struct vnode *vp, off_t off, off_t len)
   1830 {
   1831 	struct nfsnode *np = VTONFS(vp);
   1832 	off_t lo, hi;
   1833 
   1834 	if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
   1835 		return 0;
   1836 	lo = off;
   1837 	hi = lo + len;
   1838 
   1839 	return (lo >= np->n_pushlo && hi <= np->n_pushhi);
   1840 }
   1841 
   1842 void
   1843 nfs_add_committed_range(struct vnode *vp, off_t off, off_t len)
   1844 {
   1845 	struct nfsnode *np = VTONFS(vp);
   1846 	off_t lo, hi;
   1847 
   1848 	lo = off;
   1849 	hi = lo + len;
   1850 
   1851 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
   1852 		np->n_pushedlo = lo;
   1853 		np->n_pushedhi = hi;
   1854 		np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
   1855 	} else {
   1856 		/*
   1857 		 * note that it's unsafe to add more than requested.
   1858 		 */
   1859 		if (hi > np->n_pushedhi && lo <= np->n_pushedhi)
   1860 			np->n_pushedhi = hi;
   1861 		if (lo < np->n_pushedlo && hi >= np->n_pushedlo)
   1862 			np->n_pushedlo = lo;
   1863 	}
   1864 	KASSERT(np->n_pushedhi >= np->n_pushedlo);
   1865 #ifdef NFS_DEBUG_COMMIT
   1866 	printf("add: committed: %u - %u\n", (unsigned)np->n_pushedlo,
   1867 	    (unsigned)np->n_pushedhi);
   1868 #endif
   1869 }
   1870 
   1871 void
   1872 nfs_del_committed_range(struct vnode *vp, off_t off, off_t len)
   1873 {
   1874 	struct nfsnode *np = VTONFS(vp);
   1875 	off_t lo, hi;
   1876 
   1877 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
   1878 		return;
   1879 
   1880 	lo = off;
   1881 	hi = lo + len;
   1882 
   1883 	if (lo > np->n_pushedhi || hi < np->n_pushedlo)
   1884 		return;
   1885 	if (lo <= np->n_pushedlo)
   1886 		np->n_pushedlo = hi;
   1887 	else if (hi >= np->n_pushedhi)
   1888 		np->n_pushedhi = lo;
   1889 	else {
   1890 		/*
   1891 		 * XXX There's only one range. If the deleted range
   1892 		 * is in the middle, pick the largest of the
   1893 		 * contiguous ranges that it leaves.
   1894 		 */
   1895 		if ((np->n_pushedlo - lo) > (hi - np->n_pushedhi))
   1896 			np->n_pushedhi = lo;
   1897 		else
   1898 			np->n_pushedlo = hi;
   1899 	}
   1900 	if (np->n_pushedhi <= np->n_pushedlo) {
   1901 		np->n_commitflags &= ~NFS_COMMIT_PUSHED_VALID;
   1902 	}
   1903 #ifdef NFS_DEBUG_COMMIT
   1904 	printf("del: committed: %u - %u\n", (unsigned)np->n_pushedlo,
   1905 	    (unsigned)np->n_pushedhi);
   1906 #endif
   1907 }
   1908 
   1909 void
   1910 nfs_add_tobecommitted_range(struct vnode *vp, off_t off, off_t len)
   1911 {
   1912 	struct nfsnode *np = VTONFS(vp);
   1913 	off_t lo, hi;
   1914 
   1915 	lo = off;
   1916 	hi = lo + len;
   1917 
   1918 	if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID)) {
   1919 		np->n_pushlo = lo;
   1920 		np->n_pushhi = hi;
   1921 		np->n_commitflags |= NFS_COMMIT_PUSH_VALID;
   1922 	} else {
   1923 		if (lo < np->n_pushlo)
   1924 			np->n_pushlo = lo;
   1925 		if (hi > np->n_pushhi)
   1926 			np->n_pushhi = hi;
   1927 	}
   1928 	KASSERT(np->n_pushhi >= np->n_pushlo);
   1929 #ifdef NFS_DEBUG_COMMIT
   1930 	printf("add: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
   1931 	    (unsigned)np->n_pushhi);
   1932 #endif
   1933 }
   1934 
   1935 void
   1936 nfs_del_tobecommitted_range(struct vnode *vp, off_t off, off_t len)
   1937 {
   1938 	struct nfsnode *np = VTONFS(vp);
   1939 	off_t lo, hi;
   1940 
   1941 	if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
   1942 		return;
   1943 
   1944 	lo = off;
   1945 	hi = lo + len;
   1946 
   1947 	if (lo > np->n_pushhi || hi < np->n_pushlo)
   1948 		return;
   1949 
   1950 	if (lo <= np->n_pushlo)
   1951 		np->n_pushlo = hi;
   1952 	else if (hi >= np->n_pushhi)
   1953 		np->n_pushhi = lo;
   1954 	else {
   1955 		/*
   1956 		 * following can cause a lot of small (eg. 64KB) commit rpcs.
   1957 		 */
   1958 #if 0
   1959 		/*
   1960 		 * XXX There's only one range. If the deleted range
   1961 		 * is in the middle, pick the largest of the
   1962 		 * contiguous ranges that it leaves.
   1963 		 */
   1964 		if ((np->n_pushlo - lo) > (hi - np->n_pushhi))
   1965 			np->n_pushhi = lo;
   1966 		else
   1967 			np->n_pushlo = hi;
   1968 #endif
   1969 	}
   1970 	if (np->n_pushhi <= np->n_pushlo) {
   1971 		np->n_commitflags &= ~NFS_COMMIT_PUSH_VALID;
   1972 	}
   1973 #ifdef NFS_DEBUG_COMMIT
   1974 	printf("del: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
   1975 	    (unsigned)np->n_pushhi);
   1976 #endif
   1977 }
   1978 
   1979 /*
   1980  * Map errnos to NFS error numbers. For Version 3 also filter out error
   1981  * numbers not specified for the associated procedure.
   1982  */
   1983 int
   1984 nfsrv_errmap(struct nfsrv_descript *nd, int err)
   1985 {
   1986 	const short *defaulterrp, *errp;
   1987 
   1988 	if (nd->nd_flag & ND_NFSV3) {
   1989 	    if (nd->nd_procnum <= NFSPROC_COMMIT) {
   1990 		errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
   1991 		while (*++errp) {
   1992 			if (*errp == err)
   1993 				return (err);
   1994 			else if (*errp > err)
   1995 				break;
   1996 		}
   1997 		return ((int)*defaulterrp);
   1998 	    } else
   1999 		return (err & 0xffff);
   2000 	}
   2001 	if (err <= ELAST)
   2002 		return ((int)nfsrv_v2errmap[err - 1]);
   2003 	return (NFSERR_IO);
   2004 }
   2005 
   2006 u_int32_t
   2007 nfs_getxid(void)
   2008 {
   2009 	u_int32_t newxid;
   2010 
   2011 	/* get next xid.  skip 0 */
   2012 	do {
   2013 		newxid = atomic_inc_32_nv(&nfs_xid);
   2014 	} while (__predict_false(newxid == 0));
   2015 
   2016 	return txdr_unsigned(newxid);
   2017 }
   2018 
   2019 /*
   2020  * assign a new xid for existing request.
   2021  * used for NFSERR_JUKEBOX handling.
   2022  */
   2023 void
   2024 nfs_renewxid(struct nfsreq *req)
   2025 {
   2026 	u_int32_t xid;
   2027 	int off;
   2028 
   2029 	xid = nfs_getxid();
   2030 	if (req->r_nmp->nm_sotype == SOCK_STREAM)
   2031 		off = sizeof(u_int32_t); /* RPC record mark */
   2032 	else
   2033 		off = 0;
   2034 
   2035 	m_copyback(req->r_mreq, off, sizeof(xid), (void *)&xid);
   2036 	req->r_xid = xid;
   2037 }
   2038