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