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/rwlock.h>
50 #include <sys/vmmeter.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_page.h>
54 #include <sys/sysctl.h>
56 #if defined(CLUSTERDEBUG)
57 static int rcluster= 0;
58 SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0,
59 "Debug VFS clustering code");
62 static MALLOC_DEFINE(M_SEGMENT, "cl_savebuf", "cluster_save buffer");
64 static struct cluster_save *cluster_collectbufs(struct vnode *vp,
65 struct buf *last_bp, int gbflags);
66 static struct buf *cluster_rbuild(struct vnode *vp, u_quad_t filesize,
67 daddr_t lbn, daddr_t blkno, long size, int run, int gbflags,
69 static void cluster_callback(struct buf *);
71 static int write_behind = 1;
72 SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0,
73 "Cluster write-behind; 0: disable, 1: enable, 2: backed off");
75 static int read_max = 64;
76 SYSCTL_INT(_vfs, OID_AUTO, read_max, CTLFLAG_RW, &read_max, 0,
77 "Cluster read-ahead max block count");
79 static int read_min = 1;
80 SYSCTL_INT(_vfs, OID_AUTO, read_min, CTLFLAG_RW, &read_min, 0,
81 "Cluster read min block count");
83 /* Page expended to mark partially backed buffers */
84 extern vm_page_t bogus_page;
87 * Read data to a buf, including read-ahead if we find this to be beneficial.
88 * cluster_read replaces bread.
91 cluster_read(struct vnode *vp, u_quad_t filesize, daddr_t lblkno, long size,
92 struct ucred *cred, long totread, int seqcount, int gbflags,
95 struct buf *bp, *rbp, *reqbp;
97 daddr_t blkno, origblkno;
104 if (!unmapped_buf_allowed)
105 gbflags &= ~GB_UNMAPPED;
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;
113 maxra = min(read_max, maxra);
114 maxra = min(nbuf/8, maxra);
115 if (((u_quad_t)(lblkno + maxra + 1) * size) > filesize)
116 maxra = (filesize / size) - lblkno;
119 * get the requested block
121 *bpp = reqbp = bp = getblk(vp, lblkno, size, 0, 0, gbflags);
125 * if it is in the cache, then check to see if the reads have been
126 * sequential. If they have, then try some read-ahead, otherwise
127 * back-off on prospective read-aheads.
129 if (bp->b_flags & B_CACHE) {
132 } else if ((bp->b_flags & B_RAM) == 0) {
135 bp->b_flags &= ~B_RAM;
137 for (i = 1; i < maxra; i++) {
139 * Stop if the buffer does not exist or it
140 * is invalid (about to go away?)
142 rbp = gbincore(&vp->v_bufobj, lblkno+i);
143 if (rbp == NULL || (rbp->b_flags & B_INVAL))
147 * Set another read-ahead mark so we know
148 * to check again. (If we can lock the
149 * buffer without waiting)
151 if ((((i % racluster) == (racluster - 1)) ||
153 && (0 == BUF_LOCK(rbp,
154 LK_EXCLUSIVE | LK_NOWAIT, NULL))) {
155 rbp->b_flags |= B_RAM;
167 * If it isn't in the cache, then get a chunk from
168 * disk if sequential, otherwise just get the block.
171 off_t firstread = bp->b_offset;
175 KASSERT(bp->b_offset != NOOFFSET,
176 ("cluster_read: no buffer offset"));
181 * Adjust totread if needed
183 minread = read_min * size;
184 if (minread > totread)
188 * Compute the total number of blocks that we should read
191 if (firstread + totread > filesize)
192 totread = filesize - firstread;
193 nblks = howmany(totread, size);
194 if (nblks > racluster)
198 * Now compute the number of contiguous blocks.
201 error = VOP_BMAP(vp, lblkno, NULL,
202 &blkno, &ncontig, NULL);
204 * If this failed to map just do the original block.
206 if (error || blkno == -1)
211 * If we have contiguous data available do a cluster
212 * otherwise just read the requested block.
215 /* Account for our first block. */
216 ncontig = min(ncontig + 1, nblks);
219 bp = cluster_rbuild(vp, filesize, lblkno,
220 blkno, size, nblks, gbflags, bp);
221 lblkno += (bp->b_bufsize / size);
223 bp->b_flags |= B_RAM;
224 bp->b_iocmd = BIO_READ;
230 * handle the synchronous read so that it is available ASAP.
233 if ((bp->b_flags & B_CLUSTER) == 0) {
234 vfs_busy_pages(bp, 0);
236 bp->b_flags &= ~B_INVAL;
237 bp->b_ioflags &= ~BIO_ERROR;
238 if ((bp->b_flags & B_ASYNC) || bp->b_iodone != NULL)
240 bp->b_iooffset = dbtob(bp->b_blkno);
242 curthread->td_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, gbflags, NULL);
265 lblkno += (rbp->b_bufsize / size);
266 if (rbp->b_flags & B_DELWRI) {
271 rbp = getblk(vp, lblkno, size, 0, 0, gbflags);
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);
295 curthread->td_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(struct vnode *vp, u_quad_t filesize, daddr_t lbn,
311 daddr_t blkno, long size, int run, int gbflags, struct buf *fbp)
313 struct buf *bp, *tbp;
317 int i, inc, j, k, toff;
319 KASSERT(size == vp->v_mount->mnt_stat.f_iosize,
320 ("cluster_rbuild: size %ld != f_iosize %jd\n",
321 size, (intmax_t)vp->v_mount->mnt_stat.f_iosize));
326 while ((u_quad_t) size * (lbn + run) > filesize) {
332 tbp->b_iocmd = BIO_READ;
334 tbp = getblk(vp, lbn, size, 0, 0, gbflags);
335 if (tbp->b_flags & B_CACHE)
337 tbp->b_flags |= B_ASYNC | B_RAM;
338 tbp->b_iocmd = BIO_READ;
340 tbp->b_blkno = blkno;
341 if( (tbp->b_flags & B_MALLOC) ||
342 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
345 bp = trypbuf(&cluster_pbuf_freecnt);
350 * We are synthesizing a buffer out of vm_page_t's, but
351 * if the block size is not page aligned then the starting
352 * address may not be either. Inherit the b_data offset
353 * from the original buffer.
355 bp->b_flags = B_ASYNC | B_CLUSTER | B_VMIO;
356 if ((gbflags & GB_UNMAPPED) != 0) {
357 bp->b_data = unmapped_buf;
359 bp->b_data = (char *)((vm_offset_t)bp->b_data |
360 ((vm_offset_t)tbp->b_data & PAGE_MASK));
362 bp->b_iocmd = BIO_READ;
363 bp->b_iodone = cluster_callback;
366 bp->b_offset = tbp->b_offset;
367 KASSERT(bp->b_offset != NOOFFSET, ("cluster_rbuild: no buffer offset"));
370 TAILQ_INIT(&bp->b_cluster.cluster_head);
377 for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
379 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
380 vfs_drain_busy_pages(tbp);
381 vm_object_pip_add(tbp->b_bufobj->bo_object,
383 for (k = 0; k < tbp->b_npages; k++)
384 vm_page_sbusy(tbp->b_pages[k]);
385 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
387 if ((bp->b_npages * PAGE_SIZE) +
388 round_page(size) > vp->v_mount->mnt_iosize_max) {
392 tbp = getblk(vp, lbn + i, size, 0, 0, GB_LOCK_NOWAIT |
393 (gbflags & GB_UNMAPPED));
395 /* Don't wait around for locked bufs. */
400 * Stop scanning if the buffer is fully valid
401 * (marked B_CACHE), or locked (may be doing a
402 * background write), or if the buffer is not
403 * VMIO backed. The clustering code can only deal
404 * with VMIO-backed buffers. The bo lock is not
405 * required for the BKGRDINPROG check since it
406 * can not be set without the buf lock.
408 if ((tbp->b_vflags & BV_BKGRDINPROG) ||
409 (tbp->b_flags & B_CACHE) ||
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
422 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
423 for (j = 0; tsize > 0; j++) {
424 toff = off & PAGE_MASK;
426 if (toff + tinc > PAGE_SIZE)
427 tinc = PAGE_SIZE - toff;
428 VM_OBJECT_ASSERT_WLOCKED(tbp->b_pages[j]->object);
429 if ((tbp->b_pages[j]->valid &
430 vm_page_bits(toff, tinc)) != 0)
432 if (vm_page_xbusied(tbp->b_pages[j]))
434 vm_object_pip_add(tbp->b_bufobj->bo_object, 1);
435 vm_page_sbusy(tbp->b_pages[j]);
441 vm_object_pip_add(tbp->b_bufobj->bo_object, -j);
442 for (k = 0; k < j; k++)
443 vm_page_sunbusy(tbp->b_pages[k]);
444 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
448 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
451 * Set a read-ahead mark as appropriate
453 if ((fbp && (i == 1)) || (i == (run - 1)))
454 tbp->b_flags |= B_RAM;
457 * Set the buffer up for an async read (XXX should
458 * we do this only if we do not wind up brelse()ing?).
459 * Set the block number if it isn't set, otherwise
460 * if it is make sure it matches the block number we
463 tbp->b_flags |= B_ASYNC;
464 tbp->b_iocmd = BIO_READ;
465 if (tbp->b_blkno == tbp->b_lblkno) {
467 } else if (tbp->b_blkno != bn) {
468 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
473 * XXX fbp from caller may not be B_ASYNC, but we are going
474 * to biodone() it in cluster_callback() anyway
477 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
478 tbp, b_cluster.cluster_entry);
479 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
480 for (j = 0; j < tbp->b_npages; j += 1) {
483 if ((bp->b_npages == 0) ||
484 (bp->b_pages[bp->b_npages-1] != m)) {
485 bp->b_pages[bp->b_npages] = m;
488 if (m->valid == VM_PAGE_BITS_ALL)
489 tbp->b_pages[j] = bogus_page;
491 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
493 * Don't inherit tbp->b_bufsize as it may be larger due to
494 * a non-page-aligned size. Instead just aggregate using
497 if (tbp->b_bcount != size)
498 printf("warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size);
499 if (tbp->b_bufsize != size)
500 printf("warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size);
501 bp->b_bcount += size;
502 bp->b_bufsize += size;
506 * Fully valid pages in the cluster are already good and do not need
507 * to be re-read from disk. Replace the page with bogus_page
509 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
510 for (j = 0; j < bp->b_npages; j++) {
511 VM_OBJECT_ASSERT_WLOCKED(bp->b_pages[j]->object);
512 if (bp->b_pages[j]->valid == VM_PAGE_BITS_ALL)
513 bp->b_pages[j] = bogus_page;
515 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
516 if (bp->b_bufsize > bp->b_kvasize)
517 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
518 bp->b_bufsize, bp->b_kvasize);
520 if (buf_mapped(bp)) {
521 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
522 (vm_page_t *)bp->b_pages, bp->b_npages);
528 * Cleanup after a clustered read or write.
529 * This is complicated by the fact that any of the buffers might have
530 * extra memory (if there were no empty buffer headers at allocbuf time)
531 * that we will need to shift around.
537 struct buf *nbp, *tbp;
541 * Must propogate errors to all the components.
543 if (bp->b_ioflags & BIO_ERROR)
546 if (buf_mapped(bp)) {
547 pmap_qremove(trunc_page((vm_offset_t) bp->b_data),
551 * Move memory from the large cluster buffer into the component
552 * buffers and mark IO as done on these.
554 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head);
556 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry);
558 tbp->b_ioflags |= BIO_ERROR;
559 tbp->b_error = error;
561 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
562 tbp->b_flags &= ~B_INVAL;
563 tbp->b_ioflags &= ~BIO_ERROR;
565 * XXX the bdwrite()/bqrelse() issued during
566 * cluster building clears B_RELBUF (see bqrelse()
567 * comment). If direct I/O was specified, we have
568 * to restore it here to allow the buffer and VM
571 if (tbp->b_flags & B_DIRECT)
572 tbp->b_flags |= B_RELBUF;
577 relpbuf(bp, &cluster_pbuf_freecnt);
583 * Implement modified write build for cluster.
585 * write_behind = 0 write behind disabled
586 * write_behind = 1 write behind normal (default)
587 * write_behind = 2 write behind backed-off
591 cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len,
596 switch (write_behind) {
603 r = cluster_wbuild(vp, size, start_lbn, len, gbflags);
613 * Do clustered write for FFS.
616 * 1. Write is not sequential (write asynchronously)
617 * Write is sequential:
618 * 2. beginning of cluster - begin cluster
619 * 3. middle of a cluster - add to cluster
620 * 4. end of a cluster - asynchronously write cluster
623 cluster_write(struct vnode *vp, struct buf *bp, u_quad_t filesize, int seqcount,
627 int maxclen, cursize;
631 if (!unmapped_buf_allowed)
632 gbflags &= ~GB_UNMAPPED;
634 if (vp->v_type == VREG) {
635 async = DOINGASYNC(vp);
636 lblocksize = vp->v_mount->mnt_stat.f_iosize;
639 lblocksize = bp->b_bufsize;
642 KASSERT(bp->b_offset != NOOFFSET, ("cluster_write: no buffer offset"));
644 /* Initialize vnode to beginning of file. */
646 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
648 if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 ||
649 (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) {
650 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1;
651 if (vp->v_clen != 0) {
653 * Next block is not sequential.
655 * If we are not writing at end of file, the process
656 * seeked to another point in the file since its last
657 * write, or we have reached our maximum cluster size,
658 * then push the previous cluster. Otherwise try
659 * reallocating to make it sequential.
661 * Change to algorithm: only push previous cluster if
662 * it was sequential from the point of view of the
663 * seqcount heuristic, otherwise leave the buffer
664 * intact so we can potentially optimize the I/O
665 * later on in the buf_daemon or update daemon
668 cursize = vp->v_lastw - vp->v_cstart + 1;
669 if (((u_quad_t) bp->b_offset + lblocksize) != filesize ||
670 lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) {
671 if (!async && seqcount > 0) {
672 cluster_wbuild_wb(vp, lblocksize,
673 vp->v_cstart, cursize, gbflags);
676 struct buf **bpp, **endbp;
677 struct cluster_save *buflist;
679 buflist = cluster_collectbufs(vp, bp, gbflags);
680 endbp = &buflist->bs_children
681 [buflist->bs_nchildren - 1];
682 if (VOP_REALLOCBLKS(vp, buflist)) {
684 * Failed, push the previous cluster
685 * if *really* writing sequentially
686 * in the logical file (seqcount > 1),
687 * otherwise delay it in the hopes that
688 * the low level disk driver can
689 * optimize the write ordering.
691 for (bpp = buflist->bs_children;
694 free(buflist, M_SEGMENT);
696 cluster_wbuild_wb(vp,
697 lblocksize, vp->v_cstart,
702 * Succeeded, keep building cluster.
704 for (bpp = buflist->bs_children;
707 free(buflist, M_SEGMENT);
709 vp->v_lasta = bp->b_blkno;
715 * Consider beginning a cluster. If at end of file, make
716 * cluster as large as possible, otherwise find size of
719 if ((vp->v_type == VREG) &&
720 ((u_quad_t) bp->b_offset + lblocksize) != filesize &&
721 (bp->b_blkno == bp->b_lblkno) &&
722 (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) ||
723 bp->b_blkno == -1)) {
726 vp->v_lasta = bp->b_blkno;
727 vp->v_cstart = lbn + 1;
731 vp->v_clen = maxclen;
732 if (!async && maxclen == 0) { /* I/O not contiguous */
733 vp->v_cstart = lbn + 1;
735 } else { /* Wait for rest of cluster */
739 } else if (lbn == vp->v_cstart + vp->v_clen) {
741 * At end of cluster, write it out if seqcount tells us we
742 * are operating sequentially, otherwise let the buf or
743 * update daemon handle it.
747 cluster_wbuild_wb(vp, lblocksize, vp->v_cstart,
748 vp->v_clen + 1, gbflags);
751 vp->v_cstart = lbn + 1;
752 } else if (vm_page_count_severe()) {
754 * We are low on memory, get it going NOW
759 * In the middle of a cluster, so just delay the I/O for now.
764 vp->v_lasta = bp->b_blkno;
769 * This is an awful lot like cluster_rbuild...wish they could be combined.
770 * The last lbn argument is the current block on which I/O is being
771 * performed. Check to see that it doesn't fall in the middle of
772 * the current block (if last_bp == NULL).
775 cluster_wbuild(struct vnode *vp, long size, daddr_t start_lbn, int len,
778 struct buf *bp, *tbp;
781 int totalwritten = 0;
782 int dbsize = btodb(size);
784 if (!unmapped_buf_allowed)
785 gbflags &= ~GB_UNMAPPED;
790 * If the buffer is not delayed-write (i.e. dirty), or it
791 * is delayed-write but either locked or inval, it cannot
792 * partake in the clustered write.
795 if ((tbp = gbincore(&vp->v_bufobj, start_lbn)) == NULL ||
796 (tbp->b_vflags & BV_BKGRDINPROG)) {
803 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, BO_LOCKPTR(bo))) {
808 if ((tbp->b_flags & (B_INVAL | B_DELWRI)) != B_DELWRI) {
814 if (tbp->b_pin_count > 0) {
821 tbp->b_flags &= ~B_DONE;
824 * Extra memory in the buffer, punt on this buffer.
825 * XXX we could handle this in most cases, but we would
826 * have to push the extra memory down to after our max
827 * possible cluster size and then potentially pull it back
828 * up if the cluster was terminated prematurely--too much
831 if (((tbp->b_flags & (B_CLUSTEROK | B_MALLOC | B_VMIO)) !=
832 (B_CLUSTEROK | B_VMIO)) ||
833 (tbp->b_bcount != tbp->b_bufsize) ||
834 (tbp->b_bcount != size) ||
836 ((bp = (vp->v_vflag & VV_MD) != 0 ?
837 trypbuf(&cluster_pbuf_freecnt) :
838 getpbuf(&cluster_pbuf_freecnt)) == NULL)) {
839 totalwritten += tbp->b_bufsize;
847 * We got a pbuf to make the cluster in.
850 TAILQ_INIT(&bp->b_cluster.cluster_head);
854 if (tbp->b_wcred != NOCRED)
855 bp->b_wcred = crhold(tbp->b_wcred);
857 bp->b_blkno = tbp->b_blkno;
858 bp->b_lblkno = tbp->b_lblkno;
859 bp->b_offset = tbp->b_offset;
862 * We are synthesizing a buffer out of vm_page_t's, but
863 * if the block size is not page aligned then the starting
864 * address may not be either. Inherit the b_data offset
865 * from the original buffer.
867 if ((gbflags & GB_UNMAPPED) == 0 ||
868 (tbp->b_flags & B_VMIO) == 0) {
869 bp->b_data = (char *)((vm_offset_t)bp->b_data |
870 ((vm_offset_t)tbp->b_data & PAGE_MASK));
872 bp->b_data = unmapped_buf;
874 bp->b_flags |= B_CLUSTER | (tbp->b_flags & (B_VMIO |
876 bp->b_iodone = cluster_callback;
879 * From this location in the file, scan forward to see
880 * if there are buffers with adjacent data that need to
881 * be written as well.
883 for (i = 0; i < len; ++i, ++start_lbn) {
884 if (i != 0) { /* If not the first buffer */
886 * If the adjacent data is not even in core it
887 * can't need to be written.
890 if ((tbp = gbincore(bo, start_lbn)) == NULL ||
891 (tbp->b_vflags & BV_BKGRDINPROG)) {
897 * If it IS in core, but has different
898 * characteristics, or is locked (which
899 * means it could be undergoing a background
900 * I/O or be in a weird state), then don't
904 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
908 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
909 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
910 != (B_DELWRI | B_CLUSTEROK |
911 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
912 tbp->b_wcred != bp->b_wcred) {
918 * Check that the combined cluster
919 * would make sense with regard to pages
920 * and would not be too large
922 if ((tbp->b_bcount != size) ||
923 ((bp->b_blkno + (dbsize * i)) !=
925 ((tbp->b_npages + bp->b_npages) >
926 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
932 * Do not pull in pinned buffers.
934 if (tbp->b_pin_count > 0) {
940 * Ok, it's passed all the tests,
941 * so remove it from the free list
942 * and mark it busy. We will use it.
945 tbp->b_flags &= ~B_DONE;
946 } /* end of code for non-first buffers only */
948 * If the IO is via the VM then we do some
949 * special VM hackery (yuck). Since the buffer's
950 * block size may not be page-aligned it is possible
951 * for a page to be shared between two buffers. We
952 * have to get rid of the duplication when building
955 if (tbp->b_flags & B_VMIO) {
958 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
960 vfs_drain_busy_pages(tbp);
961 } else { /* if not first buffer */
962 for (j = 0; j < tbp->b_npages; j += 1) {
964 if (vm_page_xbusied(m)) {
972 for (j = 0; j < tbp->b_npages; j += 1) {
975 vm_object_pip_add(m->object, 1);
976 if ((bp->b_npages == 0) ||
977 (bp->b_pages[bp->b_npages - 1] != m)) {
978 bp->b_pages[bp->b_npages] = m;
982 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
984 bp->b_bcount += size;
985 bp->b_bufsize += size;
987 * If any of the clustered buffers have their
988 * B_BARRIER flag set, transfer that request to
991 bp->b_flags |= (tbp->b_flags & B_BARRIER);
992 tbp->b_flags &= ~(B_DONE | B_BARRIER);
993 tbp->b_flags |= B_ASYNC;
994 tbp->b_ioflags &= ~BIO_ERROR;
995 tbp->b_iocmd = BIO_WRITE;
997 reassignbuf(tbp); /* put on clean list */
998 bufobj_wref(tbp->b_bufobj);
1000 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
1001 tbp, b_cluster.cluster_entry);
1004 if (buf_mapped(bp)) {
1005 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
1006 (vm_page_t *)bp->b_pages, bp->b_npages);
1008 if (bp->b_bufsize > bp->b_kvasize)
1010 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
1011 bp->b_bufsize, bp->b_kvasize);
1012 totalwritten += bp->b_bufsize;
1014 bp->b_dirtyend = bp->b_bufsize;
1019 return totalwritten;
1023 * Collect together all the buffers in a cluster.
1024 * Plus add one additional buffer.
1026 static struct cluster_save *
1027 cluster_collectbufs(struct vnode *vp, struct buf *last_bp, int gbflags)
1029 struct cluster_save *buflist;
1034 len = vp->v_lastw - vp->v_cstart + 1;
1035 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
1036 M_SEGMENT, M_WAITOK);
1037 buflist->bs_nchildren = 0;
1038 buflist->bs_children = (struct buf **) (buflist + 1);
1039 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) {
1040 (void)bread_gb(vp, lbn, last_bp->b_bcount, NOCRED,
1042 buflist->bs_children[i] = bp;
1043 if (bp->b_blkno == bp->b_lblkno)
1044 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1047 buflist->bs_children[i] = bp = last_bp;
1048 if (bp->b_blkno == bp->b_lblkno)
1049 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1050 buflist->bs_nchildren = i + 1;