xref: /freebsd/sys/fs/nfsclient/nfs_clvnops.c (revision 0957b409)
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/jail.h>
55 #include <sys/malloc.h>
56 #include <sys/mbuf.h>
57 #include <sys/namei.h>
58 #include <sys/socket.h>
59 #include <sys/vnode.h>
60 #include <sys/dirent.h>
61 #include <sys/fcntl.h>
62 #include <sys/lockf.h>
63 #include <sys/stat.h>
64 #include <sys/sysctl.h>
65 #include <sys/signalvar.h>
66 
67 #include <vm/vm.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_object.h>
70 
71 #include <fs/nfs/nfsport.h>
72 #include <fs/nfsclient/nfsnode.h>
73 #include <fs/nfsclient/nfsmount.h>
74 #include <fs/nfsclient/nfs.h>
75 #include <fs/nfsclient/nfs_kdtrace.h>
76 
77 #include <net/if.h>
78 #include <netinet/in.h>
79 #include <netinet/in_var.h>
80 
81 #include <nfs/nfs_lock.h>
82 
83 #ifdef KDTRACE_HOOKS
84 #include <sys/dtrace_bsd.h>
85 
86 dtrace_nfsclient_accesscache_flush_probe_func_t
87 		dtrace_nfscl_accesscache_flush_done_probe;
88 uint32_t	nfscl_accesscache_flush_done_id;
89 
90 dtrace_nfsclient_accesscache_get_probe_func_t
91 		dtrace_nfscl_accesscache_get_hit_probe,
92 		dtrace_nfscl_accesscache_get_miss_probe;
93 uint32_t	nfscl_accesscache_get_hit_id;
94 uint32_t	nfscl_accesscache_get_miss_id;
95 
96 dtrace_nfsclient_accesscache_load_probe_func_t
97 		dtrace_nfscl_accesscache_load_done_probe;
98 uint32_t	nfscl_accesscache_load_done_id;
99 #endif /* !KDTRACE_HOOKS */
100 
101 /* Defs */
102 #define	TRUE	1
103 #define	FALSE	0
104 
105 extern struct nfsstatsv1 nfsstatsv1;
106 extern int nfsrv_useacl;
107 extern int nfscl_debuglevel;
108 MALLOC_DECLARE(M_NEWNFSREQ);
109 
110 static vop_read_t	nfsfifo_read;
111 static vop_write_t	nfsfifo_write;
112 static vop_close_t	nfsfifo_close;
113 static int	nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
114 		    struct thread *);
115 static vop_lookup_t	nfs_lookup;
116 static vop_create_t	nfs_create;
117 static vop_mknod_t	nfs_mknod;
118 static vop_open_t	nfs_open;
119 static vop_pathconf_t	nfs_pathconf;
120 static vop_close_t	nfs_close;
121 static vop_access_t	nfs_access;
122 static vop_getattr_t	nfs_getattr;
123 static vop_setattr_t	nfs_setattr;
124 static vop_read_t	nfs_read;
125 static vop_fsync_t	nfs_fsync;
126 static vop_remove_t	nfs_remove;
127 static vop_link_t	nfs_link;
128 static vop_rename_t	nfs_rename;
129 static vop_mkdir_t	nfs_mkdir;
130 static vop_rmdir_t	nfs_rmdir;
131 static vop_symlink_t	nfs_symlink;
132 static vop_readdir_t	nfs_readdir;
133 static vop_strategy_t	nfs_strategy;
134 static	int	nfs_lookitup(struct vnode *, char *, int,
135 		    struct ucred *, struct thread *, struct nfsnode **);
136 static	int	nfs_sillyrename(struct vnode *, struct vnode *,
137 		    struct componentname *);
138 static vop_access_t	nfsspec_access;
139 static vop_readlink_t	nfs_readlink;
140 static vop_print_t	nfs_print;
141 static vop_advlock_t	nfs_advlock;
142 static vop_advlockasync_t nfs_advlockasync;
143 static vop_getacl_t nfs_getacl;
144 static vop_setacl_t nfs_setacl;
145 static vop_set_text_t nfs_set_text;
146 
147 /*
148  * Global vfs data structures for nfs
149  */
150 
151 static struct vop_vector newnfs_vnodeops_nosig = {
152 	.vop_default =		&default_vnodeops,
153 	.vop_access =		nfs_access,
154 	.vop_advlock =		nfs_advlock,
155 	.vop_advlockasync =	nfs_advlockasync,
156 	.vop_close =		nfs_close,
157 	.vop_create =		nfs_create,
158 	.vop_fsync =		nfs_fsync,
159 	.vop_getattr =		nfs_getattr,
160 	.vop_getpages =		ncl_getpages,
161 	.vop_putpages =		ncl_putpages,
162 	.vop_inactive =		ncl_inactive,
163 	.vop_link =		nfs_link,
164 	.vop_lookup =		nfs_lookup,
165 	.vop_mkdir =		nfs_mkdir,
166 	.vop_mknod =		nfs_mknod,
167 	.vop_open =		nfs_open,
168 	.vop_pathconf =		nfs_pathconf,
169 	.vop_print =		nfs_print,
170 	.vop_read =		nfs_read,
171 	.vop_readdir =		nfs_readdir,
172 	.vop_readlink =		nfs_readlink,
173 	.vop_reclaim =		ncl_reclaim,
174 	.vop_remove =		nfs_remove,
175 	.vop_rename =		nfs_rename,
176 	.vop_rmdir =		nfs_rmdir,
177 	.vop_setattr =		nfs_setattr,
178 	.vop_strategy =		nfs_strategy,
179 	.vop_symlink =		nfs_symlink,
180 	.vop_write =		ncl_write,
181 	.vop_getacl =		nfs_getacl,
182 	.vop_setacl =		nfs_setacl,
183 	.vop_set_text =		nfs_set_text,
184 };
185 
186 static int
187 nfs_vnodeops_bypass(struct vop_generic_args *a)
188 {
189 
190 	return (vop_sigdefer(&newnfs_vnodeops_nosig, a));
191 }
192 
193 struct vop_vector newnfs_vnodeops = {
194 	.vop_default =		&default_vnodeops,
195 	.vop_bypass =		nfs_vnodeops_bypass,
196 };
197 
198 static struct vop_vector newnfs_fifoops_nosig = {
199 	.vop_default =		&fifo_specops,
200 	.vop_access =		nfsspec_access,
201 	.vop_close =		nfsfifo_close,
202 	.vop_fsync =		nfs_fsync,
203 	.vop_getattr =		nfs_getattr,
204 	.vop_inactive =		ncl_inactive,
205 	.vop_pathconf =		nfs_pathconf,
206 	.vop_print =		nfs_print,
207 	.vop_read =		nfsfifo_read,
208 	.vop_reclaim =		ncl_reclaim,
209 	.vop_setattr =		nfs_setattr,
210 	.vop_write =		nfsfifo_write,
211 };
212 
213 static int
214 nfs_fifoops_bypass(struct vop_generic_args *a)
215 {
216 
217 	return (vop_sigdefer(&newnfs_fifoops_nosig, a));
218 }
219 
220 struct vop_vector newnfs_fifoops = {
221 	.vop_default =		&default_vnodeops,
222 	.vop_bypass =		nfs_fifoops_bypass,
223 };
224 
225 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
226     struct componentname *cnp, struct vattr *vap);
227 static int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
228     int namelen, struct ucred *cred, struct thread *td);
229 static int nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp,
230     char *fnameptr, int fnamelen, struct vnode *tdvp, struct vnode *tvp,
231     char *tnameptr, int tnamelen, struct ucred *cred, struct thread *td);
232 static int nfs_renameit(struct vnode *sdvp, struct vnode *svp,
233     struct componentname *scnp, struct sillyrename *sp);
234 
235 /*
236  * Global variables
237  */
238 SYSCTL_DECL(_vfs_nfs);
239 
240 static int	nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
241 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
242 	   &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
243 
244 static int	nfs_prime_access_cache = 0;
245 SYSCTL_INT(_vfs_nfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
246 	   &nfs_prime_access_cache, 0,
247 	   "Prime NFS ACCESS cache when fetching attributes");
248 
249 static int	newnfs_commit_on_close = 0;
250 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
251     &newnfs_commit_on_close, 0, "write+commit on close, else only write");
252 
253 static int	nfs_clean_pages_on_close = 1;
254 SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
255 	   &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
256 
257 int newnfs_directio_enable = 0;
258 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
259 	   &newnfs_directio_enable, 0, "Enable NFS directio");
260 
261 int nfs_keep_dirty_on_error;
262 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_keep_dirty_on_error, CTLFLAG_RW,
263     &nfs_keep_dirty_on_error, 0, "Retry pageout if error returned");
264 
265 /*
266  * This sysctl allows other processes to mmap a file that has been opened
267  * O_DIRECT by a process.  In general, having processes mmap the file while
268  * Direct IO is in progress can lead to Data Inconsistencies.  But, we allow
269  * this by default to prevent DoS attacks - to prevent a malicious user from
270  * opening up files O_DIRECT preventing other users from mmap'ing these
271  * files.  "Protected" environments where stricter consistency guarantees are
272  * required can disable this knob.  The process that opened the file O_DIRECT
273  * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
274  * meaningful.
275  */
276 int newnfs_directio_allow_mmap = 1;
277 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
278 	   &newnfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
279 
280 #define	NFSACCESS_ALL (NFSACCESS_READ | NFSACCESS_MODIFY		\
281 			 | NFSACCESS_EXTEND | NFSACCESS_EXECUTE	\
282 			 | NFSACCESS_DELETE | NFSACCESS_LOOKUP)
283 
284 /*
285  * SMP Locking Note :
286  * The list of locks after the description of the lock is the ordering
287  * of other locks acquired with the lock held.
288  * np->n_mtx : Protects the fields in the nfsnode.
289        VM Object Lock
290        VI_MTX (acquired indirectly)
291  * nmp->nm_mtx : Protects the fields in the nfsmount.
292        rep->r_mtx
293  * ncl_iod_mutex : Global lock, protects shared nfsiod state.
294  * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
295        nmp->nm_mtx
296        rep->r_mtx
297  * rep->r_mtx : Protects the fields in an nfsreq.
298  */
299 
300 static int
301 nfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
302     struct ucred *cred, u_int32_t *retmode)
303 {
304 	int error = 0, attrflag, i, lrupos;
305 	u_int32_t rmode;
306 	struct nfsnode *np = VTONFS(vp);
307 	struct nfsvattr nfsva;
308 
309 	error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
310 	    &rmode, NULL);
311 	if (attrflag)
312 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
313 	if (!error) {
314 		lrupos = 0;
315 		mtx_lock(&np->n_mtx);
316 		for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
317 			if (np->n_accesscache[i].uid == cred->cr_uid) {
318 				np->n_accesscache[i].mode = rmode;
319 				np->n_accesscache[i].stamp = time_second;
320 				break;
321 			}
322 			if (i > 0 && np->n_accesscache[i].stamp <
323 			    np->n_accesscache[lrupos].stamp)
324 				lrupos = i;
325 		}
326 		if (i == NFS_ACCESSCACHESIZE) {
327 			np->n_accesscache[lrupos].uid = cred->cr_uid;
328 			np->n_accesscache[lrupos].mode = rmode;
329 			np->n_accesscache[lrupos].stamp = time_second;
330 		}
331 		mtx_unlock(&np->n_mtx);
332 		if (retmode != NULL)
333 			*retmode = rmode;
334 		KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, rmode, 0);
335 	} else if (NFS_ISV4(vp)) {
336 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
337 	}
338 #ifdef KDTRACE_HOOKS
339 	if (error != 0)
340 		KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, 0,
341 		    error);
342 #endif
343 	return (error);
344 }
345 
346 /*
347  * nfs access vnode op.
348  * For nfs version 2, just return ok. File accesses may fail later.
349  * For nfs version 3, use the access rpc to check accessibility. If file modes
350  * are changed on the server, accesses might still fail later.
351  */
352 static int
353 nfs_access(struct vop_access_args *ap)
354 {
355 	struct vnode *vp = ap->a_vp;
356 	int error = 0, i, gotahit;
357 	u_int32_t mode, wmode, rmode;
358 	int v34 = NFS_ISV34(vp);
359 	struct nfsnode *np = VTONFS(vp);
360 
361 	/*
362 	 * Disallow write attempts on filesystems mounted read-only;
363 	 * unless the file is a socket, fifo, or a block or character
364 	 * device resident on the filesystem.
365 	 */
366 	if ((ap->a_accmode & (VWRITE | VAPPEND | VWRITE_NAMED_ATTRS |
367 	    VDELETE_CHILD | VWRITE_ATTRIBUTES | VDELETE | VWRITE_ACL |
368 	    VWRITE_OWNER)) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
369 		switch (vp->v_type) {
370 		case VREG:
371 		case VDIR:
372 		case VLNK:
373 			return (EROFS);
374 		default:
375 			break;
376 		}
377 	}
378 	/*
379 	 * For nfs v3 or v4, check to see if we have done this recently, and if
380 	 * so return our cached result instead of making an ACCESS call.
381 	 * If not, do an access rpc, otherwise you are stuck emulating
382 	 * ufs_access() locally using the vattr. This may not be correct,
383 	 * since the server may apply other access criteria such as
384 	 * client uid-->server uid mapping that we do not know about.
385 	 */
386 	if (v34) {
387 		if (ap->a_accmode & VREAD)
388 			mode = NFSACCESS_READ;
389 		else
390 			mode = 0;
391 		if (vp->v_type != VDIR) {
392 			if (ap->a_accmode & VWRITE)
393 				mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
394 			if (ap->a_accmode & VAPPEND)
395 				mode |= NFSACCESS_EXTEND;
396 			if (ap->a_accmode & VEXEC)
397 				mode |= NFSACCESS_EXECUTE;
398 			if (ap->a_accmode & VDELETE)
399 				mode |= NFSACCESS_DELETE;
400 		} else {
401 			if (ap->a_accmode & VWRITE)
402 				mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
403 			if (ap->a_accmode & VAPPEND)
404 				mode |= NFSACCESS_EXTEND;
405 			if (ap->a_accmode & VEXEC)
406 				mode |= NFSACCESS_LOOKUP;
407 			if (ap->a_accmode & VDELETE)
408 				mode |= NFSACCESS_DELETE;
409 			if (ap->a_accmode & VDELETE_CHILD)
410 				mode |= NFSACCESS_MODIFY;
411 		}
412 		/* XXX safety belt, only make blanket request if caching */
413 		if (nfsaccess_cache_timeout > 0) {
414 			wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
415 				NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
416 				NFSACCESS_DELETE | NFSACCESS_LOOKUP;
417 		} else {
418 			wmode = mode;
419 		}
420 
421 		/*
422 		 * Does our cached result allow us to give a definite yes to
423 		 * this request?
424 		 */
425 		gotahit = 0;
426 		mtx_lock(&np->n_mtx);
427 		for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
428 			if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
429 			    if (time_second < (np->n_accesscache[i].stamp
430 				+ nfsaccess_cache_timeout) &&
431 				(np->n_accesscache[i].mode & mode) == mode) {
432 				NFSINCRGLOBAL(nfsstatsv1.accesscache_hits);
433 				gotahit = 1;
434 			    }
435 			    break;
436 			}
437 		}
438 		mtx_unlock(&np->n_mtx);
439 #ifdef KDTRACE_HOOKS
440 		if (gotahit != 0)
441 			KDTRACE_NFS_ACCESSCACHE_GET_HIT(vp,
442 			    ap->a_cred->cr_uid, mode);
443 		else
444 			KDTRACE_NFS_ACCESSCACHE_GET_MISS(vp,
445 			    ap->a_cred->cr_uid, mode);
446 #endif
447 		if (gotahit == 0) {
448 			/*
449 			 * Either a no, or a don't know.  Go to the wire.
450 			 */
451 			NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
452 		        error = nfs34_access_otw(vp, wmode, ap->a_td,
453 			    ap->a_cred, &rmode);
454 			if (!error &&
455 			    (rmode & mode) != mode)
456 				error = EACCES;
457 		}
458 		return (error);
459 	} else {
460 		if ((error = nfsspec_access(ap)) != 0) {
461 			return (error);
462 		}
463 		/*
464 		 * Attempt to prevent a mapped root from accessing a file
465 		 * which it shouldn't.  We try to read a byte from the file
466 		 * if the user is root and the file is not zero length.
467 		 * After calling nfsspec_access, we should have the correct
468 		 * file size cached.
469 		 */
470 		mtx_lock(&np->n_mtx);
471 		if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
472 		    && VTONFS(vp)->n_size > 0) {
473 			struct iovec aiov;
474 			struct uio auio;
475 			char buf[1];
476 
477 			mtx_unlock(&np->n_mtx);
478 			aiov.iov_base = buf;
479 			aiov.iov_len = 1;
480 			auio.uio_iov = &aiov;
481 			auio.uio_iovcnt = 1;
482 			auio.uio_offset = 0;
483 			auio.uio_resid = 1;
484 			auio.uio_segflg = UIO_SYSSPACE;
485 			auio.uio_rw = UIO_READ;
486 			auio.uio_td = ap->a_td;
487 
488 			if (vp->v_type == VREG)
489 				error = ncl_readrpc(vp, &auio, ap->a_cred);
490 			else if (vp->v_type == VDIR) {
491 				char* bp;
492 				bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
493 				aiov.iov_base = bp;
494 				aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
495 				error = ncl_readdirrpc(vp, &auio, ap->a_cred,
496 				    ap->a_td);
497 				free(bp, M_TEMP);
498 			} else if (vp->v_type == VLNK)
499 				error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
500 			else
501 				error = EACCES;
502 		} else
503 			mtx_unlock(&np->n_mtx);
504 		return (error);
505 	}
506 }
507 
508 
509 /*
510  * nfs open vnode op
511  * Check to see if the type is ok
512  * and that deletion is not in progress.
513  * For paged in text files, you will need to flush the page cache
514  * if consistency is lost.
515  */
516 /* ARGSUSED */
517 static int
518 nfs_open(struct vop_open_args *ap)
519 {
520 	struct vnode *vp = ap->a_vp;
521 	struct nfsnode *np = VTONFS(vp);
522 	struct vattr vattr;
523 	int error;
524 	int fmode = ap->a_mode;
525 	struct ucred *cred;
526 
527 	if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
528 		return (EOPNOTSUPP);
529 
530 	/*
531 	 * For NFSv4, we need to do the Open Op before cache validation,
532 	 * so that we conform to RFC3530 Sec. 9.3.1.
533 	 */
534 	if (NFS_ISV4(vp)) {
535 		error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
536 		if (error) {
537 			error = nfscl_maperr(ap->a_td, error, (uid_t)0,
538 			    (gid_t)0);
539 			return (error);
540 		}
541 	}
542 
543 	/*
544 	 * Now, if this Open will be doing reading, re-validate/flush the
545 	 * cache, so that Close/Open coherency is maintained.
546 	 */
547 	mtx_lock(&np->n_mtx);
548 	if (np->n_flag & NMODIFIED) {
549 		mtx_unlock(&np->n_mtx);
550 		error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
551 		if (error == EINTR || error == EIO) {
552 			if (NFS_ISV4(vp))
553 				(void) nfsrpc_close(vp, 0, ap->a_td);
554 			return (error);
555 		}
556 		mtx_lock(&np->n_mtx);
557 		np->n_attrstamp = 0;
558 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
559 		if (vp->v_type == VDIR)
560 			np->n_direofoffset = 0;
561 		mtx_unlock(&np->n_mtx);
562 		error = VOP_GETATTR(vp, &vattr, ap->a_cred);
563 		if (error) {
564 			if (NFS_ISV4(vp))
565 				(void) nfsrpc_close(vp, 0, ap->a_td);
566 			return (error);
567 		}
568 		mtx_lock(&np->n_mtx);
569 		np->n_mtime = vattr.va_mtime;
570 		if (NFS_ISV4(vp))
571 			np->n_change = vattr.va_filerev;
572 	} else {
573 		mtx_unlock(&np->n_mtx);
574 		error = VOP_GETATTR(vp, &vattr, ap->a_cred);
575 		if (error) {
576 			if (NFS_ISV4(vp))
577 				(void) nfsrpc_close(vp, 0, ap->a_td);
578 			return (error);
579 		}
580 		mtx_lock(&np->n_mtx);
581 		if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
582 		    NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
583 			if (vp->v_type == VDIR)
584 				np->n_direofoffset = 0;
585 			mtx_unlock(&np->n_mtx);
586 			error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
587 			if (error == EINTR || error == EIO) {
588 				if (NFS_ISV4(vp))
589 					(void) nfsrpc_close(vp, 0, ap->a_td);
590 				return (error);
591 			}
592 			mtx_lock(&np->n_mtx);
593 			np->n_mtime = vattr.va_mtime;
594 			if (NFS_ISV4(vp))
595 				np->n_change = vattr.va_filerev;
596 		}
597 	}
598 
599 	/*
600 	 * If the object has >= 1 O_DIRECT active opens, we disable caching.
601 	 */
602 	if (newnfs_directio_enable && (fmode & O_DIRECT) &&
603 	    (vp->v_type == VREG)) {
604 		if (np->n_directio_opens == 0) {
605 			mtx_unlock(&np->n_mtx);
606 			error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
607 			if (error) {
608 				if (NFS_ISV4(vp))
609 					(void) nfsrpc_close(vp, 0, ap->a_td);
610 				return (error);
611 			}
612 			mtx_lock(&np->n_mtx);
613 			np->n_flag |= NNONCACHE;
614 		}
615 		np->n_directio_opens++;
616 	}
617 
618 	/* If opened for writing via NFSv4.1 or later, mark that for pNFS. */
619 	if (NFSHASPNFS(VFSTONFS(vp->v_mount)) && (fmode & FWRITE) != 0)
620 		np->n_flag |= NWRITEOPENED;
621 
622 	/*
623 	 * If this is an open for writing, capture a reference to the
624 	 * credentials, so they can be used by ncl_putpages(). Using
625 	 * these write credentials is preferable to the credentials of
626 	 * whatever thread happens to be doing the VOP_PUTPAGES() since
627 	 * the write RPCs are less likely to fail with EACCES.
628 	 */
629 	if ((fmode & FWRITE) != 0) {
630 		cred = np->n_writecred;
631 		np->n_writecred = crhold(ap->a_cred);
632 	} else
633 		cred = NULL;
634 	mtx_unlock(&np->n_mtx);
635 
636 	if (cred != NULL)
637 		crfree(cred);
638 	vnode_create_vobject(vp, vattr.va_size, ap->a_td);
639 	return (0);
640 }
641 
642 /*
643  * nfs close vnode op
644  * What an NFS client should do upon close after writing is a debatable issue.
645  * Most NFS clients push delayed writes to the server upon close, basically for
646  * two reasons:
647  * 1 - So that any write errors may be reported back to the client process
648  *     doing the close system call. By far the two most likely errors are
649  *     NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
650  * 2 - To put a worst case upper bound on cache inconsistency between
651  *     multiple clients for the file.
652  * There is also a consistency problem for Version 2 of the protocol w.r.t.
653  * not being able to tell if other clients are writing a file concurrently,
654  * since there is no way of knowing if the changed modify time in the reply
655  * is only due to the write for this client.
656  * (NFS Version 3 provides weak cache consistency data in the reply that
657  *  should be sufficient to detect and handle this case.)
658  *
659  * The current code does the following:
660  * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
661  * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
662  *                     or commit them (this satisfies 1 and 2 except for the
663  *                     case where the server crashes after this close but
664  *                     before the commit RPC, which is felt to be "good
665  *                     enough". Changing the last argument to ncl_flush() to
666  *                     a 1 would force a commit operation, if it is felt a
667  *                     commit is necessary now.
668  * for NFS Version 4 - flush the dirty buffers and commit them, if
669  *		       nfscl_mustflush() says this is necessary.
670  *                     It is necessary if there is no write delegation held,
671  *                     in order to satisfy open/close coherency.
672  *                     If the file isn't cached on local stable storage,
673  *                     it may be necessary in order to detect "out of space"
674  *                     errors from the server, if the write delegation
675  *                     issued by the server doesn't allow the file to grow.
676  */
677 /* ARGSUSED */
678 static int
679 nfs_close(struct vop_close_args *ap)
680 {
681 	struct vnode *vp = ap->a_vp;
682 	struct nfsnode *np = VTONFS(vp);
683 	struct nfsvattr nfsva;
684 	struct ucred *cred;
685 	int error = 0, ret, localcred = 0;
686 	int fmode = ap->a_fflag;
687 
688 	if (NFSCL_FORCEDISM(vp->v_mount))
689 		return (0);
690 	/*
691 	 * During shutdown, a_cred isn't valid, so just use root.
692 	 */
693 	if (ap->a_cred == NOCRED) {
694 		cred = newnfs_getcred();
695 		localcred = 1;
696 	} else {
697 		cred = ap->a_cred;
698 	}
699 	if (vp->v_type == VREG) {
700 	    /*
701 	     * Examine and clean dirty pages, regardless of NMODIFIED.
702 	     * This closes a major hole in close-to-open consistency.
703 	     * We want to push out all dirty pages (and buffers) on
704 	     * close, regardless of whether they were dirtied by
705 	     * mmap'ed writes or via write().
706 	     */
707 	    if (nfs_clean_pages_on_close && vp->v_object) {
708 		VM_OBJECT_WLOCK(vp->v_object);
709 		vm_object_page_clean(vp->v_object, 0, 0, 0);
710 		VM_OBJECT_WUNLOCK(vp->v_object);
711 	    }
712 	    mtx_lock(&np->n_mtx);
713 	    if (np->n_flag & NMODIFIED) {
714 		mtx_unlock(&np->n_mtx);
715 		if (NFS_ISV3(vp)) {
716 		    /*
717 		     * Under NFSv3 we have dirty buffers to dispose of.  We
718 		     * must flush them to the NFS server.  We have the option
719 		     * of waiting all the way through the commit rpc or just
720 		     * waiting for the initial write.  The default is to only
721 		     * wait through the initial write so the data is in the
722 		     * server's cache, which is roughly similar to the state
723 		     * a standard disk subsystem leaves the file in on close().
724 		     *
725 		     * We cannot clear the NMODIFIED bit in np->n_flag due to
726 		     * potential races with other processes, and certainly
727 		     * cannot clear it if we don't commit.
728 		     * These races occur when there is no longer the old
729 		     * traditional vnode locking implemented for Vnode Ops.
730 		     */
731 		    int cm = newnfs_commit_on_close ? 1 : 0;
732 		    error = ncl_flush(vp, MNT_WAIT, ap->a_td, cm, 0);
733 		    /* np->n_flag &= ~NMODIFIED; */
734 		} else if (NFS_ISV4(vp)) {
735 			if (nfscl_mustflush(vp) != 0) {
736 				int cm = newnfs_commit_on_close ? 1 : 0;
737 				error = ncl_flush(vp, MNT_WAIT, ap->a_td,
738 				    cm, 0);
739 				/*
740 				 * as above w.r.t races when clearing
741 				 * NMODIFIED.
742 				 * np->n_flag &= ~NMODIFIED;
743 				 */
744 			}
745 		} else {
746 			error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
747 		}
748 		mtx_lock(&np->n_mtx);
749 	    }
750  	    /*
751  	     * Invalidate the attribute cache in all cases.
752  	     * An open is going to fetch fresh attrs any way, other procs
753  	     * on this node that have file open will be forced to do an
754  	     * otw attr fetch, but this is safe.
755 	     * --> A user found that their RPC count dropped by 20% when
756 	     *     this was commented out and I can't see any requirement
757 	     *     for it, so I've disabled it when negative lookups are
758 	     *     enabled. (What does this have to do with negative lookup
759 	     *     caching? Well nothing, except it was reported by the
760 	     *     same user that needed negative lookup caching and I wanted
761 	     *     there to be a way to disable it to see if it
762 	     *     is the cause of some caching/coherency issue that might
763 	     *     crop up.)
764  	     */
765 	    if (VFSTONFS(vp->v_mount)->nm_negnametimeo == 0) {
766 		    np->n_attrstamp = 0;
767 		    KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
768 	    }
769 	    if (np->n_flag & NWRITEERR) {
770 		np->n_flag &= ~NWRITEERR;
771 		error = np->n_error;
772 	    }
773 	    mtx_unlock(&np->n_mtx);
774 	}
775 
776 	if (NFS_ISV4(vp)) {
777 		/*
778 		 * Get attributes so "change" is up to date.
779 		 */
780 		if (error == 0 && nfscl_mustflush(vp) != 0 &&
781 		    vp->v_type == VREG &&
782 		    (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOCTO) == 0) {
783 			ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva,
784 			    NULL);
785 			if (!ret) {
786 				np->n_change = nfsva.na_filerev;
787 				(void) nfscl_loadattrcache(&vp, &nfsva, NULL,
788 				    NULL, 0, 0);
789 			}
790 		}
791 
792 		/*
793 		 * and do the close.
794 		 */
795 		ret = nfsrpc_close(vp, 0, ap->a_td);
796 		if (!error && ret)
797 			error = ret;
798 		if (error)
799 			error = nfscl_maperr(ap->a_td, error, (uid_t)0,
800 			    (gid_t)0);
801 	}
802 	if (newnfs_directio_enable)
803 		KASSERT((np->n_directio_asyncwr == 0),
804 			("nfs_close: dirty unflushed (%d) directio buffers\n",
805 			 np->n_directio_asyncwr));
806 	if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
807 		mtx_lock(&np->n_mtx);
808 		KASSERT((np->n_directio_opens > 0),
809 			("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
810 		np->n_directio_opens--;
811 		if (np->n_directio_opens == 0)
812 			np->n_flag &= ~NNONCACHE;
813 		mtx_unlock(&np->n_mtx);
814 	}
815 	if (localcred)
816 		NFSFREECRED(cred);
817 	return (error);
818 }
819 
820 /*
821  * nfs getattr call from vfs.
822  */
823 static int
824 nfs_getattr(struct vop_getattr_args *ap)
825 {
826 	struct vnode *vp = ap->a_vp;
827 	struct thread *td = curthread;	/* XXX */
828 	struct nfsnode *np = VTONFS(vp);
829 	int error = 0;
830 	struct nfsvattr nfsva;
831 	struct vattr *vap = ap->a_vap;
832 	struct vattr vattr;
833 
834 	/*
835 	 * Update local times for special files.
836 	 */
837 	mtx_lock(&np->n_mtx);
838 	if (np->n_flag & (NACC | NUPD))
839 		np->n_flag |= NCHG;
840 	mtx_unlock(&np->n_mtx);
841 	/*
842 	 * First look in the cache.
843 	 */
844 	if (ncl_getattrcache(vp, &vattr) == 0) {
845 		vap->va_type = vattr.va_type;
846 		vap->va_mode = vattr.va_mode;
847 		vap->va_nlink = vattr.va_nlink;
848 		vap->va_uid = vattr.va_uid;
849 		vap->va_gid = vattr.va_gid;
850 		vap->va_fsid = vattr.va_fsid;
851 		vap->va_fileid = vattr.va_fileid;
852 		vap->va_size = vattr.va_size;
853 		vap->va_blocksize = vattr.va_blocksize;
854 		vap->va_atime = vattr.va_atime;
855 		vap->va_mtime = vattr.va_mtime;
856 		vap->va_ctime = vattr.va_ctime;
857 		vap->va_gen = vattr.va_gen;
858 		vap->va_flags = vattr.va_flags;
859 		vap->va_rdev = vattr.va_rdev;
860 		vap->va_bytes = vattr.va_bytes;
861 		vap->va_filerev = vattr.va_filerev;
862 		/*
863 		 * Get the local modify time for the case of a write
864 		 * delegation.
865 		 */
866 		nfscl_deleggetmodtime(vp, &vap->va_mtime);
867 		return (0);
868 	}
869 
870 	if (NFS_ISV34(vp) && nfs_prime_access_cache &&
871 	    nfsaccess_cache_timeout > 0) {
872 		NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
873 		nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
874 		if (ncl_getattrcache(vp, ap->a_vap) == 0) {
875 			nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
876 			return (0);
877 		}
878 	}
879 	error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva, NULL);
880 	if (!error)
881 		error = nfscl_loadattrcache(&vp, &nfsva, vap, NULL, 0, 0);
882 	if (!error) {
883 		/*
884 		 * Get the local modify time for the case of a write
885 		 * delegation.
886 		 */
887 		nfscl_deleggetmodtime(vp, &vap->va_mtime);
888 	} else if (NFS_ISV4(vp)) {
889 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
890 	}
891 	return (error);
892 }
893 
894 /*
895  * nfs setattr call.
896  */
897 static int
898 nfs_setattr(struct vop_setattr_args *ap)
899 {
900 	struct vnode *vp = ap->a_vp;
901 	struct nfsnode *np = VTONFS(vp);
902 	struct thread *td = curthread;	/* XXX */
903 	struct vattr *vap = ap->a_vap;
904 	int error = 0;
905 	u_quad_t tsize;
906 
907 #ifndef nolint
908 	tsize = (u_quad_t)0;
909 #endif
910 
911 	/*
912 	 * Setting of flags and marking of atimes are not supported.
913 	 */
914 	if (vap->va_flags != VNOVAL)
915 		return (EOPNOTSUPP);
916 
917 	/*
918 	 * Disallow write attempts if the filesystem is mounted read-only.
919 	 */
920   	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
921 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
922 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
923 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
924 		return (EROFS);
925 	if (vap->va_size != VNOVAL) {
926  		switch (vp->v_type) {
927  		case VDIR:
928  			return (EISDIR);
929  		case VCHR:
930  		case VBLK:
931  		case VSOCK:
932  		case VFIFO:
933 			if (vap->va_mtime.tv_sec == VNOVAL &&
934 			    vap->va_atime.tv_sec == VNOVAL &&
935 			    vap->va_mode == (mode_t)VNOVAL &&
936 			    vap->va_uid == (uid_t)VNOVAL &&
937 			    vap->va_gid == (gid_t)VNOVAL)
938 				return (0);
939  			vap->va_size = VNOVAL;
940  			break;
941  		default:
942 			/*
943 			 * Disallow write attempts if the filesystem is
944 			 * mounted read-only.
945 			 */
946 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
947 				return (EROFS);
948 			/*
949 			 *  We run vnode_pager_setsize() early (why?),
950 			 * we must set np->n_size now to avoid vinvalbuf
951 			 * V_SAVE races that might setsize a lower
952 			 * value.
953 			 */
954 			mtx_lock(&np->n_mtx);
955 			tsize = np->n_size;
956 			mtx_unlock(&np->n_mtx);
957 			error = ncl_meta_setsize(vp, ap->a_cred, td,
958 			    vap->va_size);
959 			mtx_lock(&np->n_mtx);
960  			if (np->n_flag & NMODIFIED) {
961 			    tsize = np->n_size;
962 			    mtx_unlock(&np->n_mtx);
963 			    error = ncl_vinvalbuf(vp, vap->va_size == 0 ?
964 			        0 : V_SAVE, td, 1);
965 			    if (error != 0) {
966 				    vnode_pager_setsize(vp, tsize);
967 				    return (error);
968 			    }
969 			    /*
970 			     * Call nfscl_delegmodtime() to set the modify time
971 			     * locally, as required.
972 			     */
973 			    nfscl_delegmodtime(vp);
974  			} else
975 			    mtx_unlock(&np->n_mtx);
976 			/*
977 			 * np->n_size has already been set to vap->va_size
978 			 * in ncl_meta_setsize(). We must set it again since
979 			 * nfs_loadattrcache() could be called through
980 			 * ncl_meta_setsize() and could modify np->n_size.
981 			 */
982 			mtx_lock(&np->n_mtx);
983  			np->n_vattr.na_size = np->n_size = vap->va_size;
984 			mtx_unlock(&np->n_mtx);
985   		}
986   	} else {
987 		mtx_lock(&np->n_mtx);
988 		if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
989 		    (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
990 			mtx_unlock(&np->n_mtx);
991 			error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
992 			if (error == EINTR || error == EIO)
993 				return (error);
994 		} else
995 			mtx_unlock(&np->n_mtx);
996 	}
997 	error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
998 	if (error && vap->va_size != VNOVAL) {
999 		mtx_lock(&np->n_mtx);
1000 		np->n_size = np->n_vattr.na_size = tsize;
1001 		vnode_pager_setsize(vp, tsize);
1002 		mtx_unlock(&np->n_mtx);
1003 	}
1004 	return (error);
1005 }
1006 
1007 /*
1008  * Do an nfs setattr rpc.
1009  */
1010 static int
1011 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
1012     struct thread *td)
1013 {
1014 	struct nfsnode *np = VTONFS(vp);
1015 	int error, ret, attrflag, i;
1016 	struct nfsvattr nfsva;
1017 
1018 	if (NFS_ISV34(vp)) {
1019 		mtx_lock(&np->n_mtx);
1020 		for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
1021 			np->n_accesscache[i].stamp = 0;
1022 		np->n_flag |= NDELEGMOD;
1023 		mtx_unlock(&np->n_mtx);
1024 		KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1025 	}
1026 	error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag,
1027 	    NULL);
1028 	if (attrflag) {
1029 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1030 		if (ret && !error)
1031 			error = ret;
1032 	}
1033 	if (error && NFS_ISV4(vp))
1034 		error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1035 	return (error);
1036 }
1037 
1038 /*
1039  * nfs lookup call, one step at a time...
1040  * First look in cache
1041  * If not found, unlock the directory nfsnode and do the rpc
1042  */
1043 static int
1044 nfs_lookup(struct vop_lookup_args *ap)
1045 {
1046 	struct componentname *cnp = ap->a_cnp;
1047 	struct vnode *dvp = ap->a_dvp;
1048 	struct vnode **vpp = ap->a_vpp;
1049 	struct mount *mp = dvp->v_mount;
1050 	int flags = cnp->cn_flags;
1051 	struct vnode *newvp;
1052 	struct nfsmount *nmp;
1053 	struct nfsnode *np, *newnp;
1054 	int error = 0, attrflag, dattrflag, ltype, ncticks;
1055 	struct thread *td = cnp->cn_thread;
1056 	struct nfsfh *nfhp;
1057 	struct nfsvattr dnfsva, nfsva;
1058 	struct vattr vattr;
1059 	struct timespec nctime;
1060 
1061 	*vpp = NULLVP;
1062 	if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1063 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1064 		return (EROFS);
1065 	if (dvp->v_type != VDIR)
1066 		return (ENOTDIR);
1067 	nmp = VFSTONFS(mp);
1068 	np = VTONFS(dvp);
1069 
1070 	/* For NFSv4, wait until any remove is done. */
1071 	mtx_lock(&np->n_mtx);
1072 	while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1073 		np->n_flag |= NREMOVEWANT;
1074 		(void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1075 	}
1076 	mtx_unlock(&np->n_mtx);
1077 
1078 	if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0)
1079 		return (error);
1080 	error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1081 	if (error > 0 && error != ENOENT)
1082 		return (error);
1083 	if (error == -1) {
1084 		/*
1085 		 * Lookups of "." are special and always return the
1086 		 * current directory.  cache_lookup() already handles
1087 		 * associated locking bookkeeping, etc.
1088 		 */
1089 		if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1090 			/* XXX: Is this really correct? */
1091 			if (cnp->cn_nameiop != LOOKUP &&
1092 			    (flags & ISLASTCN))
1093 				cnp->cn_flags |= SAVENAME;
1094 			return (0);
1095 		}
1096 
1097 		/*
1098 		 * We only accept a positive hit in the cache if the
1099 		 * change time of the file matches our cached copy.
1100 		 * Otherwise, we discard the cache entry and fallback
1101 		 * to doing a lookup RPC.  We also only trust cache
1102 		 * entries for less than nm_nametimeo seconds.
1103 		 *
1104 		 * To better handle stale file handles and attributes,
1105 		 * clear the attribute cache of this node if it is a
1106 		 * leaf component, part of an open() call, and not
1107 		 * locally modified before fetching the attributes.
1108 		 * This should allow stale file handles to be detected
1109 		 * here where we can fall back to a LOOKUP RPC to
1110 		 * recover rather than having nfs_open() detect the
1111 		 * stale file handle and failing open(2) with ESTALE.
1112 		 */
1113 		newvp = *vpp;
1114 		newnp = VTONFS(newvp);
1115 		if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1116 		    (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1117 		    !(newnp->n_flag & NMODIFIED)) {
1118 			mtx_lock(&newnp->n_mtx);
1119 			newnp->n_attrstamp = 0;
1120 			KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1121 			mtx_unlock(&newnp->n_mtx);
1122 		}
1123 		if (nfscl_nodeleg(newvp, 0) == 0 ||
1124 		    ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
1125 		    VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1126 		    timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1127 			NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1128 			if (cnp->cn_nameiop != LOOKUP &&
1129 			    (flags & ISLASTCN))
1130 				cnp->cn_flags |= SAVENAME;
1131 			return (0);
1132 		}
1133 		cache_purge(newvp);
1134 		if (dvp != newvp)
1135 			vput(newvp);
1136 		else
1137 			vrele(newvp);
1138 		*vpp = NULLVP;
1139 	} else if (error == ENOENT) {
1140 		if (dvp->v_iflag & VI_DOOMED)
1141 			return (ENOENT);
1142 		/*
1143 		 * We only accept a negative hit in the cache if the
1144 		 * modification time of the parent directory matches
1145 		 * the cached copy in the name cache entry.
1146 		 * Otherwise, we discard all of the negative cache
1147 		 * entries for this directory.  We also only trust
1148 		 * negative cache entries for up to nm_negnametimeo
1149 		 * seconds.
1150 		 */
1151 		if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
1152 		    VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1153 		    timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1154 			NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1155 			return (ENOENT);
1156 		}
1157 		cache_purge_negative(dvp);
1158 	}
1159 
1160 	error = 0;
1161 	newvp = NULLVP;
1162 	NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1163 	error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1164 	    cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1165 	    NULL);
1166 	if (dattrflag)
1167 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1168 	if (error) {
1169 		if (newvp != NULLVP) {
1170 			vput(newvp);
1171 			*vpp = NULLVP;
1172 		}
1173 
1174 		if (error != ENOENT) {
1175 			if (NFS_ISV4(dvp))
1176 				error = nfscl_maperr(td, error, (uid_t)0,
1177 				    (gid_t)0);
1178 			return (error);
1179 		}
1180 
1181 		/* The requested file was not found. */
1182 		if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1183 		    (flags & ISLASTCN)) {
1184 			/*
1185 			 * XXX: UFS does a full VOP_ACCESS(dvp,
1186 			 * VWRITE) here instead of just checking
1187 			 * MNT_RDONLY.
1188 			 */
1189 			if (mp->mnt_flag & MNT_RDONLY)
1190 				return (EROFS);
1191 			cnp->cn_flags |= SAVENAME;
1192 			return (EJUSTRETURN);
1193 		}
1194 
1195 		if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1196 			/*
1197 			 * Cache the modification time of the parent
1198 			 * directory from the post-op attributes in
1199 			 * the name cache entry.  The negative cache
1200 			 * entry will be ignored once the directory
1201 			 * has changed.  Don't bother adding the entry
1202 			 * if the directory has already changed.
1203 			 */
1204 			mtx_lock(&np->n_mtx);
1205 			if (timespeccmp(&np->n_vattr.na_mtime,
1206 			    &dnfsva.na_mtime, ==)) {
1207 				mtx_unlock(&np->n_mtx);
1208 				cache_enter_time(dvp, NULL, cnp,
1209 				    &dnfsva.na_mtime, NULL);
1210 			} else
1211 				mtx_unlock(&np->n_mtx);
1212 		}
1213 		return (ENOENT);
1214 	}
1215 
1216 	/*
1217 	 * Handle RENAME case...
1218 	 */
1219 	if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1220 		if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1221 			free(nfhp, M_NFSFH);
1222 			return (EISDIR);
1223 		}
1224 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1225 		    LK_EXCLUSIVE);
1226 		if (error)
1227 			return (error);
1228 		newvp = NFSTOV(np);
1229 		if (attrflag)
1230 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1231 			    0, 1);
1232 		*vpp = newvp;
1233 		cnp->cn_flags |= SAVENAME;
1234 		return (0);
1235 	}
1236 
1237 	if (flags & ISDOTDOT) {
1238 		ltype = NFSVOPISLOCKED(dvp);
1239 		error = vfs_busy(mp, MBF_NOWAIT);
1240 		if (error != 0) {
1241 			vfs_ref(mp);
1242 			NFSVOPUNLOCK(dvp, 0);
1243 			error = vfs_busy(mp, 0);
1244 			NFSVOPLOCK(dvp, ltype | LK_RETRY);
1245 			vfs_rel(mp);
1246 			if (error == 0 && (dvp->v_iflag & VI_DOOMED)) {
1247 				vfs_unbusy(mp);
1248 				error = ENOENT;
1249 			}
1250 			if (error != 0)
1251 				return (error);
1252 		}
1253 		NFSVOPUNLOCK(dvp, 0);
1254 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1255 		    cnp->cn_lkflags);
1256 		if (error == 0)
1257 			newvp = NFSTOV(np);
1258 		vfs_unbusy(mp);
1259 		if (newvp != dvp)
1260 			NFSVOPLOCK(dvp, ltype | LK_RETRY);
1261 		if (dvp->v_iflag & VI_DOOMED) {
1262 			if (error == 0) {
1263 				if (newvp == dvp)
1264 					vrele(newvp);
1265 				else
1266 					vput(newvp);
1267 			}
1268 			error = ENOENT;
1269 		}
1270 		if (error != 0)
1271 			return (error);
1272 		if (attrflag)
1273 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1274 			    0, 1);
1275 	} else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1276 		free(nfhp, M_NFSFH);
1277 		VREF(dvp);
1278 		newvp = dvp;
1279 		if (attrflag)
1280 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1281 			    0, 1);
1282 	} else {
1283 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1284 		    cnp->cn_lkflags);
1285 		if (error)
1286 			return (error);
1287 		newvp = NFSTOV(np);
1288 		if (attrflag)
1289 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1290 			    0, 1);
1291 		else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1292 		    !(np->n_flag & NMODIFIED)) {
1293 			/*
1294 			 * Flush the attribute cache when opening a
1295 			 * leaf node to ensure that fresh attributes
1296 			 * are fetched in nfs_open() since we did not
1297 			 * fetch attributes from the LOOKUP reply.
1298 			 */
1299 			mtx_lock(&np->n_mtx);
1300 			np->n_attrstamp = 0;
1301 			KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1302 			mtx_unlock(&np->n_mtx);
1303 		}
1304 	}
1305 	if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1306 		cnp->cn_flags |= SAVENAME;
1307 	if ((cnp->cn_flags & MAKEENTRY) &&
1308 	    (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1309 	    attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1310 		cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1311 		    newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1312 	*vpp = newvp;
1313 	return (0);
1314 }
1315 
1316 /*
1317  * nfs read call.
1318  * Just call ncl_bioread() to do the work.
1319  */
1320 static int
1321 nfs_read(struct vop_read_args *ap)
1322 {
1323 	struct vnode *vp = ap->a_vp;
1324 
1325 	switch (vp->v_type) {
1326 	case VREG:
1327 		return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1328 	case VDIR:
1329 		return (EISDIR);
1330 	default:
1331 		return (EOPNOTSUPP);
1332 	}
1333 }
1334 
1335 /*
1336  * nfs readlink call
1337  */
1338 static int
1339 nfs_readlink(struct vop_readlink_args *ap)
1340 {
1341 	struct vnode *vp = ap->a_vp;
1342 
1343 	if (vp->v_type != VLNK)
1344 		return (EINVAL);
1345 	return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1346 }
1347 
1348 /*
1349  * Do a readlink rpc.
1350  * Called by ncl_doio() from below the buffer cache.
1351  */
1352 int
1353 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1354 {
1355 	int error, ret, attrflag;
1356 	struct nfsvattr nfsva;
1357 
1358 	error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1359 	    &attrflag, NULL);
1360 	if (attrflag) {
1361 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1362 		if (ret && !error)
1363 			error = ret;
1364 	}
1365 	if (error && NFS_ISV4(vp))
1366 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1367 	return (error);
1368 }
1369 
1370 /*
1371  * nfs read rpc call
1372  * Ditto above
1373  */
1374 int
1375 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1376 {
1377 	int error, ret, attrflag;
1378 	struct nfsvattr nfsva;
1379 	struct nfsmount *nmp;
1380 
1381 	nmp = VFSTONFS(vnode_mount(vp));
1382 	error = EIO;
1383 	attrflag = 0;
1384 	if (NFSHASPNFS(nmp))
1385 		error = nfscl_doiods(vp, uiop, NULL, NULL,
1386 		    NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td);
1387 	NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1388 	if (error != 0)
1389 		error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1390 		    &attrflag, NULL);
1391 	if (attrflag) {
1392 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1393 		if (ret && !error)
1394 			error = ret;
1395 	}
1396 	if (error && NFS_ISV4(vp))
1397 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1398 	return (error);
1399 }
1400 
1401 /*
1402  * nfs write call
1403  */
1404 int
1405 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1406     int *iomode, int *must_commit, int called_from_strategy)
1407 {
1408 	struct nfsvattr nfsva;
1409 	int error, attrflag, ret;
1410 	struct nfsmount *nmp;
1411 
1412 	nmp = VFSTONFS(vnode_mount(vp));
1413 	error = EIO;
1414 	attrflag = 0;
1415 	if (NFSHASPNFS(nmp))
1416 		error = nfscl_doiods(vp, uiop, iomode, must_commit,
1417 		    NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td);
1418 	NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1419 	if (error != 0)
1420 		error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1421 		    uiop->uio_td, &nfsva, &attrflag, NULL,
1422 		    called_from_strategy);
1423 	if (attrflag) {
1424 		if (VTONFS(vp)->n_flag & ND_NFSV4)
1425 			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
1426 			    1);
1427 		else
1428 			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
1429 			    1);
1430 		if (ret && !error)
1431 			error = ret;
1432 	}
1433 	if (DOINGASYNC(vp))
1434 		*iomode = NFSWRITE_FILESYNC;
1435 	if (error && NFS_ISV4(vp))
1436 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1437 	return (error);
1438 }
1439 
1440 /*
1441  * nfs mknod rpc
1442  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1443  * mode set to specify the file type and the size field for rdev.
1444  */
1445 static int
1446 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1447     struct vattr *vap)
1448 {
1449 	struct nfsvattr nfsva, dnfsva;
1450 	struct vnode *newvp = NULL;
1451 	struct nfsnode *np = NULL, *dnp;
1452 	struct nfsfh *nfhp;
1453 	struct vattr vattr;
1454 	int error = 0, attrflag, dattrflag;
1455 	u_int32_t rdev;
1456 
1457 	if (vap->va_type == VCHR || vap->va_type == VBLK)
1458 		rdev = vap->va_rdev;
1459 	else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1460 		rdev = 0xffffffff;
1461 	else
1462 		return (EOPNOTSUPP);
1463 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1464 		return (error);
1465 	error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1466 	    rdev, vap->va_type, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1467 	    &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1468 	if (!error) {
1469 		if (!nfhp)
1470 			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1471 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1472 			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1473 			    NULL);
1474 		if (nfhp)
1475 			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1476 			    cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1477 	}
1478 	if (dattrflag)
1479 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1480 	if (!error) {
1481 		newvp = NFSTOV(np);
1482 		if (attrflag != 0) {
1483 			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1484 			    0, 1);
1485 			if (error != 0)
1486 				vput(newvp);
1487 		}
1488 	}
1489 	if (!error) {
1490 		*vpp = newvp;
1491 	} else if (NFS_ISV4(dvp)) {
1492 		error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1493 		    vap->va_gid);
1494 	}
1495 	dnp = VTONFS(dvp);
1496 	mtx_lock(&dnp->n_mtx);
1497 	dnp->n_flag |= NMODIFIED;
1498 	if (!dattrflag) {
1499 		dnp->n_attrstamp = 0;
1500 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1501 	}
1502 	mtx_unlock(&dnp->n_mtx);
1503 	return (error);
1504 }
1505 
1506 /*
1507  * nfs mknod vop
1508  * just call nfs_mknodrpc() to do the work.
1509  */
1510 /* ARGSUSED */
1511 static int
1512 nfs_mknod(struct vop_mknod_args *ap)
1513 {
1514 	return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1515 }
1516 
1517 static struct mtx nfs_cverf_mtx;
1518 MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1519     MTX_DEF);
1520 
1521 static nfsquad_t
1522 nfs_get_cverf(void)
1523 {
1524 	static nfsquad_t cverf;
1525 	nfsquad_t ret;
1526 	static int cverf_initialized = 0;
1527 
1528 	mtx_lock(&nfs_cverf_mtx);
1529 	if (cverf_initialized == 0) {
1530 		cverf.lval[0] = arc4random();
1531 		cverf.lval[1] = arc4random();
1532 		cverf_initialized = 1;
1533 	} else
1534 		cverf.qval++;
1535 	ret = cverf;
1536 	mtx_unlock(&nfs_cverf_mtx);
1537 
1538 	return (ret);
1539 }
1540 
1541 /*
1542  * nfs file create call
1543  */
1544 static int
1545 nfs_create(struct vop_create_args *ap)
1546 {
1547 	struct vnode *dvp = ap->a_dvp;
1548 	struct vattr *vap = ap->a_vap;
1549 	struct componentname *cnp = ap->a_cnp;
1550 	struct nfsnode *np = NULL, *dnp;
1551 	struct vnode *newvp = NULL;
1552 	struct nfsmount *nmp;
1553 	struct nfsvattr dnfsva, nfsva;
1554 	struct nfsfh *nfhp;
1555 	nfsquad_t cverf;
1556 	int error = 0, attrflag, dattrflag, fmode = 0;
1557 	struct vattr vattr;
1558 
1559 	/*
1560 	 * Oops, not for me..
1561 	 */
1562 	if (vap->va_type == VSOCK)
1563 		return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1564 
1565 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1566 		return (error);
1567 	if (vap->va_vaflags & VA_EXCLUSIVE)
1568 		fmode |= O_EXCL;
1569 	dnp = VTONFS(dvp);
1570 	nmp = VFSTONFS(vnode_mount(dvp));
1571 again:
1572 	/* For NFSv4, wait until any remove is done. */
1573 	mtx_lock(&dnp->n_mtx);
1574 	while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1575 		dnp->n_flag |= NREMOVEWANT;
1576 		(void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1577 	}
1578 	mtx_unlock(&dnp->n_mtx);
1579 
1580 	cverf = nfs_get_cverf();
1581 	error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1582 	    vap, cverf, fmode, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva,
1583 	    &nfhp, &attrflag, &dattrflag, NULL);
1584 	if (!error) {
1585 		if (nfhp == NULL)
1586 			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1587 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1588 			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1589 			    NULL);
1590 		if (nfhp != NULL)
1591 			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1592 			    cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1593 	}
1594 	if (dattrflag)
1595 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1596 	if (!error) {
1597 		newvp = NFSTOV(np);
1598 		if (attrflag == 0)
1599 			error = nfsrpc_getattr(newvp, cnp->cn_cred,
1600 			    cnp->cn_thread, &nfsva, NULL);
1601 		if (error == 0)
1602 			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1603 			    0, 1);
1604 	}
1605 	if (error) {
1606 		if (newvp != NULL) {
1607 			vput(newvp);
1608 			newvp = NULL;
1609 		}
1610 		if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1611 		    error == NFSERR_NOTSUPP) {
1612 			fmode &= ~O_EXCL;
1613 			goto again;
1614 		}
1615 	} else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1616 		if (nfscl_checksattr(vap, &nfsva)) {
1617 			error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1618 			    cnp->cn_thread, &nfsva, &attrflag, NULL);
1619 			if (error && (vap->va_uid != (uid_t)VNOVAL ||
1620 			    vap->va_gid != (gid_t)VNOVAL)) {
1621 				/* try again without setting uid/gid */
1622 				vap->va_uid = (uid_t)VNOVAL;
1623 				vap->va_gid = (uid_t)VNOVAL;
1624 				error = nfsrpc_setattr(newvp, vap, NULL,
1625 				    cnp->cn_cred, cnp->cn_thread, &nfsva,
1626 				    &attrflag, NULL);
1627 			}
1628 			if (attrflag)
1629 				(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1630 				    NULL, 0, 1);
1631 			if (error != 0)
1632 				vput(newvp);
1633 		}
1634 	}
1635 	if (!error) {
1636 		if ((cnp->cn_flags & MAKEENTRY) && attrflag)
1637 			cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1638 			    NULL);
1639 		*ap->a_vpp = newvp;
1640 	} else if (NFS_ISV4(dvp)) {
1641 		error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1642 		    vap->va_gid);
1643 	}
1644 	mtx_lock(&dnp->n_mtx);
1645 	dnp->n_flag |= NMODIFIED;
1646 	if (!dattrflag) {
1647 		dnp->n_attrstamp = 0;
1648 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1649 	}
1650 	mtx_unlock(&dnp->n_mtx);
1651 	return (error);
1652 }
1653 
1654 /*
1655  * nfs file remove call
1656  * To try and make nfs semantics closer to ufs semantics, a file that has
1657  * other processes using the vnode is renamed instead of removed and then
1658  * removed later on the last close.
1659  * - If v_usecount > 1
1660  *	  If a rename is not already in the works
1661  *	     call nfs_sillyrename() to set it up
1662  *     else
1663  *	  do the remove rpc
1664  */
1665 static int
1666 nfs_remove(struct vop_remove_args *ap)
1667 {
1668 	struct vnode *vp = ap->a_vp;
1669 	struct vnode *dvp = ap->a_dvp;
1670 	struct componentname *cnp = ap->a_cnp;
1671 	struct nfsnode *np = VTONFS(vp);
1672 	int error = 0;
1673 	struct vattr vattr;
1674 
1675 	KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1676 	KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1677 	if (vp->v_type == VDIR)
1678 		error = EPERM;
1679 	else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1680 	    VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1681 	    vattr.va_nlink > 1)) {
1682 		/*
1683 		 * Purge the name cache so that the chance of a lookup for
1684 		 * the name succeeding while the remove is in progress is
1685 		 * minimized. Without node locking it can still happen, such
1686 		 * that an I/O op returns ESTALE, but since you get this if
1687 		 * another host removes the file..
1688 		 */
1689 		cache_purge(vp);
1690 		/*
1691 		 * throw away biocache buffers, mainly to avoid
1692 		 * unnecessary delayed writes later.
1693 		 */
1694 		error = ncl_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1695 		if (error != EINTR && error != EIO)
1696 			/* Do the rpc */
1697 			error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1698 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1699 		/*
1700 		 * Kludge City: If the first reply to the remove rpc is lost..
1701 		 *   the reply to the retransmitted request will be ENOENT
1702 		 *   since the file was in fact removed
1703 		 *   Therefore, we cheat and return success.
1704 		 */
1705 		if (error == ENOENT)
1706 			error = 0;
1707 	} else if (!np->n_sillyrename)
1708 		error = nfs_sillyrename(dvp, vp, cnp);
1709 	mtx_lock(&np->n_mtx);
1710 	np->n_attrstamp = 0;
1711 	mtx_unlock(&np->n_mtx);
1712 	KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1713 	return (error);
1714 }
1715 
1716 /*
1717  * nfs file remove rpc called from nfs_inactive
1718  */
1719 int
1720 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1721 {
1722 	/*
1723 	 * Make sure that the directory vnode is still valid.
1724 	 * XXX we should lock sp->s_dvp here.
1725 	 */
1726 	if (sp->s_dvp->v_type == VBAD)
1727 		return (0);
1728 	return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1729 	    sp->s_cred, NULL));
1730 }
1731 
1732 /*
1733  * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1734  */
1735 static int
1736 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1737     int namelen, struct ucred *cred, struct thread *td)
1738 {
1739 	struct nfsvattr dnfsva;
1740 	struct nfsnode *dnp = VTONFS(dvp);
1741 	int error = 0, dattrflag;
1742 
1743 	mtx_lock(&dnp->n_mtx);
1744 	dnp->n_flag |= NREMOVEINPROG;
1745 	mtx_unlock(&dnp->n_mtx);
1746 	error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1747 	    &dattrflag, NULL);
1748 	mtx_lock(&dnp->n_mtx);
1749 	if ((dnp->n_flag & NREMOVEWANT)) {
1750 		dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1751 		mtx_unlock(&dnp->n_mtx);
1752 		wakeup((caddr_t)dnp);
1753 	} else {
1754 		dnp->n_flag &= ~NREMOVEINPROG;
1755 		mtx_unlock(&dnp->n_mtx);
1756 	}
1757 	if (dattrflag)
1758 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1759 	mtx_lock(&dnp->n_mtx);
1760 	dnp->n_flag |= NMODIFIED;
1761 	if (!dattrflag) {
1762 		dnp->n_attrstamp = 0;
1763 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1764 	}
1765 	mtx_unlock(&dnp->n_mtx);
1766 	if (error && NFS_ISV4(dvp))
1767 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1768 	return (error);
1769 }
1770 
1771 /*
1772  * nfs file rename call
1773  */
1774 static int
1775 nfs_rename(struct vop_rename_args *ap)
1776 {
1777 	struct vnode *fvp = ap->a_fvp;
1778 	struct vnode *tvp = ap->a_tvp;
1779 	struct vnode *fdvp = ap->a_fdvp;
1780 	struct vnode *tdvp = ap->a_tdvp;
1781 	struct componentname *tcnp = ap->a_tcnp;
1782 	struct componentname *fcnp = ap->a_fcnp;
1783 	struct nfsnode *fnp = VTONFS(ap->a_fvp);
1784 	struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1785 	struct nfsv4node *newv4 = NULL;
1786 	int error;
1787 
1788 	KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1789 	    (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1790 	/* Check for cross-device rename */
1791 	if ((fvp->v_mount != tdvp->v_mount) ||
1792 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
1793 		error = EXDEV;
1794 		goto out;
1795 	}
1796 
1797 	if (fvp == tvp) {
1798 		printf("nfs_rename: fvp == tvp (can't happen)\n");
1799 		error = 0;
1800 		goto out;
1801 	}
1802 	if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
1803 		goto out;
1804 
1805 	/*
1806 	 * We have to flush B_DELWRI data prior to renaming
1807 	 * the file.  If we don't, the delayed-write buffers
1808 	 * can be flushed out later after the file has gone stale
1809 	 * under NFSV3.  NFSV2 does not have this problem because
1810 	 * ( as far as I can tell ) it flushes dirty buffers more
1811 	 * often.
1812 	 *
1813 	 * Skip the rename operation if the fsync fails, this can happen
1814 	 * due to the server's volume being full, when we pushed out data
1815 	 * that was written back to our cache earlier. Not checking for
1816 	 * this condition can result in potential (silent) data loss.
1817 	 */
1818 	error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1819 	NFSVOPUNLOCK(fvp, 0);
1820 	if (!error && tvp)
1821 		error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1822 	if (error)
1823 		goto out;
1824 
1825 	/*
1826 	 * If the tvp exists and is in use, sillyrename it before doing the
1827 	 * rename of the new file over it.
1828 	 * XXX Can't sillyrename a directory.
1829 	 */
1830 	if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1831 		tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1832 		vput(tvp);
1833 		tvp = NULL;
1834 	}
1835 
1836 	error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1837 	    tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1838 	    tcnp->cn_thread);
1839 
1840 	if (error == 0 && NFS_ISV4(tdvp)) {
1841 		/*
1842 		 * For NFSv4, check to see if it is the same name and
1843 		 * replace the name, if it is different.
1844 		 */
1845 		newv4 = malloc(
1846 		    sizeof (struct nfsv4node) +
1847 		    tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
1848 		    M_NFSV4NODE, M_WAITOK);
1849 		mtx_lock(&tdnp->n_mtx);
1850 		mtx_lock(&fnp->n_mtx);
1851 		if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
1852 		    (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
1853 		      NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
1854 		      tcnp->cn_namelen) ||
1855 		      tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
1856 		      NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1857 			tdnp->n_fhp->nfh_len))) {
1858 #ifdef notdef
1859 { char nnn[100]; int nnnl;
1860 nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
1861 bcopy(tcnp->cn_nameptr, nnn, nnnl);
1862 nnn[nnnl] = '\0';
1863 printf("ren replace=%s\n",nnn);
1864 }
1865 #endif
1866 			free(fnp->n_v4, M_NFSV4NODE);
1867 			fnp->n_v4 = newv4;
1868 			newv4 = NULL;
1869 			fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
1870 			fnp->n_v4->n4_namelen = tcnp->cn_namelen;
1871 			NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1872 			    tdnp->n_fhp->nfh_len);
1873 			NFSBCOPY(tcnp->cn_nameptr,
1874 			    NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
1875 		}
1876 		mtx_unlock(&tdnp->n_mtx);
1877 		mtx_unlock(&fnp->n_mtx);
1878 		if (newv4 != NULL)
1879 			free(newv4, M_NFSV4NODE);
1880 	}
1881 
1882 	if (fvp->v_type == VDIR) {
1883 		if (tvp != NULL && tvp->v_type == VDIR)
1884 			cache_purge(tdvp);
1885 		cache_purge(fdvp);
1886 	}
1887 
1888 out:
1889 	if (tdvp == tvp)
1890 		vrele(tdvp);
1891 	else
1892 		vput(tdvp);
1893 	if (tvp)
1894 		vput(tvp);
1895 	vrele(fdvp);
1896 	vrele(fvp);
1897 	/*
1898 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1899 	 */
1900 	if (error == ENOENT)
1901 		error = 0;
1902 	return (error);
1903 }
1904 
1905 /*
1906  * nfs file rename rpc called from nfs_remove() above
1907  */
1908 static int
1909 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
1910     struct sillyrename *sp)
1911 {
1912 
1913 	return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
1914 	    sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
1915 	    scnp->cn_thread));
1916 }
1917 
1918 /*
1919  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1920  */
1921 static int
1922 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
1923     int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
1924     int tnamelen, struct ucred *cred, struct thread *td)
1925 {
1926 	struct nfsvattr fnfsva, tnfsva;
1927 	struct nfsnode *fdnp = VTONFS(fdvp);
1928 	struct nfsnode *tdnp = VTONFS(tdvp);
1929 	int error = 0, fattrflag, tattrflag;
1930 
1931 	error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
1932 	    tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
1933 	    &tattrflag, NULL, NULL);
1934 	mtx_lock(&fdnp->n_mtx);
1935 	fdnp->n_flag |= NMODIFIED;
1936 	if (fattrflag != 0) {
1937 		mtx_unlock(&fdnp->n_mtx);
1938 		(void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
1939 	} else {
1940 		fdnp->n_attrstamp = 0;
1941 		mtx_unlock(&fdnp->n_mtx);
1942 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
1943 	}
1944 	mtx_lock(&tdnp->n_mtx);
1945 	tdnp->n_flag |= NMODIFIED;
1946 	if (tattrflag != 0) {
1947 		mtx_unlock(&tdnp->n_mtx);
1948 		(void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
1949 	} else {
1950 		tdnp->n_attrstamp = 0;
1951 		mtx_unlock(&tdnp->n_mtx);
1952 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1953 	}
1954 	if (error && NFS_ISV4(fdvp))
1955 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1956 	return (error);
1957 }
1958 
1959 /*
1960  * nfs hard link create call
1961  */
1962 static int
1963 nfs_link(struct vop_link_args *ap)
1964 {
1965 	struct vnode *vp = ap->a_vp;
1966 	struct vnode *tdvp = ap->a_tdvp;
1967 	struct componentname *cnp = ap->a_cnp;
1968 	struct nfsnode *np, *tdnp;
1969 	struct nfsvattr nfsva, dnfsva;
1970 	int error = 0, attrflag, dattrflag;
1971 
1972 	/*
1973 	 * Push all writes to the server, so that the attribute cache
1974 	 * doesn't get "out of sync" with the server.
1975 	 * XXX There should be a better way!
1976 	 */
1977 	VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1978 
1979 	error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
1980 	    cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &attrflag,
1981 	    &dattrflag, NULL);
1982 	tdnp = VTONFS(tdvp);
1983 	mtx_lock(&tdnp->n_mtx);
1984 	tdnp->n_flag |= NMODIFIED;
1985 	if (dattrflag != 0) {
1986 		mtx_unlock(&tdnp->n_mtx);
1987 		(void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
1988 	} else {
1989 		tdnp->n_attrstamp = 0;
1990 		mtx_unlock(&tdnp->n_mtx);
1991 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1992 	}
1993 	if (attrflag)
1994 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1995 	else {
1996 		np = VTONFS(vp);
1997 		mtx_lock(&np->n_mtx);
1998 		np->n_attrstamp = 0;
1999 		mtx_unlock(&np->n_mtx);
2000 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2001 	}
2002 	/*
2003 	 * If negative lookup caching is enabled, I might as well
2004 	 * add an entry for this node. Not necessary for correctness,
2005 	 * but if negative caching is enabled, then the system
2006 	 * must care about lookup caching hit rate, so...
2007 	 */
2008 	if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
2009 	    (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2010 		cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
2011 	}
2012 	if (error && NFS_ISV4(vp))
2013 		error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2014 		    (gid_t)0);
2015 	return (error);
2016 }
2017 
2018 /*
2019  * nfs symbolic link create call
2020  */
2021 static int
2022 nfs_symlink(struct vop_symlink_args *ap)
2023 {
2024 	struct vnode *dvp = ap->a_dvp;
2025 	struct vattr *vap = ap->a_vap;
2026 	struct componentname *cnp = ap->a_cnp;
2027 	struct nfsvattr nfsva, dnfsva;
2028 	struct nfsfh *nfhp;
2029 	struct nfsnode *np = NULL, *dnp;
2030 	struct vnode *newvp = NULL;
2031 	int error = 0, attrflag, dattrflag, ret;
2032 
2033 	vap->va_type = VLNK;
2034 	error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2035 	    ap->a_target, vap, cnp->cn_cred, cnp->cn_thread, &dnfsva,
2036 	    &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
2037 	if (nfhp) {
2038 		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2039 		    &np, NULL, LK_EXCLUSIVE);
2040 		if (!ret)
2041 			newvp = NFSTOV(np);
2042 		else if (!error)
2043 			error = ret;
2044 	}
2045 	if (newvp != NULL) {
2046 		if (attrflag)
2047 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2048 			    0, 1);
2049 	} else if (!error) {
2050 		/*
2051 		 * If we do not have an error and we could not extract the
2052 		 * newvp from the response due to the request being NFSv2, we
2053 		 * have to do a lookup in order to obtain a newvp to return.
2054 		 */
2055 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2056 		    cnp->cn_cred, cnp->cn_thread, &np);
2057 		if (!error)
2058 			newvp = NFSTOV(np);
2059 	}
2060 	if (error) {
2061 		if (newvp)
2062 			vput(newvp);
2063 		if (NFS_ISV4(dvp))
2064 			error = nfscl_maperr(cnp->cn_thread, error,
2065 			    vap->va_uid, vap->va_gid);
2066 	} else {
2067 		*ap->a_vpp = newvp;
2068 	}
2069 
2070 	dnp = VTONFS(dvp);
2071 	mtx_lock(&dnp->n_mtx);
2072 	dnp->n_flag |= NMODIFIED;
2073 	if (dattrflag != 0) {
2074 		mtx_unlock(&dnp->n_mtx);
2075 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2076 	} else {
2077 		dnp->n_attrstamp = 0;
2078 		mtx_unlock(&dnp->n_mtx);
2079 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2080 	}
2081 	/*
2082 	 * If negative lookup caching is enabled, I might as well
2083 	 * add an entry for this node. Not necessary for correctness,
2084 	 * but if negative caching is enabled, then the system
2085 	 * must care about lookup caching hit rate, so...
2086 	 */
2087 	if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2088 	    (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2089 		cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime, NULL);
2090 	}
2091 	return (error);
2092 }
2093 
2094 /*
2095  * nfs make dir call
2096  */
2097 static int
2098 nfs_mkdir(struct vop_mkdir_args *ap)
2099 {
2100 	struct vnode *dvp = ap->a_dvp;
2101 	struct vattr *vap = ap->a_vap;
2102 	struct componentname *cnp = ap->a_cnp;
2103 	struct nfsnode *np = NULL, *dnp;
2104 	struct vnode *newvp = NULL;
2105 	struct vattr vattr;
2106 	struct nfsfh *nfhp;
2107 	struct nfsvattr nfsva, dnfsva;
2108 	int error = 0, attrflag, dattrflag, ret;
2109 
2110 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2111 		return (error);
2112 	vap->va_type = VDIR;
2113 	error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2114 	    vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &nfhp,
2115 	    &attrflag, &dattrflag, NULL);
2116 	dnp = VTONFS(dvp);
2117 	mtx_lock(&dnp->n_mtx);
2118 	dnp->n_flag |= NMODIFIED;
2119 	if (dattrflag != 0) {
2120 		mtx_unlock(&dnp->n_mtx);
2121 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2122 	} else {
2123 		dnp->n_attrstamp = 0;
2124 		mtx_unlock(&dnp->n_mtx);
2125 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2126 	}
2127 	if (nfhp) {
2128 		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2129 		    &np, NULL, LK_EXCLUSIVE);
2130 		if (!ret) {
2131 			newvp = NFSTOV(np);
2132 			if (attrflag)
2133 			   (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2134 				NULL, 0, 1);
2135 		} else if (!error)
2136 			error = ret;
2137 	}
2138 	if (!error && newvp == NULL) {
2139 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2140 		    cnp->cn_cred, cnp->cn_thread, &np);
2141 		if (!error) {
2142 			newvp = NFSTOV(np);
2143 			if (newvp->v_type != VDIR)
2144 				error = EEXIST;
2145 		}
2146 	}
2147 	if (error) {
2148 		if (newvp)
2149 			vput(newvp);
2150 		if (NFS_ISV4(dvp))
2151 			error = nfscl_maperr(cnp->cn_thread, error,
2152 			    vap->va_uid, vap->va_gid);
2153 	} else {
2154 		/*
2155 		 * If negative lookup caching is enabled, I might as well
2156 		 * add an entry for this node. Not necessary for correctness,
2157 		 * but if negative caching is enabled, then the system
2158 		 * must care about lookup caching hit rate, so...
2159 		 */
2160 		if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2161 		    (cnp->cn_flags & MAKEENTRY) &&
2162 		    attrflag != 0 && dattrflag != 0)
2163 			cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2164 			    &dnfsva.na_ctime);
2165 		*ap->a_vpp = newvp;
2166 	}
2167 	return (error);
2168 }
2169 
2170 /*
2171  * nfs remove directory call
2172  */
2173 static int
2174 nfs_rmdir(struct vop_rmdir_args *ap)
2175 {
2176 	struct vnode *vp = ap->a_vp;
2177 	struct vnode *dvp = ap->a_dvp;
2178 	struct componentname *cnp = ap->a_cnp;
2179 	struct nfsnode *dnp;
2180 	struct nfsvattr dnfsva;
2181 	int error, dattrflag;
2182 
2183 	if (dvp == vp)
2184 		return (EINVAL);
2185 	error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2186 	    cnp->cn_cred, cnp->cn_thread, &dnfsva, &dattrflag, NULL);
2187 	dnp = VTONFS(dvp);
2188 	mtx_lock(&dnp->n_mtx);
2189 	dnp->n_flag |= NMODIFIED;
2190 	if (dattrflag != 0) {
2191 		mtx_unlock(&dnp->n_mtx);
2192 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2193 	} else {
2194 		dnp->n_attrstamp = 0;
2195 		mtx_unlock(&dnp->n_mtx);
2196 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2197 	}
2198 
2199 	cache_purge(dvp);
2200 	cache_purge(vp);
2201 	if (error && NFS_ISV4(dvp))
2202 		error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2203 		    (gid_t)0);
2204 	/*
2205 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2206 	 */
2207 	if (error == ENOENT)
2208 		error = 0;
2209 	return (error);
2210 }
2211 
2212 /*
2213  * nfs readdir call
2214  */
2215 static int
2216 nfs_readdir(struct vop_readdir_args *ap)
2217 {
2218 	struct vnode *vp = ap->a_vp;
2219 	struct nfsnode *np = VTONFS(vp);
2220 	struct uio *uio = ap->a_uio;
2221 	ssize_t tresid, left;
2222 	int error = 0;
2223 	struct vattr vattr;
2224 
2225 	if (ap->a_eofflag != NULL)
2226 		*ap->a_eofflag = 0;
2227 	if (vp->v_type != VDIR)
2228 		return(EPERM);
2229 
2230 	/*
2231 	 * First, check for hit on the EOF offset cache
2232 	 */
2233 	if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2234 	    (np->n_flag & NMODIFIED) == 0) {
2235 		if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2236 			mtx_lock(&np->n_mtx);
2237 			if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2238 			    !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2239 				mtx_unlock(&np->n_mtx);
2240 				NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2241 				if (ap->a_eofflag != NULL)
2242 					*ap->a_eofflag = 1;
2243 				return (0);
2244 			} else
2245 				mtx_unlock(&np->n_mtx);
2246 		}
2247 	}
2248 
2249 	/*
2250 	 * NFS always guarantees that directory entries don't straddle
2251 	 * DIRBLKSIZ boundaries.  As such, we need to limit the size
2252 	 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2253 	 * directory entry.
2254 	 */
2255 	left = uio->uio_resid % DIRBLKSIZ;
2256 	if (left == uio->uio_resid)
2257 		return (EINVAL);
2258 	uio->uio_resid -= left;
2259 
2260 	/*
2261 	 * Call ncl_bioread() to do the real work.
2262 	 */
2263 	tresid = uio->uio_resid;
2264 	error = ncl_bioread(vp, uio, 0, ap->a_cred);
2265 
2266 	if (!error && uio->uio_resid == tresid) {
2267 		NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2268 		if (ap->a_eofflag != NULL)
2269 			*ap->a_eofflag = 1;
2270 	}
2271 
2272 	/* Add the partial DIRBLKSIZ (left) back in. */
2273 	uio->uio_resid += left;
2274 	return (error);
2275 }
2276 
2277 /*
2278  * Readdir rpc call.
2279  * Called from below the buffer cache by ncl_doio().
2280  */
2281 int
2282 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2283     struct thread *td)
2284 {
2285 	struct nfsvattr nfsva;
2286 	nfsuint64 *cookiep, cookie;
2287 	struct nfsnode *dnp = VTONFS(vp);
2288 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2289 	int error = 0, eof, attrflag;
2290 
2291 	KASSERT(uiop->uio_iovcnt == 1 &&
2292 	    (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2293 	    (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2294 	    ("nfs readdirrpc bad uio"));
2295 
2296 	/*
2297 	 * If there is no cookie, assume directory was stale.
2298 	 */
2299 	ncl_dircookie_lock(dnp);
2300 	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2301 	if (cookiep) {
2302 		cookie = *cookiep;
2303 		ncl_dircookie_unlock(dnp);
2304 	} else {
2305 		ncl_dircookie_unlock(dnp);
2306 		return (NFSERR_BAD_COOKIE);
2307 	}
2308 
2309 	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2310 		(void)ncl_fsinfo(nmp, vp, cred, td);
2311 
2312 	error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2313 	    &attrflag, &eof, NULL);
2314 	if (attrflag)
2315 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2316 
2317 	if (!error) {
2318 		/*
2319 		 * We are now either at the end of the directory or have filled
2320 		 * the block.
2321 		 */
2322 		if (eof)
2323 			dnp->n_direofoffset = uiop->uio_offset;
2324 		else {
2325 			if (uiop->uio_resid > 0)
2326 				printf("EEK! readdirrpc resid > 0\n");
2327 			ncl_dircookie_lock(dnp);
2328 			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2329 			*cookiep = cookie;
2330 			ncl_dircookie_unlock(dnp);
2331 		}
2332 	} else if (NFS_ISV4(vp)) {
2333 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2334 	}
2335 	return (error);
2336 }
2337 
2338 /*
2339  * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2340  */
2341 int
2342 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2343     struct thread *td)
2344 {
2345 	struct nfsvattr nfsva;
2346 	nfsuint64 *cookiep, cookie;
2347 	struct nfsnode *dnp = VTONFS(vp);
2348 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2349 	int error = 0, attrflag, eof;
2350 
2351 	KASSERT(uiop->uio_iovcnt == 1 &&
2352 	    (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2353 	    (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2354 	    ("nfs readdirplusrpc bad uio"));
2355 
2356 	/*
2357 	 * If there is no cookie, assume directory was stale.
2358 	 */
2359 	ncl_dircookie_lock(dnp);
2360 	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2361 	if (cookiep) {
2362 		cookie = *cookiep;
2363 		ncl_dircookie_unlock(dnp);
2364 	} else {
2365 		ncl_dircookie_unlock(dnp);
2366 		return (NFSERR_BAD_COOKIE);
2367 	}
2368 
2369 	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2370 		(void)ncl_fsinfo(nmp, vp, cred, td);
2371 	error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2372 	    &attrflag, &eof, NULL);
2373 	if (attrflag)
2374 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2375 
2376 	if (!error) {
2377 		/*
2378 		 * We are now either at end of the directory or have filled the
2379 		 * the block.
2380 		 */
2381 		if (eof)
2382 			dnp->n_direofoffset = uiop->uio_offset;
2383 		else {
2384 			if (uiop->uio_resid > 0)
2385 				printf("EEK! readdirplusrpc resid > 0\n");
2386 			ncl_dircookie_lock(dnp);
2387 			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2388 			*cookiep = cookie;
2389 			ncl_dircookie_unlock(dnp);
2390 		}
2391 	} else if (NFS_ISV4(vp)) {
2392 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2393 	}
2394 	return (error);
2395 }
2396 
2397 /*
2398  * Silly rename. To make the NFS filesystem that is stateless look a little
2399  * more like the "ufs" a remove of an active vnode is translated to a rename
2400  * to a funny looking filename that is removed by nfs_inactive on the
2401  * nfsnode. There is the potential for another process on a different client
2402  * to create the same funny name between the nfs_lookitup() fails and the
2403  * nfs_rename() completes, but...
2404  */
2405 static int
2406 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2407 {
2408 	struct sillyrename *sp;
2409 	struct nfsnode *np;
2410 	int error;
2411 	short pid;
2412 	unsigned int lticks;
2413 
2414 	cache_purge(dvp);
2415 	np = VTONFS(vp);
2416 	KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2417 	sp = malloc(sizeof (struct sillyrename),
2418 	    M_NEWNFSREQ, M_WAITOK);
2419 	sp->s_cred = crhold(cnp->cn_cred);
2420 	sp->s_dvp = dvp;
2421 	VREF(dvp);
2422 
2423 	/*
2424 	 * Fudge together a funny name.
2425 	 * Changing the format of the funny name to accommodate more
2426 	 * sillynames per directory.
2427 	 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2428 	 * CPU ticks since boot.
2429 	 */
2430 	pid = cnp->cn_thread->td_proc->p_pid;
2431 	lticks = (unsigned int)ticks;
2432 	for ( ; ; ) {
2433 		sp->s_namlen = sprintf(sp->s_name,
2434 				       ".nfs.%08x.%04x4.4", lticks,
2435 				       pid);
2436 		if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2437 				 cnp->cn_thread, NULL))
2438 			break;
2439 		lticks++;
2440 	}
2441 	error = nfs_renameit(dvp, vp, cnp, sp);
2442 	if (error)
2443 		goto bad;
2444 	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2445 		cnp->cn_thread, &np);
2446 	np->n_sillyrename = sp;
2447 	return (0);
2448 bad:
2449 	vrele(sp->s_dvp);
2450 	crfree(sp->s_cred);
2451 	free(sp, M_NEWNFSREQ);
2452 	return (error);
2453 }
2454 
2455 /*
2456  * Look up a file name and optionally either update the file handle or
2457  * allocate an nfsnode, depending on the value of npp.
2458  * npp == NULL	--> just do the lookup
2459  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2460  *			handled too
2461  * *npp != NULL --> update the file handle in the vnode
2462  */
2463 static int
2464 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2465     struct thread *td, struct nfsnode **npp)
2466 {
2467 	struct vnode *newvp = NULL, *vp;
2468 	struct nfsnode *np, *dnp = VTONFS(dvp);
2469 	struct nfsfh *nfhp, *onfhp;
2470 	struct nfsvattr nfsva, dnfsva;
2471 	struct componentname cn;
2472 	int error = 0, attrflag, dattrflag;
2473 	u_int hash;
2474 
2475 	error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2476 	    &nfhp, &attrflag, &dattrflag, NULL);
2477 	if (dattrflag)
2478 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2479 	if (npp && !error) {
2480 		if (*npp != NULL) {
2481 		    np = *npp;
2482 		    vp = NFSTOV(np);
2483 		    /*
2484 		     * For NFSv4, check to see if it is the same name and
2485 		     * replace the name, if it is different.
2486 		     */
2487 		    if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2488 			(np->n_v4->n4_namelen != len ||
2489 			 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2490 			 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2491 			 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2492 			 dnp->n_fhp->nfh_len))) {
2493 #ifdef notdef
2494 { char nnn[100]; int nnnl;
2495 nnnl = (len < 100) ? len : 99;
2496 bcopy(name, nnn, nnnl);
2497 nnn[nnnl] = '\0';
2498 printf("replace=%s\n",nnn);
2499 }
2500 #endif
2501 			    free(np->n_v4, M_NFSV4NODE);
2502 			    np->n_v4 = malloc(
2503 				sizeof (struct nfsv4node) +
2504 				dnp->n_fhp->nfh_len + len - 1,
2505 				M_NFSV4NODE, M_WAITOK);
2506 			    np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2507 			    np->n_v4->n4_namelen = len;
2508 			    NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2509 				dnp->n_fhp->nfh_len);
2510 			    NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2511 		    }
2512 		    hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2513 			FNV1_32_INIT);
2514 		    onfhp = np->n_fhp;
2515 		    /*
2516 		     * Rehash node for new file handle.
2517 		     */
2518 		    vfs_hash_rehash(vp, hash);
2519 		    np->n_fhp = nfhp;
2520 		    if (onfhp != NULL)
2521 			free(onfhp, M_NFSFH);
2522 		    newvp = NFSTOV(np);
2523 		} else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2524 		    free(nfhp, M_NFSFH);
2525 		    VREF(dvp);
2526 		    newvp = dvp;
2527 		} else {
2528 		    cn.cn_nameptr = name;
2529 		    cn.cn_namelen = len;
2530 		    error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2531 			&np, NULL, LK_EXCLUSIVE);
2532 		    if (error)
2533 			return (error);
2534 		    newvp = NFSTOV(np);
2535 		}
2536 		if (!attrflag && *npp == NULL) {
2537 			if (newvp == dvp)
2538 				vrele(newvp);
2539 			else
2540 				vput(newvp);
2541 			return (ENOENT);
2542 		}
2543 		if (attrflag)
2544 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2545 			    0, 1);
2546 	}
2547 	if (npp && *npp == NULL) {
2548 		if (error) {
2549 			if (newvp) {
2550 				if (newvp == dvp)
2551 					vrele(newvp);
2552 				else
2553 					vput(newvp);
2554 			}
2555 		} else
2556 			*npp = np;
2557 	}
2558 	if (error && NFS_ISV4(dvp))
2559 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2560 	return (error);
2561 }
2562 
2563 /*
2564  * Nfs Version 3 and 4 commit rpc
2565  */
2566 int
2567 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2568    struct thread *td)
2569 {
2570 	struct nfsvattr nfsva;
2571 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2572 	struct nfsnode *np;
2573 	struct uio uio;
2574 	int error, attrflag;
2575 
2576 	np = VTONFS(vp);
2577 	error = EIO;
2578 	attrflag = 0;
2579 	if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) {
2580 		uio.uio_offset = offset;
2581 		uio.uio_resid = cnt;
2582 		error = nfscl_doiods(vp, &uio, NULL, NULL,
2583 		    NFSV4OPEN_ACCESSWRITE, 1, cred, td);
2584 		if (error != 0) {
2585 			mtx_lock(&np->n_mtx);
2586 			np->n_flag &= ~NDSCOMMIT;
2587 			mtx_unlock(&np->n_mtx);
2588 		}
2589 	}
2590 	if (error != 0) {
2591 		mtx_lock(&nmp->nm_mtx);
2592 		if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2593 			mtx_unlock(&nmp->nm_mtx);
2594 			return (0);
2595 		}
2596 		mtx_unlock(&nmp->nm_mtx);
2597 		error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
2598 		    &attrflag, NULL);
2599 	}
2600 	if (attrflag != 0)
2601 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL,
2602 		    0, 1);
2603 	if (error != 0 && NFS_ISV4(vp))
2604 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2605 	return (error);
2606 }
2607 
2608 /*
2609  * Strategy routine.
2610  * For async requests when nfsiod(s) are running, queue the request by
2611  * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2612  * request.
2613  */
2614 static int
2615 nfs_strategy(struct vop_strategy_args *ap)
2616 {
2617 	struct buf *bp;
2618 	struct vnode *vp;
2619 	struct ucred *cr;
2620 
2621 	bp = ap->a_bp;
2622 	vp = ap->a_vp;
2623 	KASSERT(bp->b_vp == vp, ("missing b_getvp"));
2624 	KASSERT(!(bp->b_flags & B_DONE),
2625 	    ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2626 	BUF_ASSERT_HELD(bp);
2627 
2628 	if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno)
2629 		bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize /
2630 		    DEV_BSIZE);
2631 	if (bp->b_iocmd == BIO_READ)
2632 		cr = bp->b_rcred;
2633 	else
2634 		cr = bp->b_wcred;
2635 
2636 	/*
2637 	 * If the op is asynchronous and an i/o daemon is waiting
2638 	 * queue the request, wake it up and wait for completion
2639 	 * otherwise just do it ourselves.
2640 	 */
2641 	if ((bp->b_flags & B_ASYNC) == 0 ||
2642 	    ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread))
2643 		(void) ncl_doio(vp, bp, cr, curthread, 1);
2644 	return (0);
2645 }
2646 
2647 /*
2648  * fsync vnode op. Just call ncl_flush() with commit == 1.
2649  */
2650 /* ARGSUSED */
2651 static int
2652 nfs_fsync(struct vop_fsync_args *ap)
2653 {
2654 
2655 	if (ap->a_vp->v_type != VREG) {
2656 		/*
2657 		 * For NFS, metadata is changed synchronously on the server,
2658 		 * so there is nothing to flush. Also, ncl_flush() clears
2659 		 * the NMODIFIED flag and that shouldn't be done here for
2660 		 * directories.
2661 		 */
2662 		return (0);
2663 	}
2664 	return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0));
2665 }
2666 
2667 /*
2668  * Flush all the blocks associated with a vnode.
2669  * 	Walk through the buffer pool and push any dirty pages
2670  *	associated with the vnode.
2671  * If the called_from_renewthread argument is TRUE, it has been called
2672  * from the NFSv4 renew thread and, as such, cannot block indefinitely
2673  * waiting for a buffer write to complete.
2674  */
2675 int
2676 ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
2677     int commit, int called_from_renewthread)
2678 {
2679 	struct nfsnode *np = VTONFS(vp);
2680 	struct buf *bp;
2681 	int i;
2682 	struct buf *nbp;
2683 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2684 	int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2685 	int passone = 1, trycnt = 0;
2686 	u_quad_t off, endoff, toff;
2687 	struct ucred* wcred = NULL;
2688 	struct buf **bvec = NULL;
2689 	struct bufobj *bo;
2690 #ifndef NFS_COMMITBVECSIZ
2691 #define	NFS_COMMITBVECSIZ	20
2692 #endif
2693 	struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2694 	u_int bvecsize = 0, bveccount;
2695 
2696 	if (called_from_renewthread != 0)
2697 		slptimeo = hz;
2698 	if (nmp->nm_flag & NFSMNT_INT)
2699 		slpflag = PCATCH;
2700 	if (!commit)
2701 		passone = 0;
2702 	bo = &vp->v_bufobj;
2703 	/*
2704 	 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2705 	 * server, but has not been committed to stable storage on the server
2706 	 * yet. On the first pass, the byte range is worked out and the commit
2707 	 * rpc is done. On the second pass, ncl_writebp() is called to do the
2708 	 * job.
2709 	 */
2710 again:
2711 	off = (u_quad_t)-1;
2712 	endoff = 0;
2713 	bvecpos = 0;
2714 	if (NFS_ISV34(vp) && commit) {
2715 		if (bvec != NULL && bvec != bvec_on_stack)
2716 			free(bvec, M_TEMP);
2717 		/*
2718 		 * Count up how many buffers waiting for a commit.
2719 		 */
2720 		bveccount = 0;
2721 		BO_LOCK(bo);
2722 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2723 			if (!BUF_ISLOCKED(bp) &&
2724 			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2725 				== (B_DELWRI | B_NEEDCOMMIT))
2726 				bveccount++;
2727 		}
2728 		/*
2729 		 * Allocate space to remember the list of bufs to commit.  It is
2730 		 * important to use M_NOWAIT here to avoid a race with nfs_write.
2731 		 * If we can't get memory (for whatever reason), we will end up
2732 		 * committing the buffers one-by-one in the loop below.
2733 		 */
2734 		if (bveccount > NFS_COMMITBVECSIZ) {
2735 			/*
2736 			 * Release the vnode interlock to avoid a lock
2737 			 * order reversal.
2738 			 */
2739 			BO_UNLOCK(bo);
2740 			bvec = (struct buf **)
2741 				malloc(bveccount * sizeof(struct buf *),
2742 				       M_TEMP, M_NOWAIT);
2743 			BO_LOCK(bo);
2744 			if (bvec == NULL) {
2745 				bvec = bvec_on_stack;
2746 				bvecsize = NFS_COMMITBVECSIZ;
2747 			} else
2748 				bvecsize = bveccount;
2749 		} else {
2750 			bvec = bvec_on_stack;
2751 			bvecsize = NFS_COMMITBVECSIZ;
2752 		}
2753 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2754 			if (bvecpos >= bvecsize)
2755 				break;
2756 			if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2757 				nbp = TAILQ_NEXT(bp, b_bobufs);
2758 				continue;
2759 			}
2760 			if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2761 			    (B_DELWRI | B_NEEDCOMMIT)) {
2762 				BUF_UNLOCK(bp);
2763 				nbp = TAILQ_NEXT(bp, b_bobufs);
2764 				continue;
2765 			}
2766 			BO_UNLOCK(bo);
2767 			bremfree(bp);
2768 			/*
2769 			 * Work out if all buffers are using the same cred
2770 			 * so we can deal with them all with one commit.
2771 			 *
2772 			 * NOTE: we are not clearing B_DONE here, so we have
2773 			 * to do it later on in this routine if we intend to
2774 			 * initiate I/O on the bp.
2775 			 *
2776 			 * Note: to avoid loopback deadlocks, we do not
2777 			 * assign b_runningbufspace.
2778 			 */
2779 			if (wcred == NULL)
2780 				wcred = bp->b_wcred;
2781 			else if (wcred != bp->b_wcred)
2782 				wcred = NOCRED;
2783 			vfs_busy_pages(bp, 1);
2784 
2785 			BO_LOCK(bo);
2786 			/*
2787 			 * bp is protected by being locked, but nbp is not
2788 			 * and vfs_busy_pages() may sleep.  We have to
2789 			 * recalculate nbp.
2790 			 */
2791 			nbp = TAILQ_NEXT(bp, b_bobufs);
2792 
2793 			/*
2794 			 * A list of these buffers is kept so that the
2795 			 * second loop knows which buffers have actually
2796 			 * been committed. This is necessary, since there
2797 			 * may be a race between the commit rpc and new
2798 			 * uncommitted writes on the file.
2799 			 */
2800 			bvec[bvecpos++] = bp;
2801 			toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2802 				bp->b_dirtyoff;
2803 			if (toff < off)
2804 				off = toff;
2805 			toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2806 			if (toff > endoff)
2807 				endoff = toff;
2808 		}
2809 		BO_UNLOCK(bo);
2810 	}
2811 	if (bvecpos > 0) {
2812 		/*
2813 		 * Commit data on the server, as required.
2814 		 * If all bufs are using the same wcred, then use that with
2815 		 * one call for all of them, otherwise commit each one
2816 		 * separately.
2817 		 */
2818 		if (wcred != NOCRED)
2819 			retv = ncl_commit(vp, off, (int)(endoff - off),
2820 					  wcred, td);
2821 		else {
2822 			retv = 0;
2823 			for (i = 0; i < bvecpos; i++) {
2824 				off_t off, size;
2825 				bp = bvec[i];
2826 				off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2827 					bp->b_dirtyoff;
2828 				size = (u_quad_t)(bp->b_dirtyend
2829 						  - bp->b_dirtyoff);
2830 				retv = ncl_commit(vp, off, (int)size,
2831 						  bp->b_wcred, td);
2832 				if (retv) break;
2833 			}
2834 		}
2835 
2836 		if (retv == NFSERR_STALEWRITEVERF)
2837 			ncl_clearcommit(vp->v_mount);
2838 
2839 		/*
2840 		 * Now, either mark the blocks I/O done or mark the
2841 		 * blocks dirty, depending on whether the commit
2842 		 * succeeded.
2843 		 */
2844 		for (i = 0; i < bvecpos; i++) {
2845 			bp = bvec[i];
2846 			bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2847 			if (retv) {
2848 				/*
2849 				 * Error, leave B_DELWRI intact
2850 				 */
2851 				vfs_unbusy_pages(bp);
2852 				brelse(bp);
2853 			} else {
2854 				/*
2855 				 * Success, remove B_DELWRI ( bundirty() ).
2856 				 *
2857 				 * b_dirtyoff/b_dirtyend seem to be NFS
2858 				 * specific.  We should probably move that
2859 				 * into bundirty(). XXX
2860 				 */
2861 				bufobj_wref(bo);
2862 				bp->b_flags |= B_ASYNC;
2863 				bundirty(bp);
2864 				bp->b_flags &= ~B_DONE;
2865 				bp->b_ioflags &= ~BIO_ERROR;
2866 				bp->b_dirtyoff = bp->b_dirtyend = 0;
2867 				bufdone(bp);
2868 			}
2869 		}
2870 	}
2871 
2872 	/*
2873 	 * Start/do any write(s) that are required.
2874 	 */
2875 loop:
2876 	BO_LOCK(bo);
2877 	TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2878 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2879 			if (waitfor != MNT_WAIT || passone)
2880 				continue;
2881 
2882 			error = BUF_TIMELOCK(bp,
2883 			    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2884 			    BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
2885 			if (error == 0) {
2886 				BUF_UNLOCK(bp);
2887 				goto loop;
2888 			}
2889 			if (error == ENOLCK) {
2890 				error = 0;
2891 				goto loop;
2892 			}
2893 			if (called_from_renewthread != 0) {
2894 				/*
2895 				 * Return EIO so the flush will be retried
2896 				 * later.
2897 				 */
2898 				error = EIO;
2899 				goto done;
2900 			}
2901 			if (newnfs_sigintr(nmp, td)) {
2902 				error = EINTR;
2903 				goto done;
2904 			}
2905 			if (slpflag == PCATCH) {
2906 				slpflag = 0;
2907 				slptimeo = 2 * hz;
2908 			}
2909 			goto loop;
2910 		}
2911 		if ((bp->b_flags & B_DELWRI) == 0)
2912 			panic("nfs_fsync: not dirty");
2913 		if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2914 			BUF_UNLOCK(bp);
2915 			continue;
2916 		}
2917 		BO_UNLOCK(bo);
2918 		bremfree(bp);
2919 		if (passone || !commit)
2920 		    bp->b_flags |= B_ASYNC;
2921 		else
2922 		    bp->b_flags |= B_ASYNC;
2923 		bwrite(bp);
2924 		if (newnfs_sigintr(nmp, td)) {
2925 			error = EINTR;
2926 			goto done;
2927 		}
2928 		goto loop;
2929 	}
2930 	if (passone) {
2931 		passone = 0;
2932 		BO_UNLOCK(bo);
2933 		goto again;
2934 	}
2935 	if (waitfor == MNT_WAIT) {
2936 		while (bo->bo_numoutput) {
2937 			error = bufobj_wwait(bo, slpflag, slptimeo);
2938 			if (error) {
2939 			    BO_UNLOCK(bo);
2940 			    if (called_from_renewthread != 0) {
2941 				/*
2942 				 * Return EIO so that the flush will be
2943 				 * retried later.
2944 				 */
2945 				error = EIO;
2946 				goto done;
2947 			    }
2948 			    error = newnfs_sigintr(nmp, td);
2949 			    if (error)
2950 				goto done;
2951 			    if (slpflag == PCATCH) {
2952 				slpflag = 0;
2953 				slptimeo = 2 * hz;
2954 			    }
2955 			    BO_LOCK(bo);
2956 			}
2957 		}
2958 		if (bo->bo_dirty.bv_cnt != 0 && commit) {
2959 			BO_UNLOCK(bo);
2960 			goto loop;
2961 		}
2962 		/*
2963 		 * Wait for all the async IO requests to drain
2964 		 */
2965 		BO_UNLOCK(bo);
2966 		mtx_lock(&np->n_mtx);
2967 		while (np->n_directio_asyncwr > 0) {
2968 			np->n_flag |= NFSYNCWAIT;
2969 			error = newnfs_msleep(td, &np->n_directio_asyncwr,
2970 			    &np->n_mtx, slpflag | (PRIBIO + 1),
2971 			    "nfsfsync", 0);
2972 			if (error) {
2973 				if (newnfs_sigintr(nmp, td)) {
2974 					mtx_unlock(&np->n_mtx);
2975 					error = EINTR;
2976 					goto done;
2977 				}
2978 			}
2979 		}
2980 		mtx_unlock(&np->n_mtx);
2981 	} else
2982 		BO_UNLOCK(bo);
2983 	if (NFSHASPNFS(nmp)) {
2984 		nfscl_layoutcommit(vp, td);
2985 		/*
2986 		 * Invalidate the attribute cache, since writes to a DS
2987 		 * won't update the size attribute.
2988 		 */
2989 		mtx_lock(&np->n_mtx);
2990 		np->n_attrstamp = 0;
2991 	} else
2992 		mtx_lock(&np->n_mtx);
2993 	if (np->n_flag & NWRITEERR) {
2994 		error = np->n_error;
2995 		np->n_flag &= ~NWRITEERR;
2996 	}
2997   	if (commit && bo->bo_dirty.bv_cnt == 0 &&
2998 	    bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
2999   		np->n_flag &= ~NMODIFIED;
3000 	mtx_unlock(&np->n_mtx);
3001 done:
3002 	if (bvec != NULL && bvec != bvec_on_stack)
3003 		free(bvec, M_TEMP);
3004 	if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
3005 	    (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
3006 	    np->n_directio_asyncwr != 0)) {
3007 		if (trycnt++ < 5) {
3008 			/* try, try again... */
3009 			passone = 1;
3010 			wcred = NULL;
3011 			bvec = NULL;
3012 			bvecsize = 0;
3013 			goto again;
3014 		}
3015 		vn_printf(vp, "ncl_flush failed");
3016 		error = called_from_renewthread != 0 ? EIO : EBUSY;
3017 	}
3018 	return (error);
3019 }
3020 
3021 /*
3022  * NFS advisory byte-level locks.
3023  */
3024 static int
3025 nfs_advlock(struct vop_advlock_args *ap)
3026 {
3027 	struct vnode *vp = ap->a_vp;
3028 	struct ucred *cred;
3029 	struct nfsnode *np = VTONFS(ap->a_vp);
3030 	struct proc *p = (struct proc *)ap->a_id;
3031 	struct thread *td = curthread;	/* XXX */
3032 	struct vattr va;
3033 	int ret, error;
3034 	u_quad_t size;
3035 
3036 	error = NFSVOPLOCK(vp, LK_SHARED);
3037 	if (error != 0)
3038 		return (EBADF);
3039 	if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3040 		if (vp->v_type != VREG) {
3041 			error = EINVAL;
3042 			goto out;
3043 		}
3044 		if ((ap->a_flags & F_POSIX) != 0)
3045 			cred = p->p_ucred;
3046 		else
3047 			cred = td->td_ucred;
3048 		NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
3049 		if (vp->v_iflag & VI_DOOMED) {
3050 			error = EBADF;
3051 			goto out;
3052 		}
3053 
3054 		/*
3055 		 * If this is unlocking a write locked region, flush and
3056 		 * commit them before unlocking. This is required by
3057 		 * RFC3530 Sec. 9.3.2.
3058 		 */
3059 		if (ap->a_op == F_UNLCK &&
3060 		    nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3061 		    ap->a_flags))
3062 			(void) ncl_flush(vp, MNT_WAIT, td, 1, 0);
3063 
3064 		/*
3065 		 * Loop around doing the lock op, while a blocking lock
3066 		 * must wait for the lock op to succeed.
3067 		 */
3068 		do {
3069 			ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3070 			    ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3071 			if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3072 			    ap->a_op == F_SETLK) {
3073 				NFSVOPUNLOCK(vp, 0);
3074 				error = nfs_catnap(PZERO | PCATCH, ret,
3075 				    "ncladvl");
3076 				if (error)
3077 					return (EINTR);
3078 				NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3079 				if (vp->v_iflag & VI_DOOMED) {
3080 					error = EBADF;
3081 					goto out;
3082 				}
3083 			}
3084 		} while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3085 		     ap->a_op == F_SETLK);
3086 		if (ret == NFSERR_DENIED) {
3087 			error = EAGAIN;
3088 			goto out;
3089 		} else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3090 			error = ret;
3091 			goto out;
3092 		} else if (ret != 0) {
3093 			error = EACCES;
3094 			goto out;
3095 		}
3096 
3097 		/*
3098 		 * Now, if we just got a lock, invalidate data in the buffer
3099 		 * cache, as required, so that the coherency conforms with
3100 		 * RFC3530 Sec. 9.3.2.
3101 		 */
3102 		if (ap->a_op == F_SETLK) {
3103 			if ((np->n_flag & NMODIFIED) == 0) {
3104 				np->n_attrstamp = 0;
3105 				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3106 				ret = VOP_GETATTR(vp, &va, cred);
3107 			}
3108 			if ((np->n_flag & NMODIFIED) || ret ||
3109 			    np->n_change != va.va_filerev) {
3110 				(void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3111 				np->n_attrstamp = 0;
3112 				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3113 				ret = VOP_GETATTR(vp, &va, cred);
3114 				if (!ret) {
3115 					np->n_mtime = va.va_mtime;
3116 					np->n_change = va.va_filerev;
3117 				}
3118 			}
3119 			/* Mark that a file lock has been acquired. */
3120 			mtx_lock(&np->n_mtx);
3121 			np->n_flag |= NHASBEENLOCKED;
3122 			mtx_unlock(&np->n_mtx);
3123 		}
3124 	} else if (!NFS_ISV4(vp)) {
3125 		if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3126 			size = VTONFS(vp)->n_size;
3127 			NFSVOPUNLOCK(vp, 0);
3128 			error = lf_advlock(ap, &(vp->v_lockf), size);
3129 		} else {
3130 			if (nfs_advlock_p != NULL)
3131 				error = nfs_advlock_p(ap);
3132 			else {
3133 				NFSVOPUNLOCK(vp, 0);
3134 				error = ENOLCK;
3135 			}
3136 		}
3137 		if (error == 0 && ap->a_op == F_SETLK) {
3138 			error = NFSVOPLOCK(vp, LK_SHARED);
3139 			if (error == 0) {
3140 				/* Mark that a file lock has been acquired. */
3141 				mtx_lock(&np->n_mtx);
3142 				np->n_flag |= NHASBEENLOCKED;
3143 				mtx_unlock(&np->n_mtx);
3144 				NFSVOPUNLOCK(vp, 0);
3145 			}
3146 		}
3147 		return (error);
3148 	} else
3149 		error = EOPNOTSUPP;
3150 out:
3151 	NFSVOPUNLOCK(vp, 0);
3152 	return (error);
3153 }
3154 
3155 /*
3156  * NFS advisory byte-level locks.
3157  */
3158 static int
3159 nfs_advlockasync(struct vop_advlockasync_args *ap)
3160 {
3161 	struct vnode *vp = ap->a_vp;
3162 	u_quad_t size;
3163 	int error;
3164 
3165 	if (NFS_ISV4(vp))
3166 		return (EOPNOTSUPP);
3167 	error = NFSVOPLOCK(vp, LK_SHARED);
3168 	if (error)
3169 		return (error);
3170 	if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3171 		size = VTONFS(vp)->n_size;
3172 		NFSVOPUNLOCK(vp, 0);
3173 		error = lf_advlockasync(ap, &(vp->v_lockf), size);
3174 	} else {
3175 		NFSVOPUNLOCK(vp, 0);
3176 		error = EOPNOTSUPP;
3177 	}
3178 	return (error);
3179 }
3180 
3181 /*
3182  * Print out the contents of an nfsnode.
3183  */
3184 static int
3185 nfs_print(struct vop_print_args *ap)
3186 {
3187 	struct vnode *vp = ap->a_vp;
3188 	struct nfsnode *np = VTONFS(vp);
3189 
3190 	printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid,
3191 	    (uintmax_t)np->n_vattr.na_fsid);
3192 	if (vp->v_type == VFIFO)
3193 		fifo_printinfo(vp);
3194 	printf("\n");
3195 	return (0);
3196 }
3197 
3198 /*
3199  * This is the "real" nfs::bwrite(struct buf*).
3200  * We set B_CACHE if this is a VMIO buffer.
3201  */
3202 int
3203 ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
3204 {
3205 	int oldflags, rtval;
3206 
3207 	BUF_ASSERT_HELD(bp);
3208 
3209 	if (bp->b_flags & B_INVAL) {
3210 		brelse(bp);
3211 		return (0);
3212 	}
3213 
3214 	oldflags = bp->b_flags;
3215 	bp->b_flags |= B_CACHE;
3216 
3217 	/*
3218 	 * Undirty the bp.  We will redirty it later if the I/O fails.
3219 	 */
3220 	bundirty(bp);
3221 	bp->b_flags &= ~B_DONE;
3222 	bp->b_ioflags &= ~BIO_ERROR;
3223 	bp->b_iocmd = BIO_WRITE;
3224 
3225 	bufobj_wref(bp->b_bufobj);
3226 	curthread->td_ru.ru_oublock++;
3227 
3228 	/*
3229 	 * Note: to avoid loopback deadlocks, we do not
3230 	 * assign b_runningbufspace.
3231 	 */
3232 	vfs_busy_pages(bp, 1);
3233 
3234 	BUF_KERNPROC(bp);
3235 	bp->b_iooffset = dbtob(bp->b_blkno);
3236 	bstrategy(bp);
3237 
3238 	if ((oldflags & B_ASYNC) != 0)
3239 		return (0);
3240 
3241 	rtval = bufwait(bp);
3242 	if (oldflags & B_DELWRI)
3243 		reassignbuf(bp);
3244 	brelse(bp);
3245 	return (rtval);
3246 }
3247 
3248 /*
3249  * nfs special file access vnode op.
3250  * Essentially just get vattr and then imitate iaccess() since the device is
3251  * local to the client.
3252  */
3253 static int
3254 nfsspec_access(struct vop_access_args *ap)
3255 {
3256 	struct vattr *vap;
3257 	struct ucred *cred = ap->a_cred;
3258 	struct vnode *vp = ap->a_vp;
3259 	accmode_t accmode = ap->a_accmode;
3260 	struct vattr vattr;
3261 	int error;
3262 
3263 	/*
3264 	 * Disallow write attempts on filesystems mounted read-only;
3265 	 * unless the file is a socket, fifo, or a block or character
3266 	 * device resident on the filesystem.
3267 	 */
3268 	if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3269 		switch (vp->v_type) {
3270 		case VREG:
3271 		case VDIR:
3272 		case VLNK:
3273 			return (EROFS);
3274 		default:
3275 			break;
3276 		}
3277 	}
3278 	vap = &vattr;
3279 	error = VOP_GETATTR(vp, vap, cred);
3280 	if (error)
3281 		goto out;
3282 	error  = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3283 	    accmode, cred, NULL);
3284 out:
3285 	return error;
3286 }
3287 
3288 /*
3289  * Read wrapper for fifos.
3290  */
3291 static int
3292 nfsfifo_read(struct vop_read_args *ap)
3293 {
3294 	struct nfsnode *np = VTONFS(ap->a_vp);
3295 	int error;
3296 
3297 	/*
3298 	 * Set access flag.
3299 	 */
3300 	mtx_lock(&np->n_mtx);
3301 	np->n_flag |= NACC;
3302 	vfs_timestamp(&np->n_atim);
3303 	mtx_unlock(&np->n_mtx);
3304 	error = fifo_specops.vop_read(ap);
3305 	return error;
3306 }
3307 
3308 /*
3309  * Write wrapper for fifos.
3310  */
3311 static int
3312 nfsfifo_write(struct vop_write_args *ap)
3313 {
3314 	struct nfsnode *np = VTONFS(ap->a_vp);
3315 
3316 	/*
3317 	 * Set update flag.
3318 	 */
3319 	mtx_lock(&np->n_mtx);
3320 	np->n_flag |= NUPD;
3321 	vfs_timestamp(&np->n_mtim);
3322 	mtx_unlock(&np->n_mtx);
3323 	return(fifo_specops.vop_write(ap));
3324 }
3325 
3326 /*
3327  * Close wrapper for fifos.
3328  *
3329  * Update the times on the nfsnode then do fifo close.
3330  */
3331 static int
3332 nfsfifo_close(struct vop_close_args *ap)
3333 {
3334 	struct vnode *vp = ap->a_vp;
3335 	struct nfsnode *np = VTONFS(vp);
3336 	struct vattr vattr;
3337 	struct timespec ts;
3338 
3339 	mtx_lock(&np->n_mtx);
3340 	if (np->n_flag & (NACC | NUPD)) {
3341 		vfs_timestamp(&ts);
3342 		if (np->n_flag & NACC)
3343 			np->n_atim = ts;
3344 		if (np->n_flag & NUPD)
3345 			np->n_mtim = ts;
3346 		np->n_flag |= NCHG;
3347 		if (vrefcnt(vp) == 1 &&
3348 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3349 			VATTR_NULL(&vattr);
3350 			if (np->n_flag & NACC)
3351 				vattr.va_atime = np->n_atim;
3352 			if (np->n_flag & NUPD)
3353 				vattr.va_mtime = np->n_mtim;
3354 			mtx_unlock(&np->n_mtx);
3355 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3356 			goto out;
3357 		}
3358 	}
3359 	mtx_unlock(&np->n_mtx);
3360 out:
3361 	return (fifo_specops.vop_close(ap));
3362 }
3363 
3364 /*
3365  * Just call ncl_writebp() with the force argument set to 1.
3366  *
3367  * NOTE: B_DONE may or may not be set in a_bp on call.
3368  */
3369 static int
3370 nfs_bwrite(struct buf *bp)
3371 {
3372 
3373 	return (ncl_writebp(bp, 1, curthread));
3374 }
3375 
3376 struct buf_ops buf_ops_newnfs = {
3377 	.bop_name	=	"buf_ops_nfs",
3378 	.bop_write	=	nfs_bwrite,
3379 	.bop_strategy	=	bufstrategy,
3380 	.bop_sync	=	bufsync,
3381 	.bop_bdflush	=	bufbdflush,
3382 };
3383 
3384 static int
3385 nfs_getacl(struct vop_getacl_args *ap)
3386 {
3387 	int error;
3388 
3389 	if (ap->a_type != ACL_TYPE_NFS4)
3390 		return (EOPNOTSUPP);
3391 	error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3392 	    NULL);
3393 	if (error > NFSERR_STALE) {
3394 		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3395 		error = EPERM;
3396 	}
3397 	return (error);
3398 }
3399 
3400 static int
3401 nfs_setacl(struct vop_setacl_args *ap)
3402 {
3403 	int error;
3404 
3405 	if (ap->a_type != ACL_TYPE_NFS4)
3406 		return (EOPNOTSUPP);
3407 	error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3408 	    NULL);
3409 	if (error > NFSERR_STALE) {
3410 		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3411 		error = EPERM;
3412 	}
3413 	return (error);
3414 }
3415 
3416 static int
3417 nfs_set_text(struct vop_set_text_args *ap)
3418 {
3419 	struct vnode *vp = ap->a_vp;
3420 	struct nfsnode *np;
3421 
3422 	/*
3423 	 * If the text file has been mmap'd, flush any dirty pages to the
3424 	 * buffer cache and then...
3425 	 * Make sure all writes are pushed to the NFS server.  If this is not
3426 	 * done, the modify time of the file can change while the text
3427 	 * file is being executed.  This will cause the process that is
3428 	 * executing the text file to be terminated.
3429 	 */
3430 	if (vp->v_object != NULL) {
3431 		VM_OBJECT_WLOCK(vp->v_object);
3432 		vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3433 		VM_OBJECT_WUNLOCK(vp->v_object);
3434 	}
3435 
3436 	/* Now, flush the buffer cache. */
3437 	ncl_flush(vp, MNT_WAIT, curthread, 0, 0);
3438 
3439 	/* And, finally, make sure that n_mtime is up to date. */
3440 	np = VTONFS(vp);
3441 	mtx_lock(&np->n_mtx);
3442 	np->n_mtime = np->n_vattr.na_mtime;
3443 	mtx_unlock(&np->n_mtx);
3444 
3445 	vp->v_vflag |= VV_TEXT;
3446 	return (0);
3447 }
3448 
3449 /*
3450  * Return POSIX pathconf information applicable to nfs filesystems.
3451  */
3452 static int
3453 nfs_pathconf(struct vop_pathconf_args *ap)
3454 {
3455 	struct nfsv3_pathconf pc;
3456 	struct nfsvattr nfsva;
3457 	struct vnode *vp = ap->a_vp;
3458 	struct thread *td = curthread;
3459 	int attrflag, error;
3460 
3461 	if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
3462 	    ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
3463 	    ap->a_name == _PC_NO_TRUNC)) ||
3464 	    (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) {
3465 		/*
3466 		 * Since only the above 4 a_names are returned by the NFSv3
3467 		 * Pathconf RPC, there is no point in doing it for others.
3468 		 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
3469 		 * be used for _PC_NFS4_ACL as well.
3470 		 */
3471 		error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
3472 		    &attrflag, NULL);
3473 		if (attrflag != 0)
3474 			(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
3475 			    1);
3476 		if (error != 0)
3477 			return (error);
3478 	} else {
3479 		/*
3480 		 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
3481 		 * just fake them.
3482 		 */
3483 		pc.pc_linkmax = NFS_LINK_MAX;
3484 		pc.pc_namemax = NFS_MAXNAMLEN;
3485 		pc.pc_notrunc = 1;
3486 		pc.pc_chownrestricted = 1;
3487 		pc.pc_caseinsensitive = 0;
3488 		pc.pc_casepreserving = 1;
3489 		error = 0;
3490 	}
3491 	switch (ap->a_name) {
3492 	case _PC_LINK_MAX:
3493 #ifdef _LP64
3494 		*ap->a_retval = pc.pc_linkmax;
3495 #else
3496 		*ap->a_retval = MIN(LONG_MAX, pc.pc_linkmax);
3497 #endif
3498 		break;
3499 	case _PC_NAME_MAX:
3500 		*ap->a_retval = pc.pc_namemax;
3501 		break;
3502 	case _PC_PIPE_BUF:
3503 		if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
3504 			*ap->a_retval = PIPE_BUF;
3505 		else
3506 			error = EINVAL;
3507 		break;
3508 	case _PC_CHOWN_RESTRICTED:
3509 		*ap->a_retval = pc.pc_chownrestricted;
3510 		break;
3511 	case _PC_NO_TRUNC:
3512 		*ap->a_retval = pc.pc_notrunc;
3513 		break;
3514 	case _PC_ACL_EXTENDED:
3515 		*ap->a_retval = 0;
3516 		break;
3517 	case _PC_ACL_NFS4:
3518 		if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
3519 		    NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
3520 			*ap->a_retval = 1;
3521 		else
3522 			*ap->a_retval = 0;
3523 		break;
3524 	case _PC_ACL_PATH_MAX:
3525 		if (NFS_ISV4(vp))
3526 			*ap->a_retval = ACL_MAX_ENTRIES;
3527 		else
3528 			*ap->a_retval = 3;
3529 		break;
3530 	case _PC_MAC_PRESENT:
3531 		*ap->a_retval = 0;
3532 		break;
3533 	case _PC_PRIO_IO:
3534 		*ap->a_retval = 0;
3535 		break;
3536 	case _PC_SYNC_IO:
3537 		*ap->a_retval = 0;
3538 		break;
3539 	case _PC_ALLOC_SIZE_MIN:
3540 		*ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
3541 		break;
3542 	case _PC_FILESIZEBITS:
3543 		if (NFS_ISV34(vp))
3544 			*ap->a_retval = 64;
3545 		else
3546 			*ap->a_retval = 32;
3547 		break;
3548 	case _PC_REC_INCR_XFER_SIZE:
3549 		*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3550 		break;
3551 	case _PC_REC_MAX_XFER_SIZE:
3552 		*ap->a_retval = -1; /* means ``unlimited'' */
3553 		break;
3554 	case _PC_REC_MIN_XFER_SIZE:
3555 		*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3556 		break;
3557 	case _PC_REC_XFER_ALIGN:
3558 		*ap->a_retval = PAGE_SIZE;
3559 		break;
3560 	case _PC_SYMLINK_MAX:
3561 		*ap->a_retval = NFS_MAXPATHLEN;
3562 		break;
3563 
3564 	default:
3565 		error = vop_stdpathconf(ap);
3566 		break;
3567 	}
3568 	return (error);
3569 }
3570 
3571