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