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>
57 #include <vm/vm_page2.h>
59 #include <machine/limits.h>
61 #if defined(CLUSTERDEBUG)
62 #include <sys/sysctl.h>
63 static int rcluster= 0;
64 SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0, "");
67 static MALLOC_DEFINE(M_SEGMENT, "cluster_save", "cluster_save buffer");
69 static struct cluster_save *
70 cluster_collectbufs (struct vnode *vp, struct buf *last_bp,
73 cluster_rbuild (struct vnode *vp, off_t filesize, off_t loffset,
74 off_t doffset, int blksize, int run,
76 static void cluster_callback (struct bio *);
77 static void cluster_setram (struct buf *);
79 static int write_behind = 1;
80 SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0, "");
81 static int max_readahead = 2 * 1024 * 1024;
82 SYSCTL_INT(_vfs, OID_AUTO, max_readahead, CTLFLAG_RW, &max_readahead, 0, "");
84 extern vm_page_t bogus_page;
86 extern int cluster_pbuf_freecnt;
89 * This replaces bread.
91 * filesize - read-ahead @ blksize will not cross this boundary
92 * loffset - loffset for returned *bpp
93 * blksize - blocksize for returned *bpp and read-ahead bps
94 * minreq - minimum (not a hard minimum) in bytes, typically reflects
95 * a higher level uio resid.
96 * maxreq - maximum (sequential heuristic) in bytes (highet typ ~2MB)
97 * bpp - return buffer (*bpp) for (loffset,blksize)
100 cluster_read(struct vnode *vp, off_t filesize, off_t loffset,
101 int blksize, size_t minreq, size_t maxreq, struct buf **bpp)
103 struct buf *bp, *rbp, *reqbp;
114 * Calculate the desired read-ahead in blksize'd blocks (maxra).
115 * To do this we calculate maxreq.
117 * maxreq typically starts out as a sequential heuristic. If the
118 * high level uio/resid is bigger (minreq), we pop maxreq up to
119 * minreq. This represents the case where random I/O is being
120 * performed by the userland is issuing big read()'s.
122 * Then we limit maxreq to max_readahead to ensure it is a reasonable
125 * Finally we must ensure that (loffset + maxreq) does not cross the
126 * boundary (filesize) for the current blocksize. If we allowed it
127 * to cross we could end up with buffers past the boundary with the
128 * wrong block size (HAMMER large-data areas use mixed block sizes).
129 * minreq is also absolutely limited to filesize.
133 /* minreq not used beyond this point */
135 if (maxreq > max_readahead) {
136 maxreq = max_readahead;
137 if (maxreq > 16 * 1024 * 1024)
138 maxreq = 16 * 1024 * 1024;
140 if (maxreq < blksize)
142 if (loffset + maxreq > filesize) {
143 if (loffset > filesize)
146 maxreq = filesize - loffset;
149 maxra = (int)(maxreq / blksize);
152 * Get the requested block.
154 *bpp = reqbp = bp = getblk(vp, loffset, blksize, 0, 0);
155 origoffset = loffset;
158 * Calculate the maximum cluster size for a single I/O, used
159 * by cluster_rbuild().
161 maxrbuild = vmaxiosize(vp) / blksize;
164 * if it is in the cache, then check to see if the reads have been
165 * sequential. If they have, then try some read-ahead, otherwise
166 * back-off on prospective read-aheads.
168 if (bp->b_flags & B_CACHE) {
170 * Not sequential, do not do any read-ahead
176 * No read-ahead mark, do not do any read-ahead
179 if ((bp->b_flags & B_RAM) == 0)
183 * We hit a read-ahead-mark, figure out how much read-ahead
184 * to do (maxra) and where to start (loffset).
186 * Shortcut the scan. Typically the way this works is that
187 * we've built up all the blocks inbetween except for the
188 * last in previous iterations, so if the second-to-last
189 * block is present we just skip ahead to it.
191 * This algorithm has O(1) cpu in the steady state no
192 * matter how large maxra is.
194 bp->b_flags &= ~B_RAM;
196 if (findblk(vp, loffset + (maxra - 2) * blksize, FINDBLK_TEST))
201 if (findblk(vp, loffset + i * blksize,
202 FINDBLK_TEST) == NULL) {
209 * We got everything or everything is in the cache, no
215 loffset += i * blksize;
218 __debugvar off_t firstread = bp->b_loffset;
222 * Set-up synchronous read for bp.
224 bp->b_cmd = BUF_CMD_READ;
225 bp->b_bio1.bio_done = biodone_sync;
226 bp->b_bio1.bio_flags |= BIO_SYNC;
228 KASSERT(firstread != NOOFFSET,
229 ("cluster_read: no buffer offset"));
232 * nblks is our cluster_rbuild request size, limited
233 * primarily by the device.
235 if ((nblks = maxra) > maxrbuild)
241 error = VOP_BMAP(vp, loffset, &doffset,
242 &burstbytes, NULL, BUF_CMD_READ);
244 goto single_block_read;
245 if (nblks > burstbytes / blksize)
246 nblks = burstbytes / blksize;
247 if (doffset == NOOFFSET)
248 goto single_block_read;
250 goto single_block_read;
252 bp = cluster_rbuild(vp, filesize, loffset,
253 doffset, blksize, nblks, bp);
254 loffset += bp->b_bufsize;
255 maxra -= bp->b_bufsize / blksize;
259 * If it isn't in the cache, then get a chunk from
260 * disk if sequential, otherwise just get the block.
269 * If B_CACHE was not set issue bp. bp will either be an
270 * asynchronous cluster buf or a synchronous single-buf.
271 * If it is a single buf it will be the same as reqbp.
273 * NOTE: Once an async cluster buf is issued bp becomes invalid.
276 #if defined(CLUSTERDEBUG)
278 kprintf("S(%012jx,%d,%d)\n",
279 (intmax_t)bp->b_loffset, bp->b_bcount, maxra);
281 if ((bp->b_flags & B_CLUSTER) == 0)
282 vfs_busy_pages(vp, bp);
283 bp->b_flags &= ~(B_ERROR|B_INVAL);
284 vn_strategy(vp, &bp->b_bio1);
290 * If we have been doing sequential I/O, then do some read-ahead.
291 * The code above us should have positioned us at the next likely
294 * Only mess with buffers which we can immediately lock. HAMMER
295 * will do device-readahead irrespective of what the blocks
298 while (error == 0 && maxra > 0) {
303 rbp = getblk(vp, loffset, blksize,
304 GETBLK_SZMATCH|GETBLK_NOWAIT, 0);
307 if ((rbp->b_flags & B_CACHE)) {
313 * An error from the read-ahead bmap has nothing to do
314 * with the caller's original request.
316 tmp_error = VOP_BMAP(vp, loffset, &doffset,
317 &burstbytes, NULL, BUF_CMD_READ);
318 if (tmp_error || doffset == NOOFFSET) {
319 rbp->b_flags |= B_INVAL;
324 if ((nblks = maxra) > maxrbuild)
326 if (nblks > burstbytes / blksize)
327 nblks = burstbytes / blksize;
332 rbp->b_cmd = BUF_CMD_READ;
333 /*rbp->b_flags |= B_AGE*/;
337 rbp = cluster_rbuild(vp, filesize, loffset,
341 rbp->b_bio2.bio_offset = doffset;
344 #if defined(CLUSTERDEBUG)
347 kprintf("A+(%012jx,%d,%jd) "
348 "doff=%012jx minr=%zd ra=%d\n",
349 (intmax_t)loffset, rbp->b_bcount,
350 (intmax_t)(loffset - origoffset),
351 (intmax_t)doffset, minreq, maxra);
353 kprintf("A-(%012jx,%d,%jd) "
354 "doff=%012jx minr=%zd ra=%d\n",
355 (intmax_t)rbp->b_loffset, rbp->b_bcount,
356 (intmax_t)(loffset - origoffset),
357 (intmax_t)doffset, minreq, maxra);
361 rbp->b_flags &= ~(B_ERROR|B_INVAL);
363 if ((rbp->b_flags & B_CLUSTER) == 0)
364 vfs_busy_pages(vp, rbp);
366 loffset += rbp->b_bufsize;
367 maxra -= rbp->b_bufsize / blksize;
368 vn_strategy(vp, &rbp->b_bio1);
369 /* rbp invalid now */
373 * Wait for our original buffer to complete its I/O. reqbp will
374 * be NULL if the original buffer was B_CACHE. We are returning
375 * (*bpp) which is the same as reqbp when reqbp != NULL.
379 KKASSERT(reqbp->b_bio1.bio_flags & BIO_SYNC);
380 error = biowait(&reqbp->b_bio1, "clurd");
386 * If blocks are contiguous on disk, use this to provide clustered
387 * read ahead. We will read as many blocks as possible sequentially
388 * and then parcel them up into logical blocks in the buffer hash table.
390 * This function either returns a cluster buf or it returns fbp. fbp is
391 * already expected to be set up as a synchronous or asynchronous request.
393 * If a cluster buf is returned it will always be async.
396 cluster_rbuild(struct vnode *vp, off_t filesize, off_t loffset, off_t doffset,
397 int blksize, int run, struct buf *fbp)
399 struct buf *bp, *tbp;
402 int maxiosize = vmaxiosize(vp);
407 while (loffset + run * blksize > filesize) {
412 tbp->b_bio2.bio_offset = doffset;
413 if((tbp->b_flags & B_MALLOC) ||
414 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1)) {
418 bp = trypbuf_kva(&cluster_pbuf_freecnt);
424 * We are synthesizing a buffer out of vm_page_t's, but
425 * if the block size is not page aligned then the starting
426 * address may not be either. Inherit the b_data offset
427 * from the original buffer.
429 bp->b_data = (char *)((vm_offset_t)bp->b_data |
430 ((vm_offset_t)tbp->b_data & PAGE_MASK));
431 bp->b_flags |= B_CLUSTER | B_VMIO;
432 bp->b_cmd = BUF_CMD_READ;
433 bp->b_bio1.bio_done = cluster_callback; /* default to async */
434 bp->b_bio1.bio_caller_info1.cluster_head = NULL;
435 bp->b_bio1.bio_caller_info2.cluster_tail = NULL;
436 bp->b_loffset = loffset;
437 bp->b_bio2.bio_offset = doffset;
438 KASSERT(bp->b_loffset != NOOFFSET,
439 ("cluster_rbuild: no buffer offset"));
443 bp->b_xio.xio_npages = 0;
445 for (boffset = doffset, i = 0; i < run; ++i, boffset += blksize) {
447 if ((bp->b_xio.xio_npages * PAGE_SIZE) +
448 round_page(blksize) > maxiosize) {
453 * Shortcut some checks and try to avoid buffers that
454 * would block in the lock. The same checks have to
455 * be made again after we officially get the buffer.
457 tbp = getblk(vp, loffset + i * blksize, blksize,
458 GETBLK_SZMATCH|GETBLK_NOWAIT, 0);
461 for (j = 0; j < tbp->b_xio.xio_npages; j++) {
462 if (tbp->b_xio.xio_pages[j]->valid)
465 if (j != tbp->b_xio.xio_npages) {
471 * Stop scanning if the buffer is fuly valid
472 * (marked B_CACHE), or locked (may be doing a
473 * background write), or if the buffer is not
474 * VMIO backed. The clustering code can only deal
475 * with VMIO-backed buffers.
477 if ((tbp->b_flags & (B_CACHE|B_LOCKED)) ||
478 (tbp->b_flags & B_VMIO) == 0 ||
479 (LIST_FIRST(&tbp->b_dep) != NULL &&
487 * The buffer must be completely invalid in order to
488 * take part in the cluster. If it is partially valid
491 for (j = 0;j < tbp->b_xio.xio_npages; j++) {
492 if (tbp->b_xio.xio_pages[j]->valid)
495 if (j != tbp->b_xio.xio_npages) {
501 * Set a read-ahead mark as appropriate
503 if (i == 1 || i == (run - 1))
507 * Depress the priority of buffers not explicitly
510 /* tbp->b_flags |= B_AGE; */
513 * Set the block number if it isn't set, otherwise
514 * if it is make sure it matches the block number we
517 if (tbp->b_bio2.bio_offset == NOOFFSET) {
518 tbp->b_bio2.bio_offset = boffset;
519 } else if (tbp->b_bio2.bio_offset != boffset) {
526 * The passed-in tbp (i == 0) will already be set up for
527 * async or sync operation. All other tbp's acquire in
528 * our loop are set up for async operation.
530 tbp->b_cmd = BUF_CMD_READ;
532 cluster_append(&bp->b_bio1, tbp);
533 for (j = 0; j < tbp->b_xio.xio_npages; ++j) {
535 m = tbp->b_xio.xio_pages[j];
537 vm_object_pip_add(m->object, 1);
538 if ((bp->b_xio.xio_npages == 0) ||
539 (bp->b_xio.xio_pages[bp->b_xio.xio_npages-1] != m)) {
540 bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m;
541 bp->b_xio.xio_npages++;
543 if ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL)
544 tbp->b_xio.xio_pages[j] = bogus_page;
547 * XXX shouldn't this be += size for both, like in
550 * Don't inherit tbp->b_bufsize as it may be larger due to
551 * a non-page-aligned size. Instead just aggregate using
554 if (tbp->b_bcount != blksize)
555 kprintf("warning: tbp->b_bcount wrong %d vs %d\n", tbp->b_bcount, blksize);
556 if (tbp->b_bufsize != blksize)
557 kprintf("warning: tbp->b_bufsize wrong %d vs %d\n", tbp->b_bufsize, blksize);
558 bp->b_bcount += blksize;
559 bp->b_bufsize += blksize;
563 * Fully valid pages in the cluster are already good and do not need
564 * to be re-read from disk. Replace the page with bogus_page
566 for (j = 0; j < bp->b_xio.xio_npages; j++) {
567 if ((bp->b_xio.xio_pages[j]->valid & VM_PAGE_BITS_ALL) ==
569 bp->b_xio.xio_pages[j] = bogus_page;
572 if (bp->b_bufsize > bp->b_kvasize) {
573 panic("cluster_rbuild: b_bufsize(%d) > b_kvasize(%d)",
574 bp->b_bufsize, bp->b_kvasize);
576 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
577 (vm_page_t *)bp->b_xio.xio_pages, bp->b_xio.xio_npages);
583 * Cleanup after a clustered read or write.
584 * This is complicated by the fact that any of the buffers might have
585 * extra memory (if there were no empty buffer headers at allocbuf time)
586 * that we will need to shift around.
588 * The returned bio is &bp->b_bio1
591 cluster_callback(struct bio *bio)
593 struct buf *bp = bio->bio_buf;
598 * Must propogate errors to all the components. A short read (EOF)
599 * is a critical error.
601 if (bp->b_flags & B_ERROR) {
603 } else if (bp->b_bcount != bp->b_bufsize) {
604 panic("cluster_callback: unexpected EOF on cluster %p!", bio);
607 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_xio.xio_npages);
609 * Move memory from the large cluster buffer into the component
610 * buffers and mark IO as done on these. Since the memory map
611 * is the same, no actual copying is required.
613 while ((tbp = bio->bio_caller_info1.cluster_head) != NULL) {
614 bio->bio_caller_info1.cluster_head = tbp->b_cluster_next;
616 tbp->b_flags |= B_ERROR | B_IODEBUG;
617 tbp->b_error = error;
619 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
620 tbp->b_flags &= ~(B_ERROR|B_INVAL);
621 tbp->b_flags |= B_IODEBUG;
623 * XXX the bdwrite()/bqrelse() issued during
624 * cluster building clears B_RELBUF (see bqrelse()
625 * comment). If direct I/O was specified, we have
626 * to restore it here to allow the buffer and VM
629 if (tbp->b_flags & B_DIRECT)
630 tbp->b_flags |= B_RELBUF;
632 biodone(&tbp->b_bio1);
634 relpbuf(bp, &cluster_pbuf_freecnt);
640 * Implement modified write build for cluster.
642 * write_behind = 0 write behind disabled
643 * write_behind = 1 write behind normal (default)
644 * write_behind = 2 write behind backed-off
648 cluster_wbuild_wb(struct vnode *vp, int blksize, off_t start_loffset, int len)
652 switch(write_behind) {
654 if (start_loffset < len)
656 start_loffset -= len;
659 r = cluster_wbuild(vp, blksize, start_loffset, len);
669 * Do clustered write for FFS.
672 * 1. Write is not sequential (write asynchronously)
673 * Write is sequential:
674 * 2. beginning of cluster - begin cluster
675 * 3. middle of a cluster - add to cluster
676 * 4. end of a cluster - asynchronously write cluster
679 cluster_write(struct buf *bp, off_t filesize, int blksize, int seqcount)
683 int maxclen, cursize;
687 if (vp->v_type == VREG)
688 async = vp->v_mount->mnt_flag & MNT_ASYNC;
691 loffset = bp->b_loffset;
692 KASSERT(bp->b_loffset != NOOFFSET,
693 ("cluster_write: no buffer offset"));
695 /* Initialize vnode to beginning of file. */
697 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
699 if (vp->v_clen == 0 || loffset != vp->v_lastw + blksize ||
700 bp->b_bio2.bio_offset == NOOFFSET ||
701 (bp->b_bio2.bio_offset != vp->v_lasta + blksize)) {
702 maxclen = vmaxiosize(vp);
703 if (vp->v_clen != 0) {
705 * Next block is not sequential.
707 * If we are not writing at end of file, the process
708 * seeked to another point in the file since its last
709 * write, or we have reached our maximum cluster size,
710 * then push the previous cluster. Otherwise try
711 * reallocating to make it sequential.
713 * Change to algorithm: only push previous cluster if
714 * it was sequential from the point of view of the
715 * seqcount heuristic, otherwise leave the buffer
716 * intact so we can potentially optimize the I/O
717 * later on in the buf_daemon or update daemon
720 cursize = vp->v_lastw - vp->v_cstart + blksize;
721 if (bp->b_loffset + blksize != filesize ||
722 loffset != vp->v_lastw + blksize || vp->v_clen <= cursize) {
723 if (!async && seqcount > 0) {
724 cluster_wbuild_wb(vp, blksize,
725 vp->v_cstart, cursize);
728 struct buf **bpp, **endbp;
729 struct cluster_save *buflist;
731 buflist = cluster_collectbufs(vp, bp, blksize);
732 endbp = &buflist->bs_children
733 [buflist->bs_nchildren - 1];
734 if (VOP_REALLOCBLKS(vp, buflist)) {
736 * Failed, push the previous cluster
737 * if *really* writing sequentially
738 * in the logical file (seqcount > 1),
739 * otherwise delay it in the hopes that
740 * the low level disk driver can
741 * optimize the write ordering.
743 for (bpp = buflist->bs_children;
746 kfree(buflist, M_SEGMENT);
748 cluster_wbuild_wb(vp,
749 blksize, vp->v_cstart,
754 * Succeeded, keep building cluster.
756 for (bpp = buflist->bs_children;
759 kfree(buflist, M_SEGMENT);
760 vp->v_lastw = loffset;
761 vp->v_lasta = bp->b_bio2.bio_offset;
767 * Consider beginning a cluster. If at end of file, make
768 * cluster as large as possible, otherwise find size of
771 if ((vp->v_type == VREG) &&
772 bp->b_loffset + blksize != filesize &&
773 (bp->b_bio2.bio_offset == NOOFFSET) &&
774 (VOP_BMAP(vp, loffset, &bp->b_bio2.bio_offset, &maxclen, NULL, BUF_CMD_WRITE) ||
775 bp->b_bio2.bio_offset == NOOFFSET)) {
778 vp->v_lasta = bp->b_bio2.bio_offset;
779 vp->v_cstart = loffset + blksize;
780 vp->v_lastw = loffset;
783 if (maxclen > blksize)
784 vp->v_clen = maxclen - blksize;
787 if (!async && vp->v_clen == 0) { /* I/O not contiguous */
788 vp->v_cstart = loffset + blksize;
790 } else { /* Wait for rest of cluster */
791 vp->v_cstart = loffset;
794 } else if (loffset == vp->v_cstart + vp->v_clen) {
796 * At end of cluster, write it out if seqcount tells us we
797 * are operating sequentially, otherwise let the buf or
798 * update daemon handle it.
802 cluster_wbuild_wb(vp, blksize, vp->v_cstart,
803 vp->v_clen + blksize);
805 vp->v_cstart = loffset + blksize;
806 } else if (vm_page_count_severe()) {
808 * We are low on memory, get it going NOW
813 * In the middle of a cluster, so just delay the I/O for now.
817 vp->v_lastw = loffset;
818 vp->v_lasta = bp->b_bio2.bio_offset;
823 * This is an awful lot like cluster_rbuild...wish they could be combined.
824 * The last lbn argument is the current block on which I/O is being
825 * performed. Check to see that it doesn't fall in the middle of
826 * the current block (if last_bp == NULL).
829 cluster_wbuild(struct vnode *vp, int blksize, off_t start_loffset, int bytes)
831 struct buf *bp, *tbp;
833 int totalwritten = 0;
834 int maxiosize = vmaxiosize(vp);
838 * If the buffer is not delayed-write (i.e. dirty), or it
839 * is delayed-write but either locked or inval, it cannot
840 * partake in the clustered write.
842 tbp = findblk(vp, start_loffset, FINDBLK_NBLOCK);
844 (tbp->b_flags & (B_LOCKED | B_INVAL | B_DELWRI)) != B_DELWRI ||
845 (LIST_FIRST(&tbp->b_dep) && buf_checkwrite(tbp))) {
848 start_loffset += blksize;
853 KKASSERT(tbp->b_cmd == BUF_CMD_DONE);
856 * Extra memory in the buffer, punt on this buffer.
857 * XXX we could handle this in most cases, but we would
858 * have to push the extra memory down to after our max
859 * possible cluster size and then potentially pull it back
860 * up if the cluster was terminated prematurely--too much
863 if (((tbp->b_flags & (B_CLUSTEROK|B_MALLOC)) != B_CLUSTEROK) ||
864 (tbp->b_bcount != tbp->b_bufsize) ||
865 (tbp->b_bcount != blksize) ||
866 (bytes == blksize) ||
867 ((bp = getpbuf_kva(&cluster_pbuf_freecnt)) == NULL)) {
868 totalwritten += tbp->b_bufsize;
870 start_loffset += blksize;
876 * Set up the pbuf. Track our append point with b_bcount
877 * and b_bufsize. b_bufsize is not used by the device but
878 * our caller uses it to loop clusters and we use it to
879 * detect a premature EOF on the block device.
883 bp->b_xio.xio_npages = 0;
884 bp->b_loffset = tbp->b_loffset;
885 bp->b_bio2.bio_offset = tbp->b_bio2.bio_offset;
888 * We are synthesizing a buffer out of vm_page_t's, but
889 * if the block size is not page aligned then the starting
890 * address may not be either. Inherit the b_data offset
891 * from the original buffer.
893 bp->b_data = (char *)((vm_offset_t)bp->b_data |
894 ((vm_offset_t)tbp->b_data & PAGE_MASK));
895 bp->b_flags &= ~B_ERROR;
896 bp->b_flags |= B_CLUSTER | B_BNOCLIP |
897 (tbp->b_flags & (B_VMIO | B_NEEDCOMMIT));
898 bp->b_bio1.bio_caller_info1.cluster_head = NULL;
899 bp->b_bio1.bio_caller_info2.cluster_tail = NULL;
902 * From this location in the file, scan forward to see
903 * if there are buffers with adjacent data that need to
904 * be written as well.
906 for (i = 0; i < bytes; (i += blksize), (start_loffset += blksize)) {
907 if (i != 0) { /* If not the first buffer */
908 tbp = findblk(vp, start_loffset,
911 * Buffer not found or could not be locked
918 * If it IS in core, but has different
919 * characteristics, then don't cluster
922 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
923 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
924 != (B_DELWRI | B_CLUSTEROK |
925 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
926 (tbp->b_flags & B_LOCKED) ||
927 (LIST_FIRST(&tbp->b_dep) &&
935 * Check that the combined cluster
936 * would make sense with regard to pages
937 * and would not be too large
939 if ((tbp->b_bcount != blksize) ||
940 ((bp->b_bio2.bio_offset + i) !=
941 tbp->b_bio2.bio_offset) ||
942 ((tbp->b_xio.xio_npages + bp->b_xio.xio_npages) >
943 (maxiosize / PAGE_SIZE))) {
948 * Ok, it's passed all the tests,
949 * so remove it from the free list
950 * and mark it busy. We will use it.
953 KKASSERT(tbp->b_cmd == BUF_CMD_DONE);
954 } /* end of code for non-first buffers only */
957 * If the IO is via the VM then we do some
958 * special VM hackery (yuck). Since the buffer's
959 * block size may not be page-aligned it is possible
960 * for a page to be shared between two buffers. We
961 * have to get rid of the duplication when building
964 if (tbp->b_flags & B_VMIO) {
967 if (i != 0) { /* if not first buffer */
968 for (j = 0; j < tbp->b_xio.xio_npages; ++j) {
969 m = tbp->b_xio.xio_pages[j];
970 if (m->flags & PG_BUSY) {
977 for (j = 0; j < tbp->b_xio.xio_npages; ++j) {
978 m = tbp->b_xio.xio_pages[j];
980 vm_object_pip_add(m->object, 1);
981 if ((bp->b_xio.xio_npages == 0) ||
982 (bp->b_xio.xio_pages[bp->b_xio.xio_npages - 1] != m)) {
983 bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m;
984 bp->b_xio.xio_npages++;
988 bp->b_bcount += blksize;
989 bp->b_bufsize += blksize;
992 tbp->b_flags &= ~B_ERROR;
993 tbp->b_cmd = BUF_CMD_WRITE;
995 cluster_append(&bp->b_bio1, tbp);
998 * check for latent dependencies to be handled
1000 if (LIST_FIRST(&tbp->b_dep) != NULL)
1004 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
1005 (vm_page_t *) bp->b_xio.xio_pages, bp->b_xio.xio_npages);
1006 if (bp->b_bufsize > bp->b_kvasize) {
1008 "cluster_wbuild: b_bufsize(%d) > b_kvasize(%d)\n",
1009 bp->b_bufsize, bp->b_kvasize);
1011 totalwritten += bp->b_bufsize;
1013 bp->b_dirtyend = bp->b_bufsize;
1014 bp->b_bio1.bio_done = cluster_callback;
1015 bp->b_cmd = BUF_CMD_WRITE;
1017 vfs_busy_pages(vp, bp);
1018 bsetrunningbufspace(bp, bp->b_bufsize);
1020 vn_strategy(vp, &bp->b_bio1);
1024 return totalwritten;
1028 * Collect together all the buffers in a cluster.
1029 * Plus add one additional buffer.
1031 static struct cluster_save *
1032 cluster_collectbufs(struct vnode *vp, struct buf *last_bp, int blksize)
1034 struct cluster_save *buflist;
1039 len = (int)(vp->v_lastw - vp->v_cstart + blksize) / blksize;
1040 buflist = kmalloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
1041 M_SEGMENT, M_WAITOK);
1042 buflist->bs_nchildren = 0;
1043 buflist->bs_children = (struct buf **) (buflist + 1);
1044 for (loffset = vp->v_cstart, i = 0; i < len; (loffset += blksize), i++) {
1045 (void) bread(vp, loffset, last_bp->b_bcount, &bp);
1046 buflist->bs_children[i] = bp;
1047 if (bp->b_bio2.bio_offset == NOOFFSET) {
1048 VOP_BMAP(bp->b_vp, bp->b_loffset,
1049 &bp->b_bio2.bio_offset,
1050 NULL, NULL, BUF_CMD_WRITE);
1053 buflist->bs_children[i] = bp = last_bp;
1054 if (bp->b_bio2.bio_offset == NOOFFSET) {
1055 VOP_BMAP(bp->b_vp, bp->b_loffset, &bp->b_bio2.bio_offset,
1056 NULL, NULL, BUF_CMD_WRITE);
1058 buflist->bs_nchildren = i + 1;
1063 cluster_append(struct bio *bio, struct buf *tbp)
1065 tbp->b_cluster_next = NULL;
1066 if (bio->bio_caller_info1.cluster_head == NULL) {
1067 bio->bio_caller_info1.cluster_head = tbp;
1068 bio->bio_caller_info2.cluster_tail = tbp;
1070 bio->bio_caller_info2.cluster_tail->b_cluster_next = tbp;
1071 bio->bio_caller_info2.cluster_tail = tbp;
1077 cluster_setram (struct buf *bp)
1079 bp->b_flags |= B_RAM;
1080 if (bp->b_xio.xio_npages)
1081 vm_page_flag_set(bp->b_xio.xio_pages[0], PG_RAM);