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