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