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