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.40 2008/07/14 03:09:00 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 #include <vm/vm_page2.h>
60 #include <machine/limits.h>
62 #if defined(CLUSTERDEBUG)
63 #include <sys/sysctl.h>
64 static int rcluster= 0;
65 SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0, "");
68 static MALLOC_DEFINE(M_SEGMENT, "cluster_save", "cluster_save buffer");
70 static struct cluster_save *
71 cluster_collectbufs (struct vnode *vp, struct buf *last_bp,
74 cluster_rbuild (struct vnode *vp, off_t filesize, off_t loffset,
75 off_t doffset, int blksize, int run,
77 static void cluster_callback (struct bio *);
78 static void cluster_setram (struct buf *);
79 static int cluster_wbuild(struct vnode *vp, struct buf **bpp, int blksize,
80 off_t start_loffset, int bytes);
82 static int write_behind = 1;
83 SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0,
84 "Cluster write-behind setting");
85 static int max_readahead = 2 * 1024 * 1024;
86 SYSCTL_INT(_vfs, OID_AUTO, max_readahead, CTLFLAG_RW, &max_readahead, 0,
87 "Limit in bytes for desired cluster read-ahead");
89 extern vm_page_t bogus_page;
91 extern int cluster_pbuf_freecnt;
94 * This replaces bread.
96 * filesize - read-ahead @ blksize will not cross this boundary
97 * loffset - loffset for returned *bpp
98 * blksize - blocksize for returned *bpp and read-ahead bps
99 * minreq - minimum (not a hard minimum) in bytes, typically reflects
100 * a higher level uio resid.
101 * maxreq - maximum (sequential heuristic) in bytes (highet typ ~2MB)
102 * bpp - return buffer (*bpp) for (loffset,blksize)
105 cluster_readx(struct vnode *vp, off_t filesize, off_t loffset,
106 int blksize, size_t minreq, size_t maxreq, struct buf **bpp)
108 struct buf *bp, *rbp, *reqbp;
119 * Calculate the desired read-ahead in blksize'd blocks (maxra).
120 * To do this we calculate maxreq.
122 * maxreq typically starts out as a sequential heuristic. If the
123 * high level uio/resid is bigger (minreq), we pop maxreq up to
124 * minreq. This represents the case where random I/O is being
125 * performed by the userland is issuing big read()'s.
127 * Then we limit maxreq to max_readahead to ensure it is a reasonable
130 * Finally we must ensure that (loffset + maxreq) does not cross the
131 * boundary (filesize) for the current blocksize. If we allowed it
132 * to cross we could end up with buffers past the boundary with the
133 * wrong block size (HAMMER large-data areas use mixed block sizes).
134 * minreq is also absolutely limited to filesize.
138 /* minreq not used beyond this point */
140 if (maxreq > max_readahead) {
141 maxreq = max_readahead;
142 if (maxreq > 16 * 1024 * 1024)
143 maxreq = 16 * 1024 * 1024;
145 if (maxreq < blksize)
147 if (loffset + maxreq > filesize) {
148 if (loffset > filesize)
151 maxreq = filesize - loffset;
154 maxra = (int)(maxreq / blksize);
157 * Get the requested block.
162 *bpp = reqbp = bp = getblk(vp, loffset, blksize, 0, 0);
163 origoffset = loffset;
166 * Calculate the maximum cluster size for a single I/O, used
167 * by cluster_rbuild().
169 maxrbuild = vmaxiosize(vp) / blksize;
172 * if it is in the cache, then check to see if the reads have been
173 * sequential. If they have, then try some read-ahead, otherwise
174 * back-off on prospective read-aheads.
176 if (bp->b_flags & B_CACHE) {
178 * Not sequential, do not do any read-ahead
184 * No read-ahead mark, do not do any read-ahead
187 if ((bp->b_flags & B_RAM) == 0)
191 * We hit a read-ahead-mark, figure out how much read-ahead
192 * to do (maxra) and where to start (loffset).
194 * Shortcut the scan. Typically the way this works is that
195 * we've built up all the blocks inbetween except for the
196 * last in previous iterations, so if the second-to-last
197 * block is present we just skip ahead to it.
199 * This algorithm has O(1) cpu in the steady state no
200 * matter how large maxra is.
202 bp->b_flags &= ~B_RAM;
204 if (findblk(vp, loffset + (maxra - 2) * blksize, FINDBLK_TEST))
209 if (findblk(vp, loffset + i * blksize,
210 FINDBLK_TEST) == NULL) {
217 * We got everything or everything is in the cache, no
223 loffset += i * blksize;
226 __debugvar off_t firstread = bp->b_loffset;
230 * Set-up synchronous read for bp.
232 bp->b_cmd = BUF_CMD_READ;
233 bp->b_bio1.bio_done = biodone_sync;
234 bp->b_bio1.bio_flags |= BIO_SYNC;
236 KASSERT(firstread != NOOFFSET,
237 ("cluster_read: no buffer offset"));
240 * nblks is our cluster_rbuild request size, limited
241 * primarily by the device.
243 if ((nblks = maxra) > maxrbuild)
249 error = VOP_BMAP(vp, loffset, &doffset,
250 &burstbytes, NULL, BUF_CMD_READ);
252 goto single_block_read;
253 if (nblks > burstbytes / blksize)
254 nblks = burstbytes / blksize;
255 if (doffset == NOOFFSET)
256 goto single_block_read;
258 goto single_block_read;
260 bp = cluster_rbuild(vp, filesize, loffset,
261 doffset, blksize, nblks, bp);
262 loffset += bp->b_bufsize;
263 maxra -= bp->b_bufsize / blksize;
267 * If it isn't in the cache, then get a chunk from
268 * disk if sequential, otherwise just get the block.
277 * If B_CACHE was not set issue bp. bp will either be an
278 * asynchronous cluster buf or a synchronous single-buf.
279 * If it is a single buf it will be the same as reqbp.
281 * NOTE: Once an async cluster buf is issued bp becomes invalid.
284 #if defined(CLUSTERDEBUG)
286 kprintf("S(%012jx,%d,%d)\n",
287 (intmax_t)bp->b_loffset, bp->b_bcount, maxra);
289 if ((bp->b_flags & B_CLUSTER) == 0)
290 vfs_busy_pages(vp, bp);
291 bp->b_flags &= ~(B_ERROR|B_INVAL);
292 vn_strategy(vp, &bp->b_bio1);
298 * If we have been doing sequential I/O, then do some read-ahead.
299 * The code above us should have positioned us at the next likely
302 * Only mess with buffers which we can immediately lock. HAMMER
303 * will do device-readahead irrespective of what the blocks
306 while (error == 0 && maxra > 0) {
311 rbp = getblk(vp, loffset, blksize,
312 GETBLK_SZMATCH|GETBLK_NOWAIT, 0);
315 if ((rbp->b_flags & B_CACHE)) {
321 * An error from the read-ahead bmap has nothing to do
322 * with the caller's original request.
324 tmp_error = VOP_BMAP(vp, loffset, &doffset,
325 &burstbytes, NULL, BUF_CMD_READ);
326 if (tmp_error || doffset == NOOFFSET) {
327 rbp->b_flags |= B_INVAL;
332 if ((nblks = maxra) > maxrbuild)
334 if (nblks > burstbytes / blksize)
335 nblks = burstbytes / blksize;
340 rbp->b_cmd = BUF_CMD_READ;
341 /*rbp->b_flags |= B_AGE*/;
345 rbp = cluster_rbuild(vp, filesize, loffset,
349 rbp->b_bio2.bio_offset = doffset;
352 #if defined(CLUSTERDEBUG)
355 kprintf("A+(%012jx,%d,%jd) "
356 "doff=%012jx minr=%zd ra=%d\n",
357 (intmax_t)loffset, rbp->b_bcount,
358 (intmax_t)(loffset - origoffset),
359 (intmax_t)doffset, minreq, maxra);
361 kprintf("A-(%012jx,%d,%jd) "
362 "doff=%012jx minr=%zd ra=%d\n",
363 (intmax_t)rbp->b_loffset, rbp->b_bcount,
364 (intmax_t)(loffset - origoffset),
365 (intmax_t)doffset, minreq, maxra);
369 rbp->b_flags &= ~(B_ERROR|B_INVAL);
371 if ((rbp->b_flags & B_CLUSTER) == 0)
372 vfs_busy_pages(vp, rbp);
374 loffset += rbp->b_bufsize;
375 maxra -= rbp->b_bufsize / blksize;
376 vn_strategy(vp, &rbp->b_bio1);
377 /* rbp invalid now */
381 * Wait for our original buffer to complete its I/O. reqbp will
382 * be NULL if the original buffer was B_CACHE. We are returning
383 * (*bpp) which is the same as reqbp when reqbp != NULL.
387 KKASSERT(reqbp->b_bio1.bio_flags & BIO_SYNC);
388 error = biowait(&reqbp->b_bio1, "clurd");
394 * If blocks are contiguous on disk, use this to provide clustered
395 * read ahead. We will read as many blocks as possible sequentially
396 * and then parcel them up into logical blocks in the buffer hash table.
398 * This function either returns a cluster buf or it returns fbp. fbp is
399 * already expected to be set up as a synchronous or asynchronous request.
401 * If a cluster buf is returned it will always be async.
404 cluster_rbuild(struct vnode *vp, off_t filesize, off_t loffset, off_t doffset,
405 int blksize, int run, struct buf *fbp)
407 struct buf *bp, *tbp;
410 int maxiosize = vmaxiosize(vp);
415 while (loffset + run * blksize > filesize) {
420 tbp->b_bio2.bio_offset = doffset;
421 if((tbp->b_flags & B_MALLOC) ||
422 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1)) {
426 bp = trypbuf_kva(&cluster_pbuf_freecnt);
432 * We are synthesizing a buffer out of vm_page_t's, but
433 * if the block size is not page aligned then the starting
434 * address may not be either. Inherit the b_data offset
435 * from the original buffer.
437 bp->b_data = (char *)((vm_offset_t)bp->b_data |
438 ((vm_offset_t)tbp->b_data & PAGE_MASK));
439 bp->b_flags |= B_CLUSTER | B_VMIO;
440 bp->b_cmd = BUF_CMD_READ;
441 bp->b_bio1.bio_done = cluster_callback; /* default to async */
442 bp->b_bio1.bio_caller_info1.cluster_head = NULL;
443 bp->b_bio1.bio_caller_info2.cluster_tail = NULL;
444 bp->b_loffset = loffset;
445 bp->b_bio2.bio_offset = doffset;
446 KASSERT(bp->b_loffset != NOOFFSET,
447 ("cluster_rbuild: no buffer offset"));
451 bp->b_xio.xio_npages = 0;
453 for (boffset = doffset, i = 0; i < run; ++i, boffset += blksize) {
455 if ((bp->b_xio.xio_npages * PAGE_SIZE) +
456 round_page(blksize) > maxiosize) {
461 * Shortcut some checks and try to avoid buffers that
462 * would block in the lock. The same checks have to
463 * be made again after we officially get the buffer.
465 tbp = getblk(vp, loffset + i * blksize, blksize,
466 GETBLK_SZMATCH|GETBLK_NOWAIT, 0);
469 for (j = 0; j < tbp->b_xio.xio_npages; j++) {
470 if (tbp->b_xio.xio_pages[j]->valid)
473 if (j != tbp->b_xio.xio_npages) {
479 * Stop scanning if the buffer is fuly valid
480 * (marked B_CACHE), or locked (may be doing a
481 * background write), or if the buffer is not
482 * VMIO backed. The clustering code can only deal
483 * with VMIO-backed buffers.
485 if ((tbp->b_flags & (B_CACHE|B_LOCKED)) ||
486 (tbp->b_flags & B_VMIO) == 0 ||
487 (LIST_FIRST(&tbp->b_dep) != NULL &&
495 * The buffer must be completely invalid in order to
496 * take part in the cluster. If it is partially valid
499 for (j = 0;j < tbp->b_xio.xio_npages; j++) {
500 if (tbp->b_xio.xio_pages[j]->valid)
503 if (j != tbp->b_xio.xio_npages) {
509 * Set a read-ahead mark as appropriate
511 if (i == 1 || i == (run - 1))
515 * Depress the priority of buffers not explicitly
518 /* tbp->b_flags |= B_AGE; */
521 * Set the block number if it isn't set, otherwise
522 * if it is make sure it matches the block number we
525 if (tbp->b_bio2.bio_offset == NOOFFSET) {
526 tbp->b_bio2.bio_offset = boffset;
527 } else if (tbp->b_bio2.bio_offset != boffset) {
534 * The passed-in tbp (i == 0) will already be set up for
535 * async or sync operation. All other tbp's acquire in
536 * our loop are set up for async operation.
538 tbp->b_cmd = BUF_CMD_READ;
540 cluster_append(&bp->b_bio1, tbp);
541 for (j = 0; j < tbp->b_xio.xio_npages; ++j) {
544 m = tbp->b_xio.xio_pages[j];
545 vm_page_busy_wait(m, FALSE, "clurpg");
548 vm_object_pip_add(m->object, 1);
549 if ((bp->b_xio.xio_npages == 0) ||
550 (bp->b_xio.xio_pages[bp->b_xio.xio_npages-1] != m)) {
551 bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m;
552 bp->b_xio.xio_npages++;
554 if ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL)
555 tbp->b_xio.xio_pages[j] = bogus_page;
558 * XXX shouldn't this be += size for both, like in
561 * Don't inherit tbp->b_bufsize as it may be larger due to
562 * a non-page-aligned size. Instead just aggregate using
565 if (tbp->b_bcount != blksize)
566 kprintf("warning: tbp->b_bcount wrong %d vs %d\n", tbp->b_bcount, blksize);
567 if (tbp->b_bufsize != blksize)
568 kprintf("warning: tbp->b_bufsize wrong %d vs %d\n", tbp->b_bufsize, blksize);
569 bp->b_bcount += blksize;
570 bp->b_bufsize += blksize;
574 * Fully valid pages in the cluster are already good and do not need
575 * to be re-read from disk. Replace the page with bogus_page
577 for (j = 0; j < bp->b_xio.xio_npages; j++) {
578 if ((bp->b_xio.xio_pages[j]->valid & VM_PAGE_BITS_ALL) ==
580 bp->b_xio.xio_pages[j] = bogus_page;
583 if (bp->b_bufsize > bp->b_kvasize) {
584 panic("cluster_rbuild: b_bufsize(%d) > b_kvasize(%d)",
585 bp->b_bufsize, bp->b_kvasize);
587 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
588 (vm_page_t *)bp->b_xio.xio_pages, bp->b_xio.xio_npages);
594 * Cleanup after a clustered read or write.
595 * This is complicated by the fact that any of the buffers might have
596 * extra memory (if there were no empty buffer headers at allocbuf time)
597 * that we will need to shift around.
599 * The returned bio is &bp->b_bio1
602 cluster_callback(struct bio *bio)
604 struct buf *bp = bio->bio_buf;
609 * Must propogate errors to all the components. A short read (EOF)
610 * is a critical error.
612 if (bp->b_flags & B_ERROR) {
614 } else if (bp->b_bcount != bp->b_bufsize) {
615 panic("cluster_callback: unexpected EOF on cluster %p!", bio);
618 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_xio.xio_npages);
620 * Move memory from the large cluster buffer into the component
621 * buffers and mark IO as done on these. Since the memory map
622 * is the same, no actual copying is required.
624 while ((tbp = bio->bio_caller_info1.cluster_head) != NULL) {
625 bio->bio_caller_info1.cluster_head = tbp->b_cluster_next;
627 tbp->b_flags |= B_ERROR | B_IODEBUG;
628 tbp->b_error = error;
630 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
631 tbp->b_flags &= ~(B_ERROR|B_INVAL);
632 tbp->b_flags |= B_IODEBUG;
634 * XXX the bdwrite()/bqrelse() issued during
635 * cluster building clears B_RELBUF (see bqrelse()
636 * comment). If direct I/O was specified, we have
637 * to restore it here to allow the buffer and VM
640 if (tbp->b_flags & B_DIRECT)
641 tbp->b_flags |= B_RELBUF;
643 biodone(&tbp->b_bio1);
645 relpbuf(bp, &cluster_pbuf_freecnt);
651 * Implement modified write build for cluster.
653 * write_behind = 0 write behind disabled
654 * write_behind = 1 write behind normal (default)
655 * write_behind = 2 write behind backed-off
659 cluster_wbuild_wb(struct vnode *vp, int blksize, off_t start_loffset, int len)
663 switch(write_behind) {
665 if (start_loffset < len)
667 start_loffset -= len;
670 r = cluster_wbuild(vp, NULL, blksize, start_loffset, len);
680 * Do clustered write for FFS.
683 * 1. Write is not sequential (write asynchronously)
684 * Write is sequential:
685 * 2. beginning of cluster - begin cluster
686 * 3. middle of a cluster - add to cluster
687 * 4. end of a cluster - asynchronously write cluster
690 cluster_write(struct buf *bp, off_t filesize, int blksize, int seqcount)
694 int maxclen, cursize;
698 if (vp->v_type == VREG)
699 async = vp->v_mount->mnt_flag & MNT_ASYNC;
702 loffset = bp->b_loffset;
703 KASSERT(bp->b_loffset != NOOFFSET,
704 ("cluster_write: no buffer offset"));
706 /* Initialize vnode to beginning of file. */
708 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
710 if (vp->v_clen == 0 || loffset != vp->v_lastw + blksize ||
711 bp->b_bio2.bio_offset == NOOFFSET ||
712 (bp->b_bio2.bio_offset != vp->v_lasta + blksize)) {
713 maxclen = vmaxiosize(vp);
714 if (vp->v_clen != 0) {
716 * Next block is not sequential.
718 * If we are not writing at end of file, the process
719 * seeked to another point in the file since its last
720 * write, or we have reached our maximum cluster size,
721 * then push the previous cluster. Otherwise try
722 * reallocating to make it sequential.
724 * Change to algorithm: only push previous cluster if
725 * it was sequential from the point of view of the
726 * seqcount heuristic, otherwise leave the buffer
727 * intact so we can potentially optimize the I/O
728 * later on in the buf_daemon or update daemon
731 cursize = vp->v_lastw - vp->v_cstart + blksize;
732 if (bp->b_loffset + blksize < filesize ||
733 loffset != vp->v_lastw + blksize || vp->v_clen <= cursize) {
734 if (!async && seqcount > 0) {
735 cluster_wbuild_wb(vp, blksize,
736 vp->v_cstart, cursize);
739 struct buf **bpp, **endbp;
740 struct cluster_save *buflist;
742 buflist = cluster_collectbufs(vp, bp, blksize);
743 endbp = &buflist->bs_children
744 [buflist->bs_nchildren - 1];
745 if (VOP_REALLOCBLKS(vp, buflist)) {
747 * Failed, push the previous cluster
748 * if *really* writing sequentially
749 * in the logical file (seqcount > 1),
750 * otherwise delay it in the hopes that
751 * the low level disk driver can
752 * optimize the write ordering.
754 for (bpp = buflist->bs_children;
757 kfree(buflist, M_SEGMENT);
759 cluster_wbuild_wb(vp,
760 blksize, vp->v_cstart,
765 * Succeeded, keep building cluster.
767 for (bpp = buflist->bs_children;
770 kfree(buflist, M_SEGMENT);
771 vp->v_lastw = loffset;
772 vp->v_lasta = bp->b_bio2.bio_offset;
778 * Consider beginning a cluster. If at end of file, make
779 * cluster as large as possible, otherwise find size of
782 if ((vp->v_type == VREG) &&
783 bp->b_loffset + blksize < filesize &&
784 (bp->b_bio2.bio_offset == NOOFFSET) &&
785 (VOP_BMAP(vp, loffset, &bp->b_bio2.bio_offset, &maxclen, NULL, BUF_CMD_WRITE) ||
786 bp->b_bio2.bio_offset == NOOFFSET)) {
789 vp->v_lasta = bp->b_bio2.bio_offset;
790 vp->v_cstart = loffset + blksize;
791 vp->v_lastw = loffset;
794 if (maxclen > blksize)
795 vp->v_clen = maxclen - blksize;
798 if (!async && vp->v_clen == 0) { /* I/O not contiguous */
799 vp->v_cstart = loffset + blksize;
801 } else { /* Wait for rest of cluster */
802 vp->v_cstart = loffset;
805 } else if (loffset == vp->v_cstart + vp->v_clen) {
807 * At end of cluster, write it out if seqcount tells us we
808 * are operating sequentially, otherwise let the buf or
809 * update daemon handle it.
813 cluster_wbuild_wb(vp, blksize, vp->v_cstart,
814 vp->v_clen + blksize);
816 vp->v_cstart = loffset + blksize;
817 } else if (vm_page_count_severe()) {
819 * We are low on memory, get it going NOW
824 * In the middle of a cluster, so just delay the I/O for now.
828 vp->v_lastw = loffset;
829 vp->v_lasta = bp->b_bio2.bio_offset;
833 * This is the clustered version of bawrite(). It works similarly to
834 * cluster_write() except I/O on the buffer is guaranteed to occur.
837 cluster_awrite(struct buf *bp)
842 * Don't bother if it isn't clusterable.
844 if ((bp->b_flags & B_CLUSTEROK) == 0 ||
846 (bp->b_vp->v_flag & VOBJBUF) == 0) {
847 total = bp->b_bufsize;
852 total = cluster_wbuild(bp->b_vp, &bp, bp->b_bufsize,
853 bp->b_loffset, vmaxiosize(bp->b_vp));
861 * This is an awful lot like cluster_rbuild...wish they could be combined.
862 * The last lbn argument is the current block on which I/O is being
863 * performed. Check to see that it doesn't fall in the middle of
864 * the current block (if last_bp == NULL).
866 * cluster_wbuild() normally does not guarantee anything. If bpp is
867 * non-NULL and cluster_wbuild() is able to incorporate it into the
868 * I/O it will set *bpp to NULL, otherwise it will leave it alone and
869 * the caller must dispose of *bpp.
872 cluster_wbuild(struct vnode *vp, struct buf **bpp,
873 int blksize, off_t start_loffset, int bytes)
875 struct buf *bp, *tbp;
877 int totalwritten = 0;
879 int maxiosize = vmaxiosize(vp);
883 * If the buffer matches the passed locked & removed buffer
884 * we used the passed buffer (which might not be B_DELWRI).
886 * Otherwise locate the buffer and determine if it is
889 if (bpp && (*bpp)->b_loffset == start_loffset) {
894 tbp = findblk(vp, start_loffset, FINDBLK_NBLOCK);
896 (tbp->b_flags & (B_LOCKED | B_INVAL | B_DELWRI)) !=
898 (LIST_FIRST(&tbp->b_dep) && buf_checkwrite(tbp))) {
901 start_loffset += blksize;
907 KKASSERT(tbp->b_cmd == BUF_CMD_DONE);
910 * Extra memory in the buffer, punt on this buffer.
911 * XXX we could handle this in most cases, but we would
912 * have to push the extra memory down to after our max
913 * possible cluster size and then potentially pull it back
914 * up if the cluster was terminated prematurely--too much
917 if (((tbp->b_flags & (B_CLUSTEROK|B_MALLOC)) != B_CLUSTEROK) ||
918 (tbp->b_bcount != tbp->b_bufsize) ||
919 (tbp->b_bcount != blksize) ||
920 (bytes == blksize) ||
921 ((bp = getpbuf_kva(&cluster_pbuf_freecnt)) == NULL)) {
922 totalwritten += tbp->b_bufsize;
924 start_loffset += blksize;
930 * Set up the pbuf. Track our append point with b_bcount
931 * and b_bufsize. b_bufsize is not used by the device but
932 * our caller uses it to loop clusters and we use it to
933 * detect a premature EOF on the block device.
937 bp->b_xio.xio_npages = 0;
938 bp->b_loffset = tbp->b_loffset;
939 bp->b_bio2.bio_offset = tbp->b_bio2.bio_offset;
942 * We are synthesizing a buffer out of vm_page_t's, but
943 * if the block size is not page aligned then the starting
944 * address may not be either. Inherit the b_data offset
945 * from the original buffer.
947 bp->b_data = (char *)((vm_offset_t)bp->b_data |
948 ((vm_offset_t)tbp->b_data & PAGE_MASK));
949 bp->b_flags &= ~B_ERROR;
950 bp->b_flags |= B_CLUSTER | B_BNOCLIP |
951 (tbp->b_flags & (B_VMIO | B_NEEDCOMMIT));
952 bp->b_bio1.bio_caller_info1.cluster_head = NULL;
953 bp->b_bio1.bio_caller_info2.cluster_tail = NULL;
956 * From this location in the file, scan forward to see
957 * if there are buffers with adjacent data that need to
958 * be written as well.
960 * IO *must* be initiated on index 0 at this point
961 * (particularly when called from cluster_awrite()).
963 for (i = 0; i < bytes; (i += blksize), (start_loffset += blksize)) {
971 tbp = findblk(vp, start_loffset,
974 * Buffer not found or could not be locked
981 * If it IS in core, but has different
982 * characteristics, then don't cluster
985 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
986 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
987 != (B_DELWRI | B_CLUSTEROK |
988 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
989 (tbp->b_flags & B_LOCKED)
996 * Check that the combined cluster
997 * would make sense with regard to pages
998 * and would not be too large
1000 * WARNING! buf_checkwrite() must be the last
1001 * check made. If it returns 0 then
1002 * we must initiate the I/O.
1004 if ((tbp->b_bcount != blksize) ||
1005 ((bp->b_bio2.bio_offset + i) !=
1006 tbp->b_bio2.bio_offset) ||
1007 ((tbp->b_xio.xio_npages + bp->b_xio.xio_npages) >
1008 (maxiosize / PAGE_SIZE)) ||
1009 (LIST_FIRST(&tbp->b_dep) &&
1010 buf_checkwrite(tbp))
1015 if (LIST_FIRST(&tbp->b_dep))
1018 * Ok, it's passed all the tests,
1019 * so remove it from the free list
1020 * and mark it busy. We will use it.
1023 KKASSERT(tbp->b_cmd == BUF_CMD_DONE);
1027 * If the IO is via the VM then we do some
1028 * special VM hackery (yuck). Since the buffer's
1029 * block size may not be page-aligned it is possible
1030 * for a page to be shared between two buffers. We
1031 * have to get rid of the duplication when building
1034 if (tbp->b_flags & B_VMIO) {
1038 * Try to avoid deadlocks with the VM system.
1039 * However, we cannot abort the I/O if
1040 * must_initiate is non-zero.
1042 if (must_initiate == 0) {
1044 j < tbp->b_xio.xio_npages;
1046 m = tbp->b_xio.xio_pages[j];
1047 if (m->flags & PG_BUSY) {
1054 for (j = 0; j < tbp->b_xio.xio_npages; ++j) {
1055 m = tbp->b_xio.xio_pages[j];
1056 vm_page_busy_wait(m, FALSE, "clurpg");
1057 vm_page_io_start(m);
1059 vm_object_pip_add(m->object, 1);
1060 if ((bp->b_xio.xio_npages == 0) ||
1061 (bp->b_xio.xio_pages[bp->b_xio.xio_npages - 1] != m)) {
1062 bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m;
1063 bp->b_xio.xio_npages++;
1067 bp->b_bcount += blksize;
1068 bp->b_bufsize += blksize;
1071 tbp->b_flags &= ~B_ERROR;
1072 tbp->b_cmd = BUF_CMD_WRITE;
1074 cluster_append(&bp->b_bio1, tbp);
1077 * check for latent dependencies to be handled
1079 if (LIST_FIRST(&tbp->b_dep) != NULL)
1083 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
1084 (vm_page_t *)bp->b_xio.xio_pages,
1085 bp->b_xio.xio_npages);
1086 if (bp->b_bufsize > bp->b_kvasize) {
1087 panic("cluster_wbuild: b_bufsize(%d) "
1088 "> b_kvasize(%d)\n",
1089 bp->b_bufsize, bp->b_kvasize);
1091 totalwritten += bp->b_bufsize;
1093 bp->b_dirtyend = bp->b_bufsize;
1094 bp->b_bio1.bio_done = cluster_callback;
1095 bp->b_cmd = BUF_CMD_WRITE;
1097 vfs_busy_pages(vp, bp);
1098 bsetrunningbufspace(bp, bp->b_bufsize);
1100 vn_strategy(vp, &bp->b_bio1);
1104 return totalwritten;
1108 * Collect together all the buffers in a cluster.
1109 * Plus add one additional buffer.
1111 static struct cluster_save *
1112 cluster_collectbufs(struct vnode *vp, struct buf *last_bp, int blksize)
1114 struct cluster_save *buflist;
1119 len = (int)(vp->v_lastw - vp->v_cstart + blksize) / blksize;
1120 buflist = kmalloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
1121 M_SEGMENT, M_WAITOK);
1122 buflist->bs_nchildren = 0;
1123 buflist->bs_children = (struct buf **) (buflist + 1);
1124 for (loffset = vp->v_cstart, i = 0; i < len; (loffset += blksize), i++) {
1125 (void) bread(vp, loffset, last_bp->b_bcount, &bp);
1126 buflist->bs_children[i] = bp;
1127 if (bp->b_bio2.bio_offset == NOOFFSET) {
1128 VOP_BMAP(bp->b_vp, bp->b_loffset,
1129 &bp->b_bio2.bio_offset,
1130 NULL, NULL, BUF_CMD_WRITE);
1133 buflist->bs_children[i] = bp = last_bp;
1134 if (bp->b_bio2.bio_offset == NOOFFSET) {
1135 VOP_BMAP(bp->b_vp, bp->b_loffset, &bp->b_bio2.bio_offset,
1136 NULL, NULL, BUF_CMD_WRITE);
1138 buflist->bs_nchildren = i + 1;
1143 cluster_append(struct bio *bio, struct buf *tbp)
1145 tbp->b_cluster_next = NULL;
1146 if (bio->bio_caller_info1.cluster_head == NULL) {
1147 bio->bio_caller_info1.cluster_head = tbp;
1148 bio->bio_caller_info2.cluster_tail = tbp;
1150 bio->bio_caller_info2.cluster_tail->b_cluster_next = tbp;
1151 bio->bio_caller_info2.cluster_tail = tbp;
1157 cluster_setram (struct buf *bp)
1159 bp->b_flags |= B_RAM;
1160 if (bp->b_xio.xio_npages)
1161 vm_page_flag_set(bp->b_xio.xio_pages[0], PG_RAM);