xref: /freebsd/sys/kern/vfs_cluster.c (revision 952d112864d8008aa87278a30a539d888a8493cd)
1 /*-
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * Modifications/enhancements:
5  * 	Copyright (c) 1995 John S. Dyson.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. 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  *	@(#)vfs_cluster.c	8.7 (Berkeley) 2/13/94
36  * $Id: vfs_cluster.c,v 1.43 1997/03/07 14:40:54 dyson Exp $
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/buf.h>
43 #include <sys/vnode.h>
44 #include <sys/mount.h>
45 #include <sys/malloc.h>
46 #include <sys/resourcevar.h>
47 #include <sys/vmmeter.h>
48 #include <miscfs/specfs/specdev.h>
49 #include <vm/vm.h>
50 #include <vm/vm_param.h>
51 #include <vm/vm_prot.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_page.h>
54 
55 #if defined(CLUSTERDEBUG)
56 #include <sys/sysctl.h>
57 #include <sys/kernel.h>
58 static int	rcluster= 0;
59 SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0, "");
60 #endif
61 
62 #ifdef notyet_block_reallocation_enabled
63 static struct cluster_save *
64 	cluster_collectbufs __P((struct vnode *vp, struct buf *last_bp));
65 #endif
66 static struct buf *
67 	cluster_rbuild __P((struct vnode *vp, u_quad_t filesize, daddr_t lbn,
68 			    daddr_t blkno, long size, int run, struct buf *fbp));
69 
70 extern vm_page_t	bogus_page;
71 
72 /*
73  * Maximum number of blocks for read-ahead.
74  */
75 #define MAXRA 32
76 
77 /*
78  * This replaces bread.
79  */
80 int
81 cluster_read(vp, filesize, lblkno, size, cred, totread, seqcount, bpp)
82 	struct vnode *vp;
83 	u_quad_t filesize;
84 	daddr_t lblkno;
85 	long size;
86 	struct ucred *cred;
87 	long totread;
88 	int seqcount;
89 	struct buf **bpp;
90 {
91 	struct buf *bp, *rbp, *reqbp;
92 	daddr_t blkno, rablkno, origblkno;
93 	int error, num_ra;
94 	int i;
95 	int maxra, racluster;
96 	long origtotread;
97 
98 	error = 0;
99 
100 	/*
101 	 * Try to limit the amount of read-ahead by a few
102 	 * ad-hoc parameters.  This needs work!!!
103 	 */
104 	racluster = MAXPHYS/size;
105 	maxra = 2 * racluster + (totread / size);
106 	if (maxra > MAXRA)
107 		maxra = MAXRA;
108 	if (maxra > nbuf/8)
109 		maxra = nbuf/8;
110 
111 	/*
112 	 * get the requested block
113 	 */
114 	*bpp = reqbp = bp = getblk(vp, lblkno, size, 0, 0);
115 	origblkno = lblkno;
116 	origtotread = totread;
117 
118 	/*
119 	 * if it is in the cache, then check to see if the reads have been
120 	 * sequential.  If they have, then try some read-ahead, otherwise
121 	 * back-off on prospective read-aheads.
122 	 */
123 	if (bp->b_flags & B_CACHE) {
124 		if (!seqcount) {
125 			return 0;
126 		} else if ((bp->b_flags & B_RAM) == 0) {
127 			return 0;
128 		} else {
129 			int s;
130 			struct buf *tbp;
131 			bp->b_flags &= ~B_RAM;
132 			/*
133 			 * We do the spl here so that there is no window
134 			 * between the incore and the b_usecount increment
135 			 * below.  We opt to keep the spl out of the loop
136 			 * for efficiency.
137 			 */
138 			s = splbio();
139 			for(i=1;i<maxra;i++) {
140 
141 				if (!(tbp = incore(vp, lblkno+i))) {
142 					break;
143 				}
144 
145 				/*
146 				 * Set another read-ahead mark so we know to check
147 				 * again.
148 				 */
149 				if (((i % racluster) == (racluster - 1)) ||
150 					(i == (maxra - 1)))
151 					tbp->b_flags |= B_RAM;
152 
153 #if 0
154 				if (tbp->b_usecount == 0) {
155 					/*
156 					 * Make sure that the soon-to-be used readaheads
157 					 * are still there.  The getblk/bqrelse pair will
158 					 * boost the priority of the buffer.
159 					 */
160 					tbp = getblk(vp, lblkno+i, size, 0, 0);
161 					bqrelse(tbp);
162 				}
163 #endif
164 			}
165 			splx(s);
166 			if (i >= maxra) {
167 				return 0;
168 			}
169 			lblkno += i;
170 		}
171 		reqbp = bp = NULL;
172 	} else {
173 		u_quad_t firstread;
174 		firstread = (u_quad_t) lblkno * size;
175 		if (firstread + totread > filesize)
176 			totread = filesize - firstread;
177 		if (totread > size) {
178 			int nblks = 0;
179 			int ncontigafter;
180 			while (totread > 0) {
181 				nblks++;
182 				totread -= size;
183 			}
184 			if (nblks == 1)
185 				goto single_block_read;
186 			if (nblks > racluster)
187 				nblks = racluster;
188 
189 	    		error = VOP_BMAP(vp, lblkno, NULL,
190 				&blkno, &ncontigafter, NULL);
191 			if (error)
192 				goto single_block_read;
193 			if (blkno == -1)
194 				goto single_block_read;
195 			if (ncontigafter == 0)
196 				goto single_block_read;
197 			if (ncontigafter + 1 < nblks)
198 				nblks = ncontigafter + 1;
199 
200 			bp = cluster_rbuild(vp, filesize, lblkno,
201 				blkno, size, nblks, bp);
202 			lblkno += nblks;
203 		} else {
204 single_block_read:
205 			/*
206 			 * if it isn't in the cache, then get a chunk from
207 			 * disk if sequential, otherwise just get the block.
208 			 */
209 			bp->b_flags |= B_READ | B_RAM;
210 			lblkno += 1;
211 		}
212 	}
213 
214 	/*
215 	 * if we have been doing sequential I/O, then do some read-ahead
216 	 */
217 	rbp = NULL;
218 	/* if (seqcount && (lblkno < (origblkno + maxra))) { */
219 	if (seqcount && (lblkno < (origblkno + seqcount))) {
220 		/*
221 		 * we now build the read-ahead buffer if it is desirable.
222 		 */
223 		if (((u_quad_t)(lblkno + 1) * size) <= filesize &&
224 		    !(error = VOP_BMAP(vp, lblkno, NULL, &blkno, &num_ra, NULL)) &&
225 		    blkno != -1) {
226 			int nblksread;
227 			int ntoread = num_ra + 1;
228 			nblksread = (origtotread + size - 1) / size;
229 			if (seqcount < nblksread)
230 				seqcount = nblksread;
231 			if (seqcount < ntoread)
232 				ntoread = seqcount;
233 			if (num_ra) {
234 				rbp = cluster_rbuild(vp, filesize, lblkno,
235 					blkno, size, ntoread, NULL);
236 			} else {
237 				rbp = getblk(vp, lblkno, size, 0, 0);
238 				rbp->b_flags |= B_READ | B_ASYNC | B_RAM;
239 				rbp->b_blkno = blkno;
240 			}
241 		}
242 	}
243 
244 	/*
245 	 * handle the synchronous read
246 	 */
247 	if (bp) {
248 		if (bp->b_flags & (B_DONE | B_DELWRI)) {
249 			panic("cluster_read: DONE bp");
250 		} else {
251 #if defined(CLUSTERDEBUG)
252 			if (rcluster)
253 				printf("S(%d,%d,%d) ",
254 					bp->b_lblkno, bp->b_bcount, seqcount);
255 #endif
256 			if ((bp->b_flags & B_CLUSTER) == 0)
257 				vfs_busy_pages(bp, 0);
258 			error = VOP_STRATEGY(bp);
259 			curproc->p_stats->p_ru.ru_inblock++;
260 		}
261 	}
262 	/*
263 	 * and if we have read-aheads, do them too
264 	 */
265 	if (rbp) {
266 		if (error) {
267 			rbp->b_flags &= ~(B_ASYNC | B_READ);
268 			brelse(rbp);
269 		} else if (rbp->b_flags & B_CACHE) {
270 			rbp->b_flags &= ~(B_ASYNC | B_READ);
271 			bqrelse(rbp);
272 		} else {
273 #if defined(CLUSTERDEBUG)
274 			if (rcluster) {
275 				if (bp)
276 					printf("A+(%d,%d,%d,%d) ",
277 					rbp->b_lblkno, rbp->b_bcount,
278 					rbp->b_lblkno - origblkno,
279 					seqcount);
280 				else
281 					printf("A(%d,%d,%d,%d) ",
282 					rbp->b_lblkno, rbp->b_bcount,
283 					rbp->b_lblkno - origblkno,
284 					seqcount);
285 			}
286 #endif
287 
288 			if ((rbp->b_flags & B_CLUSTER) == 0)
289 				vfs_busy_pages(rbp, 0);
290 			(void) VOP_STRATEGY(rbp);
291 			curproc->p_stats->p_ru.ru_inblock++;
292 		}
293 	}
294 	if (reqbp)
295 		return (biowait(reqbp));
296 	else
297 		return (error);
298 }
299 
300 /*
301  * If blocks are contiguous on disk, use this to provide clustered
302  * read ahead.  We will read as many blocks as possible sequentially
303  * and then parcel them up into logical blocks in the buffer hash table.
304  */
305 static struct buf *
306 cluster_rbuild(vp, filesize, lbn, blkno, size, run, fbp)
307 	struct vnode *vp;
308 	u_quad_t filesize;
309 	daddr_t lbn;
310 	daddr_t blkno;
311 	long size;
312 	int run;
313 	struct buf *fbp;
314 {
315 	struct buf *bp, *tbp;
316 	daddr_t bn;
317 	int i, inc, j;
318 
319 #ifdef DIAGNOSTIC
320 	if (size != vp->v_mount->mnt_stat.f_iosize)
321 		panic("cluster_rbuild: size %d != filesize %d\n",
322 		    size, vp->v_mount->mnt_stat.f_iosize);
323 #endif
324 	/*
325 	 * avoid a division
326 	 */
327 	while ((u_quad_t) size * (lbn + run) > filesize) {
328 		--run;
329 	}
330 
331 	if (fbp) {
332 		tbp = fbp;
333 		tbp->b_flags |= B_READ;
334 	} else {
335 		tbp = getblk(vp, lbn, size, 0, 0);
336 		if (tbp->b_flags & B_CACHE)
337 			return tbp;
338 		tbp->b_flags |= B_ASYNC | B_READ | B_RAM;
339 	}
340 
341 	tbp->b_blkno = blkno;
342 	if( (tbp->b_flags & B_MALLOC) ||
343 		((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
344 		return tbp;
345 
346 	bp = trypbuf();
347 	if (bp == 0)
348 		return tbp;
349 
350 	(vm_offset_t) bp->b_data |= ((vm_offset_t) tbp->b_data) & PAGE_MASK;
351 	bp->b_flags = B_ASYNC | B_READ | B_CALL | B_BUSY | B_CLUSTER | B_VMIO;
352 	bp->b_iodone = cluster_callback;
353 	bp->b_blkno = blkno;
354 	bp->b_lblkno = lbn;
355 	pbgetvp(vp, bp);
356 
357 	TAILQ_INIT(&bp->b_cluster.cluster_head);
358 
359 	bp->b_bcount = 0;
360 	bp->b_bufsize = 0;
361 	bp->b_npages = 0;
362 
363 	inc = btodb(size);
364 	for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
365 		if (i != 0) {
366 			if ((bp->b_npages * PAGE_SIZE) +
367 				round_page(size) > MAXPHYS)
368 				break;
369 
370 			if (incore(vp, lbn + i))
371 				break;
372 
373 			tbp = getblk(vp, lbn + i, size, 0, 0);
374 
375 			if ((tbp->b_flags & B_CACHE) ||
376 				(tbp->b_flags & B_VMIO) == 0) {
377 				bqrelse(tbp);
378 				break;
379 			}
380 
381 			for (j=0;j<tbp->b_npages;j++) {
382 				if (tbp->b_pages[j]->valid) {
383 					break;
384 				}
385 			}
386 
387 			if (j != tbp->b_npages) {
388 				/*
389 				 * force buffer to be re-constituted later
390 				 */
391 				tbp->b_flags |= B_RELBUF;
392 				brelse(tbp);
393 				break;
394 			}
395 
396 			if ((fbp && (i == 1)) || (i == (run - 1)))
397 				tbp->b_flags |= B_RAM;
398 			tbp->b_flags |= B_READ | B_ASYNC;
399 			if (tbp->b_blkno == tbp->b_lblkno) {
400 				tbp->b_blkno = bn;
401 			} else if (tbp->b_blkno != bn) {
402 				brelse(tbp);
403 				break;
404 			}
405 		}
406 		TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
407 			tbp, b_cluster.cluster_entry);
408 		for (j = 0; j < tbp->b_npages; j += 1) {
409 			vm_page_t m;
410 			m = tbp->b_pages[j];
411 			++m->busy;
412 			++m->object->paging_in_progress;
413 			if ((bp->b_npages == 0) ||
414 				(bp->b_pages[bp->b_npages-1] != m)) {
415 				bp->b_pages[bp->b_npages] = m;
416 				bp->b_npages++;
417 			}
418 			if ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL)
419 				tbp->b_pages[j] = bogus_page;
420 		}
421 		bp->b_bcount += tbp->b_bcount;
422 		bp->b_bufsize += tbp->b_bufsize;
423 	}
424 
425 	for(j=0;j<bp->b_npages;j++) {
426 		if ((bp->b_pages[j]->valid & VM_PAGE_BITS_ALL) ==
427 			VM_PAGE_BITS_ALL)
428 			bp->b_pages[j] = bogus_page;
429 	}
430 	if (bp->b_bufsize > bp->b_kvasize)
431 		panic("cluster_rbuild: b_bufsize(%d) > b_kvasize(%d)\n",
432 			bp->b_bufsize, bp->b_kvasize);
433 	bp->b_kvasize = bp->b_bufsize;
434 
435 	pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
436 		(vm_page_t *)bp->b_pages, bp->b_npages);
437 	return (bp);
438 }
439 
440 /*
441  * Cleanup after a clustered read or write.
442  * This is complicated by the fact that any of the buffers might have
443  * extra memory (if there were no empty buffer headers at allocbuf time)
444  * that we will need to shift around.
445  */
446 void
447 cluster_callback(bp)
448 	struct buf *bp;
449 {
450 	struct buf *nbp, *tbp;
451 	int error = 0;
452 
453 	/*
454 	 * Must propogate errors to all the components.
455 	 */
456 	if (bp->b_flags & B_ERROR)
457 		error = bp->b_error;
458 
459 	pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages);
460 	/*
461 	 * Move memory from the large cluster buffer into the component
462 	 * buffers and mark IO as done on these.
463 	 */
464 	for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head);
465 		tbp; tbp = nbp) {
466 		nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry);
467 		if (error) {
468 			tbp->b_flags |= B_ERROR;
469 			tbp->b_error = error;
470 		}
471 		biodone(tbp);
472 	}
473 	relpbuf(bp);
474 }
475 
476 /*
477  * Do clustered write for FFS.
478  *
479  * Three cases:
480  *	1. Write is not sequential (write asynchronously)
481  *	Write is sequential:
482  *	2.	beginning of cluster - begin cluster
483  *	3.	middle of a cluster - add to cluster
484  *	4.	end of a cluster - asynchronously write cluster
485  */
486 void
487 cluster_write(bp, filesize)
488 	struct buf *bp;
489 	u_quad_t filesize;
490 {
491 	struct vnode *vp;
492 	daddr_t lbn;
493 	int maxclen, cursize;
494 	int lblocksize;
495 	int async;
496 
497 	vp = bp->b_vp;
498 	async = vp->v_mount->mnt_flag & MNT_ASYNC;
499 	lblocksize = vp->v_mount->mnt_stat.f_iosize;
500 	lbn = bp->b_lblkno;
501 
502 	/* Initialize vnode to beginning of file. */
503 	if (lbn == 0)
504 		vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
505 
506 	if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 ||
507 	    (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) {
508 		maxclen = MAXPHYS / lblocksize - 1;
509 		if (vp->v_clen != 0) {
510 			/*
511 			 * Next block is not sequential.
512 			 *
513 			 * If we are not writing at end of file, the process
514 			 * seeked to another point in the file since its last
515 			 * write, or we have reached our maximum cluster size,
516 			 * then push the previous cluster. Otherwise try
517 			 * reallocating to make it sequential.
518 			 */
519 			cursize = vp->v_lastw - vp->v_cstart + 1;
520 #ifndef notyet_block_reallocation_enabled
521 			if (((u_quad_t)(lbn + 1) * lblocksize) != filesize ||
522 				lbn != vp->v_lastw + 1 ||
523 				vp->v_clen <= cursize) {
524 				if (!async)
525 					cluster_wbuild(vp, lblocksize,
526 						vp->v_cstart, cursize);
527 			}
528 #else
529 			if ((lbn + 1) * lblocksize != filesize ||
530 			    lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) {
531 				if (!async)
532 					cluster_wbuild(vp, lblocksize,
533 						vp->v_cstart, cursize);
534 			} else {
535 				struct buf **bpp, **endbp;
536 				struct cluster_save *buflist;
537 
538 				buflist = cluster_collectbufs(vp, bp);
539 				endbp = &buflist->bs_children
540 				    [buflist->bs_nchildren - 1];
541 				if (VOP_REALLOCBLKS(vp, buflist)) {
542 					/*
543 					 * Failed, push the previous cluster.
544 					 */
545 					for (bpp = buflist->bs_children;
546 					     bpp < endbp; bpp++)
547 						brelse(*bpp);
548 					free(buflist, M_SEGMENT);
549 					cluster_wbuild(vp, lblocksize,
550 					    vp->v_cstart, cursize);
551 				} else {
552 					/*
553 					 * Succeeded, keep building cluster.
554 					 */
555 					for (bpp = buflist->bs_children;
556 					     bpp <= endbp; bpp++)
557 						bdwrite(*bpp);
558 					free(buflist, M_SEGMENT);
559 					vp->v_lastw = lbn;
560 					vp->v_lasta = bp->b_blkno;
561 					return;
562 				}
563 			}
564 #endif /* notyet_block_reallocation_enabled */
565 		}
566 		/*
567 		 * Consider beginning a cluster. If at end of file, make
568 		 * cluster as large as possible, otherwise find size of
569 		 * existing cluster.
570 		 */
571 		if (((u_quad_t) (lbn + 1) * lblocksize) != filesize &&
572 		    (bp->b_blkno == bp->b_lblkno) &&
573 		    (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) ||
574 		     bp->b_blkno == -1)) {
575 			bawrite(bp);
576 			vp->v_clen = 0;
577 			vp->v_lasta = bp->b_blkno;
578 			vp->v_cstart = lbn + 1;
579 			vp->v_lastw = lbn;
580 			return;
581 		}
582 		vp->v_clen = maxclen;
583 		if (!async && maxclen == 0) {	/* I/O not contiguous */
584 			vp->v_cstart = lbn + 1;
585 			bawrite(bp);
586 		} else {	/* Wait for rest of cluster */
587 			vp->v_cstart = lbn;
588 			bdwrite(bp);
589 		}
590 	} else if (lbn == vp->v_cstart + vp->v_clen) {
591 		/*
592 		 * At end of cluster, write it out.
593 		 */
594 		bdwrite(bp);
595 		cluster_wbuild(vp, lblocksize, vp->v_cstart, vp->v_clen + 1);
596 		vp->v_clen = 0;
597 		vp->v_cstart = lbn + 1;
598 	} else
599 		/*
600 		 * In the middle of a cluster, so just delay the I/O for now.
601 		 */
602 		bdwrite(bp);
603 	vp->v_lastw = lbn;
604 	vp->v_lasta = bp->b_blkno;
605 }
606 
607 
608 /*
609  * This is an awful lot like cluster_rbuild...wish they could be combined.
610  * The last lbn argument is the current block on which I/O is being
611  * performed.  Check to see that it doesn't fall in the middle of
612  * the current block (if last_bp == NULL).
613  */
614 int
615 cluster_wbuild(vp, size, start_lbn, len)
616 	struct vnode *vp;
617 	long size;
618 	daddr_t start_lbn;
619 	int len;
620 {
621 	struct buf *bp, *tbp;
622 	int i, j, s;
623 	int totalwritten = 0;
624 	int dbsize = btodb(size);
625 	while (len > 0) {
626 		s = splbio();
627 		if ( ((tbp = gbincore(vp, start_lbn)) == NULL) ||
628 			((tbp->b_flags & (B_INVAL|B_BUSY|B_DELWRI)) != B_DELWRI)) {
629 			++start_lbn;
630 			--len;
631 			splx(s);
632 			continue;
633 		}
634 		bremfree(tbp);
635 		tbp->b_flags |= B_BUSY;
636 		tbp->b_flags &= ~B_DONE;
637 		splx(s);
638 
639 	/*
640 	 * Extra memory in the buffer, punt on this buffer. XXX we could
641 	 * handle this in most cases, but we would have to push the extra
642 	 * memory down to after our max possible cluster size and then
643 	 * potentially pull it back up if the cluster was terminated
644 	 * prematurely--too much hassle.
645 	 */
646 		if (((tbp->b_flags & (B_CLUSTEROK|B_MALLOC)) != B_CLUSTEROK) ||
647 			(tbp->b_bcount != tbp->b_bufsize) ||
648 			(tbp->b_bcount != size) ||
649 			len == 1) {
650 			totalwritten += tbp->b_bufsize;
651 			bawrite(tbp);
652 			++start_lbn;
653 			--len;
654 			continue;
655 		}
656 
657 		bp = trypbuf();
658 		if (bp == NULL) {
659 			totalwritten += tbp->b_bufsize;
660 			bawrite(tbp);
661 			++start_lbn;
662 			--len;
663 			continue;
664 		}
665 
666 		TAILQ_INIT(&bp->b_cluster.cluster_head);
667 		bp->b_bcount = 0;
668 		bp->b_bufsize = 0;
669 		bp->b_npages = 0;
670 		if (tbp->b_wcred != NOCRED) {
671 		    bp->b_wcred = tbp->b_wcred;
672 		    crhold(bp->b_wcred);
673 		}
674 
675 		bp->b_blkno = tbp->b_blkno;
676 		bp->b_lblkno = tbp->b_lblkno;
677 		(vm_offset_t) bp->b_data |= ((vm_offset_t) tbp->b_data) & PAGE_MASK;
678 		bp->b_flags |= B_CALL | B_BUSY | B_CLUSTER | (tbp->b_flags & (B_VMIO|B_NEEDCOMMIT));
679 		bp->b_iodone = cluster_callback;
680 		pbgetvp(vp, bp);
681 
682 		for (i = 0; i < len; ++i, ++start_lbn) {
683 			if (i != 0) {
684 				s = splbio();
685 				if ((tbp = gbincore(vp, start_lbn)) == NULL) {
686 					splx(s);
687 					break;
688 				}
689 
690 				if ((tbp->b_flags & (B_VMIO|B_CLUSTEROK|B_INVAL|B_BUSY|B_DELWRI|B_NEEDCOMMIT)) != (B_DELWRI|B_CLUSTEROK|(bp->b_flags & (B_VMIO|B_NEEDCOMMIT)))) {
691 					splx(s);
692 					break;
693 				}
694 
695 				if (tbp->b_wcred != bp->b_wcred) {
696 					splx(s);
697 					break;
698 				}
699 
700 				if ((tbp->b_bcount != size) ||
701 					((bp->b_blkno + dbsize * i) != tbp->b_blkno) ||
702 					((tbp->b_npages + bp->b_npages) > (MAXPHYS / PAGE_SIZE))) {
703 					splx(s);
704 					break;
705 				}
706 				bremfree(tbp);
707 				tbp->b_flags |= B_BUSY;
708 				tbp->b_flags &= ~B_DONE;
709 				splx(s);
710 			}
711 			if (tbp->b_flags & B_VMIO) {
712 				for (j = 0; j < tbp->b_npages; j += 1) {
713 					vm_page_t m;
714 					m = tbp->b_pages[j];
715 					++m->busy;
716 					++m->object->paging_in_progress;
717 					if ((bp->b_npages == 0) ||
718 						(bp->b_pages[bp->b_npages - 1] != m)) {
719 						bp->b_pages[bp->b_npages] = m;
720 						bp->b_npages++;
721 					}
722 				}
723 			}
724 			bp->b_bcount += size;
725 			bp->b_bufsize += size;
726 
727 			tbp->b_flags &= ~(B_READ | B_DONE | B_ERROR | B_DELWRI);
728 			tbp->b_flags |= B_ASYNC;
729 			s = splbio();
730 			reassignbuf(tbp, tbp->b_vp);	/* put on clean list */
731 			++tbp->b_vp->v_numoutput;
732 			splx(s);
733 			TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
734 				tbp, b_cluster.cluster_entry);
735 		}
736 		pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
737 			(vm_page_t *) bp->b_pages, bp->b_npages);
738 		if (bp->b_bufsize > bp->b_kvasize)
739 			panic("cluster_wbuild: b_bufsize(%d) > b_kvasize(%d)\n",
740 				bp->b_bufsize, bp->b_kvasize);
741 		bp->b_kvasize = bp->b_bufsize;
742 		totalwritten += bp->b_bufsize;
743 		bp->b_dirtyoff = 0;
744 		bp->b_dirtyend = bp->b_bufsize;
745 		bawrite(bp);
746 
747 		len -= i;
748 	}
749 	return totalwritten;
750 }
751 
752 #ifdef notyet_block_reallocation_enabled
753 /*
754  * Collect together all the buffers in a cluster.
755  * Plus add one additional buffer.
756  */
757 static struct cluster_save *
758 cluster_collectbufs(vp, last_bp)
759 	struct vnode *vp;
760 	struct buf *last_bp;
761 {
762 	struct cluster_save *buflist;
763 	daddr_t lbn;
764 	int i, len;
765 
766 	len = vp->v_lastw - vp->v_cstart + 1;
767 	buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
768 	    M_SEGMENT, M_WAITOK);
769 	buflist->bs_nchildren = 0;
770 	buflist->bs_children = (struct buf **) (buflist + 1);
771 	for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++)
772 		(void) bread(vp, lbn, last_bp->b_bcount, NOCRED,
773 		    &buflist->bs_children[i]);
774 	buflist->bs_children[i] = last_bp;
775 	buflist->bs_nchildren = i + 1;
776 	return (buflist);
777 }
778 #endif /* notyet_block_reallocation_enabled */
779