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