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