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