Home | History | Annotate | Line # | Download | only in nfs
nfs_vnops.c revision 1.253
      1  1.253      yamt /*	$NetBSD: nfs_vnops.c,v 1.253 2007/04/29 08:41:10 yamt Exp $	*/
      2   1.34       cgd 
      3    1.1       cgd /*
      4   1.32   mycroft  * Copyright (c) 1989, 1993
      5   1.32   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.177       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.89      fvdl  *	@(#)nfs_vnops.c	8.19 (Berkeley) 7/31/95
     35    1.1       cgd  */
     36    1.1       cgd 
     37    1.1       cgd /*
     38   1.59      fvdl  * vnode op calls for Sun NFS version 2 and 3
     39    1.1       cgd  */
     40  1.143     lukem 
     41  1.143     lukem #include <sys/cdefs.h>
     42  1.253      yamt __KERNEL_RCSID(0, "$NetBSD: nfs_vnops.c,v 1.253 2007/04/29 08:41:10 yamt Exp $");
     43    1.1       cgd 
     44  1.205      yamt #include "opt_inet.h"
     45  1.119     bjh21 #include "opt_nfs.h"
     46  1.124       chs #include "opt_uvmhist.h"
     47  1.119     bjh21 
     48   1.16   mycroft #include <sys/param.h>
     49   1.16   mycroft #include <sys/proc.h>
     50   1.16   mycroft #include <sys/kernel.h>
     51   1.16   mycroft #include <sys/systm.h>
     52   1.59      fvdl #include <sys/resourcevar.h>
     53   1.16   mycroft #include <sys/mount.h>
     54   1.16   mycroft #include <sys/buf.h>
     55  1.253      yamt #include <sys/condvar.h>
     56  1.232     blymn #include <sys/disk.h>
     57   1.16   mycroft #include <sys/malloc.h>
     58   1.16   mycroft #include <sys/mbuf.h>
     59  1.253      yamt #include <sys/mutex.h>
     60   1.16   mycroft #include <sys/namei.h>
     61   1.16   mycroft #include <sys/vnode.h>
     62   1.32   mycroft #include <sys/dirent.h>
     63   1.59      fvdl #include <sys/fcntl.h>
     64  1.147     lukem #include <sys/hash.h>
     65   1.32   mycroft #include <sys/lockf.h>
     66   1.75   mycroft #include <sys/stat.h>
     67   1.95    kleink #include <sys/unistd.h>
     68  1.236      elad #include <sys/kauth.h>
     69   1.31       cgd 
     70   1.87       mrg #include <uvm/uvm_extern.h>
     71  1.124       chs #include <uvm/uvm.h>
     72   1.87       mrg 
     73   1.64   mycroft #include <miscfs/fifofs/fifo.h>
     74   1.64   mycroft #include <miscfs/genfs/genfs.h>
     75  1.244      yamt #include <miscfs/genfs/genfs_node.h>
     76   1.32   mycroft #include <miscfs/specfs/specdev.h>
     77   1.32   mycroft 
     78   1.32   mycroft #include <nfs/rpcv2.h>
     79   1.59      fvdl #include <nfs/nfsproto.h>
     80   1.16   mycroft #include <nfs/nfs.h>
     81   1.16   mycroft #include <nfs/nfsnode.h>
     82   1.16   mycroft #include <nfs/nfsmount.h>
     83   1.16   mycroft #include <nfs/xdr_subs.h>
     84   1.16   mycroft #include <nfs/nfsm_subs.h>
     85   1.58  christos #include <nfs/nfs_var.h>
     86   1.27        pk 
     87   1.59      fvdl #include <net/if.h>
     88   1.59      fvdl #include <netinet/in.h>
     89   1.59      fvdl #include <netinet/in_var.h>
     90    1.1       cgd 
     91    1.1       cgd /*
     92    1.1       cgd  * Global vfs data structures for nfs
     93    1.1       cgd  */
     94   1.58  christos int (**nfsv2_vnodeop_p) __P((void *));
     95  1.127  jdolecek const struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
     96   1.32   mycroft 	{ &vop_default_desc, vn_default_error },
     97   1.64   mycroft 	{ &vop_lookup_desc, nfs_lookup },		/* lookup */
     98   1.64   mycroft 	{ &vop_create_desc, nfs_create },		/* create */
     99   1.64   mycroft 	{ &vop_mknod_desc, nfs_mknod },			/* mknod */
    100   1.64   mycroft 	{ &vop_open_desc, nfs_open },			/* open */
    101   1.64   mycroft 	{ &vop_close_desc, nfs_close },			/* close */
    102   1.64   mycroft 	{ &vop_access_desc, nfs_access },		/* access */
    103   1.64   mycroft 	{ &vop_getattr_desc, nfs_getattr },		/* getattr */
    104   1.64   mycroft 	{ &vop_setattr_desc, nfs_setattr },		/* setattr */
    105   1.64   mycroft 	{ &vop_read_desc, nfs_read },			/* read */
    106   1.64   mycroft 	{ &vop_write_desc, nfs_write },			/* write */
    107   1.64   mycroft 	{ &vop_lease_desc, nfs_lease_check },		/* lease */
    108  1.105  wrstuden 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
    109   1.64   mycroft 	{ &vop_ioctl_desc, nfs_ioctl },			/* ioctl */
    110   1.65   mycroft 	{ &vop_poll_desc, nfs_poll },			/* poll */
    111  1.156  jdolecek 	{ &vop_kqfilter_desc, nfs_kqfilter },		/* kqfilter */
    112   1.64   mycroft 	{ &vop_revoke_desc, nfs_revoke },		/* revoke */
    113   1.64   mycroft 	{ &vop_mmap_desc, nfs_mmap },			/* mmap */
    114   1.64   mycroft 	{ &vop_fsync_desc, nfs_fsync },			/* fsync */
    115   1.64   mycroft 	{ &vop_seek_desc, nfs_seek },			/* seek */
    116   1.64   mycroft 	{ &vop_remove_desc, nfs_remove },		/* remove */
    117   1.64   mycroft 	{ &vop_link_desc, nfs_link },			/* link */
    118   1.64   mycroft 	{ &vop_rename_desc, nfs_rename },		/* rename */
    119   1.64   mycroft 	{ &vop_mkdir_desc, nfs_mkdir },			/* mkdir */
    120   1.64   mycroft 	{ &vop_rmdir_desc, nfs_rmdir },			/* rmdir */
    121   1.64   mycroft 	{ &vop_symlink_desc, nfs_symlink },		/* symlink */
    122   1.64   mycroft 	{ &vop_readdir_desc, nfs_readdir },		/* readdir */
    123   1.64   mycroft 	{ &vop_readlink_desc, nfs_readlink },		/* readlink */
    124   1.64   mycroft 	{ &vop_abortop_desc, nfs_abortop },		/* abortop */
    125   1.64   mycroft 	{ &vop_inactive_desc, nfs_inactive },		/* inactive */
    126   1.64   mycroft 	{ &vop_reclaim_desc, nfs_reclaim },		/* reclaim */
    127   1.64   mycroft 	{ &vop_lock_desc, nfs_lock },			/* lock */
    128   1.64   mycroft 	{ &vop_unlock_desc, nfs_unlock },		/* unlock */
    129   1.64   mycroft 	{ &vop_bmap_desc, nfs_bmap },			/* bmap */
    130   1.64   mycroft 	{ &vop_strategy_desc, nfs_strategy },		/* strategy */
    131   1.64   mycroft 	{ &vop_print_desc, nfs_print },			/* print */
    132   1.64   mycroft 	{ &vop_islocked_desc, nfs_islocked },		/* islocked */
    133   1.64   mycroft 	{ &vop_pathconf_desc, nfs_pathconf },		/* pathconf */
    134   1.64   mycroft 	{ &vop_advlock_desc, nfs_advlock },		/* advlock */
    135  1.213      yamt 	{ &vop_bwrite_desc, genfs_badop },		/* bwrite */
    136  1.124       chs 	{ &vop_getpages_desc, nfs_getpages },		/* getpages */
    137  1.138       chs 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
    138  1.124       chs 	{ NULL, NULL }
    139    1.1       cgd };
    140  1.127  jdolecek const struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
    141   1.32   mycroft 	{ &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
    142    1.1       cgd 
    143    1.1       cgd /*
    144    1.1       cgd  * Special device vnode ops
    145    1.1       cgd  */
    146   1.58  christos int (**spec_nfsv2nodeop_p) __P((void *));
    147  1.127  jdolecek const struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = {
    148   1.32   mycroft 	{ &vop_default_desc, vn_default_error },
    149   1.64   mycroft 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
    150   1.64   mycroft 	{ &vop_create_desc, spec_create },		/* create */
    151   1.64   mycroft 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
    152   1.64   mycroft 	{ &vop_open_desc, spec_open },			/* open */
    153   1.64   mycroft 	{ &vop_close_desc, nfsspec_close },		/* close */
    154   1.64   mycroft 	{ &vop_access_desc, nfsspec_access },		/* access */
    155   1.64   mycroft 	{ &vop_getattr_desc, nfs_getattr },		/* getattr */
    156   1.64   mycroft 	{ &vop_setattr_desc, nfs_setattr },		/* setattr */
    157   1.64   mycroft 	{ &vop_read_desc, nfsspec_read },		/* read */
    158   1.64   mycroft 	{ &vop_write_desc, nfsspec_write },		/* write */
    159   1.64   mycroft 	{ &vop_lease_desc, spec_lease_check },		/* lease */
    160  1.105  wrstuden 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
    161   1.64   mycroft 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
    162   1.65   mycroft 	{ &vop_poll_desc, spec_poll },			/* poll */
    163  1.156  jdolecek 	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */
    164   1.64   mycroft 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
    165   1.64   mycroft 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
    166  1.124       chs 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
    167   1.64   mycroft 	{ &vop_seek_desc, spec_seek },			/* seek */
    168   1.64   mycroft 	{ &vop_remove_desc, spec_remove },		/* remove */
    169   1.64   mycroft 	{ &vop_link_desc, spec_link },			/* link */
    170   1.64   mycroft 	{ &vop_rename_desc, spec_rename },		/* rename */
    171   1.64   mycroft 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
    172   1.64   mycroft 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
    173   1.64   mycroft 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
    174   1.64   mycroft 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
    175   1.64   mycroft 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
    176   1.64   mycroft 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
    177   1.64   mycroft 	{ &vop_inactive_desc, nfs_inactive },		/* inactive */
    178   1.64   mycroft 	{ &vop_reclaim_desc, nfs_reclaim },		/* reclaim */
    179   1.64   mycroft 	{ &vop_lock_desc, nfs_lock },			/* lock */
    180   1.64   mycroft 	{ &vop_unlock_desc, nfs_unlock },		/* unlock */
    181   1.64   mycroft 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    182   1.64   mycroft 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    183   1.64   mycroft 	{ &vop_print_desc, nfs_print },			/* print */
    184   1.64   mycroft 	{ &vop_islocked_desc, nfs_islocked },		/* islocked */
    185   1.64   mycroft 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    186   1.64   mycroft 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    187  1.214      yamt 	{ &vop_bwrite_desc, spec_bwrite },		/* bwrite */
    188  1.137       chs 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
    189  1.137       chs 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
    190  1.124       chs 	{ NULL, NULL }
    191    1.1       cgd };
    192  1.127  jdolecek const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
    193   1.32   mycroft 	{ &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries };
    194    1.1       cgd 
    195   1.58  christos int (**fifo_nfsv2nodeop_p) __P((void *));
    196  1.127  jdolecek const struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = {
    197   1.32   mycroft 	{ &vop_default_desc, vn_default_error },
    198   1.64   mycroft 	{ &vop_lookup_desc, fifo_lookup },		/* lookup */
    199   1.64   mycroft 	{ &vop_create_desc, fifo_create },		/* create */
    200   1.64   mycroft 	{ &vop_mknod_desc, fifo_mknod },		/* mknod */
    201   1.64   mycroft 	{ &vop_open_desc, fifo_open },			/* open */
    202   1.64   mycroft 	{ &vop_close_desc, nfsfifo_close },		/* close */
    203   1.64   mycroft 	{ &vop_access_desc, nfsspec_access },		/* access */
    204   1.64   mycroft 	{ &vop_getattr_desc, nfs_getattr },		/* getattr */
    205   1.64   mycroft 	{ &vop_setattr_desc, nfs_setattr },		/* setattr */
    206   1.64   mycroft 	{ &vop_read_desc, nfsfifo_read },		/* read */
    207   1.64   mycroft 	{ &vop_write_desc, nfsfifo_write },		/* write */
    208   1.64   mycroft 	{ &vop_lease_desc, fifo_lease_check },		/* lease */
    209  1.105  wrstuden 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
    210   1.64   mycroft 	{ &vop_ioctl_desc, fifo_ioctl },		/* ioctl */
    211   1.65   mycroft 	{ &vop_poll_desc, fifo_poll },			/* poll */
    212  1.156  jdolecek 	{ &vop_kqfilter_desc, fifo_kqfilter },		/* kqfilter */
    213   1.64   mycroft 	{ &vop_revoke_desc, fifo_revoke },		/* revoke */
    214   1.64   mycroft 	{ &vop_mmap_desc, fifo_mmap },			/* mmap */
    215   1.64   mycroft 	{ &vop_fsync_desc, nfs_fsync },			/* fsync */
    216   1.64   mycroft 	{ &vop_seek_desc, fifo_seek },			/* seek */
    217   1.64   mycroft 	{ &vop_remove_desc, fifo_remove },		/* remove */
    218   1.64   mycroft 	{ &vop_link_desc, fifo_link },			/* link */
    219   1.64   mycroft 	{ &vop_rename_desc, fifo_rename },		/* rename */
    220   1.64   mycroft 	{ &vop_mkdir_desc, fifo_mkdir },		/* mkdir */
    221   1.64   mycroft 	{ &vop_rmdir_desc, fifo_rmdir },		/* rmdir */
    222   1.64   mycroft 	{ &vop_symlink_desc, fifo_symlink },		/* symlink */
    223   1.64   mycroft 	{ &vop_readdir_desc, fifo_readdir },		/* readdir */
    224   1.64   mycroft 	{ &vop_readlink_desc, fifo_readlink },		/* readlink */
    225   1.64   mycroft 	{ &vop_abortop_desc, fifo_abortop },		/* abortop */
    226   1.64   mycroft 	{ &vop_inactive_desc, nfs_inactive },		/* inactive */
    227   1.64   mycroft 	{ &vop_reclaim_desc, nfs_reclaim },		/* reclaim */
    228   1.64   mycroft 	{ &vop_lock_desc, nfs_lock },			/* lock */
    229   1.64   mycroft 	{ &vop_unlock_desc, nfs_unlock },		/* unlock */
    230   1.64   mycroft 	{ &vop_bmap_desc, fifo_bmap },			/* bmap */
    231   1.64   mycroft 	{ &vop_strategy_desc, genfs_badop },		/* strategy */
    232   1.64   mycroft 	{ &vop_print_desc, nfs_print },			/* print */
    233   1.64   mycroft 	{ &vop_islocked_desc, nfs_islocked },		/* islocked */
    234   1.64   mycroft 	{ &vop_pathconf_desc, fifo_pathconf },		/* pathconf */
    235   1.64   mycroft 	{ &vop_advlock_desc, fifo_advlock },		/* advlock */
    236  1.213      yamt 	{ &vop_bwrite_desc, genfs_badop },		/* bwrite */
    237  1.140  sommerfe 	{ &vop_putpages_desc, fifo_putpages }, 		/* putpages */
    238  1.124       chs 	{ NULL, NULL }
    239    1.1       cgd };
    240  1.127  jdolecek const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
    241   1.32   mycroft 	{ &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries };
    242    1.1       cgd 
    243  1.239      yamt static int nfs_linkrpc(struct vnode *, struct vnode *, const char *,
    244  1.239      yamt     size_t, kauth_cred_t, struct lwp *);
    245  1.252  christos static void nfs_writerpc_extfree(struct mbuf *, void *, size_t, void *);
    246  1.167      yamt 
    247    1.1       cgd /*
    248   1.32   mycroft  * Global variables
    249    1.1       cgd  */
    250   1.59      fvdl extern u_int32_t nfs_true, nfs_false;
    251   1.55   mycroft extern u_int32_t nfs_xdrneg1;
    252  1.157      matt extern const nfstype nfsv3_type[9];
    253  1.150      matt 
    254    1.1       cgd int nfs_numasync = 0;
    255  1.225  christos #define	DIRHDSIZ	_DIRENT_NAMEOFF(dp)
    256  1.225  christos #define UIO_ADVANCE(uio, siz) \
    257  1.225  christos     (void)((uio)->uio_resid -= (siz), \
    258  1.225  christos     (uio)->uio_iov->iov_base = (char *)(uio)->uio_iov->iov_base + (siz), \
    259  1.225  christos     (uio)->uio_iov->iov_len -= (siz))
    260    1.1       cgd 
    261  1.188      yamt static void nfs_cache_enter(struct vnode *, struct vnode *,
    262  1.188      yamt     struct componentname *);
    263  1.188      yamt 
    264  1.188      yamt static void
    265  1.188      yamt nfs_cache_enter(struct vnode *dvp, struct vnode *vp,
    266  1.188      yamt     struct componentname *cnp)
    267  1.188      yamt {
    268  1.215      yamt 	struct nfsnode *dnp = VTONFS(dvp);
    269  1.188      yamt 
    270  1.188      yamt 	if (vp != NULL) {
    271  1.188      yamt 		struct nfsnode *np = VTONFS(vp);
    272  1.188      yamt 
    273  1.188      yamt 		np->n_ctime = np->n_vattr->va_ctime.tv_sec;
    274  1.215      yamt 	}
    275  1.188      yamt 
    276  1.215      yamt 	if (!timespecisset(&dnp->n_nctime))
    277  1.215      yamt 		dnp->n_nctime = dnp->n_vattr->va_mtime;
    278  1.188      yamt 
    279  1.188      yamt 	cache_enter(dvp, vp, cnp);
    280  1.188      yamt }
    281  1.188      yamt 
    282    1.1       cgd /*
    283    1.1       cgd  * nfs null call from vfs.
    284    1.1       cgd  */
    285   1.32   mycroft int
    286  1.230  christos nfs_null(vp, cred, l)
    287    1.1       cgd 	struct vnode *vp;
    288  1.236      elad 	kauth_cred_t cred;
    289  1.230  christos 	struct lwp *l;
    290    1.1       cgd {
    291  1.252  christos 	char *bpos, *dpos;
    292    1.1       cgd 	int error = 0;
    293    1.1       cgd 	struct mbuf *mreq, *mrep, *md, *mb;
    294  1.165  drochner 	struct nfsnode *np = VTONFS(vp);
    295  1.220     perry 
    296  1.165  drochner 	nfsm_reqhead(np, NFSPROC_NULL, 0);
    297  1.230  christos 	nfsm_request(np, NFSPROC_NULL, l, cred);
    298    1.1       cgd 	nfsm_reqdone;
    299    1.1       cgd 	return (error);
    300    1.1       cgd }
    301    1.1       cgd 
    302    1.1       cgd /*
    303    1.1       cgd  * nfs access vnode op.
    304   1.59      fvdl  * For nfs version 2, just return ok. File accesses may fail later.
    305   1.59      fvdl  * For nfs version 3, use the access rpc to check accessibility. If file modes
    306   1.59      fvdl  * are changed on the server, accesses might still fail later.
    307    1.1       cgd  */
    308   1.32   mycroft int
    309   1.58  christos nfs_access(v)
    310   1.58  christos 	void *v;
    311   1.58  christos {
    312   1.32   mycroft 	struct vop_access_args /* {
    313   1.32   mycroft 		struct vnode *a_vp;
    314   1.32   mycroft 		int  a_mode;
    315  1.236      elad 		kauth_cred_t a_cred;
    316  1.230  christos 		struct lwp *a_l;
    317   1.58  christos 	} */ *ap = v;
    318  1.110  augustss 	struct vnode *vp = ap->a_vp;
    319  1.200  christos #ifndef NFS_V2_ONLY
    320  1.110  augustss 	u_int32_t *tl;
    321  1.252  christos 	char *cp;
    322  1.110  augustss 	int32_t t1, t2;
    323  1.252  christos 	char *bpos, *dpos, *cp2;
    324  1.200  christos 	int error = 0, attrflag;
    325  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
    326   1.59      fvdl 	u_int32_t mode, rmode;
    327  1.119     bjh21 	const int v3 = NFS_ISV3(vp);
    328  1.200  christos #endif
    329  1.200  christos 	int cachevalid;
    330  1.108      fvdl 	struct nfsnode *np = VTONFS(vp);
    331  1.227  christos 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    332  1.108      fvdl 
    333  1.108      fvdl 	cachevalid = (np->n_accstamp != -1 &&
    334  1.237    kardel 	    (time_uptime - np->n_accstamp) < NFS_ATTRTIMEO(nmp, np) &&
    335  1.236      elad 	    np->n_accuid == kauth_cred_geteuid(ap->a_cred));
    336  1.108      fvdl 
    337  1.108      fvdl 	/*
    338  1.108      fvdl 	 * Check access cache first. If this request has been made for this
    339  1.108      fvdl 	 * uid shortly before, use the cached result.
    340  1.108      fvdl 	 */
    341  1.118      fvdl 	if (cachevalid) {
    342  1.118      fvdl 		if (!np->n_accerror) {
    343  1.118      fvdl 			if  ((np->n_accmode & ap->a_mode) == ap->a_mode)
    344  1.118      fvdl 				return np->n_accerror;
    345  1.118      fvdl 		} else if ((np->n_accmode & ap->a_mode) == np->n_accmode)
    346  1.118      fvdl 			return np->n_accerror;
    347  1.118      fvdl 	}
    348    1.1       cgd 
    349  1.200  christos #ifndef NFS_V2_ONLY
    350    1.1       cgd 	/*
    351   1.59      fvdl 	 * For nfs v3, do an access rpc, otherwise you are stuck emulating
    352   1.32   mycroft 	 * ufs_access() locally using the vattr. This may not be correct,
    353   1.32   mycroft 	 * since the server may apply other access criteria such as
    354   1.32   mycroft 	 * client uid-->server uid mapping that we do not know about, but
    355   1.32   mycroft 	 * this is better than just returning anything that is lying about
    356   1.32   mycroft 	 * in the cache.
    357   1.32   mycroft 	 */
    358   1.59      fvdl 	if (v3) {
    359   1.59      fvdl 		nfsstats.rpccnt[NFSPROC_ACCESS]++;
    360  1.165  drochner 		nfsm_reqhead(np, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
    361  1.165  drochner 		nfsm_fhtom(np, v3);
    362   1.59      fvdl 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    363   1.32   mycroft 		if (ap->a_mode & VREAD)
    364   1.59      fvdl 			mode = NFSV3ACCESS_READ;
    365   1.32   mycroft 		else
    366   1.59      fvdl 			mode = 0;
    367   1.74   mycroft 		if (vp->v_type != VDIR) {
    368   1.74   mycroft 			if (ap->a_mode & VWRITE)
    369   1.74   mycroft 				mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
    370   1.74   mycroft 			if (ap->a_mode & VEXEC)
    371   1.74   mycroft 				mode |= NFSV3ACCESS_EXECUTE;
    372   1.74   mycroft 		} else {
    373   1.59      fvdl 			if (ap->a_mode & VWRITE)
    374   1.59      fvdl 				mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
    375   1.59      fvdl 					 NFSV3ACCESS_DELETE);
    376   1.74   mycroft 			if (ap->a_mode & VEXEC)
    377   1.59      fvdl 				mode |= NFSV3ACCESS_LOOKUP;
    378   1.59      fvdl 		}
    379   1.59      fvdl 		*tl = txdr_unsigned(mode);
    380  1.230  christos 		nfsm_request(np, NFSPROC_ACCESS, ap->a_l, ap->a_cred);
    381  1.153      yamt 		nfsm_postop_attr(vp, attrflag, 0);
    382   1.59      fvdl 		if (!error) {
    383   1.59      fvdl 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    384   1.59      fvdl 			rmode = fxdr_unsigned(u_int32_t, *tl);
    385   1.59      fvdl 			/*
    386   1.59      fvdl 			 * The NFS V3 spec does not clarify whether or not
    387   1.59      fvdl 			 * the returned access bits can be a superset of
    388   1.59      fvdl 			 * the ones requested, so...
    389   1.59      fvdl 			 */
    390   1.59      fvdl 			if ((rmode & mode) != mode)
    391   1.59      fvdl 				error = EACCES;
    392   1.59      fvdl 		}
    393   1.32   mycroft 		nfsm_reqdone;
    394   1.32   mycroft 	} else
    395  1.200  christos #endif
    396   1.32   mycroft 		return (nfsspec_access(ap));
    397  1.200  christos #ifndef NFS_V2_ONLY
    398   1.71      fvdl 	/*
    399   1.71      fvdl 	 * Disallow write attempts on filesystems mounted read-only;
    400   1.71      fvdl 	 * unless the file is a socket, fifo, or a block or character
    401   1.71      fvdl 	 * device resident on the filesystem.
    402   1.71      fvdl 	 */
    403  1.108      fvdl 	if (!error && (ap->a_mode & VWRITE) &&
    404  1.108      fvdl 	    (vp->v_mount->mnt_flag & MNT_RDONLY)) {
    405   1.71      fvdl 		switch (vp->v_type) {
    406   1.71      fvdl 		case VREG:
    407   1.71      fvdl 		case VDIR:
    408   1.71      fvdl 		case VLNK:
    409  1.108      fvdl 			error = EROFS;
    410   1.71      fvdl 		default:
    411   1.71      fvdl 			break;
    412   1.71      fvdl 		}
    413   1.71      fvdl 	}
    414  1.108      fvdl 
    415  1.108      fvdl 	if (!error || error == EACCES) {
    416  1.108      fvdl 		/*
    417  1.108      fvdl 		 * If we got the same result as for a previous,
    418  1.108      fvdl 		 * different request, OR it in. Don't update
    419  1.108      fvdl 		 * the timestamp in that case.
    420  1.108      fvdl 		 */
    421  1.149      fvdl 		if (cachevalid && np->n_accstamp != -1 &&
    422  1.149      fvdl 		    error == np->n_accerror) {
    423  1.118      fvdl 			if (!error)
    424  1.118      fvdl 				np->n_accmode |= ap->a_mode;
    425  1.118      fvdl 			else if ((np->n_accmode & ap->a_mode) == ap->a_mode)
    426  1.118      fvdl 				np->n_accmode = ap->a_mode;
    427  1.118      fvdl 		} else {
    428  1.237    kardel 			np->n_accstamp = time_uptime;
    429  1.236      elad 			np->n_accuid = kauth_cred_geteuid(ap->a_cred);
    430  1.108      fvdl 			np->n_accmode = ap->a_mode;
    431  1.108      fvdl 			np->n_accerror = error;
    432  1.108      fvdl 		}
    433  1.108      fvdl 	}
    434  1.108      fvdl 
    435  1.108      fvdl 	return (error);
    436  1.200  christos #endif
    437    1.1       cgd }
    438    1.1       cgd 
    439    1.1       cgd /*
    440    1.1       cgd  * nfs open vnode op
    441   1.32   mycroft  * Check to see if the type is ok
    442   1.32   mycroft  * and that deletion is not in progress.
    443   1.32   mycroft  * For paged in text files, you will need to flush the page cache
    444   1.32   mycroft  * if consistency is lost.
    445    1.1       cgd  */
    446    1.1       cgd /* ARGSUSED */
    447   1.32   mycroft int
    448   1.58  christos nfs_open(v)
    449   1.58  christos 	void *v;
    450   1.58  christos {
    451   1.32   mycroft 	struct vop_open_args /* {
    452   1.32   mycroft 		struct vnode *a_vp;
    453   1.32   mycroft 		int  a_mode;
    454  1.236      elad 		kauth_cred_t a_cred;
    455  1.230  christos 		struct lwp *a_l;
    456   1.58  christos 	} */ *ap = v;
    457  1.110  augustss 	struct vnode *vp = ap->a_vp;
    458   1.32   mycroft 	struct nfsnode *np = VTONFS(vp);
    459   1.32   mycroft 	int error;
    460    1.1       cgd 
    461   1.59      fvdl 	if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
    462    1.1       cgd 		return (EACCES);
    463   1.59      fvdl 	}
    464  1.124       chs 
    465  1.126       chs 	if (ap->a_mode & FREAD) {
    466  1.184      fvdl 		if (np->n_rcred != NULL)
    467  1.236      elad 			kauth_cred_free(np->n_rcred);
    468  1.126       chs 		np->n_rcred = ap->a_cred;
    469  1.236      elad 		kauth_cred_hold(np->n_rcred);
    470  1.126       chs 	}
    471  1.125       chs 	if (ap->a_mode & FWRITE) {
    472  1.184      fvdl 		if (np->n_wcred != NULL)
    473  1.236      elad 			kauth_cred_free(np->n_wcred);
    474  1.125       chs 		np->n_wcred = ap->a_cred;
    475  1.236      elad 		kauth_cred_hold(np->n_wcred);
    476  1.125       chs 	}
    477  1.125       chs 
    478  1.247      yamt 	error = nfs_flushstalebuf(vp, ap->a_cred, ap->a_l, 0);
    479  1.247      yamt 	if (error)
    480  1.247      yamt 		return error;
    481  1.247      yamt 
    482  1.247      yamt 	NFS_INVALIDATE_ATTRCACHE(np); /* For Open/Close consistency */
    483  1.247      yamt 
    484   1.32   mycroft 	return (0);
    485    1.1       cgd }
    486    1.1       cgd 
    487    1.1       cgd /*
    488    1.1       cgd  * nfs close vnode op
    489   1.59      fvdl  * What an NFS client should do upon close after writing is a debatable issue.
    490   1.59      fvdl  * Most NFS clients push delayed writes to the server upon close, basically for
    491   1.59      fvdl  * two reasons:
    492   1.59      fvdl  * 1 - So that any write errors may be reported back to the client process
    493   1.59      fvdl  *     doing the close system call. By far the two most likely errors are
    494   1.59      fvdl  *     NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
    495   1.59      fvdl  * 2 - To put a worst case upper bound on cache inconsistency between
    496   1.59      fvdl  *     multiple clients for the file.
    497   1.59      fvdl  * There is also a consistency problem for Version 2 of the protocol w.r.t.
    498   1.59      fvdl  * not being able to tell if other clients are writing a file concurrently,
    499   1.59      fvdl  * since there is no way of knowing if the changed modify time in the reply
    500   1.59      fvdl  * is only due to the write for this client.
    501   1.59      fvdl  * (NFS Version 3 provides weak cache consistency data in the reply that
    502   1.59      fvdl  *  should be sufficient to detect and handle this case.)
    503   1.59      fvdl  *
    504   1.59      fvdl  * The current code does the following:
    505   1.59      fvdl  * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
    506   1.59      fvdl  * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
    507   1.59      fvdl  *                     or commit them (this satisfies 1 and 2 except for the
    508   1.59      fvdl  *                     case where the server crashes after this close but
    509   1.59      fvdl  *                     before the commit RPC, which is felt to be "good
    510   1.59      fvdl  *                     enough". Changing the last argument to nfs_flush() to
    511   1.59      fvdl  *                     a 1 would force a commit operation, if it is felt a
    512   1.59      fvdl  *                     commit is necessary now.
    513    1.1       cgd  */
    514    1.1       cgd /* ARGSUSED */
    515   1.32   mycroft int
    516   1.58  christos nfs_close(v)
    517   1.58  christos 	void *v;
    518   1.58  christos {
    519   1.32   mycroft 	struct vop_close_args /* {
    520   1.32   mycroft 		struct vnodeop_desc *a_desc;
    521   1.32   mycroft 		struct vnode *a_vp;
    522   1.32   mycroft 		int  a_fflag;
    523  1.236      elad 		kauth_cred_t a_cred;
    524  1.230  christos 		struct lwp *a_l;
    525   1.58  christos 	} */ *ap = v;
    526  1.110  augustss 	struct vnode *vp = ap->a_vp;
    527  1.110  augustss 	struct nfsnode *np = VTONFS(vp);
    528    1.1       cgd 	int error = 0;
    529  1.124       chs 	UVMHIST_FUNC("nfs_close"); UVMHIST_CALLED(ubchist);
    530    1.1       cgd 
    531   1.32   mycroft 	if (vp->v_type == VREG) {
    532  1.248      yamt 	    if (np->n_flag & NMODIFIED) {
    533  1.200  christos #ifndef NFS_V2_ONLY
    534   1.59      fvdl 		if (NFS_ISV3(vp)) {
    535  1.230  christos 		    error = nfs_flush(vp, ap->a_cred, MNT_WAIT, ap->a_l, 0);
    536   1.59      fvdl 		    np->n_flag &= ~NMODIFIED;
    537   1.59      fvdl 		} else
    538  1.200  christos #endif
    539  1.230  christos 		    error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_l, 1);
    540  1.185      yamt 		NFS_INVALIDATE_ATTRCACHE(np);
    541   1.32   mycroft 	    }
    542   1.32   mycroft 	    if (np->n_flag & NWRITEERR) {
    543   1.32   mycroft 		np->n_flag &= ~NWRITEERR;
    544   1.32   mycroft 		error = np->n_error;
    545   1.32   mycroft 	    }
    546    1.1       cgd 	}
    547  1.124       chs 	UVMHIST_LOG(ubchist, "returning %d", error,0,0,0);
    548    1.1       cgd 	return (error);
    549    1.1       cgd }
    550    1.1       cgd 
    551    1.1       cgd /*
    552    1.1       cgd  * nfs getattr call from vfs.
    553    1.1       cgd  */
    554   1.32   mycroft int
    555   1.58  christos nfs_getattr(v)
    556   1.58  christos 	void *v;
    557   1.58  christos {
    558   1.32   mycroft 	struct vop_getattr_args /* {
    559   1.32   mycroft 		struct vnode *a_vp;
    560   1.32   mycroft 		struct vattr *a_vap;
    561  1.236      elad 		kauth_cred_t a_cred;
    562  1.230  christos 		struct lwp *a_l;
    563   1.58  christos 	} */ *ap = v;
    564  1.110  augustss 	struct vnode *vp = ap->a_vp;
    565  1.110  augustss 	struct nfsnode *np = VTONFS(vp);
    566  1.252  christos 	char *cp;
    567  1.110  augustss 	u_int32_t *tl;
    568  1.110  augustss 	int32_t t1, t2;
    569  1.252  christos 	char *bpos, *dpos;
    570    1.1       cgd 	int error = 0;
    571  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
    572  1.119     bjh21 	const int v3 = NFS_ISV3(vp);
    573  1.220     perry 
    574   1.32   mycroft 	/*
    575   1.32   mycroft 	 * Update local times for special files.
    576   1.32   mycroft 	 */
    577   1.32   mycroft 	if (np->n_flag & (NACC | NUPD))
    578   1.32   mycroft 		np->n_flag |= NCHG;
    579  1.181      yamt 
    580  1.181      yamt 	/*
    581  1.181      yamt 	 * if we have delayed truncation, do it now.
    582  1.181      yamt 	 */
    583  1.181      yamt 	nfs_delayedtruncate(vp);
    584  1.181      yamt 
    585   1.32   mycroft 	/*
    586   1.32   mycroft 	 * First look in the cache.
    587   1.32   mycroft 	 */
    588   1.32   mycroft 	if (nfs_getattrcache(vp, ap->a_vap) == 0)
    589    1.1       cgd 		return (0);
    590    1.1       cgd 	nfsstats.rpccnt[NFSPROC_GETATTR]++;
    591  1.165  drochner 	nfsm_reqhead(np, NFSPROC_GETATTR, NFSX_FH(v3));
    592  1.165  drochner 	nfsm_fhtom(np, v3);
    593  1.230  christos 	nfsm_request(np, NFSPROC_GETATTR, ap->a_l, ap->a_cred);
    594   1.81      fvdl 	if (!error) {
    595  1.153      yamt 		nfsm_loadattr(vp, ap->a_vap, 0);
    596   1.81      fvdl 		if (vp->v_type == VDIR &&
    597   1.81      fvdl 		    ap->a_vap->va_blocksize < NFS_DIRFRAGSIZ)
    598   1.81      fvdl 			ap->a_vap->va_blocksize = NFS_DIRFRAGSIZ;
    599   1.81      fvdl 	}
    600    1.1       cgd 	nfsm_reqdone;
    601    1.1       cgd 	return (error);
    602    1.1       cgd }
    603    1.1       cgd 
    604    1.1       cgd /*
    605    1.1       cgd  * nfs setattr call.
    606    1.1       cgd  */
    607   1.32   mycroft int
    608   1.58  christos nfs_setattr(v)
    609   1.58  christos 	void *v;
    610   1.58  christos {
    611   1.32   mycroft 	struct vop_setattr_args /* {
    612   1.32   mycroft 		struct vnodeop_desc *a_desc;
    613   1.32   mycroft 		struct vnode *a_vp;
    614   1.32   mycroft 		struct vattr *a_vap;
    615  1.236      elad 		kauth_cred_t a_cred;
    616  1.230  christos 		struct lwp *a_l;
    617   1.58  christos 	} */ *ap = v;
    618  1.110  augustss 	struct vnode *vp = ap->a_vp;
    619  1.110  augustss 	struct nfsnode *np = VTONFS(vp);
    620  1.110  augustss 	struct vattr *vap = ap->a_vap;
    621   1.59      fvdl 	int error = 0;
    622   1.59      fvdl 	u_quad_t tsize = 0;
    623    1.1       cgd 
    624   1.59      fvdl 	/*
    625   1.71      fvdl 	 * Setting of flags is not supported.
    626   1.71      fvdl 	 */
    627   1.71      fvdl 	if (vap->va_flags != VNOVAL)
    628   1.71      fvdl 		return (EOPNOTSUPP);
    629   1.71      fvdl 
    630   1.71      fvdl 	/*
    631   1.59      fvdl 	 * Disallow write attempts if the filesystem is mounted read-only.
    632   1.59      fvdl 	 */
    633   1.71      fvdl   	if ((vap->va_uid != (uid_t)VNOVAL ||
    634   1.59      fvdl 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
    635   1.59      fvdl 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
    636   1.59      fvdl 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
    637   1.59      fvdl 		return (EROFS);
    638   1.35   mycroft 	if (vap->va_size != VNOVAL) {
    639   1.59      fvdl  		switch (vp->v_type) {
    640   1.59      fvdl  		case VDIR:
    641   1.59      fvdl  			return (EISDIR);
    642   1.59      fvdl  		case VCHR:
    643   1.59      fvdl  		case VBLK:
    644   1.59      fvdl  		case VSOCK:
    645   1.59      fvdl  		case VFIFO:
    646   1.56       jtc 			if (vap->va_mtime.tv_sec == VNOVAL &&
    647   1.56       jtc 			    vap->va_atime.tv_sec == VNOVAL &&
    648   1.84  christos 			    vap->va_mode == (mode_t)VNOVAL &&
    649   1.59      fvdl 			    vap->va_uid == (uid_t)VNOVAL &&
    650   1.59      fvdl 			    vap->va_gid == (gid_t)VNOVAL)
    651   1.35   mycroft 				return (0);
    652   1.59      fvdl  			vap->va_size = VNOVAL;
    653   1.59      fvdl  			break;
    654   1.59      fvdl  		default:
    655   1.59      fvdl 			/*
    656   1.59      fvdl 			 * Disallow write attempts if the filesystem is
    657   1.59      fvdl 			 * mounted read-only.
    658   1.59      fvdl 			 */
    659   1.59      fvdl 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    660   1.59      fvdl 				return (EROFS);
    661  1.244      yamt 			genfs_node_wrlock(vp);
    662   1.87       mrg  			uvm_vnp_setsize(vp, vap->va_size);
    663  1.148      fvdl  			tsize = np->n_size;
    664  1.148      fvdl 			np->n_size = vap->va_size;
    665   1.59      fvdl  			if (vap->va_size == 0)
    666   1.59      fvdl  				error = nfs_vinvalbuf(vp, 0,
    667  1.230  christos  				     ap->a_cred, ap->a_l, 1);
    668   1.59      fvdl 			else
    669   1.59      fvdl 				error = nfs_vinvalbuf(vp, V_SAVE,
    670  1.230  christos 				     ap->a_cred, ap->a_l, 1);
    671   1.71      fvdl 			if (error) {
    672  1.148      fvdl 				uvm_vnp_setsize(vp, tsize);
    673  1.244      yamt 				genfs_node_unlock(vp);
    674   1.59      fvdl 				return (error);
    675   1.71      fvdl 			}
    676  1.148      fvdl  			np->n_vattr->va_size = vap->va_size;
    677   1.71      fvdl   		}
    678  1.242  drochner   	} else {
    679  1.242  drochner 		/*
    680  1.242  drochner 		 * flush files before setattr because a later write of
    681  1.242  drochner 		 * cached data might change timestamps or reset sugid bits
    682  1.242  drochner 		 */
    683  1.242  drochner 		if ((vap->va_mtime.tv_sec != VNOVAL ||
    684  1.242  drochner 		     vap->va_atime.tv_sec != VNOVAL ||
    685  1.242  drochner 		     vap->va_mode != VNOVAL) &&
    686  1.242  drochner 		    vp->v_type == VREG &&
    687  1.242  drochner   		    (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
    688  1.242  drochner 		 			   ap->a_l, 1)) == EINTR)
    689  1.242  drochner 			return (error);
    690  1.242  drochner 	}
    691  1.230  christos 	error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_l);
    692  1.244      yamt 	if (vap->va_size != VNOVAL) {
    693  1.244      yamt 		if (error) {
    694  1.244      yamt 			np->n_size = np->n_vattr->va_size = tsize;
    695  1.244      yamt 			uvm_vnp_setsize(vp, np->n_size);
    696  1.244      yamt 		}
    697  1.244      yamt 		genfs_node_unlock(vp);
    698   1.32   mycroft 	}
    699  1.156  jdolecek 	VN_KNOTE(vp, NOTE_ATTRIB);
    700   1.59      fvdl 	return (error);
    701   1.59      fvdl }
    702   1.59      fvdl 
    703   1.59      fvdl /*
    704   1.59      fvdl  * Do an nfs setattr rpc.
    705   1.59      fvdl  */
    706   1.59      fvdl int
    707  1.230  christos nfs_setattrrpc(vp, vap, cred, l)
    708  1.110  augustss 	struct vnode *vp;
    709  1.110  augustss 	struct vattr *vap;
    710  1.236      elad 	kauth_cred_t cred;
    711  1.230  christos 	struct lwp *l;
    712   1.59      fvdl {
    713  1.110  augustss 	struct nfsv2_sattr *sp;
    714  1.252  christos 	char *cp;
    715  1.110  augustss 	int32_t t1, t2;
    716  1.252  christos 	char *bpos, *dpos;
    717   1.59      fvdl 	u_int32_t *tl;
    718  1.200  christos 	int error = 0;
    719  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
    720  1.119     bjh21 	const int v3 = NFS_ISV3(vp);
    721  1.165  drochner 	struct nfsnode *np = VTONFS(vp);
    722  1.200  christos #ifndef NFS_V2_ONLY
    723  1.200  christos 	int wccflag = NFSV3_WCCRATTR;
    724  1.252  christos 	char *cp2;
    725  1.200  christos #endif
    726   1.59      fvdl 
    727    1.1       cgd 	nfsstats.rpccnt[NFSPROC_SETATTR]++;
    728  1.165  drochner 	nfsm_reqhead(np, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
    729  1.165  drochner 	nfsm_fhtom(np, v3);
    730  1.200  christos #ifndef NFS_V2_ONLY
    731   1.59      fvdl 	if (v3) {
    732  1.251   thorpej 		nfsm_v3attrbuild(vap, true);
    733   1.59      fvdl 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    734   1.59      fvdl 		*tl = nfs_false;
    735   1.24   mycroft 	} else {
    736  1.200  christos #endif
    737   1.59      fvdl 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
    738   1.84  christos 		if (vap->va_mode == (mode_t)VNOVAL)
    739   1.59      fvdl 			sp->sa_mode = nfs_xdrneg1;
    740   1.59      fvdl 		else
    741   1.59      fvdl 			sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
    742   1.59      fvdl 		if (vap->va_uid == (uid_t)VNOVAL)
    743   1.59      fvdl 			sp->sa_uid = nfs_xdrneg1;
    744   1.59      fvdl 		else
    745   1.59      fvdl 			sp->sa_uid = txdr_unsigned(vap->va_uid);
    746   1.59      fvdl 		if (vap->va_gid == (gid_t)VNOVAL)
    747   1.59      fvdl 			sp->sa_gid = nfs_xdrneg1;
    748   1.59      fvdl 		else
    749   1.59      fvdl 			sp->sa_gid = txdr_unsigned(vap->va_gid);
    750   1.59      fvdl 		sp->sa_size = txdr_unsigned(vap->va_size);
    751   1.59      fvdl 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
    752   1.59      fvdl 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
    753  1.200  christos #ifndef NFS_V2_ONLY
    754   1.59      fvdl 	}
    755  1.200  christos #endif
    756  1.230  christos 	nfsm_request(np, NFSPROC_SETATTR, l, cred);
    757  1.200  christos #ifndef NFS_V2_ONLY
    758   1.59      fvdl 	if (v3) {
    759  1.251   thorpej 		nfsm_wcc_data(vp, wccflag, NAC_NOTRUNC, false);
    760   1.59      fvdl 	} else
    761  1.200  christos #endif
    762  1.244      yamt 		nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
    763   1.32   mycroft 	nfsm_reqdone;
    764    1.1       cgd 	return (error);
    765    1.1       cgd }
    766    1.1       cgd 
    767    1.1       cgd /*
    768    1.1       cgd  * nfs lookup call, one step at a time...
    769    1.1       cgd  * First look in cache
    770    1.1       cgd  * If not found, unlock the directory nfsnode and do the rpc
    771  1.128      fvdl  *
    772  1.128      fvdl  * This code is full of lock/unlock statements and checks, because
    773  1.128      fvdl  * we continue after cache_lookup has finished (we need to check
    774  1.128      fvdl  * with the attr cache and do an rpc if it has timed out). This means
    775  1.128      fvdl  * that the locking effects of cache_lookup have to be taken into
    776  1.128      fvdl  * account.
    777    1.1       cgd  */
    778   1.32   mycroft int
    779   1.58  christos nfs_lookup(v)
    780   1.58  christos 	void *v;
    781   1.58  christos {
    782   1.32   mycroft 	struct vop_lookup_args /* {
    783   1.32   mycroft 		struct vnodeop_desc *a_desc;
    784   1.32   mycroft 		struct vnode *a_dvp;
    785   1.32   mycroft 		struct vnode **a_vpp;
    786   1.32   mycroft 		struct componentname *a_cnp;
    787   1.58  christos 	} */ *ap = v;
    788   1.89      fvdl 	struct componentname *cnp = ap->a_cnp;
    789   1.89      fvdl 	struct vnode *dvp = ap->a_dvp;
    790   1.89      fvdl 	struct vnode **vpp = ap->a_vpp;
    791  1.102  wrstuden 	int flags;
    792   1.89      fvdl 	struct vnode *newvp;
    793   1.89      fvdl 	u_int32_t *tl;
    794  1.252  christos 	char *cp;
    795   1.89      fvdl 	int32_t t1, t2;
    796  1.252  christos 	char *bpos, *dpos, *cp2;
    797  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
    798    1.1       cgd 	long len;
    799   1.59      fvdl 	nfsfh_t *fhp;
    800    1.1       cgd 	struct nfsnode *np;
    801  1.246       chs 	int error = 0, attrflag, fhsize;
    802  1.119     bjh21 	const int v3 = NFS_ISV3(dvp);
    803  1.128      fvdl 
    804  1.102  wrstuden 	flags = cnp->cn_flags;
    805    1.1       cgd 
    806   1.60       jtk 	*vpp = NULLVP;
    807  1.128      fvdl 	newvp = NULLVP;
    808   1.59      fvdl 	if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
    809   1.59      fvdl 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    810   1.59      fvdl 		return (EROFS);
    811   1.32   mycroft 	if (dvp->v_type != VDIR)
    812    1.1       cgd 		return (ENOTDIR);
    813  1.108      fvdl 
    814  1.198      yamt 	/*
    815  1.198      yamt 	 * RFC1813(nfsv3) 3.2 says clients should handle "." by themselves.
    816  1.198      yamt 	 */
    817  1.198      yamt 	if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
    818  1.230  christos 		error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_lwp);
    819  1.198      yamt 		if (error)
    820  1.198      yamt 			return error;
    821  1.198      yamt 		if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN))
    822  1.198      yamt 			return EISDIR;
    823  1.198      yamt 		VREF(dvp);
    824  1.198      yamt 		*vpp = dvp;
    825  1.198      yamt 		if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
    826  1.198      yamt 				cnp->cn_flags |= SAVENAME;
    827  1.198      yamt 		return 0;
    828  1.198      yamt 	}
    829  1.198      yamt 
    830   1.32   mycroft 	np = VTONFS(dvp);
    831  1.106  jdolecek 
    832  1.106  jdolecek 	/*
    833  1.106  jdolecek 	 * Before tediously performing a linear scan of the directory,
    834  1.106  jdolecek 	 * check the name cache to see if the directory/name pair
    835  1.106  jdolecek 	 * we are looking for is known already.
    836  1.106  jdolecek 	 * If the directory/name pair is found in the name cache,
    837  1.106  jdolecek 	 * we have to ensure the directory has not changed from
    838  1.106  jdolecek 	 * the time the cache entry has been created. If it has,
    839  1.220     perry 	 * the cache entry has to be ignored.
    840  1.106  jdolecek 	 */
    841  1.203      yamt 	error = cache_lookup_raw(dvp, vpp, cnp);
    842  1.203      yamt 	KASSERT(dvp != *vpp);
    843  1.203      yamt 	if (error >= 0) {
    844    1.1       cgd 		struct vattr vattr;
    845  1.108      fvdl 		int err2;
    846  1.108      fvdl 
    847  1.108      fvdl 		if (error && error != ENOENT) {
    848  1.108      fvdl 			*vpp = NULLVP;
    849  1.128      fvdl 			return error;
    850  1.128      fvdl 		}
    851  1.128      fvdl 
    852  1.230  christos 		err2 = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_lwp);
    853  1.131      fvdl 		if (err2 != 0) {
    854  1.203      yamt 			if (error == 0)
    855  1.203      yamt 				vrele(*vpp);
    856  1.108      fvdl 			*vpp = NULLVP;
    857  1.128      fvdl 			return err2;
    858  1.108      fvdl 		}
    859    1.1       cgd 
    860  1.215      yamt 		if (VOP_GETATTR(dvp, &vattr, cnp->cn_cred,
    861  1.230  christos 		    cnp->cn_lwp) || timespeccmp(&vattr.va_mtime,
    862  1.215      yamt 		    &VTONFS(dvp)->n_nctime, !=)) {
    863  1.215      yamt 			if (error == 0) {
    864  1.215      yamt 				vrele(*vpp);
    865  1.215      yamt 				*vpp = NULLVP;
    866  1.203      yamt 			}
    867  1.187      yamt 			cache_purge1(dvp, NULL, PURGE_CHILDREN);
    868  1.178      yamt 			timespecclear(&np->n_nctime);
    869   1.82      fvdl 			goto dorpc;
    870   1.82      fvdl 		}
    871   1.82      fvdl 
    872  1.215      yamt 		if (error == ENOENT) {
    873  1.215      yamt 			goto noentry;
    874  1.215      yamt 		}
    875  1.215      yamt 
    876   1.59      fvdl 		newvp = *vpp;
    877  1.230  christos 		if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, cnp->cn_lwp)
    878  1.203      yamt 		    && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
    879  1.106  jdolecek 			nfsstats.lookupcache_hits++;
    880  1.246       chs 			if ((flags & ISDOTDOT) != 0) {
    881  1.128      fvdl 				VOP_UNLOCK(dvp, 0);
    882  1.202      yamt 			}
    883  1.203      yamt 			error = vn_lock(newvp, LK_EXCLUSIVE);
    884  1.246       chs 			if ((flags & ISDOTDOT) != 0) {
    885  1.246       chs 				vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    886  1.246       chs 			}
    887  1.203      yamt 			if (error) {
    888  1.203      yamt 				/* newvp has been revoked. */
    889  1.203      yamt 				vrele(newvp);
    890  1.203      yamt 				*vpp = NULL;
    891  1.203      yamt 				return error;
    892  1.203      yamt 			}
    893  1.203      yamt 			if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
    894  1.203      yamt 				cnp->cn_flags |= SAVENAME;
    895  1.190      yamt 			KASSERT(newvp->v_type != VNON);
    896  1.106  jdolecek 			return (0);
    897    1.1       cgd 		}
    898  1.187      yamt 		cache_purge1(newvp, NULL, PURGE_PARENTS);
    899  1.203      yamt 		vrele(newvp);
    900   1.32   mycroft 		*vpp = NULLVP;
    901   1.32   mycroft 	}
    902   1.82      fvdl dorpc:
    903  1.199      yamt #if 0
    904  1.192      yamt 	/*
    905  1.192      yamt 	 * because nfsv3 has the same CREATE semantics as ours,
    906  1.192      yamt 	 * we don't have to perform LOOKUPs beforehand.
    907  1.192      yamt 	 *
    908  1.192      yamt 	 * XXX ideally we can do the same for nfsv2 in the case of !O_EXCL.
    909  1.192      yamt 	 * XXX although we have no way to know if O_EXCL is requested or not.
    910  1.192      yamt 	 */
    911  1.192      yamt 
    912  1.196      yamt 	if (v3 && cnp->cn_nameiop == CREATE &&
    913  1.196      yamt 	    (flags & (ISLASTCN|ISDOTDOT)) == ISLASTCN &&
    914  1.193      yamt 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
    915  1.192      yamt 		cnp->cn_flags |= SAVENAME;
    916  1.192      yamt 		return (EJUSTRETURN);
    917  1.192      yamt 	}
    918  1.199      yamt #endif /* 0 */
    919  1.192      yamt 
    920    1.1       cgd 	error = 0;
    921   1.59      fvdl 	newvp = NULLVP;
    922    1.1       cgd 	nfsstats.lookupcache_misses++;
    923    1.1       cgd 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
    924   1.32   mycroft 	len = cnp->cn_namelen;
    925  1.165  drochner 	nfsm_reqhead(np, NFSPROC_LOOKUP,
    926   1.59      fvdl 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
    927  1.165  drochner 	nfsm_fhtom(np, v3);
    928   1.32   mycroft 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
    929  1.230  christos 	nfsm_request(np, NFSPROC_LOOKUP, cnp->cn_lwp, cnp->cn_cred);
    930    1.1       cgd 	if (error) {
    931  1.153      yamt 		nfsm_postop_attr(dvp, attrflag, 0);
    932   1.59      fvdl 		m_freem(mrep);
    933   1.59      fvdl 		goto nfsmout;
    934   1.32   mycroft 	}
    935   1.59      fvdl 	nfsm_getfh(fhp, fhsize, v3);
    936    1.1       cgd 
    937    1.1       cgd 	/*
    938   1.32   mycroft 	 * Handle RENAME case...
    939    1.1       cgd 	 */
    940  1.246       chs 	if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
    941   1.59      fvdl 		if (NFS_CMPFH(np, fhp, fhsize)) {
    942    1.1       cgd 			m_freem(mrep);
    943    1.1       cgd 			return (EISDIR);
    944    1.1       cgd 		}
    945  1.175      fvdl 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
    946   1.59      fvdl 		if (error) {
    947    1.1       cgd 			m_freem(mrep);
    948  1.128      fvdl 			return error;
    949    1.1       cgd 		}
    950    1.1       cgd 		newvp = NFSTOV(np);
    951  1.200  christos #ifndef NFS_V2_ONLY
    952   1.59      fvdl 		if (v3) {
    953  1.153      yamt 			nfsm_postop_attr(newvp, attrflag, 0);
    954  1.153      yamt 			nfsm_postop_attr(dvp, attrflag, 0);
    955   1.59      fvdl 		} else
    956  1.200  christos #endif
    957  1.153      yamt 			nfsm_loadattr(newvp, (struct vattr *)0, 0);
    958   1.32   mycroft 		*vpp = newvp;
    959    1.1       cgd 		m_freem(mrep);
    960   1.32   mycroft 		cnp->cn_flags |= SAVENAME;
    961  1.190      yamt 		goto validate;
    962    1.1       cgd 	}
    963    1.1       cgd 
    964  1.128      fvdl 	/*
    965  1.128      fvdl 	 * The postop attr handling is duplicated for each if case,
    966  1.128      fvdl 	 * because it should be done while dvp is locked (unlocking
    967  1.128      fvdl 	 * dvp is different for each case).
    968  1.128      fvdl 	 */
    969  1.128      fvdl 
    970   1.59      fvdl 	if (NFS_CMPFH(np, fhp, fhsize)) {
    971  1.128      fvdl 		/*
    972  1.128      fvdl 		 * "." lookup
    973  1.128      fvdl 		 */
    974   1.32   mycroft 		VREF(dvp);
    975   1.32   mycroft 		newvp = dvp;
    976  1.200  christos #ifndef NFS_V2_ONLY
    977  1.128      fvdl 		if (v3) {
    978  1.153      yamt 			nfsm_postop_attr(newvp, attrflag, 0);
    979  1.153      yamt 			nfsm_postop_attr(dvp, attrflag, 0);
    980  1.128      fvdl 		} else
    981  1.200  christos #endif
    982  1.153      yamt 			nfsm_loadattr(newvp, (struct vattr *)0, 0);
    983  1.128      fvdl 	} else if (flags & ISDOTDOT) {
    984  1.128      fvdl 		/*
    985  1.128      fvdl 		 * ".." lookup
    986  1.128      fvdl 		 */
    987  1.128      fvdl 		VOP_UNLOCK(dvp, 0);
    988  1.175      fvdl 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
    989  1.246       chs 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    990  1.128      fvdl 		if (error) {
    991  1.128      fvdl 			m_freem(mrep);
    992  1.128      fvdl 			return error;
    993  1.128      fvdl 		}
    994  1.128      fvdl 		newvp = NFSTOV(np);
    995  1.128      fvdl 
    996  1.200  christos #ifndef NFS_V2_ONLY
    997  1.128      fvdl 		if (v3) {
    998  1.153      yamt 			nfsm_postop_attr(newvp, attrflag, 0);
    999  1.153      yamt 			nfsm_postop_attr(dvp, attrflag, 0);
   1000  1.128      fvdl 		} else
   1001  1.200  christos #endif
   1002  1.153      yamt 			nfsm_loadattr(newvp, (struct vattr *)0, 0);
   1003    1.1       cgd 	} else {
   1004  1.128      fvdl 		/*
   1005  1.128      fvdl 		 * Other lookups.
   1006  1.128      fvdl 		 */
   1007  1.175      fvdl 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
   1008   1.59      fvdl 		if (error) {
   1009    1.1       cgd 			m_freem(mrep);
   1010  1.128      fvdl 			return error;
   1011    1.1       cgd 		}
   1012    1.1       cgd 		newvp = NFSTOV(np);
   1013  1.200  christos #ifndef NFS_V2_ONLY
   1014  1.128      fvdl 		if (v3) {
   1015  1.153      yamt 			nfsm_postop_attr(newvp, attrflag, 0);
   1016  1.153      yamt 			nfsm_postop_attr(dvp, attrflag, 0);
   1017  1.128      fvdl 		} else
   1018  1.200  christos #endif
   1019  1.153      yamt 			nfsm_loadattr(newvp, (struct vattr *)0, 0);
   1020    1.1       cgd 	}
   1021   1.32   mycroft 	if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
   1022   1.32   mycroft 		cnp->cn_flags |= SAVENAME;
   1023   1.32   mycroft 	if ((cnp->cn_flags & MAKEENTRY) &&
   1024   1.32   mycroft 	    (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
   1025  1.188      yamt 		nfs_cache_enter(dvp, newvp, cnp);
   1026    1.1       cgd 	}
   1027   1.59      fvdl 	*vpp = newvp;
   1028   1.59      fvdl 	nfsm_reqdone;
   1029   1.59      fvdl 	if (error) {
   1030  1.128      fvdl 		/*
   1031  1.128      fvdl 		 * We get here only because of errors returned by
   1032  1.128      fvdl 		 * the RPC. Otherwise we'll have returned above
   1033  1.128      fvdl 		 * (the nfsm_* macros will jump to nfsm_reqdone
   1034  1.128      fvdl 		 * on error).
   1035  1.128      fvdl 		 */
   1036   1.82      fvdl 		if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) &&
   1037   1.82      fvdl 		    cnp->cn_nameiop != CREATE) {
   1038  1.188      yamt 			nfs_cache_enter(dvp, NULL, cnp);
   1039   1.82      fvdl 		}
   1040  1.128      fvdl 		if (newvp != NULLVP) {
   1041  1.246       chs 			if (newvp == dvp) {
   1042  1.246       chs 				vrele(newvp);
   1043  1.246       chs 			} else {
   1044  1.246       chs 				vput(newvp);
   1045  1.246       chs 			}
   1046  1.128      fvdl 		}
   1047  1.203      yamt noentry:
   1048   1.59      fvdl 		if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
   1049   1.59      fvdl 		    (flags & ISLASTCN) && error == ENOENT) {
   1050  1.203      yamt 			if (dvp->v_mount->mnt_flag & MNT_RDONLY) {
   1051   1.59      fvdl 				error = EROFS;
   1052  1.203      yamt 			} else {
   1053   1.59      fvdl 				error = EJUSTRETURN;
   1054  1.203      yamt 				cnp->cn_flags |= SAVENAME;
   1055  1.203      yamt 			}
   1056   1.59      fvdl 		}
   1057  1.128      fvdl 		*vpp = NULL;
   1058  1.190      yamt 		return error;
   1059  1.190      yamt 	}
   1060  1.190      yamt 
   1061  1.190      yamt validate:
   1062  1.190      yamt 	/*
   1063  1.190      yamt 	 * make sure we have valid type and size.
   1064  1.190      yamt 	 */
   1065  1.190      yamt 
   1066  1.190      yamt 	newvp = *vpp;
   1067  1.190      yamt 	if (newvp->v_type == VNON) {
   1068  1.190      yamt 		struct vattr vattr; /* dummy */
   1069  1.190      yamt 
   1070  1.190      yamt 		KASSERT(VTONFS(newvp)->n_attrstamp == 0);
   1071  1.230  christos 		error = VOP_GETATTR(newvp, &vattr, cnp->cn_cred, cnp->cn_lwp);
   1072  1.190      yamt 		if (error) {
   1073  1.190      yamt 			vput(newvp);
   1074  1.190      yamt 			*vpp = NULL;
   1075  1.190      yamt 		}
   1076   1.59      fvdl 	}
   1077  1.128      fvdl 
   1078  1.128      fvdl 	return error;
   1079    1.1       cgd }
   1080    1.1       cgd 
   1081    1.1       cgd /*
   1082    1.1       cgd  * nfs read call.
   1083    1.1       cgd  * Just call nfs_bioread() to do the work.
   1084    1.1       cgd  */
   1085   1.32   mycroft int
   1086   1.58  christos nfs_read(v)
   1087   1.58  christos 	void *v;
   1088   1.58  christos {
   1089   1.32   mycroft 	struct vop_read_args /* {
   1090   1.32   mycroft 		struct vnode *a_vp;
   1091   1.32   mycroft 		struct uio *a_uio;
   1092   1.32   mycroft 		int  a_ioflag;
   1093  1.236      elad 		kauth_cred_t a_cred;
   1094   1.58  christos 	} */ *ap = v;
   1095  1.110  augustss 	struct vnode *vp = ap->a_vp;
   1096   1.32   mycroft 
   1097    1.1       cgd 	if (vp->v_type != VREG)
   1098  1.229      yamt 		return EISDIR;
   1099   1.81      fvdl 	return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred, 0));
   1100    1.1       cgd }
   1101    1.1       cgd 
   1102    1.1       cgd /*
   1103    1.1       cgd  * nfs readlink call
   1104    1.1       cgd  */
   1105   1.32   mycroft int
   1106   1.58  christos nfs_readlink(v)
   1107   1.58  christos 	void *v;
   1108   1.58  christos {
   1109   1.32   mycroft 	struct vop_readlink_args /* {
   1110   1.32   mycroft 		struct vnode *a_vp;
   1111   1.32   mycroft 		struct uio *a_uio;
   1112  1.236      elad 		kauth_cred_t a_cred;
   1113   1.58  christos 	} */ *ap = v;
   1114  1.110  augustss 	struct vnode *vp = ap->a_vp;
   1115  1.224      yamt 	struct nfsnode *np = VTONFS(vp);
   1116   1.32   mycroft 
   1117    1.1       cgd 	if (vp->v_type != VLNK)
   1118    1.1       cgd 		return (EPERM);
   1119  1.224      yamt 
   1120  1.224      yamt 	if (np->n_rcred != NULL) {
   1121  1.236      elad 		kauth_cred_free(np->n_rcred);
   1122  1.224      yamt 	}
   1123  1.224      yamt 	np->n_rcred = ap->a_cred;
   1124  1.236      elad 	kauth_cred_hold(np->n_rcred);
   1125  1.224      yamt 
   1126   1.81      fvdl 	return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred, 0));
   1127    1.1       cgd }
   1128    1.1       cgd 
   1129    1.1       cgd /*
   1130    1.1       cgd  * Do a readlink rpc.
   1131    1.1       cgd  * Called by nfs_doio() from below the buffer cache.
   1132    1.1       cgd  */
   1133   1.32   mycroft int
   1134    1.1       cgd nfs_readlinkrpc(vp, uiop, cred)
   1135  1.110  augustss 	struct vnode *vp;
   1136    1.1       cgd 	struct uio *uiop;
   1137  1.236      elad 	kauth_cred_t cred;
   1138    1.1       cgd {
   1139  1.110  augustss 	u_int32_t *tl;
   1140  1.252  christos 	char *cp;
   1141  1.110  augustss 	int32_t t1, t2;
   1142  1.252  christos 	char *bpos, *dpos, *cp2;
   1143  1.200  christos 	int error = 0;
   1144  1.169      yamt 	uint32_t len;
   1145  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
   1146  1.119     bjh21 	const int v3 = NFS_ISV3(vp);
   1147  1.165  drochner 	struct nfsnode *np = VTONFS(vp);
   1148  1.200  christos #ifndef NFS_V2_ONLY
   1149  1.200  christos 	int attrflag;
   1150  1.200  christos #endif
   1151    1.1       cgd 
   1152    1.1       cgd 	nfsstats.rpccnt[NFSPROC_READLINK]++;
   1153  1.165  drochner 	nfsm_reqhead(np, NFSPROC_READLINK, NFSX_FH(v3));
   1154  1.165  drochner 	nfsm_fhtom(np, v3);
   1155  1.231      yamt 	nfsm_request(np, NFSPROC_READLINK, curlwp, cred);
   1156  1.200  christos #ifndef NFS_V2_ONLY
   1157   1.59      fvdl 	if (v3)
   1158  1.153      yamt 		nfsm_postop_attr(vp, attrflag, 0);
   1159  1.200  christos #endif
   1160   1.59      fvdl 	if (!error) {
   1161  1.200  christos #ifndef NFS_V2_ONLY
   1162  1.169      yamt 		if (v3) {
   1163  1.169      yamt 			nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
   1164  1.169      yamt 			len = fxdr_unsigned(uint32_t, *tl);
   1165  1.169      yamt 			if (len > MAXPATHLEN) {
   1166  1.169      yamt 				/*
   1167  1.169      yamt 				 * this pathname is too long for us.
   1168  1.169      yamt 				 */
   1169  1.169      yamt 				m_freem(mrep);
   1170  1.169      yamt 				/* Solaris returns EINVAL. should we follow? */
   1171  1.169      yamt 				error = ENAMETOOLONG;
   1172  1.169      yamt 				goto nfsmout;
   1173  1.169      yamt 			}
   1174  1.200  christos 		} else
   1175  1.200  christos #endif
   1176  1.200  christos 		{
   1177  1.169      yamt 			nfsm_strsiz(len, NFS_MAXPATHLEN);
   1178  1.169      yamt 		}
   1179   1.59      fvdl 		nfsm_mtouio(uiop, len);
   1180   1.59      fvdl 	}
   1181    1.1       cgd 	nfsm_reqdone;
   1182    1.1       cgd 	return (error);
   1183    1.1       cgd }
   1184    1.1       cgd 
   1185    1.1       cgd /*
   1186    1.1       cgd  * nfs read rpc call
   1187    1.1       cgd  * Ditto above
   1188    1.1       cgd  */
   1189   1.32   mycroft int
   1190  1.124       chs nfs_readrpc(vp, uiop)
   1191  1.110  augustss 	struct vnode *vp;
   1192    1.1       cgd 	struct uio *uiop;
   1193    1.1       cgd {
   1194  1.110  augustss 	u_int32_t *tl;
   1195  1.252  christos 	char *cp;
   1196  1.110  augustss 	int32_t t1, t2;
   1197  1.252  christos 	char *bpos, *dpos, *cp2;
   1198  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
   1199    1.1       cgd 	struct nfsmount *nmp;
   1200  1.232     blymn 	int error = 0, len, retlen, tsiz, eof, byte_count;
   1201  1.119     bjh21 	const int v3 = NFS_ISV3(vp);
   1202  1.165  drochner 	struct nfsnode *np = VTONFS(vp);
   1203  1.200  christos #ifndef NFS_V2_ONLY
   1204  1.200  christos 	int attrflag;
   1205  1.200  christos #endif
   1206    1.1       cgd 
   1207   1.59      fvdl #ifndef nolint
   1208   1.59      fvdl 	eof = 0;
   1209   1.59      fvdl #endif
   1210    1.1       cgd 	nmp = VFSTONFS(vp->v_mount);
   1211    1.1       cgd 	tsiz = uiop->uio_resid;
   1212   1.80      fvdl 	if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
   1213   1.32   mycroft 		return (EFBIG);
   1214  1.232     blymn 	iostat_busy(nmp->nm_stats);
   1215  1.232     blymn 	byte_count = 0; /* count bytes actually transferred */
   1216    1.1       cgd 	while (tsiz > 0) {
   1217    1.1       cgd 		nfsstats.rpccnt[NFSPROC_READ]++;
   1218    1.1       cgd 		len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
   1219  1.165  drochner 		nfsm_reqhead(np, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
   1220  1.165  drochner 		nfsm_fhtom(np, v3);
   1221   1.59      fvdl 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
   1222  1.200  christos #ifndef NFS_V2_ONLY
   1223   1.59      fvdl 		if (v3) {
   1224   1.98      fair 			txdr_hyper(uiop->uio_offset, tl);
   1225   1.32   mycroft 			*(tl + 2) = txdr_unsigned(len);
   1226  1.200  christos 		} else
   1227  1.200  christos #endif
   1228  1.200  christos 		{
   1229   1.32   mycroft 			*tl++ = txdr_unsigned(uiop->uio_offset);
   1230   1.32   mycroft 			*tl++ = txdr_unsigned(len);
   1231   1.32   mycroft 			*tl = 0;
   1232   1.32   mycroft 		}
   1233  1.231      yamt 		nfsm_request(np, NFSPROC_READ, curlwp, np->n_rcred);
   1234  1.200  christos #ifndef NFS_V2_ONLY
   1235   1.59      fvdl 		if (v3) {
   1236  1.153      yamt 			nfsm_postop_attr(vp, attrflag, NAC_NOTRUNC);
   1237   1.59      fvdl 			if (error) {
   1238   1.59      fvdl 				m_freem(mrep);
   1239   1.59      fvdl 				goto nfsmout;
   1240   1.59      fvdl 			}
   1241   1.59      fvdl 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1242   1.59      fvdl 			eof = fxdr_unsigned(int, *(tl + 1));
   1243   1.59      fvdl 		} else
   1244  1.200  christos #endif
   1245  1.153      yamt 			nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
   1246    1.1       cgd 		nfsm_strsiz(retlen, nmp->nm_rsize);
   1247    1.1       cgd 		nfsm_mtouio(uiop, retlen);
   1248    1.1       cgd 		m_freem(mrep);
   1249   1.59      fvdl 		tsiz -= retlen;
   1250  1.232     blymn 		byte_count += retlen;
   1251  1.200  christos #ifndef NFS_V2_ONLY
   1252   1.59      fvdl 		if (v3) {
   1253   1.59      fvdl 			if (eof || retlen == 0)
   1254   1.59      fvdl 				tsiz = 0;
   1255  1.200  christos 		} else
   1256  1.200  christos #endif
   1257  1.200  christos 		if (retlen < len)
   1258    1.1       cgd 			tsiz = 0;
   1259    1.1       cgd 	}
   1260    1.1       cgd nfsmout:
   1261  1.232     blymn 	iostat_unbusy(nmp->nm_stats, byte_count, 1);
   1262    1.1       cgd 	return (error);
   1263    1.1       cgd }
   1264    1.1       cgd 
   1265  1.171      yamt struct nfs_writerpc_context {
   1266  1.253      yamt 	kmutex_t nwc_lock;
   1267  1.253      yamt 	kcondvar_t nwc_cv;
   1268  1.253      yamt 	int nwc_mbufcount;
   1269  1.171      yamt };
   1270  1.171      yamt 
   1271    1.1       cgd /*
   1272  1.170      yamt  * free mbuf used to refer protected pages while write rpc call.
   1273  1.171      yamt  * called at splvm.
   1274  1.167      yamt  */
   1275  1.167      yamt static void
   1276  1.252  christos nfs_writerpc_extfree(struct mbuf *m, void *tbuf, size_t size, void *arg)
   1277  1.167      yamt {
   1278  1.171      yamt 	struct nfs_writerpc_context *ctx = arg;
   1279  1.170      yamt 
   1280  1.170      yamt 	KASSERT(m != NULL);
   1281  1.171      yamt 	KASSERT(ctx != NULL);
   1282  1.170      yamt 	pool_cache_put(&mbpool_cache, m);
   1283  1.253      yamt 	mutex_enter(&ctx->nwc_lock);
   1284  1.171      yamt 	if (--ctx->nwc_mbufcount == 0) {
   1285  1.253      yamt 		cv_signal(&ctx->nwc_cv);
   1286  1.171      yamt 	}
   1287  1.253      yamt 	mutex_exit(&ctx->nwc_lock);
   1288  1.167      yamt }
   1289  1.167      yamt 
   1290  1.167      yamt /*
   1291    1.1       cgd  * nfs write call
   1292    1.1       cgd  */
   1293   1.32   mycroft int
   1294  1.173      yamt nfs_writerpc(vp, uiop, iomode, pageprotected, stalewriteverfp)
   1295  1.110  augustss 	struct vnode *vp;
   1296  1.110  augustss 	struct uio *uiop;
   1297  1.166      yamt 	int *iomode;
   1298  1.250   thorpej 	bool pageprotected;
   1299  1.250   thorpej 	bool *stalewriteverfp;
   1300    1.1       cgd {
   1301  1.110  augustss 	u_int32_t *tl;
   1302  1.252  christos 	char *cp;
   1303  1.173      yamt 	int32_t t1, t2;
   1304  1.252  christos 	char *bpos, *dpos;
   1305  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
   1306   1.59      fvdl 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1307  1.200  christos 	int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR;
   1308  1.119     bjh21 	const int v3 = NFS_ISV3(vp);
   1309  1.119     bjh21 	int committed = NFSV3WRITE_FILESYNC;
   1310  1.165  drochner 	struct nfsnode *np = VTONFS(vp);
   1311  1.171      yamt 	struct nfs_writerpc_context ctx;
   1312  1.253      yamt 	int byte_count;
   1313  1.182  christos 	struct lwp *l = NULL;
   1314  1.173      yamt 	size_t origresid;
   1315  1.200  christos #ifndef NFS_V2_ONLY
   1316  1.252  christos 	char *cp2;
   1317  1.200  christos 	int rlen, commit;
   1318  1.200  christos #endif
   1319  1.171      yamt 
   1320  1.253      yamt 	mutex_init(&ctx.nwc_lock, MUTEX_DRIVER, IPL_VM);
   1321  1.253      yamt 	cv_init(&ctx.nwc_cv, "nfsmblk");
   1322  1.171      yamt 	ctx.nwc_mbufcount = 1;
   1323  1.125       chs 
   1324  1.125       chs 	if (vp->v_mount->mnt_flag & MNT_RDONLY) {
   1325  1.125       chs 		panic("writerpc readonly vp %p", vp);
   1326  1.125       chs 	}
   1327    1.1       cgd 
   1328  1.142     bjh21 #ifdef DIAGNOSTIC
   1329   1.59      fvdl 	if (uiop->uio_iovcnt != 1)
   1330   1.59      fvdl 		panic("nfs: writerpc iovcnt > 1");
   1331   1.59      fvdl #endif
   1332    1.1       cgd 	tsiz = uiop->uio_resid;
   1333   1.80      fvdl 	if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
   1334   1.32   mycroft 		return (EFBIG);
   1335  1.171      yamt 	if (pageprotected) {
   1336  1.171      yamt 		l = curlwp;
   1337  1.171      yamt 		PHOLD(l);
   1338  1.171      yamt 	}
   1339  1.173      yamt retry:
   1340  1.173      yamt 	origresid = uiop->uio_resid;
   1341  1.173      yamt 	KASSERT(origresid == uiop->uio_iov->iov_len);
   1342  1.232     blymn 	iostat_busy(nmp->nm_stats);
   1343  1.232     blymn 	byte_count = 0; /* count of bytes actually written */
   1344    1.1       cgd 	while (tsiz > 0) {
   1345  1.173      yamt 		uint32_t datalen; /* data bytes need to be allocated in mbuf */
   1346  1.173      yamt 		uint32_t backup;
   1347  1.251   thorpej 		bool stalewriteverf = false;
   1348  1.167      yamt 
   1349    1.1       cgd 		nfsstats.rpccnt[NFSPROC_WRITE]++;
   1350  1.124       chs 		len = min(tsiz, nmp->nm_wsize);
   1351  1.167      yamt 		datalen = pageprotected ? 0 : nfsm_rndup(len);
   1352  1.165  drochner 		nfsm_reqhead(np, NFSPROC_WRITE,
   1353  1.167      yamt 			NFSX_FH(v3) + 5 * NFSX_UNSIGNED + datalen);
   1354  1.165  drochner 		nfsm_fhtom(np, v3);
   1355  1.200  christos #ifndef NFS_V2_ONLY
   1356   1.59      fvdl 		if (v3) {
   1357   1.59      fvdl 			nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
   1358   1.98      fair 			txdr_hyper(uiop->uio_offset, tl);
   1359   1.32   mycroft 			tl += 2;
   1360   1.59      fvdl 			*tl++ = txdr_unsigned(len);
   1361   1.59      fvdl 			*tl++ = txdr_unsigned(*iomode);
   1362   1.50       gwr 			*tl = txdr_unsigned(len);
   1363  1.200  christos 		} else
   1364  1.200  christos #endif
   1365  1.200  christos 		{
   1366  1.110  augustss 			u_int32_t x;
   1367   1.59      fvdl 
   1368   1.59      fvdl 			nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
   1369   1.50       gwr 			/* Set both "begin" and "current" to non-garbage. */
   1370   1.50       gwr 			x = txdr_unsigned((u_int32_t)uiop->uio_offset);
   1371   1.59      fvdl 			*tl++ = x;      /* "begin offset" */
   1372   1.59      fvdl 			*tl++ = x;      /* "current offset" */
   1373   1.50       gwr 			x = txdr_unsigned(len);
   1374   1.59      fvdl 			*tl++ = x;      /* total to this offset */
   1375   1.59      fvdl 			*tl = x;        /* size of this write */
   1376   1.59      fvdl 
   1377   1.32   mycroft 		}
   1378  1.167      yamt 		if (pageprotected) {
   1379  1.167      yamt 			/*
   1380  1.167      yamt 			 * since we know pages can't be modified during i/o,
   1381  1.167      yamt 			 * no need to copy them for us.
   1382  1.167      yamt 			 */
   1383  1.167      yamt 			struct mbuf *m;
   1384  1.167      yamt 			struct iovec *iovp = uiop->uio_iov;
   1385  1.167      yamt 
   1386  1.167      yamt 			m = m_get(M_WAIT, MT_DATA);
   1387  1.170      yamt 			MCLAIM(m, &nfs_mowner);
   1388  1.170      yamt 			MEXTADD(m, iovp->iov_base, len, M_MBUF,
   1389  1.171      yamt 			    nfs_writerpc_extfree, &ctx);
   1390  1.167      yamt 			m->m_flags |= M_EXT_ROMAP;
   1391  1.167      yamt 			m->m_len = len;
   1392  1.167      yamt 			mb->m_next = m;
   1393  1.167      yamt 			/*
   1394  1.167      yamt 			 * no need to maintain mb and bpos here
   1395  1.167      yamt 			 * because no one care them later.
   1396  1.167      yamt 			 */
   1397  1.167      yamt #if 0
   1398  1.167      yamt 			mb = m;
   1399  1.252  christos 			bpos = mtod(void *, mb) + mb->m_len;
   1400  1.167      yamt #endif
   1401  1.225  christos 			UIO_ADVANCE(uiop, len);
   1402  1.167      yamt 			uiop->uio_offset += len;
   1403  1.253      yamt 			mutex_enter(&ctx.nwc_lock);
   1404  1.171      yamt 			ctx.nwc_mbufcount++;
   1405  1.253      yamt 			mutex_exit(&ctx.nwc_lock);
   1406  1.183      yamt 			nfs_zeropad(mb, 0, nfsm_padlen(len));
   1407  1.167      yamt 		} else {
   1408  1.167      yamt 			nfsm_uiotom(uiop, len);
   1409  1.167      yamt 		}
   1410  1.231      yamt 		nfsm_request(np, NFSPROC_WRITE, curlwp, np->n_wcred);
   1411  1.200  christos #ifndef NFS_V2_ONLY
   1412   1.59      fvdl 		if (v3) {
   1413   1.59      fvdl 			wccflag = NFSV3_WCCCHK;
   1414  1.216      yamt 			nfsm_wcc_data(vp, wccflag, NAC_NOTRUNC, !error);
   1415   1.59      fvdl 			if (!error) {
   1416   1.59      fvdl 				nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
   1417   1.59      fvdl 					+ NFSX_V3WRITEVERF);
   1418   1.59      fvdl 				rlen = fxdr_unsigned(int, *tl++);
   1419   1.59      fvdl 				if (rlen == 0) {
   1420   1.59      fvdl 					error = NFSERR_IO;
   1421   1.80      fvdl 					m_freem(mrep);
   1422   1.59      fvdl 					break;
   1423   1.59      fvdl 				} else if (rlen < len) {
   1424   1.59      fvdl 					backup = len - rlen;
   1425  1.225  christos 					UIO_ADVANCE(uiop, -backup);
   1426   1.59      fvdl 					uiop->uio_offset -= backup;
   1427   1.59      fvdl 					len = rlen;
   1428   1.59      fvdl 				}
   1429   1.59      fvdl 				commit = fxdr_unsigned(int, *tl++);
   1430   1.59      fvdl 
   1431   1.59      fvdl 				/*
   1432   1.59      fvdl 				 * Return the lowest committment level
   1433   1.59      fvdl 				 * obtained by any of the RPCs.
   1434   1.59      fvdl 				 */
   1435   1.59      fvdl 				if (committed == NFSV3WRITE_FILESYNC)
   1436   1.59      fvdl 					committed = commit;
   1437   1.59      fvdl 				else if (committed == NFSV3WRITE_DATASYNC &&
   1438   1.59      fvdl 					commit == NFSV3WRITE_UNSTABLE)
   1439   1.59      fvdl 					committed = commit;
   1440  1.166      yamt 				simple_lock(&nmp->nm_slock);
   1441   1.81      fvdl 				if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0){
   1442  1.166      yamt 					memcpy(nmp->nm_writeverf, tl,
   1443  1.166      yamt 					    NFSX_V3WRITEVERF);
   1444  1.166      yamt 					nmp->nm_iflag |= NFSMNT_HASWRITEVERF;
   1445  1.166      yamt 				} else if ((nmp->nm_iflag &
   1446  1.166      yamt 				    NFSMNT_STALEWRITEVERF) ||
   1447  1.166      yamt 				    memcmp(tl, nmp->nm_writeverf,
   1448  1.166      yamt 				    NFSX_V3WRITEVERF)) {
   1449  1.166      yamt 					memcpy(nmp->nm_writeverf, tl,
   1450  1.166      yamt 					    NFSX_V3WRITEVERF);
   1451  1.173      yamt 					/*
   1452  1.173      yamt 					 * note NFSMNT_STALEWRITEVERF
   1453  1.220     perry 					 * if we're the first thread to
   1454  1.173      yamt 					 * notice it.
   1455  1.173      yamt 					 */
   1456  1.173      yamt 					if ((nmp->nm_iflag &
   1457  1.173      yamt 					    NFSMNT_STALEWRITEVERF) == 0) {
   1458  1.251   thorpej 						stalewriteverf = true;
   1459  1.173      yamt 						nmp->nm_iflag |=
   1460  1.173      yamt 						    NFSMNT_STALEWRITEVERF;
   1461  1.173      yamt 					}
   1462   1.59      fvdl 				}
   1463  1.166      yamt 				simple_unlock(&nmp->nm_slock);
   1464   1.59      fvdl 			}
   1465   1.59      fvdl 		} else
   1466  1.200  christos #endif
   1467  1.172      yamt 			nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
   1468   1.59      fvdl 		if (wccflag)
   1469  1.180      yamt 			VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr->va_mtime;
   1470    1.1       cgd 		m_freem(mrep);
   1471   1.80      fvdl 		if (error)
   1472   1.80      fvdl 			break;
   1473    1.1       cgd 		tsiz -= len;
   1474  1.232     blymn 		byte_count += len;
   1475  1.173      yamt 		if (stalewriteverf) {
   1476  1.251   thorpej 			*stalewriteverfp = true;
   1477  1.251   thorpej 			stalewriteverf = false;
   1478  1.173      yamt 			if (committed == NFSV3WRITE_UNSTABLE &&
   1479  1.173      yamt 			    len != origresid) {
   1480  1.173      yamt 				/*
   1481  1.173      yamt 				 * if our write requests weren't atomic but
   1482  1.173      yamt 				 * unstable, datas in previous iterations
   1483  1.173      yamt 				 * might have already been lost now.
   1484  1.173      yamt 				 * then, we should resend them to nfsd.
   1485  1.173      yamt 				 */
   1486  1.173      yamt 				backup = origresid - tsiz;
   1487  1.225  christos 				UIO_ADVANCE(uiop, -backup);
   1488  1.173      yamt 				uiop->uio_offset -= backup;
   1489  1.173      yamt 				tsiz = origresid;
   1490  1.173      yamt 				goto retry;
   1491  1.173      yamt 			}
   1492  1.173      yamt 		}
   1493  1.171      yamt 	}
   1494  1.210      yamt nfsmout:
   1495  1.232     blymn 	iostat_unbusy(nmp->nm_stats, byte_count, 0);
   1496  1.171      yamt 	if (pageprotected) {
   1497  1.171      yamt 		/*
   1498  1.171      yamt 		 * wait until mbufs go away.
   1499  1.171      yamt 		 * retransmitted mbufs can survive longer than rpc requests
   1500  1.171      yamt 		 * themselves.
   1501  1.171      yamt 		 */
   1502  1.253      yamt 		mutex_enter(&ctx.nwc_lock);
   1503  1.171      yamt 		ctx.nwc_mbufcount--;
   1504  1.171      yamt 		while (ctx.nwc_mbufcount > 0) {
   1505  1.253      yamt 			cv_wait(&ctx.nwc_cv, &ctx.nwc_lock);
   1506  1.171      yamt 		}
   1507  1.253      yamt 		mutex_exit(&ctx.nwc_lock);
   1508  1.171      yamt 		PRELE(l);
   1509    1.1       cgd 	}
   1510   1.59      fvdl 	*iomode = committed;
   1511   1.32   mycroft 	if (error)
   1512   1.32   mycroft 		uiop->uio_resid = tsiz;
   1513    1.1       cgd 	return (error);
   1514    1.1       cgd }
   1515    1.1       cgd 
   1516    1.1       cgd /*
   1517   1.59      fvdl  * nfs mknod rpc
   1518   1.59      fvdl  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
   1519   1.59      fvdl  * mode set to specify the file type and the size field for rdev.
   1520    1.1       cgd  */
   1521   1.32   mycroft int
   1522   1.59      fvdl nfs_mknodrpc(dvp, vpp, cnp, vap)
   1523  1.110  augustss 	struct vnode *dvp;
   1524  1.110  augustss 	struct vnode **vpp;
   1525  1.110  augustss 	struct componentname *cnp;
   1526  1.110  augustss 	struct vattr *vap;
   1527   1.58  christos {
   1528  1.110  augustss 	struct nfsv2_sattr *sp;
   1529  1.110  augustss 	u_int32_t *tl;
   1530  1.252  christos 	char *cp;
   1531  1.110  augustss 	int32_t t1, t2;
   1532   1.59      fvdl 	struct vnode *newvp = (struct vnode *)0;
   1533  1.165  drochner 	struct nfsnode *dnp, *np;
   1534   1.32   mycroft 	char *cp2;
   1535  1.252  christos 	char *bpos, *dpos;
   1536   1.59      fvdl 	int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
   1537  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
   1538   1.53       cgd 	u_int32_t rdev;
   1539  1.119     bjh21 	const int v3 = NFS_ISV3(dvp);
   1540    1.1       cgd 
   1541    1.1       cgd 	if (vap->va_type == VCHR || vap->va_type == VBLK)
   1542    1.1       cgd 		rdev = txdr_unsigned(vap->va_rdev);
   1543   1.59      fvdl 	else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
   1544   1.59      fvdl 		rdev = nfs_xdrneg1;
   1545    1.1       cgd 	else {
   1546   1.32   mycroft 		VOP_ABORTOP(dvp, cnp);
   1547   1.32   mycroft 		vput(dvp);
   1548    1.1       cgd 		return (EOPNOTSUPP);
   1549    1.1       cgd 	}
   1550   1.59      fvdl 	nfsstats.rpccnt[NFSPROC_MKNOD]++;
   1551  1.165  drochner 	dnp = VTONFS(dvp);
   1552  1.165  drochner 	nfsm_reqhead(dnp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
   1553   1.59      fvdl 		+ nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
   1554  1.165  drochner 	nfsm_fhtom(dnp, v3);
   1555   1.32   mycroft 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1556  1.200  christos #ifndef NFS_V2_ONLY
   1557   1.59      fvdl 	if (v3) {
   1558  1.101      fvdl 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1559   1.59      fvdl 		*tl++ = vtonfsv3_type(vap->va_type);
   1560  1.251   thorpej 		nfsm_v3attrbuild(vap, false);
   1561   1.59      fvdl 		if (vap->va_type == VCHR || vap->va_type == VBLK) {
   1562   1.59      fvdl 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1563   1.59      fvdl 			*tl++ = txdr_unsigned(major(vap->va_rdev));
   1564   1.59      fvdl 			*tl = txdr_unsigned(minor(vap->va_rdev));
   1565   1.59      fvdl 		}
   1566  1.200  christos 	} else
   1567  1.200  christos #endif
   1568  1.200  christos 	{
   1569   1.59      fvdl 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   1570   1.59      fvdl 		sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
   1571   1.59      fvdl 		sp->sa_uid = nfs_xdrneg1;
   1572   1.59      fvdl 		sp->sa_gid = nfs_xdrneg1;
   1573   1.59      fvdl 		sp->sa_size = rdev;
   1574   1.59      fvdl 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   1575   1.59      fvdl 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   1576   1.59      fvdl 	}
   1577  1.230  christos 	nfsm_request(dnp, NFSPROC_MKNOD, cnp->cn_lwp, cnp->cn_cred);
   1578   1.59      fvdl 	if (!error) {
   1579  1.175      fvdl 		nfsm_mtofh(dvp, newvp, v3, gotvp);
   1580   1.59      fvdl 		if (!gotvp) {
   1581   1.59      fvdl 			error = nfs_lookitup(dvp, cnp->cn_nameptr,
   1582  1.230  christos 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_lwp, &np);
   1583   1.59      fvdl 			if (!error)
   1584   1.59      fvdl 				newvp = NFSTOV(np);
   1585   1.59      fvdl 		}
   1586   1.32   mycroft 	}
   1587  1.200  christos #ifndef NFS_V2_ONLY
   1588   1.59      fvdl 	if (v3)
   1589  1.216      yamt 		nfsm_wcc_data(dvp, wccflag, 0, !error);
   1590  1.200  christos #endif
   1591    1.1       cgd 	nfsm_reqdone;
   1592   1.59      fvdl 	if (error) {
   1593   1.59      fvdl 		if (newvp)
   1594  1.128      fvdl 			vput(newvp);
   1595   1.59      fvdl 	} else {
   1596   1.59      fvdl 		if (cnp->cn_flags & MAKEENTRY)
   1597  1.188      yamt 			nfs_cache_enter(dvp, newvp, cnp);
   1598   1.59      fvdl 		*vpp = newvp;
   1599   1.59      fvdl 	}
   1600  1.117   thorpej 	PNBUF_PUT(cnp->cn_pnbuf);
   1601   1.32   mycroft 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1602   1.59      fvdl 	if (!wccflag)
   1603  1.185      yamt 		NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
   1604  1.128      fvdl 	vput(dvp);
   1605   1.59      fvdl 	return (error);
   1606   1.59      fvdl }
   1607   1.59      fvdl 
   1608   1.59      fvdl /*
   1609   1.59      fvdl  * nfs mknod vop
   1610   1.59      fvdl  * just call nfs_mknodrpc() to do the work.
   1611   1.59      fvdl  */
   1612   1.59      fvdl /* ARGSUSED */
   1613   1.59      fvdl int
   1614   1.59      fvdl nfs_mknod(v)
   1615   1.59      fvdl 	void *v;
   1616   1.59      fvdl {
   1617   1.59      fvdl 	struct vop_mknod_args /* {
   1618   1.59      fvdl 		struct vnode *a_dvp;
   1619   1.59      fvdl 		struct vnode **a_vpp;
   1620   1.59      fvdl 		struct componentname *a_cnp;
   1621   1.59      fvdl 		struct vattr *a_vap;
   1622   1.59      fvdl 	} */ *ap = v;
   1623  1.203      yamt 	struct vnode *dvp = ap->a_dvp;
   1624  1.203      yamt 	struct componentname *cnp = ap->a_cnp;
   1625   1.59      fvdl 	int error;
   1626   1.59      fvdl 
   1627  1.203      yamt 	error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, ap->a_vap);
   1628  1.203      yamt 	VN_KNOTE(dvp, NOTE_WRITE);
   1629  1.203      yamt 	if (error == 0 || error == EEXIST)
   1630  1.203      yamt 		cache_purge1(dvp, cnp, 0);
   1631    1.1       cgd 	return (error);
   1632    1.1       cgd }
   1633    1.1       cgd 
   1634  1.200  christos #ifndef NFS_V2_ONLY
   1635   1.59      fvdl static u_long create_verf;
   1636  1.200  christos #endif
   1637    1.1       cgd /*
   1638    1.1       cgd  * nfs file create call
   1639    1.1       cgd  */
   1640   1.32   mycroft int
   1641   1.58  christos nfs_create(v)
   1642   1.58  christos 	void *v;
   1643   1.58  christos {
   1644   1.32   mycroft 	struct vop_create_args /* {
   1645   1.32   mycroft 		struct vnode *a_dvp;
   1646   1.32   mycroft 		struct vnode **a_vpp;
   1647   1.32   mycroft 		struct componentname *a_cnp;
   1648   1.32   mycroft 		struct vattr *a_vap;
   1649   1.58  christos 	} */ *ap = v;
   1650  1.110  augustss 	struct vnode *dvp = ap->a_dvp;
   1651  1.110  augustss 	struct vattr *vap = ap->a_vap;
   1652  1.110  augustss 	struct componentname *cnp = ap->a_cnp;
   1653  1.110  augustss 	struct nfsv2_sattr *sp;
   1654  1.110  augustss 	u_int32_t *tl;
   1655  1.252  christos 	char *cp;
   1656  1.110  augustss 	int32_t t1, t2;
   1657  1.165  drochner 	struct nfsnode *dnp, *np = (struct nfsnode *)0;
   1658   1.59      fvdl 	struct vnode *newvp = (struct vnode *)0;
   1659  1.252  christos 	char *bpos, *dpos, *cp2;
   1660  1.103   thorpej 	int error, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
   1661  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
   1662  1.119     bjh21 	const int v3 = NFS_ISV3(dvp);
   1663   1.59      fvdl 
   1664   1.59      fvdl 	/*
   1665   1.59      fvdl 	 * Oops, not for me..
   1666   1.59      fvdl 	 */
   1667   1.59      fvdl 	if (vap->va_type == VSOCK)
   1668   1.59      fvdl 		return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
   1669    1.1       cgd 
   1670  1.204      yamt 	KASSERT(vap->va_type == VREG);
   1671  1.204      yamt 
   1672   1.59      fvdl #ifdef VA_EXCLUSIVE
   1673   1.59      fvdl 	if (vap->va_vaflags & VA_EXCLUSIVE)
   1674   1.59      fvdl 		fmode |= O_EXCL;
   1675   1.59      fvdl #endif
   1676   1.59      fvdl again:
   1677  1.103   thorpej 	error = 0;
   1678    1.1       cgd 	nfsstats.rpccnt[NFSPROC_CREATE]++;
   1679  1.165  drochner 	dnp = VTONFS(dvp);
   1680  1.165  drochner 	nfsm_reqhead(dnp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
   1681   1.59      fvdl 		nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
   1682  1.165  drochner 	nfsm_fhtom(dnp, v3);
   1683   1.32   mycroft 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1684  1.200  christos #ifndef NFS_V2_ONLY
   1685   1.59      fvdl 	if (v3) {
   1686   1.59      fvdl 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1687   1.59      fvdl 		if (fmode & O_EXCL) {
   1688  1.101      fvdl 			*tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
   1689  1.101      fvdl 			nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
   1690  1.122    itojun #ifdef INET
   1691  1.205      yamt 			if (TAILQ_FIRST(&in_ifaddrhead))
   1692  1.205      yamt 				*tl++ = TAILQ_FIRST(&in_ifaddrhead)->
   1693  1.205      yamt 				    ia_addr.sin_addr.s_addr;
   1694  1.101      fvdl 			else
   1695  1.101      fvdl 				*tl++ = create_verf;
   1696  1.122    itojun #else
   1697  1.122    itojun 			*tl++ = create_verf;
   1698  1.122    itojun #endif
   1699  1.101      fvdl 			*tl = ++create_verf;
   1700   1.59      fvdl 		} else {
   1701  1.101      fvdl 			*tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
   1702  1.251   thorpej 			nfsm_v3attrbuild(vap, false);
   1703   1.59      fvdl 		}
   1704  1.200  christos 	} else
   1705  1.200  christos #endif
   1706  1.200  christos 	{
   1707   1.59      fvdl 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   1708   1.59      fvdl 		sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
   1709   1.59      fvdl 		sp->sa_uid = nfs_xdrneg1;
   1710   1.59      fvdl 		sp->sa_gid = nfs_xdrneg1;
   1711   1.59      fvdl 		sp->sa_size = 0;
   1712   1.59      fvdl 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   1713   1.59      fvdl 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   1714   1.32   mycroft 	}
   1715  1.230  christos 	nfsm_request(dnp, NFSPROC_CREATE, cnp->cn_lwp, cnp->cn_cred);
   1716   1.59      fvdl 	if (!error) {
   1717  1.175      fvdl 		nfsm_mtofh(dvp, newvp, v3, gotvp);
   1718   1.59      fvdl 		if (!gotvp) {
   1719   1.59      fvdl 			error = nfs_lookitup(dvp, cnp->cn_nameptr,
   1720  1.230  christos 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_lwp, &np);
   1721   1.59      fvdl 			if (!error)
   1722   1.59      fvdl 				newvp = NFSTOV(np);
   1723   1.59      fvdl 		}
   1724   1.59      fvdl 	}
   1725  1.200  christos #ifndef NFS_V2_ONLY
   1726   1.59      fvdl 	if (v3)
   1727  1.216      yamt 		nfsm_wcc_data(dvp, wccflag, 0, !error);
   1728  1.200  christos #endif
   1729    1.1       cgd 	nfsm_reqdone;
   1730   1.59      fvdl 	if (error) {
   1731  1.238      yamt 		/*
   1732  1.238      yamt 		 * nfs_request maps NFSERR_NOTSUPP to ENOTSUP.
   1733  1.238      yamt 		 */
   1734  1.238      yamt 		if (v3 && (fmode & O_EXCL) && error == ENOTSUP) {
   1735   1.59      fvdl 			fmode &= ~O_EXCL;
   1736   1.59      fvdl 			goto again;
   1737   1.59      fvdl 		}
   1738  1.204      yamt 	} else if (v3 && (fmode & O_EXCL)) {
   1739  1.237    kardel 		struct timespec ts;
   1740  1.237    kardel 
   1741  1.237    kardel 		getnanotime(&ts);
   1742  1.204      yamt 
   1743  1.204      yamt 		/*
   1744  1.204      yamt 		 * make sure that we'll update timestamps as
   1745  1.204      yamt 		 * most server implementations use them to store
   1746  1.204      yamt 		 * the create verifier.
   1747  1.204      yamt 		 *
   1748  1.204      yamt 		 * XXX it's better to use TOSERVER always.
   1749  1.204      yamt 		 */
   1750  1.204      yamt 
   1751  1.237    kardel 		if (vap->va_atime.tv_sec == VNOVAL)
   1752  1.237    kardel 			vap->va_atime = ts;
   1753  1.237    kardel 		if (vap->va_mtime.tv_sec == VNOVAL)
   1754  1.237    kardel 			vap->va_mtime = ts;
   1755  1.204      yamt 
   1756  1.230  christos 		error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_lwp);
   1757  1.204      yamt 	}
   1758  1.203      yamt 	if (error == 0) {
   1759   1.59      fvdl 		if (cnp->cn_flags & MAKEENTRY)
   1760  1.188      yamt 			nfs_cache_enter(dvp, newvp, cnp);
   1761  1.203      yamt 		else
   1762  1.203      yamt 			cache_purge1(dvp, cnp, 0);
   1763   1.59      fvdl 		*ap->a_vpp = newvp;
   1764  1.203      yamt 	} else {
   1765  1.203      yamt 		if (newvp)
   1766  1.203      yamt 			vput(newvp);
   1767  1.203      yamt 		if (error == EEXIST)
   1768  1.203      yamt 			cache_purge1(dvp, cnp, 0);
   1769   1.59      fvdl 	}
   1770  1.117   thorpej 	PNBUF_PUT(cnp->cn_pnbuf);
   1771   1.32   mycroft 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1772   1.59      fvdl 	if (!wccflag)
   1773  1.185      yamt 		NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
   1774  1.156  jdolecek 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
   1775  1.128      fvdl 	vput(dvp);
   1776    1.1       cgd 	return (error);
   1777    1.1       cgd }
   1778    1.1       cgd 
   1779    1.1       cgd /*
   1780    1.1       cgd  * nfs file remove call
   1781    1.1       cgd  * To try and make nfs semantics closer to ufs semantics, a file that has
   1782    1.1       cgd  * other processes using the vnode is renamed instead of removed and then
   1783    1.1       cgd  * removed later on the last close.
   1784    1.1       cgd  * - If v_usecount > 1
   1785    1.1       cgd  *	  If a rename is not already in the works
   1786    1.1       cgd  *	     call nfs_sillyrename() to set it up
   1787    1.1       cgd  *     else
   1788    1.1       cgd  *	  do the remove rpc
   1789    1.1       cgd  */
   1790   1.32   mycroft int
   1791   1.58  christos nfs_remove(v)
   1792   1.58  christos 	void *v;
   1793   1.58  christos {
   1794   1.32   mycroft 	struct vop_remove_args /* {
   1795   1.32   mycroft 		struct vnodeop_desc *a_desc;
   1796   1.32   mycroft 		struct vnode * a_dvp;
   1797   1.32   mycroft 		struct vnode * a_vp;
   1798   1.32   mycroft 		struct componentname * a_cnp;
   1799   1.58  christos 	} */ *ap = v;
   1800  1.110  augustss 	struct vnode *vp = ap->a_vp;
   1801  1.110  augustss 	struct vnode *dvp = ap->a_dvp;
   1802  1.110  augustss 	struct componentname *cnp = ap->a_cnp;
   1803  1.110  augustss 	struct nfsnode *np = VTONFS(vp);
   1804    1.1       cgd 	int error = 0;
   1805   1.38        pk 	struct vattr vattr;
   1806    1.1       cgd 
   1807   1.59      fvdl #ifndef DIAGNOSTIC
   1808   1.59      fvdl 	if ((cnp->cn_flags & HASBUF) == 0)
   1809   1.59      fvdl 		panic("nfs_remove: no name");
   1810   1.59      fvdl 	if (vp->v_usecount < 1)
   1811   1.59      fvdl 		panic("nfs_remove: bad v_usecount");
   1812   1.59      fvdl #endif
   1813   1.77      fvdl 	if (vp->v_type == VDIR)
   1814   1.77      fvdl 		error = EPERM;
   1815   1.77      fvdl 	else if (vp->v_usecount == 1 || (np->n_sillyrename &&
   1816  1.230  christos 	    VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_lwp) == 0 &&
   1817   1.59      fvdl 	    vattr.va_nlink > 1)) {
   1818   1.32   mycroft 		/*
   1819   1.32   mycroft 		 * Purge the name cache so that the chance of a lookup for
   1820   1.32   mycroft 		 * the name succeeding while the remove is in progress is
   1821   1.32   mycroft 		 * minimized. Without node locking it can still happen, such
   1822   1.32   mycroft 		 * that an I/O op returns ESTALE, but since you get this if
   1823   1.32   mycroft 		 * another host removes the file..
   1824   1.32   mycroft 		 */
   1825   1.32   mycroft 		cache_purge(vp);
   1826   1.32   mycroft 		/*
   1827   1.59      fvdl 		 * throw away biocache buffers, mainly to avoid
   1828   1.59      fvdl 		 * unnecessary delayed writes later.
   1829   1.32   mycroft 		 */
   1830  1.230  christos 		error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, cnp->cn_lwp, 1);
   1831   1.32   mycroft 		/* Do the rpc */
   1832   1.59      fvdl 		if (error != EINTR)
   1833   1.59      fvdl 			error = nfs_removerpc(dvp, cnp->cn_nameptr,
   1834  1.230  christos 				cnp->cn_namelen, cnp->cn_cred, cnp->cn_lwp);
   1835   1.59      fvdl 	} else if (!np->n_sillyrename)
   1836  1.251   thorpej 		error = nfs_sillyrename(dvp, vp, cnp, false);
   1837  1.117   thorpej 	PNBUF_PUT(cnp->cn_pnbuf);
   1838  1.176      yamt 	if (!error && nfs_getattrcache(vp, &vattr) == 0 &&
   1839  1.176      yamt 	    vattr.va_nlink == 1) {
   1840  1.176      yamt 		np->n_flag |= NREMOVED;
   1841  1.176      yamt 	}
   1842  1.185      yamt 	NFS_INVALIDATE_ATTRCACHE(np);
   1843  1.156  jdolecek 	VN_KNOTE(vp, NOTE_DELETE);
   1844  1.156  jdolecek 	VN_KNOTE(dvp, NOTE_WRITE);
   1845  1.152   thorpej 	if (dvp == vp)
   1846  1.152   thorpej 		vrele(vp);
   1847  1.152   thorpej 	else
   1848  1.152   thorpej 		vput(vp);
   1849  1.128      fvdl 	vput(dvp);
   1850    1.1       cgd 	return (error);
   1851    1.1       cgd }
   1852    1.1       cgd 
   1853    1.1       cgd /*
   1854    1.1       cgd  * nfs file remove rpc called from nfs_inactive
   1855    1.1       cgd  */
   1856   1.32   mycroft int
   1857   1.32   mycroft nfs_removeit(sp)
   1858  1.110  augustss 	struct sillyrename *sp;
   1859    1.1       cgd {
   1860   1.59      fvdl 
   1861   1.59      fvdl 	return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
   1862  1.230  christos 		(struct lwp *)0));
   1863   1.59      fvdl }
   1864   1.59      fvdl 
   1865   1.59      fvdl /*
   1866   1.59      fvdl  * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
   1867   1.59      fvdl  */
   1868   1.59      fvdl int
   1869  1.230  christos nfs_removerpc(dvp, name, namelen, cred, l)
   1870  1.110  augustss 	struct vnode *dvp;
   1871   1.68       cgd 	const char *name;
   1872   1.59      fvdl 	int namelen;
   1873  1.236      elad 	kauth_cred_t cred;
   1874  1.230  christos 	struct lwp *l;
   1875   1.59      fvdl {
   1876  1.110  augustss 	u_int32_t *tl;
   1877  1.252  christos 	char *cp;
   1878  1.200  christos #ifndef NFS_V2_ONLY
   1879  1.200  christos 	int32_t t1;
   1880  1.252  christos 	char *cp2;
   1881  1.200  christos #endif
   1882  1.200  christos 	int32_t t2;
   1883  1.252  christos 	char *bpos, *dpos;
   1884   1.59      fvdl 	int error = 0, wccflag = NFSV3_WCCRATTR;
   1885  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
   1886  1.119     bjh21 	const int v3 = NFS_ISV3(dvp);
   1887  1.234  christos 	int rexmit = 0;
   1888  1.165  drochner 	struct nfsnode *dnp = VTONFS(dvp);
   1889    1.1       cgd 
   1890    1.1       cgd 	nfsstats.rpccnt[NFSPROC_REMOVE]++;
   1891  1.165  drochner 	nfsm_reqhead(dnp, NFSPROC_REMOVE,
   1892   1.59      fvdl 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
   1893  1.165  drochner 	nfsm_fhtom(dnp, v3);
   1894   1.59      fvdl 	nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
   1895  1.230  christos 	nfsm_request1(dnp, NFSPROC_REMOVE, l, cred, &rexmit);
   1896  1.200  christos #ifndef NFS_V2_ONLY
   1897   1.59      fvdl 	if (v3)
   1898  1.216      yamt 		nfsm_wcc_data(dvp, wccflag, 0, !error);
   1899  1.200  christos #endif
   1900    1.1       cgd 	nfsm_reqdone;
   1901   1.59      fvdl 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1902   1.59      fvdl 	if (!wccflag)
   1903  1.185      yamt 		NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
   1904  1.197      yamt 	/*
   1905  1.197      yamt 	 * Kludge City: If the first reply to the remove rpc is lost..
   1906  1.197      yamt 	 *   the reply to the retransmitted request will be ENOENT
   1907  1.197      yamt 	 *   since the file was in fact removed
   1908  1.197      yamt 	 *   Therefore, we cheat and return success.
   1909  1.197      yamt 	 */
   1910  1.197      yamt 	if (rexmit && error == ENOENT)
   1911  1.197      yamt 		error = 0;
   1912    1.1       cgd 	return (error);
   1913    1.1       cgd }
   1914    1.1       cgd 
   1915    1.1       cgd /*
   1916    1.1       cgd  * nfs file rename call
   1917    1.1       cgd  */
   1918   1.32   mycroft int
   1919   1.58  christos nfs_rename(v)
   1920   1.58  christos 	void *v;
   1921   1.58  christos {
   1922   1.32   mycroft 	struct vop_rename_args  /* {
   1923   1.32   mycroft 		struct vnode *a_fdvp;
   1924   1.32   mycroft 		struct vnode *a_fvp;
   1925   1.32   mycroft 		struct componentname *a_fcnp;
   1926   1.32   mycroft 		struct vnode *a_tdvp;
   1927   1.32   mycroft 		struct vnode *a_tvp;
   1928   1.32   mycroft 		struct componentname *a_tcnp;
   1929   1.58  christos 	} */ *ap = v;
   1930  1.111  augustss 	struct vnode *fvp = ap->a_fvp;
   1931  1.111  augustss 	struct vnode *tvp = ap->a_tvp;
   1932  1.111  augustss 	struct vnode *fdvp = ap->a_fdvp;
   1933  1.111  augustss 	struct vnode *tdvp = ap->a_tdvp;
   1934  1.111  augustss 	struct componentname *tcnp = ap->a_tcnp;
   1935  1.111  augustss 	struct componentname *fcnp = ap->a_fcnp;
   1936   1.59      fvdl 	int error;
   1937   1.17       cgd 
   1938   1.59      fvdl #ifndef DIAGNOSTIC
   1939   1.59      fvdl 	if ((tcnp->cn_flags & HASBUF) == 0 ||
   1940   1.59      fvdl 	    (fcnp->cn_flags & HASBUF) == 0)
   1941   1.59      fvdl 		panic("nfs_rename: no name");
   1942   1.59      fvdl #endif
   1943   1.17       cgd 	/* Check for cross-device rename */
   1944   1.17       cgd 	if ((fvp->v_mount != tdvp->v_mount) ||
   1945   1.17       cgd 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
   1946   1.17       cgd 		error = EXDEV;
   1947   1.17       cgd 		goto out;
   1948   1.17       cgd 	}
   1949    1.1       cgd 
   1950   1.59      fvdl 	/*
   1951   1.59      fvdl 	 * If the tvp exists and is in use, sillyrename it before doing the
   1952   1.59      fvdl 	 * rename of the new file over it.
   1953  1.240      yamt 	 *
   1954  1.240      yamt 	 * Have sillyrename use link instead of rename if possible,
   1955  1.240      yamt 	 * so that we don't lose the file if the rename fails, and so
   1956  1.240      yamt 	 * that there's no window when the "to" file doesn't exist.
   1957   1.59      fvdl 	 */
   1958   1.59      fvdl 	if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
   1959  1.251   thorpej 	    tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp, true)) {
   1960  1.156  jdolecek 		VN_KNOTE(tvp, NOTE_DELETE);
   1961  1.130     enami 		vput(tvp);
   1962   1.59      fvdl 		tvp = NULL;
   1963   1.59      fvdl 	}
   1964   1.59      fvdl 
   1965   1.59      fvdl 	error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
   1966   1.59      fvdl 		tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
   1967  1.230  christos 		tcnp->cn_lwp);
   1968   1.32   mycroft 
   1969  1.156  jdolecek 	VN_KNOTE(fdvp, NOTE_WRITE);
   1970  1.156  jdolecek 	VN_KNOTE(tdvp, NOTE_WRITE);
   1971  1.203      yamt 	if (error == 0 || error == EEXIST) {
   1972  1.203      yamt 		if (fvp->v_type == VDIR)
   1973  1.203      yamt 			cache_purge(fvp);
   1974  1.203      yamt 		else
   1975  1.203      yamt 			cache_purge1(fdvp, fcnp, 0);
   1976   1.32   mycroft 		if (tvp != NULL && tvp->v_type == VDIR)
   1977  1.203      yamt 			cache_purge(tvp);
   1978  1.203      yamt 		else
   1979  1.203      yamt 			cache_purge1(tdvp, tcnp, 0);
   1980    1.1       cgd 	}
   1981   1.17       cgd out:
   1982   1.32   mycroft 	if (tdvp == tvp)
   1983   1.32   mycroft 		vrele(tdvp);
   1984    1.1       cgd 	else
   1985   1.32   mycroft 		vput(tdvp);
   1986   1.32   mycroft 	if (tvp)
   1987   1.32   mycroft 		vput(tvp);
   1988   1.32   mycroft 	vrele(fdvp);
   1989   1.32   mycroft 	vrele(fvp);
   1990    1.1       cgd 	return (error);
   1991    1.1       cgd }
   1992    1.1       cgd 
   1993    1.1       cgd /*
   1994    1.1       cgd  * nfs file rename rpc called from nfs_remove() above
   1995    1.1       cgd  */
   1996   1.32   mycroft int
   1997   1.32   mycroft nfs_renameit(sdvp, scnp, sp)
   1998   1.32   mycroft 	struct vnode *sdvp;
   1999   1.32   mycroft 	struct componentname *scnp;
   2000  1.111  augustss 	struct sillyrename *sp;
   2001    1.1       cgd {
   2002   1.59      fvdl 	return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
   2003  1.230  christos 		sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_lwp));
   2004   1.59      fvdl }
   2005   1.59      fvdl 
   2006   1.59      fvdl /*
   2007   1.59      fvdl  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
   2008   1.59      fvdl  */
   2009   1.59      fvdl int
   2010  1.230  christos nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, l)
   2011  1.111  augustss 	struct vnode *fdvp;
   2012   1.68       cgd 	const char *fnameptr;
   2013   1.59      fvdl 	int fnamelen;
   2014  1.111  augustss 	struct vnode *tdvp;
   2015   1.68       cgd 	const char *tnameptr;
   2016   1.59      fvdl 	int tnamelen;
   2017  1.236      elad 	kauth_cred_t cred;
   2018  1.230  christos 	struct lwp *l;
   2019   1.59      fvdl {
   2020  1.111  augustss 	u_int32_t *tl;
   2021  1.252  christos 	char *cp;
   2022  1.200  christos #ifndef NFS_V2_ONLY
   2023  1.200  christos 	int32_t t1;
   2024  1.252  christos 	char *cp2;
   2025  1.200  christos #endif
   2026  1.200  christos 	int32_t t2;
   2027  1.252  christos 	char *bpos, *dpos;
   2028   1.59      fvdl 	int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
   2029  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
   2030  1.119     bjh21 	const int v3 = NFS_ISV3(fdvp);
   2031  1.234  christos 	int rexmit = 0;
   2032  1.165  drochner 	struct nfsnode *fdnp = VTONFS(fdvp);
   2033    1.1       cgd 
   2034    1.1       cgd 	nfsstats.rpccnt[NFSPROC_RENAME]++;
   2035  1.165  drochner 	nfsm_reqhead(fdnp, NFSPROC_RENAME,
   2036   1.59      fvdl 		(NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
   2037   1.59      fvdl 		nfsm_rndup(tnamelen));
   2038  1.165  drochner 	nfsm_fhtom(fdnp, v3);
   2039   1.59      fvdl 	nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
   2040  1.165  drochner 	nfsm_fhtom(VTONFS(tdvp), v3);
   2041   1.59      fvdl 	nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
   2042  1.230  christos 	nfsm_request1(fdnp, NFSPROC_RENAME, l, cred, &rexmit);
   2043  1.200  christos #ifndef NFS_V2_ONLY
   2044   1.59      fvdl 	if (v3) {
   2045  1.216      yamt 		nfsm_wcc_data(fdvp, fwccflag, 0, !error);
   2046  1.216      yamt 		nfsm_wcc_data(tdvp, twccflag, 0, !error);
   2047   1.59      fvdl 	}
   2048  1.200  christos #endif
   2049    1.1       cgd 	nfsm_reqdone;
   2050   1.59      fvdl 	VTONFS(fdvp)->n_flag |= NMODIFIED;
   2051   1.59      fvdl 	VTONFS(tdvp)->n_flag |= NMODIFIED;
   2052   1.59      fvdl 	if (!fwccflag)
   2053  1.185      yamt 		NFS_INVALIDATE_ATTRCACHE(VTONFS(fdvp));
   2054   1.59      fvdl 	if (!twccflag)
   2055  1.185      yamt 		NFS_INVALIDATE_ATTRCACHE(VTONFS(tdvp));
   2056  1.197      yamt 	/*
   2057  1.197      yamt 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
   2058  1.197      yamt 	 */
   2059  1.197      yamt 	if (rexmit && error == ENOENT)
   2060  1.197      yamt 		error = 0;
   2061    1.1       cgd 	return (error);
   2062    1.1       cgd }
   2063    1.1       cgd 
   2064  1.240      yamt /*
   2065  1.240      yamt  * NFS link RPC, called from nfs_link.
   2066  1.240      yamt  * Assumes dvp and vp locked, and leaves them that way.
   2067  1.240      yamt  */
   2068  1.240      yamt 
   2069  1.239      yamt static int
   2070  1.239      yamt nfs_linkrpc(struct vnode *dvp, struct vnode *vp, const char *name,
   2071  1.239      yamt     size_t namelen, kauth_cred_t cred, struct lwp *l)
   2072  1.239      yamt {
   2073  1.239      yamt 	u_int32_t *tl;
   2074  1.252  christos 	char *cp;
   2075  1.239      yamt #ifndef NFS_V2_ONLY
   2076  1.239      yamt 	int32_t t1;
   2077  1.252  christos 	char *cp2;
   2078  1.239      yamt #endif
   2079  1.239      yamt 	int32_t t2;
   2080  1.252  christos 	char *bpos, *dpos;
   2081  1.239      yamt 	int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
   2082  1.239      yamt 	struct mbuf *mreq, *mrep, *md, *mb;
   2083  1.239      yamt 	const int v3 = NFS_ISV3(dvp);
   2084  1.239      yamt 	int rexmit = 0;
   2085  1.239      yamt 	struct nfsnode *np = VTONFS(vp);
   2086  1.239      yamt 
   2087  1.239      yamt 	nfsstats.rpccnt[NFSPROC_LINK]++;
   2088  1.239      yamt 	nfsm_reqhead(np, NFSPROC_LINK,
   2089  1.239      yamt 	    NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(namelen));
   2090  1.239      yamt 	nfsm_fhtom(np, v3);
   2091  1.239      yamt 	nfsm_fhtom(VTONFS(dvp), v3);
   2092  1.239      yamt 	nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
   2093  1.239      yamt 	nfsm_request1(np, NFSPROC_LINK, l, cred, &rexmit);
   2094  1.239      yamt #ifndef NFS_V2_ONLY
   2095  1.239      yamt 	if (v3) {
   2096  1.239      yamt 		nfsm_postop_attr(vp, attrflag, 0);
   2097  1.239      yamt 		nfsm_wcc_data(dvp, wccflag, 0, !error);
   2098  1.239      yamt 	}
   2099  1.239      yamt #endif
   2100  1.239      yamt 	nfsm_reqdone;
   2101  1.239      yamt 
   2102  1.239      yamt 	VTONFS(dvp)->n_flag |= NMODIFIED;
   2103  1.239      yamt 	if (!attrflag)
   2104  1.239      yamt 		NFS_INVALIDATE_ATTRCACHE(VTONFS(vp));
   2105  1.239      yamt 	if (!wccflag)
   2106  1.239      yamt 		NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
   2107  1.239      yamt 
   2108  1.239      yamt 	/*
   2109  1.239      yamt 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
   2110  1.239      yamt 	 */
   2111  1.239      yamt 	if (rexmit && error == EEXIST)
   2112  1.239      yamt 		error = 0;
   2113  1.239      yamt 
   2114  1.239      yamt 	return error;
   2115  1.239      yamt }
   2116  1.239      yamt 
   2117    1.1       cgd /*
   2118    1.1       cgd  * nfs hard link create call
   2119    1.1       cgd  */
   2120   1.32   mycroft int
   2121   1.58  christos nfs_link(v)
   2122   1.58  christos 	void *v;
   2123   1.58  christos {
   2124   1.32   mycroft 	struct vop_link_args /* {
   2125   1.57   mycroft 		struct vnode *a_dvp;
   2126   1.32   mycroft 		struct vnode *a_vp;
   2127   1.32   mycroft 		struct componentname *a_cnp;
   2128   1.58  christos 	} */ *ap = v;
   2129  1.111  augustss 	struct vnode *vp = ap->a_vp;
   2130  1.111  augustss 	struct vnode *dvp = ap->a_dvp;
   2131  1.111  augustss 	struct componentname *cnp = ap->a_cnp;
   2132  1.239      yamt 	int error = 0;
   2133    1.1       cgd 
   2134   1.57   mycroft 	if (dvp->v_mount != vp->v_mount) {
   2135  1.144  christos 		VOP_ABORTOP(dvp, cnp);
   2136   1.71      fvdl 		vput(dvp);
   2137   1.17       cgd 		return (EXDEV);
   2138   1.17       cgd 	}
   2139  1.132      fvdl 	if (dvp != vp) {
   2140  1.132      fvdl 		error = vn_lock(vp, LK_EXCLUSIVE);
   2141  1.132      fvdl 		if (error != 0) {
   2142  1.132      fvdl 			VOP_ABORTOP(dvp, cnp);
   2143  1.132      fvdl 			vput(dvp);
   2144  1.132      fvdl 			return error;
   2145  1.132      fvdl 		}
   2146  1.132      fvdl 	}
   2147   1.32   mycroft 
   2148   1.36   mycroft 	/*
   2149   1.36   mycroft 	 * Push all writes to the server, so that the attribute cache
   2150   1.36   mycroft 	 * doesn't get "out of sync" with the server.
   2151   1.36   mycroft 	 * XXX There should be a better way!
   2152   1.36   mycroft 	 */
   2153  1.230  christos 	VOP_FSYNC(vp, cnp->cn_cred, FSYNC_WAIT, 0, 0, cnp->cn_lwp);
   2154   1.36   mycroft 
   2155  1.239      yamt 	error = nfs_linkrpc(dvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
   2156  1.239      yamt 	    cnp->cn_cred, cnp->cn_lwp);
   2157  1.239      yamt 
   2158  1.239      yamt 	if (error == 0)
   2159  1.203      yamt 		cache_purge1(dvp, cnp, 0);
   2160  1.117   thorpej 	PNBUF_PUT(cnp->cn_pnbuf);
   2161  1.132      fvdl 	if (dvp != vp)
   2162  1.132      fvdl 		VOP_UNLOCK(vp, 0);
   2163  1.156  jdolecek 	VN_KNOTE(vp, NOTE_LINK);
   2164  1.156  jdolecek 	VN_KNOTE(dvp, NOTE_WRITE);
   2165   1.59      fvdl 	vput(dvp);
   2166    1.1       cgd 	return (error);
   2167    1.1       cgd }
   2168    1.1       cgd 
   2169    1.1       cgd /*
   2170    1.1       cgd  * nfs symbolic link create call
   2171    1.1       cgd  */
   2172   1.32   mycroft int
   2173   1.58  christos nfs_symlink(v)
   2174   1.58  christos 	void *v;
   2175   1.58  christos {
   2176   1.32   mycroft 	struct vop_symlink_args /* {
   2177   1.32   mycroft 		struct vnode *a_dvp;
   2178   1.32   mycroft 		struct vnode **a_vpp;
   2179   1.32   mycroft 		struct componentname *a_cnp;
   2180   1.32   mycroft 		struct vattr *a_vap;
   2181   1.32   mycroft 		char *a_target;
   2182   1.58  christos 	} */ *ap = v;
   2183  1.111  augustss 	struct vnode *dvp = ap->a_dvp;
   2184  1.111  augustss 	struct vattr *vap = ap->a_vap;
   2185  1.111  augustss 	struct componentname *cnp = ap->a_cnp;
   2186  1.111  augustss 	struct nfsv2_sattr *sp;
   2187  1.111  augustss 	u_int32_t *tl;
   2188  1.252  christos 	char *cp;
   2189  1.111  augustss 	int32_t t1, t2;
   2190  1.252  christos 	char *bpos, *dpos, *cp2;
   2191   1.59      fvdl 	int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
   2192  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
   2193   1.59      fvdl 	struct vnode *newvp = (struct vnode *)0;
   2194  1.119     bjh21 	const int v3 = NFS_ISV3(dvp);
   2195  1.233  christos 	int rexmit = 0;
   2196  1.165  drochner 	struct nfsnode *dnp = VTONFS(dvp);
   2197    1.1       cgd 
   2198  1.135     assar 	*ap->a_vpp = NULL;
   2199    1.1       cgd 	nfsstats.rpccnt[NFSPROC_SYMLINK]++;
   2200   1.32   mycroft 	slen = strlen(ap->a_target);
   2201  1.165  drochner 	nfsm_reqhead(dnp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
   2202   1.59      fvdl 	    nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
   2203  1.165  drochner 	nfsm_fhtom(dnp, v3);
   2204   1.32   mycroft 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   2205  1.200  christos #ifndef NFS_V2_ONlY
   2206  1.101      fvdl 	if (v3)
   2207  1.251   thorpej 		nfsm_v3attrbuild(vap, false);
   2208  1.200  christos #endif
   2209   1.32   mycroft 	nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
   2210  1.200  christos #ifndef NFS_V2_ONlY
   2211   1.59      fvdl 	if (!v3) {
   2212   1.59      fvdl 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   2213   1.59      fvdl 		sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
   2214   1.59      fvdl 		sp->sa_uid = nfs_xdrneg1;
   2215   1.59      fvdl 		sp->sa_gid = nfs_xdrneg1;
   2216   1.59      fvdl 		sp->sa_size = nfs_xdrneg1;
   2217   1.59      fvdl 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   2218   1.59      fvdl 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   2219   1.32   mycroft 	}
   2220  1.200  christos #endif
   2221  1.230  christos 	nfsm_request1(dnp, NFSPROC_SYMLINK, cnp->cn_lwp, cnp->cn_cred,
   2222  1.197      yamt 	    &rexmit);
   2223  1.200  christos #ifndef NFS_V2_ONlY
   2224   1.59      fvdl 	if (v3) {
   2225   1.59      fvdl 		if (!error)
   2226  1.175      fvdl 			nfsm_mtofh(dvp, newvp, v3, gotvp);
   2227  1.216      yamt 		nfsm_wcc_data(dvp, wccflag, 0, !error);
   2228   1.59      fvdl 	}
   2229  1.200  christos #endif
   2230    1.1       cgd 	nfsm_reqdone;
   2231  1.135     assar 	/*
   2232  1.135     assar 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
   2233  1.135     assar 	 */
   2234  1.197      yamt 	if (rexmit && error == EEXIST)
   2235  1.135     assar 		error = 0;
   2236  1.203      yamt 	if (error == 0 || error == EEXIST)
   2237  1.203      yamt 		cache_purge1(dvp, cnp, 0);
   2238  1.135     assar 	if (error == 0 && newvp == NULL) {
   2239  1.135     assar 		struct nfsnode *np = NULL;
   2240  1.135     assar 
   2241  1.135     assar 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
   2242  1.230  christos 		    cnp->cn_cred, cnp->cn_lwp, &np);
   2243  1.135     assar 		if (error == 0)
   2244  1.135     assar 			newvp = NFSTOV(np);
   2245  1.135     assar 	}
   2246  1.135     assar 	if (error) {
   2247  1.135     assar 		if (newvp != NULL)
   2248  1.135     assar 			vput(newvp);
   2249  1.135     assar 	} else {
   2250  1.135     assar 		*ap->a_vpp = newvp;
   2251  1.135     assar 	}
   2252  1.117   thorpej 	PNBUF_PUT(cnp->cn_pnbuf);
   2253   1.32   mycroft 	VTONFS(dvp)->n_flag |= NMODIFIED;
   2254   1.59      fvdl 	if (!wccflag)
   2255  1.185      yamt 		NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
   2256  1.156  jdolecek 	VN_KNOTE(dvp, NOTE_WRITE);
   2257  1.128      fvdl 	vput(dvp);
   2258    1.1       cgd 	return (error);
   2259    1.1       cgd }
   2260    1.1       cgd 
   2261    1.1       cgd /*
   2262    1.1       cgd  * nfs make dir call
   2263    1.1       cgd  */
   2264   1.32   mycroft int
   2265   1.58  christos nfs_mkdir(v)
   2266   1.58  christos 	void *v;
   2267   1.58  christos {
   2268   1.32   mycroft 	struct vop_mkdir_args /* {
   2269   1.32   mycroft 		struct vnode *a_dvp;
   2270   1.32   mycroft 		struct vnode **a_vpp;
   2271   1.32   mycroft 		struct componentname *a_cnp;
   2272   1.32   mycroft 		struct vattr *a_vap;
   2273   1.58  christos 	} */ *ap = v;
   2274  1.111  augustss 	struct vnode *dvp = ap->a_dvp;
   2275  1.111  augustss 	struct vattr *vap = ap->a_vap;
   2276  1.111  augustss 	struct componentname *cnp = ap->a_cnp;
   2277  1.111  augustss 	struct nfsv2_sattr *sp;
   2278  1.111  augustss 	u_int32_t *tl;
   2279  1.252  christos 	char *cp;
   2280  1.111  augustss 	int32_t t1, t2;
   2281  1.111  augustss 	int len;
   2282  1.165  drochner 	struct nfsnode *dnp = VTONFS(dvp), *np = (struct nfsnode *)0;
   2283   1.59      fvdl 	struct vnode *newvp = (struct vnode *)0;
   2284  1.252  christos 	char *bpos, *dpos, *cp2;
   2285   1.59      fvdl 	int error = 0, wccflag = NFSV3_WCCRATTR;
   2286   1.59      fvdl 	int gotvp = 0;
   2287  1.234  christos 	int rexmit = 0;
   2288  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
   2289  1.119     bjh21 	const int v3 = NFS_ISV3(dvp);
   2290    1.1       cgd 
   2291   1.32   mycroft 	len = cnp->cn_namelen;
   2292    1.1       cgd 	nfsstats.rpccnt[NFSPROC_MKDIR]++;
   2293  1.165  drochner 	nfsm_reqhead(dnp, NFSPROC_MKDIR,
   2294   1.59      fvdl 	  NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
   2295  1.165  drochner 	nfsm_fhtom(dnp, v3);
   2296   1.32   mycroft 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
   2297  1.200  christos #ifndef NFS_V2_ONLY
   2298   1.59      fvdl 	if (v3) {
   2299  1.251   thorpej 		nfsm_v3attrbuild(vap, false);
   2300  1.200  christos 	} else
   2301  1.200  christos #endif
   2302  1.200  christos 	{
   2303   1.59      fvdl 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   2304   1.59      fvdl 		sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
   2305   1.59      fvdl 		sp->sa_uid = nfs_xdrneg1;
   2306   1.59      fvdl 		sp->sa_gid = nfs_xdrneg1;
   2307   1.59      fvdl 		sp->sa_size = nfs_xdrneg1;
   2308   1.59      fvdl 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   2309   1.59      fvdl 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   2310   1.32   mycroft 	}
   2311  1.230  christos 	nfsm_request1(dnp, NFSPROC_MKDIR, cnp->cn_lwp, cnp->cn_cred, &rexmit);
   2312   1.59      fvdl 	if (!error)
   2313  1.175      fvdl 		nfsm_mtofh(dvp, newvp, v3, gotvp);
   2314   1.59      fvdl 	if (v3)
   2315  1.216      yamt 		nfsm_wcc_data(dvp, wccflag, 0, !error);
   2316    1.1       cgd 	nfsm_reqdone;
   2317   1.32   mycroft 	VTONFS(dvp)->n_flag |= NMODIFIED;
   2318   1.59      fvdl 	if (!wccflag)
   2319  1.185      yamt 		NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
   2320    1.1       cgd 	/*
   2321    1.1       cgd 	 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
   2322    1.1       cgd 	 * if we can succeed in looking up the directory.
   2323    1.1       cgd 	 */
   2324  1.197      yamt 	if ((rexmit && error == EEXIST) || (!error && !gotvp)) {
   2325   1.59      fvdl 		if (newvp) {
   2326  1.128      fvdl 			vput(newvp);
   2327   1.59      fvdl 			newvp = (struct vnode *)0;
   2328   1.59      fvdl 		}
   2329   1.59      fvdl 		error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
   2330  1.230  christos 			cnp->cn_lwp, &np);
   2331   1.59      fvdl 		if (!error) {
   2332   1.59      fvdl 			newvp = NFSTOV(np);
   2333  1.195      yamt 			if (newvp->v_type != VDIR || newvp == dvp)
   2334   1.59      fvdl 				error = EEXIST;
   2335    1.1       cgd 		}
   2336    1.1       cgd 	}
   2337   1.59      fvdl 	if (error) {
   2338  1.195      yamt 		if (newvp) {
   2339  1.195      yamt 			if (dvp != newvp)
   2340  1.195      yamt 				vput(newvp);
   2341  1.195      yamt 			else
   2342  1.195      yamt 				vrele(newvp);
   2343  1.195      yamt 		}
   2344   1.82      fvdl 	} else {
   2345  1.156  jdolecek 		VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
   2346   1.82      fvdl 		if (cnp->cn_flags & MAKEENTRY)
   2347  1.188      yamt 			nfs_cache_enter(dvp, newvp, cnp);
   2348   1.59      fvdl 		*ap->a_vpp = newvp;
   2349   1.82      fvdl 	}
   2350  1.117   thorpej 	PNBUF_PUT(cnp->cn_pnbuf);
   2351  1.128      fvdl 	vput(dvp);
   2352    1.1       cgd 	return (error);
   2353    1.1       cgd }
   2354    1.1       cgd 
   2355    1.1       cgd /*
   2356    1.1       cgd  * nfs remove directory call
   2357    1.1       cgd  */
   2358   1.32   mycroft int
   2359   1.58  christos nfs_rmdir(v)
   2360   1.58  christos 	void *v;
   2361   1.58  christos {
   2362   1.32   mycroft 	struct vop_rmdir_args /* {
   2363   1.32   mycroft 		struct vnode *a_dvp;
   2364   1.32   mycroft 		struct vnode *a_vp;
   2365   1.32   mycroft 		struct componentname *a_cnp;
   2366   1.58  christos 	} */ *ap = v;
   2367  1.111  augustss 	struct vnode *vp = ap->a_vp;
   2368  1.111  augustss 	struct vnode *dvp = ap->a_dvp;
   2369  1.111  augustss 	struct componentname *cnp = ap->a_cnp;
   2370  1.111  augustss 	u_int32_t *tl;
   2371  1.252  christos 	char *cp;
   2372  1.200  christos #ifndef NFS_V2_ONLY
   2373  1.200  christos 	int32_t t1;
   2374  1.252  christos 	char *cp2;
   2375  1.200  christos #endif
   2376  1.200  christos 	int32_t t2;
   2377  1.252  christos 	char *bpos, *dpos;
   2378   1.59      fvdl 	int error = 0, wccflag = NFSV3_WCCRATTR;
   2379  1.234  christos 	int rexmit = 0;
   2380  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
   2381  1.119     bjh21 	const int v3 = NFS_ISV3(dvp);
   2382  1.165  drochner 	struct nfsnode *dnp;
   2383    1.1       cgd 
   2384   1.32   mycroft 	if (dvp == vp) {
   2385   1.32   mycroft 		vrele(dvp);
   2386  1.129      fvdl 		vput(dvp);
   2387  1.117   thorpej 		PNBUF_PUT(cnp->cn_pnbuf);
   2388    1.1       cgd 		return (EINVAL);
   2389    1.1       cgd 	}
   2390    1.1       cgd 	nfsstats.rpccnt[NFSPROC_RMDIR]++;
   2391  1.165  drochner 	dnp = VTONFS(dvp);
   2392  1.165  drochner 	nfsm_reqhead(dnp, NFSPROC_RMDIR,
   2393   1.59      fvdl 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
   2394  1.165  drochner 	nfsm_fhtom(dnp, v3);
   2395   1.32   mycroft 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   2396  1.230  christos 	nfsm_request1(dnp, NFSPROC_RMDIR, cnp->cn_lwp, cnp->cn_cred, &rexmit);
   2397  1.200  christos #ifndef NFS_V2_ONLY
   2398   1.59      fvdl 	if (v3)
   2399  1.216      yamt 		nfsm_wcc_data(dvp, wccflag, 0, !error);
   2400  1.200  christos #endif
   2401    1.1       cgd 	nfsm_reqdone;
   2402  1.117   thorpej 	PNBUF_PUT(cnp->cn_pnbuf);
   2403   1.32   mycroft 	VTONFS(dvp)->n_flag |= NMODIFIED;
   2404   1.59      fvdl 	if (!wccflag)
   2405  1.185      yamt 		NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
   2406  1.156  jdolecek 	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
   2407  1.156  jdolecek 	VN_KNOTE(vp, NOTE_DELETE);
   2408   1.32   mycroft 	cache_purge(vp);
   2409  1.129      fvdl 	vput(vp);
   2410  1.129      fvdl 	vput(dvp);
   2411    1.1       cgd 	/*
   2412    1.1       cgd 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
   2413    1.1       cgd 	 */
   2414  1.197      yamt 	if (rexmit && error == ENOENT)
   2415    1.1       cgd 		error = 0;
   2416    1.1       cgd 	return (error);
   2417    1.1       cgd }
   2418    1.1       cgd 
   2419    1.1       cgd /*
   2420    1.1       cgd  * nfs readdir call
   2421    1.1       cgd  */
   2422   1.32   mycroft int
   2423   1.58  christos nfs_readdir(v)
   2424   1.58  christos 	void *v;
   2425   1.58  christos {
   2426   1.32   mycroft 	struct vop_readdir_args /* {
   2427   1.32   mycroft 		struct vnode *a_vp;
   2428   1.32   mycroft 		struct uio *a_uio;
   2429  1.236      elad 		kauth_cred_t a_cred;
   2430   1.32   mycroft 		int *a_eofflag;
   2431   1.89      fvdl 		off_t **a_cookies;
   2432   1.89      fvdl 		int *a_ncookies;
   2433   1.58  christos 	} */ *ap = v;
   2434  1.111  augustss 	struct vnode *vp = ap->a_vp;
   2435  1.111  augustss 	struct uio *uio = ap->a_uio;
   2436   1.85      fvdl 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2437   1.52   ghudson 	char *base = uio->uio_iov->iov_base;
   2438    1.1       cgd 	int tresid, error;
   2439   1.81      fvdl 	size_t count, lost;
   2440   1.90      fvdl 	struct dirent *dp;
   2441   1.90      fvdl 	off_t *cookies = NULL;
   2442   1.90      fvdl 	int ncookies = 0, nc;
   2443   1.32   mycroft 
   2444    1.1       cgd 	if (vp->v_type != VDIR)
   2445    1.1       cgd 		return (EPERM);
   2446   1.81      fvdl 
   2447   1.81      fvdl 	lost = uio->uio_resid & (NFS_DIRFRAGSIZ - 1);
   2448   1.81      fvdl 	count = uio->uio_resid - lost;
   2449   1.81      fvdl 	if (count <= 0)
   2450   1.81      fvdl 		return (EINVAL);
   2451   1.81      fvdl 
   2452    1.1       cgd 	/*
   2453    1.1       cgd 	 * Call nfs_bioread() to do the real work.
   2454    1.1       cgd 	 */
   2455   1.81      fvdl 	tresid = uio->uio_resid = count;
   2456   1.81      fvdl 	error = nfs_bioread(vp, uio, 0, ap->a_cred,
   2457   1.81      fvdl 		    ap->a_cookies ? NFSBIO_CACHECOOKIES : 0);
   2458    1.1       cgd 
   2459   1.90      fvdl 	if (!error && ap->a_cookies) {
   2460   1.90      fvdl 		ncookies = count / 16;
   2461  1.116   thorpej 		cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK);
   2462   1.90      fvdl 		*ap->a_cookies = cookies;
   2463   1.90      fvdl 	}
   2464   1.90      fvdl 
   2465   1.59      fvdl 	if (!error && uio->uio_resid == tresid) {
   2466   1.81      fvdl 		uio->uio_resid += lost;
   2467    1.1       cgd 		nfsstats.direofcache_misses++;
   2468   1.90      fvdl 		if (ap->a_cookies)
   2469   1.90      fvdl 			*ap->a_ncookies = 0;
   2470   1.59      fvdl 		*ap->a_eofflag = 1;
   2471   1.59      fvdl 		return (0);
   2472   1.59      fvdl 	}
   2473   1.52   ghudson 
   2474   1.52   ghudson 	if (!error && ap->a_cookies) {
   2475   1.52   ghudson 		/*
   2476   1.52   ghudson 		 * Only the NFS server and emulations use cookies, and they
   2477   1.52   ghudson 		 * load the directory block into system space, so we can
   2478   1.52   ghudson 		 * just look at it directly.
   2479   1.52   ghudson 		 */
   2480  1.231      yamt 		if (!VMSPACE_IS_KERNEL_P(uio->uio_vmspace) ||
   2481  1.231      yamt 		    uio->uio_iovcnt != 1)
   2482   1.59      fvdl 			panic("nfs_readdir: lost in space");
   2483   1.91    kleink 		for (nc = 0; ncookies-- &&
   2484   1.91    kleink 		     base < (char *)uio->uio_iov->iov_base; nc++){
   2485   1.52   ghudson 			dp = (struct dirent *) base;
   2486   1.52   ghudson 			if (dp->d_reclen == 0)
   2487   1.52   ghudson 				break;
   2488   1.85      fvdl 			if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
   2489   1.85      fvdl 				*(cookies++) = (off_t)NFS_GETCOOKIE32(dp);
   2490   1.85      fvdl 			else
   2491   1.85      fvdl 				*(cookies++) = NFS_GETCOOKIE(dp);
   2492   1.52   ghudson 			base += dp->d_reclen;
   2493   1.52   ghudson 		}
   2494   1.91    kleink 		uio->uio_resid +=
   2495  1.252  christos 		    ((char *)uio->uio_iov->iov_base - base);
   2496   1.91    kleink 		uio->uio_iov->iov_len +=
   2497  1.252  christos 		    ((char *)uio->uio_iov->iov_base - base);
   2498   1.52   ghudson 		uio->uio_iov->iov_base = base;
   2499   1.90      fvdl 		*ap->a_ncookies = nc;
   2500   1.52   ghudson 	}
   2501   1.52   ghudson 
   2502   1.81      fvdl 	uio->uio_resid += lost;
   2503   1.59      fvdl 	*ap->a_eofflag = 0;
   2504    1.1       cgd 	return (error);
   2505    1.1       cgd }
   2506    1.1       cgd 
   2507    1.1       cgd /*
   2508    1.1       cgd  * Readdir rpc call.
   2509    1.1       cgd  * Called from below the buffer cache by nfs_doio().
   2510    1.1       cgd  */
   2511   1.32   mycroft int
   2512    1.1       cgd nfs_readdirrpc(vp, uiop, cred)
   2513   1.59      fvdl 	struct vnode *vp;
   2514  1.111  augustss 	struct uio *uiop;
   2515  1.236      elad 	kauth_cred_t cred;
   2516    1.1       cgd {
   2517  1.111  augustss 	int len, left;
   2518  1.111  augustss 	struct dirent *dp = NULL;
   2519  1.111  augustss 	u_int32_t *tl;
   2520  1.252  christos 	char *cp;
   2521  1.111  augustss 	int32_t t1, t2;
   2522  1.252  christos 	char *bpos, *dpos, *cp2;
   2523  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
   2524   1.59      fvdl 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2525   1.59      fvdl 	struct nfsnode *dnp = VTONFS(vp);
   2526   1.59      fvdl 	u_quad_t fileno;
   2527  1.225  christos 	int error = 0, more_dirs = 1, blksiz = 0, bigenough = 1;
   2528  1.200  christos #ifndef NFS_V2_ONLY
   2529  1.200  christos 	int attrflag;
   2530  1.200  christos #endif
   2531  1.200  christos 	int nrpcs = 0, reclen;
   2532  1.119     bjh21 	const int v3 = NFS_ISV3(vp);
   2533   1.59      fvdl 
   2534   1.81      fvdl #ifdef DIAGNOSTIC
   2535   1.81      fvdl 	/*
   2536   1.81      fvdl 	 * Should be called from buffer cache, so only amount of
   2537   1.81      fvdl 	 * NFS_DIRBLKSIZ will be requested.
   2538   1.81      fvdl 	 */
   2539  1.219      yamt 	if (uiop->uio_iovcnt != 1 || uiop->uio_resid != NFS_DIRBLKSIZ)
   2540   1.59      fvdl 		panic("nfs readdirrpc bad uio");
   2541   1.59      fvdl #endif
   2542    1.1       cgd 
   2543    1.1       cgd 	/*
   2544   1.59      fvdl 	 * Loop around doing readdir rpc's of size nm_readdirsize
   2545   1.81      fvdl 	 * truncated to a multiple of NFS_DIRFRAGSIZ.
   2546    1.1       cgd 	 * The stopping criteria is EOF or buffer full.
   2547    1.1       cgd 	 */
   2548   1.59      fvdl 	while (more_dirs && bigenough) {
   2549   1.85      fvdl 		/*
   2550   1.85      fvdl 		 * Heuristic: don't bother to do another RPC to further
   2551   1.85      fvdl 		 * fill up this block if there is not much room left. (< 50%
   2552   1.85      fvdl 		 * of the readdir RPC size). This wastes some buffer space
   2553   1.85      fvdl 		 * but can save up to 50% in RPC calls.
   2554   1.85      fvdl 		 */
   2555   1.85      fvdl 		if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
   2556   1.85      fvdl 			bigenough = 0;
   2557   1.85      fvdl 			break;
   2558   1.85      fvdl 		}
   2559    1.1       cgd 		nfsstats.rpccnt[NFSPROC_READDIR]++;
   2560  1.165  drochner 		nfsm_reqhead(dnp, NFSPROC_READDIR, NFSX_FH(v3) +
   2561   1.59      fvdl 			NFSX_READDIR(v3));
   2562  1.165  drochner 		nfsm_fhtom(dnp, v3);
   2563  1.200  christos #ifndef NFS_V2_ONLY
   2564   1.59      fvdl 		if (v3) {
   2565   1.59      fvdl 			nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
   2566   1.81      fvdl 			if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
   2567   1.81      fvdl 				txdr_swapcookie3(uiop->uio_offset, tl);
   2568   1.81      fvdl 			} else {
   2569   1.81      fvdl 				txdr_cookie3(uiop->uio_offset, tl);
   2570   1.81      fvdl 			}
   2571   1.81      fvdl 			tl += 2;
   2572   1.59      fvdl 			*tl++ = dnp->n_cookieverf.nfsuquad[0];
   2573   1.59      fvdl 			*tl++ = dnp->n_cookieverf.nfsuquad[1];
   2574  1.200  christos 		} else
   2575  1.200  christos #endif
   2576  1.200  christos 		{
   2577   1.59      fvdl 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   2578   1.81      fvdl 			*tl++ = txdr_unsigned(uiop->uio_offset);
   2579   1.59      fvdl 		}
   2580   1.59      fvdl 		*tl = txdr_unsigned(nmp->nm_readdirsize);
   2581  1.231      yamt 		nfsm_request(dnp, NFSPROC_READDIR, curlwp, cred);
   2582   1.85      fvdl 		nrpcs++;
   2583  1.200  christos #ifndef NFS_V2_ONLY
   2584   1.59      fvdl 		if (v3) {
   2585  1.153      yamt 			nfsm_postop_attr(vp, attrflag, 0);
   2586   1.59      fvdl 			if (!error) {
   2587   1.59      fvdl 				nfsm_dissect(tl, u_int32_t *,
   2588   1.59      fvdl 				    2 * NFSX_UNSIGNED);
   2589   1.59      fvdl 				dnp->n_cookieverf.nfsuquad[0] = *tl++;
   2590   1.59      fvdl 				dnp->n_cookieverf.nfsuquad[1] = *tl;
   2591   1.59      fvdl 			} else {
   2592   1.59      fvdl 				m_freem(mrep);
   2593   1.59      fvdl 				goto nfsmout;
   2594   1.59      fvdl 			}
   2595   1.59      fvdl 		}
   2596  1.200  christos #endif
   2597   1.53       cgd 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2598    1.1       cgd 		more_dirs = fxdr_unsigned(int, *tl);
   2599  1.220     perry 
   2600    1.1       cgd 		/* loop thru the dir entries, doctoring them to 4bsd form */
   2601   1.59      fvdl 		while (more_dirs && bigenough) {
   2602  1.200  christos #ifndef NFS_V2_ONLY
   2603   1.59      fvdl 			if (v3) {
   2604   1.59      fvdl 				nfsm_dissect(tl, u_int32_t *,
   2605   1.59      fvdl 				    3 * NFSX_UNSIGNED);
   2606   1.98      fair 				fileno = fxdr_hyper(tl);
   2607   1.59      fvdl 				len = fxdr_unsigned(int, *(tl + 2));
   2608  1.200  christos 			} else
   2609  1.200  christos #endif
   2610  1.200  christos 			{
   2611   1.59      fvdl 				nfsm_dissect(tl, u_int32_t *,
   2612   1.59      fvdl 				    2 * NFSX_UNSIGNED);
   2613   1.59      fvdl 				fileno = fxdr_unsigned(u_quad_t, *tl++);
   2614   1.59      fvdl 				len = fxdr_unsigned(int, *tl);
   2615   1.59      fvdl 			}
   2616    1.1       cgd 			if (len <= 0 || len > NFS_MAXNAMLEN) {
   2617    1.1       cgd 				error = EBADRPC;
   2618    1.1       cgd 				m_freem(mrep);
   2619    1.1       cgd 				goto nfsmout;
   2620    1.1       cgd 			}
   2621  1.225  christos 			/* for cookie stashing */
   2622  1.225  christos 			reclen = _DIRENT_RECLEN(dp, len) + 2 * sizeof(off_t);
   2623   1.81      fvdl 			left = NFS_DIRFRAGSIZ - blksiz;
   2624   1.86   thorpej 			if (reclen > left) {
   2625  1.211      yamt 				memset(uiop->uio_iov->iov_base, 0, left);
   2626   1.59      fvdl 				dp->d_reclen += left;
   2627  1.225  christos 				UIO_ADVANCE(uiop, left);
   2628   1.59      fvdl 				blksiz = 0;
   2629   1.81      fvdl 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2630   1.59      fvdl 			}
   2631   1.86   thorpej 			if (reclen > uiop->uio_resid)
   2632   1.59      fvdl 				bigenough = 0;
   2633   1.59      fvdl 			if (bigenough) {
   2634  1.225  christos 				int tlen;
   2635  1.225  christos 
   2636   1.59      fvdl 				dp = (struct dirent *)uiop->uio_iov->iov_base;
   2637  1.225  christos 				dp->d_fileno = fileno;
   2638   1.59      fvdl 				dp->d_namlen = len;
   2639   1.86   thorpej 				dp->d_reclen = reclen;
   2640   1.59      fvdl 				dp->d_type = DT_UNKNOWN;
   2641  1.225  christos 				blksiz += reclen;
   2642   1.81      fvdl 				if (blksiz == NFS_DIRFRAGSIZ)
   2643   1.59      fvdl 					blksiz = 0;
   2644  1.225  christos 				UIO_ADVANCE(uiop, DIRHDSIZ);
   2645   1.59      fvdl 				nfsm_mtouio(uiop, len);
   2646  1.225  christos 				tlen = reclen - (DIRHDSIZ + len);
   2647  1.225  christos 				(void)memset(uiop->uio_iov->iov_base, 0, tlen);
   2648  1.225  christos 				UIO_ADVANCE(uiop, tlen);
   2649   1.59      fvdl 			} else
   2650   1.59      fvdl 				nfsm_adv(nfsm_rndup(len));
   2651  1.200  christos #ifndef NFS_V2_ONLY
   2652   1.59      fvdl 			if (v3) {
   2653   1.59      fvdl 				nfsm_dissect(tl, u_int32_t *,
   2654   1.59      fvdl 				    3 * NFSX_UNSIGNED);
   2655  1.220     perry 			} else
   2656  1.200  christos #endif
   2657  1.200  christos 			{
   2658   1.59      fvdl 				nfsm_dissect(tl, u_int32_t *,
   2659   1.59      fvdl 				    2 * NFSX_UNSIGNED);
   2660    1.1       cgd 			}
   2661   1.59      fvdl 			if (bigenough) {
   2662  1.200  christos #ifndef NFS_V2_ONLY
   2663   1.81      fvdl 				if (v3) {
   2664   1.81      fvdl 					if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
   2665   1.81      fvdl 						uiop->uio_offset =
   2666   1.81      fvdl 						    fxdr_swapcookie3(tl);
   2667   1.81      fvdl 					else
   2668  1.220     perry 						uiop->uio_offset =
   2669   1.81      fvdl 						    fxdr_cookie3(tl);
   2670   1.81      fvdl 				}
   2671  1.200  christos 				else
   2672  1.200  christos #endif
   2673  1.200  christos 				{
   2674   1.81      fvdl 					uiop->uio_offset =
   2675   1.81      fvdl 					    fxdr_unsigned(off_t, *tl);
   2676   1.81      fvdl 				}
   2677   1.81      fvdl 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2678   1.81      fvdl 			}
   2679   1.81      fvdl 			if (v3)
   2680   1.59      fvdl 				tl += 2;
   2681   1.59      fvdl 			else
   2682   1.59      fvdl 				tl++;
   2683    1.1       cgd 			more_dirs = fxdr_unsigned(int, *tl);
   2684    1.1       cgd 		}
   2685    1.1       cgd 		/*
   2686    1.1       cgd 		 * If at end of rpc data, get the eof boolean
   2687    1.1       cgd 		 */
   2688    1.1       cgd 		if (!more_dirs) {
   2689   1.53       cgd 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2690    1.1       cgd 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
   2691  1.219      yamt 
   2692  1.219      yamt 			/*
   2693  1.219      yamt 			 * kludge: if we got no entries, treat it as EOF.
   2694  1.219      yamt 			 * some server sometimes send a reply without any
   2695  1.219      yamt 			 * entries or EOF.
   2696  1.219      yamt 			 * although it might mean the server has very long name,
   2697  1.219      yamt 			 * we can't handle such entries anyway.
   2698  1.219      yamt 			 */
   2699  1.219      yamt 
   2700  1.219      yamt 			if (uiop->uio_resid >= NFS_DIRBLKSIZ)
   2701  1.219      yamt 				more_dirs = 0;
   2702    1.1       cgd 		}
   2703    1.1       cgd 		m_freem(mrep);
   2704    1.1       cgd 	}
   2705    1.1       cgd 	/*
   2706   1.81      fvdl 	 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
   2707    1.1       cgd 	 * by increasing d_reclen for the last record.
   2708    1.1       cgd 	 */
   2709   1.59      fvdl 	if (blksiz > 0) {
   2710   1.81      fvdl 		left = NFS_DIRFRAGSIZ - blksiz;
   2711  1.211      yamt 		memset(uiop->uio_iov->iov_base, 0, left);
   2712   1.59      fvdl 		dp->d_reclen += left;
   2713   1.81      fvdl 		NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2714  1.225  christos 		UIO_ADVANCE(uiop, left);
   2715   1.59      fvdl 	}
   2716   1.59      fvdl 
   2717   1.59      fvdl 	/*
   2718   1.59      fvdl 	 * We are now either at the end of the directory or have filled the
   2719   1.59      fvdl 	 * block.
   2720   1.59      fvdl 	 */
   2721  1.218      yamt 	if (bigenough) {
   2722   1.59      fvdl 		dnp->n_direofoffset = uiop->uio_offset;
   2723  1.218      yamt 		dnp->n_flag |= NEOFVALID;
   2724  1.218      yamt 	}
   2725    1.1       cgd nfsmout:
   2726    1.1       cgd 	return (error);
   2727    1.1       cgd }
   2728    1.1       cgd 
   2729  1.200  christos #ifndef NFS_V2_ONLY
   2730   1.32   mycroft /*
   2731   1.59      fvdl  * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
   2732   1.32   mycroft  */
   2733   1.32   mycroft int
   2734   1.59      fvdl nfs_readdirplusrpc(vp, uiop, cred)
   2735   1.32   mycroft 	struct vnode *vp;
   2736  1.111  augustss 	struct uio *uiop;
   2737  1.236      elad 	kauth_cred_t cred;
   2738   1.32   mycroft {
   2739  1.111  augustss 	int len, left;
   2740  1.111  augustss 	struct dirent *dp = NULL;
   2741  1.111  augustss 	u_int32_t *tl;
   2742  1.252  christos 	char *cp;
   2743  1.111  augustss 	int32_t t1, t2;
   2744   1.79      fvdl 	struct vnode *newvp;
   2745  1.252  christos 	char *bpos, *dpos, *cp2;
   2746  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
   2747   1.32   mycroft 	struct nameidata nami, *ndp = &nami;
   2748   1.32   mycroft 	struct componentname *cnp = &ndp->ni_cnd;
   2749   1.59      fvdl 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2750   1.59      fvdl 	struct nfsnode *dnp = VTONFS(vp), *np;
   2751   1.59      fvdl 	nfsfh_t *fhp;
   2752   1.59      fvdl 	u_quad_t fileno;
   2753  1.225  christos 	int error = 0, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
   2754   1.86   thorpej 	int attrflag, fhsize, nrpcs = 0, reclen;
   2755   1.79      fvdl 	struct nfs_fattr fattr, *fp;
   2756   1.59      fvdl 
   2757   1.81      fvdl #ifdef DIAGNOSTIC
   2758  1.219      yamt 	if (uiop->uio_iovcnt != 1 || uiop->uio_resid != NFS_DIRBLKSIZ)
   2759   1.59      fvdl 		panic("nfs readdirplusrpc bad uio");
   2760   1.59      fvdl #endif
   2761   1.32   mycroft 	ndp->ni_dvp = vp;
   2762   1.32   mycroft 	newvp = NULLVP;
   2763   1.59      fvdl 
   2764   1.59      fvdl 	/*
   2765   1.59      fvdl 	 * Loop around doing readdir rpc's of size nm_readdirsize
   2766   1.81      fvdl 	 * truncated to a multiple of NFS_DIRFRAGSIZ.
   2767   1.32   mycroft 	 * The stopping criteria is EOF or buffer full.
   2768   1.32   mycroft 	 */
   2769   1.59      fvdl 	while (more_dirs && bigenough) {
   2770   1.85      fvdl 		if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
   2771   1.85      fvdl 			bigenough = 0;
   2772   1.85      fvdl 			break;
   2773   1.85      fvdl 		}
   2774   1.59      fvdl 		nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
   2775  1.165  drochner 		nfsm_reqhead(dnp, NFSPROC_READDIRPLUS,
   2776   1.59      fvdl 			NFSX_FH(1) + 6 * NFSX_UNSIGNED);
   2777  1.165  drochner 		nfsm_fhtom(dnp, 1);
   2778   1.59      fvdl  		nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
   2779   1.81      fvdl 		if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
   2780   1.81      fvdl 			txdr_swapcookie3(uiop->uio_offset, tl);
   2781   1.81      fvdl 		} else {
   2782   1.81      fvdl 			txdr_cookie3(uiop->uio_offset, tl);
   2783   1.81      fvdl 		}
   2784   1.81      fvdl 		tl += 2;
   2785   1.59      fvdl 		*tl++ = dnp->n_cookieverf.nfsuquad[0];
   2786   1.59      fvdl 		*tl++ = dnp->n_cookieverf.nfsuquad[1];
   2787   1.59      fvdl 		*tl++ = txdr_unsigned(nmp->nm_readdirsize);
   2788   1.59      fvdl 		*tl = txdr_unsigned(nmp->nm_rsize);
   2789  1.231      yamt 		nfsm_request(dnp, NFSPROC_READDIRPLUS, curlwp, cred);
   2790  1.153      yamt 		nfsm_postop_attr(vp, attrflag, 0);
   2791   1.59      fvdl 		if (error) {
   2792   1.59      fvdl 			m_freem(mrep);
   2793   1.59      fvdl 			goto nfsmout;
   2794   1.59      fvdl 		}
   2795   1.85      fvdl 		nrpcs++;
   2796   1.59      fvdl 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   2797   1.59      fvdl 		dnp->n_cookieverf.nfsuquad[0] = *tl++;
   2798   1.59      fvdl 		dnp->n_cookieverf.nfsuquad[1] = *tl++;
   2799   1.32   mycroft 		more_dirs = fxdr_unsigned(int, *tl);
   2800  1.220     perry 
   2801   1.32   mycroft 		/* loop thru the dir entries, doctoring them to 4bsd form */
   2802   1.32   mycroft 		while (more_dirs && bigenough) {
   2803   1.59      fvdl 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   2804   1.98      fair 			fileno = fxdr_hyper(tl);
   2805   1.59      fvdl 			len = fxdr_unsigned(int, *(tl + 2));
   2806   1.32   mycroft 			if (len <= 0 || len > NFS_MAXNAMLEN) {
   2807   1.32   mycroft 				error = EBADRPC;
   2808   1.32   mycroft 				m_freem(mrep);
   2809   1.32   mycroft 				goto nfsmout;
   2810   1.32   mycroft 			}
   2811  1.225  christos 			/* for cookie stashing */
   2812  1.226      yamt 			reclen = _DIRENT_RECLEN(dp, len) + 2 * sizeof(off_t);
   2813   1.81      fvdl 			left = NFS_DIRFRAGSIZ - blksiz;
   2814   1.86   thorpej 			if (reclen > left) {
   2815   1.86   thorpej 				/*
   2816   1.86   thorpej 				 * DIRFRAGSIZ is aligned, no need to align
   2817   1.86   thorpej 				 * again here.
   2818   1.86   thorpej 				 */
   2819  1.211      yamt 				memset(uiop->uio_iov->iov_base, 0, left);
   2820   1.59      fvdl 				dp->d_reclen += left;
   2821  1.225  christos 				UIO_ADVANCE(uiop, left);
   2822   1.81      fvdl 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2823   1.59      fvdl 				blksiz = 0;
   2824   1.59      fvdl 			}
   2825   1.86   thorpej 			if (reclen > uiop->uio_resid)
   2826   1.32   mycroft 				bigenough = 0;
   2827   1.59      fvdl 			if (bigenough) {
   2828  1.225  christos 				int tlen;
   2829  1.225  christos 
   2830   1.32   mycroft 				dp = (struct dirent *)uiop->uio_iov->iov_base;
   2831  1.225  christos 				dp->d_fileno = fileno;
   2832   1.32   mycroft 				dp->d_namlen = len;
   2833   1.86   thorpej 				dp->d_reclen = reclen;
   2834   1.59      fvdl 				dp->d_type = DT_UNKNOWN;
   2835  1.225  christos 				blksiz += reclen;
   2836   1.81      fvdl 				if (blksiz == NFS_DIRFRAGSIZ)
   2837   1.59      fvdl 					blksiz = 0;
   2838  1.225  christos 				UIO_ADVANCE(uiop, DIRHDSIZ);
   2839   1.32   mycroft 				nfsm_mtouio(uiop, len);
   2840  1.225  christos 				tlen = reclen - (DIRHDSIZ + len);
   2841  1.225  christos 				(void)memset(uiop->uio_iov->iov_base, 0, tlen);
   2842  1.225  christos 				UIO_ADVANCE(uiop, tlen);
   2843  1.225  christos 				cnp->cn_nameptr = dp->d_name;
   2844  1.225  christos 				cnp->cn_namelen = dp->d_namlen;
   2845   1.59      fvdl 			} else
   2846   1.59      fvdl 				nfsm_adv(nfsm_rndup(len));
   2847   1.59      fvdl 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   2848   1.59      fvdl 			if (bigenough) {
   2849   1.81      fvdl 				if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
   2850   1.81      fvdl 					uiop->uio_offset =
   2851   1.81      fvdl 						fxdr_swapcookie3(tl);
   2852   1.81      fvdl 				else
   2853   1.81      fvdl 					uiop->uio_offset =
   2854   1.81      fvdl 						fxdr_cookie3(tl);
   2855   1.81      fvdl 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2856   1.81      fvdl 			}
   2857   1.81      fvdl 			tl += 2;
   2858   1.59      fvdl 
   2859   1.59      fvdl 			/*
   2860   1.59      fvdl 			 * Since the attributes are before the file handle
   2861   1.59      fvdl 			 * (sigh), we must skip over the attributes and then
   2862   1.59      fvdl 			 * come back and get them.
   2863   1.59      fvdl 			 */
   2864   1.59      fvdl 			attrflag = fxdr_unsigned(int, *tl);
   2865   1.59      fvdl 			if (attrflag) {
   2866   1.79      fvdl 			    nfsm_dissect(fp, struct nfs_fattr *, NFSX_V3FATTR);
   2867   1.97     perry 			    memcpy(&fattr, fp, NFSX_V3FATTR);
   2868   1.59      fvdl 			    nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2869   1.59      fvdl 			    doit = fxdr_unsigned(int, *tl);
   2870   1.59      fvdl 			    if (doit) {
   2871   1.59      fvdl 				nfsm_getfh(fhp, fhsize, 1);
   2872   1.59      fvdl 				if (NFS_CMPFH(dnp, fhp, fhsize)) {
   2873   1.59      fvdl 				    VREF(vp);
   2874   1.59      fvdl 				    newvp = vp;
   2875   1.59      fvdl 				    np = dnp;
   2876   1.59      fvdl 				} else {
   2877  1.191      yamt 				    error = nfs_nget1(vp->v_mount, fhp,
   2878  1.191      yamt 					fhsize, &np, LK_NOWAIT);
   2879   1.79      fvdl 				    if (!error)
   2880   1.59      fvdl 					newvp = NFSTOV(np);
   2881   1.59      fvdl 				}
   2882   1.79      fvdl 				if (!error) {
   2883  1.222  christos 				    const char *xcp;
   2884  1.147     lukem 
   2885  1.153      yamt 				    nfs_loadattrcache(&newvp, &fattr, 0, 0);
   2886  1.208      yamt 				    if (bigenough) {
   2887  1.208      yamt 					dp->d_type =
   2888  1.208      yamt 					   IFTODT(VTTOIF(np->n_vattr->va_type));
   2889  1.208      yamt 					if (cnp->cn_namelen <= NCHNAMLEN) {
   2890  1.208      yamt 					    ndp->ni_vp = newvp;
   2891  1.222  christos 					    xcp = cnp->cn_nameptr +
   2892  1.208      yamt 						cnp->cn_namelen;
   2893  1.208      yamt 					    cnp->cn_hash =
   2894  1.222  christos 					       namei_hash(cnp->cn_nameptr, &xcp);
   2895  1.208      yamt 					    nfs_cache_enter(ndp->ni_dvp,
   2896  1.208      yamt 						ndp->ni_vp, cnp);
   2897  1.208      yamt 					}
   2898  1.208      yamt 				    }
   2899   1.79      fvdl 				}
   2900  1.209      yamt 				error = 0;
   2901   1.79      fvdl 			   }
   2902   1.32   mycroft 			} else {
   2903   1.59      fvdl 			    /* Just skip over the file handle */
   2904   1.59      fvdl 			    nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2905   1.59      fvdl 			    i = fxdr_unsigned(int, *tl);
   2906   1.59      fvdl 			    nfsm_adv(nfsm_rndup(i));
   2907   1.32   mycroft 			}
   2908   1.32   mycroft 			if (newvp != NULLVP) {
   2909  1.220     perry 			    if (newvp == vp)
   2910  1.220     perry 				vrele(newvp);
   2911  1.220     perry 			    else
   2912  1.151       tls 				vput(newvp);
   2913   1.59      fvdl 			    newvp = NULLVP;
   2914   1.32   mycroft 			}
   2915   1.59      fvdl 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2916   1.32   mycroft 			more_dirs = fxdr_unsigned(int, *tl);
   2917   1.32   mycroft 		}
   2918   1.32   mycroft 		/*
   2919   1.32   mycroft 		 * If at end of rpc data, get the eof boolean
   2920   1.32   mycroft 		 */
   2921   1.32   mycroft 		if (!more_dirs) {
   2922   1.53       cgd 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2923   1.32   mycroft 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
   2924  1.219      yamt 
   2925  1.219      yamt 			/*
   2926  1.219      yamt 			 * kludge: see a comment in nfs_readdirrpc.
   2927  1.219      yamt 			 */
   2928  1.219      yamt 
   2929  1.219      yamt 			if (uiop->uio_resid >= NFS_DIRBLKSIZ)
   2930  1.219      yamt 				more_dirs = 0;
   2931   1.32   mycroft 		}
   2932   1.32   mycroft 		m_freem(mrep);
   2933   1.32   mycroft 	}
   2934   1.32   mycroft 	/*
   2935   1.81      fvdl 	 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
   2936   1.32   mycroft 	 * by increasing d_reclen for the last record.
   2937   1.32   mycroft 	 */
   2938   1.59      fvdl 	if (blksiz > 0) {
   2939   1.81      fvdl 		left = NFS_DIRFRAGSIZ - blksiz;
   2940  1.211      yamt 		memset(uiop->uio_iov->iov_base, 0, left);
   2941   1.59      fvdl 		dp->d_reclen += left;
   2942   1.81      fvdl 		NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2943  1.225  christos 		UIO_ADVANCE(uiop, left);
   2944   1.59      fvdl 	}
   2945   1.59      fvdl 
   2946   1.59      fvdl 	/*
   2947   1.59      fvdl 	 * We are now either at the end of the directory or have filled the
   2948   1.59      fvdl 	 * block.
   2949   1.59      fvdl 	 */
   2950  1.218      yamt 	if (bigenough) {
   2951   1.59      fvdl 		dnp->n_direofoffset = uiop->uio_offset;
   2952  1.218      yamt 		dnp->n_flag |= NEOFVALID;
   2953  1.218      yamt 	}
   2954   1.32   mycroft nfsmout:
   2955  1.128      fvdl 	if (newvp != NULLVP) {
   2956  1.151       tls 		if(newvp == vp)
   2957  1.151       tls 		    vrele(newvp);
   2958  1.151       tls 		else
   2959  1.151       tls 		    vput(newvp);
   2960  1.128      fvdl 	}
   2961   1.32   mycroft 	return (error);
   2962   1.32   mycroft }
   2963  1.200  christos #endif
   2964  1.200  christos 
   2965    1.1       cgd /*
   2966    1.1       cgd  * Silly rename. To make the NFS filesystem that is stateless look a little
   2967    1.1       cgd  * more like the "ufs" a remove of an active vnode is translated to a rename
   2968    1.1       cgd  * to a funny looking filename that is removed by nfs_inactive on the
   2969    1.1       cgd  * nfsnode. There is the potential for another process on a different client
   2970    1.1       cgd  * to create the same funny name between the nfs_lookitup() fails and the
   2971    1.1       cgd  * nfs_rename() completes, but...
   2972    1.1       cgd  */
   2973   1.32   mycroft int
   2974  1.239      yamt nfs_sillyrename(dvp, vp, cnp, dolink)
   2975   1.32   mycroft 	struct vnode *dvp, *vp;
   2976   1.32   mycroft 	struct componentname *cnp;
   2977  1.250   thorpej 	bool dolink;
   2978    1.1       cgd {
   2979  1.111  augustss 	struct sillyrename *sp;
   2980   1.59      fvdl 	struct nfsnode *np;
   2981    1.1       cgd 	int error;
   2982    1.1       cgd 	short pid;
   2983    1.1       cgd 
   2984   1.32   mycroft 	cache_purge(dvp);
   2985   1.32   mycroft 	np = VTONFS(vp);
   2986   1.59      fvdl #ifndef DIAGNOSTIC
   2987   1.59      fvdl 	if (vp->v_type == VDIR)
   2988   1.59      fvdl 		panic("nfs: sillyrename dir");
   2989   1.59      fvdl #endif
   2990    1.1       cgd 	MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
   2991    1.1       cgd 		M_NFSREQ, M_WAITOK);
   2992  1.236      elad 	sp->s_cred = kauth_cred_dup(cnp->cn_cred);
   2993   1.32   mycroft 	sp->s_dvp = dvp;
   2994   1.32   mycroft 	VREF(dvp);
   2995    1.1       cgd 
   2996    1.1       cgd 	/* Fudge together a funny name */
   2997  1.230  christos 	pid = cnp->cn_lwp->l_proc->p_pid;
   2998   1.97     perry 	memcpy(sp->s_name, ".nfsAxxxx4.4", 13);
   2999    1.1       cgd 	sp->s_namlen = 12;
   3000  1.221  christos 	sp->s_name[8] = hexdigits[pid & 0xf];
   3001  1.221  christos 	sp->s_name[7] = hexdigits[(pid >> 4) & 0xf];
   3002  1.221  christos 	sp->s_name[6] = hexdigits[(pid >> 8) & 0xf];
   3003  1.221  christos 	sp->s_name[5] = hexdigits[(pid >> 12) & 0xf];
   3004    1.1       cgd 
   3005    1.1       cgd 	/* Try lookitups until we get one that isn't there */
   3006   1.59      fvdl 	while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
   3007  1.230  christos 		cnp->cn_lwp, (struct nfsnode **)0) == 0) {
   3008    1.1       cgd 		sp->s_name[4]++;
   3009    1.1       cgd 		if (sp->s_name[4] > 'z') {
   3010    1.1       cgd 			error = EINVAL;
   3011    1.1       cgd 			goto bad;
   3012    1.1       cgd 		}
   3013    1.1       cgd 	}
   3014  1.239      yamt 	if (dolink) {
   3015  1.239      yamt 		error = nfs_linkrpc(dvp, vp, sp->s_name, sp->s_namlen,
   3016  1.239      yamt 		    sp->s_cred, cnp->cn_lwp);
   3017  1.239      yamt 		/*
   3018  1.239      yamt 		 * nfs_request maps NFSERR_NOTSUPP to ENOTSUP.
   3019  1.239      yamt 		 */
   3020  1.239      yamt 		if (error == ENOTSUP) {
   3021  1.239      yamt 			error = nfs_renameit(dvp, cnp, sp);
   3022  1.239      yamt 		}
   3023  1.239      yamt 	} else {
   3024  1.239      yamt 		error = nfs_renameit(dvp, cnp, sp);
   3025  1.239      yamt 	}
   3026   1.59      fvdl 	if (error)
   3027    1.1       cgd 		goto bad;
   3028   1.59      fvdl 	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
   3029  1.230  christos 		cnp->cn_lwp, &np);
   3030    1.1       cgd 	np->n_sillyrename = sp;
   3031    1.1       cgd 	return (0);
   3032    1.1       cgd bad:
   3033    1.1       cgd 	vrele(sp->s_dvp);
   3034  1.236      elad 	kauth_cred_free(sp->s_cred);
   3035  1.252  christos 	free((void *)sp, M_NFSREQ);
   3036    1.1       cgd 	return (error);
   3037    1.1       cgd }
   3038    1.1       cgd 
   3039    1.1       cgd /*
   3040   1.59      fvdl  * Look up a file name and optionally either update the file handle or
   3041   1.59      fvdl  * allocate an nfsnode, depending on the value of npp.
   3042   1.59      fvdl  * npp == NULL	--> just do the lookup
   3043   1.59      fvdl  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
   3044   1.59      fvdl  *			handled too
   3045   1.59      fvdl  * *npp != NULL --> update the file handle in the vnode
   3046    1.1       cgd  */
   3047   1.32   mycroft int
   3048  1.230  christos nfs_lookitup(dvp, name, len, cred, l, npp)
   3049  1.111  augustss 	struct vnode *dvp;
   3050   1.68       cgd 	const char *name;
   3051   1.59      fvdl 	int len;
   3052  1.236      elad 	kauth_cred_t cred;
   3053  1.230  christos 	struct lwp *l;
   3054   1.59      fvdl 	struct nfsnode **npp;
   3055    1.1       cgd {
   3056  1.111  augustss 	u_int32_t *tl;
   3057  1.252  christos 	char *cp;
   3058  1.111  augustss 	int32_t t1, t2;
   3059   1.59      fvdl 	struct vnode *newvp = (struct vnode *)0;
   3060   1.59      fvdl 	struct nfsnode *np, *dnp = VTONFS(dvp);
   3061  1.252  christos 	char *bpos, *dpos, *cp2;
   3062  1.200  christos 	int error = 0, fhlen;
   3063  1.200  christos #ifndef NFS_V2_ONLY
   3064  1.200  christos 	int attrflag;
   3065  1.200  christos #endif
   3066  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
   3067   1.59      fvdl 	nfsfh_t *nfhp;
   3068  1.119     bjh21 	const int v3 = NFS_ISV3(dvp);
   3069    1.1       cgd 
   3070    1.1       cgd 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
   3071  1.165  drochner 	nfsm_reqhead(dnp, NFSPROC_LOOKUP,
   3072   1.59      fvdl 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
   3073  1.165  drochner 	nfsm_fhtom(dnp, v3);
   3074   1.59      fvdl 	nfsm_strtom(name, len, NFS_MAXNAMLEN);
   3075  1.230  christos 	nfsm_request(dnp, NFSPROC_LOOKUP, l, cred);
   3076   1.59      fvdl 	if (npp && !error) {
   3077   1.59      fvdl 		nfsm_getfh(nfhp, fhlen, v3);
   3078   1.59      fvdl 		if (*npp) {
   3079   1.59      fvdl 		    np = *npp;
   3080   1.59      fvdl 		    if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
   3081  1.252  christos 			free((void *)np->n_fhp, M_NFSBIGFH);
   3082   1.59      fvdl 			np->n_fhp = &np->n_fh;
   3083  1.235  christos 		    }
   3084  1.235  christos #if NFS_SMALLFH < NFSX_V3FHMAX
   3085  1.235  christos 		    else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
   3086   1.59      fvdl 			np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
   3087  1.235  christos #endif
   3088  1.252  christos 		    memcpy((void *)np->n_fhp, (void *)nfhp, fhlen);
   3089   1.59      fvdl 		    np->n_fhsize = fhlen;
   3090   1.59      fvdl 		    newvp = NFSTOV(np);
   3091   1.59      fvdl 		} else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
   3092   1.59      fvdl 		    VREF(dvp);
   3093   1.59      fvdl 		    newvp = dvp;
   3094  1.194      yamt 		    np = dnp;
   3095   1.59      fvdl 		} else {
   3096  1.175      fvdl 		    error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
   3097   1.59      fvdl 		    if (error) {
   3098   1.59      fvdl 			m_freem(mrep);
   3099   1.59      fvdl 			return (error);
   3100   1.59      fvdl 		    }
   3101   1.59      fvdl 		    newvp = NFSTOV(np);
   3102   1.59      fvdl 		}
   3103  1.200  christos #ifndef NFS_V2_ONLY
   3104   1.59      fvdl 		if (v3) {
   3105  1.153      yamt 			nfsm_postop_attr(newvp, attrflag, 0);
   3106   1.59      fvdl 			if (!attrflag && *npp == NULL) {
   3107   1.59      fvdl 				m_freem(mrep);
   3108  1.128      fvdl 				vput(newvp);
   3109   1.59      fvdl 				return (ENOENT);
   3110   1.59      fvdl 			}
   3111   1.59      fvdl 		} else
   3112  1.200  christos #endif
   3113  1.153      yamt 			nfsm_loadattr(newvp, (struct vattr *)0, 0);
   3114   1.59      fvdl 	}
   3115   1.59      fvdl 	nfsm_reqdone;
   3116   1.59      fvdl 	if (npp && *npp == NULL) {
   3117   1.59      fvdl 		if (error) {
   3118   1.59      fvdl 			if (newvp)
   3119  1.128      fvdl 				vput(newvp);
   3120   1.59      fvdl 		} else
   3121   1.59      fvdl 			*npp = np;
   3122   1.32   mycroft 	}
   3123   1.59      fvdl 	return (error);
   3124   1.59      fvdl }
   3125   1.59      fvdl 
   3126  1.200  christos #ifndef NFS_V2_ONLY
   3127   1.59      fvdl /*
   3128   1.59      fvdl  * Nfs Version 3 commit rpc
   3129   1.59      fvdl  */
   3130   1.59      fvdl int
   3131  1.230  christos nfs_commit(vp, offset, cnt, l)
   3132  1.111  augustss 	struct vnode *vp;
   3133  1.124       chs 	off_t offset;
   3134  1.124       chs 	uint32_t cnt;
   3135  1.230  christos 	struct lwp *l;
   3136   1.59      fvdl {
   3137  1.252  christos 	char *cp;
   3138  1.111  augustss 	u_int32_t *tl;
   3139  1.111  augustss 	int32_t t1, t2;
   3140  1.111  augustss 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   3141  1.252  christos 	char *bpos, *dpos, *cp2;
   3142   1.59      fvdl 	int error = 0, wccflag = NFSV3_WCCRATTR;
   3143  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
   3144  1.165  drochner 	struct nfsnode *np;
   3145  1.120      fvdl 
   3146  1.166      yamt 	KASSERT(NFS_ISV3(vp));
   3147  1.166      yamt 
   3148  1.160      yamt #ifdef NFS_DEBUG_COMMIT
   3149  1.120      fvdl 	printf("commit %lu - %lu\n", (unsigned long)offset,
   3150  1.120      fvdl 	    (unsigned long)(offset + cnt));
   3151  1.120      fvdl #endif
   3152  1.220     perry 
   3153  1.166      yamt 	simple_lock(&nmp->nm_slock);
   3154  1.166      yamt 	if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0) {
   3155  1.166      yamt 		simple_unlock(&nmp->nm_slock);
   3156   1.59      fvdl 		return (0);
   3157  1.166      yamt 	}
   3158  1.166      yamt 	simple_unlock(&nmp->nm_slock);
   3159   1.59      fvdl 	nfsstats.rpccnt[NFSPROC_COMMIT]++;
   3160  1.165  drochner 	np = VTONFS(vp);
   3161  1.165  drochner 	nfsm_reqhead(np, NFSPROC_COMMIT, NFSX_FH(1));
   3162  1.165  drochner 	nfsm_fhtom(np, 1);
   3163   1.59      fvdl 	nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   3164   1.98      fair 	txdr_hyper(offset, tl);
   3165   1.59      fvdl 	tl += 2;
   3166   1.59      fvdl 	*tl = txdr_unsigned(cnt);
   3167  1.230  christos 	nfsm_request(np, NFSPROC_COMMIT, l, np->n_wcred);
   3168  1.251   thorpej 	nfsm_wcc_data(vp, wccflag, NAC_NOTRUNC, false);
   3169   1.59      fvdl 	if (!error) {
   3170   1.59      fvdl 		nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
   3171  1.166      yamt 		simple_lock(&nmp->nm_slock);
   3172  1.166      yamt 		if ((nmp->nm_iflag & NFSMNT_STALEWRITEVERF) ||
   3173  1.166      yamt 		    memcmp(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF)) {
   3174  1.166      yamt 			memcpy(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF);
   3175   1.59      fvdl 			error = NFSERR_STALEWRITEVERF;
   3176  1.166      yamt 			nmp->nm_iflag |= NFSMNT_STALEWRITEVERF;
   3177   1.59      fvdl 		}
   3178  1.166      yamt 		simple_unlock(&nmp->nm_slock);
   3179    1.1       cgd 	}
   3180    1.1       cgd 	nfsm_reqdone;
   3181    1.1       cgd 	return (error);
   3182    1.1       cgd }
   3183  1.200  christos #endif
   3184    1.1       cgd 
   3185    1.1       cgd /*
   3186    1.1       cgd  * Kludge City..
   3187    1.1       cgd  * - make nfs_bmap() essentially a no-op that does no translation
   3188   1.59      fvdl  * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
   3189    1.1       cgd  *   (Maybe I could use the process's page mapping, but I was concerned that
   3190    1.1       cgd  *    Kernel Write might not be enabled and also figured copyout() would do
   3191   1.97     perry  *    a lot more work than memcpy() and also it currently happens in the
   3192    1.1       cgd  *    context of the swapper process (2).
   3193    1.1       cgd  */
   3194   1.32   mycroft int
   3195   1.58  christos nfs_bmap(v)
   3196   1.58  christos 	void *v;
   3197   1.58  christos {
   3198   1.32   mycroft 	struct vop_bmap_args /* {
   3199   1.32   mycroft 		struct vnode *a_vp;
   3200   1.32   mycroft 		daddr_t  a_bn;
   3201   1.32   mycroft 		struct vnode **a_vpp;
   3202   1.32   mycroft 		daddr_t *a_bnp;
   3203   1.32   mycroft 		int *a_runp;
   3204   1.58  christos 	} */ *ap = v;
   3205  1.111  augustss 	struct vnode *vp = ap->a_vp;
   3206  1.139       chs 	int bshift = vp->v_mount->mnt_fs_bshift - vp->v_mount->mnt_dev_bshift;
   3207   1.32   mycroft 
   3208   1.32   mycroft 	if (ap->a_vpp != NULL)
   3209   1.32   mycroft 		*ap->a_vpp = vp;
   3210   1.32   mycroft 	if (ap->a_bnp != NULL)
   3211  1.139       chs 		*ap->a_bnp = ap->a_bn << bshift;
   3212  1.138       chs 	if (ap->a_runp != NULL)
   3213  1.138       chs 		*ap->a_runp = 1024 * 1024; /* XXX */
   3214    1.1       cgd 	return (0);
   3215    1.1       cgd }
   3216    1.1       cgd 
   3217    1.1       cgd /*
   3218   1.32   mycroft  * Strategy routine.
   3219   1.32   mycroft  * For async requests when nfsiod(s) are running, queue the request by
   3220   1.32   mycroft  * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
   3221   1.32   mycroft  * request.
   3222    1.1       cgd  */
   3223   1.32   mycroft int
   3224   1.58  christos nfs_strategy(v)
   3225   1.58  christos 	void *v;
   3226    1.1       cgd {
   3227   1.58  christos 	struct vop_strategy_args *ap = v;
   3228  1.111  augustss 	struct buf *bp = ap->a_bp;
   3229    1.1       cgd 	int error = 0;
   3230    1.1       cgd 
   3231   1.33        pk 	if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
   3232   1.33        pk 		panic("nfs physio/async");
   3233  1.124       chs 
   3234    1.1       cgd 	/*
   3235    1.1       cgd 	 * If the op is asynchronous and an i/o daemon is waiting
   3236    1.1       cgd 	 * queue the request, wake it up and wait for completion
   3237    1.1       cgd 	 * otherwise just do it ourselves.
   3238    1.1       cgd 	 */
   3239  1.223  christos 	if ((bp->b_flags & B_ASYNC) == 0 || nfs_asyncio(bp))
   3240  1.223  christos 		error = nfs_doio(bp);
   3241    1.1       cgd 	return (error);
   3242    1.1       cgd }
   3243    1.1       cgd 
   3244    1.1       cgd /*
   3245   1.59      fvdl  * fsync vnode op. Just call nfs_flush() with commit == 1.
   3246    1.1       cgd  */
   3247    1.1       cgd /* ARGSUSED */
   3248   1.32   mycroft int
   3249   1.58  christos nfs_fsync(v)
   3250   1.58  christos 	void *v;
   3251   1.58  christos {
   3252   1.32   mycroft 	struct vop_fsync_args /* {
   3253   1.32   mycroft 		struct vnodeop_desc *a_desc;
   3254   1.32   mycroft 		struct vnode * a_vp;
   3255  1.236      elad 		kauth_cred_t  a_cred;
   3256   1.92    kleink 		int  a_flags;
   3257  1.120      fvdl 		off_t offlo;
   3258  1.120      fvdl 		off_t offhi;
   3259  1.230  christos 		struct lwp * a_l;
   3260   1.58  christos 	} */ *ap = v;
   3261   1.59      fvdl 
   3262  1.186      yamt 	struct vnode *vp = ap->a_vp;
   3263  1.186      yamt 
   3264  1.186      yamt 	if (vp->v_type != VREG)
   3265  1.186      yamt 		return 0;
   3266  1.186      yamt 
   3267  1.186      yamt 	return (nfs_flush(vp, ap->a_cred,
   3268  1.230  christos 	    (ap->a_flags & FSYNC_WAIT) != 0 ? MNT_WAIT : 0, ap->a_l, 1));
   3269   1.59      fvdl }
   3270   1.59      fvdl 
   3271   1.59      fvdl /*
   3272  1.124       chs  * Flush all the data associated with a vnode.
   3273   1.59      fvdl  */
   3274   1.59      fvdl int
   3275  1.245      yamt nfs_flush(struct vnode *vp, kauth_cred_t cred, int waitfor, struct lwp *l,
   3276  1.245      yamt     int commit)
   3277   1.59      fvdl {
   3278  1.111  augustss 	struct nfsnode *np = VTONFS(vp);
   3279  1.124       chs 	int error;
   3280  1.124       chs 	int flushflags = PGO_ALLPAGES|PGO_CLEANIT|PGO_SYNCIO;
   3281  1.124       chs 	UVMHIST_FUNC("nfs_flush"); UVMHIST_CALLED(ubchist);
   3282    1.1       cgd 
   3283  1.145       chs 	simple_lock(&vp->v_interlock);
   3284  1.145       chs 	error = VOP_PUTPAGES(vp, 0, 0, flushflags);
   3285   1.32   mycroft 	if (np->n_flag & NWRITEERR) {
   3286    1.1       cgd 		error = np->n_error;
   3287   1.32   mycroft 		np->n_flag &= ~NWRITEERR;
   3288   1.32   mycroft 	}
   3289  1.124       chs 	UVMHIST_LOG(ubchist, "returning %d", error,0,0,0);
   3290    1.1       cgd 	return (error);
   3291    1.1       cgd }
   3292    1.1       cgd 
   3293    1.1       cgd /*
   3294   1.32   mycroft  * Return POSIX pathconf information applicable to nfs.
   3295   1.32   mycroft  *
   3296   1.95    kleink  * N.B. The NFS V2 protocol doesn't support this RPC.
   3297   1.32   mycroft  */
   3298   1.32   mycroft /* ARGSUSED */
   3299   1.58  christos int
   3300   1.58  christos nfs_pathconf(v)
   3301   1.58  christos 	void *v;
   3302   1.58  christos {
   3303   1.32   mycroft 	struct vop_pathconf_args /* {
   3304   1.32   mycroft 		struct vnode *a_vp;
   3305   1.32   mycroft 		int a_name;
   3306   1.41       cgd 		register_t *a_retval;
   3307   1.58  christos 	} */ *ap = v;
   3308   1.95    kleink 	struct nfsv3_pathconf *pcp;
   3309   1.95    kleink 	struct vnode *vp = ap->a_vp;
   3310  1.159      matt 	struct mbuf *mreq, *mrep, *md, *mb;
   3311   1.95    kleink 	int32_t t1, t2;
   3312   1.95    kleink 	u_int32_t *tl;
   3313  1.252  christos 	char *bpos, *dpos, *cp, *cp2;
   3314   1.95    kleink 	int error = 0, attrflag;
   3315  1.146  christos #ifndef NFS_V2_ONLY
   3316  1.146  christos 	struct nfsmount *nmp;
   3317   1.99    kleink 	unsigned int l;
   3318   1.99    kleink 	u_int64_t maxsize;
   3319  1.146  christos #endif
   3320  1.119     bjh21 	const int v3 = NFS_ISV3(vp);
   3321  1.165  drochner 	struct nfsnode *np = VTONFS(vp);
   3322   1.95    kleink 
   3323   1.95    kleink 	switch (ap->a_name) {
   3324   1.95    kleink 		/* Names that can be resolved locally. */
   3325   1.95    kleink 	case _PC_PIPE_BUF:
   3326   1.95    kleink 		*ap->a_retval = PIPE_BUF;
   3327   1.95    kleink 		break;
   3328   1.95    kleink 	case _PC_SYNC_IO:
   3329   1.95    kleink 		*ap->a_retval = 1;
   3330   1.95    kleink 		break;
   3331   1.95    kleink 	/* Names that cannot be resolved locally; do an RPC, if possible. */
   3332   1.95    kleink 	case _PC_LINK_MAX:
   3333   1.95    kleink 	case _PC_NAME_MAX:
   3334   1.95    kleink 	case _PC_CHOWN_RESTRICTED:
   3335   1.95    kleink 	case _PC_NO_TRUNC:
   3336   1.95    kleink 		if (!v3) {
   3337   1.96    kleink 			error = EINVAL;
   3338   1.95    kleink 			break;
   3339   1.95    kleink 		}
   3340   1.95    kleink 		nfsstats.rpccnt[NFSPROC_PATHCONF]++;
   3341  1.165  drochner 		nfsm_reqhead(np, NFSPROC_PATHCONF, NFSX_FH(1));
   3342  1.165  drochner 		nfsm_fhtom(np, 1);
   3343  1.165  drochner 		nfsm_request(np, NFSPROC_PATHCONF,
   3344  1.241        ad 		    curlwp, curlwp->l_cred);	/* XXX */
   3345  1.153      yamt 		nfsm_postop_attr(vp, attrflag, 0);
   3346   1.95    kleink 		if (!error) {
   3347   1.95    kleink 			nfsm_dissect(pcp, struct nfsv3_pathconf *,
   3348   1.95    kleink 			    NFSX_V3PATHCONF);
   3349   1.95    kleink 			switch (ap->a_name) {
   3350   1.95    kleink 			case _PC_LINK_MAX:
   3351   1.95    kleink 				*ap->a_retval =
   3352   1.95    kleink 				    fxdr_unsigned(register_t, pcp->pc_linkmax);
   3353   1.95    kleink 				break;
   3354   1.95    kleink 			case _PC_NAME_MAX:
   3355   1.95    kleink 				*ap->a_retval =
   3356   1.95    kleink 				    fxdr_unsigned(register_t, pcp->pc_namemax);
   3357   1.95    kleink 				break;
   3358   1.95    kleink 			case _PC_CHOWN_RESTRICTED:
   3359   1.95    kleink 				*ap->a_retval =
   3360   1.95    kleink 				    (pcp->pc_chownrestricted == nfs_true);
   3361   1.95    kleink 				break;
   3362   1.95    kleink 			case _PC_NO_TRUNC:
   3363   1.95    kleink 				*ap->a_retval =
   3364   1.95    kleink 				    (pcp->pc_notrunc == nfs_true);
   3365   1.95    kleink 				break;
   3366   1.95    kleink 			}
   3367   1.95    kleink 		}
   3368   1.95    kleink 		nfsm_reqdone;
   3369   1.99    kleink 		break;
   3370   1.99    kleink 	case _PC_FILESIZEBITS:
   3371  1.146  christos #ifndef NFS_V2_ONLY
   3372   1.99    kleink 		if (v3) {
   3373   1.99    kleink 			nmp = VFSTONFS(vp->v_mount);
   3374   1.99    kleink 			if ((nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
   3375   1.99    kleink 				if ((error = nfs_fsinfo(nmp, vp,
   3376  1.241        ad 				    curlwp->l_cred, curlwp)) != 0) /* XXX */
   3377   1.99    kleink 					break;
   3378   1.99    kleink 			for (l = 0, maxsize = nmp->nm_maxfilesize;
   3379   1.99    kleink 			    (maxsize >> l) > 0; l++)
   3380   1.99    kleink 				;
   3381   1.99    kleink 			*ap->a_retval = l + 1;
   3382  1.146  christos 		} else
   3383  1.146  christos #endif
   3384  1.146  christos 		{
   3385   1.99    kleink 			*ap->a_retval = 32;	/* NFS V2 limitation */
   3386   1.99    kleink 		}
   3387   1.95    kleink 		break;
   3388   1.95    kleink 	default:
   3389   1.95    kleink 		error = EINVAL;
   3390   1.95    kleink 		break;
   3391   1.95    kleink 	}
   3392   1.32   mycroft 
   3393   1.95    kleink 	return (error);
   3394   1.32   mycroft }
   3395   1.32   mycroft 
   3396   1.32   mycroft /*
   3397    1.1       cgd  * NFS advisory byte-level locks.
   3398    1.1       cgd  */
   3399   1.32   mycroft int
   3400   1.58  christos nfs_advlock(v)
   3401   1.58  christos 	void *v;
   3402   1.58  christos {
   3403   1.32   mycroft 	struct vop_advlock_args /* {
   3404   1.32   mycroft 		struct vnode *a_vp;
   3405  1.252  christos 		void *a_id;
   3406   1.32   mycroft 		int  a_op;
   3407   1.32   mycroft 		struct flock *a_fl;
   3408   1.32   mycroft 		int  a_flags;
   3409   1.58  christos 	} */ *ap = v;
   3410  1.111  augustss 	struct nfsnode *np = VTONFS(ap->a_vp);
   3411    1.1       cgd 
   3412  1.115  jdolecek 	return lf_advlock(ap, &np->n_lockf, np->n_size);
   3413    1.1       cgd }
   3414    1.1       cgd 
   3415    1.1       cgd /*
   3416    1.1       cgd  * Print out the contents of an nfsnode.
   3417    1.1       cgd  */
   3418   1.17       cgd int
   3419   1.58  christos nfs_print(v)
   3420   1.58  christos 	void *v;
   3421   1.58  christos {
   3422   1.32   mycroft 	struct vop_print_args /* {
   3423   1.32   mycroft 		struct vnode *a_vp;
   3424   1.58  christos 	} */ *ap = v;
   3425  1.111  augustss 	struct vnode *vp = ap->a_vp;
   3426  1.111  augustss 	struct nfsnode *np = VTONFS(vp);
   3427    1.1       cgd 
   3428  1.225  christos 	printf("tag VT_NFS, fileid %lld fsid 0x%lx",
   3429  1.225  christos 	    (unsigned long long)np->n_vattr->va_fileid, np->n_vattr->va_fsid);
   3430    1.1       cgd 	if (vp->v_type == VFIFO)
   3431    1.1       cgd 		fifo_printinfo(vp);
   3432   1.67  christos 	printf("\n");
   3433   1.59      fvdl 	return (0);
   3434   1.32   mycroft }
   3435   1.32   mycroft 
   3436   1.32   mycroft /*
   3437  1.153      yamt  * nfs unlock wrapper.
   3438  1.153      yamt  */
   3439  1.153      yamt int
   3440  1.153      yamt nfs_unlock(void *v)
   3441  1.153      yamt {
   3442  1.153      yamt 	struct vop_unlock_args /* {
   3443  1.153      yamt 		struct vnode *a_vp;
   3444  1.153      yamt 		int a_flags;
   3445  1.153      yamt 	} */ *ap = v;
   3446  1.153      yamt 	struct vnode *vp = ap->a_vp;
   3447  1.153      yamt 
   3448  1.155      yamt 	/*
   3449  1.155      yamt 	 * VOP_UNLOCK can be called by nfs_loadattrcache
   3450  1.155      yamt 	 * with v_data == 0.
   3451  1.155      yamt 	 */
   3452  1.155      yamt 	if (VTONFS(vp)) {
   3453  1.155      yamt 		nfs_delayedtruncate(vp);
   3454  1.155      yamt 	}
   3455  1.153      yamt 
   3456  1.153      yamt 	return genfs_unlock(v);
   3457   1.59      fvdl }
   3458   1.59      fvdl 
   3459   1.59      fvdl /*
   3460   1.32   mycroft  * nfs special file access vnode op.
   3461   1.32   mycroft  * Essentially just get vattr and then imitate iaccess() since the device is
   3462   1.32   mycroft  * local to the client.
   3463    1.3     glass  */
   3464   1.32   mycroft int
   3465   1.58  christos nfsspec_access(v)
   3466   1.58  christos 	void *v;
   3467   1.58  christos {
   3468   1.32   mycroft 	struct vop_access_args /* {
   3469   1.32   mycroft 		struct vnode *a_vp;
   3470   1.32   mycroft 		int  a_mode;
   3471  1.236      elad 		kauth_cred_t a_cred;
   3472  1.230  christos 		struct lwp *a_l;
   3473   1.58  christos 	} */ *ap = v;
   3474   1.46   mycroft 	struct vattr va;
   3475   1.59      fvdl 	struct vnode *vp = ap->a_vp;
   3476   1.32   mycroft 	int error;
   3477    1.3     glass 
   3478  1.230  christos 	error = VOP_GETATTR(vp, &va, ap->a_cred, ap->a_l);
   3479   1.71      fvdl 	if (error)
   3480   1.71      fvdl 		return (error);
   3481   1.71      fvdl 
   3482   1.59      fvdl         /*
   3483   1.59      fvdl 	 * Disallow write attempts on filesystems mounted read-only;
   3484   1.59      fvdl 	 * unless the file is a socket, fifo, or a block or character
   3485   1.59      fvdl 	 * device resident on the filesystem.
   3486   1.59      fvdl 	 */
   3487   1.59      fvdl 	if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
   3488   1.59      fvdl 		switch (vp->v_type) {
   3489   1.59      fvdl 		case VREG:
   3490   1.59      fvdl 		case VDIR:
   3491   1.59      fvdl 		case VLNK:
   3492   1.59      fvdl 			return (EROFS);
   3493   1.59      fvdl 		default:
   3494   1.59      fvdl 			break;
   3495   1.59      fvdl 		}
   3496   1.59      fvdl 	}
   3497   1.46   mycroft 
   3498   1.74   mycroft 	return (vaccess(va.va_type, va.va_mode,
   3499   1.74   mycroft 	    va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
   3500    1.3     glass }
   3501   1.22        pk 
   3502   1.22        pk /*
   3503   1.22        pk  * Read wrapper for special devices.
   3504   1.22        pk  */
   3505   1.32   mycroft int
   3506   1.58  christos nfsspec_read(v)
   3507   1.58  christos 	void *v;
   3508   1.58  christos {
   3509   1.32   mycroft 	struct vop_read_args /* {
   3510   1.32   mycroft 		struct vnode *a_vp;
   3511   1.32   mycroft 		struct uio *a_uio;
   3512   1.32   mycroft 		int  a_ioflag;
   3513  1.236      elad 		kauth_cred_t a_cred;
   3514   1.58  christos 	} */ *ap = v;
   3515  1.111  augustss 	struct nfsnode *np = VTONFS(ap->a_vp);
   3516   1.32   mycroft 
   3517   1.32   mycroft 	/*
   3518   1.32   mycroft 	 * Set access flag.
   3519   1.32   mycroft 	 */
   3520   1.32   mycroft 	np->n_flag |= NACC;
   3521  1.237    kardel 	getnanotime(&np->n_atim);
   3522   1.32   mycroft 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
   3523   1.22        pk }
   3524   1.22        pk 
   3525   1.22        pk /*
   3526   1.22        pk  * Write wrapper for special devices.
   3527   1.22        pk  */
   3528   1.32   mycroft int
   3529   1.58  christos nfsspec_write(v)
   3530   1.58  christos 	void *v;
   3531   1.58  christos {
   3532   1.32   mycroft 	struct vop_write_args /* {
   3533   1.32   mycroft 		struct vnode *a_vp;
   3534   1.32   mycroft 		struct uio *a_uio;
   3535   1.32   mycroft 		int  a_ioflag;
   3536  1.236      elad 		kauth_cred_t a_cred;
   3537   1.58  christos 	} */ *ap = v;
   3538  1.111  augustss 	struct nfsnode *np = VTONFS(ap->a_vp);
   3539   1.32   mycroft 
   3540   1.32   mycroft 	/*
   3541   1.32   mycroft 	 * Set update flag.
   3542   1.32   mycroft 	 */
   3543   1.32   mycroft 	np->n_flag |= NUPD;
   3544  1.237    kardel 	getnanotime(&np->n_mtim);
   3545   1.32   mycroft 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
   3546   1.22        pk }
   3547   1.22        pk 
   3548   1.22        pk /*
   3549   1.22        pk  * Close wrapper for special devices.
   3550   1.22        pk  *
   3551   1.32   mycroft  * Update the times on the nfsnode then do device close.
   3552   1.22        pk  */
   3553   1.32   mycroft int
   3554   1.58  christos nfsspec_close(v)
   3555   1.58  christos 	void *v;
   3556   1.58  christos {
   3557   1.32   mycroft 	struct vop_close_args /* {
   3558   1.32   mycroft 		struct vnode *a_vp;
   3559   1.32   mycroft 		int  a_fflag;
   3560  1.236      elad 		kauth_cred_t a_cred;
   3561  1.230  christos 		struct lwp *a_l;
   3562   1.58  christos 	} */ *ap = v;
   3563  1.111  augustss 	struct vnode *vp = ap->a_vp;
   3564  1.111  augustss 	struct nfsnode *np = VTONFS(vp);
   3565   1.32   mycroft 	struct vattr vattr;
   3566   1.32   mycroft 
   3567   1.32   mycroft 	if (np->n_flag & (NACC | NUPD)) {
   3568   1.32   mycroft 		np->n_flag |= NCHG;
   3569   1.32   mycroft 		if (vp->v_usecount == 1 &&
   3570   1.32   mycroft 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
   3571   1.32   mycroft 			VATTR_NULL(&vattr);
   3572   1.59      fvdl 			if (np->n_flag & NACC)
   3573   1.59      fvdl 				vattr.va_atime = np->n_atim;
   3574   1.59      fvdl 			if (np->n_flag & NUPD)
   3575   1.59      fvdl 				vattr.va_mtime = np->n_mtim;
   3576  1.230  christos 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_l);
   3577   1.32   mycroft 		}
   3578   1.22        pk 	}
   3579   1.32   mycroft 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
   3580   1.22        pk }
   3581   1.22        pk 
   3582   1.22        pk /*
   3583   1.32   mycroft  * Read wrapper for fifos.
   3584   1.22        pk  */
   3585   1.32   mycroft int
   3586   1.58  christos nfsfifo_read(v)
   3587   1.58  christos 	void *v;
   3588   1.58  christos {
   3589   1.32   mycroft 	struct vop_read_args /* {
   3590   1.32   mycroft 		struct vnode *a_vp;
   3591   1.32   mycroft 		struct uio *a_uio;
   3592   1.32   mycroft 		int  a_ioflag;
   3593  1.236      elad 		kauth_cred_t a_cred;
   3594   1.58  christos 	} */ *ap = v;
   3595  1.111  augustss 	struct nfsnode *np = VTONFS(ap->a_vp);
   3596   1.32   mycroft 
   3597   1.22        pk 	/*
   3598   1.32   mycroft 	 * Set access flag.
   3599   1.22        pk 	 */
   3600   1.32   mycroft 	np->n_flag |= NACC;
   3601  1.237    kardel 	getnanotime(&np->n_atim);
   3602   1.32   mycroft 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
   3603   1.22        pk }
   3604   1.22        pk 
   3605   1.22        pk /*
   3606   1.32   mycroft  * Write wrapper for fifos.
   3607   1.22        pk  */
   3608   1.32   mycroft int
   3609   1.58  christos nfsfifo_write(v)
   3610   1.58  christos 	void *v;
   3611   1.58  christos {
   3612   1.32   mycroft 	struct vop_write_args /* {
   3613   1.32   mycroft 		struct vnode *a_vp;
   3614   1.32   mycroft 		struct uio *a_uio;
   3615   1.32   mycroft 		int  a_ioflag;
   3616  1.236      elad 		kauth_cred_t a_cred;
   3617   1.58  christos 	} */ *ap = v;
   3618  1.111  augustss 	struct nfsnode *np = VTONFS(ap->a_vp);
   3619   1.32   mycroft 
   3620   1.22        pk 	/*
   3621   1.32   mycroft 	 * Set update flag.
   3622   1.22        pk 	 */
   3623   1.32   mycroft 	np->n_flag |= NUPD;
   3624  1.237    kardel 	getnanotime(&np->n_mtim);
   3625   1.32   mycroft 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
   3626   1.22        pk }
   3627   1.22        pk 
   3628   1.22        pk /*
   3629   1.32   mycroft  * Close wrapper for fifos.
   3630   1.22        pk  *
   3631   1.32   mycroft  * Update the times on the nfsnode then do fifo close.
   3632   1.22        pk  */
   3633   1.32   mycroft int
   3634   1.58  christos nfsfifo_close(v)
   3635   1.58  christos 	void *v;
   3636   1.58  christos {
   3637   1.32   mycroft 	struct vop_close_args /* {
   3638   1.32   mycroft 		struct vnode *a_vp;
   3639   1.32   mycroft 		int  a_fflag;
   3640  1.236      elad 		kauth_cred_t a_cred;
   3641  1.230  christos 		struct lwp *a_l;
   3642   1.58  christos 	} */ *ap = v;
   3643  1.111  augustss 	struct vnode *vp = ap->a_vp;
   3644  1.111  augustss 	struct nfsnode *np = VTONFS(vp);
   3645   1.32   mycroft 	struct vattr vattr;
   3646   1.32   mycroft 
   3647   1.32   mycroft 	if (np->n_flag & (NACC | NUPD)) {
   3648  1.237    kardel 		struct timespec ts;
   3649  1.237    kardel 
   3650  1.237    kardel 		getnanotime(&ts);
   3651  1.237    kardel 		if (np->n_flag & NACC)
   3652  1.237    kardel 			np->n_atim = ts;
   3653  1.237    kardel 		if (np->n_flag & NUPD)
   3654  1.237    kardel 			np->n_mtim = ts;
   3655   1.32   mycroft 		np->n_flag |= NCHG;
   3656   1.32   mycroft 		if (vp->v_usecount == 1 &&
   3657   1.32   mycroft 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
   3658   1.32   mycroft 			VATTR_NULL(&vattr);
   3659   1.59      fvdl 			if (np->n_flag & NACC)
   3660   1.59      fvdl 				vattr.va_atime = np->n_atim;
   3661   1.59      fvdl 			if (np->n_flag & NUPD)
   3662   1.59      fvdl 				vattr.va_mtime = np->n_mtim;
   3663  1.230  christos 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_l);
   3664   1.32   mycroft 		}
   3665   1.22        pk 	}
   3666   1.32   mycroft 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
   3667   1.22        pk }
   3668