xref: /freebsd/sys/fs/nfsclient/nfs_clvnops.c (revision 1edb7116)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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  *	from nfs_vnops.c	8.16 (Berkeley) 5/27/95
35  */
36 
37 #include <sys/cdefs.h>
38 /*
39  * vnode op calls for Sun NFS version 2, 3 and 4
40  */
41 
42 #include "opt_inet.h"
43 
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/systm.h>
47 #include <sys/resourcevar.h>
48 #include <sys/proc.h>
49 #include <sys/mount.h>
50 #include <sys/bio.h>
51 #include <sys/buf.h>
52 #include <sys/extattr.h>
53 #include <sys/filio.h>
54 #include <sys/jail.h>
55 #include <sys/malloc.h>
56 #include <sys/mbuf.h>
57 #include <sys/namei.h>
58 #include <sys/socket.h>
59 #include <sys/vnode.h>
60 #include <sys/dirent.h>
61 #include <sys/fcntl.h>
62 #include <sys/lockf.h>
63 #include <sys/stat.h>
64 #include <sys/sysctl.h>
65 #include <sys/signalvar.h>
66 
67 #include <vm/vm.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_object.h>
70 #include <vm/vnode_pager.h>
71 
72 #include <fs/nfs/nfsport.h>
73 #include <fs/nfsclient/nfsnode.h>
74 #include <fs/nfsclient/nfsmount.h>
75 #include <fs/nfsclient/nfs.h>
76 #include <fs/nfsclient/nfs_kdtrace.h>
77 
78 #include <net/if.h>
79 #include <netinet/in.h>
80 #include <netinet/in_var.h>
81 
82 #include <nfs/nfs_lock.h>
83 
84 #ifdef KDTRACE_HOOKS
85 #include <sys/dtrace_bsd.h>
86 
87 dtrace_nfsclient_accesscache_flush_probe_func_t
88 		dtrace_nfscl_accesscache_flush_done_probe;
89 uint32_t	nfscl_accesscache_flush_done_id;
90 
91 dtrace_nfsclient_accesscache_get_probe_func_t
92 		dtrace_nfscl_accesscache_get_hit_probe,
93 		dtrace_nfscl_accesscache_get_miss_probe;
94 uint32_t	nfscl_accesscache_get_hit_id;
95 uint32_t	nfscl_accesscache_get_miss_id;
96 
97 dtrace_nfsclient_accesscache_load_probe_func_t
98 		dtrace_nfscl_accesscache_load_done_probe;
99 uint32_t	nfscl_accesscache_load_done_id;
100 #endif /* !KDTRACE_HOOKS */
101 
102 /* Defs */
103 #define	TRUE	1
104 #define	FALSE	0
105 
106 extern struct nfsstatsv1 nfsstatsv1;
107 extern int nfsrv_useacl;
108 extern int nfscl_debuglevel;
109 MALLOC_DECLARE(M_NEWNFSREQ);
110 
111 static vop_read_t	nfsfifo_read;
112 static vop_write_t	nfsfifo_write;
113 static vop_close_t	nfsfifo_close;
114 static int	nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
115 		    struct thread *);
116 static vop_lookup_t	nfs_lookup;
117 static vop_create_t	nfs_create;
118 static vop_mknod_t	nfs_mknod;
119 static vop_open_t	nfs_open;
120 static vop_pathconf_t	nfs_pathconf;
121 static vop_close_t	nfs_close;
122 static vop_access_t	nfs_access;
123 static vop_getattr_t	nfs_getattr;
124 static vop_setattr_t	nfs_setattr;
125 static vop_read_t	nfs_read;
126 static vop_fsync_t	nfs_fsync;
127 static vop_remove_t	nfs_remove;
128 static vop_link_t	nfs_link;
129 static vop_rename_t	nfs_rename;
130 static vop_mkdir_t	nfs_mkdir;
131 static vop_rmdir_t	nfs_rmdir;
132 static vop_symlink_t	nfs_symlink;
133 static vop_readdir_t	nfs_readdir;
134 static vop_strategy_t	nfs_strategy;
135 static	int	nfs_lookitup(struct vnode *, char *, int,
136 		    struct ucred *, struct thread *, struct nfsnode **);
137 static	int	nfs_sillyrename(struct vnode *, struct vnode *,
138 		    struct componentname *);
139 static vop_access_t	nfsspec_access;
140 static vop_readlink_t	nfs_readlink;
141 static vop_print_t	nfs_print;
142 static vop_advlock_t	nfs_advlock;
143 static vop_advlockasync_t nfs_advlockasync;
144 static vop_getacl_t nfs_getacl;
145 static vop_setacl_t nfs_setacl;
146 static vop_advise_t nfs_advise;
147 static vop_allocate_t nfs_allocate;
148 static vop_deallocate_t nfs_deallocate;
149 static vop_copy_file_range_t nfs_copy_file_range;
150 static vop_ioctl_t nfs_ioctl;
151 static vop_getextattr_t nfs_getextattr;
152 static vop_setextattr_t nfs_setextattr;
153 static vop_listextattr_t nfs_listextattr;
154 static vop_deleteextattr_t nfs_deleteextattr;
155 static vop_lock1_t	nfs_lock;
156 
157 /*
158  * Global vfs data structures for nfs
159  */
160 
161 static struct vop_vector newnfs_vnodeops_nosig = {
162 	.vop_default =		&default_vnodeops,
163 	.vop_access =		nfs_access,
164 	.vop_advlock =		nfs_advlock,
165 	.vop_advlockasync =	nfs_advlockasync,
166 	.vop_close =		nfs_close,
167 	.vop_create =		nfs_create,
168 	.vop_fsync =		nfs_fsync,
169 	.vop_getattr =		nfs_getattr,
170 	.vop_getpages =		ncl_getpages,
171 	.vop_putpages =		ncl_putpages,
172 	.vop_inactive =		ncl_inactive,
173 	.vop_link =		nfs_link,
174 	.vop_lock1 =		nfs_lock,
175 	.vop_lookup =		nfs_lookup,
176 	.vop_mkdir =		nfs_mkdir,
177 	.vop_mknod =		nfs_mknod,
178 	.vop_open =		nfs_open,
179 	.vop_pathconf =		nfs_pathconf,
180 	.vop_print =		nfs_print,
181 	.vop_read =		nfs_read,
182 	.vop_readdir =		nfs_readdir,
183 	.vop_readlink =		nfs_readlink,
184 	.vop_reclaim =		ncl_reclaim,
185 	.vop_remove =		nfs_remove,
186 	.vop_rename =		nfs_rename,
187 	.vop_rmdir =		nfs_rmdir,
188 	.vop_setattr =		nfs_setattr,
189 	.vop_strategy =		nfs_strategy,
190 	.vop_symlink =		nfs_symlink,
191 	.vop_write =		ncl_write,
192 	.vop_getacl =		nfs_getacl,
193 	.vop_setacl =		nfs_setacl,
194 	.vop_advise =		nfs_advise,
195 	.vop_allocate =		nfs_allocate,
196 	.vop_deallocate =	nfs_deallocate,
197 	.vop_copy_file_range =	nfs_copy_file_range,
198 	.vop_ioctl =		nfs_ioctl,
199 	.vop_getextattr =	nfs_getextattr,
200 	.vop_setextattr =	nfs_setextattr,
201 	.vop_listextattr =	nfs_listextattr,
202 	.vop_deleteextattr =	nfs_deleteextattr,
203 };
204 VFS_VOP_VECTOR_REGISTER(newnfs_vnodeops_nosig);
205 
206 static int
207 nfs_vnodeops_bypass(struct vop_generic_args *a)
208 {
209 
210 	return (vop_sigdefer(&newnfs_vnodeops_nosig, a));
211 }
212 
213 struct vop_vector newnfs_vnodeops = {
214 	.vop_default =		&default_vnodeops,
215 	.vop_bypass =		nfs_vnodeops_bypass,
216 };
217 VFS_VOP_VECTOR_REGISTER(newnfs_vnodeops);
218 
219 static struct vop_vector newnfs_fifoops_nosig = {
220 	.vop_default =		&fifo_specops,
221 	.vop_access =		nfsspec_access,
222 	.vop_close =		nfsfifo_close,
223 	.vop_fsync =		nfs_fsync,
224 	.vop_getattr =		nfs_getattr,
225 	.vop_inactive =		ncl_inactive,
226 	.vop_pathconf =		nfs_pathconf,
227 	.vop_print =		nfs_print,
228 	.vop_read =		nfsfifo_read,
229 	.vop_reclaim =		ncl_reclaim,
230 	.vop_setattr =		nfs_setattr,
231 	.vop_write =		nfsfifo_write,
232 };
233 VFS_VOP_VECTOR_REGISTER(newnfs_fifoops_nosig);
234 
235 static int
236 nfs_fifoops_bypass(struct vop_generic_args *a)
237 {
238 
239 	return (vop_sigdefer(&newnfs_fifoops_nosig, a));
240 }
241 
242 struct vop_vector newnfs_fifoops = {
243 	.vop_default =		&default_vnodeops,
244 	.vop_bypass =		nfs_fifoops_bypass,
245 };
246 VFS_VOP_VECTOR_REGISTER(newnfs_fifoops);
247 
248 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
249     struct componentname *cnp, struct vattr *vap);
250 static int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
251     int namelen, struct ucred *cred, struct thread *td);
252 static int nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp,
253     char *fnameptr, int fnamelen, struct vnode *tdvp, struct vnode *tvp,
254     char *tnameptr, int tnamelen, struct ucred *cred, struct thread *td);
255 static int nfs_renameit(struct vnode *sdvp, struct vnode *svp,
256     struct componentname *scnp, struct sillyrename *sp);
257 
258 /*
259  * Global variables
260  */
261 SYSCTL_DECL(_vfs_nfs);
262 
263 static int	nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
264 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
265 	   &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
266 
267 static int	nfs_prime_access_cache = 0;
268 SYSCTL_INT(_vfs_nfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
269 	   &nfs_prime_access_cache, 0,
270 	   "Prime NFS ACCESS cache when fetching attributes");
271 
272 static int	newnfs_commit_on_close = 0;
273 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
274     &newnfs_commit_on_close, 0, "write+commit on close, else only write");
275 
276 static int	nfs_clean_pages_on_close = 1;
277 SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
278 	   &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
279 
280 int newnfs_directio_enable = 0;
281 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
282 	   &newnfs_directio_enable, 0, "Enable NFS directio");
283 
284 int nfs_keep_dirty_on_error;
285 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_keep_dirty_on_error, CTLFLAG_RW,
286     &nfs_keep_dirty_on_error, 0, "Retry pageout if error returned");
287 
288 /*
289  * This sysctl allows other processes to mmap a file that has been opened
290  * O_DIRECT by a process.  In general, having processes mmap the file while
291  * Direct IO is in progress can lead to Data Inconsistencies.  But, we allow
292  * this by default to prevent DoS attacks - to prevent a malicious user from
293  * opening up files O_DIRECT preventing other users from mmap'ing these
294  * files.  "Protected" environments where stricter consistency guarantees are
295  * required can disable this knob.  The process that opened the file O_DIRECT
296  * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
297  * meaningful.
298  */
299 int newnfs_directio_allow_mmap = 1;
300 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
301 	   &newnfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
302 
303 static uint64_t	nfs_maxalloclen = 64 * 1024 * 1024;
304 SYSCTL_U64(_vfs_nfs, OID_AUTO, maxalloclen, CTLFLAG_RW,
305 	   &nfs_maxalloclen, 0, "NFS max allocate/deallocate length");
306 
307 #define	NFSACCESS_ALL (NFSACCESS_READ | NFSACCESS_MODIFY		\
308 			 | NFSACCESS_EXTEND | NFSACCESS_EXECUTE	\
309 			 | NFSACCESS_DELETE | NFSACCESS_LOOKUP)
310 
311 /*
312  * SMP Locking Note :
313  * The list of locks after the description of the lock is the ordering
314  * of other locks acquired with the lock held.
315  * np->n_mtx : Protects the fields in the nfsnode.
316        VM Object Lock
317        VI_MTX (acquired indirectly)
318  * nmp->nm_mtx : Protects the fields in the nfsmount.
319        rep->r_mtx
320  * ncl_iod_mutex : Global lock, protects shared nfsiod state.
321  * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
322        nmp->nm_mtx
323        rep->r_mtx
324  * rep->r_mtx : Protects the fields in an nfsreq.
325  */
326 
327 static int
328 nfs_lock(struct vop_lock1_args *ap)
329 {
330 	struct vnode *vp;
331 	struct nfsnode *np;
332 	u_quad_t nsize;
333 	int error, lktype;
334 	bool onfault;
335 
336 	vp = ap->a_vp;
337 	lktype = ap->a_flags & LK_TYPE_MASK;
338 	error = VOP_LOCK1_APV(&default_vnodeops, ap);
339 	if (error != 0 || vp->v_op != &newnfs_vnodeops)
340 		return (error);
341 	np = VTONFS(vp);
342 	if (np == NULL)
343 		return (0);
344 	NFSLOCKNODE(np);
345 	if ((np->n_flag & NVNSETSZSKIP) == 0 || (lktype != LK_SHARED &&
346 	    lktype != LK_EXCLUSIVE && lktype != LK_UPGRADE &&
347 	    lktype != LK_TRYUPGRADE)) {
348 		NFSUNLOCKNODE(np);
349 		return (0);
350 	}
351 	onfault = (ap->a_flags & LK_EATTR_MASK) == LK_NOWAIT &&
352 	    (ap->a_flags & LK_INIT_MASK) == LK_CANRECURSE &&
353 	    (lktype == LK_SHARED || lktype == LK_EXCLUSIVE);
354 	if (onfault && vp->v_vnlock->lk_recurse == 0) {
355 		/*
356 		 * Force retry in vm_fault(), to make the lock request
357 		 * sleepable, which allows us to piggy-back the
358 		 * sleepable call to vnode_pager_setsize().
359 		 */
360 		NFSUNLOCKNODE(np);
361 		VOP_UNLOCK(vp);
362 		return (EBUSY);
363 	}
364 	if ((ap->a_flags & LK_NOWAIT) != 0 ||
365 	    (lktype == LK_SHARED && vp->v_vnlock->lk_recurse > 0)) {
366 		NFSUNLOCKNODE(np);
367 		return (0);
368 	}
369 	if (lktype == LK_SHARED) {
370 		NFSUNLOCKNODE(np);
371 		VOP_UNLOCK(vp);
372 		ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK);
373 		ap->a_flags |= LK_EXCLUSIVE;
374 		error = VOP_LOCK1_APV(&default_vnodeops, ap);
375 		if (error != 0 || vp->v_op != &newnfs_vnodeops)
376 			return (error);
377 		if (vp->v_data == NULL)
378 			goto downgrade;
379 		MPASS(vp->v_data == np);
380 		NFSLOCKNODE(np);
381 		if ((np->n_flag & NVNSETSZSKIP) == 0) {
382 			NFSUNLOCKNODE(np);
383 			goto downgrade;
384 		}
385 	}
386 	np->n_flag &= ~NVNSETSZSKIP;
387 	nsize = np->n_size;
388 	NFSUNLOCKNODE(np);
389 	vnode_pager_setsize(vp, nsize);
390 downgrade:
391 	if (lktype == LK_SHARED) {
392 		ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK);
393 		ap->a_flags |= LK_DOWNGRADE;
394 		(void)VOP_LOCK1_APV(&default_vnodeops, ap);
395 	}
396 	return (0);
397 }
398 
399 static int
400 nfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
401     struct ucred *cred, u_int32_t *retmode)
402 {
403 	int error = 0, attrflag, i, lrupos;
404 	u_int32_t rmode;
405 	struct nfsnode *np = VTONFS(vp);
406 	struct nfsvattr nfsva;
407 
408 	error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
409 	    &rmode);
410 	if (attrflag)
411 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
412 	if (!error) {
413 		lrupos = 0;
414 		NFSLOCKNODE(np);
415 		for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
416 			if (np->n_accesscache[i].uid == cred->cr_uid) {
417 				np->n_accesscache[i].mode = rmode;
418 				np->n_accesscache[i].stamp = time_second;
419 				break;
420 			}
421 			if (i > 0 && np->n_accesscache[i].stamp <
422 			    np->n_accesscache[lrupos].stamp)
423 				lrupos = i;
424 		}
425 		if (i == NFS_ACCESSCACHESIZE) {
426 			np->n_accesscache[lrupos].uid = cred->cr_uid;
427 			np->n_accesscache[lrupos].mode = rmode;
428 			np->n_accesscache[lrupos].stamp = time_second;
429 		}
430 		NFSUNLOCKNODE(np);
431 		if (retmode != NULL)
432 			*retmode = rmode;
433 		KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, rmode, 0);
434 	} else if (NFS_ISV4(vp)) {
435 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
436 	}
437 #ifdef KDTRACE_HOOKS
438 	if (error != 0)
439 		KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, 0,
440 		    error);
441 #endif
442 	return (error);
443 }
444 
445 /*
446  * nfs access vnode op.
447  * For nfs version 2, just return ok. File accesses may fail later.
448  * For nfs version 3, use the access rpc to check accessibility. If file modes
449  * are changed on the server, accesses might still fail later.
450  */
451 static int
452 nfs_access(struct vop_access_args *ap)
453 {
454 	struct vnode *vp = ap->a_vp;
455 	int error = 0, i, gotahit;
456 	u_int32_t mode, wmode, rmode;
457 	int v34 = NFS_ISV34(vp);
458 	struct nfsnode *np = VTONFS(vp);
459 
460 	/*
461 	 * Disallow write attempts on filesystems mounted read-only;
462 	 * unless the file is a socket, fifo, or a block or character
463 	 * device resident on the filesystem.
464 	 */
465 	if ((ap->a_accmode & (VWRITE | VAPPEND | VWRITE_NAMED_ATTRS |
466 	    VDELETE_CHILD | VWRITE_ATTRIBUTES | VDELETE | VWRITE_ACL |
467 	    VWRITE_OWNER)) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
468 		switch (vp->v_type) {
469 		case VREG:
470 		case VDIR:
471 		case VLNK:
472 			return (EROFS);
473 		default:
474 			break;
475 		}
476 	}
477 	/*
478 	 * For nfs v3 or v4, check to see if we have done this recently, and if
479 	 * so return our cached result instead of making an ACCESS call.
480 	 * If not, do an access rpc, otherwise you are stuck emulating
481 	 * ufs_access() locally using the vattr. This may not be correct,
482 	 * since the server may apply other access criteria such as
483 	 * client uid-->server uid mapping that we do not know about.
484 	 */
485 	if (v34) {
486 		if (ap->a_accmode & VREAD)
487 			mode = NFSACCESS_READ;
488 		else
489 			mode = 0;
490 		if (vp->v_type != VDIR) {
491 			if (ap->a_accmode & VWRITE)
492 				mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
493 			if (ap->a_accmode & VAPPEND)
494 				mode |= NFSACCESS_EXTEND;
495 			if (ap->a_accmode & VEXEC)
496 				mode |= NFSACCESS_EXECUTE;
497 			if (ap->a_accmode & VDELETE)
498 				mode |= NFSACCESS_DELETE;
499 		} else {
500 			if (ap->a_accmode & VWRITE)
501 				mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
502 			if (ap->a_accmode & VAPPEND)
503 				mode |= NFSACCESS_EXTEND;
504 			if (ap->a_accmode & VEXEC)
505 				mode |= NFSACCESS_LOOKUP;
506 			if (ap->a_accmode & VDELETE)
507 				mode |= NFSACCESS_DELETE;
508 			if (ap->a_accmode & VDELETE_CHILD)
509 				mode |= NFSACCESS_MODIFY;
510 		}
511 		/* XXX safety belt, only make blanket request if caching */
512 		if (nfsaccess_cache_timeout > 0) {
513 			wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
514 				NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
515 				NFSACCESS_DELETE | NFSACCESS_LOOKUP;
516 		} else {
517 			wmode = mode;
518 		}
519 
520 		/*
521 		 * Does our cached result allow us to give a definite yes to
522 		 * this request?
523 		 */
524 		gotahit = 0;
525 		NFSLOCKNODE(np);
526 		for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
527 			if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
528 			    if (time_second < (np->n_accesscache[i].stamp
529 				+ nfsaccess_cache_timeout) &&
530 				(np->n_accesscache[i].mode & mode) == mode) {
531 				NFSINCRGLOBAL(nfsstatsv1.accesscache_hits);
532 				gotahit = 1;
533 			    }
534 			    break;
535 			}
536 		}
537 		NFSUNLOCKNODE(np);
538 #ifdef KDTRACE_HOOKS
539 		if (gotahit != 0)
540 			KDTRACE_NFS_ACCESSCACHE_GET_HIT(vp,
541 			    ap->a_cred->cr_uid, mode);
542 		else
543 			KDTRACE_NFS_ACCESSCACHE_GET_MISS(vp,
544 			    ap->a_cred->cr_uid, mode);
545 #endif
546 		if (gotahit == 0) {
547 			/*
548 			 * Either a no, or a don't know.  Go to the wire.
549 			 */
550 			NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
551 		        error = nfs34_access_otw(vp, wmode, ap->a_td,
552 			    ap->a_cred, &rmode);
553 			if (!error &&
554 			    (rmode & mode) != mode)
555 				error = EACCES;
556 		}
557 		return (error);
558 	} else {
559 		if ((error = nfsspec_access(ap)) != 0) {
560 			return (error);
561 		}
562 		/*
563 		 * Attempt to prevent a mapped root from accessing a file
564 		 * which it shouldn't.  We try to read a byte from the file
565 		 * if the user is root and the file is not zero length.
566 		 * After calling nfsspec_access, we should have the correct
567 		 * file size cached.
568 		 */
569 		NFSLOCKNODE(np);
570 		if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
571 		    && VTONFS(vp)->n_size > 0) {
572 			struct iovec aiov;
573 			struct uio auio;
574 			char buf[1];
575 
576 			NFSUNLOCKNODE(np);
577 			aiov.iov_base = buf;
578 			aiov.iov_len = 1;
579 			auio.uio_iov = &aiov;
580 			auio.uio_iovcnt = 1;
581 			auio.uio_offset = 0;
582 			auio.uio_resid = 1;
583 			auio.uio_segflg = UIO_SYSSPACE;
584 			auio.uio_rw = UIO_READ;
585 			auio.uio_td = ap->a_td;
586 
587 			if (vp->v_type == VREG)
588 				error = ncl_readrpc(vp, &auio, ap->a_cred);
589 			else if (vp->v_type == VDIR) {
590 				char* bp;
591 				bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
592 				aiov.iov_base = bp;
593 				aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
594 				error = ncl_readdirrpc(vp, &auio, ap->a_cred,
595 				    ap->a_td);
596 				free(bp, M_TEMP);
597 			} else if (vp->v_type == VLNK)
598 				error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
599 			else
600 				error = EACCES;
601 		} else
602 			NFSUNLOCKNODE(np);
603 		return (error);
604 	}
605 }
606 
607 /*
608  * nfs open vnode op
609  * Check to see if the type is ok
610  * and that deletion is not in progress.
611  * For paged in text files, you will need to flush the page cache
612  * if consistency is lost.
613  */
614 /* ARGSUSED */
615 static int
616 nfs_open(struct vop_open_args *ap)
617 {
618 	struct vnode *vp = ap->a_vp;
619 	struct nfsnode *np = VTONFS(vp);
620 	struct vattr vattr;
621 	int error;
622 	int fmode = ap->a_mode;
623 	struct ucred *cred;
624 	vm_object_t obj;
625 
626 	if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
627 		return (EOPNOTSUPP);
628 
629 	/*
630 	 * For NFSv4, we need to do the Open Op before cache validation,
631 	 * so that we conform to RFC3530 Sec. 9.3.1.
632 	 */
633 	if (NFS_ISV4(vp)) {
634 		error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
635 		if (error) {
636 			error = nfscl_maperr(ap->a_td, error, (uid_t)0,
637 			    (gid_t)0);
638 			return (error);
639 		}
640 	}
641 
642 	/*
643 	 * Now, if this Open will be doing reading, re-validate/flush the
644 	 * cache, so that Close/Open coherency is maintained.
645 	 */
646 	NFSLOCKNODE(np);
647 	if (np->n_flag & NMODIFIED) {
648 		NFSUNLOCKNODE(np);
649 		if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
650 			NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
651 			if (VN_IS_DOOMED(vp))
652 				return (EBADF);
653 		}
654 		error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
655 		if (error == EINTR || error == EIO) {
656 			if (NFS_ISV4(vp))
657 				(void) nfsrpc_close(vp, 0, ap->a_td);
658 			return (error);
659 		}
660 		NFSLOCKNODE(np);
661 		np->n_attrstamp = 0;
662 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
663 		if (vp->v_type == VDIR)
664 			np->n_direofoffset = 0;
665 		NFSUNLOCKNODE(np);
666 		error = VOP_GETATTR(vp, &vattr, ap->a_cred);
667 		if (error) {
668 			if (NFS_ISV4(vp))
669 				(void) nfsrpc_close(vp, 0, ap->a_td);
670 			return (error);
671 		}
672 		NFSLOCKNODE(np);
673 		np->n_mtime = vattr.va_mtime;
674 		if (NFS_ISV4(vp))
675 			np->n_change = vattr.va_filerev;
676 	} else {
677 		NFSUNLOCKNODE(np);
678 		error = VOP_GETATTR(vp, &vattr, ap->a_cred);
679 		if (error) {
680 			if (NFS_ISV4(vp))
681 				(void) nfsrpc_close(vp, 0, ap->a_td);
682 			return (error);
683 		}
684 		NFSLOCKNODE(np);
685 		if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
686 		    NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
687 			if (vp->v_type == VDIR)
688 				np->n_direofoffset = 0;
689 			NFSUNLOCKNODE(np);
690 			if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
691 				NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
692 				if (VN_IS_DOOMED(vp))
693 					return (EBADF);
694 			}
695 			error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
696 			if (error == EINTR || error == EIO) {
697 				if (NFS_ISV4(vp))
698 					(void) nfsrpc_close(vp, 0, ap->a_td);
699 				return (error);
700 			}
701 			NFSLOCKNODE(np);
702 			np->n_mtime = vattr.va_mtime;
703 			if (NFS_ISV4(vp))
704 				np->n_change = vattr.va_filerev;
705 		}
706 	}
707 
708 	/*
709 	 * If the object has >= 1 O_DIRECT active opens, we disable caching.
710 	 */
711 	if (newnfs_directio_enable && (fmode & O_DIRECT) &&
712 	    (vp->v_type == VREG)) {
713 		if (np->n_directio_opens == 0) {
714 			NFSUNLOCKNODE(np);
715 			if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
716 				NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
717 				if (VN_IS_DOOMED(vp))
718 					return (EBADF);
719 			}
720 			error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
721 			if (error) {
722 				if (NFS_ISV4(vp))
723 					(void) nfsrpc_close(vp, 0, ap->a_td);
724 				return (error);
725 			}
726 			NFSLOCKNODE(np);
727 			np->n_flag |= NNONCACHE;
728 		}
729 		np->n_directio_opens++;
730 	}
731 
732 	/* If opened for writing via NFSv4.1 or later, mark that for pNFS. */
733 	if (NFSHASPNFS(VFSTONFS(vp->v_mount)) && (fmode & FWRITE) != 0)
734 		np->n_flag |= NWRITEOPENED;
735 
736 	/*
737 	 * If this is an open for writing, capture a reference to the
738 	 * credentials, so they can be used by ncl_putpages(). Using
739 	 * these write credentials is preferable to the credentials of
740 	 * whatever thread happens to be doing the VOP_PUTPAGES() since
741 	 * the write RPCs are less likely to fail with EACCES.
742 	 */
743 	if ((fmode & FWRITE) != 0) {
744 		cred = np->n_writecred;
745 		np->n_writecred = crhold(ap->a_cred);
746 	} else
747 		cred = NULL;
748 	NFSUNLOCKNODE(np);
749 
750 	if (cred != NULL)
751 		crfree(cred);
752 	vnode_create_vobject(vp, vattr.va_size, ap->a_td);
753 
754 	/*
755 	 * If the text file has been mmap'd, flush any dirty pages to the
756 	 * buffer cache and then...
757 	 * Make sure all writes are pushed to the NFS server.  If this is not
758 	 * done, the modify time of the file can change while the text
759 	 * file is being executed.  This will cause the process that is
760 	 * executing the text file to be terminated.
761 	 */
762 	if (vp->v_writecount <= -1) {
763 		if ((obj = vp->v_object) != NULL &&
764 		    vm_object_mightbedirty(obj)) {
765 			if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
766 				NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
767 				if (VN_IS_DOOMED(vp))
768 					return (EBADF);
769 			}
770 			vnode_pager_clean_sync(vp);
771 		}
772 
773 		/* Now, flush the buffer cache. */
774 		ncl_flush(vp, MNT_WAIT, curthread, 0, 0);
775 
776 		/* And, finally, make sure that n_mtime is up to date. */
777 		np = VTONFS(vp);
778 		NFSLOCKNODE(np);
779 		np->n_mtime = np->n_vattr.na_mtime;
780 		NFSUNLOCKNODE(np);
781 	}
782 	return (0);
783 }
784 
785 /*
786  * nfs close vnode op
787  * What an NFS client should do upon close after writing is a debatable issue.
788  * Most NFS clients push delayed writes to the server upon close, basically for
789  * two reasons:
790  * 1 - So that any write errors may be reported back to the client process
791  *     doing the close system call. By far the two most likely errors are
792  *     NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
793  * 2 - To put a worst case upper bound on cache inconsistency between
794  *     multiple clients for the file.
795  * There is also a consistency problem for Version 2 of the protocol w.r.t.
796  * not being able to tell if other clients are writing a file concurrently,
797  * since there is no way of knowing if the changed modify time in the reply
798  * is only due to the write for this client.
799  * (NFS Version 3 provides weak cache consistency data in the reply that
800  *  should be sufficient to detect and handle this case.)
801  *
802  * The current code does the following:
803  * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
804  * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
805  *                     or commit them (this satisfies 1 and 2 except for the
806  *                     case where the server crashes after this close but
807  *                     before the commit RPC, which is felt to be "good
808  *                     enough". Changing the last argument to ncl_flush() to
809  *                     a 1 would force a commit operation, if it is felt a
810  *                     commit is necessary now.
811  * for NFS Version 4 - flush the dirty buffers and commit them, if
812  *		       nfscl_mustflush() says this is necessary.
813  *                     It is necessary if there is no write delegation held,
814  *                     in order to satisfy open/close coherency.
815  *                     If the file isn't cached on local stable storage,
816  *                     it may be necessary in order to detect "out of space"
817  *                     errors from the server, if the write delegation
818  *                     issued by the server doesn't allow the file to grow.
819  */
820 /* ARGSUSED */
821 static int
822 nfs_close(struct vop_close_args *ap)
823 {
824 	struct vnode *vp = ap->a_vp;
825 	struct nfsnode *np = VTONFS(vp);
826 	struct nfsvattr nfsva;
827 	struct ucred *cred;
828 	int error = 0, ret, localcred = 0;
829 	int fmode = ap->a_fflag;
830 
831 	if (NFSCL_FORCEDISM(vp->v_mount))
832 		return (0);
833 	/*
834 	 * During shutdown, a_cred isn't valid, so just use root.
835 	 */
836 	if (ap->a_cred == NOCRED) {
837 		cred = newnfs_getcred();
838 		localcred = 1;
839 	} else {
840 		cred = ap->a_cred;
841 	}
842 	if (vp->v_type == VREG) {
843 	    /*
844 	     * Examine and clean dirty pages, regardless of NMODIFIED.
845 	     * This closes a major hole in close-to-open consistency.
846 	     * We want to push out all dirty pages (and buffers) on
847 	     * close, regardless of whether they were dirtied by
848 	     * mmap'ed writes or via write().
849 	     */
850 	    if (nfs_clean_pages_on_close && vp->v_object) {
851 		if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
852 			NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
853 			if (VN_IS_DOOMED(vp) && ap->a_fflag != FNONBLOCK)
854 				return (EBADF);
855 		}
856 		vnode_pager_clean_async(vp);
857 	    }
858 	    NFSLOCKNODE(np);
859 	    if (np->n_flag & NMODIFIED) {
860 		NFSUNLOCKNODE(np);
861 		if (NFS_ISV3(vp)) {
862 		    /*
863 		     * Under NFSv3 we have dirty buffers to dispose of.  We
864 		     * must flush them to the NFS server.  We have the option
865 		     * of waiting all the way through the commit rpc or just
866 		     * waiting for the initial write.  The default is to only
867 		     * wait through the initial write so the data is in the
868 		     * server's cache, which is roughly similar to the state
869 		     * a standard disk subsystem leaves the file in on close().
870 		     *
871 		     * We cannot clear the NMODIFIED bit in np->n_flag due to
872 		     * potential races with other processes, and certainly
873 		     * cannot clear it if we don't commit.
874 		     * These races occur when there is no longer the old
875 		     * traditional vnode locking implemented for Vnode Ops.
876 		     */
877 		    int cm = newnfs_commit_on_close ? 1 : 0;
878 		    if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
879 			    NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
880 			    if (VN_IS_DOOMED(vp) && ap->a_fflag != FNONBLOCK)
881 				    return (EBADF);
882 		    }
883 		    error = ncl_flush(vp, MNT_WAIT, ap->a_td, cm, 0);
884 		    /* np->n_flag &= ~NMODIFIED; */
885 		} else if (NFS_ISV4(vp)) {
886 			if (nfscl_mustflush(vp) != 0) {
887 				int cm = newnfs_commit_on_close ? 1 : 0;
888 				if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
889 					NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
890 					if (VN_IS_DOOMED(vp) && ap->a_fflag !=
891 					    FNONBLOCK)
892 						return (EBADF);
893 				}
894 				error = ncl_flush(vp, MNT_WAIT, ap->a_td,
895 				    cm, 0);
896 				/*
897 				 * as above w.r.t races when clearing
898 				 * NMODIFIED.
899 				 * np->n_flag &= ~NMODIFIED;
900 				 */
901 			}
902 		} else {
903 			if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
904 				NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
905 				if (VN_IS_DOOMED(vp) && ap->a_fflag !=
906 				    FNONBLOCK)
907 					return (EBADF);
908 			}
909 			error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
910 		}
911 		NFSLOCKNODE(np);
912 	    }
913  	    /*
914  	     * Invalidate the attribute cache in all cases.
915  	     * An open is going to fetch fresh attrs any way, other procs
916  	     * on this node that have file open will be forced to do an
917  	     * otw attr fetch, but this is safe.
918 	     * --> A user found that their RPC count dropped by 20% when
919 	     *     this was commented out and I can't see any requirement
920 	     *     for it, so I've disabled it when negative lookups are
921 	     *     enabled. (What does this have to do with negative lookup
922 	     *     caching? Well nothing, except it was reported by the
923 	     *     same user that needed negative lookup caching and I wanted
924 	     *     there to be a way to disable it to see if it
925 	     *     is the cause of some caching/coherency issue that might
926 	     *     crop up.)
927  	     */
928 	    if (VFSTONFS(vp->v_mount)->nm_negnametimeo == 0) {
929 		    np->n_attrstamp = 0;
930 		    KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
931 	    }
932 	    if (np->n_flag & NWRITEERR) {
933 		np->n_flag &= ~NWRITEERR;
934 		error = np->n_error;
935 	    }
936 	    NFSUNLOCKNODE(np);
937 	}
938 
939 	if (NFS_ISV4(vp)) {
940 		/*
941 		 * Get attributes so "change" is up to date.
942 		 */
943 		if (error == 0 && nfscl_mustflush(vp) != 0 &&
944 		    vp->v_type == VREG &&
945 		    (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOCTO) == 0) {
946 			ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva);
947 			if (!ret) {
948 				np->n_change = nfsva.na_filerev;
949 				(void) nfscl_loadattrcache(&vp, &nfsva, NULL,
950 				    0, 0);
951 			}
952 		}
953 
954 		/*
955 		 * and do the close.
956 		 */
957 		ret = nfsrpc_close(vp, 0, ap->a_td);
958 		if (!error && ret)
959 			error = ret;
960 		if (error)
961 			error = nfscl_maperr(ap->a_td, error, (uid_t)0,
962 			    (gid_t)0);
963 	}
964 	if (newnfs_directio_enable)
965 		KASSERT((np->n_directio_asyncwr == 0),
966 			("nfs_close: dirty unflushed (%d) directio buffers\n",
967 			 np->n_directio_asyncwr));
968 	if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
969 		NFSLOCKNODE(np);
970 		KASSERT((np->n_directio_opens > 0),
971 			("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
972 		np->n_directio_opens--;
973 		if (np->n_directio_opens == 0)
974 			np->n_flag &= ~NNONCACHE;
975 		NFSUNLOCKNODE(np);
976 	}
977 	if (localcred)
978 		NFSFREECRED(cred);
979 	return (error);
980 }
981 
982 /*
983  * nfs getattr call from vfs.
984  */
985 static int
986 nfs_getattr(struct vop_getattr_args *ap)
987 {
988 	struct vnode *vp = ap->a_vp;
989 	struct thread *td = curthread;	/* XXX */
990 	struct nfsnode *np = VTONFS(vp);
991 	int error = 0;
992 	struct nfsvattr nfsva;
993 	struct vattr *vap = ap->a_vap;
994 	struct vattr vattr;
995 	struct nfsmount *nmp;
996 
997 	nmp = VFSTONFS(vp->v_mount);
998 	/*
999 	 * Update local times for special files.
1000 	 */
1001 	NFSLOCKNODE(np);
1002 	if (np->n_flag & (NACC | NUPD))
1003 		np->n_flag |= NCHG;
1004 	NFSUNLOCKNODE(np);
1005 	/*
1006 	 * First look in the cache.
1007 	 * For "syskrb5" mounts, nm_fhsize might still be zero and
1008 	 * cached attributes should be ignored.
1009 	 */
1010 	if (nmp->nm_fhsize > 0 && ncl_getattrcache(vp, &vattr) == 0) {
1011 		ncl_copy_vattr(vap, &vattr);
1012 
1013 		/*
1014 		 * Get the local modify time for the case of a write
1015 		 * delegation.
1016 		 */
1017 		nfscl_deleggetmodtime(vp, &vap->va_mtime);
1018 		return (0);
1019 	}
1020 
1021 	if (NFS_ISV34(vp) && nfs_prime_access_cache &&
1022 	    nfsaccess_cache_timeout > 0) {
1023 		NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
1024 		nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
1025 		if (ncl_getattrcache(vp, ap->a_vap) == 0) {
1026 			nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
1027 			return (0);
1028 		}
1029 	}
1030 	error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva);
1031 	if (!error)
1032 		error = nfscl_loadattrcache(&vp, &nfsva, vap, 0, 0);
1033 	if (!error) {
1034 		/*
1035 		 * Get the local modify time for the case of a write
1036 		 * delegation.
1037 		 */
1038 		nfscl_deleggetmodtime(vp, &vap->va_mtime);
1039 	} else if (NFS_ISV4(vp)) {
1040 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1041 	}
1042 	return (error);
1043 }
1044 
1045 /*
1046  * nfs setattr call.
1047  */
1048 static int
1049 nfs_setattr(struct vop_setattr_args *ap)
1050 {
1051 	struct vnode *vp = ap->a_vp;
1052 	struct nfsnode *np = VTONFS(vp);
1053 	struct thread *td = curthread;	/* XXX */
1054 	struct vattr *vap = ap->a_vap;
1055 	int error = 0;
1056 	u_quad_t tsize;
1057 	struct timespec ts;
1058 
1059 #ifndef nolint
1060 	tsize = (u_quad_t)0;
1061 #endif
1062 
1063 	/*
1064 	 * Setting of flags and marking of atimes are not supported.
1065 	 */
1066 	if (vap->va_flags != VNOVAL)
1067 		return (EOPNOTSUPP);
1068 
1069 	/*
1070 	 * Disallow write attempts if the filesystem is mounted read-only.
1071 	 */
1072   	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
1073 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
1074 	    vap->va_mtime.tv_sec != VNOVAL ||
1075 	    vap->va_birthtime.tv_sec != VNOVAL ||
1076 	    vap->va_mode != (mode_t)VNOVAL) &&
1077 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
1078 		return (EROFS);
1079 	if (vap->va_size != VNOVAL) {
1080  		switch (vp->v_type) {
1081  		case VDIR:
1082  			return (EISDIR);
1083  		case VCHR:
1084  		case VBLK:
1085  		case VSOCK:
1086  		case VFIFO:
1087 			if (vap->va_mtime.tv_sec == VNOVAL &&
1088 			    vap->va_atime.tv_sec == VNOVAL &&
1089 			    vap->va_birthtime.tv_sec == VNOVAL &&
1090 			    vap->va_mode == (mode_t)VNOVAL &&
1091 			    vap->va_uid == (uid_t)VNOVAL &&
1092 			    vap->va_gid == (gid_t)VNOVAL)
1093 				return (0);
1094  			vap->va_size = VNOVAL;
1095  			break;
1096  		default:
1097 			/*
1098 			 * Disallow write attempts if the filesystem is
1099 			 * mounted read-only.
1100 			 */
1101 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
1102 				return (EROFS);
1103 			/*
1104 			 *  We run vnode_pager_setsize() early (why?),
1105 			 * we must set np->n_size now to avoid vinvalbuf
1106 			 * V_SAVE races that might setsize a lower
1107 			 * value.
1108 			 */
1109 			NFSLOCKNODE(np);
1110 			tsize = np->n_size;
1111 			NFSUNLOCKNODE(np);
1112 			error = ncl_meta_setsize(vp, td, vap->va_size);
1113 			NFSLOCKNODE(np);
1114  			if (np->n_flag & NMODIFIED) {
1115 			    tsize = np->n_size;
1116 			    NFSUNLOCKNODE(np);
1117 			    error = ncl_vinvalbuf(vp, vap->va_size == 0 ?
1118 			        0 : V_SAVE, td, 1);
1119 			    if (error != 0) {
1120 				    vnode_pager_setsize(vp, tsize);
1121 				    return (error);
1122 			    }
1123 			    /*
1124 			     * Call nfscl_delegmodtime() to set the modify time
1125 			     * locally, as required.
1126 			     */
1127 			    nfscl_delegmodtime(vp);
1128  			} else
1129 			    NFSUNLOCKNODE(np);
1130 			/*
1131 			 * np->n_size has already been set to vap->va_size
1132 			 * in ncl_meta_setsize(). We must set it again since
1133 			 * nfs_loadattrcache() could be called through
1134 			 * ncl_meta_setsize() and could modify np->n_size.
1135 			 */
1136 			NFSLOCKNODE(np);
1137  			np->n_vattr.na_size = np->n_size = vap->va_size;
1138 			NFSUNLOCKNODE(np);
1139   		}
1140   	} else {
1141 		NFSLOCKNODE(np);
1142 		if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
1143 		    (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
1144 			NFSUNLOCKNODE(np);
1145 			error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
1146 			if (error == EINTR || error == EIO)
1147 				return (error);
1148 		} else
1149 			NFSUNLOCKNODE(np);
1150 	}
1151 	error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
1152 	if (vap->va_size != VNOVAL) {
1153 		if (error == 0) {
1154 			nanouptime(&ts);
1155 			NFSLOCKNODE(np);
1156 			np->n_localmodtime = ts;
1157 			NFSUNLOCKNODE(np);
1158 		} else {
1159 			NFSLOCKNODE(np);
1160 			np->n_size = np->n_vattr.na_size = tsize;
1161 			vnode_pager_setsize(vp, tsize);
1162 			NFSUNLOCKNODE(np);
1163 		}
1164 	}
1165 	return (error);
1166 }
1167 
1168 /*
1169  * Do an nfs setattr rpc.
1170  */
1171 static int
1172 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
1173     struct thread *td)
1174 {
1175 	struct nfsnode *np = VTONFS(vp);
1176 	int error, ret, attrflag, i;
1177 	struct nfsvattr nfsva;
1178 
1179 	if (NFS_ISV34(vp)) {
1180 		NFSLOCKNODE(np);
1181 		for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
1182 			np->n_accesscache[i].stamp = 0;
1183 		np->n_flag |= NDELEGMOD;
1184 		NFSUNLOCKNODE(np);
1185 		KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1186 	}
1187 	error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag);
1188 	if (attrflag) {
1189 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1190 		if (ret && !error)
1191 			error = ret;
1192 	}
1193 	if (error && NFS_ISV4(vp))
1194 		error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1195 	return (error);
1196 }
1197 
1198 /*
1199  * nfs lookup call, one step at a time...
1200  * First look in cache
1201  * If not found, unlock the directory nfsnode and do the rpc
1202  */
1203 static int
1204 nfs_lookup(struct vop_lookup_args *ap)
1205 {
1206 	struct componentname *cnp = ap->a_cnp;
1207 	struct vnode *dvp = ap->a_dvp;
1208 	struct vnode **vpp = ap->a_vpp;
1209 	struct mount *mp = dvp->v_mount;
1210 	int flags = cnp->cn_flags;
1211 	struct vnode *newvp;
1212 	struct nfsmount *nmp;
1213 	struct nfsnode *np, *newnp;
1214 	int error = 0, attrflag, dattrflag, ltype, ncticks;
1215 	struct thread *td = curthread;
1216 	struct nfsfh *nfhp;
1217 	struct nfsvattr dnfsva, nfsva;
1218 	struct vattr vattr;
1219 	struct timespec nctime, ts;
1220 	uint32_t openmode;
1221 
1222 	*vpp = NULLVP;
1223 	if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1224 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1225 		return (EROFS);
1226 	if (dvp->v_type != VDIR)
1227 		return (ENOTDIR);
1228 	nmp = VFSTONFS(mp);
1229 	np = VTONFS(dvp);
1230 
1231 	/* For NFSv4, wait until any remove is done. */
1232 	NFSLOCKNODE(np);
1233 	while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1234 		np->n_flag |= NREMOVEWANT;
1235 		(void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1236 	}
1237 	NFSUNLOCKNODE(np);
1238 
1239 	error = vn_dir_check_exec(dvp, cnp);
1240 	if (error != 0)
1241 		return (error);
1242 	error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1243 	if (error > 0 && error != ENOENT)
1244 		return (error);
1245 	if (error == -1) {
1246 		/*
1247 		 * Lookups of "." are special and always return the
1248 		 * current directory.  cache_lookup() already handles
1249 		 * associated locking bookkeeping, etc.
1250 		 */
1251 		if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1252 			return (0);
1253 		}
1254 
1255 		/*
1256 		 * We only accept a positive hit in the cache if the
1257 		 * change time of the file matches our cached copy.
1258 		 * Otherwise, we discard the cache entry and fallback
1259 		 * to doing a lookup RPC.  We also only trust cache
1260 		 * entries for less than nm_nametimeo seconds.
1261 		 *
1262 		 * To better handle stale file handles and attributes,
1263 		 * clear the attribute cache of this node if it is a
1264 		 * leaf component, part of an open() call, and not
1265 		 * locally modified before fetching the attributes.
1266 		 * This should allow stale file handles to be detected
1267 		 * here where we can fall back to a LOOKUP RPC to
1268 		 * recover rather than having nfs_open() detect the
1269 		 * stale file handle and failing open(2) with ESTALE.
1270 		 */
1271 		newvp = *vpp;
1272 		newnp = VTONFS(newvp);
1273 		if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1274 		    (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1275 		    !(newnp->n_flag & NMODIFIED)) {
1276 			NFSLOCKNODE(newnp);
1277 			newnp->n_attrstamp = 0;
1278 			KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1279 			NFSUNLOCKNODE(newnp);
1280 		}
1281 		if (nfscl_nodeleg(newvp, 0) == 0 ||
1282 		    ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
1283 		    VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1284 		    timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1285 			NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1286 			return (0);
1287 		}
1288 		cache_purge(newvp);
1289 		if (dvp != newvp)
1290 			vput(newvp);
1291 		else
1292 			vrele(newvp);
1293 		*vpp = NULLVP;
1294 	} else if (error == ENOENT) {
1295 		if (VN_IS_DOOMED(dvp))
1296 			return (ENOENT);
1297 		/*
1298 		 * We only accept a negative hit in the cache if the
1299 		 * modification time of the parent directory matches
1300 		 * the cached copy in the name cache entry.
1301 		 * Otherwise, we discard all of the negative cache
1302 		 * entries for this directory.  We also only trust
1303 		 * negative cache entries for up to nm_negnametimeo
1304 		 * seconds.
1305 		 */
1306 		if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
1307 		    VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1308 		    timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1309 			NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1310 			return (ENOENT);
1311 		}
1312 		cache_purge_negative(dvp);
1313 	}
1314 
1315 	openmode = 0;
1316 	/*
1317 	 * If this an NFSv4.1/4.2 mount using the "oneopenown" mount
1318 	 * option, it is possible to do the Open operation in the same
1319 	 * compound as Lookup, so long as delegations are not being
1320 	 * issued.  This saves doing a separate RPC for Open.
1321 	 * For pnfs, do not do this, since the Open+LayoutGet will
1322 	 * be needed as a separate RPC.
1323 	 */
1324 	NFSLOCKMNT(nmp);
1325 	if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp) && !NFSHASPNFS(nmp) &&
1326 	    (nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0 &&
1327 	    (!NFSMNT_RDONLY(mp) || (flags & OPENWRITE) == 0) &&
1328 	    (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN)) {
1329 		if ((flags & OPENREAD) != 0)
1330 			openmode |= NFSV4OPEN_ACCESSREAD;
1331 		if ((flags & OPENWRITE) != 0)
1332 			openmode |= NFSV4OPEN_ACCESSWRITE;
1333 	}
1334 	NFSUNLOCKMNT(nmp);
1335 
1336 	newvp = NULLVP;
1337 	NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1338 	nanouptime(&ts);
1339 	error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1340 	    cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1341 	    openmode);
1342 	if (dattrflag)
1343 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1344 	if (error) {
1345 		if (newvp != NULLVP) {
1346 			vput(newvp);
1347 			*vpp = NULLVP;
1348 		}
1349 
1350 		if (error != ENOENT) {
1351 			if (NFS_ISV4(dvp))
1352 				error = nfscl_maperr(td, error, (uid_t)0,
1353 				    (gid_t)0);
1354 			return (error);
1355 		}
1356 
1357 		/* The requested file was not found. */
1358 		if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1359 		    (flags & ISLASTCN)) {
1360 			/*
1361 			 * XXX: UFS does a full VOP_ACCESS(dvp,
1362 			 * VWRITE) here instead of just checking
1363 			 * MNT_RDONLY.
1364 			 */
1365 			if (mp->mnt_flag & MNT_RDONLY)
1366 				return (EROFS);
1367 			return (EJUSTRETURN);
1368 		}
1369 
1370 		if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1371 			/*
1372 			 * Cache the modification time of the parent
1373 			 * directory from the post-op attributes in
1374 			 * the name cache entry.  The negative cache
1375 			 * entry will be ignored once the directory
1376 			 * has changed.  Don't bother adding the entry
1377 			 * if the directory has already changed.
1378 			 */
1379 			NFSLOCKNODE(np);
1380 			if (timespeccmp(&np->n_vattr.na_mtime,
1381 			    &dnfsva.na_mtime, ==)) {
1382 				NFSUNLOCKNODE(np);
1383 				cache_enter_time(dvp, NULL, cnp,
1384 				    &dnfsva.na_mtime, NULL);
1385 			} else
1386 				NFSUNLOCKNODE(np);
1387 		}
1388 		return (ENOENT);
1389 	}
1390 
1391 	/*
1392 	 * Handle RENAME case...
1393 	 */
1394 	if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1395 		if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1396 			free(nfhp, M_NFSFH);
1397 			return (EISDIR);
1398 		}
1399 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, LK_EXCLUSIVE);
1400 		if (error)
1401 			return (error);
1402 		newvp = NFSTOV(np);
1403 		/*
1404 		 * If n_localmodtime >= time before RPC, then
1405 		 * a file modification operation, such as
1406 		 * VOP_SETATTR() of size, has occurred while
1407 		 * the Lookup RPC and acquisition of the vnode
1408 		 * happened.  As such, the attributes might
1409 		 * be stale, with possibly an incorrect size.
1410 		 */
1411 		NFSLOCKNODE(np);
1412 		if (timespecisset(&np->n_localmodtime) &&
1413 		    timespeccmp(&np->n_localmodtime, &ts, >=)) {
1414 			NFSCL_DEBUG(4, "nfs_lookup: rename localmod "
1415 			    "stale attributes\n");
1416 			attrflag = 0;
1417 		}
1418 		NFSUNLOCKNODE(np);
1419 		if (attrflag)
1420 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1421 		*vpp = newvp;
1422 		return (0);
1423 	}
1424 
1425 	if (flags & ISDOTDOT) {
1426 		ltype = NFSVOPISLOCKED(dvp);
1427 		error = vfs_busy(mp, MBF_NOWAIT);
1428 		if (error != 0) {
1429 			vfs_ref(mp);
1430 			NFSVOPUNLOCK(dvp);
1431 			error = vfs_busy(mp, 0);
1432 			NFSVOPLOCK(dvp, ltype | LK_RETRY);
1433 			vfs_rel(mp);
1434 			if (error == 0 && VN_IS_DOOMED(dvp)) {
1435 				vfs_unbusy(mp);
1436 				error = ENOENT;
1437 			}
1438 			if (error != 0)
1439 				return (error);
1440 		}
1441 		NFSVOPUNLOCK(dvp);
1442 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1443 		    cnp->cn_lkflags);
1444 		if (error == 0)
1445 			newvp = NFSTOV(np);
1446 		vfs_unbusy(mp);
1447 		if (newvp != dvp)
1448 			NFSVOPLOCK(dvp, ltype | LK_RETRY);
1449 		if (VN_IS_DOOMED(dvp)) {
1450 			if (error == 0) {
1451 				if (newvp == dvp)
1452 					vrele(newvp);
1453 				else
1454 					vput(newvp);
1455 			}
1456 			error = ENOENT;
1457 		}
1458 		if (error != 0)
1459 			return (error);
1460 		if (attrflag)
1461 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1462 	} else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1463 		free(nfhp, M_NFSFH);
1464 		VREF(dvp);
1465 		newvp = dvp;
1466 		if (attrflag)
1467 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1468 	} else {
1469 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1470 		    cnp->cn_lkflags);
1471 		if (error)
1472 			return (error);
1473 		newvp = NFSTOV(np);
1474 		/*
1475 		 * If n_localmodtime >= time before RPC, then
1476 		 * a file modification operation, such as
1477 		 * VOP_SETATTR() of size, has occurred while
1478 		 * the Lookup RPC and acquisition of the vnode
1479 		 * happened.  As such, the attributes might
1480 		 * be stale, with possibly an incorrect size.
1481 		 */
1482 		NFSLOCKNODE(np);
1483 		if (timespecisset(&np->n_localmodtime) &&
1484 		    timespeccmp(&np->n_localmodtime, &ts, >=)) {
1485 			NFSCL_DEBUG(4, "nfs_lookup: localmod "
1486 			    "stale attributes\n");
1487 			attrflag = 0;
1488 		}
1489 		NFSUNLOCKNODE(np);
1490 		if (attrflag)
1491 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1492 		else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1493 		    !(np->n_flag & NMODIFIED)) {
1494 			/*
1495 			 * Flush the attribute cache when opening a
1496 			 * leaf node to ensure that fresh attributes
1497 			 * are fetched in nfs_open() since we did not
1498 			 * fetch attributes from the LOOKUP reply.
1499 			 */
1500 			NFSLOCKNODE(np);
1501 			np->n_attrstamp = 0;
1502 			KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1503 			NFSUNLOCKNODE(np);
1504 		}
1505 	}
1506 	if ((cnp->cn_flags & MAKEENTRY) && dvp != newvp &&
1507 	    (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1508 	    attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1509 		cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1510 		    newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1511 	*vpp = newvp;
1512 	return (0);
1513 }
1514 
1515 /*
1516  * nfs read call.
1517  * Just call ncl_bioread() to do the work.
1518  */
1519 static int
1520 nfs_read(struct vop_read_args *ap)
1521 {
1522 	struct vnode *vp = ap->a_vp;
1523 
1524 	switch (vp->v_type) {
1525 	case VREG:
1526 		return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1527 	case VDIR:
1528 		return (EISDIR);
1529 	default:
1530 		return (EOPNOTSUPP);
1531 	}
1532 }
1533 
1534 /*
1535  * nfs readlink call
1536  */
1537 static int
1538 nfs_readlink(struct vop_readlink_args *ap)
1539 {
1540 	struct vnode *vp = ap->a_vp;
1541 
1542 	if (vp->v_type != VLNK)
1543 		return (EINVAL);
1544 	return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1545 }
1546 
1547 /*
1548  * Do a readlink rpc.
1549  * Called by ncl_doio() from below the buffer cache.
1550  */
1551 int
1552 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1553 {
1554 	int error, ret, attrflag;
1555 	struct nfsvattr nfsva;
1556 
1557 	error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1558 	    &attrflag);
1559 	if (attrflag) {
1560 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1561 		if (ret && !error)
1562 			error = ret;
1563 	}
1564 	if (error && NFS_ISV4(vp))
1565 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1566 	return (error);
1567 }
1568 
1569 /*
1570  * nfs read rpc call
1571  * Ditto above
1572  */
1573 int
1574 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1575 {
1576 	int error, ret, attrflag;
1577 	struct nfsvattr nfsva;
1578 	struct nfsmount *nmp;
1579 
1580 	nmp = VFSTONFS(vp->v_mount);
1581 	error = EIO;
1582 	attrflag = 0;
1583 	if (NFSHASPNFS(nmp))
1584 		error = nfscl_doiods(vp, uiop, NULL, NULL,
1585 		    NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td);
1586 	NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1587 	if (error != 0 && error != EFAULT)
1588 		error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1589 		    &attrflag);
1590 	if (attrflag) {
1591 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1592 		if (ret && !error)
1593 			error = ret;
1594 	}
1595 	if (error && NFS_ISV4(vp))
1596 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1597 	return (error);
1598 }
1599 
1600 /*
1601  * nfs write call
1602  */
1603 int
1604 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1605     int *iomode, int *must_commit, int called_from_strategy, int ioflag)
1606 {
1607 	struct nfsvattr nfsva;
1608 	int error, attrflag, ret;
1609 	struct nfsmount *nmp;
1610 
1611 	nmp = VFSTONFS(vp->v_mount);
1612 	error = EIO;
1613 	attrflag = 0;
1614 	if (NFSHASPNFS(nmp))
1615 		error = nfscl_doiods(vp, uiop, iomode, must_commit,
1616 		    NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td);
1617 	NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1618 	if (error != 0 && error != EFAULT)
1619 		error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1620 		    uiop->uio_td, &nfsva, &attrflag, called_from_strategy,
1621 		    ioflag);
1622 	if (attrflag) {
1623 		if (VTONFS(vp)->n_flag & ND_NFSV4)
1624 			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 1, 1);
1625 		else
1626 			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1627 		if (ret && !error)
1628 			error = ret;
1629 	}
1630 	if (DOINGASYNC(vp))
1631 		*iomode = NFSWRITE_FILESYNC;
1632 	if (error && NFS_ISV4(vp))
1633 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1634 	return (error);
1635 }
1636 
1637 /*
1638  * nfs mknod rpc
1639  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1640  * mode set to specify the file type and the size field for rdev.
1641  */
1642 static int
1643 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1644     struct vattr *vap)
1645 {
1646 	struct nfsvattr nfsva, dnfsva;
1647 	struct vnode *newvp = NULL;
1648 	struct nfsnode *np = NULL, *dnp;
1649 	struct nfsfh *nfhp;
1650 	struct vattr vattr;
1651 	int error = 0, attrflag, dattrflag;
1652 	u_int32_t rdev;
1653 
1654 	if (vap->va_type == VCHR || vap->va_type == VBLK)
1655 		rdev = vap->va_rdev;
1656 	else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1657 		rdev = 0xffffffff;
1658 	else
1659 		return (EOPNOTSUPP);
1660 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1661 		return (error);
1662 	error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1663 	    rdev, vap->va_type, cnp->cn_cred, curthread, &dnfsva,
1664 	    &nfsva, &nfhp, &attrflag, &dattrflag);
1665 	if (!error) {
1666 		if (!nfhp)
1667 			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1668 			    cnp->cn_namelen, cnp->cn_cred, curthread,
1669 			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag, 0);
1670 		if (nfhp)
1671 			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1672 			    curthread, &np, LK_EXCLUSIVE);
1673 	}
1674 	if (dattrflag)
1675 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1676 	if (!error) {
1677 		newvp = NFSTOV(np);
1678 		if (attrflag != 0) {
1679 			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1680 			if (error != 0)
1681 				vput(newvp);
1682 		}
1683 	}
1684 	if (!error) {
1685 		*vpp = newvp;
1686 	} else if (NFS_ISV4(dvp)) {
1687 		error = nfscl_maperr(curthread, error, vap->va_uid,
1688 		    vap->va_gid);
1689 	}
1690 	dnp = VTONFS(dvp);
1691 	NFSLOCKNODE(dnp);
1692 	dnp->n_flag |= NMODIFIED;
1693 	if (!dattrflag) {
1694 		dnp->n_attrstamp = 0;
1695 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1696 	}
1697 	NFSUNLOCKNODE(dnp);
1698 	return (error);
1699 }
1700 
1701 /*
1702  * nfs mknod vop
1703  * just call nfs_mknodrpc() to do the work.
1704  */
1705 /* ARGSUSED */
1706 static int
1707 nfs_mknod(struct vop_mknod_args *ap)
1708 {
1709 	return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1710 }
1711 
1712 static struct mtx nfs_cverf_mtx;
1713 MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1714     MTX_DEF);
1715 
1716 static nfsquad_t
1717 nfs_get_cverf(void)
1718 {
1719 	static nfsquad_t cverf;
1720 	nfsquad_t ret;
1721 	static int cverf_initialized = 0;
1722 
1723 	mtx_lock(&nfs_cverf_mtx);
1724 	if (cverf_initialized == 0) {
1725 		cverf.lval[0] = arc4random();
1726 		cverf.lval[1] = arc4random();
1727 		cverf_initialized = 1;
1728 	} else
1729 		cverf.qval++;
1730 	ret = cverf;
1731 	mtx_unlock(&nfs_cverf_mtx);
1732 
1733 	return (ret);
1734 }
1735 
1736 /*
1737  * nfs file create call
1738  */
1739 static int
1740 nfs_create(struct vop_create_args *ap)
1741 {
1742 	struct vnode *dvp = ap->a_dvp;
1743 	struct vattr *vap = ap->a_vap;
1744 	struct componentname *cnp = ap->a_cnp;
1745 	struct nfsnode *np = NULL, *dnp;
1746 	struct vnode *newvp = NULL;
1747 	struct nfsmount *nmp;
1748 	struct nfsvattr dnfsva, nfsva;
1749 	struct nfsfh *nfhp;
1750 	nfsquad_t cverf;
1751 	int error = 0, attrflag, dattrflag, fmode = 0;
1752 	struct vattr vattr;
1753 
1754 	/*
1755 	 * Oops, not for me..
1756 	 */
1757 	if (vap->va_type == VSOCK)
1758 		return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1759 
1760 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1761 		return (error);
1762 	if (vap->va_vaflags & VA_EXCLUSIVE)
1763 		fmode |= O_EXCL;
1764 	dnp = VTONFS(dvp);
1765 	nmp = VFSTONFS(dvp->v_mount);
1766 again:
1767 	/* For NFSv4, wait until any remove is done. */
1768 	NFSLOCKNODE(dnp);
1769 	while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1770 		dnp->n_flag |= NREMOVEWANT;
1771 		(void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1772 	}
1773 	NFSUNLOCKNODE(dnp);
1774 
1775 	cverf = nfs_get_cverf();
1776 	error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1777 	    vap, cverf, fmode, cnp->cn_cred, curthread, &dnfsva, &nfsva,
1778 	    &nfhp, &attrflag, &dattrflag);
1779 	if (!error) {
1780 		if (nfhp == NULL)
1781 			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1782 			    cnp->cn_namelen, cnp->cn_cred, curthread,
1783 			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag, 0);
1784 		if (nfhp != NULL)
1785 			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1786 			    curthread, &np, LK_EXCLUSIVE);
1787 	}
1788 	if (dattrflag)
1789 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1790 	if (!error) {
1791 		newvp = NFSTOV(np);
1792 		if (attrflag == 0)
1793 			error = nfsrpc_getattr(newvp, cnp->cn_cred, curthread,
1794 			    &nfsva);
1795 		if (error == 0)
1796 			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1797 	}
1798 	if (error) {
1799 		if (newvp != NULL) {
1800 			vput(newvp);
1801 			newvp = NULL;
1802 		}
1803 		if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1804 		    error == NFSERR_NOTSUPP) {
1805 			fmode &= ~O_EXCL;
1806 			goto again;
1807 		}
1808 	} else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1809 		if (nfscl_checksattr(vap, &nfsva)) {
1810 			error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1811 			    curthread, &nfsva, &attrflag);
1812 			if (error && (vap->va_uid != (uid_t)VNOVAL ||
1813 			    vap->va_gid != (gid_t)VNOVAL)) {
1814 				/* try again without setting uid/gid */
1815 				vap->va_uid = (uid_t)VNOVAL;
1816 				vap->va_gid = (uid_t)VNOVAL;
1817 				error = nfsrpc_setattr(newvp, vap, NULL,
1818 				    cnp->cn_cred, curthread, &nfsva, &attrflag);
1819 			}
1820 			if (attrflag)
1821 				(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1822 				    0, 1);
1823 			if (error != 0)
1824 				vput(newvp);
1825 		}
1826 	}
1827 	if (!error) {
1828 		if ((cnp->cn_flags & MAKEENTRY) && attrflag) {
1829 			if (dvp != newvp)
1830 				cache_enter_time(dvp, newvp, cnp,
1831 				    &nfsva.na_ctime, NULL);
1832 			else
1833 				printf("nfs_create: bogus NFS server returned "
1834 				    "the directory as the new file object\n");
1835 		}
1836 		*ap->a_vpp = newvp;
1837 	} else if (NFS_ISV4(dvp)) {
1838 		error = nfscl_maperr(curthread, error, vap->va_uid,
1839 		    vap->va_gid);
1840 	}
1841 	NFSLOCKNODE(dnp);
1842 	dnp->n_flag |= NMODIFIED;
1843 	if (!dattrflag) {
1844 		dnp->n_attrstamp = 0;
1845 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1846 	}
1847 	NFSUNLOCKNODE(dnp);
1848 	return (error);
1849 }
1850 
1851 /*
1852  * nfs file remove call
1853  * To try and make nfs semantics closer to ufs semantics, a file that has
1854  * other processes using the vnode is renamed instead of removed and then
1855  * removed later on the last close.
1856  * - If v_usecount > 1
1857  *	  If a rename is not already in the works
1858  *	     call nfs_sillyrename() to set it up
1859  *     else
1860  *	  do the remove rpc
1861  */
1862 static int
1863 nfs_remove(struct vop_remove_args *ap)
1864 {
1865 	struct vnode *vp = ap->a_vp;
1866 	struct vnode *dvp = ap->a_dvp;
1867 	struct componentname *cnp = ap->a_cnp;
1868 	struct nfsnode *np = VTONFS(vp);
1869 	int error = 0;
1870 	struct vattr vattr;
1871 
1872 	KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1873 	if (vp->v_type == VDIR)
1874 		error = EPERM;
1875 	else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1876 	    VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1877 	    vattr.va_nlink > 1)) {
1878 		/*
1879 		 * Purge the name cache so that the chance of a lookup for
1880 		 * the name succeeding while the remove is in progress is
1881 		 * minimized. Without node locking it can still happen, such
1882 		 * that an I/O op returns ESTALE, but since you get this if
1883 		 * another host removes the file..
1884 		 */
1885 		cache_purge(vp);
1886 		/*
1887 		 * throw away biocache buffers, mainly to avoid
1888 		 * unnecessary delayed writes later.
1889 		 */
1890 		error = ncl_vinvalbuf(vp, 0, curthread, 1);
1891 		if (error != EINTR && error != EIO)
1892 			/* Do the rpc */
1893 			error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1894 			    cnp->cn_namelen, cnp->cn_cred, curthread);
1895 		/*
1896 		 * Kludge City: If the first reply to the remove rpc is lost..
1897 		 *   the reply to the retransmitted request will be ENOENT
1898 		 *   since the file was in fact removed
1899 		 *   Therefore, we cheat and return success.
1900 		 */
1901 		if (error == ENOENT)
1902 			error = 0;
1903 	} else if (!np->n_sillyrename)
1904 		error = nfs_sillyrename(dvp, vp, cnp);
1905 	NFSLOCKNODE(np);
1906 	np->n_attrstamp = 0;
1907 	NFSUNLOCKNODE(np);
1908 	KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1909 	return (error);
1910 }
1911 
1912 /*
1913  * nfs file remove rpc called from nfs_inactive
1914  */
1915 int
1916 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1917 {
1918 	/*
1919 	 * Make sure that the directory vnode is still valid.
1920 	 * XXX we should lock sp->s_dvp here.
1921 	 */
1922 	if (sp->s_dvp->v_type == VBAD)
1923 		return (0);
1924 	return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1925 	    sp->s_cred, NULL));
1926 }
1927 
1928 /*
1929  * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1930  */
1931 static int
1932 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1933     int namelen, struct ucred *cred, struct thread *td)
1934 {
1935 	struct nfsvattr dnfsva;
1936 	struct nfsnode *dnp = VTONFS(dvp);
1937 	int error = 0, dattrflag;
1938 
1939 	NFSLOCKNODE(dnp);
1940 	dnp->n_flag |= NREMOVEINPROG;
1941 	NFSUNLOCKNODE(dnp);
1942 	error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1943 	    &dattrflag);
1944 	NFSLOCKNODE(dnp);
1945 	if ((dnp->n_flag & NREMOVEWANT)) {
1946 		dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1947 		NFSUNLOCKNODE(dnp);
1948 		wakeup((caddr_t)dnp);
1949 	} else {
1950 		dnp->n_flag &= ~NREMOVEINPROG;
1951 		NFSUNLOCKNODE(dnp);
1952 	}
1953 	if (dattrflag)
1954 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1955 	NFSLOCKNODE(dnp);
1956 	dnp->n_flag |= NMODIFIED;
1957 	if (!dattrflag) {
1958 		dnp->n_attrstamp = 0;
1959 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1960 	}
1961 	NFSUNLOCKNODE(dnp);
1962 	if (error && NFS_ISV4(dvp))
1963 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1964 	return (error);
1965 }
1966 
1967 /*
1968  * nfs file rename call
1969  */
1970 static int
1971 nfs_rename(struct vop_rename_args *ap)
1972 {
1973 	struct vnode *fvp = ap->a_fvp;
1974 	struct vnode *tvp = ap->a_tvp;
1975 	struct vnode *fdvp = ap->a_fdvp;
1976 	struct vnode *tdvp = ap->a_tdvp;
1977 	struct componentname *tcnp = ap->a_tcnp;
1978 	struct componentname *fcnp = ap->a_fcnp;
1979 	struct nfsnode *fnp = VTONFS(ap->a_fvp);
1980 	struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1981 	struct nfsv4node *newv4 = NULL;
1982 	int error;
1983 
1984 	/* Check for cross-device rename */
1985 	if ((fvp->v_mount != tdvp->v_mount) ||
1986 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
1987 		error = EXDEV;
1988 		goto out;
1989 	}
1990 
1991 	if (fvp == tvp) {
1992 		printf("nfs_rename: fvp == tvp (can't happen)\n");
1993 		error = 0;
1994 		goto out;
1995 	}
1996 	if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
1997 		goto out;
1998 
1999 	/*
2000 	 * We have to flush B_DELWRI data prior to renaming
2001 	 * the file.  If we don't, the delayed-write buffers
2002 	 * can be flushed out later after the file has gone stale
2003 	 * under NFSV3.  NFSV2 does not have this problem because
2004 	 * ( as far as I can tell ) it flushes dirty buffers more
2005 	 * often.
2006 	 *
2007 	 * Skip the rename operation if the fsync fails, this can happen
2008 	 * due to the server's volume being full, when we pushed out data
2009 	 * that was written back to our cache earlier. Not checking for
2010 	 * this condition can result in potential (silent) data loss.
2011 	 */
2012 	error = VOP_FSYNC(fvp, MNT_WAIT, curthread);
2013 	NFSVOPUNLOCK(fvp);
2014 	if (!error && tvp)
2015 		error = VOP_FSYNC(tvp, MNT_WAIT, curthread);
2016 	if (error)
2017 		goto out;
2018 
2019 	/*
2020 	 * If the tvp exists and is in use, sillyrename it before doing the
2021 	 * rename of the new file over it.
2022 	 * XXX Can't sillyrename a directory.
2023 	 */
2024 	if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
2025 		tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
2026 		vput(tvp);
2027 		tvp = NULL;
2028 	}
2029 
2030 	error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2031 	    tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
2032 	    curthread);
2033 
2034 	if (error == 0 && NFS_ISV4(tdvp)) {
2035 		/*
2036 		 * For NFSv4, check to see if it is the same name and
2037 		 * replace the name, if it is different.
2038 		 */
2039 		newv4 = malloc(
2040 		    sizeof (struct nfsv4node) +
2041 		    tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
2042 		    M_NFSV4NODE, M_WAITOK);
2043 		NFSLOCKNODE(tdnp);
2044 		NFSLOCKNODE(fnp);
2045 		if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
2046 		    (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
2047 		      NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
2048 		      tcnp->cn_namelen) ||
2049 		      tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
2050 		      NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2051 			tdnp->n_fhp->nfh_len))) {
2052 			free(fnp->n_v4, M_NFSV4NODE);
2053 			fnp->n_v4 = newv4;
2054 			newv4 = NULL;
2055 			fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
2056 			fnp->n_v4->n4_namelen = tcnp->cn_namelen;
2057 			NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2058 			    tdnp->n_fhp->nfh_len);
2059 			NFSBCOPY(tcnp->cn_nameptr,
2060 			    NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
2061 		}
2062 		NFSUNLOCKNODE(tdnp);
2063 		NFSUNLOCKNODE(fnp);
2064 		if (newv4 != NULL)
2065 			free(newv4, M_NFSV4NODE);
2066 	}
2067 
2068 	if (fvp->v_type == VDIR) {
2069 		if (tvp != NULL && tvp->v_type == VDIR)
2070 			cache_purge(tdvp);
2071 		cache_purge(fdvp);
2072 	}
2073 
2074 out:
2075 	if (tdvp == tvp)
2076 		vrele(tdvp);
2077 	else
2078 		vput(tdvp);
2079 	if (tvp)
2080 		vput(tvp);
2081 	vrele(fdvp);
2082 	vrele(fvp);
2083 	/*
2084 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2085 	 */
2086 	if (error == ENOENT)
2087 		error = 0;
2088 	return (error);
2089 }
2090 
2091 /*
2092  * nfs file rename rpc called from nfs_remove() above
2093  */
2094 static int
2095 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
2096     struct sillyrename *sp)
2097 {
2098 
2099 	return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
2100 	    sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
2101 	    curthread));
2102 }
2103 
2104 /*
2105  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2106  */
2107 static int
2108 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
2109     int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
2110     int tnamelen, struct ucred *cred, struct thread *td)
2111 {
2112 	struct nfsvattr fnfsva, tnfsva;
2113 	struct nfsnode *fdnp = VTONFS(fdvp);
2114 	struct nfsnode *tdnp = VTONFS(tdvp);
2115 	int error = 0, fattrflag, tattrflag;
2116 
2117 	error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
2118 	    tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
2119 	    &tattrflag);
2120 	NFSLOCKNODE(fdnp);
2121 	fdnp->n_flag |= NMODIFIED;
2122 	if (fattrflag != 0) {
2123 		NFSUNLOCKNODE(fdnp);
2124 		(void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, 0, 1);
2125 	} else {
2126 		fdnp->n_attrstamp = 0;
2127 		NFSUNLOCKNODE(fdnp);
2128 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
2129 	}
2130 	NFSLOCKNODE(tdnp);
2131 	tdnp->n_flag |= NMODIFIED;
2132 	if (tattrflag != 0) {
2133 		NFSUNLOCKNODE(tdnp);
2134 		(void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, 0, 1);
2135 	} else {
2136 		tdnp->n_attrstamp = 0;
2137 		NFSUNLOCKNODE(tdnp);
2138 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2139 	}
2140 	if (error && NFS_ISV4(fdvp))
2141 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2142 	return (error);
2143 }
2144 
2145 /*
2146  * nfs hard link create call
2147  */
2148 static int
2149 nfs_link(struct vop_link_args *ap)
2150 {
2151 	struct vnode *vp = ap->a_vp;
2152 	struct vnode *tdvp = ap->a_tdvp;
2153 	struct componentname *cnp = ap->a_cnp;
2154 	struct nfsnode *np, *tdnp;
2155 	struct nfsvattr nfsva, dnfsva;
2156 	int error = 0, attrflag, dattrflag;
2157 
2158 	/*
2159 	 * Push all writes to the server, so that the attribute cache
2160 	 * doesn't get "out of sync" with the server.
2161 	 * XXX There should be a better way!
2162 	 */
2163 	VOP_FSYNC(vp, MNT_WAIT, curthread);
2164 
2165 	error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
2166 	    cnp->cn_cred, curthread, &dnfsva, &nfsva, &attrflag, &dattrflag);
2167 	tdnp = VTONFS(tdvp);
2168 	NFSLOCKNODE(tdnp);
2169 	tdnp->n_flag |= NMODIFIED;
2170 	if (dattrflag != 0) {
2171 		NFSUNLOCKNODE(tdnp);
2172 		(void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, 0, 1);
2173 	} else {
2174 		tdnp->n_attrstamp = 0;
2175 		NFSUNLOCKNODE(tdnp);
2176 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2177 	}
2178 	if (attrflag)
2179 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2180 	else {
2181 		np = VTONFS(vp);
2182 		NFSLOCKNODE(np);
2183 		np->n_attrstamp = 0;
2184 		NFSUNLOCKNODE(np);
2185 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2186 	}
2187 	/*
2188 	 * If negative lookup caching is enabled, I might as well
2189 	 * add an entry for this node. Not necessary for correctness,
2190 	 * but if negative caching is enabled, then the system
2191 	 * must care about lookup caching hit rate, so...
2192 	 */
2193 	if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
2194 	    (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2195 		if (tdvp != vp)
2196 			cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
2197 		else
2198 			printf("nfs_link: bogus NFS server returned "
2199 			    "the directory as the new link\n");
2200 	}
2201 	if (error && NFS_ISV4(vp))
2202 		error = nfscl_maperr(curthread, error, (uid_t)0,
2203 		    (gid_t)0);
2204 	return (error);
2205 }
2206 
2207 /*
2208  * nfs symbolic link create call
2209  */
2210 static int
2211 nfs_symlink(struct vop_symlink_args *ap)
2212 {
2213 	struct vnode *dvp = ap->a_dvp;
2214 	struct vattr *vap = ap->a_vap;
2215 	struct componentname *cnp = ap->a_cnp;
2216 	struct nfsvattr nfsva, dnfsva;
2217 	struct nfsfh *nfhp;
2218 	struct nfsnode *np = NULL, *dnp;
2219 	struct vnode *newvp = NULL;
2220 	int error = 0, attrflag, dattrflag, ret;
2221 
2222 	vap->va_type = VLNK;
2223 	error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2224 	    ap->a_target, vap, cnp->cn_cred, curthread, &dnfsva,
2225 	    &nfsva, &nfhp, &attrflag, &dattrflag);
2226 	if (nfhp) {
2227 		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2228 		    &np, LK_EXCLUSIVE);
2229 		if (!ret)
2230 			newvp = NFSTOV(np);
2231 		else if (!error)
2232 			error = ret;
2233 	}
2234 	if (newvp != NULL) {
2235 		if (attrflag)
2236 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
2237 	} else if (!error) {
2238 		/*
2239 		 * If we do not have an error and we could not extract the
2240 		 * newvp from the response due to the request being NFSv2, we
2241 		 * have to do a lookup in order to obtain a newvp to return.
2242 		 */
2243 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2244 		    cnp->cn_cred, curthread, &np);
2245 		if (!error)
2246 			newvp = NFSTOV(np);
2247 	}
2248 	if (error) {
2249 		if (newvp)
2250 			vput(newvp);
2251 		if (NFS_ISV4(dvp))
2252 			error = nfscl_maperr(curthread, error,
2253 			    vap->va_uid, vap->va_gid);
2254 	} else {
2255 		*ap->a_vpp = newvp;
2256 	}
2257 
2258 	dnp = VTONFS(dvp);
2259 	NFSLOCKNODE(dnp);
2260 	dnp->n_flag |= NMODIFIED;
2261 	if (dattrflag != 0) {
2262 		NFSUNLOCKNODE(dnp);
2263 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2264 	} else {
2265 		dnp->n_attrstamp = 0;
2266 		NFSUNLOCKNODE(dnp);
2267 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2268 	}
2269 	/*
2270 	 * If negative lookup caching is enabled, I might as well
2271 	 * add an entry for this node. Not necessary for correctness,
2272 	 * but if negative caching is enabled, then the system
2273 	 * must care about lookup caching hit rate, so...
2274 	 */
2275 	if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2276 	    (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2277 		if (dvp != newvp)
2278 			cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2279 			    NULL);
2280 		else
2281 			printf("nfs_symlink: bogus NFS server returned "
2282 			    "the directory as the new file object\n");
2283 	}
2284 	return (error);
2285 }
2286 
2287 /*
2288  * nfs make dir call
2289  */
2290 static int
2291 nfs_mkdir(struct vop_mkdir_args *ap)
2292 {
2293 	struct vnode *dvp = ap->a_dvp;
2294 	struct vattr *vap = ap->a_vap;
2295 	struct componentname *cnp = ap->a_cnp;
2296 	struct nfsnode *np = NULL, *dnp;
2297 	struct vnode *newvp = NULL;
2298 	struct vattr vattr;
2299 	struct nfsfh *nfhp;
2300 	struct nfsvattr nfsva, dnfsva;
2301 	int error = 0, attrflag, dattrflag, ret;
2302 
2303 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2304 		return (error);
2305 	vap->va_type = VDIR;
2306 	error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2307 	    vap, cnp->cn_cred, curthread, &dnfsva, &nfsva, &nfhp,
2308 	    &attrflag, &dattrflag);
2309 	dnp = VTONFS(dvp);
2310 	NFSLOCKNODE(dnp);
2311 	dnp->n_flag |= NMODIFIED;
2312 	if (dattrflag != 0) {
2313 		NFSUNLOCKNODE(dnp);
2314 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2315 	} else {
2316 		dnp->n_attrstamp = 0;
2317 		NFSUNLOCKNODE(dnp);
2318 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2319 	}
2320 	if (nfhp) {
2321 		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2322 		    &np, LK_EXCLUSIVE);
2323 		if (!ret) {
2324 			newvp = NFSTOV(np);
2325 			if (attrflag)
2326 			   (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2327 				0, 1);
2328 		} else if (!error)
2329 			error = ret;
2330 	}
2331 	if (!error && newvp == NULL) {
2332 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2333 		    cnp->cn_cred, curthread, &np);
2334 		if (!error) {
2335 			newvp = NFSTOV(np);
2336 			if (newvp->v_type != VDIR)
2337 				error = EEXIST;
2338 		}
2339 	}
2340 	if (error) {
2341 		if (newvp)
2342 			vput(newvp);
2343 		if (NFS_ISV4(dvp))
2344 			error = nfscl_maperr(curthread, error,
2345 			    vap->va_uid, vap->va_gid);
2346 	} else {
2347 		/*
2348 		 * If negative lookup caching is enabled, I might as well
2349 		 * add an entry for this node. Not necessary for correctness,
2350 		 * but if negative caching is enabled, then the system
2351 		 * must care about lookup caching hit rate, so...
2352 		 */
2353 		if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2354 		    (cnp->cn_flags & MAKEENTRY) &&
2355 		    attrflag != 0 && dattrflag != 0) {
2356 			if (dvp != newvp)
2357 				cache_enter_time(dvp, newvp, cnp,
2358 				    &nfsva.na_ctime, &dnfsva.na_ctime);
2359 			else
2360 				printf("nfs_mkdir: bogus NFS server returned "
2361 				    "the directory that the directory was "
2362 				    "created in as the new file object\n");
2363 		}
2364 		*ap->a_vpp = newvp;
2365 	}
2366 	return (error);
2367 }
2368 
2369 /*
2370  * nfs remove directory call
2371  */
2372 static int
2373 nfs_rmdir(struct vop_rmdir_args *ap)
2374 {
2375 	struct vnode *vp = ap->a_vp;
2376 	struct vnode *dvp = ap->a_dvp;
2377 	struct componentname *cnp = ap->a_cnp;
2378 	struct nfsnode *dnp;
2379 	struct nfsvattr dnfsva;
2380 	int error, dattrflag;
2381 
2382 	if (dvp == vp)
2383 		return (EINVAL);
2384 	error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2385 	    cnp->cn_cred, curthread, &dnfsva, &dattrflag);
2386 	dnp = VTONFS(dvp);
2387 	NFSLOCKNODE(dnp);
2388 	dnp->n_flag |= NMODIFIED;
2389 	if (dattrflag != 0) {
2390 		NFSUNLOCKNODE(dnp);
2391 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2392 	} else {
2393 		dnp->n_attrstamp = 0;
2394 		NFSUNLOCKNODE(dnp);
2395 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2396 	}
2397 
2398 	cache_purge(dvp);
2399 	cache_purge(vp);
2400 	if (error && NFS_ISV4(dvp))
2401 		error = nfscl_maperr(curthread, error, (uid_t)0,
2402 		    (gid_t)0);
2403 	/*
2404 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2405 	 */
2406 	if (error == ENOENT)
2407 		error = 0;
2408 	return (error);
2409 }
2410 
2411 /*
2412  * nfs readdir call
2413  */
2414 static int
2415 nfs_readdir(struct vop_readdir_args *ap)
2416 {
2417 	struct vnode *vp = ap->a_vp;
2418 	struct nfsnode *np = VTONFS(vp);
2419 	struct uio *uio = ap->a_uio;
2420 	ssize_t tresid, left;
2421 	int error = 0;
2422 	struct vattr vattr;
2423 
2424 	if (ap->a_eofflag != NULL)
2425 		*ap->a_eofflag = 0;
2426 	if (vp->v_type != VDIR)
2427 		return(EPERM);
2428 
2429 	/*
2430 	 * First, check for hit on the EOF offset cache
2431 	 */
2432 	NFSLOCKNODE(np);
2433 	if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2434 	    (np->n_flag & NMODIFIED) == 0) {
2435 		NFSUNLOCKNODE(np);
2436 		if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2437 			NFSLOCKNODE(np);
2438 			if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2439 			    !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2440 				NFSUNLOCKNODE(np);
2441 				NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2442 				if (ap->a_eofflag != NULL)
2443 					*ap->a_eofflag = 1;
2444 				return (0);
2445 			} else
2446 				NFSUNLOCKNODE(np);
2447 		}
2448 	} else
2449 		NFSUNLOCKNODE(np);
2450 
2451 	/*
2452 	 * NFS always guarantees that directory entries don't straddle
2453 	 * DIRBLKSIZ boundaries.  As such, we need to limit the size
2454 	 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2455 	 * directory entry.
2456 	 */
2457 	left = uio->uio_resid % DIRBLKSIZ;
2458 	if (left == uio->uio_resid)
2459 		return (EINVAL);
2460 	uio->uio_resid -= left;
2461 
2462 	/*
2463 	 * Call ncl_bioread() to do the real work.
2464 	 */
2465 	tresid = uio->uio_resid;
2466 	error = ncl_bioread(vp, uio, 0, ap->a_cred);
2467 
2468 	if (!error && uio->uio_resid == tresid) {
2469 		NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2470 		if (ap->a_eofflag != NULL)
2471 			*ap->a_eofflag = 1;
2472 	}
2473 
2474 	/* Add the partial DIRBLKSIZ (left) back in. */
2475 	uio->uio_resid += left;
2476 	return (error);
2477 }
2478 
2479 /*
2480  * Readdir rpc call.
2481  * Called from below the buffer cache by ncl_doio().
2482  */
2483 int
2484 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2485     struct thread *td)
2486 {
2487 	struct nfsvattr nfsva;
2488 	nfsuint64 *cookiep, cookie;
2489 	struct nfsnode *dnp = VTONFS(vp);
2490 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2491 	int error = 0, eof, attrflag;
2492 
2493 	KASSERT(uiop->uio_iovcnt == 1 &&
2494 	    (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2495 	    (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2496 	    ("nfs readdirrpc bad uio"));
2497 
2498 	/*
2499 	 * If there is no cookie, assume directory was stale.
2500 	 */
2501 	ncl_dircookie_lock(dnp);
2502 	NFSUNLOCKNODE(dnp);
2503 	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2504 	if (cookiep) {
2505 		cookie = *cookiep;
2506 		ncl_dircookie_unlock(dnp);
2507 	} else {
2508 		ncl_dircookie_unlock(dnp);
2509 		return (NFSERR_BAD_COOKIE);
2510 	}
2511 
2512 	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2513 		(void)ncl_fsinfo(nmp, vp, cred, td);
2514 
2515 	error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2516 	    &attrflag, &eof);
2517 	if (attrflag)
2518 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2519 
2520 	if (!error) {
2521 		/*
2522 		 * We are now either at the end of the directory or have filled
2523 		 * the block.
2524 		 */
2525 		if (eof) {
2526 			NFSLOCKNODE(dnp);
2527 			dnp->n_direofoffset = uiop->uio_offset;
2528 			NFSUNLOCKNODE(dnp);
2529 		} else {
2530 			if (uiop->uio_resid > 0)
2531 				printf("EEK! readdirrpc resid > 0\n");
2532 			ncl_dircookie_lock(dnp);
2533 			NFSUNLOCKNODE(dnp);
2534 			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2535 			*cookiep = cookie;
2536 			ncl_dircookie_unlock(dnp);
2537 		}
2538 	} else if (NFS_ISV4(vp)) {
2539 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2540 	}
2541 	return (error);
2542 }
2543 
2544 /*
2545  * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2546  */
2547 int
2548 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2549     struct thread *td)
2550 {
2551 	struct nfsvattr nfsva;
2552 	nfsuint64 *cookiep, cookie;
2553 	struct nfsnode *dnp = VTONFS(vp);
2554 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2555 	int error = 0, attrflag, eof;
2556 
2557 	KASSERT(uiop->uio_iovcnt == 1 &&
2558 	    (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2559 	    (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2560 	    ("nfs readdirplusrpc bad uio"));
2561 
2562 	/*
2563 	 * If there is no cookie, assume directory was stale.
2564 	 */
2565 	ncl_dircookie_lock(dnp);
2566 	NFSUNLOCKNODE(dnp);
2567 	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2568 	if (cookiep) {
2569 		cookie = *cookiep;
2570 		ncl_dircookie_unlock(dnp);
2571 	} else {
2572 		ncl_dircookie_unlock(dnp);
2573 		return (NFSERR_BAD_COOKIE);
2574 	}
2575 
2576 	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2577 		(void)ncl_fsinfo(nmp, vp, cred, td);
2578 	error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2579 	    &attrflag, &eof);
2580 	if (attrflag)
2581 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2582 
2583 	if (!error) {
2584 		/*
2585 		 * We are now either at end of the directory or have filled the
2586 		 * the block.
2587 		 */
2588 		if (eof) {
2589 			NFSLOCKNODE(dnp);
2590 			dnp->n_direofoffset = uiop->uio_offset;
2591 			NFSUNLOCKNODE(dnp);
2592 		} else {
2593 			if (uiop->uio_resid > 0)
2594 				printf("EEK! readdirplusrpc resid > 0\n");
2595 			ncl_dircookie_lock(dnp);
2596 			NFSUNLOCKNODE(dnp);
2597 			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2598 			*cookiep = cookie;
2599 			ncl_dircookie_unlock(dnp);
2600 		}
2601 	} else if (NFS_ISV4(vp)) {
2602 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2603 	}
2604 	return (error);
2605 }
2606 
2607 /*
2608  * Silly rename. To make the NFS filesystem that is stateless look a little
2609  * more like the "ufs" a remove of an active vnode is translated to a rename
2610  * to a funny looking filename that is removed by nfs_inactive on the
2611  * nfsnode. There is the potential for another process on a different client
2612  * to create the same funny name between the nfs_lookitup() fails and the
2613  * nfs_rename() completes, but...
2614  */
2615 static int
2616 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2617 {
2618 	struct sillyrename *sp;
2619 	struct nfsnode *np;
2620 	int error;
2621 	short pid;
2622 	unsigned int lticks;
2623 
2624 	cache_purge(dvp);
2625 	np = VTONFS(vp);
2626 	KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2627 	sp = malloc(sizeof (struct sillyrename),
2628 	    M_NEWNFSREQ, M_WAITOK);
2629 	sp->s_cred = crhold(cnp->cn_cred);
2630 	sp->s_dvp = dvp;
2631 	VREF(dvp);
2632 
2633 	/*
2634 	 * Fudge together a funny name.
2635 	 * Changing the format of the funny name to accommodate more
2636 	 * sillynames per directory.
2637 	 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2638 	 * CPU ticks since boot.
2639 	 */
2640 	pid = curthread->td_proc->p_pid;
2641 	lticks = (unsigned int)ticks;
2642 	for ( ; ; ) {
2643 		sp->s_namlen = sprintf(sp->s_name,
2644 				       ".nfs.%08x.%04x4.4", lticks,
2645 				       pid);
2646 		if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2647 				 curthread, NULL))
2648 			break;
2649 		lticks++;
2650 	}
2651 	error = nfs_renameit(dvp, vp, cnp, sp);
2652 	if (error)
2653 		goto bad;
2654 	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2655 		curthread, &np);
2656 	np->n_sillyrename = sp;
2657 	return (0);
2658 bad:
2659 	vrele(sp->s_dvp);
2660 	crfree(sp->s_cred);
2661 	free(sp, M_NEWNFSREQ);
2662 	return (error);
2663 }
2664 
2665 /*
2666  * Look up a file name and optionally either update the file handle or
2667  * allocate an nfsnode, depending on the value of npp.
2668  * npp == NULL	--> just do the lookup
2669  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2670  *			handled too
2671  * *npp != NULL --> update the file handle in the vnode
2672  */
2673 static int
2674 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2675     struct thread *td, struct nfsnode **npp)
2676 {
2677 	struct vnode *newvp = NULL, *vp;
2678 	struct nfsnode *np, *dnp = VTONFS(dvp);
2679 	struct nfsfh *nfhp, *onfhp;
2680 	struct nfsvattr nfsva, dnfsva;
2681 	struct componentname cn;
2682 	int error = 0, attrflag, dattrflag;
2683 	u_int hash;
2684 	struct timespec ts;
2685 
2686 	nanouptime(&ts);
2687 	error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2688 	    &nfhp, &attrflag, &dattrflag, 0);
2689 	if (dattrflag)
2690 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2691 	if (npp && !error) {
2692 		if (*npp != NULL) {
2693 		    np = *npp;
2694 		    vp = NFSTOV(np);
2695 		    /*
2696 		     * For NFSv4, check to see if it is the same name and
2697 		     * replace the name, if it is different.
2698 		     */
2699 		    if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2700 			(np->n_v4->n4_namelen != len ||
2701 			 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2702 			 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2703 			 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2704 			 dnp->n_fhp->nfh_len))) {
2705 			    free(np->n_v4, M_NFSV4NODE);
2706 			    np->n_v4 = malloc(
2707 				sizeof (struct nfsv4node) +
2708 				dnp->n_fhp->nfh_len + len - 1,
2709 				M_NFSV4NODE, M_WAITOK);
2710 			    np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2711 			    np->n_v4->n4_namelen = len;
2712 			    NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2713 				dnp->n_fhp->nfh_len);
2714 			    NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2715 		    }
2716 		    hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2717 			FNV1_32_INIT);
2718 		    onfhp = np->n_fhp;
2719 		    /*
2720 		     * Rehash node for new file handle.
2721 		     */
2722 		    vfs_hash_rehash(vp, hash);
2723 		    np->n_fhp = nfhp;
2724 		    if (onfhp != NULL)
2725 			free(onfhp, M_NFSFH);
2726 		    newvp = NFSTOV(np);
2727 		} else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2728 		    free(nfhp, M_NFSFH);
2729 		    VREF(dvp);
2730 		    newvp = dvp;
2731 		} else {
2732 		    cn.cn_nameptr = name;
2733 		    cn.cn_namelen = len;
2734 		    error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2735 			&np, LK_EXCLUSIVE);
2736 		    if (error)
2737 			return (error);
2738 		    newvp = NFSTOV(np);
2739 		    /*
2740 		     * If n_localmodtime >= time before RPC, then
2741 		     * a file modification operation, such as
2742 		     * VOP_SETATTR() of size, has occurred while
2743 		     * the Lookup RPC and acquisition of the vnode
2744 		     * happened.  As such, the attributes might
2745 		     * be stale, with possibly an incorrect size.
2746 		     */
2747 		    NFSLOCKNODE(np);
2748 		    if (timespecisset(&np->n_localmodtime) &&
2749 			timespeccmp(&np->n_localmodtime, &ts, >=)) {
2750 			NFSCL_DEBUG(4, "nfs_lookitup: localmod "
2751 			    "stale attributes\n");
2752 			attrflag = 0;
2753 		    }
2754 		    NFSUNLOCKNODE(np);
2755 		}
2756 		if (!attrflag && *npp == NULL) {
2757 			if (newvp == dvp)
2758 				vrele(newvp);
2759 			else
2760 				vput(newvp);
2761 			return (ENOENT);
2762 		}
2763 		if (attrflag)
2764 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
2765 	}
2766 	if (npp && *npp == NULL) {
2767 		if (error) {
2768 			if (newvp) {
2769 				if (newvp == dvp)
2770 					vrele(newvp);
2771 				else
2772 					vput(newvp);
2773 			}
2774 		} else
2775 			*npp = np;
2776 	}
2777 	if (error && NFS_ISV4(dvp))
2778 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2779 	return (error);
2780 }
2781 
2782 /*
2783  * Nfs Version 3 and 4 commit rpc
2784  */
2785 int
2786 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2787    struct thread *td)
2788 {
2789 	struct nfsvattr nfsva;
2790 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2791 	struct nfsnode *np;
2792 	struct uio uio;
2793 	int error, attrflag;
2794 
2795 	np = VTONFS(vp);
2796 	error = EIO;
2797 	attrflag = 0;
2798 	if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) {
2799 		uio.uio_offset = offset;
2800 		uio.uio_resid = cnt;
2801 		error = nfscl_doiods(vp, &uio, NULL, NULL,
2802 		    NFSV4OPEN_ACCESSWRITE, 1, cred, td);
2803 		if (error != 0) {
2804 			NFSLOCKNODE(np);
2805 			np->n_flag &= ~NDSCOMMIT;
2806 			NFSUNLOCKNODE(np);
2807 		}
2808 	}
2809 	if (error != 0) {
2810 		mtx_lock(&nmp->nm_mtx);
2811 		if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2812 			mtx_unlock(&nmp->nm_mtx);
2813 			return (0);
2814 		}
2815 		mtx_unlock(&nmp->nm_mtx);
2816 		error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
2817 		    &attrflag);
2818 	}
2819 	if (attrflag != 0)
2820 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2821 	if (error != 0 && NFS_ISV4(vp))
2822 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2823 	return (error);
2824 }
2825 
2826 /*
2827  * Strategy routine.
2828  * For async requests when nfsiod(s) are running, queue the request by
2829  * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2830  * request.
2831  */
2832 static int
2833 nfs_strategy(struct vop_strategy_args *ap)
2834 {
2835 	struct buf *bp;
2836 	struct vnode *vp;
2837 	struct ucred *cr;
2838 
2839 	bp = ap->a_bp;
2840 	vp = ap->a_vp;
2841 	KASSERT(bp->b_vp == vp, ("missing b_getvp"));
2842 	KASSERT(!(bp->b_flags & B_DONE),
2843 	    ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2844 
2845 	if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno)
2846 		bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize /
2847 		    DEV_BSIZE);
2848 	if (bp->b_iocmd == BIO_READ)
2849 		cr = bp->b_rcred;
2850 	else
2851 		cr = bp->b_wcred;
2852 
2853 	/*
2854 	 * If the op is asynchronous and an i/o daemon is waiting
2855 	 * queue the request, wake it up and wait for completion
2856 	 * otherwise just do it ourselves.
2857 	 */
2858 	if ((bp->b_flags & B_ASYNC) == 0 ||
2859 	    ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread))
2860 		(void) ncl_doio(vp, bp, cr, curthread, 1);
2861 	return (0);
2862 }
2863 
2864 /*
2865  * fsync vnode op. Just call ncl_flush() with commit == 1.
2866  */
2867 /* ARGSUSED */
2868 static int
2869 nfs_fsync(struct vop_fsync_args *ap)
2870 {
2871 
2872 	if (ap->a_vp->v_type != VREG) {
2873 		/*
2874 		 * For NFS, metadata is changed synchronously on the server,
2875 		 * so there is nothing to flush. Also, ncl_flush() clears
2876 		 * the NMODIFIED flag and that shouldn't be done here for
2877 		 * directories.
2878 		 */
2879 		return (0);
2880 	}
2881 	return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0));
2882 }
2883 
2884 /*
2885  * Flush all the blocks associated with a vnode.
2886  * 	Walk through the buffer pool and push any dirty pages
2887  *	associated with the vnode.
2888  * If the called_from_renewthread argument is TRUE, it has been called
2889  * from the NFSv4 renew thread and, as such, cannot block indefinitely
2890  * waiting for a buffer write to complete.
2891  */
2892 int
2893 ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
2894     int commit, int called_from_renewthread)
2895 {
2896 	struct nfsnode *np = VTONFS(vp);
2897 	struct buf *bp;
2898 	int i;
2899 	struct buf *nbp;
2900 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2901 	int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2902 	int passone = 1, trycnt = 0;
2903 	u_quad_t off, endoff, toff;
2904 	struct ucred* wcred = NULL;
2905 	struct buf **bvec = NULL;
2906 	struct bufobj *bo;
2907 #ifndef NFS_COMMITBVECSIZ
2908 #define	NFS_COMMITBVECSIZ	20
2909 #endif
2910 	struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2911 	u_int bvecsize = 0, bveccount;
2912 	struct timespec ts;
2913 
2914 	if (called_from_renewthread != 0)
2915 		slptimeo = hz;
2916 	if (nmp->nm_flag & NFSMNT_INT)
2917 		slpflag = PCATCH;
2918 	if (!commit)
2919 		passone = 0;
2920 	bo = &vp->v_bufobj;
2921 	/*
2922 	 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2923 	 * server, but has not been committed to stable storage on the server
2924 	 * yet. On the first pass, the byte range is worked out and the commit
2925 	 * rpc is done. On the second pass, bwrite() is called to do the
2926 	 * job.
2927 	 */
2928 again:
2929 	off = (u_quad_t)-1;
2930 	endoff = 0;
2931 	bvecpos = 0;
2932 	if (NFS_ISV34(vp) && commit) {
2933 		if (bvec != NULL && bvec != bvec_on_stack)
2934 			free(bvec, M_TEMP);
2935 		/*
2936 		 * Count up how many buffers waiting for a commit.
2937 		 */
2938 		bveccount = 0;
2939 		BO_LOCK(bo);
2940 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2941 			if (!BUF_ISLOCKED(bp) &&
2942 			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2943 				== (B_DELWRI | B_NEEDCOMMIT))
2944 				bveccount++;
2945 		}
2946 		/*
2947 		 * Allocate space to remember the list of bufs to commit.  It is
2948 		 * important to use M_NOWAIT here to avoid a race with nfs_write.
2949 		 * If we can't get memory (for whatever reason), we will end up
2950 		 * committing the buffers one-by-one in the loop below.
2951 		 */
2952 		if (bveccount > NFS_COMMITBVECSIZ) {
2953 			/*
2954 			 * Release the vnode interlock to avoid a lock
2955 			 * order reversal.
2956 			 */
2957 			BO_UNLOCK(bo);
2958 			bvec = (struct buf **)
2959 				malloc(bveccount * sizeof(struct buf *),
2960 				       M_TEMP, M_NOWAIT);
2961 			BO_LOCK(bo);
2962 			if (bvec == NULL) {
2963 				bvec = bvec_on_stack;
2964 				bvecsize = NFS_COMMITBVECSIZ;
2965 			} else
2966 				bvecsize = bveccount;
2967 		} else {
2968 			bvec = bvec_on_stack;
2969 			bvecsize = NFS_COMMITBVECSIZ;
2970 		}
2971 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2972 			if (bvecpos >= bvecsize)
2973 				break;
2974 			if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2975 				nbp = TAILQ_NEXT(bp, b_bobufs);
2976 				continue;
2977 			}
2978 			if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2979 			    (B_DELWRI | B_NEEDCOMMIT)) {
2980 				BUF_UNLOCK(bp);
2981 				nbp = TAILQ_NEXT(bp, b_bobufs);
2982 				continue;
2983 			}
2984 			BO_UNLOCK(bo);
2985 			bremfree(bp);
2986 			/*
2987 			 * Work out if all buffers are using the same cred
2988 			 * so we can deal with them all with one commit.
2989 			 *
2990 			 * NOTE: we are not clearing B_DONE here, so we have
2991 			 * to do it later on in this routine if we intend to
2992 			 * initiate I/O on the bp.
2993 			 *
2994 			 * Note: to avoid loopback deadlocks, we do not
2995 			 * assign b_runningbufspace.
2996 			 */
2997 			if (wcred == NULL)
2998 				wcred = bp->b_wcred;
2999 			else if (wcred != bp->b_wcred)
3000 				wcred = NOCRED;
3001 			vfs_busy_pages(bp, 0);
3002 
3003 			BO_LOCK(bo);
3004 			/*
3005 			 * bp is protected by being locked, but nbp is not
3006 			 * and vfs_busy_pages() may sleep.  We have to
3007 			 * recalculate nbp.
3008 			 */
3009 			nbp = TAILQ_NEXT(bp, b_bobufs);
3010 
3011 			/*
3012 			 * A list of these buffers is kept so that the
3013 			 * second loop knows which buffers have actually
3014 			 * been committed. This is necessary, since there
3015 			 * may be a race between the commit rpc and new
3016 			 * uncommitted writes on the file.
3017 			 */
3018 			bvec[bvecpos++] = bp;
3019 			toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3020 				bp->b_dirtyoff;
3021 			if (toff < off)
3022 				off = toff;
3023 			toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
3024 			if (toff > endoff)
3025 				endoff = toff;
3026 		}
3027 		BO_UNLOCK(bo);
3028 	}
3029 	if (bvecpos > 0) {
3030 		/*
3031 		 * Commit data on the server, as required.
3032 		 * If all bufs are using the same wcred, then use that with
3033 		 * one call for all of them, otherwise commit each one
3034 		 * separately.
3035 		 */
3036 		if (wcred != NOCRED)
3037 			retv = ncl_commit(vp, off, (int)(endoff - off),
3038 					  wcred, td);
3039 		else {
3040 			retv = 0;
3041 			for (i = 0; i < bvecpos; i++) {
3042 				off_t off, size;
3043 				bp = bvec[i];
3044 				off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3045 					bp->b_dirtyoff;
3046 				size = (u_quad_t)(bp->b_dirtyend
3047 						  - bp->b_dirtyoff);
3048 				retv = ncl_commit(vp, off, (int)size,
3049 						  bp->b_wcred, td);
3050 				if (retv) break;
3051 			}
3052 		}
3053 
3054 		if (retv == NFSERR_STALEWRITEVERF)
3055 			ncl_clearcommit(vp->v_mount);
3056 
3057 		/*
3058 		 * Now, either mark the blocks I/O done or mark the
3059 		 * blocks dirty, depending on whether the commit
3060 		 * succeeded.
3061 		 */
3062 		for (i = 0; i < bvecpos; i++) {
3063 			bp = bvec[i];
3064 			bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3065 			if (!NFSCL_FORCEDISM(vp->v_mount) && retv) {
3066 				/*
3067 				 * Error, leave B_DELWRI intact
3068 				 */
3069 				vfs_unbusy_pages(bp);
3070 				brelse(bp);
3071 			} else {
3072 				/*
3073 				 * Success, remove B_DELWRI ( bundirty() ).
3074 				 *
3075 				 * b_dirtyoff/b_dirtyend seem to be NFS
3076 				 * specific.  We should probably move that
3077 				 * into bundirty(). XXX
3078 				 */
3079 				bufobj_wref(bo);
3080 				bp->b_flags |= B_ASYNC;
3081 				bundirty(bp);
3082 				bp->b_flags &= ~B_DONE;
3083 				bp->b_ioflags &= ~BIO_ERROR;
3084 				bp->b_dirtyoff = bp->b_dirtyend = 0;
3085 				bufdone(bp);
3086 			}
3087 		}
3088 	}
3089 
3090 	/*
3091 	 * Start/do any write(s) that are required.
3092 	 */
3093 loop:
3094 	BO_LOCK(bo);
3095 	TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3096 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3097 			if (waitfor != MNT_WAIT || passone)
3098 				continue;
3099 
3100 			error = BUF_TIMELOCK(bp,
3101 			    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
3102 			    BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
3103 			if (error == 0) {
3104 				BUF_UNLOCK(bp);
3105 				goto loop;
3106 			}
3107 			if (error == ENOLCK) {
3108 				error = 0;
3109 				goto loop;
3110 			}
3111 			if (called_from_renewthread != 0) {
3112 				/*
3113 				 * Return EIO so the flush will be retried
3114 				 * later.
3115 				 */
3116 				error = EIO;
3117 				goto done;
3118 			}
3119 			if (newnfs_sigintr(nmp, td)) {
3120 				error = EINTR;
3121 				goto done;
3122 			}
3123 			if (slpflag == PCATCH) {
3124 				slpflag = 0;
3125 				slptimeo = 2 * hz;
3126 			}
3127 			goto loop;
3128 		}
3129 		if ((bp->b_flags & B_DELWRI) == 0)
3130 			panic("nfs_fsync: not dirty");
3131 		if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3132 			BUF_UNLOCK(bp);
3133 			continue;
3134 		}
3135 		BO_UNLOCK(bo);
3136 		bremfree(bp);
3137 		bp->b_flags |= B_ASYNC;
3138 		bwrite(bp);
3139 		if (newnfs_sigintr(nmp, td)) {
3140 			error = EINTR;
3141 			goto done;
3142 		}
3143 		goto loop;
3144 	}
3145 	if (passone) {
3146 		passone = 0;
3147 		BO_UNLOCK(bo);
3148 		goto again;
3149 	}
3150 	if (waitfor == MNT_WAIT) {
3151 		while (bo->bo_numoutput) {
3152 			error = bufobj_wwait(bo, slpflag, slptimeo);
3153 			if (error) {
3154 			    BO_UNLOCK(bo);
3155 			    if (called_from_renewthread != 0) {
3156 				/*
3157 				 * Return EIO so that the flush will be
3158 				 * retried later.
3159 				 */
3160 				error = EIO;
3161 				goto done;
3162 			    }
3163 			    error = newnfs_sigintr(nmp, td);
3164 			    if (error)
3165 				goto done;
3166 			    if (slpflag == PCATCH) {
3167 				slpflag = 0;
3168 				slptimeo = 2 * hz;
3169 			    }
3170 			    BO_LOCK(bo);
3171 			}
3172 		}
3173 		if (bo->bo_dirty.bv_cnt != 0 && commit) {
3174 			BO_UNLOCK(bo);
3175 			goto loop;
3176 		}
3177 		/*
3178 		 * Wait for all the async IO requests to drain
3179 		 */
3180 		BO_UNLOCK(bo);
3181 		NFSLOCKNODE(np);
3182 		while (np->n_directio_asyncwr > 0) {
3183 			np->n_flag |= NFSYNCWAIT;
3184 			error = newnfs_msleep(td, &np->n_directio_asyncwr,
3185 			    &np->n_mtx, slpflag | (PRIBIO + 1),
3186 			    "nfsfsync", 0);
3187 			if (error) {
3188 				if (newnfs_sigintr(nmp, td)) {
3189 					NFSUNLOCKNODE(np);
3190 					error = EINTR;
3191 					goto done;
3192 				}
3193 			}
3194 		}
3195 		NFSUNLOCKNODE(np);
3196 	} else
3197 		BO_UNLOCK(bo);
3198 	if (NFSHASPNFS(nmp)) {
3199 		nfscl_layoutcommit(vp, td);
3200 		/*
3201 		 * Invalidate the attribute cache, since writes to a DS
3202 		 * won't update the size attribute.
3203 		 */
3204 		NFSLOCKNODE(np);
3205 		np->n_attrstamp = 0;
3206 	} else
3207 		NFSLOCKNODE(np);
3208 	if (np->n_flag & NWRITEERR) {
3209 		error = np->n_error;
3210 		np->n_flag &= ~NWRITEERR;
3211 	}
3212   	if (commit && bo->bo_dirty.bv_cnt == 0 &&
3213 	    bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
3214   		np->n_flag &= ~NMODIFIED;
3215 	NFSUNLOCKNODE(np);
3216 done:
3217 	if (bvec != NULL && bvec != bvec_on_stack)
3218 		free(bvec, M_TEMP);
3219 	if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
3220 	    (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
3221 	    np->n_directio_asyncwr != 0)) {
3222 		if (trycnt++ < 5) {
3223 			/* try, try again... */
3224 			passone = 1;
3225 			wcred = NULL;
3226 			bvec = NULL;
3227 			bvecsize = 0;
3228 			goto again;
3229 		}
3230 		vn_printf(vp, "ncl_flush failed");
3231 		error = called_from_renewthread != 0 ? EIO : EBUSY;
3232 	}
3233 	if (error == 0) {
3234 		nanouptime(&ts);
3235 		NFSLOCKNODE(np);
3236 		np->n_localmodtime = ts;
3237 		NFSUNLOCKNODE(np);
3238 	}
3239 	return (error);
3240 }
3241 
3242 /*
3243  * NFS advisory byte-level locks.
3244  */
3245 static int
3246 nfs_advlock(struct vop_advlock_args *ap)
3247 {
3248 	struct vnode *vp = ap->a_vp;
3249 	struct ucred *cred;
3250 	struct nfsnode *np = VTONFS(ap->a_vp);
3251 	struct proc *p = (struct proc *)ap->a_id;
3252 	struct thread *td = curthread;	/* XXX */
3253 	struct vattr va;
3254 	int ret, error;
3255 	u_quad_t size;
3256 	struct nfsmount *nmp;
3257 
3258 	error = NFSVOPLOCK(vp, LK_SHARED);
3259 	if (error != 0)
3260 		return (EBADF);
3261 	nmp = VFSTONFS(vp->v_mount);
3262 	if (!NFS_ISV4(vp) || (nmp->nm_flag & NFSMNT_NOLOCKD) != 0) {
3263 		if ((nmp->nm_flag & NFSMNT_NOLOCKD) != 0) {
3264 			size = np->n_size;
3265 			NFSVOPUNLOCK(vp);
3266 			error = lf_advlock(ap, &(vp->v_lockf), size);
3267 		} else {
3268 			if (nfs_advlock_p != NULL)
3269 				error = nfs_advlock_p(ap);
3270 			else {
3271 				NFSVOPUNLOCK(vp);
3272 				error = ENOLCK;
3273 			}
3274 		}
3275 		if (error == 0 && ap->a_op == F_SETLK) {
3276 			error = NFSVOPLOCK(vp, LK_SHARED);
3277 			if (error == 0) {
3278 				/* Mark that a file lock has been acquired. */
3279 				NFSLOCKNODE(np);
3280 				np->n_flag |= NHASBEENLOCKED;
3281 				NFSUNLOCKNODE(np);
3282 				NFSVOPUNLOCK(vp);
3283 			}
3284 		}
3285 		return (error);
3286 	} else if ((ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3287 		if (vp->v_type != VREG) {
3288 			error = EINVAL;
3289 			goto out;
3290 		}
3291 		if ((ap->a_flags & F_POSIX) != 0)
3292 			cred = p->p_ucred;
3293 		else
3294 			cred = td->td_ucred;
3295 		NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
3296 		if (VN_IS_DOOMED(vp)) {
3297 			error = EBADF;
3298 			goto out;
3299 		}
3300 
3301 		/*
3302 		 * If this is unlocking a write locked region, flush and
3303 		 * commit them before unlocking. This is required by
3304 		 * RFC3530 Sec. 9.3.2.
3305 		 */
3306 		if (ap->a_op == F_UNLCK &&
3307 		    nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3308 		    ap->a_flags))
3309 			(void) ncl_flush(vp, MNT_WAIT, td, 1, 0);
3310 
3311 		/*
3312 		 * Mark NFS node as might have acquired a lock.
3313 		 * This is separate from NHASBEENLOCKED, because it must
3314 		 * be done before the nfsrpc_advlock() call, which might
3315 		 * add a nfscllock structure to the client state.
3316 		 * It is used to check for the case where a nfscllock
3317 		 * state structure cannot exist for the file.
3318 		 * Only done for "oneopenown" NFSv4.1/4.2 mounts.
3319 		 */
3320 		if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) {
3321 			NFSLOCKNODE(np);
3322 			np->n_flag |= NMIGHTBELOCKED;
3323 			NFSUNLOCKNODE(np);
3324 		}
3325 
3326 		/*
3327 		 * Loop around doing the lock op, while a blocking lock
3328 		 * must wait for the lock op to succeed.
3329 		 */
3330 		do {
3331 			ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3332 			    ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3333 			if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3334 			    ap->a_op == F_SETLK) {
3335 				NFSVOPUNLOCK(vp);
3336 				error = nfs_catnap(PZERO | PCATCH, ret,
3337 				    "ncladvl");
3338 				if (error)
3339 					return (EINTR);
3340 				NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3341 				if (VN_IS_DOOMED(vp)) {
3342 					error = EBADF;
3343 					goto out;
3344 				}
3345 			}
3346 		} while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3347 		     ap->a_op == F_SETLK);
3348 		if (ret == NFSERR_DENIED) {
3349 			error = EAGAIN;
3350 			goto out;
3351 		} else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3352 			error = ret;
3353 			goto out;
3354 		} else if (ret != 0) {
3355 			error = EACCES;
3356 			goto out;
3357 		}
3358 
3359 		/*
3360 		 * Now, if we just got a lock, invalidate data in the buffer
3361 		 * cache, as required, so that the coherency conforms with
3362 		 * RFC3530 Sec. 9.3.2.
3363 		 */
3364 		if (ap->a_op == F_SETLK) {
3365 			if ((np->n_flag & NMODIFIED) == 0) {
3366 				np->n_attrstamp = 0;
3367 				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3368 				ret = VOP_GETATTR(vp, &va, cred);
3369 			}
3370 			if ((np->n_flag & NMODIFIED) || ret ||
3371 			    np->n_change != va.va_filerev) {
3372 				(void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3373 				np->n_attrstamp = 0;
3374 				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3375 				ret = VOP_GETATTR(vp, &va, cred);
3376 				if (!ret) {
3377 					np->n_mtime = va.va_mtime;
3378 					np->n_change = va.va_filerev;
3379 				}
3380 			}
3381 			/* Mark that a file lock has been acquired. */
3382 			NFSLOCKNODE(np);
3383 			np->n_flag |= NHASBEENLOCKED;
3384 			NFSUNLOCKNODE(np);
3385 		}
3386 	} else
3387 		error = EOPNOTSUPP;
3388 out:
3389 	NFSVOPUNLOCK(vp);
3390 	return (error);
3391 }
3392 
3393 /*
3394  * NFS advisory byte-level locks.
3395  */
3396 static int
3397 nfs_advlockasync(struct vop_advlockasync_args *ap)
3398 {
3399 	struct vnode *vp = ap->a_vp;
3400 	u_quad_t size;
3401 	int error;
3402 
3403 	error = NFSVOPLOCK(vp, LK_SHARED);
3404 	if (error)
3405 		return (error);
3406 	if (NFS_ISV4(vp)) {
3407 		NFSVOPUNLOCK(vp);
3408 		return (EOPNOTSUPP);
3409 	}
3410 	if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3411 		size = VTONFS(vp)->n_size;
3412 		NFSVOPUNLOCK(vp);
3413 		error = lf_advlockasync(ap, &(vp->v_lockf), size);
3414 	} else {
3415 		NFSVOPUNLOCK(vp);
3416 		error = EOPNOTSUPP;
3417 	}
3418 	return (error);
3419 }
3420 
3421 /*
3422  * Print out the contents of an nfsnode.
3423  */
3424 static int
3425 nfs_print(struct vop_print_args *ap)
3426 {
3427 	struct vnode *vp = ap->a_vp;
3428 	struct nfsnode *np = VTONFS(vp);
3429 
3430 	printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid,
3431 	    (uintmax_t)np->n_vattr.na_fsid);
3432 	if (vp->v_type == VFIFO)
3433 		fifo_printinfo(vp);
3434 	printf("\n");
3435 	return (0);
3436 }
3437 
3438 /*
3439  * nfs special file access vnode op.
3440  * Essentially just get vattr and then imitate iaccess() since the device is
3441  * local to the client.
3442  */
3443 static int
3444 nfsspec_access(struct vop_access_args *ap)
3445 {
3446 	struct vattr *vap;
3447 	struct ucred *cred = ap->a_cred;
3448 	struct vnode *vp = ap->a_vp;
3449 	accmode_t accmode = ap->a_accmode;
3450 	struct vattr vattr;
3451 	int error;
3452 
3453 	/*
3454 	 * Disallow write attempts on filesystems mounted read-only;
3455 	 * unless the file is a socket, fifo, or a block or character
3456 	 * device resident on the filesystem.
3457 	 */
3458 	if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3459 		switch (vp->v_type) {
3460 		case VREG:
3461 		case VDIR:
3462 		case VLNK:
3463 			return (EROFS);
3464 		default:
3465 			break;
3466 		}
3467 	}
3468 	vap = &vattr;
3469 	error = VOP_GETATTR(vp, vap, cred);
3470 	if (error)
3471 		goto out;
3472 	error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3473 	    accmode, cred);
3474 out:
3475 	return error;
3476 }
3477 
3478 /*
3479  * Read wrapper for fifos.
3480  */
3481 static int
3482 nfsfifo_read(struct vop_read_args *ap)
3483 {
3484 	struct nfsnode *np = VTONFS(ap->a_vp);
3485 	int error;
3486 
3487 	/*
3488 	 * Set access flag.
3489 	 */
3490 	NFSLOCKNODE(np);
3491 	np->n_flag |= NACC;
3492 	vfs_timestamp(&np->n_atim);
3493 	NFSUNLOCKNODE(np);
3494 	error = fifo_specops.vop_read(ap);
3495 	return error;
3496 }
3497 
3498 /*
3499  * Write wrapper for fifos.
3500  */
3501 static int
3502 nfsfifo_write(struct vop_write_args *ap)
3503 {
3504 	struct nfsnode *np = VTONFS(ap->a_vp);
3505 
3506 	/*
3507 	 * Set update flag.
3508 	 */
3509 	NFSLOCKNODE(np);
3510 	np->n_flag |= NUPD;
3511 	vfs_timestamp(&np->n_mtim);
3512 	NFSUNLOCKNODE(np);
3513 	return(fifo_specops.vop_write(ap));
3514 }
3515 
3516 /*
3517  * Close wrapper for fifos.
3518  *
3519  * Update the times on the nfsnode then do fifo close.
3520  */
3521 static int
3522 nfsfifo_close(struct vop_close_args *ap)
3523 {
3524 	struct vnode *vp = ap->a_vp;
3525 	struct nfsnode *np = VTONFS(vp);
3526 	struct vattr vattr;
3527 	struct timespec ts;
3528 
3529 	NFSLOCKNODE(np);
3530 	if (np->n_flag & (NACC | NUPD)) {
3531 		vfs_timestamp(&ts);
3532 		if (np->n_flag & NACC)
3533 			np->n_atim = ts;
3534 		if (np->n_flag & NUPD)
3535 			np->n_mtim = ts;
3536 		np->n_flag |= NCHG;
3537 		if (vrefcnt(vp) == 1 &&
3538 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3539 			VATTR_NULL(&vattr);
3540 			if (np->n_flag & NACC)
3541 				vattr.va_atime = np->n_atim;
3542 			if (np->n_flag & NUPD)
3543 				vattr.va_mtime = np->n_mtim;
3544 			NFSUNLOCKNODE(np);
3545 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3546 			goto out;
3547 		}
3548 	}
3549 	NFSUNLOCKNODE(np);
3550 out:
3551 	return (fifo_specops.vop_close(ap));
3552 }
3553 
3554 static int
3555 nfs_getacl(struct vop_getacl_args *ap)
3556 {
3557 	int error;
3558 
3559 	if (ap->a_type != ACL_TYPE_NFS4)
3560 		return (EOPNOTSUPP);
3561 	error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp);
3562 	if (error > NFSERR_STALE) {
3563 		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3564 		error = EPERM;
3565 	}
3566 	return (error);
3567 }
3568 
3569 static int
3570 nfs_setacl(struct vop_setacl_args *ap)
3571 {
3572 	int error;
3573 
3574 	if (ap->a_type != ACL_TYPE_NFS4)
3575 		return (EOPNOTSUPP);
3576 	error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp);
3577 	if (error > NFSERR_STALE) {
3578 		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3579 		error = EPERM;
3580 	}
3581 	return (error);
3582 }
3583 
3584 /*
3585  * VOP_ADVISE for NFS.
3586  * Just return 0 for any errors, since it is just a hint.
3587  */
3588 static int
3589 nfs_advise(struct vop_advise_args *ap)
3590 {
3591 	struct thread *td = curthread;
3592 	struct nfsmount *nmp;
3593 	uint64_t len;
3594 	int error;
3595 
3596 	/*
3597 	 * First do vop_stdadvise() to handle the buffer cache.
3598 	 */
3599 	error = vop_stdadvise(ap);
3600 	if (error != 0)
3601 		return (error);
3602 	if (ap->a_start < 0 || ap->a_end < 0)
3603 		return (0);
3604 	if (ap->a_end == OFF_MAX)
3605 		len = 0;
3606 	else if (ap->a_end < ap->a_start)
3607 		return (0);
3608 	else
3609 		len = ap->a_end - ap->a_start + 1;
3610 	nmp = VFSTONFS(ap->a_vp->v_mount);
3611 	mtx_lock(&nmp->nm_mtx);
3612 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
3613 	    (NFSHASPNFS(nmp) && (nmp->nm_privflag & NFSMNTP_IOADVISETHRUMDS) ==
3614 	    0) || (nmp->nm_privflag & NFSMNTP_NOADVISE) != 0) {
3615 		mtx_unlock(&nmp->nm_mtx);
3616 		return (0);
3617 	}
3618 	mtx_unlock(&nmp->nm_mtx);
3619 	error = nfsrpc_advise(ap->a_vp, ap->a_start, len, ap->a_advice,
3620 	    td->td_ucred, td);
3621 	if (error == NFSERR_NOTSUPP) {
3622 		mtx_lock(&nmp->nm_mtx);
3623 		nmp->nm_privflag |= NFSMNTP_NOADVISE;
3624 		mtx_unlock(&nmp->nm_mtx);
3625 	}
3626 	return (0);
3627 }
3628 
3629 /*
3630  * nfs allocate call
3631  */
3632 static int
3633 nfs_allocate(struct vop_allocate_args *ap)
3634 {
3635 	struct vnode *vp = ap->a_vp;
3636 	struct thread *td = curthread;
3637 	struct nfsvattr nfsva;
3638 	struct nfsmount *nmp;
3639 	struct nfsnode *np;
3640 	off_t alen;
3641 	int attrflag, error, ret;
3642 	struct timespec ts;
3643 	struct uio io;
3644 
3645 	attrflag = 0;
3646 	nmp = VFSTONFS(vp->v_mount);
3647 	np = VTONFS(vp);
3648 	mtx_lock(&nmp->nm_mtx);
3649 	if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3650 	    (nmp->nm_privflag & NFSMNTP_NOALLOCATE) == 0) {
3651 		mtx_unlock(&nmp->nm_mtx);
3652 		alen = *ap->a_len;
3653 		if ((uint64_t)alen > nfs_maxalloclen)
3654 			alen = nfs_maxalloclen;
3655 
3656 		/* Check the file size limit. */
3657 		io.uio_offset = *ap->a_offset;
3658 		io.uio_resid = alen;
3659 		error = vn_rlimit_fsize(vp, &io, td);
3660 
3661 		/*
3662 		 * Flush first to ensure that the allocate adds to the
3663 		 * file's allocation on the server.
3664 		 */
3665 		if (error == 0) {
3666 			vnode_pager_clean_sync(vp);
3667 			error = ncl_flush(vp, MNT_WAIT, td, 1, 0);
3668 		}
3669 		if (error == 0)
3670 			error = nfsrpc_allocate(vp, *ap->a_offset, alen,
3671 			    &nfsva, &attrflag, ap->a_cred, td);
3672 		if (error == 0) {
3673 			*ap->a_offset += alen;
3674 			*ap->a_len -= alen;
3675 			nanouptime(&ts);
3676 			NFSLOCKNODE(np);
3677 			np->n_localmodtime = ts;
3678 			NFSUNLOCKNODE(np);
3679 		} else if (error == NFSERR_NOTSUPP) {
3680 			mtx_lock(&nmp->nm_mtx);
3681 			nmp->nm_privflag |= NFSMNTP_NOALLOCATE;
3682 			mtx_unlock(&nmp->nm_mtx);
3683 			error = EINVAL;
3684 		}
3685 	} else {
3686 		mtx_unlock(&nmp->nm_mtx);
3687 		error = EINVAL;
3688 	}
3689 	if (attrflag != 0) {
3690 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
3691 		if (error == 0 && ret != 0)
3692 			error = ret;
3693 	}
3694 	if (error != 0)
3695 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3696 	return (error);
3697 }
3698 
3699 /*
3700  * nfs deallocate call
3701  */
3702 static int
3703 nfs_deallocate(struct vop_deallocate_args *ap)
3704 {
3705 	struct vnode *vp = ap->a_vp;
3706 	struct thread *td = curthread;
3707 	struct nfsvattr nfsva;
3708 	struct nfsmount *nmp;
3709 	struct nfsnode *np;
3710 	off_t tlen, mlen;
3711 	int attrflag, error, ret;
3712 	bool clipped;
3713 	struct timespec ts;
3714 
3715 	error = 0;
3716 	attrflag = 0;
3717 	nmp = VFSTONFS(vp->v_mount);
3718 	np = VTONFS(vp);
3719 	mtx_lock(&nmp->nm_mtx);
3720 	if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3721 	    (nmp->nm_privflag & NFSMNTP_NODEALLOCATE) == 0) {
3722 		mtx_unlock(&nmp->nm_mtx);
3723 		tlen = omin(OFF_MAX - *ap->a_offset, *ap->a_len);
3724 		NFSCL_DEBUG(4, "dealloc: off=%jd len=%jd maxfilesize=%ju\n",
3725 		    (intmax_t)*ap->a_offset, (intmax_t)tlen,
3726 		    (uintmax_t)nmp->nm_maxfilesize);
3727 		if ((uint64_t)*ap->a_offset >= nmp->nm_maxfilesize) {
3728 			/* Avoid EFBIG error return from the NFSv4.2 server. */
3729 			*ap->a_len = 0;
3730 			return (0);
3731 		}
3732 		clipped = false;
3733 		if ((uint64_t)*ap->a_offset + tlen > nmp->nm_maxfilesize)
3734 			tlen = nmp->nm_maxfilesize - *ap->a_offset;
3735 		if ((uint64_t)*ap->a_offset < np->n_size) {
3736 			/* Limit the len to nfs_maxalloclen before EOF. */
3737 			mlen = omin((off_t)np->n_size - *ap->a_offset, tlen);
3738 			if ((uint64_t)mlen > nfs_maxalloclen) {
3739 				NFSCL_DEBUG(4, "dealloc: tlen maxalloclen\n");
3740 				tlen = nfs_maxalloclen;
3741 				clipped = true;
3742 			}
3743 		}
3744 		if (error == 0)
3745 			error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
3746 		if (error == 0) {
3747 			vnode_pager_purge_range(vp, *ap->a_offset,
3748 			    *ap->a_offset + tlen);
3749 			error = nfsrpc_deallocate(vp, *ap->a_offset, tlen,
3750 			    &nfsva, &attrflag, ap->a_cred, td);
3751 			NFSCL_DEBUG(4, "dealloc: rpc=%d\n", error);
3752 		}
3753 		if (error == 0) {
3754 			NFSCL_DEBUG(4, "dealloc: attrflag=%d na_size=%ju\n",
3755 			    attrflag, (uintmax_t)nfsva.na_size);
3756 			nanouptime(&ts);
3757 			NFSLOCKNODE(np);
3758 			np->n_localmodtime = ts;
3759 			NFSUNLOCKNODE(np);
3760 			if (attrflag != 0) {
3761 				if ((uint64_t)*ap->a_offset < nfsva.na_size)
3762 					*ap->a_offset += omin((off_t)
3763 					    nfsva.na_size - *ap->a_offset,
3764 					    tlen);
3765 			}
3766 			if (clipped && tlen < *ap->a_len)
3767 				*ap->a_len -= tlen;
3768 			else
3769 				*ap->a_len = 0;
3770 		} else if (error == NFSERR_NOTSUPP) {
3771 			mtx_lock(&nmp->nm_mtx);
3772 			nmp->nm_privflag |= NFSMNTP_NODEALLOCATE;
3773 			mtx_unlock(&nmp->nm_mtx);
3774 		}
3775 	} else {
3776 		mtx_unlock(&nmp->nm_mtx);
3777 		error = EIO;
3778 	}
3779 	/*
3780 	 * If the NFS server cannot perform the Deallocate operation, just call
3781 	 * vop_stddeallocate() to perform it.
3782 	 */
3783 	if (error != 0 && error != NFSERR_FBIG && error != NFSERR_INVAL) {
3784 		error = vop_stddeallocate(ap);
3785 		NFSCL_DEBUG(4, "dealloc: stddeallocate=%d\n", error);
3786 	}
3787 	if (attrflag != 0) {
3788 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
3789 		if (error == 0 && ret != 0)
3790 			error = ret;
3791 	}
3792 	if (error != 0)
3793 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3794 	return (error);
3795 }
3796 
3797 /*
3798  * nfs copy_file_range call
3799  */
3800 static int
3801 nfs_copy_file_range(struct vop_copy_file_range_args *ap)
3802 {
3803 	struct vnode *invp = ap->a_invp;
3804 	struct vnode *outvp = ap->a_outvp;
3805 	struct mount *mp;
3806 	vm_object_t invp_obj;
3807 	struct nfsvattr innfsva, outnfsva;
3808 	struct vattr va, *vap;
3809 	struct uio io;
3810 	struct nfsmount *nmp;
3811 	size_t len, len2;
3812 	ssize_t r;
3813 	int error, inattrflag, outattrflag, ret, ret2, invp_lock;
3814 	off_t inoff, outoff;
3815 	bool consecutive, must_commit, tryoutcred;
3816 
3817 	/*
3818 	 * NFSv4.2 Copy is not permitted for infile == outfile.
3819 	 * TODO: copy_file_range() between multiple NFS mountpoints
3820 	 */
3821 	if (invp == outvp || invp->v_mount != outvp->v_mount) {
3822 generic_copy:
3823 		return (ENOSYS);
3824 	}
3825 
3826 	invp_lock = LK_SHARED;
3827 relock:
3828 
3829 	/* Lock both vnodes, avoiding risk of deadlock. */
3830 	do {
3831 		mp = NULL;
3832 		error = vn_start_write(outvp, &mp, V_WAIT);
3833 		if (error == 0) {
3834 			error = vn_lock(outvp, LK_EXCLUSIVE);
3835 			if (error == 0) {
3836 				error = vn_lock(invp, invp_lock | LK_NOWAIT);
3837 				if (error == 0)
3838 					break;
3839 				VOP_UNLOCK(outvp);
3840 				if (mp != NULL)
3841 					vn_finished_write(mp);
3842 				mp = NULL;
3843 				error = vn_lock(invp, invp_lock);
3844 				if (error == 0)
3845 					VOP_UNLOCK(invp);
3846 			}
3847 		}
3848 		if (mp != NULL)
3849 			vn_finished_write(mp);
3850 	} while (error == 0);
3851 	if (error != 0)
3852 		return (error);
3853 
3854 	/*
3855 	 * More reasons to avoid nfs copy: not NFSv4.2, or explicitly
3856 	 * disabled.
3857 	 */
3858 	nmp = VFSTONFS(invp->v_mount);
3859 	mtx_lock(&nmp->nm_mtx);
3860 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
3861 	    (nmp->nm_privflag & NFSMNTP_NOCOPY) != 0) {
3862 		mtx_unlock(&nmp->nm_mtx);
3863 		VOP_UNLOCK(invp);
3864 		VOP_UNLOCK(outvp);
3865 		if (mp != NULL)
3866 			vn_finished_write(mp);
3867 		goto generic_copy;
3868 	}
3869 	mtx_unlock(&nmp->nm_mtx);
3870 
3871 	/*
3872 	 * Do the vn_rlimit_fsize() check.  Should this be above the VOP layer?
3873 	 */
3874 	io.uio_offset = *ap->a_outoffp;
3875 	io.uio_resid = *ap->a_lenp;
3876 	error = vn_rlimit_fsizex(outvp, &io, 0, &r, ap->a_fsizetd);
3877 	*ap->a_lenp = io.uio_resid;
3878 	/*
3879 	 * No need to call vn_rlimit_fsizex_res before return, since the uio is
3880 	 * local.
3881 	 */
3882 
3883 	/*
3884 	 * Flush the input file so that the data is up to date before
3885 	 * the copy.  Flush writes for the output file so that they
3886 	 * do not overwrite the data copied to the output file by the Copy.
3887 	 * Set the commit argument for both flushes so that the data is on
3888 	 * stable storage before the Copy RPC.  This is done in case the
3889 	 * server reboots during the Copy and needs to be redone.
3890 	 */
3891 	if (error == 0) {
3892 		invp_obj = invp->v_object;
3893 		if (invp_obj != NULL && vm_object_mightbedirty(invp_obj)) {
3894 			if (invp_lock != LK_EXCLUSIVE) {
3895 				invp_lock = LK_EXCLUSIVE;
3896 				VOP_UNLOCK(invp);
3897 				VOP_UNLOCK(outvp);
3898 				if (mp != NULL)
3899 					vn_finished_write(mp);
3900 				goto relock;
3901 			}
3902 			vnode_pager_clean_sync(invp);
3903 		}
3904 		error = ncl_flush(invp, MNT_WAIT, curthread, 1, 0);
3905 	}
3906 	if (error == 0)
3907 		error = ncl_vinvalbuf(outvp, V_SAVE, curthread, 0);
3908 
3909 	/* Do the actual NFSv4.2 RPC. */
3910 	ret = ret2 = 0;
3911 	len = *ap->a_lenp;
3912 	mtx_lock(&nmp->nm_mtx);
3913 	if ((nmp->nm_privflag & NFSMNTP_NOCONSECUTIVE) == 0)
3914 		consecutive = true;
3915 	else
3916 		consecutive = false;
3917 	mtx_unlock(&nmp->nm_mtx);
3918 	inoff = *ap->a_inoffp;
3919 	outoff = *ap->a_outoffp;
3920 	tryoutcred = true;
3921 	must_commit = false;
3922 	if (error == 0) {
3923 		vap = &VTONFS(invp)->n_vattr.na_vattr;
3924 		error = VOP_GETATTR(invp, vap, ap->a_incred);
3925 		if (error == 0) {
3926 			/*
3927 			 * Clip "len" at va_size so that RFC compliant servers
3928 			 * will not reply NFSERR_INVAL.
3929 			 * Setting "len == 0" for the RPC would be preferred,
3930 			 * but some Linux servers do not support that.
3931 			 * If the len is being set to 0, do a Setattr RPC to
3932 			 * set the server's atime.  This behaviour was the
3933 			 * preferred one for the FreeBSD "collective".
3934 			 */
3935 			if (inoff >= vap->va_size) {
3936 				*ap->a_lenp = len = 0;
3937 				if ((nmp->nm_mountp->mnt_flag & MNT_NOATIME) ==
3938 				    0) {
3939 					VATTR_NULL(&va);
3940 					va.va_atime.tv_sec = 0;
3941 					va.va_atime.tv_nsec = 0;
3942 					va.va_vaflags = VA_UTIMES_NULL;
3943 					inattrflag = 0;
3944 					error = nfsrpc_setattr(invp, &va, NULL,
3945 					    ap->a_incred, curthread, &innfsva,
3946 					    &inattrflag);
3947 					if (inattrflag != 0)
3948 						ret = nfscl_loadattrcache(&invp,
3949 						    &innfsva, NULL, 0, 1);
3950 					if (error == 0 && ret != 0)
3951 						error = ret;
3952 				}
3953 			} else if (inoff + len > vap->va_size)
3954 				*ap->a_lenp = len = vap->va_size - inoff;
3955 		} else
3956 			error = 0;
3957 	}
3958 
3959 	/*
3960 	 * len will be set to 0 upon a successful Copy RPC.
3961 	 * As such, this only loops when the Copy RPC needs to be retried.
3962 	 */
3963 	while (len > 0 && error == 0) {
3964 		inattrflag = outattrflag = 0;
3965 		len2 = len;
3966 		if (tryoutcred)
3967 			error = nfsrpc_copy_file_range(invp, ap->a_inoffp,
3968 			    outvp, ap->a_outoffp, &len2, ap->a_flags,
3969 			    &inattrflag, &innfsva, &outattrflag, &outnfsva,
3970 			    ap->a_outcred, consecutive, &must_commit);
3971 		else
3972 			error = nfsrpc_copy_file_range(invp, ap->a_inoffp,
3973 			    outvp, ap->a_outoffp, &len2, ap->a_flags,
3974 			    &inattrflag, &innfsva, &outattrflag, &outnfsva,
3975 			    ap->a_incred, consecutive, &must_commit);
3976 		if (inattrflag != 0)
3977 			ret = nfscl_loadattrcache(&invp, &innfsva, NULL, 0, 1);
3978 		if (outattrflag != 0)
3979 			ret2 = nfscl_loadattrcache(&outvp, &outnfsva, NULL,
3980 			    1, 1);
3981 		if (error == 0) {
3982 			if (consecutive == false) {
3983 				if (len2 == len) {
3984 					mtx_lock(&nmp->nm_mtx);
3985 					nmp->nm_privflag |=
3986 					    NFSMNTP_NOCONSECUTIVE;
3987 					mtx_unlock(&nmp->nm_mtx);
3988 				} else
3989 					error = NFSERR_OFFLOADNOREQS;
3990 			}
3991 			*ap->a_lenp = len2;
3992 			len = 0;
3993 			if (len2 > 0 && must_commit && error == 0)
3994 				error = ncl_commit(outvp, outoff, *ap->a_lenp,
3995 				    ap->a_outcred, curthread);
3996 			if (error == 0 && ret != 0)
3997 				error = ret;
3998 			if (error == 0 && ret2 != 0)
3999 				error = ret2;
4000 		} else if (error == NFSERR_OFFLOADNOREQS && consecutive) {
4001 			/*
4002 			 * Try consecutive == false, which is ok only if all
4003 			 * bytes are copied.
4004 			 * If only some bytes were copied when consecutive
4005 			 * is false, there is no way to know which bytes
4006 			 * still need to be written.
4007 			 */
4008 			consecutive = false;
4009 			error = 0;
4010 		} else if (error == NFSERR_ACCES && tryoutcred) {
4011 			/* Try again with incred. */
4012 			tryoutcred = false;
4013 			error = 0;
4014 		}
4015 		if (error == NFSERR_STALEWRITEVERF) {
4016 			/*
4017 			 * Server rebooted, so do it all again.
4018 			 */
4019 			*ap->a_inoffp = inoff;
4020 			*ap->a_outoffp = outoff;
4021 			len = *ap->a_lenp;
4022 			must_commit = false;
4023 			error = 0;
4024 		}
4025 	}
4026 	VOP_UNLOCK(invp);
4027 	VOP_UNLOCK(outvp);
4028 	if (mp != NULL)
4029 		vn_finished_write(mp);
4030 	if (error == NFSERR_NOTSUPP || error == NFSERR_OFFLOADNOREQS ||
4031 	    error == NFSERR_ACCES) {
4032 		/*
4033 		 * Unlike the NFSv4.2 Copy, vn_generic_copy_file_range() can
4034 		 * use a_incred for the read and a_outcred for the write, so
4035 		 * try this for NFSERR_ACCES failures for the Copy.
4036 		 * For NFSERR_NOTSUPP and NFSERR_OFFLOADNOREQS, the Copy can
4037 		 * never succeed, so disable it.
4038 		 */
4039 		if (error != NFSERR_ACCES) {
4040 			/* Can never do Copy on this mount. */
4041 			mtx_lock(&nmp->nm_mtx);
4042 			nmp->nm_privflag |= NFSMNTP_NOCOPY;
4043 			mtx_unlock(&nmp->nm_mtx);
4044 		}
4045 		*ap->a_inoffp = inoff;
4046 		*ap->a_outoffp = outoff;
4047 		error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
4048 		    ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
4049 		    ap->a_incred, ap->a_outcred, ap->a_fsizetd);
4050 	} else if (error != 0)
4051 		*ap->a_lenp = 0;
4052 
4053 	if (error != 0)
4054 		error = nfscl_maperr(curthread, error, (uid_t)0, (gid_t)0);
4055 	return (error);
4056 }
4057 
4058 /*
4059  * nfs ioctl call
4060  */
4061 static int
4062 nfs_ioctl(struct vop_ioctl_args *ap)
4063 {
4064 	struct vnode *vp = ap->a_vp;
4065 	struct nfsvattr nfsva;
4066 	struct nfsmount *nmp;
4067 	int attrflag, content, error, ret;
4068 	bool eof = false;			/* shut up compiler. */
4069 
4070 	/* Do the actual NFSv4.2 RPC. */
4071 	switch (ap->a_command) {
4072 	case FIOSEEKDATA:
4073 		content = NFSV4CONTENT_DATA;
4074 		break;
4075 	case FIOSEEKHOLE:
4076 		content = NFSV4CONTENT_HOLE;
4077 		break;
4078 	default:
4079 		return (ENOTTY);
4080 	}
4081 
4082 	error = vn_lock(vp, LK_EXCLUSIVE);
4083 	if (error != 0)
4084 		return (EBADF);
4085 
4086 	if (vp->v_type != VREG) {
4087 		VOP_UNLOCK(vp);
4088 		return (ENOTTY);
4089 	}
4090 	nmp = VFSTONFS(vp->v_mount);
4091 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION) {
4092 		VOP_UNLOCK(vp);
4093 		error = vop_stdioctl(ap);
4094 		return (error);
4095 	}
4096 
4097 	attrflag = 0;
4098 	if (*((off_t *)ap->a_data) >= VTONFS(vp)->n_size)
4099 		error = ENXIO;
4100 	else {
4101 		/*
4102 		 * Flush all writes, so that the server is up to date.
4103 		 * Although a Commit is not required, the commit argument
4104 		 * is set so that, for a pNFS File/Flexible File Layout
4105 		 * server, the LayoutCommit will be done to ensure the file
4106 		 * size is up to date on the Metadata Server.
4107 		 */
4108 
4109 		vnode_pager_clean_sync(vp);
4110 		error = ncl_flush(vp, MNT_WAIT, ap->a_td, 1, 0);
4111 		if (error == 0)
4112 			error = nfsrpc_seek(vp, (off_t *)ap->a_data, &eof,
4113 			    content, ap->a_cred, &nfsva, &attrflag);
4114 		/* If at eof for FIOSEEKDATA, return ENXIO. */
4115 		if (eof && error == 0 && content == NFSV4CONTENT_DATA)
4116 			error = ENXIO;
4117 	}
4118 	if (attrflag != 0) {
4119 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4120 		if (error == 0 && ret != 0)
4121 			error = ret;
4122 	}
4123 	NFSVOPUNLOCK(vp);
4124 
4125 	if (error != 0)
4126 		error = ENXIO;
4127 	return (error);
4128 }
4129 
4130 /*
4131  * nfs getextattr call
4132  */
4133 static int
4134 nfs_getextattr(struct vop_getextattr_args *ap)
4135 {
4136 	struct vnode *vp = ap->a_vp;
4137 	struct nfsmount *nmp;
4138 	struct ucred *cred;
4139 	struct thread *td = ap->a_td;
4140 	struct nfsvattr nfsva;
4141 	ssize_t len;
4142 	int attrflag, error, ret;
4143 
4144 	nmp = VFSTONFS(vp->v_mount);
4145 	mtx_lock(&nmp->nm_mtx);
4146 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4147 	    (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4148 	    ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4149 		mtx_unlock(&nmp->nm_mtx);
4150 		return (EOPNOTSUPP);
4151 	}
4152 	mtx_unlock(&nmp->nm_mtx);
4153 
4154 	cred = ap->a_cred;
4155 	if (cred == NULL)
4156 		cred = td->td_ucred;
4157 	/* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4158 	attrflag = 0;
4159 	error = nfsrpc_getextattr(vp, ap->a_name, ap->a_uio, &len, &nfsva,
4160 	    &attrflag, cred, td);
4161 	if (attrflag != 0) {
4162 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4163 		if (error == 0 && ret != 0)
4164 			error = ret;
4165 	}
4166 	if (error == 0 && ap->a_size != NULL)
4167 		*ap->a_size = len;
4168 
4169 	switch (error) {
4170 	case NFSERR_NOTSUPP:
4171 	case NFSERR_OPILLEGAL:
4172 		mtx_lock(&nmp->nm_mtx);
4173 		nmp->nm_privflag |= NFSMNTP_NOXATTR;
4174 		mtx_unlock(&nmp->nm_mtx);
4175 		error = EOPNOTSUPP;
4176 		break;
4177 	case NFSERR_NOXATTR:
4178 	case NFSERR_XATTR2BIG:
4179 		error = ENOATTR;
4180 		break;
4181 	default:
4182 		error = nfscl_maperr(td, error, 0, 0);
4183 		break;
4184 	}
4185 	return (error);
4186 }
4187 
4188 /*
4189  * nfs setextattr call
4190  */
4191 static int
4192 nfs_setextattr(struct vop_setextattr_args *ap)
4193 {
4194 	struct vnode *vp = ap->a_vp;
4195 	struct nfsmount *nmp;
4196 	struct ucred *cred;
4197 	struct thread *td = ap->a_td;
4198 	struct nfsvattr nfsva;
4199 	int attrflag, error, ret;
4200 
4201 	nmp = VFSTONFS(vp->v_mount);
4202 	mtx_lock(&nmp->nm_mtx);
4203 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4204 	    (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4205 	    ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4206 		mtx_unlock(&nmp->nm_mtx);
4207 		return (EOPNOTSUPP);
4208 	}
4209 	mtx_unlock(&nmp->nm_mtx);
4210 
4211 	if (ap->a_uio->uio_resid < 0)
4212 		return (EINVAL);
4213 	cred = ap->a_cred;
4214 	if (cred == NULL)
4215 		cred = td->td_ucred;
4216 	/* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4217 	attrflag = 0;
4218 	error = nfsrpc_setextattr(vp, ap->a_name, ap->a_uio, &nfsva,
4219 	    &attrflag, cred, td);
4220 	if (attrflag != 0) {
4221 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4222 		if (error == 0 && ret != 0)
4223 			error = ret;
4224 	}
4225 
4226 	switch (error) {
4227 	case NFSERR_NOTSUPP:
4228 	case NFSERR_OPILLEGAL:
4229 		mtx_lock(&nmp->nm_mtx);
4230 		nmp->nm_privflag |= NFSMNTP_NOXATTR;
4231 		mtx_unlock(&nmp->nm_mtx);
4232 		error = EOPNOTSUPP;
4233 		break;
4234 	case NFSERR_NOXATTR:
4235 	case NFSERR_XATTR2BIG:
4236 		error = ENOATTR;
4237 		break;
4238 	default:
4239 		error = nfscl_maperr(td, error, 0, 0);
4240 		break;
4241 	}
4242 	return (error);
4243 }
4244 
4245 /*
4246  * nfs listextattr call
4247  */
4248 static int
4249 nfs_listextattr(struct vop_listextattr_args *ap)
4250 {
4251 	struct vnode *vp = ap->a_vp;
4252 	struct nfsmount *nmp;
4253 	struct ucred *cred;
4254 	struct thread *td = ap->a_td;
4255 	struct nfsvattr nfsva;
4256 	size_t len, len2;
4257 	uint64_t cookie;
4258 	int attrflag, error, ret;
4259 	bool eof;
4260 
4261 	nmp = VFSTONFS(vp->v_mount);
4262 	mtx_lock(&nmp->nm_mtx);
4263 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4264 	    (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4265 	    ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4266 		mtx_unlock(&nmp->nm_mtx);
4267 		return (EOPNOTSUPP);
4268 	}
4269 	mtx_unlock(&nmp->nm_mtx);
4270 
4271 	cred = ap->a_cred;
4272 	if (cred == NULL)
4273 		cred = td->td_ucred;
4274 
4275 	/* Loop around doing List Extended Attribute RPCs. */
4276 	eof = false;
4277 	cookie = 0;
4278 	len2 = 0;
4279 	error = 0;
4280 	while (!eof && error == 0) {
4281 		len = nmp->nm_rsize;
4282 		attrflag = 0;
4283 		error = nfsrpc_listextattr(vp, &cookie, ap->a_uio, &len, &eof,
4284 		    &nfsva, &attrflag, cred, td);
4285 		if (attrflag != 0) {
4286 			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4287 			if (error == 0 && ret != 0)
4288 				error = ret;
4289 		}
4290 		if (error == 0) {
4291 			len2 += len;
4292 			if (len2 > SSIZE_MAX)
4293 				error = ENOATTR;
4294 		}
4295 	}
4296 	if (error == 0 && ap->a_size != NULL)
4297 		*ap->a_size = len2;
4298 
4299 	switch (error) {
4300 	case NFSERR_NOTSUPP:
4301 	case NFSERR_OPILLEGAL:
4302 		mtx_lock(&nmp->nm_mtx);
4303 		nmp->nm_privflag |= NFSMNTP_NOXATTR;
4304 		mtx_unlock(&nmp->nm_mtx);
4305 		error = EOPNOTSUPP;
4306 		break;
4307 	case NFSERR_NOXATTR:
4308 	case NFSERR_XATTR2BIG:
4309 		error = ENOATTR;
4310 		break;
4311 	default:
4312 		error = nfscl_maperr(td, error, 0, 0);
4313 		break;
4314 	}
4315 	return (error);
4316 }
4317 
4318 /*
4319  * nfs setextattr call
4320  */
4321 static int
4322 nfs_deleteextattr(struct vop_deleteextattr_args *ap)
4323 {
4324 	struct vnode *vp = ap->a_vp;
4325 	struct nfsmount *nmp;
4326 	struct nfsvattr nfsva;
4327 	int attrflag, error, ret;
4328 
4329 	nmp = VFSTONFS(vp->v_mount);
4330 	mtx_lock(&nmp->nm_mtx);
4331 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4332 	    (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4333 	    ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4334 		mtx_unlock(&nmp->nm_mtx);
4335 		return (EOPNOTSUPP);
4336 	}
4337 	mtx_unlock(&nmp->nm_mtx);
4338 
4339 	/* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4340 	attrflag = 0;
4341 	error = nfsrpc_rmextattr(vp, ap->a_name, &nfsva, &attrflag, ap->a_cred,
4342 	    ap->a_td);
4343 	if (attrflag != 0) {
4344 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4345 		if (error == 0 && ret != 0)
4346 			error = ret;
4347 	}
4348 
4349 	switch (error) {
4350 	case NFSERR_NOTSUPP:
4351 	case NFSERR_OPILLEGAL:
4352 		mtx_lock(&nmp->nm_mtx);
4353 		nmp->nm_privflag |= NFSMNTP_NOXATTR;
4354 		mtx_unlock(&nmp->nm_mtx);
4355 		error = EOPNOTSUPP;
4356 		break;
4357 	case NFSERR_NOXATTR:
4358 	case NFSERR_XATTR2BIG:
4359 		error = ENOATTR;
4360 		break;
4361 	default:
4362 		error = nfscl_maperr(ap->a_td, error, 0, 0);
4363 		break;
4364 	}
4365 	return (error);
4366 }
4367 
4368 /*
4369  * Return POSIX pathconf information applicable to nfs filesystems.
4370  */
4371 static int
4372 nfs_pathconf(struct vop_pathconf_args *ap)
4373 {
4374 	struct nfsv3_pathconf pc;
4375 	struct nfsvattr nfsva;
4376 	struct vnode *vp = ap->a_vp;
4377 	struct nfsmount *nmp;
4378 	struct thread *td = curthread;
4379 	off_t off;
4380 	bool eof;
4381 	int attrflag, error;
4382 
4383 	if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
4384 	    ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
4385 	    ap->a_name == _PC_NO_TRUNC)) ||
4386 	    (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) {
4387 		/*
4388 		 * Since only the above 4 a_names are returned by the NFSv3
4389 		 * Pathconf RPC, there is no point in doing it for others.
4390 		 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
4391 		 * be used for _PC_NFS4_ACL as well.
4392 		 */
4393 		error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
4394 		    &attrflag);
4395 		if (attrflag != 0)
4396 			(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4397 		if (error != 0)
4398 			return (error);
4399 	} else {
4400 		/*
4401 		 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
4402 		 * just fake them.
4403 		 */
4404 		pc.pc_linkmax = NFS_LINK_MAX;
4405 		pc.pc_namemax = NFS_MAXNAMLEN;
4406 		pc.pc_notrunc = 1;
4407 		pc.pc_chownrestricted = 1;
4408 		pc.pc_caseinsensitive = 0;
4409 		pc.pc_casepreserving = 1;
4410 		error = 0;
4411 	}
4412 	switch (ap->a_name) {
4413 	case _PC_LINK_MAX:
4414 #ifdef _LP64
4415 		*ap->a_retval = pc.pc_linkmax;
4416 #else
4417 		*ap->a_retval = MIN(LONG_MAX, pc.pc_linkmax);
4418 #endif
4419 		break;
4420 	case _PC_NAME_MAX:
4421 		*ap->a_retval = pc.pc_namemax;
4422 		break;
4423 	case _PC_PIPE_BUF:
4424 		if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
4425 			*ap->a_retval = PIPE_BUF;
4426 		else
4427 			error = EINVAL;
4428 		break;
4429 	case _PC_CHOWN_RESTRICTED:
4430 		*ap->a_retval = pc.pc_chownrestricted;
4431 		break;
4432 	case _PC_NO_TRUNC:
4433 		*ap->a_retval = pc.pc_notrunc;
4434 		break;
4435 	case _PC_ACL_NFS4:
4436 		if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
4437 		    NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
4438 			*ap->a_retval = 1;
4439 		else
4440 			*ap->a_retval = 0;
4441 		break;
4442 	case _PC_ACL_PATH_MAX:
4443 		if (NFS_ISV4(vp))
4444 			*ap->a_retval = ACL_MAX_ENTRIES;
4445 		else
4446 			*ap->a_retval = 3;
4447 		break;
4448 	case _PC_PRIO_IO:
4449 		*ap->a_retval = 0;
4450 		break;
4451 	case _PC_SYNC_IO:
4452 		*ap->a_retval = 0;
4453 		break;
4454 	case _PC_ALLOC_SIZE_MIN:
4455 		*ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
4456 		break;
4457 	case _PC_FILESIZEBITS:
4458 		if (NFS_ISV34(vp))
4459 			*ap->a_retval = 64;
4460 		else
4461 			*ap->a_retval = 32;
4462 		break;
4463 	case _PC_REC_INCR_XFER_SIZE:
4464 		*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4465 		break;
4466 	case _PC_REC_MAX_XFER_SIZE:
4467 		*ap->a_retval = -1; /* means ``unlimited'' */
4468 		break;
4469 	case _PC_REC_MIN_XFER_SIZE:
4470 		*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4471 		break;
4472 	case _PC_REC_XFER_ALIGN:
4473 		*ap->a_retval = PAGE_SIZE;
4474 		break;
4475 	case _PC_SYMLINK_MAX:
4476 		*ap->a_retval = NFS_MAXPATHLEN;
4477 		break;
4478 	case _PC_MIN_HOLE_SIZE:
4479 		/* Only some NFSv4.2 servers support Seek for Holes. */
4480 		*ap->a_retval = 0;
4481 		nmp = VFSTONFS(vp->v_mount);
4482 		if (NFS_ISV4(vp) && nmp->nm_minorvers == NFSV42_MINORVERSION) {
4483 			/*
4484 			 * NFSv4.2 doesn't have an attribute for hole size,
4485 			 * so all we can do is see if the Seek operation is
4486 			 * supported and then use f_iosize as a "best guess".
4487 			 */
4488 			mtx_lock(&nmp->nm_mtx);
4489 			if ((nmp->nm_privflag & NFSMNTP_SEEKTESTED) == 0) {
4490 				mtx_unlock(&nmp->nm_mtx);
4491 				off = 0;
4492 				attrflag = 0;
4493 				error = nfsrpc_seek(vp, &off, &eof,
4494 				    NFSV4CONTENT_HOLE, td->td_ucred, &nfsva,
4495 				    &attrflag);
4496 				if (attrflag != 0)
4497 					(void) nfscl_loadattrcache(&vp, &nfsva,
4498 					    NULL, 0, 1);
4499 				mtx_lock(&nmp->nm_mtx);
4500 				if (error == NFSERR_NOTSUPP)
4501 					nmp->nm_privflag |= NFSMNTP_SEEKTESTED;
4502 				else
4503 					nmp->nm_privflag |= NFSMNTP_SEEKTESTED |
4504 					    NFSMNTP_SEEK;
4505 				error = 0;
4506 			}
4507 			if ((nmp->nm_privflag & NFSMNTP_SEEK) != 0)
4508 				*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4509 			mtx_unlock(&nmp->nm_mtx);
4510 		}
4511 		break;
4512 
4513 	default:
4514 		error = vop_stdpathconf(ap);
4515 		break;
4516 	}
4517 	return (error);
4518 }
4519