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