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