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.7 2003/07/03 17:24:02 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 buffer", "cluster_save buffer");
67 static struct cluster_save *
68 cluster_collectbufs __P((struct vnode *vp, struct buf *last_bp));
70 cluster_rbuild __P((struct vnode *vp, u_quad_t filesize, daddr_t lbn,
71 daddr_t blkno, long size, int run, struct buf *fbp));
73 static int write_behind = 1;
74 SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0, "");
76 extern vm_page_t bogus_page;
78 extern int cluster_pbuf_freecnt;
81 * Maximum number of blocks for read-ahead.
86 * This replaces bread.
89 cluster_read(vp, filesize, lblkno, size, totread, seqcount, bpp)
98 struct buf *bp, *rbp, *reqbp;
99 daddr_t blkno, origblkno;
102 int maxra, racluster;
108 * Try to limit the amount of read-ahead by a few
109 * ad-hoc parameters. This needs work!!!
111 racluster = vp->v_mount->mnt_iosize_max / size;
112 maxra = 2 * racluster + (totread / size);
119 * get the requested block
121 *bpp = reqbp = bp = getblk(vp, lblkno, size, 0, 0);
123 origtotread = totread;
126 * if it is in the cache, then check to see if the reads have been
127 * sequential. If they have, then try some read-ahead, otherwise
128 * back-off on prospective read-aheads.
130 if (bp->b_flags & B_CACHE) {
133 } else if ((bp->b_flags & B_RAM) == 0) {
138 bp->b_flags &= ~B_RAM;
140 * We do the spl here so that there is no window
141 * between the incore and the b_usecount increment
142 * below. We opt to keep the spl out of the loop
146 for (i = 1; i < maxra; i++) {
148 if (!(tbp = incore(vp, lblkno+i))) {
153 * Set another read-ahead mark so we know
156 if (((i % racluster) == (racluster - 1)) ||
158 tbp->b_flags |= B_RAM;
168 off_t firstread = bp->b_offset;
170 KASSERT(bp->b_offset != NOOFFSET,
171 ("cluster_read: no buffer offset"));
172 if (firstread + totread > filesize)
173 totread = filesize - firstread;
174 if (totread > size) {
177 while (totread > 0) {
182 goto single_block_read;
183 if (nblks > racluster)
186 error = VOP_BMAP(vp, lblkno, NULL,
187 &blkno, &ncontigafter, NULL);
189 goto single_block_read;
191 goto single_block_read;
192 if (ncontigafter == 0)
193 goto single_block_read;
194 if (ncontigafter + 1 < nblks)
195 nblks = ncontigafter + 1;
197 bp = cluster_rbuild(vp, filesize, lblkno,
198 blkno, size, nblks, bp);
199 lblkno += (bp->b_bufsize / size);
203 * if it isn't in the cache, then get a chunk from
204 * disk if sequential, otherwise just get the block.
206 bp->b_flags |= B_READ | B_RAM;
212 * if we have been doing sequential I/O, then do some read-ahead
215 if (seqcount && (lblkno < (origblkno + seqcount))) {
217 * we now build the read-ahead buffer if it is desirable.
219 if (((u_quad_t)(lblkno + 1) * size) <= filesize &&
220 !(error = VOP_BMAP(vp, lblkno, NULL, &blkno, &num_ra, NULL)) &&
223 int ntoread = num_ra + 1;
224 nblksread = (origtotread + size - 1) / size;
225 if (seqcount < nblksread)
226 seqcount = nblksread;
227 if (seqcount < ntoread)
230 rbp = cluster_rbuild(vp, filesize, lblkno,
231 blkno, size, ntoread, NULL);
233 rbp = getblk(vp, lblkno, size, 0, 0);
234 rbp->b_flags |= B_READ | B_ASYNC | B_RAM;
235 rbp->b_blkno = blkno;
241 * handle the synchronous read
244 #if defined(CLUSTERDEBUG)
246 printf("S(%ld,%ld,%d) ",
247 (long)bp->b_lblkno, bp->b_bcount, seqcount);
249 if ((bp->b_flags & B_CLUSTER) == 0) {
250 vfs_busy_pages(bp, 0);
252 bp->b_flags &= ~(B_ERROR|B_INVAL);
253 if (bp->b_flags & (B_ASYNC|B_CALL))
255 error = VOP_STRATEGY(vp, bp);
259 * and if we have read-aheads, do them too
263 rbp->b_flags &= ~(B_ASYNC | B_READ);
265 } else if (rbp->b_flags & B_CACHE) {
266 rbp->b_flags &= ~(B_ASYNC | B_READ);
269 #if defined(CLUSTERDEBUG)
272 printf("A+(%ld,%ld,%ld,%d) ",
273 (long)rbp->b_lblkno, rbp->b_bcount,
274 (long)(rbp->b_lblkno - origblkno),
277 printf("A(%ld,%ld,%ld,%d) ",
278 (long)rbp->b_lblkno, rbp->b_bcount,
279 (long)(rbp->b_lblkno - origblkno),
284 if ((rbp->b_flags & B_CLUSTER) == 0) {
285 vfs_busy_pages(rbp, 0);
287 rbp->b_flags &= ~(B_ERROR|B_INVAL);
288 if (rbp->b_flags & (B_ASYNC|B_CALL))
290 (void) VOP_STRATEGY(vp, rbp);
294 return (biowait(reqbp));
300 * If blocks are contiguous on disk, use this to provide clustered
301 * read ahead. We will read as many blocks as possible sequentially
302 * and then parcel them up into logical blocks in the buffer hash table.
305 cluster_rbuild(vp, filesize, lbn, blkno, size, run, 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) {
331 tbp->b_flags |= B_READ;
333 tbp = getblk(vp, lbn, size, 0, 0);
334 if (tbp->b_flags & B_CACHE)
336 tbp->b_flags |= B_ASYNC | B_READ | B_RAM;
339 tbp->b_blkno = blkno;
340 if( (tbp->b_flags & B_MALLOC) ||
341 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
344 bp = trypbuf(&cluster_pbuf_freecnt);
349 * We are synthesizing a buffer out of vm_page_t's, but
350 * if the block size is not page aligned then the starting
351 * address may not be either. Inherit the b_data offset
352 * from the original buffer.
354 bp->b_data = (char *)((vm_offset_t)bp->b_data |
355 ((vm_offset_t)tbp->b_data & PAGE_MASK));
356 bp->b_flags = B_ASYNC | B_READ | B_CALL | B_CLUSTER | B_VMIO;
357 bp->b_iodone = cluster_callback;
360 bp->b_offset = tbp->b_offset;
361 KASSERT(bp->b_offset != NOOFFSET, ("cluster_rbuild: no buffer offset"));
364 TAILQ_INIT(&bp->b_cluster.cluster_head);
371 for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
373 if ((bp->b_npages * PAGE_SIZE) +
374 round_page(size) > vp->v_mount->mnt_iosize_max) {
379 * Shortcut some checks and try to avoid buffers that
380 * would block in the lock. The same checks have to
381 * be made again after we officially get the buffer.
383 if ((tbp = incore(vp, lbn + i)) != NULL) {
384 if (BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT))
388 for (j = 0; j < tbp->b_npages; j++) {
389 if (tbp->b_pages[j]->valid)
393 if (j != tbp->b_npages)
396 if (tbp->b_bcount != size)
400 tbp = getblk(vp, lbn + i, size, 0, 0);
403 * Stop scanning if the buffer is fuly valid
404 * (marked B_CACHE), or locked (may be doing a
405 * background write), or if the buffer is not
406 * VMIO backed. The clustering code can only deal
407 * with VMIO-backed buffers.
409 if ((tbp->b_flags & (B_CACHE|B_LOCKED)) ||
410 (tbp->b_flags & B_VMIO) == 0) {
416 * The buffer must be completely invalid in order to
417 * take part in the cluster. If it is partially valid
420 for (j = 0;j < tbp->b_npages; j++) {
421 if (tbp->b_pages[j]->valid)
424 if (j != tbp->b_npages) {
430 * Set a read-ahead mark as appropriate
432 if ((fbp && (i == 1)) || (i == (run - 1)))
433 tbp->b_flags |= B_RAM;
436 * Set the buffer up for an async read (XXX should
437 * we do this only if we do not wind up brelse()ing?).
438 * Set the block number if it isn't set, otherwise
439 * if it is make sure it matches the block number we
442 tbp->b_flags |= B_READ | B_ASYNC;
443 if (tbp->b_blkno == tbp->b_lblkno) {
445 } else if (tbp->b_blkno != bn) {
451 * XXX fbp from caller may not be B_ASYNC, but we are going
452 * to biodone() it in cluster_callback() anyway
455 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
456 tbp, b_cluster.cluster_entry);
457 for (j = 0; j < tbp->b_npages; j += 1) {
461 vm_object_pip_add(m->object, 1);
462 if ((bp->b_npages == 0) ||
463 (bp->b_pages[bp->b_npages-1] != m)) {
464 bp->b_pages[bp->b_npages] = m;
467 if ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL)
468 tbp->b_pages[j] = bogus_page;
471 * XXX shouldn't this be += size for both, like in
474 * Don't inherit tbp->b_bufsize as it may be larger due to
475 * a non-page-aligned size. Instead just aggregate using
478 if (tbp->b_bcount != size)
479 printf("warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size);
480 if (tbp->b_bufsize != size)
481 printf("warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size);
482 bp->b_bcount += size;
483 bp->b_bufsize += size;
487 * Fully valid pages in the cluster are already good and do not need
488 * to be re-read from disk. Replace the page with bogus_page
490 for (j = 0; j < bp->b_npages; j++) {
491 if ((bp->b_pages[j]->valid & VM_PAGE_BITS_ALL) ==
493 bp->b_pages[j] = bogus_page;
496 if (bp->b_bufsize > bp->b_kvasize)
497 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
498 bp->b_bufsize, bp->b_kvasize);
499 bp->b_kvasize = bp->b_bufsize;
501 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
502 (vm_page_t *)bp->b_pages, bp->b_npages);
507 * Cleanup after a clustered read or write.
508 * This is complicated by the fact that any of the buffers might have
509 * extra memory (if there were no empty buffer headers at allocbuf time)
510 * that we will need to shift around.
516 struct buf *nbp, *tbp;
520 * Must propogate errors to all the components.
522 if (bp->b_flags & B_ERROR)
525 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages);
527 * Move memory from the large cluster buffer into the component
528 * buffers and mark IO as done on these.
530 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head);
532 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry);
534 tbp->b_flags |= B_ERROR;
535 tbp->b_error = error;
537 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
538 tbp->b_flags &= ~(B_ERROR|B_INVAL);
540 * XXX the bdwrite()/bqrelse() issued during
541 * cluster building clears B_RELBUF (see bqrelse()
542 * comment). If direct I/O was specified, we have
543 * to restore it here to allow the buffer and VM
546 if (tbp->b_flags & B_DIRECT)
547 tbp->b_flags |= B_RELBUF;
551 relpbuf(bp, &cluster_pbuf_freecnt);
557 * Implement modified write build for cluster.
559 * write_behind = 0 write behind disabled
560 * write_behind = 1 write behind normal (default)
561 * write_behind = 2 write behind backed-off
565 cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len)
569 switch(write_behind) {
576 r = cluster_wbuild(vp, size, start_lbn, len);
586 * Do clustered write for FFS.
589 * 1. Write is not sequential (write asynchronously)
590 * Write is sequential:
591 * 2. beginning of cluster - begin cluster
592 * 3. middle of a cluster - add to cluster
593 * 4. end of a cluster - asynchronously write cluster
596 cluster_write(bp, filesize, seqcount)
603 int maxclen, cursize;
608 if (vp->v_type == VREG) {
609 async = vp->v_mount->mnt_flag & MNT_ASYNC;
610 lblocksize = vp->v_mount->mnt_stat.f_iosize;
613 lblocksize = bp->b_bufsize;
616 KASSERT(bp->b_offset != NOOFFSET, ("cluster_write: no buffer offset"));
618 /* Initialize vnode to beginning of file. */
620 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
622 if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 ||
623 (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) {
624 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1;
625 if (vp->v_clen != 0) {
627 * Next block is not sequential.
629 * If we are not writing at end of file, the process
630 * seeked to another point in the file since its last
631 * write, or we have reached our maximum cluster size,
632 * then push the previous cluster. Otherwise try
633 * reallocating to make it sequential.
635 * Change to algorithm: only push previous cluster if
636 * it was sequential from the point of view of the
637 * seqcount heuristic, otherwise leave the buffer
638 * intact so we can potentially optimize the I/O
639 * later on in the buf_daemon or update daemon
642 cursize = vp->v_lastw - vp->v_cstart + 1;
643 if (((u_quad_t) bp->b_offset + lblocksize) != filesize ||
644 lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) {
645 if (!async && seqcount > 0) {
646 cluster_wbuild_wb(vp, lblocksize,
647 vp->v_cstart, cursize);
650 struct buf **bpp, **endbp;
651 struct cluster_save *buflist;
653 buflist = cluster_collectbufs(vp, bp);
654 endbp = &buflist->bs_children
655 [buflist->bs_nchildren - 1];
656 if (VOP_REALLOCBLKS(vp, buflist)) {
658 * Failed, push the previous cluster
659 * if *really* writing sequentially
660 * in the logical file (seqcount > 1),
661 * otherwise delay it in the hopes that
662 * the low level disk driver can
663 * optimize the write ordering.
665 for (bpp = buflist->bs_children;
668 free(buflist, M_SEGMENT);
670 cluster_wbuild_wb(vp,
671 lblocksize, vp->v_cstart,
676 * Succeeded, keep building cluster.
678 for (bpp = buflist->bs_children;
681 free(buflist, M_SEGMENT);
683 vp->v_lasta = bp->b_blkno;
689 * Consider beginning a cluster. If at end of file, make
690 * cluster as large as possible, otherwise find size of
693 if ((vp->v_type == VREG) &&
694 ((u_quad_t) bp->b_offset + lblocksize) != filesize &&
695 (bp->b_blkno == bp->b_lblkno) &&
696 (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) ||
697 bp->b_blkno == -1)) {
700 vp->v_lasta = bp->b_blkno;
701 vp->v_cstart = lbn + 1;
705 vp->v_clen = maxclen;
706 if (!async && maxclen == 0) { /* I/O not contiguous */
707 vp->v_cstart = lbn + 1;
709 } else { /* Wait for rest of cluster */
713 } else if (lbn == vp->v_cstart + vp->v_clen) {
715 * At end of cluster, write it out if seqcount tells us we
716 * are operating sequentially, otherwise let the buf or
717 * update daemon handle it.
721 cluster_wbuild_wb(vp, lblocksize, vp->v_cstart, vp->v_clen + 1);
723 vp->v_cstart = lbn + 1;
724 } else if (vm_page_count_severe()) {
726 * We are low on memory, get it going NOW
731 * In the middle of a cluster, so just delay the I/O for now.
736 vp->v_lasta = bp->b_blkno;
741 * This is an awful lot like cluster_rbuild...wish they could be combined.
742 * The last lbn argument is the current block on which I/O is being
743 * performed. Check to see that it doesn't fall in the middle of
744 * the current block (if last_bp == NULL).
747 cluster_wbuild(vp, size, start_lbn, len)
753 struct buf *bp, *tbp;
755 int totalwritten = 0;
756 int dbsize = btodb(size);
761 * If the buffer is not delayed-write (i.e. dirty), or it
762 * is delayed-write but either locked or inval, it cannot
763 * partake in the clustered write.
765 if (((tbp = gbincore(vp, start_lbn)) == NULL) ||
766 ((tbp->b_flags & (B_LOCKED | B_INVAL | B_DELWRI)) != B_DELWRI) ||
767 BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) {
774 tbp->b_flags &= ~B_DONE;
778 * Extra memory in the buffer, punt on this buffer.
779 * XXX we could handle this in most cases, but we would
780 * have to push the extra memory down to after our max
781 * possible cluster size and then potentially pull it back
782 * up if the cluster was terminated prematurely--too much
785 if (((tbp->b_flags & (B_CLUSTEROK|B_MALLOC)) != B_CLUSTEROK) ||
786 (tbp->b_bcount != tbp->b_bufsize) ||
787 (tbp->b_bcount != size) ||
789 ((bp = getpbuf(&cluster_pbuf_freecnt)) == NULL)) {
790 totalwritten += tbp->b_bufsize;
798 * We got a pbuf to make the cluster in.
801 TAILQ_INIT(&bp->b_cluster.cluster_head);
805 bp->b_blkno = tbp->b_blkno;
806 bp->b_lblkno = tbp->b_lblkno;
807 bp->b_offset = tbp->b_offset;
810 * We are synthesizing a buffer out of vm_page_t's, but
811 * if the block size is not page aligned then the starting
812 * address may not be either. Inherit the b_data offset
813 * from the original buffer.
815 bp->b_data = (char *)((vm_offset_t)bp->b_data |
816 ((vm_offset_t)tbp->b_data & PAGE_MASK));
817 bp->b_flags |= B_CALL | B_CLUSTER |
818 (tbp->b_flags & (B_VMIO | B_NEEDCOMMIT | B_NOWDRAIN));
819 bp->b_iodone = cluster_callback;
822 * From this location in the file, scan forward to see
823 * if there are buffers with adjacent data that need to
824 * be written as well.
826 for (i = 0; i < len; ++i, ++start_lbn) {
827 if (i != 0) { /* If not the first buffer */
830 * If the adjacent data is not even in core it
831 * can't need to be written.
833 if ((tbp = gbincore(vp, start_lbn)) == NULL) {
839 * If it IS in core, but has different
840 * characteristics, or is locked (which
841 * means it could be undergoing a background
842 * I/O or be in a weird state), then don't
845 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
846 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
847 != (B_DELWRI | B_CLUSTEROK |
848 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
849 (tbp->b_flags & B_LOCKED) ||
850 BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) {
856 * Check that the combined cluster
857 * would make sense with regard to pages
858 * and would not be too large
860 if ((tbp->b_bcount != size) ||
861 ((bp->b_blkno + (dbsize * i)) !=
863 ((tbp->b_npages + bp->b_npages) >
864 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
870 * Ok, it's passed all the tests,
871 * so remove it from the free list
872 * and mark it busy. We will use it.
875 tbp->b_flags &= ~B_DONE;
877 } /* end of code for non-first buffers only */
878 /* check for latent dependencies to be handled */
879 if ((LIST_FIRST(&tbp->b_dep)) != NULL &&
881 (*bioops.io_start)(tbp);
883 * If the IO is via the VM then we do some
884 * special VM hackery (yuck). Since the buffer's
885 * block size may not be page-aligned it is possible
886 * for a page to be shared between two buffers. We
887 * have to get rid of the duplication when building
890 if (tbp->b_flags & B_VMIO) {
893 if (i != 0) { /* if not first buffer */
894 for (j = 0; j < tbp->b_npages; j += 1) {
896 if (m->flags & PG_BUSY) {
903 for (j = 0; j < tbp->b_npages; j += 1) {
906 vm_object_pip_add(m->object, 1);
907 if ((bp->b_npages == 0) ||
908 (bp->b_pages[bp->b_npages - 1] != m)) {
909 bp->b_pages[bp->b_npages] = m;
914 bp->b_bcount += size;
915 bp->b_bufsize += size;
919 tbp->b_flags &= ~(B_READ | B_DONE | B_ERROR);
920 tbp->b_flags |= B_ASYNC;
921 reassignbuf(tbp, tbp->b_vp); /* put on clean list */
922 ++tbp->b_vp->v_numoutput;
925 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
926 tbp, b_cluster.cluster_entry);
929 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
930 (vm_page_t *) bp->b_pages, bp->b_npages);
931 if (bp->b_bufsize > bp->b_kvasize)
933 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
934 bp->b_bufsize, bp->b_kvasize);
935 bp->b_kvasize = bp->b_bufsize;
936 totalwritten += bp->b_bufsize;
938 bp->b_dirtyend = bp->b_bufsize;
947 * Collect together all the buffers in a cluster.
948 * Plus add one additional buffer.
950 static struct cluster_save *
951 cluster_collectbufs(vp, last_bp)
955 struct cluster_save *buflist;
960 len = vp->v_lastw - vp->v_cstart + 1;
961 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
962 M_SEGMENT, M_WAITOK);
963 buflist->bs_nchildren = 0;
964 buflist->bs_children = (struct buf **) (buflist + 1);
965 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) {
966 (void) bread(vp, lbn, last_bp->b_bcount, &bp);
967 buflist->bs_children[i] = bp;
968 if (bp->b_blkno == bp->b_lblkno)
969 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno,
972 buflist->bs_children[i] = bp = last_bp;
973 if (bp->b_blkno == bp->b_lblkno)
974 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno,
976 buflist->bs_nchildren = i + 1;