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