3 * The Regents of the University of California. All rights reserved.
4 * Modifications/enhancements:
5 * Copyright (c) 1995 John S. Dyson. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)vfs_cluster.c 8.7 (Berkeley) 2/13/94
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_debug_cluster.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/resourcevar.h>
49 #include <sys/vmmeter.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
53 #include <sys/sysctl.h>
55 #if defined(CLUSTERDEBUG)
56 static int rcluster= 0;
57 SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0,
58 "Debug VFS clustering code");
61 static MALLOC_DEFINE(M_SEGMENT, "cl_savebuf", "cluster_save buffer");
63 static struct cluster_save *cluster_collectbufs(struct vnode *vp,
64 struct buf *last_bp, int gbflags);
65 static struct buf *cluster_rbuild(struct vnode *vp, u_quad_t filesize,
66 daddr_t lbn, daddr_t blkno, long size, int run, int gbflags,
68 static void cluster_callback(struct buf *);
70 static int write_behind = 1;
71 SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0,
72 "Cluster write-behind; 0: disable, 1: enable, 2: backed off");
74 static int read_max = 64;
75 SYSCTL_INT(_vfs, OID_AUTO, read_max, CTLFLAG_RW, &read_max, 0,
76 "Cluster read-ahead max block count");
78 static int read_min = 1;
79 SYSCTL_INT(_vfs, OID_AUTO, read_min, CTLFLAG_RW, &read_min, 0,
80 "Cluster read min block count");
82 /* Page expended to mark partially backed buffers */
83 extern vm_page_t bogus_page;
86 * Read data to a buf, including read-ahead if we find this to be beneficial.
87 * cluster_read replaces bread.
90 cluster_read(struct vnode *vp, u_quad_t filesize, daddr_t lblkno, long size,
91 struct ucred *cred, long totread, int seqcount, struct buf **bpp)
94 return (cluster_read_gb(vp, filesize, lblkno, size, cred, totread,
99 cluster_read_gb(struct vnode *vp, u_quad_t filesize, daddr_t lblkno, long size,
100 struct ucred *cred, long totread, int seqcount, int gbflags,
103 struct buf *bp, *rbp, *reqbp;
105 daddr_t blkno, origblkno;
106 int maxra, racluster;
112 if (!unmapped_buf_allowed)
113 gbflags &= ~GB_UNMAPPED;
116 * Try to limit the amount of read-ahead by a few
117 * ad-hoc parameters. This needs work!!!
119 racluster = vp->v_mount->mnt_iosize_max / size;
121 maxra = min(read_max, maxra);
122 maxra = min(nbuf/8, maxra);
123 if (((u_quad_t)(lblkno + maxra + 1) * size) > filesize)
124 maxra = (filesize / size) - lblkno;
127 * get the requested block
129 *bpp = reqbp = bp = getblk(vp, lblkno, size, 0, 0, gbflags);
133 * if it is in the cache, then check to see if the reads have been
134 * sequential. If they have, then try some read-ahead, otherwise
135 * back-off on prospective read-aheads.
137 if (bp->b_flags & B_CACHE) {
140 } else if ((bp->b_flags & B_RAM) == 0) {
143 bp->b_flags &= ~B_RAM;
145 for (i = 1; i < maxra; i++) {
147 * Stop if the buffer does not exist or it
148 * is invalid (about to go away?)
150 rbp = gbincore(&vp->v_bufobj, lblkno+i);
151 if (rbp == NULL || (rbp->b_flags & B_INVAL))
155 * Set another read-ahead mark so we know
156 * to check again. (If we can lock the
157 * buffer without waiting)
159 if ((((i % racluster) == (racluster - 1)) ||
161 && (0 == BUF_LOCK(rbp,
162 LK_EXCLUSIVE | LK_NOWAIT, NULL))) {
163 rbp->b_flags |= B_RAM;
175 * If it isn't in the cache, then get a chunk from
176 * disk if sequential, otherwise just get the block.
179 off_t firstread = bp->b_offset;
183 KASSERT(bp->b_offset != NOOFFSET,
184 ("cluster_read: no buffer offset"));
189 * Adjust totread if needed
191 minread = read_min * size;
192 if (minread > totread)
196 * Compute the total number of blocks that we should read
199 if (firstread + totread > filesize)
200 totread = filesize - firstread;
201 nblks = howmany(totread, size);
202 if (nblks > racluster)
206 * Now compute the number of contiguous blocks.
209 error = VOP_BMAP(vp, lblkno, NULL,
210 &blkno, &ncontig, NULL);
212 * If this failed to map just do the original block.
214 if (error || blkno == -1)
219 * If we have contiguous data available do a cluster
220 * otherwise just read the requested block.
223 /* Account for our first block. */
224 ncontig = min(ncontig + 1, nblks);
227 bp = cluster_rbuild(vp, filesize, lblkno,
228 blkno, size, nblks, gbflags, bp);
229 lblkno += (bp->b_bufsize / size);
231 bp->b_flags |= B_RAM;
232 bp->b_iocmd = BIO_READ;
238 * handle the synchronous read so that it is available ASAP.
241 if ((bp->b_flags & B_CLUSTER) == 0) {
242 vfs_busy_pages(bp, 0);
244 bp->b_flags &= ~B_INVAL;
245 bp->b_ioflags &= ~BIO_ERROR;
246 if ((bp->b_flags & B_ASYNC) || bp->b_iodone != NULL)
248 bp->b_iooffset = dbtob(bp->b_blkno);
250 curthread->td_ru.ru_inblock++;
254 * If we have been doing sequential I/O, then do some read-ahead.
256 while (lblkno < (origblkno + maxra)) {
257 error = VOP_BMAP(vp, lblkno, NULL, &blkno, &ncontig, NULL);
265 * We could throttle ncontig here by maxra but we might as
266 * well read the data if it is contiguous. We're throttled
267 * by racluster anyway.
270 ncontig = min(ncontig + 1, racluster);
271 rbp = cluster_rbuild(vp, filesize, lblkno, blkno,
272 size, ncontig, gbflags, NULL);
273 lblkno += (rbp->b_bufsize / size);
274 if (rbp->b_flags & B_DELWRI) {
279 rbp = getblk(vp, lblkno, size, 0, 0, gbflags);
281 if (rbp->b_flags & B_DELWRI) {
285 rbp->b_flags |= B_ASYNC | B_RAM;
286 rbp->b_iocmd = BIO_READ;
287 rbp->b_blkno = blkno;
289 if (rbp->b_flags & B_CACHE) {
290 rbp->b_flags &= ~B_ASYNC;
294 if ((rbp->b_flags & B_CLUSTER) == 0) {
295 vfs_busy_pages(rbp, 0);
297 rbp->b_flags &= ~B_INVAL;
298 rbp->b_ioflags &= ~BIO_ERROR;
299 if ((rbp->b_flags & B_ASYNC) || rbp->b_iodone != NULL)
301 rbp->b_iooffset = dbtob(rbp->b_blkno);
303 curthread->td_ru.ru_inblock++;
307 return (bufwait(reqbp));
313 * If blocks are contiguous on disk, use this to provide clustered
314 * read ahead. We will read as many blocks as possible sequentially
315 * and then parcel them up into logical blocks in the buffer hash table.
318 cluster_rbuild(struct vnode *vp, u_quad_t filesize, daddr_t lbn,
319 daddr_t blkno, long size, int run, int gbflags, struct buf *fbp)
322 struct buf *bp, *tbp;
328 KASSERT(size == vp->v_mount->mnt_stat.f_iosize,
329 ("cluster_rbuild: size %ld != filesize %jd\n",
330 size, (intmax_t)vp->v_mount->mnt_stat.f_iosize));
335 while ((u_quad_t) size * (lbn + run) > filesize) {
341 tbp->b_iocmd = BIO_READ;
343 tbp = getblk(vp, lbn, size, 0, 0, gbflags);
344 if (tbp->b_flags & B_CACHE)
346 tbp->b_flags |= B_ASYNC | B_RAM;
347 tbp->b_iocmd = BIO_READ;
349 tbp->b_blkno = blkno;
350 if( (tbp->b_flags & B_MALLOC) ||
351 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
354 bp = trypbuf(&cluster_pbuf_freecnt);
359 * We are synthesizing a buffer out of vm_page_t's, but
360 * if the block size is not page aligned then the starting
361 * address may not be either. Inherit the b_data offset
362 * from the original buffer.
364 bp->b_flags = B_ASYNC | B_CLUSTER | B_VMIO;
365 if ((gbflags & GB_UNMAPPED) != 0) {
366 bp->b_flags |= B_UNMAPPED;
367 bp->b_data = unmapped_buf;
369 bp->b_data = (char *)((vm_offset_t)bp->b_data |
370 ((vm_offset_t)tbp->b_data & PAGE_MASK));
372 bp->b_iocmd = BIO_READ;
373 bp->b_iodone = cluster_callback;
376 bp->b_offset = tbp->b_offset;
377 KASSERT(bp->b_offset != NOOFFSET, ("cluster_rbuild: no buffer offset"));
380 TAILQ_INIT(&bp->b_cluster.cluster_head);
388 for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
390 if ((bp->b_npages * PAGE_SIZE) +
391 round_page(size) > vp->v_mount->mnt_iosize_max) {
395 tbp = getblk(vp, lbn + i, size, 0, 0, GB_LOCK_NOWAIT |
396 (gbflags & GB_UNMAPPED));
398 /* Don't wait around for locked bufs. */
403 * Stop scanning if the buffer is fully valid
404 * (marked B_CACHE), or locked (may be doing a
405 * background write), or if the buffer is not
406 * VMIO backed. The clustering code can only deal
407 * with VMIO-backed buffers.
410 if ((tbp->b_vflags & BV_BKGRDINPROG) ||
411 (tbp->b_flags & B_CACHE) ||
412 (tbp->b_flags & B_VMIO) == 0) {
420 * The buffer must be completely invalid in order to
421 * take part in the cluster. If it is partially valid
426 VM_OBJECT_LOCK(tbp->b_bufobj->bo_object);
427 for (j = 0; tsize > 0; j++) {
428 toff = off & PAGE_MASK;
430 if (toff + tinc > PAGE_SIZE)
431 tinc = PAGE_SIZE - toff;
432 VM_OBJECT_LOCK_ASSERT(tbp->b_pages[j]->object,
434 if ((tbp->b_pages[j]->valid &
435 vm_page_bits(toff, tinc)) != 0)
440 VM_OBJECT_UNLOCK(tbp->b_bufobj->bo_object);
447 * Set a read-ahead mark as appropriate
449 if ((fbp && (i == 1)) || (i == (run - 1)))
450 tbp->b_flags |= B_RAM;
453 * Set the buffer up for an async read (XXX should
454 * we do this only if we do not wind up brelse()ing?).
455 * Set the block number if it isn't set, otherwise
456 * if it is make sure it matches the block number we
459 tbp->b_flags |= B_ASYNC;
460 tbp->b_iocmd = BIO_READ;
461 if (tbp->b_blkno == tbp->b_lblkno) {
463 } else if (tbp->b_blkno != bn) {
469 * XXX fbp from caller may not be B_ASYNC, but we are going
470 * to biodone() it in cluster_callback() anyway
473 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
474 tbp, b_cluster.cluster_entry);
475 VM_OBJECT_LOCK(tbp->b_bufobj->bo_object);
476 for (j = 0; j < tbp->b_npages; j += 1) {
480 vm_object_pip_add(m->object, 1);
481 if ((bp->b_npages == 0) ||
482 (bp->b_pages[bp->b_npages-1] != m)) {
483 bp->b_pages[bp->b_npages] = m;
486 if (m->valid == VM_PAGE_BITS_ALL)
487 tbp->b_pages[j] = bogus_page;
489 VM_OBJECT_UNLOCK(tbp->b_bufobj->bo_object);
491 * Don't inherit tbp->b_bufsize as it may be larger due to
492 * a non-page-aligned size. Instead just aggregate using
495 if (tbp->b_bcount != size)
496 printf("warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size);
497 if (tbp->b_bufsize != size)
498 printf("warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size);
499 bp->b_bcount += size;
500 bp->b_bufsize += size;
504 * Fully valid pages in the cluster are already good and do not need
505 * to be re-read from disk. Replace the page with bogus_page
507 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
508 for (j = 0; j < bp->b_npages; j++) {
509 VM_OBJECT_LOCK_ASSERT(bp->b_pages[j]->object, MA_OWNED);
510 if (bp->b_pages[j]->valid == VM_PAGE_BITS_ALL)
511 bp->b_pages[j] = bogus_page;
513 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
514 if (bp->b_bufsize > bp->b_kvasize)
515 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
516 bp->b_bufsize, bp->b_kvasize);
517 bp->b_kvasize = bp->b_bufsize;
519 if ((bp->b_flags & B_UNMAPPED) == 0) {
520 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
521 (vm_page_t *)bp->b_pages, bp->b_npages);
527 * Cleanup after a clustered read or write.
528 * This is complicated by the fact that any of the buffers might have
529 * extra memory (if there were no empty buffer headers at allocbuf time)
530 * that we will need to shift around.
536 struct buf *nbp, *tbp;
540 * Must propogate errors to all the components.
542 if (bp->b_ioflags & BIO_ERROR)
545 if ((bp->b_flags & B_UNMAPPED) == 0) {
546 pmap_qremove(trunc_page((vm_offset_t) bp->b_data),
550 * Move memory from the large cluster buffer into the component
551 * buffers and mark IO as done on these.
553 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head);
555 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry);
557 tbp->b_ioflags |= BIO_ERROR;
558 tbp->b_error = error;
560 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
561 tbp->b_flags &= ~B_INVAL;
562 tbp->b_ioflags &= ~BIO_ERROR;
564 * XXX the bdwrite()/bqrelse() issued during
565 * cluster building clears B_RELBUF (see bqrelse()
566 * comment). If direct I/O was specified, we have
567 * to restore it here to allow the buffer and VM
570 if (tbp->b_flags & B_DIRECT)
571 tbp->b_flags |= B_RELBUF;
576 relpbuf(bp, &cluster_pbuf_freecnt);
582 * Implement modified write build for cluster.
584 * write_behind = 0 write behind disabled
585 * write_behind = 1 write behind normal (default)
586 * write_behind = 2 write behind backed-off
590 cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len,
595 switch (write_behind) {
602 r = cluster_wbuild_gb(vp, size, start_lbn, len, gbflags);
612 * Do clustered write for FFS.
615 * 1. Write is not sequential (write asynchronously)
616 * Write is sequential:
617 * 2. beginning of cluster - begin cluster
618 * 3. middle of a cluster - add to cluster
619 * 4. end of a cluster - asynchronously write cluster
622 cluster_write(struct vnode *vp, struct buf *bp, u_quad_t filesize, int seqcount)
625 cluster_write_gb(vp, bp, filesize, seqcount, 0);
629 cluster_write_gb(struct vnode *vp, struct buf *bp, u_quad_t filesize,
630 int seqcount, int gbflags)
633 int maxclen, cursize;
637 if (!unmapped_buf_allowed)
638 gbflags &= ~GB_UNMAPPED;
640 if (vp->v_type == VREG) {
641 async = DOINGASYNC(vp);
642 lblocksize = vp->v_mount->mnt_stat.f_iosize;
645 lblocksize = bp->b_bufsize;
648 KASSERT(bp->b_offset != NOOFFSET, ("cluster_write: no buffer offset"));
650 /* Initialize vnode to beginning of file. */
652 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
654 if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 ||
655 (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) {
656 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1;
657 if (vp->v_clen != 0) {
659 * Next block is not sequential.
661 * If we are not writing at end of file, the process
662 * seeked to another point in the file since its last
663 * write, or we have reached our maximum cluster size,
664 * then push the previous cluster. Otherwise try
665 * reallocating to make it sequential.
667 * Change to algorithm: only push previous cluster if
668 * it was sequential from the point of view of the
669 * seqcount heuristic, otherwise leave the buffer
670 * intact so we can potentially optimize the I/O
671 * later on in the buf_daemon or update daemon
674 cursize = vp->v_lastw - vp->v_cstart + 1;
675 if (((u_quad_t) bp->b_offset + lblocksize) != filesize ||
676 lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) {
677 if (!async && seqcount > 0) {
678 cluster_wbuild_wb(vp, lblocksize,
679 vp->v_cstart, cursize, gbflags);
682 struct buf **bpp, **endbp;
683 struct cluster_save *buflist;
685 buflist = cluster_collectbufs(vp, bp, gbflags);
686 endbp = &buflist->bs_children
687 [buflist->bs_nchildren - 1];
688 if (VOP_REALLOCBLKS(vp, buflist)) {
690 * Failed, push the previous cluster
691 * if *really* writing sequentially
692 * in the logical file (seqcount > 1),
693 * otherwise delay it in the hopes that
694 * the low level disk driver can
695 * optimize the write ordering.
697 for (bpp = buflist->bs_children;
700 free(buflist, M_SEGMENT);
702 cluster_wbuild_wb(vp,
703 lblocksize, vp->v_cstart,
708 * Succeeded, keep building cluster.
710 for (bpp = buflist->bs_children;
713 free(buflist, M_SEGMENT);
715 vp->v_lasta = bp->b_blkno;
721 * Consider beginning a cluster. If at end of file, make
722 * cluster as large as possible, otherwise find size of
725 if ((vp->v_type == VREG) &&
726 ((u_quad_t) bp->b_offset + lblocksize) != filesize &&
727 (bp->b_blkno == bp->b_lblkno) &&
728 (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) ||
729 bp->b_blkno == -1)) {
732 vp->v_lasta = bp->b_blkno;
733 vp->v_cstart = lbn + 1;
737 vp->v_clen = maxclen;
738 if (!async && maxclen == 0) { /* I/O not contiguous */
739 vp->v_cstart = lbn + 1;
741 } else { /* Wait for rest of cluster */
745 } else if (lbn == vp->v_cstart + vp->v_clen) {
747 * At end of cluster, write it out if seqcount tells us we
748 * are operating sequentially, otherwise let the buf or
749 * update daemon handle it.
753 cluster_wbuild_wb(vp, lblocksize, vp->v_cstart,
754 vp->v_clen + 1, gbflags);
757 vp->v_cstart = lbn + 1;
758 } else if (vm_page_count_severe()) {
760 * We are low on memory, get it going NOW
765 * In the middle of a cluster, so just delay the I/O for now.
770 vp->v_lasta = bp->b_blkno;
775 * This is an awful lot like cluster_rbuild...wish they could be combined.
776 * The last lbn argument is the current block on which I/O is being
777 * performed. Check to see that it doesn't fall in the middle of
778 * the current block (if last_bp == NULL).
781 cluster_wbuild(struct vnode *vp, long size, daddr_t start_lbn, int len)
784 return (cluster_wbuild_gb(vp, size, start_lbn, len, 0));
788 cluster_wbuild_gb(struct vnode *vp, long size, daddr_t start_lbn, int len,
791 struct buf *bp, *tbp;
794 int totalwritten = 0;
795 int dbsize = btodb(size);
797 if (!unmapped_buf_allowed)
798 gbflags &= ~GB_UNMAPPED;
803 * If the buffer is not delayed-write (i.e. dirty), or it
804 * is delayed-write but either locked or inval, it cannot
805 * partake in the clustered write.
808 if ((tbp = gbincore(&vp->v_bufobj, start_lbn)) == NULL ||
809 (tbp->b_vflags & BV_BKGRDINPROG)) {
816 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, BO_MTX(bo))) {
821 if ((tbp->b_flags & (B_INVAL | B_DELWRI)) != B_DELWRI) {
827 if (tbp->b_pin_count > 0) {
834 tbp->b_flags &= ~B_DONE;
837 * Extra memory in the buffer, punt on this buffer.
838 * XXX we could handle this in most cases, but we would
839 * have to push the extra memory down to after our max
840 * possible cluster size and then potentially pull it back
841 * up if the cluster was terminated prematurely--too much
844 if (((tbp->b_flags & (B_CLUSTEROK | B_MALLOC | B_VMIO)) !=
845 (B_CLUSTEROK | B_VMIO)) ||
846 (tbp->b_bcount != tbp->b_bufsize) ||
847 (tbp->b_bcount != size) ||
849 ((bp = (vp->v_vflag & VV_MD) != 0 ?
850 trypbuf(&cluster_pbuf_freecnt) :
851 getpbuf(&cluster_pbuf_freecnt)) == NULL)) {
852 totalwritten += tbp->b_bufsize;
860 * We got a pbuf to make the cluster in.
863 TAILQ_INIT(&bp->b_cluster.cluster_head);
867 if (tbp->b_wcred != NOCRED)
868 bp->b_wcred = crhold(tbp->b_wcred);
870 bp->b_blkno = tbp->b_blkno;
871 bp->b_lblkno = tbp->b_lblkno;
872 bp->b_offset = tbp->b_offset;
875 * We are synthesizing a buffer out of vm_page_t's, but
876 * if the block size is not page aligned then the starting
877 * address may not be either. Inherit the b_data offset
878 * from the original buffer.
880 if ((gbflags & GB_UNMAPPED) == 0 ||
881 (tbp->b_flags & B_VMIO) == 0) {
882 bp->b_data = (char *)((vm_offset_t)bp->b_data |
883 ((vm_offset_t)tbp->b_data & PAGE_MASK));
885 bp->b_flags |= B_UNMAPPED;
886 bp->b_data = unmapped_buf;
888 bp->b_flags |= B_CLUSTER | (tbp->b_flags & (B_VMIO |
890 bp->b_iodone = cluster_callback;
893 * From this location in the file, scan forward to see
894 * if there are buffers with adjacent data that need to
895 * be written as well.
897 for (i = 0; i < len; ++i, ++start_lbn) {
898 if (i != 0) { /* If not the first buffer */
900 * If the adjacent data is not even in core it
901 * can't need to be written.
904 if ((tbp = gbincore(bo, start_lbn)) == NULL ||
905 (tbp->b_vflags & BV_BKGRDINPROG)) {
911 * If it IS in core, but has different
912 * characteristics, or is locked (which
913 * means it could be undergoing a background
914 * I/O or be in a weird state), then don't
918 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
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_wcred != bp->b_wcred) {
932 * Check that the combined cluster
933 * would make sense with regard to pages
934 * and would not be too large
936 if ((tbp->b_bcount != size) ||
937 ((bp->b_blkno + (dbsize * i)) !=
939 ((tbp->b_npages + bp->b_npages) >
940 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
946 * Do not pull in pinned buffers.
948 if (tbp->b_pin_count > 0) {
954 * Ok, it's passed all the tests,
955 * so remove it from the free list
956 * and mark it busy. We will use it.
959 tbp->b_flags &= ~B_DONE;
960 } /* end of code for non-first buffers only */
962 * If the IO is via the VM then we do some
963 * special VM hackery (yuck). Since the buffer's
964 * block size may not be page-aligned it is possible
965 * for a page to be shared between two buffers. We
966 * have to get rid of the duplication when building
969 if (tbp->b_flags & B_VMIO) {
972 VM_OBJECT_LOCK(tbp->b_bufobj->bo_object);
973 if (i != 0) { /* if not first buffer */
974 for (j = 0; j < tbp->b_npages; j += 1) {
976 if (m->oflags & VPO_BUSY) {
984 for (j = 0; j < tbp->b_npages; j += 1) {
987 vm_object_pip_add(m->object, 1);
988 if ((bp->b_npages == 0) ||
989 (bp->b_pages[bp->b_npages - 1] != m)) {
990 bp->b_pages[bp->b_npages] = m;
994 VM_OBJECT_UNLOCK(tbp->b_bufobj->bo_object);
996 bp->b_bcount += size;
997 bp->b_bufsize += size;
999 * If any of the clustered buffers have their
1000 * B_BARRIER flag set, transfer that request to
1003 bp->b_flags |= (tbp->b_flags & B_BARRIER);
1004 tbp->b_flags &= ~(B_DONE | B_BARRIER);
1005 tbp->b_flags |= B_ASYNC;
1006 tbp->b_ioflags &= ~BIO_ERROR;
1007 tbp->b_iocmd = BIO_WRITE;
1009 reassignbuf(tbp); /* put on clean list */
1010 bufobj_wref(tbp->b_bufobj);
1012 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
1013 tbp, b_cluster.cluster_entry);
1016 if ((bp->b_flags & B_UNMAPPED) == 0) {
1017 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
1018 (vm_page_t *)bp->b_pages, bp->b_npages);
1020 if (bp->b_bufsize > bp->b_kvasize)
1022 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
1023 bp->b_bufsize, bp->b_kvasize);
1024 bp->b_kvasize = bp->b_bufsize;
1025 totalwritten += bp->b_bufsize;
1027 bp->b_dirtyend = bp->b_bufsize;
1032 return totalwritten;
1036 * Collect together all the buffers in a cluster.
1037 * Plus add one additional buffer.
1039 static struct cluster_save *
1040 cluster_collectbufs(struct vnode *vp, struct buf *last_bp, int gbflags)
1042 struct cluster_save *buflist;
1047 len = vp->v_lastw - vp->v_cstart + 1;
1048 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
1049 M_SEGMENT, M_WAITOK);
1050 buflist->bs_nchildren = 0;
1051 buflist->bs_children = (struct buf **) (buflist + 1);
1052 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) {
1053 (void)bread_gb(vp, lbn, last_bp->b_bcount, NOCRED,
1055 buflist->bs_children[i] = bp;
1056 if (bp->b_blkno == bp->b_lblkno)
1057 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1060 buflist->bs_children[i] = bp = last_bp;
1061 if (bp->b_blkno == bp->b_lblkno)
1062 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1063 buflist->bs_nchildren = i + 1;