3 * The Regents of the University of California. All rights reserved.
4 * Modifications/enhancements:
5 * Copyright (c) 1995 John S. Dyson. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
35 * @(#)vfs_cluster.c 8.7 (Berkeley) 2/13/94
36 * $FreeBSD: src/sys/kern/vfs_cluster.c,v 1.92.2.9 2001/11/18 07:10:59 dillon Exp $
37 * $DragonFly: src/sys/kern/vfs_cluster.c,v 1.18 2006/03/10 17:51:54 dillon Exp $
40 #include "opt_debug_cluster.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.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>
53 #include <vm/vm_object.h>
54 #include <vm/vm_page.h>
55 #include <sys/sysctl.h>
57 #include <vm/vm_page2.h>
59 #if defined(CLUSTERDEBUG)
60 #include <sys/sysctl.h>
61 static int rcluster= 0;
62 SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0, "");
65 static MALLOC_DEFINE(M_SEGMENT, "cluster_save", "cluster_save buffer");
67 static struct cluster_save *
68 cluster_collectbufs (struct vnode *vp, struct buf *last_bp);
70 cluster_rbuild (struct vnode *vp, u_quad_t filesize, daddr_t lbn,
71 daddr_t blkno, long size, int run, struct buf *fbp);
72 static void cluster_callback (struct bio *);
75 static int write_behind = 1;
76 SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0, "");
78 extern vm_page_t bogus_page;
80 extern int cluster_pbuf_freecnt;
83 * Maximum number of blocks for read-ahead.
88 * This replaces bread.
91 cluster_read(struct vnode *vp, u_quad_t filesize, daddr_t lblkno,
92 long size, long totread, int seqcount, struct buf **bpp)
94 struct buf *bp, *rbp, *reqbp;
95 daddr_t blkno, origblkno;
104 * Try to limit the amount of read-ahead by a few
105 * ad-hoc parameters. This needs work!!!
107 racluster = vp->v_mount->mnt_iosize_max / size;
108 maxra = 2 * racluster + (totread / size);
115 * get the requested block
117 *bpp = reqbp = bp = getblk(vp, lblkno, size, 0, 0);
119 origtotread = totread;
122 * if it is in the cache, then check to see if the reads have been
123 * sequential. If they have, then try some read-ahead, otherwise
124 * back-off on prospective read-aheads.
126 if (bp->b_flags & B_CACHE) {
129 } else if ((bp->b_flags & B_RAM) == 0) {
133 bp->b_flags &= ~B_RAM;
135 * We do the crit here so that there is no window
136 * between the findblk and the b_usecount increment
137 * below. We opt to keep the crit out of the loop
141 for (i = 1; i < maxra; i++) {
143 if (!(tbp = findblk(vp, lblkno+i))) {
148 * Set another read-ahead mark so we know
151 if (((i % racluster) == (racluster - 1)) ||
153 tbp->b_flags |= B_RAM;
163 off_t firstread = bp->b_loffset;
165 KASSERT(firstread != NOOFFSET,
166 ("cluster_read: no buffer offset"));
167 if (firstread + totread > filesize)
168 totread = filesize - firstread;
169 if (totread > size) {
172 while (totread > 0) {
177 goto single_block_read;
178 if (nblks > racluster)
181 error = VOP_BMAP(vp, lblkno, NULL,
182 &blkno, &ncontigafter, NULL);
184 goto single_block_read;
186 goto single_block_read;
187 if (ncontigafter == 0)
188 goto single_block_read;
189 if (ncontigafter + 1 < nblks)
190 nblks = ncontigafter + 1;
192 bp = cluster_rbuild(vp, filesize, lblkno,
193 blkno, size, nblks, bp);
194 lblkno += (bp->b_bufsize / size);
198 * if it isn't in the cache, then get a chunk from
199 * disk if sequential, otherwise just get the block.
201 bp->b_flags |= B_READ | B_RAM;
207 * If we have been doing sequential I/O, then do some read-ahead.
211 lblkno < origblkno + seqcount &&
212 (u_quad_t)(lblkno + 1) * size <= filesize
214 rbp = getblk(vp, lblkno, size, 0, 0);
215 if ((rbp->b_flags & B_CACHE) == 0) {
219 error = VOP_BMAP(vp, lblkno, NULL,
220 &blkno, &num_ra, NULL);
221 if (error || blkno == (daddr_t)-1) {
222 rbp->b_flags &= ~(B_ASYNC | B_READ);
227 ntoread = num_ra + 1;
228 nblksread = (origtotread + size - 1) / size;
229 if (seqcount < nblksread)
230 seqcount = nblksread;
231 if (seqcount < ntoread)
234 rbp->b_flags |= B_READ | B_ASYNC | B_RAM;
236 rbp = cluster_rbuild(vp, filesize, lblkno,
237 blkno, size, ntoread, rbp);
239 rbp->b_bio2.bio_blkno = blkno;
246 * Handle the synchronous read
249 #if defined(CLUSTERDEBUG)
251 printf("S(%ld,%ld,%d) ",
252 (long)bp->b_lblkno, bp->b_bcount, seqcount);
254 if ((bp->b_flags & B_CLUSTER) == 0) {
255 vfs_busy_pages(bp, 0);
257 bp->b_flags &= ~(B_ERROR|B_INVAL);
258 if ((bp->b_flags & B_ASYNC) || bp->b_bio1.bio_done != NULL)
260 vn_strategy(vp, &bp->b_bio1);
265 * And if we have read-aheads, do them too
269 rbp->b_flags &= ~(B_ASYNC | B_READ);
271 } else if (rbp->b_flags & B_CACHE) {
272 rbp->b_flags &= ~(B_ASYNC | B_READ);
275 #if defined(CLUSTERDEBUG)
278 printf("A+(%ld,%ld,%ld,%d) ",
279 (long)rbp->b_lblkno, rbp->b_bcount,
280 (long)(rbp->b_lblkno - origblkno),
283 printf("A(%ld,%ld,%ld,%d) ",
284 (long)rbp->b_lblkno, rbp->b_bcount,
285 (long)(rbp->b_lblkno - origblkno),
290 if ((rbp->b_flags & B_CLUSTER) == 0) {
291 vfs_busy_pages(rbp, 0);
293 rbp->b_flags &= ~(B_ERROR|B_INVAL);
294 if ((rbp->b_flags & B_ASYNC) || rbp->b_bio1.bio_done != NULL)
296 vn_strategy(vp, &rbp->b_bio1);
300 return (biowait(reqbp));
306 * If blocks are contiguous on disk, use this to provide clustered
307 * read ahead. We will read as many blocks as possible sequentially
308 * and then parcel them up into logical blocks in the buffer hash table.
311 cluster_rbuild(struct vnode *vp, u_quad_t filesize, daddr_t lbn,
312 daddr_t blkno, long size, int run, struct buf *fbp)
314 struct buf *bp, *tbp;
318 KASSERT(size == vp->v_mount->mnt_stat.f_iosize,
319 ("cluster_rbuild: size %ld != filesize %ld\n",
320 size, vp->v_mount->mnt_stat.f_iosize));
325 while ((u_quad_t) size * (lbn + run) > filesize) {
330 tbp->b_flags |= B_READ;
331 tbp->b_bio2.bio_blkno = blkno;
332 if( (tbp->b_flags & B_MALLOC) ||
333 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
336 bp = trypbuf(&cluster_pbuf_freecnt);
341 * We are synthesizing a buffer out of vm_page_t's, but
342 * if the block size is not page aligned then the starting
343 * address may not be either. Inherit the b_data offset
344 * from the original buffer.
346 bp->b_data = (char *)((vm_offset_t)bp->b_data |
347 ((vm_offset_t)tbp->b_data & PAGE_MASK));
348 bp->b_flags = B_ASYNC | B_READ | B_CLUSTER | B_VMIO;
349 bp->b_bio1.bio_done = cluster_callback;
350 bp->b_bio1.bio_caller_info1.cluster_head = NULL;
351 bp->b_bio1.bio_caller_info2.cluster_tail = NULL;
353 bp->b_loffset = tbp->b_loffset;
354 bp->b_bio2.bio_blkno = (daddr_t)-1;
355 KASSERT(bp->b_loffset != NOOFFSET,
356 ("cluster_rbuild: no buffer offset"));
361 bp->b_xio.xio_npages = 0;
364 for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
366 if ((bp->b_xio.xio_npages * PAGE_SIZE) +
367 round_page(size) > vp->v_mount->mnt_iosize_max) {
372 * Shortcut some checks and try to avoid buffers that
373 * would block in the lock. The same checks have to
374 * be made again after we officially get the buffer.
376 if ((tbp = findblk(vp, lbn + i)) != NULL) {
377 if (BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT))
381 for (j = 0; j < tbp->b_xio.xio_npages; j++) {
382 if (tbp->b_xio.xio_pages[j]->valid)
386 if (j != tbp->b_xio.xio_npages)
389 if (tbp->b_bcount != size)
393 tbp = getblk(vp, lbn + i, size, 0, 0);
396 * Stop scanning if the buffer is fuly valid
397 * (marked B_CACHE), or locked (may be doing a
398 * background write), or if the buffer is not
399 * VMIO backed. The clustering code can only deal
400 * with VMIO-backed buffers.
402 if ((tbp->b_flags & (B_CACHE|B_LOCKED)) ||
403 (tbp->b_flags & B_VMIO) == 0) {
409 * The buffer must be completely invalid in order to
410 * take part in the cluster. If it is partially valid
413 for (j = 0;j < tbp->b_xio.xio_npages; j++) {
414 if (tbp->b_xio.xio_pages[j]->valid)
417 if (j != tbp->b_xio.xio_npages) {
423 * Set a read-ahead mark as appropriate
425 if (i == 1 || i == (run - 1))
426 tbp->b_flags |= B_RAM;
429 * Set the buffer up for an async read (XXX should
430 * we do this only if we do not wind up brelse()ing?).
431 * Set the block number if it isn't set, otherwise
432 * if it is make sure it matches the block number we
435 tbp->b_flags |= B_READ | B_ASYNC;
436 if (tbp->b_bio2.bio_blkno == (daddr_t)-1) {
437 tbp->b_bio2.bio_blkno = bn;
438 } else if (tbp->b_bio2.bio_blkno != bn) {
444 * XXX fbp from caller may not be B_ASYNC, but we are going
445 * to biodone() it in cluster_callback() anyway
448 cluster_append(&bp->b_bio1, tbp);
449 for (j = 0; j < tbp->b_xio.xio_npages; j += 1) {
451 m = tbp->b_xio.xio_pages[j];
453 vm_object_pip_add(m->object, 1);
454 if ((bp->b_xio.xio_npages == 0) ||
455 (bp->b_xio.xio_pages[bp->b_xio.xio_npages-1] != m)) {
456 bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m;
457 bp->b_xio.xio_npages++;
459 if ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL)
460 tbp->b_xio.xio_pages[j] = bogus_page;
463 * XXX shouldn't this be += size for both, like in
466 * Don't inherit tbp->b_bufsize as it may be larger due to
467 * a non-page-aligned size. Instead just aggregate using
470 if (tbp->b_bcount != size)
471 printf("warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size);
472 if (tbp->b_bufsize != size)
473 printf("warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size);
474 bp->b_bcount += size;
475 bp->b_bufsize += size;
479 * Fully valid pages in the cluster are already good and do not need
480 * to be re-read from disk. Replace the page with bogus_page
482 for (j = 0; j < bp->b_xio.xio_npages; j++) {
483 if ((bp->b_xio.xio_pages[j]->valid & VM_PAGE_BITS_ALL) ==
485 bp->b_xio.xio_pages[j] = bogus_page;
488 if (bp->b_bufsize > bp->b_kvasize)
489 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)",
490 bp->b_bufsize, bp->b_kvasize);
491 bp->b_kvasize = bp->b_bufsize;
493 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
494 (vm_page_t *)bp->b_xio.xio_pages, bp->b_xio.xio_npages);
499 * Cleanup after a clustered read or write.
500 * This is complicated by the fact that any of the buffers might have
501 * extra memory (if there were no empty buffer headers at allocbuf time)
502 * that we will need to shift around.
504 * The returned bio is &bp->b_bio1
507 cluster_callback(struct bio *bio)
509 struct buf *bp = bio->bio_buf;
514 * Must propogate errors to all the components.
516 if (bp->b_flags & B_ERROR)
519 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_xio.xio_npages);
521 * Move memory from the large cluster buffer into the component
522 * buffers and mark IO as done on these. Since the memory map
523 * is the same, no actual copying is required.
525 while ((tbp = bio->bio_caller_info1.cluster_head) != NULL) {
526 bio->bio_caller_info1.cluster_head = tbp->b_cluster_next;
528 tbp->b_flags |= B_ERROR;
529 tbp->b_error = error;
531 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
532 tbp->b_flags &= ~(B_ERROR|B_INVAL);
534 * XXX the bdwrite()/bqrelse() issued during
535 * cluster building clears B_RELBUF (see bqrelse()
536 * comment). If direct I/O was specified, we have
537 * to restore it here to allow the buffer and VM
540 if (tbp->b_flags & B_DIRECT)
541 tbp->b_flags |= B_RELBUF;
543 biodone(&tbp->b_bio1);
545 relpbuf(bp, &cluster_pbuf_freecnt);
551 * Implement modified write build for cluster.
553 * write_behind = 0 write behind disabled
554 * write_behind = 1 write behind normal (default)
555 * write_behind = 2 write behind backed-off
559 cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len)
563 switch(write_behind) {
570 r = cluster_wbuild(vp, size, start_lbn, len);
580 * Do clustered write for FFS.
583 * 1. Write is not sequential (write asynchronously)
584 * Write is sequential:
585 * 2. beginning of cluster - begin cluster
586 * 3. middle of a cluster - add to cluster
587 * 4. end of a cluster - asynchronously write cluster
590 cluster_write(struct buf *bp, u_quad_t filesize, int seqcount)
594 int maxclen, cursize;
599 if (vp->v_type == VREG) {
600 async = vp->v_mount->mnt_flag & MNT_ASYNC;
601 lblocksize = vp->v_mount->mnt_stat.f_iosize;
604 lblocksize = bp->b_bufsize;
607 KASSERT(bp->b_loffset != NOOFFSET,
608 ("cluster_write: no buffer offset"));
610 /* Initialize vnode to beginning of file. */
612 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
614 if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 ||
615 bp->b_bio2.bio_blkno == (daddr_t)-1 ||
616 (bp->b_bio2.bio_blkno != vp->v_lasta + btodb(lblocksize))) {
617 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1;
618 if (vp->v_clen != 0) {
620 * Next block is not sequential.
622 * If we are not writing at end of file, the process
623 * seeked to another point in the file since its last
624 * write, or we have reached our maximum cluster size,
625 * then push the previous cluster. Otherwise try
626 * reallocating to make it sequential.
628 * Change to algorithm: only push previous cluster if
629 * it was sequential from the point of view of the
630 * seqcount heuristic, otherwise leave the buffer
631 * intact so we can potentially optimize the I/O
632 * later on in the buf_daemon or update daemon
635 cursize = vp->v_lastw - vp->v_cstart + 1;
636 if (((u_quad_t) bp->b_loffset + lblocksize) != filesize ||
637 lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) {
638 if (!async && seqcount > 0) {
639 cluster_wbuild_wb(vp, lblocksize,
640 vp->v_cstart, cursize);
643 struct buf **bpp, **endbp;
644 struct cluster_save *buflist;
646 buflist = cluster_collectbufs(vp, bp);
647 endbp = &buflist->bs_children
648 [buflist->bs_nchildren - 1];
649 if (VOP_REALLOCBLKS(vp, buflist)) {
651 * Failed, push the previous cluster
652 * if *really* writing sequentially
653 * in the logical file (seqcount > 1),
654 * otherwise delay it in the hopes that
655 * the low level disk driver can
656 * optimize the write ordering.
658 for (bpp = buflist->bs_children;
661 free(buflist, M_SEGMENT);
663 cluster_wbuild_wb(vp,
664 lblocksize, vp->v_cstart,
669 * Succeeded, keep building cluster.
671 for (bpp = buflist->bs_children;
674 free(buflist, M_SEGMENT);
676 vp->v_lasta = bp->b_bio2.bio_blkno;
682 * Consider beginning a cluster. If at end of file, make
683 * cluster as large as possible, otherwise find size of
686 if ((vp->v_type == VREG) &&
687 ((u_quad_t) bp->b_loffset + lblocksize) != filesize &&
688 (bp->b_bio2.bio_blkno == (daddr_t)-1) &&
689 (VOP_BMAP(vp, lbn, NULL, &bp->b_bio2.bio_blkno, &maxclen, NULL) ||
690 bp->b_bio2.bio_blkno == (daddr_t)-1)) {
693 vp->v_lasta = bp->b_bio2.bio_blkno;
694 vp->v_cstart = lbn + 1;
698 vp->v_clen = maxclen;
699 if (!async && maxclen == 0) { /* I/O not contiguous */
700 vp->v_cstart = lbn + 1;
702 } else { /* Wait for rest of cluster */
706 } else if (lbn == vp->v_cstart + vp->v_clen) {
708 * At end of cluster, write it out if seqcount tells us we
709 * are operating sequentially, otherwise let the buf or
710 * update daemon handle it.
714 cluster_wbuild_wb(vp, lblocksize, vp->v_cstart, vp->v_clen + 1);
716 vp->v_cstart = lbn + 1;
717 } else if (vm_page_count_severe()) {
719 * We are low on memory, get it going NOW
724 * In the middle of a cluster, so just delay the I/O for now.
729 vp->v_lasta = bp->b_bio2.bio_blkno;
734 * This is an awful lot like cluster_rbuild...wish they could be combined.
735 * The last lbn argument is the current block on which I/O is being
736 * performed. Check to see that it doesn't fall in the middle of
737 * the current block (if last_bp == NULL).
740 cluster_wbuild(struct vnode *vp, long size, daddr_t start_lbn, int len)
742 struct buf *bp, *tbp;
744 int totalwritten = 0;
745 int dbsize = btodb(size);
750 * If the buffer is not delayed-write (i.e. dirty), or it
751 * is delayed-write but either locked or inval, it cannot
752 * partake in the clustered write.
754 if (((tbp = findblk(vp, start_lbn)) == NULL) ||
755 ((tbp->b_flags & (B_LOCKED | B_INVAL | B_DELWRI)) != B_DELWRI) ||
756 BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) {
763 tbp->b_flags &= ~B_DONE;
767 * Extra memory in the buffer, punt on this buffer.
768 * XXX we could handle this in most cases, but we would
769 * have to push the extra memory down to after our max
770 * possible cluster size and then potentially pull it back
771 * up if the cluster was terminated prematurely--too much
774 if (((tbp->b_flags & (B_CLUSTEROK|B_MALLOC)) != B_CLUSTEROK) ||
775 (tbp->b_bcount != tbp->b_bufsize) ||
776 (tbp->b_bcount != size) ||
778 ((bp = getpbuf(&cluster_pbuf_freecnt)) == NULL)) {
779 totalwritten += tbp->b_bufsize;
787 * We got a pbuf to make the cluster in.
792 bp->b_xio.xio_npages = 0;
793 bp->b_lblkno = tbp->b_lblkno;
794 bp->b_loffset = tbp->b_loffset;
795 bp->b_bio2.bio_blkno = tbp->b_bio2.bio_blkno;
798 * We are synthesizing a buffer out of vm_page_t's, but
799 * if the block size is not page aligned then the starting
800 * address may not be either. Inherit the b_data offset
801 * from the original buffer.
803 bp->b_data = (char *)((vm_offset_t)bp->b_data |
804 ((vm_offset_t)tbp->b_data & PAGE_MASK));
805 bp->b_flags |= B_CLUSTER |
806 (tbp->b_flags & (B_VMIO | B_NEEDCOMMIT | B_NOWDRAIN));
807 bp->b_bio1.bio_done = cluster_callback;
808 bp->b_bio1.bio_caller_info1.cluster_head = NULL;
809 bp->b_bio1.bio_caller_info2.cluster_tail = NULL;
812 * From this location in the file, scan forward to see
813 * if there are buffers with adjacent data that need to
814 * be written as well.
816 for (i = 0; i < len; ++i, ++start_lbn) {
817 if (i != 0) { /* If not the first buffer */
820 * If the adjacent data is not even in core it
821 * can't need to be written.
823 if ((tbp = findblk(vp, start_lbn)) == NULL) {
829 * If it IS in core, but has different
830 * characteristics, or is locked (which
831 * means it could be undergoing a background
832 * I/O or be in a weird state), then don't
835 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
836 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
837 != (B_DELWRI | B_CLUSTEROK |
838 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
839 (tbp->b_flags & B_LOCKED) ||
840 BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) {
846 * Check that the combined cluster
847 * would make sense with regard to pages
848 * and would not be too large
850 if ((tbp->b_bcount != size) ||
851 ((bp->b_bio2.bio_blkno + (dbsize * i)) !=
852 tbp->b_bio2.bio_blkno) ||
853 ((tbp->b_xio.xio_npages + bp->b_xio.xio_npages) >
854 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
860 * Ok, it's passed all the tests,
861 * so remove it from the free list
862 * and mark it busy. We will use it.
865 tbp->b_flags &= ~B_DONE;
867 } /* end of code for non-first buffers only */
870 * If the IO is via the VM then we do some
871 * special VM hackery (yuck). Since the buffer's
872 * block size may not be page-aligned it is possible
873 * for a page to be shared between two buffers. We
874 * have to get rid of the duplication when building
877 if (tbp->b_flags & B_VMIO) {
880 if (i != 0) { /* if not first buffer */
881 for (j = 0; j < tbp->b_xio.xio_npages; j += 1) {
882 m = tbp->b_xio.xio_pages[j];
883 if (m->flags & PG_BUSY) {
890 for (j = 0; j < tbp->b_xio.xio_npages; j += 1) {
891 m = tbp->b_xio.xio_pages[j];
893 vm_object_pip_add(m->object, 1);
894 if ((bp->b_xio.xio_npages == 0) ||
895 (bp->b_xio.xio_pages[bp->b_xio.xio_npages - 1] != m)) {
896 bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m;
897 bp->b_xio.xio_npages++;
901 bp->b_bcount += size;
902 bp->b_bufsize += size;
906 tbp->b_flags &= ~(B_READ | B_DONE | B_ERROR);
907 tbp->b_flags |= B_ASYNC;
910 cluster_append(&bp->b_bio1, tbp);
913 * check for latent dependencies to be handled
915 if (LIST_FIRST(&tbp->b_dep) != NULL && bioops.io_start)
916 (*bioops.io_start)(tbp);
920 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
921 (vm_page_t *) bp->b_xio.xio_pages, bp->b_xio.xio_npages);
922 if (bp->b_bufsize > bp->b_kvasize)
924 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
925 bp->b_bufsize, bp->b_kvasize);
926 bp->b_kvasize = bp->b_bufsize;
927 totalwritten += bp->b_bufsize;
929 bp->b_dirtyend = bp->b_bufsize;
938 * Collect together all the buffers in a cluster.
939 * Plus add one additional buffer.
941 static struct cluster_save *
942 cluster_collectbufs(struct vnode *vp, struct buf *last_bp)
944 struct cluster_save *buflist;
949 len = vp->v_lastw - vp->v_cstart + 1;
950 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
951 M_SEGMENT, M_WAITOK);
952 buflist->bs_nchildren = 0;
953 buflist->bs_children = (struct buf **) (buflist + 1);
954 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) {
955 (void) bread(vp, lbn, last_bp->b_bcount, &bp);
956 buflist->bs_children[i] = bp;
957 if (bp->b_bio2.bio_blkno == (daddr_t)-1)
958 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_bio2.bio_blkno,
961 buflist->bs_children[i] = bp = last_bp;
962 if (bp->b_bio2.bio_blkno == (daddr_t)-1)
963 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_bio2.bio_blkno,
965 buflist->bs_nchildren = i + 1;
970 cluster_append(struct bio *bio, struct buf *tbp)
972 tbp->b_cluster_next = NULL;
973 if (bio->bio_caller_info1.cluster_head == NULL) {
974 bio->bio_caller_info1.cluster_head = tbp;
975 bio->bio_caller_info2.cluster_tail = tbp;
977 bio->bio_caller_info2.cluster_tail->b_cluster_next = tbp;
978 bio->bio_caller_info2.cluster_tail = tbp;