xref: /freebsd/sys/fs/nfsclient/nfs_clvnops.c (revision 5405b282e1f319b6f3597bb77f68be903e7f248c)
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, td, vap->va_size);
983 			mtx_lock(&np->n_mtx);
984  			if (np->n_flag & NMODIFIED) {
985 			    tsize = np->n_size;
986 			    mtx_unlock(&np->n_mtx);
987 			    error = ncl_vinvalbuf(vp, vap->va_size == 0 ?
988 			        0 : V_SAVE, td, 1);
989 			    if (error != 0) {
990 				    vnode_pager_setsize(vp, tsize);
991 				    return (error);
992 			    }
993 			    /*
994 			     * Call nfscl_delegmodtime() to set the modify time
995 			     * locally, as required.
996 			     */
997 			    nfscl_delegmodtime(vp);
998  			} else
999 			    mtx_unlock(&np->n_mtx);
1000 			/*
1001 			 * np->n_size has already been set to vap->va_size
1002 			 * in ncl_meta_setsize(). We must set it again since
1003 			 * nfs_loadattrcache() could be called through
1004 			 * ncl_meta_setsize() and could modify np->n_size.
1005 			 */
1006 			mtx_lock(&np->n_mtx);
1007  			np->n_vattr.na_size = np->n_size = vap->va_size;
1008 			mtx_unlock(&np->n_mtx);
1009   		}
1010   	} else {
1011 		mtx_lock(&np->n_mtx);
1012 		if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
1013 		    (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
1014 			mtx_unlock(&np->n_mtx);
1015 			error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
1016 			if (error == EINTR || error == EIO)
1017 				return (error);
1018 		} else
1019 			mtx_unlock(&np->n_mtx);
1020 	}
1021 	error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
1022 	if (error && vap->va_size != VNOVAL) {
1023 		mtx_lock(&np->n_mtx);
1024 		np->n_size = np->n_vattr.na_size = tsize;
1025 		vnode_pager_setsize(vp, tsize);
1026 		mtx_unlock(&np->n_mtx);
1027 	}
1028 	return (error);
1029 }
1030 
1031 /*
1032  * Do an nfs setattr rpc.
1033  */
1034 static int
1035 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
1036     struct thread *td)
1037 {
1038 	struct nfsnode *np = VTONFS(vp);
1039 	int error, ret, attrflag, i;
1040 	struct nfsvattr nfsva;
1041 
1042 	if (NFS_ISV34(vp)) {
1043 		mtx_lock(&np->n_mtx);
1044 		for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
1045 			np->n_accesscache[i].stamp = 0;
1046 		np->n_flag |= NDELEGMOD;
1047 		mtx_unlock(&np->n_mtx);
1048 		KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1049 	}
1050 	error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag,
1051 	    NULL);
1052 	if (attrflag) {
1053 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1054 		if (ret && !error)
1055 			error = ret;
1056 	}
1057 	if (error && NFS_ISV4(vp))
1058 		error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1059 	return (error);
1060 }
1061 
1062 /*
1063  * nfs lookup call, one step at a time...
1064  * First look in cache
1065  * If not found, unlock the directory nfsnode and do the rpc
1066  */
1067 static int
1068 nfs_lookup(struct vop_lookup_args *ap)
1069 {
1070 	struct componentname *cnp = ap->a_cnp;
1071 	struct vnode *dvp = ap->a_dvp;
1072 	struct vnode **vpp = ap->a_vpp;
1073 	struct mount *mp = dvp->v_mount;
1074 	int flags = cnp->cn_flags;
1075 	struct vnode *newvp;
1076 	struct nfsmount *nmp;
1077 	struct nfsnode *np, *newnp;
1078 	int error = 0, attrflag, dattrflag, ltype, ncticks;
1079 	struct thread *td = cnp->cn_thread;
1080 	struct nfsfh *nfhp;
1081 	struct nfsvattr dnfsva, nfsva;
1082 	struct vattr vattr;
1083 	struct timespec nctime;
1084 
1085 	*vpp = NULLVP;
1086 	if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1087 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1088 		return (EROFS);
1089 	if (dvp->v_type != VDIR)
1090 		return (ENOTDIR);
1091 	nmp = VFSTONFS(mp);
1092 	np = VTONFS(dvp);
1093 
1094 	/* For NFSv4, wait until any remove is done. */
1095 	mtx_lock(&np->n_mtx);
1096 	while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1097 		np->n_flag |= NREMOVEWANT;
1098 		(void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1099 	}
1100 	mtx_unlock(&np->n_mtx);
1101 
1102 	if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0)
1103 		return (error);
1104 	error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1105 	if (error > 0 && error != ENOENT)
1106 		return (error);
1107 	if (error == -1) {
1108 		/*
1109 		 * Lookups of "." are special and always return the
1110 		 * current directory.  cache_lookup() already handles
1111 		 * associated locking bookkeeping, etc.
1112 		 */
1113 		if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1114 			/* XXX: Is this really correct? */
1115 			if (cnp->cn_nameiop != LOOKUP &&
1116 			    (flags & ISLASTCN))
1117 				cnp->cn_flags |= SAVENAME;
1118 			return (0);
1119 		}
1120 
1121 		/*
1122 		 * We only accept a positive hit in the cache if the
1123 		 * change time of the file matches our cached copy.
1124 		 * Otherwise, we discard the cache entry and fallback
1125 		 * to doing a lookup RPC.  We also only trust cache
1126 		 * entries for less than nm_nametimeo seconds.
1127 		 *
1128 		 * To better handle stale file handles and attributes,
1129 		 * clear the attribute cache of this node if it is a
1130 		 * leaf component, part of an open() call, and not
1131 		 * locally modified before fetching the attributes.
1132 		 * This should allow stale file handles to be detected
1133 		 * here where we can fall back to a LOOKUP RPC to
1134 		 * recover rather than having nfs_open() detect the
1135 		 * stale file handle and failing open(2) with ESTALE.
1136 		 */
1137 		newvp = *vpp;
1138 		newnp = VTONFS(newvp);
1139 		if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1140 		    (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1141 		    !(newnp->n_flag & NMODIFIED)) {
1142 			mtx_lock(&newnp->n_mtx);
1143 			newnp->n_attrstamp = 0;
1144 			KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1145 			mtx_unlock(&newnp->n_mtx);
1146 		}
1147 		if (nfscl_nodeleg(newvp, 0) == 0 ||
1148 		    ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
1149 		    VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1150 		    timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1151 			NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1152 			if (cnp->cn_nameiop != LOOKUP &&
1153 			    (flags & ISLASTCN))
1154 				cnp->cn_flags |= SAVENAME;
1155 			return (0);
1156 		}
1157 		cache_purge(newvp);
1158 		if (dvp != newvp)
1159 			vput(newvp);
1160 		else
1161 			vrele(newvp);
1162 		*vpp = NULLVP;
1163 	} else if (error == ENOENT) {
1164 		if (dvp->v_iflag & VI_DOOMED)
1165 			return (ENOENT);
1166 		/*
1167 		 * We only accept a negative hit in the cache if the
1168 		 * modification time of the parent directory matches
1169 		 * the cached copy in the name cache entry.
1170 		 * Otherwise, we discard all of the negative cache
1171 		 * entries for this directory.  We also only trust
1172 		 * negative cache entries for up to nm_negnametimeo
1173 		 * seconds.
1174 		 */
1175 		if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
1176 		    VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1177 		    timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1178 			NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1179 			return (ENOENT);
1180 		}
1181 		cache_purge_negative(dvp);
1182 	}
1183 
1184 	error = 0;
1185 	newvp = NULLVP;
1186 	NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1187 	error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1188 	    cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1189 	    NULL);
1190 	if (dattrflag)
1191 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1192 	if (error) {
1193 		if (newvp != NULLVP) {
1194 			vput(newvp);
1195 			*vpp = NULLVP;
1196 		}
1197 
1198 		if (error != ENOENT) {
1199 			if (NFS_ISV4(dvp))
1200 				error = nfscl_maperr(td, error, (uid_t)0,
1201 				    (gid_t)0);
1202 			return (error);
1203 		}
1204 
1205 		/* The requested file was not found. */
1206 		if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1207 		    (flags & ISLASTCN)) {
1208 			/*
1209 			 * XXX: UFS does a full VOP_ACCESS(dvp,
1210 			 * VWRITE) here instead of just checking
1211 			 * MNT_RDONLY.
1212 			 */
1213 			if (mp->mnt_flag & MNT_RDONLY)
1214 				return (EROFS);
1215 			cnp->cn_flags |= SAVENAME;
1216 			return (EJUSTRETURN);
1217 		}
1218 
1219 		if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1220 			/*
1221 			 * Cache the modification time of the parent
1222 			 * directory from the post-op attributes in
1223 			 * the name cache entry.  The negative cache
1224 			 * entry will be ignored once the directory
1225 			 * has changed.  Don't bother adding the entry
1226 			 * if the directory has already changed.
1227 			 */
1228 			mtx_lock(&np->n_mtx);
1229 			if (timespeccmp(&np->n_vattr.na_mtime,
1230 			    &dnfsva.na_mtime, ==)) {
1231 				mtx_unlock(&np->n_mtx);
1232 				cache_enter_time(dvp, NULL, cnp,
1233 				    &dnfsva.na_mtime, NULL);
1234 			} else
1235 				mtx_unlock(&np->n_mtx);
1236 		}
1237 		return (ENOENT);
1238 	}
1239 
1240 	/*
1241 	 * Handle RENAME case...
1242 	 */
1243 	if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1244 		if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1245 			free(nfhp, M_NFSFH);
1246 			return (EISDIR);
1247 		}
1248 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1249 		    LK_EXCLUSIVE);
1250 		if (error)
1251 			return (error);
1252 		newvp = NFSTOV(np);
1253 		if (attrflag)
1254 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1255 			    0, 1);
1256 		*vpp = newvp;
1257 		cnp->cn_flags |= SAVENAME;
1258 		return (0);
1259 	}
1260 
1261 	if (flags & ISDOTDOT) {
1262 		ltype = NFSVOPISLOCKED(dvp);
1263 		error = vfs_busy(mp, MBF_NOWAIT);
1264 		if (error != 0) {
1265 			vfs_ref(mp);
1266 			NFSVOPUNLOCK(dvp, 0);
1267 			error = vfs_busy(mp, 0);
1268 			NFSVOPLOCK(dvp, ltype | LK_RETRY);
1269 			vfs_rel(mp);
1270 			if (error == 0 && (dvp->v_iflag & VI_DOOMED)) {
1271 				vfs_unbusy(mp);
1272 				error = ENOENT;
1273 			}
1274 			if (error != 0)
1275 				return (error);
1276 		}
1277 		NFSVOPUNLOCK(dvp, 0);
1278 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1279 		    cnp->cn_lkflags);
1280 		if (error == 0)
1281 			newvp = NFSTOV(np);
1282 		vfs_unbusy(mp);
1283 		if (newvp != dvp)
1284 			NFSVOPLOCK(dvp, ltype | LK_RETRY);
1285 		if (dvp->v_iflag & VI_DOOMED) {
1286 			if (error == 0) {
1287 				if (newvp == dvp)
1288 					vrele(newvp);
1289 				else
1290 					vput(newvp);
1291 			}
1292 			error = ENOENT;
1293 		}
1294 		if (error != 0)
1295 			return (error);
1296 		if (attrflag)
1297 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1298 			    0, 1);
1299 	} else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1300 		free(nfhp, M_NFSFH);
1301 		VREF(dvp);
1302 		newvp = dvp;
1303 		if (attrflag)
1304 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1305 			    0, 1);
1306 	} else {
1307 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1308 		    cnp->cn_lkflags);
1309 		if (error)
1310 			return (error);
1311 		newvp = NFSTOV(np);
1312 		if (attrflag)
1313 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1314 			    0, 1);
1315 		else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1316 		    !(np->n_flag & NMODIFIED)) {
1317 			/*
1318 			 * Flush the attribute cache when opening a
1319 			 * leaf node to ensure that fresh attributes
1320 			 * are fetched in nfs_open() since we did not
1321 			 * fetch attributes from the LOOKUP reply.
1322 			 */
1323 			mtx_lock(&np->n_mtx);
1324 			np->n_attrstamp = 0;
1325 			KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1326 			mtx_unlock(&np->n_mtx);
1327 		}
1328 	}
1329 	if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1330 		cnp->cn_flags |= SAVENAME;
1331 	if ((cnp->cn_flags & MAKEENTRY) &&
1332 	    (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1333 	    attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1334 		cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1335 		    newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1336 	*vpp = newvp;
1337 	return (0);
1338 }
1339 
1340 /*
1341  * nfs read call.
1342  * Just call ncl_bioread() to do the work.
1343  */
1344 static int
1345 nfs_read(struct vop_read_args *ap)
1346 {
1347 	struct vnode *vp = ap->a_vp;
1348 
1349 	switch (vp->v_type) {
1350 	case VREG:
1351 		return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1352 	case VDIR:
1353 		return (EISDIR);
1354 	default:
1355 		return (EOPNOTSUPP);
1356 	}
1357 }
1358 
1359 /*
1360  * nfs readlink call
1361  */
1362 static int
1363 nfs_readlink(struct vop_readlink_args *ap)
1364 {
1365 	struct vnode *vp = ap->a_vp;
1366 
1367 	if (vp->v_type != VLNK)
1368 		return (EINVAL);
1369 	return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1370 }
1371 
1372 /*
1373  * Do a readlink rpc.
1374  * Called by ncl_doio() from below the buffer cache.
1375  */
1376 int
1377 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1378 {
1379 	int error, ret, attrflag;
1380 	struct nfsvattr nfsva;
1381 
1382 	error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1383 	    &attrflag, NULL);
1384 	if (attrflag) {
1385 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1386 		if (ret && !error)
1387 			error = ret;
1388 	}
1389 	if (error && NFS_ISV4(vp))
1390 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1391 	return (error);
1392 }
1393 
1394 /*
1395  * nfs read rpc call
1396  * Ditto above
1397  */
1398 int
1399 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1400 {
1401 	int error, ret, attrflag;
1402 	struct nfsvattr nfsva;
1403 	struct nfsmount *nmp;
1404 
1405 	nmp = VFSTONFS(vnode_mount(vp));
1406 	error = EIO;
1407 	attrflag = 0;
1408 	if (NFSHASPNFS(nmp))
1409 		error = nfscl_doiods(vp, uiop, NULL, NULL,
1410 		    NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td);
1411 	NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1412 	if (error != 0)
1413 		error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1414 		    &attrflag, NULL);
1415 	if (attrflag) {
1416 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1417 		if (ret && !error)
1418 			error = ret;
1419 	}
1420 	if (error && NFS_ISV4(vp))
1421 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1422 	return (error);
1423 }
1424 
1425 /*
1426  * nfs write call
1427  */
1428 int
1429 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1430     int *iomode, int *must_commit, int called_from_strategy)
1431 {
1432 	struct nfsvattr nfsva;
1433 	int error, attrflag, ret;
1434 	struct nfsmount *nmp;
1435 
1436 	nmp = VFSTONFS(vnode_mount(vp));
1437 	error = EIO;
1438 	attrflag = 0;
1439 	if (NFSHASPNFS(nmp))
1440 		error = nfscl_doiods(vp, uiop, iomode, must_commit,
1441 		    NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td);
1442 	NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1443 	if (error != 0)
1444 		error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1445 		    uiop->uio_td, &nfsva, &attrflag, NULL,
1446 		    called_from_strategy);
1447 	if (attrflag) {
1448 		if (VTONFS(vp)->n_flag & ND_NFSV4)
1449 			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
1450 			    1);
1451 		else
1452 			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
1453 			    1);
1454 		if (ret && !error)
1455 			error = ret;
1456 	}
1457 	if (DOINGASYNC(vp))
1458 		*iomode = NFSWRITE_FILESYNC;
1459 	if (error && NFS_ISV4(vp))
1460 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1461 	return (error);
1462 }
1463 
1464 /*
1465  * nfs mknod rpc
1466  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1467  * mode set to specify the file type and the size field for rdev.
1468  */
1469 static int
1470 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1471     struct vattr *vap)
1472 {
1473 	struct nfsvattr nfsva, dnfsva;
1474 	struct vnode *newvp = NULL;
1475 	struct nfsnode *np = NULL, *dnp;
1476 	struct nfsfh *nfhp;
1477 	struct vattr vattr;
1478 	int error = 0, attrflag, dattrflag;
1479 	u_int32_t rdev;
1480 
1481 	if (vap->va_type == VCHR || vap->va_type == VBLK)
1482 		rdev = vap->va_rdev;
1483 	else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1484 		rdev = 0xffffffff;
1485 	else
1486 		return (EOPNOTSUPP);
1487 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1488 		return (error);
1489 	error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1490 	    rdev, vap->va_type, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1491 	    &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1492 	if (!error) {
1493 		if (!nfhp)
1494 			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1495 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1496 			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1497 			    NULL);
1498 		if (nfhp)
1499 			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1500 			    cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1501 	}
1502 	if (dattrflag)
1503 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1504 	if (!error) {
1505 		newvp = NFSTOV(np);
1506 		if (attrflag != 0) {
1507 			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1508 			    0, 1);
1509 			if (error != 0)
1510 				vput(newvp);
1511 		}
1512 	}
1513 	if (!error) {
1514 		*vpp = newvp;
1515 	} else if (NFS_ISV4(dvp)) {
1516 		error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1517 		    vap->va_gid);
1518 	}
1519 	dnp = VTONFS(dvp);
1520 	mtx_lock(&dnp->n_mtx);
1521 	dnp->n_flag |= NMODIFIED;
1522 	if (!dattrflag) {
1523 		dnp->n_attrstamp = 0;
1524 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1525 	}
1526 	mtx_unlock(&dnp->n_mtx);
1527 	return (error);
1528 }
1529 
1530 /*
1531  * nfs mknod vop
1532  * just call nfs_mknodrpc() to do the work.
1533  */
1534 /* ARGSUSED */
1535 static int
1536 nfs_mknod(struct vop_mknod_args *ap)
1537 {
1538 	return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1539 }
1540 
1541 static struct mtx nfs_cverf_mtx;
1542 MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1543     MTX_DEF);
1544 
1545 static nfsquad_t
1546 nfs_get_cverf(void)
1547 {
1548 	static nfsquad_t cverf;
1549 	nfsquad_t ret;
1550 	static int cverf_initialized = 0;
1551 
1552 	mtx_lock(&nfs_cverf_mtx);
1553 	if (cverf_initialized == 0) {
1554 		cverf.lval[0] = arc4random();
1555 		cverf.lval[1] = arc4random();
1556 		cverf_initialized = 1;
1557 	} else
1558 		cverf.qval++;
1559 	ret = cverf;
1560 	mtx_unlock(&nfs_cverf_mtx);
1561 
1562 	return (ret);
1563 }
1564 
1565 /*
1566  * nfs file create call
1567  */
1568 static int
1569 nfs_create(struct vop_create_args *ap)
1570 {
1571 	struct vnode *dvp = ap->a_dvp;
1572 	struct vattr *vap = ap->a_vap;
1573 	struct componentname *cnp = ap->a_cnp;
1574 	struct nfsnode *np = NULL, *dnp;
1575 	struct vnode *newvp = NULL;
1576 	struct nfsmount *nmp;
1577 	struct nfsvattr dnfsva, nfsva;
1578 	struct nfsfh *nfhp;
1579 	nfsquad_t cverf;
1580 	int error = 0, attrflag, dattrflag, fmode = 0;
1581 	struct vattr vattr;
1582 
1583 	/*
1584 	 * Oops, not for me..
1585 	 */
1586 	if (vap->va_type == VSOCK)
1587 		return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1588 
1589 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1590 		return (error);
1591 	if (vap->va_vaflags & VA_EXCLUSIVE)
1592 		fmode |= O_EXCL;
1593 	dnp = VTONFS(dvp);
1594 	nmp = VFSTONFS(vnode_mount(dvp));
1595 again:
1596 	/* For NFSv4, wait until any remove is done. */
1597 	mtx_lock(&dnp->n_mtx);
1598 	while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1599 		dnp->n_flag |= NREMOVEWANT;
1600 		(void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1601 	}
1602 	mtx_unlock(&dnp->n_mtx);
1603 
1604 	cverf = nfs_get_cverf();
1605 	error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1606 	    vap, cverf, fmode, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva,
1607 	    &nfhp, &attrflag, &dattrflag, NULL);
1608 	if (!error) {
1609 		if (nfhp == NULL)
1610 			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1611 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1612 			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1613 			    NULL);
1614 		if (nfhp != NULL)
1615 			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1616 			    cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1617 	}
1618 	if (dattrflag)
1619 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1620 	if (!error) {
1621 		newvp = NFSTOV(np);
1622 		if (attrflag == 0)
1623 			error = nfsrpc_getattr(newvp, cnp->cn_cred,
1624 			    cnp->cn_thread, &nfsva, NULL);
1625 		if (error == 0)
1626 			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1627 			    0, 1);
1628 	}
1629 	if (error) {
1630 		if (newvp != NULL) {
1631 			vput(newvp);
1632 			newvp = NULL;
1633 		}
1634 		if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1635 		    error == NFSERR_NOTSUPP) {
1636 			fmode &= ~O_EXCL;
1637 			goto again;
1638 		}
1639 	} else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1640 		if (nfscl_checksattr(vap, &nfsva)) {
1641 			error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1642 			    cnp->cn_thread, &nfsva, &attrflag, NULL);
1643 			if (error && (vap->va_uid != (uid_t)VNOVAL ||
1644 			    vap->va_gid != (gid_t)VNOVAL)) {
1645 				/* try again without setting uid/gid */
1646 				vap->va_uid = (uid_t)VNOVAL;
1647 				vap->va_gid = (uid_t)VNOVAL;
1648 				error = nfsrpc_setattr(newvp, vap, NULL,
1649 				    cnp->cn_cred, cnp->cn_thread, &nfsva,
1650 				    &attrflag, NULL);
1651 			}
1652 			if (attrflag)
1653 				(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1654 				    NULL, 0, 1);
1655 			if (error != 0)
1656 				vput(newvp);
1657 		}
1658 	}
1659 	if (!error) {
1660 		if ((cnp->cn_flags & MAKEENTRY) && attrflag)
1661 			cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1662 			    NULL);
1663 		*ap->a_vpp = newvp;
1664 	} else if (NFS_ISV4(dvp)) {
1665 		error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1666 		    vap->va_gid);
1667 	}
1668 	mtx_lock(&dnp->n_mtx);
1669 	dnp->n_flag |= NMODIFIED;
1670 	if (!dattrflag) {
1671 		dnp->n_attrstamp = 0;
1672 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1673 	}
1674 	mtx_unlock(&dnp->n_mtx);
1675 	return (error);
1676 }
1677 
1678 /*
1679  * nfs file remove call
1680  * To try and make nfs semantics closer to ufs semantics, a file that has
1681  * other processes using the vnode is renamed instead of removed and then
1682  * removed later on the last close.
1683  * - If v_usecount > 1
1684  *	  If a rename is not already in the works
1685  *	     call nfs_sillyrename() to set it up
1686  *     else
1687  *	  do the remove rpc
1688  */
1689 static int
1690 nfs_remove(struct vop_remove_args *ap)
1691 {
1692 	struct vnode *vp = ap->a_vp;
1693 	struct vnode *dvp = ap->a_dvp;
1694 	struct componentname *cnp = ap->a_cnp;
1695 	struct nfsnode *np = VTONFS(vp);
1696 	int error = 0;
1697 	struct vattr vattr;
1698 
1699 	KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1700 	KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1701 	if (vp->v_type == VDIR)
1702 		error = EPERM;
1703 	else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1704 	    VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1705 	    vattr.va_nlink > 1)) {
1706 		/*
1707 		 * Purge the name cache so that the chance of a lookup for
1708 		 * the name succeeding while the remove is in progress is
1709 		 * minimized. Without node locking it can still happen, such
1710 		 * that an I/O op returns ESTALE, but since you get this if
1711 		 * another host removes the file..
1712 		 */
1713 		cache_purge(vp);
1714 		/*
1715 		 * throw away biocache buffers, mainly to avoid
1716 		 * unnecessary delayed writes later.
1717 		 */
1718 		error = ncl_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1719 		if (error != EINTR && error != EIO)
1720 			/* Do the rpc */
1721 			error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1722 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1723 		/*
1724 		 * Kludge City: If the first reply to the remove rpc is lost..
1725 		 *   the reply to the retransmitted request will be ENOENT
1726 		 *   since the file was in fact removed
1727 		 *   Therefore, we cheat and return success.
1728 		 */
1729 		if (error == ENOENT)
1730 			error = 0;
1731 	} else if (!np->n_sillyrename)
1732 		error = nfs_sillyrename(dvp, vp, cnp);
1733 	mtx_lock(&np->n_mtx);
1734 	np->n_attrstamp = 0;
1735 	mtx_unlock(&np->n_mtx);
1736 	KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1737 	return (error);
1738 }
1739 
1740 /*
1741  * nfs file remove rpc called from nfs_inactive
1742  */
1743 int
1744 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1745 {
1746 	/*
1747 	 * Make sure that the directory vnode is still valid.
1748 	 * XXX we should lock sp->s_dvp here.
1749 	 */
1750 	if (sp->s_dvp->v_type == VBAD)
1751 		return (0);
1752 	return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1753 	    sp->s_cred, NULL));
1754 }
1755 
1756 /*
1757  * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1758  */
1759 static int
1760 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1761     int namelen, struct ucred *cred, struct thread *td)
1762 {
1763 	struct nfsvattr dnfsva;
1764 	struct nfsnode *dnp = VTONFS(dvp);
1765 	int error = 0, dattrflag;
1766 
1767 	mtx_lock(&dnp->n_mtx);
1768 	dnp->n_flag |= NREMOVEINPROG;
1769 	mtx_unlock(&dnp->n_mtx);
1770 	error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1771 	    &dattrflag, NULL);
1772 	mtx_lock(&dnp->n_mtx);
1773 	if ((dnp->n_flag & NREMOVEWANT)) {
1774 		dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1775 		mtx_unlock(&dnp->n_mtx);
1776 		wakeup((caddr_t)dnp);
1777 	} else {
1778 		dnp->n_flag &= ~NREMOVEINPROG;
1779 		mtx_unlock(&dnp->n_mtx);
1780 	}
1781 	if (dattrflag)
1782 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1783 	mtx_lock(&dnp->n_mtx);
1784 	dnp->n_flag |= NMODIFIED;
1785 	if (!dattrflag) {
1786 		dnp->n_attrstamp = 0;
1787 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1788 	}
1789 	mtx_unlock(&dnp->n_mtx);
1790 	if (error && NFS_ISV4(dvp))
1791 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1792 	return (error);
1793 }
1794 
1795 /*
1796  * nfs file rename call
1797  */
1798 static int
1799 nfs_rename(struct vop_rename_args *ap)
1800 {
1801 	struct vnode *fvp = ap->a_fvp;
1802 	struct vnode *tvp = ap->a_tvp;
1803 	struct vnode *fdvp = ap->a_fdvp;
1804 	struct vnode *tdvp = ap->a_tdvp;
1805 	struct componentname *tcnp = ap->a_tcnp;
1806 	struct componentname *fcnp = ap->a_fcnp;
1807 	struct nfsnode *fnp = VTONFS(ap->a_fvp);
1808 	struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1809 	struct nfsv4node *newv4 = NULL;
1810 	int error;
1811 
1812 	KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1813 	    (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1814 	/* Check for cross-device rename */
1815 	if ((fvp->v_mount != tdvp->v_mount) ||
1816 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
1817 		error = EXDEV;
1818 		goto out;
1819 	}
1820 
1821 	if (fvp == tvp) {
1822 		printf("nfs_rename: fvp == tvp (can't happen)\n");
1823 		error = 0;
1824 		goto out;
1825 	}
1826 	if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
1827 		goto out;
1828 
1829 	/*
1830 	 * We have to flush B_DELWRI data prior to renaming
1831 	 * the file.  If we don't, the delayed-write buffers
1832 	 * can be flushed out later after the file has gone stale
1833 	 * under NFSV3.  NFSV2 does not have this problem because
1834 	 * ( as far as I can tell ) it flushes dirty buffers more
1835 	 * often.
1836 	 *
1837 	 * Skip the rename operation if the fsync fails, this can happen
1838 	 * due to the server's volume being full, when we pushed out data
1839 	 * that was written back to our cache earlier. Not checking for
1840 	 * this condition can result in potential (silent) data loss.
1841 	 */
1842 	error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1843 	NFSVOPUNLOCK(fvp, 0);
1844 	if (!error && tvp)
1845 		error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1846 	if (error)
1847 		goto out;
1848 
1849 	/*
1850 	 * If the tvp exists and is in use, sillyrename it before doing the
1851 	 * rename of the new file over it.
1852 	 * XXX Can't sillyrename a directory.
1853 	 */
1854 	if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1855 		tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1856 		vput(tvp);
1857 		tvp = NULL;
1858 	}
1859 
1860 	error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1861 	    tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1862 	    tcnp->cn_thread);
1863 
1864 	if (error == 0 && NFS_ISV4(tdvp)) {
1865 		/*
1866 		 * For NFSv4, check to see if it is the same name and
1867 		 * replace the name, if it is different.
1868 		 */
1869 		newv4 = malloc(
1870 		    sizeof (struct nfsv4node) +
1871 		    tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
1872 		    M_NFSV4NODE, M_WAITOK);
1873 		mtx_lock(&tdnp->n_mtx);
1874 		mtx_lock(&fnp->n_mtx);
1875 		if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
1876 		    (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
1877 		      NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
1878 		      tcnp->cn_namelen) ||
1879 		      tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
1880 		      NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1881 			tdnp->n_fhp->nfh_len))) {
1882 #ifdef notdef
1883 { char nnn[100]; int nnnl;
1884 nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
1885 bcopy(tcnp->cn_nameptr, nnn, nnnl);
1886 nnn[nnnl] = '\0';
1887 printf("ren replace=%s\n",nnn);
1888 }
1889 #endif
1890 			free(fnp->n_v4, M_NFSV4NODE);
1891 			fnp->n_v4 = newv4;
1892 			newv4 = NULL;
1893 			fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
1894 			fnp->n_v4->n4_namelen = tcnp->cn_namelen;
1895 			NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1896 			    tdnp->n_fhp->nfh_len);
1897 			NFSBCOPY(tcnp->cn_nameptr,
1898 			    NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
1899 		}
1900 		mtx_unlock(&tdnp->n_mtx);
1901 		mtx_unlock(&fnp->n_mtx);
1902 		if (newv4 != NULL)
1903 			free(newv4, M_NFSV4NODE);
1904 	}
1905 
1906 	if (fvp->v_type == VDIR) {
1907 		if (tvp != NULL && tvp->v_type == VDIR)
1908 			cache_purge(tdvp);
1909 		cache_purge(fdvp);
1910 	}
1911 
1912 out:
1913 	if (tdvp == tvp)
1914 		vrele(tdvp);
1915 	else
1916 		vput(tdvp);
1917 	if (tvp)
1918 		vput(tvp);
1919 	vrele(fdvp);
1920 	vrele(fvp);
1921 	/*
1922 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1923 	 */
1924 	if (error == ENOENT)
1925 		error = 0;
1926 	return (error);
1927 }
1928 
1929 /*
1930  * nfs file rename rpc called from nfs_remove() above
1931  */
1932 static int
1933 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
1934     struct sillyrename *sp)
1935 {
1936 
1937 	return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
1938 	    sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
1939 	    scnp->cn_thread));
1940 }
1941 
1942 /*
1943  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1944  */
1945 static int
1946 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
1947     int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
1948     int tnamelen, struct ucred *cred, struct thread *td)
1949 {
1950 	struct nfsvattr fnfsva, tnfsva;
1951 	struct nfsnode *fdnp = VTONFS(fdvp);
1952 	struct nfsnode *tdnp = VTONFS(tdvp);
1953 	int error = 0, fattrflag, tattrflag;
1954 
1955 	error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
1956 	    tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
1957 	    &tattrflag, NULL, NULL);
1958 	mtx_lock(&fdnp->n_mtx);
1959 	fdnp->n_flag |= NMODIFIED;
1960 	if (fattrflag != 0) {
1961 		mtx_unlock(&fdnp->n_mtx);
1962 		(void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
1963 	} else {
1964 		fdnp->n_attrstamp = 0;
1965 		mtx_unlock(&fdnp->n_mtx);
1966 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
1967 	}
1968 	mtx_lock(&tdnp->n_mtx);
1969 	tdnp->n_flag |= NMODIFIED;
1970 	if (tattrflag != 0) {
1971 		mtx_unlock(&tdnp->n_mtx);
1972 		(void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
1973 	} else {
1974 		tdnp->n_attrstamp = 0;
1975 		mtx_unlock(&tdnp->n_mtx);
1976 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1977 	}
1978 	if (error && NFS_ISV4(fdvp))
1979 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1980 	return (error);
1981 }
1982 
1983 /*
1984  * nfs hard link create call
1985  */
1986 static int
1987 nfs_link(struct vop_link_args *ap)
1988 {
1989 	struct vnode *vp = ap->a_vp;
1990 	struct vnode *tdvp = ap->a_tdvp;
1991 	struct componentname *cnp = ap->a_cnp;
1992 	struct nfsnode *np, *tdnp;
1993 	struct nfsvattr nfsva, dnfsva;
1994 	int error = 0, attrflag, dattrflag;
1995 
1996 	/*
1997 	 * Push all writes to the server, so that the attribute cache
1998 	 * doesn't get "out of sync" with the server.
1999 	 * XXX There should be a better way!
2000 	 */
2001 	VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
2002 
2003 	error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
2004 	    cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &attrflag,
2005 	    &dattrflag, NULL);
2006 	tdnp = VTONFS(tdvp);
2007 	mtx_lock(&tdnp->n_mtx);
2008 	tdnp->n_flag |= NMODIFIED;
2009 	if (dattrflag != 0) {
2010 		mtx_unlock(&tdnp->n_mtx);
2011 		(void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
2012 	} else {
2013 		tdnp->n_attrstamp = 0;
2014 		mtx_unlock(&tdnp->n_mtx);
2015 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2016 	}
2017 	if (attrflag)
2018 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2019 	else {
2020 		np = VTONFS(vp);
2021 		mtx_lock(&np->n_mtx);
2022 		np->n_attrstamp = 0;
2023 		mtx_unlock(&np->n_mtx);
2024 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2025 	}
2026 	/*
2027 	 * If negative lookup caching is enabled, I might as well
2028 	 * add an entry for this node. Not necessary for correctness,
2029 	 * but if negative caching is enabled, then the system
2030 	 * must care about lookup caching hit rate, so...
2031 	 */
2032 	if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
2033 	    (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2034 		cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
2035 	}
2036 	if (error && NFS_ISV4(vp))
2037 		error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2038 		    (gid_t)0);
2039 	return (error);
2040 }
2041 
2042 /*
2043  * nfs symbolic link create call
2044  */
2045 static int
2046 nfs_symlink(struct vop_symlink_args *ap)
2047 {
2048 	struct vnode *dvp = ap->a_dvp;
2049 	struct vattr *vap = ap->a_vap;
2050 	struct componentname *cnp = ap->a_cnp;
2051 	struct nfsvattr nfsva, dnfsva;
2052 	struct nfsfh *nfhp;
2053 	struct nfsnode *np = NULL, *dnp;
2054 	struct vnode *newvp = NULL;
2055 	int error = 0, attrflag, dattrflag, ret;
2056 
2057 	vap->va_type = VLNK;
2058 	error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2059 	    ap->a_target, vap, cnp->cn_cred, cnp->cn_thread, &dnfsva,
2060 	    &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
2061 	if (nfhp) {
2062 		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2063 		    &np, NULL, LK_EXCLUSIVE);
2064 		if (!ret)
2065 			newvp = NFSTOV(np);
2066 		else if (!error)
2067 			error = ret;
2068 	}
2069 	if (newvp != NULL) {
2070 		if (attrflag)
2071 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2072 			    0, 1);
2073 	} else if (!error) {
2074 		/*
2075 		 * If we do not have an error and we could not extract the
2076 		 * newvp from the response due to the request being NFSv2, we
2077 		 * have to do a lookup in order to obtain a newvp to return.
2078 		 */
2079 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2080 		    cnp->cn_cred, cnp->cn_thread, &np);
2081 		if (!error)
2082 			newvp = NFSTOV(np);
2083 	}
2084 	if (error) {
2085 		if (newvp)
2086 			vput(newvp);
2087 		if (NFS_ISV4(dvp))
2088 			error = nfscl_maperr(cnp->cn_thread, error,
2089 			    vap->va_uid, vap->va_gid);
2090 	} else {
2091 		*ap->a_vpp = newvp;
2092 	}
2093 
2094 	dnp = VTONFS(dvp);
2095 	mtx_lock(&dnp->n_mtx);
2096 	dnp->n_flag |= NMODIFIED;
2097 	if (dattrflag != 0) {
2098 		mtx_unlock(&dnp->n_mtx);
2099 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2100 	} else {
2101 		dnp->n_attrstamp = 0;
2102 		mtx_unlock(&dnp->n_mtx);
2103 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2104 	}
2105 	/*
2106 	 * If negative lookup caching is enabled, I might as well
2107 	 * add an entry for this node. Not necessary for correctness,
2108 	 * but if negative caching is enabled, then the system
2109 	 * must care about lookup caching hit rate, so...
2110 	 */
2111 	if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2112 	    (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2113 		cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime, NULL);
2114 	}
2115 	return (error);
2116 }
2117 
2118 /*
2119  * nfs make dir call
2120  */
2121 static int
2122 nfs_mkdir(struct vop_mkdir_args *ap)
2123 {
2124 	struct vnode *dvp = ap->a_dvp;
2125 	struct vattr *vap = ap->a_vap;
2126 	struct componentname *cnp = ap->a_cnp;
2127 	struct nfsnode *np = NULL, *dnp;
2128 	struct vnode *newvp = NULL;
2129 	struct vattr vattr;
2130 	struct nfsfh *nfhp;
2131 	struct nfsvattr nfsva, dnfsva;
2132 	int error = 0, attrflag, dattrflag, ret;
2133 
2134 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2135 		return (error);
2136 	vap->va_type = VDIR;
2137 	error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2138 	    vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &nfhp,
2139 	    &attrflag, &dattrflag, NULL);
2140 	dnp = VTONFS(dvp);
2141 	mtx_lock(&dnp->n_mtx);
2142 	dnp->n_flag |= NMODIFIED;
2143 	if (dattrflag != 0) {
2144 		mtx_unlock(&dnp->n_mtx);
2145 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2146 	} else {
2147 		dnp->n_attrstamp = 0;
2148 		mtx_unlock(&dnp->n_mtx);
2149 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2150 	}
2151 	if (nfhp) {
2152 		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2153 		    &np, NULL, LK_EXCLUSIVE);
2154 		if (!ret) {
2155 			newvp = NFSTOV(np);
2156 			if (attrflag)
2157 			   (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2158 				NULL, 0, 1);
2159 		} else if (!error)
2160 			error = ret;
2161 	}
2162 	if (!error && newvp == NULL) {
2163 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2164 		    cnp->cn_cred, cnp->cn_thread, &np);
2165 		if (!error) {
2166 			newvp = NFSTOV(np);
2167 			if (newvp->v_type != VDIR)
2168 				error = EEXIST;
2169 		}
2170 	}
2171 	if (error) {
2172 		if (newvp)
2173 			vput(newvp);
2174 		if (NFS_ISV4(dvp))
2175 			error = nfscl_maperr(cnp->cn_thread, error,
2176 			    vap->va_uid, vap->va_gid);
2177 	} else {
2178 		/*
2179 		 * If negative lookup caching is enabled, I might as well
2180 		 * add an entry for this node. Not necessary for correctness,
2181 		 * but if negative caching is enabled, then the system
2182 		 * must care about lookup caching hit rate, so...
2183 		 */
2184 		if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2185 		    (cnp->cn_flags & MAKEENTRY) &&
2186 		    attrflag != 0 && dattrflag != 0)
2187 			cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2188 			    &dnfsva.na_ctime);
2189 		*ap->a_vpp = newvp;
2190 	}
2191 	return (error);
2192 }
2193 
2194 /*
2195  * nfs remove directory call
2196  */
2197 static int
2198 nfs_rmdir(struct vop_rmdir_args *ap)
2199 {
2200 	struct vnode *vp = ap->a_vp;
2201 	struct vnode *dvp = ap->a_dvp;
2202 	struct componentname *cnp = ap->a_cnp;
2203 	struct nfsnode *dnp;
2204 	struct nfsvattr dnfsva;
2205 	int error, dattrflag;
2206 
2207 	if (dvp == vp)
2208 		return (EINVAL);
2209 	error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2210 	    cnp->cn_cred, cnp->cn_thread, &dnfsva, &dattrflag, NULL);
2211 	dnp = VTONFS(dvp);
2212 	mtx_lock(&dnp->n_mtx);
2213 	dnp->n_flag |= NMODIFIED;
2214 	if (dattrflag != 0) {
2215 		mtx_unlock(&dnp->n_mtx);
2216 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2217 	} else {
2218 		dnp->n_attrstamp = 0;
2219 		mtx_unlock(&dnp->n_mtx);
2220 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2221 	}
2222 
2223 	cache_purge(dvp);
2224 	cache_purge(vp);
2225 	if (error && NFS_ISV4(dvp))
2226 		error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2227 		    (gid_t)0);
2228 	/*
2229 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2230 	 */
2231 	if (error == ENOENT)
2232 		error = 0;
2233 	return (error);
2234 }
2235 
2236 /*
2237  * nfs readdir call
2238  */
2239 static int
2240 nfs_readdir(struct vop_readdir_args *ap)
2241 {
2242 	struct vnode *vp = ap->a_vp;
2243 	struct nfsnode *np = VTONFS(vp);
2244 	struct uio *uio = ap->a_uio;
2245 	ssize_t tresid, left;
2246 	int error = 0;
2247 	struct vattr vattr;
2248 
2249 	if (ap->a_eofflag != NULL)
2250 		*ap->a_eofflag = 0;
2251 	if (vp->v_type != VDIR)
2252 		return(EPERM);
2253 
2254 	/*
2255 	 * First, check for hit on the EOF offset cache
2256 	 */
2257 	if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2258 	    (np->n_flag & NMODIFIED) == 0) {
2259 		if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2260 			mtx_lock(&np->n_mtx);
2261 			if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2262 			    !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2263 				mtx_unlock(&np->n_mtx);
2264 				NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2265 				if (ap->a_eofflag != NULL)
2266 					*ap->a_eofflag = 1;
2267 				return (0);
2268 			} else
2269 				mtx_unlock(&np->n_mtx);
2270 		}
2271 	}
2272 
2273 	/*
2274 	 * NFS always guarantees that directory entries don't straddle
2275 	 * DIRBLKSIZ boundaries.  As such, we need to limit the size
2276 	 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2277 	 * directory entry.
2278 	 */
2279 	left = uio->uio_resid % DIRBLKSIZ;
2280 	if (left == uio->uio_resid)
2281 		return (EINVAL);
2282 	uio->uio_resid -= left;
2283 
2284 	/*
2285 	 * Call ncl_bioread() to do the real work.
2286 	 */
2287 	tresid = uio->uio_resid;
2288 	error = ncl_bioread(vp, uio, 0, ap->a_cred);
2289 
2290 	if (!error && uio->uio_resid == tresid) {
2291 		NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2292 		if (ap->a_eofflag != NULL)
2293 			*ap->a_eofflag = 1;
2294 	}
2295 
2296 	/* Add the partial DIRBLKSIZ (left) back in. */
2297 	uio->uio_resid += left;
2298 	return (error);
2299 }
2300 
2301 /*
2302  * Readdir rpc call.
2303  * Called from below the buffer cache by ncl_doio().
2304  */
2305 int
2306 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2307     struct thread *td)
2308 {
2309 	struct nfsvattr nfsva;
2310 	nfsuint64 *cookiep, cookie;
2311 	struct nfsnode *dnp = VTONFS(vp);
2312 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2313 	int error = 0, eof, attrflag;
2314 
2315 	KASSERT(uiop->uio_iovcnt == 1 &&
2316 	    (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2317 	    (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2318 	    ("nfs readdirrpc bad uio"));
2319 
2320 	/*
2321 	 * If there is no cookie, assume directory was stale.
2322 	 */
2323 	ncl_dircookie_lock(dnp);
2324 	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2325 	if (cookiep) {
2326 		cookie = *cookiep;
2327 		ncl_dircookie_unlock(dnp);
2328 	} else {
2329 		ncl_dircookie_unlock(dnp);
2330 		return (NFSERR_BAD_COOKIE);
2331 	}
2332 
2333 	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2334 		(void)ncl_fsinfo(nmp, vp, cred, td);
2335 
2336 	error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2337 	    &attrflag, &eof, NULL);
2338 	if (attrflag)
2339 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2340 
2341 	if (!error) {
2342 		/*
2343 		 * We are now either at the end of the directory or have filled
2344 		 * the block.
2345 		 */
2346 		if (eof)
2347 			dnp->n_direofoffset = uiop->uio_offset;
2348 		else {
2349 			if (uiop->uio_resid > 0)
2350 				printf("EEK! readdirrpc resid > 0\n");
2351 			ncl_dircookie_lock(dnp);
2352 			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2353 			*cookiep = cookie;
2354 			ncl_dircookie_unlock(dnp);
2355 		}
2356 	} else if (NFS_ISV4(vp)) {
2357 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2358 	}
2359 	return (error);
2360 }
2361 
2362 /*
2363  * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2364  */
2365 int
2366 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2367     struct thread *td)
2368 {
2369 	struct nfsvattr nfsva;
2370 	nfsuint64 *cookiep, cookie;
2371 	struct nfsnode *dnp = VTONFS(vp);
2372 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2373 	int error = 0, attrflag, eof;
2374 
2375 	KASSERT(uiop->uio_iovcnt == 1 &&
2376 	    (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2377 	    (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2378 	    ("nfs readdirplusrpc bad uio"));
2379 
2380 	/*
2381 	 * If there is no cookie, assume directory was stale.
2382 	 */
2383 	ncl_dircookie_lock(dnp);
2384 	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2385 	if (cookiep) {
2386 		cookie = *cookiep;
2387 		ncl_dircookie_unlock(dnp);
2388 	} else {
2389 		ncl_dircookie_unlock(dnp);
2390 		return (NFSERR_BAD_COOKIE);
2391 	}
2392 
2393 	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2394 		(void)ncl_fsinfo(nmp, vp, cred, td);
2395 	error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2396 	    &attrflag, &eof, NULL);
2397 	if (attrflag)
2398 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2399 
2400 	if (!error) {
2401 		/*
2402 		 * We are now either at end of the directory or have filled the
2403 		 * the block.
2404 		 */
2405 		if (eof)
2406 			dnp->n_direofoffset = uiop->uio_offset;
2407 		else {
2408 			if (uiop->uio_resid > 0)
2409 				printf("EEK! readdirplusrpc resid > 0\n");
2410 			ncl_dircookie_lock(dnp);
2411 			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2412 			*cookiep = cookie;
2413 			ncl_dircookie_unlock(dnp);
2414 		}
2415 	} else if (NFS_ISV4(vp)) {
2416 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2417 	}
2418 	return (error);
2419 }
2420 
2421 /*
2422  * Silly rename. To make the NFS filesystem that is stateless look a little
2423  * more like the "ufs" a remove of an active vnode is translated to a rename
2424  * to a funny looking filename that is removed by nfs_inactive on the
2425  * nfsnode. There is the potential for another process on a different client
2426  * to create the same funny name between the nfs_lookitup() fails and the
2427  * nfs_rename() completes, but...
2428  */
2429 static int
2430 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2431 {
2432 	struct sillyrename *sp;
2433 	struct nfsnode *np;
2434 	int error;
2435 	short pid;
2436 	unsigned int lticks;
2437 
2438 	cache_purge(dvp);
2439 	np = VTONFS(vp);
2440 	KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2441 	sp = malloc(sizeof (struct sillyrename),
2442 	    M_NEWNFSREQ, M_WAITOK);
2443 	sp->s_cred = crhold(cnp->cn_cred);
2444 	sp->s_dvp = dvp;
2445 	VREF(dvp);
2446 
2447 	/*
2448 	 * Fudge together a funny name.
2449 	 * Changing the format of the funny name to accommodate more
2450 	 * sillynames per directory.
2451 	 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2452 	 * CPU ticks since boot.
2453 	 */
2454 	pid = cnp->cn_thread->td_proc->p_pid;
2455 	lticks = (unsigned int)ticks;
2456 	for ( ; ; ) {
2457 		sp->s_namlen = sprintf(sp->s_name,
2458 				       ".nfs.%08x.%04x4.4", lticks,
2459 				       pid);
2460 		if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2461 				 cnp->cn_thread, NULL))
2462 			break;
2463 		lticks++;
2464 	}
2465 	error = nfs_renameit(dvp, vp, cnp, sp);
2466 	if (error)
2467 		goto bad;
2468 	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2469 		cnp->cn_thread, &np);
2470 	np->n_sillyrename = sp;
2471 	return (0);
2472 bad:
2473 	vrele(sp->s_dvp);
2474 	crfree(sp->s_cred);
2475 	free(sp, M_NEWNFSREQ);
2476 	return (error);
2477 }
2478 
2479 /*
2480  * Look up a file name and optionally either update the file handle or
2481  * allocate an nfsnode, depending on the value of npp.
2482  * npp == NULL	--> just do the lookup
2483  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2484  *			handled too
2485  * *npp != NULL --> update the file handle in the vnode
2486  */
2487 static int
2488 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2489     struct thread *td, struct nfsnode **npp)
2490 {
2491 	struct vnode *newvp = NULL, *vp;
2492 	struct nfsnode *np, *dnp = VTONFS(dvp);
2493 	struct nfsfh *nfhp, *onfhp;
2494 	struct nfsvattr nfsva, dnfsva;
2495 	struct componentname cn;
2496 	int error = 0, attrflag, dattrflag;
2497 	u_int hash;
2498 
2499 	error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2500 	    &nfhp, &attrflag, &dattrflag, NULL);
2501 	if (dattrflag)
2502 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2503 	if (npp && !error) {
2504 		if (*npp != NULL) {
2505 		    np = *npp;
2506 		    vp = NFSTOV(np);
2507 		    /*
2508 		     * For NFSv4, check to see if it is the same name and
2509 		     * replace the name, if it is different.
2510 		     */
2511 		    if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2512 			(np->n_v4->n4_namelen != len ||
2513 			 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2514 			 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2515 			 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2516 			 dnp->n_fhp->nfh_len))) {
2517 #ifdef notdef
2518 { char nnn[100]; int nnnl;
2519 nnnl = (len < 100) ? len : 99;
2520 bcopy(name, nnn, nnnl);
2521 nnn[nnnl] = '\0';
2522 printf("replace=%s\n",nnn);
2523 }
2524 #endif
2525 			    free(np->n_v4, M_NFSV4NODE);
2526 			    np->n_v4 = malloc(
2527 				sizeof (struct nfsv4node) +
2528 				dnp->n_fhp->nfh_len + len - 1,
2529 				M_NFSV4NODE, M_WAITOK);
2530 			    np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2531 			    np->n_v4->n4_namelen = len;
2532 			    NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2533 				dnp->n_fhp->nfh_len);
2534 			    NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2535 		    }
2536 		    hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2537 			FNV1_32_INIT);
2538 		    onfhp = np->n_fhp;
2539 		    /*
2540 		     * Rehash node for new file handle.
2541 		     */
2542 		    vfs_hash_rehash(vp, hash);
2543 		    np->n_fhp = nfhp;
2544 		    if (onfhp != NULL)
2545 			free(onfhp, M_NFSFH);
2546 		    newvp = NFSTOV(np);
2547 		} else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2548 		    free(nfhp, M_NFSFH);
2549 		    VREF(dvp);
2550 		    newvp = dvp;
2551 		} else {
2552 		    cn.cn_nameptr = name;
2553 		    cn.cn_namelen = len;
2554 		    error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2555 			&np, NULL, LK_EXCLUSIVE);
2556 		    if (error)
2557 			return (error);
2558 		    newvp = NFSTOV(np);
2559 		}
2560 		if (!attrflag && *npp == NULL) {
2561 			if (newvp == dvp)
2562 				vrele(newvp);
2563 			else
2564 				vput(newvp);
2565 			return (ENOENT);
2566 		}
2567 		if (attrflag)
2568 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2569 			    0, 1);
2570 	}
2571 	if (npp && *npp == NULL) {
2572 		if (error) {
2573 			if (newvp) {
2574 				if (newvp == dvp)
2575 					vrele(newvp);
2576 				else
2577 					vput(newvp);
2578 			}
2579 		} else
2580 			*npp = np;
2581 	}
2582 	if (error && NFS_ISV4(dvp))
2583 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2584 	return (error);
2585 }
2586 
2587 /*
2588  * Nfs Version 3 and 4 commit rpc
2589  */
2590 int
2591 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2592    struct thread *td)
2593 {
2594 	struct nfsvattr nfsva;
2595 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2596 	struct nfsnode *np;
2597 	struct uio uio;
2598 	int error, attrflag;
2599 
2600 	np = VTONFS(vp);
2601 	error = EIO;
2602 	attrflag = 0;
2603 	if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) {
2604 		uio.uio_offset = offset;
2605 		uio.uio_resid = cnt;
2606 		error = nfscl_doiods(vp, &uio, NULL, NULL,
2607 		    NFSV4OPEN_ACCESSWRITE, 1, cred, td);
2608 		if (error != 0) {
2609 			mtx_lock(&np->n_mtx);
2610 			np->n_flag &= ~NDSCOMMIT;
2611 			mtx_unlock(&np->n_mtx);
2612 		}
2613 	}
2614 	if (error != 0) {
2615 		mtx_lock(&nmp->nm_mtx);
2616 		if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2617 			mtx_unlock(&nmp->nm_mtx);
2618 			return (0);
2619 		}
2620 		mtx_unlock(&nmp->nm_mtx);
2621 		error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
2622 		    &attrflag, NULL);
2623 	}
2624 	if (attrflag != 0)
2625 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL,
2626 		    0, 1);
2627 	if (error != 0 && NFS_ISV4(vp))
2628 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2629 	return (error);
2630 }
2631 
2632 /*
2633  * Strategy routine.
2634  * For async requests when nfsiod(s) are running, queue the request by
2635  * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2636  * request.
2637  */
2638 static int
2639 nfs_strategy(struct vop_strategy_args *ap)
2640 {
2641 	struct buf *bp;
2642 	struct vnode *vp;
2643 	struct ucred *cr;
2644 
2645 	bp = ap->a_bp;
2646 	vp = ap->a_vp;
2647 	KASSERT(bp->b_vp == vp, ("missing b_getvp"));
2648 	KASSERT(!(bp->b_flags & B_DONE),
2649 	    ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2650 	BUF_ASSERT_HELD(bp);
2651 
2652 	if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno)
2653 		bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize /
2654 		    DEV_BSIZE);
2655 	if (bp->b_iocmd == BIO_READ)
2656 		cr = bp->b_rcred;
2657 	else
2658 		cr = bp->b_wcred;
2659 
2660 	/*
2661 	 * If the op is asynchronous and an i/o daemon is waiting
2662 	 * queue the request, wake it up and wait for completion
2663 	 * otherwise just do it ourselves.
2664 	 */
2665 	if ((bp->b_flags & B_ASYNC) == 0 ||
2666 	    ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread))
2667 		(void) ncl_doio(vp, bp, cr, curthread, 1);
2668 	return (0);
2669 }
2670 
2671 /*
2672  * fsync vnode op. Just call ncl_flush() with commit == 1.
2673  */
2674 /* ARGSUSED */
2675 static int
2676 nfs_fsync(struct vop_fsync_args *ap)
2677 {
2678 
2679 	if (ap->a_vp->v_type != VREG) {
2680 		/*
2681 		 * For NFS, metadata is changed synchronously on the server,
2682 		 * so there is nothing to flush. Also, ncl_flush() clears
2683 		 * the NMODIFIED flag and that shouldn't be done here for
2684 		 * directories.
2685 		 */
2686 		return (0);
2687 	}
2688 	return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0));
2689 }
2690 
2691 /*
2692  * Flush all the blocks associated with a vnode.
2693  * 	Walk through the buffer pool and push any dirty pages
2694  *	associated with the vnode.
2695  * If the called_from_renewthread argument is TRUE, it has been called
2696  * from the NFSv4 renew thread and, as such, cannot block indefinitely
2697  * waiting for a buffer write to complete.
2698  */
2699 int
2700 ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
2701     int commit, int called_from_renewthread)
2702 {
2703 	struct nfsnode *np = VTONFS(vp);
2704 	struct buf *bp;
2705 	int i;
2706 	struct buf *nbp;
2707 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2708 	int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2709 	int passone = 1, trycnt = 0;
2710 	u_quad_t off, endoff, toff;
2711 	struct ucred* wcred = NULL;
2712 	struct buf **bvec = NULL;
2713 	struct bufobj *bo;
2714 #ifndef NFS_COMMITBVECSIZ
2715 #define	NFS_COMMITBVECSIZ	20
2716 #endif
2717 	struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2718 	u_int bvecsize = 0, bveccount;
2719 
2720 	if (called_from_renewthread != 0)
2721 		slptimeo = hz;
2722 	if (nmp->nm_flag & NFSMNT_INT)
2723 		slpflag = PCATCH;
2724 	if (!commit)
2725 		passone = 0;
2726 	bo = &vp->v_bufobj;
2727 	/*
2728 	 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2729 	 * server, but has not been committed to stable storage on the server
2730 	 * yet. On the first pass, the byte range is worked out and the commit
2731 	 * rpc is done. On the second pass, ncl_writebp() is called to do the
2732 	 * job.
2733 	 */
2734 again:
2735 	off = (u_quad_t)-1;
2736 	endoff = 0;
2737 	bvecpos = 0;
2738 	if (NFS_ISV34(vp) && commit) {
2739 		if (bvec != NULL && bvec != bvec_on_stack)
2740 			free(bvec, M_TEMP);
2741 		/*
2742 		 * Count up how many buffers waiting for a commit.
2743 		 */
2744 		bveccount = 0;
2745 		BO_LOCK(bo);
2746 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2747 			if (!BUF_ISLOCKED(bp) &&
2748 			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2749 				== (B_DELWRI | B_NEEDCOMMIT))
2750 				bveccount++;
2751 		}
2752 		/*
2753 		 * Allocate space to remember the list of bufs to commit.  It is
2754 		 * important to use M_NOWAIT here to avoid a race with nfs_write.
2755 		 * If we can't get memory (for whatever reason), we will end up
2756 		 * committing the buffers one-by-one in the loop below.
2757 		 */
2758 		if (bveccount > NFS_COMMITBVECSIZ) {
2759 			/*
2760 			 * Release the vnode interlock to avoid a lock
2761 			 * order reversal.
2762 			 */
2763 			BO_UNLOCK(bo);
2764 			bvec = (struct buf **)
2765 				malloc(bveccount * sizeof(struct buf *),
2766 				       M_TEMP, M_NOWAIT);
2767 			BO_LOCK(bo);
2768 			if (bvec == NULL) {
2769 				bvec = bvec_on_stack;
2770 				bvecsize = NFS_COMMITBVECSIZ;
2771 			} else
2772 				bvecsize = bveccount;
2773 		} else {
2774 			bvec = bvec_on_stack;
2775 			bvecsize = NFS_COMMITBVECSIZ;
2776 		}
2777 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2778 			if (bvecpos >= bvecsize)
2779 				break;
2780 			if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2781 				nbp = TAILQ_NEXT(bp, b_bobufs);
2782 				continue;
2783 			}
2784 			if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2785 			    (B_DELWRI | B_NEEDCOMMIT)) {
2786 				BUF_UNLOCK(bp);
2787 				nbp = TAILQ_NEXT(bp, b_bobufs);
2788 				continue;
2789 			}
2790 			BO_UNLOCK(bo);
2791 			bremfree(bp);
2792 			/*
2793 			 * Work out if all buffers are using the same cred
2794 			 * so we can deal with them all with one commit.
2795 			 *
2796 			 * NOTE: we are not clearing B_DONE here, so we have
2797 			 * to do it later on in this routine if we intend to
2798 			 * initiate I/O on the bp.
2799 			 *
2800 			 * Note: to avoid loopback deadlocks, we do not
2801 			 * assign b_runningbufspace.
2802 			 */
2803 			if (wcred == NULL)
2804 				wcred = bp->b_wcred;
2805 			else if (wcred != bp->b_wcred)
2806 				wcred = NOCRED;
2807 			vfs_busy_pages(bp, 1);
2808 
2809 			BO_LOCK(bo);
2810 			/*
2811 			 * bp is protected by being locked, but nbp is not
2812 			 * and vfs_busy_pages() may sleep.  We have to
2813 			 * recalculate nbp.
2814 			 */
2815 			nbp = TAILQ_NEXT(bp, b_bobufs);
2816 
2817 			/*
2818 			 * A list of these buffers is kept so that the
2819 			 * second loop knows which buffers have actually
2820 			 * been committed. This is necessary, since there
2821 			 * may be a race between the commit rpc and new
2822 			 * uncommitted writes on the file.
2823 			 */
2824 			bvec[bvecpos++] = bp;
2825 			toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2826 				bp->b_dirtyoff;
2827 			if (toff < off)
2828 				off = toff;
2829 			toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2830 			if (toff > endoff)
2831 				endoff = toff;
2832 		}
2833 		BO_UNLOCK(bo);
2834 	}
2835 	if (bvecpos > 0) {
2836 		/*
2837 		 * Commit data on the server, as required.
2838 		 * If all bufs are using the same wcred, then use that with
2839 		 * one call for all of them, otherwise commit each one
2840 		 * separately.
2841 		 */
2842 		if (wcred != NOCRED)
2843 			retv = ncl_commit(vp, off, (int)(endoff - off),
2844 					  wcred, td);
2845 		else {
2846 			retv = 0;
2847 			for (i = 0; i < bvecpos; i++) {
2848 				off_t off, size;
2849 				bp = bvec[i];
2850 				off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2851 					bp->b_dirtyoff;
2852 				size = (u_quad_t)(bp->b_dirtyend
2853 						  - bp->b_dirtyoff);
2854 				retv = ncl_commit(vp, off, (int)size,
2855 						  bp->b_wcred, td);
2856 				if (retv) break;
2857 			}
2858 		}
2859 
2860 		if (retv == NFSERR_STALEWRITEVERF)
2861 			ncl_clearcommit(vp->v_mount);
2862 
2863 		/*
2864 		 * Now, either mark the blocks I/O done or mark the
2865 		 * blocks dirty, depending on whether the commit
2866 		 * succeeded.
2867 		 */
2868 		for (i = 0; i < bvecpos; i++) {
2869 			bp = bvec[i];
2870 			bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2871 			if (retv) {
2872 				/*
2873 				 * Error, leave B_DELWRI intact
2874 				 */
2875 				vfs_unbusy_pages(bp);
2876 				brelse(bp);
2877 			} else {
2878 				/*
2879 				 * Success, remove B_DELWRI ( bundirty() ).
2880 				 *
2881 				 * b_dirtyoff/b_dirtyend seem to be NFS
2882 				 * specific.  We should probably move that
2883 				 * into bundirty(). XXX
2884 				 */
2885 				bufobj_wref(bo);
2886 				bp->b_flags |= B_ASYNC;
2887 				bundirty(bp);
2888 				bp->b_flags &= ~B_DONE;
2889 				bp->b_ioflags &= ~BIO_ERROR;
2890 				bp->b_dirtyoff = bp->b_dirtyend = 0;
2891 				bufdone(bp);
2892 			}
2893 		}
2894 	}
2895 
2896 	/*
2897 	 * Start/do any write(s) that are required.
2898 	 */
2899 loop:
2900 	BO_LOCK(bo);
2901 	TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2902 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2903 			if (waitfor != MNT_WAIT || passone)
2904 				continue;
2905 
2906 			error = BUF_TIMELOCK(bp,
2907 			    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2908 			    BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
2909 			if (error == 0) {
2910 				BUF_UNLOCK(bp);
2911 				goto loop;
2912 			}
2913 			if (error == ENOLCK) {
2914 				error = 0;
2915 				goto loop;
2916 			}
2917 			if (called_from_renewthread != 0) {
2918 				/*
2919 				 * Return EIO so the flush will be retried
2920 				 * later.
2921 				 */
2922 				error = EIO;
2923 				goto done;
2924 			}
2925 			if (newnfs_sigintr(nmp, td)) {
2926 				error = EINTR;
2927 				goto done;
2928 			}
2929 			if (slpflag == PCATCH) {
2930 				slpflag = 0;
2931 				slptimeo = 2 * hz;
2932 			}
2933 			goto loop;
2934 		}
2935 		if ((bp->b_flags & B_DELWRI) == 0)
2936 			panic("nfs_fsync: not dirty");
2937 		if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2938 			BUF_UNLOCK(bp);
2939 			continue;
2940 		}
2941 		BO_UNLOCK(bo);
2942 		bremfree(bp);
2943 		if (passone || !commit)
2944 		    bp->b_flags |= B_ASYNC;
2945 		else
2946 		    bp->b_flags |= B_ASYNC;
2947 		bwrite(bp);
2948 		if (newnfs_sigintr(nmp, td)) {
2949 			error = EINTR;
2950 			goto done;
2951 		}
2952 		goto loop;
2953 	}
2954 	if (passone) {
2955 		passone = 0;
2956 		BO_UNLOCK(bo);
2957 		goto again;
2958 	}
2959 	if (waitfor == MNT_WAIT) {
2960 		while (bo->bo_numoutput) {
2961 			error = bufobj_wwait(bo, slpflag, slptimeo);
2962 			if (error) {
2963 			    BO_UNLOCK(bo);
2964 			    if (called_from_renewthread != 0) {
2965 				/*
2966 				 * Return EIO so that the flush will be
2967 				 * retried later.
2968 				 */
2969 				error = EIO;
2970 				goto done;
2971 			    }
2972 			    error = newnfs_sigintr(nmp, td);
2973 			    if (error)
2974 				goto done;
2975 			    if (slpflag == PCATCH) {
2976 				slpflag = 0;
2977 				slptimeo = 2 * hz;
2978 			    }
2979 			    BO_LOCK(bo);
2980 			}
2981 		}
2982 		if (bo->bo_dirty.bv_cnt != 0 && commit) {
2983 			BO_UNLOCK(bo);
2984 			goto loop;
2985 		}
2986 		/*
2987 		 * Wait for all the async IO requests to drain
2988 		 */
2989 		BO_UNLOCK(bo);
2990 		mtx_lock(&np->n_mtx);
2991 		while (np->n_directio_asyncwr > 0) {
2992 			np->n_flag |= NFSYNCWAIT;
2993 			error = newnfs_msleep(td, &np->n_directio_asyncwr,
2994 			    &np->n_mtx, slpflag | (PRIBIO + 1),
2995 			    "nfsfsync", 0);
2996 			if (error) {
2997 				if (newnfs_sigintr(nmp, td)) {
2998 					mtx_unlock(&np->n_mtx);
2999 					error = EINTR;
3000 					goto done;
3001 				}
3002 			}
3003 		}
3004 		mtx_unlock(&np->n_mtx);
3005 	} else
3006 		BO_UNLOCK(bo);
3007 	if (NFSHASPNFS(nmp)) {
3008 		nfscl_layoutcommit(vp, td);
3009 		/*
3010 		 * Invalidate the attribute cache, since writes to a DS
3011 		 * won't update the size attribute.
3012 		 */
3013 		mtx_lock(&np->n_mtx);
3014 		np->n_attrstamp = 0;
3015 	} else
3016 		mtx_lock(&np->n_mtx);
3017 	if (np->n_flag & NWRITEERR) {
3018 		error = np->n_error;
3019 		np->n_flag &= ~NWRITEERR;
3020 	}
3021   	if (commit && bo->bo_dirty.bv_cnt == 0 &&
3022 	    bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
3023   		np->n_flag &= ~NMODIFIED;
3024 	mtx_unlock(&np->n_mtx);
3025 done:
3026 	if (bvec != NULL && bvec != bvec_on_stack)
3027 		free(bvec, M_TEMP);
3028 	if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
3029 	    (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
3030 	    np->n_directio_asyncwr != 0)) {
3031 		if (trycnt++ < 5) {
3032 			/* try, try again... */
3033 			passone = 1;
3034 			wcred = NULL;
3035 			bvec = NULL;
3036 			bvecsize = 0;
3037 			goto again;
3038 		}
3039 		vn_printf(vp, "ncl_flush failed");
3040 		error = called_from_renewthread != 0 ? EIO : EBUSY;
3041 	}
3042 	return (error);
3043 }
3044 
3045 /*
3046  * NFS advisory byte-level locks.
3047  */
3048 static int
3049 nfs_advlock(struct vop_advlock_args *ap)
3050 {
3051 	struct vnode *vp = ap->a_vp;
3052 	struct ucred *cred;
3053 	struct nfsnode *np = VTONFS(ap->a_vp);
3054 	struct proc *p = (struct proc *)ap->a_id;
3055 	struct thread *td = curthread;	/* XXX */
3056 	struct vattr va;
3057 	int ret, error;
3058 	u_quad_t size;
3059 
3060 	error = NFSVOPLOCK(vp, LK_SHARED);
3061 	if (error != 0)
3062 		return (EBADF);
3063 	if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3064 		if (vp->v_type != VREG) {
3065 			error = EINVAL;
3066 			goto out;
3067 		}
3068 		if ((ap->a_flags & F_POSIX) != 0)
3069 			cred = p->p_ucred;
3070 		else
3071 			cred = td->td_ucred;
3072 		NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
3073 		if (vp->v_iflag & VI_DOOMED) {
3074 			error = EBADF;
3075 			goto out;
3076 		}
3077 
3078 		/*
3079 		 * If this is unlocking a write locked region, flush and
3080 		 * commit them before unlocking. This is required by
3081 		 * RFC3530 Sec. 9.3.2.
3082 		 */
3083 		if (ap->a_op == F_UNLCK &&
3084 		    nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3085 		    ap->a_flags))
3086 			(void) ncl_flush(vp, MNT_WAIT, td, 1, 0);
3087 
3088 		/*
3089 		 * Loop around doing the lock op, while a blocking lock
3090 		 * must wait for the lock op to succeed.
3091 		 */
3092 		do {
3093 			ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3094 			    ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3095 			if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3096 			    ap->a_op == F_SETLK) {
3097 				NFSVOPUNLOCK(vp, 0);
3098 				error = nfs_catnap(PZERO | PCATCH, ret,
3099 				    "ncladvl");
3100 				if (error)
3101 					return (EINTR);
3102 				NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3103 				if (vp->v_iflag & VI_DOOMED) {
3104 					error = EBADF;
3105 					goto out;
3106 				}
3107 			}
3108 		} while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3109 		     ap->a_op == F_SETLK);
3110 		if (ret == NFSERR_DENIED) {
3111 			error = EAGAIN;
3112 			goto out;
3113 		} else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3114 			error = ret;
3115 			goto out;
3116 		} else if (ret != 0) {
3117 			error = EACCES;
3118 			goto out;
3119 		}
3120 
3121 		/*
3122 		 * Now, if we just got a lock, invalidate data in the buffer
3123 		 * cache, as required, so that the coherency conforms with
3124 		 * RFC3530 Sec. 9.3.2.
3125 		 */
3126 		if (ap->a_op == F_SETLK) {
3127 			if ((np->n_flag & NMODIFIED) == 0) {
3128 				np->n_attrstamp = 0;
3129 				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3130 				ret = VOP_GETATTR(vp, &va, cred);
3131 			}
3132 			if ((np->n_flag & NMODIFIED) || ret ||
3133 			    np->n_change != va.va_filerev) {
3134 				(void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3135 				np->n_attrstamp = 0;
3136 				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3137 				ret = VOP_GETATTR(vp, &va, cred);
3138 				if (!ret) {
3139 					np->n_mtime = va.va_mtime;
3140 					np->n_change = va.va_filerev;
3141 				}
3142 			}
3143 			/* Mark that a file lock has been acquired. */
3144 			mtx_lock(&np->n_mtx);
3145 			np->n_flag |= NHASBEENLOCKED;
3146 			mtx_unlock(&np->n_mtx);
3147 		}
3148 	} else if (!NFS_ISV4(vp)) {
3149 		if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3150 			size = VTONFS(vp)->n_size;
3151 			NFSVOPUNLOCK(vp, 0);
3152 			error = lf_advlock(ap, &(vp->v_lockf), size);
3153 		} else {
3154 			if (nfs_advlock_p != NULL)
3155 				error = nfs_advlock_p(ap);
3156 			else {
3157 				NFSVOPUNLOCK(vp, 0);
3158 				error = ENOLCK;
3159 			}
3160 		}
3161 		if (error == 0 && ap->a_op == F_SETLK) {
3162 			error = NFSVOPLOCK(vp, LK_SHARED);
3163 			if (error == 0) {
3164 				/* Mark that a file lock has been acquired. */
3165 				mtx_lock(&np->n_mtx);
3166 				np->n_flag |= NHASBEENLOCKED;
3167 				mtx_unlock(&np->n_mtx);
3168 				NFSVOPUNLOCK(vp, 0);
3169 			}
3170 		}
3171 		return (error);
3172 	} else
3173 		error = EOPNOTSUPP;
3174 out:
3175 	NFSVOPUNLOCK(vp, 0);
3176 	return (error);
3177 }
3178 
3179 /*
3180  * NFS advisory byte-level locks.
3181  */
3182 static int
3183 nfs_advlockasync(struct vop_advlockasync_args *ap)
3184 {
3185 	struct vnode *vp = ap->a_vp;
3186 	u_quad_t size;
3187 	int error;
3188 
3189 	if (NFS_ISV4(vp))
3190 		return (EOPNOTSUPP);
3191 	error = NFSVOPLOCK(vp, LK_SHARED);
3192 	if (error)
3193 		return (error);
3194 	if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3195 		size = VTONFS(vp)->n_size;
3196 		NFSVOPUNLOCK(vp, 0);
3197 		error = lf_advlockasync(ap, &(vp->v_lockf), size);
3198 	} else {
3199 		NFSVOPUNLOCK(vp, 0);
3200 		error = EOPNOTSUPP;
3201 	}
3202 	return (error);
3203 }
3204 
3205 /*
3206  * Print out the contents of an nfsnode.
3207  */
3208 static int
3209 nfs_print(struct vop_print_args *ap)
3210 {
3211 	struct vnode *vp = ap->a_vp;
3212 	struct nfsnode *np = VTONFS(vp);
3213 
3214 	printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid,
3215 	    (uintmax_t)np->n_vattr.na_fsid);
3216 	if (vp->v_type == VFIFO)
3217 		fifo_printinfo(vp);
3218 	printf("\n");
3219 	return (0);
3220 }
3221 
3222 /*
3223  * This is the "real" nfs::bwrite(struct buf*).
3224  * We set B_CACHE if this is a VMIO buffer.
3225  */
3226 int
3227 ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
3228 {
3229 	int oldflags, rtval;
3230 
3231 	BUF_ASSERT_HELD(bp);
3232 
3233 	if (bp->b_flags & B_INVAL) {
3234 		brelse(bp);
3235 		return (0);
3236 	}
3237 
3238 	oldflags = bp->b_flags;
3239 	bp->b_flags |= B_CACHE;
3240 
3241 	/*
3242 	 * Undirty the bp.  We will redirty it later if the I/O fails.
3243 	 */
3244 	bundirty(bp);
3245 	bp->b_flags &= ~B_DONE;
3246 	bp->b_ioflags &= ~BIO_ERROR;
3247 	bp->b_iocmd = BIO_WRITE;
3248 
3249 	bufobj_wref(bp->b_bufobj);
3250 	curthread->td_ru.ru_oublock++;
3251 
3252 	/*
3253 	 * Note: to avoid loopback deadlocks, we do not
3254 	 * assign b_runningbufspace.
3255 	 */
3256 	vfs_busy_pages(bp, 1);
3257 
3258 	BUF_KERNPROC(bp);
3259 	bp->b_iooffset = dbtob(bp->b_blkno);
3260 	bstrategy(bp);
3261 
3262 	if ((oldflags & B_ASYNC) != 0)
3263 		return (0);
3264 
3265 	rtval = bufwait(bp);
3266 	if (oldflags & B_DELWRI)
3267 		reassignbuf(bp);
3268 	brelse(bp);
3269 	return (rtval);
3270 }
3271 
3272 /*
3273  * nfs special file access vnode op.
3274  * Essentially just get vattr and then imitate iaccess() since the device is
3275  * local to the client.
3276  */
3277 static int
3278 nfsspec_access(struct vop_access_args *ap)
3279 {
3280 	struct vattr *vap;
3281 	struct ucred *cred = ap->a_cred;
3282 	struct vnode *vp = ap->a_vp;
3283 	accmode_t accmode = ap->a_accmode;
3284 	struct vattr vattr;
3285 	int error;
3286 
3287 	/*
3288 	 * Disallow write attempts on filesystems mounted read-only;
3289 	 * unless the file is a socket, fifo, or a block or character
3290 	 * device resident on the filesystem.
3291 	 */
3292 	if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3293 		switch (vp->v_type) {
3294 		case VREG:
3295 		case VDIR:
3296 		case VLNK:
3297 			return (EROFS);
3298 		default:
3299 			break;
3300 		}
3301 	}
3302 	vap = &vattr;
3303 	error = VOP_GETATTR(vp, vap, cred);
3304 	if (error)
3305 		goto out;
3306 	error  = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3307 	    accmode, cred, NULL);
3308 out:
3309 	return error;
3310 }
3311 
3312 /*
3313  * Read wrapper for fifos.
3314  */
3315 static int
3316 nfsfifo_read(struct vop_read_args *ap)
3317 {
3318 	struct nfsnode *np = VTONFS(ap->a_vp);
3319 	int error;
3320 
3321 	/*
3322 	 * Set access flag.
3323 	 */
3324 	mtx_lock(&np->n_mtx);
3325 	np->n_flag |= NACC;
3326 	vfs_timestamp(&np->n_atim);
3327 	mtx_unlock(&np->n_mtx);
3328 	error = fifo_specops.vop_read(ap);
3329 	return error;
3330 }
3331 
3332 /*
3333  * Write wrapper for fifos.
3334  */
3335 static int
3336 nfsfifo_write(struct vop_write_args *ap)
3337 {
3338 	struct nfsnode *np = VTONFS(ap->a_vp);
3339 
3340 	/*
3341 	 * Set update flag.
3342 	 */
3343 	mtx_lock(&np->n_mtx);
3344 	np->n_flag |= NUPD;
3345 	vfs_timestamp(&np->n_mtim);
3346 	mtx_unlock(&np->n_mtx);
3347 	return(fifo_specops.vop_write(ap));
3348 }
3349 
3350 /*
3351  * Close wrapper for fifos.
3352  *
3353  * Update the times on the nfsnode then do fifo close.
3354  */
3355 static int
3356 nfsfifo_close(struct vop_close_args *ap)
3357 {
3358 	struct vnode *vp = ap->a_vp;
3359 	struct nfsnode *np = VTONFS(vp);
3360 	struct vattr vattr;
3361 	struct timespec ts;
3362 
3363 	mtx_lock(&np->n_mtx);
3364 	if (np->n_flag & (NACC | NUPD)) {
3365 		vfs_timestamp(&ts);
3366 		if (np->n_flag & NACC)
3367 			np->n_atim = ts;
3368 		if (np->n_flag & NUPD)
3369 			np->n_mtim = ts;
3370 		np->n_flag |= NCHG;
3371 		if (vrefcnt(vp) == 1 &&
3372 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3373 			VATTR_NULL(&vattr);
3374 			if (np->n_flag & NACC)
3375 				vattr.va_atime = np->n_atim;
3376 			if (np->n_flag & NUPD)
3377 				vattr.va_mtime = np->n_mtim;
3378 			mtx_unlock(&np->n_mtx);
3379 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3380 			goto out;
3381 		}
3382 	}
3383 	mtx_unlock(&np->n_mtx);
3384 out:
3385 	return (fifo_specops.vop_close(ap));
3386 }
3387 
3388 /*
3389  * Just call ncl_writebp() with the force argument set to 1.
3390  *
3391  * NOTE: B_DONE may or may not be set in a_bp on call.
3392  */
3393 static int
3394 nfs_bwrite(struct buf *bp)
3395 {
3396 
3397 	return (ncl_writebp(bp, 1, curthread));
3398 }
3399 
3400 struct buf_ops buf_ops_newnfs = {
3401 	.bop_name	=	"buf_ops_nfs",
3402 	.bop_write	=	nfs_bwrite,
3403 	.bop_strategy	=	bufstrategy,
3404 	.bop_sync	=	bufsync,
3405 	.bop_bdflush	=	bufbdflush,
3406 };
3407 
3408 static int
3409 nfs_getacl(struct vop_getacl_args *ap)
3410 {
3411 	int error;
3412 
3413 	if (ap->a_type != ACL_TYPE_NFS4)
3414 		return (EOPNOTSUPP);
3415 	error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3416 	    NULL);
3417 	if (error > NFSERR_STALE) {
3418 		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3419 		error = EPERM;
3420 	}
3421 	return (error);
3422 }
3423 
3424 static int
3425 nfs_setacl(struct vop_setacl_args *ap)
3426 {
3427 	int error;
3428 
3429 	if (ap->a_type != ACL_TYPE_NFS4)
3430 		return (EOPNOTSUPP);
3431 	error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3432 	    NULL);
3433 	if (error > NFSERR_STALE) {
3434 		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3435 		error = EPERM;
3436 	}
3437 	return (error);
3438 }
3439 
3440 /*
3441  * Return POSIX pathconf information applicable to nfs filesystems.
3442  */
3443 static int
3444 nfs_pathconf(struct vop_pathconf_args *ap)
3445 {
3446 	struct nfsv3_pathconf pc;
3447 	struct nfsvattr nfsva;
3448 	struct vnode *vp = ap->a_vp;
3449 	struct thread *td = curthread;
3450 	int attrflag, error;
3451 
3452 	if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
3453 	    ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
3454 	    ap->a_name == _PC_NO_TRUNC)) ||
3455 	    (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) {
3456 		/*
3457 		 * Since only the above 4 a_names are returned by the NFSv3
3458 		 * Pathconf RPC, there is no point in doing it for others.
3459 		 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
3460 		 * be used for _PC_NFS4_ACL as well.
3461 		 */
3462 		error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
3463 		    &attrflag, NULL);
3464 		if (attrflag != 0)
3465 			(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
3466 			    1);
3467 		if (error != 0)
3468 			return (error);
3469 	} else {
3470 		/*
3471 		 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
3472 		 * just fake them.
3473 		 */
3474 		pc.pc_linkmax = NFS_LINK_MAX;
3475 		pc.pc_namemax = NFS_MAXNAMLEN;
3476 		pc.pc_notrunc = 1;
3477 		pc.pc_chownrestricted = 1;
3478 		pc.pc_caseinsensitive = 0;
3479 		pc.pc_casepreserving = 1;
3480 		error = 0;
3481 	}
3482 	switch (ap->a_name) {
3483 	case _PC_LINK_MAX:
3484 #ifdef _LP64
3485 		*ap->a_retval = pc.pc_linkmax;
3486 #else
3487 		*ap->a_retval = MIN(LONG_MAX, pc.pc_linkmax);
3488 #endif
3489 		break;
3490 	case _PC_NAME_MAX:
3491 		*ap->a_retval = pc.pc_namemax;
3492 		break;
3493 	case _PC_PIPE_BUF:
3494 		if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
3495 			*ap->a_retval = PIPE_BUF;
3496 		else
3497 			error = EINVAL;
3498 		break;
3499 	case _PC_CHOWN_RESTRICTED:
3500 		*ap->a_retval = pc.pc_chownrestricted;
3501 		break;
3502 	case _PC_NO_TRUNC:
3503 		*ap->a_retval = pc.pc_notrunc;
3504 		break;
3505 	case _PC_ACL_NFS4:
3506 		if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
3507 		    NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
3508 			*ap->a_retval = 1;
3509 		else
3510 			*ap->a_retval = 0;
3511 		break;
3512 	case _PC_ACL_PATH_MAX:
3513 		if (NFS_ISV4(vp))
3514 			*ap->a_retval = ACL_MAX_ENTRIES;
3515 		else
3516 			*ap->a_retval = 3;
3517 		break;
3518 	case _PC_PRIO_IO:
3519 		*ap->a_retval = 0;
3520 		break;
3521 	case _PC_SYNC_IO:
3522 		*ap->a_retval = 0;
3523 		break;
3524 	case _PC_ALLOC_SIZE_MIN:
3525 		*ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
3526 		break;
3527 	case _PC_FILESIZEBITS:
3528 		if (NFS_ISV34(vp))
3529 			*ap->a_retval = 64;
3530 		else
3531 			*ap->a_retval = 32;
3532 		break;
3533 	case _PC_REC_INCR_XFER_SIZE:
3534 		*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3535 		break;
3536 	case _PC_REC_MAX_XFER_SIZE:
3537 		*ap->a_retval = -1; /* means ``unlimited'' */
3538 		break;
3539 	case _PC_REC_MIN_XFER_SIZE:
3540 		*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3541 		break;
3542 	case _PC_REC_XFER_ALIGN:
3543 		*ap->a_retval = PAGE_SIZE;
3544 		break;
3545 	case _PC_SYMLINK_MAX:
3546 		*ap->a_retval = NFS_MAXPATHLEN;
3547 		break;
3548 
3549 	default:
3550 		error = vop_stdpathconf(ap);
3551 		break;
3552 	}
3553 	return (error);
3554 }
3555 
3556