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 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)vfs_cluster.c 8.7 (Berkeley) 2/13/94
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_debug_cluster.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/resourcevar.h>
49 #include <sys/vmmeter.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
53 #include <sys/sysctl.h>
55 #if defined(CLUSTERDEBUG)
56 static int rcluster= 0;
57 SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0,
58 "Debug VFS clustering code");
61 static MALLOC_DEFINE(M_SEGMENT, "cluster_save buffer", "cluster_save buffer");
63 static struct cluster_save *
64 cluster_collectbufs(struct vnode *vp, struct buf *last_bp);
66 cluster_rbuild(struct vnode *vp, u_quad_t filesize, daddr_t lbn,
67 daddr_t blkno, long size, int run, struct buf *fbp);
69 static int write_behind = 1;
70 SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0,
71 "Cluster write-behind; 0: disable, 1: enable, 2: backed off");
73 static int read_max = 8;
74 SYSCTL_INT(_vfs, OID_AUTO, read_max, CTLFLAG_RW, &read_max, 0,
75 "Cluster read-ahead max block count");
77 /* Page expended to mark partially backed buffers */
78 extern vm_page_t bogus_page;
81 * Number of physical bufs (pbufs) this subsystem is allowed.
82 * Manipulated by vm_pager.c
84 extern int cluster_pbuf_freecnt;
87 * Read data to a buf, including read-ahead if we find this to be beneficial.
88 * cluster_read replaces bread.
91 cluster_read(vp, filesize, lblkno, size, cred, totread, seqcount, bpp)
101 struct buf *bp, *rbp, *reqbp;
102 daddr_t blkno, origblkno;
103 int maxra, racluster;
110 * Try to limit the amount of read-ahead by a few
111 * ad-hoc parameters. This needs work!!!
113 racluster = vp->v_mount->mnt_iosize_max / size;
115 maxra = min(read_max, maxra);
116 maxra = min(nbuf/8, maxra);
117 if (((u_quad_t)(lblkno + maxra + 1) * size) > filesize)
118 maxra = (filesize / size) - lblkno;
121 * get the requested block
123 *bpp = reqbp = bp = getblk(vp, lblkno, size, 0, 0, 0);
127 * if it is in the cache, then check to see if the reads have been
128 * sequential. If they have, then try some read-ahead, otherwise
129 * back-off on prospective read-aheads.
131 if (bp->b_flags & B_CACHE) {
134 } 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
147 for (i = 1; i < maxra; i++) {
149 * Stop if the buffer does not exist or it
150 * is invalid (about to go away?)
152 rbp = gbincore(vp, lblkno+i);
153 if (rbp == NULL || (rbp->b_flags & B_INVAL))
157 * Set another read-ahead mark so we know
160 if (((i % racluster) == (racluster - 1)) ||
162 rbp->b_flags |= B_RAM;
173 * If it isn't in the cache, then get a chunk from
174 * disk if sequential, otherwise just get the block.
177 off_t firstread = bp->b_offset;
180 KASSERT(bp->b_offset != NOOFFSET,
181 ("cluster_read: no buffer offset"));
186 * Compute the total number of blocks that we should read
189 if (firstread + totread > filesize)
190 totread = filesize - firstread;
191 nblks = howmany(totread, size);
192 if (nblks > racluster)
196 * Now compute the number of contiguous blocks.
199 error = VOP_BMAP(vp, lblkno, NULL,
200 &blkno, &ncontig, NULL);
202 * If this failed to map just do the original block.
204 if (error || blkno == -1)
209 * If we have contiguous data available do a cluster
210 * otherwise just read the requested block.
213 /* Account for our first block. */
214 ncontig = min(ncontig + 1, nblks);
217 bp = cluster_rbuild(vp, filesize, lblkno,
218 blkno, size, nblks, bp);
219 lblkno += (bp->b_bufsize / size);
221 bp->b_flags |= B_RAM;
222 bp->b_iocmd = BIO_READ;
228 * handle the synchronous read so that it is available ASAP.
231 if ((bp->b_flags & B_CLUSTER) == 0) {
232 vfs_busy_pages(bp, 0);
234 bp->b_flags &= ~B_INVAL;
235 bp->b_ioflags &= ~BIO_ERROR;
236 if ((bp->b_flags & B_ASYNC) || bp->b_iodone != NULL)
238 bp->b_iooffset = dbtob(bp->b_blkno);
239 error = VOP_STRATEGY(vp, bp);
240 curproc->p_stats->p_ru.ru_inblock++;
246 * If we have been doing sequential I/O, then do some read-ahead.
248 while (lblkno < (origblkno + maxra)) {
249 error = VOP_BMAP(vp, lblkno, NULL, &blkno, &ncontig, NULL);
257 * We could throttle ncontig here by maxra but we might as
258 * well read the data if it is contiguous. We're throttled
259 * by racluster anyway.
262 ncontig = min(ncontig + 1, racluster);
263 rbp = cluster_rbuild(vp, filesize, lblkno, blkno,
264 size, ncontig, NULL);
265 lblkno += (rbp->b_bufsize / size);
266 if (rbp->b_flags & B_DELWRI) {
271 rbp = getblk(vp, lblkno, size, 0, 0, 0);
273 if (rbp->b_flags & B_DELWRI) {
277 rbp->b_flags |= B_ASYNC | B_RAM;
278 rbp->b_iocmd = BIO_READ;
279 rbp->b_blkno = blkno;
281 if (rbp->b_flags & B_CACHE) {
282 rbp->b_flags &= ~B_ASYNC;
286 if ((rbp->b_flags & B_CLUSTER) == 0) {
287 vfs_busy_pages(rbp, 0);
289 rbp->b_flags &= ~B_INVAL;
290 rbp->b_ioflags &= ~BIO_ERROR;
291 if ((rbp->b_flags & B_ASYNC) || rbp->b_iodone != NULL)
293 rbp->b_iooffset = dbtob(rbp->b_blkno);
294 (void) VOP_STRATEGY(vp, rbp);
295 curproc->p_stats->p_ru.ru_inblock++;
299 return (bufwait(reqbp));
305 * If blocks are contiguous on disk, use this to provide clustered
306 * read ahead. We will read as many blocks as possible sequentially
307 * and then parcel them up into logical blocks in the buffer hash table.
310 cluster_rbuild(vp, filesize, lbn, blkno, size, run, fbp)
319 struct buf *bp, *tbp;
325 KASSERT(size == vp->v_mount->mnt_stat.f_iosize,
326 ("cluster_rbuild: size %ld != filesize %jd\n",
327 size, (intmax_t)vp->v_mount->mnt_stat.f_iosize));
332 while ((u_quad_t) size * (lbn + run) > filesize) {
338 tbp->b_iocmd = BIO_READ;
340 tbp = getblk(vp, lbn, size, 0, 0, 0);
341 if (tbp->b_flags & B_CACHE)
343 tbp->b_flags |= B_ASYNC | B_RAM;
344 tbp->b_iocmd = BIO_READ;
347 tbp->b_blkno = blkno;
348 if( (tbp->b_flags & B_MALLOC) ||
349 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
352 bp = trypbuf(&cluster_pbuf_freecnt);
357 * We are synthesizing a buffer out of vm_page_t's, but
358 * if the block size is not page aligned then the starting
359 * address may not be either. Inherit the b_data offset
360 * from the original buffer.
362 bp->b_data = (char *)((vm_offset_t)bp->b_data |
363 ((vm_offset_t)tbp->b_data & PAGE_MASK));
364 bp->b_flags = B_ASYNC | B_CLUSTER | B_VMIO;
365 bp->b_iocmd = BIO_READ;
366 bp->b_iodone = cluster_callback;
369 bp->b_offset = tbp->b_offset;
370 KASSERT(bp->b_offset != NOOFFSET, ("cluster_rbuild: no buffer offset"));
373 TAILQ_INIT(&bp->b_cluster.cluster_head);
380 for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
382 if ((bp->b_npages * PAGE_SIZE) +
383 round_page(size) > vp->v_mount->mnt_iosize_max) {
387 tbp = getblk(vp, lbn + i, size, 0, 0, GB_LOCK_NOWAIT);
389 /* Don't wait around for locked bufs. */
394 * Stop scanning if the buffer is fully 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.
401 if ((tbp->b_vflags & BV_BKGRDINPROG) ||
402 (tbp->b_flags & B_CACHE) ||
403 (tbp->b_flags & B_VMIO) == 0) {
411 * The buffer must be completely invalid in order to
412 * take part in the cluster. If it is partially valid
415 VM_OBJECT_LOCK(tbp->b_object);
416 for (j = 0;j < tbp->b_npages; j++) {
417 VM_OBJECT_LOCK_ASSERT(tbp->b_pages[j]->object,
419 if (tbp->b_pages[j]->valid)
422 VM_OBJECT_UNLOCK(tbp->b_object);
423 if (j != tbp->b_npages) {
429 * Set a read-ahead mark as appropriate
431 if ((fbp && (i == 1)) || (i == (run - 1)))
432 tbp->b_flags |= B_RAM;
435 * Set the buffer up for an async read (XXX should
436 * we do this only if we do not wind up brelse()ing?).
437 * Set the block number if it isn't set, otherwise
438 * if it is make sure it matches the block number we
441 tbp->b_flags |= B_ASYNC;
442 tbp->b_iocmd = BIO_READ;
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 VM_OBJECT_LOCK(tbp->b_object);
458 vm_page_lock_queues();
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;
472 vm_page_unlock_queues();
473 VM_OBJECT_UNLOCK(tbp->b_object);
475 * XXX shouldn't this be += size for both, like in
478 * Don't inherit tbp->b_bufsize as it may be larger due to
479 * a non-page-aligned size. Instead just aggregate using
482 if (tbp->b_bcount != size)
483 printf("warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size);
484 if (tbp->b_bufsize != size)
485 printf("warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size);
486 bp->b_bcount += size;
487 bp->b_bufsize += size;
491 * Fully valid pages in the cluster are already good and do not need
492 * to be re-read from disk. Replace the page with bogus_page
494 VM_OBJECT_LOCK(bp->b_object);
495 for (j = 0; j < bp->b_npages; j++) {
496 VM_OBJECT_LOCK_ASSERT(bp->b_pages[j]->object, MA_OWNED);
497 if ((bp->b_pages[j]->valid & VM_PAGE_BITS_ALL) ==
499 bp->b_pages[j] = bogus_page;
502 VM_OBJECT_UNLOCK(bp->b_object);
503 if (bp->b_bufsize > bp->b_kvasize)
504 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
505 bp->b_bufsize, bp->b_kvasize);
506 bp->b_kvasize = bp->b_bufsize;
508 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
509 (vm_page_t *)bp->b_pages, bp->b_npages);
514 * Cleanup after a clustered read or write.
515 * This is complicated by the fact that any of the buffers might have
516 * extra memory (if there were no empty buffer headers at allocbuf time)
517 * that we will need to shift around.
523 struct buf *nbp, *tbp;
529 * Must propogate errors to all the components.
531 if (bp->b_ioflags & BIO_ERROR)
534 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages);
536 * Move memory from the large cluster buffer into the component
537 * buffers and mark IO as done on these.
539 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head);
541 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry);
543 tbp->b_ioflags |= BIO_ERROR;
544 tbp->b_error = error;
546 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
547 tbp->b_flags &= ~B_INVAL;
548 tbp->b_ioflags &= ~BIO_ERROR;
550 * XXX the bdwrite()/bqrelse() issued during
551 * cluster building clears B_RELBUF (see bqrelse()
552 * comment). If direct I/O was specified, we have
553 * to restore it here to allow the buffer and VM
556 if (tbp->b_flags & B_DIRECT)
557 tbp->b_flags |= B_RELBUF;
561 relpbuf(bp, &cluster_pbuf_freecnt);
567 * Implement modified write build for cluster.
569 * write_behind = 0 write behind disabled
570 * write_behind = 1 write behind normal (default)
571 * write_behind = 2 write behind backed-off
575 cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len)
579 switch(write_behind) {
586 r = cluster_wbuild(vp, size, start_lbn, len);
596 * Do clustered write for FFS.
599 * 1. Write is not sequential (write asynchronously)
600 * Write is sequential:
601 * 2. beginning of cluster - begin cluster
602 * 3. middle of a cluster - add to cluster
603 * 4. end of a cluster - asynchronously write cluster
606 cluster_write(bp, filesize, seqcount)
613 int maxclen, cursize;
618 if (vp->v_type == VREG) {
619 async = vp->v_mount->mnt_flag & MNT_ASYNC;
620 lblocksize = vp->v_mount->mnt_stat.f_iosize;
623 lblocksize = bp->b_bufsize;
626 KASSERT(bp->b_offset != NOOFFSET, ("cluster_write: no buffer offset"));
628 /* Initialize vnode to beginning of file. */
630 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
632 if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 ||
633 (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) {
634 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1;
635 if (vp->v_clen != 0) {
637 * Next block is not sequential.
639 * If we are not writing at end of file, the process
640 * seeked to another point in the file since its last
641 * write, or we have reached our maximum cluster size,
642 * then push the previous cluster. Otherwise try
643 * reallocating to make it sequential.
645 * Change to algorithm: only push previous cluster if
646 * it was sequential from the point of view of the
647 * seqcount heuristic, otherwise leave the buffer
648 * intact so we can potentially optimize the I/O
649 * later on in the buf_daemon or update daemon
652 cursize = vp->v_lastw - vp->v_cstart + 1;
653 if (((u_quad_t) bp->b_offset + lblocksize) != filesize ||
654 lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) {
655 if (!async && seqcount > 0) {
656 cluster_wbuild_wb(vp, lblocksize,
657 vp->v_cstart, cursize);
660 struct buf **bpp, **endbp;
661 struct cluster_save *buflist;
663 buflist = cluster_collectbufs(vp, bp);
664 endbp = &buflist->bs_children
665 [buflist->bs_nchildren - 1];
666 if (VOP_REALLOCBLKS(vp, buflist)) {
668 * Failed, push the previous cluster
669 * if *really* writing sequentially
670 * in the logical file (seqcount > 1),
671 * otherwise delay it in the hopes that
672 * the low level disk driver can
673 * optimize the write ordering.
675 for (bpp = buflist->bs_children;
678 free(buflist, M_SEGMENT);
680 cluster_wbuild_wb(vp,
681 lblocksize, vp->v_cstart,
686 * Succeeded, keep building cluster.
688 for (bpp = buflist->bs_children;
691 free(buflist, M_SEGMENT);
693 vp->v_lasta = bp->b_blkno;
699 * Consider beginning a cluster. If at end of file, make
700 * cluster as large as possible, otherwise find size of
703 if ((vp->v_type == VREG) &&
704 ((u_quad_t) bp->b_offset + lblocksize) != filesize &&
705 (bp->b_blkno == bp->b_lblkno) &&
706 (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) ||
707 bp->b_blkno == -1)) {
710 vp->v_lasta = bp->b_blkno;
711 vp->v_cstart = lbn + 1;
715 vp->v_clen = maxclen;
716 if (!async && maxclen == 0) { /* I/O not contiguous */
717 vp->v_cstart = lbn + 1;
719 } else { /* Wait for rest of cluster */
723 } else if (lbn == vp->v_cstart + vp->v_clen) {
725 * At end of cluster, write it out if seqcount tells us we
726 * are operating sequentially, otherwise let the buf or
727 * update daemon handle it.
731 cluster_wbuild_wb(vp, lblocksize, vp->v_cstart, vp->v_clen + 1);
733 vp->v_cstart = lbn + 1;
734 } else if (vm_page_count_severe()) {
736 * We are low on memory, get it going NOW
741 * In the middle of a cluster, so just delay the I/O for now.
746 vp->v_lasta = bp->b_blkno;
751 * This is an awful lot like cluster_rbuild...wish they could be combined.
752 * The last lbn argument is the current block on which I/O is being
753 * performed. Check to see that it doesn't fall in the middle of
754 * the current block (if last_bp == NULL).
757 cluster_wbuild(vp, size, start_lbn, len)
763 struct buf *bp, *tbp;
765 int totalwritten = 0;
766 int dbsize = btodb(size);
773 * If the buffer is not delayed-write (i.e. dirty), or it
774 * is delayed-write but either locked or inval, it cannot
775 * partake in the clustered write.
778 if ((tbp = gbincore(vp, start_lbn)) == NULL ||
779 (tbp->b_vflags & BV_BKGRDINPROG)) {
787 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, VI_MTX(vp))) {
793 if ((tbp->b_flags & (B_INVAL | B_DELWRI)) != B_DELWRI) {
801 tbp->b_flags &= ~B_DONE;
805 * Extra memory in the buffer, punt on this buffer.
806 * XXX we could handle this in most cases, but we would
807 * have to push the extra memory down to after our max
808 * possible cluster size and then potentially pull it back
809 * up if the cluster was terminated prematurely--too much
812 if (((tbp->b_flags & (B_CLUSTEROK | B_MALLOC | B_VMIO)) !=
813 (B_CLUSTEROK | B_VMIO)) ||
814 (tbp->b_bcount != tbp->b_bufsize) ||
815 (tbp->b_bcount != size) ||
817 ((bp = getpbuf(&cluster_pbuf_freecnt)) == NULL)) {
818 totalwritten += tbp->b_bufsize;
826 * We got a pbuf to make the cluster in.
829 TAILQ_INIT(&bp->b_cluster.cluster_head);
831 bp->b_magic = tbp->b_magic;
832 bp->b_op = tbp->b_op;
835 if (tbp->b_wcred != NOCRED)
836 bp->b_wcred = crhold(tbp->b_wcred);
838 bp->b_blkno = tbp->b_blkno;
839 bp->b_lblkno = tbp->b_lblkno;
840 bp->b_offset = tbp->b_offset;
843 * We are synthesizing a buffer out of vm_page_t's, but
844 * if the block size is not page aligned then the starting
845 * address may not be either. Inherit the b_data offset
846 * from the original buffer.
848 bp->b_data = (char *)((vm_offset_t)bp->b_data |
849 ((vm_offset_t)tbp->b_data & PAGE_MASK));
850 bp->b_flags |= B_CLUSTER |
851 (tbp->b_flags & (B_VMIO | B_NEEDCOMMIT));
852 bp->b_iodone = cluster_callback;
855 * From this location in the file, scan forward to see
856 * if there are buffers with adjacent data that need to
857 * be written as well.
859 for (i = 0; i < len; ++i, ++start_lbn) {
860 if (i != 0) { /* If not the first buffer */
863 * If the adjacent data is not even in core it
864 * can't need to be written.
867 if ((tbp = gbincore(vp, start_lbn)) == NULL ||
868 (tbp->b_vflags & BV_BKGRDINPROG)) {
875 * If it IS in core, but has different
876 * characteristics, or is locked (which
877 * means it could be undergoing a background
878 * I/O or be in a weird state), then don't
882 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
888 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
889 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
890 != (B_DELWRI | B_CLUSTEROK |
891 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
892 tbp->b_wcred != bp->b_wcred) {
899 * Check that the combined cluster
900 * would make sense with regard to pages
901 * and would not be too large
903 if ((tbp->b_bcount != size) ||
904 ((bp->b_blkno + (dbsize * i)) !=
906 ((tbp->b_npages + bp->b_npages) >
907 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
913 * Ok, it's passed all the tests,
914 * so remove it from the free list
915 * and mark it busy. We will use it.
918 tbp->b_flags &= ~B_DONE;
920 } /* end of code for non-first buffers only */
921 /* check for latent dependencies to be handled */
922 if ((LIST_FIRST(&tbp->b_dep)) != NULL) {
923 tbp->b_iocmd = BIO_WRITE;
927 * If the IO is via the VM then we do some
928 * special VM hackery (yuck). Since the buffer's
929 * block size may not be page-aligned it is possible
930 * for a page to be shared between two buffers. We
931 * have to get rid of the duplication when building
934 if (tbp->b_flags & B_VMIO) {
937 if (i != 0) { /* if not first buffer */
938 for (j = 0; j < tbp->b_npages; j += 1) {
940 if (m->flags & PG_BUSY) {
946 if (tbp->b_object != NULL)
947 VM_OBJECT_LOCK(tbp->b_object);
948 vm_page_lock_queues();
949 for (j = 0; j < tbp->b_npages; j += 1) {
952 vm_object_pip_add(m->object, 1);
953 if ((bp->b_npages == 0) ||
954 (bp->b_pages[bp->b_npages - 1] != m)) {
955 bp->b_pages[bp->b_npages] = m;
959 vm_page_unlock_queues();
960 if (tbp->b_object != NULL)
961 VM_OBJECT_UNLOCK(tbp->b_object);
963 bp->b_bcount += size;
964 bp->b_bufsize += size;
968 tbp->b_flags &= ~B_DONE;
969 tbp->b_ioflags &= ~BIO_ERROR;
970 tbp->b_flags |= B_ASYNC;
971 tbp->b_iocmd = BIO_WRITE;
972 reassignbuf(tbp); /* put on clean list */
974 ++tbp->b_vp->v_numoutput;
975 VI_UNLOCK(tbp->b_vp);
978 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
979 tbp, b_cluster.cluster_entry);
982 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
983 (vm_page_t *) bp->b_pages, bp->b_npages);
984 if (bp->b_bufsize > bp->b_kvasize)
986 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
987 bp->b_bufsize, bp->b_kvasize);
988 bp->b_kvasize = bp->b_bufsize;
989 totalwritten += bp->b_bufsize;
991 bp->b_dirtyend = bp->b_bufsize;
1000 * Collect together all the buffers in a cluster.
1001 * Plus add one additional buffer.
1003 static struct cluster_save *
1004 cluster_collectbufs(vp, last_bp)
1006 struct buf *last_bp;
1008 struct cluster_save *buflist;
1013 len = vp->v_lastw - vp->v_cstart + 1;
1014 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
1015 M_SEGMENT, M_WAITOK);
1016 buflist->bs_nchildren = 0;
1017 buflist->bs_children = (struct buf **) (buflist + 1);
1018 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) {
1019 (void) bread(vp, lbn, last_bp->b_bcount, NOCRED, &bp);
1020 buflist->bs_children[i] = bp;
1021 if (bp->b_blkno == bp->b_lblkno)
1022 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno,
1025 buflist->bs_children[i] = bp = last_bp;
1026 if (bp->b_blkno == bp->b_lblkno)
1027 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno,
1029 buflist->bs_nchildren = i + 1;