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