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