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