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