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.17 2006/03/05 18:38:34 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
210 if (seqcount && (lblkno < (origblkno + seqcount))) {
212 * we now build the read-ahead buffer if it is desirable.
214 if (((u_quad_t)(lblkno + 1) * size) <= filesize &&
215 !(error = VOP_BMAP(vp, lblkno, NULL, &blkno, &num_ra, NULL)) &&
218 int ntoread = num_ra + 1;
219 nblksread = (origtotread + size - 1) / size;
220 if (seqcount < nblksread)
221 seqcount = nblksread;
222 if (seqcount < ntoread)
225 rbp = cluster_rbuild(vp, filesize, lblkno,
226 blkno, size, ntoread, NULL);
228 rbp = getblk(vp, lblkno, size, 0, 0);
229 rbp->b_flags |= B_READ | B_ASYNC | B_RAM;
230 rbp->b_bio2.bio_blkno = blkno;
236 * handle the synchronous read
239 #if defined(CLUSTERDEBUG)
241 printf("S(%ld,%ld,%d) ",
242 (long)bp->b_lblkno, bp->b_bcount, seqcount);
244 if ((bp->b_flags & B_CLUSTER) == 0) {
245 vfs_busy_pages(bp, 0);
247 bp->b_flags &= ~(B_ERROR|B_INVAL);
248 if ((bp->b_flags & B_ASYNC) || bp->b_bio1.bio_done != NULL)
250 vn_strategy(vp, &bp->b_bio1);
255 * and if we have read-aheads, do them too
259 rbp->b_flags &= ~(B_ASYNC | B_READ);
261 } else if (rbp->b_flags & B_CACHE) {
262 rbp->b_flags &= ~(B_ASYNC | B_READ);
265 #if defined(CLUSTERDEBUG)
268 printf("A+(%ld,%ld,%ld,%d) ",
269 (long)rbp->b_lblkno, rbp->b_bcount,
270 (long)(rbp->b_lblkno - origblkno),
273 printf("A(%ld,%ld,%ld,%d) ",
274 (long)rbp->b_lblkno, rbp->b_bcount,
275 (long)(rbp->b_lblkno - origblkno),
280 if ((rbp->b_flags & B_CLUSTER) == 0) {
281 vfs_busy_pages(rbp, 0);
283 rbp->b_flags &= ~(B_ERROR|B_INVAL);
284 if ((rbp->b_flags & B_ASYNC) || rbp->b_bio1.bio_done != NULL)
286 vn_strategy(vp, &rbp->b_bio1);
290 return (biowait(reqbp));
296 * If blocks are contiguous on disk, use this to provide clustered
297 * read ahead. We will read as many blocks as possible sequentially
298 * and then parcel them up into logical blocks in the buffer hash table.
301 cluster_rbuild(struct vnode *vp, u_quad_t filesize, daddr_t lbn,
302 daddr_t blkno, long size, int run, struct buf *fbp)
304 struct buf *bp, *tbp;
308 KASSERT(size == vp->v_mount->mnt_stat.f_iosize,
309 ("cluster_rbuild: size %ld != filesize %ld\n",
310 size, vp->v_mount->mnt_stat.f_iosize));
315 while ((u_quad_t) size * (lbn + run) > filesize) {
321 tbp->b_flags |= B_READ;
323 tbp = getblk(vp, lbn, size, 0, 0);
324 if (tbp->b_flags & B_CACHE)
326 tbp->b_flags |= B_ASYNC | B_READ | B_RAM;
329 tbp->b_bio2.bio_blkno = blkno;
330 if( (tbp->b_flags & B_MALLOC) ||
331 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
334 bp = trypbuf(&cluster_pbuf_freecnt);
339 * We are synthesizing a buffer out of vm_page_t's, but
340 * if the block size is not page aligned then the starting
341 * address may not be either. Inherit the b_data offset
342 * from the original buffer.
344 bp->b_data = (char *)((vm_offset_t)bp->b_data |
345 ((vm_offset_t)tbp->b_data & PAGE_MASK));
346 bp->b_flags = B_ASYNC | B_READ | B_CLUSTER | B_VMIO;
347 bp->b_bio1.bio_done = cluster_callback;
348 bp->b_bio1.bio_caller_info1.cluster_head = NULL;
349 bp->b_bio1.bio_caller_info2.cluster_tail = NULL;
351 bp->b_loffset = tbp->b_loffset;
352 bp->b_bio2.bio_blkno = (daddr_t)-1;
353 KASSERT(bp->b_loffset != NOOFFSET,
354 ("cluster_rbuild: no buffer offset"));
359 bp->b_xio.xio_npages = 0;
362 for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
364 if ((bp->b_xio.xio_npages * PAGE_SIZE) +
365 round_page(size) > vp->v_mount->mnt_iosize_max) {
370 * Shortcut some checks and try to avoid buffers that
371 * would block in the lock. The same checks have to
372 * be made again after we officially get the buffer.
374 if ((tbp = findblk(vp, lbn + i)) != NULL) {
375 if (BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT))
379 for (j = 0; j < tbp->b_xio.xio_npages; j++) {
380 if (tbp->b_xio.xio_pages[j]->valid)
384 if (j != tbp->b_xio.xio_npages)
387 if (tbp->b_bcount != size)
391 tbp = getblk(vp, lbn + i, size, 0, 0);
394 * Stop scanning if the buffer is fuly valid
395 * (marked B_CACHE), or locked (may be doing a
396 * background write), or if the buffer is not
397 * VMIO backed. The clustering code can only deal
398 * with VMIO-backed buffers.
400 if ((tbp->b_flags & (B_CACHE|B_LOCKED)) ||
401 (tbp->b_flags & B_VMIO) == 0) {
407 * The buffer must be completely invalid in order to
408 * take part in the cluster. If it is partially valid
411 for (j = 0;j < tbp->b_xio.xio_npages; j++) {
412 if (tbp->b_xio.xio_pages[j]->valid)
415 if (j != tbp->b_xio.xio_npages) {
421 * Set a read-ahead mark as appropriate
423 if ((fbp && (i == 1)) || (i == (run - 1)))
424 tbp->b_flags |= B_RAM;
427 * Set the buffer up for an async read (XXX should
428 * we do this only if we do not wind up brelse()ing?).
429 * Set the block number if it isn't set, otherwise
430 * if it is make sure it matches the block number we
433 tbp->b_flags |= B_READ | B_ASYNC;
434 if (tbp->b_bio2.bio_blkno == (daddr_t)-1) {
435 tbp->b_bio2.bio_blkno = bn;
436 } else if (tbp->b_bio2.bio_blkno != bn) {
442 * XXX fbp from caller may not be B_ASYNC, but we are going
443 * to biodone() it in cluster_callback() anyway
446 cluster_append(&bp->b_bio1, tbp);
447 for (j = 0; j < tbp->b_xio.xio_npages; j += 1) {
449 m = tbp->b_xio.xio_pages[j];
451 vm_object_pip_add(m->object, 1);
452 if ((bp->b_xio.xio_npages == 0) ||
453 (bp->b_xio.xio_pages[bp->b_xio.xio_npages-1] != m)) {
454 bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m;
455 bp->b_xio.xio_npages++;
457 if ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL)
458 tbp->b_xio.xio_pages[j] = bogus_page;
461 * XXX shouldn't this be += size for both, like in
464 * Don't inherit tbp->b_bufsize as it may be larger due to
465 * a non-page-aligned size. Instead just aggregate using
468 if (tbp->b_bcount != size)
469 printf("warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size);
470 if (tbp->b_bufsize != size)
471 printf("warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size);
472 bp->b_bcount += size;
473 bp->b_bufsize += size;
477 * Fully valid pages in the cluster are already good and do not need
478 * to be re-read from disk. Replace the page with bogus_page
480 for (j = 0; j < bp->b_xio.xio_npages; j++) {
481 if ((bp->b_xio.xio_pages[j]->valid & VM_PAGE_BITS_ALL) ==
483 bp->b_xio.xio_pages[j] = bogus_page;
486 if (bp->b_bufsize > bp->b_kvasize)
487 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)",
488 bp->b_bufsize, bp->b_kvasize);
489 bp->b_kvasize = bp->b_bufsize;
491 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
492 (vm_page_t *)bp->b_xio.xio_pages, bp->b_xio.xio_npages);
497 * Cleanup after a clustered read or write.
498 * This is complicated by the fact that any of the buffers might have
499 * extra memory (if there were no empty buffer headers at allocbuf time)
500 * that we will need to shift around.
502 * The returned bio is &bp->b_bio1
505 cluster_callback(struct bio *bio)
507 struct buf *bp = bio->bio_buf;
512 * Must propogate errors to all the components.
514 if (bp->b_flags & B_ERROR)
517 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_xio.xio_npages);
519 * Move memory from the large cluster buffer into the component
520 * buffers and mark IO as done on these. Since the memory map
521 * is the same, no actual copying is required.
523 while ((tbp = bio->bio_caller_info1.cluster_head) != NULL) {
524 bio->bio_caller_info1.cluster_head = tbp->b_cluster_next;
526 tbp->b_flags |= B_ERROR;
527 tbp->b_error = error;
529 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
530 tbp->b_flags &= ~(B_ERROR|B_INVAL);
532 * XXX the bdwrite()/bqrelse() issued during
533 * cluster building clears B_RELBUF (see bqrelse()
534 * comment). If direct I/O was specified, we have
535 * to restore it here to allow the buffer and VM
538 if (tbp->b_flags & B_DIRECT)
539 tbp->b_flags |= B_RELBUF;
541 biodone(&tbp->b_bio1);
543 relpbuf(bp, &cluster_pbuf_freecnt);
549 * Implement modified write build for cluster.
551 * write_behind = 0 write behind disabled
552 * write_behind = 1 write behind normal (default)
553 * write_behind = 2 write behind backed-off
557 cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len)
561 switch(write_behind) {
568 r = cluster_wbuild(vp, size, start_lbn, len);
578 * Do clustered write for FFS.
581 * 1. Write is not sequential (write asynchronously)
582 * Write is sequential:
583 * 2. beginning of cluster - begin cluster
584 * 3. middle of a cluster - add to cluster
585 * 4. end of a cluster - asynchronously write cluster
588 cluster_write(struct buf *bp, u_quad_t filesize, int seqcount)
592 int maxclen, cursize;
597 if (vp->v_type == VREG) {
598 async = vp->v_mount->mnt_flag & MNT_ASYNC;
599 lblocksize = vp->v_mount->mnt_stat.f_iosize;
602 lblocksize = bp->b_bufsize;
605 KASSERT(bp->b_loffset != NOOFFSET,
606 ("cluster_write: no buffer offset"));
608 /* Initialize vnode to beginning of file. */
610 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
612 if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 ||
613 bp->b_bio2.bio_blkno == (daddr_t)-1 ||
614 (bp->b_bio2.bio_blkno != vp->v_lasta + btodb(lblocksize))) {
615 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1;
616 if (vp->v_clen != 0) {
618 * Next block is not sequential.
620 * If we are not writing at end of file, the process
621 * seeked to another point in the file since its last
622 * write, or we have reached our maximum cluster size,
623 * then push the previous cluster. Otherwise try
624 * reallocating to make it sequential.
626 * Change to algorithm: only push previous cluster if
627 * it was sequential from the point of view of the
628 * seqcount heuristic, otherwise leave the buffer
629 * intact so we can potentially optimize the I/O
630 * later on in the buf_daemon or update daemon
633 cursize = vp->v_lastw - vp->v_cstart + 1;
634 if (((u_quad_t) bp->b_loffset + lblocksize) != filesize ||
635 lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) {
636 if (!async && seqcount > 0) {
637 cluster_wbuild_wb(vp, lblocksize,
638 vp->v_cstart, cursize);
641 struct buf **bpp, **endbp;
642 struct cluster_save *buflist;
644 buflist = cluster_collectbufs(vp, bp);
645 endbp = &buflist->bs_children
646 [buflist->bs_nchildren - 1];
647 if (VOP_REALLOCBLKS(vp, buflist)) {
649 * Failed, push the previous cluster
650 * if *really* writing sequentially
651 * in the logical file (seqcount > 1),
652 * otherwise delay it in the hopes that
653 * the low level disk driver can
654 * optimize the write ordering.
656 for (bpp = buflist->bs_children;
659 free(buflist, M_SEGMENT);
661 cluster_wbuild_wb(vp,
662 lblocksize, vp->v_cstart,
667 * Succeeded, keep building cluster.
669 for (bpp = buflist->bs_children;
672 free(buflist, M_SEGMENT);
674 vp->v_lasta = bp->b_bio2.bio_blkno;
680 * Consider beginning a cluster. If at end of file, make
681 * cluster as large as possible, otherwise find size of
684 if ((vp->v_type == VREG) &&
685 ((u_quad_t) bp->b_loffset + lblocksize) != filesize &&
686 (bp->b_bio2.bio_blkno == (daddr_t)-1) &&
687 (VOP_BMAP(vp, lbn, NULL, &bp->b_bio2.bio_blkno, &maxclen, NULL) ||
688 bp->b_bio2.bio_blkno == (daddr_t)-1)) {
691 vp->v_lasta = bp->b_bio2.bio_blkno;
692 vp->v_cstart = lbn + 1;
696 vp->v_clen = maxclen;
697 if (!async && maxclen == 0) { /* I/O not contiguous */
698 vp->v_cstart = lbn + 1;
700 } else { /* Wait for rest of cluster */
704 } else if (lbn == vp->v_cstart + vp->v_clen) {
706 * At end of cluster, write it out if seqcount tells us we
707 * are operating sequentially, otherwise let the buf or
708 * update daemon handle it.
712 cluster_wbuild_wb(vp, lblocksize, vp->v_cstart, vp->v_clen + 1);
714 vp->v_cstart = lbn + 1;
715 } else if (vm_page_count_severe()) {
717 * We are low on memory, get it going NOW
722 * In the middle of a cluster, so just delay the I/O for now.
727 vp->v_lasta = bp->b_bio2.bio_blkno;
732 * This is an awful lot like cluster_rbuild...wish they could be combined.
733 * The last lbn argument is the current block on which I/O is being
734 * performed. Check to see that it doesn't fall in the middle of
735 * the current block (if last_bp == NULL).
738 cluster_wbuild(struct vnode *vp, long size, daddr_t start_lbn, int len)
740 struct buf *bp, *tbp;
742 int totalwritten = 0;
743 int dbsize = btodb(size);
748 * If the buffer is not delayed-write (i.e. dirty), or it
749 * is delayed-write but either locked or inval, it cannot
750 * partake in the clustered write.
752 if (((tbp = findblk(vp, start_lbn)) == NULL) ||
753 ((tbp->b_flags & (B_LOCKED | B_INVAL | B_DELWRI)) != B_DELWRI) ||
754 BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) {
761 tbp->b_flags &= ~B_DONE;
765 * Extra memory in the buffer, punt on this buffer.
766 * XXX we could handle this in most cases, but we would
767 * have to push the extra memory down to after our max
768 * possible cluster size and then potentially pull it back
769 * up if the cluster was terminated prematurely--too much
772 if (((tbp->b_flags & (B_CLUSTEROK|B_MALLOC)) != B_CLUSTEROK) ||
773 (tbp->b_bcount != tbp->b_bufsize) ||
774 (tbp->b_bcount != size) ||
776 ((bp = getpbuf(&cluster_pbuf_freecnt)) == NULL)) {
777 totalwritten += tbp->b_bufsize;
785 * We got a pbuf to make the cluster in.
790 bp->b_xio.xio_npages = 0;
791 bp->b_lblkno = tbp->b_lblkno;
792 bp->b_loffset = tbp->b_loffset;
793 bp->b_bio2.bio_blkno = tbp->b_bio2.bio_blkno;
796 * We are synthesizing a buffer out of vm_page_t's, but
797 * if the block size is not page aligned then the starting
798 * address may not be either. Inherit the b_data offset
799 * from the original buffer.
801 bp->b_data = (char *)((vm_offset_t)bp->b_data |
802 ((vm_offset_t)tbp->b_data & PAGE_MASK));
803 bp->b_flags |= B_CLUSTER |
804 (tbp->b_flags & (B_VMIO | B_NEEDCOMMIT | B_NOWDRAIN));
805 bp->b_bio1.bio_done = cluster_callback;
806 bp->b_bio1.bio_caller_info1.cluster_head = NULL;
807 bp->b_bio1.bio_caller_info2.cluster_tail = NULL;
810 * From this location in the file, scan forward to see
811 * if there are buffers with adjacent data that need to
812 * be written as well.
814 for (i = 0; i < len; ++i, ++start_lbn) {
815 if (i != 0) { /* If not the first buffer */
818 * If the adjacent data is not even in core it
819 * can't need to be written.
821 if ((tbp = findblk(vp, start_lbn)) == NULL) {
827 * If it IS in core, but has different
828 * characteristics, or is locked (which
829 * means it could be undergoing a background
830 * I/O or be in a weird state), then don't
833 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
834 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
835 != (B_DELWRI | B_CLUSTEROK |
836 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
837 (tbp->b_flags & B_LOCKED) ||
838 BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) {
844 * Check that the combined cluster
845 * would make sense with regard to pages
846 * and would not be too large
848 if ((tbp->b_bcount != size) ||
849 ((bp->b_bio2.bio_blkno + (dbsize * i)) !=
850 tbp->b_bio2.bio_blkno) ||
851 ((tbp->b_xio.xio_npages + bp->b_xio.xio_npages) >
852 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
858 * Ok, it's passed all the tests,
859 * so remove it from the free list
860 * and mark it busy. We will use it.
863 tbp->b_flags &= ~B_DONE;
865 } /* end of code for non-first buffers only */
868 * If the IO is via the VM then we do some
869 * special VM hackery (yuck). Since the buffer's
870 * block size may not be page-aligned it is possible
871 * for a page to be shared between two buffers. We
872 * have to get rid of the duplication when building
875 if (tbp->b_flags & B_VMIO) {
878 if (i != 0) { /* if not first buffer */
879 for (j = 0; j < tbp->b_xio.xio_npages; j += 1) {
880 m = tbp->b_xio.xio_pages[j];
881 if (m->flags & PG_BUSY) {
888 for (j = 0; j < tbp->b_xio.xio_npages; j += 1) {
889 m = tbp->b_xio.xio_pages[j];
891 vm_object_pip_add(m->object, 1);
892 if ((bp->b_xio.xio_npages == 0) ||
893 (bp->b_xio.xio_pages[bp->b_xio.xio_npages - 1] != m)) {
894 bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m;
895 bp->b_xio.xio_npages++;
899 bp->b_bcount += size;
900 bp->b_bufsize += size;
904 tbp->b_flags &= ~(B_READ | B_DONE | B_ERROR);
905 tbp->b_flags |= B_ASYNC;
908 cluster_append(&bp->b_bio1, tbp);
911 * check for latent dependencies to be handled
913 if (LIST_FIRST(&tbp->b_dep) != NULL && bioops.io_start)
914 (*bioops.io_start)(tbp);
918 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
919 (vm_page_t *) bp->b_xio.xio_pages, bp->b_xio.xio_npages);
920 if (bp->b_bufsize > bp->b_kvasize)
922 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
923 bp->b_bufsize, bp->b_kvasize);
924 bp->b_kvasize = bp->b_bufsize;
925 totalwritten += bp->b_bufsize;
927 bp->b_dirtyend = bp->b_bufsize;
936 * Collect together all the buffers in a cluster.
937 * Plus add one additional buffer.
939 static struct cluster_save *
940 cluster_collectbufs(struct vnode *vp, struct buf *last_bp)
942 struct cluster_save *buflist;
947 len = vp->v_lastw - vp->v_cstart + 1;
948 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
949 M_SEGMENT, M_WAITOK);
950 buflist->bs_nchildren = 0;
951 buflist->bs_children = (struct buf **) (buflist + 1);
952 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) {
953 (void) bread(vp, lbn, last_bp->b_bcount, &bp);
954 buflist->bs_children[i] = bp;
955 if (bp->b_bio2.bio_blkno == (daddr_t)-1)
956 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_bio2.bio_blkno,
959 buflist->bs_children[i] = bp = last_bp;
960 if (bp->b_bio2.bio_blkno == (daddr_t)-1)
961 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_bio2.bio_blkno,
963 buflist->bs_nchildren = i + 1;
968 cluster_append(struct bio *bio, struct buf *tbp)
970 tbp->b_cluster_next = NULL;
971 if (bio->bio_caller_info1.cluster_head == NULL) {
972 bio->bio_caller_info1.cluster_head = tbp;
973 bio->bio_caller_info2.cluster_tail = tbp;
975 bio->bio_caller_info2.cluster_tail->b_cluster_next = tbp;
976 bio->bio_caller_info2.cluster_tail = tbp;