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.10 2004/06/01 22:19:30 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 (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);
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(struct vnode *vp, u_quad_t filesize, daddr_t lblkno,
90 long size, long totread, int seqcount, struct buf **bpp)
92 struct buf *bp, *rbp, *reqbp;
93 daddr_t blkno, origblkno;
102 * Try to limit the amount of read-ahead by a few
103 * ad-hoc parameters. This needs work!!!
105 racluster = vp->v_mount->mnt_iosize_max / size;
106 maxra = 2 * racluster + (totread / size);
113 * get the requested block
115 *bpp = reqbp = bp = getblk(vp, lblkno, size, 0, 0);
117 origtotread = totread;
120 * if it is in the cache, then check to see if the reads have been
121 * sequential. If they have, then try some read-ahead, otherwise
122 * back-off on prospective read-aheads.
124 if (bp->b_flags & B_CACHE) {
127 } else if ((bp->b_flags & B_RAM) == 0) {
132 bp->b_flags &= ~B_RAM;
134 * We do the spl here so that there is no window
135 * between the incore and the b_usecount increment
136 * below. We opt to keep the spl out of the loop
140 for (i = 1; i < maxra; i++) {
142 if (!(tbp = incore(vp, lblkno+i))) {
147 * Set another read-ahead mark so we know
150 if (((i % racluster) == (racluster - 1)) ||
152 tbp->b_flags |= B_RAM;
162 off_t firstread = bp->b_offset;
164 KASSERT(bp->b_offset != NOOFFSET,
165 ("cluster_read: no buffer offset"));
166 if (firstread + totread > filesize)
167 totread = filesize - firstread;
168 if (totread > size) {
171 while (totread > 0) {
176 goto single_block_read;
177 if (nblks > racluster)
180 error = VOP_BMAP(vp, lblkno, NULL,
181 &blkno, &ncontigafter, NULL);
183 goto single_block_read;
185 goto single_block_read;
186 if (ncontigafter == 0)
187 goto single_block_read;
188 if (ncontigafter + 1 < nblks)
189 nblks = ncontigafter + 1;
191 bp = cluster_rbuild(vp, filesize, lblkno,
192 blkno, size, nblks, bp);
193 lblkno += (bp->b_bufsize / size);
197 * if it isn't in the cache, then get a chunk from
198 * disk if sequential, otherwise just get the block.
200 bp->b_flags |= B_READ | B_RAM;
206 * if we have been doing sequential I/O, then do some read-ahead
209 if (seqcount && (lblkno < (origblkno + seqcount))) {
211 * we now build the read-ahead buffer if it is desirable.
213 if (((u_quad_t)(lblkno + 1) * size) <= filesize &&
214 !(error = VOP_BMAP(vp, lblkno, NULL, &blkno, &num_ra, NULL)) &&
217 int ntoread = num_ra + 1;
218 nblksread = (origtotread + size - 1) / size;
219 if (seqcount < nblksread)
220 seqcount = nblksread;
221 if (seqcount < ntoread)
224 rbp = cluster_rbuild(vp, filesize, lblkno,
225 blkno, size, ntoread, NULL);
227 rbp = getblk(vp, lblkno, size, 0, 0);
228 rbp->b_flags |= B_READ | B_ASYNC | B_RAM;
229 rbp->b_blkno = blkno;
235 * handle the synchronous read
238 #if defined(CLUSTERDEBUG)
240 printf("S(%ld,%ld,%d) ",
241 (long)bp->b_lblkno, bp->b_bcount, seqcount);
243 if ((bp->b_flags & B_CLUSTER) == 0) {
244 vfs_busy_pages(bp, 0);
246 bp->b_flags &= ~(B_ERROR|B_INVAL);
247 if (bp->b_flags & (B_ASYNC|B_CALL))
249 error = VOP_STRATEGY(vp, bp);
253 * and if we have read-aheads, do them too
257 rbp->b_flags &= ~(B_ASYNC | B_READ);
259 } else if (rbp->b_flags & B_CACHE) {
260 rbp->b_flags &= ~(B_ASYNC | B_READ);
263 #if defined(CLUSTERDEBUG)
266 printf("A+(%ld,%ld,%ld,%d) ",
267 (long)rbp->b_lblkno, rbp->b_bcount,
268 (long)(rbp->b_lblkno - origblkno),
271 printf("A(%ld,%ld,%ld,%d) ",
272 (long)rbp->b_lblkno, rbp->b_bcount,
273 (long)(rbp->b_lblkno - origblkno),
278 if ((rbp->b_flags & B_CLUSTER) == 0) {
279 vfs_busy_pages(rbp, 0);
281 rbp->b_flags &= ~(B_ERROR|B_INVAL);
282 if (rbp->b_flags & (B_ASYNC|B_CALL))
284 (void) VOP_STRATEGY(vp, rbp);
288 return (biowait(reqbp));
294 * If blocks are contiguous on disk, use this to provide clustered
295 * read ahead. We will read as many blocks as possible sequentially
296 * and then parcel them up into logical blocks in the buffer hash table.
299 cluster_rbuild(struct vnode *vp, u_quad_t filesize, daddr_t lbn,
300 daddr_t blkno, long size, int run, struct buf *fbp)
302 struct buf *bp, *tbp;
306 KASSERT(size == vp->v_mount->mnt_stat.f_iosize,
307 ("cluster_rbuild: size %ld != filesize %ld\n",
308 size, vp->v_mount->mnt_stat.f_iosize));
313 while ((u_quad_t) size * (lbn + run) > filesize) {
319 tbp->b_flags |= B_READ;
321 tbp = getblk(vp, lbn, size, 0, 0);
322 if (tbp->b_flags & B_CACHE)
324 tbp->b_flags |= B_ASYNC | B_READ | B_RAM;
327 tbp->b_blkno = blkno;
328 if( (tbp->b_flags & B_MALLOC) ||
329 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
332 bp = trypbuf(&cluster_pbuf_freecnt);
337 * We are synthesizing a buffer out of vm_page_t's, but
338 * if the block size is not page aligned then the starting
339 * address may not be either. Inherit the b_data offset
340 * from the original buffer.
342 bp->b_data = (char *)((vm_offset_t)bp->b_data |
343 ((vm_offset_t)tbp->b_data & PAGE_MASK));
344 bp->b_flags = B_ASYNC | B_READ | B_CALL | B_CLUSTER | B_VMIO;
345 bp->b_iodone = cluster_callback;
348 bp->b_offset = tbp->b_offset;
349 KASSERT(bp->b_offset != NOOFFSET, ("cluster_rbuild: no buffer offset"));
352 TAILQ_INIT(&bp->b_cluster.cluster_head);
359 for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
361 if ((bp->b_npages * PAGE_SIZE) +
362 round_page(size) > vp->v_mount->mnt_iosize_max) {
367 * Shortcut some checks and try to avoid buffers that
368 * would block in the lock. The same checks have to
369 * be made again after we officially get the buffer.
371 if ((tbp = incore(vp, lbn + i)) != NULL) {
372 if (BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT))
376 for (j = 0; j < tbp->b_npages; j++) {
377 if (tbp->b_pages[j]->valid)
381 if (j != tbp->b_npages)
384 if (tbp->b_bcount != size)
388 tbp = getblk(vp, lbn + i, size, 0, 0);
391 * Stop scanning if the buffer is fuly valid
392 * (marked B_CACHE), or locked (may be doing a
393 * background write), or if the buffer is not
394 * VMIO backed. The clustering code can only deal
395 * with VMIO-backed buffers.
397 if ((tbp->b_flags & (B_CACHE|B_LOCKED)) ||
398 (tbp->b_flags & B_VMIO) == 0) {
404 * The buffer must be completely invalid in order to
405 * take part in the cluster. If it is partially valid
408 for (j = 0;j < tbp->b_npages; j++) {
409 if (tbp->b_pages[j]->valid)
412 if (j != tbp->b_npages) {
418 * Set a read-ahead mark as appropriate
420 if ((fbp && (i == 1)) || (i == (run - 1)))
421 tbp->b_flags |= B_RAM;
424 * Set the buffer up for an async read (XXX should
425 * we do this only if we do not wind up brelse()ing?).
426 * Set the block number if it isn't set, otherwise
427 * if it is make sure it matches the block number we
430 tbp->b_flags |= B_READ | B_ASYNC;
431 if (tbp->b_blkno == tbp->b_lblkno) {
433 } else if (tbp->b_blkno != bn) {
439 * XXX fbp from caller may not be B_ASYNC, but we are going
440 * to biodone() it in cluster_callback() anyway
443 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
444 tbp, b_cluster.cluster_entry);
445 for (j = 0; j < tbp->b_npages; j += 1) {
449 vm_object_pip_add(m->object, 1);
450 if ((bp->b_npages == 0) ||
451 (bp->b_pages[bp->b_npages-1] != m)) {
452 bp->b_pages[bp->b_npages] = m;
455 if ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL)
456 tbp->b_pages[j] = bogus_page;
459 * XXX shouldn't this be += size for both, like in
462 * Don't inherit tbp->b_bufsize as it may be larger due to
463 * a non-page-aligned size. Instead just aggregate using
466 if (tbp->b_bcount != size)
467 printf("warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size);
468 if (tbp->b_bufsize != size)
469 printf("warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size);
470 bp->b_bcount += size;
471 bp->b_bufsize += size;
475 * Fully valid pages in the cluster are already good and do not need
476 * to be re-read from disk. Replace the page with bogus_page
478 for (j = 0; j < bp->b_npages; j++) {
479 if ((bp->b_pages[j]->valid & VM_PAGE_BITS_ALL) ==
481 bp->b_pages[j] = bogus_page;
484 if (bp->b_bufsize > bp->b_kvasize)
485 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)",
486 bp->b_bufsize, bp->b_kvasize);
487 bp->b_kvasize = bp->b_bufsize;
489 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
490 (vm_page_t *)bp->b_pages, bp->b_npages);
495 * Cleanup after a clustered read or write.
496 * This is complicated by the fact that any of the buffers might have
497 * extra memory (if there were no empty buffer headers at allocbuf time)
498 * that we will need to shift around.
501 cluster_callback(struct buf *bp)
503 struct buf *nbp, *tbp;
507 * Must propogate errors to all the components.
509 if (bp->b_flags & B_ERROR)
512 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages);
514 * Move memory from the large cluster buffer into the component
515 * buffers and mark IO as done on these.
517 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head);
519 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry);
521 tbp->b_flags |= B_ERROR;
522 tbp->b_error = error;
524 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
525 tbp->b_flags &= ~(B_ERROR|B_INVAL);
527 * XXX the bdwrite()/bqrelse() issued during
528 * cluster building clears B_RELBUF (see bqrelse()
529 * comment). If direct I/O was specified, we have
530 * to restore it here to allow the buffer and VM
533 if (tbp->b_flags & B_DIRECT)
534 tbp->b_flags |= B_RELBUF;
538 relpbuf(bp, &cluster_pbuf_freecnt);
544 * Implement modified write build for cluster.
546 * write_behind = 0 write behind disabled
547 * write_behind = 1 write behind normal (default)
548 * write_behind = 2 write behind backed-off
552 cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len)
556 switch(write_behind) {
563 r = cluster_wbuild(vp, size, start_lbn, len);
573 * Do clustered write for FFS.
576 * 1. Write is not sequential (write asynchronously)
577 * Write is sequential:
578 * 2. beginning of cluster - begin cluster
579 * 3. middle of a cluster - add to cluster
580 * 4. end of a cluster - asynchronously write cluster
583 cluster_write(struct buf *bp, u_quad_t filesize, int seqcount)
587 int maxclen, cursize;
592 if (vp->v_type == VREG) {
593 async = vp->v_mount->mnt_flag & MNT_ASYNC;
594 lblocksize = vp->v_mount->mnt_stat.f_iosize;
597 lblocksize = bp->b_bufsize;
600 KASSERT(bp->b_offset != NOOFFSET, ("cluster_write: no buffer offset"));
602 /* Initialize vnode to beginning of file. */
604 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
606 if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 ||
607 (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) {
608 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1;
609 if (vp->v_clen != 0) {
611 * Next block is not sequential.
613 * If we are not writing at end of file, the process
614 * seeked to another point in the file since its last
615 * write, or we have reached our maximum cluster size,
616 * then push the previous cluster. Otherwise try
617 * reallocating to make it sequential.
619 * Change to algorithm: only push previous cluster if
620 * it was sequential from the point of view of the
621 * seqcount heuristic, otherwise leave the buffer
622 * intact so we can potentially optimize the I/O
623 * later on in the buf_daemon or update daemon
626 cursize = vp->v_lastw - vp->v_cstart + 1;
627 if (((u_quad_t) bp->b_offset + lblocksize) != filesize ||
628 lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) {
629 if (!async && seqcount > 0) {
630 cluster_wbuild_wb(vp, lblocksize,
631 vp->v_cstart, cursize);
634 struct buf **bpp, **endbp;
635 struct cluster_save *buflist;
637 buflist = cluster_collectbufs(vp, bp);
638 endbp = &buflist->bs_children
639 [buflist->bs_nchildren - 1];
640 if (VOP_REALLOCBLKS(vp, buflist)) {
642 * Failed, push the previous cluster
643 * if *really* writing sequentially
644 * in the logical file (seqcount > 1),
645 * otherwise delay it in the hopes that
646 * the low level disk driver can
647 * optimize the write ordering.
649 for (bpp = buflist->bs_children;
652 free(buflist, M_SEGMENT);
654 cluster_wbuild_wb(vp,
655 lblocksize, vp->v_cstart,
660 * Succeeded, keep building cluster.
662 for (bpp = buflist->bs_children;
665 free(buflist, M_SEGMENT);
667 vp->v_lasta = bp->b_blkno;
673 * Consider beginning a cluster. If at end of file, make
674 * cluster as large as possible, otherwise find size of
677 if ((vp->v_type == VREG) &&
678 ((u_quad_t) bp->b_offset + lblocksize) != filesize &&
679 (bp->b_blkno == bp->b_lblkno) &&
680 (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) ||
681 bp->b_blkno == -1)) {
684 vp->v_lasta = bp->b_blkno;
685 vp->v_cstart = lbn + 1;
689 vp->v_clen = maxclen;
690 if (!async && maxclen == 0) { /* I/O not contiguous */
691 vp->v_cstart = lbn + 1;
693 } else { /* Wait for rest of cluster */
697 } else if (lbn == vp->v_cstart + vp->v_clen) {
699 * At end of cluster, write it out if seqcount tells us we
700 * are operating sequentially, otherwise let the buf or
701 * update daemon handle it.
705 cluster_wbuild_wb(vp, lblocksize, vp->v_cstart, vp->v_clen + 1);
707 vp->v_cstart = lbn + 1;
708 } else if (vm_page_count_severe()) {
710 * We are low on memory, get it going NOW
715 * In the middle of a cluster, so just delay the I/O for now.
720 vp->v_lasta = bp->b_blkno;
725 * This is an awful lot like cluster_rbuild...wish they could be combined.
726 * The last lbn argument is the current block on which I/O is being
727 * performed. Check to see that it doesn't fall in the middle of
728 * the current block (if last_bp == NULL).
731 cluster_wbuild(struct vnode *vp, long size, daddr_t start_lbn, int len)
733 struct buf *bp, *tbp;
735 int totalwritten = 0;
736 int dbsize = btodb(size);
741 * If the buffer is not delayed-write (i.e. dirty), or it
742 * is delayed-write but either locked or inval, it cannot
743 * partake in the clustered write.
745 if (((tbp = gbincore(vp, start_lbn)) == NULL) ||
746 ((tbp->b_flags & (B_LOCKED | B_INVAL | B_DELWRI)) != B_DELWRI) ||
747 BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) {
754 tbp->b_flags &= ~B_DONE;
758 * Extra memory in the buffer, punt on this buffer.
759 * XXX we could handle this in most cases, but we would
760 * have to push the extra memory down to after our max
761 * possible cluster size and then potentially pull it back
762 * up if the cluster was terminated prematurely--too much
765 if (((tbp->b_flags & (B_CLUSTEROK|B_MALLOC)) != B_CLUSTEROK) ||
766 (tbp->b_bcount != tbp->b_bufsize) ||
767 (tbp->b_bcount != size) ||
769 ((bp = getpbuf(&cluster_pbuf_freecnt)) == NULL)) {
770 totalwritten += tbp->b_bufsize;
778 * We got a pbuf to make the cluster in.
781 TAILQ_INIT(&bp->b_cluster.cluster_head);
785 bp->b_blkno = tbp->b_blkno;
786 bp->b_lblkno = tbp->b_lblkno;
787 bp->b_offset = tbp->b_offset;
790 * We are synthesizing a buffer out of vm_page_t's, but
791 * if the block size is not page aligned then the starting
792 * address may not be either. Inherit the b_data offset
793 * from the original buffer.
795 bp->b_data = (char *)((vm_offset_t)bp->b_data |
796 ((vm_offset_t)tbp->b_data & PAGE_MASK));
797 bp->b_flags |= B_CALL | B_CLUSTER |
798 (tbp->b_flags & (B_VMIO | B_NEEDCOMMIT | B_NOWDRAIN));
799 bp->b_iodone = cluster_callback;
802 * From this location in the file, scan forward to see
803 * if there are buffers with adjacent data that need to
804 * be written as well.
806 for (i = 0; i < len; ++i, ++start_lbn) {
807 if (i != 0) { /* If not the first buffer */
810 * If the adjacent data is not even in core it
811 * can't need to be written.
813 if ((tbp = gbincore(vp, start_lbn)) == NULL) {
819 * If it IS in core, but has different
820 * characteristics, or is locked (which
821 * means it could be undergoing a background
822 * I/O or be in a weird state), then don't
825 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
826 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
827 != (B_DELWRI | B_CLUSTEROK |
828 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
829 (tbp->b_flags & B_LOCKED) ||
830 BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) {
836 * Check that the combined cluster
837 * would make sense with regard to pages
838 * and would not be too large
840 if ((tbp->b_bcount != size) ||
841 ((bp->b_blkno + (dbsize * i)) !=
843 ((tbp->b_npages + bp->b_npages) >
844 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
850 * Ok, it's passed all the tests,
851 * so remove it from the free list
852 * and mark it busy. We will use it.
855 tbp->b_flags &= ~B_DONE;
857 } /* end of code for non-first buffers only */
858 /* check for latent dependencies to be handled */
859 if ((LIST_FIRST(&tbp->b_dep)) != NULL &&
861 (*bioops.io_start)(tbp);
863 * If the IO is via the VM then we do some
864 * special VM hackery (yuck). Since the buffer's
865 * block size may not be page-aligned it is possible
866 * for a page to be shared between two buffers. We
867 * have to get rid of the duplication when building
870 if (tbp->b_flags & B_VMIO) {
873 if (i != 0) { /* if not first buffer */
874 for (j = 0; j < tbp->b_npages; j += 1) {
876 if (m->flags & PG_BUSY) {
883 for (j = 0; j < tbp->b_npages; j += 1) {
886 vm_object_pip_add(m->object, 1);
887 if ((bp->b_npages == 0) ||
888 (bp->b_pages[bp->b_npages - 1] != m)) {
889 bp->b_pages[bp->b_npages] = m;
894 bp->b_bcount += size;
895 bp->b_bufsize += size;
899 tbp->b_flags &= ~(B_READ | B_DONE | B_ERROR);
900 tbp->b_flags |= B_ASYNC;
901 reassignbuf(tbp, tbp->b_vp); /* put on clean list */
902 ++tbp->b_vp->v_numoutput;
905 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
906 tbp, b_cluster.cluster_entry);
909 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
910 (vm_page_t *) bp->b_pages, bp->b_npages);
911 if (bp->b_bufsize > bp->b_kvasize)
913 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
914 bp->b_bufsize, bp->b_kvasize);
915 bp->b_kvasize = bp->b_bufsize;
916 totalwritten += bp->b_bufsize;
918 bp->b_dirtyend = bp->b_bufsize;
927 * Collect together all the buffers in a cluster.
928 * Plus add one additional buffer.
930 static struct cluster_save *
931 cluster_collectbufs(struct vnode *vp, struct buf *last_bp)
933 struct cluster_save *buflist;
938 len = vp->v_lastw - vp->v_cstart + 1;
939 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
940 M_SEGMENT, M_WAITOK);
941 buflist->bs_nchildren = 0;
942 buflist->bs_children = (struct buf **) (buflist + 1);
943 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) {
944 (void) bread(vp, lbn, last_bp->b_bcount, &bp);
945 buflist->bs_children[i] = bp;
946 if (bp->b_blkno == bp->b_lblkno)
947 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno,
950 buflist->bs_children[i] = bp = last_bp;
951 if (bp->b_blkno == bp->b_lblkno)
952 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno,
954 buflist->bs_nchildren = i + 1;