xref: /openbsd-src/sys/nfs/nfs_bio.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: nfs_bio.c,v 1.72 2010/08/07 03:50:02 krw Exp $	*/
2 /*	$NetBSD: nfs_bio.c,v 1.25.4.2 1996/07/08 20:47:04 jtc Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Rick Macklem at The University of Guelph.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)nfs_bio.c	8.9 (Berkeley) 3/30/95
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/resourcevar.h>
41 #include <sys/signalvar.h>
42 #include <sys/proc.h>
43 #include <sys/buf.h>
44 #include <sys/vnode.h>
45 #include <sys/mount.h>
46 #include <sys/kernel.h>
47 #include <sys/namei.h>
48 #include <sys/queue.h>
49 #include <sys/time.h>
50 
51 #include <uvm/uvm_extern.h>
52 
53 #include <nfs/rpcv2.h>
54 #include <nfs/nfsproto.h>
55 #include <nfs/nfs.h>
56 #include <nfs/nfsmount.h>
57 #include <nfs/nfsnode.h>
58 #include <nfs/nfs_var.h>
59 
60 extern int nfs_numasync;
61 extern struct nfsstats nfsstats;
62 struct nfs_bufqhead nfs_bufq;
63 uint32_t nfs_bufqmax, nfs_bufqlen;
64 
65 /*
66  * Vnode op for read using bio
67  * Any similarity to readip() is purely coincidental
68  */
69 int
70 nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred)
71 {
72 	struct nfsnode *np = VTONFS(vp);
73 	int biosize, diff;
74 	struct buf *bp = NULL, *rabp;
75 	struct vattr vattr;
76 	struct proc *p;
77 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
78 	daddr64_t lbn, bn, rabn;
79 	caddr_t baddr;
80 	int got_buf = 0, nra, error = 0, n = 0, on = 0, not_readin;
81 	off_t offdiff;
82 
83 #ifdef DIAGNOSTIC
84 	if (uio->uio_rw != UIO_READ)
85 		panic("nfs_read mode");
86 #endif
87 	if (uio->uio_resid == 0)
88 		return (0);
89 	if (uio->uio_offset < 0)
90 		return (EINVAL);
91 	p = uio->uio_procp;
92 	if ((nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_GOTFSINFO)) == NFSMNT_NFSV3)
93 		(void)nfs_fsinfo(nmp, vp, cred, p);
94 	biosize = nmp->nm_rsize;
95 	/*
96 	 * For nfs, cache consistency can only be maintained approximately.
97 	 * Although RFC1094 does not specify the criteria, the following is
98 	 * believed to be compatible with the reference port.
99 	 * For nfs:
100 	 * If the file's modify time on the server has changed since the
101 	 * last read rpc or you have written to the file,
102 	 * you may have lost data cache consistency with the
103 	 * server, so flush all of the file's data out of the cache.
104 	 * Then force a getattr rpc to ensure that you have up to date
105 	 * attributes.
106 	 */
107 	if (np->n_flag & NMODIFIED) {
108 		NFS_INVALIDATE_ATTRCACHE(np);
109 		error = VOP_GETATTR(vp, &vattr, cred, p);
110 		if (error)
111 			return (error);
112 		np->n_mtime = vattr.va_mtime;
113 	} else {
114 		error = VOP_GETATTR(vp, &vattr, cred, p);
115 		if (error)
116 			return (error);
117 		if (timespeccmp(&np->n_mtime, &vattr.va_mtime, !=)) {
118 			error = nfs_vinvalbuf(vp, V_SAVE, cred, p);
119 			if (error)
120 				return (error);
121 			np->n_mtime = vattr.va_mtime;
122 		}
123 	}
124 
125 	/*
126 	 * update the cache read creds for this vnode
127 	 */
128 	if (np->n_rcred)
129 		crfree(np->n_rcred);
130 	np->n_rcred = cred;
131 	crhold(cred);
132 
133 	do {
134 	    if ((vp->v_flag & VROOT) && vp->v_type == VLNK) {
135 		    return (nfs_readlinkrpc(vp, uio, cred));
136 	    }
137 	    baddr = (caddr_t)0;
138 	    switch (vp->v_type) {
139 	    case VREG:
140 		nfsstats.biocache_reads++;
141 		lbn = uio->uio_offset / biosize;
142 		on = uio->uio_offset & (biosize - 1);
143 		bn = lbn * (biosize / DEV_BSIZE);
144 		not_readin = 1;
145 
146 		/*
147 		 * Start the read ahead(s), as required.
148 		 */
149 		if (nfs_numasync > 0 && nmp->nm_readahead > 0) {
150 		    for (nra = 0; nra < nmp->nm_readahead &&
151 			(lbn + 1 + nra) * biosize < np->n_size; nra++) {
152 			rabn = (lbn + 1 + nra) * (biosize / DEV_BSIZE);
153 			if (!incore(vp, rabn)) {
154 			    rabp = nfs_getcacheblk(vp, rabn, biosize, p);
155 			    if (!rabp)
156 				return (EINTR);
157 			    if ((rabp->b_flags & (B_DELWRI | B_DONE)) == 0) {
158 				rabp->b_flags |= (B_READ | B_ASYNC);
159 				if (nfs_asyncio(rabp, 1)) {
160 				    rabp->b_flags |= B_INVAL;
161 				    brelse(rabp);
162 				}
163 			    } else
164 				brelse(rabp);
165 			}
166 		    }
167 		}
168 
169 again:
170 		bp = nfs_getcacheblk(vp, bn, biosize, p);
171 		if (!bp)
172 			return (EINTR);
173 		got_buf = 1;
174 		if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0) {
175 			bp->b_flags |= B_READ;
176 			not_readin = 0;
177 			error = nfs_doio(bp, p);
178 			if (error) {
179 			    brelse(bp);
180 			    return (error);
181 			}
182 		}
183 		n = min((unsigned)(biosize - on), uio->uio_resid);
184 		offdiff = np->n_size - uio->uio_offset;
185 		if (offdiff < (off_t)n)
186 			n = (int)offdiff;
187 		if (not_readin && n > 0) {
188 			if (on < bp->b_validoff || (on + n) > bp->b_validend) {
189 				bp->b_flags |= B_INVAFTERWRITE;
190 				if (bp->b_dirtyend > 0) {
191 				    if ((bp->b_flags & B_DELWRI) == 0)
192 					panic("nfsbioread");
193 				    if (VOP_BWRITE(bp) == EINTR)
194 					return (EINTR);
195 				} else
196 				    brelse(bp);
197 				goto again;
198 			}
199 		}
200 		diff = (on >= bp->b_validend) ? 0 : (bp->b_validend - on);
201 		if (diff < n)
202 			n = diff;
203 		break;
204 	    case VLNK:
205 		nfsstats.biocache_readlinks++;
206 		bp = nfs_getcacheblk(vp, 0, NFS_MAXPATHLEN, p);
207 		if (!bp)
208 			return (EINTR);
209 		if ((bp->b_flags & B_DONE) == 0) {
210 			bp->b_flags |= B_READ;
211 			error = nfs_doio(bp, p);
212 			if (error) {
213 				brelse(bp);
214 				return (error);
215 			}
216 		}
217 		n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
218 		got_buf = 1;
219 		on = 0;
220 		break;
221 	    default:
222 		panic("nfsbioread: type %x unexpected", vp->v_type);
223 		break;
224 	    }
225 
226 	    if (n > 0) {
227 		if (!baddr)
228 			baddr = bp->b_data;
229 		error = uiomove(baddr + on, (int)n, uio);
230 	    }
231 
232 	    if (vp->v_type == VLNK)
233 		n = 0;
234 
235 	    if (got_buf)
236 		brelse(bp);
237 	} while (error == 0 && uio->uio_resid > 0 && n > 0);
238 	return (error);
239 }
240 
241 /*
242  * Vnode op for write using bio
243  */
244 int
245 nfs_write(void *v)
246 {
247 	struct vop_write_args *ap = v;
248 	int biosize;
249 	struct uio *uio = ap->a_uio;
250 	struct proc *p = uio->uio_procp;
251 	struct vnode *vp = ap->a_vp;
252 	struct nfsnode *np = VTONFS(vp);
253 	struct ucred *cred = ap->a_cred;
254 	int ioflag = ap->a_ioflag;
255 	struct buf *bp;
256 	struct vattr vattr;
257 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
258 	daddr64_t lbn, bn;
259 	int n, on, error = 0, extended = 0, wrotedta = 0, truncated = 0;
260 
261 #ifdef DIAGNOSTIC
262 	if (uio->uio_rw != UIO_WRITE)
263 		panic("nfs_write mode");
264 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
265 		panic("nfs_write proc");
266 #endif
267 	if (vp->v_type != VREG)
268 		return (EIO);
269 	if (np->n_flag & NWRITEERR) {
270 		np->n_flag &= ~NWRITEERR;
271 		return (np->n_error);
272 	}
273 	if ((nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_GOTFSINFO)) == NFSMNT_NFSV3)
274 		(void)nfs_fsinfo(nmp, vp, cred, p);
275 	if (ioflag & (IO_APPEND | IO_SYNC)) {
276 		if (np->n_flag & NMODIFIED) {
277 			NFS_INVALIDATE_ATTRCACHE(np);
278 			error = nfs_vinvalbuf(vp, V_SAVE, cred, p);
279 			if (error)
280 				return (error);
281 		}
282 		if (ioflag & IO_APPEND) {
283 			NFS_INVALIDATE_ATTRCACHE(np);
284 			error = VOP_GETATTR(vp, &vattr, cred, p);
285 			if (error)
286 				return (error);
287 			uio->uio_offset = np->n_size;
288 		}
289 	}
290 	if (uio->uio_offset < 0)
291 		return (EINVAL);
292 	if (uio->uio_resid == 0)
293 		return (0);
294 	/*
295 	 * Maybe this should be above the vnode op call, but so long as
296 	 * file servers have no limits, i don't think it matters
297 	 */
298 	if (p && uio->uio_offset + uio->uio_resid >
299 	      p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
300 		psignal(p, SIGXFSZ);
301 		return (EFBIG);
302 	}
303 
304 	/*
305 	 * update the cache write creds for this node.
306 	 */
307 	if (np->n_wcred)
308 		crfree(np->n_wcred);
309 	np->n_wcred = cred;
310 	crhold(cred);
311 
312 	/*
313 	 * I use nm_rsize, not nm_wsize so that all buffer cache blocks
314 	 * will be the same size within a filesystem. nfs_writerpc will
315 	 * still use nm_wsize when sizing the rpc's.
316 	 */
317 	biosize = nmp->nm_rsize;
318 	do {
319 
320 		/*
321 		 * XXX make sure we aren't cached in the VM page cache
322 		 */
323 		uvm_vnp_uncache(vp);
324 
325 		nfsstats.biocache_writes++;
326 		lbn = uio->uio_offset / biosize;
327 		on = uio->uio_offset & (biosize-1);
328 		n = min((unsigned)(biosize - on), uio->uio_resid);
329 		bn = lbn * (biosize / DEV_BSIZE);
330 again:
331 		bp = nfs_getcacheblk(vp, bn, biosize, p);
332 		if (!bp)
333 			return (EINTR);
334 		np->n_flag |= NMODIFIED;
335 		if (uio->uio_offset + n > np->n_size) {
336 			np->n_size = uio->uio_offset + n;
337 			uvm_vnp_setsize(vp, (u_long)np->n_size);
338 			extended = 1;
339 		} else if (uio->uio_offset + n < np->n_size)
340 			truncated = 1;
341 
342 		/*
343 		 * If the new write will leave a contiguous dirty
344 		 * area, just update the b_dirtyoff and b_dirtyend,
345 		 * otherwise force a write rpc of the old dirty area.
346 		 */
347 		if (bp->b_dirtyend > 0 &&
348 		    (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
349 			bp->b_proc = p;
350 			if (VOP_BWRITE(bp) == EINTR)
351 				return (EINTR);
352 			goto again;
353 		}
354 
355 		error = uiomove((char *)bp->b_data + on, n, uio);
356 		if (error) {
357 			bp->b_flags |= B_ERROR;
358 			brelse(bp);
359 			return (error);
360 		}
361 		if (bp->b_dirtyend > 0) {
362 			bp->b_dirtyoff = min(on, bp->b_dirtyoff);
363 			bp->b_dirtyend = max((on + n), bp->b_dirtyend);
364 		} else {
365 			bp->b_dirtyoff = on;
366 			bp->b_dirtyend = on + n;
367 		}
368 		if (bp->b_validend == 0 || bp->b_validend < bp->b_dirtyoff ||
369 		    bp->b_validoff > bp->b_dirtyend) {
370 			bp->b_validoff = bp->b_dirtyoff;
371 			bp->b_validend = bp->b_dirtyend;
372 		} else {
373 			bp->b_validoff = min(bp->b_validoff, bp->b_dirtyoff);
374 			bp->b_validend = max(bp->b_validend, bp->b_dirtyend);
375 		}
376 
377 		wrotedta = 1;
378 
379 		/*
380 		 * Since this block is being modified, it must be written
381 		 * again and not just committed.
382 		 */
383 
384 		if (NFS_ISV3(vp)) {
385 			rw_enter_write(&np->n_commitlock);
386 			if (bp->b_flags & B_NEEDCOMMIT) {
387 				bp->b_flags &= ~B_NEEDCOMMIT;
388 				nfs_del_tobecommitted_range(vp, bp);
389 			}
390 			nfs_del_committed_range(vp, bp);
391 			rw_exit_write(&np->n_commitlock);
392 		} else
393 			bp->b_flags &= ~B_NEEDCOMMIT;
394 
395 		if (ioflag & IO_SYNC) {
396 			bp->b_proc = p;
397 			error = VOP_BWRITE(bp);
398 			if (error)
399 				return (error);
400 		} else if ((n + on) == biosize) {
401 			bp->b_proc = NULL;
402 			bp->b_flags |= B_ASYNC;
403 			(void)nfs_writebp(bp, 0);
404 		} else {
405 			bdwrite(bp);
406 		}
407 	} while (uio->uio_resid > 0 && n > 0);
408 
409 	if (wrotedta)
410 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0) |
411 		    (truncated ? NOTE_TRUNCATE : 0));
412 
413 	return (0);
414 }
415 
416 /*
417  * Get an nfs cache block.
418  * Allocate a new one if the block isn't currently in the cache
419  * and return the block marked busy. If the calling process is
420  * interrupted by a signal for an interruptible mount point, return
421  * NULL.
422  */
423 struct buf *
424 nfs_getcacheblk(struct vnode *vp, daddr64_t bn, int size, struct proc *p)
425 {
426 	struct buf *bp;
427 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
428 
429 	if (nmp->nm_flag & NFSMNT_INT) {
430 		bp = getblk(vp, bn, size, PCATCH, 0);
431 		while (bp == NULL) {
432 			if (nfs_sigintr(nmp, NULL, p))
433 				return (NULL);
434 			bp = getblk(vp, bn, size, 0, 2 * hz);
435 		}
436 	} else
437 		bp = getblk(vp, bn, size, 0, 0);
438 	return (bp);
439 }
440 
441 /*
442  * Flush and invalidate all dirty buffers. If another process is already
443  * doing the flush, just wait for completion.
444  */
445 int
446 nfs_vinvalbuf(struct vnode *vp, int flags, struct ucred *cred, struct proc *p)
447 {
448 	struct nfsmount		*nmp= VFSTONFS(vp->v_mount);
449 	struct nfsnode		*np = VTONFS(vp);
450 	int			 error, sintr, stimeo;
451 
452 	error = sintr = stimeo = 0;
453 
454 	if (ISSET(nmp->nm_flag, NFSMNT_INT)) {
455 		sintr = PCATCH;
456 		stimeo = 2 * hz;
457 	}
458 
459 	/* First wait for any other process doing a flush to complete. */
460 	while (np->n_flag & NFLUSHINPROG) {
461 		np->n_flag |= NFLUSHWANT;
462 		error = tsleep(&np->n_flag, PRIBIO|sintr, "nfsvinval", stimeo);
463 		if (error && sintr && nfs_sigintr(nmp, NULL, p))
464 			return (EINTR);
465 	}
466 
467 	/* Now, flush as required. */
468 	np->n_flag |= NFLUSHINPROG;
469 	error = vinvalbuf(vp, flags, cred, p, sintr, 0);
470 	while (error) {
471 		if (sintr && nfs_sigintr(nmp, NULL, p)) {
472 			np->n_flag &= ~NFLUSHINPROG;
473 			if (np->n_flag & NFLUSHWANT) {
474 				np->n_flag &= ~NFLUSHWANT;
475 				wakeup(&np->n_flag);
476 			}
477 			return (EINTR);
478 		}
479 		error = vinvalbuf(vp, flags, cred, p, 0, stimeo);
480 	}
481 	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
482 	if (np->n_flag & NFLUSHWANT) {
483 		np->n_flag &= ~NFLUSHWANT;
484 		wakeup(&np->n_flag);
485 	}
486 	return (0);
487 }
488 
489 /*
490  * Initiate asynchronous I/O. Return an error if no nfsiods are available.
491  * This is mainly to avoid queueing async I/O requests when the nfsiods
492  * are all hung on a dead server.
493  */
494 int
495 nfs_asyncio(struct buf *bp, int readahead)
496 {
497 	if (nfs_numasync == 0)
498 		goto out;
499 
500 	while (nfs_bufqlen > nfs_bufqmax)
501 		if (readahead)
502 			goto out;
503 		else
504 			tsleep(&nfs_bufqlen, PRIBIO, "nfs_bufq", 0);
505 
506 	if ((bp->b_flags & B_READ) == 0) {
507 		bp->b_flags |= B_WRITEINPROG;
508 	}
509 
510 	TAILQ_INSERT_TAIL(&nfs_bufq, bp, b_freelist);
511 	nfs_bufqlen++;
512 
513 	wakeup_one(&nfs_bufq);
514 	return (0);
515 
516 out:
517 	nfsstats.forcedsync++;
518 	return (EIO);
519 }
520 
521 /*
522  * Do an I/O operation to/from a cache block. This may be called
523  * synchronously or from an nfsiod.
524  */
525 int
526 nfs_doio(struct buf *bp, struct proc *p)
527 {
528 	struct uio *uiop;
529 	struct vnode *vp;
530 	struct nfsnode *np;
531 	struct nfsmount *nmp;
532 	int s, error = 0, diff, len, iomode, must_commit = 0;
533 	struct uio uio;
534 	struct iovec io;
535 
536 	vp = bp->b_vp;
537 	np = VTONFS(vp);
538 	nmp = VFSTONFS(vp->v_mount);
539 	uiop = &uio;
540 	uiop->uio_iov = &io;
541 	uiop->uio_iovcnt = 1;
542 	uiop->uio_segflg = UIO_SYSSPACE;
543 	uiop->uio_procp = p;
544 
545 	/*
546 	 * Historically, paging was done with physio, but no more.
547 	 */
548 	if (bp->b_flags & B_PHYS) {
549 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
550 	    /* mapping was done by vmapbuf() */
551 	    io.iov_base = bp->b_data;
552 	    uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
553 	    if (bp->b_flags & B_READ) {
554 		uiop->uio_rw = UIO_READ;
555 		nfsstats.read_physios++;
556 		error = nfs_readrpc(vp, uiop);
557 	    } else {
558 		iomode = NFSV3WRITE_DATASYNC;
559 		uiop->uio_rw = UIO_WRITE;
560 		nfsstats.write_physios++;
561 		error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
562 	    }
563 	    if (error) {
564 		bp->b_flags |= B_ERROR;
565 		bp->b_error = error;
566 	    }
567 	} else if (bp->b_flags & B_READ) {
568 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
569 	    io.iov_base = bp->b_data;
570 	    uiop->uio_rw = UIO_READ;
571 	    switch (vp->v_type) {
572 	    case VREG:
573 		uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
574 		nfsstats.read_bios++;
575 		bcstats.pendingreads++;
576 		bcstats.numreads++;
577 		error = nfs_readrpc(vp, uiop);
578 		if (!error) {
579 		    bp->b_validoff = 0;
580 		    if (uiop->uio_resid) {
581 			/*
582 			 * If len > 0, there is a hole in the file and
583 			 * no writes after the hole have been pushed to
584 			 * the server yet.
585 			 * Just zero fill the rest of the valid area.
586 			 */
587 			diff = bp->b_bcount - uiop->uio_resid;
588 			len = np->n_size - ((((off_t)bp->b_blkno) << DEV_BSHIFT)
589 				+ diff);
590 			if (len > 0) {
591 			    len = min(len, uiop->uio_resid);
592 			    bzero((char *)bp->b_data + diff, len);
593 			    bp->b_validend = diff + len;
594 			} else
595 			    bp->b_validend = diff;
596 		    } else
597 			bp->b_validend = bp->b_bcount;
598 		}
599 		if (p && (vp->v_flag & VTEXT) &&
600 		    (timespeccmp(&np->n_mtime, &np->n_vattr.va_mtime, !=))) {
601 			uprintf("Process killed due to text file modification\n");
602 			psignal(p, SIGKILL);
603 		}
604 		break;
605 	    case VLNK:
606 		uiop->uio_offset = (off_t)0;
607 		nfsstats.readlink_bios++;
608 		bcstats.pendingreads++;
609 		bcstats.numreads++;
610 		error = nfs_readlinkrpc(vp, uiop, curproc->p_ucred);
611 		break;
612 	    default:
613 		panic("nfs_doio:  type %x unexpected", vp->v_type);
614 		break;
615 	    };
616 	    if (error) {
617 		bp->b_flags |= B_ERROR;
618 		bp->b_error = error;
619 	    }
620 	} else {
621 	    io.iov_len = uiop->uio_resid = bp->b_dirtyend
622 		- bp->b_dirtyoff;
623 	    uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE
624 		+ bp->b_dirtyoff;
625 	    io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
626 	    uiop->uio_rw = UIO_WRITE;
627 	    nfsstats.write_bios++;
628 	    bcstats.pendingwrites++;
629 	    bcstats.numwrites++;
630 	    if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE)) == B_ASYNC)
631 		iomode = NFSV3WRITE_UNSTABLE;
632 	    else
633 		iomode = NFSV3WRITE_FILESYNC;
634 	    bp->b_flags |= B_WRITEINPROG;
635 	    error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
636 
637 	    rw_enter_write(&np->n_commitlock);
638 	    if (!error && iomode == NFSV3WRITE_UNSTABLE) {
639 		bp->b_flags |= B_NEEDCOMMIT;
640 		nfs_add_tobecommitted_range(vp, bp);
641 	    } else {
642 		bp->b_flags &= ~B_NEEDCOMMIT;
643 		nfs_del_committed_range(vp, bp);
644 	    }
645 	    rw_exit_write(&np->n_commitlock);
646 
647 	    bp->b_flags &= ~B_WRITEINPROG;
648 
649 	    /*
650 	     * For an interrupted write, the buffer is still valid and the
651 	     * write hasn't been pushed to the server yet, so we can't set
652 	     * B_ERROR and report the interruption by setting B_EINTR. For
653 	     * the B_ASYNC case, B_EINTR is not relevant, so the rpc attempt
654 	     * is essentially a noop.
655 	     * For the case of a V3 write rpc not being committed to stable
656 	     * storage, the block is still dirty and requires either a commit
657 	     * rpc or another write rpc with iomode == NFSV3WRITE_FILESYNC
658 	     * before the block is reused. This is indicated by setting the
659 	     * B_DELWRI and B_NEEDCOMMIT flags.
660 	     */
661 	    if (error == EINTR || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
662 		    s = splbio();
663 		    buf_dirty(bp);
664 		    splx(s);
665 
666 		    if (!(bp->b_flags & B_ASYNC) && error)
667 			    bp->b_flags |= B_EINTR;
668 	    } else {
669 		if (error) {
670 		    bp->b_flags |= B_ERROR;
671 		    bp->b_error = np->n_error = error;
672 		    np->n_flag |= NWRITEERR;
673 		}
674 		bp->b_dirtyoff = bp->b_dirtyend = 0;
675 	    }
676 	}
677 	bp->b_resid = uiop->uio_resid;
678 	if (must_commit)
679 		nfs_clearcommit(vp->v_mount);
680 	s = splbio();
681 	biodone(bp);
682 	splx(s);
683 	return (error);
684 }
685