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