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 <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/racct.h>
49 #include <sys/resourcevar.h>
50 #include <sys/rwlock.h>
51 #include <sys/vmmeter.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_page.h>
55 #include <sys/sysctl.h>
57 static MALLOC_DEFINE(M_SEGMENT, "cl_savebuf", "cluster_save buffer");
58 static uma_zone_t cluster_pbuf_zone;
60 static void cluster_init(void *);
61 static struct cluster_save *cluster_collectbufs(struct vnode *vp,
62 struct buf *last_bp, int gbflags);
63 static struct buf *cluster_rbuild(struct vnode *vp, u_quad_t filesize,
64 daddr_t lbn, daddr_t blkno, long size, int run, int gbflags,
66 static void cluster_callback(struct buf *);
68 static int write_behind = 1;
69 SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0,
70 "Cluster write-behind; 0: disable, 1: enable, 2: backed off");
72 static int read_max = 64;
73 SYSCTL_INT(_vfs, OID_AUTO, read_max, CTLFLAG_RW, &read_max, 0,
74 "Cluster read-ahead max block count");
76 static int read_min = 1;
77 SYSCTL_INT(_vfs, OID_AUTO, read_min, CTLFLAG_RW, &read_min, 0,
78 "Cluster read min block count");
80 SYSINIT(cluster, SI_SUB_CPU, SI_ORDER_ANY, cluster_init, NULL);
83 cluster_init(void *dummy)
86 cluster_pbuf_zone = pbuf_zsecond_create("clpbuf", nswbuf / 2);
90 * Read data to a buf, including read-ahead if we find this to be beneficial.
91 * cluster_read replaces bread.
94 cluster_read(struct vnode *vp, u_quad_t filesize, daddr_t lblkno, long size,
95 struct ucred *cred, long totread, int seqcount, int gbflags,
98 struct buf *bp, *rbp, *reqbp;
101 daddr_t blkno, origblkno;
102 int maxra, racluster;
109 if (!unmapped_buf_allowed)
110 gbflags &= ~GB_UNMAPPED;
113 * Try to limit the amount of read-ahead by a few
114 * ad-hoc parameters. This needs work!!!
116 racluster = vp->v_mount->mnt_iosize_max / size;
118 maxra = min(read_max, maxra);
119 maxra = min(nbuf/8, maxra);
120 if (((u_quad_t)(lblkno + maxra + 1) * size) > filesize)
121 maxra = (filesize / size) - lblkno;
124 * get the requested block
126 error = getblkx(vp, lblkno, lblkno, size, 0, 0, gbflags, &bp);
131 gbflags &= ~GB_NOSPARSE;
136 * if it is in the cache, then check to see if the reads have been
137 * sequential. If they have, then try some read-ahead, otherwise
138 * back-off on prospective read-aheads.
140 if (bp->b_flags & B_CACHE) {
143 } else if ((bp->b_flags & B_RAM) == 0) {
146 bp->b_flags &= ~B_RAM;
148 for (i = 1; i < maxra; i++) {
150 * Stop if the buffer does not exist or it
151 * is invalid (about to go away?)
153 rbp = gbincore(&vp->v_bufobj, lblkno+i);
154 if (rbp == NULL || (rbp->b_flags & B_INVAL))
158 * Set another read-ahead mark so we know
159 * to check again. (If we can lock the
160 * buffer without waiting)
162 if ((((i % racluster) == (racluster - 1)) ||
164 && (0 == BUF_LOCK(rbp,
165 LK_EXCLUSIVE | LK_NOWAIT, NULL))) {
166 rbp->b_flags |= B_RAM;
178 * If it isn't in the cache, then get a chunk from
179 * disk if sequential, otherwise just get the block.
182 off_t firstread = bp->b_offset;
186 KASSERT(bp->b_offset != NOOFFSET,
187 ("cluster_read: no buffer offset"));
192 * Adjust totread if needed
194 minread = read_min * size;
195 if (minread > totread)
199 * Compute the total number of blocks that we should read
202 if (firstread + totread > filesize)
203 totread = filesize - firstread;
204 nblks = howmany(totread, size);
205 if (nblks > racluster)
209 * Now compute the number of contiguous blocks.
212 error = VOP_BMAP(vp, lblkno, NULL,
213 &blkno, &ncontig, NULL);
215 * If this failed to map just do the original block.
217 if (error || blkno == -1)
222 * If we have contiguous data available do a cluster
223 * otherwise just read the requested block.
226 /* Account for our first block. */
227 ncontig = min(ncontig + 1, nblks);
230 bp = cluster_rbuild(vp, filesize, lblkno,
231 blkno, size, nblks, gbflags, bp);
232 lblkno += (bp->b_bufsize / size);
234 bp->b_flags |= B_RAM;
235 bp->b_iocmd = BIO_READ;
241 * handle the synchronous read so that it is available ASAP.
244 if ((bp->b_flags & B_CLUSTER) == 0) {
245 vfs_busy_pages(bp, 0);
247 bp->b_flags &= ~B_INVAL;
248 bp->b_ioflags &= ~BIO_ERROR;
249 if ((bp->b_flags & B_ASYNC) || bp->b_iodone != NULL)
251 bp->b_iooffset = dbtob(bp->b_blkno);
255 PROC_LOCK(td->td_proc);
256 racct_add_buf(td->td_proc, bp, 0);
257 PROC_UNLOCK(td->td_proc);
260 td->td_ru.ru_inblock++;
264 * If we have been doing sequential I/O, then do some read-ahead.
266 while (lblkno < (origblkno + maxra)) {
267 error = VOP_BMAP(vp, lblkno, NULL, &blkno, &ncontig, NULL);
275 * We could throttle ncontig here by maxra but we might as
276 * well read the data if it is contiguous. We're throttled
277 * by racluster anyway.
280 ncontig = min(ncontig + 1, racluster);
281 rbp = cluster_rbuild(vp, filesize, lblkno, blkno,
282 size, ncontig, gbflags, NULL);
283 lblkno += (rbp->b_bufsize / size);
284 if (rbp->b_flags & B_DELWRI) {
289 rbp = getblk(vp, lblkno, size, 0, 0, gbflags);
291 if (rbp->b_flags & B_DELWRI) {
295 rbp->b_flags |= B_ASYNC | B_RAM;
296 rbp->b_iocmd = BIO_READ;
297 rbp->b_blkno = blkno;
299 if (rbp->b_flags & B_CACHE) {
300 rbp->b_flags &= ~B_ASYNC;
304 if ((rbp->b_flags & B_CLUSTER) == 0) {
305 vfs_busy_pages(rbp, 0);
307 rbp->b_flags &= ~B_INVAL;
308 rbp->b_ioflags &= ~BIO_ERROR;
309 if ((rbp->b_flags & B_ASYNC) || rbp->b_iodone != NULL)
311 rbp->b_iooffset = dbtob(rbp->b_blkno);
315 PROC_LOCK(td->td_proc);
316 racct_add_buf(td->td_proc, rbp, 0);
317 PROC_UNLOCK(td->td_proc);
320 td->td_ru.ru_inblock++;
325 * Like bread, always brelse() the buffer when
326 * returning an error.
328 error = bufwait(reqbp);
338 * If blocks are contiguous on disk, use this to provide clustered
339 * read ahead. We will read as many blocks as possible sequentially
340 * and then parcel them up into logical blocks in the buffer hash table.
343 cluster_rbuild(struct vnode *vp, u_quad_t filesize, daddr_t lbn,
344 daddr_t blkno, long size, int run, int gbflags, struct buf *fbp)
346 struct buf *bp, *tbp;
350 int i, inc, j, k, toff;
352 KASSERT(size == vp->v_mount->mnt_stat.f_iosize,
353 ("cluster_rbuild: size %ld != f_iosize %jd\n",
354 size, (intmax_t)vp->v_mount->mnt_stat.f_iosize));
359 while ((u_quad_t) size * (lbn + run) > filesize) {
365 tbp->b_iocmd = BIO_READ;
367 tbp = getblk(vp, lbn, size, 0, 0, gbflags);
368 if (tbp->b_flags & B_CACHE)
370 tbp->b_flags |= B_ASYNC | B_RAM;
371 tbp->b_iocmd = BIO_READ;
373 tbp->b_blkno = blkno;
374 if( (tbp->b_flags & B_MALLOC) ||
375 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
378 bp = uma_zalloc(cluster_pbuf_zone, M_NOWAIT);
381 MPASS((bp->b_flags & B_MAXPHYS) != 0);
384 * We are synthesizing a buffer out of vm_page_t's, but
385 * if the block size is not page aligned then the starting
386 * address may not be either. Inherit the b_data offset
387 * from the original buffer.
389 bp->b_flags = B_ASYNC | B_CLUSTER | B_VMIO;
390 if ((gbflags & GB_UNMAPPED) != 0) {
391 bp->b_data = unmapped_buf;
393 bp->b_data = (char *)((vm_offset_t)bp->b_data |
394 ((vm_offset_t)tbp->b_data & PAGE_MASK));
396 bp->b_iocmd = BIO_READ;
397 bp->b_iodone = cluster_callback;
400 bp->b_offset = tbp->b_offset;
401 KASSERT(bp->b_offset != NOOFFSET, ("cluster_rbuild: no buffer offset"));
404 TAILQ_INIT(&bp->b_cluster.cluster_head);
411 for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
413 vm_object_pip_add(tbp->b_bufobj->bo_object,
415 vfs_busy_pages_acquire(tbp);
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 for (j = 0; tsize > 0; j++) {
453 toff = off & PAGE_MASK;
455 if (toff + tinc > PAGE_SIZE)
456 tinc = PAGE_SIZE - toff;
457 if (vm_page_trysbusy(tbp->b_pages[j]) == 0)
459 if ((tbp->b_pages[j]->valid &
460 vm_page_bits(toff, tinc)) != 0) {
461 vm_page_sunbusy(tbp->b_pages[j]);
464 vm_object_pip_add(tbp->b_bufobj->bo_object, 1);
470 vm_object_pip_wakeupn(tbp->b_bufobj->bo_object,
472 for (k = 0; k < j; k++)
473 vm_page_sunbusy(tbp->b_pages[k]);
479 * Set a read-ahead mark as appropriate
481 if ((fbp && (i == 1)) || (i == (run - 1)))
482 tbp->b_flags |= B_RAM;
485 * Set the buffer up for an async read (XXX should
486 * we do this only if we do not wind up brelse()ing?).
487 * Set the block number if it isn't set, otherwise
488 * if it is make sure it matches the block number we
491 tbp->b_flags |= B_ASYNC;
492 tbp->b_iocmd = BIO_READ;
493 if (tbp->b_blkno == tbp->b_lblkno) {
495 } else if (tbp->b_blkno != bn) {
500 * XXX fbp from caller may not be B_ASYNC, but we are going
501 * to biodone() it in cluster_callback() anyway
504 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
505 tbp, b_cluster.cluster_entry);
506 for (j = 0; j < tbp->b_npages; j += 1) {
510 if ((bp->b_npages == 0) ||
511 (bp->b_pages[bp->b_npages-1] != m)) {
512 bp->b_pages[bp->b_npages] = m;
515 if (vm_page_all_valid(m))
516 tbp->b_pages[j] = bogus_page;
520 * Don't inherit tbp->b_bufsize as it may be larger due to
521 * a non-page-aligned size. Instead just aggregate using
524 if (tbp->b_bcount != size)
525 printf("warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size);
526 if (tbp->b_bufsize != size)
527 printf("warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size);
528 bp->b_bcount += size;
529 bp->b_bufsize += size;
533 * Fully valid pages in the cluster are already good and do not need
534 * to be re-read from disk. Replace the page with bogus_page
536 for (j = 0; j < bp->b_npages; j++) {
537 if (vm_page_all_valid(bp->b_pages[j]))
538 bp->b_pages[j] = bogus_page;
540 if (bp->b_bufsize > bp->b_kvasize)
541 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
542 bp->b_bufsize, bp->b_kvasize);
544 if (buf_mapped(bp)) {
545 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
546 (vm_page_t *)bp->b_pages, bp->b_npages);
552 * Cleanup after a clustered read or write.
553 * This is complicated by the fact that any of the buffers might have
554 * extra memory (if there were no empty buffer headers at allocbuf time)
555 * that we will need to shift around.
558 cluster_callback(struct buf *bp)
560 struct buf *nbp, *tbp;
564 * Must propagate errors to all the components.
566 if (bp->b_ioflags & BIO_ERROR)
569 if (buf_mapped(bp)) {
570 pmap_qremove(trunc_page((vm_offset_t) bp->b_data),
574 * Move memory from the large cluster buffer into the component
575 * buffers and mark IO as done on these.
577 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head);
579 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry);
581 tbp->b_ioflags |= BIO_ERROR;
582 tbp->b_error = error;
584 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
585 tbp->b_flags &= ~B_INVAL;
586 tbp->b_ioflags &= ~BIO_ERROR;
588 * XXX the bdwrite()/bqrelse() issued during
589 * cluster building clears B_RELBUF (see bqrelse()
590 * comment). If direct I/O was specified, we have
591 * to restore it here to allow the buffer and VM
594 if (tbp->b_flags & B_DIRECT)
595 tbp->b_flags |= B_RELBUF;
600 uma_zfree(cluster_pbuf_zone, bp);
606 * Implement modified write build for cluster.
608 * write_behind = 0 write behind disabled
609 * write_behind = 1 write behind normal (default)
610 * write_behind = 2 write behind backed-off
614 cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len,
619 switch (write_behind) {
626 r = cluster_wbuild(vp, size, start_lbn, len, gbflags);
636 * Do clustered write for FFS.
639 * 1. Write is not sequential (write asynchronously)
640 * Write is sequential:
641 * 2. beginning of cluster - begin cluster
642 * 3. middle of a cluster - add to cluster
643 * 4. end of a cluster - asynchronously write cluster
646 cluster_write(struct vnode *vp, struct buf *bp, u_quad_t filesize, int seqcount,
650 int maxclen, cursize;
654 if (!unmapped_buf_allowed)
655 gbflags &= ~GB_UNMAPPED;
657 if (vp->v_type == VREG) {
658 async = DOINGASYNC(vp);
659 lblocksize = vp->v_mount->mnt_stat.f_iosize;
662 lblocksize = bp->b_bufsize;
665 KASSERT(bp->b_offset != NOOFFSET, ("cluster_write: no buffer offset"));
667 /* Initialize vnode to beginning of file. */
669 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
671 if (vp->v_clen == 0 || lbn != vp->v_lastw + 1 ||
672 (bp->b_blkno != vp->v_lasta + btodb(lblocksize))) {
673 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1;
674 if (vp->v_clen != 0) {
676 * Next block is not sequential.
678 * If we are not writing at end of file, the process
679 * seeked to another point in the file since its last
680 * write, or we have reached our maximum cluster size,
681 * then push the previous cluster. Otherwise try
682 * reallocating to make it sequential.
684 * Change to algorithm: only push previous cluster if
685 * it was sequential from the point of view of the
686 * seqcount heuristic, otherwise leave the buffer
687 * intact so we can potentially optimize the I/O
688 * later on in the buf_daemon or update daemon
691 cursize = vp->v_lastw - vp->v_cstart + 1;
692 if (((u_quad_t) bp->b_offset + lblocksize) != filesize ||
693 lbn != vp->v_lastw + 1 || vp->v_clen <= cursize) {
694 if (!async && seqcount > 0) {
695 cluster_wbuild_wb(vp, lblocksize,
696 vp->v_cstart, cursize, gbflags);
699 struct buf **bpp, **endbp;
700 struct cluster_save *buflist;
702 buflist = cluster_collectbufs(vp, bp, gbflags);
703 if (buflist == NULL) {
705 * Cluster build failed so just write
711 endbp = &buflist->bs_children
712 [buflist->bs_nchildren - 1];
713 if (VOP_REALLOCBLKS(vp, buflist)) {
715 * Failed, push the previous cluster
716 * if *really* writing sequentially
717 * in the logical file (seqcount > 1),
718 * otherwise delay it in the hopes that
719 * the low level disk driver can
720 * optimize the write ordering.
722 for (bpp = buflist->bs_children;
725 free(buflist, M_SEGMENT);
727 cluster_wbuild_wb(vp,
728 lblocksize, vp->v_cstart,
733 * Succeeded, keep building cluster.
735 for (bpp = buflist->bs_children;
738 free(buflist, M_SEGMENT);
740 vp->v_lasta = bp->b_blkno;
746 * Consider beginning a cluster. If at end of file, make
747 * cluster as large as possible, otherwise find size of
750 if ((vp->v_type == VREG) &&
751 ((u_quad_t) bp->b_offset + lblocksize) != filesize &&
752 (bp->b_blkno == bp->b_lblkno) &&
753 (VOP_BMAP(vp, lbn, NULL, &bp->b_blkno, &maxclen, NULL) ||
754 bp->b_blkno == -1)) {
757 vp->v_lasta = bp->b_blkno;
758 vp->v_cstart = lbn + 1;
762 vp->v_clen = maxclen;
763 if (!async && maxclen == 0) { /* I/O not contiguous */
764 vp->v_cstart = lbn + 1;
766 } else { /* Wait for rest of cluster */
770 } else if (lbn == vp->v_cstart + vp->v_clen) {
772 * At end of cluster, write it out if seqcount tells us we
773 * are operating sequentially, otherwise let the buf or
774 * update daemon handle it.
778 cluster_wbuild_wb(vp, lblocksize, vp->v_cstart,
779 vp->v_clen + 1, gbflags);
782 vp->v_cstart = lbn + 1;
783 } else if (vm_page_count_severe()) {
785 * We are low on memory, get it going NOW
790 * In the middle of a cluster, so just delay the I/O for now.
795 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 = uma_zalloc(cluster_pbuf_zone, M_NOWAIT)) == NULL)) {
861 totalwritten += tbp->b_bufsize;
867 MPASS((bp->b_flags & B_MAXPHYS) != 0);
870 * We got a pbuf to make the cluster in.
873 TAILQ_INIT(&bp->b_cluster.cluster_head);
877 if (tbp->b_wcred != NOCRED)
878 bp->b_wcred = crhold(tbp->b_wcred);
880 bp->b_blkno = tbp->b_blkno;
881 bp->b_lblkno = tbp->b_lblkno;
882 bp->b_offset = tbp->b_offset;
885 * We are synthesizing a buffer out of vm_page_t's, but
886 * if the block size is not page aligned then the starting
887 * address may not be either. Inherit the b_data offset
888 * from the original buffer.
890 if ((gbflags & GB_UNMAPPED) == 0 ||
891 (tbp->b_flags & B_VMIO) == 0) {
892 bp->b_data = (char *)((vm_offset_t)bp->b_data |
893 ((vm_offset_t)tbp->b_data & PAGE_MASK));
895 bp->b_data = unmapped_buf;
897 bp->b_flags |= B_CLUSTER | (tbp->b_flags & (B_VMIO |
899 bp->b_iodone = cluster_callback;
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 < len; ++i, ++start_lbn) {
907 if (i != 0) { /* If not the first buffer */
909 * If the adjacent data is not even in core it
910 * can't need to be written.
913 if ((tbp = gbincore(bo, start_lbn)) == NULL ||
914 (tbp->b_vflags & BV_BKGRDINPROG)) {
920 * If it IS in core, but has different
921 * characteristics, or is locked (which
922 * means it could be undergoing a background
923 * I/O or be in a weird state), then don't
927 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
931 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
932 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
933 != (B_DELWRI | B_CLUSTEROK |
934 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
935 tbp->b_wcred != bp->b_wcred) {
941 * Check that the combined cluster
942 * would make sense with regard to pages
943 * and would not be too large
945 if ((tbp->b_bcount != size) ||
946 ((bp->b_blkno + (dbsize * i)) !=
948 ((tbp->b_npages + bp->b_npages) >
949 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
955 * Ok, it's passed all the tests,
956 * so remove it from the free list
957 * and mark it busy. We will use it.
960 tbp->b_flags &= ~B_DONE;
961 } /* end of code for non-first buffers only */
963 * If the IO is via the VM then we do some
964 * special VM hackery (yuck). Since the buffer's
965 * block size may not be page-aligned it is possible
966 * for a page to be shared between two buffers. We
967 * have to get rid of the duplication when building
970 if (tbp->b_flags & B_VMIO) {
974 vfs_busy_pages_acquire(tbp);
975 } else { /* if not first buffer */
976 for (j = 0; j < tbp->b_npages; j += 1) {
978 if (vm_page_trysbusy(m) == 0) {
979 for (j--; j >= 0; j--)
987 vm_object_pip_add(tbp->b_bufobj->bo_object,
989 for (j = 0; j < tbp->b_npages; j += 1) {
991 if ((bp->b_npages == 0) ||
992 (bp->b_pages[bp->b_npages - 1] != m)) {
993 bp->b_pages[bp->b_npages] = m;
998 bp->b_bcount += size;
999 bp->b_bufsize += size;
1001 * If any of the clustered buffers have their
1002 * B_BARRIER flag set, transfer that request to
1005 bp->b_flags |= (tbp->b_flags & B_BARRIER);
1006 tbp->b_flags &= ~(B_DONE | B_BARRIER);
1007 tbp->b_flags |= B_ASYNC;
1008 tbp->b_ioflags &= ~BIO_ERROR;
1009 tbp->b_iocmd = BIO_WRITE;
1011 reassignbuf(tbp); /* put on clean list */
1012 bufobj_wref(tbp->b_bufobj);
1014 buf_track(tbp, __func__);
1015 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
1016 tbp, b_cluster.cluster_entry);
1019 if (buf_mapped(bp)) {
1020 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
1021 (vm_page_t *)bp->b_pages, bp->b_npages);
1023 if (bp->b_bufsize > bp->b_kvasize)
1025 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
1026 bp->b_bufsize, bp->b_kvasize);
1027 totalwritten += bp->b_bufsize;
1029 bp->b_dirtyend = bp->b_bufsize;
1034 return totalwritten;
1038 * Collect together all the buffers in a cluster.
1039 * Plus add one additional buffer.
1041 static struct cluster_save *
1042 cluster_collectbufs(struct vnode *vp, struct buf *last_bp, int gbflags)
1044 struct cluster_save *buflist;
1047 int i, j, len, error;
1049 len = vp->v_lastw - vp->v_cstart + 1;
1050 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
1051 M_SEGMENT, M_WAITOK);
1052 buflist->bs_nchildren = 0;
1053 buflist->bs_children = (struct buf **) (buflist + 1);
1054 for (lbn = vp->v_cstart, i = 0; i < len; lbn++, i++) {
1055 error = bread_gb(vp, lbn, last_bp->b_bcount, NOCRED,
1059 * If read fails, release collected buffers
1060 * and return failure.
1062 for (j = 0; j < i; j++)
1063 brelse(buflist->bs_children[j]);
1064 free(buflist, M_SEGMENT);
1067 buflist->bs_children[i] = bp;
1068 if (bp->b_blkno == bp->b_lblkno)
1069 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1072 buflist->bs_children[i] = bp = last_bp;
1073 if (bp->b_blkno == bp->b_lblkno)
1074 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1075 buflist->bs_nchildren = i + 1;