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 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
43 #include <sys/vnode.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/racct.h>
47 #include <sys/resourcevar.h>
48 #include <sys/rwlock.h>
49 #include <sys/vmmeter.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
53 #include <sys/sysctl.h>
55 static MALLOC_DEFINE(M_SEGMENT, "cl_savebuf", "cluster_save buffer");
56 static uma_zone_t cluster_pbuf_zone;
58 static void cluster_init(void *);
59 static struct cluster_save *cluster_collectbufs(struct vnode *vp,
60 struct vn_clusterw *vnc, struct buf *last_bp, int gbflags);
61 static struct buf *cluster_rbuild(struct vnode *vp, u_quad_t filesize,
62 daddr_t lbn, daddr_t blkno, long size, int run, int gbflags,
64 static void cluster_callback(struct buf *);
66 static int write_behind = 1;
67 SYSCTL_INT(_vfs, OID_AUTO, write_behind, CTLFLAG_RW, &write_behind, 0,
68 "Cluster write-behind; 0: disable, 1: enable, 2: backed off");
70 static int read_max = 64;
71 SYSCTL_INT(_vfs, OID_AUTO, read_max, CTLFLAG_RW, &read_max, 0,
72 "Cluster read-ahead max block count");
74 static int read_min = 1;
75 SYSCTL_INT(_vfs, OID_AUTO, read_min, CTLFLAG_RW, &read_min, 0,
76 "Cluster read min block count");
78 SYSINIT(cluster, SI_SUB_CPU, SI_ORDER_ANY, cluster_init, NULL);
81 cluster_init(void *dummy)
84 cluster_pbuf_zone = pbuf_zsecond_create("clpbuf", nswbuf / 2);
88 * Read data to a buf, including read-ahead if we find this to be beneficial.
89 * cluster_read replaces bread.
92 cluster_read(struct vnode *vp, u_quad_t filesize, daddr_t lblkno, long size,
93 struct ucred *cred, long totread, int seqcount, int gbflags,
96 struct buf *bp, *rbp, *reqbp;
99 daddr_t blkno, origblkno;
100 int maxra, racluster;
107 if (!unmapped_buf_allowed)
108 gbflags &= ~GB_UNMAPPED;
111 * Try to limit the amount of read-ahead by a few
112 * ad-hoc parameters. This needs work!!!
114 racluster = vp->v_mount->mnt_iosize_max / size;
116 maxra = min(read_max, maxra);
117 maxra = min(nbuf/8, maxra);
118 if (((u_quad_t)(lblkno + maxra + 1) * size) > filesize)
119 maxra = (filesize / size) - lblkno;
122 * get the requested block
124 error = getblkx(vp, lblkno, lblkno, size, 0, 0, gbflags, &bp);
129 gbflags &= ~GB_NOSPARSE;
134 * if it is in the cache, then check to see if the reads have been
135 * sequential. If they have, then try some read-ahead, otherwise
136 * back-off on prospective read-aheads.
138 if (bp->b_flags & B_CACHE) {
141 } else if ((bp->b_flags & B_RAM) == 0) {
144 bp->b_flags &= ~B_RAM;
146 for (i = 1; i < maxra; i++) {
148 * Stop if the buffer does not exist or it
149 * is invalid (about to go away?)
151 rbp = gbincore(&vp->v_bufobj, lblkno+i);
152 if (rbp == NULL || (rbp->b_flags & B_INVAL))
156 * Set another read-ahead mark so we know
157 * to check again. (If we can lock the
158 * buffer without waiting)
160 if ((((i % racluster) == (racluster - 1)) ||
162 && (0 == BUF_LOCK(rbp,
163 LK_EXCLUSIVE | LK_NOWAIT, NULL))) {
164 rbp->b_flags |= B_RAM;
176 * If it isn't in the cache, then get a chunk from
177 * disk if sequential, otherwise just get the block.
180 off_t firstread = bp->b_offset;
184 KASSERT(bp->b_offset != NOOFFSET,
185 ("cluster_read: no buffer offset"));
190 * Adjust totread if needed
192 minread = read_min * size;
193 if (minread > totread)
197 * Compute the total number of blocks that we should read
200 if (firstread + totread > filesize)
201 totread = filesize - firstread;
202 nblks = howmany(totread, size);
203 if (nblks > racluster)
207 * Now compute the number of contiguous blocks.
210 error = VOP_BMAP(vp, lblkno, NULL,
211 &blkno, &ncontig, NULL);
213 * If this failed to map just do the original block.
215 if (error || blkno == -1)
220 * If we have contiguous data available do a cluster
221 * otherwise just read the requested block.
224 /* Account for our first block. */
225 ncontig = min(ncontig + 1, nblks);
228 bp = cluster_rbuild(vp, filesize, lblkno,
229 blkno, size, nblks, gbflags, bp);
230 lblkno += (bp->b_bufsize / size);
232 bp->b_flags |= B_RAM;
233 bp->b_iocmd = BIO_READ;
239 * handle the synchronous read so that it is available ASAP.
242 if ((bp->b_flags & B_CLUSTER) == 0) {
243 vfs_busy_pages(bp, 0);
245 bp->b_flags &= ~B_INVAL;
246 bp->b_ioflags &= ~BIO_ERROR;
247 if ((bp->b_flags & B_ASYNC) || bp->b_iodone != NULL)
249 bp->b_iooffset = dbtob(bp->b_blkno);
253 PROC_LOCK(td->td_proc);
254 racct_add_buf(td->td_proc, bp, 0);
255 PROC_UNLOCK(td->td_proc);
258 td->td_ru.ru_inblock++;
262 * If we have been doing sequential I/O, then do some read-ahead.
264 while (lblkno < (origblkno + maxra)) {
265 error = VOP_BMAP(vp, lblkno, NULL, &blkno, &ncontig, NULL);
273 * We could throttle ncontig here by maxra but we might as
274 * well read the data if it is contiguous. We're throttled
275 * by racluster anyway.
278 ncontig = min(ncontig + 1, racluster);
279 rbp = cluster_rbuild(vp, filesize, lblkno, blkno,
280 size, ncontig, gbflags, NULL);
281 lblkno += (rbp->b_bufsize / size);
282 if (rbp->b_flags & B_DELWRI) {
287 rbp = getblk(vp, lblkno, size, 0, 0, gbflags);
289 if (rbp->b_flags & B_DELWRI) {
293 rbp->b_flags |= B_ASYNC | B_RAM;
294 rbp->b_iocmd = BIO_READ;
295 rbp->b_blkno = blkno;
297 if (rbp->b_flags & B_CACHE) {
298 rbp->b_flags &= ~B_ASYNC;
302 if ((rbp->b_flags & B_CLUSTER) == 0) {
303 vfs_busy_pages(rbp, 0);
305 rbp->b_flags &= ~B_INVAL;
306 rbp->b_ioflags &= ~BIO_ERROR;
307 if ((rbp->b_flags & B_ASYNC) || rbp->b_iodone != NULL)
309 rbp->b_iooffset = dbtob(rbp->b_blkno);
313 PROC_LOCK(td->td_proc);
314 racct_add_buf(td->td_proc, rbp, 0);
315 PROC_UNLOCK(td->td_proc);
318 td->td_ru.ru_inblock++;
323 * Like bread, always brelse() the buffer when
324 * returning an error.
326 error = bufwait(reqbp);
336 * If blocks are contiguous on disk, use this to provide clustered
337 * read ahead. We will read as many blocks as possible sequentially
338 * and then parcel them up into logical blocks in the buffer hash table.
341 cluster_rbuild(struct vnode *vp, u_quad_t filesize, daddr_t lbn,
342 daddr_t blkno, long size, int run, int gbflags, struct buf *fbp)
344 struct buf *bp, *tbp;
348 int i, inc, j, k, toff;
350 KASSERT(size == vp->v_mount->mnt_stat.f_iosize,
351 ("cluster_rbuild: size %ld != f_iosize %jd\n",
352 size, (intmax_t)vp->v_mount->mnt_stat.f_iosize));
357 while ((u_quad_t) size * (lbn + run) > filesize) {
363 tbp->b_iocmd = BIO_READ;
365 tbp = getblk(vp, lbn, size, 0, 0, gbflags);
366 if (tbp->b_flags & B_CACHE)
368 tbp->b_flags |= B_ASYNC | B_RAM;
369 tbp->b_iocmd = BIO_READ;
371 tbp->b_blkno = blkno;
372 if ( (tbp->b_flags & B_MALLOC) ||
373 ((tbp->b_flags & B_VMIO) == 0) || (run <= 1) )
376 bp = uma_zalloc(cluster_pbuf_zone, M_NOWAIT);
379 MPASS((bp->b_flags & B_MAXPHYS) != 0);
382 * We are synthesizing a buffer out of vm_page_t's, but
383 * if the block size is not page aligned then the starting
384 * address may not be either. Inherit the b_data offset
385 * from the original buffer.
387 bp->b_flags = B_ASYNC | B_CLUSTER | B_VMIO;
388 if ((gbflags & GB_UNMAPPED) != 0) {
389 bp->b_data = unmapped_buf;
391 bp->b_data = (char *)((vm_offset_t)bp->b_data |
392 ((vm_offset_t)tbp->b_data & PAGE_MASK));
394 bp->b_iocmd = BIO_READ;
395 bp->b_iodone = cluster_callback;
398 bp->b_offset = tbp->b_offset;
399 KASSERT(bp->b_offset != NOOFFSET, ("cluster_rbuild: no buffer offset"));
402 TAILQ_INIT(&bp->b_cluster.cluster_head);
409 for (bn = blkno, i = 0; i < run; ++i, bn += inc) {
411 vm_object_pip_add(tbp->b_bufobj->bo_object,
413 vfs_busy_pages_acquire(tbp);
415 if ((bp->b_npages * PAGE_SIZE) +
416 round_page(size) > vp->v_mount->mnt_iosize_max) {
420 tbp = getblk(vp, lbn + i, size, 0, 0, GB_LOCK_NOWAIT |
421 (gbflags & GB_UNMAPPED));
423 /* Don't wait around for locked bufs. */
428 * Stop scanning if the buffer is fully valid
429 * (marked B_CACHE), or locked (may be doing a
430 * background write), or if the buffer is not
431 * VMIO backed. The clustering code can only deal
432 * with VMIO-backed buffers. The bo lock is not
433 * required for the BKGRDINPROG check since it
434 * can not be set without the buf lock.
436 if ((tbp->b_vflags & BV_BKGRDINPROG) ||
437 (tbp->b_flags & B_CACHE) ||
438 (tbp->b_flags & B_VMIO) == 0) {
444 * The buffer must be completely invalid in order to
445 * take part in the cluster. If it is partially valid
450 for (j = 0; tsize > 0; j++) {
451 toff = off & PAGE_MASK;
453 if (toff + tinc > PAGE_SIZE)
454 tinc = PAGE_SIZE - toff;
455 if (vm_page_trysbusy(tbp->b_pages[j]) == 0)
457 if ((tbp->b_pages[j]->valid &
458 vm_page_bits(toff, tinc)) != 0) {
459 vm_page_sunbusy(tbp->b_pages[j]);
462 vm_object_pip_add(tbp->b_bufobj->bo_object, 1);
468 vm_object_pip_wakeupn(tbp->b_bufobj->bo_object,
470 for (k = 0; k < j; k++)
471 vm_page_sunbusy(tbp->b_pages[k]);
477 * Set a read-ahead mark as appropriate
479 if ((fbp && (i == 1)) || (i == (run - 1)))
480 tbp->b_flags |= B_RAM;
483 * Set the buffer up for an async read (XXX should
484 * we do this only if we do not wind up brelse()ing?).
485 * Set the block number if it isn't set, otherwise
486 * if it is make sure it matches the block number we
489 tbp->b_flags |= B_ASYNC;
490 tbp->b_iocmd = BIO_READ;
491 if (tbp->b_blkno == tbp->b_lblkno) {
493 } else if (tbp->b_blkno != bn) {
498 * XXX fbp from caller may not be B_ASYNC, but we are going
499 * to biodone() it in cluster_callback() anyway
502 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
503 tbp, b_cluster.cluster_entry);
504 for (j = 0; j < tbp->b_npages; j += 1) {
508 if ((bp->b_npages == 0) ||
509 (bp->b_pages[bp->b_npages-1] != m)) {
510 bp->b_pages[bp->b_npages] = m;
513 if (vm_page_all_valid(m))
514 tbp->b_pages[j] = bogus_page;
518 * Don't inherit tbp->b_bufsize as it may be larger due to
519 * a non-page-aligned size. Instead just aggregate using
522 if (tbp->b_bcount != size)
523 printf("warning: tbp->b_bcount wrong %ld vs %ld\n", tbp->b_bcount, size);
524 if (tbp->b_bufsize != size)
525 printf("warning: tbp->b_bufsize wrong %ld vs %ld\n", tbp->b_bufsize, size);
526 bp->b_bcount += size;
527 bp->b_bufsize += size;
531 * Fully valid pages in the cluster are already good and do not need
532 * to be re-read from disk. Replace the page with bogus_page
534 for (j = 0; j < bp->b_npages; j++) {
535 if (vm_page_all_valid(bp->b_pages[j]))
536 bp->b_pages[j] = bogus_page;
538 if (bp->b_bufsize > bp->b_kvasize)
539 panic("cluster_rbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
540 bp->b_bufsize, bp->b_kvasize);
542 if (buf_mapped(bp)) {
543 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
544 (vm_page_t *)bp->b_pages, bp->b_npages);
550 * Cleanup after a clustered read or write.
551 * This is complicated by the fact that any of the buffers might have
552 * extra memory (if there were no empty buffer headers at allocbuf time)
553 * that we will need to shift around.
556 cluster_callback(struct buf *bp)
558 struct buf *nbp, *tbp;
562 * Must propagate errors to all the components.
564 if (bp->b_ioflags & BIO_ERROR)
567 if (buf_mapped(bp)) {
568 pmap_qremove(trunc_page((vm_offset_t) bp->b_data),
572 * Move memory from the large cluster buffer into the component
573 * buffers and mark IO as done on these.
575 for (tbp = TAILQ_FIRST(&bp->b_cluster.cluster_head);
577 nbp = TAILQ_NEXT(&tbp->b_cluster, cluster_entry);
579 tbp->b_ioflags |= BIO_ERROR;
580 tbp->b_error = error;
582 tbp->b_dirtyoff = tbp->b_dirtyend = 0;
583 tbp->b_flags &= ~B_INVAL;
584 tbp->b_ioflags &= ~BIO_ERROR;
586 * XXX the bdwrite()/bqrelse() issued during
587 * cluster building clears B_RELBUF (see bqrelse()
588 * comment). If direct I/O was specified, we have
589 * to restore it here to allow the buffer and VM
592 if (tbp->b_flags & B_DIRECT)
593 tbp->b_flags |= B_RELBUF;
598 uma_zfree(cluster_pbuf_zone, bp);
604 * Implement modified write build for cluster.
606 * write_behind = 0 write behind disabled
607 * write_behind = 1 write behind normal (default)
608 * write_behind = 2 write behind backed-off
612 cluster_wbuild_wb(struct vnode *vp, long size, daddr_t start_lbn, int len,
617 switch (write_behind) {
624 r = cluster_wbuild(vp, size, start_lbn, len, gbflags);
634 * Do clustered write for FFS.
637 * 1. Write is not sequential (write asynchronously)
638 * Write is sequential:
639 * 2. beginning of cluster - begin cluster
640 * 3. middle of a cluster - add to cluster
641 * 4. end of a cluster - asynchronously write cluster
644 cluster_write(struct vnode *vp, struct vn_clusterw *vnc, struct buf *bp,
645 u_quad_t filesize, int seqcount, int gbflags)
648 int maxclen, cursize;
652 if (!unmapped_buf_allowed)
653 gbflags &= ~GB_UNMAPPED;
655 if (vp->v_type == VREG) {
656 async = DOINGASYNC(vp);
657 lblocksize = vp->v_mount->mnt_stat.f_iosize;
660 lblocksize = bp->b_bufsize;
663 KASSERT(bp->b_offset != NOOFFSET, ("cluster_write: no buffer offset"));
665 /* Initialize vnode to beginning of file. */
667 vnc->v_lasta = vnc->v_clen = vnc->v_cstart = vnc->v_lastw = 0;
669 if (vnc->v_clen == 0 || lbn != vnc->v_lastw + 1 ||
670 (bp->b_blkno != vnc->v_lasta + btodb(lblocksize))) {
671 maxclen = vp->v_mount->mnt_iosize_max / lblocksize - 1;
672 if (vnc->v_clen != 0) {
674 * Next block is not sequential.
676 * If we are not writing at end of file, the process
677 * seeked to another point in the file since its last
678 * write, or we have reached our maximum cluster size,
679 * then push the previous cluster. Otherwise try
680 * reallocating to make it sequential.
682 * Change to algorithm: only push previous cluster if
683 * it was sequential from the point of view of the
684 * seqcount heuristic, otherwise leave the buffer
685 * intact so we can potentially optimize the I/O
686 * later on in the buf_daemon or update daemon
689 cursize = vnc->v_lastw - vnc->v_cstart + 1;
690 if ((u_quad_t)bp->b_offset + lblocksize != filesize ||
691 lbn != vnc->v_lastw + 1 || vnc->v_clen <= cursize) {
692 if (!async && seqcount > 0) {
693 cluster_wbuild_wb(vp, lblocksize,
694 vnc->v_cstart, cursize, gbflags);
697 struct buf **bpp, **endbp;
698 struct cluster_save *buflist;
700 buflist = cluster_collectbufs(vp, vnc, bp,
702 if (buflist == NULL) {
704 * Cluster build failed so just write
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, vnc->v_cstart,
732 * Succeeded, keep building cluster.
734 for (bpp = buflist->bs_children;
737 free(buflist, M_SEGMENT);
739 vnc->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,
753 NULL) != 0 || bp->b_blkno == -1)) {
758 vnc->v_cstart = lbn + 1;
762 vnc->v_clen = maxclen;
764 if (!async && maxclen == 0) { /* I/O not contiguous */
765 vnc->v_cstart = lbn + 1;
767 } else { /* Wait for rest of cluster */
771 } else if (lbn == vnc->v_cstart + vnc->v_clen) {
773 * At end of cluster, write it out if seqcount tells us we
774 * are operating sequentially, otherwise let the buf or
775 * update daemon handle it.
780 cluster_wbuild_wb(vp, lblocksize, vnc->v_cstart,
781 vnc->v_clen + 1, gbflags);
784 vnc->v_cstart = lbn + 1;
785 } else if (vm_page_count_severe()) {
787 * We are low on memory, get it going NOW
793 * In the middle of a cluster, so just delay the I/O for now.
803 * This is an awful lot like cluster_rbuild...wish they could be combined.
804 * The last lbn argument is the current block on which I/O is being
805 * performed. Check to see that it doesn't fall in the middle of
806 * the current block (if last_bp == NULL).
809 cluster_wbuild(struct vnode *vp, long size, daddr_t start_lbn, int len,
812 struct buf *bp, *tbp;
815 int totalwritten = 0;
816 int dbsize = btodb(size);
818 if (!unmapped_buf_allowed)
819 gbflags &= ~GB_UNMAPPED;
824 * If the buffer is not delayed-write (i.e. dirty), or it
825 * is delayed-write but either locked or inval, it cannot
826 * partake in the clustered write.
829 if ((tbp = gbincore(&vp->v_bufobj, start_lbn)) == NULL ||
830 (tbp->b_vflags & BV_BKGRDINPROG)) {
837 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, BO_LOCKPTR(bo))) {
842 if ((tbp->b_flags & (B_INVAL | B_DELWRI)) != B_DELWRI) {
849 tbp->b_flags &= ~B_DONE;
852 * Extra memory in the buffer, punt on this buffer.
853 * XXX we could handle this in most cases, but we would
854 * have to push the extra memory down to after our max
855 * possible cluster size and then potentially pull it back
856 * up if the cluster was terminated prematurely--too much
859 if (((tbp->b_flags & (B_CLUSTEROK | B_MALLOC | B_VMIO)) !=
860 (B_CLUSTEROK | B_VMIO)) ||
861 (tbp->b_bcount != tbp->b_bufsize) ||
862 (tbp->b_bcount != size) ||
864 ((bp = uma_zalloc(cluster_pbuf_zone, M_NOWAIT)) == NULL)) {
865 totalwritten += tbp->b_bufsize;
871 MPASS((bp->b_flags & B_MAXPHYS) != 0);
874 * We got a pbuf to make the cluster in.
877 TAILQ_INIT(&bp->b_cluster.cluster_head);
881 if (tbp->b_wcred != NOCRED)
882 bp->b_wcred = crhold(tbp->b_wcred);
884 bp->b_blkno = tbp->b_blkno;
885 bp->b_lblkno = tbp->b_lblkno;
886 bp->b_offset = tbp->b_offset;
889 * We are synthesizing a buffer out of vm_page_t's, but
890 * if the block size is not page aligned then the starting
891 * address may not be either. Inherit the b_data offset
892 * from the original buffer.
894 if ((gbflags & GB_UNMAPPED) == 0 ||
895 (tbp->b_flags & B_VMIO) == 0) {
896 bp->b_data = (char *)((vm_offset_t)bp->b_data |
897 ((vm_offset_t)tbp->b_data & PAGE_MASK));
899 bp->b_data = unmapped_buf;
901 bp->b_flags |= B_CLUSTER | (tbp->b_flags & (B_VMIO |
903 bp->b_iodone = cluster_callback;
906 * From this location in the file, scan forward to see
907 * if there are buffers with adjacent data that need to
908 * be written as well.
910 for (i = 0; i < len; ++i, ++start_lbn) {
911 if (i != 0) { /* If not the first buffer */
913 * If the adjacent data is not even in core it
914 * can't need to be written.
917 if ((tbp = gbincore(bo, start_lbn)) == NULL ||
918 (tbp->b_vflags & BV_BKGRDINPROG)) {
924 * If it IS in core, but has different
925 * characteristics, or is locked (which
926 * means it could be undergoing a background
927 * I/O or be in a weird state), then don't
931 LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK,
935 if ((tbp->b_flags & (B_VMIO | B_CLUSTEROK |
936 B_INVAL | B_DELWRI | B_NEEDCOMMIT))
937 != (B_DELWRI | B_CLUSTEROK |
938 (bp->b_flags & (B_VMIO | B_NEEDCOMMIT))) ||
939 tbp->b_wcred != bp->b_wcred) {
945 * Check that the combined cluster
946 * would make sense with regard to pages
947 * and would not be too large
949 if ((tbp->b_bcount != size) ||
950 ((bp->b_blkno + (dbsize * i)) !=
952 ((tbp->b_npages + bp->b_npages) >
953 (vp->v_mount->mnt_iosize_max / PAGE_SIZE))) {
959 * Ok, it's passed all the tests,
960 * so remove it from the free list
961 * and mark it busy. We will use it.
964 tbp->b_flags &= ~B_DONE;
965 } /* end of code for non-first buffers only */
967 * If the IO is via the VM then we do some
968 * special VM hackery (yuck). Since the buffer's
969 * block size may not be page-aligned it is possible
970 * for a page to be shared between two buffers. We
971 * have to get rid of the duplication when building
974 if (tbp->b_flags & B_VMIO) {
978 vfs_busy_pages_acquire(tbp);
979 } else { /* if not first buffer */
980 for (j = 0; j < tbp->b_npages; j += 1) {
982 if (vm_page_trysbusy(m) == 0) {
983 for (j--; j >= 0; j--)
991 vm_object_pip_add(tbp->b_bufobj->bo_object,
993 for (j = 0; j < tbp->b_npages; j += 1) {
995 if ((bp->b_npages == 0) ||
996 (bp->b_pages[bp->b_npages - 1] != m)) {
997 bp->b_pages[bp->b_npages] = m;
1002 bp->b_bcount += size;
1003 bp->b_bufsize += size;
1005 * If any of the clustered buffers have their
1006 * B_BARRIER flag set, transfer that request to
1009 bp->b_flags |= (tbp->b_flags & B_BARRIER);
1010 tbp->b_flags &= ~(B_DONE | B_BARRIER);
1011 tbp->b_flags |= B_ASYNC;
1012 tbp->b_ioflags &= ~BIO_ERROR;
1013 tbp->b_iocmd = BIO_WRITE;
1015 reassignbuf(tbp); /* put on clean list */
1016 bufobj_wref(tbp->b_bufobj);
1018 buf_track(tbp, __func__);
1019 TAILQ_INSERT_TAIL(&bp->b_cluster.cluster_head,
1020 tbp, b_cluster.cluster_entry);
1023 if (buf_mapped(bp)) {
1024 pmap_qenter(trunc_page((vm_offset_t) bp->b_data),
1025 (vm_page_t *)bp->b_pages, bp->b_npages);
1027 if (bp->b_bufsize > bp->b_kvasize)
1029 "cluster_wbuild: b_bufsize(%ld) > b_kvasize(%d)\n",
1030 bp->b_bufsize, bp->b_kvasize);
1031 totalwritten += bp->b_bufsize;
1033 bp->b_dirtyend = bp->b_bufsize;
1038 return totalwritten;
1042 * Collect together all the buffers in a cluster.
1043 * Plus add one additional buffer.
1045 static struct cluster_save *
1046 cluster_collectbufs(struct vnode *vp, struct vn_clusterw *vnc,
1047 struct buf *last_bp, int gbflags)
1049 struct cluster_save *buflist;
1052 int i, j, len, error;
1054 len = vnc->v_lastw - vnc->v_cstart + 1;
1055 buflist = malloc(sizeof(struct buf *) * (len + 1) + sizeof(*buflist),
1056 M_SEGMENT, M_WAITOK);
1057 buflist->bs_nchildren = 0;
1058 buflist->bs_children = (struct buf **) (buflist + 1);
1059 for (lbn = vnc->v_cstart, i = 0; i < len; lbn++, i++) {
1060 error = bread_gb(vp, lbn, last_bp->b_bcount, NOCRED,
1064 * If read fails, release collected buffers
1065 * and return failure.
1067 for (j = 0; j < i; j++)
1068 brelse(buflist->bs_children[j]);
1069 free(buflist, M_SEGMENT);
1072 buflist->bs_children[i] = bp;
1073 if (bp->b_blkno == bp->b_lblkno)
1074 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1077 buflist->bs_children[i] = bp = last_bp;
1078 if (bp->b_blkno == bp->b_lblkno)
1079 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1080 buflist->bs_nchildren = i + 1;
1085 cluster_init_vn(struct vn_clusterw *vnc)