Home | History | Annotate | Line # | Download | only in nfs
nfs_vnops.c revision 1.295
      1 /*	$NetBSD: nfs_vnops.c,v 1.295 2012/07/22 00:53:21 rmind 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.295 2012/07/22 00:53:21 rmind 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 should be
    953 		 * a broken server.
    954 		 */
    955 		vref(dvp);
    956 		newvp = dvp;
    957 #ifndef NFS_V2_ONLY
    958 		if (v3) {
    959 			nfsm_postop_attr(newvp, attrflag, 0);
    960 			nfsm_postop_attr(dvp, attrflag, 0);
    961 		} else
    962 #endif
    963 			nfsm_loadattr(newvp, (struct vattr *)0, 0);
    964 	} else if (flags & ISDOTDOT) {
    965 		/*
    966 		 * ".." lookup
    967 		 */
    968 		VOP_UNLOCK(dvp);
    969 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
    970 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    971 		if (error) {
    972 			m_freem(mrep);
    973 			return error;
    974 		}
    975 		newvp = NFSTOV(np);
    976 
    977 #ifndef NFS_V2_ONLY
    978 		if (v3) {
    979 			nfsm_postop_attr(newvp, attrflag, 0);
    980 			nfsm_postop_attr(dvp, attrflag, 0);
    981 		} else
    982 #endif
    983 			nfsm_loadattr(newvp, (struct vattr *)0, 0);
    984 	} else {
    985 		/*
    986 		 * Other lookups.
    987 		 */
    988 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
    989 		if (error) {
    990 			m_freem(mrep);
    991 			return error;
    992 		}
    993 		newvp = NFSTOV(np);
    994 #ifndef NFS_V2_ONLY
    995 		if (v3) {
    996 			nfsm_postop_attr(newvp, attrflag, 0);
    997 			nfsm_postop_attr(dvp, attrflag, 0);
    998 		} else
    999 #endif
   1000 			nfsm_loadattr(newvp, (struct vattr *)0, 0);
   1001 	}
   1002 	if (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) {
   1003 		nfs_cache_enter(dvp, newvp, cnp);
   1004 	}
   1005 	*vpp = newvp;
   1006 	nfsm_reqdone;
   1007 	if (error) {
   1008 		/*
   1009 		 * We get here only because of errors returned by
   1010 		 * the RPC. Otherwise we'll have returned above
   1011 		 * (the nfsm_* macros will jump to nfsm_reqdone
   1012 		 * on error).
   1013 		 */
   1014 		if (error == ENOENT && cnp->cn_nameiop != CREATE) {
   1015 			nfs_cache_enter(dvp, NULL, cnp);
   1016 		}
   1017 		if (newvp != NULLVP) {
   1018 			if (newvp == dvp) {
   1019 				vrele(newvp);
   1020 			} else {
   1021 				vput(newvp);
   1022 			}
   1023 		}
   1024 noentry:
   1025 		if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
   1026 		    (flags & ISLASTCN) && error == ENOENT) {
   1027 			if (dvp->v_mount->mnt_flag & MNT_RDONLY) {
   1028 				error = EROFS;
   1029 			} else {
   1030 				error = EJUSTRETURN;
   1031 			}
   1032 		}
   1033 		*vpp = NULL;
   1034 		return error;
   1035 	}
   1036 
   1037 validate:
   1038 	/*
   1039 	 * make sure we have valid type and size.
   1040 	 */
   1041 
   1042 	newvp = *vpp;
   1043 	if (newvp->v_type == VNON) {
   1044 		struct vattr vattr; /* dummy */
   1045 
   1046 		KASSERT(VTONFS(newvp)->n_attrstamp == 0);
   1047 		error = VOP_GETATTR(newvp, &vattr, cnp->cn_cred);
   1048 		if (error) {
   1049 			vput(newvp);
   1050 			*vpp = NULL;
   1051 		}
   1052 	}
   1053 
   1054 	return error;
   1055 }
   1056 
   1057 /*
   1058  * nfs read call.
   1059  * Just call nfs_bioread() to do the work.
   1060  */
   1061 int
   1062 nfs_read(void *v)
   1063 {
   1064 	struct vop_read_args /* {
   1065 		struct vnode *a_vp;
   1066 		struct uio *a_uio;
   1067 		int  a_ioflag;
   1068 		kauth_cred_t a_cred;
   1069 	} */ *ap = v;
   1070 	struct vnode *vp = ap->a_vp;
   1071 
   1072 	if (vp->v_type != VREG)
   1073 		return EISDIR;
   1074 	return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred, 0));
   1075 }
   1076 
   1077 /*
   1078  * nfs readlink call
   1079  */
   1080 int
   1081 nfs_readlink(void *v)
   1082 {
   1083 	struct vop_readlink_args /* {
   1084 		struct vnode *a_vp;
   1085 		struct uio *a_uio;
   1086 		kauth_cred_t a_cred;
   1087 	} */ *ap = v;
   1088 	struct vnode *vp = ap->a_vp;
   1089 	struct nfsnode *np = VTONFS(vp);
   1090 
   1091 	if (vp->v_type != VLNK)
   1092 		return (EPERM);
   1093 
   1094 	if (np->n_rcred != NULL) {
   1095 		kauth_cred_free(np->n_rcred);
   1096 	}
   1097 	np->n_rcred = ap->a_cred;
   1098 	kauth_cred_hold(np->n_rcred);
   1099 
   1100 	return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred, 0));
   1101 }
   1102 
   1103 /*
   1104  * Do a readlink rpc.
   1105  * Called by nfs_doio() from below the buffer cache.
   1106  */
   1107 int
   1108 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop, kauth_cred_t cred)
   1109 {
   1110 	u_int32_t *tl;
   1111 	char *cp;
   1112 	int32_t t1, t2;
   1113 	char *bpos, *dpos, *cp2;
   1114 	int error = 0;
   1115 	uint32_t len;
   1116 	struct mbuf *mreq, *mrep, *md, *mb;
   1117 	const int v3 = NFS_ISV3(vp);
   1118 	struct nfsnode *np = VTONFS(vp);
   1119 #ifndef NFS_V2_ONLY
   1120 	int attrflag;
   1121 #endif
   1122 
   1123 	nfsstats.rpccnt[NFSPROC_READLINK]++;
   1124 	nfsm_reqhead(np, NFSPROC_READLINK, NFSX_FH(v3));
   1125 	nfsm_fhtom(np, v3);
   1126 	nfsm_request(np, NFSPROC_READLINK, curlwp, cred);
   1127 #ifndef NFS_V2_ONLY
   1128 	if (v3)
   1129 		nfsm_postop_attr(vp, attrflag, 0);
   1130 #endif
   1131 	if (!error) {
   1132 #ifndef NFS_V2_ONLY
   1133 		if (v3) {
   1134 			nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
   1135 			len = fxdr_unsigned(uint32_t, *tl);
   1136 			if (len > NFS_MAXPATHLEN) {
   1137 				/*
   1138 				 * this pathname is too long for us.
   1139 				 */
   1140 				m_freem(mrep);
   1141 				/* Solaris returns EINVAL. should we follow? */
   1142 				error = ENAMETOOLONG;
   1143 				goto nfsmout;
   1144 			}
   1145 		} else
   1146 #endif
   1147 		{
   1148 			nfsm_strsiz(len, NFS_MAXPATHLEN);
   1149 		}
   1150 		nfsm_mtouio(uiop, len);
   1151 	}
   1152 	nfsm_reqdone;
   1153 	return (error);
   1154 }
   1155 
   1156 /*
   1157  * nfs read rpc call
   1158  * Ditto above
   1159  */
   1160 int
   1161 nfs_readrpc(struct vnode *vp, struct uio *uiop)
   1162 {
   1163 	u_int32_t *tl;
   1164 	char *cp;
   1165 	int32_t t1, t2;
   1166 	char *bpos, *dpos, *cp2;
   1167 	struct mbuf *mreq, *mrep, *md, *mb;
   1168 	struct nfsmount *nmp;
   1169 	int error = 0, len, retlen, tsiz, eof, byte_count;
   1170 	const int v3 = NFS_ISV3(vp);
   1171 	struct nfsnode *np = VTONFS(vp);
   1172 #ifndef NFS_V2_ONLY
   1173 	int attrflag;
   1174 #endif
   1175 
   1176 #ifndef nolint
   1177 	eof = 0;
   1178 #endif
   1179 	nmp = VFSTONFS(vp->v_mount);
   1180 	tsiz = uiop->uio_resid;
   1181 	if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
   1182 		return (EFBIG);
   1183 	iostat_busy(nmp->nm_stats);
   1184 	byte_count = 0; /* count bytes actually transferred */
   1185 	while (tsiz > 0) {
   1186 		nfsstats.rpccnt[NFSPROC_READ]++;
   1187 		len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
   1188 		nfsm_reqhead(np, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
   1189 		nfsm_fhtom(np, v3);
   1190 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
   1191 #ifndef NFS_V2_ONLY
   1192 		if (v3) {
   1193 			txdr_hyper(uiop->uio_offset, tl);
   1194 			*(tl + 2) = txdr_unsigned(len);
   1195 		} else
   1196 #endif
   1197 		{
   1198 			*tl++ = txdr_unsigned(uiop->uio_offset);
   1199 			*tl++ = txdr_unsigned(len);
   1200 			*tl = 0;
   1201 		}
   1202 		nfsm_request(np, NFSPROC_READ, curlwp, np->n_rcred);
   1203 #ifndef NFS_V2_ONLY
   1204 		if (v3) {
   1205 			nfsm_postop_attr(vp, attrflag, NAC_NOTRUNC);
   1206 			if (error) {
   1207 				m_freem(mrep);
   1208 				goto nfsmout;
   1209 			}
   1210 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1211 			eof = fxdr_unsigned(int, *(tl + 1));
   1212 		} else
   1213 #endif
   1214 			nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
   1215 		nfsm_strsiz(retlen, nmp->nm_rsize);
   1216 		nfsm_mtouio(uiop, retlen);
   1217 		m_freem(mrep);
   1218 		tsiz -= retlen;
   1219 		byte_count += retlen;
   1220 #ifndef NFS_V2_ONLY
   1221 		if (v3) {
   1222 			if (eof || retlen == 0)
   1223 				tsiz = 0;
   1224 		} else
   1225 #endif
   1226 		if (retlen < len)
   1227 			tsiz = 0;
   1228 	}
   1229 nfsmout:
   1230 	iostat_unbusy(nmp->nm_stats, byte_count, 1);
   1231 	return (error);
   1232 }
   1233 
   1234 struct nfs_writerpc_context {
   1235 	kmutex_t nwc_lock;
   1236 	kcondvar_t nwc_cv;
   1237 	int nwc_mbufcount;
   1238 };
   1239 
   1240 /*
   1241  * free mbuf used to refer protected pages while write rpc call.
   1242  * called at splvm.
   1243  */
   1244 static void
   1245 nfs_writerpc_extfree(struct mbuf *m, void *tbuf, size_t size, void *arg)
   1246 {
   1247 	struct nfs_writerpc_context *ctx = arg;
   1248 
   1249 	KASSERT(m != NULL);
   1250 	KASSERT(ctx != NULL);
   1251 	pool_cache_put(mb_cache, m);
   1252 	mutex_enter(&ctx->nwc_lock);
   1253 	if (--ctx->nwc_mbufcount == 0) {
   1254 		cv_signal(&ctx->nwc_cv);
   1255 	}
   1256 	mutex_exit(&ctx->nwc_lock);
   1257 }
   1258 
   1259 /*
   1260  * nfs write call
   1261  */
   1262 int
   1263 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, bool pageprotected, bool *stalewriteverfp)
   1264 {
   1265 	u_int32_t *tl;
   1266 	char *cp;
   1267 	int32_t t1, t2;
   1268 	char *bpos, *dpos;
   1269 	struct mbuf *mreq, *mrep, *md, *mb;
   1270 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1271 	int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR;
   1272 	const int v3 = NFS_ISV3(vp);
   1273 	int committed = NFSV3WRITE_FILESYNC;
   1274 	struct nfsnode *np = VTONFS(vp);
   1275 	struct nfs_writerpc_context ctx;
   1276 	int byte_count;
   1277 	size_t origresid;
   1278 #ifndef NFS_V2_ONLY
   1279 	char *cp2;
   1280 	int rlen, commit;
   1281 #endif
   1282 
   1283 	if (vp->v_mount->mnt_flag & MNT_RDONLY) {
   1284 		panic("writerpc readonly vp %p", vp);
   1285 	}
   1286 
   1287 #ifdef DIAGNOSTIC
   1288 	if (uiop->uio_iovcnt != 1)
   1289 		panic("nfs: writerpc iovcnt > 1");
   1290 #endif
   1291 	tsiz = uiop->uio_resid;
   1292 	if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
   1293 		return EFBIG;
   1294 
   1295 	mutex_init(&ctx.nwc_lock, MUTEX_DRIVER, IPL_VM);
   1296 	cv_init(&ctx.nwc_cv, "nfsmblk");
   1297 	ctx.nwc_mbufcount = 1;
   1298 
   1299 retry:
   1300 	origresid = uiop->uio_resid;
   1301 	KASSERT(origresid == uiop->uio_iov->iov_len);
   1302 	iostat_busy(nmp->nm_stats);
   1303 	byte_count = 0; /* count of bytes actually written */
   1304 	while (tsiz > 0) {
   1305 		uint32_t datalen; /* data bytes need to be allocated in mbuf */
   1306 		uint32_t backup;
   1307 		bool stalewriteverf = false;
   1308 
   1309 		nfsstats.rpccnt[NFSPROC_WRITE]++;
   1310 		len = min(tsiz, nmp->nm_wsize);
   1311 		datalen = pageprotected ? 0 : nfsm_rndup(len);
   1312 		nfsm_reqhead(np, NFSPROC_WRITE,
   1313 			NFSX_FH(v3) + 5 * NFSX_UNSIGNED + datalen);
   1314 		nfsm_fhtom(np, v3);
   1315 #ifndef NFS_V2_ONLY
   1316 		if (v3) {
   1317 			nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
   1318 			txdr_hyper(uiop->uio_offset, tl);
   1319 			tl += 2;
   1320 			*tl++ = txdr_unsigned(len);
   1321 			*tl++ = txdr_unsigned(*iomode);
   1322 			*tl = txdr_unsigned(len);
   1323 		} else
   1324 #endif
   1325 		{
   1326 			u_int32_t x;
   1327 
   1328 			nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
   1329 			/* Set both "begin" and "current" to non-garbage. */
   1330 			x = txdr_unsigned((u_int32_t)uiop->uio_offset);
   1331 			*tl++ = x;      /* "begin offset" */
   1332 			*tl++ = x;      /* "current offset" */
   1333 			x = txdr_unsigned(len);
   1334 			*tl++ = x;      /* total to this offset */
   1335 			*tl = x;        /* size of this write */
   1336 
   1337 		}
   1338 		if (pageprotected) {
   1339 			/*
   1340 			 * since we know pages can't be modified during i/o,
   1341 			 * no need to copy them for us.
   1342 			 */
   1343 			struct mbuf *m;
   1344 			struct iovec *iovp = uiop->uio_iov;
   1345 
   1346 			m = m_get(M_WAIT, MT_DATA);
   1347 			MCLAIM(m, &nfs_mowner);
   1348 			MEXTADD(m, iovp->iov_base, len, M_MBUF,
   1349 			    nfs_writerpc_extfree, &ctx);
   1350 			m->m_flags |= M_EXT_ROMAP;
   1351 			m->m_len = len;
   1352 			mb->m_next = m;
   1353 			/*
   1354 			 * no need to maintain mb and bpos here
   1355 			 * because no one care them later.
   1356 			 */
   1357 #if 0
   1358 			mb = m;
   1359 			bpos = mtod(void *, mb) + mb->m_len;
   1360 #endif
   1361 			UIO_ADVANCE(uiop, len);
   1362 			uiop->uio_offset += len;
   1363 			mutex_enter(&ctx.nwc_lock);
   1364 			ctx.nwc_mbufcount++;
   1365 			mutex_exit(&ctx.nwc_lock);
   1366 			nfs_zeropad(mb, 0, nfsm_padlen(len));
   1367 		} else {
   1368 			nfsm_uiotom(uiop, len);
   1369 		}
   1370 		nfsm_request(np, NFSPROC_WRITE, curlwp, np->n_wcred);
   1371 #ifndef NFS_V2_ONLY
   1372 		if (v3) {
   1373 			wccflag = NFSV3_WCCCHK;
   1374 			nfsm_wcc_data(vp, wccflag, NAC_NOTRUNC, !error);
   1375 			if (!error) {
   1376 				nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
   1377 					+ NFSX_V3WRITEVERF);
   1378 				rlen = fxdr_unsigned(int, *tl++);
   1379 				if (rlen == 0) {
   1380 					error = NFSERR_IO;
   1381 					m_freem(mrep);
   1382 					break;
   1383 				} else if (rlen < len) {
   1384 					backup = len - rlen;
   1385 					UIO_ADVANCE(uiop, -backup);
   1386 					uiop->uio_offset -= backup;
   1387 					len = rlen;
   1388 				}
   1389 				commit = fxdr_unsigned(int, *tl++);
   1390 
   1391 				/*
   1392 				 * Return the lowest committment level
   1393 				 * obtained by any of the RPCs.
   1394 				 */
   1395 				if (committed == NFSV3WRITE_FILESYNC)
   1396 					committed = commit;
   1397 				else if (committed == NFSV3WRITE_DATASYNC &&
   1398 					commit == NFSV3WRITE_UNSTABLE)
   1399 					committed = commit;
   1400 				mutex_enter(&nmp->nm_lock);
   1401 				if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0){
   1402 					memcpy(nmp->nm_writeverf, tl,
   1403 					    NFSX_V3WRITEVERF);
   1404 					nmp->nm_iflag |= NFSMNT_HASWRITEVERF;
   1405 				} else if ((nmp->nm_iflag &
   1406 				    NFSMNT_STALEWRITEVERF) ||
   1407 				    memcmp(tl, nmp->nm_writeverf,
   1408 				    NFSX_V3WRITEVERF)) {
   1409 					memcpy(nmp->nm_writeverf, tl,
   1410 					    NFSX_V3WRITEVERF);
   1411 					/*
   1412 					 * note NFSMNT_STALEWRITEVERF
   1413 					 * if we're the first thread to
   1414 					 * notice it.
   1415 					 */
   1416 					if ((nmp->nm_iflag &
   1417 					    NFSMNT_STALEWRITEVERF) == 0) {
   1418 						stalewriteverf = true;
   1419 						nmp->nm_iflag |=
   1420 						    NFSMNT_STALEWRITEVERF;
   1421 					}
   1422 				}
   1423 				mutex_exit(&nmp->nm_lock);
   1424 			}
   1425 		} else
   1426 #endif
   1427 			nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
   1428 		if (wccflag)
   1429 			VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr->va_mtime;
   1430 		m_freem(mrep);
   1431 		if (error)
   1432 			break;
   1433 		tsiz -= len;
   1434 		byte_count += len;
   1435 		if (stalewriteverf) {
   1436 			*stalewriteverfp = true;
   1437 			stalewriteverf = false;
   1438 			if (committed == NFSV3WRITE_UNSTABLE &&
   1439 			    len != origresid) {
   1440 				/*
   1441 				 * if our write requests weren't atomic but
   1442 				 * unstable, datas in previous iterations
   1443 				 * might have already been lost now.
   1444 				 * then, we should resend them to nfsd.
   1445 				 */
   1446 				backup = origresid - tsiz;
   1447 				UIO_ADVANCE(uiop, -backup);
   1448 				uiop->uio_offset -= backup;
   1449 				tsiz = origresid;
   1450 				goto retry;
   1451 			}
   1452 		}
   1453 	}
   1454 nfsmout:
   1455 	iostat_unbusy(nmp->nm_stats, byte_count, 0);
   1456 	if (pageprotected) {
   1457 		/*
   1458 		 * wait until mbufs go away.
   1459 		 * retransmitted mbufs can survive longer than rpc requests
   1460 		 * themselves.
   1461 		 */
   1462 		mutex_enter(&ctx.nwc_lock);
   1463 		ctx.nwc_mbufcount--;
   1464 		while (ctx.nwc_mbufcount > 0) {
   1465 			cv_wait(&ctx.nwc_cv, &ctx.nwc_lock);
   1466 		}
   1467 		mutex_exit(&ctx.nwc_lock);
   1468 	}
   1469 	mutex_destroy(&ctx.nwc_lock);
   1470 	cv_destroy(&ctx.nwc_cv);
   1471 	*iomode = committed;
   1472 	if (error)
   1473 		uiop->uio_resid = tsiz;
   1474 	return (error);
   1475 }
   1476 
   1477 /*
   1478  * nfs mknod rpc
   1479  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
   1480  * mode set to specify the file type and the size field for rdev.
   1481  */
   1482 int
   1483 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, struct vattr *vap)
   1484 {
   1485 	struct nfsv2_sattr *sp;
   1486 	u_int32_t *tl;
   1487 	char *cp;
   1488 	int32_t t1, t2;
   1489 	struct vnode *newvp = (struct vnode *)0;
   1490 	struct nfsnode *dnp, *np;
   1491 	char *cp2;
   1492 	char *bpos, *dpos;
   1493 	int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
   1494 	struct mbuf *mreq, *mrep, *md, *mb;
   1495 	u_int32_t rdev;
   1496 	const int v3 = NFS_ISV3(dvp);
   1497 
   1498 	if (vap->va_type == VCHR || vap->va_type == VBLK)
   1499 		rdev = txdr_unsigned(vap->va_rdev);
   1500 	else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
   1501 		rdev = nfs_xdrneg1;
   1502 	else {
   1503 		VOP_ABORTOP(dvp, cnp);
   1504 		vput(dvp);
   1505 		return (EOPNOTSUPP);
   1506 	}
   1507 	nfsstats.rpccnt[NFSPROC_MKNOD]++;
   1508 	dnp = VTONFS(dvp);
   1509 	nfsm_reqhead(dnp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
   1510 		+ nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
   1511 	nfsm_fhtom(dnp, v3);
   1512 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1513 #ifndef NFS_V2_ONLY
   1514 	if (v3) {
   1515 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1516 		*tl++ = vtonfsv3_type(vap->va_type);
   1517 		nfsm_v3attrbuild(vap, false);
   1518 		if (vap->va_type == VCHR || vap->va_type == VBLK) {
   1519 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1520 			*tl++ = txdr_unsigned(major(vap->va_rdev));
   1521 			*tl = txdr_unsigned(minor(vap->va_rdev));
   1522 		}
   1523 	} else
   1524 #endif
   1525 	{
   1526 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   1527 		sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
   1528 		sp->sa_uid = nfs_xdrneg1;
   1529 		sp->sa_gid = nfs_xdrneg1;
   1530 		sp->sa_size = rdev;
   1531 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   1532 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   1533 	}
   1534 	nfsm_request(dnp, NFSPROC_MKNOD, curlwp, cnp->cn_cred);
   1535 	if (!error) {
   1536 		nfsm_mtofh(dvp, newvp, v3, gotvp);
   1537 		if (!gotvp) {
   1538 			error = nfs_lookitup(dvp, cnp->cn_nameptr,
   1539 			    cnp->cn_namelen, cnp->cn_cred, curlwp, &np);
   1540 			if (!error)
   1541 				newvp = NFSTOV(np);
   1542 		}
   1543 	}
   1544 #ifndef NFS_V2_ONLY
   1545 	if (v3)
   1546 		nfsm_wcc_data(dvp, wccflag, 0, !error);
   1547 #endif
   1548 	nfsm_reqdone;
   1549 	if (error) {
   1550 		if (newvp)
   1551 			vput(newvp);
   1552 	} else {
   1553 		nfs_cache_enter(dvp, newvp, cnp);
   1554 		*vpp = newvp;
   1555 	}
   1556 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1557 	if (!wccflag)
   1558 		NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
   1559 	vput(dvp);
   1560 	return (error);
   1561 }
   1562 
   1563 /*
   1564  * nfs mknod vop
   1565  * just call nfs_mknodrpc() to do the work.
   1566  */
   1567 /* ARGSUSED */
   1568 int
   1569 nfs_mknod(void *v)
   1570 {
   1571 	struct vop_mknod_args /* {
   1572 		struct vnode *a_dvp;
   1573 		struct vnode **a_vpp;
   1574 		struct componentname *a_cnp;
   1575 		struct vattr *a_vap;
   1576 	} */ *ap = v;
   1577 	struct vnode *dvp = ap->a_dvp;
   1578 	struct componentname *cnp = ap->a_cnp;
   1579 	int error;
   1580 
   1581 	error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, ap->a_vap);
   1582 	VN_KNOTE(dvp, NOTE_WRITE);
   1583 	if (error == 0 || error == EEXIST)
   1584 		cache_purge1(dvp, cnp, 0);
   1585 	return (error);
   1586 }
   1587 
   1588 /*
   1589  * nfs file create call
   1590  */
   1591 int
   1592 nfs_create(void *v)
   1593 {
   1594 	struct vop_create_args /* {
   1595 		struct vnode *a_dvp;
   1596 		struct vnode **a_vpp;
   1597 		struct componentname *a_cnp;
   1598 		struct vattr *a_vap;
   1599 	} */ *ap = v;
   1600 	struct vnode *dvp = ap->a_dvp;
   1601 	struct vattr *vap = ap->a_vap;
   1602 	struct componentname *cnp = ap->a_cnp;
   1603 	struct nfsv2_sattr *sp;
   1604 	u_int32_t *tl;
   1605 	char *cp;
   1606 	int32_t t1, t2;
   1607 	struct nfsnode *dnp, *np = (struct nfsnode *)0;
   1608 	struct vnode *newvp = (struct vnode *)0;
   1609 	char *bpos, *dpos, *cp2;
   1610 	int error, wccflag = NFSV3_WCCRATTR, gotvp = 0;
   1611 	struct mbuf *mreq, *mrep, *md, *mb;
   1612 	const int v3 = NFS_ISV3(dvp);
   1613 	u_int32_t excl_mode = NFSV3CREATE_UNCHECKED;
   1614 
   1615 	/*
   1616 	 * Oops, not for me..
   1617 	 */
   1618 	if (vap->va_type == VSOCK)
   1619 		return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
   1620 
   1621 	KASSERT(vap->va_type == VREG);
   1622 
   1623 #ifdef VA_EXCLUSIVE
   1624 	if (vap->va_vaflags & VA_EXCLUSIVE) {
   1625 		excl_mode = NFSV3CREATE_EXCLUSIVE;
   1626 	}
   1627 #endif
   1628 again:
   1629 	error = 0;
   1630 	nfsstats.rpccnt[NFSPROC_CREATE]++;
   1631 	dnp = VTONFS(dvp);
   1632 	nfsm_reqhead(dnp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
   1633 		nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
   1634 	nfsm_fhtom(dnp, v3);
   1635 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   1636 #ifndef NFS_V2_ONLY
   1637 	if (v3) {
   1638 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1639 		if (excl_mode == NFSV3CREATE_EXCLUSIVE) {
   1640 			*tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
   1641 			nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
   1642 			*tl++ = cprng_fast32();
   1643 			*tl = cprng_fast32();
   1644 		} else {
   1645 			*tl = txdr_unsigned(excl_mode);
   1646 			nfsm_v3attrbuild(vap, false);
   1647 		}
   1648 	} else
   1649 #endif
   1650 	{
   1651 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   1652 		sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
   1653 		sp->sa_uid = nfs_xdrneg1;
   1654 		sp->sa_gid = nfs_xdrneg1;
   1655 		sp->sa_size = 0;
   1656 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   1657 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   1658 	}
   1659 	nfsm_request(dnp, NFSPROC_CREATE, curlwp, cnp->cn_cred);
   1660 	if (!error) {
   1661 		nfsm_mtofh(dvp, newvp, v3, gotvp);
   1662 		if (!gotvp) {
   1663 			error = nfs_lookitup(dvp, cnp->cn_nameptr,
   1664 			    cnp->cn_namelen, cnp->cn_cred, curlwp, &np);
   1665 			if (!error)
   1666 				newvp = NFSTOV(np);
   1667 		}
   1668 	}
   1669 #ifndef NFS_V2_ONLY
   1670 	if (v3)
   1671 		nfsm_wcc_data(dvp, wccflag, 0, !error);
   1672 #endif
   1673 	nfsm_reqdone;
   1674 	if (error) {
   1675 		/*
   1676 		 * nfs_request maps NFSERR_NOTSUPP to ENOTSUP.
   1677 		 */
   1678 		if (v3 && error == ENOTSUP) {
   1679 			if (excl_mode == NFSV3CREATE_EXCLUSIVE) {
   1680 				excl_mode = NFSV3CREATE_GUARDED;
   1681 				goto again;
   1682 			} else if (excl_mode == NFSV3CREATE_GUARDED) {
   1683 				excl_mode = NFSV3CREATE_UNCHECKED;
   1684 				goto again;
   1685 			}
   1686 		}
   1687 	} else if (v3 && (excl_mode == NFSV3CREATE_EXCLUSIVE)) {
   1688 		struct timespec ts;
   1689 
   1690 		getnanotime(&ts);
   1691 
   1692 		/*
   1693 		 * make sure that we'll update timestamps as
   1694 		 * most server implementations use them to store
   1695 		 * the create verifier.
   1696 		 *
   1697 		 * XXX it's better to use TOSERVER always.
   1698 		 */
   1699 
   1700 		if (vap->va_atime.tv_sec == VNOVAL)
   1701 			vap->va_atime = ts;
   1702 		if (vap->va_mtime.tv_sec == VNOVAL)
   1703 			vap->va_mtime = ts;
   1704 
   1705 		error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, curlwp);
   1706 	}
   1707 	if (error == 0) {
   1708 		if (cnp->cn_flags & MAKEENTRY)
   1709 			nfs_cache_enter(dvp, newvp, cnp);
   1710 		else
   1711 			cache_purge1(dvp, cnp, 0);
   1712 		*ap->a_vpp = newvp;
   1713 	} else {
   1714 		if (newvp)
   1715 			vput(newvp);
   1716 		if (error == EEXIST)
   1717 			cache_purge1(dvp, cnp, 0);
   1718 	}
   1719 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1720 	if (!wccflag)
   1721 		NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
   1722 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
   1723 	vput(dvp);
   1724 	return (error);
   1725 }
   1726 
   1727 /*
   1728  * nfs file remove call
   1729  * To try and make nfs semantics closer to ufs semantics, a file that has
   1730  * other processes using the vnode is renamed instead of removed and then
   1731  * removed later on the last close.
   1732  * - If v_usecount > 1
   1733  *	  If a rename is not already in the works
   1734  *	     call nfs_sillyrename() to set it up
   1735  *     else
   1736  *	  do the remove rpc
   1737  */
   1738 int
   1739 nfs_remove(void *v)
   1740 {
   1741 	struct vop_remove_args /* {
   1742 		struct vnodeop_desc *a_desc;
   1743 		struct vnode * a_dvp;
   1744 		struct vnode * a_vp;
   1745 		struct componentname * a_cnp;
   1746 	} */ *ap = v;
   1747 	struct vnode *vp = ap->a_vp;
   1748 	struct vnode *dvp = ap->a_dvp;
   1749 	struct componentname *cnp = ap->a_cnp;
   1750 	struct nfsnode *np = VTONFS(vp);
   1751 	int error = 0;
   1752 	struct vattr vattr;
   1753 
   1754 #ifndef DIAGNOSTIC
   1755 	if (vp->v_usecount < 1)
   1756 		panic("nfs_remove: bad v_usecount");
   1757 #endif
   1758 	if (vp->v_type == VDIR)
   1759 		error = EPERM;
   1760 	else if (vp->v_usecount == 1 || (np->n_sillyrename &&
   1761 	    VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
   1762 	    vattr.va_nlink > 1)) {
   1763 		/*
   1764 		 * Purge the name cache so that the chance of a lookup for
   1765 		 * the name succeeding while the remove is in progress is
   1766 		 * minimized. Without node locking it can still happen, such
   1767 		 * that an I/O op returns ESTALE, but since you get this if
   1768 		 * another host removes the file..
   1769 		 */
   1770 		cache_purge(vp);
   1771 		/*
   1772 		 * throw away biocache buffers, mainly to avoid
   1773 		 * unnecessary delayed writes later.
   1774 		 */
   1775 		error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, curlwp, 1);
   1776 		/* Do the rpc */
   1777 		if (error != EINTR)
   1778 			error = nfs_removerpc(dvp, cnp->cn_nameptr,
   1779 				cnp->cn_namelen, cnp->cn_cred, curlwp);
   1780 	} else if (!np->n_sillyrename)
   1781 		error = nfs_sillyrename(dvp, vp, cnp, false);
   1782 	if (!error && nfs_getattrcache(vp, &vattr) == 0 &&
   1783 	    vattr.va_nlink == 1) {
   1784 		np->n_flag |= NREMOVED;
   1785 	}
   1786 	NFS_INVALIDATE_ATTRCACHE(np);
   1787 	VN_KNOTE(vp, NOTE_DELETE);
   1788 	VN_KNOTE(dvp, NOTE_WRITE);
   1789 	if (dvp == vp)
   1790 		vrele(vp);
   1791 	else
   1792 		vput(vp);
   1793 	vput(dvp);
   1794 	return (error);
   1795 }
   1796 
   1797 /*
   1798  * nfs file remove rpc called from nfs_inactive
   1799  */
   1800 int
   1801 nfs_removeit(struct sillyrename *sp)
   1802 {
   1803 
   1804 	return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
   1805 		(struct lwp *)0));
   1806 }
   1807 
   1808 /*
   1809  * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
   1810  */
   1811 int
   1812 nfs_removerpc(struct vnode *dvp, const char *name, int namelen, kauth_cred_t cred, struct lwp *l)
   1813 {
   1814 	u_int32_t *tl;
   1815 	char *cp;
   1816 #ifndef NFS_V2_ONLY
   1817 	int32_t t1;
   1818 	char *cp2;
   1819 #endif
   1820 	int32_t t2;
   1821 	char *bpos, *dpos;
   1822 	int error = 0, wccflag = NFSV3_WCCRATTR;
   1823 	struct mbuf *mreq, *mrep, *md, *mb;
   1824 	const int v3 = NFS_ISV3(dvp);
   1825 	int rexmit = 0;
   1826 	struct nfsnode *dnp = VTONFS(dvp);
   1827 
   1828 	nfsstats.rpccnt[NFSPROC_REMOVE]++;
   1829 	nfsm_reqhead(dnp, NFSPROC_REMOVE,
   1830 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
   1831 	nfsm_fhtom(dnp, v3);
   1832 	nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
   1833 	nfsm_request1(dnp, NFSPROC_REMOVE, l, cred, &rexmit);
   1834 #ifndef NFS_V2_ONLY
   1835 	if (v3)
   1836 		nfsm_wcc_data(dvp, wccflag, 0, !error);
   1837 #endif
   1838 	nfsm_reqdone;
   1839 	VTONFS(dvp)->n_flag |= NMODIFIED;
   1840 	if (!wccflag)
   1841 		NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
   1842 	/*
   1843 	 * Kludge City: If the first reply to the remove rpc is lost..
   1844 	 *   the reply to the retransmitted request will be ENOENT
   1845 	 *   since the file was in fact removed
   1846 	 *   Therefore, we cheat and return success.
   1847 	 */
   1848 	if (rexmit && error == ENOENT)
   1849 		error = 0;
   1850 	return (error);
   1851 }
   1852 
   1853 /*
   1854  * nfs file rename call
   1855  */
   1856 int
   1857 nfs_rename(void *v)
   1858 {
   1859 	struct vop_rename_args  /* {
   1860 		struct vnode *a_fdvp;
   1861 		struct vnode *a_fvp;
   1862 		struct componentname *a_fcnp;
   1863 		struct vnode *a_tdvp;
   1864 		struct vnode *a_tvp;
   1865 		struct componentname *a_tcnp;
   1866 	} */ *ap = v;
   1867 	struct vnode *fvp = ap->a_fvp;
   1868 	struct vnode *tvp = ap->a_tvp;
   1869 	struct vnode *fdvp = ap->a_fdvp;
   1870 	struct vnode *tdvp = ap->a_tdvp;
   1871 	struct componentname *tcnp = ap->a_tcnp;
   1872 	struct componentname *fcnp = ap->a_fcnp;
   1873 	int error;
   1874 
   1875 	/* Check for cross-device rename */
   1876 	if ((fvp->v_mount != tdvp->v_mount) ||
   1877 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
   1878 		error = EXDEV;
   1879 		goto out;
   1880 	}
   1881 
   1882 	/*
   1883 	 * If the tvp exists and is in use, sillyrename it before doing the
   1884 	 * rename of the new file over it.
   1885 	 *
   1886 	 * Have sillyrename use link instead of rename if possible,
   1887 	 * so that we don't lose the file if the rename fails, and so
   1888 	 * that there's no window when the "to" file doesn't exist.
   1889 	 */
   1890 	if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
   1891 	    tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp, true)) {
   1892 		VN_KNOTE(tvp, NOTE_DELETE);
   1893 		vput(tvp);
   1894 		tvp = NULL;
   1895 	}
   1896 
   1897 	error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
   1898 		tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
   1899 		curlwp);
   1900 
   1901 	VN_KNOTE(fdvp, NOTE_WRITE);
   1902 	VN_KNOTE(tdvp, NOTE_WRITE);
   1903 	if (error == 0 || error == EEXIST) {
   1904 		if (fvp->v_type == VDIR)
   1905 			cache_purge(fvp);
   1906 		else
   1907 			cache_purge1(fdvp, fcnp, 0);
   1908 		if (tvp != NULL && tvp->v_type == VDIR)
   1909 			cache_purge(tvp);
   1910 		else
   1911 			cache_purge1(tdvp, tcnp, 0);
   1912 	}
   1913 out:
   1914 	if (tdvp == tvp)
   1915 		vrele(tdvp);
   1916 	else
   1917 		vput(tdvp);
   1918 	if (tvp)
   1919 		vput(tvp);
   1920 	vrele(fdvp);
   1921 	vrele(fvp);
   1922 	return (error);
   1923 }
   1924 
   1925 /*
   1926  * nfs file rename rpc called from nfs_remove() above
   1927  */
   1928 int
   1929 nfs_renameit(struct vnode *sdvp, struct componentname *scnp, struct sillyrename *sp)
   1930 {
   1931 	return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
   1932 		sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, curlwp));
   1933 }
   1934 
   1935 /*
   1936  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
   1937  */
   1938 int
   1939 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)
   1940 {
   1941 	u_int32_t *tl;
   1942 	char *cp;
   1943 #ifndef NFS_V2_ONLY
   1944 	int32_t t1;
   1945 	char *cp2;
   1946 #endif
   1947 	int32_t t2;
   1948 	char *bpos, *dpos;
   1949 	int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
   1950 	struct mbuf *mreq, *mrep, *md, *mb;
   1951 	const int v3 = NFS_ISV3(fdvp);
   1952 	int rexmit = 0;
   1953 	struct nfsnode *fdnp = VTONFS(fdvp);
   1954 
   1955 	nfsstats.rpccnt[NFSPROC_RENAME]++;
   1956 	nfsm_reqhead(fdnp, NFSPROC_RENAME,
   1957 		(NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
   1958 		nfsm_rndup(tnamelen));
   1959 	nfsm_fhtom(fdnp, v3);
   1960 	nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
   1961 	nfsm_fhtom(VTONFS(tdvp), v3);
   1962 	nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
   1963 	nfsm_request1(fdnp, NFSPROC_RENAME, l, cred, &rexmit);
   1964 #ifndef NFS_V2_ONLY
   1965 	if (v3) {
   1966 		nfsm_wcc_data(fdvp, fwccflag, 0, !error);
   1967 		nfsm_wcc_data(tdvp, twccflag, 0, !error);
   1968 	}
   1969 #endif
   1970 	nfsm_reqdone;
   1971 	VTONFS(fdvp)->n_flag |= NMODIFIED;
   1972 	VTONFS(tdvp)->n_flag |= NMODIFIED;
   1973 	if (!fwccflag)
   1974 		NFS_INVALIDATE_ATTRCACHE(VTONFS(fdvp));
   1975 	if (!twccflag)
   1976 		NFS_INVALIDATE_ATTRCACHE(VTONFS(tdvp));
   1977 	/*
   1978 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
   1979 	 */
   1980 	if (rexmit && error == ENOENT)
   1981 		error = 0;
   1982 	return (error);
   1983 }
   1984 
   1985 /*
   1986  * NFS link RPC, called from nfs_link.
   1987  * Assumes dvp and vp locked, and leaves them that way.
   1988  */
   1989 
   1990 static int
   1991 nfs_linkrpc(struct vnode *dvp, struct vnode *vp, const char *name,
   1992     size_t namelen, kauth_cred_t cred, struct lwp *l)
   1993 {
   1994 	u_int32_t *tl;
   1995 	char *cp;
   1996 #ifndef NFS_V2_ONLY
   1997 	int32_t t1;
   1998 	char *cp2;
   1999 #endif
   2000 	int32_t t2;
   2001 	char *bpos, *dpos;
   2002 	int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
   2003 	struct mbuf *mreq, *mrep, *md, *mb;
   2004 	const int v3 = NFS_ISV3(dvp);
   2005 	int rexmit = 0;
   2006 	struct nfsnode *np = VTONFS(vp);
   2007 
   2008 	nfsstats.rpccnt[NFSPROC_LINK]++;
   2009 	nfsm_reqhead(np, NFSPROC_LINK,
   2010 	    NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(namelen));
   2011 	nfsm_fhtom(np, v3);
   2012 	nfsm_fhtom(VTONFS(dvp), v3);
   2013 	nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
   2014 	nfsm_request1(np, NFSPROC_LINK, l, cred, &rexmit);
   2015 #ifndef NFS_V2_ONLY
   2016 	if (v3) {
   2017 		nfsm_postop_attr(vp, attrflag, 0);
   2018 		nfsm_wcc_data(dvp, wccflag, 0, !error);
   2019 	}
   2020 #endif
   2021 	nfsm_reqdone;
   2022 
   2023 	VTONFS(dvp)->n_flag |= NMODIFIED;
   2024 	if (!attrflag)
   2025 		NFS_INVALIDATE_ATTRCACHE(VTONFS(vp));
   2026 	if (!wccflag)
   2027 		NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
   2028 
   2029 	/*
   2030 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
   2031 	 */
   2032 	if (rexmit && error == EEXIST)
   2033 		error = 0;
   2034 
   2035 	return error;
   2036 }
   2037 
   2038 /*
   2039  * nfs hard link create call
   2040  */
   2041 int
   2042 nfs_link(void *v)
   2043 {
   2044 	struct vop_link_args /* {
   2045 		struct vnode *a_dvp;
   2046 		struct vnode *a_vp;
   2047 		struct componentname *a_cnp;
   2048 	} */ *ap = v;
   2049 	struct vnode *vp = ap->a_vp;
   2050 	struct vnode *dvp = ap->a_dvp;
   2051 	struct componentname *cnp = ap->a_cnp;
   2052 	int error = 0;
   2053 
   2054 	error = vn_lock(vp, LK_EXCLUSIVE);
   2055 	if (error != 0) {
   2056 		VOP_ABORTOP(dvp, cnp);
   2057 		vput(dvp);
   2058 		return error;
   2059 	}
   2060 
   2061 	/*
   2062 	 * Push all writes to the server, so that the attribute cache
   2063 	 * doesn't get "out of sync" with the server.
   2064 	 * XXX There should be a better way!
   2065 	 */
   2066 	VOP_FSYNC(vp, cnp->cn_cred, FSYNC_WAIT, 0, 0);
   2067 
   2068 	error = nfs_linkrpc(dvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
   2069 	    cnp->cn_cred, curlwp);
   2070 
   2071 	if (error == 0) {
   2072 		cache_purge1(dvp, cnp, 0);
   2073 	}
   2074 	VOP_UNLOCK(vp);
   2075 	VN_KNOTE(vp, NOTE_LINK);
   2076 	VN_KNOTE(dvp, NOTE_WRITE);
   2077 	vput(dvp);
   2078 	return (error);
   2079 }
   2080 
   2081 /*
   2082  * nfs symbolic link create call
   2083  */
   2084 int
   2085 nfs_symlink(void *v)
   2086 {
   2087 	struct vop_symlink_args /* {
   2088 		struct vnode *a_dvp;
   2089 		struct vnode **a_vpp;
   2090 		struct componentname *a_cnp;
   2091 		struct vattr *a_vap;
   2092 		char *a_target;
   2093 	} */ *ap = v;
   2094 	struct vnode *dvp = ap->a_dvp;
   2095 	struct vattr *vap = ap->a_vap;
   2096 	struct componentname *cnp = ap->a_cnp;
   2097 	struct nfsv2_sattr *sp;
   2098 	u_int32_t *tl;
   2099 	char *cp;
   2100 	int32_t t1, t2;
   2101 	char *bpos, *dpos, *cp2;
   2102 	int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
   2103 	struct mbuf *mreq, *mrep, *md, *mb;
   2104 	struct vnode *newvp = (struct vnode *)0;
   2105 	const int v3 = NFS_ISV3(dvp);
   2106 	int rexmit = 0;
   2107 	struct nfsnode *dnp = VTONFS(dvp);
   2108 
   2109 	*ap->a_vpp = NULL;
   2110 	nfsstats.rpccnt[NFSPROC_SYMLINK]++;
   2111 	slen = strlen(ap->a_target);
   2112 	nfsm_reqhead(dnp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
   2113 	    nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
   2114 	nfsm_fhtom(dnp, v3);
   2115 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   2116 #ifndef NFS_V2_ONlY
   2117 	if (v3)
   2118 		nfsm_v3attrbuild(vap, false);
   2119 #endif
   2120 	nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
   2121 #ifndef NFS_V2_ONlY
   2122 	if (!v3) {
   2123 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   2124 		sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
   2125 		sp->sa_uid = nfs_xdrneg1;
   2126 		sp->sa_gid = nfs_xdrneg1;
   2127 		sp->sa_size = nfs_xdrneg1;
   2128 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   2129 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   2130 	}
   2131 #endif
   2132 	nfsm_request1(dnp, NFSPROC_SYMLINK, curlwp, cnp->cn_cred,
   2133 	    &rexmit);
   2134 #ifndef NFS_V2_ONlY
   2135 	if (v3) {
   2136 		if (!error)
   2137 			nfsm_mtofh(dvp, newvp, v3, gotvp);
   2138 		nfsm_wcc_data(dvp, wccflag, 0, !error);
   2139 	}
   2140 #endif
   2141 	nfsm_reqdone;
   2142 	/*
   2143 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
   2144 	 */
   2145 	if (rexmit && error == EEXIST)
   2146 		error = 0;
   2147 	if (error == 0 || error == EEXIST)
   2148 		cache_purge1(dvp, cnp, 0);
   2149 	if (error == 0 && newvp == NULL) {
   2150 		struct nfsnode *np = NULL;
   2151 
   2152 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
   2153 		    cnp->cn_cred, curlwp, &np);
   2154 		if (error == 0)
   2155 			newvp = NFSTOV(np);
   2156 	}
   2157 	if (error) {
   2158 		if (newvp != NULL)
   2159 			vput(newvp);
   2160 	} else {
   2161 		*ap->a_vpp = newvp;
   2162 	}
   2163 	VTONFS(dvp)->n_flag |= NMODIFIED;
   2164 	if (!wccflag)
   2165 		NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
   2166 	VN_KNOTE(dvp, NOTE_WRITE);
   2167 	vput(dvp);
   2168 	return (error);
   2169 }
   2170 
   2171 /*
   2172  * nfs make dir call
   2173  */
   2174 int
   2175 nfs_mkdir(void *v)
   2176 {
   2177 	struct vop_mkdir_args /* {
   2178 		struct vnode *a_dvp;
   2179 		struct vnode **a_vpp;
   2180 		struct componentname *a_cnp;
   2181 		struct vattr *a_vap;
   2182 	} */ *ap = v;
   2183 	struct vnode *dvp = ap->a_dvp;
   2184 	struct vattr *vap = ap->a_vap;
   2185 	struct componentname *cnp = ap->a_cnp;
   2186 	struct nfsv2_sattr *sp;
   2187 	u_int32_t *tl;
   2188 	char *cp;
   2189 	int32_t t1, t2;
   2190 	int len;
   2191 	struct nfsnode *dnp = VTONFS(dvp), *np = (struct nfsnode *)0;
   2192 	struct vnode *newvp = (struct vnode *)0;
   2193 	char *bpos, *dpos, *cp2;
   2194 	int error = 0, wccflag = NFSV3_WCCRATTR;
   2195 	int gotvp = 0;
   2196 	int rexmit = 0;
   2197 	struct mbuf *mreq, *mrep, *md, *mb;
   2198 	const int v3 = NFS_ISV3(dvp);
   2199 
   2200 	len = cnp->cn_namelen;
   2201 	nfsstats.rpccnt[NFSPROC_MKDIR]++;
   2202 	nfsm_reqhead(dnp, NFSPROC_MKDIR,
   2203 	  NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
   2204 	nfsm_fhtom(dnp, v3);
   2205 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
   2206 #ifndef NFS_V2_ONLY
   2207 	if (v3) {
   2208 		nfsm_v3attrbuild(vap, false);
   2209 	} else
   2210 #endif
   2211 	{
   2212 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
   2213 		sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
   2214 		sp->sa_uid = nfs_xdrneg1;
   2215 		sp->sa_gid = nfs_xdrneg1;
   2216 		sp->sa_size = nfs_xdrneg1;
   2217 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
   2218 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
   2219 	}
   2220 	nfsm_request1(dnp, NFSPROC_MKDIR, curlwp, cnp->cn_cred, &rexmit);
   2221 	if (!error)
   2222 		nfsm_mtofh(dvp, newvp, v3, gotvp);
   2223 	if (v3)
   2224 		nfsm_wcc_data(dvp, wccflag, 0, !error);
   2225 	nfsm_reqdone;
   2226 	VTONFS(dvp)->n_flag |= NMODIFIED;
   2227 	if (!wccflag)
   2228 		NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
   2229 	/*
   2230 	 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
   2231 	 * if we can succeed in looking up the directory.
   2232 	 */
   2233 	if ((rexmit && error == EEXIST) || (!error && !gotvp)) {
   2234 		if (newvp) {
   2235 			vput(newvp);
   2236 			newvp = (struct vnode *)0;
   2237 		}
   2238 		error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
   2239 			curlwp, &np);
   2240 		if (!error) {
   2241 			newvp = NFSTOV(np);
   2242 			if (newvp->v_type != VDIR || newvp == dvp)
   2243 				error = EEXIST;
   2244 		}
   2245 	}
   2246 	if (error) {
   2247 		if (newvp) {
   2248 			if (dvp != newvp)
   2249 				vput(newvp);
   2250 			else
   2251 				vrele(newvp);
   2252 		}
   2253 	} else {
   2254 		VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
   2255 		nfs_cache_enter(dvp, newvp, cnp);
   2256 		*ap->a_vpp = newvp;
   2257 	}
   2258 	vput(dvp);
   2259 	return (error);
   2260 }
   2261 
   2262 /*
   2263  * nfs remove directory call
   2264  */
   2265 int
   2266 nfs_rmdir(void *v)
   2267 {
   2268 	struct vop_rmdir_args /* {
   2269 		struct vnode *a_dvp;
   2270 		struct vnode *a_vp;
   2271 		struct componentname *a_cnp;
   2272 	} */ *ap = v;
   2273 	struct vnode *vp = ap->a_vp;
   2274 	struct vnode *dvp = ap->a_dvp;
   2275 	struct componentname *cnp = ap->a_cnp;
   2276 	u_int32_t *tl;
   2277 	char *cp;
   2278 #ifndef NFS_V2_ONLY
   2279 	int32_t t1;
   2280 	char *cp2;
   2281 #endif
   2282 	int32_t t2;
   2283 	char *bpos, *dpos;
   2284 	int error = 0, wccflag = NFSV3_WCCRATTR;
   2285 	int rexmit = 0;
   2286 	struct mbuf *mreq, *mrep, *md, *mb;
   2287 	const int v3 = NFS_ISV3(dvp);
   2288 	struct nfsnode *dnp;
   2289 
   2290 	if (dvp == vp) {
   2291 		vrele(dvp);
   2292 		vput(dvp);
   2293 		return (EINVAL);
   2294 	}
   2295 	nfsstats.rpccnt[NFSPROC_RMDIR]++;
   2296 	dnp = VTONFS(dvp);
   2297 	nfsm_reqhead(dnp, NFSPROC_RMDIR,
   2298 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
   2299 	nfsm_fhtom(dnp, v3);
   2300 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
   2301 	nfsm_request1(dnp, NFSPROC_RMDIR, curlwp, cnp->cn_cred, &rexmit);
   2302 #ifndef NFS_V2_ONLY
   2303 	if (v3)
   2304 		nfsm_wcc_data(dvp, wccflag, 0, !error);
   2305 #endif
   2306 	nfsm_reqdone;
   2307 	VTONFS(dvp)->n_flag |= NMODIFIED;
   2308 	if (!wccflag)
   2309 		NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
   2310 	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
   2311 	VN_KNOTE(vp, NOTE_DELETE);
   2312 	cache_purge(vp);
   2313 	vput(vp);
   2314 	vput(dvp);
   2315 	/*
   2316 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
   2317 	 */
   2318 	if (rexmit && error == ENOENT)
   2319 		error = 0;
   2320 	return (error);
   2321 }
   2322 
   2323 /*
   2324  * nfs readdir call
   2325  */
   2326 int
   2327 nfs_readdir(void *v)
   2328 {
   2329 	struct vop_readdir_args /* {
   2330 		struct vnode *a_vp;
   2331 		struct uio *a_uio;
   2332 		kauth_cred_t a_cred;
   2333 		int *a_eofflag;
   2334 		off_t **a_cookies;
   2335 		int *a_ncookies;
   2336 	} */ *ap = v;
   2337 	struct vnode *vp = ap->a_vp;
   2338 	struct uio *uio = ap->a_uio;
   2339 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2340 	char *base = uio->uio_iov->iov_base;
   2341 	int tresid, error;
   2342 	size_t count, lost;
   2343 	struct dirent *dp;
   2344 	off_t *cookies = NULL;
   2345 	int ncookies = 0, nc;
   2346 
   2347 	if (vp->v_type != VDIR)
   2348 		return (EPERM);
   2349 
   2350 	lost = uio->uio_resid & (NFS_DIRFRAGSIZ - 1);
   2351 	count = uio->uio_resid - lost;
   2352 	if (count <= 0)
   2353 		return (EINVAL);
   2354 
   2355 	/*
   2356 	 * Call nfs_bioread() to do the real work.
   2357 	 */
   2358 	tresid = uio->uio_resid = count;
   2359 	error = nfs_bioread(vp, uio, 0, ap->a_cred,
   2360 		    ap->a_cookies ? NFSBIO_CACHECOOKIES : 0);
   2361 
   2362 	if (!error && ap->a_cookies) {
   2363 		ncookies = count / 16;
   2364 		cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK);
   2365 		*ap->a_cookies = cookies;
   2366 	}
   2367 
   2368 	if (!error && uio->uio_resid == tresid) {
   2369 		uio->uio_resid += lost;
   2370 		nfsstats.direofcache_misses++;
   2371 		if (ap->a_cookies)
   2372 			*ap->a_ncookies = 0;
   2373 		*ap->a_eofflag = 1;
   2374 		return (0);
   2375 	}
   2376 
   2377 	if (!error && ap->a_cookies) {
   2378 		/*
   2379 		 * Only the NFS server and emulations use cookies, and they
   2380 		 * load the directory block into system space, so we can
   2381 		 * just look at it directly.
   2382 		 */
   2383 		if (!VMSPACE_IS_KERNEL_P(uio->uio_vmspace) ||
   2384 		    uio->uio_iovcnt != 1)
   2385 			panic("nfs_readdir: lost in space");
   2386 		for (nc = 0; ncookies-- &&
   2387 		     base < (char *)uio->uio_iov->iov_base; nc++){
   2388 			dp = (struct dirent *) base;
   2389 			if (dp->d_reclen == 0)
   2390 				break;
   2391 			if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
   2392 				*(cookies++) = (off_t)NFS_GETCOOKIE32(dp);
   2393 			else
   2394 				*(cookies++) = NFS_GETCOOKIE(dp);
   2395 			base += dp->d_reclen;
   2396 		}
   2397 		uio->uio_resid +=
   2398 		    ((char *)uio->uio_iov->iov_base - base);
   2399 		uio->uio_iov->iov_len +=
   2400 		    ((char *)uio->uio_iov->iov_base - base);
   2401 		uio->uio_iov->iov_base = base;
   2402 		*ap->a_ncookies = nc;
   2403 	}
   2404 
   2405 	uio->uio_resid += lost;
   2406 	*ap->a_eofflag = 0;
   2407 	return (error);
   2408 }
   2409 
   2410 /*
   2411  * Readdir rpc call.
   2412  * Called from below the buffer cache by nfs_doio().
   2413  */
   2414 int
   2415 nfs_readdirrpc(struct vnode *vp, struct uio *uiop, kauth_cred_t cred)
   2416 {
   2417 	int len, left;
   2418 	struct dirent *dp = NULL;
   2419 	u_int32_t *tl;
   2420 	char *cp;
   2421 	int32_t t1, t2;
   2422 	char *bpos, *dpos, *cp2;
   2423 	struct mbuf *mreq, *mrep, *md, *mb;
   2424 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2425 	struct nfsnode *dnp = VTONFS(vp);
   2426 	u_quad_t fileno;
   2427 	int error = 0, more_dirs = 1, blksiz = 0, bigenough = 1;
   2428 #ifndef NFS_V2_ONLY
   2429 	int attrflag;
   2430 #endif
   2431 	int nrpcs = 0, reclen;
   2432 	const int v3 = NFS_ISV3(vp);
   2433 
   2434 #ifdef DIAGNOSTIC
   2435 	/*
   2436 	 * Should be called from buffer cache, so only amount of
   2437 	 * NFS_DIRBLKSIZ will be requested.
   2438 	 */
   2439 	if (uiop->uio_iovcnt != 1 || uiop->uio_resid != NFS_DIRBLKSIZ)
   2440 		panic("nfs readdirrpc bad uio");
   2441 #endif
   2442 
   2443 	/*
   2444 	 * Loop around doing readdir rpc's of size nm_readdirsize
   2445 	 * truncated to a multiple of NFS_DIRFRAGSIZ.
   2446 	 * The stopping criteria is EOF or buffer full.
   2447 	 */
   2448 	while (more_dirs && bigenough) {
   2449 		/*
   2450 		 * Heuristic: don't bother to do another RPC to further
   2451 		 * fill up this block if there is not much room left. (< 50%
   2452 		 * of the readdir RPC size). This wastes some buffer space
   2453 		 * but can save up to 50% in RPC calls.
   2454 		 */
   2455 		if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
   2456 			bigenough = 0;
   2457 			break;
   2458 		}
   2459 		nfsstats.rpccnt[NFSPROC_READDIR]++;
   2460 		nfsm_reqhead(dnp, NFSPROC_READDIR, NFSX_FH(v3) +
   2461 			NFSX_READDIR(v3));
   2462 		nfsm_fhtom(dnp, v3);
   2463 #ifndef NFS_V2_ONLY
   2464 		if (v3) {
   2465 			nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
   2466 			if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
   2467 				txdr_swapcookie3(uiop->uio_offset, tl);
   2468 			} else {
   2469 				txdr_cookie3(uiop->uio_offset, tl);
   2470 			}
   2471 			tl += 2;
   2472 			*tl++ = dnp->n_cookieverf.nfsuquad[0];
   2473 			*tl++ = dnp->n_cookieverf.nfsuquad[1];
   2474 		} else
   2475 #endif
   2476 		{
   2477 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   2478 			*tl++ = txdr_unsigned(uiop->uio_offset);
   2479 		}
   2480 		*tl = txdr_unsigned(nmp->nm_readdirsize);
   2481 		nfsm_request(dnp, NFSPROC_READDIR, curlwp, cred);
   2482 		nrpcs++;
   2483 #ifndef NFS_V2_ONLY
   2484 		if (v3) {
   2485 			nfsm_postop_attr(vp, attrflag, 0);
   2486 			if (!error) {
   2487 				nfsm_dissect(tl, u_int32_t *,
   2488 				    2 * NFSX_UNSIGNED);
   2489 				dnp->n_cookieverf.nfsuquad[0] = *tl++;
   2490 				dnp->n_cookieverf.nfsuquad[1] = *tl;
   2491 			} else {
   2492 				m_freem(mrep);
   2493 				goto nfsmout;
   2494 			}
   2495 		}
   2496 #endif
   2497 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2498 		more_dirs = fxdr_unsigned(int, *tl);
   2499 
   2500 		/* loop thru the dir entries, doctoring them to 4bsd form */
   2501 		while (more_dirs && bigenough) {
   2502 #ifndef NFS_V2_ONLY
   2503 			if (v3) {
   2504 				nfsm_dissect(tl, u_int32_t *,
   2505 				    3 * NFSX_UNSIGNED);
   2506 				fileno = fxdr_hyper(tl);
   2507 				len = fxdr_unsigned(int, *(tl + 2));
   2508 			} else
   2509 #endif
   2510 			{
   2511 				nfsm_dissect(tl, u_int32_t *,
   2512 				    2 * NFSX_UNSIGNED);
   2513 				fileno = fxdr_unsigned(u_quad_t, *tl++);
   2514 				len = fxdr_unsigned(int, *tl);
   2515 			}
   2516 			if (len <= 0 || len > NFS_MAXNAMLEN) {
   2517 				error = EBADRPC;
   2518 				m_freem(mrep);
   2519 				goto nfsmout;
   2520 			}
   2521 			/* for cookie stashing */
   2522 			reclen = _DIRENT_RECLEN(dp, len) + 2 * sizeof(off_t);
   2523 			left = NFS_DIRFRAGSIZ - blksiz;
   2524 			if (reclen > left) {
   2525 				memset(uiop->uio_iov->iov_base, 0, left);
   2526 				dp->d_reclen += left;
   2527 				UIO_ADVANCE(uiop, left);
   2528 				blksiz = 0;
   2529 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2530 			}
   2531 			if (reclen > uiop->uio_resid)
   2532 				bigenough = 0;
   2533 			if (bigenough) {
   2534 				int tlen;
   2535 
   2536 				dp = (struct dirent *)uiop->uio_iov->iov_base;
   2537 				dp->d_fileno = fileno;
   2538 				dp->d_namlen = len;
   2539 				dp->d_reclen = reclen;
   2540 				dp->d_type = DT_UNKNOWN;
   2541 				blksiz += reclen;
   2542 				if (blksiz == NFS_DIRFRAGSIZ)
   2543 					blksiz = 0;
   2544 				UIO_ADVANCE(uiop, DIRHDSIZ);
   2545 				nfsm_mtouio(uiop, len);
   2546 				tlen = reclen - (DIRHDSIZ + len);
   2547 				(void)memset(uiop->uio_iov->iov_base, 0, tlen);
   2548 				UIO_ADVANCE(uiop, tlen);
   2549 			} else
   2550 				nfsm_adv(nfsm_rndup(len));
   2551 #ifndef NFS_V2_ONLY
   2552 			if (v3) {
   2553 				nfsm_dissect(tl, u_int32_t *,
   2554 				    3 * NFSX_UNSIGNED);
   2555 			} else
   2556 #endif
   2557 			{
   2558 				nfsm_dissect(tl, u_int32_t *,
   2559 				    2 * NFSX_UNSIGNED);
   2560 			}
   2561 			if (bigenough) {
   2562 #ifndef NFS_V2_ONLY
   2563 				if (v3) {
   2564 					if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
   2565 						uiop->uio_offset =
   2566 						    fxdr_swapcookie3(tl);
   2567 					else
   2568 						uiop->uio_offset =
   2569 						    fxdr_cookie3(tl);
   2570 				}
   2571 				else
   2572 #endif
   2573 				{
   2574 					uiop->uio_offset =
   2575 					    fxdr_unsigned(off_t, *tl);
   2576 				}
   2577 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2578 			}
   2579 			if (v3)
   2580 				tl += 2;
   2581 			else
   2582 				tl++;
   2583 			more_dirs = fxdr_unsigned(int, *tl);
   2584 		}
   2585 		/*
   2586 		 * If at end of rpc data, get the eof boolean
   2587 		 */
   2588 		if (!more_dirs) {
   2589 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2590 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
   2591 
   2592 			/*
   2593 			 * kludge: if we got no entries, treat it as EOF.
   2594 			 * some server sometimes send a reply without any
   2595 			 * entries or EOF.
   2596 			 * although it might mean the server has very long name,
   2597 			 * we can't handle such entries anyway.
   2598 			 */
   2599 
   2600 			if (uiop->uio_resid >= NFS_DIRBLKSIZ)
   2601 				more_dirs = 0;
   2602 		}
   2603 		m_freem(mrep);
   2604 	}
   2605 	/*
   2606 	 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
   2607 	 * by increasing d_reclen for the last record.
   2608 	 */
   2609 	if (blksiz > 0) {
   2610 		left = NFS_DIRFRAGSIZ - blksiz;
   2611 		memset(uiop->uio_iov->iov_base, 0, left);
   2612 		dp->d_reclen += left;
   2613 		NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2614 		UIO_ADVANCE(uiop, left);
   2615 	}
   2616 
   2617 	/*
   2618 	 * We are now either at the end of the directory or have filled the
   2619 	 * block.
   2620 	 */
   2621 	if (bigenough) {
   2622 		dnp->n_direofoffset = uiop->uio_offset;
   2623 		dnp->n_flag |= NEOFVALID;
   2624 	}
   2625 nfsmout:
   2626 	return (error);
   2627 }
   2628 
   2629 #ifndef NFS_V2_ONLY
   2630 /*
   2631  * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
   2632  */
   2633 int
   2634 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop, kauth_cred_t cred)
   2635 {
   2636 	int len, left;
   2637 	struct dirent *dp = NULL;
   2638 	u_int32_t *tl;
   2639 	char *cp;
   2640 	int32_t t1, t2;
   2641 	struct vnode *newvp;
   2642 	char *bpos, *dpos, *cp2;
   2643 	struct mbuf *mreq, *mrep, *md, *mb;
   2644 	struct nameidata nami, *ndp = &nami;
   2645 	struct componentname *cnp = &ndp->ni_cnd;
   2646 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   2647 	struct nfsnode *dnp = VTONFS(vp), *np;
   2648 	nfsfh_t *fhp;
   2649 	u_quad_t fileno;
   2650 	int error = 0, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
   2651 	int attrflag, fhsize, nrpcs = 0, reclen;
   2652 	struct nfs_fattr fattr, *fp;
   2653 
   2654 #ifdef DIAGNOSTIC
   2655 	if (uiop->uio_iovcnt != 1 || uiop->uio_resid != NFS_DIRBLKSIZ)
   2656 		panic("nfs readdirplusrpc bad uio");
   2657 #endif
   2658 	ndp->ni_dvp = vp;
   2659 	newvp = NULLVP;
   2660 
   2661 	/*
   2662 	 * Loop around doing readdir rpc's of size nm_readdirsize
   2663 	 * truncated to a multiple of NFS_DIRFRAGSIZ.
   2664 	 * The stopping criteria is EOF or buffer full.
   2665 	 */
   2666 	while (more_dirs && bigenough) {
   2667 		if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
   2668 			bigenough = 0;
   2669 			break;
   2670 		}
   2671 		nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
   2672 		nfsm_reqhead(dnp, NFSPROC_READDIRPLUS,
   2673 			NFSX_FH(1) + 6 * NFSX_UNSIGNED);
   2674 		nfsm_fhtom(dnp, 1);
   2675  		nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
   2676 		if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
   2677 			txdr_swapcookie3(uiop->uio_offset, tl);
   2678 		} else {
   2679 			txdr_cookie3(uiop->uio_offset, tl);
   2680 		}
   2681 		tl += 2;
   2682 		*tl++ = dnp->n_cookieverf.nfsuquad[0];
   2683 		*tl++ = dnp->n_cookieverf.nfsuquad[1];
   2684 		*tl++ = txdr_unsigned(nmp->nm_readdirsize);
   2685 		*tl = txdr_unsigned(nmp->nm_rsize);
   2686 		nfsm_request(dnp, NFSPROC_READDIRPLUS, curlwp, cred);
   2687 		nfsm_postop_attr(vp, attrflag, 0);
   2688 		if (error) {
   2689 			m_freem(mrep);
   2690 			goto nfsmout;
   2691 		}
   2692 		nrpcs++;
   2693 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   2694 		dnp->n_cookieverf.nfsuquad[0] = *tl++;
   2695 		dnp->n_cookieverf.nfsuquad[1] = *tl++;
   2696 		more_dirs = fxdr_unsigned(int, *tl);
   2697 
   2698 		/* loop thru the dir entries, doctoring them to 4bsd form */
   2699 		while (more_dirs && bigenough) {
   2700 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   2701 			fileno = fxdr_hyper(tl);
   2702 			len = fxdr_unsigned(int, *(tl + 2));
   2703 			if (len <= 0 || len > NFS_MAXNAMLEN) {
   2704 				error = EBADRPC;
   2705 				m_freem(mrep);
   2706 				goto nfsmout;
   2707 			}
   2708 			/* for cookie stashing */
   2709 			reclen = _DIRENT_RECLEN(dp, len) + 2 * sizeof(off_t);
   2710 			left = NFS_DIRFRAGSIZ - blksiz;
   2711 			if (reclen > left) {
   2712 				/*
   2713 				 * DIRFRAGSIZ is aligned, no need to align
   2714 				 * again here.
   2715 				 */
   2716 				memset(uiop->uio_iov->iov_base, 0, left);
   2717 				dp->d_reclen += left;
   2718 				UIO_ADVANCE(uiop, left);
   2719 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2720 				blksiz = 0;
   2721 			}
   2722 			if (reclen > uiop->uio_resid)
   2723 				bigenough = 0;
   2724 			if (bigenough) {
   2725 				int tlen;
   2726 
   2727 				dp = (struct dirent *)uiop->uio_iov->iov_base;
   2728 				dp->d_fileno = fileno;
   2729 				dp->d_namlen = len;
   2730 				dp->d_reclen = reclen;
   2731 				dp->d_type = DT_UNKNOWN;
   2732 				blksiz += reclen;
   2733 				if (blksiz == NFS_DIRFRAGSIZ)
   2734 					blksiz = 0;
   2735 				UIO_ADVANCE(uiop, DIRHDSIZ);
   2736 				nfsm_mtouio(uiop, len);
   2737 				tlen = reclen - (DIRHDSIZ + len);
   2738 				(void)memset(uiop->uio_iov->iov_base, 0, tlen);
   2739 				UIO_ADVANCE(uiop, tlen);
   2740 				cnp->cn_nameptr = dp->d_name;
   2741 				cnp->cn_namelen = dp->d_namlen;
   2742 			} else
   2743 				nfsm_adv(nfsm_rndup(len));
   2744 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   2745 			if (bigenough) {
   2746 				if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
   2747 					uiop->uio_offset =
   2748 						fxdr_swapcookie3(tl);
   2749 				else
   2750 					uiop->uio_offset =
   2751 						fxdr_cookie3(tl);
   2752 				NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2753 			}
   2754 			tl += 2;
   2755 
   2756 			/*
   2757 			 * Since the attributes are before the file handle
   2758 			 * (sigh), we must skip over the attributes and then
   2759 			 * come back and get them.
   2760 			 */
   2761 			attrflag = fxdr_unsigned(int, *tl);
   2762 			if (attrflag) {
   2763 			    nfsm_dissect(fp, struct nfs_fattr *, NFSX_V3FATTR);
   2764 			    memcpy(&fattr, fp, NFSX_V3FATTR);
   2765 			    nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2766 			    doit = fxdr_unsigned(int, *tl);
   2767 			    if (doit) {
   2768 				nfsm_getfh(fhp, fhsize, 1);
   2769 				if (NFS_CMPFH(dnp, fhp, fhsize)) {
   2770 				    vref(vp);
   2771 				    newvp = vp;
   2772 				    np = dnp;
   2773 				} else {
   2774 				    error = nfs_nget1(vp->v_mount, fhp,
   2775 					fhsize, &np, LK_NOWAIT);
   2776 				    if (!error)
   2777 					newvp = NFSTOV(np);
   2778 				}
   2779 				if (!error) {
   2780 				    const char *xcp;
   2781 
   2782 				    nfs_loadattrcache(&newvp, &fattr, 0, 0);
   2783 				    if (bigenough) {
   2784 					dp->d_type =
   2785 					   IFTODT(VTTOIF(np->n_vattr->va_type));
   2786 					if (cnp->cn_namelen <= NCHNAMLEN) {
   2787 					    ndp->ni_vp = newvp;
   2788 					    xcp = cnp->cn_nameptr +
   2789 						cnp->cn_namelen;
   2790 					    cnp->cn_hash =
   2791 					       namei_hash(cnp->cn_nameptr, &xcp);
   2792 					    nfs_cache_enter(ndp->ni_dvp,
   2793 						ndp->ni_vp, cnp);
   2794 					}
   2795 				    }
   2796 				}
   2797 				error = 0;
   2798 			   }
   2799 			} else {
   2800 			    /* Just skip over the file handle */
   2801 			    nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2802 			    i = fxdr_unsigned(int, *tl);
   2803 			    nfsm_adv(nfsm_rndup(i));
   2804 			}
   2805 			if (newvp != NULLVP) {
   2806 			    if (newvp == vp)
   2807 				vrele(newvp);
   2808 			    else
   2809 				vput(newvp);
   2810 			    newvp = NULLVP;
   2811 			}
   2812 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2813 			more_dirs = fxdr_unsigned(int, *tl);
   2814 		}
   2815 		/*
   2816 		 * If at end of rpc data, get the eof boolean
   2817 		 */
   2818 		if (!more_dirs) {
   2819 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   2820 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
   2821 
   2822 			/*
   2823 			 * kludge: see a comment in nfs_readdirrpc.
   2824 			 */
   2825 
   2826 			if (uiop->uio_resid >= NFS_DIRBLKSIZ)
   2827 				more_dirs = 0;
   2828 		}
   2829 		m_freem(mrep);
   2830 	}
   2831 	/*
   2832 	 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
   2833 	 * by increasing d_reclen for the last record.
   2834 	 */
   2835 	if (blksiz > 0) {
   2836 		left = NFS_DIRFRAGSIZ - blksiz;
   2837 		memset(uiop->uio_iov->iov_base, 0, left);
   2838 		dp->d_reclen += left;
   2839 		NFS_STASHCOOKIE(dp, uiop->uio_offset);
   2840 		UIO_ADVANCE(uiop, left);
   2841 	}
   2842 
   2843 	/*
   2844 	 * We are now either at the end of the directory or have filled the
   2845 	 * block.
   2846 	 */
   2847 	if (bigenough) {
   2848 		dnp->n_direofoffset = uiop->uio_offset;
   2849 		dnp->n_flag |= NEOFVALID;
   2850 	}
   2851 nfsmout:
   2852 	if (newvp != NULLVP) {
   2853 		if(newvp == vp)
   2854 		    vrele(newvp);
   2855 		else
   2856 		    vput(newvp);
   2857 	}
   2858 	return (error);
   2859 }
   2860 #endif
   2861 
   2862 /*
   2863  * Silly rename. To make the NFS filesystem that is stateless look a little
   2864  * more like the "ufs" a remove of an active vnode is translated to a rename
   2865  * to a funny looking filename that is removed by nfs_inactive on the
   2866  * nfsnode. There is the potential for another process on a different client
   2867  * to create the same funny name between the nfs_lookitup() fails and the
   2868  * nfs_rename() completes, but...
   2869  */
   2870 int
   2871 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp, bool dolink)
   2872 {
   2873 	struct sillyrename *sp;
   2874 	struct nfsnode *np;
   2875 	int error;
   2876 	pid_t pid;
   2877 
   2878 	cache_purge(dvp);
   2879 	np = VTONFS(vp);
   2880 #ifndef DIAGNOSTIC
   2881 	if (vp->v_type == VDIR)
   2882 		panic("nfs: sillyrename dir");
   2883 #endif
   2884 	sp = kmem_alloc(sizeof(*sp), KM_SLEEP);
   2885 	sp->s_cred = kauth_cred_dup(cnp->cn_cred);
   2886 	sp->s_dvp = dvp;
   2887 	vref(dvp);
   2888 
   2889 	/* Fudge together a funny name */
   2890 	pid = curlwp->l_proc->p_pid;
   2891 	memcpy(sp->s_name, ".nfsAxxxx4.4", 13);
   2892 	sp->s_namlen = 12;
   2893 	sp->s_name[8] = hexdigits[pid & 0xf];
   2894 	sp->s_name[7] = hexdigits[(pid >> 4) & 0xf];
   2895 	sp->s_name[6] = hexdigits[(pid >> 8) & 0xf];
   2896 	sp->s_name[5] = hexdigits[(pid >> 12) & 0xf];
   2897 
   2898 	/* Try lookitups until we get one that isn't there */
   2899 	while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
   2900 		curlwp, (struct nfsnode **)0) == 0) {
   2901 		sp->s_name[4]++;
   2902 		if (sp->s_name[4] > 'z') {
   2903 			error = EINVAL;
   2904 			goto bad;
   2905 		}
   2906 	}
   2907 	if (dolink) {
   2908 		error = nfs_linkrpc(dvp, vp, sp->s_name, sp->s_namlen,
   2909 		    sp->s_cred, curlwp);
   2910 		/*
   2911 		 * nfs_request maps NFSERR_NOTSUPP to ENOTSUP.
   2912 		 */
   2913 		if (error == ENOTSUP) {
   2914 			error = nfs_renameit(dvp, cnp, sp);
   2915 		}
   2916 	} else {
   2917 		error = nfs_renameit(dvp, cnp, sp);
   2918 	}
   2919 	if (error)
   2920 		goto bad;
   2921 	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
   2922 		curlwp, &np);
   2923 	np->n_sillyrename = sp;
   2924 	return (0);
   2925 bad:
   2926 	vrele(sp->s_dvp);
   2927 	kauth_cred_free(sp->s_cred);
   2928 	kmem_free(sp, sizeof(*sp));
   2929 	return (error);
   2930 }
   2931 
   2932 /*
   2933  * Look up a file name and optionally either update the file handle or
   2934  * allocate an nfsnode, depending on the value of npp.
   2935  * npp == NULL	--> just do the lookup
   2936  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
   2937  *			handled too
   2938  * *npp != NULL --> update the file handle in the vnode
   2939  */
   2940 int
   2941 nfs_lookitup(struct vnode *dvp, const char *name, int len, kauth_cred_t cred, struct lwp *l, struct nfsnode **npp)
   2942 {
   2943 	u_int32_t *tl;
   2944 	char *cp;
   2945 	int32_t t1, t2;
   2946 	struct vnode *newvp = (struct vnode *)0;
   2947 	struct nfsnode *np, *dnp = VTONFS(dvp);
   2948 	char *bpos, *dpos, *cp2;
   2949 	int error = 0, fhlen;
   2950 #ifndef NFS_V2_ONLY
   2951 	int attrflag;
   2952 #endif
   2953 	struct mbuf *mreq, *mrep, *md, *mb;
   2954 	nfsfh_t *nfhp;
   2955 	const int v3 = NFS_ISV3(dvp);
   2956 
   2957 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
   2958 	nfsm_reqhead(dnp, NFSPROC_LOOKUP,
   2959 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
   2960 	nfsm_fhtom(dnp, v3);
   2961 	nfsm_strtom(name, len, NFS_MAXNAMLEN);
   2962 	nfsm_request(dnp, NFSPROC_LOOKUP, l, cred);
   2963 	if (npp && !error) {
   2964 		nfsm_getfh(nfhp, fhlen, v3);
   2965 		if (*npp) {
   2966 		    np = *npp;
   2967 		    if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
   2968 			kmem_free(np->n_fhp, np->n_fhsize);
   2969 			np->n_fhp = &np->n_fh;
   2970 		    }
   2971 #if NFS_SMALLFH < NFSX_V3FHMAX
   2972 		    else if (np->n_fhsize <= NFS_SMALLFH && fhlen > NFS_SMALLFH)
   2973 			np->n_fhp = kmem_alloc(fhlen, KM_SLEEP);
   2974 #endif
   2975 		    memcpy(np->n_fhp, nfhp, fhlen);
   2976 		    np->n_fhsize = fhlen;
   2977 		    newvp = NFSTOV(np);
   2978 		} else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
   2979 		    vref(dvp);
   2980 		    newvp = dvp;
   2981 		    np = dnp;
   2982 		} else {
   2983 		    error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
   2984 		    if (error) {
   2985 			m_freem(mrep);
   2986 			return (error);
   2987 		    }
   2988 		    newvp = NFSTOV(np);
   2989 		}
   2990 #ifndef NFS_V2_ONLY
   2991 		if (v3) {
   2992 			nfsm_postop_attr(newvp, attrflag, 0);
   2993 			if (!attrflag && *npp == NULL) {
   2994 				m_freem(mrep);
   2995 				vput(newvp);
   2996 				return (ENOENT);
   2997 			}
   2998 		} else
   2999 #endif
   3000 			nfsm_loadattr(newvp, (struct vattr *)0, 0);
   3001 	}
   3002 	nfsm_reqdone;
   3003 	if (npp && *npp == NULL) {
   3004 		if (error) {
   3005 			if (newvp)
   3006 				vput(newvp);
   3007 		} else
   3008 			*npp = np;
   3009 	}
   3010 	return (error);
   3011 }
   3012 
   3013 #ifndef NFS_V2_ONLY
   3014 /*
   3015  * Nfs Version 3 commit rpc
   3016  */
   3017 int
   3018 nfs_commit(struct vnode *vp, off_t offset, uint32_t cnt, struct lwp *l)
   3019 {
   3020 	char *cp;
   3021 	u_int32_t *tl;
   3022 	int32_t t1, t2;
   3023 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   3024 	char *bpos, *dpos, *cp2;
   3025 	int error = 0, wccflag = NFSV3_WCCRATTR;
   3026 	struct mbuf *mreq, *mrep, *md, *mb;
   3027 	struct nfsnode *np;
   3028 
   3029 	KASSERT(NFS_ISV3(vp));
   3030 
   3031 #ifdef NFS_DEBUG_COMMIT
   3032 	printf("commit %lu - %lu\n", (unsigned long)offset,
   3033 	    (unsigned long)(offset + cnt));
   3034 #endif
   3035 
   3036 	mutex_enter(&nmp->nm_lock);
   3037 	if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0) {
   3038 		mutex_exit(&nmp->nm_lock);
   3039 		return (0);
   3040 	}
   3041 	mutex_exit(&nmp->nm_lock);
   3042 	nfsstats.rpccnt[NFSPROC_COMMIT]++;
   3043 	np = VTONFS(vp);
   3044 	nfsm_reqhead(np, NFSPROC_COMMIT, NFSX_FH(1));
   3045 	nfsm_fhtom(np, 1);
   3046 	nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   3047 	txdr_hyper(offset, tl);
   3048 	tl += 2;
   3049 	*tl = txdr_unsigned(cnt);
   3050 	nfsm_request(np, NFSPROC_COMMIT, l, np->n_wcred);
   3051 	nfsm_wcc_data(vp, wccflag, NAC_NOTRUNC, false);
   3052 	if (!error) {
   3053 		nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
   3054 		mutex_enter(&nmp->nm_lock);
   3055 		if ((nmp->nm_iflag & NFSMNT_STALEWRITEVERF) ||
   3056 		    memcmp(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF)) {
   3057 			memcpy(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF);
   3058 			error = NFSERR_STALEWRITEVERF;
   3059 			nmp->nm_iflag |= NFSMNT_STALEWRITEVERF;
   3060 		}
   3061 		mutex_exit(&nmp->nm_lock);
   3062 	}
   3063 	nfsm_reqdone;
   3064 	return (error);
   3065 }
   3066 #endif
   3067 
   3068 /*
   3069  * Kludge City..
   3070  * - make nfs_bmap() essentially a no-op that does no translation
   3071  * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
   3072  *   (Maybe I could use the process's page mapping, but I was concerned that
   3073  *    Kernel Write might not be enabled and also figured copyout() would do
   3074  *    a lot more work than memcpy() and also it currently happens in the
   3075  *    context of the swapper process (2).
   3076  */
   3077 int
   3078 nfs_bmap(void *v)
   3079 {
   3080 	struct vop_bmap_args /* {
   3081 		struct vnode *a_vp;
   3082 		daddr_t  a_bn;
   3083 		struct vnode **a_vpp;
   3084 		daddr_t *a_bnp;
   3085 		int *a_runp;
   3086 	} */ *ap = v;
   3087 	struct vnode *vp = ap->a_vp;
   3088 	int bshift = vp->v_mount->mnt_fs_bshift - vp->v_mount->mnt_dev_bshift;
   3089 
   3090 	if (ap->a_vpp != NULL)
   3091 		*ap->a_vpp = vp;
   3092 	if (ap->a_bnp != NULL)
   3093 		*ap->a_bnp = ap->a_bn << bshift;
   3094 	if (ap->a_runp != NULL)
   3095 		*ap->a_runp = 1024 * 1024; /* XXX */
   3096 	return (0);
   3097 }
   3098 
   3099 /*
   3100  * Strategy routine.
   3101  * For async requests when nfsiod(s) are running, queue the request by
   3102  * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
   3103  * request.
   3104  */
   3105 int
   3106 nfs_strategy(void *v)
   3107 {
   3108 	struct vop_strategy_args *ap = v;
   3109 	struct buf *bp = ap->a_bp;
   3110 	int error = 0;
   3111 
   3112 	if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
   3113 		panic("nfs physio/async");
   3114 
   3115 	/*
   3116 	 * If the op is asynchronous and an i/o daemon is waiting
   3117 	 * queue the request, wake it up and wait for completion
   3118 	 * otherwise just do it ourselves.
   3119 	 */
   3120 	if ((bp->b_flags & B_ASYNC) == 0 || nfs_asyncio(bp))
   3121 		error = nfs_doio(bp);
   3122 	return (error);
   3123 }
   3124 
   3125 /*
   3126  * fsync vnode op. Just call nfs_flush() with commit == 1.
   3127  */
   3128 /* ARGSUSED */
   3129 int
   3130 nfs_fsync(void *v)
   3131 {
   3132 	struct vop_fsync_args /* {
   3133 		struct vnodeop_desc *a_desc;
   3134 		struct vnode * a_vp;
   3135 		kauth_cred_t  a_cred;
   3136 		int  a_flags;
   3137 		off_t offlo;
   3138 		off_t offhi;
   3139 		struct lwp * a_l;
   3140 	} */ *ap = v;
   3141 
   3142 	struct vnode *vp = ap->a_vp;
   3143 
   3144 	if (vp->v_type != VREG)
   3145 		return 0;
   3146 
   3147 	return (nfs_flush(vp, ap->a_cred,
   3148 	    (ap->a_flags & FSYNC_WAIT) != 0 ? MNT_WAIT : 0, curlwp, 1));
   3149 }
   3150 
   3151 /*
   3152  * Flush all the data associated with a vnode.
   3153  */
   3154 int
   3155 nfs_flush(struct vnode *vp, kauth_cred_t cred, int waitfor, struct lwp *l,
   3156     int commit)
   3157 {
   3158 	struct nfsnode *np = VTONFS(vp);
   3159 	int error;
   3160 	int flushflags = PGO_ALLPAGES|PGO_CLEANIT|PGO_SYNCIO;
   3161 	UVMHIST_FUNC("nfs_flush"); UVMHIST_CALLED(ubchist);
   3162 
   3163 	mutex_enter(vp->v_interlock);
   3164 	error = VOP_PUTPAGES(vp, 0, 0, flushflags);
   3165 	if (np->n_flag & NWRITEERR) {
   3166 		error = np->n_error;
   3167 		np->n_flag &= ~NWRITEERR;
   3168 	}
   3169 	UVMHIST_LOG(ubchist, "returning %d", error,0,0,0);
   3170 	return (error);
   3171 }
   3172 
   3173 /*
   3174  * Return POSIX pathconf information applicable to nfs.
   3175  *
   3176  * N.B. The NFS V2 protocol doesn't support this RPC.
   3177  */
   3178 /* ARGSUSED */
   3179 int
   3180 nfs_pathconf(void *v)
   3181 {
   3182 	struct vop_pathconf_args /* {
   3183 		struct vnode *a_vp;
   3184 		int a_name;
   3185 		register_t *a_retval;
   3186 	} */ *ap = v;
   3187 	struct nfsv3_pathconf *pcp;
   3188 	struct vnode *vp = ap->a_vp;
   3189 	struct mbuf *mreq, *mrep, *md, *mb;
   3190 	int32_t t1, t2;
   3191 	u_int32_t *tl;
   3192 	char *bpos, *dpos, *cp, *cp2;
   3193 	int error = 0, attrflag;
   3194 #ifndef NFS_V2_ONLY
   3195 	struct nfsmount *nmp;
   3196 	unsigned int l;
   3197 	u_int64_t maxsize;
   3198 #endif
   3199 	const int v3 = NFS_ISV3(vp);
   3200 	struct nfsnode *np = VTONFS(vp);
   3201 
   3202 	switch (ap->a_name) {
   3203 		/* Names that can be resolved locally. */
   3204 	case _PC_PIPE_BUF:
   3205 		*ap->a_retval = PIPE_BUF;
   3206 		break;
   3207 	case _PC_SYNC_IO:
   3208 		*ap->a_retval = 1;
   3209 		break;
   3210 	/* Names that cannot be resolved locally; do an RPC, if possible. */
   3211 	case _PC_LINK_MAX:
   3212 	case _PC_NAME_MAX:
   3213 	case _PC_CHOWN_RESTRICTED:
   3214 	case _PC_NO_TRUNC:
   3215 		if (!v3) {
   3216 			error = EINVAL;
   3217 			break;
   3218 		}
   3219 		nfsstats.rpccnt[NFSPROC_PATHCONF]++;
   3220 		nfsm_reqhead(np, NFSPROC_PATHCONF, NFSX_FH(1));
   3221 		nfsm_fhtom(np, 1);
   3222 		nfsm_request(np, NFSPROC_PATHCONF,
   3223 		    curlwp, curlwp->l_cred);	/* XXX */
   3224 		nfsm_postop_attr(vp, attrflag, 0);
   3225 		if (!error) {
   3226 			nfsm_dissect(pcp, struct nfsv3_pathconf *,
   3227 			    NFSX_V3PATHCONF);
   3228 			switch (ap->a_name) {
   3229 			case _PC_LINK_MAX:
   3230 				*ap->a_retval =
   3231 				    fxdr_unsigned(register_t, pcp->pc_linkmax);
   3232 				break;
   3233 			case _PC_NAME_MAX:
   3234 				*ap->a_retval =
   3235 				    fxdr_unsigned(register_t, pcp->pc_namemax);
   3236 				break;
   3237 			case _PC_CHOWN_RESTRICTED:
   3238 				*ap->a_retval =
   3239 				    (pcp->pc_chownrestricted == nfs_true);
   3240 				break;
   3241 			case _PC_NO_TRUNC:
   3242 				*ap->a_retval =
   3243 				    (pcp->pc_notrunc == nfs_true);
   3244 				break;
   3245 			}
   3246 		}
   3247 		nfsm_reqdone;
   3248 		break;
   3249 	case _PC_FILESIZEBITS:
   3250 #ifndef NFS_V2_ONLY
   3251 		if (v3) {
   3252 			nmp = VFSTONFS(vp->v_mount);
   3253 			if ((nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
   3254 				if ((error = nfs_fsinfo(nmp, vp,
   3255 				    curlwp->l_cred, curlwp)) != 0) /* XXX */
   3256 					break;
   3257 			for (l = 0, maxsize = nmp->nm_maxfilesize;
   3258 			    (maxsize >> l) > 0; l++)
   3259 				;
   3260 			*ap->a_retval = l + 1;
   3261 		} else
   3262 #endif
   3263 		{
   3264 			*ap->a_retval = 32;	/* NFS V2 limitation */
   3265 		}
   3266 		break;
   3267 	default:
   3268 		error = EINVAL;
   3269 		break;
   3270 	}
   3271 
   3272 	return (error);
   3273 }
   3274 
   3275 /*
   3276  * NFS advisory byte-level locks.
   3277  */
   3278 int
   3279 nfs_advlock(void *v)
   3280 {
   3281 	struct vop_advlock_args /* {
   3282 		struct vnode *a_vp;
   3283 		void *a_id;
   3284 		int  a_op;
   3285 		struct flock *a_fl;
   3286 		int  a_flags;
   3287 	} */ *ap = v;
   3288 	struct nfsnode *np = VTONFS(ap->a_vp);
   3289 
   3290 	return lf_advlock(ap, &np->n_lockf, np->n_size);
   3291 }
   3292 
   3293 /*
   3294  * Print out the contents of an nfsnode.
   3295  */
   3296 int
   3297 nfs_print(void *v)
   3298 {
   3299 	struct vop_print_args /* {
   3300 		struct vnode *a_vp;
   3301 	} */ *ap = v;
   3302 	struct vnode *vp = ap->a_vp;
   3303 	struct nfsnode *np = VTONFS(vp);
   3304 
   3305 	printf("tag VT_NFS, fileid %lld fsid 0x%llx",
   3306 	    (unsigned long long)np->n_vattr->va_fileid,
   3307 	    (unsigned long long)np->n_vattr->va_fsid);
   3308 	if (vp->v_type == VFIFO)
   3309 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
   3310 	printf("\n");
   3311 	return (0);
   3312 }
   3313 
   3314 /*
   3315  * nfs unlock wrapper.
   3316  */
   3317 int
   3318 nfs_unlock(void *v)
   3319 {
   3320 	struct vop_unlock_args /* {
   3321 		struct vnode *a_vp;
   3322 		int a_flags;
   3323 	} */ *ap = v;
   3324 	struct vnode *vp = ap->a_vp;
   3325 
   3326 	/*
   3327 	 * VOP_UNLOCK can be called by nfs_loadattrcache
   3328 	 * with v_data == 0.
   3329 	 */
   3330 	if (VTONFS(vp)) {
   3331 		nfs_delayedtruncate(vp);
   3332 	}
   3333 
   3334 	return genfs_unlock(v);
   3335 }
   3336 
   3337 /*
   3338  * nfs special file access vnode op.
   3339  * Essentially just get vattr and then imitate iaccess() since the device is
   3340  * local to the client.
   3341  */
   3342 int
   3343 nfsspec_access(void *v)
   3344 {
   3345 	struct vop_access_args /* {
   3346 		struct vnode *a_vp;
   3347 		int  a_mode;
   3348 		kauth_cred_t a_cred;
   3349 		struct lwp *a_l;
   3350 	} */ *ap = v;
   3351 	struct vattr va;
   3352 	struct vnode *vp = ap->a_vp;
   3353 	int error;
   3354 
   3355 	error = VOP_GETATTR(vp, &va, ap->a_cred);
   3356 	if (error)
   3357 		return (error);
   3358 
   3359         /*
   3360 	 * Disallow write attempts on filesystems mounted read-only;
   3361 	 * unless the file is a socket, fifo, or a block or character
   3362 	 * device resident on the filesystem.
   3363 	 */
   3364 	if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
   3365 		switch (vp->v_type) {
   3366 		case VREG:
   3367 		case VDIR:
   3368 		case VLNK:
   3369 			return (EROFS);
   3370 		default:
   3371 			break;
   3372 		}
   3373 	}
   3374 
   3375 	return kauth_authorize_vnode(ap->a_cred, kauth_access_action(ap->a_mode,
   3376 	    va.va_type, va.va_mode), vp, NULL, genfs_can_access(va.va_type,
   3377 	    va.va_mode, va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
   3378 }
   3379 
   3380 /*
   3381  * Read wrapper for special devices.
   3382  */
   3383 int
   3384 nfsspec_read(void *v)
   3385 {
   3386 	struct vop_read_args /* {
   3387 		struct vnode *a_vp;
   3388 		struct uio *a_uio;
   3389 		int  a_ioflag;
   3390 		kauth_cred_t a_cred;
   3391 	} */ *ap = v;
   3392 	struct nfsnode *np = VTONFS(ap->a_vp);
   3393 
   3394 	/*
   3395 	 * Set access flag.
   3396 	 */
   3397 	np->n_flag |= NACC;
   3398 	getnanotime(&np->n_atim);
   3399 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
   3400 }
   3401 
   3402 /*
   3403  * Write wrapper for special devices.
   3404  */
   3405 int
   3406 nfsspec_write(void *v)
   3407 {
   3408 	struct vop_write_args /* {
   3409 		struct vnode *a_vp;
   3410 		struct uio *a_uio;
   3411 		int  a_ioflag;
   3412 		kauth_cred_t a_cred;
   3413 	} */ *ap = v;
   3414 	struct nfsnode *np = VTONFS(ap->a_vp);
   3415 
   3416 	/*
   3417 	 * Set update flag.
   3418 	 */
   3419 	np->n_flag |= NUPD;
   3420 	getnanotime(&np->n_mtim);
   3421 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
   3422 }
   3423 
   3424 /*
   3425  * Close wrapper for special devices.
   3426  *
   3427  * Update the times on the nfsnode then do device close.
   3428  */
   3429 int
   3430 nfsspec_close(void *v)
   3431 {
   3432 	struct vop_close_args /* {
   3433 		struct vnode *a_vp;
   3434 		int  a_fflag;
   3435 		kauth_cred_t a_cred;
   3436 		struct lwp *a_l;
   3437 	} */ *ap = v;
   3438 	struct vnode *vp = ap->a_vp;
   3439 	struct nfsnode *np = VTONFS(vp);
   3440 	struct vattr vattr;
   3441 
   3442 	if (np->n_flag & (NACC | NUPD)) {
   3443 		np->n_flag |= NCHG;
   3444 		if (vp->v_usecount == 1 &&
   3445 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
   3446 			vattr_null(&vattr);
   3447 			if (np->n_flag & NACC)
   3448 				vattr.va_atime = np->n_atim;
   3449 			if (np->n_flag & NUPD)
   3450 				vattr.va_mtime = np->n_mtim;
   3451 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred);
   3452 		}
   3453 	}
   3454 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
   3455 }
   3456 
   3457 /*
   3458  * Read wrapper for fifos.
   3459  */
   3460 int
   3461 nfsfifo_read(void *v)
   3462 {
   3463 	struct vop_read_args /* {
   3464 		struct vnode *a_vp;
   3465 		struct uio *a_uio;
   3466 		int  a_ioflag;
   3467 		kauth_cred_t a_cred;
   3468 	} */ *ap = v;
   3469 	struct nfsnode *np = VTONFS(ap->a_vp);
   3470 
   3471 	/*
   3472 	 * Set access flag.
   3473 	 */
   3474 	np->n_flag |= NACC;
   3475 	getnanotime(&np->n_atim);
   3476 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
   3477 }
   3478 
   3479 /*
   3480  * Write wrapper for fifos.
   3481  */
   3482 int
   3483 nfsfifo_write(void *v)
   3484 {
   3485 	struct vop_write_args /* {
   3486 		struct vnode *a_vp;
   3487 		struct uio *a_uio;
   3488 		int  a_ioflag;
   3489 		kauth_cred_t a_cred;
   3490 	} */ *ap = v;
   3491 	struct nfsnode *np = VTONFS(ap->a_vp);
   3492 
   3493 	/*
   3494 	 * Set update flag.
   3495 	 */
   3496 	np->n_flag |= NUPD;
   3497 	getnanotime(&np->n_mtim);
   3498 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
   3499 }
   3500 
   3501 /*
   3502  * Close wrapper for fifos.
   3503  *
   3504  * Update the times on the nfsnode then do fifo close.
   3505  */
   3506 int
   3507 nfsfifo_close(void *v)
   3508 {
   3509 	struct vop_close_args /* {
   3510 		struct vnode *a_vp;
   3511 		int  a_fflag;
   3512 		kauth_cred_t a_cred;
   3513 		struct lwp *a_l;
   3514 	} */ *ap = v;
   3515 	struct vnode *vp = ap->a_vp;
   3516 	struct nfsnode *np = VTONFS(vp);
   3517 	struct vattr vattr;
   3518 
   3519 	if (np->n_flag & (NACC | NUPD)) {
   3520 		struct timespec ts;
   3521 
   3522 		getnanotime(&ts);
   3523 		if (np->n_flag & NACC)
   3524 			np->n_atim = ts;
   3525 		if (np->n_flag & NUPD)
   3526 			np->n_mtim = ts;
   3527 		np->n_flag |= NCHG;
   3528 		if (vp->v_usecount == 1 &&
   3529 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
   3530 			vattr_null(&vattr);
   3531 			if (np->n_flag & NACC)
   3532 				vattr.va_atime = np->n_atim;
   3533 			if (np->n_flag & NUPD)
   3534 				vattr.va_mtime = np->n_mtim;
   3535 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred);
   3536 		}
   3537 	}
   3538 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
   3539 }
   3540