xref: /openbsd-src/sys/nfs/nfs_bio.c (revision 5054e3e78af0749a9bb00ba9a024b3ee2d90290f)
1 /*	$OpenBSD: nfs_bio.c,v 1.68 2009/10/19 22:24:18 jsg 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)) {
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 		printf(" nfsbioread: type %x unexpected\n",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 	    switch (vp->v_type) {
232 	    case VREG:
233 		break;
234 	    case VLNK:
235 		n = 0;
236 		break;
237 	    default:
238 		printf(" nfsbioread: type %x unexpected\n",vp->v_type);
239 	    }
240 	    if (got_buf)
241 		brelse(bp);
242 	} while (error == 0 && uio->uio_resid > 0 && n > 0);
243 	return (error);
244 }
245 
246 /*
247  * Vnode op for write using bio
248  */
249 int
250 nfs_write(void *v)
251 {
252 	struct vop_write_args *ap = v;
253 	int biosize;
254 	struct uio *uio = ap->a_uio;
255 	struct proc *p = uio->uio_procp;
256 	struct vnode *vp = ap->a_vp;
257 	struct nfsnode *np = VTONFS(vp);
258 	struct ucred *cred = ap->a_cred;
259 	int ioflag = ap->a_ioflag;
260 	struct buf *bp;
261 	struct vattr vattr;
262 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
263 	daddr64_t lbn, bn;
264 	int n, on, error = 0, extended = 0, wrotedta = 0, truncated = 0;
265 
266 #ifdef DIAGNOSTIC
267 	if (uio->uio_rw != UIO_WRITE)
268 		panic("nfs_write mode");
269 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
270 		panic("nfs_write proc");
271 #endif
272 	if (vp->v_type != VREG)
273 		return (EIO);
274 	if (np->n_flag & NWRITEERR) {
275 		np->n_flag &= ~NWRITEERR;
276 		return (np->n_error);
277 	}
278 	if ((nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_GOTFSINFO)) == NFSMNT_NFSV3)
279 		(void)nfs_fsinfo(nmp, vp, cred, p);
280 	if (ioflag & (IO_APPEND | IO_SYNC)) {
281 		if (np->n_flag & NMODIFIED) {
282 			NFS_INVALIDATE_ATTRCACHE(np);
283 			error = nfs_vinvalbuf(vp, V_SAVE, cred, p);
284 			if (error)
285 				return (error);
286 		}
287 		if (ioflag & IO_APPEND) {
288 			NFS_INVALIDATE_ATTRCACHE(np);
289 			error = VOP_GETATTR(vp, &vattr, cred, p);
290 			if (error)
291 				return (error);
292 			uio->uio_offset = np->n_size;
293 		}
294 	}
295 	if (uio->uio_offset < 0)
296 		return (EINVAL);
297 	if (uio->uio_resid == 0)
298 		return (0);
299 	/*
300 	 * Maybe this should be above the vnode op call, but so long as
301 	 * file servers have no limits, i don't think it matters
302 	 */
303 	if (p && uio->uio_offset + uio->uio_resid >
304 	      p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
305 		psignal(p, SIGXFSZ);
306 		return (EFBIG);
307 	}
308 
309 	/*
310 	 * update the cache write creds for this node.
311 	 */
312 	if (np->n_wcred)
313 		crfree(np->n_wcred);
314 	np->n_wcred = cred;
315 	crhold(cred);
316 
317 	/*
318 	 * I use nm_rsize, not nm_wsize so that all buffer cache blocks
319 	 * will be the same size within a filesystem. nfs_writerpc will
320 	 * still use nm_wsize when sizing the rpc's.
321 	 */
322 	biosize = nmp->nm_rsize;
323 	do {
324 
325 		/*
326 		 * XXX make sure we aren't cached in the VM page cache
327 		 */
328 		uvm_vnp_uncache(vp);
329 
330 		nfsstats.biocache_writes++;
331 		lbn = uio->uio_offset / biosize;
332 		on = uio->uio_offset & (biosize-1);
333 		n = min((unsigned)(biosize - on), uio->uio_resid);
334 		bn = lbn * (biosize / DEV_BSIZE);
335 again:
336 		bp = nfs_getcacheblk(vp, bn, biosize, p);
337 		if (!bp)
338 			return (EINTR);
339 		np->n_flag |= NMODIFIED;
340 		if (uio->uio_offset + n > np->n_size) {
341 			np->n_size = uio->uio_offset + n;
342 			uvm_vnp_setsize(vp, (u_long)np->n_size);
343 			extended = 1;
344 		} else if (uio->uio_offset + n < np->n_size)
345 			truncated = 1;
346 
347 		/*
348 		 * If the new write will leave a contiguous dirty
349 		 * area, just update the b_dirtyoff and b_dirtyend,
350 		 * otherwise force a write rpc of the old dirty area.
351 		 */
352 		if (bp->b_dirtyend > 0 &&
353 		    (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
354 			bp->b_proc = p;
355 			if (VOP_BWRITE(bp) == EINTR)
356 				return (EINTR);
357 			goto again;
358 		}
359 
360 		error = uiomove((char *)bp->b_data + on, n, uio);
361 		if (error) {
362 			bp->b_flags |= B_ERROR;
363 			brelse(bp);
364 			return (error);
365 		}
366 		if (bp->b_dirtyend > 0) {
367 			bp->b_dirtyoff = min(on, bp->b_dirtyoff);
368 			bp->b_dirtyend = max((on + n), bp->b_dirtyend);
369 		} else {
370 			bp->b_dirtyoff = on;
371 			bp->b_dirtyend = on + n;
372 		}
373 		if (bp->b_validend == 0 || bp->b_validend < bp->b_dirtyoff ||
374 		    bp->b_validoff > bp->b_dirtyend) {
375 			bp->b_validoff = bp->b_dirtyoff;
376 			bp->b_validend = bp->b_dirtyend;
377 		} else {
378 			bp->b_validoff = min(bp->b_validoff, bp->b_dirtyoff);
379 			bp->b_validend = max(bp->b_validend, bp->b_dirtyend);
380 		}
381 
382 		wrotedta = 1;
383 
384 		/*
385 		 * Since this block is being modified, it must be written
386 		 * again and not just committed.
387 		 */
388 
389 		if (NFS_ISV3(vp)) {
390 			rw_enter_write(&np->n_commitlock);
391 			if (bp->b_flags & B_NEEDCOMMIT) {
392 				bp->b_flags &= ~B_NEEDCOMMIT;
393 				nfs_del_tobecommitted_range(vp, bp);
394 			}
395 			nfs_del_committed_range(vp, bp);
396 			rw_exit_write(&np->n_commitlock);
397 		} else
398 			bp->b_flags &= ~B_NEEDCOMMIT;
399 
400 		if (ioflag & IO_SYNC) {
401 			bp->b_proc = p;
402 			error = VOP_BWRITE(bp);
403 			if (error)
404 				return (error);
405 		} else if ((n + on) == biosize) {
406 			bp->b_proc = NULL;
407 			bp->b_flags |= B_ASYNC;
408 			(void)nfs_writebp(bp, 0);
409 		} else {
410 			bdwrite(bp);
411 		}
412 	} while (uio->uio_resid > 0 && n > 0);
413 
414 	if (wrotedta)
415 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0) |
416 		    (truncated ? NOTE_TRUNCATE : 0));
417 
418 	return (0);
419 }
420 
421 /*
422  * Get an nfs cache block.
423  * Allocate a new one if the block isn't currently in the cache
424  * and return the block marked busy. If the calling process is
425  * interrupted by a signal for an interruptible mount point, return
426  * NULL.
427  */
428 struct buf *
429 nfs_getcacheblk(struct vnode *vp, daddr64_t bn, int size, struct proc *p)
430 {
431 	struct buf *bp;
432 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
433 
434 	if (nmp->nm_flag & NFSMNT_INT) {
435 		bp = getblk(vp, bn, size, PCATCH, 0);
436 		while (bp == NULL) {
437 			if (nfs_sigintr(nmp, NULL, p))
438 				return (NULL);
439 			bp = getblk(vp, bn, size, 0, 2 * hz);
440 		}
441 	} else
442 		bp = getblk(vp, bn, size, 0, 0);
443 	return (bp);
444 }
445 
446 /*
447  * Flush and invalidate all dirty buffers. If another process is already
448  * doing the flush, just wait for completion.
449  */
450 int
451 nfs_vinvalbuf(struct vnode *vp, int flags, struct ucred *cred, struct proc *p)
452 {
453 	struct nfsmount		*nmp= VFSTONFS(vp->v_mount);
454 	struct nfsnode		*np = VTONFS(vp);
455 	int			 error, sintr, stimeo;
456 
457 	error = sintr = stimeo = 0;
458 
459 	if (ISSET(nmp->nm_flag, NFSMNT_INT)) {
460 		sintr = PCATCH;
461 		stimeo = 2 * hz;
462 	}
463 
464 	/* First wait for any other process doing a flush to complete. */
465 	while (np->n_flag & NFLUSHINPROG) {
466 		np->n_flag |= NFLUSHWANT;
467 		error = tsleep(&np->n_flag, PRIBIO|sintr, "nfsvinval", stimeo);
468 		if (error && sintr && nfs_sigintr(nmp, NULL, p))
469 			return (EINTR);
470 	}
471 
472 	/* Now, flush as required. */
473 	np->n_flag |= NFLUSHINPROG;
474 	error = vinvalbuf(vp, flags, cred, p, sintr, 0);
475 	while (error) {
476 		if (sintr && nfs_sigintr(nmp, NULL, p)) {
477 			np->n_flag &= ~NFLUSHINPROG;
478 			if (np->n_flag & NFLUSHWANT) {
479 				np->n_flag &= ~NFLUSHWANT;
480 				wakeup(&np->n_flag);
481 			}
482 			return (EINTR);
483 		}
484 		error = vinvalbuf(vp, flags, cred, p, 0, stimeo);
485 	}
486 	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
487 	if (np->n_flag & NFLUSHWANT) {
488 		np->n_flag &= ~NFLUSHWANT;
489 		wakeup(&np->n_flag);
490 	}
491 	return (0);
492 }
493 
494 /*
495  * Initiate asynchronous I/O. Return an error if no nfsiods are available.
496  * This is mainly to avoid queueing async I/O requests when the nfsiods
497  * are all hung on a dead server.
498  */
499 int
500 nfs_asyncio(struct buf *bp)
501 {
502 	if (nfs_numasync == 0)
503 		goto out;
504 
505 	if (nfs_bufqlen > nfs_bufqmax)
506 		goto out; /* too many bufs in use, force sync */
507 
508 	if ((bp->b_flags & B_READ) == 0) {
509 		bp->b_flags |= B_WRITEINPROG;
510 	}
511 
512 	TAILQ_INSERT_TAIL(&nfs_bufq, bp, b_freelist);
513 	nfs_bufqlen++;
514 
515 	wakeup_one(&nfs_bufq);
516 	return (0);
517 
518 out:
519 	nfsstats.forcedsync++;
520 	return (EIO);
521 }
522 
523 /*
524  * Do an I/O operation to/from a cache block. This may be called
525  * synchronously or from an nfsiod.
526  */
527 int
528 nfs_doio(struct buf *bp, struct proc *p)
529 {
530 	struct uio *uiop;
531 	struct vnode *vp;
532 	struct nfsnode *np;
533 	struct nfsmount *nmp;
534 	int s, error = 0, diff, len, iomode, must_commit = 0;
535 	struct uio uio;
536 	struct iovec io;
537 
538 	vp = bp->b_vp;
539 	np = VTONFS(vp);
540 	nmp = VFSTONFS(vp->v_mount);
541 	uiop = &uio;
542 	uiop->uio_iov = &io;
543 	uiop->uio_iovcnt = 1;
544 	uiop->uio_segflg = UIO_SYSSPACE;
545 	uiop->uio_procp = p;
546 
547 	/*
548 	 * Historically, paging was done with physio, but no more.
549 	 */
550 	if (bp->b_flags & B_PHYS) {
551 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
552 	    /* mapping was done by vmapbuf() */
553 	    io.iov_base = bp->b_data;
554 	    uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
555 	    if (bp->b_flags & B_READ) {
556 		uiop->uio_rw = UIO_READ;
557 		nfsstats.read_physios++;
558 		error = nfs_readrpc(vp, uiop);
559 	    } else {
560 		iomode = NFSV3WRITE_DATASYNC;
561 		uiop->uio_rw = UIO_WRITE;
562 		nfsstats.write_physios++;
563 		error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
564 	    }
565 	    if (error) {
566 		bp->b_flags |= B_ERROR;
567 		bp->b_error = error;
568 	    }
569 	} else if (bp->b_flags & B_READ) {
570 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
571 	    io.iov_base = bp->b_data;
572 	    uiop->uio_rw = UIO_READ;
573 	    switch (vp->v_type) {
574 	    case VREG:
575 		uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
576 		nfsstats.read_bios++;
577 		bcstats.pendingreads++;
578 		bcstats.numreads++;
579 		error = nfs_readrpc(vp, uiop);
580 		if (!error) {
581 		    bp->b_validoff = 0;
582 		    if (uiop->uio_resid) {
583 			/*
584 			 * If len > 0, there is a hole in the file and
585 			 * no writes after the hole have been pushed to
586 			 * the server yet.
587 			 * Just zero fill the rest of the valid area.
588 			 */
589 			diff = bp->b_bcount - uiop->uio_resid;
590 			len = np->n_size - ((((off_t)bp->b_blkno) << DEV_BSHIFT)
591 				+ diff);
592 			if (len > 0) {
593 			    len = min(len, uiop->uio_resid);
594 			    bzero((char *)bp->b_data + diff, len);
595 			    bp->b_validend = diff + len;
596 			} else
597 			    bp->b_validend = diff;
598 		    } else
599 			bp->b_validend = bp->b_bcount;
600 		}
601 		if (p && (vp->v_flag & VTEXT) &&
602 		    (timespeccmp(&np->n_mtime, &np->n_vattr.va_mtime, !=))) {
603 			uprintf("Process killed due to text file modification\n");
604 			psignal(p, SIGKILL);
605 		}
606 		break;
607 	    case VLNK:
608 		uiop->uio_offset = (off_t)0;
609 		nfsstats.readlink_bios++;
610 		bcstats.pendingreads++;
611 		bcstats.numreads++;
612 		error = nfs_readlinkrpc(vp, uiop, curproc->p_ucred);
613 		break;
614 	    default:
615 		printf("nfs_doio:  type %x unexpected\n", vp->v_type);
616 		break;
617 	    };
618 	    if (error) {
619 		bp->b_flags |= B_ERROR;
620 		bp->b_error = error;
621 	    }
622 	} else {
623 	    io.iov_len = uiop->uio_resid = bp->b_dirtyend
624 		- bp->b_dirtyoff;
625 	    uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE
626 		+ bp->b_dirtyoff;
627 	    io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
628 	    uiop->uio_rw = UIO_WRITE;
629 	    nfsstats.write_bios++;
630 	    bcstats.pendingwrites++;
631 	    bcstats.numwrites++;
632 	    if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE)) == B_ASYNC)
633 		iomode = NFSV3WRITE_UNSTABLE;
634 	    else
635 		iomode = NFSV3WRITE_FILESYNC;
636 	    bp->b_flags |= B_WRITEINPROG;
637 	    error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
638 
639 	    rw_enter_write(&np->n_commitlock);
640 	    if (!error && iomode == NFSV3WRITE_UNSTABLE) {
641 		bp->b_flags |= B_NEEDCOMMIT;
642 		nfs_add_tobecommitted_range(vp, bp);
643 	    } else {
644 		bp->b_flags &= ~B_NEEDCOMMIT;
645 		nfs_del_committed_range(vp, bp);
646 	    }
647 	    rw_exit_write(&np->n_commitlock);
648 
649 	    bp->b_flags &= ~B_WRITEINPROG;
650 
651 	    /*
652 	     * For an interrupted write, the buffer is still valid and the
653 	     * write hasn't been pushed to the server yet, so we can't set
654 	     * B_ERROR and report the interruption by setting B_EINTR. For
655 	     * the B_ASYNC case, B_EINTR is not relevant, so the rpc attempt
656 	     * is essentially a noop.
657 	     * For the case of a V3 write rpc not being committed to stable
658 	     * storage, the block is still dirty and requires either a commit
659 	     * rpc or another write rpc with iomode == NFSV3WRITE_FILESYNC
660 	     * before the block is reused. This is indicated by setting the
661 	     * B_DELWRI and B_NEEDCOMMIT flags.
662 	     */
663 	    if (error == EINTR || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
664 		    s = splbio();
665 		    buf_dirty(bp);
666 		    splx(s);
667 
668 		    if (!(bp->b_flags & B_ASYNC) && error)
669 			    bp->b_flags |= B_EINTR;
670 	    } else {
671 		if (error) {
672 		    bp->b_flags |= B_ERROR;
673 		    bp->b_error = np->n_error = error;
674 		    np->n_flag |= NWRITEERR;
675 		}
676 		bp->b_dirtyoff = bp->b_dirtyend = 0;
677 	    }
678 	}
679 	bp->b_resid = uiop->uio_resid;
680 	if (must_commit)
681 		nfs_clearcommit(vp->v_mount);
682 	s = splbio();
683 	biodone(bp);
684 	splx(s);
685 	return (error);
686 }
687