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