xref: /freebsd/sys/kern/vfs_cluster.c (revision eaf13dd73a6fe8d8d8555e4701e376dfbeedecc1)
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.52 1998/01/31 07:23:11 eivind Exp $
37  */
38 
39 #include "opt_debug_cluster.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/buf.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
47 #include <sys/resourcevar.h>
48 #include <vm/vm.h>
49 #include <vm/vm_prot.h>
50 #include <vm/vm_object.h>
51 #include <vm/vm_page.h>
52 
53 #if defined(CLUSTERDEBUG)
54 #include <sys/sysctl.h>
55 #include <sys/kernel.h>
56 static int	rcluster= 0;
57 SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0, "");
58 #endif
59 
60 #ifdef notyet_block_reallocation_enabled
61 static struct cluster_save *
62 	cluster_collectbufs __P((struct vnode *vp, struct buf *last_bp));
63 #endif
64 static struct buf *
65 	cluster_rbuild __P((struct vnode *vp, u_quad_t filesize, daddr_t lbn,
66 			    daddr_t blkno, long size, int run, struct buf *fbp));
67 
68 extern vm_page_t	bogus_page;
69 
70 /*
71  * Maximum number of blocks for read-ahead.
72  */
73 #define MAXRA 32
74 
75 /*
76  * This replaces bread.
77  */
78 int
79 cluster_read(vp, filesize, lblkno, size, cred, totread, seqcount, bpp)
80 	struct vnode *vp;
81 	u_quad_t filesize;
82 	daddr_t lblkno;
83 	long size;
84 	struct ucred *cred;
85 	long totread;
86 	int seqcount;
87 	struct buf **bpp;
88 {
89 	struct buf *bp, *rbp, *reqbp;
90 	daddr_t blkno, origblkno;
91 	int error, num_ra;
92 	int i;
93 	int maxra, racluster;
94 	long origtotread;
95 
96 	error = 0;
97 	if (vp->v_maxio == 0)
98 		vp->v_maxio = DFLTPHYS;
99 
100 	/*
101 	 * Try to limit the amount of read-ahead by a few
102 	 * ad-hoc parameters.  This needs work!!!
103 	 */
104 	racluster = vp->v_maxio/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 	if (vp->v_maxio == 0)
364 		vp->v_maxio = DFLTPHYS;
365 	inc = btodb(size);
366 	for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
367 		if (i != 0) {
368 			if ((bp->b_npages * PAGE_SIZE) +
369 				round_page(size) > vp->v_maxio)
370 				break;
371 
372 			if (incore(vp, lbn + i))
373 				break;
374 
375 			tbp = getblk(vp, lbn + i, size, 0, 0);
376 
377 			if ((tbp->b_flags & B_CACHE) ||
378 				(tbp->b_flags & B_VMIO) == 0) {
379 				bqrelse(tbp);
380 				break;
381 			}
382 
383 			for (j=0;j<tbp->b_npages;j++) {
384 				if (tbp->b_pages[j]->valid) {
385 					break;
386 				}
387 			}
388 
389 			if (j != tbp->b_npages) {
390 				/*
391 				 * force buffer to be re-constituted later
392 				 */
393 				tbp->b_flags |= B_RELBUF;
394 				brelse(tbp);
395 				break;
396 			}
397 
398 			if ((fbp && (i == 1)) || (i == (run - 1)))
399 				tbp->b_flags |= B_RAM;
400 			tbp->b_flags |= B_READ | B_ASYNC;
401 			if (tbp->b_blkno == tbp->b_lblkno) {
402 				tbp->b_blkno = bn;
403 			} else if (tbp->b_blkno != bn) {
404 				brelse(tbp);
405 				break;
406 			}
407 		}
408 		TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
409 			tbp, b_cluster.cluster_entry);
410 		for (j = 0; j < tbp->b_npages; j += 1) {
411 			vm_page_t m;
412 			m = tbp->b_pages[j];
413 			++m->busy;
414 			++m->object->paging_in_progress;
415 			if ((bp->b_npages == 0) ||
416 				(bp->b_pages[bp->b_npages-1] != m)) {
417 				bp->b_pages[bp->b_npages] = m;
418 				bp->b_npages++;
419 			}
420 			if ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL)
421 				tbp->b_pages[j] = bogus_page;
422 		}
423 		bp->b_bcount += tbp->b_bcount;
424 		bp->b_bufsize += tbp->b_bufsize;
425 	}
426 
427 	for(j=0;j<bp->b_npages;j++) {
428 		if ((bp->b_pages[j]->valid & VM_PAGE_BITS_ALL) ==
429 			VM_PAGE_BITS_ALL)
430 			bp->b_pages[j] = bogus_page;
431 	}
432 	if (bp->b_bufsize > bp->b_kvasize)
433 		panic("cluster_rbuild: b_bufsize(%d) > b_kvasize(%d)\n",
434 			bp->b_bufsize, bp->b_kvasize);
435 	bp->b_kvasize = bp->b_bufsize;
436 
437 	pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
438 		(vm_page_t *)bp->b_pages, bp->b_npages);
439 	return (bp);
440 }
441 
442 /*
443  * Cleanup after a clustered read or write.
444  * This is complicated by the fact that any of the buffers might have
445  * extra memory (if there were no empty buffer headers at allocbuf time)
446  * that we will need to shift around.
447  */
448 void
449 cluster_callback(bp)
450 	struct buf *bp;
451 {
452 	struct buf *nbp, *tbp;
453 	int error = 0;
454 
455 	/*
456 	 * Must propogate errors to all the components.
457 	 */
458 	if (bp->b_flags & B_ERROR)
459 		error = bp->b_error;
460 
461 	pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages);
462 	/*
463 	 * Move memory from the large cluster buffer into the component
464 	 * buffers and mark IO as done on these.
465 	 */
466 	for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head);
467 		tbp; tbp = nbp) {
468 		nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry);
469 		if (error) {
470 			tbp->b_flags |= B_ERROR;
471 			tbp->b_error = error;
472 		} else
473 		    tbp->b_dirtyoff = tbp->b_dirtyend = 0;
474 		biodone(tbp);
475 	}
476 	relpbuf(bp);
477 }
478 
479 /*
480  * Do clustered write for FFS.
481  *
482  * Three cases:
483  *	1. Write is not sequential (write asynchronously)
484  *	Write is sequential:
485  *	2.	beginning of cluster - begin cluster
486  *	3.	middle of a cluster - add to cluster
487  *	4.	end of a cluster - asynchronously write cluster
488  */
489 void
490 cluster_write(bp, filesize)
491 	struct buf *bp;
492 	u_quad_t filesize;
493 {
494 	struct vnode *vp;
495 	daddr_t lbn;
496 	int maxclen, cursize;
497 	int lblocksize;
498 	int async;
499 
500 	vp = bp->b_vp;
501 	if (vp->v_maxio == 0)
502 		vp->v_maxio = DFLTPHYS;
503 	if (vp->v_type == VREG) {
504 		async = vp->v_mount->mnt_flag & MNT_ASYNC;
505 		lblocksize = vp->v_mount->mnt_stat.f_iosize;
506 	} else {
507 		async = 0;
508 		lblocksize = bp->b_bufsize;
509 	}
510 	lbn = bp->b_lblkno;
511 
512 	/* Initialize vnode to beginning of file. */
513 	if (lbn == 0)
514 		vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
515 
516 	if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 ||
517 	    (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) {
518 		maxclen = vp->v_maxio / lblocksize - 1;
519 		if (vp->v_clen != 0) {
520 			/*
521 			 * Next block is not sequential.
522 			 *
523 			 * If we are not writing at end of file, the process
524 			 * seeked to another point in the file since its last
525 			 * write, or we have reached our maximum cluster size,
526 			 * then push the previous cluster. Otherwise try
527 			 * reallocating to make it sequential.
528 			 */
529 			cursize = vp->v_lastw - vp->v_cstart + 1;
530 #ifndef notyet_block_reallocation_enabled
531 			if (((u_quad_t)(lbn + 1) * lblocksize) != filesize ||
532 				lbn != vp->v_lastw + 1 ||
533 				vp->v_clen <= cursize) {
534 				if (!async)
535 					cluster_wbuild(vp, lblocksize,
536 						vp->v_cstart, cursize);
537 			}
538 #else
539 			if ((lbn + 1) * lblocksize != filesize ||
540 			    lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) {
541 				if (!async)
542 					cluster_wbuild(vp, lblocksize,
543 						vp->v_cstart, cursize);
544 			} else {
545 				struct buf **bpp, **endbp;
546 				struct cluster_save *buflist;
547 
548 				buflist = cluster_collectbufs(vp, bp);
549 				endbp = &buflist->bs_children
550 				    [buflist->bs_nchildren - 1];
551 				if (VOP_REALLOCBLKS(vp, buflist)) {
552 					/*
553 					 * Failed, push the previous cluster.
554 					 */
555 					for (bpp = buflist->bs_children;
556 					     bpp < endbp; bpp++)
557 						brelse(*bpp);
558 					free(buflist, M_SEGMENT);
559 					cluster_wbuild(vp, lblocksize,
560 					    vp->v_cstart, cursize);
561 				} else {
562 					/*
563 					 * Succeeded, keep building cluster.
564 					 */
565 					for (bpp = buflist->bs_children;
566 					     bpp <= endbp; bpp++)
567 						bdwrite(*bpp);
568 					free(buflist, M_SEGMENT);
569 					vp->v_lastw = lbn;
570 					vp->v_lasta = bp->b_blkno;
571 					return;
572 				}
573 			}
574 #endif /* notyet_block_reallocation_enabled */
575 		}
576 		/*
577 		 * Consider beginning a cluster. If at end of file, make
578 		 * cluster as large as possible, otherwise find size of
579 		 * existing cluster.
580 		 */
581 		if ((vp->v_type == VREG) &&
582 			((u_quad_t) (lbn + 1) * lblocksize) != filesize &&
583 		    (bp->b_blkno == bp->b_lblkno) &&
584 		    (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) ||
585 		     bp->b_blkno == -1)) {
586 			bawrite(bp);
587 			vp->v_clen = 0;
588 			vp->v_lasta = bp->b_blkno;
589 			vp->v_cstart = lbn + 1;
590 			vp->v_lastw = lbn;
591 			return;
592 		}
593 		vp->v_clen = maxclen;
594 		if (!async && maxclen == 0) {	/* I/O not contiguous */
595 			vp->v_cstart = lbn + 1;
596 			bawrite(bp);
597 		} else {	/* Wait for rest of cluster */
598 			vp->v_cstart = lbn;
599 			bdwrite(bp);
600 		}
601 	} else if (lbn == vp->v_cstart + vp->v_clen) {
602 		/*
603 		 * At end of cluster, write it out.
604 		 */
605 		bdwrite(bp);
606 		cluster_wbuild(vp, lblocksize, vp->v_cstart, vp->v_clen + 1);
607 		vp->v_clen = 0;
608 		vp->v_cstart = lbn + 1;
609 	} else
610 		/*
611 		 * In the middle of a cluster, so just delay the I/O for now.
612 		 */
613 		bdwrite(bp);
614 	vp->v_lastw = lbn;
615 	vp->v_lasta = bp->b_blkno;
616 }
617 
618 
619 /*
620  * This is an awful lot like cluster_rbuild...wish they could be combined.
621  * The last lbn argument is the current block on which I/O is being
622  * performed.  Check to see that it doesn't fall in the middle of
623  * the current block (if last_bp == NULL).
624  */
625 int
626 cluster_wbuild(vp, size, start_lbn, len)
627 	struct vnode *vp;
628 	long size;
629 	daddr_t start_lbn;
630 	int len;
631 {
632 	struct buf *bp, *tbp;
633 	int i, j, s;
634 	int totalwritten = 0;
635 	int dbsize = btodb(size);
636 	while (len > 0) {
637 		s = splbio();
638 		if (((tbp = gbincore(vp, start_lbn)) == NULL) ||
639 			((tbp->b_flags & (B_INVAL|B_BUSY|B_DELWRI)) != B_DELWRI)) {
640 			++start_lbn;
641 			--len;
642 			splx(s);
643 			continue;
644 		}
645 		bremfree(tbp);
646 		tbp->b_flags |= B_BUSY;
647 		tbp->b_flags &= ~B_DONE;
648 		splx(s);
649 
650 	/*
651 	 * Extra memory in the buffer, punt on this buffer. XXX we could
652 	 * handle this in most cases, but we would have to push the extra
653 	 * memory down to after our max possible cluster size and then
654 	 * potentially pull it back up if the cluster was terminated
655 	 * prematurely--too much hassle.
656 	 */
657 		if (((tbp->b_flags & (B_CLUSTEROK|B_MALLOC)) != B_CLUSTEROK) ||
658 			(tbp->b_bcount != tbp->b_bufsize) ||
659 			(tbp->b_bcount != size) ||
660 			len == 1) {
661 			totalwritten += tbp->b_bufsize;
662 			bawrite(tbp);
663 			++start_lbn;
664 			--len;
665 			continue;
666 		}
667 
668 		bp = trypbuf();
669 		if (bp == NULL) {
670 			totalwritten += tbp->b_bufsize;
671 			bawrite(tbp);
672 			++start_lbn;
673 			--len;
674 			continue;
675 		}
676 
677 		TAILQ_INIT(&bp->b_cluster.cluster_head);
678 		bp->b_bcount = 0;
679 		bp->b_bufsize = 0;
680 		bp->b_npages = 0;
681 		if (tbp->b_wcred != NOCRED) {
682 		    bp->b_wcred = tbp->b_wcred;
683 		    crhold(bp->b_wcred);
684 		}
685 
686 		bp->b_blkno = tbp->b_blkno;
687 		bp->b_lblkno = tbp->b_lblkno;
688 		(vm_offset_t) bp->b_data |= ((vm_offset_t) tbp->b_data) & PAGE_MASK;
689 		bp->b_flags |= B_CALL | B_BUSY | B_CLUSTER |
690 						(tbp->b_flags & (B_VMIO|B_NEEDCOMMIT));
691 		bp->b_iodone = cluster_callback;
692 		pbgetvp(vp, bp);
693 
694 		for (i = 0; i < len; ++i, ++start_lbn) {
695 			if (i != 0) {
696 				s = splbio();
697 				if ((tbp = gbincore(vp, start_lbn)) == NULL) {
698 					splx(s);
699 					break;
700 				}
701 
702 				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)))) {
703 					splx(s);
704 					break;
705 				}
706 
707 				if (tbp->b_wcred != bp->b_wcred) {
708 					splx(s);
709 					break;
710 				}
711 
712 				if ((tbp->b_bcount != size) ||
713 					((bp->b_blkno + dbsize * i) != tbp->b_blkno) ||
714 					((tbp->b_npages + bp->b_npages) > (vp->v_maxio / PAGE_SIZE))) {
715 					splx(s);
716 					break;
717 				}
718 				bremfree(tbp);
719 				tbp->b_flags |= B_BUSY;
720 				tbp->b_flags &= ~B_DONE;
721 				splx(s);
722 			}
723 
724 			if (tbp->b_flags & B_VMIO) {
725 				vm_page_t m;
726 
727 				if (i != 0) {
728 					for (j = 0; j < tbp->b_npages; j += 1) {
729 						m = tbp->b_pages[j];
730 						if (m->flags & PG_BUSY)
731 							goto finishcluster;
732 					}
733 				}
734 
735 				for (j = 0; j < tbp->b_npages; j += 1) {
736 					m = tbp->b_pages[j];
737 					++m->busy;
738 					++m->object->paging_in_progress;
739 					if ((bp->b_npages == 0) ||
740 						(bp->b_pages[bp->b_npages - 1] != m)) {
741 						bp->b_pages[bp->b_npages] = m;
742 						bp->b_npages++;
743 					}
744 				}
745 			}
746 			bp->b_bcount += size;
747 			bp->b_bufsize += size;
748 
749 			--numdirtybuffers;
750 			tbp->b_flags &= ~(B_READ | B_DONE | B_ERROR | B_DELWRI);
751 			tbp->b_flags |= B_ASYNC;
752 			s = splbio();
753 			reassignbuf(tbp, tbp->b_vp);	/* put on clean list */
754 			++tbp->b_vp->v_numoutput;
755 			splx(s);
756 			TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
757 				tbp, b_cluster.cluster_entry);
758 		}
759 	finishcluster:
760 		pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
761 			(vm_page_t *) bp->b_pages, bp->b_npages);
762 		if (bp->b_bufsize > bp->b_kvasize)
763 			panic("cluster_wbuild: b_bufsize(%d) > b_kvasize(%d)\n",
764 				bp->b_bufsize, bp->b_kvasize);
765 		bp->b_kvasize = bp->b_bufsize;
766 		totalwritten += bp->b_bufsize;
767 		bp->b_dirtyoff = 0;
768 		bp->b_dirtyend = bp->b_bufsize;
769 		bawrite(bp);
770 
771 		len -= i;
772 	}
773 	return totalwritten;
774 }
775 
776 #ifdef notyet_block_reallocation_enabled
777 /*
778  * Collect together all the buffers in a cluster.
779  * Plus add one additional buffer.
780  */
781 static struct cluster_save *
782 cluster_collectbufs(vp, last_bp)
783 	struct vnode *vp;
784 	struct buf *last_bp;
785 {
786 	struct cluster_save *buflist;
787 	daddr_t lbn;
788 	int i, len;
789 
790 	len = vp->v_lastw - vp->v_cstart + 1;
791 	buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
792 	    M_SEGMENT, M_WAITOK);
793 	buflist->bs_nchildren = 0;
794 	buflist->bs_children = (struct buf **) (buflist + 1);
795 	for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++)
796 		(void) bread(vp, lbn, last_bp->b_bcount, NOCRED,
797 		    &buflist->bs_children[i]);
798 	buflist->bs_children[i] = last_bp;
799 	buflist->bs_nchildren = i + 1;
800 	return (buflist);
801 }
802 #endif /* notyet_block_reallocation_enabled */
803