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.3 2003/06/19 01:55:06 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>
58 #if defined(CLUSTERDEBUG)
59 #include <sys/sysctl.h>
60 static int rcluster= 0;
61 SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0, "");
64 static MALLOC_DEFINE(M_SEGMENT, "cluster_save buffer", "cluster_save buffer");
66 static struct cluster_save *
67 cluster_collectbufs __P((struct vnode *vp, struct buf *last_bp));
69 cluster_rbuild __P((struct vnode *vp, u_quad_t filesize, daddr_t lbn,
70 daddr_t blkno, long size, int run, struct buf *fbp));
72 static int write_behind = 1;
73 SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0, "");
75 extern vm_page_t bogus_page;
77 extern int cluster_pbuf_freecnt;
80 * Maximum number of blocks for read-ahead.
85 * This replaces bread.
88 cluster_read(vp, filesize, lblkno, size, cred, 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);
256 curproc->p_stats->p_ru.ru_inblock++;
260 * and if we have read-aheads, do them too
264 rbp->b_flags &= ~(B_ASYNC | B_READ);
266 } else if (rbp->b_flags & B_CACHE) {
267 rbp->b_flags &= ~(B_ASYNC | B_READ);
270 #if defined(CLUSTERDEBUG)
273 printf("A+(%ld,%ld,%ld,%d) ",
274 (long)rbp->b_lblkno, rbp->b_bcount,
275 (long)(rbp->b_lblkno - origblkno),
278 printf("A(%ld,%ld,%ld,%d) ",
279 (long)rbp->b_lblkno, rbp->b_bcount,
280 (long)(rbp->b_lblkno - origblkno),
285 if ((rbp->b_flags & B_CLUSTER) == 0) {
286 vfs_busy_pages(rbp, 0);
288 rbp->b_flags &= ~(B_ERROR|B_INVAL);
289 if (rbp->b_flags & (B_ASYNC|B_CALL))
291 (void) VOP_STRATEGY(vp, rbp);
292 curproc->p_stats->p_ru.ru_inblock++;
296 return (biowait(reqbp));
302 * If blocks are contiguous on disk, use this to provide clustered
303 * read ahead. We will read as many blocks as possible sequentially
304 * and then parcel them up into logical blocks in the buffer hash table.
307 cluster_rbuild(vp, filesize, lbn, blkno, size, run, fbp)
316 struct buf *bp, *tbp;
320 KASSERT(size == vp->v_mount->mnt_stat.f_iosize,
321 ("cluster_rbuild: size %ld != filesize %ld\n",
322 size, vp->v_mount->mnt_stat.f_iosize));
327 while ((u_quad_t) size * (lbn + run) > filesize) {
333 tbp->b_flags |= B_READ;
335 tbp = getblk(vp, lbn, size, 0, 0);
336 if (tbp->b_flags & B_CACHE)
338 tbp->b_flags |= B_ASYNC | B_READ | B_RAM;
341 tbp->b_blkno = blkno;
342 if( (tbp->b_flags & B_MALLOC) ||
343 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
346 bp = trypbuf(&cluster_pbuf_freecnt);
351 * We are synthesizing a buffer out of vm_page_t's, but
352 * if the block size is not page aligned then the starting
353 * address may not be either. Inherit the b_data offset
354 * from the original buffer.
356 bp->b_data = (char *)((vm_offset_t)bp->b_data |
357 ((vm_offset_t)tbp->b_data & PAGE_MASK));
358 bp->b_flags = B_ASYNC | B_READ | B_CALL | B_CLUSTER | B_VMIO;
359 bp->b_iodone = cluster_callback;
362 bp->b_offset = tbp->b_offset;
363 KASSERT(bp->b_offset != NOOFFSET, ("cluster_rbuild: no buffer offset"));
366 TAILQ_INIT(&bp->b_cluster.cluster_head);
373 for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
375 if ((bp->b_npages * PAGE_SIZE) +
376 round_page(size) > vp->v_mount->mnt_iosize_max) {
381 * Shortcut some checks and try to avoid buffers that
382 * would block in the lock. The same checks have to
383 * be made again after we officially get the buffer.
385 if ((tbp = incore(vp, lbn + i)) != NULL) {
386 if (BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT))
390 for (j = 0; j < tbp->b_npages; j++) {
391 if (tbp->b_pages[j]->valid)
395 if (j != tbp->b_npages)
398 if (tbp->b_bcount != size)
402 tbp = getblk(vp, lbn + i, size, 0, 0);
405 * Stop scanning if the buffer is fuly valid
406 * (marked B_CACHE), or locked (may be doing a
407 * background write), or if the buffer is not
408 * VMIO backed. The clustering code can only deal
409 * with VMIO-backed buffers.
411 if ((tbp->b_flags & (B_CACHE|B_LOCKED)) ||
412 (tbp->b_flags & B_VMIO) == 0) {
418 * The buffer must be completely invalid in order to
419 * take part in the cluster. If it is partially valid
422 for (j = 0;j < tbp->b_npages; j++) {
423 if (tbp->b_pages[j]->valid)
426 if (j != tbp->b_npages) {
432 * Set a read-ahead mark as appropriate
434 if ((fbp && (i == 1)) || (i == (run - 1)))
435 tbp->b_flags |= B_RAM;
438 * Set the buffer up for an async read (XXX should
439 * we do this only if we do not wind up brelse()ing?).
440 * Set the block number if it isn't set, otherwise
441 * if it is make sure it matches the block number we
444 tbp->b_flags |= B_READ | B_ASYNC;
445 if (tbp->b_blkno == tbp->b_lblkno) {
447 } else if (tbp->b_blkno != bn) {
453 * XXX fbp from caller may not be B_ASYNC, but we are going
454 * to biodone() it in cluster_callback() anyway
457 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
458 tbp, b_cluster.cluster_entry);
459 for (j = 0; j < tbp->b_npages; j += 1) {
463 vm_object_pip_add(m->object, 1);
464 if ((bp->b_npages == 0) ||
465 (bp->b_pages[bp->b_npages-1] != m)) {
466 bp->b_pages[bp->b_npages] = m;
469 if ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL)
470 tbp->b_pages[j] = bogus_page;
473 * XXX shouldn't this be += size for both, like in
476 * Don't inherit tbp->b_bufsize as it may be larger due to
477 * a non-page-aligned size. Instead just aggregate using
480 if (tbp->b_bcount != size)
481 printf("warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size);
482 if (tbp->b_bufsize != size)
483 printf("warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size);
484 bp->b_bcount += size;
485 bp->b_bufsize += size;
489 * Fully valid pages in the cluster are already good and do not need
490 * to be re-read from disk. Replace the page with bogus_page
492 for (j = 0; j < bp->b_npages; j++) {
493 if ((bp->b_pages[j]->valid & VM_PAGE_BITS_ALL) ==
495 bp->b_pages[j] = bogus_page;
498 if (bp->b_bufsize > bp->b_kvasize)
499 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
500 bp->b_bufsize, bp->b_kvasize);
501 bp->b_kvasize = bp->b_bufsize;
503 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
504 (vm_page_t *)bp->b_pages, bp->b_npages);
509 * Cleanup after a clustered read or write.
510 * This is complicated by the fact that any of the buffers might have
511 * extra memory (if there were no empty buffer headers at allocbuf time)
512 * that we will need to shift around.
518 struct buf *nbp, *tbp;
522 * Must propogate errors to all the components.
524 if (bp->b_flags & B_ERROR)
527 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages);
529 * Move memory from the large cluster buffer into the component
530 * buffers and mark IO as done on these.
532 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head);
534 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry);
536 tbp->b_flags |= B_ERROR;
537 tbp->b_error = error;
539 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
540 tbp->b_flags &= ~(B_ERROR|B_INVAL);
542 * XXX the bdwrite()/bqrelse() issued during
543 * cluster building clears B_RELBUF (see bqrelse()
544 * comment). If direct I/O was specified, we have
545 * to restore it here to allow the buffer and VM
548 if (tbp->b_flags & B_DIRECT)
549 tbp->b_flags |= B_RELBUF;
553 relpbuf(bp, &cluster_pbuf_freecnt);
559 * Implement modified write build for cluster.
561 * write_behind = 0 write behind disabled
562 * write_behind = 1 write behind normal (default)
563 * write_behind = 2 write behind backed-off
567 cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len)
571 switch(write_behind) {
578 r = cluster_wbuild(vp, size, start_lbn, len);
588 * Do clustered write for FFS.
591 * 1. Write is not sequential (write asynchronously)
592 * Write is sequential:
593 * 2. beginning of cluster - begin cluster
594 * 3. middle of a cluster - add to cluster
595 * 4. end of a cluster - asynchronously write cluster
598 cluster_write(bp, filesize, seqcount)
605 int maxclen, cursize;
610 if (vp->v_type == VREG) {
611 async = vp->v_mount->mnt_flag & MNT_ASYNC;
612 lblocksize = vp->v_mount->mnt_stat.f_iosize;
615 lblocksize = bp->b_bufsize;
618 KASSERT(bp->b_offset != NOOFFSET, ("cluster_write: no buffer offset"));
620 /* Initialize vnode to beginning of file. */
622 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
624 if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 ||
625 (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) {
626 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1;
627 if (vp->v_clen != 0) {
629 * Next block is not sequential.
631 * If we are not writing at end of file, the process
632 * seeked to another point in the file since its last
633 * write, or we have reached our maximum cluster size,
634 * then push the previous cluster. Otherwise try
635 * reallocating to make it sequential.
637 * Change to algorithm: only push previous cluster if
638 * it was sequential from the point of view of the
639 * seqcount heuristic, otherwise leave the buffer
640 * intact so we can potentially optimize the I/O
641 * later on in the buf_daemon or update daemon
644 cursize = vp->v_lastw - vp->v_cstart + 1;
645 if (((u_quad_t) bp->b_offset + lblocksize) != filesize ||
646 lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) {
647 if (!async && seqcount > 0) {
648 cluster_wbuild_wb(vp, lblocksize,
649 vp->v_cstart, cursize);
652 struct buf **bpp, **endbp;
653 struct cluster_save *buflist;
655 buflist = cluster_collectbufs(vp, bp);
656 endbp = &buflist->bs_children
657 [buflist->bs_nchildren - 1];
658 if (VOP_REALLOCBLKS(vp, buflist)) {
660 * Failed, push the previous cluster
661 * if *really* writing sequentially
662 * in the logical file (seqcount > 1),
663 * otherwise delay it in the hopes that
664 * the low level disk driver can
665 * optimize the write ordering.
667 for (bpp = buflist->bs_children;
670 free(buflist, M_SEGMENT);
672 cluster_wbuild_wb(vp,
673 lblocksize, vp->v_cstart,
678 * Succeeded, keep building cluster.
680 for (bpp = buflist->bs_children;
683 free(buflist, M_SEGMENT);
685 vp->v_lasta = bp->b_blkno;
691 * Consider beginning a cluster. If at end of file, make
692 * cluster as large as possible, otherwise find size of
695 if ((vp->v_type == VREG) &&
696 ((u_quad_t) bp->b_offset + lblocksize) != filesize &&
697 (bp->b_blkno == bp->b_lblkno) &&
698 (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) ||
699 bp->b_blkno == -1)) {
702 vp->v_lasta = bp->b_blkno;
703 vp->v_cstart = lbn + 1;
707 vp->v_clen = maxclen;
708 if (!async && maxclen == 0) { /* I/O not contiguous */
709 vp->v_cstart = lbn + 1;
711 } else { /* Wait for rest of cluster */
715 } else if (lbn == vp->v_cstart + vp->v_clen) {
717 * At end of cluster, write it out if seqcount tells us we
718 * are operating sequentially, otherwise let the buf or
719 * update daemon handle it.
723 cluster_wbuild_wb(vp, lblocksize, vp->v_cstart, vp->v_clen + 1);
725 vp->v_cstart = lbn + 1;
726 } else if (vm_page_count_severe()) {
728 * We are low on memory, get it going NOW
733 * In the middle of a cluster, so just delay the I/O for now.
738 vp->v_lasta = bp->b_blkno;
743 * This is an awful lot like cluster_rbuild...wish they could be combined.
744 * The last lbn argument is the current block on which I/O is being
745 * performed. Check to see that it doesn't fall in the middle of
746 * the current block (if last_bp == NULL).
749 cluster_wbuild(vp, size, start_lbn, len)
755 struct buf *bp, *tbp;
757 int totalwritten = 0;
758 int dbsize = btodb(size);
763 * If the buffer is not delayed-write (i.e. dirty), or it
764 * is delayed-write but either locked or inval, it cannot
765 * partake in the clustered write.
767 if (((tbp = gbincore(vp, start_lbn)) == NULL) ||
768 ((tbp->b_flags & (B_LOCKED | B_INVAL | B_DELWRI)) != B_DELWRI) ||
769 BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) {
776 tbp->b_flags &= ~B_DONE;
780 * Extra memory in the buffer, punt on this buffer.
781 * XXX we could handle this in most cases, but we would
782 * have to push the extra memory down to after our max
783 * possible cluster size and then potentially pull it back
784 * up if the cluster was terminated prematurely--too much
787 if (((tbp->b_flags & (B_CLUSTEROK|B_MALLOC)) != B_CLUSTEROK) ||
788 (tbp->b_bcount != tbp->b_bufsize) ||
789 (tbp->b_bcount != size) ||
791 ((bp = getpbuf(&cluster_pbuf_freecnt)) == NULL)) {
792 totalwritten += tbp->b_bufsize;
800 * We got a pbuf to make the cluster in.
803 TAILQ_INIT(&bp->b_cluster.cluster_head);
807 if (tbp->b_wcred != NOCRED) {
808 bp->b_wcred = tbp->b_wcred;
812 bp->b_blkno = tbp->b_blkno;
813 bp->b_lblkno = tbp->b_lblkno;
814 bp->b_offset = tbp->b_offset;
817 * We are synthesizing a buffer out of vm_page_t's, but
818 * if the block size is not page aligned then the starting
819 * address may not be either. Inherit the b_data offset
820 * from the original buffer.
822 bp->b_data = (char *)((vm_offset_t)bp->b_data |
823 ((vm_offset_t)tbp->b_data & PAGE_MASK));
824 bp->b_flags |= B_CALL | B_CLUSTER |
825 (tbp->b_flags & (B_VMIO | B_NEEDCOMMIT | B_NOWDRAIN));
826 bp->b_iodone = cluster_callback;
829 * From this location in the file, scan forward to see
830 * if there are buffers with adjacent data that need to
831 * be written as well.
833 for (i = 0; i < len; ++i, ++start_lbn) {
834 if (i != 0) { /* If not the first buffer */
837 * If the adjacent data is not even in core it
838 * can't need to be written.
840 if ((tbp = gbincore(vp, start_lbn)) == NULL) {
846 * If it IS in core, but has different
847 * characteristics, or is locked (which
848 * means it could be undergoing a background
849 * I/O or be in a weird state), then don't
852 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
853 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
854 != (B_DELWRI | B_CLUSTEROK |
855 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
856 (tbp->b_flags & B_LOCKED) ||
857 tbp->b_wcred != bp->b_wcred ||
858 BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) {
864 * Check that the combined cluster
865 * would make sense with regard to pages
866 * and would not be too large
868 if ((tbp->b_bcount != size) ||
869 ((bp->b_blkno + (dbsize * i)) !=
871 ((tbp->b_npages + bp->b_npages) >
872 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
878 * Ok, it's passed all the tests,
879 * so remove it from the free list
880 * and mark it busy. We will use it.
883 tbp->b_flags &= ~B_DONE;
885 } /* end of code for non-first buffers only */
886 /* check for latent dependencies to be handled */
887 if ((LIST_FIRST(&tbp->b_dep)) != NULL &&
889 (*bioops.io_start)(tbp);
891 * If the IO is via the VM then we do some
892 * special VM hackery (yuck). Since the buffer's
893 * block size may not be page-aligned it is possible
894 * for a page to be shared between two buffers. We
895 * have to get rid of the duplication when building
898 if (tbp->b_flags & B_VMIO) {
901 if (i != 0) { /* if not first buffer */
902 for (j = 0; j < tbp->b_npages; j += 1) {
904 if (m->flags & PG_BUSY) {
911 for (j = 0; j < tbp->b_npages; j += 1) {
914 vm_object_pip_add(m->object, 1);
915 if ((bp->b_npages == 0) ||
916 (bp->b_pages[bp->b_npages - 1] != m)) {
917 bp->b_pages[bp->b_npages] = m;
922 bp->b_bcount += size;
923 bp->b_bufsize += size;
927 tbp->b_flags &= ~(B_READ | B_DONE | B_ERROR);
928 tbp->b_flags |= B_ASYNC;
929 reassignbuf(tbp, tbp->b_vp); /* put on clean list */
930 ++tbp->b_vp->v_numoutput;
933 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
934 tbp, b_cluster.cluster_entry);
937 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
938 (vm_page_t *) bp->b_pages, bp->b_npages);
939 if (bp->b_bufsize > bp->b_kvasize)
941 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
942 bp->b_bufsize, bp->b_kvasize);
943 bp->b_kvasize = bp->b_bufsize;
944 totalwritten += bp->b_bufsize;
946 bp->b_dirtyend = bp->b_bufsize;
955 * Collect together all the buffers in a cluster.
956 * Plus add one additional buffer.
958 static struct cluster_save *
959 cluster_collectbufs(vp, last_bp)
963 struct cluster_save *buflist;
968 len = vp->v_lastw - vp->v_cstart + 1;
969 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
970 M_SEGMENT, M_WAITOK);
971 buflist->bs_nchildren = 0;
972 buflist->bs_children = (struct buf **) (buflist + 1);
973 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) {
974 (void) bread(vp, lbn, last_bp->b_bcount, NOCRED, &bp);
975 buflist->bs_children[i] = bp;
976 if (bp->b_blkno == bp->b_lblkno)
977 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno,
980 buflist->bs_children[i] = bp = last_bp;
981 if (bp->b_blkno == bp->b_lblkno)
982 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno,
984 buflist->bs_nchildren = i + 1;