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