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