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