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