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