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