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