Home | History | Annotate | Line # | Download | only in nfs
nfs_subs.c revision 1.84
      1 /*	$NetBSD: nfs_subs.c,v 1.84 2000/09/19 23:26:26 bjh21 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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)nfs_subs.c	8.8 (Berkeley) 5/22/95
     39  */
     40 
     41 /*
     42  * Copyright 2000 Wasabi Systems, Inc.
     43  * All rights reserved.
     44  *
     45  * Written by Frank van der Linden for Wasabi Systems, Inc.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. All advertising materials mentioning features or use of this software
     56  *    must display the following acknowledgement:
     57  *      This product includes software developed for the NetBSD Project by
     58  *      Wasabi Systems, Inc.
     59  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     60  *    or promote products derived from this software without specific prior
     61  *    written permission.
     62  *
     63  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     65  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     66  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     67  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     68  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     69  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     70  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     71  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     72  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     73  * POSSIBILITY OF SUCH DAMAGE.
     74  */
     75 
     76 #include "fs_nfs.h"
     77 #include "opt_nfs.h"
     78 #include "opt_nfsserver.h"
     79 #include "opt_iso.h"
     80 #include "opt_inet.h"
     81 
     82 /*
     83  * These functions support the macros and help fiddle mbuf chains for
     84  * the nfs op functions. They do things like create the rpc header and
     85  * copy data between mbuf chains and uio lists.
     86  */
     87 #include <sys/param.h>
     88 #include <sys/proc.h>
     89 #include <sys/systm.h>
     90 #include <sys/kernel.h>
     91 #include <sys/mount.h>
     92 #include <sys/vnode.h>
     93 #include <sys/namei.h>
     94 #include <sys/mbuf.h>
     95 #include <sys/socket.h>
     96 #include <sys/stat.h>
     97 #include <sys/malloc.h>
     98 #include <sys/time.h>
     99 #include <sys/dirent.h>
    100 
    101 #include <uvm/uvm_extern.h>
    102 
    103 #include <nfs/rpcv2.h>
    104 #include <nfs/nfsproto.h>
    105 #include <nfs/nfsnode.h>
    106 #include <nfs/nfs.h>
    107 #include <nfs/xdr_subs.h>
    108 #include <nfs/nfsm_subs.h>
    109 #include <nfs/nfsmount.h>
    110 #include <nfs/nqnfs.h>
    111 #include <nfs/nfsrtt.h>
    112 #include <nfs/nfs_var.h>
    113 
    114 #include <miscfs/specfs/specdev.h>
    115 
    116 #include <netinet/in.h>
    117 #ifdef ISO
    118 #include <netiso/iso.h>
    119 #endif
    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, nqnfs_prog, nfs_true, nfs_false;
    130 
    131 /* And other global data */
    132 static u_int32_t nfs_xid = 0;
    133 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
    134 		      NFCHR, NFNON };
    135 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
    136 		      NFFIFO, NFNON };
    137 enum vtype nv2tov_type[8] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON };
    138 enum vtype nv3tov_type[8]={ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO };
    139 int nfs_ticks;
    140 
    141 /* NFS client/server stats. */
    142 struct nfsstats nfsstats;
    143 
    144 /*
    145  * Mapping of old NFS Version 2 RPC numbers to generic numbers.
    146  */
    147 int nfsv3_procid[NFS_NPROCS] = {
    148 	NFSPROC_NULL,
    149 	NFSPROC_GETATTR,
    150 	NFSPROC_SETATTR,
    151 	NFSPROC_NOOP,
    152 	NFSPROC_LOOKUP,
    153 	NFSPROC_READLINK,
    154 	NFSPROC_READ,
    155 	NFSPROC_NOOP,
    156 	NFSPROC_WRITE,
    157 	NFSPROC_CREATE,
    158 	NFSPROC_REMOVE,
    159 	NFSPROC_RENAME,
    160 	NFSPROC_LINK,
    161 	NFSPROC_SYMLINK,
    162 	NFSPROC_MKDIR,
    163 	NFSPROC_RMDIR,
    164 	NFSPROC_READDIR,
    165 	NFSPROC_FSSTAT,
    166 	NFSPROC_NOOP,
    167 	NFSPROC_NOOP,
    168 	NFSPROC_NOOP,
    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 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 	NFSV2PROC_NOOP,
    204 	NFSV2PROC_NOOP,
    205 	NFSV2PROC_NOOP,
    206 };
    207 
    208 /*
    209  * Maps errno values to nfs error numbers.
    210  * Use NFSERR_IO as the catch all for ones not specifically defined in
    211  * RFC 1094.
    212  */
    213 static u_char nfsrv_v2errmap[ELAST] = {
    214   NFSERR_PERM,	NFSERR_NOENT,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    215   NFSERR_NXIO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    216   NFSERR_IO,	NFSERR_IO,	NFSERR_ACCES,	NFSERR_IO,	NFSERR_IO,
    217   NFSERR_IO,	NFSERR_EXIST,	NFSERR_IO,	NFSERR_NODEV,	NFSERR_NOTDIR,
    218   NFSERR_ISDIR,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    219   NFSERR_IO,	NFSERR_FBIG,	NFSERR_NOSPC,	NFSERR_IO,	NFSERR_ROFS,
    220   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    221   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    222   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    223   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    224   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    225   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    226   NFSERR_IO,	NFSERR_IO,	NFSERR_NAMETOL,	NFSERR_IO,	NFSERR_IO,
    227   NFSERR_NOTEMPTY, NFSERR_IO,	NFSERR_IO,	NFSERR_DQUOT,	NFSERR_STALE,
    228   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    229   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    230   NFSERR_IO,	NFSERR_IO,
    231 };
    232 
    233 /*
    234  * Maps errno values to nfs error numbers.
    235  * Although it is not obvious whether or not NFS clients really care if
    236  * a returned error value is in the specified list for the procedure, the
    237  * safest thing to do is filter them appropriately. For Version 2, the
    238  * X/Open XNFS document is the only specification that defines error values
    239  * for each RPC (The RFC simply lists all possible error values for all RPCs),
    240  * so I have decided to not do this for Version 2.
    241  * The first entry is the default error return and the rest are the valid
    242  * errors for that RPC in increasing numeric order.
    243  */
    244 static short nfsv3err_null[] = {
    245 	0,
    246 	0,
    247 };
    248 
    249 static short nfsv3err_getattr[] = {
    250 	NFSERR_IO,
    251 	NFSERR_IO,
    252 	NFSERR_STALE,
    253 	NFSERR_BADHANDLE,
    254 	NFSERR_SERVERFAULT,
    255 	0,
    256 };
    257 
    258 static short nfsv3err_setattr[] = {
    259 	NFSERR_IO,
    260 	NFSERR_PERM,
    261 	NFSERR_IO,
    262 	NFSERR_ACCES,
    263 	NFSERR_INVAL,
    264 	NFSERR_NOSPC,
    265 	NFSERR_ROFS,
    266 	NFSERR_DQUOT,
    267 	NFSERR_STALE,
    268 	NFSERR_BADHANDLE,
    269 	NFSERR_NOT_SYNC,
    270 	NFSERR_SERVERFAULT,
    271 	0,
    272 };
    273 
    274 static short nfsv3err_lookup[] = {
    275 	NFSERR_IO,
    276 	NFSERR_NOENT,
    277 	NFSERR_IO,
    278 	NFSERR_ACCES,
    279 	NFSERR_NOTDIR,
    280 	NFSERR_NAMETOL,
    281 	NFSERR_STALE,
    282 	NFSERR_BADHANDLE,
    283 	NFSERR_SERVERFAULT,
    284 	0,
    285 };
    286 
    287 static short nfsv3err_access[] = {
    288 	NFSERR_IO,
    289 	NFSERR_IO,
    290 	NFSERR_STALE,
    291 	NFSERR_BADHANDLE,
    292 	NFSERR_SERVERFAULT,
    293 	0,
    294 };
    295 
    296 static short nfsv3err_readlink[] = {
    297 	NFSERR_IO,
    298 	NFSERR_IO,
    299 	NFSERR_ACCES,
    300 	NFSERR_INVAL,
    301 	NFSERR_STALE,
    302 	NFSERR_BADHANDLE,
    303 	NFSERR_NOTSUPP,
    304 	NFSERR_SERVERFAULT,
    305 	0,
    306 };
    307 
    308 static short nfsv3err_read[] = {
    309 	NFSERR_IO,
    310 	NFSERR_IO,
    311 	NFSERR_NXIO,
    312 	NFSERR_ACCES,
    313 	NFSERR_INVAL,
    314 	NFSERR_STALE,
    315 	NFSERR_BADHANDLE,
    316 	NFSERR_SERVERFAULT,
    317 	NFSERR_JUKEBOX,
    318 	0,
    319 };
    320 
    321 static short nfsv3err_write[] = {
    322 	NFSERR_IO,
    323 	NFSERR_IO,
    324 	NFSERR_ACCES,
    325 	NFSERR_INVAL,
    326 	NFSERR_FBIG,
    327 	NFSERR_NOSPC,
    328 	NFSERR_ROFS,
    329 	NFSERR_DQUOT,
    330 	NFSERR_STALE,
    331 	NFSERR_BADHANDLE,
    332 	NFSERR_SERVERFAULT,
    333 	NFSERR_JUKEBOX,
    334 	0,
    335 };
    336 
    337 static short nfsv3err_create[] = {
    338 	NFSERR_IO,
    339 	NFSERR_IO,
    340 	NFSERR_ACCES,
    341 	NFSERR_EXIST,
    342 	NFSERR_NOTDIR,
    343 	NFSERR_NOSPC,
    344 	NFSERR_ROFS,
    345 	NFSERR_NAMETOL,
    346 	NFSERR_DQUOT,
    347 	NFSERR_STALE,
    348 	NFSERR_BADHANDLE,
    349 	NFSERR_NOTSUPP,
    350 	NFSERR_SERVERFAULT,
    351 	0,
    352 };
    353 
    354 static short nfsv3err_mkdir[] = {
    355 	NFSERR_IO,
    356 	NFSERR_IO,
    357 	NFSERR_ACCES,
    358 	NFSERR_EXIST,
    359 	NFSERR_NOTDIR,
    360 	NFSERR_NOSPC,
    361 	NFSERR_ROFS,
    362 	NFSERR_NAMETOL,
    363 	NFSERR_DQUOT,
    364 	NFSERR_STALE,
    365 	NFSERR_BADHANDLE,
    366 	NFSERR_NOTSUPP,
    367 	NFSERR_SERVERFAULT,
    368 	0,
    369 };
    370 
    371 static short nfsv3err_symlink[] = {
    372 	NFSERR_IO,
    373 	NFSERR_IO,
    374 	NFSERR_ACCES,
    375 	NFSERR_EXIST,
    376 	NFSERR_NOTDIR,
    377 	NFSERR_NOSPC,
    378 	NFSERR_ROFS,
    379 	NFSERR_NAMETOL,
    380 	NFSERR_DQUOT,
    381 	NFSERR_STALE,
    382 	NFSERR_BADHANDLE,
    383 	NFSERR_NOTSUPP,
    384 	NFSERR_SERVERFAULT,
    385 	0,
    386 };
    387 
    388 static short nfsv3err_mknod[] = {
    389 	NFSERR_IO,
    390 	NFSERR_IO,
    391 	NFSERR_ACCES,
    392 	NFSERR_EXIST,
    393 	NFSERR_NOTDIR,
    394 	NFSERR_NOSPC,
    395 	NFSERR_ROFS,
    396 	NFSERR_NAMETOL,
    397 	NFSERR_DQUOT,
    398 	NFSERR_STALE,
    399 	NFSERR_BADHANDLE,
    400 	NFSERR_NOTSUPP,
    401 	NFSERR_SERVERFAULT,
    402 	NFSERR_BADTYPE,
    403 	0,
    404 };
    405 
    406 static short nfsv3err_remove[] = {
    407 	NFSERR_IO,
    408 	NFSERR_NOENT,
    409 	NFSERR_IO,
    410 	NFSERR_ACCES,
    411 	NFSERR_NOTDIR,
    412 	NFSERR_ROFS,
    413 	NFSERR_NAMETOL,
    414 	NFSERR_STALE,
    415 	NFSERR_BADHANDLE,
    416 	NFSERR_SERVERFAULT,
    417 	0,
    418 };
    419 
    420 static short nfsv3err_rmdir[] = {
    421 	NFSERR_IO,
    422 	NFSERR_NOENT,
    423 	NFSERR_IO,
    424 	NFSERR_ACCES,
    425 	NFSERR_EXIST,
    426 	NFSERR_NOTDIR,
    427 	NFSERR_INVAL,
    428 	NFSERR_ROFS,
    429 	NFSERR_NAMETOL,
    430 	NFSERR_NOTEMPTY,
    431 	NFSERR_STALE,
    432 	NFSERR_BADHANDLE,
    433 	NFSERR_NOTSUPP,
    434 	NFSERR_SERVERFAULT,
    435 	0,
    436 };
    437 
    438 static short nfsv3err_rename[] = {
    439 	NFSERR_IO,
    440 	NFSERR_NOENT,
    441 	NFSERR_IO,
    442 	NFSERR_ACCES,
    443 	NFSERR_EXIST,
    444 	NFSERR_XDEV,
    445 	NFSERR_NOTDIR,
    446 	NFSERR_ISDIR,
    447 	NFSERR_INVAL,
    448 	NFSERR_NOSPC,
    449 	NFSERR_ROFS,
    450 	NFSERR_MLINK,
    451 	NFSERR_NAMETOL,
    452 	NFSERR_NOTEMPTY,
    453 	NFSERR_DQUOT,
    454 	NFSERR_STALE,
    455 	NFSERR_BADHANDLE,
    456 	NFSERR_NOTSUPP,
    457 	NFSERR_SERVERFAULT,
    458 	0,
    459 };
    460 
    461 static short nfsv3err_link[] = {
    462 	NFSERR_IO,
    463 	NFSERR_IO,
    464 	NFSERR_ACCES,
    465 	NFSERR_EXIST,
    466 	NFSERR_XDEV,
    467 	NFSERR_NOTDIR,
    468 	NFSERR_INVAL,
    469 	NFSERR_NOSPC,
    470 	NFSERR_ROFS,
    471 	NFSERR_MLINK,
    472 	NFSERR_NAMETOL,
    473 	NFSERR_DQUOT,
    474 	NFSERR_STALE,
    475 	NFSERR_BADHANDLE,
    476 	NFSERR_NOTSUPP,
    477 	NFSERR_SERVERFAULT,
    478 	0,
    479 };
    480 
    481 static short nfsv3err_readdir[] = {
    482 	NFSERR_IO,
    483 	NFSERR_IO,
    484 	NFSERR_ACCES,
    485 	NFSERR_NOTDIR,
    486 	NFSERR_STALE,
    487 	NFSERR_BADHANDLE,
    488 	NFSERR_BAD_COOKIE,
    489 	NFSERR_TOOSMALL,
    490 	NFSERR_SERVERFAULT,
    491 	0,
    492 };
    493 
    494 static short nfsv3err_readdirplus[] = {
    495 	NFSERR_IO,
    496 	NFSERR_IO,
    497 	NFSERR_ACCES,
    498 	NFSERR_NOTDIR,
    499 	NFSERR_STALE,
    500 	NFSERR_BADHANDLE,
    501 	NFSERR_BAD_COOKIE,
    502 	NFSERR_NOTSUPP,
    503 	NFSERR_TOOSMALL,
    504 	NFSERR_SERVERFAULT,
    505 	0,
    506 };
    507 
    508 static short nfsv3err_fsstat[] = {
    509 	NFSERR_IO,
    510 	NFSERR_IO,
    511 	NFSERR_STALE,
    512 	NFSERR_BADHANDLE,
    513 	NFSERR_SERVERFAULT,
    514 	0,
    515 };
    516 
    517 static short nfsv3err_fsinfo[] = {
    518 	NFSERR_STALE,
    519 	NFSERR_STALE,
    520 	NFSERR_BADHANDLE,
    521 	NFSERR_SERVERFAULT,
    522 	0,
    523 };
    524 
    525 static short nfsv3err_pathconf[] = {
    526 	NFSERR_STALE,
    527 	NFSERR_STALE,
    528 	NFSERR_BADHANDLE,
    529 	NFSERR_SERVERFAULT,
    530 	0,
    531 };
    532 
    533 static short nfsv3err_commit[] = {
    534 	NFSERR_IO,
    535 	NFSERR_IO,
    536 	NFSERR_STALE,
    537 	NFSERR_BADHANDLE,
    538 	NFSERR_SERVERFAULT,
    539 	0,
    540 };
    541 
    542 static short *nfsrv_v3errmap[] = {
    543 	nfsv3err_null,
    544 	nfsv3err_getattr,
    545 	nfsv3err_setattr,
    546 	nfsv3err_lookup,
    547 	nfsv3err_access,
    548 	nfsv3err_readlink,
    549 	nfsv3err_read,
    550 	nfsv3err_write,
    551 	nfsv3err_create,
    552 	nfsv3err_mkdir,
    553 	nfsv3err_symlink,
    554 	nfsv3err_mknod,
    555 	nfsv3err_remove,
    556 	nfsv3err_rmdir,
    557 	nfsv3err_rename,
    558 	nfsv3err_link,
    559 	nfsv3err_readdir,
    560 	nfsv3err_readdirplus,
    561 	nfsv3err_fsstat,
    562 	nfsv3err_fsinfo,
    563 	nfsv3err_pathconf,
    564 	nfsv3err_commit,
    565 };
    566 
    567 extern struct nfsrtt nfsrtt;
    568 extern time_t nqnfsstarttime;
    569 extern int nqsrv_clockskew;
    570 extern int nqsrv_writeslack;
    571 extern int nqsrv_maxlease;
    572 extern int nqnfs_piggy[NFS_NPROCS];
    573 extern struct nfsnodehashhead *nfsnodehashtbl;
    574 extern u_long nfsnodehash;
    575 
    576 LIST_HEAD(nfsnodehashhead, nfsnode);
    577 u_long nfsdirhashmask;
    578 
    579 int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *));
    580 
    581 /*
    582  * Create the header for an rpc request packet
    583  * The hsiz is the size of the rest of the nfs request header.
    584  * (just used to decide if a cluster is a good idea)
    585  */
    586 struct mbuf *
    587 nfsm_reqh(vp, procid, hsiz, bposp)
    588 	struct vnode *vp;
    589 	u_long procid;
    590 	int hsiz;
    591 	caddr_t *bposp;
    592 {
    593 	struct mbuf *mb;
    594 	u_int32_t *tl;
    595 	caddr_t bpos;
    596 	struct mbuf *mb2;
    597 	struct nfsmount *nmp;
    598 	int nqflag;
    599 
    600 	MGET(mb, M_WAIT, MT_DATA);
    601 	if (hsiz >= MINCLSIZE)
    602 		MCLGET(mb, M_WAIT);
    603 	mb->m_len = 0;
    604 	bpos = mtod(mb, caddr_t);
    605 
    606 	/*
    607 	 * For NQNFS, add lease request.
    608 	 */
    609 	if (vp) {
    610 		nmp = VFSTONFS(vp->v_mount);
    611 #ifndef NFS_V2_ONLY
    612 		if (nmp->nm_flag & NFSMNT_NQNFS) {
    613 			nqflag = NQNFS_NEEDLEASE(vp, procid);
    614 			if (nqflag) {
    615 				nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
    616 				*tl++ = txdr_unsigned(nqflag);
    617 				*tl = txdr_unsigned(nmp->nm_leaseterm);
    618 			} else {
    619 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    620 				*tl = 0;
    621 			}
    622 		}
    623 #endif
    624 	}
    625 	/* Finally, return values */
    626 	*bposp = bpos;
    627 	return (mb);
    628 }
    629 
    630 /*
    631  * Build the RPC header and fill in the authorization info.
    632  * The authorization string argument is only used when the credentials
    633  * come from outside of the kernel.
    634  * Returns the head of the mbuf list.
    635  */
    636 struct mbuf *
    637 nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
    638 	verf_str, mrest, mrest_len, mbp, xidp)
    639 	struct ucred *cr;
    640 	int nmflag;
    641 	int procid;
    642 	int auth_type;
    643 	int auth_len;
    644 	char *auth_str;
    645 	int verf_len;
    646 	char *verf_str;
    647 	struct mbuf *mrest;
    648 	int mrest_len;
    649 	struct mbuf **mbp;
    650 	u_int32_t *xidp;
    651 {
    652 	struct mbuf *mb;
    653 	u_int32_t *tl;
    654 	caddr_t bpos;
    655 	int i;
    656 	struct mbuf *mreq, *mb2;
    657 	int siz, grpsiz, authsiz;
    658 	struct timeval tv;
    659 	static u_int32_t base;
    660 
    661 	authsiz = nfsm_rndup(auth_len);
    662 	MGETHDR(mb, M_WAIT, MT_DATA);
    663 	if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
    664 		MCLGET(mb, M_WAIT);
    665 	} else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
    666 		MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
    667 	} else {
    668 		MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
    669 	}
    670 	mb->m_len = 0;
    671 	mreq = mb;
    672 	bpos = mtod(mb, caddr_t);
    673 
    674 	/*
    675 	 * First the RPC header.
    676 	 */
    677 	nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
    678 
    679 	/*
    680 	 * derive initial xid from system time
    681 	 * XXX time is invalid if root not yet mounted
    682 	 */
    683 	if (!base && (rootvp)) {
    684 		microtime(&tv);
    685 		base = tv.tv_sec << 12;
    686 		nfs_xid = base;
    687 	}
    688 	/*
    689 	 * Skip zero xid if it should ever happen.
    690 	 */
    691 	if (++nfs_xid == 0)
    692 		nfs_xid++;
    693 
    694 	*tl++ = *xidp = txdr_unsigned(nfs_xid);
    695 	*tl++ = rpc_call;
    696 	*tl++ = rpc_vers;
    697 	if (nmflag & NFSMNT_NQNFS) {
    698 		*tl++ = txdr_unsigned(NQNFS_PROG);
    699 		*tl++ = txdr_unsigned(NQNFS_VER3);
    700 	} else {
    701 		*tl++ = txdr_unsigned(NFS_PROG);
    702 		if (nmflag & NFSMNT_NFSV3)
    703 			*tl++ = txdr_unsigned(NFS_VER3);
    704 		else
    705 			*tl++ = txdr_unsigned(NFS_VER2);
    706 	}
    707 	if (nmflag & NFSMNT_NFSV3)
    708 		*tl++ = txdr_unsigned(procid);
    709 	else
    710 		*tl++ = txdr_unsigned(nfsv2_procid[procid]);
    711 
    712 	/*
    713 	 * And then the authorization cred.
    714 	 */
    715 	*tl++ = txdr_unsigned(auth_type);
    716 	*tl = txdr_unsigned(authsiz);
    717 	switch (auth_type) {
    718 	case RPCAUTH_UNIX:
    719 		nfsm_build(tl, u_int32_t *, auth_len);
    720 		*tl++ = 0;		/* stamp ?? */
    721 		*tl++ = 0;		/* NULL hostname */
    722 		*tl++ = txdr_unsigned(cr->cr_uid);
    723 		*tl++ = txdr_unsigned(cr->cr_gid);
    724 		grpsiz = (auth_len >> 2) - 5;
    725 		*tl++ = txdr_unsigned(grpsiz);
    726 		for (i = 0; i < grpsiz; i++)
    727 			*tl++ = txdr_unsigned(cr->cr_groups[i]);
    728 		break;
    729 	case RPCAUTH_KERB4:
    730 		siz = auth_len;
    731 		while (siz > 0) {
    732 			if (M_TRAILINGSPACE(mb) == 0) {
    733 				MGET(mb2, M_WAIT, MT_DATA);
    734 				if (siz >= MINCLSIZE)
    735 					MCLGET(mb2, M_WAIT);
    736 				mb->m_next = mb2;
    737 				mb = mb2;
    738 				mb->m_len = 0;
    739 				bpos = mtod(mb, caddr_t);
    740 			}
    741 			i = min(siz, M_TRAILINGSPACE(mb));
    742 			memcpy(bpos, auth_str, i);
    743 			mb->m_len += i;
    744 			auth_str += i;
    745 			bpos += i;
    746 			siz -= i;
    747 		}
    748 		if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
    749 			for (i = 0; i < siz; i++)
    750 				*bpos++ = '\0';
    751 			mb->m_len += siz;
    752 		}
    753 		break;
    754 	};
    755 
    756 	/*
    757 	 * And the verifier...
    758 	 */
    759 	nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
    760 	if (verf_str) {
    761 		*tl++ = txdr_unsigned(RPCAUTH_KERB4);
    762 		*tl = txdr_unsigned(verf_len);
    763 		siz = verf_len;
    764 		while (siz > 0) {
    765 			if (M_TRAILINGSPACE(mb) == 0) {
    766 				MGET(mb2, M_WAIT, MT_DATA);
    767 				if (siz >= MINCLSIZE)
    768 					MCLGET(mb2, M_WAIT);
    769 				mb->m_next = mb2;
    770 				mb = mb2;
    771 				mb->m_len = 0;
    772 				bpos = mtod(mb, caddr_t);
    773 			}
    774 			i = min(siz, M_TRAILINGSPACE(mb));
    775 			memcpy(bpos, verf_str, i);
    776 			mb->m_len += i;
    777 			verf_str += i;
    778 			bpos += i;
    779 			siz -= i;
    780 		}
    781 		if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
    782 			for (i = 0; i < siz; i++)
    783 				*bpos++ = '\0';
    784 			mb->m_len += siz;
    785 		}
    786 	} else {
    787 		*tl++ = txdr_unsigned(RPCAUTH_NULL);
    788 		*tl = 0;
    789 	}
    790 	mb->m_next = mrest;
    791 	mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
    792 	mreq->m_pkthdr.rcvif = (struct ifnet *)0;
    793 	*mbp = mb;
    794 	return (mreq);
    795 }
    796 
    797 /*
    798  * copies mbuf chain to the uio scatter/gather list
    799  */
    800 int
    801 nfsm_mbuftouio(mrep, uiop, siz, dpos)
    802 	struct mbuf **mrep;
    803 	struct uio *uiop;
    804 	int siz;
    805 	caddr_t *dpos;
    806 {
    807 	char *mbufcp, *uiocp;
    808 	int xfer, left, len;
    809 	struct mbuf *mp;
    810 	long uiosiz, rem;
    811 	int error = 0;
    812 
    813 	mp = *mrep;
    814 	mbufcp = *dpos;
    815 	len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
    816 	rem = nfsm_rndup(siz)-siz;
    817 	while (siz > 0) {
    818 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
    819 			return (EFBIG);
    820 		left = uiop->uio_iov->iov_len;
    821 		uiocp = uiop->uio_iov->iov_base;
    822 		if (left > siz)
    823 			left = siz;
    824 		uiosiz = left;
    825 		while (left > 0) {
    826 			while (len == 0) {
    827 				mp = mp->m_next;
    828 				if (mp == NULL)
    829 					return (EBADRPC);
    830 				mbufcp = mtod(mp, caddr_t);
    831 				len = mp->m_len;
    832 			}
    833 			xfer = (left > len) ? len : left;
    834 #ifdef notdef
    835 			/* Not Yet.. */
    836 			if (uiop->uio_iov->iov_op != NULL)
    837 				(*(uiop->uio_iov->iov_op))
    838 				(mbufcp, uiocp, xfer);
    839 			else
    840 #endif
    841 			if (uiop->uio_segflg == UIO_SYSSPACE)
    842 				memcpy(uiocp, mbufcp, xfer);
    843 			else
    844 				copyout(mbufcp, uiocp, xfer);
    845 			left -= xfer;
    846 			len -= xfer;
    847 			mbufcp += xfer;
    848 			uiocp += xfer;
    849 			uiop->uio_offset += xfer;
    850 			uiop->uio_resid -= xfer;
    851 		}
    852 		if (uiop->uio_iov->iov_len <= siz) {
    853 			uiop->uio_iovcnt--;
    854 			uiop->uio_iov++;
    855 		} else {
    856 			(caddr_t)uiop->uio_iov->iov_base += uiosiz;
    857 			uiop->uio_iov->iov_len -= uiosiz;
    858 		}
    859 		siz -= uiosiz;
    860 	}
    861 	*dpos = mbufcp;
    862 	*mrep = mp;
    863 	if (rem > 0) {
    864 		if (len < rem)
    865 			error = nfs_adv(mrep, dpos, rem, len);
    866 		else
    867 			*dpos += rem;
    868 	}
    869 	return (error);
    870 }
    871 
    872 /*
    873  * copies a uio scatter/gather list to an mbuf chain.
    874  * NOTE: can ony handle iovcnt == 1
    875  */
    876 int
    877 nfsm_uiotombuf(uiop, mq, siz, bpos)
    878 	struct uio *uiop;
    879 	struct mbuf **mq;
    880 	int siz;
    881 	caddr_t *bpos;
    882 {
    883 	char *uiocp;
    884 	struct mbuf *mp, *mp2;
    885 	int xfer, left, mlen;
    886 	int uiosiz, clflg, rem;
    887 	char *cp;
    888 
    889 #ifdef DIAGNOSTIC
    890 	if (uiop->uio_iovcnt != 1)
    891 		panic("nfsm_uiotombuf: iovcnt != 1");
    892 #endif
    893 
    894 	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
    895 		clflg = 1;
    896 	else
    897 		clflg = 0;
    898 	rem = nfsm_rndup(siz)-siz;
    899 	mp = mp2 = *mq;
    900 	while (siz > 0) {
    901 		left = uiop->uio_iov->iov_len;
    902 		uiocp = uiop->uio_iov->iov_base;
    903 		if (left > siz)
    904 			left = siz;
    905 		uiosiz = left;
    906 		while (left > 0) {
    907 			mlen = M_TRAILINGSPACE(mp);
    908 			if (mlen == 0) {
    909 				MGET(mp, M_WAIT, MT_DATA);
    910 				if (clflg)
    911 					MCLGET(mp, M_WAIT);
    912 				mp->m_len = 0;
    913 				mp2->m_next = mp;
    914 				mp2 = mp;
    915 				mlen = M_TRAILINGSPACE(mp);
    916 			}
    917 			xfer = (left > mlen) ? mlen : left;
    918 #ifdef notdef
    919 			/* Not Yet.. */
    920 			if (uiop->uio_iov->iov_op != NULL)
    921 				(*(uiop->uio_iov->iov_op))
    922 				(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
    923 			else
    924 #endif
    925 			if (uiop->uio_segflg == UIO_SYSSPACE)
    926 				memcpy(mtod(mp, caddr_t)+mp->m_len, uiocp, xfer);
    927 			else
    928 				copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
    929 			mp->m_len += xfer;
    930 			left -= xfer;
    931 			uiocp += xfer;
    932 			uiop->uio_offset += xfer;
    933 			uiop->uio_resid -= xfer;
    934 		}
    935 		(caddr_t)uiop->uio_iov->iov_base += uiosiz;
    936 		uiop->uio_iov->iov_len -= uiosiz;
    937 		siz -= uiosiz;
    938 	}
    939 	if (rem > 0) {
    940 		if (rem > M_TRAILINGSPACE(mp)) {
    941 			MGET(mp, M_WAIT, MT_DATA);
    942 			mp->m_len = 0;
    943 			mp2->m_next = mp;
    944 		}
    945 		cp = mtod(mp, caddr_t)+mp->m_len;
    946 		for (left = 0; left < rem; left++)
    947 			*cp++ = '\0';
    948 		mp->m_len += rem;
    949 		*bpos = cp;
    950 	} else
    951 		*bpos = mtod(mp, caddr_t)+mp->m_len;
    952 	*mq = mp;
    953 	return (0);
    954 }
    955 
    956 /*
    957  * Get at least "siz" bytes of correctly aligned data.
    958  * When called the mbuf pointers are not necessarily correct,
    959  * dsosp points to what ought to be in m_data and left contains
    960  * what ought to be in m_len.
    961  * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
    962  * cases. (The macros use the vars. dpos and dpos2)
    963  */
    964 int
    965 nfsm_disct(mdp, dposp, siz, left, cp2)
    966 	struct mbuf **mdp;
    967 	caddr_t *dposp;
    968 	int siz;
    969 	int left;
    970 	caddr_t *cp2;
    971 {
    972 	struct mbuf *m1, *m2;
    973 	struct mbuf *havebuf = NULL;
    974 	caddr_t src = *dposp;
    975 	caddr_t dst;
    976 	int len;
    977 
    978 #ifdef DEBUG
    979 	if (left < 0)
    980 		panic("nfsm_disct: left < 0");
    981 #endif
    982 	m1 = *mdp;
    983 	/*
    984 	 * Skip through the mbuf chain looking for an mbuf with
    985 	 * some data. If the first mbuf found has enough data
    986 	 * and it is correctly aligned return it.
    987 	 */
    988 	while (left == 0) {
    989 		havebuf = m1;
    990 		*mdp = m1 = m1->m_next;
    991 		if (m1 == NULL)
    992 			return (EBADRPC);
    993 		src = mtod(m1, caddr_t);
    994 		left = m1->m_len;
    995 		/*
    996 		 * If we start a new mbuf and it is big enough
    997 		 * and correctly aligned just return it, don't
    998 		 * do any pull up.
    999 		 */
   1000 		if (left >= siz && nfsm_aligned(src)) {
   1001 			*cp2 = src;
   1002 			*dposp = src + siz;
   1003 			return (0);
   1004 		}
   1005 	}
   1006 	if (m1->m_flags & M_EXT) {
   1007 		if (havebuf) {
   1008 			/* If the first mbuf with data has external data
   1009 			 * and there is a previous empty mbuf use it
   1010 			 * to move the data into.
   1011 			 */
   1012 			m2 = m1;
   1013 			*mdp = m1 = havebuf;
   1014 			if (m1->m_flags & M_EXT) {
   1015 				MEXTREMOVE(m1);
   1016 			}
   1017 		} else {
   1018 			/*
   1019 			 * If the first mbuf has a external data
   1020 			 * and there is no previous empty mbuf
   1021 			 * allocate a new mbuf and move the external
   1022 			 * data to the new mbuf. Also make the first
   1023 			 * mbuf look empty.
   1024 			 */
   1025 			m2 = m_get(M_WAIT, MT_DATA);
   1026 			m2->m_ext = m1->m_ext;
   1027 			m2->m_data = src;
   1028 			m2->m_len = left;
   1029 			MCLADDREFERENCE(m1, m2);
   1030 			MEXTREMOVE(m1);
   1031 			m2->m_next = m1->m_next;
   1032 			m1->m_next = m2;
   1033 		}
   1034 		m1->m_len = 0;
   1035 		dst = m1->m_dat;
   1036 	} else {
   1037 		/*
   1038 		 * If the first mbuf has no external data
   1039 		 * move the data to the front of the mbuf.
   1040 		 */
   1041 		if ((dst = m1->m_dat) != src)
   1042 			memmove(dst, src, left);
   1043 		dst += left;
   1044 		m1->m_len = left;
   1045 		m2 = m1->m_next;
   1046 	}
   1047 	m1->m_flags &= ~M_PKTHDR;
   1048 	*cp2 = m1->m_data = m1->m_dat;   /* data is at beginning of buffer */
   1049 	*dposp = mtod(m1, caddr_t) + siz;
   1050 	/*
   1051 	 * Loop through mbufs pulling data up into first mbuf until
   1052 	 * the first mbuf is full or there is no more data to
   1053 	 * pullup.
   1054 	 */
   1055 	while ((len = (MLEN - m1->m_len)) != 0 && m2) {
   1056 		if ((len = min(len, m2->m_len)) != 0)
   1057 			memcpy(dst, m2->m_data, len);
   1058 		m1->m_len += len;
   1059 		dst += len;
   1060 		m2->m_data += len;
   1061 		m2->m_len -= len;
   1062 		m2 = m2->m_next;
   1063 	}
   1064 	if (m1->m_len < siz)
   1065 		return (EBADRPC);
   1066 	return (0);
   1067 }
   1068 
   1069 /*
   1070  * Advance the position in the mbuf chain.
   1071  */
   1072 int
   1073 nfs_adv(mdp, dposp, offs, left)
   1074 	struct mbuf **mdp;
   1075 	caddr_t *dposp;
   1076 	int offs;
   1077 	int left;
   1078 {
   1079 	struct mbuf *m;
   1080 	int s;
   1081 
   1082 	m = *mdp;
   1083 	s = left;
   1084 	while (s < offs) {
   1085 		offs -= s;
   1086 		m = m->m_next;
   1087 		if (m == NULL)
   1088 			return (EBADRPC);
   1089 		s = m->m_len;
   1090 	}
   1091 	*mdp = m;
   1092 	*dposp = mtod(m, caddr_t)+offs;
   1093 	return (0);
   1094 }
   1095 
   1096 /*
   1097  * Copy a string into mbufs for the hard cases...
   1098  */
   1099 int
   1100 nfsm_strtmbuf(mb, bpos, cp, siz)
   1101 	struct mbuf **mb;
   1102 	char **bpos;
   1103 	const char *cp;
   1104 	long siz;
   1105 {
   1106 	struct mbuf *m1 = NULL, *m2;
   1107 	long left, xfer, len, tlen;
   1108 	u_int32_t *tl;
   1109 	int putsize;
   1110 
   1111 	putsize = 1;
   1112 	m2 = *mb;
   1113 	left = M_TRAILINGSPACE(m2);
   1114 	if (left > 0) {
   1115 		tl = ((u_int32_t *)(*bpos));
   1116 		*tl++ = txdr_unsigned(siz);
   1117 		putsize = 0;
   1118 		left -= NFSX_UNSIGNED;
   1119 		m2->m_len += NFSX_UNSIGNED;
   1120 		if (left > 0) {
   1121 			memcpy((caddr_t) tl, cp, left);
   1122 			siz -= left;
   1123 			cp += left;
   1124 			m2->m_len += left;
   1125 			left = 0;
   1126 		}
   1127 	}
   1128 	/* Loop around adding mbufs */
   1129 	while (siz > 0) {
   1130 		MGET(m1, M_WAIT, MT_DATA);
   1131 		if (siz > MLEN)
   1132 			MCLGET(m1, M_WAIT);
   1133 		m1->m_len = NFSMSIZ(m1);
   1134 		m2->m_next = m1;
   1135 		m2 = m1;
   1136 		tl = mtod(m1, u_int32_t *);
   1137 		tlen = 0;
   1138 		if (putsize) {
   1139 			*tl++ = txdr_unsigned(siz);
   1140 			m1->m_len -= NFSX_UNSIGNED;
   1141 			tlen = NFSX_UNSIGNED;
   1142 			putsize = 0;
   1143 		}
   1144 		if (siz < m1->m_len) {
   1145 			len = nfsm_rndup(siz);
   1146 			xfer = siz;
   1147 			if (xfer < len)
   1148 				*(tl+(xfer>>2)) = 0;
   1149 		} else {
   1150 			xfer = len = m1->m_len;
   1151 		}
   1152 		memcpy((caddr_t) tl, cp, xfer);
   1153 		m1->m_len = len+tlen;
   1154 		siz -= xfer;
   1155 		cp += xfer;
   1156 	}
   1157 	*mb = m1;
   1158 	*bpos = mtod(m1, caddr_t)+m1->m_len;
   1159 	return (0);
   1160 }
   1161 
   1162 /*
   1163  * Directory caching routines. They work as follows:
   1164  * - a cache is maintained per VDIR nfsnode.
   1165  * - for each offset cookie that is exported to userspace, and can
   1166  *   thus be thrown back at us as an offset to VOP_READDIR, store
   1167  *   information in the cache.
   1168  * - cached are:
   1169  *   - cookie itself
   1170  *   - blocknumber (essentially just a search key in the buffer cache)
   1171  *   - entry number in block.
   1172  *   - offset cookie of block in which this entry is stored
   1173  *   - 32 bit cookie if NFSMNT_XLATECOOKIE is used.
   1174  * - entries are looked up in a hash table
   1175  * - also maintained is an LRU list of entries, used to determine
   1176  *   which ones to delete if the cache grows too large.
   1177  * - if 32 <-> 64 translation mode is requested for a filesystem,
   1178  *   the cache also functions as a translation table
   1179  * - in the translation case, invalidating the cache does not mean
   1180  *   flushing it, but just marking entries as invalid, except for
   1181  *   the <64bit cookie, 32bitcookie> pair which is still valid, to
   1182  *   still be able to use the cache as a translation table.
   1183  * - 32 bit cookies are uniquely created by combining the hash table
   1184  *   entry value, and one generation count per hash table entry,
   1185  *   incremented each time an entry is appended to the chain.
   1186  * - the cache is invalidated each time a direcory is modified
   1187  * - sanity checks are also done; if an entry in a block turns
   1188  *   out not to have a matching cookie, the cache is invalidated
   1189  *   and a new block starting from the wanted offset is fetched from
   1190  *   the server.
   1191  * - directory entries as read from the server are extended to contain
   1192  *   the 64bit and, optionally, the 32bit cookies, for sanity checking
   1193  *   the cache and exporting them to userspace through the cookie
   1194  *   argument to VOP_READDIR.
   1195  */
   1196 
   1197 u_long
   1198 nfs_dirhash(off)
   1199 	off_t off;
   1200 {
   1201 	int i;
   1202 	char *cp = (char *)&off;
   1203 	u_long sum = 0L;
   1204 
   1205 	for (i = 0 ; i < sizeof (off); i++)
   1206 		sum += *cp++;
   1207 
   1208 	return sum;
   1209 }
   1210 
   1211 void
   1212 nfs_initdircache(vp)
   1213 	struct vnode *vp;
   1214 {
   1215 	struct nfsnode *np = VTONFS(vp);
   1216 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1217 
   1218 	np->n_dircachesize = 0;
   1219 	np->n_dblkno = 1;
   1220 	np->n_dircache =
   1221 	    hashinit(NFS_DIRHASHSIZ, M_NFSDIROFF, M_WAITOK, &nfsdirhashmask);
   1222 	TAILQ_INIT(&np->n_dirchain);
   1223 	if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
   1224 		MALLOC(np->n_dirgens, unsigned *,
   1225 		    NFS_DIRHASHSIZ * sizeof (unsigned), M_NFSDIROFF,
   1226 		    M_WAITOK);
   1227 		memset((caddr_t)np->n_dirgens, 0,
   1228 		    NFS_DIRHASHSIZ * sizeof (unsigned));
   1229 	}
   1230 }
   1231 
   1232 static struct nfsdircache dzero = {0, 0, {0, 0}, {0, 0}, 0, 0, 0};
   1233 
   1234 struct nfsdircache *
   1235 nfs_searchdircache(vp, off, do32, hashent)
   1236 	struct vnode *vp;
   1237 	off_t off;
   1238 	int do32;
   1239 	int *hashent;
   1240 {
   1241 	struct nfsdirhashhead *ndhp;
   1242 	struct nfsdircache *ndp = NULL;
   1243 	struct nfsnode *np = VTONFS(vp);
   1244 	unsigned ent;
   1245 
   1246 	/*
   1247 	 * Zero is always a valid cookie.
   1248 	 */
   1249 	if (off == 0)
   1250 		return &dzero;
   1251 
   1252 	/*
   1253 	 * We use a 32bit cookie as search key, directly reconstruct
   1254 	 * the hashentry. Else use the hashfunction.
   1255 	 */
   1256 	if (do32) {
   1257 		ent = (u_int32_t)off >> 24;
   1258 		if (ent >= NFS_DIRHASHSIZ)
   1259 			return NULL;
   1260 		ndhp = &np->n_dircache[ent];
   1261 	} else {
   1262 		ndhp = NFSDIRHASH(np, off);
   1263 	}
   1264 
   1265 	if (hashent)
   1266 		*hashent = (int)(ndhp - np->n_dircache);
   1267 	if (do32) {
   1268 		for (ndp = ndhp->lh_first; ndp; ndp = ndp->dc_hash.le_next) {
   1269 			if (ndp->dc_cookie32 == (u_int32_t)off) {
   1270 				/*
   1271 				 * An invalidated entry will become the
   1272 				 * start of a new block fetched from
   1273 				 * the server.
   1274 				 */
   1275 				if (ndp->dc_blkno == -1) {
   1276 					ndp->dc_blkcookie = ndp->dc_cookie;
   1277 					ndp->dc_blkno = np->n_dblkno++;
   1278 					ndp->dc_entry = 0;
   1279 				}
   1280 				break;
   1281 			}
   1282 		}
   1283 	} else {
   1284 		for (ndp = ndhp->lh_first; ndp; ndp = ndp->dc_hash.le_next)
   1285 			if (ndp->dc_cookie == off)
   1286 				break;
   1287 	}
   1288 	return ndp;
   1289 }
   1290 
   1291 
   1292 struct nfsdircache *
   1293 nfs_enterdircache(vp, off, blkoff, en, blkno)
   1294 	struct vnode *vp;
   1295 	off_t off, blkoff;
   1296 	daddr_t blkno;
   1297 	int en;
   1298 {
   1299 	struct nfsnode *np = VTONFS(vp);
   1300 	struct nfsdirhashhead *ndhp;
   1301 	struct nfsdircache *ndp = NULL, *first;
   1302 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1303 	int hashent, gen, overwrite;
   1304 
   1305 	if (!np->n_dircache)
   1306 		/*
   1307 		 * XXX would like to do this in nfs_nget but vtype
   1308 		 * isn't known at that time.
   1309 		 */
   1310 		nfs_initdircache(vp);
   1311 
   1312 	/*
   1313 	 * XXX refuse entries for offset 0. amd(8) erroneously sets
   1314 	 * cookie 0 for the '.' entry, making this necessary. This
   1315 	 * isn't so bad, as 0 is a special case anyway.
   1316 	 */
   1317 	if (off == 0)
   1318 		return &dzero;
   1319 
   1320 	ndp = nfs_searchdircache(vp, off, 0, &hashent);
   1321 
   1322 	if (ndp && ndp->dc_blkno != -1) {
   1323 		/*
   1324 		 * Overwriting an old entry. Check if it's the same.
   1325 		 * If so, just return. If not, remove the old entry.
   1326 		 */
   1327 		if (ndp->dc_blkcookie == blkoff && ndp->dc_entry == en)
   1328 			return ndp;
   1329 		TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain);
   1330 		LIST_REMOVE(ndp, dc_hash);
   1331 		FREE(ndp, M_NFSDIROFF);
   1332 		ndp = 0;
   1333 	}
   1334 
   1335 	ndhp = &np->n_dircache[hashent];
   1336 
   1337 	if (!ndp) {
   1338 		MALLOC(ndp, struct nfsdircache *, sizeof (*ndp), M_NFSDIROFF,
   1339 		    M_WAITOK);
   1340 		overwrite = 0;
   1341 		if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
   1342 			/*
   1343 			 * We're allocating a new entry, so bump the
   1344 			 * generation number.
   1345 			 */
   1346 			gen = ++np->n_dirgens[hashent];
   1347 			if (gen == 0) {
   1348 				np->n_dirgens[hashent]++;
   1349 				gen++;
   1350 			}
   1351 			ndp->dc_cookie32 = (hashent << 24) | (gen & 0xffffff);
   1352 		}
   1353 	} else
   1354 		overwrite = 1;
   1355 
   1356 	/*
   1357 	 * If the entry number is 0, we are at the start of a new block, so
   1358 	 * allocate a new blocknumber.
   1359 	 */
   1360 	if (en == 0)
   1361 		ndp->dc_blkno = np->n_dblkno++;
   1362 	else
   1363 		ndp->dc_blkno = blkno;
   1364 
   1365 	ndp->dc_cookie = off;
   1366 	ndp->dc_blkcookie = blkoff;
   1367 	ndp->dc_entry = en;
   1368 
   1369 	if (overwrite)
   1370 		return ndp;
   1371 
   1372 	/*
   1373 	 * If the maximum directory cookie cache size has been reached
   1374 	 * for this node, take one off the front. The idea is that
   1375 	 * directories are typically read front-to-back once, so that
   1376 	 * the oldest entries can be thrown away without much performance
   1377 	 * loss.
   1378 	 */
   1379 	if (np->n_dircachesize == NFS_MAXDIRCACHE) {
   1380 		first = np->n_dirchain.tqh_first;
   1381 		TAILQ_REMOVE(&np->n_dirchain, first, dc_chain);
   1382 		LIST_REMOVE(first, dc_hash);
   1383 		FREE(first, M_NFSDIROFF);
   1384 	} else
   1385 		np->n_dircachesize++;
   1386 
   1387 	LIST_INSERT_HEAD(ndhp, ndp, dc_hash);
   1388 	TAILQ_INSERT_TAIL(&np->n_dirchain, ndp, dc_chain);
   1389 	return ndp;
   1390 }
   1391 
   1392 void
   1393 nfs_invaldircache(vp, forcefree)
   1394 	struct vnode *vp;
   1395 	int forcefree;
   1396 {
   1397 	struct nfsnode *np = VTONFS(vp);
   1398 	struct nfsdircache *ndp = NULL;
   1399 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1400 
   1401 #ifdef DIAGNOSTIC
   1402 	if (vp->v_type != VDIR)
   1403 		panic("nfs: invaldircache: not dir");
   1404 #endif
   1405 
   1406 	if (!np->n_dircache)
   1407 		return;
   1408 
   1409 	if (!(nmp->nm_flag & NFSMNT_XLATECOOKIE) || forcefree) {
   1410 		while ((ndp = np->n_dirchain.tqh_first)) {
   1411 			TAILQ_REMOVE(&np->n_dirchain, ndp, dc_chain);
   1412 			LIST_REMOVE(ndp, dc_hash);
   1413 			FREE(ndp, M_NFSDIROFF);
   1414 		}
   1415 		np->n_dircachesize = 0;
   1416 		if (forcefree && np->n_dirgens) {
   1417 			FREE(np->n_dirgens, M_NFSDIROFF);
   1418 		}
   1419 	} else {
   1420 		for (ndp = np->n_dirchain.tqh_first; ndp;
   1421 		    ndp = ndp->dc_chain.tqe_next)
   1422 			ndp->dc_blkno = -1;
   1423 	}
   1424 
   1425 	np->n_dblkno = 1;
   1426 }
   1427 
   1428 /*
   1429  * Called once before VFS init to initialize shared and
   1430  * server-specific data structures.
   1431  */
   1432 void
   1433 nfs_init()
   1434 {
   1435 	nfsrtt.pos = 0;
   1436 	rpc_vers = txdr_unsigned(RPC_VER2);
   1437 	rpc_call = txdr_unsigned(RPC_CALL);
   1438 	rpc_reply = txdr_unsigned(RPC_REPLY);
   1439 	rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
   1440 	rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
   1441 	rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
   1442 	rpc_autherr = txdr_unsigned(RPC_AUTHERR);
   1443 	rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
   1444 	rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
   1445 	nfs_prog = txdr_unsigned(NFS_PROG);
   1446 	nqnfs_prog = txdr_unsigned(NQNFS_PROG);
   1447 	nfs_true = txdr_unsigned(TRUE);
   1448 	nfs_false = txdr_unsigned(FALSE);
   1449 	nfs_xdrneg1 = txdr_unsigned(-1);
   1450 	nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
   1451 	if (nfs_ticks < 1)
   1452 		nfs_ticks = 1;
   1453 #ifdef NFSSERVER
   1454 	nfsrv_init(0);			/* Init server data structures */
   1455 	nfsrv_initcache();		/* Init the server request cache */
   1456 #endif /* NFSSERVER */
   1457 
   1458 #if defined(NFSSERVER) || !defined(NFS_V2_ONLY)
   1459 	/*
   1460 	 * Initialize the nqnfs data structures.
   1461 	 */
   1462 	if (nqnfsstarttime == 0) {
   1463 		nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
   1464 			+ nqsrv_clockskew + nqsrv_writeslack;
   1465 		NQLOADNOVRAM(nqnfsstarttime);
   1466 		CIRCLEQ_INIT(&nqtimerhead);
   1467 		nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, M_WAITOK, &nqfhhash);
   1468 	}
   1469 #endif
   1470 
   1471 	/*
   1472 	 * Initialize reply list and start timer
   1473 	 */
   1474 	TAILQ_INIT(&nfs_reqq);
   1475 	nfs_timer(NULL);
   1476 }
   1477 
   1478 #ifdef NFS
   1479 /*
   1480  * Called once at VFS init to initialize client-specific data structures.
   1481  */
   1482 void
   1483 nfs_vfs_init()
   1484 {
   1485 	int i;
   1486 
   1487 	/* Ensure async daemons disabled */
   1488 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
   1489 		nfs_iodwant[i] = (struct proc *)0;
   1490 		nfs_iodmount[i] = (struct nfsmount *)0;
   1491 	}
   1492 	nfs_nhinit();			/* Init the nfsnode table */
   1493 }
   1494 
   1495 void
   1496 nfs_vfs_done()
   1497 {
   1498 	nfs_nhdone();
   1499 }
   1500 
   1501 /*
   1502  * Attribute cache routines.
   1503  * nfs_loadattrcache() - loads or updates the cache contents from attributes
   1504  *	that are on the mbuf list
   1505  * nfs_getattrcache() - returns valid attributes if found in cache, returns
   1506  *	error otherwise
   1507  */
   1508 
   1509 /*
   1510  * Load the attribute cache (that lives in the nfsnode entry) with
   1511  * the values on the mbuf list and
   1512  * Iff vap not NULL
   1513  *    copy the attributes to *vaper
   1514  */
   1515 int
   1516 nfsm_loadattrcache(vpp, mdp, dposp, vaper)
   1517 	struct vnode **vpp;
   1518 	struct mbuf **mdp;
   1519 	caddr_t *dposp;
   1520 	struct vattr *vaper;
   1521 {
   1522 	int32_t t1;
   1523 	caddr_t cp2;
   1524 	int error = 0;
   1525 	struct mbuf *md;
   1526 	int v3 = NFS_ISV3(*vpp);
   1527 
   1528 	md = *mdp;
   1529 	t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
   1530 	error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2);
   1531 	if (error)
   1532 		return (error);
   1533 	return nfs_loadattrcache(vpp, (struct nfs_fattr *)cp2, vaper);
   1534 }
   1535 
   1536 int
   1537 nfs_loadattrcache(vpp, fp, vaper)
   1538 	struct vnode **vpp;
   1539 	struct nfs_fattr *fp;
   1540 	struct vattr *vaper;
   1541 {
   1542 	struct vnode *vp = *vpp;
   1543 	struct vattr *vap;
   1544 	int v3 = NFS_ISV3(vp);
   1545 	enum vtype vtyp;
   1546 	u_short vmode;
   1547 	struct timespec mtime;
   1548 	struct vnode *nvp;
   1549 	int32_t rdev;
   1550 	struct nfsnode *np;
   1551 	extern int (**spec_nfsv2nodeop_p) __P((void *));
   1552 
   1553 	if (v3) {
   1554 		vtyp = nfsv3tov_type(fp->fa_type);
   1555 		vmode = fxdr_unsigned(u_short, fp->fa_mode);
   1556 		rdev = makedev(fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata1),
   1557 			fxdr_unsigned(u_int32_t, fp->fa3_rdev.specdata2));
   1558 		fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
   1559 	} else {
   1560 		vtyp = nfsv2tov_type(fp->fa_type);
   1561 		vmode = fxdr_unsigned(u_short, fp->fa_mode);
   1562 		if (vtyp == VNON || vtyp == VREG)
   1563 			vtyp = IFTOVT(vmode);
   1564 		rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
   1565 		fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
   1566 
   1567 		/*
   1568 		 * Really ugly NFSv2 kludge.
   1569 		 */
   1570 		if (vtyp == VCHR && rdev == 0xffffffff)
   1571 			vtyp = VFIFO;
   1572 	}
   1573 
   1574 	/*
   1575 	 * If v_type == VNON it is a new node, so fill in the v_type,
   1576 	 * n_mtime fields. Check to see if it represents a special
   1577 	 * device, and if so, check for a possible alias. Once the
   1578 	 * correct vnode has been obtained, fill in the rest of the
   1579 	 * information.
   1580 	 */
   1581 	np = VTONFS(vp);
   1582 	if (vp->v_type != vtyp) {
   1583 		vp->v_type = vtyp;
   1584 		if (vp->v_type == VFIFO) {
   1585 			extern int (**fifo_nfsv2nodeop_p) __P((void *));
   1586 			vp->v_op = fifo_nfsv2nodeop_p;
   1587 		}
   1588 		if (vp->v_type == VCHR || vp->v_type == VBLK) {
   1589 			vp->v_op = spec_nfsv2nodeop_p;
   1590 			nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
   1591 			if (nvp) {
   1592 				/*
   1593 				 * Discard unneeded vnode, but save its nfsnode.
   1594 				 * Since the nfsnode does not have a lock, its
   1595 				 * vnode lock has to be carried over.
   1596 				 */
   1597 				nvp->v_data = vp->v_data;
   1598 				vp->v_data = NULL;
   1599 				vp->v_op = spec_vnodeop_p;
   1600 				vput(vp);
   1601 				vgone(vp);
   1602 				/*
   1603 				 * XXX When nfs starts locking, we need to
   1604 				 * lock the new node here.
   1605 				 */
   1606 				/*
   1607 				 * Reinitialize aliased node.
   1608 				 */
   1609 				np->n_vnode = nvp;
   1610 				*vpp = vp = nvp;
   1611 			}
   1612 		}
   1613 		np->n_mtime = mtime.tv_sec;
   1614 	}
   1615 	vap = np->n_vattr;
   1616 	vap->va_type = vtyp;
   1617 	vap->va_mode = vmode & ALLPERMS;
   1618 	vap->va_rdev = (dev_t)rdev;
   1619 	vap->va_mtime = mtime;
   1620 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
   1621 	switch (vtyp) {
   1622 	case VDIR:
   1623 		vap->va_blocksize = NFS_DIRFRAGSIZ;
   1624 		break;
   1625 	case VBLK:
   1626 		vap->va_blocksize = BLKDEV_IOSIZE;
   1627 		break;
   1628 	case VCHR:
   1629 		vap->va_blocksize = MAXBSIZE;
   1630 		break;
   1631 	default:
   1632 		vap->va_blocksize = v3 ? vp->v_mount->mnt_stat.f_iosize :
   1633 		    fxdr_unsigned(int32_t, fp->fa2_blocksize);
   1634 		break;
   1635 	}
   1636 	if (v3) {
   1637 		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
   1638 		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
   1639 		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
   1640 		vap->va_size = fxdr_hyper(&fp->fa3_size);
   1641 		vap->va_bytes = fxdr_hyper(&fp->fa3_used);
   1642 		vap->va_fileid = fxdr_unsigned(int32_t,
   1643 		    fp->fa3_fileid.nfsuquad[1]);
   1644 		fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
   1645 		fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
   1646 		vap->va_flags = 0;
   1647 		vap->va_filerev = 0;
   1648 	} else {
   1649 		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
   1650 		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
   1651 		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
   1652 		vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
   1653 		vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks)
   1654 		    * NFS_FABLKSIZE;
   1655 		vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
   1656 		fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
   1657 		vap->va_flags = 0;
   1658 		vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
   1659 		    fp->fa2_ctime.nfsv2_sec);
   1660 		vap->va_ctime.tv_nsec = 0;
   1661 		vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
   1662 		vap->va_filerev = 0;
   1663 	}
   1664 	if (vap->va_size != np->n_size) {
   1665 		if (vap->va_type == VREG) {
   1666 			if (np->n_flag & NMODIFIED) {
   1667 				if (vap->va_size < np->n_size)
   1668 					vap->va_size = np->n_size;
   1669 				else
   1670 					np->n_size = vap->va_size;
   1671 			} else
   1672 				np->n_size = vap->va_size;
   1673 			uvm_vnp_setsize(vp, np->n_size);
   1674 		} else
   1675 			np->n_size = vap->va_size;
   1676 	}
   1677 	np->n_attrstamp = time.tv_sec;
   1678 	if (vaper != NULL) {
   1679 		memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(*vap));
   1680 		if (np->n_flag & NCHG) {
   1681 			if (np->n_flag & NACC)
   1682 				vaper->va_atime = np->n_atim;
   1683 			if (np->n_flag & NUPD)
   1684 				vaper->va_mtime = np->n_mtim;
   1685 		}
   1686 	}
   1687 	return (0);
   1688 }
   1689 
   1690 /*
   1691  * Check the time stamp
   1692  * If the cache is valid, copy contents to *vap and return 0
   1693  * otherwise return an error
   1694  */
   1695 int
   1696 nfs_getattrcache(vp, vaper)
   1697 	struct vnode *vp;
   1698 	struct vattr *vaper;
   1699 {
   1700 	struct nfsnode *np = VTONFS(vp);
   1701 	struct vattr *vap;
   1702 
   1703 	if ((time.tv_sec - np->n_attrstamp) >= NFS_ATTRTIMEO(np)) {
   1704 		nfsstats.attrcache_misses++;
   1705 		return (ENOENT);
   1706 	}
   1707 	nfsstats.attrcache_hits++;
   1708 	vap = np->n_vattr;
   1709 	if (vap->va_size != np->n_size) {
   1710 		if (vap->va_type == VREG) {
   1711 			if (np->n_flag & NMODIFIED) {
   1712 				if (vap->va_size < np->n_size)
   1713 					vap->va_size = np->n_size;
   1714 				else
   1715 					np->n_size = vap->va_size;
   1716 			} else
   1717 				np->n_size = vap->va_size;
   1718 			uvm_vnp_setsize(vp, np->n_size);
   1719 		} else
   1720 			np->n_size = vap->va_size;
   1721 	}
   1722 	memcpy((caddr_t)vaper, (caddr_t)vap, sizeof(struct vattr));
   1723 	if (np->n_flag & NCHG) {
   1724 		if (np->n_flag & NACC)
   1725 			vaper->va_atime = np->n_atim;
   1726 		if (np->n_flag & NUPD)
   1727 			vaper->va_mtime = np->n_mtim;
   1728 	}
   1729 	return (0);
   1730 }
   1731 
   1732 /*
   1733  * Heuristic to see if the server XDR encodes directory cookies or not.
   1734  * it is not supposed to, but a lot of servers may do this. Also, since
   1735  * most/all servers will implement V2 as well, it is expected that they
   1736  * may return just 32 bits worth of cookie information, so we need to
   1737  * find out in which 32 bits this information is available. We do this
   1738  * to avoid trouble with emulated binaries that can't handle 64 bit
   1739  * directory offsets.
   1740  */
   1741 
   1742 void
   1743 nfs_cookieheuristic(vp, flagp, p, cred)
   1744 	struct vnode *vp;
   1745 	int *flagp;
   1746 	struct proc *p;
   1747 	struct ucred *cred;
   1748 {
   1749 	struct uio auio;
   1750 	struct iovec aiov;
   1751 	caddr_t buf, cp;
   1752 	struct dirent *dp;
   1753 	off_t *cookies = NULL, *cop;
   1754 	int error, eof, nc, len;
   1755 
   1756 	MALLOC(buf, caddr_t, NFS_DIRFRAGSIZ, M_TEMP, M_WAITOK);
   1757 
   1758 	aiov.iov_base = buf;
   1759 	aiov.iov_len = NFS_DIRFRAGSIZ;
   1760 	auio.uio_iov = &aiov;
   1761 	auio.uio_iovcnt = 1;
   1762 	auio.uio_rw = UIO_READ;
   1763 	auio.uio_segflg = UIO_SYSSPACE;
   1764 	auio.uio_procp = p;
   1765 	auio.uio_resid = NFS_DIRFRAGSIZ;
   1766 	auio.uio_offset = 0;
   1767 
   1768 	error = VOP_READDIR(vp, &auio, cred, &eof, &cookies, &nc);
   1769 
   1770 	len = NFS_DIRFRAGSIZ - auio.uio_resid;
   1771 	if (error || len == 0) {
   1772 		FREE(buf, M_TEMP);
   1773 		if (cookies)
   1774 			free(cookies, M_TEMP);
   1775 		return;
   1776 	}
   1777 
   1778 	/*
   1779 	 * Find the first valid entry and look at its offset cookie.
   1780 	 */
   1781 
   1782 	cp = buf;
   1783 	for (cop = cookies; len > 0; len -= dp->d_reclen) {
   1784 		dp = (struct dirent *)cp;
   1785 		if (dp->d_fileno != 0 && len >= dp->d_reclen) {
   1786 			if ((*cop >> 32) != 0 && (*cop & 0xffffffffLL) == 0) {
   1787 				*flagp |= NFSMNT_SWAPCOOKIE;
   1788 				nfs_invaldircache(vp, 0);
   1789 				nfs_vinvalbuf(vp, 0, cred, p, 1);
   1790 			}
   1791 			break;
   1792 		}
   1793 		cop++;
   1794 		cp += dp->d_reclen;
   1795 	}
   1796 
   1797 	FREE(buf, M_TEMP);
   1798 	free(cookies, M_TEMP);
   1799 }
   1800 #endif /* NFS */
   1801 
   1802 /*
   1803  * Set up nameidata for a lookup() call and do it.
   1804  *
   1805  * If pubflag is set, this call is done for a lookup operation on the
   1806  * public filehandle. In that case we allow crossing mountpoints and
   1807  * absolute pathnames. However, the caller is expected to check that
   1808  * the lookup result is within the public fs, and deny access if
   1809  * it is not.
   1810  */
   1811 int
   1812 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
   1813 	struct nameidata *ndp;
   1814 	fhandle_t *fhp;
   1815 	int len;
   1816 	struct nfssvc_sock *slp;
   1817 	struct mbuf *nam;
   1818 	struct mbuf **mdp;
   1819 	caddr_t *dposp;
   1820 	struct vnode **retdirp;
   1821 	struct proc *p;
   1822 	int kerbflag, pubflag;
   1823 {
   1824 	int i, rem;
   1825 	struct mbuf *md;
   1826 	char *fromcp, *tocp, *cp;
   1827 	struct iovec aiov;
   1828 	struct uio auio;
   1829 	struct vnode *dp;
   1830 	int error, rdonly, linklen;
   1831 	struct componentname *cnp = &ndp->ni_cnd;
   1832 
   1833 	*retdirp = (struct vnode *)0;
   1834 
   1835 	if ((len + 1) > MAXPATHLEN)
   1836 		return (ENAMETOOLONG);
   1837 	cnp->cn_pnbuf = PNBUF_GET();
   1838 
   1839 	/*
   1840 	 * Copy the name from the mbuf list to ndp->ni_pnbuf
   1841 	 * and set the various ndp fields appropriately.
   1842 	 */
   1843 	fromcp = *dposp;
   1844 	tocp = cnp->cn_pnbuf;
   1845 	md = *mdp;
   1846 	rem = mtod(md, caddr_t) + md->m_len - fromcp;
   1847 	for (i = 0; i < len; i++) {
   1848 		while (rem == 0) {
   1849 			md = md->m_next;
   1850 			if (md == NULL) {
   1851 				error = EBADRPC;
   1852 				goto out;
   1853 			}
   1854 			fromcp = mtod(md, caddr_t);
   1855 			rem = md->m_len;
   1856 		}
   1857 		if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
   1858 			error = EACCES;
   1859 			goto out;
   1860 		}
   1861 		*tocp++ = *fromcp++;
   1862 		rem--;
   1863 	}
   1864 	*tocp = '\0';
   1865 	*mdp = md;
   1866 	*dposp = fromcp;
   1867 	len = nfsm_rndup(len)-len;
   1868 	if (len > 0) {
   1869 		if (rem >= len)
   1870 			*dposp += len;
   1871 		else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
   1872 			goto out;
   1873 	}
   1874 
   1875 	/*
   1876 	 * Extract and set starting directory.
   1877 	 */
   1878 	error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
   1879 	    nam, &rdonly, kerbflag, pubflag);
   1880 	if (error)
   1881 		goto out;
   1882 	if (dp->v_type != VDIR) {
   1883 		vrele(dp);
   1884 		error = ENOTDIR;
   1885 		goto out;
   1886 	}
   1887 
   1888 	if (rdonly)
   1889 		cnp->cn_flags |= RDONLY;
   1890 
   1891 	*retdirp = dp;
   1892 
   1893 	if (pubflag) {
   1894 		/*
   1895 		 * Oh joy. For WebNFS, handle those pesky '%' escapes,
   1896 		 * and the 'native path' indicator.
   1897 		 */
   1898 		cp = PNBUF_GET();
   1899 		fromcp = cnp->cn_pnbuf;
   1900 		tocp = cp;
   1901 		if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
   1902 			switch ((unsigned char)*fromcp) {
   1903 			case WEBNFS_NATIVE_CHAR:
   1904 				/*
   1905 				 * 'Native' path for us is the same
   1906 				 * as a path according to the NFS spec,
   1907 				 * just skip the escape char.
   1908 				 */
   1909 				fromcp++;
   1910 				break;
   1911 			/*
   1912 			 * More may be added in the future, range 0x80-0xff
   1913 			 */
   1914 			default:
   1915 				error = EIO;
   1916 				FREE(cp, M_NAMEI);
   1917 				goto out;
   1918 			}
   1919 		}
   1920 		/*
   1921 		 * Translate the '%' escapes, URL-style.
   1922 		 */
   1923 		while (*fromcp != '\0') {
   1924 			if (*fromcp == WEBNFS_ESC_CHAR) {
   1925 				if (fromcp[1] != '\0' && fromcp[2] != '\0') {
   1926 					fromcp++;
   1927 					*tocp++ = HEXSTRTOI(fromcp);
   1928 					fromcp += 2;
   1929 					continue;
   1930 				} else {
   1931 					error = ENOENT;
   1932 					FREE(cp, M_NAMEI);
   1933 					goto out;
   1934 				}
   1935 			} else
   1936 				*tocp++ = *fromcp++;
   1937 		}
   1938 		*tocp = '\0';
   1939 		PNBUF_PUT(cnp->cn_pnbuf);
   1940 		cnp->cn_pnbuf = cp;
   1941 	}
   1942 
   1943 	ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
   1944 	ndp->ni_segflg = UIO_SYSSPACE;
   1945 
   1946 	if (pubflag) {
   1947 		ndp->ni_rootdir = rootvnode;
   1948 		ndp->ni_loopcnt = 0;
   1949 		if (cnp->cn_pnbuf[0] == '/')
   1950 			dp = rootvnode;
   1951 	} else {
   1952 		cnp->cn_flags |= NOCROSSMOUNT;
   1953 	}
   1954 
   1955 	cnp->cn_proc = p;
   1956 	VREF(dp);
   1957 
   1958     for (;;) {
   1959 	cnp->cn_nameptr = cnp->cn_pnbuf;
   1960 	ndp->ni_startdir = dp;
   1961 	/*
   1962 	 * And call lookup() to do the real work
   1963 	 */
   1964 	error = lookup(ndp);
   1965 	if (error)
   1966 		break;
   1967 	/*
   1968 	 * Check for encountering a symbolic link
   1969 	 */
   1970 	if ((cnp->cn_flags & ISSYMLINK) == 0) {
   1971 		if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
   1972 			cnp->cn_flags |= HASBUF;
   1973 			return (0);
   1974 		}
   1975 		break;
   1976 	} else {
   1977 		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
   1978 			VOP_UNLOCK(ndp->ni_dvp, 0);
   1979 		if (!pubflag) {
   1980 			vrele(ndp->ni_dvp);
   1981 			vput(ndp->ni_vp);
   1982 			ndp->ni_vp = NULL;
   1983 			error = EINVAL;
   1984 			break;
   1985 		}
   1986 
   1987 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
   1988 			error = ELOOP;
   1989 			break;
   1990 		}
   1991 		if (ndp->ni_pathlen > 1)
   1992 			cp = PNBUF_GET();
   1993 		else
   1994 			cp = cnp->cn_pnbuf;
   1995 		aiov.iov_base = cp;
   1996 		aiov.iov_len = MAXPATHLEN;
   1997 		auio.uio_iov = &aiov;
   1998 		auio.uio_iovcnt = 1;
   1999 		auio.uio_offset = 0;
   2000 		auio.uio_rw = UIO_READ;
   2001 		auio.uio_segflg = UIO_SYSSPACE;
   2002 		auio.uio_procp = (struct proc *)0;
   2003 		auio.uio_resid = MAXPATHLEN;
   2004 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
   2005 		if (error) {
   2006 		badlink:
   2007 			if (ndp->ni_pathlen > 1)
   2008 				PNBUF_PUT(cp);
   2009 			break;
   2010 		}
   2011 		linklen = MAXPATHLEN - auio.uio_resid;
   2012 		if (linklen == 0) {
   2013 			error = ENOENT;
   2014 			goto badlink;
   2015 		}
   2016 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
   2017 			error = ENAMETOOLONG;
   2018 			goto badlink;
   2019 		}
   2020 		if (ndp->ni_pathlen > 1) {
   2021 			memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
   2022 			PNBUF_PUT(cnp->cn_pnbuf);
   2023 			cnp->cn_pnbuf = cp;
   2024 		} else
   2025 			cnp->cn_pnbuf[linklen] = '\0';
   2026 		ndp->ni_pathlen += linklen;
   2027 		vput(ndp->ni_vp);
   2028 		dp = ndp->ni_dvp;
   2029 		/*
   2030 		 * Check if root directory should replace current directory.
   2031 		 */
   2032 		if (cnp->cn_pnbuf[0] == '/') {
   2033 			vrele(dp);
   2034 			dp = ndp->ni_rootdir;
   2035 			VREF(dp);
   2036 		}
   2037 	}
   2038    }
   2039 out:
   2040 	PNBUF_PUT(cnp->cn_pnbuf);
   2041 	return (error);
   2042 }
   2043 
   2044 /*
   2045  * A fiddled version of m_adj() that ensures null fill to a long
   2046  * boundary and only trims off the back end
   2047  */
   2048 void
   2049 nfsm_adj(mp, len, nul)
   2050 	struct mbuf *mp;
   2051 	int len;
   2052 	int nul;
   2053 {
   2054 	struct mbuf *m;
   2055 	int count, i;
   2056 	char *cp;
   2057 
   2058 	/*
   2059 	 * Trim from tail.  Scan the mbuf chain,
   2060 	 * calculating its length and finding the last mbuf.
   2061 	 * If the adjustment only affects this mbuf, then just
   2062 	 * adjust and return.  Otherwise, rescan and truncate
   2063 	 * after the remaining size.
   2064 	 */
   2065 	count = 0;
   2066 	m = mp;
   2067 	for (;;) {
   2068 		count += m->m_len;
   2069 		if (m->m_next == (struct mbuf *)0)
   2070 			break;
   2071 		m = m->m_next;
   2072 	}
   2073 	if (m->m_len > len) {
   2074 		m->m_len -= len;
   2075 		if (nul > 0) {
   2076 			cp = mtod(m, caddr_t)+m->m_len-nul;
   2077 			for (i = 0; i < nul; i++)
   2078 				*cp++ = '\0';
   2079 		}
   2080 		return;
   2081 	}
   2082 	count -= len;
   2083 	if (count < 0)
   2084 		count = 0;
   2085 	/*
   2086 	 * Correct length for chain is "count".
   2087 	 * Find the mbuf with last data, adjust its length,
   2088 	 * and toss data from remaining mbufs on chain.
   2089 	 */
   2090 	for (m = mp; m; m = m->m_next) {
   2091 		if (m->m_len >= count) {
   2092 			m->m_len = count;
   2093 			if (nul > 0) {
   2094 				cp = mtod(m, caddr_t)+m->m_len-nul;
   2095 				for (i = 0; i < nul; i++)
   2096 					*cp++ = '\0';
   2097 			}
   2098 			break;
   2099 		}
   2100 		count -= m->m_len;
   2101 	}
   2102 	for (m = m->m_next;m;m = m->m_next)
   2103 		m->m_len = 0;
   2104 }
   2105 
   2106 /*
   2107  * Make these functions instead of macros, so that the kernel text size
   2108  * doesn't get too big...
   2109  */
   2110 void
   2111 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
   2112 	struct nfsrv_descript *nfsd;
   2113 	int before_ret;
   2114 	struct vattr *before_vap;
   2115 	int after_ret;
   2116 	struct vattr *after_vap;
   2117 	struct mbuf **mbp;
   2118 	char **bposp;
   2119 {
   2120 	struct mbuf *mb = *mbp, *mb2;
   2121 	char *bpos = *bposp;
   2122 	u_int32_t *tl;
   2123 
   2124 	if (before_ret) {
   2125 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   2126 		*tl = nfs_false;
   2127 	} else {
   2128 		nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
   2129 		*tl++ = nfs_true;
   2130 		txdr_hyper(before_vap->va_size, tl);
   2131 		tl += 2;
   2132 		txdr_nfsv3time(&(before_vap->va_mtime), tl);
   2133 		tl += 2;
   2134 		txdr_nfsv3time(&(before_vap->va_ctime), tl);
   2135 	}
   2136 	*bposp = bpos;
   2137 	*mbp = mb;
   2138 	nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
   2139 }
   2140 
   2141 void
   2142 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
   2143 	struct nfsrv_descript *nfsd;
   2144 	int after_ret;
   2145 	struct vattr *after_vap;
   2146 	struct mbuf **mbp;
   2147 	char **bposp;
   2148 {
   2149 	struct mbuf *mb = *mbp, *mb2;
   2150 	char *bpos = *bposp;
   2151 	u_int32_t *tl;
   2152 	struct nfs_fattr *fp;
   2153 
   2154 	if (after_ret) {
   2155 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   2156 		*tl = nfs_false;
   2157 	} else {
   2158 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
   2159 		*tl++ = nfs_true;
   2160 		fp = (struct nfs_fattr *)tl;
   2161 		nfsm_srvfattr(nfsd, after_vap, fp);
   2162 	}
   2163 	*mbp = mb;
   2164 	*bposp = bpos;
   2165 }
   2166 
   2167 void
   2168 nfsm_srvfattr(nfsd, vap, fp)
   2169 	struct nfsrv_descript *nfsd;
   2170 	struct vattr *vap;
   2171 	struct nfs_fattr *fp;
   2172 {
   2173 
   2174 	fp->fa_nlink = txdr_unsigned(vap->va_nlink);
   2175 	fp->fa_uid = txdr_unsigned(vap->va_uid);
   2176 	fp->fa_gid = txdr_unsigned(vap->va_gid);
   2177 	if (nfsd->nd_flag & ND_NFSV3) {
   2178 		fp->fa_type = vtonfsv3_type(vap->va_type);
   2179 		fp->fa_mode = vtonfsv3_mode(vap->va_mode);
   2180 		txdr_hyper(vap->va_size, &fp->fa3_size);
   2181 		txdr_hyper(vap->va_bytes, &fp->fa3_used);
   2182 		fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
   2183 		fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
   2184 		fp->fa3_fsid.nfsuquad[0] = 0;
   2185 		fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
   2186 		fp->fa3_fileid.nfsuquad[0] = 0;
   2187 		fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
   2188 		txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
   2189 		txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
   2190 		txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
   2191 	} else {
   2192 		fp->fa_type = vtonfsv2_type(vap->va_type);
   2193 		fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
   2194 		fp->fa2_size = txdr_unsigned(vap->va_size);
   2195 		fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
   2196 		if (vap->va_type == VFIFO)
   2197 			fp->fa2_rdev = 0xffffffff;
   2198 		else
   2199 			fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
   2200 		fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
   2201 		fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
   2202 		fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
   2203 		txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
   2204 		txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
   2205 		txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
   2206 	}
   2207 }
   2208 
   2209 /*
   2210  * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
   2211  * 	- look up fsid in mount list (if not found ret error)
   2212  *	- get vp and export rights by calling VFS_FHTOVP()
   2213  *	- if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
   2214  *	- if not lockflag unlock it with VOP_UNLOCK()
   2215  */
   2216 int
   2217 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
   2218 	fhandle_t *fhp;
   2219 	int lockflag;
   2220 	struct vnode **vpp;
   2221 	struct ucred *cred;
   2222 	struct nfssvc_sock *slp;
   2223 	struct mbuf *nam;
   2224 	int *rdonlyp;
   2225 	int kerbflag;
   2226 {
   2227 	struct mount *mp;
   2228 	int i;
   2229 	struct ucred *credanon;
   2230 	int error, exflags;
   2231 	struct sockaddr_in *saddr;
   2232 
   2233 	*vpp = (struct vnode *)0;
   2234 
   2235 	if (nfs_ispublicfh(fhp)) {
   2236 		if (!pubflag || !nfs_pub.np_valid)
   2237 			return (ESTALE);
   2238 		fhp = &nfs_pub.np_handle;
   2239 	}
   2240 
   2241 	mp = vfs_getvfs(&fhp->fh_fsid);
   2242 	if (!mp)
   2243 		return (ESTALE);
   2244 	error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
   2245 	if (error)
   2246 		return (error);
   2247 	error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
   2248 	if (error)
   2249 		return (error);
   2250 
   2251 	if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
   2252 		saddr = mtod(nam, struct sockaddr_in *);
   2253 		if ((saddr->sin_family == AF_INET) &&
   2254 		    ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
   2255 			vput(*vpp);
   2256 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
   2257 		}
   2258 #ifdef INET6
   2259 		if ((saddr->sin_family == AF_INET6) &&
   2260 		    ntohs(saddr->sin_port) >= IPV6PORT_RESERVED) {
   2261 			vput(*vpp);
   2262 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
   2263 		}
   2264 #endif
   2265 	}
   2266 	/*
   2267 	 * Check/setup credentials.
   2268 	 */
   2269 	if (exflags & MNT_EXKERB) {
   2270 		if (!kerbflag) {
   2271 			vput(*vpp);
   2272 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
   2273 		}
   2274 	} else if (kerbflag) {
   2275 		vput(*vpp);
   2276 		return (NFSERR_AUTHERR | AUTH_TOOWEAK);
   2277 	} else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
   2278 		cred->cr_uid = credanon->cr_uid;
   2279 		cred->cr_gid = credanon->cr_gid;
   2280 		for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
   2281 			cred->cr_groups[i] = credanon->cr_groups[i];
   2282 		cred->cr_ngroups = i;
   2283 	}
   2284 	if (exflags & MNT_EXRDONLY)
   2285 		*rdonlyp = 1;
   2286 	else
   2287 		*rdonlyp = 0;
   2288 	if (!lockflag)
   2289 		VOP_UNLOCK(*vpp, 0);
   2290 	return (0);
   2291 }
   2292 
   2293 /*
   2294  * WebNFS: check if a filehandle is a public filehandle. For v3, this
   2295  * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
   2296  * transformed this to all zeroes in both cases, so check for it.
   2297  */
   2298 int
   2299 nfs_ispublicfh(fhp)
   2300 	fhandle_t *fhp;
   2301 {
   2302 	char *cp = (char *)fhp;
   2303 	int i;
   2304 
   2305 	for (i = 0; i < NFSX_V3FH; i++)
   2306 		if (*cp++ != 0)
   2307 			return (FALSE);
   2308 	return (TRUE);
   2309 }
   2310 
   2311 /*
   2312  * This function compares two net addresses by family and returns TRUE
   2313  * if they are the same host.
   2314  * If there is any doubt, return FALSE.
   2315  * The AF_INET family is handled as a special case so that address mbufs
   2316  * don't need to be saved to store "struct in_addr", which is only 4 bytes.
   2317  */
   2318 int
   2319 netaddr_match(family, haddr, nam)
   2320 	int family;
   2321 	union nethostaddr *haddr;
   2322 	struct mbuf *nam;
   2323 {
   2324 	struct sockaddr_in *inetaddr;
   2325 
   2326 	switch (family) {
   2327 	case AF_INET:
   2328 		inetaddr = mtod(nam, struct sockaddr_in *);
   2329 		if (inetaddr->sin_family == AF_INET &&
   2330 		    inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
   2331 			return (1);
   2332 		break;
   2333 #ifdef INET6
   2334 	case AF_INET6:
   2335 	    {
   2336 		struct sockaddr_in6 *sin6_1, *sin6_2;
   2337 
   2338 		sin6_1 = mtod(nam, struct sockaddr_in6 *);
   2339 		sin6_2 = mtod(haddr->had_nam, struct sockaddr_in6 *);
   2340 		if (sin6_1->sin6_family == AF_INET6 &&
   2341 		    IN6_ARE_ADDR_EQUAL(&sin6_1->sin6_addr, &sin6_2->sin6_addr))
   2342 			return 1;
   2343 	    }
   2344 #endif
   2345 #ifdef ISO
   2346 	case AF_ISO:
   2347 	    {
   2348 		struct sockaddr_iso *isoaddr1, *isoaddr2;
   2349 
   2350 		isoaddr1 = mtod(nam, struct sockaddr_iso *);
   2351 		isoaddr2 = mtod(haddr->had_nam, struct sockaddr_iso *);
   2352 		if (isoaddr1->siso_family == AF_ISO &&
   2353 		    isoaddr1->siso_nlen > 0 &&
   2354 		    isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
   2355 		    SAME_ISOADDR(isoaddr1, isoaddr2))
   2356 			return (1);
   2357 		break;
   2358 	    }
   2359 #endif	/* ISO */
   2360 	default:
   2361 		break;
   2362 	};
   2363 	return (0);
   2364 }
   2365 
   2366 
   2367 /*
   2368  * The write verifier has changed (probably due to a server reboot), so all
   2369  * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
   2370  * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
   2371  * flag. Once done the new write verifier can be set for the mount point.
   2372  */
   2373 void
   2374 nfs_clearcommit(mp)
   2375 	struct mount *mp;
   2376 {
   2377 	struct vnode *vp, *nvp;
   2378 	struct buf *bp, *nbp;
   2379 	struct nfsnode *np;
   2380 	int s;
   2381 
   2382 	s = splbio();
   2383 loop:
   2384 	for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
   2385 		if (vp->v_mount != mp)	/* Paranoia */
   2386 			goto loop;
   2387 		np = VTONFS(vp);
   2388 		np->n_pushlo = np->n_pushhi = np->n_pushedlo =
   2389 		    np->n_pushedhi = 0;
   2390 		np->n_commitflags &=
   2391 		    ~(NFS_COMMIT_PUSH_VALID | NFS_COMMIT_PUSHED_VALID);
   2392 		nvp = vp->v_mntvnodes.le_next;
   2393 		for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
   2394 			nbp = bp->b_vnbufs.le_next;
   2395 			if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
   2396 				== (B_DELWRI | B_NEEDCOMMIT))
   2397 				bp->b_flags &= ~B_NEEDCOMMIT;
   2398 		}
   2399 	}
   2400 	splx(s);
   2401 }
   2402 
   2403 void
   2404 nfs_merge_commit_ranges(vp)
   2405 	struct vnode *vp;
   2406 {
   2407 	struct nfsnode *np = VTONFS(vp);
   2408 
   2409 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
   2410 		np->n_pushedlo = np->n_pushlo;
   2411 		np->n_pushedhi = np->n_pushhi;
   2412 		np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
   2413 	} else {
   2414 		if (np->n_pushlo < np->n_pushedlo)
   2415 			np->n_pushedlo = np->n_pushlo;
   2416 		if (np->n_pushhi > np->n_pushedhi)
   2417 			np->n_pushedhi = np->n_pushhi;
   2418 	}
   2419 
   2420 	np->n_pushlo = np->n_pushhi = 0;
   2421 	np->n_commitflags &= ~NFS_COMMIT_PUSH_VALID;
   2422 
   2423 #ifdef fvdl_debug
   2424 	printf("merge: committed: %u - %u\n", (unsigned)np->n_pushedlo,
   2425 	    (unsigned)np->n_pushedhi);
   2426 #endif
   2427 }
   2428 
   2429 int
   2430 nfs_in_committed_range(vp, bp)
   2431 	struct vnode *vp;
   2432 	struct buf *bp;
   2433 {
   2434 	struct nfsnode *np = VTONFS(vp);
   2435 	off_t lo, hi;
   2436 
   2437 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
   2438 		return 0;
   2439 	lo = (off_t)bp->b_blkno * DEV_BSIZE;
   2440 	hi = lo + bp->b_dirtyend;
   2441 
   2442 	return (lo >= np->n_pushedlo && hi <= np->n_pushedhi);
   2443 }
   2444 
   2445 int
   2446 nfs_in_tobecommitted_range(vp, bp)
   2447 	struct vnode *vp;
   2448 	struct buf *bp;
   2449 {
   2450 	struct nfsnode *np = VTONFS(vp);
   2451 	off_t lo, hi;
   2452 
   2453 	if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
   2454 		return 0;
   2455 	lo = (off_t)bp->b_blkno * DEV_BSIZE;
   2456 	hi = lo + bp->b_dirtyend;
   2457 
   2458 	return (lo >= np->n_pushlo && hi <= np->n_pushhi);
   2459 }
   2460 
   2461 void
   2462 nfs_add_committed_range(vp, bp)
   2463 	struct vnode *vp;
   2464 	struct buf *bp;
   2465 {
   2466 	struct nfsnode *np = VTONFS(vp);
   2467 	off_t lo, hi;
   2468 
   2469 	lo = (off_t)bp->b_blkno * DEV_BSIZE;
   2470 	hi = lo + bp->b_dirtyend;
   2471 
   2472 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID)) {
   2473 		np->n_pushedlo = lo;
   2474 		np->n_pushedhi = hi;
   2475 		np->n_commitflags |= NFS_COMMIT_PUSHED_VALID;
   2476 	} else {
   2477 		if (hi > np->n_pushedhi)
   2478 			np->n_pushedhi = hi;
   2479 		if (lo < np->n_pushedlo)
   2480 			np->n_pushedlo = lo;
   2481 	}
   2482 #ifdef fvdl_debug
   2483 	printf("add: committed: %u - %u\n", (unsigned)np->n_pushedlo,
   2484 	    (unsigned)np->n_pushedhi);
   2485 #endif
   2486 }
   2487 
   2488 void
   2489 nfs_del_committed_range(vp, bp)
   2490 	struct vnode *vp;
   2491 	struct buf *bp;
   2492 {
   2493 	struct nfsnode *np = VTONFS(vp);
   2494 	off_t lo, hi;
   2495 
   2496 	if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
   2497 		return;
   2498 
   2499 	lo = (off_t)bp->b_blkno * DEV_BSIZE;
   2500 	hi = lo + bp->b_dirtyend;
   2501 
   2502 	if (lo > np->n_pushedhi || hi < np->n_pushedlo)
   2503 		return;
   2504 	if (lo <= np->n_pushedlo)
   2505 		np->n_pushedlo = hi;
   2506 	else if (hi >= np->n_pushedhi)
   2507 		np->n_pushedhi = lo;
   2508 	else {
   2509 		/*
   2510 		 * XXX There's only one range. If the deleted range
   2511 		 * is in the middle, pick the largest of the
   2512 		 * contiguous ranges that it leaves.
   2513 		 */
   2514 		if ((np->n_pushedlo - lo) > (hi - np->n_pushedhi))
   2515 			np->n_pushedhi = lo;
   2516 		else
   2517 			np->n_pushedlo = hi;
   2518 	}
   2519 #ifdef fvdl_debug
   2520 	printf("del: committed: %u - %u\n", (unsigned)np->n_pushedlo,
   2521 	    (unsigned)np->n_pushedhi);
   2522 #endif
   2523 }
   2524 
   2525 void
   2526 nfs_add_tobecommitted_range(vp, bp)
   2527 	struct vnode *vp;
   2528 	struct buf *bp;
   2529 {
   2530 	struct nfsnode *np = VTONFS(vp);
   2531 	off_t lo, hi;
   2532 
   2533 	lo = (off_t)bp->b_blkno * DEV_BSIZE;
   2534 	hi = lo + bp->b_dirtyend;
   2535 
   2536 	if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID)) {
   2537 		np->n_pushlo = lo;
   2538 		np->n_pushhi = hi;
   2539 		np->n_commitflags |= NFS_COMMIT_PUSH_VALID;
   2540 	} else {
   2541 		if (lo < np->n_pushlo)
   2542 			np->n_pushlo = lo;
   2543 		if (hi > np->n_pushhi)
   2544 			np->n_pushhi = hi;
   2545 	}
   2546 #ifdef fvdl_debug
   2547 	printf("add: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
   2548 	    (unsigned)np->n_pushhi);
   2549 #endif
   2550 }
   2551 
   2552 void
   2553 nfs_del_tobecommitted_range(vp, bp)
   2554 	struct vnode *vp;
   2555 	struct buf *bp;
   2556 {
   2557 	struct nfsnode *np = VTONFS(vp);
   2558 	off_t lo, hi;
   2559 
   2560 	if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
   2561 		return;
   2562 
   2563 	lo = (off_t)bp->b_blkno * DEV_BSIZE;
   2564 	hi = lo + bp->b_dirtyend;
   2565 
   2566 	if (lo > np->n_pushhi || hi < np->n_pushlo)
   2567 		return;
   2568 
   2569 	if (lo <= np->n_pushlo)
   2570 		np->n_pushlo = hi;
   2571 	else if (hi >= np->n_pushhi)
   2572 		np->n_pushhi = lo;
   2573 	else {
   2574 		/*
   2575 		 * XXX There's only one range. If the deleted range
   2576 		 * is in the middle, pick the largest of the
   2577 		 * contiguous ranges that it leaves.
   2578 		 */
   2579 		if ((np->n_pushlo - lo) > (hi - np->n_pushhi))
   2580 			np->n_pushhi = lo;
   2581 		else
   2582 			np->n_pushlo = hi;
   2583 	}
   2584 #ifdef fvdl_debug
   2585 	printf("del: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
   2586 	    (unsigned)np->n_pushhi);
   2587 #endif
   2588 }
   2589 
   2590 /*
   2591  * Map errnos to NFS error numbers. For Version 3 also filter out error
   2592  * numbers not specified for the associated procedure.
   2593  */
   2594 int
   2595 nfsrv_errmap(nd, err)
   2596 	struct nfsrv_descript *nd;
   2597 	int err;
   2598 {
   2599 	short *defaulterrp, *errp;
   2600 
   2601 	if (nd->nd_flag & ND_NFSV3) {
   2602 	    if (nd->nd_procnum <= NFSPROC_COMMIT) {
   2603 		errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
   2604 		while (*++errp) {
   2605 			if (*errp == err)
   2606 				return (err);
   2607 			else if (*errp > err)
   2608 				break;
   2609 		}
   2610 		return ((int)*defaulterrp);
   2611 	    } else
   2612 		return (err & 0xffff);
   2613 	}
   2614 	if (err <= ELAST)
   2615 		return ((int)nfsrv_v2errmap[err - 1]);
   2616 	return (NFSERR_IO);
   2617 }
   2618 
   2619 /*
   2620  * Sort the group list in increasing numerical order.
   2621  * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
   2622  *  that used to be here.)
   2623  */
   2624 void
   2625 nfsrvw_sort(list, num)
   2626         gid_t *list;
   2627         int num;
   2628 {
   2629 	int i, j;
   2630 	gid_t v;
   2631 
   2632 	/* Insertion sort. */
   2633 	for (i = 1; i < num; i++) {
   2634 		v = list[i];
   2635 		/* find correct slot for value v, moving others up */
   2636 		for (j = i; --j >= 0 && v < list[j];)
   2637 			list[j + 1] = list[j];
   2638 		list[j + 1] = v;
   2639 	}
   2640 }
   2641 
   2642 /*
   2643  * copy credentials making sure that the result can be compared with memcmp().
   2644  */
   2645 void
   2646 nfsrv_setcred(incred, outcred)
   2647 	struct ucred *incred, *outcred;
   2648 {
   2649 	int i;
   2650 
   2651 	memset((caddr_t)outcred, 0, sizeof (struct ucred));
   2652 	outcred->cr_ref = 1;
   2653 	outcred->cr_uid = incred->cr_uid;
   2654 	outcred->cr_gid = incred->cr_gid;
   2655 	outcred->cr_ngroups = incred->cr_ngroups;
   2656 	for (i = 0; i < incred->cr_ngroups; i++)
   2657 		outcred->cr_groups[i] = incred->cr_groups[i];
   2658 	nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
   2659 }
   2660