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