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.23 2006/04/30 18:52:36 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 #if defined(CLUSTERDEBUG)
60 #include <sys/sysctl.h>
61 static int rcluster= 0;
62 SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0, "");
65 static MALLOC_DEFINE(M_SEGMENT, "cluster_save", "cluster_save buffer");
67 static struct cluster_save *
68 cluster_collectbufs (struct vnode *vp, struct buf *last_bp,
71 cluster_rbuild (struct vnode *vp, off_t filesize, off_t loffset,
72 off_t doffset, int size, int run,
73 struct buf *fbp, int doasync);
74 static void cluster_callback (struct bio *);
77 static int write_behind = 1;
78 SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0, "");
80 extern vm_page_t bogus_page;
82 extern int cluster_pbuf_freecnt;
85 * Maximum number of blocks for read-ahead.
90 * This replaces bread.
93 cluster_read(struct vnode *vp, off_t filesize, off_t loffset,
94 int size, int totread, int seqcount, struct buf **bpp)
96 struct buf *bp, *rbp, *reqbp;
101 int maxra, racluster;
106 * Try to limit the amount of read-ahead by a few
107 * ad-hoc parameters. This needs work!!!
109 racluster = vp->v_mount->mnt_iosize_max / size;
110 maxra = 2 * racluster + (totread / size);
117 * get the requested block
119 *bpp = reqbp = bp = getblk(vp, loffset, size, 0, 0);
120 origoffset = loffset;
123 * if it is in the cache, then check to see if the reads have been
124 * sequential. If they have, then try some read-ahead, otherwise
125 * back-off on prospective read-aheads.
127 if (bp->b_flags & B_CACHE) {
130 } else if ((bp->b_flags & B_RAM) == 0) {
134 bp->b_flags &= ~B_RAM;
136 * We do the crit here so that there is no window
137 * between the findblk and the b_usecount increment
138 * below. We opt to keep the crit out of the loop
142 for (i = 1; i < maxra; i++) {
143 if (!(tbp = findblk(vp, loffset + i * size))) {
148 * Set another read-ahead mark so we know
151 if (((i % racluster) == (racluster - 1)) ||
153 tbp->b_flags |= B_RAM;
163 off_t firstread = bp->b_loffset;
166 KASSERT(firstread != NOOFFSET,
167 ("cluster_read: no buffer offset"));
168 if (firstread + totread > filesize)
169 totread = (int)(filesize - firstread);
170 nblks = totread / size;
174 if (nblks > racluster)
177 error = VOP_BMAP(vp, loffset, NULL,
178 &doffset, &burstbytes, NULL);
180 goto single_block_read;
181 if (doffset == NOOFFSET)
182 goto single_block_read;
183 if (burstbytes < size * 2)
184 goto single_block_read;
185 if (nblks > burstbytes / size)
186 nblks = burstbytes / size;
188 bp = cluster_rbuild(vp, filesize, loffset,
189 doffset, size, nblks, bp, 0);
190 loffset += bp->b_bufsize;
194 * if it isn't in the cache, then get a chunk from
195 * disk if sequential, otherwise just get the block.
197 bp->b_flags |= B_RAM;
203 * If we have been doing sequential I/O, then do some read-ahead.
207 loffset < origoffset + seqcount * size &&
208 loffset + size <= filesize
210 rbp = getblk(vp, loffset, size, 0, 0);
211 if ((rbp->b_flags & B_CACHE) == 0) {
216 error = VOP_BMAP(vp, loffset, NULL,
217 &doffset, &burstbytes, NULL);
218 if (error || doffset == NOOFFSET) {
223 ntoread = burstbytes / size;
224 nblksread = (totread + size - 1) / size;
225 if (seqcount < nblksread)
226 seqcount = nblksread;
227 if (seqcount < ntoread)
230 rbp->b_flags |= B_RAM;
232 rbp = cluster_rbuild(vp, filesize, loffset,
236 rbp->b_bio2.bio_offset = doffset;
243 * Handle the synchronous read. This only occurs if B_CACHE was
244 * not set. bp (and rbp) could be either a cluster bp or a normal
245 * bp depending on the what cluster_rbuild() decided to do. If
246 * it is a cluster bp, vfs_busy_pages() has already been called.
249 #if defined(CLUSTERDEBUG)
251 printf("S(%lld,%d,%d) ",
252 bp->b_loffset, bp->b_bcount, seqcount);
254 bp->b_cmd = BUF_CMD_READ;
255 if ((bp->b_flags & B_CLUSTER) == 0)
256 vfs_busy_pages(vp, bp);
257 bp->b_flags &= ~(B_ERROR|B_INVAL);
258 if ((bp->b_flags & B_ASYNC) || bp->b_bio1.bio_done != NULL)
260 vn_strategy(vp, &bp->b_bio1);
265 * And if we have read-aheads, do them too.
270 } else if (rbp->b_flags & B_CACHE) {
273 #if defined(CLUSTERDEBUG)
276 printf("A+(%lld,%d,%lld,%d) ",
277 rbp->b_loffset, rbp->b_bcount,
278 rbp->b_loffset - origoffset,
281 printf("A(%lld,%d,%lld,%d) ",
282 rbp->b_loffset, rbp->b_bcount,
283 rbp->b_loffset - origoffset,
287 rbp->b_flags &= ~(B_ERROR|B_INVAL);
288 rbp->b_flags |= B_ASYNC;
289 rbp->b_cmd = BUF_CMD_READ;
291 if ((rbp->b_flags & B_CLUSTER) == 0)
292 vfs_busy_pages(vp, rbp);
293 if ((rbp->b_flags & B_ASYNC) || rbp->b_bio1.bio_done != NULL)
295 vn_strategy(vp, &rbp->b_bio1);
299 return (biowait(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, off_t filesize, off_t loffset,
311 off_t doffset, int size, int run, struct buf *fbp, int doasync)
313 struct buf *bp, *tbp;
317 KASSERT(size == vp->v_mount->mnt_stat.f_iosize,
318 ("cluster_rbuild: size %d != filesize %ld\n",
319 size, vp->v_mount->mnt_stat.f_iosize));
324 while (loffset + run * size > filesize) {
329 tbp->b_bio2.bio_offset = doffset;
330 if((tbp->b_flags & B_MALLOC) ||
331 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1)) {
335 bp = trypbuf(&cluster_pbuf_freecnt);
340 * We are synthesizing a buffer out of vm_page_t's, but
341 * if the block size is not page aligned then the starting
342 * address may not be either. Inherit the b_data offset
343 * from the original buffer.
345 bp->b_data = (char *)((vm_offset_t)bp->b_data |
346 ((vm_offset_t)tbp->b_data & PAGE_MASK));
347 bp->b_flags |= B_ASYNC | B_CLUSTER | B_VMIO;
348 bp->b_cmd = BUF_CMD_READ;
349 bp->b_bio1.bio_done = cluster_callback;
350 bp->b_bio1.bio_caller_info1.cluster_head = NULL;
351 bp->b_bio1.bio_caller_info2.cluster_tail = NULL;
352 bp->b_loffset = loffset;
353 bp->b_bio2.bio_offset = NOOFFSET;
354 KASSERT(bp->b_loffset != NOOFFSET,
355 ("cluster_rbuild: no buffer offset"));
359 bp->b_xio.xio_npages = 0;
361 for (boffset = doffset, i = 0; i < run; ++i, boffset += size) {
363 if ((bp->b_xio.xio_npages * PAGE_SIZE) +
364 round_page(size) > vp->v_mount->mnt_iosize_max) {
369 * Shortcut some checks and try to avoid buffers that
370 * would block in the lock. The same checks have to
371 * be made again after we officially get the buffer.
373 if ((tbp = findblk(vp, loffset + i * size)) != NULL) {
374 if (BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT))
378 for (j = 0; j < tbp->b_xio.xio_npages; j++) {
379 if (tbp->b_xio.xio_pages[j]->valid)
383 if (j != tbp->b_xio.xio_npages)
386 if (tbp->b_bcount != size)
390 tbp = getblk(vp, loffset + i * size, size, 0, 0);
393 * Stop scanning if the buffer is fuly valid
394 * (marked B_CACHE), or locked (may be doing a
395 * background write), or if the buffer is not
396 * VMIO backed. The clustering code can only deal
397 * with VMIO-backed buffers.
399 if ((tbp->b_flags & (B_CACHE|B_LOCKED)) ||
400 (tbp->b_flags & B_VMIO) == 0) {
406 * The buffer must be completely invalid in order to
407 * take part in the cluster. If it is partially valid
410 for (j = 0;j < tbp->b_xio.xio_npages; j++) {
411 if (tbp->b_xio.xio_pages[j]->valid)
414 if (j != tbp->b_xio.xio_npages) {
420 * Set a read-ahead mark as appropriate
422 if (i == 1 || i == (run - 1))
423 tbp->b_flags |= B_RAM;
426 * Set the block number if it isn't set, otherwise
427 * if it is make sure it matches the block number we
430 if (tbp->b_bio2.bio_offset == NOOFFSET) {
431 tbp->b_bio2.bio_offset = boffset;
432 } else if (tbp->b_bio2.bio_offset != boffset) {
438 * The first buffer is setup async if doasync is specified.
439 * All other buffers in the cluster are setup async. This
440 * way the caller can decide how to deal with the requested
444 tbp->b_flags |= B_ASYNC;
445 tbp->b_cmd = BUF_CMD_READ;
447 cluster_append(&bp->b_bio1, tbp);
448 for (j = 0; j < tbp->b_xio.xio_npages; ++j) {
450 m = tbp->b_xio.xio_pages[j];
452 vm_object_pip_add(m->object, 1);
453 if ((bp->b_xio.xio_npages == 0) ||
454 (bp->b_xio.xio_pages[bp->b_xio.xio_npages-1] != m)) {
455 bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m;
456 bp->b_xio.xio_npages++;
458 if ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL)
459 tbp->b_xio.xio_pages[j] = bogus_page;
462 * XXX shouldn't this be += size for both, like in
465 * Don't inherit tbp->b_bufsize as it may be larger due to
466 * a non-page-aligned size. Instead just aggregate using
469 if (tbp->b_bcount != size)
470 printf("warning: tbp->b_bcount wrong %d vs %d\n", tbp->b_bcount, size);
471 if (tbp->b_bufsize != size)
472 printf("warning: tbp->b_bufsize wrong %d vs %d\n", tbp->b_bufsize, size);
473 bp->b_bcount += size;
474 bp->b_bufsize += size;
478 * Fully valid pages in the cluster are already good and do not need
479 * to be re-read from disk. Replace the page with bogus_page
481 for (j = 0; j < bp->b_xio.xio_npages; j++) {
482 if ((bp->b_xio.xio_pages[j]->valid & VM_PAGE_BITS_ALL) ==
484 bp->b_xio.xio_pages[j] = bogus_page;
487 if (bp->b_bufsize > bp->b_kvasize) {
488 panic("cluster_rbuild: b_bufsize(%d) > b_kvasize(%d)",
489 bp->b_bufsize, bp->b_kvasize);
492 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
493 (vm_page_t *)bp->b_xio.xio_pages, bp->b_xio.xio_npages);
498 * Cleanup after a clustered read or write.
499 * This is complicated by the fact that any of the buffers might have
500 * extra memory (if there were no empty buffer headers at allocbuf time)
501 * that we will need to shift around.
503 * The returned bio is &bp->b_bio1
506 cluster_callback(struct bio *bio)
508 struct buf *bp = bio->bio_buf;
513 * Must propogate errors to all the components.
515 if (bp->b_flags & B_ERROR)
518 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_xio.xio_npages);
520 * Move memory from the large cluster buffer into the component
521 * buffers and mark IO as done on these. Since the memory map
522 * is the same, no actual copying is required.
524 while ((tbp = bio->bio_caller_info1.cluster_head) != NULL) {
525 bio->bio_caller_info1.cluster_head = tbp->b_cluster_next;
527 tbp->b_flags |= B_ERROR;
528 tbp->b_error = error;
530 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
531 tbp->b_flags &= ~(B_ERROR|B_INVAL);
533 * XXX the bdwrite()/bqrelse() issued during
534 * cluster building clears B_RELBUF (see bqrelse()
535 * comment). If direct I/O was specified, we have
536 * to restore it here to allow the buffer and VM
539 if (tbp->b_flags & B_DIRECT)
540 tbp->b_flags |= B_RELBUF;
542 biodone(&tbp->b_bio1);
544 relpbuf(bp, &cluster_pbuf_freecnt);
550 * Implement modified write build for cluster.
552 * write_behind = 0 write behind disabled
553 * write_behind = 1 write behind normal (default)
554 * write_behind = 2 write behind backed-off
558 cluster_wbuild_wb(struct vnode *vp, int size, off_t start_loffset, int len)
562 switch(write_behind) {
564 if (start_loffset < len)
566 start_loffset -= len;
569 r = cluster_wbuild(vp, size, start_loffset, len);
579 * Do clustered write for FFS.
582 * 1. Write is not sequential (write asynchronously)
583 * Write is sequential:
584 * 2. beginning of cluster - begin cluster
585 * 3. middle of a cluster - add to cluster
586 * 4. end of a cluster - asynchronously write cluster
589 cluster_write(struct buf *bp, off_t filesize, int seqcount)
593 int maxclen, cursize;
598 if (vp->v_type == VREG) {
599 async = vp->v_mount->mnt_flag & MNT_ASYNC;
600 lblocksize = vp->v_mount->mnt_stat.f_iosize;
603 lblocksize = bp->b_bufsize;
605 loffset = bp->b_loffset;
606 KASSERT(bp->b_loffset != NOOFFSET,
607 ("cluster_write: no buffer offset"));
609 /* Initialize vnode to beginning of file. */
611 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
613 if (vp->v_clen == 0 || loffset != vp->v_lastw + lblocksize ||
614 bp->b_bio2.bio_offset == NOOFFSET ||
615 (bp->b_bio2.bio_offset != vp->v_lasta + lblocksize)) {
616 maxclen = vp->v_mount->mnt_iosize_max;
617 if (vp->v_clen != 0) {
619 * Next block is not sequential.
621 * If we are not writing at end of file, the process
622 * seeked to another point in the file since its last
623 * write, or we have reached our maximum cluster size,
624 * then push the previous cluster. Otherwise try
625 * reallocating to make it sequential.
627 * Change to algorithm: only push previous cluster if
628 * it was sequential from the point of view of the
629 * seqcount heuristic, otherwise leave the buffer
630 * intact so we can potentially optimize the I/O
631 * later on in the buf_daemon or update daemon
634 cursize = vp->v_lastw - vp->v_cstart + lblocksize;
635 if (bp->b_loffset + lblocksize != filesize ||
636 loffset != vp->v_lastw + lblocksize || vp->v_clen <= cursize) {
637 if (!async && seqcount > 0) {
638 cluster_wbuild_wb(vp, lblocksize,
639 vp->v_cstart, cursize);
642 struct buf **bpp, **endbp;
643 struct cluster_save *buflist;
645 buflist = cluster_collectbufs(vp, bp,
647 endbp = &buflist->bs_children
648 [buflist->bs_nchildren - 1];
649 if (VOP_REALLOCBLKS(vp, buflist)) {
651 * Failed, push the previous cluster
652 * if *really* writing sequentially
653 * in the logical file (seqcount > 1),
654 * otherwise delay it in the hopes that
655 * the low level disk driver can
656 * optimize the write ordering.
658 for (bpp = buflist->bs_children;
661 free(buflist, M_SEGMENT);
663 cluster_wbuild_wb(vp,
664 lblocksize, vp->v_cstart,
669 * Succeeded, keep building cluster.
671 for (bpp = buflist->bs_children;
674 free(buflist, M_SEGMENT);
675 vp->v_lastw = loffset;
676 vp->v_lasta = bp->b_bio2.bio_offset;
682 * Consider beginning a cluster. If at end of file, make
683 * cluster as large as possible, otherwise find size of
686 if ((vp->v_type == VREG) &&
687 bp->b_loffset + lblocksize != filesize &&
688 (bp->b_bio2.bio_offset == NOOFFSET) &&
689 (VOP_BMAP(vp, loffset, NULL, &bp->b_bio2.bio_offset, &maxclen, NULL) ||
690 bp->b_bio2.bio_offset == NOOFFSET)) {
693 vp->v_lasta = bp->b_bio2.bio_offset;
694 vp->v_cstart = loffset + lblocksize;
695 vp->v_lastw = loffset;
698 if (maxclen > lblocksize)
699 vp->v_clen = maxclen - lblocksize;
702 if (!async && vp->v_clen == 0) { /* I/O not contiguous */
703 vp->v_cstart = loffset + lblocksize;
705 } else { /* Wait for rest of cluster */
706 vp->v_cstart = loffset;
709 } else if (loffset == vp->v_cstart + vp->v_clen) {
711 * At end of cluster, write it out if seqcount tells us we
712 * are operating sequentially, otherwise let the buf or
713 * update daemon handle it.
717 cluster_wbuild_wb(vp, lblocksize, vp->v_cstart,
718 vp->v_clen + lblocksize);
720 vp->v_cstart = loffset + lblocksize;
721 } else if (vm_page_count_severe()) {
723 * We are low on memory, get it going NOW
728 * In the middle of a cluster, so just delay the I/O for now.
732 vp->v_lastw = loffset;
733 vp->v_lasta = bp->b_bio2.bio_offset;
738 * This is an awful lot like cluster_rbuild...wish they could be combined.
739 * The last lbn argument is the current block on which I/O is being
740 * performed. Check to see that it doesn't fall in the middle of
741 * the current block (if last_bp == NULL).
744 cluster_wbuild(struct vnode *vp, int size, off_t start_loffset, int bytes)
746 struct buf *bp, *tbp;
748 int totalwritten = 0;
753 * If the buffer is not delayed-write (i.e. dirty), or it
754 * is delayed-write but either locked or inval, it cannot
755 * partake in the clustered write.
757 if (((tbp = findblk(vp, start_loffset)) == NULL) ||
758 ((tbp->b_flags & (B_LOCKED | B_INVAL | B_DELWRI)) != B_DELWRI) ||
759 BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) {
760 start_loffset += size;
766 KKASSERT(tbp->b_cmd == BUF_CMD_DONE);
770 * Extra memory in the buffer, punt on this buffer.
771 * XXX we could handle this in most cases, but we would
772 * have to push the extra memory down to after our max
773 * possible cluster size and then potentially pull it back
774 * up if the cluster was terminated prematurely--too much
777 if (((tbp->b_flags & (B_CLUSTEROK|B_MALLOC)) != B_CLUSTEROK) ||
778 (tbp->b_bcount != tbp->b_bufsize) ||
779 (tbp->b_bcount != size) ||
781 ((bp = getpbuf(&cluster_pbuf_freecnt)) == NULL)) {
782 totalwritten += tbp->b_bufsize;
784 start_loffset += size;
790 * We got a pbuf to make the cluster in.
795 bp->b_xio.xio_npages = 0;
796 bp->b_loffset = tbp->b_loffset;
797 bp->b_bio2.bio_offset = tbp->b_bio2.bio_offset;
800 * We are synthesizing a buffer out of vm_page_t's, but
801 * if the block size is not page aligned then the starting
802 * address may not be either. Inherit the b_data offset
803 * from the original buffer.
805 bp->b_data = (char *)((vm_offset_t)bp->b_data |
806 ((vm_offset_t)tbp->b_data & PAGE_MASK));
807 bp->b_flags &= ~B_ERROR;
808 bp->b_flags |= B_CLUSTER |
809 (tbp->b_flags & (B_VMIO | B_NEEDCOMMIT | B_NOWDRAIN));
810 bp->b_bio1.bio_done = cluster_callback;
811 bp->b_bio1.bio_caller_info1.cluster_head = NULL;
812 bp->b_bio1.bio_caller_info2.cluster_tail = NULL;
814 * From this location in the file, scan forward to see
815 * if there are buffers with adjacent data that need to
816 * be written as well.
818 for (i = 0; i < bytes; (i += size), (start_loffset += size)) {
819 if (i != 0) { /* If not the first buffer */
822 * If the adjacent data is not even in core it
823 * can't need to be written.
825 if ((tbp = findblk(vp, start_loffset)) == NULL) {
831 * If it IS in core, but has different
832 * characteristics, or is locked (which
833 * means it could be undergoing a background
834 * I/O or be in a weird state), then don't
837 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
838 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
839 != (B_DELWRI | B_CLUSTEROK |
840 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
841 (tbp->b_flags & B_LOCKED) ||
842 BUF_LOCK(tbp, LK_EXCLUSIVE | LK_NOWAIT)) {
848 * Check that the combined cluster
849 * would make sense with regard to pages
850 * and would not be too large
852 if ((tbp->b_bcount != size) ||
853 ((bp->b_bio2.bio_offset + i) !=
854 tbp->b_bio2.bio_offset) ||
855 ((tbp->b_xio.xio_npages + bp->b_xio.xio_npages) >
856 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
862 * Ok, it's passed all the tests,
863 * so remove it from the free list
864 * and mark it busy. We will use it.
867 KKASSERT(tbp->b_cmd == BUF_CMD_DONE);
869 } /* end of code for non-first buffers only */
872 * If the IO is via the VM then we do some
873 * special VM hackery (yuck). Since the buffer's
874 * block size may not be page-aligned it is possible
875 * for a page to be shared between two buffers. We
876 * have to get rid of the duplication when building
879 if (tbp->b_flags & B_VMIO) {
882 if (i != 0) { /* if not first buffer */
883 for (j = 0; j < tbp->b_xio.xio_npages; ++j) {
884 m = tbp->b_xio.xio_pages[j];
885 if (m->flags & PG_BUSY) {
892 for (j = 0; j < tbp->b_xio.xio_npages; ++j) {
893 m = tbp->b_xio.xio_pages[j];
895 vm_object_pip_add(m->object, 1);
896 if ((bp->b_xio.xio_npages == 0) ||
897 (bp->b_xio.xio_pages[bp->b_xio.xio_npages - 1] != m)) {
898 bp->b_xio.xio_pages[bp->b_xio.xio_npages] = m;
899 bp->b_xio.xio_npages++;
903 bp->b_bcount += size;
904 bp->b_bufsize += size;
908 tbp->b_flags &= ~B_ERROR;
909 tbp->b_flags |= B_ASYNC;
910 tbp->b_cmd = BUF_CMD_WRITE;
913 cluster_append(&bp->b_bio1, tbp);
916 * check for latent dependencies to be handled
918 if (LIST_FIRST(&tbp->b_dep) != NULL && bioops.io_start)
919 (*bioops.io_start)(tbp);
923 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
924 (vm_page_t *) bp->b_xio.xio_pages, bp->b_xio.xio_npages);
925 if (bp->b_bufsize > bp->b_kvasize) {
927 "cluster_wbuild: b_bufsize(%d) > b_kvasize(%d)\n",
928 bp->b_bufsize, bp->b_kvasize);
930 totalwritten += bp->b_bufsize;
932 bp->b_dirtyend = bp->b_bufsize;
933 bp->b_flags |= B_ASYNC;
934 bp->b_cmd = BUF_CMD_WRITE;
935 vfs_busy_pages(vp, bp);
936 bp->b_runningbufspace = bp->b_bufsize;
937 runningbufspace += bp->b_runningbufspace;
938 BUF_KERNPROC(bp); /* B_ASYNC */
939 vn_strategy(vp, &bp->b_bio1);
947 * Collect together all the buffers in a cluster.
948 * Plus add one additional buffer.
950 static struct cluster_save *
951 cluster_collectbufs(struct vnode *vp, struct buf *last_bp, int lblocksize)
953 struct cluster_save *buflist;
958 len = (int)(vp->v_lastw - vp->v_cstart + lblocksize) / lblocksize;
959 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
960 M_SEGMENT, M_WAITOK);
961 buflist->bs_nchildren = 0;
962 buflist->bs_children = (struct buf **) (buflist + 1);
963 for (loffset = vp->v_cstart, i = 0; i < len; (loffset += lblocksize), i++) {
964 (void) bread(vp, loffset, last_bp->b_bcount, &bp);
965 buflist->bs_children[i] = bp;
966 if (bp->b_bio2.bio_offset == NOOFFSET) {
967 VOP_BMAP(bp->b_vp, bp->b_loffset, NULL,
968 &bp->b_bio2.bio_offset, NULL, NULL);
971 buflist->bs_children[i] = bp = last_bp;
972 if (bp->b_bio2.bio_offset == NOOFFSET) {
973 VOP_BMAP(bp->b_vp, bp->b_loffset, NULL,
974 &bp->b_bio2.bio_offset, NULL, NULL);
976 buflist->bs_nchildren = i + 1;
981 cluster_append(struct bio *bio, struct buf *tbp)
983 tbp->b_cluster_next = NULL;
984 if (bio->bio_caller_info1.cluster_head == NULL) {
985 bio->bio_caller_info1.cluster_head = tbp;
986 bio->bio_caller_info2.cluster_tail = tbp;
988 bio->bio_caller_info2.cluster_tail->b_cluster_next = tbp;
989 bio->bio_caller_info2.cluster_tail = tbp;