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