nfs_clvnops.c revision 264842
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: head/sys/fs/nfsclient/nfs_clvnops.c 264842 2014-04-23 22:13:10Z rmacklem $");
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
84dtrace_nfsclient_accesscache_flush_probe_func_t
85		dtrace_nfscl_accesscache_flush_done_probe;
86uint32_t	nfscl_accesscache_flush_done_id;
87
88dtrace_nfsclient_accesscache_get_probe_func_t
89		dtrace_nfscl_accesscache_get_hit_probe,
90		dtrace_nfscl_accesscache_get_miss_probe;
91uint32_t	nfscl_accesscache_get_hit_id;
92uint32_t	nfscl_accesscache_get_miss_id;
93
94dtrace_nfsclient_accesscache_load_probe_func_t
95		dtrace_nfscl_accesscache_load_done_probe;
96uint32_t	nfscl_accesscache_load_done_id;
97#endif /* !KDTRACE_HOOKS */
98
99/* Defs */
100#define	TRUE	1
101#define	FALSE	0
102
103extern struct nfsstats newnfsstats;
104extern int nfsrv_useacl;
105extern int nfscl_debuglevel;
106MALLOC_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
117static vop_read_t	nfsfifo_read;
118static vop_write_t	nfsfifo_write;
119static vop_close_t	nfsfifo_close;
120static int	nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
121		    struct thread *);
122static vop_lookup_t	nfs_lookup;
123static vop_create_t	nfs_create;
124static vop_mknod_t	nfs_mknod;
125static vop_open_t	nfs_open;
126static vop_pathconf_t	nfs_pathconf;
127static vop_close_t	nfs_close;
128static vop_access_t	nfs_access;
129static vop_getattr_t	nfs_getattr;
130static vop_setattr_t	nfs_setattr;
131static vop_read_t	nfs_read;
132static vop_fsync_t	nfs_fsync;
133static vop_remove_t	nfs_remove;
134static vop_link_t	nfs_link;
135static vop_rename_t	nfs_rename;
136static vop_mkdir_t	nfs_mkdir;
137static vop_rmdir_t	nfs_rmdir;
138static vop_symlink_t	nfs_symlink;
139static vop_readdir_t	nfs_readdir;
140static vop_strategy_t	nfs_strategy;
141static vop_lock1_t	nfs_lock1;
142static	int	nfs_lookitup(struct vnode *, char *, int,
143		    struct ucred *, struct thread *, struct nfsnode **);
144static	int	nfs_sillyrename(struct vnode *, struct vnode *,
145		    struct componentname *);
146static vop_access_t	nfsspec_access;
147static vop_readlink_t	nfs_readlink;
148static vop_print_t	nfs_print;
149static vop_advlock_t	nfs_advlock;
150static vop_advlockasync_t nfs_advlockasync;
151static vop_getacl_t nfs_getacl;
152static vop_setacl_t nfs_setacl;
153
154/*
155 * Global vfs data structures for nfs
156 */
157struct 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
192struct 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
206static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
207    struct componentname *cnp, struct vattr *vap);
208static int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
209    int namelen, struct ucred *cred, struct thread *td);
210static 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);
213static 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
221SYSCTL_DECL(_vfs_nfs);
222
223static int	nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
224SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
225	   &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
226
227static int	nfs_prime_access_cache = 0;
228SYSCTL_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
232static int	newnfs_commit_on_close = 0;
233SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
234    &newnfs_commit_on_close, 0, "write+commit on close, else only write");
235
236static int	nfs_clean_pages_on_close = 1;
237SYSCTL_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
240int newnfs_directio_enable = 0;
241SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
242	   &newnfs_directio_enable, 0, "Enable NFS directio");
243
244int nfs_keep_dirty_on_error;
245SYSCTL_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 */
259int newnfs_directio_allow_mmap = 1;
260SYSCTL_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
264SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
265	   &newnfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
266
267SYSCTL_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
291static int
292nfs34_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 */
343static int
344nfs_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 */
508static int
509nfs_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 */
669static int
670nfs_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 */
813static int
814nfs_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 */
887static int
888nfs_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 */
1002static int
1003nfs_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 */
1035static int
1036nfs_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) && cnp->cn_nameiop != CREATE &&
1188		    dattrflag) {
1189			/*
1190			 * Cache the modification time of the parent
1191			 * directory from the post-op attributes in
1192			 * the name cache entry.  The negative cache
1193			 * entry will be ignored once the directory
1194			 * has changed.  Don't bother adding the entry
1195			 * if the directory has already changed.
1196			 */
1197			mtx_lock(&np->n_mtx);
1198			if (timespeccmp(&np->n_vattr.na_mtime,
1199			    &dnfsva.na_mtime, ==)) {
1200				mtx_unlock(&np->n_mtx);
1201				cache_enter_time(dvp, NULL, cnp,
1202				    &dnfsva.na_mtime, NULL);
1203			} else
1204				mtx_unlock(&np->n_mtx);
1205		}
1206		return (ENOENT);
1207	}
1208
1209	/*
1210	 * Handle RENAME case...
1211	 */
1212	if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1213		if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1214			FREE((caddr_t)nfhp, M_NFSFH);
1215			return (EISDIR);
1216		}
1217		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1218		    LK_EXCLUSIVE);
1219		if (error)
1220			return (error);
1221		newvp = NFSTOV(np);
1222		if (attrflag)
1223			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1224			    0, 1);
1225		*vpp = newvp;
1226		cnp->cn_flags |= SAVENAME;
1227		return (0);
1228	}
1229
1230	if (flags & ISDOTDOT) {
1231		ltype = NFSVOPISLOCKED(dvp);
1232		error = vfs_busy(mp, MBF_NOWAIT);
1233		if (error != 0) {
1234			vfs_ref(mp);
1235			NFSVOPUNLOCK(dvp, 0);
1236			error = vfs_busy(mp, 0);
1237			NFSVOPLOCK(dvp, ltype | LK_RETRY);
1238			vfs_rel(mp);
1239			if (error == 0 && (dvp->v_iflag & VI_DOOMED)) {
1240				vfs_unbusy(mp);
1241				error = ENOENT;
1242			}
1243			if (error != 0)
1244				return (error);
1245		}
1246		NFSVOPUNLOCK(dvp, 0);
1247		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1248		    cnp->cn_lkflags);
1249		if (error == 0)
1250			newvp = NFSTOV(np);
1251		vfs_unbusy(mp);
1252		if (newvp != dvp)
1253			NFSVOPLOCK(dvp, ltype | LK_RETRY);
1254		if (dvp->v_iflag & VI_DOOMED) {
1255			if (error == 0) {
1256				if (newvp == dvp)
1257					vrele(newvp);
1258				else
1259					vput(newvp);
1260			}
1261			error = ENOENT;
1262		}
1263		if (error != 0)
1264			return (error);
1265		if (attrflag)
1266			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1267			    0, 1);
1268	} else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1269		FREE((caddr_t)nfhp, M_NFSFH);
1270		VREF(dvp);
1271		newvp = dvp;
1272		if (attrflag)
1273			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1274			    0, 1);
1275	} else {
1276		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1277		    cnp->cn_lkflags);
1278		if (error)
1279			return (error);
1280		newvp = NFSTOV(np);
1281		if (attrflag)
1282			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1283			    0, 1);
1284		else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1285		    !(np->n_flag & NMODIFIED)) {
1286			/*
1287			 * Flush the attribute cache when opening a
1288			 * leaf node to ensure that fresh attributes
1289			 * are fetched in nfs_open() since we did not
1290			 * fetch attributes from the LOOKUP reply.
1291			 */
1292			mtx_lock(&np->n_mtx);
1293			np->n_attrstamp = 0;
1294			KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1295			mtx_unlock(&np->n_mtx);
1296		}
1297	}
1298	if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1299		cnp->cn_flags |= SAVENAME;
1300	if ((cnp->cn_flags & MAKEENTRY) &&
1301	    (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1302	    attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1303		cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1304		    newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1305	*vpp = newvp;
1306	return (0);
1307}
1308
1309/*
1310 * nfs read call.
1311 * Just call ncl_bioread() to do the work.
1312 */
1313static int
1314nfs_read(struct vop_read_args *ap)
1315{
1316	struct vnode *vp = ap->a_vp;
1317
1318	switch (vp->v_type) {
1319	case VREG:
1320		return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1321	case VDIR:
1322		return (EISDIR);
1323	default:
1324		return (EOPNOTSUPP);
1325	}
1326}
1327
1328/*
1329 * nfs readlink call
1330 */
1331static int
1332nfs_readlink(struct vop_readlink_args *ap)
1333{
1334	struct vnode *vp = ap->a_vp;
1335
1336	if (vp->v_type != VLNK)
1337		return (EINVAL);
1338	return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1339}
1340
1341/*
1342 * Do a readlink rpc.
1343 * Called by ncl_doio() from below the buffer cache.
1344 */
1345int
1346ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1347{
1348	int error, ret, attrflag;
1349	struct nfsvattr nfsva;
1350
1351	error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1352	    &attrflag, NULL);
1353	if (attrflag) {
1354		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1355		if (ret && !error)
1356			error = ret;
1357	}
1358	if (error && NFS_ISV4(vp))
1359		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1360	return (error);
1361}
1362
1363/*
1364 * nfs read rpc call
1365 * Ditto above
1366 */
1367int
1368ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1369{
1370	int error, ret, attrflag;
1371	struct nfsvattr nfsva;
1372	struct nfsmount *nmp;
1373
1374	nmp = VFSTONFS(vnode_mount(vp));
1375	error = EIO;
1376	attrflag = 0;
1377	if (NFSHASPNFS(nmp))
1378		error = nfscl_doiods(vp, uiop, NULL, NULL,
1379		    NFSV4OPEN_ACCESSREAD, cred, uiop->uio_td);
1380	NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1381	if (error != 0)
1382		error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1383		    &attrflag, NULL);
1384	if (attrflag) {
1385		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1386		if (ret && !error)
1387			error = ret;
1388	}
1389	if (error && NFS_ISV4(vp))
1390		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1391	return (error);
1392}
1393
1394/*
1395 * nfs write call
1396 */
1397int
1398ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1399    int *iomode, int *must_commit, int called_from_strategy)
1400{
1401	struct nfsvattr nfsva;
1402	int error, attrflag, ret;
1403	struct nfsmount *nmp;
1404
1405	nmp = VFSTONFS(vnode_mount(vp));
1406	error = EIO;
1407	attrflag = 0;
1408	if (NFSHASPNFS(nmp))
1409		error = nfscl_doiods(vp, uiop, iomode, must_commit,
1410		    NFSV4OPEN_ACCESSWRITE, cred, uiop->uio_td);
1411	NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1412	if (error != 0)
1413		error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1414		    uiop->uio_td, &nfsva, &attrflag, NULL,
1415		    called_from_strategy);
1416	if (attrflag) {
1417		if (VTONFS(vp)->n_flag & ND_NFSV4)
1418			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
1419			    1);
1420		else
1421			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
1422			    1);
1423		if (ret && !error)
1424			error = ret;
1425	}
1426	if (DOINGASYNC(vp))
1427		*iomode = NFSWRITE_FILESYNC;
1428	if (error && NFS_ISV4(vp))
1429		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1430	return (error);
1431}
1432
1433/*
1434 * nfs mknod rpc
1435 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1436 * mode set to specify the file type and the size field for rdev.
1437 */
1438static int
1439nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1440    struct vattr *vap)
1441{
1442	struct nfsvattr nfsva, dnfsva;
1443	struct vnode *newvp = NULL;
1444	struct nfsnode *np = NULL, *dnp;
1445	struct nfsfh *nfhp;
1446	struct vattr vattr;
1447	int error = 0, attrflag, dattrflag;
1448	u_int32_t rdev;
1449
1450	if (vap->va_type == VCHR || vap->va_type == VBLK)
1451		rdev = vap->va_rdev;
1452	else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1453		rdev = 0xffffffff;
1454	else
1455		return (EOPNOTSUPP);
1456	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1457		return (error);
1458	error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1459	    rdev, vap->va_type, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1460	    &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1461	if (!error) {
1462		if (!nfhp)
1463			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1464			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1465			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1466			    NULL);
1467		if (nfhp)
1468			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1469			    cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1470	}
1471	if (dattrflag)
1472		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1473	if (!error) {
1474		newvp = NFSTOV(np);
1475		if (attrflag != 0) {
1476			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1477			    0, 1);
1478			if (error != 0)
1479				vput(newvp);
1480		}
1481	}
1482	if (!error) {
1483		*vpp = newvp;
1484	} else if (NFS_ISV4(dvp)) {
1485		error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1486		    vap->va_gid);
1487	}
1488	dnp = VTONFS(dvp);
1489	mtx_lock(&dnp->n_mtx);
1490	dnp->n_flag |= NMODIFIED;
1491	if (!dattrflag) {
1492		dnp->n_attrstamp = 0;
1493		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1494	}
1495	mtx_unlock(&dnp->n_mtx);
1496	return (error);
1497}
1498
1499/*
1500 * nfs mknod vop
1501 * just call nfs_mknodrpc() to do the work.
1502 */
1503/* ARGSUSED */
1504static int
1505nfs_mknod(struct vop_mknod_args *ap)
1506{
1507	return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1508}
1509
1510static struct mtx nfs_cverf_mtx;
1511MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1512    MTX_DEF);
1513
1514static nfsquad_t
1515nfs_get_cverf(void)
1516{
1517	static nfsquad_t cverf;
1518	nfsquad_t ret;
1519	static int cverf_initialized = 0;
1520
1521	mtx_lock(&nfs_cverf_mtx);
1522	if (cverf_initialized == 0) {
1523		cverf.lval[0] = arc4random();
1524		cverf.lval[1] = arc4random();
1525		cverf_initialized = 1;
1526	} else
1527		cverf.qval++;
1528	ret = cverf;
1529	mtx_unlock(&nfs_cverf_mtx);
1530
1531	return (ret);
1532}
1533
1534/*
1535 * nfs file create call
1536 */
1537static int
1538nfs_create(struct vop_create_args *ap)
1539{
1540	struct vnode *dvp = ap->a_dvp;
1541	struct vattr *vap = ap->a_vap;
1542	struct componentname *cnp = ap->a_cnp;
1543	struct nfsnode *np = NULL, *dnp;
1544	struct vnode *newvp = NULL;
1545	struct nfsmount *nmp;
1546	struct nfsvattr dnfsva, nfsva;
1547	struct nfsfh *nfhp;
1548	nfsquad_t cverf;
1549	int error = 0, attrflag, dattrflag, fmode = 0;
1550	struct vattr vattr;
1551
1552	/*
1553	 * Oops, not for me..
1554	 */
1555	if (vap->va_type == VSOCK)
1556		return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1557
1558	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1559		return (error);
1560	if (vap->va_vaflags & VA_EXCLUSIVE)
1561		fmode |= O_EXCL;
1562	dnp = VTONFS(dvp);
1563	nmp = VFSTONFS(vnode_mount(dvp));
1564again:
1565	/* For NFSv4, wait until any remove is done. */
1566	mtx_lock(&dnp->n_mtx);
1567	while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1568		dnp->n_flag |= NREMOVEWANT;
1569		(void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1570	}
1571	mtx_unlock(&dnp->n_mtx);
1572
1573	cverf = nfs_get_cverf();
1574	error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1575	    vap, cverf, fmode, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva,
1576	    &nfhp, &attrflag, &dattrflag, NULL);
1577	if (!error) {
1578		if (nfhp == NULL)
1579			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1580			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1581			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1582			    NULL);
1583		if (nfhp != NULL)
1584			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1585			    cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1586	}
1587	if (dattrflag)
1588		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1589	if (!error) {
1590		newvp = NFSTOV(np);
1591		if (attrflag == 0)
1592			error = nfsrpc_getattr(newvp, cnp->cn_cred,
1593			    cnp->cn_thread, &nfsva, NULL);
1594		if (error == 0)
1595			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1596			    0, 1);
1597	}
1598	if (error) {
1599		if (newvp != NULL) {
1600			vput(newvp);
1601			newvp = NULL;
1602		}
1603		if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1604		    error == NFSERR_NOTSUPP) {
1605			fmode &= ~O_EXCL;
1606			goto again;
1607		}
1608	} else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1609		if (nfscl_checksattr(vap, &nfsva)) {
1610			/*
1611			 * We are normally called with only a partially
1612			 * initialized VAP. Since the NFSv3 spec says that
1613			 * the server may use the file attributes to
1614			 * store the verifier, the spec requires us to do a
1615			 * SETATTR RPC. FreeBSD servers store the verifier in
1616			 * atime, but we can't really assume that all servers
1617			 * will so we ensure that our SETATTR sets both atime
1618			 * and mtime.
1619			 */
1620			if (vap->va_mtime.tv_sec == VNOVAL)
1621				vfs_timestamp(&vap->va_mtime);
1622			if (vap->va_atime.tv_sec == VNOVAL)
1623				vap->va_atime = vap->va_mtime;
1624			error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1625			    cnp->cn_thread, &nfsva, &attrflag, NULL);
1626			if (error && (vap->va_uid != (uid_t)VNOVAL ||
1627			    vap->va_gid != (gid_t)VNOVAL)) {
1628				/* try again without setting uid/gid */
1629				vap->va_uid = (uid_t)VNOVAL;
1630				vap->va_gid = (uid_t)VNOVAL;
1631				error = nfsrpc_setattr(newvp, vap, NULL,
1632				    cnp->cn_cred, cnp->cn_thread, &nfsva,
1633				    &attrflag, NULL);
1634			}
1635			if (attrflag)
1636				(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1637				    NULL, 0, 1);
1638			if (error != 0)
1639				vput(newvp);
1640		}
1641	}
1642	if (!error) {
1643		if ((cnp->cn_flags & MAKEENTRY) && attrflag)
1644			cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1645			    NULL);
1646		*ap->a_vpp = newvp;
1647	} else if (NFS_ISV4(dvp)) {
1648		error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1649		    vap->va_gid);
1650	}
1651	mtx_lock(&dnp->n_mtx);
1652	dnp->n_flag |= NMODIFIED;
1653	if (!dattrflag) {
1654		dnp->n_attrstamp = 0;
1655		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1656	}
1657	mtx_unlock(&dnp->n_mtx);
1658	return (error);
1659}
1660
1661/*
1662 * nfs file remove call
1663 * To try and make nfs semantics closer to ufs semantics, a file that has
1664 * other processes using the vnode is renamed instead of removed and then
1665 * removed later on the last close.
1666 * - If v_usecount > 1
1667 *	  If a rename is not already in the works
1668 *	     call nfs_sillyrename() to set it up
1669 *     else
1670 *	  do the remove rpc
1671 */
1672static int
1673nfs_remove(struct vop_remove_args *ap)
1674{
1675	struct vnode *vp = ap->a_vp;
1676	struct vnode *dvp = ap->a_dvp;
1677	struct componentname *cnp = ap->a_cnp;
1678	struct nfsnode *np = VTONFS(vp);
1679	int error = 0;
1680	struct vattr vattr;
1681
1682	KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1683	KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1684	if (vp->v_type == VDIR)
1685		error = EPERM;
1686	else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1687	    VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1688	    vattr.va_nlink > 1)) {
1689		/*
1690		 * Purge the name cache so that the chance of a lookup for
1691		 * the name succeeding while the remove is in progress is
1692		 * minimized. Without node locking it can still happen, such
1693		 * that an I/O op returns ESTALE, but since you get this if
1694		 * another host removes the file..
1695		 */
1696		cache_purge(vp);
1697		/*
1698		 * throw away biocache buffers, mainly to avoid
1699		 * unnecessary delayed writes later.
1700		 */
1701		error = ncl_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1702		/* Do the rpc */
1703		if (error != EINTR && error != EIO)
1704			error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1705			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1706		/*
1707		 * Kludge City: If the first reply to the remove rpc is lost..
1708		 *   the reply to the retransmitted request will be ENOENT
1709		 *   since the file was in fact removed
1710		 *   Therefore, we cheat and return success.
1711		 */
1712		if (error == ENOENT)
1713			error = 0;
1714	} else if (!np->n_sillyrename)
1715		error = nfs_sillyrename(dvp, vp, cnp);
1716	mtx_lock(&np->n_mtx);
1717	np->n_attrstamp = 0;
1718	mtx_unlock(&np->n_mtx);
1719	KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1720	return (error);
1721}
1722
1723/*
1724 * nfs file remove rpc called from nfs_inactive
1725 */
1726int
1727ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1728{
1729	/*
1730	 * Make sure that the directory vnode is still valid.
1731	 * XXX we should lock sp->s_dvp here.
1732	 */
1733	if (sp->s_dvp->v_type == VBAD)
1734		return (0);
1735	return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1736	    sp->s_cred, NULL));
1737}
1738
1739/*
1740 * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1741 */
1742static int
1743nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1744    int namelen, struct ucred *cred, struct thread *td)
1745{
1746	struct nfsvattr dnfsva;
1747	struct nfsnode *dnp = VTONFS(dvp);
1748	int error = 0, dattrflag;
1749
1750	mtx_lock(&dnp->n_mtx);
1751	dnp->n_flag |= NREMOVEINPROG;
1752	mtx_unlock(&dnp->n_mtx);
1753	error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1754	    &dattrflag, NULL);
1755	mtx_lock(&dnp->n_mtx);
1756	if ((dnp->n_flag & NREMOVEWANT)) {
1757		dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1758		mtx_unlock(&dnp->n_mtx);
1759		wakeup((caddr_t)dnp);
1760	} else {
1761		dnp->n_flag &= ~NREMOVEINPROG;
1762		mtx_unlock(&dnp->n_mtx);
1763	}
1764	if (dattrflag)
1765		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1766	mtx_lock(&dnp->n_mtx);
1767	dnp->n_flag |= NMODIFIED;
1768	if (!dattrflag) {
1769		dnp->n_attrstamp = 0;
1770		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1771	}
1772	mtx_unlock(&dnp->n_mtx);
1773	if (error && NFS_ISV4(dvp))
1774		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1775	return (error);
1776}
1777
1778/*
1779 * nfs file rename call
1780 */
1781static int
1782nfs_rename(struct vop_rename_args *ap)
1783{
1784	struct vnode *fvp = ap->a_fvp;
1785	struct vnode *tvp = ap->a_tvp;
1786	struct vnode *fdvp = ap->a_fdvp;
1787	struct vnode *tdvp = ap->a_tdvp;
1788	struct componentname *tcnp = ap->a_tcnp;
1789	struct componentname *fcnp = ap->a_fcnp;
1790	struct nfsnode *fnp = VTONFS(ap->a_fvp);
1791	struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1792	struct nfsv4node *newv4 = NULL;
1793	int error;
1794
1795	KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1796	    (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1797	/* Check for cross-device rename */
1798	if ((fvp->v_mount != tdvp->v_mount) ||
1799	    (tvp && (fvp->v_mount != tvp->v_mount))) {
1800		error = EXDEV;
1801		goto out;
1802	}
1803
1804	if (fvp == tvp) {
1805		ncl_printf("nfs_rename: fvp == tvp (can't happen)\n");
1806		error = 0;
1807		goto out;
1808	}
1809	if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
1810		goto out;
1811
1812	/*
1813	 * We have to flush B_DELWRI data prior to renaming
1814	 * the file.  If we don't, the delayed-write buffers
1815	 * can be flushed out later after the file has gone stale
1816	 * under NFSV3.  NFSV2 does not have this problem because
1817	 * ( as far as I can tell ) it flushes dirty buffers more
1818	 * often.
1819	 *
1820	 * Skip the rename operation if the fsync fails, this can happen
1821	 * due to the server's volume being full, when we pushed out data
1822	 * that was written back to our cache earlier. Not checking for
1823	 * this condition can result in potential (silent) data loss.
1824	 */
1825	error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1826	NFSVOPUNLOCK(fvp, 0);
1827	if (!error && tvp)
1828		error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1829	if (error)
1830		goto out;
1831
1832	/*
1833	 * If the tvp exists and is in use, sillyrename it before doing the
1834	 * rename of the new file over it.
1835	 * XXX Can't sillyrename a directory.
1836	 */
1837	if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1838		tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1839		vput(tvp);
1840		tvp = NULL;
1841	}
1842
1843	error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1844	    tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1845	    tcnp->cn_thread);
1846
1847	if (error == 0 && NFS_ISV4(tdvp)) {
1848		/*
1849		 * For NFSv4, check to see if it is the same name and
1850		 * replace the name, if it is different.
1851		 */
1852		MALLOC(newv4, struct nfsv4node *,
1853		    sizeof (struct nfsv4node) +
1854		    tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
1855		    M_NFSV4NODE, M_WAITOK);
1856		mtx_lock(&tdnp->n_mtx);
1857		mtx_lock(&fnp->n_mtx);
1858		if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
1859		    (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
1860		      NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
1861		      tcnp->cn_namelen) ||
1862		      tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
1863		      NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1864			tdnp->n_fhp->nfh_len))) {
1865#ifdef notdef
1866{ char nnn[100]; int nnnl;
1867nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
1868bcopy(tcnp->cn_nameptr, nnn, nnnl);
1869nnn[nnnl] = '\0';
1870printf("ren replace=%s\n",nnn);
1871}
1872#endif
1873			FREE((caddr_t)fnp->n_v4, M_NFSV4NODE);
1874			fnp->n_v4 = newv4;
1875			newv4 = NULL;
1876			fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
1877			fnp->n_v4->n4_namelen = tcnp->cn_namelen;
1878			NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1879			    tdnp->n_fhp->nfh_len);
1880			NFSBCOPY(tcnp->cn_nameptr,
1881			    NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
1882		}
1883		mtx_unlock(&tdnp->n_mtx);
1884		mtx_unlock(&fnp->n_mtx);
1885		if (newv4 != NULL)
1886			FREE((caddr_t)newv4, M_NFSV4NODE);
1887	}
1888
1889	if (fvp->v_type == VDIR) {
1890		if (tvp != NULL && tvp->v_type == VDIR)
1891			cache_purge(tdvp);
1892		cache_purge(fdvp);
1893	}
1894
1895out:
1896	if (tdvp == tvp)
1897		vrele(tdvp);
1898	else
1899		vput(tdvp);
1900	if (tvp)
1901		vput(tvp);
1902	vrele(fdvp);
1903	vrele(fvp);
1904	/*
1905	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1906	 */
1907	if (error == ENOENT)
1908		error = 0;
1909	return (error);
1910}
1911
1912/*
1913 * nfs file rename rpc called from nfs_remove() above
1914 */
1915static int
1916nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
1917    struct sillyrename *sp)
1918{
1919
1920	return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
1921	    sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
1922	    scnp->cn_thread));
1923}
1924
1925/*
1926 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1927 */
1928static int
1929nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
1930    int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
1931    int tnamelen, struct ucred *cred, struct thread *td)
1932{
1933	struct nfsvattr fnfsva, tnfsva;
1934	struct nfsnode *fdnp = VTONFS(fdvp);
1935	struct nfsnode *tdnp = VTONFS(tdvp);
1936	int error = 0, fattrflag, tattrflag;
1937
1938	error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
1939	    tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
1940	    &tattrflag, NULL, NULL);
1941	mtx_lock(&fdnp->n_mtx);
1942	fdnp->n_flag |= NMODIFIED;
1943	if (fattrflag != 0) {
1944		mtx_unlock(&fdnp->n_mtx);
1945		(void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
1946	} else {
1947		fdnp->n_attrstamp = 0;
1948		mtx_unlock(&fdnp->n_mtx);
1949		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
1950	}
1951	mtx_lock(&tdnp->n_mtx);
1952	tdnp->n_flag |= NMODIFIED;
1953	if (tattrflag != 0) {
1954		mtx_unlock(&tdnp->n_mtx);
1955		(void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
1956	} else {
1957		tdnp->n_attrstamp = 0;
1958		mtx_unlock(&tdnp->n_mtx);
1959		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1960	}
1961	if (error && NFS_ISV4(fdvp))
1962		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1963	return (error);
1964}
1965
1966/*
1967 * nfs hard link create call
1968 */
1969static int
1970nfs_link(struct vop_link_args *ap)
1971{
1972	struct vnode *vp = ap->a_vp;
1973	struct vnode *tdvp = ap->a_tdvp;
1974	struct componentname *cnp = ap->a_cnp;
1975	struct nfsnode *np, *tdnp;
1976	struct nfsvattr nfsva, dnfsva;
1977	int error = 0, attrflag, dattrflag;
1978
1979	if (vp->v_mount != tdvp->v_mount) {
1980		return (EXDEV);
1981	}
1982
1983	/*
1984	 * Push all writes to the server, so that the attribute cache
1985	 * doesn't get "out of sync" with the server.
1986	 * XXX There should be a better way!
1987	 */
1988	VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1989
1990	error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
1991	    cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &attrflag,
1992	    &dattrflag, NULL);
1993	tdnp = VTONFS(tdvp);
1994	mtx_lock(&tdnp->n_mtx);
1995	tdnp->n_flag |= NMODIFIED;
1996	if (dattrflag != 0) {
1997		mtx_unlock(&tdnp->n_mtx);
1998		(void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
1999	} else {
2000		tdnp->n_attrstamp = 0;
2001		mtx_unlock(&tdnp->n_mtx);
2002		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2003	}
2004	if (attrflag)
2005		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2006	else {
2007		np = VTONFS(vp);
2008		mtx_lock(&np->n_mtx);
2009		np->n_attrstamp = 0;
2010		mtx_unlock(&np->n_mtx);
2011		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2012	}
2013	/*
2014	 * If negative lookup caching is enabled, I might as well
2015	 * add an entry for this node. Not necessary for correctness,
2016	 * but if negative caching is enabled, then the system
2017	 * must care about lookup caching hit rate, so...
2018	 */
2019	if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
2020	    (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2021		cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
2022	}
2023	if (error && NFS_ISV4(vp))
2024		error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2025		    (gid_t)0);
2026	return (error);
2027}
2028
2029/*
2030 * nfs symbolic link create call
2031 */
2032static int
2033nfs_symlink(struct vop_symlink_args *ap)
2034{
2035	struct vnode *dvp = ap->a_dvp;
2036	struct vattr *vap = ap->a_vap;
2037	struct componentname *cnp = ap->a_cnp;
2038	struct nfsvattr nfsva, dnfsva;
2039	struct nfsfh *nfhp;
2040	struct nfsnode *np = NULL, *dnp;
2041	struct vnode *newvp = NULL;
2042	int error = 0, attrflag, dattrflag, ret;
2043
2044	vap->va_type = VLNK;
2045	error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2046	    ap->a_target, vap, cnp->cn_cred, cnp->cn_thread, &dnfsva,
2047	    &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
2048	if (nfhp) {
2049		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2050		    &np, NULL, LK_EXCLUSIVE);
2051		if (!ret)
2052			newvp = NFSTOV(np);
2053		else if (!error)
2054			error = ret;
2055	}
2056	if (newvp != NULL) {
2057		if (attrflag)
2058			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2059			    0, 1);
2060	} else if (!error) {
2061		/*
2062		 * If we do not have an error and we could not extract the
2063		 * newvp from the response due to the request being NFSv2, we
2064		 * have to do a lookup in order to obtain a newvp to return.
2065		 */
2066		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2067		    cnp->cn_cred, cnp->cn_thread, &np);
2068		if (!error)
2069			newvp = NFSTOV(np);
2070	}
2071	if (error) {
2072		if (newvp)
2073			vput(newvp);
2074		if (NFS_ISV4(dvp))
2075			error = nfscl_maperr(cnp->cn_thread, error,
2076			    vap->va_uid, vap->va_gid);
2077	} else {
2078		*ap->a_vpp = newvp;
2079	}
2080
2081	dnp = VTONFS(dvp);
2082	mtx_lock(&dnp->n_mtx);
2083	dnp->n_flag |= NMODIFIED;
2084	if (dattrflag != 0) {
2085		mtx_unlock(&dnp->n_mtx);
2086		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2087	} else {
2088		dnp->n_attrstamp = 0;
2089		mtx_unlock(&dnp->n_mtx);
2090		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2091	}
2092	/*
2093	 * If negative lookup caching is enabled, I might as well
2094	 * add an entry for this node. Not necessary for correctness,
2095	 * but if negative caching is enabled, then the system
2096	 * must care about lookup caching hit rate, so...
2097	 */
2098	if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2099	    (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2100		cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime, NULL);
2101	}
2102	return (error);
2103}
2104
2105/*
2106 * nfs make dir call
2107 */
2108static int
2109nfs_mkdir(struct vop_mkdir_args *ap)
2110{
2111	struct vnode *dvp = ap->a_dvp;
2112	struct vattr *vap = ap->a_vap;
2113	struct componentname *cnp = ap->a_cnp;
2114	struct nfsnode *np = NULL, *dnp;
2115	struct vnode *newvp = NULL;
2116	struct vattr vattr;
2117	struct nfsfh *nfhp;
2118	struct nfsvattr nfsva, dnfsva;
2119	int error = 0, attrflag, dattrflag, ret;
2120
2121	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2122		return (error);
2123	vap->va_type = VDIR;
2124	error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2125	    vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &nfhp,
2126	    &attrflag, &dattrflag, NULL);
2127	dnp = VTONFS(dvp);
2128	mtx_lock(&dnp->n_mtx);
2129	dnp->n_flag |= NMODIFIED;
2130	if (dattrflag != 0) {
2131		mtx_unlock(&dnp->n_mtx);
2132		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2133	} else {
2134		dnp->n_attrstamp = 0;
2135		mtx_unlock(&dnp->n_mtx);
2136		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2137	}
2138	if (nfhp) {
2139		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2140		    &np, NULL, LK_EXCLUSIVE);
2141		if (!ret) {
2142			newvp = NFSTOV(np);
2143			if (attrflag)
2144			   (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2145				NULL, 0, 1);
2146		} else if (!error)
2147			error = ret;
2148	}
2149	if (!error && newvp == NULL) {
2150		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2151		    cnp->cn_cred, cnp->cn_thread, &np);
2152		if (!error) {
2153			newvp = NFSTOV(np);
2154			if (newvp->v_type != VDIR)
2155				error = EEXIST;
2156		}
2157	}
2158	if (error) {
2159		if (newvp)
2160			vput(newvp);
2161		if (NFS_ISV4(dvp))
2162			error = nfscl_maperr(cnp->cn_thread, error,
2163			    vap->va_uid, vap->va_gid);
2164	} else {
2165		/*
2166		 * If negative lookup caching is enabled, I might as well
2167		 * add an entry for this node. Not necessary for correctness,
2168		 * but if negative caching is enabled, then the system
2169		 * must care about lookup caching hit rate, so...
2170		 */
2171		if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2172		    (cnp->cn_flags & MAKEENTRY) &&
2173		    attrflag != 0 && dattrflag != 0)
2174			cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2175			    &dnfsva.na_ctime);
2176		*ap->a_vpp = newvp;
2177	}
2178	return (error);
2179}
2180
2181/*
2182 * nfs remove directory call
2183 */
2184static int
2185nfs_rmdir(struct vop_rmdir_args *ap)
2186{
2187	struct vnode *vp = ap->a_vp;
2188	struct vnode *dvp = ap->a_dvp;
2189	struct componentname *cnp = ap->a_cnp;
2190	struct nfsnode *dnp;
2191	struct nfsvattr dnfsva;
2192	int error, dattrflag;
2193
2194	if (dvp == vp)
2195		return (EINVAL);
2196	error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2197	    cnp->cn_cred, cnp->cn_thread, &dnfsva, &dattrflag, NULL);
2198	dnp = VTONFS(dvp);
2199	mtx_lock(&dnp->n_mtx);
2200	dnp->n_flag |= NMODIFIED;
2201	if (dattrflag != 0) {
2202		mtx_unlock(&dnp->n_mtx);
2203		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2204	} else {
2205		dnp->n_attrstamp = 0;
2206		mtx_unlock(&dnp->n_mtx);
2207		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2208	}
2209
2210	cache_purge(dvp);
2211	cache_purge(vp);
2212	if (error && NFS_ISV4(dvp))
2213		error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2214		    (gid_t)0);
2215	/*
2216	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2217	 */
2218	if (error == ENOENT)
2219		error = 0;
2220	return (error);
2221}
2222
2223/*
2224 * nfs readdir call
2225 */
2226static int
2227nfs_readdir(struct vop_readdir_args *ap)
2228{
2229	struct vnode *vp = ap->a_vp;
2230	struct nfsnode *np = VTONFS(vp);
2231	struct uio *uio = ap->a_uio;
2232	ssize_t tresid;
2233	int error = 0;
2234	struct vattr vattr;
2235
2236	if (ap->a_eofflag != NULL)
2237		*ap->a_eofflag = 0;
2238	if (vp->v_type != VDIR)
2239		return(EPERM);
2240
2241	/*
2242	 * First, check for hit on the EOF offset cache
2243	 */
2244	if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2245	    (np->n_flag & NMODIFIED) == 0) {
2246		if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2247			mtx_lock(&np->n_mtx);
2248			if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2249			    !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2250				mtx_unlock(&np->n_mtx);
2251				NFSINCRGLOBAL(newnfsstats.direofcache_hits);
2252				if (ap->a_eofflag != NULL)
2253					*ap->a_eofflag = 1;
2254				return (0);
2255			} else
2256				mtx_unlock(&np->n_mtx);
2257		}
2258	}
2259
2260	/*
2261	 * Call ncl_bioread() to do the real work.
2262	 */
2263	tresid = uio->uio_resid;
2264	error = ncl_bioread(vp, uio, 0, ap->a_cred);
2265
2266	if (!error && uio->uio_resid == tresid) {
2267		NFSINCRGLOBAL(newnfsstats.direofcache_misses);
2268		if (ap->a_eofflag != NULL)
2269			*ap->a_eofflag = 1;
2270	}
2271	return (error);
2272}
2273
2274/*
2275 * Readdir rpc call.
2276 * Called from below the buffer cache by ncl_doio().
2277 */
2278int
2279ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2280    struct thread *td)
2281{
2282	struct nfsvattr nfsva;
2283	nfsuint64 *cookiep, cookie;
2284	struct nfsnode *dnp = VTONFS(vp);
2285	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2286	int error = 0, eof, attrflag;
2287
2288	KASSERT(uiop->uio_iovcnt == 1 &&
2289	    (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2290	    (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2291	    ("nfs readdirrpc bad uio"));
2292
2293	/*
2294	 * If there is no cookie, assume directory was stale.
2295	 */
2296	ncl_dircookie_lock(dnp);
2297	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2298	if (cookiep) {
2299		cookie = *cookiep;
2300		ncl_dircookie_unlock(dnp);
2301	} else {
2302		ncl_dircookie_unlock(dnp);
2303		return (NFSERR_BAD_COOKIE);
2304	}
2305
2306	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2307		(void)ncl_fsinfo(nmp, vp, cred, td);
2308
2309	error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2310	    &attrflag, &eof, NULL);
2311	if (attrflag)
2312		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2313
2314	if (!error) {
2315		/*
2316		 * We are now either at the end of the directory or have filled
2317		 * the block.
2318		 */
2319		if (eof)
2320			dnp->n_direofoffset = uiop->uio_offset;
2321		else {
2322			if (uiop->uio_resid > 0)
2323				ncl_printf("EEK! readdirrpc resid > 0\n");
2324			ncl_dircookie_lock(dnp);
2325			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2326			*cookiep = cookie;
2327			ncl_dircookie_unlock(dnp);
2328		}
2329	} else if (NFS_ISV4(vp)) {
2330		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2331	}
2332	return (error);
2333}
2334
2335/*
2336 * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2337 */
2338int
2339ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2340    struct thread *td)
2341{
2342	struct nfsvattr nfsva;
2343	nfsuint64 *cookiep, cookie;
2344	struct nfsnode *dnp = VTONFS(vp);
2345	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2346	int error = 0, attrflag, eof;
2347
2348	KASSERT(uiop->uio_iovcnt == 1 &&
2349	    (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2350	    (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2351	    ("nfs readdirplusrpc bad uio"));
2352
2353	/*
2354	 * If there is no cookie, assume directory was stale.
2355	 */
2356	ncl_dircookie_lock(dnp);
2357	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2358	if (cookiep) {
2359		cookie = *cookiep;
2360		ncl_dircookie_unlock(dnp);
2361	} else {
2362		ncl_dircookie_unlock(dnp);
2363		return (NFSERR_BAD_COOKIE);
2364	}
2365
2366	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2367		(void)ncl_fsinfo(nmp, vp, cred, td);
2368	error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2369	    &attrflag, &eof, NULL);
2370	if (attrflag)
2371		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2372
2373	if (!error) {
2374		/*
2375		 * We are now either at end of the directory or have filled the
2376		 * the block.
2377		 */
2378		if (eof)
2379			dnp->n_direofoffset = uiop->uio_offset;
2380		else {
2381			if (uiop->uio_resid > 0)
2382				ncl_printf("EEK! readdirplusrpc resid > 0\n");
2383			ncl_dircookie_lock(dnp);
2384			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2385			*cookiep = cookie;
2386			ncl_dircookie_unlock(dnp);
2387		}
2388	} else if (NFS_ISV4(vp)) {
2389		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2390	}
2391	return (error);
2392}
2393
2394/*
2395 * Silly rename. To make the NFS filesystem that is stateless look a little
2396 * more like the "ufs" a remove of an active vnode is translated to a rename
2397 * to a funny looking filename that is removed by nfs_inactive on the
2398 * nfsnode. There is the potential for another process on a different client
2399 * to create the same funny name between the nfs_lookitup() fails and the
2400 * nfs_rename() completes, but...
2401 */
2402static int
2403nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2404{
2405	struct sillyrename *sp;
2406	struct nfsnode *np;
2407	int error;
2408	short pid;
2409	unsigned int lticks;
2410
2411	cache_purge(dvp);
2412	np = VTONFS(vp);
2413	KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2414	MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2415	    M_NEWNFSREQ, M_WAITOK);
2416	sp->s_cred = crhold(cnp->cn_cred);
2417	sp->s_dvp = dvp;
2418	VREF(dvp);
2419
2420	/*
2421	 * Fudge together a funny name.
2422	 * Changing the format of the funny name to accomodate more
2423	 * sillynames per directory.
2424	 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2425	 * CPU ticks since boot.
2426	 */
2427	pid = cnp->cn_thread->td_proc->p_pid;
2428	lticks = (unsigned int)ticks;
2429	for ( ; ; ) {
2430		sp->s_namlen = sprintf(sp->s_name,
2431				       ".nfs.%08x.%04x4.4", lticks,
2432				       pid);
2433		if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2434				 cnp->cn_thread, NULL))
2435			break;
2436		lticks++;
2437	}
2438	error = nfs_renameit(dvp, vp, cnp, sp);
2439	if (error)
2440		goto bad;
2441	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2442		cnp->cn_thread, &np);
2443	np->n_sillyrename = sp;
2444	return (0);
2445bad:
2446	vrele(sp->s_dvp);
2447	crfree(sp->s_cred);
2448	free((caddr_t)sp, M_NEWNFSREQ);
2449	return (error);
2450}
2451
2452/*
2453 * Look up a file name and optionally either update the file handle or
2454 * allocate an nfsnode, depending on the value of npp.
2455 * npp == NULL	--> just do the lookup
2456 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2457 *			handled too
2458 * *npp != NULL --> update the file handle in the vnode
2459 */
2460static int
2461nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2462    struct thread *td, struct nfsnode **npp)
2463{
2464	struct vnode *newvp = NULL, *vp;
2465	struct nfsnode *np, *dnp = VTONFS(dvp);
2466	struct nfsfh *nfhp, *onfhp;
2467	struct nfsvattr nfsva, dnfsva;
2468	struct componentname cn;
2469	int error = 0, attrflag, dattrflag;
2470	u_int hash;
2471
2472	error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2473	    &nfhp, &attrflag, &dattrflag, NULL);
2474	if (dattrflag)
2475		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2476	if (npp && !error) {
2477		if (*npp != NULL) {
2478		    np = *npp;
2479		    vp = NFSTOV(np);
2480		    /*
2481		     * For NFSv4, check to see if it is the same name and
2482		     * replace the name, if it is different.
2483		     */
2484		    if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2485			(np->n_v4->n4_namelen != len ||
2486			 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2487			 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2488			 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2489			 dnp->n_fhp->nfh_len))) {
2490#ifdef notdef
2491{ char nnn[100]; int nnnl;
2492nnnl = (len < 100) ? len : 99;
2493bcopy(name, nnn, nnnl);
2494nnn[nnnl] = '\0';
2495printf("replace=%s\n",nnn);
2496}
2497#endif
2498			    FREE((caddr_t)np->n_v4, M_NFSV4NODE);
2499			    MALLOC(np->n_v4, struct nfsv4node *,
2500				sizeof (struct nfsv4node) +
2501				dnp->n_fhp->nfh_len + len - 1,
2502				M_NFSV4NODE, M_WAITOK);
2503			    np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2504			    np->n_v4->n4_namelen = len;
2505			    NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2506				dnp->n_fhp->nfh_len);
2507			    NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2508		    }
2509		    hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2510			FNV1_32_INIT);
2511		    onfhp = np->n_fhp;
2512		    /*
2513		     * Rehash node for new file handle.
2514		     */
2515		    vfs_hash_rehash(vp, hash);
2516		    np->n_fhp = nfhp;
2517		    if (onfhp != NULL)
2518			FREE((caddr_t)onfhp, M_NFSFH);
2519		    newvp = NFSTOV(np);
2520		} else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2521		    FREE((caddr_t)nfhp, M_NFSFH);
2522		    VREF(dvp);
2523		    newvp = dvp;
2524		} else {
2525		    cn.cn_nameptr = name;
2526		    cn.cn_namelen = len;
2527		    error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2528			&np, NULL, LK_EXCLUSIVE);
2529		    if (error)
2530			return (error);
2531		    newvp = NFSTOV(np);
2532		}
2533		if (!attrflag && *npp == NULL) {
2534			if (newvp == dvp)
2535				vrele(newvp);
2536			else
2537				vput(newvp);
2538			return (ENOENT);
2539		}
2540		if (attrflag)
2541			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2542			    0, 1);
2543	}
2544	if (npp && *npp == NULL) {
2545		if (error) {
2546			if (newvp) {
2547				if (newvp == dvp)
2548					vrele(newvp);
2549				else
2550					vput(newvp);
2551			}
2552		} else
2553			*npp = np;
2554	}
2555	if (error && NFS_ISV4(dvp))
2556		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2557	return (error);
2558}
2559
2560/*
2561 * Nfs Version 3 and 4 commit rpc
2562 */
2563int
2564ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2565   struct thread *td)
2566{
2567	struct nfsvattr nfsva;
2568	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2569	int error, attrflag;
2570
2571	mtx_lock(&nmp->nm_mtx);
2572	if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2573		mtx_unlock(&nmp->nm_mtx);
2574		return (0);
2575	}
2576	mtx_unlock(&nmp->nm_mtx);
2577	error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
2578	    &attrflag, NULL);
2579	if (attrflag != 0)
2580		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL,
2581		    0, 1);
2582	if (error != 0 && NFS_ISV4(vp))
2583		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2584	return (error);
2585}
2586
2587/*
2588 * Strategy routine.
2589 * For async requests when nfsiod(s) are running, queue the request by
2590 * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2591 * request.
2592 */
2593static int
2594nfs_strategy(struct vop_strategy_args *ap)
2595{
2596	struct buf *bp = ap->a_bp;
2597	struct ucred *cr;
2598
2599	KASSERT(!(bp->b_flags & B_DONE),
2600	    ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2601	BUF_ASSERT_HELD(bp);
2602
2603	if (bp->b_iocmd == BIO_READ)
2604		cr = bp->b_rcred;
2605	else
2606		cr = bp->b_wcred;
2607
2608	/*
2609	 * If the op is asynchronous and an i/o daemon is waiting
2610	 * queue the request, wake it up and wait for completion
2611	 * otherwise just do it ourselves.
2612	 */
2613	if ((bp->b_flags & B_ASYNC) == 0 ||
2614	    ncl_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
2615		(void) ncl_doio(ap->a_vp, bp, cr, curthread, 1);
2616	return (0);
2617}
2618
2619/*
2620 * fsync vnode op. Just call ncl_flush() with commit == 1.
2621 */
2622/* ARGSUSED */
2623static int
2624nfs_fsync(struct vop_fsync_args *ap)
2625{
2626
2627	if (ap->a_vp->v_type != VREG) {
2628		/*
2629		 * For NFS, metadata is changed synchronously on the server,
2630		 * so there is nothing to flush. Also, ncl_flush() clears
2631		 * the NMODIFIED flag and that shouldn't be done here for
2632		 * directories.
2633		 */
2634		return (0);
2635	}
2636	return (ncl_flush(ap->a_vp, ap->a_waitfor, NULL, ap->a_td, 1, 0));
2637}
2638
2639/*
2640 * Flush all the blocks associated with a vnode.
2641 * 	Walk through the buffer pool and push any dirty pages
2642 *	associated with the vnode.
2643 * If the called_from_renewthread argument is TRUE, it has been called
2644 * from the NFSv4 renew thread and, as such, cannot block indefinitely
2645 * waiting for a buffer write to complete.
2646 */
2647int
2648ncl_flush(struct vnode *vp, int waitfor, struct ucred *cred, struct thread *td,
2649    int commit, int called_from_renewthread)
2650{
2651	struct nfsnode *np = VTONFS(vp);
2652	struct buf *bp;
2653	int i;
2654	struct buf *nbp;
2655	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2656	int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2657	int passone = 1, trycnt = 0;
2658	u_quad_t off, endoff, toff;
2659	struct ucred* wcred = NULL;
2660	struct buf **bvec = NULL;
2661	struct bufobj *bo;
2662#ifndef NFS_COMMITBVECSIZ
2663#define	NFS_COMMITBVECSIZ	20
2664#endif
2665	struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2666	int bvecsize = 0, bveccount;
2667
2668	if (called_from_renewthread != 0)
2669		slptimeo = hz;
2670	if (nmp->nm_flag & NFSMNT_INT)
2671		slpflag = PCATCH;
2672	if (!commit)
2673		passone = 0;
2674	bo = &vp->v_bufobj;
2675	/*
2676	 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2677	 * server, but has not been committed to stable storage on the server
2678	 * yet. On the first pass, the byte range is worked out and the commit
2679	 * rpc is done. On the second pass, ncl_writebp() is called to do the
2680	 * job.
2681	 */
2682again:
2683	off = (u_quad_t)-1;
2684	endoff = 0;
2685	bvecpos = 0;
2686	if (NFS_ISV34(vp) && commit) {
2687		if (bvec != NULL && bvec != bvec_on_stack)
2688			free(bvec, M_TEMP);
2689		/*
2690		 * Count up how many buffers waiting for a commit.
2691		 */
2692		bveccount = 0;
2693		BO_LOCK(bo);
2694		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2695			if (!BUF_ISLOCKED(bp) &&
2696			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2697				== (B_DELWRI | B_NEEDCOMMIT))
2698				bveccount++;
2699		}
2700		/*
2701		 * Allocate space to remember the list of bufs to commit.  It is
2702		 * important to use M_NOWAIT here to avoid a race with nfs_write.
2703		 * If we can't get memory (for whatever reason), we will end up
2704		 * committing the buffers one-by-one in the loop below.
2705		 */
2706		if (bveccount > NFS_COMMITBVECSIZ) {
2707			/*
2708			 * Release the vnode interlock to avoid a lock
2709			 * order reversal.
2710			 */
2711			BO_UNLOCK(bo);
2712			bvec = (struct buf **)
2713				malloc(bveccount * sizeof(struct buf *),
2714				       M_TEMP, M_NOWAIT);
2715			BO_LOCK(bo);
2716			if (bvec == NULL) {
2717				bvec = bvec_on_stack;
2718				bvecsize = NFS_COMMITBVECSIZ;
2719			} else
2720				bvecsize = bveccount;
2721		} else {
2722			bvec = bvec_on_stack;
2723			bvecsize = NFS_COMMITBVECSIZ;
2724		}
2725		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2726			if (bvecpos >= bvecsize)
2727				break;
2728			if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2729				nbp = TAILQ_NEXT(bp, b_bobufs);
2730				continue;
2731			}
2732			if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2733			    (B_DELWRI | B_NEEDCOMMIT)) {
2734				BUF_UNLOCK(bp);
2735				nbp = TAILQ_NEXT(bp, b_bobufs);
2736				continue;
2737			}
2738			BO_UNLOCK(bo);
2739			bremfree(bp);
2740			/*
2741			 * Work out if all buffers are using the same cred
2742			 * so we can deal with them all with one commit.
2743			 *
2744			 * NOTE: we are not clearing B_DONE here, so we have
2745			 * to do it later on in this routine if we intend to
2746			 * initiate I/O on the bp.
2747			 *
2748			 * Note: to avoid loopback deadlocks, we do not
2749			 * assign b_runningbufspace.
2750			 */
2751			if (wcred == NULL)
2752				wcred = bp->b_wcred;
2753			else if (wcred != bp->b_wcred)
2754				wcred = NOCRED;
2755			vfs_busy_pages(bp, 1);
2756
2757			BO_LOCK(bo);
2758			/*
2759			 * bp is protected by being locked, but nbp is not
2760			 * and vfs_busy_pages() may sleep.  We have to
2761			 * recalculate nbp.
2762			 */
2763			nbp = TAILQ_NEXT(bp, b_bobufs);
2764
2765			/*
2766			 * A list of these buffers is kept so that the
2767			 * second loop knows which buffers have actually
2768			 * been committed. This is necessary, since there
2769			 * may be a race between the commit rpc and new
2770			 * uncommitted writes on the file.
2771			 */
2772			bvec[bvecpos++] = bp;
2773			toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2774				bp->b_dirtyoff;
2775			if (toff < off)
2776				off = toff;
2777			toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2778			if (toff > endoff)
2779				endoff = toff;
2780		}
2781		BO_UNLOCK(bo);
2782	}
2783	if (bvecpos > 0) {
2784		/*
2785		 * Commit data on the server, as required.
2786		 * If all bufs are using the same wcred, then use that with
2787		 * one call for all of them, otherwise commit each one
2788		 * separately.
2789		 */
2790		if (wcred != NOCRED)
2791			retv = ncl_commit(vp, off, (int)(endoff - off),
2792					  wcred, td);
2793		else {
2794			retv = 0;
2795			for (i = 0; i < bvecpos; i++) {
2796				off_t off, size;
2797				bp = bvec[i];
2798				off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2799					bp->b_dirtyoff;
2800				size = (u_quad_t)(bp->b_dirtyend
2801						  - bp->b_dirtyoff);
2802				retv = ncl_commit(vp, off, (int)size,
2803						  bp->b_wcred, td);
2804				if (retv) break;
2805			}
2806		}
2807
2808		if (retv == NFSERR_STALEWRITEVERF)
2809			ncl_clearcommit(vp->v_mount);
2810
2811		/*
2812		 * Now, either mark the blocks I/O done or mark the
2813		 * blocks dirty, depending on whether the commit
2814		 * succeeded.
2815		 */
2816		for (i = 0; i < bvecpos; i++) {
2817			bp = bvec[i];
2818			bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2819			if (retv) {
2820				/*
2821				 * Error, leave B_DELWRI intact
2822				 */
2823				vfs_unbusy_pages(bp);
2824				brelse(bp);
2825			} else {
2826				/*
2827				 * Success, remove B_DELWRI ( bundirty() ).
2828				 *
2829				 * b_dirtyoff/b_dirtyend seem to be NFS
2830				 * specific.  We should probably move that
2831				 * into bundirty(). XXX
2832				 */
2833				bufobj_wref(bo);
2834				bp->b_flags |= B_ASYNC;
2835				bundirty(bp);
2836				bp->b_flags &= ~B_DONE;
2837				bp->b_ioflags &= ~BIO_ERROR;
2838				bp->b_dirtyoff = bp->b_dirtyend = 0;
2839				bufdone(bp);
2840			}
2841		}
2842	}
2843
2844	/*
2845	 * Start/do any write(s) that are required.
2846	 */
2847loop:
2848	BO_LOCK(bo);
2849	TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2850		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2851			if (waitfor != MNT_WAIT || passone)
2852				continue;
2853
2854			error = BUF_TIMELOCK(bp,
2855			    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2856			    BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
2857			if (error == 0) {
2858				BUF_UNLOCK(bp);
2859				goto loop;
2860			}
2861			if (error == ENOLCK) {
2862				error = 0;
2863				goto loop;
2864			}
2865			if (called_from_renewthread != 0) {
2866				/*
2867				 * Return EIO so the flush will be retried
2868				 * later.
2869				 */
2870				error = EIO;
2871				goto done;
2872			}
2873			if (newnfs_sigintr(nmp, td)) {
2874				error = EINTR;
2875				goto done;
2876			}
2877			if (slpflag == PCATCH) {
2878				slpflag = 0;
2879				slptimeo = 2 * hz;
2880			}
2881			goto loop;
2882		}
2883		if ((bp->b_flags & B_DELWRI) == 0)
2884			panic("nfs_fsync: not dirty");
2885		if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2886			BUF_UNLOCK(bp);
2887			continue;
2888		}
2889		BO_UNLOCK(bo);
2890		bremfree(bp);
2891		if (passone || !commit)
2892		    bp->b_flags |= B_ASYNC;
2893		else
2894		    bp->b_flags |= B_ASYNC;
2895		bwrite(bp);
2896		if (newnfs_sigintr(nmp, td)) {
2897			error = EINTR;
2898			goto done;
2899		}
2900		goto loop;
2901	}
2902	if (passone) {
2903		passone = 0;
2904		BO_UNLOCK(bo);
2905		goto again;
2906	}
2907	if (waitfor == MNT_WAIT) {
2908		while (bo->bo_numoutput) {
2909			error = bufobj_wwait(bo, slpflag, slptimeo);
2910			if (error) {
2911			    BO_UNLOCK(bo);
2912			    if (called_from_renewthread != 0) {
2913				/*
2914				 * Return EIO so that the flush will be
2915				 * retried later.
2916				 */
2917				error = EIO;
2918				goto done;
2919			    }
2920			    error = newnfs_sigintr(nmp, td);
2921			    if (error)
2922				goto done;
2923			    if (slpflag == PCATCH) {
2924				slpflag = 0;
2925				slptimeo = 2 * hz;
2926			    }
2927			    BO_LOCK(bo);
2928			}
2929		}
2930		if (bo->bo_dirty.bv_cnt != 0 && commit) {
2931			BO_UNLOCK(bo);
2932			goto loop;
2933		}
2934		/*
2935		 * Wait for all the async IO requests to drain
2936		 */
2937		BO_UNLOCK(bo);
2938		mtx_lock(&np->n_mtx);
2939		while (np->n_directio_asyncwr > 0) {
2940			np->n_flag |= NFSYNCWAIT;
2941			error = newnfs_msleep(td, &np->n_directio_asyncwr,
2942			    &np->n_mtx, slpflag | (PRIBIO + 1),
2943			    "nfsfsync", 0);
2944			if (error) {
2945				if (newnfs_sigintr(nmp, td)) {
2946					mtx_unlock(&np->n_mtx);
2947					error = EINTR;
2948					goto done;
2949				}
2950			}
2951		}
2952		mtx_unlock(&np->n_mtx);
2953	} else
2954		BO_UNLOCK(bo);
2955	if (NFSHASPNFS(nmp)) {
2956		nfscl_layoutcommit(vp, td);
2957		/*
2958		 * Invalidate the attribute cache, since writes to a DS
2959		 * won't update the size attribute.
2960		 */
2961		mtx_lock(&np->n_mtx);
2962		np->n_attrstamp = 0;
2963	} else
2964		mtx_lock(&np->n_mtx);
2965	if (np->n_flag & NWRITEERR) {
2966		error = np->n_error;
2967		np->n_flag &= ~NWRITEERR;
2968	}
2969  	if (commit && bo->bo_dirty.bv_cnt == 0 &&
2970	    bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
2971  		np->n_flag &= ~NMODIFIED;
2972	mtx_unlock(&np->n_mtx);
2973done:
2974	if (bvec != NULL && bvec != bvec_on_stack)
2975		free(bvec, M_TEMP);
2976	if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
2977	    (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
2978	     np->n_directio_asyncwr != 0) && trycnt++ < 5) {
2979		/* try, try again... */
2980		passone = 1;
2981		wcred = NULL;
2982		bvec = NULL;
2983		bvecsize = 0;
2984printf("try%d\n", trycnt);
2985		goto again;
2986	}
2987	return (error);
2988}
2989
2990/*
2991 * NFS advisory byte-level locks.
2992 */
2993static int
2994nfs_advlock(struct vop_advlock_args *ap)
2995{
2996	struct vnode *vp = ap->a_vp;
2997	struct ucred *cred;
2998	struct nfsnode *np = VTONFS(ap->a_vp);
2999	struct proc *p = (struct proc *)ap->a_id;
3000	struct thread *td = curthread;	/* XXX */
3001	struct vattr va;
3002	int ret, error = EOPNOTSUPP;
3003	u_quad_t size;
3004
3005	if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3006		if (vp->v_type != VREG)
3007			return (EINVAL);
3008		if ((ap->a_flags & F_POSIX) != 0)
3009			cred = p->p_ucred;
3010		else
3011			cred = td->td_ucred;
3012		NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3013		if (vp->v_iflag & VI_DOOMED) {
3014			NFSVOPUNLOCK(vp, 0);
3015			return (EBADF);
3016		}
3017
3018		/*
3019		 * If this is unlocking a write locked region, flush and
3020		 * commit them before unlocking. This is required by
3021		 * RFC3530 Sec. 9.3.2.
3022		 */
3023		if (ap->a_op == F_UNLCK &&
3024		    nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3025		    ap->a_flags))
3026			(void) ncl_flush(vp, MNT_WAIT, cred, td, 1, 0);
3027
3028		/*
3029		 * Loop around doing the lock op, while a blocking lock
3030		 * must wait for the lock op to succeed.
3031		 */
3032		do {
3033			ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3034			    ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3035			if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3036			    ap->a_op == F_SETLK) {
3037				NFSVOPUNLOCK(vp, 0);
3038				error = nfs_catnap(PZERO | PCATCH, ret,
3039				    "ncladvl");
3040				if (error)
3041					return (EINTR);
3042				NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3043				if (vp->v_iflag & VI_DOOMED) {
3044					NFSVOPUNLOCK(vp, 0);
3045					return (EBADF);
3046				}
3047			}
3048		} while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3049		     ap->a_op == F_SETLK);
3050		if (ret == NFSERR_DENIED) {
3051			NFSVOPUNLOCK(vp, 0);
3052			return (EAGAIN);
3053		} else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3054			NFSVOPUNLOCK(vp, 0);
3055			return (ret);
3056		} else if (ret != 0) {
3057			NFSVOPUNLOCK(vp, 0);
3058			return (EACCES);
3059		}
3060
3061		/*
3062		 * Now, if we just got a lock, invalidate data in the buffer
3063		 * cache, as required, so that the coherency conforms with
3064		 * RFC3530 Sec. 9.3.2.
3065		 */
3066		if (ap->a_op == F_SETLK) {
3067			if ((np->n_flag & NMODIFIED) == 0) {
3068				np->n_attrstamp = 0;
3069				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3070				ret = VOP_GETATTR(vp, &va, cred);
3071			}
3072			if ((np->n_flag & NMODIFIED) || ret ||
3073			    np->n_change != va.va_filerev) {
3074				(void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3075				np->n_attrstamp = 0;
3076				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3077				ret = VOP_GETATTR(vp, &va, cred);
3078				if (!ret) {
3079					np->n_mtime = va.va_mtime;
3080					np->n_change = va.va_filerev;
3081				}
3082			}
3083			/* Mark that a file lock has been acquired. */
3084			mtx_lock(&np->n_mtx);
3085			np->n_flag |= NHASBEENLOCKED;
3086			mtx_unlock(&np->n_mtx);
3087		}
3088		NFSVOPUNLOCK(vp, 0);
3089		return (0);
3090	} else if (!NFS_ISV4(vp)) {
3091		error = NFSVOPLOCK(vp, LK_SHARED);
3092		if (error)
3093			return (error);
3094		if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3095			size = VTONFS(vp)->n_size;
3096			NFSVOPUNLOCK(vp, 0);
3097			error = lf_advlock(ap, &(vp->v_lockf), size);
3098		} else {
3099			if (nfs_advlock_p != NULL)
3100				error = nfs_advlock_p(ap);
3101			else {
3102				NFSVOPUNLOCK(vp, 0);
3103				error = ENOLCK;
3104			}
3105		}
3106		if (error == 0 && ap->a_op == F_SETLK) {
3107			/* Mark that a file lock has been acquired. */
3108			mtx_lock(&np->n_mtx);
3109			np->n_flag |= NHASBEENLOCKED;
3110			mtx_unlock(&np->n_mtx);
3111		}
3112	}
3113	return (error);
3114}
3115
3116/*
3117 * NFS advisory byte-level locks.
3118 */
3119static int
3120nfs_advlockasync(struct vop_advlockasync_args *ap)
3121{
3122	struct vnode *vp = ap->a_vp;
3123	u_quad_t size;
3124	int error;
3125
3126	if (NFS_ISV4(vp))
3127		return (EOPNOTSUPP);
3128	error = NFSVOPLOCK(vp, LK_SHARED);
3129	if (error)
3130		return (error);
3131	if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3132		size = VTONFS(vp)->n_size;
3133		NFSVOPUNLOCK(vp, 0);
3134		error = lf_advlockasync(ap, &(vp->v_lockf), size);
3135	} else {
3136		NFSVOPUNLOCK(vp, 0);
3137		error = EOPNOTSUPP;
3138	}
3139	return (error);
3140}
3141
3142/*
3143 * Print out the contents of an nfsnode.
3144 */
3145static int
3146nfs_print(struct vop_print_args *ap)
3147{
3148	struct vnode *vp = ap->a_vp;
3149	struct nfsnode *np = VTONFS(vp);
3150
3151	ncl_printf("\tfileid %ld fsid 0x%x",
3152	   np->n_vattr.na_fileid, np->n_vattr.na_fsid);
3153	if (vp->v_type == VFIFO)
3154		fifo_printinfo(vp);
3155	printf("\n");
3156	return (0);
3157}
3158
3159/*
3160 * This is the "real" nfs::bwrite(struct buf*).
3161 * We set B_CACHE if this is a VMIO buffer.
3162 */
3163int
3164ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
3165{
3166	int s;
3167	int oldflags = bp->b_flags;
3168#if 0
3169	int retv = 1;
3170	off_t off;
3171#endif
3172
3173	BUF_ASSERT_HELD(bp);
3174
3175	if (bp->b_flags & B_INVAL) {
3176		brelse(bp);
3177		return(0);
3178	}
3179
3180	bp->b_flags |= B_CACHE;
3181
3182	/*
3183	 * Undirty the bp.  We will redirty it later if the I/O fails.
3184	 */
3185
3186	s = splbio();
3187	bundirty(bp);
3188	bp->b_flags &= ~B_DONE;
3189	bp->b_ioflags &= ~BIO_ERROR;
3190	bp->b_iocmd = BIO_WRITE;
3191
3192	bufobj_wref(bp->b_bufobj);
3193	curthread->td_ru.ru_oublock++;
3194	splx(s);
3195
3196	/*
3197	 * Note: to avoid loopback deadlocks, we do not
3198	 * assign b_runningbufspace.
3199	 */
3200	vfs_busy_pages(bp, 1);
3201
3202	BUF_KERNPROC(bp);
3203	bp->b_iooffset = dbtob(bp->b_blkno);
3204	bstrategy(bp);
3205
3206	if( (oldflags & B_ASYNC) == 0) {
3207		int rtval = bufwait(bp);
3208
3209		if (oldflags & B_DELWRI) {
3210			s = splbio();
3211			reassignbuf(bp);
3212			splx(s);
3213		}
3214		brelse(bp);
3215		return (rtval);
3216	}
3217
3218	return (0);
3219}
3220
3221/*
3222 * nfs special file access vnode op.
3223 * Essentially just get vattr and then imitate iaccess() since the device is
3224 * local to the client.
3225 */
3226static int
3227nfsspec_access(struct vop_access_args *ap)
3228{
3229	struct vattr *vap;
3230	struct ucred *cred = ap->a_cred;
3231	struct vnode *vp = ap->a_vp;
3232	accmode_t accmode = ap->a_accmode;
3233	struct vattr vattr;
3234	int error;
3235
3236	/*
3237	 * Disallow write attempts on filesystems mounted read-only;
3238	 * unless the file is a socket, fifo, or a block or character
3239	 * device resident on the filesystem.
3240	 */
3241	if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3242		switch (vp->v_type) {
3243		case VREG:
3244		case VDIR:
3245		case VLNK:
3246			return (EROFS);
3247		default:
3248			break;
3249		}
3250	}
3251	vap = &vattr;
3252	error = VOP_GETATTR(vp, vap, cred);
3253	if (error)
3254		goto out;
3255	error  = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3256	    accmode, cred, NULL);
3257out:
3258	return error;
3259}
3260
3261/*
3262 * Read wrapper for fifos.
3263 */
3264static int
3265nfsfifo_read(struct vop_read_args *ap)
3266{
3267	struct nfsnode *np = VTONFS(ap->a_vp);
3268	int error;
3269
3270	/*
3271	 * Set access flag.
3272	 */
3273	mtx_lock(&np->n_mtx);
3274	np->n_flag |= NACC;
3275	vfs_timestamp(&np->n_atim);
3276	mtx_unlock(&np->n_mtx);
3277	error = fifo_specops.vop_read(ap);
3278	return error;
3279}
3280
3281/*
3282 * Write wrapper for fifos.
3283 */
3284static int
3285nfsfifo_write(struct vop_write_args *ap)
3286{
3287	struct nfsnode *np = VTONFS(ap->a_vp);
3288
3289	/*
3290	 * Set update flag.
3291	 */
3292	mtx_lock(&np->n_mtx);
3293	np->n_flag |= NUPD;
3294	vfs_timestamp(&np->n_mtim);
3295	mtx_unlock(&np->n_mtx);
3296	return(fifo_specops.vop_write(ap));
3297}
3298
3299/*
3300 * Close wrapper for fifos.
3301 *
3302 * Update the times on the nfsnode then do fifo close.
3303 */
3304static int
3305nfsfifo_close(struct vop_close_args *ap)
3306{
3307	struct vnode *vp = ap->a_vp;
3308	struct nfsnode *np = VTONFS(vp);
3309	struct vattr vattr;
3310	struct timespec ts;
3311
3312	mtx_lock(&np->n_mtx);
3313	if (np->n_flag & (NACC | NUPD)) {
3314		vfs_timestamp(&ts);
3315		if (np->n_flag & NACC)
3316			np->n_atim = ts;
3317		if (np->n_flag & NUPD)
3318			np->n_mtim = ts;
3319		np->n_flag |= NCHG;
3320		if (vrefcnt(vp) == 1 &&
3321		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3322			VATTR_NULL(&vattr);
3323			if (np->n_flag & NACC)
3324				vattr.va_atime = np->n_atim;
3325			if (np->n_flag & NUPD)
3326				vattr.va_mtime = np->n_mtim;
3327			mtx_unlock(&np->n_mtx);
3328			(void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3329			goto out;
3330		}
3331	}
3332	mtx_unlock(&np->n_mtx);
3333out:
3334	return (fifo_specops.vop_close(ap));
3335}
3336
3337/*
3338 * Just call ncl_writebp() with the force argument set to 1.
3339 *
3340 * NOTE: B_DONE may or may not be set in a_bp on call.
3341 */
3342static int
3343nfs_bwrite(struct buf *bp)
3344{
3345
3346	return (ncl_writebp(bp, 1, curthread));
3347}
3348
3349struct buf_ops buf_ops_newnfs = {
3350	.bop_name	=	"buf_ops_nfs",
3351	.bop_write	=	nfs_bwrite,
3352	.bop_strategy	=	bufstrategy,
3353	.bop_sync	=	bufsync,
3354	.bop_bdflush	=	bufbdflush,
3355};
3356
3357/*
3358 * Cloned from vop_stdlock(), and then the ugly hack added.
3359 */
3360static int
3361nfs_lock1(struct vop_lock1_args *ap)
3362{
3363	struct vnode *vp = ap->a_vp;
3364	int error = 0;
3365
3366	/*
3367	 * Since vfs_hash_get() calls vget() and it will no longer work
3368	 * for FreeBSD8 with flags == 0, I can only think of this horrible
3369	 * hack to work around it. I call vfs_hash_get() with LK_EXCLOTHER
3370	 * and then handle it here. All I want for this case is a v_usecount
3371	 * on the vnode to use for recovery, while another thread might
3372	 * hold a lock on the vnode. I have the other threads blocked, so
3373	 * there isn't any race problem.
3374	 */
3375	if ((ap->a_flags & LK_TYPE_MASK) == LK_EXCLOTHER) {
3376		if ((ap->a_flags & LK_INTERLOCK) == 0)
3377			panic("ncllock1");
3378		if ((vp->v_iflag & VI_DOOMED))
3379			error = ENOENT;
3380		VI_UNLOCK(vp);
3381		return (error);
3382	}
3383	return (_lockmgr_args(vp->v_vnlock, ap->a_flags, VI_MTX(vp),
3384	    LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, ap->a_file,
3385	    ap->a_line));
3386}
3387
3388static int
3389nfs_getacl(struct vop_getacl_args *ap)
3390{
3391	int error;
3392
3393	if (ap->a_type != ACL_TYPE_NFS4)
3394		return (EOPNOTSUPP);
3395	error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3396	    NULL);
3397	if (error > NFSERR_STALE) {
3398		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3399		error = EPERM;
3400	}
3401	return (error);
3402}
3403
3404static int
3405nfs_setacl(struct vop_setacl_args *ap)
3406{
3407	int error;
3408
3409	if (ap->a_type != ACL_TYPE_NFS4)
3410		return (EOPNOTSUPP);
3411	error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3412	    NULL);
3413	if (error > NFSERR_STALE) {
3414		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3415		error = EPERM;
3416	}
3417	return (error);
3418}
3419
3420/*
3421 * Return POSIX pathconf information applicable to nfs filesystems.
3422 */
3423static int
3424nfs_pathconf(struct vop_pathconf_args *ap)
3425{
3426	struct nfsv3_pathconf pc;
3427	struct nfsvattr nfsva;
3428	struct vnode *vp = ap->a_vp;
3429	struct thread *td = curthread;
3430	int attrflag, error;
3431
3432	if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
3433	    ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
3434	    ap->a_name == _PC_NO_TRUNC)) ||
3435	    (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) {
3436		/*
3437		 * Since only the above 4 a_names are returned by the NFSv3
3438		 * Pathconf RPC, there is no point in doing it for others.
3439		 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
3440		 * be used for _PC_NFS4_ACL as well.
3441		 */
3442		error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
3443		    &attrflag, NULL);
3444		if (attrflag != 0)
3445			(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
3446			    1);
3447		if (error != 0)
3448			return (error);
3449	} else {
3450		/*
3451		 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
3452		 * just fake them.
3453		 */
3454		pc.pc_linkmax = LINK_MAX;
3455		pc.pc_namemax = NFS_MAXNAMLEN;
3456		pc.pc_notrunc = 1;
3457		pc.pc_chownrestricted = 1;
3458		pc.pc_caseinsensitive = 0;
3459		pc.pc_casepreserving = 1;
3460		error = 0;
3461	}
3462	switch (ap->a_name) {
3463	case _PC_LINK_MAX:
3464		*ap->a_retval = pc.pc_linkmax;
3465		break;
3466	case _PC_NAME_MAX:
3467		*ap->a_retval = pc.pc_namemax;
3468		break;
3469	case _PC_PATH_MAX:
3470		*ap->a_retval = PATH_MAX;
3471		break;
3472	case _PC_PIPE_BUF:
3473		*ap->a_retval = PIPE_BUF;
3474		break;
3475	case _PC_CHOWN_RESTRICTED:
3476		*ap->a_retval = pc.pc_chownrestricted;
3477		break;
3478	case _PC_NO_TRUNC:
3479		*ap->a_retval = pc.pc_notrunc;
3480		break;
3481	case _PC_ACL_EXTENDED:
3482		*ap->a_retval = 0;
3483		break;
3484	case _PC_ACL_NFS4:
3485		if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
3486		    NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
3487			*ap->a_retval = 1;
3488		else
3489			*ap->a_retval = 0;
3490		break;
3491	case _PC_ACL_PATH_MAX:
3492		if (NFS_ISV4(vp))
3493			*ap->a_retval = ACL_MAX_ENTRIES;
3494		else
3495			*ap->a_retval = 3;
3496		break;
3497	case _PC_MAC_PRESENT:
3498		*ap->a_retval = 0;
3499		break;
3500	case _PC_ASYNC_IO:
3501		/* _PC_ASYNC_IO should have been handled by upper layers. */
3502		KASSERT(0, ("_PC_ASYNC_IO should not get here"));
3503		error = EINVAL;
3504		break;
3505	case _PC_PRIO_IO:
3506		*ap->a_retval = 0;
3507		break;
3508	case _PC_SYNC_IO:
3509		*ap->a_retval = 0;
3510		break;
3511	case _PC_ALLOC_SIZE_MIN:
3512		*ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
3513		break;
3514	case _PC_FILESIZEBITS:
3515		if (NFS_ISV34(vp))
3516			*ap->a_retval = 64;
3517		else
3518			*ap->a_retval = 32;
3519		break;
3520	case _PC_REC_INCR_XFER_SIZE:
3521		*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3522		break;
3523	case _PC_REC_MAX_XFER_SIZE:
3524		*ap->a_retval = -1; /* means ``unlimited'' */
3525		break;
3526	case _PC_REC_MIN_XFER_SIZE:
3527		*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3528		break;
3529	case _PC_REC_XFER_ALIGN:
3530		*ap->a_retval = PAGE_SIZE;
3531		break;
3532	case _PC_SYMLINK_MAX:
3533		*ap->a_retval = NFS_MAXPATHLEN;
3534		break;
3535
3536	default:
3537		error = EINVAL;
3538		break;
3539	}
3540	return (error);
3541}
3542
3543