nfs_clvnops.c revision 201029
1254939Sdes/*-
2254939Sdes * Copyright (c) 1989, 1993
3254939Sdes *	The Regents of the University of California.  All rights reserved.
4254939Sdes *
5254939Sdes * This code is derived from software contributed to Berkeley by
6254939Sdes * Rick Macklem at The University of Guelph.
7254939Sdes *
8254939Sdes * Redistribution and use in source and binary forms, with or without
9254939Sdes * modification, are permitted provided that the following conditions
10254939Sdes * are met:
11254939Sdes * 1. Redistributions of source code must retain the above copyright
12254939Sdes *    notice, this list of conditions and the following disclaimer.
13254939Sdes * 2. Redistributions in binary form must reproduce the above copyright
14254939Sdes *    notice, this list of conditions and the following disclaimer in the
15254939Sdes *    documentation and/or other materials provided with the distribution.
16254939Sdes * 4. Neither the name of the University nor the names of its contributors
17254939Sdes *    may be used to endorse or promote products derived from this software
18254939Sdes *    without specific prior written permission.
19254939Sdes *
20254939Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21254939Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22254939Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23254939Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24254939Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25254939Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26254939Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27254939Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28254939Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29254939Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30254939Sdes * SUCH DAMAGE.
31254939Sdes *
32254939Sdes *	from nfs_vnops.c	8.16 (Berkeley) 5/27/95
33254939Sdes */
34254939Sdes
35254939Sdes#include <sys/cdefs.h>
36254939Sdes__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clvnops.c 201029 2009-12-26 19:15:15Z rmacklem $");
37254939Sdes
38254939Sdes/*
39254939Sdes * vnode op calls for Sun NFS version 2, 3 and 4
40254939Sdes */
41254939Sdes
42254939Sdes#include "opt_inet.h"
43254939Sdes
44254939Sdes#include <sys/param.h>
45254939Sdes#include <sys/kernel.h>
46254939Sdes#include <sys/systm.h>
47254939Sdes#include <sys/resourcevar.h>
48254939Sdes#include <sys/proc.h>
49254939Sdes#include <sys/mount.h>
50254939Sdes#include <sys/bio.h>
51254939Sdes#include <sys/buf.h>
52254939Sdes#include <sys/jail.h>
53254939Sdes#include <sys/malloc.h>
54254939Sdes#include <sys/mbuf.h>
55254939Sdes#include <sys/namei.h>
56254939Sdes#include <sys/socket.h>
57254939Sdes#include <sys/vnode.h>
58254939Sdes#include <sys/dirent.h>
59254939Sdes#include <sys/fcntl.h>
60254939Sdes#include <sys/lockf.h>
61254939Sdes#include <sys/stat.h>
62254939Sdes#include <sys/sysctl.h>
63254939Sdes#include <sys/signalvar.h>
64254939Sdes
65254939Sdes#include <vm/vm.h>
66254939Sdes#include <vm/vm_object.h>
67254939Sdes#include <vm/vm_extern.h>
68254939Sdes#include <vm/vm_object.h>
69254939Sdes
70254939Sdes
71254939Sdes#include <fs/nfs/nfsport.h>
72254939Sdes#include <fs/nfsclient/nfsnode.h>
73254939Sdes#include <fs/nfsclient/nfsmount.h>
74254939Sdes#include <fs/nfsclient/nfs.h>
75254939Sdes#include <fs/nfsclient/nfs_lock.h>
76254939Sdes
77254939Sdes#include <net/if.h>
78254939Sdes#include <netinet/in.h>
79254939Sdes#include <netinet/in_var.h>
80254939Sdes
81254939Sdes/* Defs */
82254939Sdes#define	TRUE	1
83254939Sdes#define	FALSE	0
84254939Sdes
85254939Sdesextern struct nfsstats newnfsstats;
86254939SdesMALLOC_DECLARE(M_NEWNFSREQ);
87254939Sdesvop_advlock_t	*ncl_advlock_p = ncl_dolock;
88254939Sdes
89254939Sdes/*
90254939Sdes * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
91254939Sdes * calls are not in getblk() and brelse() so that they would not be necessary
92254939Sdes * here.
93254939Sdes */
94254939Sdes#ifndef B_VMIO
95254939Sdes#define	vfs_busy_pages(bp, f)
96254939Sdes#endif
97254939Sdes
98254939Sdesstatic vop_read_t	nfsfifo_read;
99254939Sdesstatic vop_write_t	nfsfifo_write;
100254939Sdesstatic vop_close_t	nfsfifo_close;
101254939Sdesstatic int	nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
102254939Sdes		    struct thread *);
103254939Sdesstatic vop_lookup_t	nfs_lookup;
104254939Sdesstatic vop_create_t	nfs_create;
105254939Sdesstatic vop_mknod_t	nfs_mknod;
106254939Sdesstatic vop_open_t	nfs_open;
107254939Sdesstatic vop_close_t	nfs_close;
108254939Sdesstatic vop_access_t	nfs_access;
109254939Sdesstatic vop_getattr_t	nfs_getattr;
110254939Sdesstatic vop_setattr_t	nfs_setattr;
111254939Sdesstatic vop_read_t	nfs_read;
112254939Sdesstatic vop_fsync_t	nfs_fsync;
113254939Sdesstatic vop_remove_t	nfs_remove;
114254939Sdesstatic vop_link_t	nfs_link;
115254939Sdesstatic vop_rename_t	nfs_rename;
116254939Sdesstatic vop_mkdir_t	nfs_mkdir;
117254939Sdesstatic vop_rmdir_t	nfs_rmdir;
118254939Sdesstatic vop_symlink_t	nfs_symlink;
119254939Sdesstatic vop_readdir_t	nfs_readdir;
120254939Sdesstatic vop_strategy_t	nfs_strategy;
121254939Sdesstatic vop_lock1_t	nfs_lock1;
122254939Sdesstatic	int	nfs_lookitup(struct vnode *, char *, int,
123254939Sdes		    struct ucred *, struct thread *, struct nfsnode **);
124254939Sdesstatic	int	nfs_sillyrename(struct vnode *, struct vnode *,
125254939Sdes		    struct componentname *);
126254939Sdesstatic vop_access_t	nfsspec_access;
127254939Sdesstatic vop_readlink_t	nfs_readlink;
128254939Sdesstatic vop_print_t	nfs_print;
129254939Sdesstatic vop_advlock_t	nfs_advlock;
130254939Sdesstatic vop_advlockasync_t nfs_advlockasync;
131254939Sdesstatic vop_getacl_t nfs_getacl;
132254939Sdesstatic vop_setacl_t nfs_setacl;
133254939Sdes
134254939Sdes/*
135254939Sdes * Global vfs data structures for nfs
136254939Sdes */
137254939Sdesstruct vop_vector newnfs_vnodeops = {
138254939Sdes	.vop_default =		&default_vnodeops,
139254939Sdes	.vop_access =		nfs_access,
140254939Sdes	.vop_advlock =		nfs_advlock,
141254939Sdes	.vop_advlockasync =	nfs_advlockasync,
142254939Sdes	.vop_close =		nfs_close,
143254939Sdes	.vop_create =		nfs_create,
144254939Sdes	.vop_fsync =		nfs_fsync,
145254939Sdes	.vop_getattr =		nfs_getattr,
146254939Sdes	.vop_getpages =		ncl_getpages,
147254939Sdes	.vop_putpages =		ncl_putpages,
148254939Sdes	.vop_inactive =		ncl_inactive,
149254939Sdes	.vop_link =		nfs_link,
150254939Sdes	.vop_lock1 = 		nfs_lock1,
151254939Sdes	.vop_lookup =		nfs_lookup,
152254939Sdes	.vop_mkdir =		nfs_mkdir,
153254939Sdes	.vop_mknod =		nfs_mknod,
154254939Sdes	.vop_open =		nfs_open,
155254939Sdes	.vop_print =		nfs_print,
156254939Sdes	.vop_read =		nfs_read,
157254939Sdes	.vop_readdir =		nfs_readdir,
158254939Sdes	.vop_readlink =		nfs_readlink,
159254939Sdes	.vop_reclaim =		ncl_reclaim,
160254939Sdes	.vop_remove =		nfs_remove,
161254939Sdes	.vop_rename =		nfs_rename,
162254939Sdes	.vop_rmdir =		nfs_rmdir,
163254939Sdes	.vop_setattr =		nfs_setattr,
164254939Sdes	.vop_strategy =		nfs_strategy,
165254939Sdes	.vop_symlink =		nfs_symlink,
166254939Sdes	.vop_write =		ncl_write,
167254939Sdes	.vop_getacl =		nfs_getacl,
168254939Sdes	.vop_setacl =		nfs_setacl,
169254939Sdes};
170254939Sdes
171254939Sdesstruct vop_vector newnfs_fifoops = {
172254939Sdes	.vop_default =		&fifo_specops,
173254939Sdes	.vop_access =		nfsspec_access,
174254939Sdes	.vop_close =		nfsfifo_close,
175254939Sdes	.vop_fsync =		nfs_fsync,
176254939Sdes	.vop_getattr =		nfs_getattr,
177254939Sdes	.vop_inactive =		ncl_inactive,
178254939Sdes	.vop_print =		nfs_print,
179254939Sdes	.vop_read =		nfsfifo_read,
180254939Sdes	.vop_reclaim =		ncl_reclaim,
181254939Sdes	.vop_setattr =		nfs_setattr,
182254939Sdes	.vop_write =		nfsfifo_write,
183254939Sdes};
184254939Sdes
185254939Sdesstatic int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
186254939Sdes    struct componentname *cnp, struct vattr *vap);
187254939Sdesstatic int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
188254939Sdes    int namelen, struct ucred *cred, struct thread *td);
189254939Sdesstatic int nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp,
190254939Sdes    char *fnameptr, int fnamelen, struct vnode *tdvp, struct vnode *tvp,
191254939Sdes    char *tnameptr, int tnamelen, struct ucred *cred, struct thread *td);
192254939Sdesstatic int nfs_renameit(struct vnode *sdvp, struct vnode *svp,
193254939Sdes    struct componentname *scnp, struct sillyrename *sp);
194254939Sdes
195254939Sdes/*
196254939Sdes * Global variables
197254939Sdes */
198254939Sdes#define	DIRHDSIZ	(sizeof (struct dirent) - (MAXNAMLEN + 1))
199254939Sdes
200254939SdesSYSCTL_DECL(_vfs_newnfs);
201254939Sdes
202254939Sdesstatic int	nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
203254939SdesSYSCTL_INT(_vfs_newnfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
204254939Sdes	   &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
205254939Sdes
206254939Sdesstatic int	nfs_prime_access_cache = 0;
207254939SdesSYSCTL_INT(_vfs_newnfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
208254939Sdes	   &nfs_prime_access_cache, 0,
209254939Sdes	   "Prime NFS ACCESS cache when fetching attributes");
210254939Sdes
211254939Sdesstatic int	newnfs_commit_on_close = 0;
212254939SdesSYSCTL_INT(_vfs_newnfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
213254939Sdes    &newnfs_commit_on_close, 0, "write+commit on close, else only write");
214254939Sdes
215254939Sdesstatic int	nfs_clean_pages_on_close = 1;
216254939SdesSYSCTL_INT(_vfs_newnfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
217254939Sdes	   &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
218254939Sdes
219254939Sdesint newnfs_directio_enable = 0;
220254939SdesSYSCTL_INT(_vfs_newnfs, OID_AUTO, directio_enable, CTLFLAG_RW,
221254939Sdes	   &newnfs_directio_enable, 0, "Enable NFS directio");
222254939Sdes
223254939Sdesstatic int newnfs_neglookup_enable = 1;
224254939SdesSYSCTL_INT(_vfs_newnfs, OID_AUTO, neglookup_enable, CTLFLAG_RW,
225254939Sdes    &newnfs_neglookup_enable, 0, "Enable NFS negative lookup caching");
226254939Sdes
227254939Sdes/*
228254939Sdes * This sysctl allows other processes to mmap a file that has been opened
229254939Sdes * O_DIRECT by a process.  In general, having processes mmap the file while
230254939Sdes * Direct IO is in progress can lead to Data Inconsistencies.  But, we allow
231254939Sdes * this by default to prevent DoS attacks - to prevent a malicious user from
232254939Sdes * opening up files O_DIRECT preventing other users from mmap'ing these
233254939Sdes * files.  "Protected" environments where stricter consistency guarantees are
234254939Sdes * required can disable this knob.  The process that opened the file O_DIRECT
235254939Sdes * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
236254939Sdes * meaningful.
237254939Sdes */
238254939Sdesint newnfs_directio_allow_mmap = 1;
239254939SdesSYSCTL_INT(_vfs_newnfs, OID_AUTO, directio_allow_mmap, CTLFLAG_RW,
240254939Sdes	   &newnfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
241254939Sdes
242254939Sdes#if 0
243254939SdesSYSCTL_INT(_vfs_newnfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
244254939Sdes	   &newnfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
245254939Sdes
246254939SdesSYSCTL_INT(_vfs_newnfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
247254939Sdes	   &newnfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
248254939Sdes#endif
249254939Sdes
250254939Sdes#define	NFSACCESS_ALL (NFSACCESS_READ | NFSACCESS_MODIFY		\
251254939Sdes			 | NFSACCESS_EXTEND | NFSACCESS_EXECUTE	\
252254939Sdes			 | NFSACCESS_DELETE | NFSACCESS_LOOKUP)
253254939Sdes
254254939Sdes/*
255254939Sdes * SMP Locking Note :
256254939Sdes * The list of locks after the description of the lock is the ordering
257254939Sdes * of other locks acquired with the lock held.
258254939Sdes * np->n_mtx : Protects the fields in the nfsnode.
259254939Sdes       VM Object Lock
260254939Sdes       VI_MTX (acquired indirectly)
261254939Sdes * nmp->nm_mtx : Protects the fields in the nfsmount.
262254939Sdes       rep->r_mtx
263254939Sdes * ncl_iod_mutex : Global lock, protects shared nfsiod state.
264254939Sdes * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
265254939Sdes       nmp->nm_mtx
266254939Sdes       rep->r_mtx
267254939Sdes * rep->r_mtx : Protects the fields in an nfsreq.
268254939Sdes */
269254939Sdes
270254939Sdesstatic int
271254939Sdesnfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
272254939Sdes    struct ucred *cred, u_int32_t *retmode)
273254939Sdes{
274254939Sdes	int error = 0, attrflag, i, lrupos;
275254939Sdes	u_int32_t rmode;
276254939Sdes	struct nfsnode *np = VTONFS(vp);
277254939Sdes	struct nfsvattr nfsva;
278254939Sdes
279254939Sdes	error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
280254939Sdes	    &rmode, NULL);
281254939Sdes	if (attrflag)
282254939Sdes		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
283254939Sdes	if (!error) {
284254939Sdes		lrupos = 0;
285254939Sdes		mtx_lock(&np->n_mtx);
286254939Sdes		for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
287254939Sdes			if (np->n_accesscache[i].uid == cred->cr_uid) {
288254939Sdes				np->n_accesscache[i].mode = rmode;
289254939Sdes				np->n_accesscache[i].stamp = time_second;
290254939Sdes				break;
291254939Sdes			}
292254939Sdes			if (i > 0 && np->n_accesscache[i].stamp <
293254939Sdes			    np->n_accesscache[lrupos].stamp)
294254939Sdes				lrupos = i;
295254939Sdes		}
296254939Sdes		if (i == NFS_ACCESSCACHESIZE) {
297254939Sdes			np->n_accesscache[lrupos].uid = cred->cr_uid;
298254939Sdes			np->n_accesscache[lrupos].mode = rmode;
299254939Sdes			np->n_accesscache[lrupos].stamp = time_second;
300254939Sdes		}
301254939Sdes		mtx_unlock(&np->n_mtx);
302254939Sdes		if (retmode != NULL)
303254939Sdes			*retmode = rmode;
304254939Sdes	} else if (NFS_ISV4(vp)) {
305254939Sdes		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
306254939Sdes	}
307254939Sdes	return (error);
308254939Sdes}
309254939Sdes
310254939Sdes/*
311254939Sdes * nfs access vnode op.
312254939Sdes * For nfs version 2, just return ok. File accesses may fail later.
313254939Sdes * For nfs version 3, use the access rpc to check accessibility. If file modes
314254939Sdes * are changed on the server, accesses might still fail later.
315254939Sdes */
316254939Sdesstatic int
317254939Sdesnfs_access(struct vop_access_args *ap)
318254939Sdes{
319254939Sdes	struct vnode *vp = ap->a_vp;
320254939Sdes	int error = 0, i, gotahit;
321254939Sdes	u_int32_t mode, wmode, rmode;
322254939Sdes	int v34 = NFS_ISV34(vp);
323254939Sdes	struct nfsnode *np = VTONFS(vp);
324254939Sdes
325254939Sdes	/*
326254939Sdes	 * Disallow write attempts on filesystems mounted read-only;
327254939Sdes	 * unless the file is a socket, fifo, or a block or character
328254939Sdes	 * device resident on the filesystem.
329254939Sdes	 */
330254939Sdes	if ((ap->a_accmode & (VWRITE | VAPPEND | VWRITE_NAMED_ATTRS |
331254939Sdes	    VDELETE_CHILD | VWRITE_ATTRIBUTES | VDELETE | VWRITE_ACL |
332254939Sdes	    VWRITE_OWNER)) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
333254939Sdes		switch (vp->v_type) {
334254939Sdes		case VREG:
335254939Sdes		case VDIR:
336254939Sdes		case VLNK:
337254939Sdes			return (EROFS);
338254939Sdes		default:
339254939Sdes			break;
340254939Sdes		}
341254939Sdes	}
342254939Sdes	/*
343254939Sdes	 * For nfs v3 or v4, check to see if we have done this recently, and if
344254939Sdes	 * so return our cached result instead of making an ACCESS call.
345254939Sdes	 * If not, do an access rpc, otherwise you are stuck emulating
346254939Sdes	 * ufs_access() locally using the vattr. This may not be correct,
347254939Sdes	 * since the server may apply other access criteria such as
348254939Sdes	 * client uid-->server uid mapping that we do not know about.
349254939Sdes	 */
350254939Sdes	if (v34) {
351254939Sdes		if (ap->a_accmode & VREAD)
352254939Sdes			mode = NFSACCESS_READ;
353254939Sdes		else
354254939Sdes			mode = 0;
355254939Sdes		if (vp->v_type != VDIR) {
356254939Sdes			if (ap->a_accmode & VWRITE)
357254939Sdes				mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
358254939Sdes			if (ap->a_accmode & VAPPEND)
359254939Sdes				mode |= NFSACCESS_EXTEND;
360254939Sdes			if (ap->a_accmode & VEXEC)
361254939Sdes				mode |= NFSACCESS_EXECUTE;
362254939Sdes			if (ap->a_accmode & VDELETE)
363254939Sdes				mode |= NFSACCESS_DELETE;
364254939Sdes		} else {
365254939Sdes			if (ap->a_accmode & VWRITE)
366254939Sdes				mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
367254939Sdes			if (ap->a_accmode & VAPPEND)
368254939Sdes				mode |= NFSACCESS_EXTEND;
369254939Sdes			if (ap->a_accmode & VEXEC)
370254939Sdes				mode |= NFSACCESS_LOOKUP;
371254939Sdes			if (ap->a_accmode & VDELETE)
372254939Sdes				mode |= NFSACCESS_DELETE;
373254939Sdes			if (ap->a_accmode & VDELETE_CHILD)
374254939Sdes				mode |= NFSACCESS_MODIFY;
375254939Sdes		}
376254939Sdes		/* XXX safety belt, only make blanket request if caching */
377254939Sdes		if (nfsaccess_cache_timeout > 0) {
378254939Sdes			wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
379254939Sdes				NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
380254939Sdes				NFSACCESS_DELETE | NFSACCESS_LOOKUP;
381254939Sdes		} else {
382254939Sdes			wmode = mode;
383254939Sdes		}
384254939Sdes
385254939Sdes		/*
386254939Sdes		 * Does our cached result allow us to give a definite yes to
387254939Sdes		 * this request?
388254939Sdes		 */
389254939Sdes		gotahit = 0;
390254939Sdes		mtx_lock(&np->n_mtx);
391254939Sdes		for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
392254939Sdes			if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
393254939Sdes			    if (time_second < (np->n_accesscache[i].stamp
394254939Sdes				+ nfsaccess_cache_timeout) &&
395254939Sdes				(np->n_accesscache[i].mode & mode) == mode) {
396254939Sdes				NFSINCRGLOBAL(newnfsstats.accesscache_hits);
397254939Sdes				gotahit = 1;
398254939Sdes			    }
399254939Sdes			    break;
400254939Sdes			}
401254939Sdes		}
402254939Sdes		mtx_unlock(&np->n_mtx);
403254939Sdes		if (gotahit == 0) {
404254939Sdes			/*
405254939Sdes			 * Either a no, or a don't know.  Go to the wire.
406254939Sdes			 */
407254939Sdes			NFSINCRGLOBAL(newnfsstats.accesscache_misses);
408254939Sdes		        error = nfs34_access_otw(vp, wmode, ap->a_td,
409254939Sdes			    ap->a_cred, &rmode);
410254939Sdes			if (!error &&
411254939Sdes			    (rmode & mode) != mode)
412254939Sdes				error = EACCES;
413254939Sdes		}
414254939Sdes		return (error);
415254939Sdes	} else {
416254939Sdes		if ((error = nfsspec_access(ap)) != 0) {
417254939Sdes			return (error);
418254939Sdes		}
419254939Sdes		/*
420254939Sdes		 * Attempt to prevent a mapped root from accessing a file
421254939Sdes		 * which it shouldn't.  We try to read a byte from the file
422254939Sdes		 * if the user is root and the file is not zero length.
423254939Sdes		 * After calling nfsspec_access, we should have the correct
424254939Sdes		 * file size cached.
425254939Sdes		 */
426254939Sdes		mtx_lock(&np->n_mtx);
427254939Sdes		if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
428254939Sdes		    && VTONFS(vp)->n_size > 0) {
429254939Sdes			struct iovec aiov;
430254939Sdes			struct uio auio;
431254939Sdes			char buf[1];
432254939Sdes
433254939Sdes			mtx_unlock(&np->n_mtx);
434254939Sdes			aiov.iov_base = buf;
435254939Sdes			aiov.iov_len = 1;
436254939Sdes			auio.uio_iov = &aiov;
437254939Sdes			auio.uio_iovcnt = 1;
438254939Sdes			auio.uio_offset = 0;
439254939Sdes			auio.uio_resid = 1;
440254939Sdes			auio.uio_segflg = UIO_SYSSPACE;
441254939Sdes			auio.uio_rw = UIO_READ;
442254939Sdes			auio.uio_td = ap->a_td;
443254939Sdes
444254939Sdes			if (vp->v_type == VREG)
445254939Sdes				error = ncl_readrpc(vp, &auio, ap->a_cred);
446254939Sdes			else if (vp->v_type == VDIR) {
447254939Sdes				char* bp;
448254939Sdes				bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
449254939Sdes				aiov.iov_base = bp;
450254939Sdes				aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
451254939Sdes				error = ncl_readdirrpc(vp, &auio, ap->a_cred,
452254939Sdes				    ap->a_td);
453254939Sdes				free(bp, M_TEMP);
454254939Sdes			} else if (vp->v_type == VLNK)
455254939Sdes				error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
456254939Sdes			else
457254939Sdes				error = EACCES;
458254939Sdes		} else
459254939Sdes			mtx_unlock(&np->n_mtx);
460254939Sdes		return (error);
461254939Sdes	}
462254939Sdes}
463254939Sdes
464254939Sdes
465254939Sdes/*
466254939Sdes * nfs open vnode op
467254939Sdes * Check to see if the type is ok
468254939Sdes * and that deletion is not in progress.
469254939Sdes * For paged in text files, you will need to flush the page cache
470254939Sdes * if consistency is lost.
471254939Sdes */
472254939Sdes/* ARGSUSED */
473254939Sdesstatic int
474254939Sdesnfs_open(struct vop_open_args *ap)
475254939Sdes{
476254939Sdes	struct vnode *vp = ap->a_vp;
477254939Sdes	struct nfsnode *np = VTONFS(vp);
478254939Sdes	struct vattr vattr;
479254939Sdes	int error;
480254939Sdes	int fmode = ap->a_mode;
481254939Sdes
482254939Sdes	if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
483254939Sdes		return (EOPNOTSUPP);
484254939Sdes
485254939Sdes	/*
486254939Sdes	 * For NFSv4, we need to do the Open Op before cache validation,
487254939Sdes	 * so that we conform to RFC3530 Sec. 9.3.1.
488254939Sdes	 */
489254939Sdes	if (NFS_ISV4(vp)) {
490254939Sdes		error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
491254939Sdes		if (error) {
492254939Sdes			error = nfscl_maperr(ap->a_td, error, (uid_t)0,
493254939Sdes			    (gid_t)0);
494254939Sdes			return (error);
495254939Sdes		}
496254939Sdes	}
497254939Sdes
498254939Sdes	/*
499254939Sdes	 * Now, if this Open will be doing reading, re-validate/flush the
500254939Sdes	 * cache, so that Close/Open coherency is maintained.
501254939Sdes	 */
502254939Sdes	if ((fmode & FREAD) && (!NFS_ISV4(vp) || nfscl_mustflush(vp))) {
503254939Sdes		mtx_lock(&np->n_mtx);
504254939Sdes		if (np->n_flag & NMODIFIED) {
505254939Sdes			mtx_unlock(&np->n_mtx);
506254939Sdes			error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
507254939Sdes			if (error == EINTR || error == EIO) {
508254939Sdes				if (NFS_ISV4(vp))
509254939Sdes					(void) nfsrpc_close(vp, 0, ap->a_td);
510254939Sdes				return (error);
511254939Sdes			}
512254939Sdes			np->n_attrstamp = 0;
513254939Sdes			if (vp->v_type == VDIR)
514254939Sdes				np->n_direofoffset = 0;
515254939Sdes			error = VOP_GETATTR(vp, &vattr, ap->a_cred);
516254939Sdes			if (error) {
517254939Sdes				if (NFS_ISV4(vp))
518254939Sdes					(void) nfsrpc_close(vp, 0, ap->a_td);
519254939Sdes				return (error);
520254939Sdes			}
521254939Sdes			mtx_lock(&np->n_mtx);
522254939Sdes			np->n_mtime = vattr.va_mtime;
523254939Sdes			if (NFS_ISV4(vp))
524254939Sdes				np->n_change = vattr.va_filerev;
525254939Sdes			mtx_unlock(&np->n_mtx);
526254939Sdes		} else {
527254939Sdes			struct thread *td = curthread;
528254939Sdes
529254939Sdes			if (np->n_ac_ts_syscalls != td->td_syscalls ||
530254939Sdes			    np->n_ac_ts_tid != td->td_tid ||
531254939Sdes			    td->td_proc == NULL ||
532254939Sdes			    np->n_ac_ts_pid != td->td_proc->p_pid) {
533254939Sdes				np->n_attrstamp = 0;
534254939Sdes			}
535254939Sdes			mtx_unlock(&np->n_mtx);
536254939Sdes			error = VOP_GETATTR(vp, &vattr, ap->a_cred);
537254939Sdes			if (error) {
538254939Sdes				if (NFS_ISV4(vp))
539254939Sdes					(void) nfsrpc_close(vp, 0, ap->a_td);
540254939Sdes				return (error);
541254939Sdes			}
542254939Sdes			mtx_lock(&np->n_mtx);
543254939Sdes			if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
544254939Sdes			    NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
545254939Sdes				if (vp->v_type == VDIR)
546254939Sdes					np->n_direofoffset = 0;
547254939Sdes				mtx_unlock(&np->n_mtx);
548254939Sdes				error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
549254939Sdes				if (error == EINTR || error == EIO) {
550254939Sdes					if (NFS_ISV4(vp))
551254939Sdes						(void) nfsrpc_close(vp, 0,
552254939Sdes						    ap->a_td);
553254939Sdes					return (error);
554254939Sdes				}
555254939Sdes				mtx_lock(&np->n_mtx);
556254939Sdes				np->n_mtime = vattr.va_mtime;
557254939Sdes				if (NFS_ISV4(vp))
558254939Sdes					np->n_change = vattr.va_filerev;
559254939Sdes			}
560254939Sdes			mtx_unlock(&np->n_mtx);
561254939Sdes		}
562254939Sdes	}
563254939Sdes
564254939Sdes	/*
565254939Sdes	 * If the object has >= 1 O_DIRECT active opens, we disable caching.
566254939Sdes	 */
567254939Sdes	if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
568254939Sdes		if (np->n_directio_opens == 0) {
569254939Sdes			error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
570254939Sdes			if (error) {
571254939Sdes				if (NFS_ISV4(vp))
572254939Sdes					(void) nfsrpc_close(vp, 0, ap->a_td);
573254939Sdes				return (error);
574254939Sdes			}
575254939Sdes			mtx_lock(&np->n_mtx);
576254939Sdes			np->n_flag |= NNONCACHE;
577254939Sdes		} else {
578254939Sdes			mtx_lock(&np->n_mtx);
579254939Sdes		}
580254939Sdes		np->n_directio_opens++;
581254939Sdes		mtx_unlock(&np->n_mtx);
582254939Sdes	}
583254939Sdes	vnode_create_vobject(vp, vattr.va_size, ap->a_td);
584254939Sdes	return (0);
585254939Sdes}
586254939Sdes
587254939Sdes/*
588254939Sdes * nfs close vnode op
589254939Sdes * What an NFS client should do upon close after writing is a debatable issue.
590254939Sdes * Most NFS clients push delayed writes to the server upon close, basically for
591254939Sdes * two reasons:
592254939Sdes * 1 - So that any write errors may be reported back to the client process
593254939Sdes *     doing the close system call. By far the two most likely errors are
594254939Sdes *     NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
595254939Sdes * 2 - To put a worst case upper bound on cache inconsistency between
596254939Sdes *     multiple clients for the file.
597254939Sdes * There is also a consistency problem for Version 2 of the protocol w.r.t.
598254939Sdes * not being able to tell if other clients are writing a file concurrently,
599254939Sdes * since there is no way of knowing if the changed modify time in the reply
600254939Sdes * is only due to the write for this client.
601254939Sdes * (NFS Version 3 provides weak cache consistency data in the reply that
602254939Sdes *  should be sufficient to detect and handle this case.)
603254939Sdes *
604254939Sdes * The current code does the following:
605254939Sdes * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
606254939Sdes * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
607254939Sdes *                     or commit them (this satisfies 1 and 2 except for the
608254939Sdes *                     case where the server crashes after this close but
609254939Sdes *                     before the commit RPC, which is felt to be "good
610254939Sdes *                     enough". Changing the last argument to ncl_flush() to
611254939Sdes *                     a 1 would force a commit operation, if it is felt a
612254939Sdes *                     commit is necessary now.
613254939Sdes * for NFS Version 4 - flush the dirty buffers and commit them, if
614254939Sdes *		       nfscl_mustflush() says this is necessary.
615254939Sdes *                     It is necessary if there is no write delegation held,
616254939Sdes *                     in order to satisfy open/close coherency.
617254939Sdes *                     If the file isn't cached on local stable storage,
618254939Sdes *                     it may be necessary in order to detect "out of space"
619254939Sdes *                     errors from the server, if the write delegation
620254939Sdes *                     issued by the server doesn't allow the file to grow.
621254939Sdes */
622254939Sdes/* ARGSUSED */
623254939Sdesstatic int
624254939Sdesnfs_close(struct vop_close_args *ap)
625254939Sdes{
626254939Sdes	struct vnode *vp = ap->a_vp;
627254939Sdes	struct nfsnode *np = VTONFS(vp);
628254939Sdes	struct nfsvattr nfsva;
629254939Sdes	struct ucred *cred;
630254939Sdes	int error = 0, ret, localcred = 0;
631254939Sdes	int fmode = ap->a_fflag;
632254939Sdes
633254939Sdes	if ((vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF))
634254939Sdes		return (0);
635254939Sdes	/*
636254939Sdes	 * During shutdown, a_cred isn't valid, so just use root.
637254939Sdes	 */
638254939Sdes	if (ap->a_cred == NOCRED) {
639254939Sdes		cred = newnfs_getcred();
640254939Sdes		localcred = 1;
641254939Sdes	} else {
642254939Sdes		cred = ap->a_cred;
643254939Sdes	}
644254939Sdes	if (vp->v_type == VREG) {
645254939Sdes	    /*
646254939Sdes	     * Examine and clean dirty pages, regardless of NMODIFIED.
647254939Sdes	     * This closes a major hole in close-to-open consistency.
648254939Sdes	     * We want to push out all dirty pages (and buffers) on
649254939Sdes	     * close, regardless of whether they were dirtied by
650254939Sdes	     * mmap'ed writes or via write().
651254939Sdes	     */
652254939Sdes	    if (nfs_clean_pages_on_close && vp->v_object) {
653254939Sdes		VM_OBJECT_LOCK(vp->v_object);
654254939Sdes		vm_object_page_clean(vp->v_object, 0, 0, 0);
655254939Sdes		VM_OBJECT_UNLOCK(vp->v_object);
656254939Sdes	    }
657254939Sdes	    mtx_lock(&np->n_mtx);
658254939Sdes	    if (np->n_flag & NMODIFIED) {
659254939Sdes		mtx_unlock(&np->n_mtx);
660254939Sdes		if (NFS_ISV3(vp)) {
661254939Sdes		    /*
662254939Sdes		     * Under NFSv3 we have dirty buffers to dispose of.  We
663254939Sdes		     * must flush them to the NFS server.  We have the option
664254939Sdes		     * of waiting all the way through the commit rpc or just
665254939Sdes		     * waiting for the initial write.  The default is to only
666254939Sdes		     * wait through the initial write so the data is in the
667254939Sdes		     * server's cache, which is roughly similar to the state
668254939Sdes		     * a standard disk subsystem leaves the file in on close().
669254939Sdes		     *
670254939Sdes		     * We cannot clear the NMODIFIED bit in np->n_flag due to
671254939Sdes		     * potential races with other processes, and certainly
672254939Sdes		     * cannot clear it if we don't commit.
673254939Sdes		     * These races occur when there is no longer the old
674254939Sdes		     * traditional vnode locking implemented for Vnode Ops.
675254939Sdes		     */
676254939Sdes		    int cm = newnfs_commit_on_close ? 1 : 0;
677254939Sdes		    error = ncl_flush(vp, MNT_WAIT, cred, ap->a_td, cm);
678254939Sdes		    /* np->n_flag &= ~NMODIFIED; */
679254939Sdes		} else if (NFS_ISV4(vp)) {
680254939Sdes			if (nfscl_mustflush(vp)) {
681254939Sdes				int cm = newnfs_commit_on_close ? 1 : 0;
682254939Sdes				error = ncl_flush(vp, MNT_WAIT, cred, ap->a_td,
683254939Sdes				    cm);
684254939Sdes				/*
685254939Sdes				 * as above w.r.t races when clearing
686254939Sdes				 * NMODIFIED.
687254939Sdes				 * np->n_flag &= ~NMODIFIED;
688254939Sdes				 */
689254939Sdes			}
690254939Sdes		} else
691254939Sdes		    error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
692254939Sdes		mtx_lock(&np->n_mtx);
693254939Sdes	    }
694254939Sdes 	    /*
695254939Sdes 	     * Invalidate the attribute cache in all cases.
696254939Sdes 	     * An open is going to fetch fresh attrs any way, other procs
697254939Sdes 	     * on this node that have file open will be forced to do an
698254939Sdes 	     * otw attr fetch, but this is safe.
699254939Sdes	     * --> A user found that their RPC count dropped by 20% when
700254939Sdes	     *     this was commented out and I can't see any requirement
701254939Sdes	     *     for it, so I've disabled it when negative lookups are
702254939Sdes	     *     enabled. (What does this have to do with negative lookup
703254939Sdes	     *     caching? Well nothing, except it was reported by the
704254939Sdes	     *     same user that needed negative lookup caching and I wanted
705254939Sdes	     *     there to be a way to disable it via sysctl to see if it
706254939Sdes	     *     is the cause of some caching/coherency issue that might
707254939Sdes	     *     crop up.)
708254939Sdes 	     */
709254939Sdes	    if (newnfs_neglookup_enable == 0)
710254939Sdes		    np->n_attrstamp = 0;
711254939Sdes	    if (np->n_flag & NWRITEERR) {
712254939Sdes		np->n_flag &= ~NWRITEERR;
713254939Sdes		error = np->n_error;
714254939Sdes	    }
715254939Sdes	    mtx_unlock(&np->n_mtx);
716254939Sdes	}
717254939Sdes
718254939Sdes	if (NFS_ISV4(vp)) {
719254939Sdes		/*
720254939Sdes		 * Get attributes so "change" is up to date.
721254939Sdes		 */
722254939Sdes		if (!error) {
723254939Sdes			ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva,
724254939Sdes			    NULL);
725254939Sdes			if (!ret) {
726254939Sdes				np->n_change = nfsva.na_filerev;
727254939Sdes				(void) nfscl_loadattrcache(&vp, &nfsva, NULL,
728254939Sdes				    NULL, 0, 0);
729254939Sdes			}
730254939Sdes		}
731254939Sdes
732254939Sdes		/*
733254939Sdes		 * and do the close.
734254939Sdes		 */
735254939Sdes		ret = nfsrpc_close(vp, 0, ap->a_td);
736254939Sdes		if (!error && ret)
737254939Sdes			error = ret;
738254939Sdes		if (error)
739254939Sdes			error = nfscl_maperr(ap->a_td, error, (uid_t)0,
740254939Sdes			    (gid_t)0);
741254939Sdes	}
742254939Sdes	if (newnfs_directio_enable)
743254939Sdes		KASSERT((np->n_directio_asyncwr == 0),
744254939Sdes			("nfs_close: dirty unflushed (%d) directio buffers\n",
745254939Sdes			 np->n_directio_asyncwr));
746254939Sdes	if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
747254939Sdes		mtx_lock(&np->n_mtx);
748254939Sdes		KASSERT((np->n_directio_opens > 0),
749254939Sdes			("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
750254939Sdes		np->n_directio_opens--;
751254939Sdes		if (np->n_directio_opens == 0)
752254939Sdes			np->n_flag &= ~NNONCACHE;
753254939Sdes		mtx_unlock(&np->n_mtx);
754254939Sdes	}
755254939Sdes	if (localcred)
756254939Sdes		NFSFREECRED(cred);
757254939Sdes	return (error);
758254939Sdes}
759254939Sdes
760254939Sdes/*
761254939Sdes * nfs getattr call from vfs.
762254939Sdes */
763254939Sdesstatic int
764254939Sdesnfs_getattr(struct vop_getattr_args *ap)
765254939Sdes{
766254939Sdes	struct vnode *vp = ap->a_vp;
767254939Sdes	struct thread *td = curthread;	/* XXX */
768254939Sdes	struct nfsnode *np = VTONFS(vp);
769254939Sdes	int error = 0;
770254939Sdes	struct nfsvattr nfsva;
771254939Sdes	struct vattr *vap = ap->a_vap;
772254939Sdes	struct vattr vattr;
773254939Sdes
774254939Sdes	/*
775254939Sdes	 * Update local times for special files.
776254939Sdes	 */
777254939Sdes	mtx_lock(&np->n_mtx);
778254939Sdes	if (np->n_flag & (NACC | NUPD))
779254939Sdes		np->n_flag |= NCHG;
780254939Sdes	mtx_unlock(&np->n_mtx);
781254939Sdes	/*
782254939Sdes	 * First look in the cache.
783254939Sdes	 */
784254939Sdes	if (ncl_getattrcache(vp, &vattr) == 0) {
785254939Sdes		vap->va_type = vattr.va_type;
786254939Sdes		vap->va_mode = vattr.va_mode;
787254939Sdes		vap->va_nlink = vattr.va_nlink;
788254939Sdes		vap->va_uid = vattr.va_uid;
789254939Sdes		vap->va_gid = vattr.va_gid;
790254939Sdes		vap->va_fsid = vattr.va_fsid;
791254939Sdes		vap->va_fileid = vattr.va_fileid;
792254939Sdes		vap->va_size = vattr.va_size;
793254939Sdes		vap->va_blocksize = vattr.va_blocksize;
794254939Sdes		vap->va_atime = vattr.va_atime;
795254939Sdes		vap->va_mtime = vattr.va_mtime;
796254939Sdes		vap->va_ctime = vattr.va_ctime;
797254939Sdes		vap->va_gen = vattr.va_gen;
798254939Sdes		vap->va_flags = vattr.va_flags;
799254939Sdes		vap->va_rdev = vattr.va_rdev;
800254939Sdes		vap->va_bytes = vattr.va_bytes;
801254939Sdes		vap->va_filerev = vattr.va_filerev;
802254939Sdes		/*
803254939Sdes		 * Get the local modify time for the case of a write
804254939Sdes		 * delegation.
805254939Sdes		 */
806254939Sdes		nfscl_deleggetmodtime(vp, &vap->va_mtime);
807254939Sdes		return (0);
808254939Sdes	}
809254939Sdes
810254939Sdes	if (NFS_ISV34(vp) && nfs_prime_access_cache &&
811254939Sdes	    nfsaccess_cache_timeout > 0) {
812254939Sdes		NFSINCRGLOBAL(newnfsstats.accesscache_misses);
813254939Sdes		nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
814254939Sdes		if (ncl_getattrcache(vp, ap->a_vap) == 0) {
815254939Sdes			nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
816254939Sdes			return (0);
817254939Sdes		}
818254939Sdes	}
819254939Sdes	error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva, NULL);
820254939Sdes	if (!error)
821254939Sdes		error = nfscl_loadattrcache(&vp, &nfsva, vap, NULL, 0, 0);
822254939Sdes	if (!error) {
823254939Sdes		/*
824254939Sdes		 * Get the local modify time for the case of a write
825254939Sdes		 * delegation.
826254939Sdes		 */
827254939Sdes		nfscl_deleggetmodtime(vp, &vap->va_mtime);
828254939Sdes	} else if (NFS_ISV4(vp)) {
829254939Sdes		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
830254939Sdes	}
831254939Sdes	return (error);
832254939Sdes}
833254939Sdes
834254939Sdes/*
835254939Sdes * nfs setattr call.
836254939Sdes */
837254939Sdesstatic int
838254939Sdesnfs_setattr(struct vop_setattr_args *ap)
839254939Sdes{
840254939Sdes	struct vnode *vp = ap->a_vp;
841254939Sdes	struct nfsnode *np = VTONFS(vp);
842254939Sdes	struct thread *td = curthread;	/* XXX */
843254939Sdes	struct vattr *vap = ap->a_vap;
844254939Sdes	int error = 0;
845254939Sdes	u_quad_t tsize;
846254939Sdes
847254939Sdes#ifndef nolint
848254939Sdes	tsize = (u_quad_t)0;
849254939Sdes#endif
850254939Sdes
851254939Sdes	/*
852254939Sdes	 * Setting of flags and marking of atimes are not supported.
853254939Sdes	 */
854254939Sdes	if (vap->va_flags != VNOVAL)
855254939Sdes		return (EOPNOTSUPP);
856254939Sdes
857254939Sdes	/*
858254939Sdes	 * Disallow write attempts if the filesystem is mounted read-only.
859254939Sdes	 */
860254939Sdes  	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
861254939Sdes	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
862254939Sdes	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
863254939Sdes	    (vp->v_mount->mnt_flag & MNT_RDONLY))
864254939Sdes		return (EROFS);
865254939Sdes	if (vap->va_size != VNOVAL) {
866254939Sdes 		switch (vp->v_type) {
867254939Sdes 		case VDIR:
868254939Sdes 			return (EISDIR);
869254939Sdes 		case VCHR:
870254939Sdes 		case VBLK:
871254939Sdes 		case VSOCK:
872254939Sdes 		case VFIFO:
873254939Sdes			if (vap->va_mtime.tv_sec == VNOVAL &&
874254939Sdes			    vap->va_atime.tv_sec == VNOVAL &&
875254939Sdes			    vap->va_mode == (mode_t)VNOVAL &&
876254939Sdes			    vap->va_uid == (uid_t)VNOVAL &&
877254939Sdes			    vap->va_gid == (gid_t)VNOVAL)
878254939Sdes				return (0);
879254939Sdes 			vap->va_size = VNOVAL;
880254939Sdes 			break;
881254939Sdes 		default:
882			/*
883			 * Disallow write attempts if the filesystem is
884			 * mounted read-only.
885			 */
886			if (vp->v_mount->mnt_flag & MNT_RDONLY)
887				return (EROFS);
888			/*
889			 *  We run vnode_pager_setsize() early (why?),
890			 * we must set np->n_size now to avoid vinvalbuf
891			 * V_SAVE races that might setsize a lower
892			 * value.
893			 */
894			mtx_lock(&np->n_mtx);
895			tsize = np->n_size;
896			mtx_unlock(&np->n_mtx);
897			error = ncl_meta_setsize(vp, ap->a_cred, td,
898			    vap->va_size);
899			mtx_lock(&np->n_mtx);
900 			if (np->n_flag & NMODIFIED) {
901			    tsize = np->n_size;
902			    mtx_unlock(&np->n_mtx);
903 			    if (vap->va_size == 0)
904 				error = ncl_vinvalbuf(vp, 0, td, 1);
905 			    else
906 				error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
907 			    if (error) {
908				vnode_pager_setsize(vp, tsize);
909				return (error);
910			    }
911			    /*
912			     * Call nfscl_delegmodtime() to set the modify time
913			     * locally, as required.
914			     */
915			    nfscl_delegmodtime(vp);
916 			} else
917			    mtx_unlock(&np->n_mtx);
918			/*
919			 * np->n_size has already been set to vap->va_size
920			 * in ncl_meta_setsize(). We must set it again since
921			 * nfs_loadattrcache() could be called through
922			 * ncl_meta_setsize() and could modify np->n_size.
923			 */
924			mtx_lock(&np->n_mtx);
925 			np->n_vattr.na_size = np->n_size = vap->va_size;
926			mtx_unlock(&np->n_mtx);
927  		};
928  	} else {
929		mtx_lock(&np->n_mtx);
930		if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
931		    (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
932			mtx_unlock(&np->n_mtx);
933			if ((error = ncl_vinvalbuf(vp, V_SAVE, td, 1)) != 0 &&
934			    (error == EINTR || error == EIO))
935				return (error);
936		} else
937			mtx_unlock(&np->n_mtx);
938	}
939	error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
940	if (error && vap->va_size != VNOVAL) {
941		mtx_lock(&np->n_mtx);
942		np->n_size = np->n_vattr.na_size = tsize;
943		vnode_pager_setsize(vp, tsize);
944		mtx_unlock(&np->n_mtx);
945	}
946	return (error);
947}
948
949/*
950 * Do an nfs setattr rpc.
951 */
952static int
953nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
954    struct thread *td)
955{
956	struct nfsnode *np = VTONFS(vp);
957	int error, ret, attrflag, i;
958	struct nfsvattr nfsva;
959
960	if (NFS_ISV34(vp)) {
961		mtx_lock(&np->n_mtx);
962		for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
963			np->n_accesscache[i].stamp = 0;
964		np->n_flag |= NDELEGMOD;
965		mtx_unlock(&np->n_mtx);
966	}
967	error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag,
968	    NULL);
969	if (attrflag) {
970		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
971		if (ret && !error)
972			error = ret;
973	}
974	if (error && NFS_ISV4(vp))
975		error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
976	return (error);
977}
978
979/*
980 * nfs lookup call, one step at a time...
981 * First look in cache
982 * If not found, unlock the directory nfsnode and do the rpc
983 */
984static int
985nfs_lookup(struct vop_lookup_args *ap)
986{
987	struct componentname *cnp = ap->a_cnp;
988	struct vnode *dvp = ap->a_dvp;
989	struct vnode **vpp = ap->a_vpp;
990	struct mount *mp = dvp->v_mount;
991	int flags = cnp->cn_flags;
992	struct vnode *newvp;
993	struct nfsmount *nmp;
994	struct nfsnode *np;
995	int error = 0, attrflag, dattrflag, ltype;
996	struct thread *td = cnp->cn_thread;
997	struct nfsfh *nfhp;
998	struct nfsvattr dnfsva, nfsva;
999
1000	*vpp = NULLVP;
1001	if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1002	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1003		return (EROFS);
1004	if (dvp->v_type != VDIR)
1005		return (ENOTDIR);
1006	nmp = VFSTONFS(mp);
1007	np = VTONFS(dvp);
1008
1009	/* For NFSv4, wait until any remove is done. */
1010	mtx_lock(&np->n_mtx);
1011	while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1012		np->n_flag |= NREMOVEWANT;
1013		(void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1014	}
1015	mtx_unlock(&np->n_mtx);
1016
1017	if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0)
1018		return (error);
1019	if ((error = cache_lookup(dvp, vpp, cnp)) &&
1020	    (error != ENOENT || newnfs_neglookup_enable != 0)) {
1021		struct vattr vattr;
1022
1023		if (error == ENOENT) {
1024			if (!VOP_GETATTR(dvp, &vattr, cnp->cn_cred) &&
1025			    vattr.va_mtime.tv_sec == np->n_dmtime) {
1026			     NFSINCRGLOBAL(newnfsstats.lookupcache_hits);
1027				return (ENOENT);
1028			}
1029			cache_purge_negative(dvp);
1030			np->n_dmtime = 0;
1031		} else {
1032			newvp = *vpp;
1033			if (nfscl_nodeleg(newvp, 0) == 0 ||
1034			    (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred) &&
1035			     vattr.va_ctime.tv_sec==VTONFS(newvp)->n_ctime)) {
1036			     NFSINCRGLOBAL(newnfsstats.lookupcache_hits);
1037			     if (cnp->cn_nameiop != LOOKUP &&
1038				 (flags & ISLASTCN))
1039				     cnp->cn_flags |= SAVENAME;
1040			     return (0);
1041			}
1042			cache_purge(newvp);
1043			if (dvp != newvp)
1044				vput(newvp);
1045			else
1046				vrele(newvp);
1047			*vpp = NULLVP;
1048		}
1049	}
1050	error = 0;
1051	newvp = NULLVP;
1052	NFSINCRGLOBAL(newnfsstats.lookupcache_misses);
1053	error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1054	    cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1055	    NULL);
1056	if (dattrflag)
1057		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1058	if (error) {
1059		if (newnfs_neglookup_enable != 0 &&
1060		    error == ENOENT && (cnp->cn_flags & MAKEENTRY) &&
1061		    cnp->cn_nameiop != CREATE) {
1062			if (np->n_dmtime == 0)
1063				np->n_dmtime = np->n_vattr.na_mtime.tv_sec;
1064			cache_enter(dvp, NULL, cnp);
1065		}
1066		if (newvp != NULLVP) {
1067			vput(newvp);
1068			*vpp = NULLVP;
1069		}
1070		if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1071		    (flags & ISLASTCN) && error == ENOENT) {
1072			if (mp->mnt_flag & MNT_RDONLY)
1073				error = EROFS;
1074			else
1075				error = EJUSTRETURN;
1076		}
1077		if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1078			cnp->cn_flags |= SAVENAME;
1079		if (NFS_ISV4(dvp))
1080			error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1081		return (error);
1082	}
1083
1084	/*
1085	 * Handle RENAME case...
1086	 */
1087	if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1088		if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1089			FREE((caddr_t)nfhp, M_NFSFH);
1090			return (EISDIR);
1091		}
1092		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL);
1093		if (error)
1094			return (error);
1095		newvp = NFSTOV(np);
1096		if (attrflag)
1097			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1098			    0, 1);
1099		*vpp = newvp;
1100		cnp->cn_flags |= SAVENAME;
1101		return (0);
1102	}
1103
1104	if (flags & ISDOTDOT) {
1105		ltype = VOP_ISLOCKED(dvp);
1106		error = vfs_busy(mp, MBF_NOWAIT);
1107		if (error != 0) {
1108			vfs_ref(mp);
1109			VOP_UNLOCK(dvp, 0);
1110			error = vfs_busy(mp, 0);
1111			vn_lock(dvp, ltype | LK_RETRY);
1112			vfs_rel(mp);
1113			if (error == 0 && (dvp->v_iflag & VI_DOOMED)) {
1114				vfs_unbusy(mp);
1115				error = ENOENT;
1116			}
1117			if (error != 0)
1118				return (error);
1119		}
1120		VOP_UNLOCK(dvp, 0);
1121		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL);
1122		if (error == 0)
1123			newvp = NFSTOV(np);
1124		vfs_unbusy(mp);
1125		if (newvp != dvp)
1126			vn_lock(dvp, ltype | LK_RETRY);
1127		if (dvp->v_iflag & VI_DOOMED) {
1128			if (error == 0) {
1129				if (newvp == dvp)
1130					vrele(newvp);
1131				else
1132					vput(newvp);
1133			}
1134			error = ENOENT;
1135		}
1136		if (error != 0)
1137			return (error);
1138		if (attrflag)
1139			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1140			    0, 1);
1141	} else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1142		FREE((caddr_t)nfhp, M_NFSFH);
1143		VREF(dvp);
1144		newvp = dvp;
1145		if (attrflag)
1146			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1147			    0, 1);
1148	} else {
1149		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL);
1150		if (error)
1151			return (error);
1152		newvp = NFSTOV(np);
1153		if (attrflag)
1154			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1155			    0, 1);
1156	}
1157	if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1158		cnp->cn_flags |= SAVENAME;
1159	if ((cnp->cn_flags & MAKEENTRY) &&
1160	    (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
1161		np->n_ctime = np->n_vattr.na_vattr.va_ctime.tv_sec;
1162		cache_enter(dvp, newvp, cnp);
1163	}
1164	*vpp = newvp;
1165	return (0);
1166}
1167
1168/*
1169 * nfs read call.
1170 * Just call ncl_bioread() to do the work.
1171 */
1172static int
1173nfs_read(struct vop_read_args *ap)
1174{
1175	struct vnode *vp = ap->a_vp;
1176
1177	switch (vp->v_type) {
1178	case VREG:
1179		return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1180	case VDIR:
1181		return (EISDIR);
1182	default:
1183		return (EOPNOTSUPP);
1184	}
1185}
1186
1187/*
1188 * nfs readlink call
1189 */
1190static int
1191nfs_readlink(struct vop_readlink_args *ap)
1192{
1193	struct vnode *vp = ap->a_vp;
1194
1195	if (vp->v_type != VLNK)
1196		return (EINVAL);
1197	return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1198}
1199
1200/*
1201 * Do a readlink rpc.
1202 * Called by ncl_doio() from below the buffer cache.
1203 */
1204int
1205ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1206{
1207	int error, ret, attrflag;
1208	struct nfsvattr nfsva;
1209
1210	error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1211	    &attrflag, NULL);
1212	if (attrflag) {
1213		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1214		if (ret && !error)
1215			error = ret;
1216	}
1217	if (error && NFS_ISV4(vp))
1218		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1219	return (error);
1220}
1221
1222/*
1223 * nfs read rpc call
1224 * Ditto above
1225 */
1226int
1227ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1228{
1229	int error, ret, attrflag;
1230	struct nfsvattr nfsva;
1231
1232	error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva, &attrflag,
1233	    NULL);
1234	if (attrflag) {
1235		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1236		if (ret && !error)
1237			error = ret;
1238	}
1239	if (error && NFS_ISV4(vp))
1240		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1241	return (error);
1242}
1243
1244/*
1245 * nfs write call
1246 */
1247int
1248ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1249	     int *iomode, int *must_commit)
1250{
1251	struct nfsvattr nfsva;
1252	int error = 0, attrflag, ret;
1253	u_char verf[NFSX_VERF];
1254	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1255
1256	*must_commit = 0;
1257	error = nfsrpc_write(vp, uiop, iomode, verf, cred,
1258	    uiop->uio_td, &nfsva, &attrflag, NULL);
1259	NFSLOCKMNT(nmp);
1260	if (!error && NFSHASWRITEVERF(nmp) &&
1261	    NFSBCMP(verf, nmp->nm_verf, NFSX_VERF)) {
1262		*must_commit = 1;
1263		NFSBCOPY(verf, nmp->nm_verf, NFSX_VERF);
1264	}
1265	NFSUNLOCKMNT(nmp);
1266	if (attrflag) {
1267		if (VTONFS(vp)->n_flag & ND_NFSV4)
1268			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
1269			    1);
1270		else
1271			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
1272			    1);
1273		if (ret && !error)
1274			error = ret;
1275	}
1276	if (vp->v_mount->mnt_kern_flag & MNTK_ASYNC)
1277		*iomode = NFSWRITE_FILESYNC;
1278	if (error && NFS_ISV4(vp))
1279		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1280	return (error);
1281}
1282
1283/*
1284 * nfs mknod rpc
1285 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1286 * mode set to specify the file type and the size field for rdev.
1287 */
1288static int
1289nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1290    struct vattr *vap)
1291{
1292	struct nfsvattr nfsva, dnfsva;
1293	struct vnode *newvp = NULL;
1294	struct nfsnode *np = NULL, *dnp;
1295	struct nfsfh *nfhp;
1296	struct vattr vattr;
1297	int error = 0, attrflag, dattrflag;
1298	u_int32_t rdev;
1299
1300	if (vap->va_type == VCHR || vap->va_type == VBLK)
1301		rdev = vap->va_rdev;
1302	else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1303		rdev = 0xffffffff;
1304	else
1305		return (EOPNOTSUPP);
1306	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1307		return (error);
1308	error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1309	    rdev, vap->va_type, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1310	    &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1311	if (!error) {
1312		if (!nfhp)
1313			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1314			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1315			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1316			    NULL);
1317		if (nfhp)
1318			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1319			    cnp->cn_thread, &np, NULL);
1320	}
1321	if (dattrflag)
1322		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1323	if (!error) {
1324		newvp = NFSTOV(np);
1325		if (attrflag)
1326			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1327			    0, 1);
1328	}
1329	if (!error) {
1330		if ((cnp->cn_flags & MAKEENTRY))
1331			cache_enter(dvp, newvp, cnp);
1332		*vpp = newvp;
1333	} else if (NFS_ISV4(dvp)) {
1334		error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1335		    vap->va_gid);
1336	}
1337	dnp = VTONFS(dvp);
1338	mtx_lock(&dnp->n_mtx);
1339	dnp->n_flag |= NMODIFIED;
1340	if (!dattrflag)
1341		dnp->n_attrstamp = 0;
1342	mtx_unlock(&dnp->n_mtx);
1343	return (error);
1344}
1345
1346/*
1347 * nfs mknod vop
1348 * just call nfs_mknodrpc() to do the work.
1349 */
1350/* ARGSUSED */
1351static int
1352nfs_mknod(struct vop_mknod_args *ap)
1353{
1354	return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1355}
1356
1357static struct mtx nfs_cverf_mtx;
1358MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1359    MTX_DEF);
1360
1361static nfsquad_t
1362nfs_get_cverf(void)
1363{
1364	static nfsquad_t cverf;
1365	nfsquad_t ret;
1366	static int cverf_initialized = 0;
1367
1368	mtx_lock(&nfs_cverf_mtx);
1369	if (cverf_initialized == 0) {
1370		cverf.lval[0] = arc4random();
1371		cverf.lval[1] = arc4random();
1372		cverf_initialized = 1;
1373	} else
1374		cverf.qval++;
1375	ret = cverf;
1376	mtx_unlock(&nfs_cverf_mtx);
1377
1378	return (ret);
1379}
1380
1381/*
1382 * nfs file create call
1383 */
1384static int
1385nfs_create(struct vop_create_args *ap)
1386{
1387	struct vnode *dvp = ap->a_dvp;
1388	struct vattr *vap = ap->a_vap;
1389	struct componentname *cnp = ap->a_cnp;
1390	struct nfsnode *np = NULL, *dnp;
1391	struct vnode *newvp = NULL;
1392	struct nfsmount *nmp;
1393	struct nfsvattr dnfsva, nfsva;
1394	struct nfsfh *nfhp;
1395	nfsquad_t cverf;
1396	int error = 0, attrflag, dattrflag, fmode = 0;
1397	struct vattr vattr;
1398
1399	/*
1400	 * Oops, not for me..
1401	 */
1402	if (vap->va_type == VSOCK)
1403		return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1404
1405	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1406		return (error);
1407	if (vap->va_vaflags & VA_EXCLUSIVE)
1408		fmode |= O_EXCL;
1409	dnp = VTONFS(dvp);
1410	nmp = VFSTONFS(vnode_mount(dvp));
1411again:
1412	/* For NFSv4, wait until any remove is done. */
1413	mtx_lock(&dnp->n_mtx);
1414	while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1415		dnp->n_flag |= NREMOVEWANT;
1416		(void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1417	}
1418	mtx_unlock(&dnp->n_mtx);
1419
1420	cverf = nfs_get_cverf();
1421	error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1422	    vap, cverf, fmode, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva,
1423	    &nfhp, &attrflag, &dattrflag, NULL);
1424	if (!error) {
1425		if (nfhp == NULL)
1426			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1427			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1428			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1429			    NULL);
1430		if (nfhp != NULL)
1431			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1432			    cnp->cn_thread, &np, NULL);
1433	}
1434	if (dattrflag)
1435		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1436	if (!error) {
1437		newvp = NFSTOV(np);
1438		if (attrflag)
1439			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1440			    0, 1);
1441	}
1442	if (error) {
1443		if (newvp != NULL) {
1444			vrele(newvp);
1445			newvp = NULL;
1446		}
1447		if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1448		    error == NFSERR_NOTSUPP) {
1449			fmode &= ~O_EXCL;
1450			goto again;
1451		}
1452	} else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1453		if (nfscl_checksattr(vap, &nfsva)) {
1454			error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1455			    cnp->cn_thread, &nfsva, &attrflag, NULL);
1456			if (error && (vap->va_uid != (uid_t)VNOVAL ||
1457			    vap->va_gid != (gid_t)VNOVAL)) {
1458				/* try again without setting uid/gid */
1459				vap->va_uid = (uid_t)VNOVAL;
1460				vap->va_gid = (uid_t)VNOVAL;
1461				error = nfsrpc_setattr(newvp, vap, NULL,
1462				    cnp->cn_cred, cnp->cn_thread, &nfsva,
1463				    &attrflag, NULL);
1464			}
1465			if (attrflag)
1466				(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1467				    NULL, 0, 1);
1468		}
1469	}
1470	if (!error) {
1471		if (cnp->cn_flags & MAKEENTRY)
1472			cache_enter(dvp, newvp, cnp);
1473		*ap->a_vpp = newvp;
1474	} else if (NFS_ISV4(dvp)) {
1475		error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1476		    vap->va_gid);
1477	}
1478	mtx_lock(&dnp->n_mtx);
1479	dnp->n_flag |= NMODIFIED;
1480	if (!dattrflag)
1481		dnp->n_attrstamp = 0;
1482	mtx_unlock(&dnp->n_mtx);
1483	return (error);
1484}
1485
1486/*
1487 * nfs file remove call
1488 * To try and make nfs semantics closer to ufs semantics, a file that has
1489 * other processes using the vnode is renamed instead of removed and then
1490 * removed later on the last close.
1491 * - If v_usecount > 1
1492 *	  If a rename is not already in the works
1493 *	     call nfs_sillyrename() to set it up
1494 *     else
1495 *	  do the remove rpc
1496 */
1497static int
1498nfs_remove(struct vop_remove_args *ap)
1499{
1500	struct vnode *vp = ap->a_vp;
1501	struct vnode *dvp = ap->a_dvp;
1502	struct componentname *cnp = ap->a_cnp;
1503	struct nfsnode *np = VTONFS(vp);
1504	int error = 0;
1505	struct vattr vattr;
1506
1507#ifndef DIAGNOSTIC
1508	if ((cnp->cn_flags & HASBUF) == 0)
1509		panic("nfs_remove: no name");
1510	if (vrefcnt(vp) < 1)
1511		panic("nfs_remove: bad v_usecount");
1512#endif
1513	if (vp->v_type == VDIR)
1514		error = EPERM;
1515	else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1516	    VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1517	    vattr.va_nlink > 1)) {
1518		/*
1519		 * Purge the name cache so that the chance of a lookup for
1520		 * the name succeeding while the remove is in progress is
1521		 * minimized. Without node locking it can still happen, such
1522		 * that an I/O op returns ESTALE, but since you get this if
1523		 * another host removes the file..
1524		 */
1525		cache_purge(vp);
1526		/*
1527		 * throw away biocache buffers, mainly to avoid
1528		 * unnecessary delayed writes later.
1529		 */
1530		error = ncl_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1531		/* Do the rpc */
1532		if (error != EINTR && error != EIO)
1533			error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1534			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1535		/*
1536		 * Kludge City: If the first reply to the remove rpc is lost..
1537		 *   the reply to the retransmitted request will be ENOENT
1538		 *   since the file was in fact removed
1539		 *   Therefore, we cheat and return success.
1540		 */
1541		if (error == ENOENT)
1542			error = 0;
1543	} else if (!np->n_sillyrename)
1544		error = nfs_sillyrename(dvp, vp, cnp);
1545	np->n_attrstamp = 0;
1546	return (error);
1547}
1548
1549/*
1550 * nfs file remove rpc called from nfs_inactive
1551 */
1552int
1553ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1554{
1555	/*
1556	 * Make sure that the directory vnode is still valid.
1557	 * XXX we should lock sp->s_dvp here.
1558	 */
1559	if (sp->s_dvp->v_type == VBAD)
1560		return (0);
1561	return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1562	    sp->s_cred, NULL));
1563}
1564
1565/*
1566 * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1567 */
1568static int
1569nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1570    int namelen, struct ucred *cred, struct thread *td)
1571{
1572	struct nfsvattr dnfsva;
1573	struct nfsnode *dnp = VTONFS(dvp);
1574	int error = 0, dattrflag;
1575
1576	mtx_lock(&dnp->n_mtx);
1577	dnp->n_flag |= NREMOVEINPROG;
1578	mtx_unlock(&dnp->n_mtx);
1579	error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1580	    &dattrflag, NULL);
1581	mtx_lock(&dnp->n_mtx);
1582	if ((dnp->n_flag & NREMOVEWANT)) {
1583		dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1584		mtx_unlock(&dnp->n_mtx);
1585		wakeup((caddr_t)dnp);
1586	} else {
1587		dnp->n_flag &= ~NREMOVEINPROG;
1588		mtx_unlock(&dnp->n_mtx);
1589	}
1590	if (dattrflag)
1591		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1592	mtx_lock(&dnp->n_mtx);
1593	dnp->n_flag |= NMODIFIED;
1594	if (!dattrflag)
1595		dnp->n_attrstamp = 0;
1596	mtx_unlock(&dnp->n_mtx);
1597	if (error && NFS_ISV4(dvp))
1598		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1599	return (error);
1600}
1601
1602/*
1603 * nfs file rename call
1604 */
1605static int
1606nfs_rename(struct vop_rename_args *ap)
1607{
1608	struct vnode *fvp = ap->a_fvp;
1609	struct vnode *tvp = ap->a_tvp;
1610	struct vnode *fdvp = ap->a_fdvp;
1611	struct vnode *tdvp = ap->a_tdvp;
1612	struct componentname *tcnp = ap->a_tcnp;
1613	struct componentname *fcnp = ap->a_fcnp;
1614	struct nfsnode *fnp = VTONFS(ap->a_fvp);
1615	struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1616	struct nfsv4node *newv4 = NULL;
1617	int error;
1618
1619#ifndef DIAGNOSTIC
1620	if ((tcnp->cn_flags & HASBUF) == 0 ||
1621	    (fcnp->cn_flags & HASBUF) == 0)
1622		panic("nfs_rename: no name");
1623#endif
1624	/* Check for cross-device rename */
1625	if ((fvp->v_mount != tdvp->v_mount) ||
1626	    (tvp && (fvp->v_mount != tvp->v_mount))) {
1627		error = EXDEV;
1628		goto out;
1629	}
1630
1631	if (fvp == tvp) {
1632		ncl_printf("nfs_rename: fvp == tvp (can't happen)\n");
1633		error = 0;
1634		goto out;
1635	}
1636	if ((error = vn_lock(fvp, LK_EXCLUSIVE)))
1637		goto out;
1638
1639	/*
1640	 * We have to flush B_DELWRI data prior to renaming
1641	 * the file.  If we don't, the delayed-write buffers
1642	 * can be flushed out later after the file has gone stale
1643	 * under NFSV3.  NFSV2 does not have this problem because
1644	 * ( as far as I can tell ) it flushes dirty buffers more
1645	 * often.
1646	 *
1647	 * Skip the rename operation if the fsync fails, this can happen
1648	 * due to the server's volume being full, when we pushed out data
1649	 * that was written back to our cache earlier. Not checking for
1650	 * this condition can result in potential (silent) data loss.
1651	 */
1652	error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1653	VOP_UNLOCK(fvp, 0);
1654	if (!error && tvp)
1655		error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1656	if (error)
1657		goto out;
1658
1659	/*
1660	 * If the tvp exists and is in use, sillyrename it before doing the
1661	 * rename of the new file over it.
1662	 * XXX Can't sillyrename a directory.
1663	 */
1664	if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1665		tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1666		vput(tvp);
1667		tvp = NULL;
1668	}
1669
1670	error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1671	    tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1672	    tcnp->cn_thread);
1673
1674	if (!error) {
1675		/*
1676		 * For NFSv4, check to see if it is the same name and
1677		 * replace the name, if it is different.
1678		 */
1679		MALLOC(newv4, struct nfsv4node *,
1680		    sizeof (struct nfsv4node) +
1681		    tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
1682		    M_NFSV4NODE, M_WAITOK);
1683		mtx_lock(&tdnp->n_mtx);
1684		mtx_lock(&fnp->n_mtx);
1685		if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
1686		    (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
1687		      NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
1688		      tcnp->cn_namelen) ||
1689		      tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
1690		      NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1691			tdnp->n_fhp->nfh_len))) {
1692#ifdef notdef
1693{ char nnn[100]; int nnnl;
1694nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
1695bcopy(tcnp->cn_nameptr, nnn, nnnl);
1696nnn[nnnl] = '\0';
1697printf("ren replace=%s\n",nnn);
1698}
1699#endif
1700			FREE((caddr_t)fnp->n_v4, M_NFSV4NODE);
1701			fnp->n_v4 = newv4;
1702			newv4 = NULL;
1703			fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
1704			fnp->n_v4->n4_namelen = tcnp->cn_namelen;
1705			NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1706			    tdnp->n_fhp->nfh_len);
1707			NFSBCOPY(tcnp->cn_nameptr,
1708			    NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
1709		}
1710		mtx_unlock(&tdnp->n_mtx);
1711		mtx_unlock(&fnp->n_mtx);
1712		if (newv4 != NULL)
1713			FREE((caddr_t)newv4, M_NFSV4NODE);
1714	}
1715
1716	if (fvp->v_type == VDIR) {
1717		if (tvp != NULL && tvp->v_type == VDIR)
1718			cache_purge(tdvp);
1719		cache_purge(fdvp);
1720	}
1721
1722out:
1723	if (tdvp == tvp)
1724		vrele(tdvp);
1725	else
1726		vput(tdvp);
1727	if (tvp)
1728		vput(tvp);
1729	vrele(fdvp);
1730	vrele(fvp);
1731	/*
1732	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1733	 */
1734	if (error == ENOENT)
1735		error = 0;
1736	return (error);
1737}
1738
1739/*
1740 * nfs file rename rpc called from nfs_remove() above
1741 */
1742static int
1743nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
1744    struct sillyrename *sp)
1745{
1746
1747	return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
1748	    sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
1749	    scnp->cn_thread));
1750}
1751
1752/*
1753 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1754 */
1755static int
1756nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
1757    int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
1758    int tnamelen, struct ucred *cred, struct thread *td)
1759{
1760	struct nfsvattr fnfsva, tnfsva;
1761	struct nfsnode *fdnp = VTONFS(fdvp);
1762	struct nfsnode *tdnp = VTONFS(tdvp);
1763	int error = 0, fattrflag, tattrflag;
1764
1765	error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
1766	    tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
1767	    &tattrflag, NULL, NULL);
1768	mtx_lock(&fdnp->n_mtx);
1769	fdnp->n_flag |= NMODIFIED;
1770	mtx_unlock(&fdnp->n_mtx);
1771	mtx_lock(&tdnp->n_mtx);
1772	tdnp->n_flag |= NMODIFIED;
1773	mtx_unlock(&tdnp->n_mtx);
1774	if (fattrflag)
1775		(void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
1776	else
1777		fdnp->n_attrstamp = 0;
1778	if (tattrflag)
1779		(void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
1780	else
1781		tdnp->n_attrstamp = 0;
1782	if (error && NFS_ISV4(fdvp))
1783		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1784	return (error);
1785}
1786
1787/*
1788 * nfs hard link create call
1789 */
1790static int
1791nfs_link(struct vop_link_args *ap)
1792{
1793	struct vnode *vp = ap->a_vp;
1794	struct vnode *tdvp = ap->a_tdvp;
1795	struct componentname *cnp = ap->a_cnp;
1796	struct nfsnode *tdnp;
1797	struct nfsvattr nfsva, dnfsva;
1798	int error = 0, attrflag, dattrflag;
1799
1800	if (vp->v_mount != tdvp->v_mount) {
1801		return (EXDEV);
1802	}
1803
1804	/*
1805	 * Push all writes to the server, so that the attribute cache
1806	 * doesn't get "out of sync" with the server.
1807	 * XXX There should be a better way!
1808	 */
1809	VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1810
1811	error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
1812	    cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &attrflag,
1813	    &dattrflag, NULL);
1814	tdnp = VTONFS(tdvp);
1815	mtx_lock(&tdnp->n_mtx);
1816	tdnp->n_flag |= NMODIFIED;
1817	mtx_unlock(&tdnp->n_mtx);
1818	if (attrflag)
1819		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1820	else
1821		VTONFS(vp)->n_attrstamp = 0;
1822	if (dattrflag)
1823		(void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
1824	else
1825		tdnp->n_attrstamp = 0;
1826	/*
1827	 * If negative lookup caching is enabled, I might as well
1828	 * add an entry for this node. Not necessary for correctness,
1829	 * but if negative caching is enabled, then the system
1830	 * must care about lookup caching hit rate, so...
1831	 */
1832	if (newnfs_neglookup_enable != 0 &&
1833	    (cnp->cn_flags & MAKEENTRY))
1834		cache_enter(tdvp, vp, cnp);
1835	if (error && NFS_ISV4(vp))
1836		error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
1837		    (gid_t)0);
1838	return (error);
1839}
1840
1841/*
1842 * nfs symbolic link create call
1843 */
1844static int
1845nfs_symlink(struct vop_symlink_args *ap)
1846{
1847	struct vnode *dvp = ap->a_dvp;
1848	struct vattr *vap = ap->a_vap;
1849	struct componentname *cnp = ap->a_cnp;
1850	struct nfsvattr nfsva, dnfsva;
1851	struct nfsfh *nfhp;
1852	struct nfsnode *np = NULL, *dnp;
1853	struct vnode *newvp = NULL;
1854	int error = 0, attrflag, dattrflag, ret;
1855
1856	vap->va_type = VLNK;
1857	error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1858	    ap->a_target, vap, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1859	    &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1860	if (nfhp) {
1861		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
1862		    &np, NULL);
1863		if (!ret)
1864			newvp = NFSTOV(np);
1865		else if (!error)
1866			error = ret;
1867	}
1868	if (newvp != NULL) {
1869		if (attrflag)
1870			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1871			    0, 1);
1872	} else if (!error) {
1873		/*
1874		 * If we do not have an error and we could not extract the
1875		 * newvp from the response due to the request being NFSv2, we
1876		 * have to do a lookup in order to obtain a newvp to return.
1877		 */
1878		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1879		    cnp->cn_cred, cnp->cn_thread, &np);
1880		if (!error)
1881			newvp = NFSTOV(np);
1882	}
1883	if (error) {
1884		if (newvp)
1885			vput(newvp);
1886		if (NFS_ISV4(dvp))
1887			error = nfscl_maperr(cnp->cn_thread, error,
1888			    vap->va_uid, vap->va_gid);
1889	} else {
1890		/*
1891		 * If negative lookup caching is enabled, I might as well
1892		 * add an entry for this node. Not necessary for correctness,
1893		 * but if negative caching is enabled, then the system
1894		 * must care about lookup caching hit rate, so...
1895		 */
1896		if (newnfs_neglookup_enable != 0 &&
1897		    (cnp->cn_flags & MAKEENTRY))
1898			cache_enter(dvp, newvp, cnp);
1899		*ap->a_vpp = newvp;
1900	}
1901
1902	dnp = VTONFS(dvp);
1903	mtx_lock(&dnp->n_mtx);
1904	dnp->n_flag |= NMODIFIED;
1905	mtx_unlock(&dnp->n_mtx);
1906	if (dattrflag)
1907		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1908	else
1909		dnp->n_attrstamp = 0;
1910	return (error);
1911}
1912
1913/*
1914 * nfs make dir call
1915 */
1916static int
1917nfs_mkdir(struct vop_mkdir_args *ap)
1918{
1919	struct vnode *dvp = ap->a_dvp;
1920	struct vattr *vap = ap->a_vap;
1921	struct componentname *cnp = ap->a_cnp;
1922	struct nfsnode *np = NULL, *dnp;
1923	struct vnode *newvp = NULL;
1924	struct vattr vattr;
1925	struct nfsfh *nfhp;
1926	struct nfsvattr nfsva, dnfsva;
1927	int error = 0, attrflag, dattrflag, ret;
1928
1929	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1930		return (error);
1931	vap->va_type = VDIR;
1932	error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1933	    vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &nfhp,
1934	    &attrflag, &dattrflag, NULL);
1935	dnp = VTONFS(dvp);
1936	mtx_lock(&dnp->n_mtx);
1937	dnp->n_flag |= NMODIFIED;
1938	mtx_unlock(&dnp->n_mtx);
1939	if (dattrflag)
1940		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1941	else
1942		dnp->n_attrstamp = 0;
1943	if (nfhp) {
1944		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
1945		    &np, NULL);
1946		if (!ret) {
1947			newvp = NFSTOV(np);
1948			if (attrflag)
1949			   (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1950				NULL, 0, 1);
1951		} else if (!error)
1952			error = ret;
1953	}
1954	if (!error && newvp == NULL) {
1955		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1956		    cnp->cn_cred, cnp->cn_thread, &np);
1957		if (!error) {
1958			newvp = NFSTOV(np);
1959			if (newvp->v_type != VDIR)
1960				error = EEXIST;
1961		}
1962	}
1963	if (error) {
1964		if (newvp)
1965			vput(newvp);
1966		if (NFS_ISV4(dvp))
1967			error = nfscl_maperr(cnp->cn_thread, error,
1968			    vap->va_uid, vap->va_gid);
1969	} else {
1970		/*
1971		 * If negative lookup caching is enabled, I might as well
1972		 * add an entry for this node. Not necessary for correctness,
1973		 * but if negative caching is enabled, then the system
1974		 * must care about lookup caching hit rate, so...
1975		 */
1976		if (newnfs_neglookup_enable != 0 &&
1977		    (cnp->cn_flags & MAKEENTRY))
1978			cache_enter(dvp, newvp, cnp);
1979		*ap->a_vpp = newvp;
1980	}
1981	return (error);
1982}
1983
1984/*
1985 * nfs remove directory call
1986 */
1987static int
1988nfs_rmdir(struct vop_rmdir_args *ap)
1989{
1990	struct vnode *vp = ap->a_vp;
1991	struct vnode *dvp = ap->a_dvp;
1992	struct componentname *cnp = ap->a_cnp;
1993	struct nfsnode *dnp;
1994	struct nfsvattr dnfsva;
1995	int error, dattrflag;
1996
1997	if (dvp == vp)
1998		return (EINVAL);
1999	error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2000	    cnp->cn_cred, cnp->cn_thread, &dnfsva, &dattrflag, NULL);
2001	dnp = VTONFS(dvp);
2002	mtx_lock(&dnp->n_mtx);
2003	dnp->n_flag |= NMODIFIED;
2004	mtx_unlock(&dnp->n_mtx);
2005	if (dattrflag)
2006		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2007	else
2008		dnp->n_attrstamp = 0;
2009
2010	cache_purge(dvp);
2011	cache_purge(vp);
2012	if (error && NFS_ISV4(dvp))
2013		error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2014		    (gid_t)0);
2015	/*
2016	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2017	 */
2018	if (error == ENOENT)
2019		error = 0;
2020	return (error);
2021}
2022
2023/*
2024 * nfs readdir call
2025 */
2026static int
2027nfs_readdir(struct vop_readdir_args *ap)
2028{
2029	struct vnode *vp = ap->a_vp;
2030	struct nfsnode *np = VTONFS(vp);
2031	struct uio *uio = ap->a_uio;
2032	int tresid, error = 0;
2033	struct vattr vattr;
2034
2035	if (vp->v_type != VDIR)
2036		return(EPERM);
2037
2038	/*
2039	 * First, check for hit on the EOF offset cache
2040	 */
2041	if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2042	    (np->n_flag & NMODIFIED) == 0) {
2043		if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2044			mtx_lock(&np->n_mtx);
2045			if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2046			    !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2047				mtx_unlock(&np->n_mtx);
2048				NFSINCRGLOBAL(newnfsstats.direofcache_hits);
2049				return (0);
2050			} else
2051				mtx_unlock(&np->n_mtx);
2052		}
2053	}
2054
2055	/*
2056	 * Call ncl_bioread() to do the real work.
2057	 */
2058	tresid = uio->uio_resid;
2059	error = ncl_bioread(vp, uio, 0, ap->a_cred);
2060
2061	if (!error && uio->uio_resid == tresid)
2062		NFSINCRGLOBAL(newnfsstats.direofcache_misses);
2063	return (error);
2064}
2065
2066/*
2067 * Readdir rpc call.
2068 * Called from below the buffer cache by ncl_doio().
2069 */
2070int
2071ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2072    struct thread *td)
2073{
2074	struct nfsvattr nfsva;
2075	nfsuint64 *cookiep, cookie;
2076	struct nfsnode *dnp = VTONFS(vp);
2077	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2078	int error = 0, eof, attrflag;
2079
2080#ifndef DIAGNOSTIC
2081	if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2082		(uiop->uio_resid & (DIRBLKSIZ - 1)))
2083		panic("nfs readdirrpc bad uio");
2084#endif
2085
2086	/*
2087	 * If there is no cookie, assume directory was stale.
2088	 */
2089	ncl_dircookie_lock(dnp);
2090	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2091	if (cookiep) {
2092		cookie = *cookiep;
2093		ncl_dircookie_unlock(dnp);
2094	} else {
2095		ncl_dircookie_unlock(dnp);
2096		return (NFSERR_BAD_COOKIE);
2097	}
2098
2099	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2100		(void)ncl_fsinfo(nmp, vp, cred, td);
2101
2102	error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2103	    &attrflag, &eof, NULL);
2104	if (attrflag)
2105		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2106
2107	if (!error) {
2108		/*
2109		 * We are now either at the end of the directory or have filled
2110		 * the block.
2111		 */
2112		if (eof)
2113			dnp->n_direofoffset = uiop->uio_offset;
2114		else {
2115			if (uiop->uio_resid > 0)
2116				ncl_printf("EEK! readdirrpc resid > 0\n");
2117			ncl_dircookie_lock(dnp);
2118			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2119			*cookiep = cookie;
2120			ncl_dircookie_unlock(dnp);
2121		}
2122	} else if (NFS_ISV4(vp)) {
2123		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2124	}
2125	return (error);
2126}
2127
2128/*
2129 * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2130 */
2131int
2132ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2133    struct thread *td)
2134{
2135	struct nfsvattr nfsva;
2136	nfsuint64 *cookiep, cookie;
2137	struct nfsnode *dnp = VTONFS(vp);
2138	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2139	int error = 0, attrflag, eof;
2140
2141#ifndef DIAGNOSTIC
2142	if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2143		(uiop->uio_resid & (DIRBLKSIZ - 1)))
2144		panic("nfs readdirplusrpc bad uio");
2145#endif
2146
2147	/*
2148	 * If there is no cookie, assume directory was stale.
2149	 */
2150	ncl_dircookie_lock(dnp);
2151	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2152	if (cookiep) {
2153		cookie = *cookiep;
2154		ncl_dircookie_unlock(dnp);
2155	} else {
2156		ncl_dircookie_unlock(dnp);
2157		return (NFSERR_BAD_COOKIE);
2158	}
2159
2160	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2161		(void)ncl_fsinfo(nmp, vp, cred, td);
2162	error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2163	    &attrflag, &eof, NULL);
2164	if (attrflag)
2165		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2166
2167	if (!error) {
2168		/*
2169		 * We are now either at end of the directory or have filled the
2170		 * the block.
2171		 */
2172		if (eof)
2173			dnp->n_direofoffset = uiop->uio_offset;
2174		else {
2175			if (uiop->uio_resid > 0)
2176				ncl_printf("EEK! readdirplusrpc resid > 0\n");
2177			ncl_dircookie_lock(dnp);
2178			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2179			*cookiep = cookie;
2180			ncl_dircookie_unlock(dnp);
2181		}
2182	} else if (NFS_ISV4(vp)) {
2183		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2184	}
2185	return (error);
2186}
2187
2188/*
2189 * Silly rename. To make the NFS filesystem that is stateless look a little
2190 * more like the "ufs" a remove of an active vnode is translated to a rename
2191 * to a funny looking filename that is removed by nfs_inactive on the
2192 * nfsnode. There is the potential for another process on a different client
2193 * to create the same funny name between the nfs_lookitup() fails and the
2194 * nfs_rename() completes, but...
2195 */
2196static int
2197nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2198{
2199	struct sillyrename *sp;
2200	struct nfsnode *np;
2201	int error;
2202	short pid;
2203	unsigned int lticks;
2204
2205	cache_purge(dvp);
2206	np = VTONFS(vp);
2207#ifndef DIAGNOSTIC
2208	if (vp->v_type == VDIR)
2209		panic("nfs: sillyrename dir");
2210#endif
2211	MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2212	    M_NEWNFSREQ, M_WAITOK);
2213	sp->s_cred = crhold(cnp->cn_cred);
2214	sp->s_dvp = dvp;
2215	VREF(dvp);
2216
2217	/*
2218	 * Fudge together a funny name.
2219	 * Changing the format of the funny name to accomodate more
2220	 * sillynames per directory.
2221	 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2222	 * CPU ticks since boot.
2223	 */
2224	pid = cnp->cn_thread->td_proc->p_pid;
2225	lticks = (unsigned int)ticks;
2226	for ( ; ; ) {
2227		sp->s_namlen = sprintf(sp->s_name,
2228				       ".nfs.%08x.%04x4.4", lticks,
2229				       pid);
2230		if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2231				 cnp->cn_thread, NULL))
2232			break;
2233		lticks++;
2234	}
2235	error = nfs_renameit(dvp, vp, cnp, sp);
2236	if (error)
2237		goto bad;
2238	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2239		cnp->cn_thread, &np);
2240	np->n_sillyrename = sp;
2241	return (0);
2242bad:
2243	vrele(sp->s_dvp);
2244	crfree(sp->s_cred);
2245	free((caddr_t)sp, M_NEWNFSREQ);
2246	return (error);
2247}
2248
2249/*
2250 * Look up a file name and optionally either update the file handle or
2251 * allocate an nfsnode, depending on the value of npp.
2252 * npp == NULL	--> just do the lookup
2253 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2254 *			handled too
2255 * *npp != NULL --> update the file handle in the vnode
2256 */
2257static int
2258nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2259    struct thread *td, struct nfsnode **npp)
2260{
2261	struct vnode *newvp = NULL, *vp;
2262	struct nfsnode *np, *dnp = VTONFS(dvp);
2263	struct nfsfh *nfhp, *onfhp;
2264	struct nfsvattr nfsva, dnfsva;
2265	struct componentname cn;
2266	int error = 0, attrflag, dattrflag;
2267	u_int hash;
2268
2269	error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2270	    &nfhp, &attrflag, &dattrflag, NULL);
2271	if (dattrflag)
2272		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2273	if (npp && !error) {
2274		if (*npp != NULL) {
2275		    np = *npp;
2276		    vp = NFSTOV(np);
2277		    /*
2278		     * For NFSv4, check to see if it is the same name and
2279		     * replace the name, if it is different.
2280		     */
2281		    if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2282			(np->n_v4->n4_namelen != len ||
2283			 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2284			 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2285			 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2286			 dnp->n_fhp->nfh_len))) {
2287#ifdef notdef
2288{ char nnn[100]; int nnnl;
2289nnnl = (len < 100) ? len : 99;
2290bcopy(name, nnn, nnnl);
2291nnn[nnnl] = '\0';
2292printf("replace=%s\n",nnn);
2293}
2294#endif
2295			    FREE((caddr_t)np->n_v4, M_NFSV4NODE);
2296			    MALLOC(np->n_v4, struct nfsv4node *,
2297				sizeof (struct nfsv4node) +
2298				dnp->n_fhp->nfh_len + len - 1,
2299				M_NFSV4NODE, M_WAITOK);
2300			    np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2301			    np->n_v4->n4_namelen = len;
2302			    NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2303				dnp->n_fhp->nfh_len);
2304			    NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2305		    }
2306		    hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2307			FNV1_32_INIT);
2308		    onfhp = np->n_fhp;
2309		    /*
2310		     * Rehash node for new file handle.
2311		     */
2312		    vfs_hash_rehash(vp, hash);
2313		    np->n_fhp = nfhp;
2314		    if (onfhp != NULL)
2315			FREE((caddr_t)onfhp, M_NFSFH);
2316		    newvp = NFSTOV(np);
2317		} else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2318		    FREE((caddr_t)nfhp, M_NFSFH);
2319		    VREF(dvp);
2320		    newvp = dvp;
2321		} else {
2322		    cn.cn_nameptr = name;
2323		    cn.cn_namelen = len;
2324		    error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2325			&np, NULL);
2326		    if (error)
2327			return (error);
2328		    newvp = NFSTOV(np);
2329		}
2330		if (!attrflag && *npp == NULL) {
2331			vrele(newvp);
2332			return (ENOENT);
2333		}
2334		if (attrflag)
2335			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2336			    0, 1);
2337	}
2338	if (npp && *npp == NULL) {
2339		if (error) {
2340			if (newvp) {
2341				if (newvp == dvp)
2342					vrele(newvp);
2343				else
2344					vput(newvp);
2345			}
2346		} else
2347			*npp = np;
2348	}
2349	if (error && NFS_ISV4(dvp))
2350		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2351	return (error);
2352}
2353
2354/*
2355 * Nfs Version 3 and 4 commit rpc
2356 */
2357int
2358ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2359   struct thread *td)
2360{
2361	struct nfsvattr nfsva;
2362	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2363	int error, attrflag;
2364	u_char verf[NFSX_VERF];
2365
2366	mtx_lock(&nmp->nm_mtx);
2367	if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2368		mtx_unlock(&nmp->nm_mtx);
2369		return (0);
2370	}
2371	mtx_unlock(&nmp->nm_mtx);
2372	error = nfsrpc_commit(vp, offset, cnt, cred, td, verf, &nfsva,
2373	    &attrflag, NULL);
2374	if (!error) {
2375		if (NFSBCMP((caddr_t)nmp->nm_verf, verf, NFSX_VERF)) {
2376			NFSBCOPY(verf, (caddr_t)nmp->nm_verf, NFSX_VERF);
2377			error = NFSERR_STALEWRITEVERF;
2378		}
2379		if (!error && attrflag)
2380			(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL,
2381			    0, 1);
2382	} else if (NFS_ISV4(vp)) {
2383		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2384	}
2385	return (error);
2386}
2387
2388/*
2389 * Strategy routine.
2390 * For async requests when nfsiod(s) are running, queue the request by
2391 * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2392 * request.
2393 */
2394static int
2395nfs_strategy(struct vop_strategy_args *ap)
2396{
2397	struct buf *bp = ap->a_bp;
2398	struct ucred *cr;
2399
2400	KASSERT(!(bp->b_flags & B_DONE),
2401	    ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2402	BUF_ASSERT_HELD(bp);
2403
2404	if (bp->b_iocmd == BIO_READ)
2405		cr = bp->b_rcred;
2406	else
2407		cr = bp->b_wcred;
2408
2409	/*
2410	 * If the op is asynchronous and an i/o daemon is waiting
2411	 * queue the request, wake it up and wait for completion
2412	 * otherwise just do it ourselves.
2413	 */
2414	if ((bp->b_flags & B_ASYNC) == 0 ||
2415	    ncl_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
2416		(void)ncl_doio(ap->a_vp, bp, cr, curthread);
2417	return (0);
2418}
2419
2420/*
2421 * fsync vnode op. Just call ncl_flush() with commit == 1.
2422 */
2423/* ARGSUSED */
2424static int
2425nfs_fsync(struct vop_fsync_args *ap)
2426{
2427	return (ncl_flush(ap->a_vp, ap->a_waitfor, NULL, ap->a_td, 1));
2428}
2429
2430/*
2431 * Flush all the blocks associated with a vnode.
2432 * 	Walk through the buffer pool and push any dirty pages
2433 *	associated with the vnode.
2434 */
2435int
2436ncl_flush(struct vnode *vp, int waitfor, struct ucred *cred, struct thread *td,
2437    int commit)
2438{
2439	struct nfsnode *np = VTONFS(vp);
2440	struct buf *bp;
2441	int i;
2442	struct buf *nbp;
2443	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2444	int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2445	int passone = 1, trycnt = 0;
2446	u_quad_t off, endoff, toff;
2447	struct ucred* wcred = NULL;
2448	struct buf **bvec = NULL;
2449	struct bufobj *bo;
2450#ifndef NFS_COMMITBVECSIZ
2451#define	NFS_COMMITBVECSIZ	20
2452#endif
2453	struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2454	int bvecsize = 0, bveccount;
2455
2456	if (nmp->nm_flag & NFSMNT_INT)
2457		slpflag = NFS_PCATCH;
2458	if (!commit)
2459		passone = 0;
2460	bo = &vp->v_bufobj;
2461	/*
2462	 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2463	 * server, but has not been committed to stable storage on the server
2464	 * yet. On the first pass, the byte range is worked out and the commit
2465	 * rpc is done. On the second pass, ncl_writebp() is called to do the
2466	 * job.
2467	 */
2468again:
2469	off = (u_quad_t)-1;
2470	endoff = 0;
2471	bvecpos = 0;
2472	if (NFS_ISV34(vp) && commit) {
2473		if (bvec != NULL && bvec != bvec_on_stack)
2474			free(bvec, M_TEMP);
2475		/*
2476		 * Count up how many buffers waiting for a commit.
2477		 */
2478		bveccount = 0;
2479		BO_LOCK(bo);
2480		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2481			if (!BUF_ISLOCKED(bp) &&
2482			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2483				== (B_DELWRI | B_NEEDCOMMIT))
2484				bveccount++;
2485		}
2486		/*
2487		 * Allocate space to remember the list of bufs to commit.  It is
2488		 * important to use M_NOWAIT here to avoid a race with nfs_write.
2489		 * If we can't get memory (for whatever reason), we will end up
2490		 * committing the buffers one-by-one in the loop below.
2491		 */
2492		if (bveccount > NFS_COMMITBVECSIZ) {
2493			/*
2494			 * Release the vnode interlock to avoid a lock
2495			 * order reversal.
2496			 */
2497			BO_UNLOCK(bo);
2498			bvec = (struct buf **)
2499				malloc(bveccount * sizeof(struct buf *),
2500				       M_TEMP, M_NOWAIT);
2501			BO_LOCK(bo);
2502			if (bvec == NULL) {
2503				bvec = bvec_on_stack;
2504				bvecsize = NFS_COMMITBVECSIZ;
2505			} else
2506				bvecsize = bveccount;
2507		} else {
2508			bvec = bvec_on_stack;
2509			bvecsize = NFS_COMMITBVECSIZ;
2510		}
2511		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2512			if (bvecpos >= bvecsize)
2513				break;
2514			if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2515				nbp = TAILQ_NEXT(bp, b_bobufs);
2516				continue;
2517			}
2518			if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2519			    (B_DELWRI | B_NEEDCOMMIT)) {
2520				BUF_UNLOCK(bp);
2521				nbp = TAILQ_NEXT(bp, b_bobufs);
2522				continue;
2523			}
2524			BO_UNLOCK(bo);
2525			bremfree(bp);
2526			/*
2527			 * Work out if all buffers are using the same cred
2528			 * so we can deal with them all with one commit.
2529			 *
2530			 * NOTE: we are not clearing B_DONE here, so we have
2531			 * to do it later on in this routine if we intend to
2532			 * initiate I/O on the bp.
2533			 *
2534			 * Note: to avoid loopback deadlocks, we do not
2535			 * assign b_runningbufspace.
2536			 */
2537			if (wcred == NULL)
2538				wcred = bp->b_wcred;
2539			else if (wcred != bp->b_wcred)
2540				wcred = NOCRED;
2541			vfs_busy_pages(bp, 1);
2542
2543			BO_LOCK(bo);
2544			/*
2545			 * bp is protected by being locked, but nbp is not
2546			 * and vfs_busy_pages() may sleep.  We have to
2547			 * recalculate nbp.
2548			 */
2549			nbp = TAILQ_NEXT(bp, b_bobufs);
2550
2551			/*
2552			 * A list of these buffers is kept so that the
2553			 * second loop knows which buffers have actually
2554			 * been committed. This is necessary, since there
2555			 * may be a race between the commit rpc and new
2556			 * uncommitted writes on the file.
2557			 */
2558			bvec[bvecpos++] = bp;
2559			toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2560				bp->b_dirtyoff;
2561			if (toff < off)
2562				off = toff;
2563			toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2564			if (toff > endoff)
2565				endoff = toff;
2566		}
2567		BO_UNLOCK(bo);
2568	}
2569	if (bvecpos > 0) {
2570		/*
2571		 * Commit data on the server, as required.
2572		 * If all bufs are using the same wcred, then use that with
2573		 * one call for all of them, otherwise commit each one
2574		 * separately.
2575		 */
2576		if (wcred != NOCRED)
2577			retv = ncl_commit(vp, off, (int)(endoff - off),
2578					  wcred, td);
2579		else {
2580			retv = 0;
2581			for (i = 0; i < bvecpos; i++) {
2582				off_t off, size;
2583				bp = bvec[i];
2584				off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2585					bp->b_dirtyoff;
2586				size = (u_quad_t)(bp->b_dirtyend
2587						  - bp->b_dirtyoff);
2588				retv = ncl_commit(vp, off, (int)size,
2589						  bp->b_wcred, td);
2590				if (retv) break;
2591			}
2592		}
2593
2594		if (retv == NFSERR_STALEWRITEVERF)
2595			ncl_clearcommit(vp->v_mount);
2596
2597		/*
2598		 * Now, either mark the blocks I/O done or mark the
2599		 * blocks dirty, depending on whether the commit
2600		 * succeeded.
2601		 */
2602		for (i = 0; i < bvecpos; i++) {
2603			bp = bvec[i];
2604			bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2605			if (retv) {
2606				/*
2607				 * Error, leave B_DELWRI intact
2608				 */
2609				vfs_unbusy_pages(bp);
2610				brelse(bp);
2611			} else {
2612				/*
2613				 * Success, remove B_DELWRI ( bundirty() ).
2614				 *
2615				 * b_dirtyoff/b_dirtyend seem to be NFS
2616				 * specific.  We should probably move that
2617				 * into bundirty(). XXX
2618				 */
2619				bufobj_wref(bo);
2620				bp->b_flags |= B_ASYNC;
2621				bundirty(bp);
2622				bp->b_flags &= ~B_DONE;
2623				bp->b_ioflags &= ~BIO_ERROR;
2624				bp->b_dirtyoff = bp->b_dirtyend = 0;
2625				bufdone(bp);
2626			}
2627		}
2628	}
2629
2630	/*
2631	 * Start/do any write(s) that are required.
2632	 */
2633loop:
2634	BO_LOCK(bo);
2635	TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2636		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2637			if (waitfor != MNT_WAIT || passone)
2638				continue;
2639
2640			error = BUF_TIMELOCK(bp,
2641			    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2642			    BO_MTX(bo), "nfsfsync", slpflag, slptimeo);
2643			if (error == 0) {
2644				BUF_UNLOCK(bp);
2645				goto loop;
2646			}
2647			if (error == ENOLCK) {
2648				error = 0;
2649				goto loop;
2650			}
2651			if (newnfs_sigintr(nmp, td)) {
2652				error = EINTR;
2653				goto done;
2654			}
2655			if (slpflag & PCATCH) {
2656				slpflag = 0;
2657				slptimeo = 2 * hz;
2658			}
2659			goto loop;
2660		}
2661		if ((bp->b_flags & B_DELWRI) == 0)
2662			panic("nfs_fsync: not dirty");
2663		if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2664			BUF_UNLOCK(bp);
2665			continue;
2666		}
2667		BO_UNLOCK(bo);
2668		bremfree(bp);
2669		if (passone || !commit)
2670		    bp->b_flags |= B_ASYNC;
2671		else
2672		    bp->b_flags |= B_ASYNC;
2673		bwrite(bp);
2674		if (newnfs_sigintr(nmp, td)) {
2675			error = EINTR;
2676			goto done;
2677		}
2678		goto loop;
2679	}
2680	if (passone) {
2681		passone = 0;
2682		BO_UNLOCK(bo);
2683		goto again;
2684	}
2685	if (waitfor == MNT_WAIT) {
2686		while (bo->bo_numoutput) {
2687			error = bufobj_wwait(bo, slpflag, slptimeo);
2688			if (error) {
2689			    BO_UNLOCK(bo);
2690			    error = newnfs_sigintr(nmp, td);
2691			    if (error)
2692				goto done;
2693			    if (slpflag & PCATCH) {
2694				slpflag = 0;
2695				slptimeo = 2 * hz;
2696			    }
2697			    BO_LOCK(bo);
2698			}
2699		}
2700		if (bo->bo_dirty.bv_cnt != 0 && commit) {
2701			BO_UNLOCK(bo);
2702			goto loop;
2703		}
2704		/*
2705		 * Wait for all the async IO requests to drain
2706		 */
2707		BO_UNLOCK(bo);
2708		mtx_lock(&np->n_mtx);
2709		while (np->n_directio_asyncwr > 0) {
2710			np->n_flag |= NFSYNCWAIT;
2711			error = newnfs_msleep(td, &np->n_directio_asyncwr,
2712			    &np->n_mtx, slpflag | (PRIBIO + 1),
2713			    "nfsfsync", 0);
2714			if (error) {
2715				if (newnfs_sigintr(nmp, td)) {
2716					mtx_unlock(&np->n_mtx);
2717					error = EINTR;
2718					goto done;
2719				}
2720			}
2721		}
2722		mtx_unlock(&np->n_mtx);
2723	} else
2724		BO_UNLOCK(bo);
2725	mtx_lock(&np->n_mtx);
2726	if (np->n_flag & NWRITEERR) {
2727		error = np->n_error;
2728		np->n_flag &= ~NWRITEERR;
2729	}
2730  	if (commit && bo->bo_dirty.bv_cnt == 0 &&
2731	    bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
2732  		np->n_flag &= ~NMODIFIED;
2733	mtx_unlock(&np->n_mtx);
2734done:
2735	if (bvec != NULL && bvec != bvec_on_stack)
2736		free(bvec, M_TEMP);
2737	if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
2738	    (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
2739	     np->n_directio_asyncwr != 0) && trycnt++ < 5) {
2740		/* try, try again... */
2741		passone = 1;
2742		wcred = NULL;
2743		bvec = NULL;
2744		bvecsize = 0;
2745printf("try%d\n", trycnt);
2746		goto again;
2747	}
2748	return (error);
2749}
2750
2751/*
2752 * NFS advisory byte-level locks.
2753 */
2754static int
2755nfs_advlock(struct vop_advlock_args *ap)
2756{
2757	struct vnode *vp = ap->a_vp;
2758	struct ucred *cred;
2759	struct nfsnode *np = VTONFS(ap->a_vp);
2760	struct proc *p = (struct proc *)ap->a_id;
2761	struct thread *td = curthread;	/* XXX */
2762	struct vattr va;
2763	int ret, error = EOPNOTSUPP;
2764	u_quad_t size;
2765
2766	if (NFS_ISV4(vp) && (ap->a_flags & F_POSIX)) {
2767		cred = p->p_ucred;
2768		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2769		if (vp->v_iflag & VI_DOOMED) {
2770			VOP_UNLOCK(vp, 0);
2771			return (EBADF);
2772		}
2773
2774		/*
2775		 * If this is unlocking a write locked region, flush and
2776		 * commit them before unlocking. This is required by
2777		 * RFC3530 Sec. 9.3.2.
2778		 */
2779		if (ap->a_op == F_UNLCK &&
2780		    nfscl_checkwritelocked(vp, ap->a_fl, cred, td))
2781			(void) ncl_flush(vp, MNT_WAIT, cred, td, 1);
2782
2783		/*
2784		 * Loop around doing the lock op, while a blocking lock
2785		 * must wait for the lock op to succeed.
2786		 */
2787		do {
2788			ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
2789			    ap->a_fl, 0, cred, td);
2790			if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
2791			    ap->a_op == F_SETLK) {
2792				VOP_UNLOCK(vp, 0);
2793				error = nfs_catnap(PZERO | PCATCH, "ncladvl");
2794				if (error)
2795					return (EINTR);
2796				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2797				if (vp->v_iflag & VI_DOOMED) {
2798					VOP_UNLOCK(vp, 0);
2799					return (EBADF);
2800				}
2801			}
2802		} while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
2803		     ap->a_op == F_SETLK);
2804		if (ret == NFSERR_DENIED) {
2805			VOP_UNLOCK(vp, 0);
2806			return (EAGAIN);
2807		} else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
2808			VOP_UNLOCK(vp, 0);
2809			return (ret);
2810		} else if (ret != 0) {
2811			VOP_UNLOCK(vp, 0);
2812			return (EACCES);
2813		}
2814
2815		/*
2816		 * Now, if we just got a lock, invalidate data in the buffer
2817		 * cache, as required, so that the coherency conforms with
2818		 * RFC3530 Sec. 9.3.2.
2819		 */
2820		if (ap->a_op == F_SETLK) {
2821			if ((np->n_flag & NMODIFIED) == 0) {
2822				np->n_attrstamp = 0;
2823				ret = VOP_GETATTR(vp, &va, cred);
2824			}
2825			if ((np->n_flag & NMODIFIED) || ret ||
2826			    np->n_change != va.va_filerev) {
2827				(void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
2828				np->n_attrstamp = 0;
2829				ret = VOP_GETATTR(vp, &va, cred);
2830				if (!ret) {
2831					np->n_mtime = va.va_mtime;
2832					np->n_change = va.va_filerev;
2833				}
2834			}
2835		}
2836		VOP_UNLOCK(vp, 0);
2837		return (0);
2838	} else if (!NFS_ISV4(vp)) {
2839		error = vn_lock(vp, LK_SHARED);
2840		if (error)
2841			return (error);
2842		if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
2843			size = VTONFS(vp)->n_size;
2844			VOP_UNLOCK(vp, 0);
2845			error = lf_advlock(ap, &(vp->v_lockf), size);
2846		} else {
2847			if (ncl_advlock_p)
2848				error = ncl_advlock_p(ap);
2849			else
2850				error = ENOLCK;
2851		}
2852	}
2853	return (error);
2854}
2855
2856/*
2857 * NFS advisory byte-level locks.
2858 */
2859static int
2860nfs_advlockasync(struct vop_advlockasync_args *ap)
2861{
2862	struct vnode *vp = ap->a_vp;
2863	u_quad_t size;
2864	int error;
2865
2866	if (NFS_ISV4(vp))
2867		return (EOPNOTSUPP);
2868	error = vn_lock(vp, LK_SHARED);
2869	if (error)
2870		return (error);
2871	if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
2872		size = VTONFS(vp)->n_size;
2873		VOP_UNLOCK(vp, 0);
2874		error = lf_advlockasync(ap, &(vp->v_lockf), size);
2875	} else {
2876		VOP_UNLOCK(vp, 0);
2877		error = EOPNOTSUPP;
2878	}
2879	return (error);
2880}
2881
2882/*
2883 * Print out the contents of an nfsnode.
2884 */
2885static int
2886nfs_print(struct vop_print_args *ap)
2887{
2888	struct vnode *vp = ap->a_vp;
2889	struct nfsnode *np = VTONFS(vp);
2890
2891	ncl_printf("\tfileid %ld fsid 0x%x",
2892	   np->n_vattr.na_fileid, np->n_vattr.na_fsid);
2893	if (vp->v_type == VFIFO)
2894		fifo_printinfo(vp);
2895	printf("\n");
2896	return (0);
2897}
2898
2899/*
2900 * This is the "real" nfs::bwrite(struct buf*).
2901 * We set B_CACHE if this is a VMIO buffer.
2902 */
2903int
2904ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
2905{
2906	int s;
2907	int oldflags = bp->b_flags;
2908#if 0
2909	int retv = 1;
2910	off_t off;
2911#endif
2912
2913	BUF_ASSERT_HELD(bp);
2914
2915	if (bp->b_flags & B_INVAL) {
2916		brelse(bp);
2917		return(0);
2918	}
2919
2920	bp->b_flags |= B_CACHE;
2921
2922	/*
2923	 * Undirty the bp.  We will redirty it later if the I/O fails.
2924	 */
2925
2926	s = splbio();
2927	bundirty(bp);
2928	bp->b_flags &= ~B_DONE;
2929	bp->b_ioflags &= ~BIO_ERROR;
2930	bp->b_iocmd = BIO_WRITE;
2931
2932	bufobj_wref(bp->b_bufobj);
2933	curthread->td_ru.ru_oublock++;
2934	splx(s);
2935
2936	/*
2937	 * Note: to avoid loopback deadlocks, we do not
2938	 * assign b_runningbufspace.
2939	 */
2940	vfs_busy_pages(bp, 1);
2941
2942	BUF_KERNPROC(bp);
2943	bp->b_iooffset = dbtob(bp->b_blkno);
2944	bstrategy(bp);
2945
2946	if( (oldflags & B_ASYNC) == 0) {
2947		int rtval = bufwait(bp);
2948
2949		if (oldflags & B_DELWRI) {
2950			s = splbio();
2951			reassignbuf(bp);
2952			splx(s);
2953		}
2954		brelse(bp);
2955		return (rtval);
2956	}
2957
2958	return (0);
2959}
2960
2961/*
2962 * nfs special file access vnode op.
2963 * Essentially just get vattr and then imitate iaccess() since the device is
2964 * local to the client.
2965 */
2966static int
2967nfsspec_access(struct vop_access_args *ap)
2968{
2969	struct vattr *vap;
2970	struct ucred *cred = ap->a_cred;
2971	struct vnode *vp = ap->a_vp;
2972	accmode_t accmode = ap->a_accmode;
2973	struct vattr vattr;
2974	int error;
2975
2976	/*
2977	 * Disallow write attempts on filesystems mounted read-only;
2978	 * unless the file is a socket, fifo, or a block or character
2979	 * device resident on the filesystem.
2980	 */
2981	if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
2982		switch (vp->v_type) {
2983		case VREG:
2984		case VDIR:
2985		case VLNK:
2986			return (EROFS);
2987		default:
2988			break;
2989		}
2990	}
2991	vap = &vattr;
2992	error = VOP_GETATTR(vp, vap, cred);
2993	if (error)
2994		goto out;
2995	error  = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
2996	    accmode, cred, NULL);
2997out:
2998	return error;
2999}
3000
3001/*
3002 * Read wrapper for fifos.
3003 */
3004static int
3005nfsfifo_read(struct vop_read_args *ap)
3006{
3007	struct nfsnode *np = VTONFS(ap->a_vp);
3008	int error;
3009
3010	/*
3011	 * Set access flag.
3012	 */
3013	mtx_lock(&np->n_mtx);
3014	np->n_flag |= NACC;
3015	getnanotime(&np->n_atim);
3016	mtx_unlock(&np->n_mtx);
3017	error = fifo_specops.vop_read(ap);
3018	return error;
3019}
3020
3021/*
3022 * Write wrapper for fifos.
3023 */
3024static int
3025nfsfifo_write(struct vop_write_args *ap)
3026{
3027	struct nfsnode *np = VTONFS(ap->a_vp);
3028
3029	/*
3030	 * Set update flag.
3031	 */
3032	mtx_lock(&np->n_mtx);
3033	np->n_flag |= NUPD;
3034	getnanotime(&np->n_mtim);
3035	mtx_unlock(&np->n_mtx);
3036	return(fifo_specops.vop_write(ap));
3037}
3038
3039/*
3040 * Close wrapper for fifos.
3041 *
3042 * Update the times on the nfsnode then do fifo close.
3043 */
3044static int
3045nfsfifo_close(struct vop_close_args *ap)
3046{
3047	struct vnode *vp = ap->a_vp;
3048	struct nfsnode *np = VTONFS(vp);
3049	struct vattr vattr;
3050	struct timespec ts;
3051
3052	mtx_lock(&np->n_mtx);
3053	if (np->n_flag & (NACC | NUPD)) {
3054		getnanotime(&ts);
3055		if (np->n_flag & NACC)
3056			np->n_atim = ts;
3057		if (np->n_flag & NUPD)
3058			np->n_mtim = ts;
3059		np->n_flag |= NCHG;
3060		if (vrefcnt(vp) == 1 &&
3061		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3062			VATTR_NULL(&vattr);
3063			if (np->n_flag & NACC)
3064				vattr.va_atime = np->n_atim;
3065			if (np->n_flag & NUPD)
3066				vattr.va_mtime = np->n_mtim;
3067			mtx_unlock(&np->n_mtx);
3068			(void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3069			goto out;
3070		}
3071	}
3072	mtx_unlock(&np->n_mtx);
3073out:
3074	return (fifo_specops.vop_close(ap));
3075}
3076
3077/*
3078 * Just call ncl_writebp() with the force argument set to 1.
3079 *
3080 * NOTE: B_DONE may or may not be set in a_bp on call.
3081 */
3082static int
3083nfs_bwrite(struct buf *bp)
3084{
3085
3086	return (ncl_writebp(bp, 1, curthread));
3087}
3088
3089struct buf_ops buf_ops_newnfs = {
3090	.bop_name	=	"buf_ops_nfs",
3091	.bop_write	=	nfs_bwrite,
3092	.bop_strategy	=	bufstrategy,
3093	.bop_sync	=	bufsync,
3094	.bop_bdflush	=	bufbdflush,
3095};
3096
3097/*
3098 * Cloned from vop_stdlock(), and then the ugly hack added.
3099 */
3100static int
3101nfs_lock1(struct vop_lock1_args *ap)
3102{
3103	struct vnode *vp = ap->a_vp;
3104	int error = 0;
3105
3106	/*
3107	 * Since vfs_hash_get() calls vget() and it will no longer work
3108	 * for FreeBSD8 with flags == 0, I can only think of this horrible
3109	 * hack to work around it. I call vfs_hash_get() with LK_EXCLOTHER
3110	 * and then handle it here. All I want for this case is a v_usecount
3111	 * on the vnode to use for recovery, while another thread might
3112	 * hold a lock on the vnode. I have the other threads blocked, so
3113	 * there isn't any race problem.
3114	 */
3115	if ((ap->a_flags & LK_TYPE_MASK) == LK_EXCLOTHER) {
3116		if ((ap->a_flags & LK_INTERLOCK) == 0)
3117			panic("ncllock1");
3118		if ((vp->v_iflag & VI_DOOMED))
3119			error = ENOENT;
3120		VI_UNLOCK(vp);
3121		return (error);
3122	}
3123	return (_lockmgr_args(vp->v_vnlock, ap->a_flags, VI_MTX(vp),
3124	    LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, ap->a_file,
3125	    ap->a_line));
3126}
3127
3128static int
3129nfs_getacl(struct vop_getacl_args *ap)
3130{
3131	int error;
3132
3133	if (ap->a_type != ACL_TYPE_NFS4)
3134		return (EOPNOTSUPP);
3135	error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3136	    NULL);
3137	if (error > NFSERR_STALE) {
3138		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3139		error = EPERM;
3140	}
3141	return (error);
3142}
3143
3144static int
3145nfs_setacl(struct vop_setacl_args *ap)
3146{
3147	int error;
3148
3149	if (ap->a_type != ACL_TYPE_NFS4)
3150		return (EOPNOTSUPP);
3151	error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3152	    NULL);
3153	if (error > NFSERR_STALE) {
3154		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3155		error = EPERM;
3156	}
3157	return (error);
3158}
3159