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