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