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, "cl_savebuf", "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);
68 static void cluster_callback(struct buf *);
70 static int write_behind = 1;
71 SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0,
72 "Cluster write-behind; 0: disable, 1: enable, 2: backed off");
74 static int read_max = 64;
75 SYSCTL_INT(_vfs, OID_AUTO, read_max, CTLFLAG_RW, &read_max, 0,
76 "Cluster read-ahead max block count");
78 static int read_min = 1;
79 SYSCTL_INT(_vfs, OID_AUTO, read_min, CTLFLAG_RW, &read_min, 0,
80 "Cluster read min block count");
82 /* Page expended to mark partially backed buffers */
83 extern vm_page_t bogus_page;
86 * Read data to a buf, including read-ahead if we find this to be beneficial.
87 * cluster_read replaces bread.
90 cluster_read(vp, filesize, lblkno, size, cred, totread, seqcount, bpp)
100 struct buf *bp, *rbp, *reqbp;
102 daddr_t blkno, origblkno;
103 int maxra, racluster;
111 * Try to limit the amount of read-ahead by a few
112 * ad-hoc parameters. This needs work!!!
114 racluster = vp->v_mount->mnt_iosize_max / size;
116 maxra = min(read_max, maxra);
117 maxra = min(nbuf/8, maxra);
118 if (((u_quad_t)(lblkno + maxra + 1) * size) > filesize)
119 maxra = (filesize / size) - lblkno;
122 * get the requested block
124 *bpp = reqbp = bp = getblk(vp, lblkno, size, 0, 0, 0);
128 * if it is in the cache, then check to see if the reads have been
129 * sequential. If they have, then try some read-ahead, otherwise
130 * back-off on prospective read-aheads.
132 if (bp->b_flags & B_CACHE) {
135 } else if ((bp->b_flags & B_RAM) == 0) {
138 bp->b_flags &= ~B_RAM;
140 for (i = 1; i < maxra; i++) {
142 * Stop if the buffer does not exist or it
143 * is invalid (about to go away?)
145 rbp = gbincore(&vp->v_bufobj, lblkno+i);
146 if (rbp == NULL || (rbp->b_flags & B_INVAL))
150 * Set another read-ahead mark so we know
151 * to check again. (If we can lock the
152 * buffer without waiting)
154 if ((((i % racluster) == (racluster - 1)) ||
156 && (0 == BUF_LOCK(rbp,
157 LK_EXCLUSIVE | LK_NOWAIT, NULL))) {
158 rbp->b_flags |= B_RAM;
170 * If it isn't in the cache, then get a chunk from
171 * disk if sequential, otherwise just get the block.
174 off_t firstread = bp->b_offset;
178 KASSERT(bp->b_offset != NOOFFSET,
179 ("cluster_read: no buffer offset"));
184 * Adjust totread if needed
186 minread = read_min * size;
187 if (minread > totread)
191 * Compute the total number of blocks that we should read
194 if (firstread + totread > filesize)
195 totread = filesize - firstread;
196 nblks = howmany(totread, size);
197 if (nblks > racluster)
201 * Now compute the number of contiguous blocks.
204 error = VOP_BMAP(vp, lblkno, NULL,
205 &blkno, &ncontig, NULL);
207 * If this failed to map just do the original block.
209 if (error || blkno == -1)
214 * If we have contiguous data available do a cluster
215 * otherwise just read the requested block.
218 /* Account for our first block. */
219 ncontig = min(ncontig + 1, nblks);
222 bp = cluster_rbuild(vp, filesize, lblkno,
223 blkno, size, nblks, bp);
224 lblkno += (bp->b_bufsize / size);
226 bp->b_flags |= B_RAM;
227 bp->b_iocmd = BIO_READ;
233 * handle the synchronous read so that it is available ASAP.
236 if ((bp->b_flags & B_CLUSTER) == 0) {
237 vfs_busy_pages(bp, 0);
239 bp->b_flags &= ~B_INVAL;
240 bp->b_ioflags &= ~BIO_ERROR;
241 if ((bp->b_flags & B_ASYNC) || bp->b_iodone != NULL)
243 bp->b_iooffset = dbtob(bp->b_blkno);
245 curthread->td_ru.ru_inblock++;
249 * If we have been doing sequential I/O, then do some read-ahead.
251 while (lblkno < (origblkno + maxra)) {
252 error = VOP_BMAP(vp, lblkno, NULL, &blkno, &ncontig, NULL);
260 * We could throttle ncontig here by maxra but we might as
261 * well read the data if it is contiguous. We're throttled
262 * by racluster anyway.
265 ncontig = min(ncontig + 1, racluster);
266 rbp = cluster_rbuild(vp, filesize, lblkno, blkno,
267 size, ncontig, NULL);
268 lblkno += (rbp->b_bufsize / size);
269 if (rbp->b_flags & B_DELWRI) {
274 rbp = getblk(vp, lblkno, size, 0, 0, 0);
276 if (rbp->b_flags & B_DELWRI) {
280 rbp->b_flags |= B_ASYNC | B_RAM;
281 rbp->b_iocmd = BIO_READ;
282 rbp->b_blkno = blkno;
284 if (rbp->b_flags & B_CACHE) {
285 rbp->b_flags &= ~B_ASYNC;
289 if ((rbp->b_flags & B_CLUSTER) == 0) {
290 vfs_busy_pages(rbp, 0);
292 rbp->b_flags &= ~B_INVAL;
293 rbp->b_ioflags &= ~BIO_ERROR;
294 if ((rbp->b_flags & B_ASYNC) || rbp->b_iodone != NULL)
296 rbp->b_iooffset = dbtob(rbp->b_blkno);
298 curthread->td_ru.ru_inblock++;
302 return (bufwait(reqbp));
308 * If blocks are contiguous on disk, use this to provide clustered
309 * read ahead. We will read as many blocks as possible sequentially
310 * and then parcel them up into logical blocks in the buffer hash table.
313 cluster_rbuild(vp, filesize, lbn, blkno, size, run, fbp)
323 struct buf *bp, *tbp;
329 KASSERT(size == vp->v_mount->mnt_stat.f_iosize,
330 ("cluster_rbuild: size %ld != filesize %jd\n",
331 size, (intmax_t)vp->v_mount->mnt_stat.f_iosize));
336 while ((u_quad_t) size * (lbn + run) > filesize) {
342 tbp->b_iocmd = BIO_READ;
344 tbp = getblk(vp, lbn, size, 0, 0, 0);
345 if (tbp->b_flags & B_CACHE)
347 tbp->b_flags |= B_ASYNC | B_RAM;
348 tbp->b_iocmd = BIO_READ;
350 tbp->b_blkno = blkno;
351 if( (tbp->b_flags & B_MALLOC) ||
352 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
355 bp = trypbuf(&cluster_pbuf_freecnt);
360 * We are synthesizing a buffer out of vm_page_t's, but
361 * if the block size is not page aligned then the starting
362 * address may not be either. Inherit the b_data offset
363 * from the original buffer.
365 bp->b_data = (char *)((vm_offset_t)bp->b_data |
366 ((vm_offset_t)tbp->b_data & PAGE_MASK));
367 bp->b_flags = B_ASYNC | B_CLUSTER | B_VMIO;
368 bp->b_iocmd = BIO_READ;
369 bp->b_iodone = cluster_callback;
372 bp->b_offset = tbp->b_offset;
373 KASSERT(bp->b_offset != NOOFFSET, ("cluster_rbuild: no buffer offset"));
376 TAILQ_INIT(&bp->b_cluster.cluster_head);
384 for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
386 if ((bp->b_npages * PAGE_SIZE) +
387 round_page(size) > vp->v_mount->mnt_iosize_max) {
391 tbp = getblk(vp, lbn + i, size, 0, 0, GB_LOCK_NOWAIT);
393 /* Don't wait around for locked bufs. */
398 * Stop scanning if the buffer is fully valid
399 * (marked B_CACHE), or locked (may be doing a
400 * background write), or if the buffer is not
401 * VMIO backed. The clustering code can only deal
402 * with VMIO-backed buffers.
405 if ((tbp->b_vflags & BV_BKGRDINPROG) ||
406 (tbp->b_flags & B_CACHE) ||
407 (tbp->b_flags & B_VMIO) == 0) {
415 * The buffer must be completely invalid in order to
416 * take part in the cluster. If it is partially valid
421 VM_OBJECT_LOCK(tbp->b_bufobj->bo_object);
422 for (j = 0; tsize > 0; j++) {
423 toff = off & PAGE_MASK;
425 if (toff + tinc > PAGE_SIZE)
426 tinc = PAGE_SIZE - toff;
427 VM_OBJECT_LOCK_ASSERT(tbp->b_pages[j]->object,
429 if ((tbp->b_pages[j]->valid &
430 vm_page_bits(toff, tinc)) != 0)
435 VM_OBJECT_UNLOCK(tbp->b_bufobj->bo_object);
442 * Set a read-ahead mark as appropriate
444 if ((fbp && (i == 1)) || (i == (run - 1)))
445 tbp->b_flags |= B_RAM;
448 * Set the buffer up for an async read (XXX should
449 * we do this only if we do not wind up brelse()ing?).
450 * Set the block number if it isn't set, otherwise
451 * if it is make sure it matches the block number we
454 tbp->b_flags |= B_ASYNC;
455 tbp->b_iocmd = BIO_READ;
456 if (tbp->b_blkno == tbp->b_lblkno) {
458 } else if (tbp->b_blkno != bn) {
464 * XXX fbp from caller may not be B_ASYNC, but we are going
465 * to biodone() it in cluster_callback() anyway
468 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
469 tbp, b_cluster.cluster_entry);
470 VM_OBJECT_LOCK(tbp->b_bufobj->bo_object);
471 for (j = 0; j < tbp->b_npages; j += 1) {
475 vm_object_pip_add(m->object, 1);
476 if ((bp->b_npages == 0) ||
477 (bp->b_pages[bp->b_npages-1] != m)) {
478 bp->b_pages[bp->b_npages] = m;
481 if (m->valid == VM_PAGE_BITS_ALL)
482 tbp->b_pages[j] = bogus_page;
484 VM_OBJECT_UNLOCK(tbp->b_bufobj->bo_object);
486 * Don't inherit tbp->b_bufsize as it may be larger due to
487 * a non-page-aligned size. Instead just aggregate using
490 if (tbp->b_bcount != size)
491 printf("warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size);
492 if (tbp->b_bufsize != size)
493 printf("warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size);
494 bp->b_bcount += size;
495 bp->b_bufsize += size;
499 * Fully valid pages in the cluster are already good and do not need
500 * to be re-read from disk. Replace the page with bogus_page
502 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
503 for (j = 0; j < bp->b_npages; j++) {
504 VM_OBJECT_LOCK_ASSERT(bp->b_pages[j]->object, MA_OWNED);
505 if (bp->b_pages[j]->valid == VM_PAGE_BITS_ALL)
506 bp->b_pages[j] = bogus_page;
508 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
509 if (bp->b_bufsize > bp->b_kvasize)
510 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
511 bp->b_bufsize, bp->b_kvasize);
512 bp->b_kvasize = bp->b_bufsize;
514 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
515 (vm_page_t *)bp->b_pages, bp->b_npages);
520 * Cleanup after a clustered read or write.
521 * This is complicated by the fact that any of the buffers might have
522 * extra memory (if there were no empty buffer headers at allocbuf time)
523 * that we will need to shift around.
529 struct buf *nbp, *tbp;
533 * Must propogate errors to all the components.
535 if (bp->b_ioflags & BIO_ERROR)
538 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages);
540 * Move memory from the large cluster buffer into the component
541 * buffers and mark IO as done on these.
543 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head);
545 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry);
547 tbp->b_ioflags |= BIO_ERROR;
548 tbp->b_error = error;
550 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
551 tbp->b_flags &= ~B_INVAL;
552 tbp->b_ioflags &= ~BIO_ERROR;
554 * XXX the bdwrite()/bqrelse() issued during
555 * cluster building clears B_RELBUF (see bqrelse()
556 * comment). If direct I/O was specified, we have
557 * to restore it here to allow the buffer and VM
560 if (tbp->b_flags & B_DIRECT)
561 tbp->b_flags |= B_RELBUF;
566 relpbuf(bp, &cluster_pbuf_freecnt);
572 * Implement modified write build for cluster.
574 * write_behind = 0 write behind disabled
575 * write_behind = 1 write behind normal (default)
576 * write_behind = 2 write behind backed-off
580 cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len)
584 switch (write_behind) {
591 r = cluster_wbuild(vp, size, start_lbn, len);
601 * Do clustered write for FFS.
604 * 1. Write is not sequential (write asynchronously)
605 * Write is sequential:
606 * 2. beginning of cluster - begin cluster
607 * 3. middle of a cluster - add to cluster
608 * 4. end of a cluster - asynchronously write cluster
611 cluster_write(struct vnode *vp, struct buf *bp, u_quad_t filesize, int seqcount)
614 int maxclen, cursize;
618 if (vp->v_type == VREG) {
619 async = DOINGASYNC(vp);
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;
766 int totalwritten = 0;
767 int dbsize = btodb(size);
772 * If the buffer is not delayed-write (i.e. dirty), or it
773 * is delayed-write but either locked or inval, it cannot
774 * partake in the clustered write.
777 if ((tbp = gbincore(&vp->v_bufobj, start_lbn)) == NULL ||
778 (tbp->b_vflags & BV_BKGRDINPROG)) {
785 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, BO_MTX(bo))) {
790 if ((tbp->b_flags & (B_INVAL | B_DELWRI)) != B_DELWRI) {
796 if (tbp->b_pin_count > 0) {
803 tbp->b_flags &= ~B_DONE;
806 * Extra memory in the buffer, punt on this buffer.
807 * XXX we could handle this in most cases, but we would
808 * have to push the extra memory down to after our max
809 * possible cluster size and then potentially pull it back
810 * up if the cluster was terminated prematurely--too much
813 if (((tbp->b_flags & (B_CLUSTEROK | B_MALLOC | B_VMIO)) !=
814 (B_CLUSTEROK | B_VMIO)) ||
815 (tbp->b_bcount != tbp->b_bufsize) ||
816 (tbp->b_bcount != size) ||
818 ((bp = getpbuf(&cluster_pbuf_freecnt)) == NULL)) {
819 totalwritten += tbp->b_bufsize;
827 * We got a pbuf to make the cluster in.
830 TAILQ_INIT(&bp->b_cluster.cluster_head);
834 if (tbp->b_wcred != NOCRED)
835 bp->b_wcred = crhold(tbp->b_wcred);
837 bp->b_blkno = tbp->b_blkno;
838 bp->b_lblkno = tbp->b_lblkno;
839 bp->b_offset = tbp->b_offset;
842 * We are synthesizing a buffer out of vm_page_t's, but
843 * if the block size is not page aligned then the starting
844 * address may not be either. Inherit the b_data offset
845 * from the original buffer.
847 bp->b_data = (char *)((vm_offset_t)bp->b_data |
848 ((vm_offset_t)tbp->b_data & PAGE_MASK));
849 bp->b_flags |= B_CLUSTER |
850 (tbp->b_flags & (B_VMIO | B_NEEDCOMMIT));
851 bp->b_iodone = cluster_callback;
854 * From this location in the file, scan forward to see
855 * if there are buffers with adjacent data that need to
856 * be written as well.
858 for (i = 0; i < len; ++i, ++start_lbn) {
859 if (i != 0) { /* If not the first buffer */
861 * If the adjacent data is not even in core it
862 * can't need to be written.
865 if ((tbp = gbincore(bo, start_lbn)) == NULL ||
866 (tbp->b_vflags & BV_BKGRDINPROG)) {
872 * If it IS in core, but has different
873 * characteristics, or is locked (which
874 * means it could be undergoing a background
875 * I/O or be in a weird state), then don't
879 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
883 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
884 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
885 != (B_DELWRI | B_CLUSTEROK |
886 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
887 tbp->b_wcred != bp->b_wcred) {
893 * Check that the combined cluster
894 * would make sense with regard to pages
895 * and would not be too large
897 if ((tbp->b_bcount != size) ||
898 ((bp->b_blkno + (dbsize * i)) !=
900 ((tbp->b_npages + bp->b_npages) >
901 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
907 * Do not pull in pinned buffers.
909 if (tbp->b_pin_count > 0) {
915 * Ok, it's passed all the tests,
916 * so remove it from the free list
917 * and mark it busy. We will use it.
920 tbp->b_flags &= ~B_DONE;
921 } /* end of code for non-first buffers only */
923 * If the IO is via the VM then we do some
924 * special VM hackery (yuck). Since the buffer's
925 * block size may not be page-aligned it is possible
926 * for a page to be shared between two buffers. We
927 * have to get rid of the duplication when building
930 if (tbp->b_flags & B_VMIO) {
933 VM_OBJECT_LOCK(tbp->b_bufobj->bo_object);
934 if (i != 0) { /* if not first buffer */
935 for (j = 0; j < tbp->b_npages; j += 1) {
937 if (m->oflags & VPO_BUSY) {
945 for (j = 0; j < tbp->b_npages; j += 1) {
948 vm_object_pip_add(m->object, 1);
949 if ((bp->b_npages == 0) ||
950 (bp->b_pages[bp->b_npages - 1] != m)) {
951 bp->b_pages[bp->b_npages] = m;
955 VM_OBJECT_UNLOCK(tbp->b_bufobj->bo_object);
957 bp->b_bcount += size;
958 bp->b_bufsize += size;
960 * If any of the clustered buffers have their
961 * B_BARRIER flag set, transfer that request to
964 bp->b_flags |= (tbp->b_flags & B_BARRIER);
965 tbp->b_flags &= ~(B_DONE | B_BARRIER);
966 tbp->b_flags |= B_ASYNC;
967 tbp->b_ioflags &= ~BIO_ERROR;
968 tbp->b_iocmd = BIO_WRITE;
970 reassignbuf(tbp); /* put on clean list */
971 bufobj_wref(tbp->b_bufobj);
973 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
974 tbp, b_cluster.cluster_entry);
977 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
978 (vm_page_t *) bp->b_pages, bp->b_npages);
979 if (bp->b_bufsize > bp->b_kvasize)
981 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
982 bp->b_bufsize, bp->b_kvasize);
983 bp->b_kvasize = bp->b_bufsize;
984 totalwritten += bp->b_bufsize;
986 bp->b_dirtyend = bp->b_bufsize;
995 * Collect together all the buffers in a cluster.
996 * Plus add one additional buffer.
998 static struct cluster_save *
999 cluster_collectbufs(vp, last_bp)
1001 struct buf *last_bp;
1003 struct cluster_save *buflist;
1008 len = vp->v_lastw - vp->v_cstart + 1;
1009 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
1010 M_SEGMENT, M_WAITOK);
1011 buflist->bs_nchildren = 0;
1012 buflist->bs_children = (struct buf **) (buflist + 1);
1013 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) {
1014 (void) bread(vp, lbn, last_bp->b_bcount, NOCRED, &bp);
1015 buflist->bs_children[i] = bp;
1016 if (bp->b_blkno == bp->b_lblkno)
1017 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1020 buflist->bs_children[i] = bp = last_bp;
1021 if (bp->b_blkno == bp->b_lblkno)
1022 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1023 buflist->bs_nchildren = i + 1;