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