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