2 * SPDX-License-Identifier: BSD-3-Clause
5 * The Regents of the University of California. All rights reserved.
6 * Modifications/enhancements:
7 * Copyright (c) 1995 John S. Dyson. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)vfs_cluster.c 8.7 (Berkeley) 2/13/94
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include "opt_debug_cluster.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
47 #include <sys/vnode.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/racct.h>
51 #include <sys/resourcevar.h>
52 #include <sys/rwlock.h>
53 #include <sys/vmmeter.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_page.h>
57 #include <sys/sysctl.h>
59 #if defined(CLUSTERDEBUG)
60 static int rcluster= 0;
61 SYSCTL_INT(_debug, OID_AUTO, rcluster, CTLFLAG_RW, &rcluster, 0,
62 "Debug VFS clustering code");
65 static MALLOC_DEFINE(M_SEGMENT, "cl_savebuf", "cluster_save buffer");
67 static struct cluster_save *cluster_collectbufs(struct vnode *vp,
68 struct buf *last_bp, int gbflags);
69 static struct buf *cluster_rbuild(struct vnode *vp, u_quad_t filesize,
70 daddr_t lbn, daddr_t blkno, long size, int run, int gbflags,
72 static void cluster_callback(struct buf *);
74 static int write_behind = 1;
75 SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0,
76 "Cluster write-behind; 0: disable, 1: enable, 2: backed off");
78 static int read_max = 64;
79 SYSCTL_INT(_vfs, OID_AUTO, read_max, CTLFLAG_RW, &read_max, 0,
80 "Cluster read-ahead max block count");
82 static int read_min = 1;
83 SYSCTL_INT(_vfs, OID_AUTO, read_min, CTLFLAG_RW, &read_min, 0,
84 "Cluster read min block count");
87 * Read data to a buf, including read-ahead if we find this to be beneficial.
88 * cluster_read replaces bread.
91 cluster_read(struct vnode *vp, u_quad_t filesize, daddr_t lblkno, long size,
92 struct ucred *cred, long totread, int seqcount, int gbflags,
95 struct buf *bp, *rbp, *reqbp;
98 daddr_t blkno, origblkno;
106 if (!unmapped_buf_allowed)
107 gbflags &= ~GB_UNMAPPED;
110 * Try to limit the amount of read-ahead by a few
111 * ad-hoc parameters. This needs work!!!
113 racluster = vp->v_mount->mnt_iosize_max / size;
115 maxra = min(read_max, maxra);
116 maxra = min(nbuf/8, maxra);
117 if (((u_quad_t)(lblkno + maxra + 1) * size) > filesize)
118 maxra = (filesize / size) - lblkno;
121 * get the requested block
123 error = getblkx(vp, lblkno, size, 0, 0, gbflags, &bp);
128 gbflags &= ~GB_NOSPARSE;
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);
252 PROC_LOCK(td->td_proc);
253 racct_add_buf(td->td_proc, bp, 0);
254 PROC_UNLOCK(td->td_proc);
257 td->td_ru.ru_inblock++;
261 * If we have been doing sequential I/O, then do some read-ahead.
263 while (lblkno < (origblkno + maxra)) {
264 error = VOP_BMAP(vp, lblkno, NULL, &blkno, &ncontig, NULL);
272 * We could throttle ncontig here by maxra but we might as
273 * well read the data if it is contiguous. We're throttled
274 * by racluster anyway.
277 ncontig = min(ncontig + 1, racluster);
278 rbp = cluster_rbuild(vp, filesize, lblkno, blkno,
279 size, ncontig, gbflags, NULL);
280 lblkno += (rbp->b_bufsize / size);
281 if (rbp->b_flags & B_DELWRI) {
286 rbp = getblk(vp, lblkno, size, 0, 0, gbflags);
288 if (rbp->b_flags & B_DELWRI) {
292 rbp->b_flags |= B_ASYNC | B_RAM;
293 rbp->b_iocmd = BIO_READ;
294 rbp->b_blkno = blkno;
296 if (rbp->b_flags & B_CACHE) {
297 rbp->b_flags &= ~B_ASYNC;
301 if ((rbp->b_flags & B_CLUSTER) == 0) {
302 vfs_busy_pages(rbp, 0);
304 rbp->b_flags &= ~B_INVAL;
305 rbp->b_ioflags &= ~BIO_ERROR;
306 if ((rbp->b_flags & B_ASYNC) || rbp->b_iodone != NULL)
308 rbp->b_iooffset = dbtob(rbp->b_blkno);
312 PROC_LOCK(td->td_proc);
313 racct_add_buf(td->td_proc, rbp, 0);
314 PROC_UNLOCK(td->td_proc);
317 td->td_ru.ru_inblock++;
322 * Like bread, always brelse() the buffer when
323 * returning an error.
325 error = bufwait(reqbp);
335 * If blocks are contiguous on disk, use this to provide clustered
336 * read ahead. We will read as many blocks as possible sequentially
337 * and then parcel them up into logical blocks in the buffer hash table.
340 cluster_rbuild(struct vnode *vp, u_quad_t filesize, daddr_t lbn,
341 daddr_t blkno, long size, int run, int gbflags, struct buf *fbp)
343 struct buf *bp, *tbp;
347 int i, inc, j, k, toff;
349 KASSERT(size == vp->v_mount->mnt_stat.f_iosize,
350 ("cluster_rbuild: size %ld != f_iosize %jd\n",
351 size, (intmax_t)vp->v_mount->mnt_stat.f_iosize));
356 while ((u_quad_t) size * (lbn + run) > filesize) {
362 tbp->b_iocmd = BIO_READ;
364 tbp = getblk(vp, lbn, size, 0, 0, gbflags);
365 if (tbp->b_flags & B_CACHE)
367 tbp->b_flags |= B_ASYNC | B_RAM;
368 tbp->b_iocmd = BIO_READ;
370 tbp->b_blkno = blkno;
371 if( (tbp->b_flags & B_MALLOC) ||
372 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
375 bp = trypbuf(&cluster_pbuf_freecnt);
380 * We are synthesizing a buffer out of vm_page_t's, but
381 * if the block size is not page aligned then the starting
382 * address may not be either. Inherit the b_data offset
383 * from the original buffer.
385 bp->b_flags = B_ASYNC | B_CLUSTER | B_VMIO;
386 if ((gbflags & GB_UNMAPPED) != 0) {
387 bp->b_data = unmapped_buf;
389 bp->b_data = (char *)((vm_offset_t)bp->b_data |
390 ((vm_offset_t)tbp->b_data & PAGE_MASK));
392 bp->b_iocmd = BIO_READ;
393 bp->b_iodone = cluster_callback;
396 bp->b_offset = tbp->b_offset;
397 KASSERT(bp->b_offset != NOOFFSET, ("cluster_rbuild: no buffer offset"));
400 TAILQ_INIT(&bp->b_cluster.cluster_head);
407 for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
409 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
410 vfs_drain_busy_pages(tbp);
411 vm_object_pip_add(tbp->b_bufobj->bo_object,
413 for (k = 0; k < tbp->b_npages; k++)
414 vm_page_sbusy(tbp->b_pages[k]);
415 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
417 if ((bp->b_npages * PAGE_SIZE) +
418 round_page(size) > vp->v_mount->mnt_iosize_max) {
422 tbp = getblk(vp, lbn + i, size, 0, 0, GB_LOCK_NOWAIT |
423 (gbflags & GB_UNMAPPED));
425 /* Don't wait around for locked bufs. */
430 * Stop scanning if the buffer is fully valid
431 * (marked B_CACHE), or locked (may be doing a
432 * background write), or if the buffer is not
433 * VMIO backed. The clustering code can only deal
434 * with VMIO-backed buffers. The bo lock is not
435 * required for the BKGRDINPROG check since it
436 * can not be set without the buf lock.
438 if ((tbp->b_vflags & BV_BKGRDINPROG) ||
439 (tbp->b_flags & B_CACHE) ||
440 (tbp->b_flags & B_VMIO) == 0) {
446 * The buffer must be completely invalid in order to
447 * take part in the cluster. If it is partially valid
452 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
453 for (j = 0; tsize > 0; j++) {
454 toff = off & PAGE_MASK;
456 if (toff + tinc > PAGE_SIZE)
457 tinc = PAGE_SIZE - toff;
458 VM_OBJECT_ASSERT_WLOCKED(tbp->b_pages[j]->object);
459 if ((tbp->b_pages[j]->valid &
460 vm_page_bits(toff, tinc)) != 0)
462 if (vm_page_xbusied(tbp->b_pages[j]))
464 vm_object_pip_add(tbp->b_bufobj->bo_object, 1);
465 vm_page_sbusy(tbp->b_pages[j]);
471 vm_object_pip_add(tbp->b_bufobj->bo_object, -j);
472 for (k = 0; k < j; k++)
473 vm_page_sunbusy(tbp->b_pages[k]);
474 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
478 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
481 * Set a read-ahead mark as appropriate
483 if ((fbp && (i == 1)) || (i == (run - 1)))
484 tbp->b_flags |= B_RAM;
487 * Set the buffer up for an async read (XXX should
488 * we do this only if we do not wind up brelse()ing?).
489 * Set the block number if it isn't set, otherwise
490 * if it is make sure it matches the block number we
493 tbp->b_flags |= B_ASYNC;
494 tbp->b_iocmd = BIO_READ;
495 if (tbp->b_blkno == tbp->b_lblkno) {
497 } else if (tbp->b_blkno != bn) {
498 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
503 * XXX fbp from caller may not be B_ASYNC, but we are going
504 * to biodone() it in cluster_callback() anyway
507 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
508 tbp, b_cluster.cluster_entry);
509 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
510 for (j = 0; j < tbp->b_npages; j += 1) {
513 if ((bp->b_npages == 0) ||
514 (bp->b_pages[bp->b_npages-1] != m)) {
515 bp->b_pages[bp->b_npages] = m;
518 if (m->valid == VM_PAGE_BITS_ALL)
519 tbp->b_pages[j] = bogus_page;
521 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
523 * Don't inherit tbp->b_bufsize as it may be larger due to
524 * a non-page-aligned size. Instead just aggregate using
527 if (tbp->b_bcount != size)
528 printf("warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size);
529 if (tbp->b_bufsize != size)
530 printf("warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size);
531 bp->b_bcount += size;
532 bp->b_bufsize += size;
536 * Fully valid pages in the cluster are already good and do not need
537 * to be re-read from disk. Replace the page with bogus_page
539 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
540 for (j = 0; j < bp->b_npages; j++) {
541 VM_OBJECT_ASSERT_WLOCKED(bp->b_pages[j]->object);
542 if (bp->b_pages[j]->valid == VM_PAGE_BITS_ALL)
543 bp->b_pages[j] = bogus_page;
545 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
546 if (bp->b_bufsize > bp->b_kvasize)
547 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
548 bp->b_bufsize, bp->b_kvasize);
550 if (buf_mapped(bp)) {
551 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
552 (vm_page_t *)bp->b_pages, bp->b_npages);
558 * Cleanup after a clustered read or write.
559 * This is complicated by the fact that any of the buffers might have
560 * extra memory (if there were no empty buffer headers at allocbuf time)
561 * that we will need to shift around.
567 struct buf *nbp, *tbp;
571 * Must propagate errors to all the components.
573 if (bp->b_ioflags & BIO_ERROR)
576 if (buf_mapped(bp)) {
577 pmap_qremove(trunc_page((vm_offset_t) bp->b_data),
581 * Move memory from the large cluster buffer into the component
582 * buffers and mark IO as done on these.
584 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head);
586 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry);
588 tbp->b_ioflags |= BIO_ERROR;
589 tbp->b_error = error;
591 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
592 tbp->b_flags &= ~B_INVAL;
593 tbp->b_ioflags &= ~BIO_ERROR;
595 * XXX the bdwrite()/bqrelse() issued during
596 * cluster building clears B_RELBUF (see bqrelse()
597 * comment). If direct I/O was specified, we have
598 * to restore it here to allow the buffer and VM
601 if (tbp->b_flags & B_DIRECT)
602 tbp->b_flags |= B_RELBUF;
607 relpbuf(bp, &cluster_pbuf_freecnt);
613 * Implement modified write build for cluster.
615 * write_behind = 0 write behind disabled
616 * write_behind = 1 write behind normal (default)
617 * write_behind = 2 write behind backed-off
621 cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len,
626 switch (write_behind) {
633 r = cluster_wbuild(vp, size, start_lbn, len, gbflags);
643 * Do clustered write for FFS.
646 * 1. Write is not sequential (write asynchronously)
647 * Write is sequential:
648 * 2. beginning of cluster - begin cluster
649 * 3. middle of a cluster - add to cluster
650 * 4. end of a cluster - asynchronously write cluster
653 cluster_write(struct vnode *vp, struct buf *bp, u_quad_t filesize, int seqcount,
657 int maxclen, cursize;
661 if (!unmapped_buf_allowed)
662 gbflags &= ~GB_UNMAPPED;
664 if (vp->v_type == VREG) {
665 async = DOINGASYNC(vp);
666 lblocksize = vp->v_mount->mnt_stat.f_iosize;
669 lblocksize = bp->b_bufsize;
672 KASSERT(bp->b_offset != NOOFFSET, ("cluster_write: no buffer offset"));
674 /* Initialize vnode to beginning of file. */
676 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
678 if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 ||
679 (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) {
680 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1;
681 if (vp->v_clen != 0) {
683 * Next block is not sequential.
685 * If we are not writing at end of file, the process
686 * seeked to another point in the file since its last
687 * write, or we have reached our maximum cluster size,
688 * then push the previous cluster. Otherwise try
689 * reallocating to make it sequential.
691 * Change to algorithm: only push previous cluster if
692 * it was sequential from the point of view of the
693 * seqcount heuristic, otherwise leave the buffer
694 * intact so we can potentially optimize the I/O
695 * later on in the buf_daemon or update daemon
698 cursize = vp->v_lastw - vp->v_cstart + 1;
699 if (((u_quad_t) bp->b_offset + lblocksize) != filesize ||
700 lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) {
701 if (!async && seqcount > 0) {
702 cluster_wbuild_wb(vp, lblocksize,
703 vp->v_cstart, cursize, gbflags);
706 struct buf **bpp, **endbp;
707 struct cluster_save *buflist;
709 buflist = cluster_collectbufs(vp, bp, gbflags);
710 endbp = &buflist->bs_children
711 [buflist->bs_nchildren - 1];
712 if (VOP_REALLOCBLKS(vp, buflist)) {
714 * Failed, push the previous cluster
715 * if *really* writing sequentially
716 * in the logical file (seqcount > 1),
717 * otherwise delay it in the hopes that
718 * the low level disk driver can
719 * optimize the write ordering.
721 for (bpp = buflist->bs_children;
724 free(buflist, M_SEGMENT);
726 cluster_wbuild_wb(vp,
727 lblocksize, vp->v_cstart,
732 * Succeeded, keep building cluster.
734 for (bpp = buflist->bs_children;
737 free(buflist, M_SEGMENT);
739 vp->v_lasta = bp->b_blkno;
745 * Consider beginning a cluster. If at end of file, make
746 * cluster as large as possible, otherwise find size of
749 if ((vp->v_type == VREG) &&
750 ((u_quad_t) bp->b_offset + lblocksize) != filesize &&
751 (bp->b_blkno == bp->b_lblkno) &&
752 (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) ||
753 bp->b_blkno == -1)) {
756 vp->v_lasta = bp->b_blkno;
757 vp->v_cstart = lbn + 1;
761 vp->v_clen = maxclen;
762 if (!async && maxclen == 0) { /* I/O not contiguous */
763 vp->v_cstart = lbn + 1;
765 } else { /* Wait for rest of cluster */
769 } else if (lbn == vp->v_cstart + vp->v_clen) {
771 * At end of cluster, write it out if seqcount tells us we
772 * are operating sequentially, otherwise let the buf or
773 * update daemon handle it.
777 cluster_wbuild_wb(vp, lblocksize, vp->v_cstart,
778 vp->v_clen + 1, gbflags);
781 vp->v_cstart = lbn + 1;
782 } else if (vm_page_count_severe()) {
784 * We are low on memory, get it going NOW
789 * In the middle of a cluster, so just delay the I/O for now.
794 vp->v_lasta = bp->b_blkno;
799 * This is an awful lot like cluster_rbuild...wish they could be combined.
800 * The last lbn argument is the current block on which I/O is being
801 * performed. Check to see that it doesn't fall in the middle of
802 * the current block (if last_bp == NULL).
805 cluster_wbuild(struct vnode *vp, long size, daddr_t start_lbn, int len,
808 struct buf *bp, *tbp;
811 int totalwritten = 0;
812 int dbsize = btodb(size);
814 if (!unmapped_buf_allowed)
815 gbflags &= ~GB_UNMAPPED;
820 * If the buffer is not delayed-write (i.e. dirty), or it
821 * is delayed-write but either locked or inval, it cannot
822 * partake in the clustered write.
825 if ((tbp = gbincore(&vp->v_bufobj, start_lbn)) == NULL ||
826 (tbp->b_vflags & BV_BKGRDINPROG)) {
833 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, BO_LOCKPTR(bo))) {
838 if ((tbp->b_flags & (B_INVAL | B_DELWRI)) != B_DELWRI) {
845 tbp->b_flags &= ~B_DONE;
848 * Extra memory in the buffer, punt on this buffer.
849 * XXX we could handle this in most cases, but we would
850 * have to push the extra memory down to after our max
851 * possible cluster size and then potentially pull it back
852 * up if the cluster was terminated prematurely--too much
855 if (((tbp->b_flags & (B_CLUSTEROK | B_MALLOC | B_VMIO)) !=
856 (B_CLUSTEROK | B_VMIO)) ||
857 (tbp->b_bcount != tbp->b_bufsize) ||
858 (tbp->b_bcount != size) ||
860 ((bp = (vp->v_vflag & VV_MD) != 0 ?
861 trypbuf(&cluster_pbuf_freecnt) :
862 getpbuf(&cluster_pbuf_freecnt)) == NULL)) {
863 totalwritten += tbp->b_bufsize;
871 * We got a pbuf to make the cluster in.
874 TAILQ_INIT(&bp->b_cluster.cluster_head);
878 if (tbp->b_wcred != NOCRED)
879 bp->b_wcred = crhold(tbp->b_wcred);
881 bp->b_blkno = tbp->b_blkno;
882 bp->b_lblkno = tbp->b_lblkno;
883 bp->b_offset = tbp->b_offset;
886 * We are synthesizing a buffer out of vm_page_t's, but
887 * if the block size is not page aligned then the starting
888 * address may not be either. Inherit the b_data offset
889 * from the original buffer.
891 if ((gbflags & GB_UNMAPPED) == 0 ||
892 (tbp->b_flags & B_VMIO) == 0) {
893 bp->b_data = (char *)((vm_offset_t)bp->b_data |
894 ((vm_offset_t)tbp->b_data & PAGE_MASK));
896 bp->b_data = unmapped_buf;
898 bp->b_flags |= B_CLUSTER | (tbp->b_flags & (B_VMIO |
900 bp->b_iodone = cluster_callback;
903 * From this location in the file, scan forward to see
904 * if there are buffers with adjacent data that need to
905 * be written as well.
907 for (i = 0; i < len; ++i, ++start_lbn) {
908 if (i != 0) { /* If not the first buffer */
910 * If the adjacent data is not even in core it
911 * can't need to be written.
914 if ((tbp = gbincore(bo, start_lbn)) == NULL ||
915 (tbp->b_vflags & BV_BKGRDINPROG)) {
921 * If it IS in core, but has different
922 * characteristics, or is locked (which
923 * means it could be undergoing a background
924 * I/O or be in a weird state), then don't
928 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
932 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
933 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
934 != (B_DELWRI | B_CLUSTEROK |
935 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
936 tbp->b_wcred != bp->b_wcred) {
942 * Check that the combined cluster
943 * would make sense with regard to pages
944 * and would not be too large
946 if ((tbp->b_bcount != size) ||
947 ((bp->b_blkno + (dbsize * i)) !=
949 ((tbp->b_npages + bp->b_npages) >
950 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
956 * Ok, it's passed all the tests,
957 * so remove it from the free list
958 * and mark it busy. We will use it.
961 tbp->b_flags &= ~B_DONE;
962 } /* end of code for non-first buffers only */
964 * If the IO is via the VM then we do some
965 * special VM hackery (yuck). Since the buffer's
966 * block size may not be page-aligned it is possible
967 * for a page to be shared between two buffers. We
968 * have to get rid of the duplication when building
971 if (tbp->b_flags & B_VMIO) {
974 VM_OBJECT_WLOCK(tbp->b_bufobj->bo_object);
976 vfs_drain_busy_pages(tbp);
977 } else { /* if not first buffer */
978 for (j = 0; j < tbp->b_npages; j += 1) {
980 if (vm_page_xbusied(m)) {
988 for (j = 0; j < tbp->b_npages; j += 1) {
991 vm_object_pip_add(m->object, 1);
992 if ((bp->b_npages == 0) ||
993 (bp->b_pages[bp->b_npages - 1] != m)) {
994 bp->b_pages[bp->b_npages] = m;
998 VM_OBJECT_WUNLOCK(tbp->b_bufobj->bo_object);
1000 bp->b_bcount += size;
1001 bp->b_bufsize += size;
1003 * If any of the clustered buffers have their
1004 * B_BARRIER flag set, transfer that request to
1007 bp->b_flags |= (tbp->b_flags & B_BARRIER);
1008 tbp->b_flags &= ~(B_DONE | B_BARRIER);
1009 tbp->b_flags |= B_ASYNC;
1010 tbp->b_ioflags &= ~BIO_ERROR;
1011 tbp->b_iocmd = BIO_WRITE;
1013 reassignbuf(tbp); /* put on clean list */
1014 bufobj_wref(tbp->b_bufobj);
1016 buf_track(tbp, __func__);
1017 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
1018 tbp, b_cluster.cluster_entry);
1021 if (buf_mapped(bp)) {
1022 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
1023 (vm_page_t *)bp->b_pages, bp->b_npages);
1025 if (bp->b_bufsize > bp->b_kvasize)
1027 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
1028 bp->b_bufsize, bp->b_kvasize);
1029 totalwritten += bp->b_bufsize;
1031 bp->b_dirtyend = bp->b_bufsize;
1036 return totalwritten;
1040 * Collect together all the buffers in a cluster.
1041 * Plus add one additional buffer.
1043 static struct cluster_save *
1044 cluster_collectbufs(struct vnode *vp, struct buf *last_bp, int gbflags)
1046 struct cluster_save *buflist;
1051 len = vp->v_lastw - vp->v_cstart + 1;
1052 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
1053 M_SEGMENT, M_WAITOK);
1054 buflist->bs_nchildren = 0;
1055 buflist->bs_children = (struct buf **) (buflist + 1);
1056 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) {
1057 (void)bread_gb(vp, lbn, last_bp->b_bcount, NOCRED,
1059 buflist->bs_children[i] = bp;
1060 if (bp->b_blkno == bp->b_lblkno)
1061 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1064 buflist->bs_children[i] = bp = last_bp;
1065 if (bp->b_blkno == bp->b_lblkno)
1066 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1067 buflist->bs_nchildren = i + 1;