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