nfs_bio.c revision 137846
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_bio.c	8.9 (Berkeley) 3/30/95
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: head/sys/nfsclient/nfs_bio.c 137846 2004-11-18 08:44:09Z jeff $");
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/bio.h>
41#include <sys/buf.h>
42#include <sys/kernel.h>
43#include <sys/mount.h>
44#include <sys/proc.h>
45#include <sys/resourcevar.h>
46#include <sys/signalvar.h>
47#include <sys/vmmeter.h>
48#include <sys/vnode.h>
49
50#include <vm/vm.h>
51#include <vm/vm_extern.h>
52#include <vm/vm_page.h>
53#include <vm/vm_object.h>
54#include <vm/vm_pager.h>
55#include <vm/vnode_pager.h>
56
57#include <rpc/rpcclnt.h>
58
59#include <nfs/rpcv2.h>
60#include <nfs/nfsproto.h>
61#include <nfsclient/nfs.h>
62#include <nfsclient/nfsmount.h>
63#include <nfsclient/nfsnode.h>
64
65#include <nfs4client/nfs4.h>
66
67static struct buf *nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size,
68		    struct thread *td);
69
70/*
71 * Vnode op for VM getpages.
72 */
73int
74nfs_getpages(struct vop_getpages_args *ap)
75{
76	int i, error, nextoff, size, toff, count, npages;
77	struct uio uio;
78	struct iovec iov;
79	vm_offset_t kva;
80	struct buf *bp;
81	struct vnode *vp;
82	struct thread *td;
83	struct ucred *cred;
84	struct nfsmount *nmp;
85	vm_object_t object;
86	vm_page_t *pages;
87
88	GIANT_REQUIRED;
89
90	vp = ap->a_vp;
91	td = curthread;				/* XXX */
92	cred = curthread->td_ucred;		/* XXX */
93	nmp = VFSTONFS(vp->v_mount);
94	pages = ap->a_m;
95	count = ap->a_count;
96
97	if ((object = vp->v_object) == NULL) {
98		printf("nfs_getpages: called with non-merged cache vnode??\n");
99		return VM_PAGER_ERROR;
100	}
101
102	if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
103	    (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
104		/* We'll never get here for v4, because we always have fsinfo */
105		(void)nfs_fsinfo(nmp, vp, cred, td);
106	}
107
108	npages = btoc(count);
109
110	/*
111	 * If the requested page is partially valid, just return it and
112	 * allow the pager to zero-out the blanks.  Partially valid pages
113	 * can only occur at the file EOF.
114	 */
115
116	{
117		vm_page_t m = pages[ap->a_reqpage];
118
119		VM_OBJECT_LOCK(object);
120		vm_page_lock_queues();
121		if (m->valid != 0) {
122			/* handled by vm_fault now	  */
123			/* vm_page_zero_invalid(m, TRUE); */
124			for (i = 0; i < npages; ++i) {
125				if (i != ap->a_reqpage)
126					vm_page_free(pages[i]);
127			}
128			vm_page_unlock_queues();
129			VM_OBJECT_UNLOCK(object);
130			return(0);
131		}
132		vm_page_unlock_queues();
133		VM_OBJECT_UNLOCK(object);
134	}
135
136	/*
137	 * We use only the kva address for the buffer, but this is extremely
138	 * convienient and fast.
139	 */
140	bp = getpbuf(&nfs_pbuf_freecnt);
141
142	kva = (vm_offset_t) bp->b_data;
143	pmap_qenter(kva, pages, npages);
144	cnt.v_vnodein++;
145	cnt.v_vnodepgsin += npages;
146
147	iov.iov_base = (caddr_t) kva;
148	iov.iov_len = count;
149	uio.uio_iov = &iov;
150	uio.uio_iovcnt = 1;
151	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
152	uio.uio_resid = count;
153	uio.uio_segflg = UIO_SYSSPACE;
154	uio.uio_rw = UIO_READ;
155	uio.uio_td = td;
156
157	error = (nmp->nm_rpcops->nr_readrpc)(vp, &uio, cred);
158	pmap_qremove(kva, npages);
159
160	relpbuf(bp, &nfs_pbuf_freecnt);
161
162	if (error && (uio.uio_resid == count)) {
163		printf("nfs_getpages: error %d\n", error);
164		VM_OBJECT_LOCK(object);
165		vm_page_lock_queues();
166		for (i = 0; i < npages; ++i) {
167			if (i != ap->a_reqpage)
168				vm_page_free(pages[i]);
169		}
170		vm_page_unlock_queues();
171		VM_OBJECT_UNLOCK(object);
172		return VM_PAGER_ERROR;
173	}
174
175	/*
176	 * Calculate the number of bytes read and validate only that number
177	 * of bytes.  Note that due to pending writes, size may be 0.  This
178	 * does not mean that the remaining data is invalid!
179	 */
180
181	size = count - uio.uio_resid;
182	VM_OBJECT_LOCK(object);
183	vm_page_lock_queues();
184	for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
185		vm_page_t m;
186		nextoff = toff + PAGE_SIZE;
187		m = pages[i];
188
189		if (nextoff <= size) {
190			/*
191			 * Read operation filled an entire page
192			 */
193			m->valid = VM_PAGE_BITS_ALL;
194			vm_page_undirty(m);
195		} else if (size > toff) {
196			/*
197			 * Read operation filled a partial page.
198			 */
199			m->valid = 0;
200			vm_page_set_validclean(m, 0, size - toff);
201			/* handled by vm_fault now	  */
202			/* vm_page_zero_invalid(m, TRUE); */
203		} else {
204			/*
205			 * Read operation was short.  If no error occured
206			 * we may have hit a zero-fill section.   We simply
207			 * leave valid set to 0.
208			 */
209			;
210		}
211		if (i != ap->a_reqpage) {
212			/*
213			 * Whether or not to leave the page activated is up in
214			 * the air, but we should put the page on a page queue
215			 * somewhere (it already is in the object).  Result:
216			 * It appears that emperical results show that
217			 * deactivating pages is best.
218			 */
219
220			/*
221			 * Just in case someone was asking for this page we
222			 * now tell them that it is ok to use.
223			 */
224			if (!error) {
225				if (m->flags & PG_WANTED)
226					vm_page_activate(m);
227				else
228					vm_page_deactivate(m);
229				vm_page_wakeup(m);
230			} else {
231				vm_page_free(m);
232			}
233		}
234	}
235	vm_page_unlock_queues();
236	VM_OBJECT_UNLOCK(object);
237	return 0;
238}
239
240/*
241 * Vnode op for VM putpages.
242 */
243int
244nfs_putpages(struct vop_putpages_args *ap)
245{
246	struct uio uio;
247	struct iovec iov;
248	vm_offset_t kva;
249	struct buf *bp;
250	int iomode, must_commit, i, error, npages, count;
251	off_t offset;
252	int *rtvals;
253	struct vnode *vp;
254	struct thread *td;
255	struct ucred *cred;
256	struct nfsmount *nmp;
257	struct nfsnode *np;
258	vm_page_t *pages;
259
260	GIANT_REQUIRED;
261
262	vp = ap->a_vp;
263	np = VTONFS(vp);
264	td = curthread;				/* XXX */
265	cred = curthread->td_ucred;		/* XXX */
266	nmp = VFSTONFS(vp->v_mount);
267	pages = ap->a_m;
268	count = ap->a_count;
269	rtvals = ap->a_rtvals;
270	npages = btoc(count);
271	offset = IDX_TO_OFF(pages[0]->pindex);
272
273	if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
274	    (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
275		(void)nfs_fsinfo(nmp, vp, cred, td);
276	}
277
278	for (i = 0; i < npages; i++)
279		rtvals[i] = VM_PAGER_AGAIN;
280
281	/*
282	 * When putting pages, do not extend file past EOF.
283	 */
284
285	if (offset + count > np->n_size) {
286		count = np->n_size - offset;
287		if (count < 0)
288			count = 0;
289	}
290
291	/*
292	 * We use only the kva address for the buffer, but this is extremely
293	 * convienient and fast.
294	 */
295	bp = getpbuf(&nfs_pbuf_freecnt);
296
297	kva = (vm_offset_t) bp->b_data;
298	pmap_qenter(kva, pages, npages);
299	cnt.v_vnodeout++;
300	cnt.v_vnodepgsout += count;
301
302	iov.iov_base = (caddr_t) kva;
303	iov.iov_len = count;
304	uio.uio_iov = &iov;
305	uio.uio_iovcnt = 1;
306	uio.uio_offset = offset;
307	uio.uio_resid = count;
308	uio.uio_segflg = UIO_SYSSPACE;
309	uio.uio_rw = UIO_WRITE;
310	uio.uio_td = td;
311
312	if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
313	    iomode = NFSV3WRITE_UNSTABLE;
314	else
315	    iomode = NFSV3WRITE_FILESYNC;
316
317	error = (nmp->nm_rpcops->nr_writerpc)(vp, &uio, cred, &iomode, &must_commit);
318
319	pmap_qremove(kva, npages);
320	relpbuf(bp, &nfs_pbuf_freecnt);
321
322	if (!error) {
323		int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
324		for (i = 0; i < nwritten; i++) {
325			rtvals[i] = VM_PAGER_OK;
326			vm_page_undirty(pages[i]);
327		}
328		if (must_commit) {
329			nfs_clearcommit(vp->v_mount);
330		}
331	}
332	return rtvals[0];
333}
334
335/*
336 * Vnode op for read using bio
337 */
338int
339nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred)
340{
341	struct nfsnode *np = VTONFS(vp);
342	int biosize, i;
343	struct buf *bp = 0, *rabp;
344	struct vattr vattr;
345	struct thread *td;
346	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
347	daddr_t lbn, rabn;
348	int bcount;
349	int seqcount;
350	int nra, error = 0, n = 0, on = 0;
351
352#ifdef DIAGNOSTIC
353	if (uio->uio_rw != UIO_READ)
354		panic("nfs_read mode");
355#endif
356	if (uio->uio_resid == 0)
357		return (0);
358	if (uio->uio_offset < 0)	/* XXX VDIR cookies can be negative */
359		return (EINVAL);
360	td = uio->uio_td;
361
362	if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
363	    (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
364		(void)nfs_fsinfo(nmp, vp, cred, td);
365	if (vp->v_type != VDIR &&
366	    (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
367		return (EFBIG);
368	biosize = vp->v_mount->mnt_stat.f_iosize;
369	seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
370	/*
371	 * For nfs, cache consistency can only be maintained approximately.
372	 * Although RFC1094 does not specify the criteria, the following is
373	 * believed to be compatible with the reference port.
374	 * For nfs:
375	 * If the file's modify time on the server has changed since the
376	 * last read rpc or you have written to the file,
377	 * you may have lost data cache consistency with the
378	 * server, so flush all of the file's data out of the cache.
379	 * Then force a getattr rpc to ensure that you have up to date
380	 * attributes.
381	 * NB: This implies that cache data can be read when up to
382	 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
383	 * attributes this could be forced by setting n_attrstamp to 0 before
384	 * the VOP_GETATTR() call.
385	 */
386	if (np->n_flag & NMODIFIED) {
387		if (vp->v_type != VREG) {
388			if (vp->v_type != VDIR)
389				panic("nfs: bioread, not dir");
390			(nmp->nm_rpcops->nr_invaldir)(vp);
391			error = nfs_vinvalbuf(vp, V_SAVE, cred, td, 1);
392			if (error)
393				return (error);
394		}
395		np->n_attrstamp = 0;
396		error = VOP_GETATTR(vp, &vattr, cred, td);
397		if (error)
398			return (error);
399		np->n_mtime = vattr.va_mtime.tv_sec;
400	} else {
401		error = VOP_GETATTR(vp, &vattr, cred, td);
402		if (error)
403			return (error);
404		if ((np->n_flag & NSIZECHANGED)
405		    || (np->n_mtime != vattr.va_mtime.tv_sec)) {
406			if (vp->v_type == VDIR)
407				(nmp->nm_rpcops->nr_invaldir)(vp);
408			error = nfs_vinvalbuf(vp, V_SAVE, cred, td, 1);
409			if (error)
410				return (error);
411			np->n_mtime = vattr.va_mtime.tv_sec;
412			np->n_flag &= ~NSIZECHANGED;
413		}
414	}
415	do {
416	    switch (vp->v_type) {
417	    case VREG:
418		nfsstats.biocache_reads++;
419		lbn = uio->uio_offset / biosize;
420		on = uio->uio_offset & (biosize - 1);
421
422		/*
423		 * Start the read ahead(s), as required.
424		 */
425		if (nmp->nm_readahead > 0) {
426		    for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
427			(off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
428			rabn = lbn + 1 + nra;
429			if (incore(&vp->v_bufobj, rabn) == NULL) {
430			    rabp = nfs_getcacheblk(vp, rabn, biosize, td);
431			    if (!rabp) {
432				error = nfs_sigintr(nmp, NULL, td);
433				return (error ? error : EINTR);
434			    }
435			    if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
436				rabp->b_flags |= B_ASYNC;
437				rabp->b_iocmd = BIO_READ;
438				vfs_busy_pages(rabp, 0);
439				if (nfs_asyncio(nmp, rabp, cred, td)) {
440				    rabp->b_flags |= B_INVAL;
441				    rabp->b_ioflags |= BIO_ERROR;
442				    vfs_unbusy_pages(rabp);
443				    brelse(rabp);
444				    break;
445				}
446			    } else {
447				brelse(rabp);
448			    }
449			}
450		    }
451		}
452
453		/*
454		 * Obtain the buffer cache block.  Figure out the buffer size
455		 * when we are at EOF.  If we are modifying the size of the
456		 * buffer based on an EOF condition we need to hold
457		 * nfs_rslock() through obtaining the buffer to prevent
458		 * a potential writer-appender from messing with n_size.
459		 * Otherwise we may accidently truncate the buffer and
460		 * lose dirty data.
461		 *
462		 * Note that bcount is *not* DEV_BSIZE aligned.
463		 */
464
465again:
466		bcount = biosize;
467		if ((off_t)lbn * biosize >= np->n_size) {
468			bcount = 0;
469		} else if ((off_t)(lbn + 1) * biosize > np->n_size) {
470			bcount = np->n_size - (off_t)lbn * biosize;
471		}
472		if (bcount != biosize) {
473			switch(nfs_rslock(np, td)) {
474			case ENOLCK:
475				goto again;
476				/* not reached */
477			case EIO:
478				return (EIO);
479			case EINTR:
480			case ERESTART:
481				return(EINTR);
482				/* not reached */
483			default:
484				break;
485			}
486		}
487
488		bp = nfs_getcacheblk(vp, lbn, bcount, td);
489
490		if (bcount != biosize)
491			nfs_rsunlock(np, td);
492		if (!bp) {
493			error = nfs_sigintr(nmp, NULL, td);
494			return (error ? error : EINTR);
495		}
496
497		/*
498		 * If B_CACHE is not set, we must issue the read.  If this
499		 * fails, we return an error.
500		 */
501
502		if ((bp->b_flags & B_CACHE) == 0) {
503		    bp->b_iocmd = BIO_READ;
504		    vfs_busy_pages(bp, 0);
505		    error = nfs_doio(vp, bp, cred, td);
506		    if (error) {
507			brelse(bp);
508			return (error);
509		    }
510		}
511
512		/*
513		 * on is the offset into the current bp.  Figure out how many
514		 * bytes we can copy out of the bp.  Note that bcount is
515		 * NOT DEV_BSIZE aligned.
516		 *
517		 * Then figure out how many bytes we can copy into the uio.
518		 */
519
520		n = 0;
521		if (on < bcount)
522			n = min((unsigned)(bcount - on), uio->uio_resid);
523		break;
524	    case VLNK:
525		nfsstats.biocache_readlinks++;
526		bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, td);
527		if (!bp) {
528			error = nfs_sigintr(nmp, NULL, td);
529			return (error ? error : EINTR);
530		}
531		if ((bp->b_flags & B_CACHE) == 0) {
532		    bp->b_iocmd = BIO_READ;
533		    vfs_busy_pages(bp, 0);
534		    error = nfs_doio(vp, bp, cred, td);
535		    if (error) {
536			bp->b_ioflags |= BIO_ERROR;
537			brelse(bp);
538			return (error);
539		    }
540		}
541		n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
542		on = 0;
543		break;
544	    case VDIR:
545		nfsstats.biocache_readdirs++;
546		if (np->n_direofoffset
547		    && uio->uio_offset >= np->n_direofoffset) {
548		    return (0);
549		}
550		lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
551		on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
552		bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, td);
553		if (!bp) {
554		    error = nfs_sigintr(nmp, NULL, td);
555		    return (error ? error : EINTR);
556		}
557		if ((bp->b_flags & B_CACHE) == 0) {
558		    bp->b_iocmd = BIO_READ;
559		    vfs_busy_pages(bp, 0);
560		    error = nfs_doio(vp, bp, cred, td);
561		    if (error) {
562			    brelse(bp);
563		    }
564		    while (error == NFSERR_BAD_COOKIE) {
565			(nmp->nm_rpcops->nr_invaldir)(vp);
566			error = nfs_vinvalbuf(vp, 0, cred, td, 1);
567			/*
568			 * Yuck! The directory has been modified on the
569			 * server. The only way to get the block is by
570			 * reading from the beginning to get all the
571			 * offset cookies.
572			 *
573			 * Leave the last bp intact unless there is an error.
574			 * Loop back up to the while if the error is another
575			 * NFSERR_BAD_COOKIE (double yuch!).
576			 */
577			for (i = 0; i <= lbn && !error; i++) {
578			    if (np->n_direofoffset
579				&& (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
580				    return (0);
581			    bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, td);
582			    if (!bp) {
583				error = nfs_sigintr(nmp, NULL, td);
584				return (error ? error : EINTR);
585			    }
586			    if ((bp->b_flags & B_CACHE) == 0) {
587				    bp->b_iocmd = BIO_READ;
588				    vfs_busy_pages(bp, 0);
589				    error = nfs_doio(vp, bp, cred, td);
590				    /*
591				     * no error + B_INVAL == directory EOF,
592				     * use the block.
593				     */
594				    if (error == 0 && (bp->b_flags & B_INVAL))
595					    break;
596			    }
597			    /*
598			     * An error will throw away the block and the
599			     * for loop will break out.  If no error and this
600			     * is not the block we want, we throw away the
601			     * block and go for the next one via the for loop.
602			     */
603			    if (error || i < lbn)
604				    brelse(bp);
605			}
606		    }
607		    /*
608		     * The above while is repeated if we hit another cookie
609		     * error.  If we hit an error and it wasn't a cookie error,
610		     * we give up.
611		     */
612		    if (error)
613			    return (error);
614		}
615
616		/*
617		 * If not eof and read aheads are enabled, start one.
618		 * (You need the current block first, so that you have the
619		 *  directory offset cookie of the next block.)
620		 */
621		if (nmp->nm_readahead > 0 &&
622		    (bp->b_flags & B_INVAL) == 0 &&
623		    (np->n_direofoffset == 0 ||
624		    (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
625		    incore(&vp->v_bufobj, lbn + 1) == NULL) {
626			rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, td);
627			if (rabp) {
628			    if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
629				rabp->b_flags |= B_ASYNC;
630				rabp->b_iocmd = BIO_READ;
631				vfs_busy_pages(rabp, 0);
632				if (nfs_asyncio(nmp, rabp, cred, td)) {
633				    rabp->b_flags |= B_INVAL;
634				    rabp->b_ioflags |= BIO_ERROR;
635				    vfs_unbusy_pages(rabp);
636				    brelse(rabp);
637				}
638			    } else {
639				brelse(rabp);
640			    }
641			}
642		}
643		/*
644		 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
645		 * chopped for the EOF condition, we cannot tell how large
646		 * NFS directories are going to be until we hit EOF.  So
647		 * an NFS directory buffer is *not* chopped to its EOF.  Now,
648		 * it just so happens that b_resid will effectively chop it
649		 * to EOF.  *BUT* this information is lost if the buffer goes
650		 * away and is reconstituted into a B_CACHE state ( due to
651		 * being VMIO ) later.  So we keep track of the directory eof
652		 * in np->n_direofoffset and chop it off as an extra step
653		 * right here.
654		 */
655		n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
656		if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
657			n = np->n_direofoffset - uio->uio_offset;
658		break;
659	    default:
660		printf(" nfs_bioread: type %x unexpected\n", vp->v_type);
661		break;
662	    };
663
664	    if (n > 0) {
665		    error = uiomove(bp->b_data + on, (int)n, uio);
666	    }
667	    switch (vp->v_type) {
668	    case VREG:
669		break;
670	    case VLNK:
671		n = 0;
672		break;
673	    case VDIR:
674		break;
675	    default:
676		printf(" nfs_bioread: type %x unexpected\n", vp->v_type);
677	    }
678	    brelse(bp);
679	} while (error == 0 && uio->uio_resid > 0 && n > 0);
680	return (error);
681}
682
683/*
684 * Vnode op for write using bio
685 */
686int
687nfs_write(struct vop_write_args *ap)
688{
689	int biosize;
690	struct uio *uio = ap->a_uio;
691	struct thread *td = uio->uio_td;
692	struct vnode *vp = ap->a_vp;
693	struct nfsnode *np = VTONFS(vp);
694	struct ucred *cred = ap->a_cred;
695	int ioflag = ap->a_ioflag;
696	struct buf *bp;
697	struct vattr vattr;
698	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
699	daddr_t lbn;
700	int bcount;
701	int n, on, error = 0;
702	int haverslock = 0;
703	struct proc *p = td?td->td_proc:NULL;
704
705	GIANT_REQUIRED;
706
707#ifdef DIAGNOSTIC
708	if (uio->uio_rw != UIO_WRITE)
709		panic("nfs_write mode");
710	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_td != curthread)
711		panic("nfs_write proc");
712#endif
713	if (vp->v_type != VREG)
714		return (EIO);
715	if (np->n_flag & NWRITEERR) {
716		np->n_flag &= ~NWRITEERR;
717		return (np->n_error);
718	}
719	if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
720	    (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
721		(void)nfs_fsinfo(nmp, vp, cred, td);
722
723	/*
724	 * Synchronously flush pending buffers if we are in synchronous
725	 * mode or if we are appending.
726	 */
727	if (ioflag & (IO_APPEND | IO_SYNC)) {
728		if (np->n_flag & NMODIFIED) {
729			np->n_attrstamp = 0;
730			error = nfs_vinvalbuf(vp, V_SAVE, cred, td, 1);
731			if (error)
732				return (error);
733		}
734	}
735
736	/*
737	 * If IO_APPEND then load uio_offset.  We restart here if we cannot
738	 * get the append lock.
739	 */
740restart:
741	if (ioflag & IO_APPEND) {
742		np->n_attrstamp = 0;
743		error = VOP_GETATTR(vp, &vattr, cred, td);
744		if (error)
745			return (error);
746		uio->uio_offset = np->n_size;
747	}
748
749	if (uio->uio_offset < 0)
750		return (EINVAL);
751	if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
752		return (EFBIG);
753	if (uio->uio_resid == 0)
754		return (0);
755
756	/*
757	 * We need to obtain the rslock if we intend to modify np->n_size
758	 * in order to guarentee the append point with multiple contending
759	 * writers, to guarentee that no other appenders modify n_size
760	 * while we are trying to obtain a truncated buffer (i.e. to avoid
761	 * accidently truncating data written by another appender due to
762	 * the race), and to ensure that the buffer is populated prior to
763	 * our extending of the file.  We hold rslock through the entire
764	 * operation.
765	 *
766	 * Note that we do not synchronize the case where someone truncates
767	 * the file while we are appending to it because attempting to lock
768	 * this case may deadlock other parts of the system unexpectedly.
769	 */
770	if ((ioflag & IO_APPEND) ||
771	    uio->uio_offset + uio->uio_resid > np->n_size) {
772		switch(nfs_rslock(np, td)) {
773		case ENOLCK:
774			goto restart;
775			/* not reached */
776		case EIO:
777			return (EIO);
778		case EINTR:
779		case ERESTART:
780			return(EINTR);
781			/* not reached */
782		default:
783			break;
784		}
785		haverslock = 1;
786	}
787
788	/*
789	 * Maybe this should be above the vnode op call, but so long as
790	 * file servers have no limits, i don't think it matters
791	 */
792	if (p != NULL) {
793		PROC_LOCK(p);
794		if (uio->uio_offset + uio->uio_resid >
795		    lim_cur(p, RLIMIT_FSIZE)) {
796			psignal(p, SIGXFSZ);
797			PROC_UNLOCK(p);
798			if (haverslock)
799				nfs_rsunlock(np, td);
800			return (EFBIG);
801		}
802		PROC_UNLOCK(p);
803	}
804
805	biosize = vp->v_mount->mnt_stat.f_iosize;
806
807	do {
808		nfsstats.biocache_writes++;
809		lbn = uio->uio_offset / biosize;
810		on = uio->uio_offset & (biosize-1);
811		n = min((unsigned)(biosize - on), uio->uio_resid);
812again:
813		/*
814		 * Handle direct append and file extension cases, calculate
815		 * unaligned buffer size.
816		 */
817
818		if (uio->uio_offset == np->n_size && n) {
819			/*
820			 * Get the buffer (in its pre-append state to maintain
821			 * B_CACHE if it was previously set).  Resize the
822			 * nfsnode after we have locked the buffer to prevent
823			 * readers from reading garbage.
824			 */
825			bcount = on;
826			bp = nfs_getcacheblk(vp, lbn, bcount, td);
827
828			if (bp != NULL) {
829				long save;
830
831				np->n_size = uio->uio_offset + n;
832				np->n_flag |= NMODIFIED;
833				vnode_pager_setsize(vp, np->n_size);
834
835				save = bp->b_flags & B_CACHE;
836				bcount += n;
837				allocbuf(bp, bcount);
838				bp->b_flags |= save;
839			}
840		} else {
841			/*
842			 * Obtain the locked cache block first, and then
843			 * adjust the file's size as appropriate.
844			 */
845			bcount = on + n;
846			if ((off_t)lbn * biosize + bcount < np->n_size) {
847				if ((off_t)(lbn + 1) * biosize < np->n_size)
848					bcount = biosize;
849				else
850					bcount = np->n_size - (off_t)lbn * biosize;
851			}
852			bp = nfs_getcacheblk(vp, lbn, bcount, td);
853			if (uio->uio_offset + n > np->n_size) {
854				np->n_size = uio->uio_offset + n;
855				np->n_flag |= NMODIFIED;
856				vnode_pager_setsize(vp, np->n_size);
857			}
858		}
859
860		if (!bp) {
861			error = nfs_sigintr(nmp, NULL, td);
862			if (!error)
863				error = EINTR;
864			break;
865		}
866
867		/*
868		 * Issue a READ if B_CACHE is not set.  In special-append
869		 * mode, B_CACHE is based on the buffer prior to the write
870		 * op and is typically set, avoiding the read.  If a read
871		 * is required in special append mode, the server will
872		 * probably send us a short-read since we extended the file
873		 * on our end, resulting in b_resid == 0 and, thusly,
874		 * B_CACHE getting set.
875		 *
876		 * We can also avoid issuing the read if the write covers
877		 * the entire buffer.  We have to make sure the buffer state
878		 * is reasonable in this case since we will not be initiating
879		 * I/O.  See the comments in kern/vfs_bio.c's getblk() for
880		 * more information.
881		 *
882		 * B_CACHE may also be set due to the buffer being cached
883		 * normally.
884		 */
885
886		if (on == 0 && n == bcount) {
887			bp->b_flags |= B_CACHE;
888			bp->b_flags &= ~B_INVAL;
889			bp->b_ioflags &= ~BIO_ERROR;
890		}
891
892		if ((bp->b_flags & B_CACHE) == 0) {
893			bp->b_iocmd = BIO_READ;
894			vfs_busy_pages(bp, 0);
895			error = nfs_doio(vp, bp, cred, td);
896			if (error) {
897				brelse(bp);
898				break;
899			}
900		}
901		if (!bp) {
902			error = nfs_sigintr(nmp, NULL, td);
903			if (!error)
904				error = EINTR;
905			break;
906		}
907		if (bp->b_wcred == NOCRED)
908			bp->b_wcred = crhold(cred);
909		np->n_flag |= NMODIFIED;
910
911		/*
912		 * If dirtyend exceeds file size, chop it down.  This should
913		 * not normally occur but there is an append race where it
914		 * might occur XXX, so we log it.
915		 *
916		 * If the chopping creates a reverse-indexed or degenerate
917		 * situation with dirtyoff/end, we 0 both of them.
918		 */
919
920		if (bp->b_dirtyend > bcount) {
921			printf("NFS append race @%lx:%d\n",
922			    (long)bp->b_blkno * DEV_BSIZE,
923			    bp->b_dirtyend - bcount);
924			bp->b_dirtyend = bcount;
925		}
926
927		if (bp->b_dirtyoff >= bp->b_dirtyend)
928			bp->b_dirtyoff = bp->b_dirtyend = 0;
929
930		/*
931		 * If the new write will leave a contiguous dirty
932		 * area, just update the b_dirtyoff and b_dirtyend,
933		 * otherwise force a write rpc of the old dirty area.
934		 *
935		 * While it is possible to merge discontiguous writes due to
936		 * our having a B_CACHE buffer ( and thus valid read data
937		 * for the hole), we don't because it could lead to
938		 * significant cache coherency problems with multiple clients,
939		 * especially if locking is implemented later on.
940		 *
941		 * as an optimization we could theoretically maintain
942		 * a linked list of discontinuous areas, but we would still
943		 * have to commit them separately so there isn't much
944		 * advantage to it except perhaps a bit of asynchronization.
945		 */
946
947		if (bp->b_dirtyend > 0 &&
948		    (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
949			if (bwrite(bp) == EINTR) {
950				error = EINTR;
951				break;
952			}
953			goto again;
954		}
955
956		error = uiomove((char *)bp->b_data + on, n, uio);
957
958		/*
959		 * Since this block is being modified, it must be written
960		 * again and not just committed.  Since write clustering does
961		 * not work for the stage 1 data write, only the stage 2
962		 * commit rpc, we have to clear B_CLUSTEROK as well.
963		 */
964		bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
965
966		if (error) {
967			bp->b_ioflags |= BIO_ERROR;
968			brelse(bp);
969			break;
970		}
971
972		/*
973		 * Only update dirtyoff/dirtyend if not a degenerate
974		 * condition.
975		 */
976		if (n) {
977			if (bp->b_dirtyend > 0) {
978				bp->b_dirtyoff = min(on, bp->b_dirtyoff);
979				bp->b_dirtyend = max((on + n), bp->b_dirtyend);
980			} else {
981				bp->b_dirtyoff = on;
982				bp->b_dirtyend = on + n;
983			}
984			vfs_bio_set_validclean(bp, on, n);
985		}
986
987		/*
988		 * If IO_SYNC do bwrite().
989		 *
990		 * IO_INVAL appears to be unused.  The idea appears to be
991		 * to turn off caching in this case.  Very odd.  XXX
992		 */
993		if ((ioflag & IO_SYNC)) {
994			if (ioflag & IO_INVAL)
995				bp->b_flags |= B_NOCACHE;
996			error = bwrite(bp);
997			if (error)
998				break;
999		} else if ((n + on) == biosize) {
1000			bp->b_flags |= B_ASYNC;
1001			(void) (nmp->nm_rpcops->nr_writebp)(bp, 0, 0);
1002		} else {
1003			bdwrite(bp);
1004		}
1005	} while (uio->uio_resid > 0 && n > 0);
1006
1007	if (haverslock)
1008		nfs_rsunlock(np, td);
1009
1010	return (error);
1011}
1012
1013/*
1014 * Get an nfs cache block.
1015 *
1016 * Allocate a new one if the block isn't currently in the cache
1017 * and return the block marked busy. If the calling process is
1018 * interrupted by a signal for an interruptible mount point, return
1019 * NULL.
1020 *
1021 * The caller must carefully deal with the possible B_INVAL state of
1022 * the buffer.  nfs_doio() clears B_INVAL (and nfs_asyncio() clears it
1023 * indirectly), so synchronous reads can be issued without worrying about
1024 * the B_INVAL state.  We have to be a little more careful when dealing
1025 * with writes (see comments in nfs_write()) when extending a file past
1026 * its EOF.
1027 */
1028static struct buf *
1029nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, struct thread *td)
1030{
1031	struct buf *bp;
1032	struct mount *mp;
1033	struct nfsmount *nmp;
1034
1035	mp = vp->v_mount;
1036	nmp = VFSTONFS(mp);
1037
1038	if (nmp->nm_flag & NFSMNT_INT) {
1039		bp = getblk(vp, bn, size, PCATCH, 0, 0);
1040		while (bp == NULL) {
1041			if (nfs_sigintr(nmp, NULL, td))
1042				return (NULL);
1043			bp = getblk(vp, bn, size, 0, 2 * hz, 0);
1044		}
1045	} else {
1046		bp = getblk(vp, bn, size, 0, 0, 0);
1047	}
1048
1049	if (vp->v_type == VREG) {
1050		int biosize;
1051
1052		biosize = mp->mnt_stat.f_iosize;
1053		bp->b_blkno = bn * (biosize / DEV_BSIZE);
1054	}
1055	return (bp);
1056}
1057
1058/*
1059 * Flush and invalidate all dirty buffers. If another process is already
1060 * doing the flush, just wait for completion.
1061 */
1062int
1063nfs_vinvalbuf(struct vnode *vp, int flags, struct ucred *cred,
1064    struct thread *td, int intrflg)
1065{
1066	struct nfsnode *np = VTONFS(vp);
1067	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1068	int error = 0, slpflag, slptimeo;
1069
1070	ASSERT_VOP_LOCKED(vp, "nfs_vinvalbuf");
1071
1072	/*
1073	 * XXX This check stops us from needlessly doing a vinvalbuf when
1074	 * being called through vclean().  It is not clear that this is
1075	 * unsafe.
1076	 */
1077	if (vp->v_iflag & VI_XLOCK)
1078		return (0);
1079
1080	if ((nmp->nm_flag & NFSMNT_INT) == 0)
1081		intrflg = 0;
1082	if (intrflg) {
1083		slpflag = PCATCH;
1084		slptimeo = 2 * hz;
1085	} else {
1086		slpflag = 0;
1087		slptimeo = 0;
1088	}
1089	/*
1090	 * First wait for any other process doing a flush to complete.
1091	 */
1092	while (np->n_flag & NFLUSHINPROG) {
1093		np->n_flag |= NFLUSHWANT;
1094		error = tsleep(&np->n_flag, PRIBIO + 2, "nfsvinval",
1095			slptimeo);
1096		if (error && intrflg &&
1097		    nfs_sigintr(nmp, NULL, td))
1098			return (EINTR);
1099	}
1100
1101	/*
1102	 * Now, flush as required.
1103	 */
1104	np->n_flag |= NFLUSHINPROG;
1105	error = vinvalbuf(vp, flags, cred, td, slpflag, 0);
1106	while (error) {
1107		if (intrflg && (error = nfs_sigintr(nmp, NULL, td))) {
1108			np->n_flag &= ~NFLUSHINPROG;
1109			if (np->n_flag & NFLUSHWANT) {
1110				np->n_flag &= ~NFLUSHWANT;
1111				wakeup(&np->n_flag);
1112			}
1113			return (error);
1114		}
1115		error = vinvalbuf(vp, flags, cred, td, 0, slptimeo);
1116	}
1117	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
1118	if (np->n_flag & NFLUSHWANT) {
1119		np->n_flag &= ~NFLUSHWANT;
1120		wakeup(&np->n_flag);
1121	}
1122	return (0);
1123}
1124
1125/*
1126 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
1127 * This is mainly to avoid queueing async I/O requests when the nfsiods
1128 * are all hung on a dead server.
1129 *
1130 * Note: nfs_asyncio() does not clear (BIO_ERROR|B_INVAL) but when the bp
1131 * is eventually dequeued by the async daemon, nfs_doio() *will*.
1132 */
1133int
1134nfs_asyncio(struct nfsmount *nmp, struct buf *bp, struct ucred *cred, struct thread *td)
1135{
1136	int iod;
1137	int gotiod;
1138	int slpflag = 0;
1139	int slptimeo = 0;
1140	int error, error2;
1141
1142	/*
1143	 * Commits are usually short and sweet so lets save some cpu and
1144	 * leave the async daemons for more important rpc's (such as reads
1145	 * and writes).
1146	 */
1147	if (bp->b_iocmd == BIO_WRITE && (bp->b_flags & B_NEEDCOMMIT) &&
1148	    (nmp->nm_bufqiods > nfs_numasync / 2)) {
1149		return(EIO);
1150	}
1151
1152again:
1153	if (nmp->nm_flag & NFSMNT_INT)
1154		slpflag = PCATCH;
1155	gotiod = FALSE;
1156
1157	/*
1158	 * Find a free iod to process this request.
1159	 */
1160	for (iod = 0; iod < nfs_numasync; iod++)
1161		if (nfs_iodwant[iod]) {
1162			gotiod = TRUE;
1163			break;
1164		}
1165
1166	/*
1167	 * Try to create one if none are free.
1168	 */
1169	if (!gotiod) {
1170		iod = nfs_nfsiodnew();
1171		if (iod != -1)
1172			gotiod = TRUE;
1173	}
1174
1175	if (gotiod) {
1176		/*
1177		 * Found one, so wake it up and tell it which
1178		 * mount to process.
1179		 */
1180		NFS_DPF(ASYNCIO, ("nfs_asyncio: waking iod %d for mount %p\n",
1181		    iod, nmp));
1182		nfs_iodwant[iod] = NULL;
1183		nfs_iodmount[iod] = nmp;
1184		nmp->nm_bufqiods++;
1185		wakeup(&nfs_iodwant[iod]);
1186	}
1187
1188	/*
1189	 * If none are free, we may already have an iod working on this mount
1190	 * point.  If so, it will process our request.
1191	 */
1192	if (!gotiod) {
1193		if (nmp->nm_bufqiods > 0) {
1194			NFS_DPF(ASYNCIO,
1195				("nfs_asyncio: %d iods are already processing mount %p\n",
1196				 nmp->nm_bufqiods, nmp));
1197			gotiod = TRUE;
1198		}
1199	}
1200
1201	/*
1202	 * If we have an iod which can process the request, then queue
1203	 * the buffer.
1204	 */
1205	if (gotiod) {
1206		/*
1207		 * Ensure that the queue never grows too large.  We still want
1208		 * to asynchronize so we block rather then return EIO.
1209		 */
1210		while (nmp->nm_bufqlen >= 2*nfs_numasync) {
1211			NFS_DPF(ASYNCIO,
1212				("nfs_asyncio: waiting for mount %p queue to drain\n", nmp));
1213			nmp->nm_bufqwant = TRUE;
1214			error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
1215				       "nfsaio", slptimeo);
1216			if (error) {
1217				error2 = nfs_sigintr(nmp, NULL, td);
1218				if (error2)
1219					return (error2);
1220				if (slpflag == PCATCH) {
1221					slpflag = 0;
1222					slptimeo = 2 * hz;
1223				}
1224			}
1225			/*
1226			 * We might have lost our iod while sleeping,
1227			 * so check and loop if nescessary.
1228			 */
1229			if (nmp->nm_bufqiods == 0) {
1230				NFS_DPF(ASYNCIO,
1231					("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
1232				goto again;
1233			}
1234		}
1235
1236		if (bp->b_iocmd == BIO_READ) {
1237			if (bp->b_rcred == NOCRED && cred != NOCRED)
1238				bp->b_rcred = crhold(cred);
1239		} else {
1240			if (bp->b_wcred == NOCRED && cred != NOCRED)
1241				bp->b_wcred = crhold(cred);
1242		}
1243
1244		if (bp->b_flags & B_REMFREE)
1245			bremfreef(bp);
1246		BUF_KERNPROC(bp);
1247		TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
1248		nmp->nm_bufqlen++;
1249		return (0);
1250	}
1251
1252	/*
1253	 * All the iods are busy on other mounts, so return EIO to
1254	 * force the caller to process the i/o synchronously.
1255	 */
1256	NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n"));
1257	return (EIO);
1258}
1259
1260/*
1261 * Do an I/O operation to/from a cache block. This may be called
1262 * synchronously or from an nfsiod.
1263 */
1264int
1265nfs_doio(struct vnode *vp, struct buf *bp, struct ucred *cr, struct thread *td)
1266{
1267	struct uio *uiop;
1268	struct nfsnode *np;
1269	struct nfsmount *nmp;
1270	int error = 0, iomode, must_commit = 0;
1271	struct uio uio;
1272	struct iovec io;
1273	struct proc *p = td ? td->td_proc : NULL;
1274
1275	np = VTONFS(vp);
1276	nmp = VFSTONFS(vp->v_mount);
1277	uiop = &uio;
1278	uiop->uio_iov = &io;
1279	uiop->uio_iovcnt = 1;
1280	uiop->uio_segflg = UIO_SYSSPACE;
1281	uiop->uio_td = td;
1282
1283	/*
1284	 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O.  We
1285	 * do this here so we do not have to do it in all the code that
1286	 * calls us.
1287	 */
1288	bp->b_flags &= ~B_INVAL;
1289	bp->b_ioflags &= ~BIO_ERROR;
1290
1291	KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp));
1292
1293	if (bp->b_iocmd == BIO_READ) {
1294	    io.iov_len = uiop->uio_resid = bp->b_bcount;
1295	    io.iov_base = bp->b_data;
1296	    uiop->uio_rw = UIO_READ;
1297
1298	    switch (vp->v_type) {
1299	    case VREG:
1300		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1301		nfsstats.read_bios++;
1302		error = (nmp->nm_rpcops->nr_readrpc)(vp, uiop, cr);
1303
1304		if (!error) {
1305		    if (uiop->uio_resid) {
1306			/*
1307			 * If we had a short read with no error, we must have
1308			 * hit a file hole.  We should zero-fill the remainder.
1309			 * This can also occur if the server hits the file EOF.
1310			 *
1311			 * Holes used to be able to occur due to pending
1312			 * writes, but that is not possible any longer.
1313			 */
1314			int nread = bp->b_bcount - uiop->uio_resid;
1315			int left  = uiop->uio_resid;
1316
1317			if (left > 0)
1318				bzero((char *)bp->b_data + nread, left);
1319			uiop->uio_resid = 0;
1320		    }
1321		}
1322		/* ASSERT_VOP_LOCKED(vp, "nfs_doio"); */
1323		if (p && (vp->v_vflag & VV_TEXT) &&
1324			(np->n_mtime != np->n_vattr.va_mtime.tv_sec)) {
1325			PROC_LOCK(p);
1326			killproc(p, "text file modification");
1327			PROC_UNLOCK(p);
1328		}
1329		break;
1330	    case VLNK:
1331		uiop->uio_offset = (off_t)0;
1332		nfsstats.readlink_bios++;
1333		error = (nmp->nm_rpcops->nr_readlinkrpc)(vp, uiop, cr);
1334		break;
1335	    case VDIR:
1336		nfsstats.readdir_bios++;
1337		uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
1338		if ((nmp->nm_flag & NFSMNT_NFSV4) != 0)
1339			error = nfs4_readdirrpc(vp, uiop, cr);
1340		else {
1341			if ((nmp->nm_flag & NFSMNT_RDIRPLUS) != 0) {
1342				error = nfs_readdirplusrpc(vp, uiop, cr);
1343				if (error == NFSERR_NOTSUPP)
1344					nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1345			}
1346			if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1347				error = nfs_readdirrpc(vp, uiop, cr);
1348		}
1349		/*
1350		 * end-of-directory sets B_INVAL but does not generate an
1351		 * error.
1352		 */
1353		if (error == 0 && uiop->uio_resid == bp->b_bcount)
1354			bp->b_flags |= B_INVAL;
1355		break;
1356	    default:
1357		printf("nfs_doio:  type %x unexpected\n", vp->v_type);
1358		break;
1359	    };
1360	    if (error) {
1361		bp->b_ioflags |= BIO_ERROR;
1362		bp->b_error = error;
1363	    }
1364	} else {
1365	    /*
1366	     * If we only need to commit, try to commit
1367	     */
1368	    if (bp->b_flags & B_NEEDCOMMIT) {
1369		    int retv;
1370		    off_t off;
1371
1372		    off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
1373		    retv = (nmp->nm_rpcops->nr_commit)(
1374				vp, off, bp->b_dirtyend-bp->b_dirtyoff,
1375				bp->b_wcred, td);
1376		    if (retv == 0) {
1377			    bp->b_dirtyoff = bp->b_dirtyend = 0;
1378			    bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1379			    bp->b_resid = 0;
1380			    bufdone(bp);
1381			    return (0);
1382		    }
1383		    if (retv == NFSERR_STALEWRITEVERF) {
1384			    nfs_clearcommit(vp->v_mount);
1385		    }
1386	    }
1387
1388	    /*
1389	     * Setup for actual write
1390	     */
1391
1392	    if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
1393		bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
1394
1395	    if (bp->b_dirtyend > bp->b_dirtyoff) {
1396		io.iov_len = uiop->uio_resid = bp->b_dirtyend
1397		    - bp->b_dirtyoff;
1398		uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE
1399		    + bp->b_dirtyoff;
1400		io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1401		uiop->uio_rw = UIO_WRITE;
1402		nfsstats.write_bios++;
1403
1404		if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC)
1405		    iomode = NFSV3WRITE_UNSTABLE;
1406		else
1407		    iomode = NFSV3WRITE_FILESYNC;
1408
1409		error = (nmp->nm_rpcops->nr_writerpc)(vp, uiop, cr, &iomode, &must_commit);
1410
1411		/*
1412		 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
1413		 * to cluster the buffers needing commit.  This will allow
1414		 * the system to submit a single commit rpc for the whole
1415		 * cluster.  We can do this even if the buffer is not 100%
1416		 * dirty (relative to the NFS blocksize), so we optimize the
1417		 * append-to-file-case.
1418		 *
1419		 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
1420		 * cleared because write clustering only works for commit
1421		 * rpc's, not for the data portion of the write).
1422		 */
1423
1424		if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1425		    bp->b_flags |= B_NEEDCOMMIT;
1426		    if (bp->b_dirtyoff == 0
1427			&& bp->b_dirtyend == bp->b_bcount)
1428			bp->b_flags |= B_CLUSTEROK;
1429		} else {
1430		    bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1431		}
1432
1433		/*
1434		 * For an interrupted write, the buffer is still valid
1435		 * and the write hasn't been pushed to the server yet,
1436		 * so we can't set BIO_ERROR and report the interruption
1437		 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
1438		 * is not relevant, so the rpc attempt is essentially
1439		 * a noop.  For the case of a V3 write rpc not being
1440		 * committed to stable storage, the block is still
1441		 * dirty and requires either a commit rpc or another
1442		 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1443		 * the block is reused. This is indicated by setting
1444		 * the B_DELWRI and B_NEEDCOMMIT flags.
1445		 *
1446		 * If the buffer is marked B_PAGING, it does not reside on
1447		 * the vp's paging queues so we cannot call bdirty().  The
1448		 * bp in this case is not an NFS cache block so we should
1449		 * be safe. XXX
1450		 */
1451    		if (error == EINTR || error == EIO
1452		    || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1453			int s;
1454
1455			s = splbio();
1456			bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1457			if ((bp->b_flags & B_PAGING) == 0) {
1458			    bdirty(bp);
1459			    bp->b_flags &= ~B_DONE;
1460			}
1461			if (error && (bp->b_flags & B_ASYNC) == 0)
1462			    bp->b_flags |= B_EINTR;
1463			splx(s);
1464	    	} else {
1465		    if (error) {
1466			bp->b_ioflags |= BIO_ERROR;
1467			bp->b_error = np->n_error = error;
1468			np->n_flag |= NWRITEERR;
1469		    }
1470		    bp->b_dirtyoff = bp->b_dirtyend = 0;
1471		}
1472	    } else {
1473		bp->b_resid = 0;
1474		bufdone(bp);
1475		return (0);
1476	    }
1477	}
1478	bp->b_resid = uiop->uio_resid;
1479	if (must_commit)
1480	    nfs_clearcommit(vp->v_mount);
1481	bufdone(bp);
1482	return (error);
1483}
1484
1485/*
1486 * Used to aid in handling ftruncate() operations on the NFS client side.
1487 * Truncation creates a number of special problems for NFS.  We have to
1488 * throw away VM pages and buffer cache buffers that are beyond EOF, and
1489 * we have to properly handle VM pages or (potentially dirty) buffers
1490 * that straddle the truncation point.
1491 */
1492
1493int
1494nfs_meta_setsize(struct vnode *vp, struct ucred *cred, struct thread *td, u_quad_t nsize)
1495{
1496	struct nfsnode *np = VTONFS(vp);
1497	u_quad_t tsize = np->n_size;
1498	int biosize = vp->v_mount->mnt_stat.f_iosize;
1499	int error = 0;
1500
1501	np->n_size = nsize;
1502
1503	if (np->n_size < tsize) {
1504		struct buf *bp;
1505		daddr_t lbn;
1506		int bufsize;
1507
1508		/*
1509		 * vtruncbuf() doesn't get the buffer overlapping the
1510		 * truncation point.  We may have a B_DELWRI and/or B_CACHE
1511		 * buffer that now needs to be truncated.
1512		 */
1513		error = vtruncbuf(vp, cred, td, nsize, biosize);
1514		lbn = nsize / biosize;
1515		bufsize = nsize & (biosize - 1);
1516		bp = nfs_getcacheblk(vp, lbn, bufsize, td);
1517		if (bp->b_dirtyoff > bp->b_bcount)
1518			bp->b_dirtyoff = bp->b_bcount;
1519		if (bp->b_dirtyend > bp->b_bcount)
1520			bp->b_dirtyend = bp->b_bcount;
1521		bp->b_flags |= B_RELBUF;  /* don't leave garbage around */
1522		brelse(bp);
1523	} else {
1524		vnode_pager_setsize(vp, nsize);
1525	}
1526	return(error);
1527}
1528
1529