xref: /freebsd/sys/fs/nfsclient/nfs_clvnops.c (revision 5bb3134a8c21cb87b30e135ef168483f0333dabb)
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, NULL);
411 	if (attrflag)
412 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, 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 			    NULL);
953 			if (!ret) {
954 				np->n_change = nfsva.na_filerev;
955 				(void) nfscl_loadattrcache(&vp, &nfsva, NULL,
956 				    NULL, 0, 0);
957 			}
958 		}
959 
960 		/*
961 		 * and do the close.
962 		 */
963 		ret = nfsrpc_close(vp, 0, ap->a_td);
964 		if (!error && ret)
965 			error = ret;
966 		if (error)
967 			error = nfscl_maperr(ap->a_td, error, (uid_t)0,
968 			    (gid_t)0);
969 	}
970 	if (newnfs_directio_enable)
971 		KASSERT((np->n_directio_asyncwr == 0),
972 			("nfs_close: dirty unflushed (%d) directio buffers\n",
973 			 np->n_directio_asyncwr));
974 	if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
975 		NFSLOCKNODE(np);
976 		KASSERT((np->n_directio_opens > 0),
977 			("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
978 		np->n_directio_opens--;
979 		if (np->n_directio_opens == 0)
980 			np->n_flag &= ~NNONCACHE;
981 		NFSUNLOCKNODE(np);
982 	}
983 	if (localcred)
984 		NFSFREECRED(cred);
985 	return (error);
986 }
987 
988 /*
989  * nfs getattr call from vfs.
990  */
991 static int
992 nfs_getattr(struct vop_getattr_args *ap)
993 {
994 	struct vnode *vp = ap->a_vp;
995 	struct thread *td = curthread;	/* XXX */
996 	struct nfsnode *np = VTONFS(vp);
997 	int error = 0;
998 	struct nfsvattr nfsva;
999 	struct vattr *vap = ap->a_vap;
1000 	struct vattr vattr;
1001 
1002 	/*
1003 	 * Update local times for special files.
1004 	 */
1005 	NFSLOCKNODE(np);
1006 	if (np->n_flag & (NACC | NUPD))
1007 		np->n_flag |= NCHG;
1008 	NFSUNLOCKNODE(np);
1009 	/*
1010 	 * First look in the cache.
1011 	 */
1012 	if (ncl_getattrcache(vp, &vattr) == 0) {
1013 		ncl_copy_vattr(vap, &vattr);
1014 
1015 		/*
1016 		 * Get the local modify time for the case of a write
1017 		 * delegation.
1018 		 */
1019 		nfscl_deleggetmodtime(vp, &vap->va_mtime);
1020 		return (0);
1021 	}
1022 
1023 	if (NFS_ISV34(vp) && nfs_prime_access_cache &&
1024 	    nfsaccess_cache_timeout > 0) {
1025 		NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
1026 		nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
1027 		if (ncl_getattrcache(vp, ap->a_vap) == 0) {
1028 			nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
1029 			return (0);
1030 		}
1031 	}
1032 	error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva, NULL);
1033 	if (!error)
1034 		error = nfscl_loadattrcache(&vp, &nfsva, vap, NULL, 0, 0);
1035 	if (!error) {
1036 		/*
1037 		 * Get the local modify time for the case of a write
1038 		 * delegation.
1039 		 */
1040 		nfscl_deleggetmodtime(vp, &vap->va_mtime);
1041 	} else if (NFS_ISV4(vp)) {
1042 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1043 	}
1044 	return (error);
1045 }
1046 
1047 /*
1048  * nfs setattr call.
1049  */
1050 static int
1051 nfs_setattr(struct vop_setattr_args *ap)
1052 {
1053 	struct vnode *vp = ap->a_vp;
1054 	struct nfsnode *np = VTONFS(vp);
1055 	struct thread *td = curthread;	/* XXX */
1056 	struct vattr *vap = ap->a_vap;
1057 	int error = 0;
1058 	u_quad_t tsize;
1059 	struct timespec ts;
1060 
1061 #ifndef nolint
1062 	tsize = (u_quad_t)0;
1063 #endif
1064 
1065 	/*
1066 	 * Setting of flags and marking of atimes are not supported.
1067 	 */
1068 	if (vap->va_flags != VNOVAL)
1069 		return (EOPNOTSUPP);
1070 
1071 	/*
1072 	 * Disallow write attempts if the filesystem is mounted read-only.
1073 	 */
1074   	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
1075 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
1076 	    vap->va_mtime.tv_sec != VNOVAL ||
1077 	    vap->va_birthtime.tv_sec != VNOVAL ||
1078 	    vap->va_mode != (mode_t)VNOVAL) &&
1079 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
1080 		return (EROFS);
1081 	if (vap->va_size != VNOVAL) {
1082  		switch (vp->v_type) {
1083  		case VDIR:
1084  			return (EISDIR);
1085  		case VCHR:
1086  		case VBLK:
1087  		case VSOCK:
1088  		case VFIFO:
1089 			if (vap->va_mtime.tv_sec == VNOVAL &&
1090 			    vap->va_atime.tv_sec == VNOVAL &&
1091 			    vap->va_birthtime.tv_sec == VNOVAL &&
1092 			    vap->va_mode == (mode_t)VNOVAL &&
1093 			    vap->va_uid == (uid_t)VNOVAL &&
1094 			    vap->va_gid == (gid_t)VNOVAL)
1095 				return (0);
1096  			vap->va_size = VNOVAL;
1097  			break;
1098  		default:
1099 			/*
1100 			 * Disallow write attempts if the filesystem is
1101 			 * mounted read-only.
1102 			 */
1103 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
1104 				return (EROFS);
1105 			/*
1106 			 *  We run vnode_pager_setsize() early (why?),
1107 			 * we must set np->n_size now to avoid vinvalbuf
1108 			 * V_SAVE races that might setsize a lower
1109 			 * value.
1110 			 */
1111 			NFSLOCKNODE(np);
1112 			tsize = np->n_size;
1113 			NFSUNLOCKNODE(np);
1114 			error = ncl_meta_setsize(vp, td, vap->va_size);
1115 			NFSLOCKNODE(np);
1116  			if (np->n_flag & NMODIFIED) {
1117 			    tsize = np->n_size;
1118 			    NFSUNLOCKNODE(np);
1119 			    error = ncl_vinvalbuf(vp, vap->va_size == 0 ?
1120 			        0 : V_SAVE, td, 1);
1121 			    if (error != 0) {
1122 				    vnode_pager_setsize(vp, tsize);
1123 				    return (error);
1124 			    }
1125 			    /*
1126 			     * Call nfscl_delegmodtime() to set the modify time
1127 			     * locally, as required.
1128 			     */
1129 			    nfscl_delegmodtime(vp);
1130  			} else
1131 			    NFSUNLOCKNODE(np);
1132 			/*
1133 			 * np->n_size has already been set to vap->va_size
1134 			 * in ncl_meta_setsize(). We must set it again since
1135 			 * nfs_loadattrcache() could be called through
1136 			 * ncl_meta_setsize() and could modify np->n_size.
1137 			 */
1138 			NFSLOCKNODE(np);
1139  			np->n_vattr.na_size = np->n_size = vap->va_size;
1140 			NFSUNLOCKNODE(np);
1141   		}
1142   	} else {
1143 		NFSLOCKNODE(np);
1144 		if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
1145 		    (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
1146 			NFSUNLOCKNODE(np);
1147 			error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
1148 			if (error == EINTR || error == EIO)
1149 				return (error);
1150 		} else
1151 			NFSUNLOCKNODE(np);
1152 	}
1153 	error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
1154 	if (vap->va_size != VNOVAL) {
1155 		if (error == 0) {
1156 			nanouptime(&ts);
1157 			NFSLOCKNODE(np);
1158 			np->n_localmodtime = ts;
1159 			NFSUNLOCKNODE(np);
1160 		} else {
1161 			NFSLOCKNODE(np);
1162 			np->n_size = np->n_vattr.na_size = tsize;
1163 			vnode_pager_setsize(vp, tsize);
1164 			NFSUNLOCKNODE(np);
1165 		}
1166 	}
1167 	return (error);
1168 }
1169 
1170 /*
1171  * Do an nfs setattr rpc.
1172  */
1173 static int
1174 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
1175     struct thread *td)
1176 {
1177 	struct nfsnode *np = VTONFS(vp);
1178 	int error, ret, attrflag, i;
1179 	struct nfsvattr nfsva;
1180 
1181 	if (NFS_ISV34(vp)) {
1182 		NFSLOCKNODE(np);
1183 		for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
1184 			np->n_accesscache[i].stamp = 0;
1185 		np->n_flag |= NDELEGMOD;
1186 		NFSUNLOCKNODE(np);
1187 		KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1188 	}
1189 	error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag,
1190 	    NULL);
1191 	if (attrflag) {
1192 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1193 		if (ret && !error)
1194 			error = ret;
1195 	}
1196 	if (error && NFS_ISV4(vp))
1197 		error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1198 	return (error);
1199 }
1200 
1201 /*
1202  * nfs lookup call, one step at a time...
1203  * First look in cache
1204  * If not found, unlock the directory nfsnode and do the rpc
1205  */
1206 static int
1207 nfs_lookup(struct vop_lookup_args *ap)
1208 {
1209 	struct componentname *cnp = ap->a_cnp;
1210 	struct vnode *dvp = ap->a_dvp;
1211 	struct vnode **vpp = ap->a_vpp;
1212 	struct mount *mp = dvp->v_mount;
1213 	int flags = cnp->cn_flags;
1214 	struct vnode *newvp;
1215 	struct nfsmount *nmp;
1216 	struct nfsnode *np, *newnp;
1217 	int error = 0, attrflag, dattrflag, ltype, ncticks;
1218 	struct thread *td = curthread;
1219 	struct nfsfh *nfhp;
1220 	struct nfsvattr dnfsva, nfsva;
1221 	struct vattr vattr;
1222 	struct timespec nctime, ts;
1223 	uint32_t openmode;
1224 
1225 	*vpp = NULLVP;
1226 	if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1227 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1228 		return (EROFS);
1229 	if (dvp->v_type != VDIR)
1230 		return (ENOTDIR);
1231 	nmp = VFSTONFS(mp);
1232 	np = VTONFS(dvp);
1233 
1234 	/* For NFSv4, wait until any remove is done. */
1235 	NFSLOCKNODE(np);
1236 	while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1237 		np->n_flag |= NREMOVEWANT;
1238 		(void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1239 	}
1240 	NFSUNLOCKNODE(np);
1241 
1242 	error = vn_dir_check_exec(dvp, cnp);
1243 	if (error != 0)
1244 		return (error);
1245 	error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1246 	if (error > 0 && error != ENOENT)
1247 		return (error);
1248 	if (error == -1) {
1249 		/*
1250 		 * Lookups of "." are special and always return the
1251 		 * current directory.  cache_lookup() already handles
1252 		 * associated locking bookkeeping, etc.
1253 		 */
1254 		if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1255 			/* XXX: Is this really correct? */
1256 			if (cnp->cn_nameiop != LOOKUP &&
1257 			    (flags & ISLASTCN))
1258 				cnp->cn_flags |= SAVENAME;
1259 			return (0);
1260 		}
1261 
1262 		/*
1263 		 * We only accept a positive hit in the cache if the
1264 		 * change time of the file matches our cached copy.
1265 		 * Otherwise, we discard the cache entry and fallback
1266 		 * to doing a lookup RPC.  We also only trust cache
1267 		 * entries for less than nm_nametimeo seconds.
1268 		 *
1269 		 * To better handle stale file handles and attributes,
1270 		 * clear the attribute cache of this node if it is a
1271 		 * leaf component, part of an open() call, and not
1272 		 * locally modified before fetching the attributes.
1273 		 * This should allow stale file handles to be detected
1274 		 * here where we can fall back to a LOOKUP RPC to
1275 		 * recover rather than having nfs_open() detect the
1276 		 * stale file handle and failing open(2) with ESTALE.
1277 		 */
1278 		newvp = *vpp;
1279 		newnp = VTONFS(newvp);
1280 		if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1281 		    (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1282 		    !(newnp->n_flag & NMODIFIED)) {
1283 			NFSLOCKNODE(newnp);
1284 			newnp->n_attrstamp = 0;
1285 			KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1286 			NFSUNLOCKNODE(newnp);
1287 		}
1288 		if (nfscl_nodeleg(newvp, 0) == 0 ||
1289 		    ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
1290 		    VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1291 		    timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1292 			NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1293 			if (cnp->cn_nameiop != LOOKUP &&
1294 			    (flags & ISLASTCN))
1295 				cnp->cn_flags |= SAVENAME;
1296 			return (0);
1297 		}
1298 		cache_purge(newvp);
1299 		if (dvp != newvp)
1300 			vput(newvp);
1301 		else
1302 			vrele(newvp);
1303 		*vpp = NULLVP;
1304 	} else if (error == ENOENT) {
1305 		if (VN_IS_DOOMED(dvp))
1306 			return (ENOENT);
1307 		/*
1308 		 * We only accept a negative hit in the cache if the
1309 		 * modification time of the parent directory matches
1310 		 * the cached copy in the name cache entry.
1311 		 * Otherwise, we discard all of the negative cache
1312 		 * entries for this directory.  We also only trust
1313 		 * negative cache entries for up to nm_negnametimeo
1314 		 * seconds.
1315 		 */
1316 		if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
1317 		    VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1318 		    timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1319 			NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1320 			return (ENOENT);
1321 		}
1322 		cache_purge_negative(dvp);
1323 	}
1324 
1325 	/*
1326 	 * If this an NFSv4.1/4.2 mount using the "oneopenown" mount
1327 	 * option, it is possible to do the Open operation in the same
1328 	 * compound as Lookup, so long as delegations are not being
1329 	 * issued.  This saves doing a separate RPC for Open.
1330 	 */
1331 	openmode = 0;
1332 	NFSLOCKMNT(nmp);
1333 	if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp) &&
1334 	    (nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0 &&
1335 	    (!NFSMNT_RDONLY(mp) || (flags & OPENWRITE) == 0) &&
1336 	    (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN)) {
1337 		if ((flags & OPENREAD) != 0)
1338 			openmode |= NFSV4OPEN_ACCESSREAD;
1339 		if ((flags & OPENWRITE) != 0)
1340 			openmode |= NFSV4OPEN_ACCESSWRITE;
1341 	}
1342 	NFSUNLOCKMNT(nmp);
1343 
1344 	newvp = NULLVP;
1345 	NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1346 	nanouptime(&ts);
1347 	error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1348 	    cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1349 	    NULL, openmode);
1350 	if (dattrflag)
1351 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1352 	if (error) {
1353 		if (newvp != NULLVP) {
1354 			vput(newvp);
1355 			*vpp = NULLVP;
1356 		}
1357 
1358 		if (error != ENOENT) {
1359 			if (NFS_ISV4(dvp))
1360 				error = nfscl_maperr(td, error, (uid_t)0,
1361 				    (gid_t)0);
1362 			return (error);
1363 		}
1364 
1365 		/* The requested file was not found. */
1366 		if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1367 		    (flags & ISLASTCN)) {
1368 			/*
1369 			 * XXX: UFS does a full VOP_ACCESS(dvp,
1370 			 * VWRITE) here instead of just checking
1371 			 * MNT_RDONLY.
1372 			 */
1373 			if (mp->mnt_flag & MNT_RDONLY)
1374 				return (EROFS);
1375 			cnp->cn_flags |= SAVENAME;
1376 			return (EJUSTRETURN);
1377 		}
1378 
1379 		if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1380 			/*
1381 			 * Cache the modification time of the parent
1382 			 * directory from the post-op attributes in
1383 			 * the name cache entry.  The negative cache
1384 			 * entry will be ignored once the directory
1385 			 * has changed.  Don't bother adding the entry
1386 			 * if the directory has already changed.
1387 			 */
1388 			NFSLOCKNODE(np);
1389 			if (timespeccmp(&np->n_vattr.na_mtime,
1390 			    &dnfsva.na_mtime, ==)) {
1391 				NFSUNLOCKNODE(np);
1392 				cache_enter_time(dvp, NULL, cnp,
1393 				    &dnfsva.na_mtime, NULL);
1394 			} else
1395 				NFSUNLOCKNODE(np);
1396 		}
1397 		return (ENOENT);
1398 	}
1399 
1400 	/*
1401 	 * Handle RENAME case...
1402 	 */
1403 	if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1404 		if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1405 			free(nfhp, M_NFSFH);
1406 			return (EISDIR);
1407 		}
1408 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1409 		    LK_EXCLUSIVE);
1410 		if (error)
1411 			return (error);
1412 		newvp = NFSTOV(np);
1413 		/*
1414 		 * If n_localmodtime >= time before RPC, then
1415 		 * a file modification operation, such as
1416 		 * VOP_SETATTR() of size, has occurred while
1417 		 * the Lookup RPC and acquisition of the vnode
1418 		 * happened.  As such, the attributes might
1419 		 * be stale, with possibly an incorrect size.
1420 		 */
1421 		NFSLOCKNODE(np);
1422 		if (timespecisset(&np->n_localmodtime) &&
1423 		    timespeccmp(&np->n_localmodtime, &ts, >=)) {
1424 			NFSCL_DEBUG(4, "nfs_lookup: rename localmod "
1425 			    "stale attributes\n");
1426 			attrflag = 0;
1427 		}
1428 		NFSUNLOCKNODE(np);
1429 		if (attrflag)
1430 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1431 			    0, 1);
1432 		*vpp = newvp;
1433 		cnp->cn_flags |= SAVENAME;
1434 		return (0);
1435 	}
1436 
1437 	if (flags & ISDOTDOT) {
1438 		ltype = NFSVOPISLOCKED(dvp);
1439 		error = vfs_busy(mp, MBF_NOWAIT);
1440 		if (error != 0) {
1441 			vfs_ref(mp);
1442 			NFSVOPUNLOCK(dvp);
1443 			error = vfs_busy(mp, 0);
1444 			NFSVOPLOCK(dvp, ltype | LK_RETRY);
1445 			vfs_rel(mp);
1446 			if (error == 0 && VN_IS_DOOMED(dvp)) {
1447 				vfs_unbusy(mp);
1448 				error = ENOENT;
1449 			}
1450 			if (error != 0)
1451 				return (error);
1452 		}
1453 		NFSVOPUNLOCK(dvp);
1454 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1455 		    cnp->cn_lkflags);
1456 		if (error == 0)
1457 			newvp = NFSTOV(np);
1458 		vfs_unbusy(mp);
1459 		if (newvp != dvp)
1460 			NFSVOPLOCK(dvp, ltype | LK_RETRY);
1461 		if (VN_IS_DOOMED(dvp)) {
1462 			if (error == 0) {
1463 				if (newvp == dvp)
1464 					vrele(newvp);
1465 				else
1466 					vput(newvp);
1467 			}
1468 			error = ENOENT;
1469 		}
1470 		if (error != 0)
1471 			return (error);
1472 		if (attrflag)
1473 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1474 			    0, 1);
1475 	} else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1476 		free(nfhp, M_NFSFH);
1477 		VREF(dvp);
1478 		newvp = dvp;
1479 		if (attrflag)
1480 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1481 			    0, 1);
1482 	} else {
1483 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1484 		    cnp->cn_lkflags);
1485 		if (error)
1486 			return (error);
1487 		newvp = NFSTOV(np);
1488 		/*
1489 		 * If n_localmodtime >= time before RPC, then
1490 		 * a file modification operation, such as
1491 		 * VOP_SETATTR() of size, has occurred while
1492 		 * the Lookup RPC and acquisition of the vnode
1493 		 * happened.  As such, the attributes might
1494 		 * be stale, with possibly an incorrect size.
1495 		 */
1496 		NFSLOCKNODE(np);
1497 		if (timespecisset(&np->n_localmodtime) &&
1498 		    timespeccmp(&np->n_localmodtime, &ts, >=)) {
1499 			NFSCL_DEBUG(4, "nfs_lookup: localmod "
1500 			    "stale attributes\n");
1501 			attrflag = 0;
1502 		}
1503 		NFSUNLOCKNODE(np);
1504 		if (attrflag)
1505 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1506 			    0, 1);
1507 		else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1508 		    !(np->n_flag & NMODIFIED)) {
1509 			/*
1510 			 * Flush the attribute cache when opening a
1511 			 * leaf node to ensure that fresh attributes
1512 			 * are fetched in nfs_open() since we did not
1513 			 * fetch attributes from the LOOKUP reply.
1514 			 */
1515 			NFSLOCKNODE(np);
1516 			np->n_attrstamp = 0;
1517 			KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1518 			NFSUNLOCKNODE(np);
1519 		}
1520 	}
1521 	if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1522 		cnp->cn_flags |= SAVENAME;
1523 	if ((cnp->cn_flags & MAKEENTRY) && dvp != newvp &&
1524 	    (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1525 	    attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1526 		cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1527 		    newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1528 	*vpp = newvp;
1529 	return (0);
1530 }
1531 
1532 /*
1533  * nfs read call.
1534  * Just call ncl_bioread() to do the work.
1535  */
1536 static int
1537 nfs_read(struct vop_read_args *ap)
1538 {
1539 	struct vnode *vp = ap->a_vp;
1540 
1541 	switch (vp->v_type) {
1542 	case VREG:
1543 		return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1544 	case VDIR:
1545 		return (EISDIR);
1546 	default:
1547 		return (EOPNOTSUPP);
1548 	}
1549 }
1550 
1551 /*
1552  * nfs readlink call
1553  */
1554 static int
1555 nfs_readlink(struct vop_readlink_args *ap)
1556 {
1557 	struct vnode *vp = ap->a_vp;
1558 
1559 	if (vp->v_type != VLNK)
1560 		return (EINVAL);
1561 	return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1562 }
1563 
1564 /*
1565  * Do a readlink rpc.
1566  * Called by ncl_doio() from below the buffer cache.
1567  */
1568 int
1569 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1570 {
1571 	int error, ret, attrflag;
1572 	struct nfsvattr nfsva;
1573 
1574 	error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1575 	    &attrflag, NULL);
1576 	if (attrflag) {
1577 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1578 		if (ret && !error)
1579 			error = ret;
1580 	}
1581 	if (error && NFS_ISV4(vp))
1582 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1583 	return (error);
1584 }
1585 
1586 /*
1587  * nfs read rpc call
1588  * Ditto above
1589  */
1590 int
1591 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1592 {
1593 	int error, ret, attrflag;
1594 	struct nfsvattr nfsva;
1595 	struct nfsmount *nmp;
1596 
1597 	nmp = VFSTONFS(vp->v_mount);
1598 	error = EIO;
1599 	attrflag = 0;
1600 	if (NFSHASPNFS(nmp))
1601 		error = nfscl_doiods(vp, uiop, NULL, NULL,
1602 		    NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td);
1603 	NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1604 	if (error != 0)
1605 		error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1606 		    &attrflag, NULL);
1607 	if (attrflag) {
1608 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1609 		if (ret && !error)
1610 			error = ret;
1611 	}
1612 	if (error && NFS_ISV4(vp))
1613 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1614 	return (error);
1615 }
1616 
1617 /*
1618  * nfs write call
1619  */
1620 int
1621 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1622     int *iomode, int *must_commit, int called_from_strategy)
1623 {
1624 	struct nfsvattr nfsva;
1625 	int error, attrflag, ret;
1626 	struct nfsmount *nmp;
1627 
1628 	nmp = VFSTONFS(vp->v_mount);
1629 	error = EIO;
1630 	attrflag = 0;
1631 	if (NFSHASPNFS(nmp))
1632 		error = nfscl_doiods(vp, uiop, iomode, must_commit,
1633 		    NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td);
1634 	NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1635 	if (error != 0)
1636 		error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1637 		    uiop->uio_td, &nfsva, &attrflag, NULL,
1638 		    called_from_strategy);
1639 	if (attrflag) {
1640 		if (VTONFS(vp)->n_flag & ND_NFSV4)
1641 			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
1642 			    1);
1643 		else
1644 			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
1645 			    1);
1646 		if (ret && !error)
1647 			error = ret;
1648 	}
1649 	if (DOINGASYNC(vp))
1650 		*iomode = NFSWRITE_FILESYNC;
1651 	if (error && NFS_ISV4(vp))
1652 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1653 	return (error);
1654 }
1655 
1656 /*
1657  * nfs mknod rpc
1658  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1659  * mode set to specify the file type and the size field for rdev.
1660  */
1661 static int
1662 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1663     struct vattr *vap)
1664 {
1665 	struct nfsvattr nfsva, dnfsva;
1666 	struct vnode *newvp = NULL;
1667 	struct nfsnode *np = NULL, *dnp;
1668 	struct nfsfh *nfhp;
1669 	struct vattr vattr;
1670 	int error = 0, attrflag, dattrflag;
1671 	u_int32_t rdev;
1672 
1673 	if (vap->va_type == VCHR || vap->va_type == VBLK)
1674 		rdev = vap->va_rdev;
1675 	else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1676 		rdev = 0xffffffff;
1677 	else
1678 		return (EOPNOTSUPP);
1679 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1680 		return (error);
1681 	error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1682 	    rdev, vap->va_type, cnp->cn_cred, curthread, &dnfsva,
1683 	    &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1684 	if (!error) {
1685 		if (!nfhp)
1686 			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1687 			    cnp->cn_namelen, cnp->cn_cred, curthread,
1688 			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1689 			    NULL, 0);
1690 		if (nfhp)
1691 			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1692 			    curthread, &np, NULL, LK_EXCLUSIVE);
1693 	}
1694 	if (dattrflag)
1695 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1696 	if (!error) {
1697 		newvp = NFSTOV(np);
1698 		if (attrflag != 0) {
1699 			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1700 			    0, 1);
1701 			if (error != 0)
1702 				vput(newvp);
1703 		}
1704 	}
1705 	if (!error) {
1706 		*vpp = newvp;
1707 	} else if (NFS_ISV4(dvp)) {
1708 		error = nfscl_maperr(curthread, error, vap->va_uid,
1709 		    vap->va_gid);
1710 	}
1711 	dnp = VTONFS(dvp);
1712 	NFSLOCKNODE(dnp);
1713 	dnp->n_flag |= NMODIFIED;
1714 	if (!dattrflag) {
1715 		dnp->n_attrstamp = 0;
1716 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1717 	}
1718 	NFSUNLOCKNODE(dnp);
1719 	return (error);
1720 }
1721 
1722 /*
1723  * nfs mknod vop
1724  * just call nfs_mknodrpc() to do the work.
1725  */
1726 /* ARGSUSED */
1727 static int
1728 nfs_mknod(struct vop_mknod_args *ap)
1729 {
1730 	return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1731 }
1732 
1733 static struct mtx nfs_cverf_mtx;
1734 MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1735     MTX_DEF);
1736 
1737 static nfsquad_t
1738 nfs_get_cverf(void)
1739 {
1740 	static nfsquad_t cverf;
1741 	nfsquad_t ret;
1742 	static int cverf_initialized = 0;
1743 
1744 	mtx_lock(&nfs_cverf_mtx);
1745 	if (cverf_initialized == 0) {
1746 		cverf.lval[0] = arc4random();
1747 		cverf.lval[1] = arc4random();
1748 		cverf_initialized = 1;
1749 	} else
1750 		cverf.qval++;
1751 	ret = cverf;
1752 	mtx_unlock(&nfs_cverf_mtx);
1753 
1754 	return (ret);
1755 }
1756 
1757 /*
1758  * nfs file create call
1759  */
1760 static int
1761 nfs_create(struct vop_create_args *ap)
1762 {
1763 	struct vnode *dvp = ap->a_dvp;
1764 	struct vattr *vap = ap->a_vap;
1765 	struct componentname *cnp = ap->a_cnp;
1766 	struct nfsnode *np = NULL, *dnp;
1767 	struct vnode *newvp = NULL;
1768 	struct nfsmount *nmp;
1769 	struct nfsvattr dnfsva, nfsva;
1770 	struct nfsfh *nfhp;
1771 	nfsquad_t cverf;
1772 	int error = 0, attrflag, dattrflag, fmode = 0;
1773 	struct vattr vattr;
1774 
1775 	/*
1776 	 * Oops, not for me..
1777 	 */
1778 	if (vap->va_type == VSOCK)
1779 		return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1780 
1781 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1782 		return (error);
1783 	if (vap->va_vaflags & VA_EXCLUSIVE)
1784 		fmode |= O_EXCL;
1785 	dnp = VTONFS(dvp);
1786 	nmp = VFSTONFS(dvp->v_mount);
1787 again:
1788 	/* For NFSv4, wait until any remove is done. */
1789 	NFSLOCKNODE(dnp);
1790 	while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1791 		dnp->n_flag |= NREMOVEWANT;
1792 		(void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1793 	}
1794 	NFSUNLOCKNODE(dnp);
1795 
1796 	cverf = nfs_get_cverf();
1797 	error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1798 	    vap, cverf, fmode, cnp->cn_cred, curthread, &dnfsva, &nfsva,
1799 	    &nfhp, &attrflag, &dattrflag, NULL);
1800 	if (!error) {
1801 		if (nfhp == NULL)
1802 			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1803 			    cnp->cn_namelen, cnp->cn_cred, curthread,
1804 			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1805 			    NULL, 0);
1806 		if (nfhp != NULL)
1807 			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1808 			    curthread, &np, NULL, LK_EXCLUSIVE);
1809 	}
1810 	if (dattrflag)
1811 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1812 	if (!error) {
1813 		newvp = NFSTOV(np);
1814 		if (attrflag == 0)
1815 			error = nfsrpc_getattr(newvp, cnp->cn_cred,
1816 			    curthread, &nfsva, NULL);
1817 		if (error == 0)
1818 			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1819 			    0, 1);
1820 	}
1821 	if (error) {
1822 		if (newvp != NULL) {
1823 			vput(newvp);
1824 			newvp = NULL;
1825 		}
1826 		if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1827 		    error == NFSERR_NOTSUPP) {
1828 			fmode &= ~O_EXCL;
1829 			goto again;
1830 		}
1831 	} else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1832 		if (nfscl_checksattr(vap, &nfsva)) {
1833 			error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1834 			    curthread, &nfsva, &attrflag, NULL);
1835 			if (error && (vap->va_uid != (uid_t)VNOVAL ||
1836 			    vap->va_gid != (gid_t)VNOVAL)) {
1837 				/* try again without setting uid/gid */
1838 				vap->va_uid = (uid_t)VNOVAL;
1839 				vap->va_gid = (uid_t)VNOVAL;
1840 				error = nfsrpc_setattr(newvp, vap, NULL,
1841 				    cnp->cn_cred, curthread, &nfsva,
1842 				    &attrflag, NULL);
1843 			}
1844 			if (attrflag)
1845 				(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1846 				    NULL, 0, 1);
1847 			if (error != 0)
1848 				vput(newvp);
1849 		}
1850 	}
1851 	if (!error) {
1852 		if ((cnp->cn_flags & MAKEENTRY) && attrflag) {
1853 			if (dvp != newvp)
1854 				cache_enter_time(dvp, newvp, cnp,
1855 				    &nfsva.na_ctime, NULL);
1856 			else
1857 				printf("nfs_create: bogus NFS server returned "
1858 				    "the directory as the new file object\n");
1859 		}
1860 		*ap->a_vpp = newvp;
1861 	} else if (NFS_ISV4(dvp)) {
1862 		error = nfscl_maperr(curthread, error, vap->va_uid,
1863 		    vap->va_gid);
1864 	}
1865 	NFSLOCKNODE(dnp);
1866 	dnp->n_flag |= NMODIFIED;
1867 	if (!dattrflag) {
1868 		dnp->n_attrstamp = 0;
1869 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1870 	}
1871 	NFSUNLOCKNODE(dnp);
1872 	return (error);
1873 }
1874 
1875 /*
1876  * nfs file remove call
1877  * To try and make nfs semantics closer to ufs semantics, a file that has
1878  * other processes using the vnode is renamed instead of removed and then
1879  * removed later on the last close.
1880  * - If v_usecount > 1
1881  *	  If a rename is not already in the works
1882  *	     call nfs_sillyrename() to set it up
1883  *     else
1884  *	  do the remove rpc
1885  */
1886 static int
1887 nfs_remove(struct vop_remove_args *ap)
1888 {
1889 	struct vnode *vp = ap->a_vp;
1890 	struct vnode *dvp = ap->a_dvp;
1891 	struct componentname *cnp = ap->a_cnp;
1892 	struct nfsnode *np = VTONFS(vp);
1893 	int error = 0;
1894 	struct vattr vattr;
1895 
1896 	KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1897 	KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1898 	if (vp->v_type == VDIR)
1899 		error = EPERM;
1900 	else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1901 	    VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1902 	    vattr.va_nlink > 1)) {
1903 		/*
1904 		 * Purge the name cache so that the chance of a lookup for
1905 		 * the name succeeding while the remove is in progress is
1906 		 * minimized. Without node locking it can still happen, such
1907 		 * that an I/O op returns ESTALE, but since you get this if
1908 		 * another host removes the file..
1909 		 */
1910 		cache_purge(vp);
1911 		/*
1912 		 * throw away biocache buffers, mainly to avoid
1913 		 * unnecessary delayed writes later.
1914 		 */
1915 		error = ncl_vinvalbuf(vp, 0, curthread, 1);
1916 		if (error != EINTR && error != EIO)
1917 			/* Do the rpc */
1918 			error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1919 			    cnp->cn_namelen, cnp->cn_cred, curthread);
1920 		/*
1921 		 * Kludge City: If the first reply to the remove rpc is lost..
1922 		 *   the reply to the retransmitted request will be ENOENT
1923 		 *   since the file was in fact removed
1924 		 *   Therefore, we cheat and return success.
1925 		 */
1926 		if (error == ENOENT)
1927 			error = 0;
1928 	} else if (!np->n_sillyrename)
1929 		error = nfs_sillyrename(dvp, vp, cnp);
1930 	NFSLOCKNODE(np);
1931 	np->n_attrstamp = 0;
1932 	NFSUNLOCKNODE(np);
1933 	KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1934 	return (error);
1935 }
1936 
1937 /*
1938  * nfs file remove rpc called from nfs_inactive
1939  */
1940 int
1941 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1942 {
1943 	/*
1944 	 * Make sure that the directory vnode is still valid.
1945 	 * XXX we should lock sp->s_dvp here.
1946 	 */
1947 	if (sp->s_dvp->v_type == VBAD)
1948 		return (0);
1949 	return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1950 	    sp->s_cred, NULL));
1951 }
1952 
1953 /*
1954  * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1955  */
1956 static int
1957 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1958     int namelen, struct ucred *cred, struct thread *td)
1959 {
1960 	struct nfsvattr dnfsva;
1961 	struct nfsnode *dnp = VTONFS(dvp);
1962 	int error = 0, dattrflag;
1963 
1964 	NFSLOCKNODE(dnp);
1965 	dnp->n_flag |= NREMOVEINPROG;
1966 	NFSUNLOCKNODE(dnp);
1967 	error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1968 	    &dattrflag, NULL);
1969 	NFSLOCKNODE(dnp);
1970 	if ((dnp->n_flag & NREMOVEWANT)) {
1971 		dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1972 		NFSUNLOCKNODE(dnp);
1973 		wakeup((caddr_t)dnp);
1974 	} else {
1975 		dnp->n_flag &= ~NREMOVEINPROG;
1976 		NFSUNLOCKNODE(dnp);
1977 	}
1978 	if (dattrflag)
1979 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1980 	NFSLOCKNODE(dnp);
1981 	dnp->n_flag |= NMODIFIED;
1982 	if (!dattrflag) {
1983 		dnp->n_attrstamp = 0;
1984 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1985 	}
1986 	NFSUNLOCKNODE(dnp);
1987 	if (error && NFS_ISV4(dvp))
1988 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1989 	return (error);
1990 }
1991 
1992 /*
1993  * nfs file rename call
1994  */
1995 static int
1996 nfs_rename(struct vop_rename_args *ap)
1997 {
1998 	struct vnode *fvp = ap->a_fvp;
1999 	struct vnode *tvp = ap->a_tvp;
2000 	struct vnode *fdvp = ap->a_fdvp;
2001 	struct vnode *tdvp = ap->a_tdvp;
2002 	struct componentname *tcnp = ap->a_tcnp;
2003 	struct componentname *fcnp = ap->a_fcnp;
2004 	struct nfsnode *fnp = VTONFS(ap->a_fvp);
2005 	struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
2006 	struct nfsv4node *newv4 = NULL;
2007 	int error;
2008 
2009 	KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
2010 	    (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
2011 	/* Check for cross-device rename */
2012 	if ((fvp->v_mount != tdvp->v_mount) ||
2013 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
2014 		error = EXDEV;
2015 		goto out;
2016 	}
2017 
2018 	if (fvp == tvp) {
2019 		printf("nfs_rename: fvp == tvp (can't happen)\n");
2020 		error = 0;
2021 		goto out;
2022 	}
2023 	if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
2024 		goto out;
2025 
2026 	/*
2027 	 * We have to flush B_DELWRI data prior to renaming
2028 	 * the file.  If we don't, the delayed-write buffers
2029 	 * can be flushed out later after the file has gone stale
2030 	 * under NFSV3.  NFSV2 does not have this problem because
2031 	 * ( as far as I can tell ) it flushes dirty buffers more
2032 	 * often.
2033 	 *
2034 	 * Skip the rename operation if the fsync fails, this can happen
2035 	 * due to the server's volume being full, when we pushed out data
2036 	 * that was written back to our cache earlier. Not checking for
2037 	 * this condition can result in potential (silent) data loss.
2038 	 */
2039 	error = VOP_FSYNC(fvp, MNT_WAIT, curthread);
2040 	NFSVOPUNLOCK(fvp);
2041 	if (!error && tvp)
2042 		error = VOP_FSYNC(tvp, MNT_WAIT, curthread);
2043 	if (error)
2044 		goto out;
2045 
2046 	/*
2047 	 * If the tvp exists and is in use, sillyrename it before doing the
2048 	 * rename of the new file over it.
2049 	 * XXX Can't sillyrename a directory.
2050 	 */
2051 	if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
2052 		tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
2053 		vput(tvp);
2054 		tvp = NULL;
2055 	}
2056 
2057 	error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2058 	    tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
2059 	    curthread);
2060 
2061 	if (error == 0 && NFS_ISV4(tdvp)) {
2062 		/*
2063 		 * For NFSv4, check to see if it is the same name and
2064 		 * replace the name, if it is different.
2065 		 */
2066 		newv4 = malloc(
2067 		    sizeof (struct nfsv4node) +
2068 		    tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
2069 		    M_NFSV4NODE, M_WAITOK);
2070 		NFSLOCKNODE(tdnp);
2071 		NFSLOCKNODE(fnp);
2072 		if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
2073 		    (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
2074 		      NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
2075 		      tcnp->cn_namelen) ||
2076 		      tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
2077 		      NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2078 			tdnp->n_fhp->nfh_len))) {
2079 #ifdef notdef
2080 { char nnn[100]; int nnnl;
2081 nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
2082 bcopy(tcnp->cn_nameptr, nnn, nnnl);
2083 nnn[nnnl] = '\0';
2084 printf("ren replace=%s\n",nnn);
2085 }
2086 #endif
2087 			free(fnp->n_v4, M_NFSV4NODE);
2088 			fnp->n_v4 = newv4;
2089 			newv4 = NULL;
2090 			fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
2091 			fnp->n_v4->n4_namelen = tcnp->cn_namelen;
2092 			NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2093 			    tdnp->n_fhp->nfh_len);
2094 			NFSBCOPY(tcnp->cn_nameptr,
2095 			    NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
2096 		}
2097 		NFSUNLOCKNODE(tdnp);
2098 		NFSUNLOCKNODE(fnp);
2099 		if (newv4 != NULL)
2100 			free(newv4, M_NFSV4NODE);
2101 	}
2102 
2103 	if (fvp->v_type == VDIR) {
2104 		if (tvp != NULL && tvp->v_type == VDIR)
2105 			cache_purge(tdvp);
2106 		cache_purge(fdvp);
2107 	}
2108 
2109 out:
2110 	if (tdvp == tvp)
2111 		vrele(tdvp);
2112 	else
2113 		vput(tdvp);
2114 	if (tvp)
2115 		vput(tvp);
2116 	vrele(fdvp);
2117 	vrele(fvp);
2118 	/*
2119 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2120 	 */
2121 	if (error == ENOENT)
2122 		error = 0;
2123 	return (error);
2124 }
2125 
2126 /*
2127  * nfs file rename rpc called from nfs_remove() above
2128  */
2129 static int
2130 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
2131     struct sillyrename *sp)
2132 {
2133 
2134 	return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
2135 	    sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
2136 	    curthread));
2137 }
2138 
2139 /*
2140  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2141  */
2142 static int
2143 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
2144     int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
2145     int tnamelen, struct ucred *cred, struct thread *td)
2146 {
2147 	struct nfsvattr fnfsva, tnfsva;
2148 	struct nfsnode *fdnp = VTONFS(fdvp);
2149 	struct nfsnode *tdnp = VTONFS(tdvp);
2150 	int error = 0, fattrflag, tattrflag;
2151 
2152 	error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
2153 	    tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
2154 	    &tattrflag, NULL, NULL);
2155 	NFSLOCKNODE(fdnp);
2156 	fdnp->n_flag |= NMODIFIED;
2157 	if (fattrflag != 0) {
2158 		NFSUNLOCKNODE(fdnp);
2159 		(void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
2160 	} else {
2161 		fdnp->n_attrstamp = 0;
2162 		NFSUNLOCKNODE(fdnp);
2163 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
2164 	}
2165 	NFSLOCKNODE(tdnp);
2166 	tdnp->n_flag |= NMODIFIED;
2167 	if (tattrflag != 0) {
2168 		NFSUNLOCKNODE(tdnp);
2169 		(void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
2170 	} else {
2171 		tdnp->n_attrstamp = 0;
2172 		NFSUNLOCKNODE(tdnp);
2173 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2174 	}
2175 	if (error && NFS_ISV4(fdvp))
2176 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2177 	return (error);
2178 }
2179 
2180 /*
2181  * nfs hard link create call
2182  */
2183 static int
2184 nfs_link(struct vop_link_args *ap)
2185 {
2186 	struct vnode *vp = ap->a_vp;
2187 	struct vnode *tdvp = ap->a_tdvp;
2188 	struct componentname *cnp = ap->a_cnp;
2189 	struct nfsnode *np, *tdnp;
2190 	struct nfsvattr nfsva, dnfsva;
2191 	int error = 0, attrflag, dattrflag;
2192 
2193 	/*
2194 	 * Push all writes to the server, so that the attribute cache
2195 	 * doesn't get "out of sync" with the server.
2196 	 * XXX There should be a better way!
2197 	 */
2198 	VOP_FSYNC(vp, MNT_WAIT, curthread);
2199 
2200 	error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
2201 	    cnp->cn_cred, curthread, &dnfsva, &nfsva, &attrflag,
2202 	    &dattrflag, NULL);
2203 	tdnp = VTONFS(tdvp);
2204 	NFSLOCKNODE(tdnp);
2205 	tdnp->n_flag |= NMODIFIED;
2206 	if (dattrflag != 0) {
2207 		NFSUNLOCKNODE(tdnp);
2208 		(void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
2209 	} else {
2210 		tdnp->n_attrstamp = 0;
2211 		NFSUNLOCKNODE(tdnp);
2212 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2213 	}
2214 	if (attrflag)
2215 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2216 	else {
2217 		np = VTONFS(vp);
2218 		NFSLOCKNODE(np);
2219 		np->n_attrstamp = 0;
2220 		NFSUNLOCKNODE(np);
2221 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2222 	}
2223 	/*
2224 	 * If negative lookup caching is enabled, I might as well
2225 	 * add an entry for this node. Not necessary for correctness,
2226 	 * but if negative caching is enabled, then the system
2227 	 * must care about lookup caching hit rate, so...
2228 	 */
2229 	if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
2230 	    (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2231 		if (tdvp != vp)
2232 			cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
2233 		else
2234 			printf("nfs_link: bogus NFS server returned "
2235 			    "the directory as the new link\n");
2236 	}
2237 	if (error && NFS_ISV4(vp))
2238 		error = nfscl_maperr(curthread, error, (uid_t)0,
2239 		    (gid_t)0);
2240 	return (error);
2241 }
2242 
2243 /*
2244  * nfs symbolic link create call
2245  */
2246 static int
2247 nfs_symlink(struct vop_symlink_args *ap)
2248 {
2249 	struct vnode *dvp = ap->a_dvp;
2250 	struct vattr *vap = ap->a_vap;
2251 	struct componentname *cnp = ap->a_cnp;
2252 	struct nfsvattr nfsva, dnfsva;
2253 	struct nfsfh *nfhp;
2254 	struct nfsnode *np = NULL, *dnp;
2255 	struct vnode *newvp = NULL;
2256 	int error = 0, attrflag, dattrflag, ret;
2257 
2258 	vap->va_type = VLNK;
2259 	error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2260 	    ap->a_target, vap, cnp->cn_cred, curthread, &dnfsva,
2261 	    &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
2262 	if (nfhp) {
2263 		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2264 		    &np, NULL, LK_EXCLUSIVE);
2265 		if (!ret)
2266 			newvp = NFSTOV(np);
2267 		else if (!error)
2268 			error = ret;
2269 	}
2270 	if (newvp != NULL) {
2271 		if (attrflag)
2272 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2273 			    0, 1);
2274 	} else if (!error) {
2275 		/*
2276 		 * If we do not have an error and we could not extract the
2277 		 * newvp from the response due to the request being NFSv2, we
2278 		 * have to do a lookup in order to obtain a newvp to return.
2279 		 */
2280 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2281 		    cnp->cn_cred, curthread, &np);
2282 		if (!error)
2283 			newvp = NFSTOV(np);
2284 	}
2285 	if (error) {
2286 		if (newvp)
2287 			vput(newvp);
2288 		if (NFS_ISV4(dvp))
2289 			error = nfscl_maperr(curthread, error,
2290 			    vap->va_uid, vap->va_gid);
2291 	} else {
2292 		*ap->a_vpp = newvp;
2293 	}
2294 
2295 	dnp = VTONFS(dvp);
2296 	NFSLOCKNODE(dnp);
2297 	dnp->n_flag |= NMODIFIED;
2298 	if (dattrflag != 0) {
2299 		NFSUNLOCKNODE(dnp);
2300 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2301 	} else {
2302 		dnp->n_attrstamp = 0;
2303 		NFSUNLOCKNODE(dnp);
2304 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2305 	}
2306 	/*
2307 	 * If negative lookup caching is enabled, I might as well
2308 	 * add an entry for this node. Not necessary for correctness,
2309 	 * but if negative caching is enabled, then the system
2310 	 * must care about lookup caching hit rate, so...
2311 	 */
2312 	if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2313 	    (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2314 		if (dvp != newvp)
2315 			cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2316 			    NULL);
2317 		else
2318 			printf("nfs_symlink: bogus NFS server returned "
2319 			    "the directory as the new file object\n");
2320 	}
2321 	return (error);
2322 }
2323 
2324 /*
2325  * nfs make dir call
2326  */
2327 static int
2328 nfs_mkdir(struct vop_mkdir_args *ap)
2329 {
2330 	struct vnode *dvp = ap->a_dvp;
2331 	struct vattr *vap = ap->a_vap;
2332 	struct componentname *cnp = ap->a_cnp;
2333 	struct nfsnode *np = NULL, *dnp;
2334 	struct vnode *newvp = NULL;
2335 	struct vattr vattr;
2336 	struct nfsfh *nfhp;
2337 	struct nfsvattr nfsva, dnfsva;
2338 	int error = 0, attrflag, dattrflag, ret;
2339 
2340 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2341 		return (error);
2342 	vap->va_type = VDIR;
2343 	error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2344 	    vap, cnp->cn_cred, curthread, &dnfsva, &nfsva, &nfhp,
2345 	    &attrflag, &dattrflag, NULL);
2346 	dnp = VTONFS(dvp);
2347 	NFSLOCKNODE(dnp);
2348 	dnp->n_flag |= NMODIFIED;
2349 	if (dattrflag != 0) {
2350 		NFSUNLOCKNODE(dnp);
2351 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2352 	} else {
2353 		dnp->n_attrstamp = 0;
2354 		NFSUNLOCKNODE(dnp);
2355 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2356 	}
2357 	if (nfhp) {
2358 		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2359 		    &np, NULL, LK_EXCLUSIVE);
2360 		if (!ret) {
2361 			newvp = NFSTOV(np);
2362 			if (attrflag)
2363 			   (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2364 				NULL, 0, 1);
2365 		} else if (!error)
2366 			error = ret;
2367 	}
2368 	if (!error && newvp == NULL) {
2369 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2370 		    cnp->cn_cred, curthread, &np);
2371 		if (!error) {
2372 			newvp = NFSTOV(np);
2373 			if (newvp->v_type != VDIR)
2374 				error = EEXIST;
2375 		}
2376 	}
2377 	if (error) {
2378 		if (newvp)
2379 			vput(newvp);
2380 		if (NFS_ISV4(dvp))
2381 			error = nfscl_maperr(curthread, error,
2382 			    vap->va_uid, vap->va_gid);
2383 	} else {
2384 		/*
2385 		 * If negative lookup caching is enabled, I might as well
2386 		 * add an entry for this node. Not necessary for correctness,
2387 		 * but if negative caching is enabled, then the system
2388 		 * must care about lookup caching hit rate, so...
2389 		 */
2390 		if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2391 		    (cnp->cn_flags & MAKEENTRY) &&
2392 		    attrflag != 0 && dattrflag != 0) {
2393 			if (dvp != newvp)
2394 				cache_enter_time(dvp, newvp, cnp,
2395 				    &nfsva.na_ctime, &dnfsva.na_ctime);
2396 			else
2397 				printf("nfs_mkdir: bogus NFS server returned "
2398 				    "the directory that the directory was "
2399 				    "created in as the new file object\n");
2400 		}
2401 		*ap->a_vpp = newvp;
2402 	}
2403 	return (error);
2404 }
2405 
2406 /*
2407  * nfs remove directory call
2408  */
2409 static int
2410 nfs_rmdir(struct vop_rmdir_args *ap)
2411 {
2412 	struct vnode *vp = ap->a_vp;
2413 	struct vnode *dvp = ap->a_dvp;
2414 	struct componentname *cnp = ap->a_cnp;
2415 	struct nfsnode *dnp;
2416 	struct nfsvattr dnfsva;
2417 	int error, dattrflag;
2418 
2419 	if (dvp == vp)
2420 		return (EINVAL);
2421 	error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2422 	    cnp->cn_cred, curthread, &dnfsva, &dattrflag, NULL);
2423 	dnp = VTONFS(dvp);
2424 	NFSLOCKNODE(dnp);
2425 	dnp->n_flag |= NMODIFIED;
2426 	if (dattrflag != 0) {
2427 		NFSUNLOCKNODE(dnp);
2428 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2429 	} else {
2430 		dnp->n_attrstamp = 0;
2431 		NFSUNLOCKNODE(dnp);
2432 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2433 	}
2434 
2435 	cache_purge(dvp);
2436 	cache_purge(vp);
2437 	if (error && NFS_ISV4(dvp))
2438 		error = nfscl_maperr(curthread, error, (uid_t)0,
2439 		    (gid_t)0);
2440 	/*
2441 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2442 	 */
2443 	if (error == ENOENT)
2444 		error = 0;
2445 	return (error);
2446 }
2447 
2448 /*
2449  * nfs readdir call
2450  */
2451 static int
2452 nfs_readdir(struct vop_readdir_args *ap)
2453 {
2454 	struct vnode *vp = ap->a_vp;
2455 	struct nfsnode *np = VTONFS(vp);
2456 	struct uio *uio = ap->a_uio;
2457 	ssize_t tresid, left;
2458 	int error = 0;
2459 	struct vattr vattr;
2460 
2461 	if (ap->a_eofflag != NULL)
2462 		*ap->a_eofflag = 0;
2463 	if (vp->v_type != VDIR)
2464 		return(EPERM);
2465 
2466 	/*
2467 	 * First, check for hit on the EOF offset cache
2468 	 */
2469 	NFSLOCKNODE(np);
2470 	if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2471 	    (np->n_flag & NMODIFIED) == 0) {
2472 		NFSUNLOCKNODE(np);
2473 		if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2474 			NFSLOCKNODE(np);
2475 			if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2476 			    !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2477 				NFSUNLOCKNODE(np);
2478 				NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2479 				if (ap->a_eofflag != NULL)
2480 					*ap->a_eofflag = 1;
2481 				return (0);
2482 			} else
2483 				NFSUNLOCKNODE(np);
2484 		}
2485 	} else
2486 		NFSUNLOCKNODE(np);
2487 
2488 	/*
2489 	 * NFS always guarantees that directory entries don't straddle
2490 	 * DIRBLKSIZ boundaries.  As such, we need to limit the size
2491 	 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2492 	 * directory entry.
2493 	 */
2494 	left = uio->uio_resid % DIRBLKSIZ;
2495 	if (left == uio->uio_resid)
2496 		return (EINVAL);
2497 	uio->uio_resid -= left;
2498 
2499 	/*
2500 	 * Call ncl_bioread() to do the real work.
2501 	 */
2502 	tresid = uio->uio_resid;
2503 	error = ncl_bioread(vp, uio, 0, ap->a_cred);
2504 
2505 	if (!error && uio->uio_resid == tresid) {
2506 		NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2507 		if (ap->a_eofflag != NULL)
2508 			*ap->a_eofflag = 1;
2509 	}
2510 
2511 	/* Add the partial DIRBLKSIZ (left) back in. */
2512 	uio->uio_resid += left;
2513 	return (error);
2514 }
2515 
2516 /*
2517  * Readdir rpc call.
2518  * Called from below the buffer cache by ncl_doio().
2519  */
2520 int
2521 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2522     struct thread *td)
2523 {
2524 	struct nfsvattr nfsva;
2525 	nfsuint64 *cookiep, cookie;
2526 	struct nfsnode *dnp = VTONFS(vp);
2527 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2528 	int error = 0, eof, attrflag;
2529 
2530 	KASSERT(uiop->uio_iovcnt == 1 &&
2531 	    (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2532 	    (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2533 	    ("nfs readdirrpc bad uio"));
2534 
2535 	/*
2536 	 * If there is no cookie, assume directory was stale.
2537 	 */
2538 	ncl_dircookie_lock(dnp);
2539 	NFSUNLOCKNODE(dnp);
2540 	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2541 	if (cookiep) {
2542 		cookie = *cookiep;
2543 		ncl_dircookie_unlock(dnp);
2544 	} else {
2545 		ncl_dircookie_unlock(dnp);
2546 		return (NFSERR_BAD_COOKIE);
2547 	}
2548 
2549 	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2550 		(void)ncl_fsinfo(nmp, vp, cred, td);
2551 
2552 	error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2553 	    &attrflag, &eof, NULL);
2554 	if (attrflag)
2555 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2556 
2557 	if (!error) {
2558 		/*
2559 		 * We are now either at the end of the directory or have filled
2560 		 * the block.
2561 		 */
2562 		if (eof) {
2563 			NFSLOCKNODE(dnp);
2564 			dnp->n_direofoffset = uiop->uio_offset;
2565 			NFSUNLOCKNODE(dnp);
2566 		} else {
2567 			if (uiop->uio_resid > 0)
2568 				printf("EEK! readdirrpc resid > 0\n");
2569 			ncl_dircookie_lock(dnp);
2570 			NFSUNLOCKNODE(dnp);
2571 			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2572 			*cookiep = cookie;
2573 			ncl_dircookie_unlock(dnp);
2574 		}
2575 	} else if (NFS_ISV4(vp)) {
2576 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2577 	}
2578 	return (error);
2579 }
2580 
2581 /*
2582  * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2583  */
2584 int
2585 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2586     struct thread *td)
2587 {
2588 	struct nfsvattr nfsva;
2589 	nfsuint64 *cookiep, cookie;
2590 	struct nfsnode *dnp = VTONFS(vp);
2591 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2592 	int error = 0, attrflag, eof;
2593 
2594 	KASSERT(uiop->uio_iovcnt == 1 &&
2595 	    (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2596 	    (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2597 	    ("nfs readdirplusrpc bad uio"));
2598 
2599 	/*
2600 	 * If there is no cookie, assume directory was stale.
2601 	 */
2602 	ncl_dircookie_lock(dnp);
2603 	NFSUNLOCKNODE(dnp);
2604 	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2605 	if (cookiep) {
2606 		cookie = *cookiep;
2607 		ncl_dircookie_unlock(dnp);
2608 	} else {
2609 		ncl_dircookie_unlock(dnp);
2610 		return (NFSERR_BAD_COOKIE);
2611 	}
2612 
2613 	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2614 		(void)ncl_fsinfo(nmp, vp, cred, td);
2615 	error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2616 	    &attrflag, &eof, NULL);
2617 	if (attrflag)
2618 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2619 
2620 	if (!error) {
2621 		/*
2622 		 * We are now either at end of the directory or have filled the
2623 		 * the block.
2624 		 */
2625 		if (eof) {
2626 			NFSLOCKNODE(dnp);
2627 			dnp->n_direofoffset = uiop->uio_offset;
2628 			NFSUNLOCKNODE(dnp);
2629 		} else {
2630 			if (uiop->uio_resid > 0)
2631 				printf("EEK! readdirplusrpc resid > 0\n");
2632 			ncl_dircookie_lock(dnp);
2633 			NFSUNLOCKNODE(dnp);
2634 			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2635 			*cookiep = cookie;
2636 			ncl_dircookie_unlock(dnp);
2637 		}
2638 	} else if (NFS_ISV4(vp)) {
2639 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2640 	}
2641 	return (error);
2642 }
2643 
2644 /*
2645  * Silly rename. To make the NFS filesystem that is stateless look a little
2646  * more like the "ufs" a remove of an active vnode is translated to a rename
2647  * to a funny looking filename that is removed by nfs_inactive on the
2648  * nfsnode. There is the potential for another process on a different client
2649  * to create the same funny name between the nfs_lookitup() fails and the
2650  * nfs_rename() completes, but...
2651  */
2652 static int
2653 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2654 {
2655 	struct sillyrename *sp;
2656 	struct nfsnode *np;
2657 	int error;
2658 	short pid;
2659 	unsigned int lticks;
2660 
2661 	cache_purge(dvp);
2662 	np = VTONFS(vp);
2663 	KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2664 	sp = malloc(sizeof (struct sillyrename),
2665 	    M_NEWNFSREQ, M_WAITOK);
2666 	sp->s_cred = crhold(cnp->cn_cred);
2667 	sp->s_dvp = dvp;
2668 	VREF(dvp);
2669 
2670 	/*
2671 	 * Fudge together a funny name.
2672 	 * Changing the format of the funny name to accommodate more
2673 	 * sillynames per directory.
2674 	 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2675 	 * CPU ticks since boot.
2676 	 */
2677 	pid = curthread->td_proc->p_pid;
2678 	lticks = (unsigned int)ticks;
2679 	for ( ; ; ) {
2680 		sp->s_namlen = sprintf(sp->s_name,
2681 				       ".nfs.%08x.%04x4.4", lticks,
2682 				       pid);
2683 		if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2684 				 curthread, NULL))
2685 			break;
2686 		lticks++;
2687 	}
2688 	error = nfs_renameit(dvp, vp, cnp, sp);
2689 	if (error)
2690 		goto bad;
2691 	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2692 		curthread, &np);
2693 	np->n_sillyrename = sp;
2694 	return (0);
2695 bad:
2696 	vrele(sp->s_dvp);
2697 	crfree(sp->s_cred);
2698 	free(sp, M_NEWNFSREQ);
2699 	return (error);
2700 }
2701 
2702 /*
2703  * Look up a file name and optionally either update the file handle or
2704  * allocate an nfsnode, depending on the value of npp.
2705  * npp == NULL	--> just do the lookup
2706  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2707  *			handled too
2708  * *npp != NULL --> update the file handle in the vnode
2709  */
2710 static int
2711 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2712     struct thread *td, struct nfsnode **npp)
2713 {
2714 	struct vnode *newvp = NULL, *vp;
2715 	struct nfsnode *np, *dnp = VTONFS(dvp);
2716 	struct nfsfh *nfhp, *onfhp;
2717 	struct nfsvattr nfsva, dnfsva;
2718 	struct componentname cn;
2719 	int error = 0, attrflag, dattrflag;
2720 	u_int hash;
2721 	struct timespec ts;
2722 
2723 	nanouptime(&ts);
2724 	error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2725 	    &nfhp, &attrflag, &dattrflag, NULL, 0);
2726 	if (dattrflag)
2727 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2728 	if (npp && !error) {
2729 		if (*npp != NULL) {
2730 		    np = *npp;
2731 		    vp = NFSTOV(np);
2732 		    /*
2733 		     * For NFSv4, check to see if it is the same name and
2734 		     * replace the name, if it is different.
2735 		     */
2736 		    if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2737 			(np->n_v4->n4_namelen != len ||
2738 			 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2739 			 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2740 			 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2741 			 dnp->n_fhp->nfh_len))) {
2742 #ifdef notdef
2743 { char nnn[100]; int nnnl;
2744 nnnl = (len < 100) ? len : 99;
2745 bcopy(name, nnn, nnnl);
2746 nnn[nnnl] = '\0';
2747 printf("replace=%s\n",nnn);
2748 }
2749 #endif
2750 			    free(np->n_v4, M_NFSV4NODE);
2751 			    np->n_v4 = malloc(
2752 				sizeof (struct nfsv4node) +
2753 				dnp->n_fhp->nfh_len + len - 1,
2754 				M_NFSV4NODE, M_WAITOK);
2755 			    np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2756 			    np->n_v4->n4_namelen = len;
2757 			    NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2758 				dnp->n_fhp->nfh_len);
2759 			    NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2760 		    }
2761 		    hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2762 			FNV1_32_INIT);
2763 		    onfhp = np->n_fhp;
2764 		    /*
2765 		     * Rehash node for new file handle.
2766 		     */
2767 		    vfs_hash_rehash(vp, hash);
2768 		    np->n_fhp = nfhp;
2769 		    if (onfhp != NULL)
2770 			free(onfhp, M_NFSFH);
2771 		    newvp = NFSTOV(np);
2772 		} else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2773 		    free(nfhp, M_NFSFH);
2774 		    VREF(dvp);
2775 		    newvp = dvp;
2776 		} else {
2777 		    cn.cn_nameptr = name;
2778 		    cn.cn_namelen = len;
2779 		    error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2780 			&np, NULL, LK_EXCLUSIVE);
2781 		    if (error)
2782 			return (error);
2783 		    newvp = NFSTOV(np);
2784 		    /*
2785 		     * If n_localmodtime >= time before RPC, then
2786 		     * a file modification operation, such as
2787 		     * VOP_SETATTR() of size, has occurred while
2788 		     * the Lookup RPC and acquisition of the vnode
2789 		     * happened.  As such, the attributes might
2790 		     * be stale, with possibly an incorrect size.
2791 		     */
2792 		    NFSLOCKNODE(np);
2793 		    if (timespecisset(&np->n_localmodtime) &&
2794 			timespeccmp(&np->n_localmodtime, &ts, >=)) {
2795 			NFSCL_DEBUG(4, "nfs_lookitup: localmod "
2796 			    "stale attributes\n");
2797 			attrflag = 0;
2798 		    }
2799 		    NFSUNLOCKNODE(np);
2800 		}
2801 		if (!attrflag && *npp == NULL) {
2802 			if (newvp == dvp)
2803 				vrele(newvp);
2804 			else
2805 				vput(newvp);
2806 			return (ENOENT);
2807 		}
2808 		if (attrflag)
2809 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2810 			    0, 1);
2811 	}
2812 	if (npp && *npp == NULL) {
2813 		if (error) {
2814 			if (newvp) {
2815 				if (newvp == dvp)
2816 					vrele(newvp);
2817 				else
2818 					vput(newvp);
2819 			}
2820 		} else
2821 			*npp = np;
2822 	}
2823 	if (error && NFS_ISV4(dvp))
2824 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2825 	return (error);
2826 }
2827 
2828 /*
2829  * Nfs Version 3 and 4 commit rpc
2830  */
2831 int
2832 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2833    struct thread *td)
2834 {
2835 	struct nfsvattr nfsva;
2836 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2837 	struct nfsnode *np;
2838 	struct uio uio;
2839 	int error, attrflag;
2840 
2841 	np = VTONFS(vp);
2842 	error = EIO;
2843 	attrflag = 0;
2844 	if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) {
2845 		uio.uio_offset = offset;
2846 		uio.uio_resid = cnt;
2847 		error = nfscl_doiods(vp, &uio, NULL, NULL,
2848 		    NFSV4OPEN_ACCESSWRITE, 1, cred, td);
2849 		if (error != 0) {
2850 			NFSLOCKNODE(np);
2851 			np->n_flag &= ~NDSCOMMIT;
2852 			NFSUNLOCKNODE(np);
2853 		}
2854 	}
2855 	if (error != 0) {
2856 		mtx_lock(&nmp->nm_mtx);
2857 		if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2858 			mtx_unlock(&nmp->nm_mtx);
2859 			return (0);
2860 		}
2861 		mtx_unlock(&nmp->nm_mtx);
2862 		error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
2863 		    &attrflag, NULL);
2864 	}
2865 	if (attrflag != 0)
2866 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL,
2867 		    0, 1);
2868 	if (error != 0 && NFS_ISV4(vp))
2869 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2870 	return (error);
2871 }
2872 
2873 /*
2874  * Strategy routine.
2875  * For async requests when nfsiod(s) are running, queue the request by
2876  * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2877  * request.
2878  */
2879 static int
2880 nfs_strategy(struct vop_strategy_args *ap)
2881 {
2882 	struct buf *bp;
2883 	struct vnode *vp;
2884 	struct ucred *cr;
2885 
2886 	bp = ap->a_bp;
2887 	vp = ap->a_vp;
2888 	KASSERT(bp->b_vp == vp, ("missing b_getvp"));
2889 	KASSERT(!(bp->b_flags & B_DONE),
2890 	    ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2891 
2892 	if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno)
2893 		bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize /
2894 		    DEV_BSIZE);
2895 	if (bp->b_iocmd == BIO_READ)
2896 		cr = bp->b_rcred;
2897 	else
2898 		cr = bp->b_wcred;
2899 
2900 	/*
2901 	 * If the op is asynchronous and an i/o daemon is waiting
2902 	 * queue the request, wake it up and wait for completion
2903 	 * otherwise just do it ourselves.
2904 	 */
2905 	if ((bp->b_flags & B_ASYNC) == 0 ||
2906 	    ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread))
2907 		(void) ncl_doio(vp, bp, cr, curthread, 1);
2908 	return (0);
2909 }
2910 
2911 /*
2912  * fsync vnode op. Just call ncl_flush() with commit == 1.
2913  */
2914 /* ARGSUSED */
2915 static int
2916 nfs_fsync(struct vop_fsync_args *ap)
2917 {
2918 
2919 	if (ap->a_vp->v_type != VREG) {
2920 		/*
2921 		 * For NFS, metadata is changed synchronously on the server,
2922 		 * so there is nothing to flush. Also, ncl_flush() clears
2923 		 * the NMODIFIED flag and that shouldn't be done here for
2924 		 * directories.
2925 		 */
2926 		return (0);
2927 	}
2928 	return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0));
2929 }
2930 
2931 /*
2932  * Flush all the blocks associated with a vnode.
2933  * 	Walk through the buffer pool and push any dirty pages
2934  *	associated with the vnode.
2935  * If the called_from_renewthread argument is TRUE, it has been called
2936  * from the NFSv4 renew thread and, as such, cannot block indefinitely
2937  * waiting for a buffer write to complete.
2938  */
2939 int
2940 ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
2941     int commit, int called_from_renewthread)
2942 {
2943 	struct nfsnode *np = VTONFS(vp);
2944 	struct buf *bp;
2945 	int i;
2946 	struct buf *nbp;
2947 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2948 	int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2949 	int passone = 1, trycnt = 0;
2950 	u_quad_t off, endoff, toff;
2951 	struct ucred* wcred = NULL;
2952 	struct buf **bvec = NULL;
2953 	struct bufobj *bo;
2954 #ifndef NFS_COMMITBVECSIZ
2955 #define	NFS_COMMITBVECSIZ	20
2956 #endif
2957 	struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2958 	u_int bvecsize = 0, bveccount;
2959 	struct timespec ts;
2960 
2961 	if (called_from_renewthread != 0)
2962 		slptimeo = hz;
2963 	if (nmp->nm_flag & NFSMNT_INT)
2964 		slpflag = PCATCH;
2965 	if (!commit)
2966 		passone = 0;
2967 	bo = &vp->v_bufobj;
2968 	/*
2969 	 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2970 	 * server, but has not been committed to stable storage on the server
2971 	 * yet. On the first pass, the byte range is worked out and the commit
2972 	 * rpc is done. On the second pass, ncl_writebp() is called to do the
2973 	 * job.
2974 	 */
2975 again:
2976 	off = (u_quad_t)-1;
2977 	endoff = 0;
2978 	bvecpos = 0;
2979 	if (NFS_ISV34(vp) && commit) {
2980 		if (bvec != NULL && bvec != bvec_on_stack)
2981 			free(bvec, M_TEMP);
2982 		/*
2983 		 * Count up how many buffers waiting for a commit.
2984 		 */
2985 		bveccount = 0;
2986 		BO_LOCK(bo);
2987 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2988 			if (!BUF_ISLOCKED(bp) &&
2989 			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2990 				== (B_DELWRI | B_NEEDCOMMIT))
2991 				bveccount++;
2992 		}
2993 		/*
2994 		 * Allocate space to remember the list of bufs to commit.  It is
2995 		 * important to use M_NOWAIT here to avoid a race with nfs_write.
2996 		 * If we can't get memory (for whatever reason), we will end up
2997 		 * committing the buffers one-by-one in the loop below.
2998 		 */
2999 		if (bveccount > NFS_COMMITBVECSIZ) {
3000 			/*
3001 			 * Release the vnode interlock to avoid a lock
3002 			 * order reversal.
3003 			 */
3004 			BO_UNLOCK(bo);
3005 			bvec = (struct buf **)
3006 				malloc(bveccount * sizeof(struct buf *),
3007 				       M_TEMP, M_NOWAIT);
3008 			BO_LOCK(bo);
3009 			if (bvec == NULL) {
3010 				bvec = bvec_on_stack;
3011 				bvecsize = NFS_COMMITBVECSIZ;
3012 			} else
3013 				bvecsize = bveccount;
3014 		} else {
3015 			bvec = bvec_on_stack;
3016 			bvecsize = NFS_COMMITBVECSIZ;
3017 		}
3018 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3019 			if (bvecpos >= bvecsize)
3020 				break;
3021 			if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3022 				nbp = TAILQ_NEXT(bp, b_bobufs);
3023 				continue;
3024 			}
3025 			if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3026 			    (B_DELWRI | B_NEEDCOMMIT)) {
3027 				BUF_UNLOCK(bp);
3028 				nbp = TAILQ_NEXT(bp, b_bobufs);
3029 				continue;
3030 			}
3031 			BO_UNLOCK(bo);
3032 			bremfree(bp);
3033 			/*
3034 			 * Work out if all buffers are using the same cred
3035 			 * so we can deal with them all with one commit.
3036 			 *
3037 			 * NOTE: we are not clearing B_DONE here, so we have
3038 			 * to do it later on in this routine if we intend to
3039 			 * initiate I/O on the bp.
3040 			 *
3041 			 * Note: to avoid loopback deadlocks, we do not
3042 			 * assign b_runningbufspace.
3043 			 */
3044 			if (wcred == NULL)
3045 				wcred = bp->b_wcred;
3046 			else if (wcred != bp->b_wcred)
3047 				wcred = NOCRED;
3048 			vfs_busy_pages(bp, 1);
3049 
3050 			BO_LOCK(bo);
3051 			/*
3052 			 * bp is protected by being locked, but nbp is not
3053 			 * and vfs_busy_pages() may sleep.  We have to
3054 			 * recalculate nbp.
3055 			 */
3056 			nbp = TAILQ_NEXT(bp, b_bobufs);
3057 
3058 			/*
3059 			 * A list of these buffers is kept so that the
3060 			 * second loop knows which buffers have actually
3061 			 * been committed. This is necessary, since there
3062 			 * may be a race between the commit rpc and new
3063 			 * uncommitted writes on the file.
3064 			 */
3065 			bvec[bvecpos++] = bp;
3066 			toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3067 				bp->b_dirtyoff;
3068 			if (toff < off)
3069 				off = toff;
3070 			toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
3071 			if (toff > endoff)
3072 				endoff = toff;
3073 		}
3074 		BO_UNLOCK(bo);
3075 	}
3076 	if (bvecpos > 0) {
3077 		/*
3078 		 * Commit data on the server, as required.
3079 		 * If all bufs are using the same wcred, then use that with
3080 		 * one call for all of them, otherwise commit each one
3081 		 * separately.
3082 		 */
3083 		if (wcred != NOCRED)
3084 			retv = ncl_commit(vp, off, (int)(endoff - off),
3085 					  wcred, td);
3086 		else {
3087 			retv = 0;
3088 			for (i = 0; i < bvecpos; i++) {
3089 				off_t off, size;
3090 				bp = bvec[i];
3091 				off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3092 					bp->b_dirtyoff;
3093 				size = (u_quad_t)(bp->b_dirtyend
3094 						  - bp->b_dirtyoff);
3095 				retv = ncl_commit(vp, off, (int)size,
3096 						  bp->b_wcred, td);
3097 				if (retv) break;
3098 			}
3099 		}
3100 
3101 		if (retv == NFSERR_STALEWRITEVERF)
3102 			ncl_clearcommit(vp->v_mount);
3103 
3104 		/*
3105 		 * Now, either mark the blocks I/O done or mark the
3106 		 * blocks dirty, depending on whether the commit
3107 		 * succeeded.
3108 		 */
3109 		for (i = 0; i < bvecpos; i++) {
3110 			bp = bvec[i];
3111 			bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3112 			if (!NFSCL_FORCEDISM(vp->v_mount) && retv) {
3113 				/*
3114 				 * Error, leave B_DELWRI intact
3115 				 */
3116 				vfs_unbusy_pages(bp);
3117 				brelse(bp);
3118 			} else {
3119 				/*
3120 				 * Success, remove B_DELWRI ( bundirty() ).
3121 				 *
3122 				 * b_dirtyoff/b_dirtyend seem to be NFS
3123 				 * specific.  We should probably move that
3124 				 * into bundirty(). XXX
3125 				 */
3126 				bufobj_wref(bo);
3127 				bp->b_flags |= B_ASYNC;
3128 				bundirty(bp);
3129 				bp->b_flags &= ~B_DONE;
3130 				bp->b_ioflags &= ~BIO_ERROR;
3131 				bp->b_dirtyoff = bp->b_dirtyend = 0;
3132 				bufdone(bp);
3133 			}
3134 		}
3135 	}
3136 
3137 	/*
3138 	 * Start/do any write(s) that are required.
3139 	 */
3140 loop:
3141 	BO_LOCK(bo);
3142 	TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3143 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3144 			if (waitfor != MNT_WAIT || passone)
3145 				continue;
3146 
3147 			error = BUF_TIMELOCK(bp,
3148 			    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
3149 			    BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
3150 			if (error == 0) {
3151 				BUF_UNLOCK(bp);
3152 				goto loop;
3153 			}
3154 			if (error == ENOLCK) {
3155 				error = 0;
3156 				goto loop;
3157 			}
3158 			if (called_from_renewthread != 0) {
3159 				/*
3160 				 * Return EIO so the flush will be retried
3161 				 * later.
3162 				 */
3163 				error = EIO;
3164 				goto done;
3165 			}
3166 			if (newnfs_sigintr(nmp, td)) {
3167 				error = EINTR;
3168 				goto done;
3169 			}
3170 			if (slpflag == PCATCH) {
3171 				slpflag = 0;
3172 				slptimeo = 2 * hz;
3173 			}
3174 			goto loop;
3175 		}
3176 		if ((bp->b_flags & B_DELWRI) == 0)
3177 			panic("nfs_fsync: not dirty");
3178 		if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3179 			BUF_UNLOCK(bp);
3180 			continue;
3181 		}
3182 		BO_UNLOCK(bo);
3183 		bremfree(bp);
3184 		bp->b_flags |= B_ASYNC;
3185 		bwrite(bp);
3186 		if (newnfs_sigintr(nmp, td)) {
3187 			error = EINTR;
3188 			goto done;
3189 		}
3190 		goto loop;
3191 	}
3192 	if (passone) {
3193 		passone = 0;
3194 		BO_UNLOCK(bo);
3195 		goto again;
3196 	}
3197 	if (waitfor == MNT_WAIT) {
3198 		while (bo->bo_numoutput) {
3199 			error = bufobj_wwait(bo, slpflag, slptimeo);
3200 			if (error) {
3201 			    BO_UNLOCK(bo);
3202 			    if (called_from_renewthread != 0) {
3203 				/*
3204 				 * Return EIO so that the flush will be
3205 				 * retried later.
3206 				 */
3207 				error = EIO;
3208 				goto done;
3209 			    }
3210 			    error = newnfs_sigintr(nmp, td);
3211 			    if (error)
3212 				goto done;
3213 			    if (slpflag == PCATCH) {
3214 				slpflag = 0;
3215 				slptimeo = 2 * hz;
3216 			    }
3217 			    BO_LOCK(bo);
3218 			}
3219 		}
3220 		if (bo->bo_dirty.bv_cnt != 0 && commit) {
3221 			BO_UNLOCK(bo);
3222 			goto loop;
3223 		}
3224 		/*
3225 		 * Wait for all the async IO requests to drain
3226 		 */
3227 		BO_UNLOCK(bo);
3228 		NFSLOCKNODE(np);
3229 		while (np->n_directio_asyncwr > 0) {
3230 			np->n_flag |= NFSYNCWAIT;
3231 			error = newnfs_msleep(td, &np->n_directio_asyncwr,
3232 			    &np->n_mtx, slpflag | (PRIBIO + 1),
3233 			    "nfsfsync", 0);
3234 			if (error) {
3235 				if (newnfs_sigintr(nmp, td)) {
3236 					NFSUNLOCKNODE(np);
3237 					error = EINTR;
3238 					goto done;
3239 				}
3240 			}
3241 		}
3242 		NFSUNLOCKNODE(np);
3243 	} else
3244 		BO_UNLOCK(bo);
3245 	if (NFSHASPNFS(nmp)) {
3246 		nfscl_layoutcommit(vp, td);
3247 		/*
3248 		 * Invalidate the attribute cache, since writes to a DS
3249 		 * won't update the size attribute.
3250 		 */
3251 		NFSLOCKNODE(np);
3252 		np->n_attrstamp = 0;
3253 	} else
3254 		NFSLOCKNODE(np);
3255 	if (np->n_flag & NWRITEERR) {
3256 		error = np->n_error;
3257 		np->n_flag &= ~NWRITEERR;
3258 	}
3259   	if (commit && bo->bo_dirty.bv_cnt == 0 &&
3260 	    bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
3261   		np->n_flag &= ~NMODIFIED;
3262 	NFSUNLOCKNODE(np);
3263 done:
3264 	if (bvec != NULL && bvec != bvec_on_stack)
3265 		free(bvec, M_TEMP);
3266 	if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
3267 	    (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
3268 	    np->n_directio_asyncwr != 0)) {
3269 		if (trycnt++ < 5) {
3270 			/* try, try again... */
3271 			passone = 1;
3272 			wcred = NULL;
3273 			bvec = NULL;
3274 			bvecsize = 0;
3275 			goto again;
3276 		}
3277 		vn_printf(vp, "ncl_flush failed");
3278 		error = called_from_renewthread != 0 ? EIO : EBUSY;
3279 	}
3280 	if (error == 0) {
3281 		nanouptime(&ts);
3282 		NFSLOCKNODE(np);
3283 		np->n_localmodtime = ts;
3284 		NFSUNLOCKNODE(np);
3285 	}
3286 	return (error);
3287 }
3288 
3289 /*
3290  * NFS advisory byte-level locks.
3291  */
3292 static int
3293 nfs_advlock(struct vop_advlock_args *ap)
3294 {
3295 	struct vnode *vp = ap->a_vp;
3296 	struct ucred *cred;
3297 	struct nfsnode *np = VTONFS(ap->a_vp);
3298 	struct proc *p = (struct proc *)ap->a_id;
3299 	struct thread *td = curthread;	/* XXX */
3300 	struct vattr va;
3301 	int ret, error;
3302 	u_quad_t size;
3303 	struct nfsmount *nmp;
3304 
3305 	error = NFSVOPLOCK(vp, LK_SHARED);
3306 	if (error != 0)
3307 		return (EBADF);
3308 	if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3309 		if (vp->v_type != VREG) {
3310 			error = EINVAL;
3311 			goto out;
3312 		}
3313 		if ((ap->a_flags & F_POSIX) != 0)
3314 			cred = p->p_ucred;
3315 		else
3316 			cred = td->td_ucred;
3317 		NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
3318 		if (VN_IS_DOOMED(vp)) {
3319 			error = EBADF;
3320 			goto out;
3321 		}
3322 
3323 		/*
3324 		 * If this is unlocking a write locked region, flush and
3325 		 * commit them before unlocking. This is required by
3326 		 * RFC3530 Sec. 9.3.2.
3327 		 */
3328 		if (ap->a_op == F_UNLCK &&
3329 		    nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3330 		    ap->a_flags))
3331 			(void) ncl_flush(vp, MNT_WAIT, td, 1, 0);
3332 
3333 		/*
3334 		 * Mark NFS node as might have acquired a lock.
3335 		 * This is separate from NHASBEENLOCKED, because it must
3336 		 * be done before the nfsrpc_advlock() call, which might
3337 		 * add a nfscllock structure to the client state.
3338 		 * It is used to check for the case where a nfscllock
3339 		 * state structure cannot exist for the file.
3340 		 * Only done for "oneopenown" NFSv4.1/4.2 mounts.
3341 		 */
3342 		nmp = VFSTONFS(vp->v_mount);
3343 		if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) {
3344 			NFSLOCKNODE(np);
3345 			np->n_flag |= NMIGHTBELOCKED;
3346 			NFSUNLOCKNODE(np);
3347 		}
3348 
3349 		/*
3350 		 * Loop around doing the lock op, while a blocking lock
3351 		 * must wait for the lock op to succeed.
3352 		 */
3353 		do {
3354 			ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3355 			    ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3356 			if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3357 			    ap->a_op == F_SETLK) {
3358 				NFSVOPUNLOCK(vp);
3359 				error = nfs_catnap(PZERO | PCATCH, ret,
3360 				    "ncladvl");
3361 				if (error)
3362 					return (EINTR);
3363 				NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3364 				if (VN_IS_DOOMED(vp)) {
3365 					error = EBADF;
3366 					goto out;
3367 				}
3368 			}
3369 		} while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3370 		     ap->a_op == F_SETLK);
3371 		if (ret == NFSERR_DENIED) {
3372 			error = EAGAIN;
3373 			goto out;
3374 		} else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3375 			error = ret;
3376 			goto out;
3377 		} else if (ret != 0) {
3378 			error = EACCES;
3379 			goto out;
3380 		}
3381 
3382 		/*
3383 		 * Now, if we just got a lock, invalidate data in the buffer
3384 		 * cache, as required, so that the coherency conforms with
3385 		 * RFC3530 Sec. 9.3.2.
3386 		 */
3387 		if (ap->a_op == F_SETLK) {
3388 			if ((np->n_flag & NMODIFIED) == 0) {
3389 				np->n_attrstamp = 0;
3390 				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3391 				ret = VOP_GETATTR(vp, &va, cred);
3392 			}
3393 			if ((np->n_flag & NMODIFIED) || ret ||
3394 			    np->n_change != va.va_filerev) {
3395 				(void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3396 				np->n_attrstamp = 0;
3397 				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3398 				ret = VOP_GETATTR(vp, &va, cred);
3399 				if (!ret) {
3400 					np->n_mtime = va.va_mtime;
3401 					np->n_change = va.va_filerev;
3402 				}
3403 			}
3404 			/* Mark that a file lock has been acquired. */
3405 			NFSLOCKNODE(np);
3406 			np->n_flag |= NHASBEENLOCKED;
3407 			NFSUNLOCKNODE(np);
3408 		}
3409 	} else if (!NFS_ISV4(vp)) {
3410 		if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3411 			size = VTONFS(vp)->n_size;
3412 			NFSVOPUNLOCK(vp);
3413 			error = lf_advlock(ap, &(vp->v_lockf), size);
3414 		} else {
3415 			if (nfs_advlock_p != NULL)
3416 				error = nfs_advlock_p(ap);
3417 			else {
3418 				NFSVOPUNLOCK(vp);
3419 				error = ENOLCK;
3420 			}
3421 		}
3422 		if (error == 0 && ap->a_op == F_SETLK) {
3423 			error = NFSVOPLOCK(vp, LK_SHARED);
3424 			if (error == 0) {
3425 				/* Mark that a file lock has been acquired. */
3426 				NFSLOCKNODE(np);
3427 				np->n_flag |= NHASBEENLOCKED;
3428 				NFSUNLOCKNODE(np);
3429 				NFSVOPUNLOCK(vp);
3430 			}
3431 		}
3432 		return (error);
3433 	} else
3434 		error = EOPNOTSUPP;
3435 out:
3436 	NFSVOPUNLOCK(vp);
3437 	return (error);
3438 }
3439 
3440 /*
3441  * NFS advisory byte-level locks.
3442  */
3443 static int
3444 nfs_advlockasync(struct vop_advlockasync_args *ap)
3445 {
3446 	struct vnode *vp = ap->a_vp;
3447 	u_quad_t size;
3448 	int error;
3449 
3450 	if (NFS_ISV4(vp))
3451 		return (EOPNOTSUPP);
3452 	error = NFSVOPLOCK(vp, LK_SHARED);
3453 	if (error)
3454 		return (error);
3455 	if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3456 		size = VTONFS(vp)->n_size;
3457 		NFSVOPUNLOCK(vp);
3458 		error = lf_advlockasync(ap, &(vp->v_lockf), size);
3459 	} else {
3460 		NFSVOPUNLOCK(vp);
3461 		error = EOPNOTSUPP;
3462 	}
3463 	return (error);
3464 }
3465 
3466 /*
3467  * Print out the contents of an nfsnode.
3468  */
3469 static int
3470 nfs_print(struct vop_print_args *ap)
3471 {
3472 	struct vnode *vp = ap->a_vp;
3473 	struct nfsnode *np = VTONFS(vp);
3474 
3475 	printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid,
3476 	    (uintmax_t)np->n_vattr.na_fsid);
3477 	if (vp->v_type == VFIFO)
3478 		fifo_printinfo(vp);
3479 	printf("\n");
3480 	return (0);
3481 }
3482 
3483 /*
3484  * This is the "real" nfs::bwrite(struct buf*).
3485  * We set B_CACHE if this is a VMIO buffer.
3486  */
3487 int
3488 ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
3489 {
3490 	int oldflags, rtval;
3491 
3492 	if (bp->b_flags & B_INVAL) {
3493 		brelse(bp);
3494 		return (0);
3495 	}
3496 
3497 	oldflags = bp->b_flags;
3498 	bp->b_flags |= B_CACHE;
3499 
3500 	/*
3501 	 * Undirty the bp.  We will redirty it later if the I/O fails.
3502 	 */
3503 	bundirty(bp);
3504 	bp->b_flags &= ~B_DONE;
3505 	bp->b_ioflags &= ~BIO_ERROR;
3506 	bp->b_iocmd = BIO_WRITE;
3507 
3508 	bufobj_wref(bp->b_bufobj);
3509 	curthread->td_ru.ru_oublock++;
3510 
3511 	/*
3512 	 * Note: to avoid loopback deadlocks, we do not
3513 	 * assign b_runningbufspace.
3514 	 */
3515 	vfs_busy_pages(bp, 1);
3516 
3517 	BUF_KERNPROC(bp);
3518 	bp->b_iooffset = dbtob(bp->b_blkno);
3519 	bstrategy(bp);
3520 
3521 	if ((oldflags & B_ASYNC) != 0)
3522 		return (0);
3523 
3524 	rtval = bufwait(bp);
3525 	if (oldflags & B_DELWRI)
3526 		reassignbuf(bp);
3527 	brelse(bp);
3528 	return (rtval);
3529 }
3530 
3531 /*
3532  * nfs special file access vnode op.
3533  * Essentially just get vattr and then imitate iaccess() since the device is
3534  * local to the client.
3535  */
3536 static int
3537 nfsspec_access(struct vop_access_args *ap)
3538 {
3539 	struct vattr *vap;
3540 	struct ucred *cred = ap->a_cred;
3541 	struct vnode *vp = ap->a_vp;
3542 	accmode_t accmode = ap->a_accmode;
3543 	struct vattr vattr;
3544 	int error;
3545 
3546 	/*
3547 	 * Disallow write attempts on filesystems mounted read-only;
3548 	 * unless the file is a socket, fifo, or a block or character
3549 	 * device resident on the filesystem.
3550 	 */
3551 	if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3552 		switch (vp->v_type) {
3553 		case VREG:
3554 		case VDIR:
3555 		case VLNK:
3556 			return (EROFS);
3557 		default:
3558 			break;
3559 		}
3560 	}
3561 	vap = &vattr;
3562 	error = VOP_GETATTR(vp, vap, cred);
3563 	if (error)
3564 		goto out;
3565 	error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3566 	    accmode, cred);
3567 out:
3568 	return error;
3569 }
3570 
3571 /*
3572  * Read wrapper for fifos.
3573  */
3574 static int
3575 nfsfifo_read(struct vop_read_args *ap)
3576 {
3577 	struct nfsnode *np = VTONFS(ap->a_vp);
3578 	int error;
3579 
3580 	/*
3581 	 * Set access flag.
3582 	 */
3583 	NFSLOCKNODE(np);
3584 	np->n_flag |= NACC;
3585 	vfs_timestamp(&np->n_atim);
3586 	NFSUNLOCKNODE(np);
3587 	error = fifo_specops.vop_read(ap);
3588 	return error;
3589 }
3590 
3591 /*
3592  * Write wrapper for fifos.
3593  */
3594 static int
3595 nfsfifo_write(struct vop_write_args *ap)
3596 {
3597 	struct nfsnode *np = VTONFS(ap->a_vp);
3598 
3599 	/*
3600 	 * Set update flag.
3601 	 */
3602 	NFSLOCKNODE(np);
3603 	np->n_flag |= NUPD;
3604 	vfs_timestamp(&np->n_mtim);
3605 	NFSUNLOCKNODE(np);
3606 	return(fifo_specops.vop_write(ap));
3607 }
3608 
3609 /*
3610  * Close wrapper for fifos.
3611  *
3612  * Update the times on the nfsnode then do fifo close.
3613  */
3614 static int
3615 nfsfifo_close(struct vop_close_args *ap)
3616 {
3617 	struct vnode *vp = ap->a_vp;
3618 	struct nfsnode *np = VTONFS(vp);
3619 	struct vattr vattr;
3620 	struct timespec ts;
3621 
3622 	NFSLOCKNODE(np);
3623 	if (np->n_flag & (NACC | NUPD)) {
3624 		vfs_timestamp(&ts);
3625 		if (np->n_flag & NACC)
3626 			np->n_atim = ts;
3627 		if (np->n_flag & NUPD)
3628 			np->n_mtim = ts;
3629 		np->n_flag |= NCHG;
3630 		if (vrefcnt(vp) == 1 &&
3631 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3632 			VATTR_NULL(&vattr);
3633 			if (np->n_flag & NACC)
3634 				vattr.va_atime = np->n_atim;
3635 			if (np->n_flag & NUPD)
3636 				vattr.va_mtime = np->n_mtim;
3637 			NFSUNLOCKNODE(np);
3638 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3639 			goto out;
3640 		}
3641 	}
3642 	NFSUNLOCKNODE(np);
3643 out:
3644 	return (fifo_specops.vop_close(ap));
3645 }
3646 
3647 /*
3648  * Just call ncl_writebp() with the force argument set to 1.
3649  *
3650  * NOTE: B_DONE may or may not be set in a_bp on call.
3651  */
3652 static int
3653 nfs_bwrite(struct buf *bp)
3654 {
3655 
3656 	return (ncl_writebp(bp, 1, curthread));
3657 }
3658 
3659 struct buf_ops buf_ops_newnfs = {
3660 	.bop_name	=	"buf_ops_nfs",
3661 	.bop_write	=	nfs_bwrite,
3662 	.bop_strategy	=	bufstrategy,
3663 	.bop_sync	=	bufsync,
3664 	.bop_bdflush	=	bufbdflush,
3665 };
3666 
3667 static int
3668 nfs_getacl(struct vop_getacl_args *ap)
3669 {
3670 	int error;
3671 
3672 	if (ap->a_type != ACL_TYPE_NFS4)
3673 		return (EOPNOTSUPP);
3674 	error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3675 	    NULL);
3676 	if (error > NFSERR_STALE) {
3677 		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3678 		error = EPERM;
3679 	}
3680 	return (error);
3681 }
3682 
3683 static int
3684 nfs_setacl(struct vop_setacl_args *ap)
3685 {
3686 	int error;
3687 
3688 	if (ap->a_type != ACL_TYPE_NFS4)
3689 		return (EOPNOTSUPP);
3690 	error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3691 	    NULL);
3692 	if (error > NFSERR_STALE) {
3693 		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3694 		error = EPERM;
3695 	}
3696 	return (error);
3697 }
3698 
3699 /*
3700  * VOP_ADVISE for NFS.
3701  * Just return 0 for any errors, since it is just a hint.
3702  */
3703 static int
3704 nfs_advise(struct vop_advise_args *ap)
3705 {
3706 	struct thread *td = curthread;
3707 	struct nfsmount *nmp;
3708 	uint64_t len;
3709 	int error;
3710 
3711 	/*
3712 	 * First do vop_stdadvise() to handle the buffer cache.
3713 	 */
3714 	error = vop_stdadvise(ap);
3715 	if (error != 0)
3716 		return (error);
3717 	if (ap->a_start < 0 || ap->a_end < 0)
3718 		return (0);
3719 	if (ap->a_end == OFF_MAX)
3720 		len = 0;
3721 	else if (ap->a_end < ap->a_start)
3722 		return (0);
3723 	else
3724 		len = ap->a_end - ap->a_start + 1;
3725 	nmp = VFSTONFS(ap->a_vp->v_mount);
3726 	mtx_lock(&nmp->nm_mtx);
3727 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
3728 	    (NFSHASPNFS(nmp) && (nmp->nm_privflag & NFSMNTP_IOADVISETHRUMDS) ==
3729 	    0) || (nmp->nm_privflag & NFSMNTP_NOADVISE) != 0) {
3730 		mtx_unlock(&nmp->nm_mtx);
3731 		return (0);
3732 	}
3733 	mtx_unlock(&nmp->nm_mtx);
3734 	error = nfsrpc_advise(ap->a_vp, ap->a_start, len, ap->a_advice,
3735 	    td->td_ucred, td);
3736 	if (error == NFSERR_NOTSUPP) {
3737 		mtx_lock(&nmp->nm_mtx);
3738 		nmp->nm_privflag |= NFSMNTP_NOADVISE;
3739 		mtx_unlock(&nmp->nm_mtx);
3740 	}
3741 	return (0);
3742 }
3743 
3744 /*
3745  * nfs allocate call
3746  */
3747 static int
3748 nfs_allocate(struct vop_allocate_args *ap)
3749 {
3750 	struct vnode *vp = ap->a_vp;
3751 	struct thread *td = curthread;
3752 	struct nfsvattr nfsva;
3753 	struct nfsmount *nmp;
3754 	struct nfsnode *np;
3755 	off_t alen;
3756 	int attrflag, error, ret;
3757 	struct timespec ts;
3758 
3759 	attrflag = 0;
3760 	nmp = VFSTONFS(vp->v_mount);
3761 	np = VTONFS(vp);
3762 	mtx_lock(&nmp->nm_mtx);
3763 	if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3764 	    (nmp->nm_privflag & NFSMNTP_NOALLOCATE) == 0) {
3765 		mtx_unlock(&nmp->nm_mtx);
3766 		/*
3767 		 * Flush first to ensure that the allocate adds to the
3768 		 * file's allocation on the server.
3769 		 */
3770 		error = ncl_flush(vp, MNT_WAIT, td, 1, 0);
3771 		if (error == 0) {
3772 			alen = *ap->a_len;
3773 			if ((uint64_t)alen > nfs_maxalloclen)
3774 				alen = nfs_maxalloclen;
3775 			error = nfsrpc_allocate(vp, *ap->a_offset, alen,
3776 			    &nfsva, &attrflag, ap->a_cred, td, NULL);
3777 		}
3778 		if (error == 0) {
3779 			*ap->a_offset += alen;
3780 			*ap->a_len -= alen;
3781 			nanouptime(&ts);
3782 			NFSLOCKNODE(np);
3783 			np->n_localmodtime = ts;
3784 			NFSUNLOCKNODE(np);
3785 		} else if (error == NFSERR_NOTSUPP) {
3786 			mtx_lock(&nmp->nm_mtx);
3787 			nmp->nm_privflag |= NFSMNTP_NOALLOCATE;
3788 			mtx_unlock(&nmp->nm_mtx);
3789 			error = EINVAL;
3790 		}
3791 	} else {
3792 		mtx_unlock(&nmp->nm_mtx);
3793 		error = EINVAL;
3794 	}
3795 	if (attrflag != 0) {
3796 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
3797 		if (error == 0 && ret != 0)
3798 			error = ret;
3799 	}
3800 	if (error != 0)
3801 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3802 	return (error);
3803 }
3804 
3805 /*
3806  * nfs deallocate call
3807  */
3808 static int
3809 nfs_deallocate(struct vop_deallocate_args *ap)
3810 {
3811 	struct vnode *vp = ap->a_vp;
3812 	struct thread *td = curthread;
3813 	struct nfsvattr nfsva;
3814 	struct nfsmount *nmp;
3815 	struct nfsnode *np;
3816 	off_t tlen, mlen;
3817 	int attrflag, error, ret;
3818 	bool clipped;
3819 	struct timespec ts;
3820 
3821 	error = 0;
3822 	attrflag = 0;
3823 	nmp = VFSTONFS(vp->v_mount);
3824 	np = VTONFS(vp);
3825 	mtx_lock(&nmp->nm_mtx);
3826 	if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3827 	    (nmp->nm_privflag & NFSMNTP_NODEALLOCATE) == 0) {
3828 		mtx_unlock(&nmp->nm_mtx);
3829 		tlen = omin(OFF_MAX - *ap->a_offset, *ap->a_len);
3830 		NFSCL_DEBUG(4, "dealloc: off=%jd len=%jd maxfilesize=%ju\n",
3831 		    (intmax_t)*ap->a_offset, (intmax_t)tlen,
3832 		    (uintmax_t)nmp->nm_maxfilesize);
3833 		if ((uint64_t)*ap->a_offset >= nmp->nm_maxfilesize) {
3834 			/* Avoid EFBIG error return from the NFSv4.2 server. */
3835 			*ap->a_len = 0;
3836 			return (0);
3837 		}
3838 		clipped = false;
3839 		if ((uint64_t)*ap->a_offset + tlen > nmp->nm_maxfilesize)
3840 			tlen = nmp->nm_maxfilesize - *ap->a_offset;
3841 		if ((uint64_t)*ap->a_offset < np->n_size) {
3842 			/* Limit the len to nfs_maxalloclen before EOF. */
3843 			mlen = omin((off_t)np->n_size - *ap->a_offset, tlen);
3844 			if ((uint64_t)mlen > nfs_maxalloclen) {
3845 				NFSCL_DEBUG(4, "dealloc: tlen maxalloclen\n");
3846 				tlen = nfs_maxalloclen;
3847 				clipped = true;
3848 			}
3849 		}
3850 		if (error == 0)
3851 			error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
3852 		if (error == 0) {
3853 			vnode_pager_purge_range(vp, *ap->a_offset,
3854 			    *ap->a_offset + tlen);
3855 			error = nfsrpc_deallocate(vp, *ap->a_offset, tlen,
3856 			    &nfsva, &attrflag, ap->a_cred, td, NULL);
3857 			NFSCL_DEBUG(4, "dealloc: rpc=%d\n", error);
3858 		}
3859 		if (error == 0) {
3860 			NFSCL_DEBUG(4, "dealloc: attrflag=%d na_size=%ju\n",
3861 			    attrflag, (uintmax_t)nfsva.na_size);
3862 			nanouptime(&ts);
3863 			NFSLOCKNODE(np);
3864 			np->n_localmodtime = ts;
3865 			NFSUNLOCKNODE(np);
3866 			if (attrflag != 0) {
3867 				if ((uint64_t)*ap->a_offset < nfsva.na_size)
3868 					*ap->a_offset += omin((off_t)
3869 					    nfsva.na_size - *ap->a_offset,
3870 					    tlen);
3871 			}
3872 			if (clipped && tlen < *ap->a_len)
3873 				*ap->a_len -= tlen;
3874 			else
3875 				*ap->a_len = 0;
3876 		} else if (error == NFSERR_NOTSUPP) {
3877 			mtx_lock(&nmp->nm_mtx);
3878 			nmp->nm_privflag |= NFSMNTP_NODEALLOCATE;
3879 			mtx_unlock(&nmp->nm_mtx);
3880 		}
3881 	} else {
3882 		mtx_unlock(&nmp->nm_mtx);
3883 		error = EIO;
3884 	}
3885 	/*
3886 	 * If the NFS server cannot perform the Deallocate operation, just call
3887 	 * vop_stddeallocate() to perform it.
3888 	 */
3889 	if (error != 0 && error != NFSERR_FBIG && error != NFSERR_INVAL) {
3890 		error = vop_stddeallocate(ap);
3891 		NFSCL_DEBUG(4, "dealloc: stddeallocate=%d\n", error);
3892 	}
3893 	if (attrflag != 0) {
3894 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
3895 		if (error == 0 && ret != 0)
3896 			error = ret;
3897 	}
3898 	if (error != 0)
3899 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3900 	return (error);
3901 }
3902 
3903 /*
3904  * nfs copy_file_range call
3905  */
3906 static int
3907 nfs_copy_file_range(struct vop_copy_file_range_args *ap)
3908 {
3909 	struct vnode *invp = ap->a_invp;
3910 	struct vnode *outvp = ap->a_outvp;
3911 	struct mount *mp;
3912 	struct nfsvattr innfsva, outnfsva;
3913 	struct vattr *vap;
3914 	struct uio io;
3915 	struct nfsmount *nmp;
3916 	size_t len, len2;
3917 	int error, inattrflag, outattrflag, ret, ret2;
3918 	off_t inoff, outoff;
3919 	bool consecutive, must_commit, tryoutcred;
3920 
3921 	ret = ret2 = 0;
3922 	nmp = VFSTONFS(invp->v_mount);
3923 	mtx_lock(&nmp->nm_mtx);
3924 	/* NFSv4.2 Copy is not permitted for infile == outfile. */
3925 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
3926 	    (nmp->nm_privflag & NFSMNTP_NOCOPY) != 0 || invp == outvp) {
3927 		mtx_unlock(&nmp->nm_mtx);
3928 		error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
3929 		    ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
3930 		    ap->a_incred, ap->a_outcred, ap->a_fsizetd);
3931 		return (error);
3932 	}
3933 	mtx_unlock(&nmp->nm_mtx);
3934 
3935 	/* Lock both vnodes, avoiding risk of deadlock. */
3936 	do {
3937 		mp = NULL;
3938 		error = vn_start_write(outvp, &mp, V_WAIT);
3939 		if (error == 0) {
3940 			error = vn_lock(outvp, LK_EXCLUSIVE);
3941 			if (error == 0) {
3942 				error = vn_lock(invp, LK_SHARED | LK_NOWAIT);
3943 				if (error == 0)
3944 					break;
3945 				VOP_UNLOCK(outvp);
3946 				if (mp != NULL)
3947 					vn_finished_write(mp);
3948 				mp = NULL;
3949 				error = vn_lock(invp, LK_SHARED);
3950 				if (error == 0)
3951 					VOP_UNLOCK(invp);
3952 			}
3953 		}
3954 		if (mp != NULL)
3955 			vn_finished_write(mp);
3956 	} while (error == 0);
3957 	if (error != 0)
3958 		return (error);
3959 
3960 	/*
3961 	 * Do the vn_rlimit_fsize() check.  Should this be above the VOP layer?
3962 	 */
3963 	io.uio_offset = *ap->a_outoffp;
3964 	io.uio_resid = *ap->a_lenp;
3965 	error = vn_rlimit_fsize(outvp, &io, ap->a_fsizetd);
3966 
3967 	/*
3968 	 * Flush the input file so that the data is up to date before
3969 	 * the copy.  Flush writes for the output file so that they
3970 	 * do not overwrite the data copied to the output file by the Copy.
3971 	 * Set the commit argument for both flushes so that the data is on
3972 	 * stable storage before the Copy RPC.  This is done in case the
3973 	 * server reboots during the Copy and needs to be redone.
3974 	 */
3975 	if (error == 0)
3976 		error = ncl_flush(invp, MNT_WAIT, curthread, 1, 0);
3977 	if (error == 0)
3978 		error = ncl_flush(outvp, MNT_WAIT, curthread, 1, 0);
3979 
3980 	/* Do the actual NFSv4.2 RPC. */
3981 	len = *ap->a_lenp;
3982 	mtx_lock(&nmp->nm_mtx);
3983 	if ((nmp->nm_privflag & NFSMNTP_NOCONSECUTIVE) == 0)
3984 		consecutive = true;
3985 	else
3986 		consecutive = false;
3987 	mtx_unlock(&nmp->nm_mtx);
3988 	inoff = *ap->a_inoffp;
3989 	outoff = *ap->a_outoffp;
3990 	tryoutcred = true;
3991 	must_commit = false;
3992 	if (error == 0) {
3993 		vap = &VTONFS(invp)->n_vattr.na_vattr;
3994 		error = VOP_GETATTR(invp, vap, ap->a_incred);
3995 		if (error == 0) {
3996 			/*
3997 			 * Clip "len" at va_size so that RFC compliant servers
3998 			 * will not reply NFSERR_INVAL.
3999 			 * Setting "len == 0" for the RPC would be preferred,
4000 			 * but some Linux servers do not support that.
4001 			 */
4002 			if (inoff >= vap->va_size)
4003 				*ap->a_lenp = len = 0;
4004 			else if (inoff + len > vap->va_size)
4005 				*ap->a_lenp = len = vap->va_size - inoff;
4006 		} else
4007 			error = 0;
4008 	}
4009 
4010 	/*
4011 	 * len will be set to 0 upon a successful Copy RPC.
4012 	 * As such, this only loops when the Copy RPC needs to be retried.
4013 	 */
4014 	while (len > 0 && error == 0) {
4015 		inattrflag = outattrflag = 0;
4016 		len2 = len;
4017 		if (tryoutcred)
4018 			error = nfsrpc_copy_file_range(invp, ap->a_inoffp,
4019 			    outvp, ap->a_outoffp, &len2, ap->a_flags,
4020 			    &inattrflag, &innfsva, &outattrflag, &outnfsva,
4021 			    ap->a_outcred, consecutive, &must_commit);
4022 		else
4023 			error = nfsrpc_copy_file_range(invp, ap->a_inoffp,
4024 			    outvp, ap->a_outoffp, &len2, ap->a_flags,
4025 			    &inattrflag, &innfsva, &outattrflag, &outnfsva,
4026 			    ap->a_incred, consecutive, &must_commit);
4027 		if (inattrflag != 0)
4028 			ret = nfscl_loadattrcache(&invp, &innfsva, NULL, NULL,
4029 			    0, 1);
4030 		if (outattrflag != 0)
4031 			ret2 = nfscl_loadattrcache(&outvp, &outnfsva, NULL,
4032 			    NULL, 1, 1);
4033 		if (error == 0) {
4034 			if (consecutive == false) {
4035 				if (len2 == len) {
4036 					mtx_lock(&nmp->nm_mtx);
4037 					nmp->nm_privflag |=
4038 					    NFSMNTP_NOCONSECUTIVE;
4039 					mtx_unlock(&nmp->nm_mtx);
4040 				} else
4041 					error = NFSERR_OFFLOADNOREQS;
4042 			}
4043 			*ap->a_lenp = len2;
4044 			len = 0;
4045 			if (len2 > 0 && must_commit && error == 0)
4046 				error = ncl_commit(outvp, outoff, *ap->a_lenp,
4047 				    ap->a_outcred, curthread);
4048 			if (error == 0 && ret != 0)
4049 				error = ret;
4050 			if (error == 0 && ret2 != 0)
4051 				error = ret2;
4052 		} else if (error == NFSERR_OFFLOADNOREQS && consecutive) {
4053 			/*
4054 			 * Try consecutive == false, which is ok only if all
4055 			 * bytes are copied.
4056 			 * If only some bytes were copied when consecutive
4057 			 * is false, there is no way to know which bytes
4058 			 * still need to be written.
4059 			 */
4060 			consecutive = false;
4061 			error = 0;
4062 		} else if (error == NFSERR_ACCES && tryoutcred) {
4063 			/* Try again with incred. */
4064 			tryoutcred = false;
4065 			error = 0;
4066 		}
4067 		if (error == NFSERR_STALEWRITEVERF) {
4068 			/*
4069 			 * Server rebooted, so do it all again.
4070 			 */
4071 			*ap->a_inoffp = inoff;
4072 			*ap->a_outoffp = outoff;
4073 			len = *ap->a_lenp;
4074 			must_commit = false;
4075 			error = 0;
4076 		}
4077 	}
4078 	VOP_UNLOCK(invp);
4079 	VOP_UNLOCK(outvp);
4080 	if (mp != NULL)
4081 		vn_finished_write(mp);
4082 	if (error == NFSERR_NOTSUPP || error == NFSERR_OFFLOADNOREQS ||
4083 	    error == NFSERR_ACCES) {
4084 		/*
4085 		 * Unlike the NFSv4.2 Copy, vn_generic_copy_file_range() can
4086 		 * use a_incred for the read and a_outcred for the write, so
4087 		 * try this for NFSERR_ACCES failures for the Copy.
4088 		 * For NFSERR_NOTSUPP and NFSERR_OFFLOADNOREQS, the Copy can
4089 		 * never succeed, so disable it.
4090 		 */
4091 		if (error != NFSERR_ACCES) {
4092 			/* Can never do Copy on this mount. */
4093 			mtx_lock(&nmp->nm_mtx);
4094 			nmp->nm_privflag |= NFSMNTP_NOCOPY;
4095 			mtx_unlock(&nmp->nm_mtx);
4096 		}
4097 		*ap->a_inoffp = inoff;
4098 		*ap->a_outoffp = outoff;
4099 		error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
4100 		    ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
4101 		    ap->a_incred, ap->a_outcred, ap->a_fsizetd);
4102 	} else if (error != 0)
4103 		*ap->a_lenp = 0;
4104 
4105 	if (error != 0)
4106 		error = nfscl_maperr(curthread, error, (uid_t)0, (gid_t)0);
4107 	return (error);
4108 }
4109 
4110 /*
4111  * nfs ioctl call
4112  */
4113 static int
4114 nfs_ioctl(struct vop_ioctl_args *ap)
4115 {
4116 	struct vnode *vp = ap->a_vp;
4117 	struct nfsvattr nfsva;
4118 	struct nfsmount *nmp;
4119 	int attrflag, content, error, ret;
4120 	bool eof = false;			/* shut up compiler. */
4121 
4122 	if (vp->v_type != VREG)
4123 		return (ENOTTY);
4124 	nmp = VFSTONFS(vp->v_mount);
4125 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION) {
4126 		error = vop_stdioctl(ap);
4127 		return (error);
4128 	}
4129 
4130 	/* Do the actual NFSv4.2 RPC. */
4131 	switch (ap->a_command) {
4132 	case FIOSEEKDATA:
4133 		content = NFSV4CONTENT_DATA;
4134 		break;
4135 	case FIOSEEKHOLE:
4136 		content = NFSV4CONTENT_HOLE;
4137 		break;
4138 	default:
4139 		return (ENOTTY);
4140 	}
4141 
4142 	error = vn_lock(vp, LK_SHARED);
4143 	if (error != 0)
4144 		return (EBADF);
4145 	attrflag = 0;
4146 	if (*((off_t *)ap->a_data) >= VTONFS(vp)->n_size)
4147 		error = ENXIO;
4148 	else {
4149 		/*
4150 		 * Flush all writes, so that the server is up to date.
4151 		 * Although a Commit is not required, the commit argument
4152 		 * is set so that, for a pNFS File/Flexible File Layout
4153 		 * server, the LayoutCommit will be done to ensure the file
4154 		 * size is up to date on the Metadata Server.
4155 		 */
4156 		error = ncl_flush(vp, MNT_WAIT, ap->a_td, 1, 0);
4157 		if (error == 0)
4158 			error = nfsrpc_seek(vp, (off_t *)ap->a_data, &eof,
4159 			    content, ap->a_cred, &nfsva, &attrflag);
4160 		/* If at eof for FIOSEEKDATA, return ENXIO. */
4161 		if (eof && error == 0 && content == NFSV4CONTENT_DATA)
4162 			error = ENXIO;
4163 	}
4164 	if (attrflag != 0) {
4165 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
4166 		if (error == 0 && ret != 0)
4167 			error = ret;
4168 	}
4169 	NFSVOPUNLOCK(vp);
4170 
4171 	if (error != 0)
4172 		error = ENXIO;
4173 	return (error);
4174 }
4175 
4176 /*
4177  * nfs getextattr call
4178  */
4179 static int
4180 nfs_getextattr(struct vop_getextattr_args *ap)
4181 {
4182 	struct vnode *vp = ap->a_vp;
4183 	struct nfsmount *nmp;
4184 	struct ucred *cred;
4185 	struct thread *td = ap->a_td;
4186 	struct nfsvattr nfsva;
4187 	ssize_t len;
4188 	int attrflag, error, ret;
4189 
4190 	nmp = VFSTONFS(vp->v_mount);
4191 	mtx_lock(&nmp->nm_mtx);
4192 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4193 	    (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4194 	    ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4195 		mtx_unlock(&nmp->nm_mtx);
4196 		return (EOPNOTSUPP);
4197 	}
4198 	mtx_unlock(&nmp->nm_mtx);
4199 
4200 	cred = ap->a_cred;
4201 	if (cred == NULL)
4202 		cred = td->td_ucred;
4203 	/* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4204 	attrflag = 0;
4205 	error = nfsrpc_getextattr(vp, ap->a_name, ap->a_uio, &len, &nfsva,
4206 	    &attrflag, cred, td);
4207 	if (attrflag != 0) {
4208 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
4209 		if (error == 0 && ret != 0)
4210 			error = ret;
4211 	}
4212 	if (error == 0 && ap->a_size != NULL)
4213 		*ap->a_size = len;
4214 
4215 	switch (error) {
4216 	case NFSERR_NOTSUPP:
4217 	case NFSERR_OPILLEGAL:
4218 		mtx_lock(&nmp->nm_mtx);
4219 		nmp->nm_privflag |= NFSMNTP_NOXATTR;
4220 		mtx_unlock(&nmp->nm_mtx);
4221 		error = EOPNOTSUPP;
4222 		break;
4223 	case NFSERR_NOXATTR:
4224 	case NFSERR_XATTR2BIG:
4225 		error = ENOATTR;
4226 		break;
4227 	default:
4228 		error = nfscl_maperr(td, error, 0, 0);
4229 		break;
4230 	}
4231 	return (error);
4232 }
4233 
4234 /*
4235  * nfs setextattr call
4236  */
4237 static int
4238 nfs_setextattr(struct vop_setextattr_args *ap)
4239 {
4240 	struct vnode *vp = ap->a_vp;
4241 	struct nfsmount *nmp;
4242 	struct ucred *cred;
4243 	struct thread *td = ap->a_td;
4244 	struct nfsvattr nfsva;
4245 	int attrflag, error, ret;
4246 
4247 	nmp = VFSTONFS(vp->v_mount);
4248 	mtx_lock(&nmp->nm_mtx);
4249 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4250 	    (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4251 	    ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4252 		mtx_unlock(&nmp->nm_mtx);
4253 		return (EOPNOTSUPP);
4254 	}
4255 	mtx_unlock(&nmp->nm_mtx);
4256 
4257 	if (ap->a_uio->uio_resid < 0)
4258 		return (EINVAL);
4259 	cred = ap->a_cred;
4260 	if (cred == NULL)
4261 		cred = td->td_ucred;
4262 	/* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4263 	attrflag = 0;
4264 	error = nfsrpc_setextattr(vp, ap->a_name, ap->a_uio, &nfsva,
4265 	    &attrflag, cred, td);
4266 	if (attrflag != 0) {
4267 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
4268 		if (error == 0 && ret != 0)
4269 			error = ret;
4270 	}
4271 
4272 	switch (error) {
4273 	case NFSERR_NOTSUPP:
4274 	case NFSERR_OPILLEGAL:
4275 		mtx_lock(&nmp->nm_mtx);
4276 		nmp->nm_privflag |= NFSMNTP_NOXATTR;
4277 		mtx_unlock(&nmp->nm_mtx);
4278 		error = EOPNOTSUPP;
4279 		break;
4280 	case NFSERR_NOXATTR:
4281 	case NFSERR_XATTR2BIG:
4282 		error = ENOATTR;
4283 		break;
4284 	default:
4285 		error = nfscl_maperr(td, error, 0, 0);
4286 		break;
4287 	}
4288 	return (error);
4289 }
4290 
4291 /*
4292  * nfs listextattr call
4293  */
4294 static int
4295 nfs_listextattr(struct vop_listextattr_args *ap)
4296 {
4297 	struct vnode *vp = ap->a_vp;
4298 	struct nfsmount *nmp;
4299 	struct ucred *cred;
4300 	struct thread *td = ap->a_td;
4301 	struct nfsvattr nfsva;
4302 	size_t len, len2;
4303 	uint64_t cookie;
4304 	int attrflag, error, ret;
4305 	bool eof;
4306 
4307 	nmp = VFSTONFS(vp->v_mount);
4308 	mtx_lock(&nmp->nm_mtx);
4309 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4310 	    (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4311 	    ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4312 		mtx_unlock(&nmp->nm_mtx);
4313 		return (EOPNOTSUPP);
4314 	}
4315 	mtx_unlock(&nmp->nm_mtx);
4316 
4317 	cred = ap->a_cred;
4318 	if (cred == NULL)
4319 		cred = td->td_ucred;
4320 
4321 	/* Loop around doing List Extended Attribute RPCs. */
4322 	eof = false;
4323 	cookie = 0;
4324 	len2 = 0;
4325 	error = 0;
4326 	while (!eof && error == 0) {
4327 		len = nmp->nm_rsize;
4328 		attrflag = 0;
4329 		error = nfsrpc_listextattr(vp, &cookie, ap->a_uio, &len, &eof,
4330 		    &nfsva, &attrflag, cred, td);
4331 		if (attrflag != 0) {
4332 			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
4333 			    1);
4334 			if (error == 0 && ret != 0)
4335 				error = ret;
4336 		}
4337 		if (error == 0) {
4338 			len2 += len;
4339 			if (len2 > SSIZE_MAX)
4340 				error = ENOATTR;
4341 		}
4342 	}
4343 	if (error == 0 && ap->a_size != NULL)
4344 		*ap->a_size = len2;
4345 
4346 	switch (error) {
4347 	case NFSERR_NOTSUPP:
4348 	case NFSERR_OPILLEGAL:
4349 		mtx_lock(&nmp->nm_mtx);
4350 		nmp->nm_privflag |= NFSMNTP_NOXATTR;
4351 		mtx_unlock(&nmp->nm_mtx);
4352 		error = EOPNOTSUPP;
4353 		break;
4354 	case NFSERR_NOXATTR:
4355 	case NFSERR_XATTR2BIG:
4356 		error = ENOATTR;
4357 		break;
4358 	default:
4359 		error = nfscl_maperr(td, error, 0, 0);
4360 		break;
4361 	}
4362 	return (error);
4363 }
4364 
4365 /*
4366  * nfs setextattr call
4367  */
4368 static int
4369 nfs_deleteextattr(struct vop_deleteextattr_args *ap)
4370 {
4371 	struct vnode *vp = ap->a_vp;
4372 	struct nfsmount *nmp;
4373 	struct nfsvattr nfsva;
4374 	int attrflag, error, ret;
4375 
4376 	nmp = VFSTONFS(vp->v_mount);
4377 	mtx_lock(&nmp->nm_mtx);
4378 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4379 	    (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4380 	    ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4381 		mtx_unlock(&nmp->nm_mtx);
4382 		return (EOPNOTSUPP);
4383 	}
4384 	mtx_unlock(&nmp->nm_mtx);
4385 
4386 	/* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4387 	attrflag = 0;
4388 	error = nfsrpc_rmextattr(vp, ap->a_name, &nfsva, &attrflag, ap->a_cred,
4389 	    ap->a_td);
4390 	if (attrflag != 0) {
4391 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
4392 		if (error == 0 && ret != 0)
4393 			error = ret;
4394 	}
4395 
4396 	switch (error) {
4397 	case NFSERR_NOTSUPP:
4398 	case NFSERR_OPILLEGAL:
4399 		mtx_lock(&nmp->nm_mtx);
4400 		nmp->nm_privflag |= NFSMNTP_NOXATTR;
4401 		mtx_unlock(&nmp->nm_mtx);
4402 		error = EOPNOTSUPP;
4403 		break;
4404 	case NFSERR_NOXATTR:
4405 	case NFSERR_XATTR2BIG:
4406 		error = ENOATTR;
4407 		break;
4408 	default:
4409 		error = nfscl_maperr(ap->a_td, error, 0, 0);
4410 		break;
4411 	}
4412 	return (error);
4413 }
4414 
4415 /*
4416  * Return POSIX pathconf information applicable to nfs filesystems.
4417  */
4418 static int
4419 nfs_pathconf(struct vop_pathconf_args *ap)
4420 {
4421 	struct nfsv3_pathconf pc;
4422 	struct nfsvattr nfsva;
4423 	struct vnode *vp = ap->a_vp;
4424 	struct nfsmount *nmp;
4425 	struct thread *td = curthread;
4426 	off_t off;
4427 	bool eof;
4428 	int attrflag, error;
4429 
4430 	if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
4431 	    ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
4432 	    ap->a_name == _PC_NO_TRUNC)) ||
4433 	    (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) {
4434 		/*
4435 		 * Since only the above 4 a_names are returned by the NFSv3
4436 		 * Pathconf RPC, there is no point in doing it for others.
4437 		 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
4438 		 * be used for _PC_NFS4_ACL as well.
4439 		 */
4440 		error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
4441 		    &attrflag, NULL);
4442 		if (attrflag != 0)
4443 			(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
4444 			    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 					nfscl_loadattrcache(&vp, &nfsva,
4546 					    NULL, 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