nfs_bio.c revision 54480
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from software contributed to Berkeley by
61541Srgrimes * Rick Macklem at The University of Guelph.
71541Srgrimes *
81541Srgrimes * Redistribution and use in source and binary forms, with or without
91541Srgrimes * modification, are permitted provided that the following conditions
101541Srgrimes * are met:
111541Srgrimes * 1. Redistributions of source code must retain the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer.
131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer in the
151541Srgrimes *    documentation and/or other materials provided with the distribution.
161541Srgrimes * 3. All advertising materials mentioning features or use of this software
171541Srgrimes *    must display the following acknowledgement:
181541Srgrimes *	This product includes software developed by the University of
191541Srgrimes *	California, Berkeley and its contributors.
201541Srgrimes * 4. Neither the name of the University nor the names of its contributors
211541Srgrimes *    may be used to endorse or promote products derived from this software
221541Srgrimes *    without specific prior written permission.
231541Srgrimes *
241541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341541Srgrimes * SUCH DAMAGE.
351541Srgrimes *
3622521Sdyson *	@(#)nfs_bio.c	8.9 (Berkeley) 3/30/95
3750477Speter * $FreeBSD: head/sys/nfsclient/nfs_bio.c 54480 1999-12-12 06:09:57Z dillon $
381541Srgrimes */
391541Srgrimes
4022521Sdyson
411541Srgrimes#include <sys/param.h>
421541Srgrimes#include <sys/systm.h>
431541Srgrimes#include <sys/resourcevar.h>
443305Sphk#include <sys/signalvar.h>
451541Srgrimes#include <sys/proc.h>
461541Srgrimes#include <sys/buf.h>
471541Srgrimes#include <sys/vnode.h>
481541Srgrimes#include <sys/mount.h>
491541Srgrimes#include <sys/kernel.h>
501541Srgrimes
511541Srgrimes#include <vm/vm.h>
5212662Sdg#include <vm/vm_extern.h>
5325930Sdfr#include <vm/vm_page.h>
5425930Sdfr#include <vm/vm_object.h>
5525930Sdfr#include <vm/vm_pager.h>
5625930Sdfr#include <vm/vnode_pager.h>
571541Srgrimes
581541Srgrimes#include <nfs/rpcv2.h>
599336Sdfr#include <nfs/nfsproto.h>
601541Srgrimes#include <nfs/nfs.h>
611541Srgrimes#include <nfs/nfsmount.h>
621541Srgrimes#include <nfs/nqnfs.h>
639336Sdfr#include <nfs/nfsnode.h>
641541Srgrimes
6512911Sphkstatic struct buf *nfs_getcacheblk __P((struct vnode *vp, daddr_t bn, int size,
6612588Sbde					struct proc *p));
6712588Sbde
681541Srgrimesextern int nfs_numasync;
6942957Sdillonextern int nfs_pbuf_freecnt;
709336Sdfrextern struct nfsstats nfsstats;
711541Srgrimes
721541Srgrimes/*
7325930Sdfr * Vnode op for VM getpages.
7425930Sdfr */
7525930Sdfrint
7625930Sdfrnfs_getpages(ap)
7736563Speter	struct vop_getpages_args /* {
7836563Speter		struct vnode *a_vp;
7936563Speter		vm_page_t *a_m;
8036563Speter		int a_count;
8136563Speter		int a_reqpage;
8236563Speter		vm_ooffset_t a_offset;
8336563Speter	} */ *ap;
8425930Sdfr{
8546349Salc	int i, error, nextoff, size, toff, count, npages;
8632755Sdyson	struct uio uio;
8732755Sdyson	struct iovec iov;
8832755Sdyson	vm_offset_t kva;
8934206Sdyson	struct buf *bp;
9036563Speter	struct vnode *vp;
9136563Speter	struct proc *p;
9236563Speter	struct ucred *cred;
9336563Speter	struct nfsmount *nmp;
9436563Speter	vm_page_t *pages;
9525930Sdfr
9636563Speter	vp = ap->a_vp;
9736563Speter	p = curproc;				/* XXX */
9836563Speter	cred = curproc->p_ucred;		/* XXX */
9936563Speter	nmp = VFSTONFS(vp->v_mount);
10036563Speter	pages = ap->a_m;
10136563Speter	count = ap->a_count;
10236563Speter
10336563Speter	if (vp->v_object == NULL) {
10432286Sdyson		printf("nfs_getpages: called with non-merged cache vnode??\n");
10536563Speter		return VM_PAGER_ERROR;
10625930Sdfr	}
10725930Sdfr
10836563Speter	if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
10936563Speter	    (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
11036563Speter		(void)nfs_fsinfo(nmp, vp, cred, p);
11146349Salc
11246349Salc	npages = btoc(count);
11346349Salc
11434206Sdyson	/*
11546349Salc	 * If the requested page is partially valid, just return it and
11646349Salc	 * allow the pager to zero-out the blanks.  Partially valid pages
11746349Salc	 * can only occur at the file EOF.
11846349Salc	 */
11946349Salc
12046349Salc	{
12146349Salc		vm_page_t m = pages[ap->a_reqpage];
12246349Salc
12346349Salc		if (m->valid != 0) {
12446349Salc			/* handled by vm_fault now	  */
12546349Salc			/* vm_page_zero_invalid(m, TRUE); */
12646349Salc			for (i = 0; i < npages; ++i) {
12746349Salc				if (i != ap->a_reqpage)
12846349Salc					vnode_pager_freepage(pages[i]);
12946349Salc			}
13046349Salc			return(0);
13146349Salc		}
13246349Salc	}
13346349Salc
13446349Salc	/*
13534206Sdyson	 * We use only the kva address for the buffer, but this is extremely
13634206Sdyson	 * convienient and fast.
13734206Sdyson	 */
13842957Sdillon	bp = getpbuf(&nfs_pbuf_freecnt);
13925930Sdfr
14034206Sdyson	kva = (vm_offset_t) bp->b_data;
14136563Speter	pmap_qenter(kva, pages, npages);
14234206Sdyson
14332755Sdyson	iov.iov_base = (caddr_t) kva;
14436563Speter	iov.iov_len = count;
14532755Sdyson	uio.uio_iov = &iov;
14632755Sdyson	uio.uio_iovcnt = 1;
14736563Speter	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
14836563Speter	uio.uio_resid = count;
14932755Sdyson	uio.uio_segflg = UIO_SYSSPACE;
15032755Sdyson	uio.uio_rw = UIO_READ;
15136563Speter	uio.uio_procp = p;
15225930Sdfr
15336563Speter	error = nfs_readrpc(vp, &uio, cred);
15434206Sdyson	pmap_qremove(kva, npages);
15532755Sdyson
15642957Sdillon	relpbuf(bp, &nfs_pbuf_freecnt);
15734206Sdyson
15842957Sdillon	if (error && (uio.uio_resid == count)) {
15942957Sdillon		printf("nfs_getpages: error %d\n", error);
16042957Sdillon		for (i = 0; i < npages; ++i) {
16142957Sdillon			if (i != ap->a_reqpage)
16242957Sdillon				vnode_pager_freepage(pages[i]);
16342957Sdillon		}
16434206Sdyson		return VM_PAGER_ERROR;
16542957Sdillon	}
16634206Sdyson
16745347Sjulian	/*
16845347Sjulian	 * Calculate the number of bytes read and validate only that number
16945347Sjulian	 * of bytes.  Note that due to pending writes, size may be 0.  This
17045347Sjulian	 * does not mean that the remaining data is invalid!
17145347Sjulian	 */
17245347Sjulian
17336563Speter	size = count - uio.uio_resid;
17434206Sdyson
17534206Sdyson	for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
17634206Sdyson		vm_page_t m;
17734206Sdyson		nextoff = toff + PAGE_SIZE;
17836563Speter		m = pages[i];
17934206Sdyson
18034206Sdyson		m->flags &= ~PG_ZERO;
18134206Sdyson
18234206Sdyson		if (nextoff <= size) {
18345347Sjulian			/*
18445347Sjulian			 * Read operation filled an entire page
18545347Sjulian			 */
18634206Sdyson			m->valid = VM_PAGE_BITS_ALL;
18749945Salc			vm_page_undirty(m);
18845347Sjulian		} else if (size > toff) {
18945347Sjulian			/*
19046349Salc			 * Read operation filled a partial page.
19145347Sjulian			 */
19246349Salc			m->valid = 0;
19345347Sjulian			vm_page_set_validclean(m, 0, size - toff);
19446349Salc			/* handled by vm_fault now	  */
19546349Salc			/* vm_page_zero_invalid(m, TRUE); */
19634206Sdyson		}
19734206Sdyson
19825930Sdfr		if (i != ap->a_reqpage) {
19934206Sdyson			/*
20034206Sdyson			 * Whether or not to leave the page activated is up in
20134206Sdyson			 * the air, but we should put the page on a page queue
20234206Sdyson			 * somewhere (it already is in the object).  Result:
20334206Sdyson			 * It appears that emperical results show that
20434206Sdyson			 * deactivating pages is best.
20534206Sdyson			 */
20634206Sdyson
20734206Sdyson			/*
20834206Sdyson			 * Just in case someone was asking for this page we
20934206Sdyson			 * now tell them that it is ok to use.
21034206Sdyson			 */
21134206Sdyson			if (!error) {
21234206Sdyson				if (m->flags & PG_WANTED)
21334206Sdyson					vm_page_activate(m);
21434206Sdyson				else
21534206Sdyson					vm_page_deactivate(m);
21638799Sdfr				vm_page_wakeup(m);
21734206Sdyson			} else {
21834206Sdyson				vnode_pager_freepage(m);
21934206Sdyson			}
22025930Sdfr		}
22125930Sdfr	}
22225930Sdfr	return 0;
22325930Sdfr}
22425930Sdfr
22525930Sdfr/*
22634206Sdyson * Vnode op for VM putpages.
22734096Smsmith */
22834096Smsmithint
22934096Smsmithnfs_putpages(ap)
23036563Speter	struct vop_putpages_args /* {
23136563Speter		struct vnode *a_vp;
23236563Speter		vm_page_t *a_m;
23336563Speter		int a_count;
23436563Speter		int a_sync;
23536563Speter		int *a_rtvals;
23636563Speter		vm_ooffset_t a_offset;
23736563Speter	} */ *ap;
23834096Smsmith{
23934206Sdyson	struct uio uio;
24034206Sdyson	struct iovec iov;
24134206Sdyson	vm_offset_t kva;
24234206Sdyson	struct buf *bp;
24336563Speter	int iomode, must_commit, i, error, npages, count;
24446349Salc	off_t offset;
24534206Sdyson	int *rtvals;
24636563Speter	struct vnode *vp;
24736563Speter	struct proc *p;
24836563Speter	struct ucred *cred;
24936563Speter	struct nfsmount *nmp;
25046349Salc	struct nfsnode *np;
25136563Speter	vm_page_t *pages;
25234206Sdyson
25336563Speter	vp = ap->a_vp;
25446349Salc	np = VTONFS(vp);
25536563Speter	p = curproc;				/* XXX */
25636563Speter	cred = curproc->p_ucred;		/* XXX */
25736563Speter	nmp = VFSTONFS(vp->v_mount);
25836563Speter	pages = ap->a_m;
25936563Speter	count = ap->a_count;
26034206Sdyson	rtvals = ap->a_rtvals;
26136563Speter	npages = btoc(count);
26246349Salc	offset = IDX_TO_OFF(pages[0]->pindex);
26334206Sdyson
26436563Speter	if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
26536563Speter	    (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
26636563Speter		(void)nfs_fsinfo(nmp, vp, cred, p);
26734206Sdyson
26834206Sdyson	for (i = 0; i < npages; i++) {
26934206Sdyson		rtvals[i] = VM_PAGER_AGAIN;
27034206Sdyson	}
27134206Sdyson
27234206Sdyson	/*
27346349Salc	 * When putting pages, do not extend file past EOF.
27446349Salc	 */
27546349Salc
27646349Salc	if (offset + count > np->n_size) {
27746349Salc		count = np->n_size - offset;
27846349Salc		if (count < 0)
27946349Salc			count = 0;
28046349Salc	}
28146349Salc
28246349Salc	/*
28334206Sdyson	 * We use only the kva address for the buffer, but this is extremely
28434206Sdyson	 * convienient and fast.
28534206Sdyson	 */
28642957Sdillon	bp = getpbuf(&nfs_pbuf_freecnt);
28734206Sdyson
28834206Sdyson	kva = (vm_offset_t) bp->b_data;
28936563Speter	pmap_qenter(kva, pages, npages);
29034206Sdyson
29134206Sdyson	iov.iov_base = (caddr_t) kva;
29236563Speter	iov.iov_len = count;
29334206Sdyson	uio.uio_iov = &iov;
29434206Sdyson	uio.uio_iovcnt = 1;
29546349Salc	uio.uio_offset = offset;
29636563Speter	uio.uio_resid = count;
29734206Sdyson	uio.uio_segflg = UIO_SYSSPACE;
29834206Sdyson	uio.uio_rw = UIO_WRITE;
29936563Speter	uio.uio_procp = p;
30034206Sdyson
30134206Sdyson	if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
30234206Sdyson	    iomode = NFSV3WRITE_UNSTABLE;
30334206Sdyson	else
30434206Sdyson	    iomode = NFSV3WRITE_FILESYNC;
30534206Sdyson
30636563Speter	error = nfs_writerpc(vp, &uio, cred, &iomode, &must_commit);
30734206Sdyson
30834206Sdyson	pmap_qremove(kva, npages);
30942957Sdillon	relpbuf(bp, &nfs_pbuf_freecnt);
31034206Sdyson
31134206Sdyson	if (!error) {
31236563Speter		int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
31334206Sdyson		for (i = 0; i < nwritten; i++) {
31434206Sdyson			rtvals[i] = VM_PAGER_OK;
31549945Salc			vm_page_undirty(pages[i]);
31634206Sdyson		}
31734206Sdyson		if (must_commit)
31836563Speter			nfs_clearcommit(vp->v_mount);
31934206Sdyson	}
32036563Speter	return rtvals[0];
32134096Smsmith}
32234096Smsmith
32334096Smsmith/*
3241541Srgrimes * Vnode op for read using bio
3251541Srgrimes */
3261549Srgrimesint
32746349Salcnfs_bioread(vp, uio, ioflag, cred)
3281541Srgrimes	register struct vnode *vp;
3291541Srgrimes	register struct uio *uio;
3301541Srgrimes	int ioflag;
3311541Srgrimes	struct ucred *cred;
3321541Srgrimes{
3331541Srgrimes	register struct nfsnode *np = VTONFS(vp);
33441791Sdt	register int biosize, i;
3351549Srgrimes	struct buf *bp = 0, *rabp;
3361541Srgrimes	struct vattr vattr;
3371541Srgrimes	struct proc *p;
3389336Sdfr	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3395455Sdg	daddr_t lbn, rabn;
34046349Salc	int bcount;
34151344Sdillon	int seqcount;
34246349Salc	int nra, error = 0, n = 0, on = 0;
3431541Srgrimes
3441541Srgrimes#ifdef DIAGNOSTIC
3451541Srgrimes	if (uio->uio_rw != UIO_READ)
3461541Srgrimes		panic("nfs_read mode");
3471541Srgrimes#endif
3481541Srgrimes	if (uio->uio_resid == 0)
3491541Srgrimes		return (0);
35036473Speter	if (uio->uio_offset < 0)	/* XXX VDIR cookies can be negative */
3511541Srgrimes		return (EINVAL);
3521541Srgrimes	p = uio->uio_procp;
35351344Sdillon
35436176Speter	if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
35536176Speter	    (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
3569336Sdfr		(void)nfs_fsinfo(nmp, vp, cred, p);
35736473Speter	if (vp->v_type != VDIR &&
35836473Speter	    (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
35936473Speter		return (EFBIG);
3609428Sdfr	biosize = vp->v_mount->mnt_stat.f_iosize;
36151344Sdillon	seqcount = (int)((off_t)(ioflag >> 16) * biosize / BKVASIZE);
3621541Srgrimes	/*
3631541Srgrimes	 * For nfs, cache consistency can only be maintained approximately.
3641541Srgrimes	 * Although RFC1094 does not specify the criteria, the following is
3651541Srgrimes	 * believed to be compatible with the reference port.
3661541Srgrimes	 * For nqnfs, full cache consistency is maintained within the loop.
3671541Srgrimes	 * For nfs:
3681541Srgrimes	 * If the file's modify time on the server has changed since the
3691541Srgrimes	 * last read rpc or you have written to the file,
3701541Srgrimes	 * you may have lost data cache consistency with the
3711541Srgrimes	 * server, so flush all of the file's data out of the cache.
3721541Srgrimes	 * Then force a getattr rpc to ensure that you have up to date
3731541Srgrimes	 * attributes.
3741541Srgrimes	 * NB: This implies that cache data can be read when up to
3751541Srgrimes	 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
3761541Srgrimes	 * attributes this could be forced by setting n_attrstamp to 0 before
3771541Srgrimes	 * the VOP_GETATTR() call.
3781541Srgrimes	 */
37910219Sdfr	if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) {
3801541Srgrimes		if (np->n_flag & NMODIFIED) {
3819336Sdfr			if (vp->v_type != VREG) {
3829336Sdfr				if (vp->v_type != VDIR)
3839336Sdfr					panic("nfs: bioread, not dir");
3849336Sdfr				nfs_invaldir(vp);
3853305Sphk				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
3863305Sphk				if (error)
3871541Srgrimes					return (error);
3881541Srgrimes			}
3891541Srgrimes			np->n_attrstamp = 0;
3903305Sphk			error = VOP_GETATTR(vp, &vattr, cred, p);
3913305Sphk			if (error)
3921541Srgrimes				return (error);
39318397Snate			np->n_mtime = vattr.va_mtime.tv_sec;
3941541Srgrimes		} else {
3953305Sphk			error = VOP_GETATTR(vp, &vattr, cred, p);
3963305Sphk			if (error)
3971541Srgrimes				return (error);
39818397Snate			if (np->n_mtime != vattr.va_mtime.tv_sec) {
3999336Sdfr				if (vp->v_type == VDIR)
4009336Sdfr					nfs_invaldir(vp);
4013305Sphk				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
4023305Sphk				if (error)
4031541Srgrimes					return (error);
40418397Snate				np->n_mtime = vattr.va_mtime.tv_sec;
4051541Srgrimes			}
4061541Srgrimes		}
4071541Srgrimes	}
4081541Srgrimes	do {
4091541Srgrimes
4101541Srgrimes	    /*
4111541Srgrimes	     * Get a valid lease. If cached data is stale, flush it.
4121541Srgrimes	     */
4131541Srgrimes	    if (nmp->nm_flag & NFSMNT_NQNFS) {
4149336Sdfr		if (NQNFS_CKINVALID(vp, np, ND_READ)) {
4151541Srgrimes		    do {
4169336Sdfr			error = nqnfs_getlease(vp, ND_READ, cred, p);
4171541Srgrimes		    } while (error == NQNFS_EXPIRED);
4181541Srgrimes		    if (error)
4191541Srgrimes			return (error);
4201541Srgrimes		    if (np->n_lrev != np->n_brev ||
4211541Srgrimes			(np->n_flag & NQNFSNONCACHE) ||
4221541Srgrimes			((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
4239336Sdfr			if (vp->v_type == VDIR)
4249336Sdfr			    nfs_invaldir(vp);
4253305Sphk			error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
4263305Sphk			if (error)
4271541Srgrimes			    return (error);
4281541Srgrimes			np->n_brev = np->n_lrev;
4291541Srgrimes		    }
4301541Srgrimes		} else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
4319336Sdfr		    nfs_invaldir(vp);
4323305Sphk		    error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
4333305Sphk		    if (error)
4341541Srgrimes			return (error);
4351541Srgrimes		}
4361541Srgrimes	    }
4371541Srgrimes	    if (np->n_flag & NQNFSNONCACHE) {
4381541Srgrimes		switch (vp->v_type) {
4391541Srgrimes		case VREG:
4409336Sdfr			return (nfs_readrpc(vp, uio, cred));
4411541Srgrimes		case VLNK:
4429336Sdfr			return (nfs_readlinkrpc(vp, uio, cred));
4431541Srgrimes		case VDIR:
4441541Srgrimes			break;
4453305Sphk		default:
44622521Sdyson			printf(" NQNFSNONCACHE: type %x unexpected\n",
4473305Sphk				vp->v_type);
4481541Srgrimes		};
4491541Srgrimes	    }
4501541Srgrimes	    switch (vp->v_type) {
4511541Srgrimes	    case VREG:
4521541Srgrimes		nfsstats.biocache_reads++;
4531541Srgrimes		lbn = uio->uio_offset / biosize;
4549336Sdfr		on = uio->uio_offset & (biosize - 1);
4551541Srgrimes
4561541Srgrimes		/*
4571541Srgrimes		 * Start the read ahead(s), as required.
4581541Srgrimes		 */
4599336Sdfr		if (nfs_numasync > 0 && nmp->nm_readahead > 0) {
46051344Sdillon		    for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
46113612Smpp			(off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
4625455Sdg			rabn = lbn + 1 + nra;
4631541Srgrimes			if (!incore(vp, rabn)) {
4641541Srgrimes			    rabp = nfs_getcacheblk(vp, rabn, biosize, p);
4651541Srgrimes			    if (!rabp)
4661541Srgrimes				return (EINTR);
4678692Sdg			    if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
4681541Srgrimes				rabp->b_flags |= (B_READ | B_ASYNC);
4695455Sdg				vfs_busy_pages(rabp, 0);
47046580Sphk				if (nfs_asyncio(rabp, cred, p)) {
4715455Sdg				    rabp->b_flags |= B_INVAL|B_ERROR;
4725455Sdg				    vfs_unbusy_pages(rabp);
4731541Srgrimes				    brelse(rabp);
4741541Srgrimes				}
47522521Sdyson			    } else
4765471Sdg				brelse(rabp);
4771541Srgrimes			}
4781541Srgrimes		    }
4791541Srgrimes		}
4801541Srgrimes
4811541Srgrimes		/*
48246349Salc		 * Obtain the buffer cache block.  Figure out the buffer size
48346349Salc		 * when we are at EOF.  nfs_getcacheblk() will also force
48446349Salc		 * uncached delayed-writes to be flushed to the server.
48546349Salc		 *
48646349Salc		 * Note that bcount is *not* DEV_BSIZE aligned.
4871541Srgrimes		 */
48846349Salc
48946349Salc		bcount = biosize;
49046349Salc		if ((off_t)lbn * biosize >= np->n_size) {
49146349Salc			bcount = 0;
49246349Salc		} else if ((off_t)(lbn + 1) * biosize > np->n_size) {
49346349Salc			bcount = np->n_size - (off_t)lbn * biosize;
4948692Sdg		}
49546349Salc
49646349Salc		bp = nfs_getcacheblk(vp, lbn, bcount, p);
4977871Sdg		if (!bp)
4987871Sdg			return (EINTR);
49942957Sdillon
50025930Sdfr		/*
50146349Salc		 * If B_CACHE is not set, we must issue the read.  If this
50246349Salc		 * fails, we return an error.
50325930Sdfr		 */
50446349Salc
5057871Sdg		if ((bp->b_flags & B_CACHE) == 0) {
50632755Sdyson		    bp->b_flags |= B_READ;
50732755Sdyson		    vfs_busy_pages(bp, 0);
50832755Sdyson		    error = nfs_doio(bp, cred, p);
50932755Sdyson		    if (error) {
51032755Sdyson			brelse(bp);
51132755Sdyson			return (error);
51232755Sdyson		    }
5131541Srgrimes		}
51446349Salc
51546349Salc		/*
51646349Salc		 * on is the offset into the current bp.  Figure out how many
51746349Salc		 * bytes we can copy out of the bp.  Note that bcount is
51846349Salc		 * NOT DEV_BSIZE aligned.
51946349Salc		 *
52046349Salc		 * Then figure out how many bytes we can copy into the uio.
52146349Salc		 */
52246349Salc
52346349Salc		n = 0;
52446349Salc		if (on < bcount)
52546349Salc			n = min((unsigned)(bcount - on), uio->uio_resid);
5261541Srgrimes		break;
5271541Srgrimes	    case VLNK:
5281541Srgrimes		nfsstats.biocache_readlinks++;
5291541Srgrimes		bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
5301541Srgrimes		if (!bp)
5311541Srgrimes			return (EINTR);
5327871Sdg		if ((bp->b_flags & B_CACHE) == 0) {
53332755Sdyson		    bp->b_flags |= B_READ;
53432755Sdyson		    vfs_busy_pages(bp, 0);
53532755Sdyson		    error = nfs_doio(bp, cred, p);
53632755Sdyson		    if (error) {
53732755Sdyson			bp->b_flags |= B_ERROR;
53832755Sdyson			brelse(bp);
53932755Sdyson			return (error);
54032755Sdyson		    }
5411541Srgrimes		}
5421541Srgrimes		n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
5431541Srgrimes		on = 0;
5441541Srgrimes		break;
5451541Srgrimes	    case VDIR:
5461541Srgrimes		nfsstats.biocache_readdirs++;
54724577Sdfr		if (np->n_direofoffset
54824577Sdfr		    && uio->uio_offset >= np->n_direofoffset) {
54924577Sdfr		    return (0);
55024577Sdfr		}
55136979Sbde		lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
5529336Sdfr		on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
5535455Sdg		bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, p);
5541541Srgrimes		if (!bp)
5559336Sdfr		    return (EINTR);
5567871Sdg		if ((bp->b_flags & B_CACHE) == 0) {
5579336Sdfr		    bp->b_flags |= B_READ;
5589336Sdfr		    vfs_busy_pages(bp, 0);
5599336Sdfr		    error = nfs_doio(bp, cred, p);
56032912Stegge		    if (error) {
56132912Stegge			    brelse(bp);
56232912Stegge		    }
56332755Sdyson		    while (error == NFSERR_BAD_COOKIE) {
56446349Salc			printf("got bad cookie vp %p bp %p\n", vp, bp);
56532755Sdyson			nfs_invaldir(vp);
56632755Sdyson			error = nfs_vinvalbuf(vp, 0, cred, p, 1);
56732755Sdyson			/*
56832755Sdyson			 * Yuck! The directory has been modified on the
56932755Sdyson			 * server. The only way to get the block is by
57032755Sdyson			 * reading from the beginning to get all the
57132755Sdyson			 * offset cookies.
57246349Salc			 *
57346349Salc			 * Leave the last bp intact unless there is an error.
57446349Salc			 * Loop back up to the while if the error is another
57546349Salc			 * NFSERR_BAD_COOKIE (double yuch!).
57632755Sdyson			 */
57732755Sdyson			for (i = 0; i <= lbn && !error; i++) {
57832755Sdyson			    if (np->n_direofoffset
57932755Sdyson				&& (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
58024577Sdfr				    return (0);
58132755Sdyson			    bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, p);
58232755Sdyson			    if (!bp)
58332755Sdyson				return (EINTR);
58446349Salc			    if ((bp->b_flags & B_CACHE) == 0) {
58546349Salc				    bp->b_flags |= B_READ;
58646349Salc				    vfs_busy_pages(bp, 0);
58746349Salc				    error = nfs_doio(bp, cred, p);
58846349Salc				    /*
58946349Salc				     * no error + B_INVAL == directory EOF,
59046349Salc				     * use the block.
59146349Salc				     */
59246349Salc				    if (error == 0 && (bp->b_flags & B_INVAL))
59346349Salc					    break;
59446349Salc			    }
59546349Salc			    /*
59646349Salc			     * An error will throw away the block and the
59746349Salc			     * for loop will break out.  If no error and this
59846349Salc			     * is not the block we want, we throw away the
59946349Salc			     * block and go for the next one via the for loop.
60046349Salc			     */
60146349Salc			    if (error || i < lbn)
60232755Sdyson				    brelse(bp);
6031541Srgrimes			}
60432912Stegge		    }
60546349Salc		    /*
60646349Salc		     * The above while is repeated if we hit another cookie
60746349Salc		     * error.  If we hit an error and it wasn't a cookie error,
60846349Salc		     * we give up.
60946349Salc		     */
61032912Stegge		    if (error)
6119336Sdfr			    return (error);
6121541Srgrimes		}
6131541Srgrimes
6141541Srgrimes		/*
6151541Srgrimes		 * If not eof and read aheads are enabled, start one.
6161541Srgrimes		 * (You need the current block first, so that you have the
6179336Sdfr		 *  directory offset cookie of the next block.)
6181541Srgrimes		 */
6191541Srgrimes		if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
62039782Smckusick		    (bp->b_flags & B_INVAL) == 0 &&
6219336Sdfr		    (np->n_direofoffset == 0 ||
6229336Sdfr		    (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
6239336Sdfr		    !(np->n_flag & NQNFSNONCACHE) &&
6249336Sdfr		    !incore(vp, lbn + 1)) {
6259336Sdfr			rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, p);
6261541Srgrimes			if (rabp) {
6278692Sdg			    if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
6281541Srgrimes				rabp->b_flags |= (B_READ | B_ASYNC);
6295455Sdg				vfs_busy_pages(rabp, 0);
63046580Sphk				if (nfs_asyncio(rabp, cred, p)) {
6316148Sdg				    rabp->b_flags |= B_INVAL|B_ERROR;
6325455Sdg				    vfs_unbusy_pages(rabp);
6331541Srgrimes				    brelse(rabp);
6341541Srgrimes				}
6355471Sdg			    } else {
6365471Sdg				brelse(rabp);
6371541Srgrimes			    }
6381541Srgrimes			}
6391541Srgrimes		}
64026469Sdfr		/*
64146349Salc		 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
64246349Salc		 * chopped for the EOF condition, we cannot tell how large
64346349Salc		 * NFS directories are going to be until we hit EOF.  So
64446349Salc		 * an NFS directory buffer is *not* chopped to its EOF.  Now,
64546349Salc		 * it just so happens that b_resid will effectively chop it
64646349Salc		 * to EOF.  *BUT* this information is lost if the buffer goes
64746349Salc		 * away and is reconstituted into a B_CACHE state ( due to
64846349Salc		 * being VMIO ) later.  So we keep track of the directory eof
64946349Salc		 * in np->n_direofoffset and chop it off as an extra step
65046349Salc		 * right here.
65126469Sdfr		 */
65226469Sdfr		n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
65346349Salc		if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
65446349Salc			n = np->n_direofoffset - uio->uio_offset;
6551541Srgrimes		break;
6563305Sphk	    default:
6579336Sdfr		printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
6583305Sphk		break;
6591541Srgrimes	    };
6601541Srgrimes
6611541Srgrimes	    if (n > 0) {
66234206Sdyson		    error = uiomove(bp->b_data + on, (int)n, uio);
6631541Srgrimes	    }
6641541Srgrimes	    switch (vp->v_type) {
6651541Srgrimes	    case VREG:
6661541Srgrimes		break;
6671541Srgrimes	    case VLNK:
6681541Srgrimes		n = 0;
6691541Srgrimes		break;
6701541Srgrimes	    case VDIR:
67146349Salc		/*
67246349Salc		 * Invalidate buffer if caching is disabled, forcing a
67346349Salc		 * re-read from the remote later.
67446349Salc		 */
6759336Sdfr		if (np->n_flag & NQNFSNONCACHE)
6769336Sdfr			bp->b_flags |= B_INVAL;
6771541Srgrimes		break;
6783305Sphk	    default:
6799336Sdfr		printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
6803305Sphk	    }
68132755Sdyson	    brelse(bp);
6821541Srgrimes	} while (error == 0 && uio->uio_resid > 0 && n > 0);
6831541Srgrimes	return (error);
6841541Srgrimes}
6851541Srgrimes
6861541Srgrimes/*
6871541Srgrimes * Vnode op for write using bio
6881541Srgrimes */
6891549Srgrimesint
6901541Srgrimesnfs_write(ap)
6911541Srgrimes	struct vop_write_args /* {
6921541Srgrimes		struct vnode *a_vp;
6931541Srgrimes		struct uio *a_uio;
6941541Srgrimes		int  a_ioflag;
6951541Srgrimes		struct ucred *a_cred;
6961541Srgrimes	} */ *ap;
6971541Srgrimes{
69846349Salc	int biosize;
69946349Salc	struct uio *uio = ap->a_uio;
7001541Srgrimes	struct proc *p = uio->uio_procp;
70146349Salc	struct vnode *vp = ap->a_vp;
7021541Srgrimes	struct nfsnode *np = VTONFS(vp);
70346349Salc	struct ucred *cred = ap->a_cred;
7041541Srgrimes	int ioflag = ap->a_ioflag;
7051541Srgrimes	struct buf *bp;
7061541Srgrimes	struct vattr vattr;
7079336Sdfr	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
70811921Sphk	daddr_t lbn;
70946349Salc	int bcount;
7109336Sdfr	int n, on, error = 0, iomode, must_commit;
7111541Srgrimes
7121541Srgrimes#ifdef DIAGNOSTIC
7131541Srgrimes	if (uio->uio_rw != UIO_WRITE)
7141541Srgrimes		panic("nfs_write mode");
7151541Srgrimes	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
7161541Srgrimes		panic("nfs_write proc");
7171541Srgrimes#endif
7181541Srgrimes	if (vp->v_type != VREG)
7191541Srgrimes		return (EIO);
7201541Srgrimes	if (np->n_flag & NWRITEERR) {
7211541Srgrimes		np->n_flag &= ~NWRITEERR;
7221541Srgrimes		return (np->n_error);
7231541Srgrimes	}
72436176Speter	if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
72536176Speter	    (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
7269336Sdfr		(void)nfs_fsinfo(nmp, vp, cred, p);
7271541Srgrimes	if (ioflag & (IO_APPEND | IO_SYNC)) {
7281541Srgrimes		if (np->n_flag & NMODIFIED) {
7291541Srgrimes			np->n_attrstamp = 0;
7303305Sphk			error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
7313305Sphk			if (error)
7321541Srgrimes				return (error);
7331541Srgrimes		}
7341541Srgrimes		if (ioflag & IO_APPEND) {
7351541Srgrimes			np->n_attrstamp = 0;
7363305Sphk			error = VOP_GETATTR(vp, &vattr, cred, p);
7373305Sphk			if (error)
7381541Srgrimes				return (error);
7391541Srgrimes			uio->uio_offset = np->n_size;
7401541Srgrimes		}
7411541Srgrimes	}
7421541Srgrimes	if (uio->uio_offset < 0)
7431541Srgrimes		return (EINVAL);
74436473Speter	if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
74536473Speter		return (EFBIG);
7461541Srgrimes	if (uio->uio_resid == 0)
7471541Srgrimes		return (0);
7481541Srgrimes	/*
7491541Srgrimes	 * Maybe this should be above the vnode op call, but so long as
7501541Srgrimes	 * file servers have no limits, i don't think it matters
7511541Srgrimes	 */
7521541Srgrimes	if (p && uio->uio_offset + uio->uio_resid >
7531541Srgrimes	      p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
7541541Srgrimes		psignal(p, SIGXFSZ);
7551541Srgrimes		return (EFBIG);
7561541Srgrimes	}
75746349Salc
7589428Sdfr	biosize = vp->v_mount->mnt_stat.f_iosize;
75946349Salc
7601541Srgrimes	do {
7611541Srgrimes		/*
7621541Srgrimes		 * Check for a valid write lease.
7631541Srgrimes		 */
7641541Srgrimes		if ((nmp->nm_flag & NFSMNT_NQNFS) &&
7659336Sdfr		    NQNFS_CKINVALID(vp, np, ND_WRITE)) {
7661541Srgrimes			do {
7679336Sdfr				error = nqnfs_getlease(vp, ND_WRITE, cred, p);
7681541Srgrimes			} while (error == NQNFS_EXPIRED);
7691541Srgrimes			if (error)
7701541Srgrimes				return (error);
7711541Srgrimes			if (np->n_lrev != np->n_brev ||
7721541Srgrimes			    (np->n_flag & NQNFSNONCACHE)) {
7733305Sphk				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
7743305Sphk				if (error)
7751541Srgrimes					return (error);
7761541Srgrimes				np->n_brev = np->n_lrev;
7771541Srgrimes			}
7781541Srgrimes		}
7799336Sdfr		if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
7809336Sdfr		    iomode = NFSV3WRITE_FILESYNC;
7819336Sdfr		    error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit);
7829336Sdfr		    if (must_commit)
7839336Sdfr			nfs_clearcommit(vp->v_mount);
7849336Sdfr		    return (error);
7859336Sdfr		}
7861541Srgrimes		nfsstats.biocache_writes++;
7871541Srgrimes		lbn = uio->uio_offset / biosize;
7881541Srgrimes		on = uio->uio_offset & (biosize-1);
7891541Srgrimes		n = min((unsigned)(biosize - on), uio->uio_resid);
7901541Srgrimesagain:
79146349Salc		/*
79246349Salc		 * Handle direct append and file extension cases, calculate
79346349Salc		 * unaligned buffer size.
79446349Salc		 */
79546349Salc
79646349Salc		if (uio->uio_offset == np->n_size && n) {
79746349Salc			/*
79846349Salc			 * special append case.  Obtain buffer prior to
79946349Salc			 * resizing it to maintain B_CACHE.
80046349Salc			 */
80146349Salc			long save;
80246349Salc
80346349Salc			bcount = on;
80446349Salc			bp = nfs_getcacheblk(vp, lbn, bcount, p);
80549659Sdt			if (!bp)
80649659Sdt				return (EINTR);
80746349Salc			save = bp->b_flags & B_CACHE;
80846349Salc
8098692Sdg			np->n_size = uio->uio_offset + n;
81025023Sdfr			np->n_flag |= NMODIFIED;
81141026Speter			vnode_pager_setsize(vp, np->n_size);
81246349Salc
81346349Salc			bcount += n;
81446349Salc			allocbuf(bp, bcount);
81546349Salc			bp->b_flags |= save;
81646349Salc		} else {
81746349Salc			if (uio->uio_offset + n > np->n_size) {
81846349Salc				np->n_size = uio->uio_offset + n;
81946349Salc				np->n_flag |= NMODIFIED;
82046349Salc				vnode_pager_setsize(vp, np->n_size);
82146349Salc			}
82246349Salc			bcount = biosize;
82346349Salc			if ((off_t)(lbn + 1) * biosize > np->n_size)
82446349Salc				bcount = np->n_size - (off_t)lbn * biosize;
82546349Salc			bp = nfs_getcacheblk(vp, lbn, bcount, p);
82649659Sdt			if (!bp)
82749659Sdt				return (EINTR);
8288692Sdg		}
82946349Salc
83046349Salc		/*
83146349Salc		 * Issue a READ if B_CACHE is not set.  In special-append
83246349Salc		 * mode, B_CACHE is based on the buffer prior to the write
83346349Salc		 * op and is typically set, avoiding the read.  If a read
83446349Salc		 * is required in special append mode, the server will
83546349Salc		 * probably send us a short-read since we extended the file
83646349Salc		 * on our end, resulting in b_resid == 0 and, thusly,
83746349Salc		 * B_CACHE getting set.
83846349Salc		 *
83946349Salc		 * We can also avoid issuing the read if the write covers
84046349Salc		 * the entire buffer.  We have to make sure the buffer state
84146349Salc		 * is reasonable in this case since we will not be initiating
84246349Salc		 * I/O.  See the comments in kern/vfs_bio.c's getblk() for
84346349Salc		 * more information.
84446349Salc		 *
84546349Salc		 * B_CACHE may also be set due to the buffer being cached
84646349Salc		 * normally.
84746349Salc		 */
84846349Salc
84946349Salc		if (on == 0 && n == bcount) {
85046349Salc			bp->b_flags |= B_CACHE;
85146349Salc			bp->b_flags &= ~(B_ERROR | B_INVAL);
8528692Sdg		}
85346349Salc
85446349Salc		if ((bp->b_flags & B_CACHE) == 0) {
85546349Salc			bp->b_flags |= B_READ;
85646349Salc			vfs_busy_pages(bp, 0);
85746349Salc			error = nfs_doio(bp, cred, p);
85846349Salc			if (error) {
85946349Salc				brelse(bp);
86046349Salc				return (error);
86146349Salc			}
86246349Salc		}
8631541Srgrimes		if (!bp)
8641541Srgrimes			return (EINTR);
8651541Srgrimes		if (bp->b_wcred == NOCRED) {
8661541Srgrimes			crhold(cred);
8671541Srgrimes			bp->b_wcred = cred;
8681541Srgrimes		}
8691541Srgrimes		np->n_flag |= NMODIFIED;
8708692Sdg
87145347Sjulian		/*
87245347Sjulian		 * If dirtyend exceeds file size, chop it down.  If this
87345347Sjulian		 * creates a reverse-indexed or degenerate situation with
87445347Sjulian		 * dirtyoff/end, 0 them.
87545347Sjulian		 */
87645347Sjulian
87741791Sdt		if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
87841791Sdt			bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
87945347Sjulian		if (bp->b_dirtyoff >= bp->b_dirtyend)
88045347Sjulian			bp->b_dirtyoff = bp->b_dirtyend = 0;
88131617Sdyson
8821541Srgrimes		/*
88331617Sdyson		 * If the new write will leave a contiguous dirty
88431617Sdyson		 * area, just update the b_dirtyoff and b_dirtyend,
88531617Sdyson		 * otherwise force a write rpc of the old dirty area.
88646349Salc		 *
88746349Salc		 * While it is possible to merge discontiguous writes due to
88846349Salc		 * our having a B_CACHE buffer ( and thus valid read data
88946349Salc		 * for the hole), we don't because it could lead to
89046349Salc		 * significant cache coherency problems with multiple clients,
89146349Salc		 * especially if locking is implemented later on.
89246349Salc		 *
89346349Salc		 * as an optimization we could theoretically maintain
89446349Salc		 * a linked list of discontinuous areas, but we would still
89546349Salc		 * have to commit them separately so there isn't much
89646349Salc		 * advantage to it except perhaps a bit of asynchronization.
89731617Sdyson		 */
89842957Sdillon
89931617Sdyson		if (bp->b_dirtyend > 0 &&
90031617Sdyson		    (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
90147964Smckusick			if (VOP_BWRITE(bp->b_vp, bp) == EINTR)
90231617Sdyson				return (EINTR);
90331617Sdyson			goto again;
90431617Sdyson		}
90531617Sdyson
90631617Sdyson		/*
9071541Srgrimes		 * Check for valid write lease and get one as required.
9081541Srgrimes		 * In case getblk() and/or bwrite() delayed us.
9091541Srgrimes		 */
9101541Srgrimes		if ((nmp->nm_flag & NFSMNT_NQNFS) &&
9119336Sdfr		    NQNFS_CKINVALID(vp, np, ND_WRITE)) {
9121541Srgrimes			do {
9139336Sdfr				error = nqnfs_getlease(vp, ND_WRITE, cred, p);
9141541Srgrimes			} while (error == NQNFS_EXPIRED);
9151541Srgrimes			if (error) {
9161541Srgrimes				brelse(bp);
9171541Srgrimes				return (error);
9181541Srgrimes			}
9191541Srgrimes			if (np->n_lrev != np->n_brev ||
9201541Srgrimes			    (np->n_flag & NQNFSNONCACHE)) {
9211541Srgrimes				brelse(bp);
9223305Sphk				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
9233305Sphk				if (error)
9241541Srgrimes					return (error);
9251541Srgrimes				np->n_brev = np->n_lrev;
9261541Srgrimes				goto again;
9271541Srgrimes			}
9281541Srgrimes		}
92934206Sdyson
9303305Sphk		error = uiomove((char *)bp->b_data + on, n, uio);
93154480Sdillon
93254480Sdillon		/*
93354480Sdillon		 * Since this block is being modified, it must be written
93454480Sdillon		 * again and not just committed.  Since write clustering does
93554480Sdillon		 * not work for the stage 1 data write, only the stage 2
93654480Sdillon		 * commit rpc, we have to clear B_CLUSTEROK as well.
93754480Sdillon		 */
93854480Sdillon		bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
93954480Sdillon
9403305Sphk		if (error) {
9411541Srgrimes			bp->b_flags |= B_ERROR;
9421541Srgrimes			brelse(bp);
9431541Srgrimes			return (error);
9441541Srgrimes		}
94534206Sdyson
94634206Sdyson		/*
94745347Sjulian		 * Only update dirtyoff/dirtyend if not a degenerate
94845347Sjulian		 * condition.
94945347Sjulian		 */
95045347Sjulian		if (n) {
95145347Sjulian			if (bp->b_dirtyend > 0) {
95245347Sjulian				bp->b_dirtyoff = min(on, bp->b_dirtyoff);
95345347Sjulian				bp->b_dirtyend = max((on + n), bp->b_dirtyend);
95445347Sjulian			} else {
95545347Sjulian				bp->b_dirtyoff = on;
95645347Sjulian				bp->b_dirtyend = on + n;
95745347Sjulian			}
95846349Salc			vfs_bio_set_validclean(bp, on, n);
9591541Srgrimes		}
96045347Sjulian
96144679Sjulian		/*
9621541Srgrimes		 * If the lease is non-cachable or IO_SYNC do bwrite().
96346349Salc		 *
96446349Salc		 * IO_INVAL appears to be unused.  The idea appears to be
96546349Salc		 * to turn off caching in this case.  Very odd.  XXX
9661541Srgrimes		 */
9671541Srgrimes		if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
96834206Sdyson			if (ioflag & IO_INVAL)
96946349Salc				bp->b_flags |= B_NOCACHE;
97047964Smckusick			error = VOP_BWRITE(bp->b_vp, bp);
9713305Sphk			if (error)
9721541Srgrimes				return (error);
9739336Sdfr			if (np->n_flag & NQNFSNONCACHE) {
9749336Sdfr				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
9759336Sdfr				if (error)
9769336Sdfr					return (error);
9779336Sdfr			}
9781541Srgrimes		} else if ((n + on) == biosize &&
9791541Srgrimes			(nmp->nm_flag & NFSMNT_NQNFS) == 0) {
9809336Sdfr			bp->b_flags |= B_ASYNC;
98146580Sphk			(void)nfs_writebp(bp, 0, 0);
98246349Salc		} else {
9831541Srgrimes			bdwrite(bp);
98446349Salc		}
9851541Srgrimes	} while (uio->uio_resid > 0 && n > 0);
9861541Srgrimes	return (0);
9871541Srgrimes}
9881541Srgrimes
9891541Srgrimes/*
9901541Srgrimes * Get an nfs cache block.
99154480Sdillon *
9921541Srgrimes * Allocate a new one if the block isn't currently in the cache
9931541Srgrimes * and return the block marked busy. If the calling process is
9941541Srgrimes * interrupted by a signal for an interruptible mount point, return
9951541Srgrimes * NULL.
99654480Sdillon *
99754480Sdillon * The caller must carefully deal with the possible B_INVAL state of
99854480Sdillon * the buffer.  nfs_doio() clears B_INVAL (and nfs_asyncio() clears it
99954480Sdillon * indirectly), so synchronous reads can be issued without worrying about
100054480Sdillon * the B_INVAL state.  We have to be a little more careful when dealing
100154480Sdillon * with writes (see comments in nfs_write()) when extending a file past
100254480Sdillon * its EOF.
10031541Srgrimes */
100412911Sphkstatic struct buf *
10051541Srgrimesnfs_getcacheblk(vp, bn, size, p)
10061541Srgrimes	struct vnode *vp;
10071541Srgrimes	daddr_t bn;
10081541Srgrimes	int size;
10091541Srgrimes	struct proc *p;
10101541Srgrimes{
10111541Srgrimes	register struct buf *bp;
101232755Sdyson	struct mount *mp;
101332755Sdyson	struct nfsmount *nmp;
10141541Srgrimes
101532755Sdyson	mp = vp->v_mount;
101632755Sdyson	nmp = VFSTONFS(mp);
101732755Sdyson
10181541Srgrimes	if (nmp->nm_flag & NFSMNT_INT) {
10191541Srgrimes		bp = getblk(vp, bn, size, PCATCH, 0);
10201541Srgrimes		while (bp == (struct buf *)0) {
10211541Srgrimes			if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
10221541Srgrimes				return ((struct buf *)0);
10231541Srgrimes			bp = getblk(vp, bn, size, 0, 2 * hz);
10241541Srgrimes		}
102546349Salc	} else {
10261541Srgrimes		bp = getblk(vp, bn, size, 0, 0);
102746349Salc	}
10285455Sdg
102941791Sdt	if (vp->v_type == VREG) {
103032755Sdyson		int biosize;
103146349Salc
103232755Sdyson		biosize = mp->mnt_stat.f_iosize;
103341791Sdt		bp->b_blkno = bn * (biosize / DEV_BSIZE);
103432755Sdyson	}
10351541Srgrimes	return (bp);
10361541Srgrimes}
10371541Srgrimes
10381541Srgrimes/*
10391541Srgrimes * Flush and invalidate all dirty buffers. If another process is already
10401541Srgrimes * doing the flush, just wait for completion.
10411541Srgrimes */
10421549Srgrimesint
10431541Srgrimesnfs_vinvalbuf(vp, flags, cred, p, intrflg)
10441541Srgrimes	struct vnode *vp;
10451541Srgrimes	int flags;
10461541Srgrimes	struct ucred *cred;
10471541Srgrimes	struct proc *p;
10481541Srgrimes	int intrflg;
10491541Srgrimes{
10501541Srgrimes	register struct nfsnode *np = VTONFS(vp);
10511541Srgrimes	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
10521541Srgrimes	int error = 0, slpflag, slptimeo;
10531541Srgrimes
105432755Sdyson	if (vp->v_flag & VXLOCK) {
105532755Sdyson		return (0);
105632755Sdyson	}
105732755Sdyson
10581541Srgrimes	if ((nmp->nm_flag & NFSMNT_INT) == 0)
10591541Srgrimes		intrflg = 0;
10601541Srgrimes	if (intrflg) {
10611541Srgrimes		slpflag = PCATCH;
10621541Srgrimes		slptimeo = 2 * hz;
10631541Srgrimes	} else {
10641541Srgrimes		slpflag = 0;
10651541Srgrimes		slptimeo = 0;
10661541Srgrimes	}
10671541Srgrimes	/*
10681541Srgrimes	 * First wait for any other process doing a flush to complete.
10691541Srgrimes	 */
10701541Srgrimes	while (np->n_flag & NFLUSHINPROG) {
10711541Srgrimes		np->n_flag |= NFLUSHWANT;
10721541Srgrimes		error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
10731541Srgrimes			slptimeo);
10741541Srgrimes		if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p))
10751541Srgrimes			return (EINTR);
10761541Srgrimes	}
10771541Srgrimes
10781541Srgrimes	/*
10791541Srgrimes	 * Now, flush as required.
10801541Srgrimes	 */
10811541Srgrimes	np->n_flag |= NFLUSHINPROG;
10821541Srgrimes	error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
10831541Srgrimes	while (error) {
10841541Srgrimes		if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
10851541Srgrimes			np->n_flag &= ~NFLUSHINPROG;
10861541Srgrimes			if (np->n_flag & NFLUSHWANT) {
10871541Srgrimes				np->n_flag &= ~NFLUSHWANT;
10881541Srgrimes				wakeup((caddr_t)&np->n_flag);
10891541Srgrimes			}
10901541Srgrimes			return (EINTR);
10911541Srgrimes		}
10921541Srgrimes		error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
10931541Srgrimes	}
10941541Srgrimes	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
10951541Srgrimes	if (np->n_flag & NFLUSHWANT) {
10961541Srgrimes		np->n_flag &= ~NFLUSHWANT;
10971541Srgrimes		wakeup((caddr_t)&np->n_flag);
10981541Srgrimes	}
10991541Srgrimes	return (0);
11001541Srgrimes}
11011541Srgrimes
11021541Srgrimes/*
11031541Srgrimes * Initiate asynchronous I/O. Return an error if no nfsiods are available.
11041541Srgrimes * This is mainly to avoid queueing async I/O requests when the nfsiods
11051541Srgrimes * are all hung on a dead server.
110646349Salc *
110746349Salc * Note: nfs_asyncio() does not clear (B_ERROR|B_INVAL) but when the bp
110846349Salc * is eventually dequeued by the async daemon, nfs_doio() *will*.
11091541Srgrimes */
11101549Srgrimesint
111146580Sphknfs_asyncio(bp, cred, procp)
11121541Srgrimes	register struct buf *bp;
11131541Srgrimes	struct ucred *cred;
111446580Sphk	struct proc *procp;
11151541Srgrimes{
111619449Sdfr	struct nfsmount *nmp;
111719449Sdfr	int i;
111819449Sdfr	int gotiod;
111919449Sdfr	int slpflag = 0;
112019449Sdfr	int slptimeo = 0;
112119449Sdfr	int error;
11221541Srgrimes
11231541Srgrimes	if (nfs_numasync == 0)
11241541Srgrimes		return (EIO);
112544679Sjulian
112619449Sdfr	nmp = VFSTONFS(bp->b_vp->v_mount);
112719449Sdfragain:
112819449Sdfr	if (nmp->nm_flag & NFSMNT_INT)
112919449Sdfr		slpflag = PCATCH;
113019449Sdfr	gotiod = FALSE;
113119449Sdfr
113219449Sdfr	/*
113319449Sdfr	 * Find a free iod to process this request.
113419449Sdfr	 */
11351541Srgrimes	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
113619449Sdfr		if (nfs_iodwant[i]) {
113719449Sdfr			/*
113819449Sdfr			 * Found one, so wake it up and tell it which
113919449Sdfr			 * mount to process.
114019449Sdfr			 */
114119449Sdfr			NFS_DPF(ASYNCIO,
114219449Sdfr				("nfs_asyncio: waking iod %d for mount %p\n",
114319449Sdfr				 i, nmp));
114419449Sdfr			nfs_iodwant[i] = (struct proc *)0;
114519449Sdfr			nfs_iodmount[i] = nmp;
114619449Sdfr			nmp->nm_bufqiods++;
114719449Sdfr			wakeup((caddr_t)&nfs_iodwant[i]);
114819449Sdfr			gotiod = TRUE;
114925023Sdfr			break;
115019449Sdfr		}
115119449Sdfr
115219449Sdfr	/*
115319449Sdfr	 * If none are free, we may already have an iod working on this mount
115419449Sdfr	 * point.  If so, it will process our request.
115519449Sdfr	 */
115619449Sdfr	if (!gotiod) {
115719449Sdfr		if (nmp->nm_bufqiods > 0) {
115819449Sdfr			NFS_DPF(ASYNCIO,
115919449Sdfr				("nfs_asyncio: %d iods are already processing mount %p\n",
116019449Sdfr				 nmp->nm_bufqiods, nmp));
116119449Sdfr			gotiod = TRUE;
116219449Sdfr		}
116319449Sdfr	}
116419449Sdfr
116519449Sdfr	/*
116619449Sdfr	 * If we have an iod which can process the request, then queue
116719449Sdfr	 * the buffer.
116819449Sdfr	 */
116919449Sdfr	if (gotiod) {
117019449Sdfr		/*
117119449Sdfr		 * Ensure that the queue never grows too large.
117219449Sdfr		 */
117319449Sdfr		while (nmp->nm_bufqlen >= 2*nfs_numasync) {
117419449Sdfr			NFS_DPF(ASYNCIO,
117519449Sdfr				("nfs_asyncio: waiting for mount %p queue to drain\n", nmp));
117619449Sdfr			nmp->nm_bufqwant = TRUE;
117719449Sdfr			error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
117819449Sdfr				       "nfsaio", slptimeo);
117919449Sdfr			if (error) {
118046580Sphk				if (nfs_sigintr(nmp, NULL, procp))
118119449Sdfr					return (EINTR);
118219449Sdfr				if (slpflag == PCATCH) {
118319449Sdfr					slpflag = 0;
118419449Sdfr					slptimeo = 2 * hz;
118519449Sdfr				}
118619449Sdfr			}
118719449Sdfr			/*
118819449Sdfr			 * We might have lost our iod while sleeping,
118919449Sdfr			 * so check and loop if nescessary.
119019449Sdfr			 */
119119449Sdfr			if (nmp->nm_bufqiods == 0) {
119219449Sdfr				NFS_DPF(ASYNCIO,
119319449Sdfr					("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
119419449Sdfr				goto again;
119519449Sdfr			}
119619449Sdfr		}
119719449Sdfr
11981541Srgrimes		if (bp->b_flags & B_READ) {
11991541Srgrimes			if (bp->b_rcred == NOCRED && cred != NOCRED) {
12001541Srgrimes				crhold(cred);
12011541Srgrimes				bp->b_rcred = cred;
12021541Srgrimes			}
12031541Srgrimes		} else {
12049336Sdfr			bp->b_flags |= B_WRITEINPROG;
12051541Srgrimes			if (bp->b_wcred == NOCRED && cred != NOCRED) {
12061541Srgrimes				crhold(cred);
12071541Srgrimes				bp->b_wcred = cred;
12081541Srgrimes			}
12091541Srgrimes		}
12108876Srgrimes
121148225Smckusick		BUF_KERNPROC(bp);
121219449Sdfr		TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
121319449Sdfr		nmp->nm_bufqlen++;
12141541Srgrimes		return (0);
121519449Sdfr	}
12169336Sdfr
12179336Sdfr	/*
121819449Sdfr	 * All the iods are busy on other mounts, so return EIO to
121919449Sdfr	 * force the caller to process the i/o synchronously.
12209336Sdfr	 */
122119449Sdfr	NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n"));
122219449Sdfr	return (EIO);
12231541Srgrimes}
12241541Srgrimes
12251541Srgrimes/*
12261541Srgrimes * Do an I/O operation to/from a cache block. This may be called
12271541Srgrimes * synchronously or from an nfsiod.
12281541Srgrimes */
12291541Srgrimesint
12301541Srgrimesnfs_doio(bp, cr, p)
123144679Sjulian	struct buf *bp;
12323305Sphk	struct ucred *cr;
12331541Srgrimes	struct proc *p;
12341541Srgrimes{
123544679Sjulian	struct uio *uiop;
123644679Sjulian	struct vnode *vp;
12371541Srgrimes	struct nfsnode *np;
12381541Srgrimes	struct nfsmount *nmp;
123946349Salc	int error = 0, iomode, must_commit = 0;
12401541Srgrimes	struct uio uio;
12411541Srgrimes	struct iovec io;
12421541Srgrimes
12431541Srgrimes	vp = bp->b_vp;
12441541Srgrimes	np = VTONFS(vp);
12451541Srgrimes	nmp = VFSTONFS(vp->v_mount);
12461541Srgrimes	uiop = &uio;
12471541Srgrimes	uiop->uio_iov = &io;
12481541Srgrimes	uiop->uio_iovcnt = 1;
12491541Srgrimes	uiop->uio_segflg = UIO_SYSSPACE;
12501541Srgrimes	uiop->uio_procp = p;
12511541Srgrimes
125246349Salc	/*
125346349Salc	 * clear B_ERROR and B_INVAL state prior to initiating the I/O.  We
125446349Salc	 * do this here so we do not have to do it in all the code that
125546349Salc	 * calls us.
125646349Salc	 */
125746349Salc	bp->b_flags &= ~(B_ERROR | B_INVAL);
125846349Salc
125944679Sjulian	KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp));
126044679Sjulian
12611541Srgrimes	/*
12621541Srgrimes	 * Historically, paging was done with physio, but no more.
12631541Srgrimes	 */
12643664Sphk	if (bp->b_flags & B_PHYS) {
12653664Sphk	    /*
12663664Sphk	     * ...though reading /dev/drum still gets us here.
12673664Sphk	     */
12681541Srgrimes	    io.iov_len = uiop->uio_resid = bp->b_bcount;
12693664Sphk	    /* mapping was done by vmapbuf() */
12701541Srgrimes	    io.iov_base = bp->b_data;
12719336Sdfr	    uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
12723664Sphk	    if (bp->b_flags & B_READ) {
12733664Sphk		uiop->uio_rw = UIO_READ;
12743664Sphk		nfsstats.read_physios++;
12753664Sphk		error = nfs_readrpc(vp, uiop, cr);
12763664Sphk	    } else {
12779336Sdfr		int com;
12789336Sdfr
12799336Sdfr		iomode = NFSV3WRITE_DATASYNC;
12803664Sphk		uiop->uio_rw = UIO_WRITE;
12813664Sphk		nfsstats.write_physios++;
12829336Sdfr		error = nfs_writerpc(vp, uiop, cr, &iomode, &com);
12833664Sphk	    }
12843664Sphk	    if (error) {
12853664Sphk		bp->b_flags |= B_ERROR;
12863664Sphk		bp->b_error = error;
12873664Sphk	    }
12883664Sphk	} else if (bp->b_flags & B_READ) {
12893664Sphk	    io.iov_len = uiop->uio_resid = bp->b_bcount;
12903664Sphk	    io.iov_base = bp->b_data;
12911541Srgrimes	    uiop->uio_rw = UIO_READ;
12921541Srgrimes	    switch (vp->v_type) {
12931541Srgrimes	    case VREG:
12949336Sdfr		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
12951541Srgrimes		nfsstats.read_bios++;
12961541Srgrimes		error = nfs_readrpc(vp, uiop, cr);
12971541Srgrimes		if (!error) {
12981541Srgrimes		    if (uiop->uio_resid) {
12991541Srgrimes			/*
130046349Salc			 * If we had a short read with no error, we must have
130146349Salc			 * hit a file hole.  We should zero-fill the remainder.
130246349Salc			 * This can also occur if the server hits the file EOF.
130346349Salc			 *
130446349Salc			 * Holes used to be able to occur due to pending
130546349Salc			 * writes, but that is not possible any longer.
13061541Srgrimes			 */
130746349Salc			int nread = bp->b_bcount - uiop->uio_resid;
130846349Salc			int left  = bp->b_bcount - nread;
130946349Salc
131046349Salc			if (left > 0)
131146349Salc				bzero((char *)bp->b_data + nread, left);
131246349Salc			uiop->uio_resid = 0;
131346349Salc		    }
13141541Srgrimes		}
13151541Srgrimes		if (p && (vp->v_flag & VTEXT) &&
13161541Srgrimes			(((nmp->nm_flag & NFSMNT_NQNFS) &&
13179336Sdfr			  NQNFS_CKINVALID(vp, np, ND_READ) &&
13181541Srgrimes			  np->n_lrev != np->n_brev) ||
13191541Srgrimes			 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
132018397Snate			  np->n_mtime != np->n_vattr.va_mtime.tv_sec))) {
13211541Srgrimes			uprintf("Process killed due to text file modification\n");
13221541Srgrimes			psignal(p, SIGKILL);
132345361Speter			PHOLD(p);
13241541Srgrimes		}
13251541Srgrimes		break;
13261541Srgrimes	    case VLNK:
13279336Sdfr		uiop->uio_offset = (off_t)0;
13281541Srgrimes		nfsstats.readlink_bios++;
13291541Srgrimes		error = nfs_readlinkrpc(vp, uiop, cr);
13301541Srgrimes		break;
13311541Srgrimes	    case VDIR:
13321541Srgrimes		nfsstats.readdir_bios++;
13339336Sdfr		uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
13349336Sdfr		if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
13359336Sdfr			error = nfs_readdirplusrpc(vp, uiop, cr);
13369336Sdfr			if (error == NFSERR_NOTSUPP)
13379336Sdfr				nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
13389336Sdfr		}
13399336Sdfr		if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
13409336Sdfr			error = nfs_readdirrpc(vp, uiop, cr);
134146349Salc		/*
134246349Salc		 * end-of-directory sets B_INVAL but does not generate an
134346349Salc		 * error.
134446349Salc		 */
134539782Smckusick		if (error == 0 && uiop->uio_resid == bp->b_bcount)
134639782Smckusick			bp->b_flags |= B_INVAL;
13471541Srgrimes		break;
13483305Sphk	    default:
13493305Sphk		printf("nfs_doio:  type %x unexpected\n",vp->v_type);
13503305Sphk		break;
13511541Srgrimes	    };
13521541Srgrimes	    if (error) {
13531541Srgrimes		bp->b_flags |= B_ERROR;
13541541Srgrimes		bp->b_error = error;
13551541Srgrimes	    }
13561541Srgrimes	} else {
135751344Sdillon	    /*
135851344Sdillon	     * If we only need to commit, try to commit
135951344Sdillon	     */
136051344Sdillon	    if (bp->b_flags & B_NEEDCOMMIT) {
136151344Sdillon		    int retv;
136251344Sdillon		    off_t off;
136351344Sdillon
136451344Sdillon		    off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
136551344Sdillon		    bp->b_flags |= B_WRITEINPROG;
136651344Sdillon		    retv = nfs_commit(
136751344Sdillon				bp->b_vp, off, bp->b_dirtyend-bp->b_dirtyoff,
136851344Sdillon				bp->b_wcred, p);
136951344Sdillon		    bp->b_flags &= ~B_WRITEINPROG;
137051344Sdillon		    if (retv == 0) {
137151344Sdillon			    bp->b_dirtyoff = bp->b_dirtyend = 0;
137254480Sdillon			    bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
137351344Sdillon			    bp->b_resid = 0;
137451344Sdillon			    biodone(bp);
137551344Sdillon			    return (0);
137651344Sdillon		    }
137751344Sdillon		    if (retv == NFSERR_STALEWRITEVERF) {
137851344Sdillon			    nfs_clearcommit(bp->b_vp->v_mount);
137951344Sdillon		    }
138051344Sdillon	    }
138151344Sdillon
138251344Sdillon	    /*
138351344Sdillon	     * Setup for actual write
138451344Sdillon	     */
138551344Sdillon
138641791Sdt	    if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
138741791Sdt		bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
13888692Sdg
13898692Sdg	    if (bp->b_dirtyend > bp->b_dirtyoff) {
13908692Sdg		io.iov_len = uiop->uio_resid = bp->b_dirtyend
13919336Sdfr		    - bp->b_dirtyoff;
139241791Sdt		uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE
13939336Sdfr		    + bp->b_dirtyoff;
13948692Sdg		io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
13958692Sdg		uiop->uio_rw = UIO_WRITE;
13968692Sdg		nfsstats.write_bios++;
139744679Sjulian
139825785Sdfr		if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC)
13999336Sdfr		    iomode = NFSV3WRITE_UNSTABLE;
14008692Sdg		else
14019336Sdfr		    iomode = NFSV3WRITE_FILESYNC;
140244679Sjulian
14039336Sdfr		bp->b_flags |= B_WRITEINPROG;
14049336Sdfr		error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
140551475Sdillon
140651475Sdillon		/*
140751475Sdillon		 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
140851475Sdillon		 * to cluster the buffers needing commit.  This will allow
140951475Sdillon		 * the system to submit a single commit rpc for the whole
141054480Sdillon		 * cluster.  We can do this even if the buffer is not 100%
141154480Sdillon		 * dirty (relative to the NFS blocksize), so we optimize the
141254480Sdillon		 * append-to-file-case.
141354480Sdillon		 *
141454480Sdillon		 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
141554480Sdillon		 * cleared because write clustering only works for commit
141654480Sdillon		 * rpc's, not for the data portion of the write).
141751475Sdillon		 */
141851475Sdillon
141925003Sdfr		if (!error && iomode == NFSV3WRITE_UNSTABLE) {
142025003Sdfr		    bp->b_flags |= B_NEEDCOMMIT;
142125003Sdfr		    if (bp->b_dirtyoff == 0
142246349Salc			&& bp->b_dirtyend == bp->b_bcount)
142325003Sdfr			bp->b_flags |= B_CLUSTEROK;
142444679Sjulian		} else {
142554480Sdillon		    bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
142644679Sjulian		}
14279336Sdfr		bp->b_flags &= ~B_WRITEINPROG;
14288692Sdg
14299336Sdfr		/*
14309336Sdfr		 * For an interrupted write, the buffer is still valid
14319336Sdfr		 * and the write hasn't been pushed to the server yet,
14329336Sdfr		 * so we can't set B_ERROR and report the interruption
14339336Sdfr		 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
14349336Sdfr		 * is not relevant, so the rpc attempt is essentially
14359336Sdfr		 * a noop.  For the case of a V3 write rpc not being
14369336Sdfr		 * committed to stable storage, the block is still
14379336Sdfr		 * dirty and requires either a commit rpc or another
14389336Sdfr		 * write rpc with iomode == NFSV3WRITE_FILESYNC before
14399336Sdfr		 * the block is reused. This is indicated by setting
14409336Sdfr		 * the B_DELWRI and B_NEEDCOMMIT flags.
144142957Sdillon		 *
144242957Sdillon		 * If the buffer is marked B_PAGING, it does not reside on
144344679Sjulian		 * the vp's paging queues so we cannot call bdirty().  The
144444679Sjulian		 * bp in this case is not an NFS cache block so we should
144544679Sjulian		 * be safe. XXX
14469336Sdfr		 */
14479336Sdfr    		if (error == EINTR
14489336Sdfr		    || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
144934266Sjulian			int s;
145034266Sjulian
145144679Sjulian			s = splbio();
14528692Sdg			bp->b_flags &= ~(B_INVAL|B_NOCACHE);
145342957Sdillon			if ((bp->b_flags & B_PAGING) == 0) {
145444679Sjulian			    bdirty(bp);
145544679Sjulian			    bp->b_flags &= ~B_DONE;
145642957Sdillon			}
145747749Speter			if (error && (bp->b_flags & B_ASYNC) == 0)
145832755Sdyson			    bp->b_flags |= B_EINTR;
145944679Sjulian			splx(s);
14608692Sdg	    	} else {
146144679Sjulian		    if (error) {
146244679Sjulian			bp->b_flags |= B_ERROR;
146344679Sjulian			bp->b_error = np->n_error = error;
146444679Sjulian			np->n_flag |= NWRITEERR;
146544679Sjulian		    }
146644679Sjulian		    bp->b_dirtyoff = bp->b_dirtyend = 0;
14678692Sdg		}
14681541Srgrimes	    } else {
14698692Sdg		bp->b_resid = 0;
14708692Sdg		biodone(bp);
14718692Sdg		return (0);
14721541Srgrimes	    }
14731541Srgrimes	}
14741541Srgrimes	bp->b_resid = uiop->uio_resid;
14759336Sdfr	if (must_commit)
147644679Sjulian	    nfs_clearcommit(vp->v_mount);
14771541Srgrimes	biodone(bp);
14781541Srgrimes	return (error);
14791541Srgrimes}
1480