3 * The Regents of the University of California. All rights reserved.
4 * Modifications/enhancements:
5 * Copyright (c) 1995 John S. Dyson. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)vfs_cluster.c 8.7 (Berkeley) 2/13/94
36 * $FreeBSD: src/sys/kern/vfs_cluster.c,v 1.92.2.9 2001/11/18 07:10:59 dillon Exp $
37 * $DragonFly: src/sys/kern/vfs_cluster.c,v 1.40 2008/07/14 03:09:00 dillon Exp $
40 #include "opt_debug_cluster.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
47 #include <sys/vnode.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/resourcevar.h>
51 #include <sys/vmmeter.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_page.h>
55 #include <sys/sysctl.h>
57 #include <vm/vm_page2.h>
59 #include <machine/limits.h>
61 #if defined(CLUSTERDEBUG)
62 #include <sys/sysctl.h>
63 static int rcluster
= 0;
64 SYSCTL_INT(_debug
, OID_AUTO
, rcluster
, CTLFLAG_RW
, &rcluster
, 0, "");
67 static MALLOC_DEFINE(M_SEGMENT
, "cluster_save", "cluster_save buffer");
69 static struct cluster_save
*
70 cluster_collectbufs (struct vnode
*vp
, struct buf
*last_bp
,
73 cluster_rbuild (struct vnode
*vp
, off_t filesize
, off_t loffset
,
74 off_t doffset
, int blksize
, int run
,
76 static void cluster_callback (struct bio
*);
77 static void cluster_setram (struct buf
*);
79 static int write_behind
= 1;
80 SYSCTL_INT(_vfs
, OID_AUTO
, write_behind
, CTLFLAG_RW
, &write_behind
, 0, "");
82 extern vm_page_t bogus_page
;
84 extern int cluster_pbuf_freecnt
;
87 * Maximum number of blocks for read-ahead.
92 * This replaces bread.
95 cluster_read(struct vnode
*vp
, off_t filesize
, off_t loffset
,
96 int blksize
, size_t resid
, int seqcount
, struct buf
**bpp
)
98 struct buf
*bp
, *rbp
, *reqbp
;
103 int maxra
, racluster
;
107 totread
= (resid
> INT_MAX
) ? INT_MAX
: (int)resid
;
110 * racluster - calculate maximum cluster IO size (limited by
111 * backing block device).
113 * Try to limit the amount of read-ahead by a few ad-hoc parameters.
116 * NOTE! The BMAP operations may involve synchronous I/O so we
117 * really want several cluster IOs in progress to absorb
120 racluster
= vmaxiosize(vp
) / blksize
;
121 maxra
= 2 * racluster
+ (totread
/ blksize
);
124 if (maxra
> nbuf
/ 8)
128 * Get the requested block.
130 *bpp
= reqbp
= bp
= getblk(vp
, loffset
, blksize
, 0, 0);
131 origoffset
= loffset
;
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
) {
140 * Not sequential, do not do any read-ahead
142 if (seqcount
== 0 || maxra
== 0)
146 * No read-ahead mark, do not do any read-ahead
149 if ((bp
->b_flags
& B_RAM
) == 0)
153 * We hit a read-ahead-mark, figure out how much read-ahead
154 * to do (maxra) and where to start (loffset).
156 * Shortcut the scan. Typically the way this works is that
157 * we've built up all the blocks inbetween except for the
158 * last in previous iterations, so if the second-to-last
159 * block is present we just skip ahead to it.
161 * This algorithm has O(1) cpu in the steady state no
162 * matter how large maxra is.
164 bp
->b_flags
&= ~B_RAM
;
166 if (findblk(vp
, loffset
+ (maxra
- 2) * blksize
, FINDBLK_TEST
))
171 if (findblk(vp
, loffset
+ i
* blksize
,
172 FINDBLK_TEST
) == NULL
) {
180 loffset
+= i
* blksize
;
183 off_t firstread
= bp
->b_loffset
;
187 * Set-up synchronous read for bp.
189 bp
->b_cmd
= BUF_CMD_READ
;
190 bp
->b_bio1
.bio_done
= biodone_sync
;
191 bp
->b_bio1
.bio_flags
|= BIO_SYNC
;
193 KASSERT(firstread
!= NOOFFSET
,
194 ("cluster_read: no buffer offset"));
195 if (firstread
+ totread
> filesize
)
196 totread
= (int)(filesize
- firstread
);
197 nblks
= totread
/ blksize
;
201 if (nblks
> racluster
)
204 error
= VOP_BMAP(vp
, loffset
, &doffset
,
205 &burstbytes
, NULL
, BUF_CMD_READ
);
207 goto single_block_read
;
208 if (doffset
== NOOFFSET
)
209 goto single_block_read
;
210 if (burstbytes
< blksize
* 2)
211 goto single_block_read
;
212 if (nblks
> burstbytes
/ blksize
)
213 nblks
= burstbytes
/ blksize
;
215 bp
= cluster_rbuild(vp
, filesize
, loffset
,
216 doffset
, blksize
, nblks
, bp
);
217 loffset
+= bp
->b_bufsize
;
218 maxra
-= (bp
->b_bufsize
- blksize
) / blksize
;
222 * if it isn't in the cache, then get a chunk from
223 * disk if sequential, otherwise just get the block.
231 * If B_CACHE was not set issue bp. bp will either be an
232 * asynchronous cluster buf or a synchronous single-buf.
233 * If it is a single buf it will be the same as reqbp.
235 * NOTE: Once an async cluster buf is issued bp becomes invalid.
238 #if defined(CLUSTERDEBUG)
240 kprintf("S(%lld,%d,%d) ",
241 bp
->b_loffset
, bp
->b_bcount
, seqcount
);
243 if ((bp
->b_flags
& B_CLUSTER
) == 0)
244 vfs_busy_pages(vp
, bp
);
245 bp
->b_flags
&= ~(B_ERROR
|B_INVAL
);
246 vn_strategy(vp
, &bp
->b_bio1
);
252 * If we have been doing sequential I/O, then do some read-ahead.
253 * The code above us should have positioned us at the next likely
256 * Only mess with buffers which we can immediately lock. HAMMER
257 * will do device-readahead irrespective of what the blocks
260 while (!error
&& seqcount
&& maxra
> 0 &&
261 loffset
+ blksize
<= filesize
) {
267 rbp
= getblk(vp
, loffset
, blksize
,
268 GETBLK_SZMATCH
|GETBLK_NOWAIT
, 0);
271 if ((rbp
->b_flags
& B_CACHE
)) {
277 * An error from the read-ahead bmap has nothing to do
278 * with the caller's original request.
280 tmp_error
= VOP_BMAP(vp
, loffset
, &doffset
,
281 &burstbytes
, NULL
, BUF_CMD_READ
);
282 if (tmp_error
|| doffset
== NOOFFSET
) {
283 rbp
->b_flags
|= B_INVAL
;
288 ntoread
= burstbytes
/ blksize
;
289 nblksread
= (totread
+ blksize
- 1) / blksize
;
290 if (seqcount
< nblksread
)
291 seqcount
= nblksread
;
292 if (ntoread
> seqcount
)
298 rbp
->b_cmd
= BUF_CMD_READ
;
299 /*rbp->b_flags |= B_AGE*/;
303 rbp
= cluster_rbuild(vp
, filesize
, loffset
,
307 rbp
->b_bio2
.bio_offset
= doffset
;
309 #if defined(CLUSTERDEBUG)
312 kprintf("A+(%lld,%d,%lld,%d) ",
313 rbp
->b_loffset
, rbp
->b_bcount
,
314 rbp
->b_loffset
- origoffset
,
317 kprintf("A(%lld,%d,%lld,%d) ",
318 rbp
->b_loffset
, rbp
->b_bcount
,
319 rbp
->b_loffset
- origoffset
,
323 rbp
->b_flags
&= ~(B_ERROR
|B_INVAL
);
325 if ((rbp
->b_flags
& B_CLUSTER
) == 0)
326 vfs_busy_pages(vp
, rbp
);
328 loffset
+= rbp
->b_bufsize
;
329 maxra
-= rbp
->b_bufsize
/ blksize
;
330 vn_strategy(vp
, &rbp
->b_bio1
);
331 /* rbp invalid now */
335 * Wait for our original buffer to complete its I/O. reqbp will
336 * be NULL if the original buffer was B_CACHE. We are returning
337 * (*bpp) which is the same as reqbp when reqbp != NULL.
341 KKASSERT(reqbp
->b_bio1
.bio_flags
& BIO_SYNC
);
342 error
= biowait(&reqbp
->b_bio1
, "clurd");
348 * If blocks are contiguous on disk, use this to provide clustered
349 * read ahead. We will read as many blocks as possible sequentially
350 * and then parcel them up into logical blocks in the buffer hash table.
352 * This function either returns a cluster buf or it returns fbp. fbp is
353 * already expected to be set up as a synchronous or asynchronous request.
355 * If a cluster buf is returned it will always be async.
358 cluster_rbuild(struct vnode
*vp
, off_t filesize
, off_t loffset
, off_t doffset
,
359 int blksize
, int run
, struct buf
*fbp
)
361 struct buf
*bp
, *tbp
;
364 int maxiosize
= vmaxiosize(vp
);
369 while (loffset
+ run
* blksize
> filesize
) {
374 tbp
->b_bio2
.bio_offset
= doffset
;
375 if((tbp
->b_flags
& B_MALLOC
) ||
376 ((tbp
->b_flags
& B_VMIO
) == 0) || (run
<= 1)) {
380 bp
= trypbuf(&cluster_pbuf_freecnt
);
386 * We are synthesizing a buffer out of vm_page_t's, but
387 * if the block size is not page aligned then the starting
388 * address may not be either. Inherit the b_data offset
389 * from the original buffer.
391 bp
->b_data
= (char *)((vm_offset_t
)bp
->b_data
|
392 ((vm_offset_t
)tbp
->b_data
& PAGE_MASK
));
393 bp
->b_flags
|= B_CLUSTER
| B_VMIO
;
394 bp
->b_cmd
= BUF_CMD_READ
;
395 bp
->b_bio1
.bio_done
= cluster_callback
; /* default to async */
396 bp
->b_bio1
.bio_caller_info1
.cluster_head
= NULL
;
397 bp
->b_bio1
.bio_caller_info2
.cluster_tail
= NULL
;
398 bp
->b_loffset
= loffset
;
399 bp
->b_bio2
.bio_offset
= doffset
;
400 KASSERT(bp
->b_loffset
!= NOOFFSET
,
401 ("cluster_rbuild: no buffer offset"));
405 bp
->b_xio
.xio_npages
= 0;
407 for (boffset
= doffset
, i
= 0; i
< run
; ++i
, boffset
+= blksize
) {
409 if ((bp
->b_xio
.xio_npages
* PAGE_SIZE
) +
410 round_page(blksize
) > maxiosize
) {
415 * Shortcut some checks and try to avoid buffers that
416 * would block in the lock. The same checks have to
417 * be made again after we officially get the buffer.
419 tbp
= getblk(vp
, loffset
+ i
* blksize
, blksize
,
420 GETBLK_SZMATCH
|GETBLK_NOWAIT
, 0);
423 for (j
= 0; j
< tbp
->b_xio
.xio_npages
; j
++) {
424 if (tbp
->b_xio
.xio_pages
[j
]->valid
)
427 if (j
!= tbp
->b_xio
.xio_npages
) {
433 * Stop scanning if the buffer is fuly valid
434 * (marked B_CACHE), or locked (may be doing a
435 * background write), or if the buffer is not
436 * VMIO backed. The clustering code can only deal
437 * with VMIO-backed buffers.
439 if ((tbp
->b_flags
& (B_CACHE
|B_LOCKED
)) ||
440 (tbp
->b_flags
& B_VMIO
) == 0 ||
441 (LIST_FIRST(&tbp
->b_dep
) != NULL
&&
449 * The buffer must be completely invalid in order to
450 * take part in the cluster. If it is partially valid
453 for (j
= 0;j
< tbp
->b_xio
.xio_npages
; j
++) {
454 if (tbp
->b_xio
.xio_pages
[j
]->valid
)
457 if (j
!= tbp
->b_xio
.xio_npages
) {
463 * Set a read-ahead mark as appropriate
465 if (i
== 1 || i
== (run
- 1))
469 * Depress the priority of buffers not explicitly
472 /* tbp->b_flags |= B_AGE; */
475 * Set the block number if it isn't set, otherwise
476 * if it is make sure it matches the block number we
479 if (tbp
->b_bio2
.bio_offset
== NOOFFSET
) {
480 tbp
->b_bio2
.bio_offset
= boffset
;
481 } else if (tbp
->b_bio2
.bio_offset
!= boffset
) {
488 * The passed-in tbp (i == 0) will already be set up for
489 * async or sync operation. All other tbp's acquire in
490 * our loop are set up for async operation.
492 tbp
->b_cmd
= BUF_CMD_READ
;
494 cluster_append(&bp
->b_bio1
, tbp
);
495 for (j
= 0; j
< tbp
->b_xio
.xio_npages
; ++j
) {
497 m
= tbp
->b_xio
.xio_pages
[j
];
499 vm_object_pip_add(m
->object
, 1);
500 if ((bp
->b_xio
.xio_npages
== 0) ||
501 (bp
->b_xio
.xio_pages
[bp
->b_xio
.xio_npages
-1] != m
)) {
502 bp
->b_xio
.xio_pages
[bp
->b_xio
.xio_npages
] = m
;
503 bp
->b_xio
.xio_npages
++;
505 if ((m
->valid
& VM_PAGE_BITS_ALL
) == VM_PAGE_BITS_ALL
)
506 tbp
->b_xio
.xio_pages
[j
] = bogus_page
;
509 * XXX shouldn't this be += size for both, like in
512 * Don't inherit tbp->b_bufsize as it may be larger due to
513 * a non-page-aligned size. Instead just aggregate using
516 if (tbp
->b_bcount
!= blksize
)
517 kprintf("warning: tbp->b_bcount wrong %d vs %d\n", tbp
->b_bcount
, blksize
);
518 if (tbp
->b_bufsize
!= blksize
)
519 kprintf("warning: tbp->b_bufsize wrong %d vs %d\n", tbp
->b_bufsize
, blksize
);
520 bp
->b_bcount
+= blksize
;
521 bp
->b_bufsize
+= blksize
;
525 * Fully valid pages in the cluster are already good and do not need
526 * to be re-read from disk. Replace the page with bogus_page
528 for (j
= 0; j
< bp
->b_xio
.xio_npages
; j
++) {
529 if ((bp
->b_xio
.xio_pages
[j
]->valid
& VM_PAGE_BITS_ALL
) ==
531 bp
->b_xio
.xio_pages
[j
] = bogus_page
;
534 if (bp
->b_bufsize
> bp
->b_kvasize
) {
535 panic("cluster_rbuild: b_bufsize(%d) > b_kvasize(%d)",
536 bp
->b_bufsize
, bp
->b_kvasize
);
538 pmap_qenter(trunc_page((vm_offset_t
) bp
->b_data
),
539 (vm_page_t
*)bp
->b_xio
.xio_pages
, bp
->b_xio
.xio_npages
);
545 * Cleanup after a clustered read or write.
546 * This is complicated by the fact that any of the buffers might have
547 * extra memory (if there were no empty buffer headers at allocbuf time)
548 * that we will need to shift around.
550 * The returned bio is &bp->b_bio1
553 cluster_callback(struct bio
*bio
)
555 struct buf
*bp
= bio
->bio_buf
;
560 * Must propogate errors to all the components. A short read (EOF)
561 * is a critical error.
563 if (bp
->b_flags
& B_ERROR
) {
565 } else if (bp
->b_bcount
!= bp
->b_bufsize
) {
566 panic("cluster_callback: unexpected EOF on cluster %p!", bio
);
569 pmap_qremove(trunc_page((vm_offset_t
) bp
->b_data
), bp
->b_xio
.xio_npages
);
571 * Move memory from the large cluster buffer into the component
572 * buffers and mark IO as done on these. Since the memory map
573 * is the same, no actual copying is required.
575 while ((tbp
= bio
->bio_caller_info1
.cluster_head
) != NULL
) {
576 bio
->bio_caller_info1
.cluster_head
= tbp
->b_cluster_next
;
578 tbp
->b_flags
|= B_ERROR
;
579 tbp
->b_error
= error
;
581 tbp
->b_dirtyoff
= tbp
->b_dirtyend
= 0;
582 tbp
->b_flags
&= ~(B_ERROR
|B_INVAL
);
584 * XXX the bdwrite()/bqrelse() issued during
585 * cluster building clears B_RELBUF (see bqrelse()
586 * comment). If direct I/O was specified, we have
587 * to restore it here to allow the buffer and VM
590 if (tbp
->b_flags
& B_DIRECT
)
591 tbp
->b_flags
|= B_RELBUF
;
593 biodone(&tbp
->b_bio1
);
595 relpbuf(bp
, &cluster_pbuf_freecnt
);
601 * Implement modified write build for cluster.
603 * write_behind = 0 write behind disabled
604 * write_behind = 1 write behind normal (default)
605 * write_behind = 2 write behind backed-off
609 cluster_wbuild_wb(struct vnode
*vp
, int blksize
, off_t start_loffset
, int len
)
613 switch(write_behind
) {
615 if (start_loffset
< len
)
617 start_loffset
-= len
;
620 r
= cluster_wbuild(vp
, blksize
, start_loffset
, len
);
630 * Do clustered write for FFS.
633 * 1. Write is not sequential (write asynchronously)
634 * Write is sequential:
635 * 2. beginning of cluster - begin cluster
636 * 3. middle of a cluster - add to cluster
637 * 4. end of a cluster - asynchronously write cluster
640 cluster_write(struct buf
*bp
, off_t filesize
, int blksize
, int seqcount
)
644 int maxclen
, cursize
;
648 if (vp
->v_type
== VREG
)
649 async
= vp
->v_mount
->mnt_flag
& MNT_ASYNC
;
652 loffset
= bp
->b_loffset
;
653 KASSERT(bp
->b_loffset
!= NOOFFSET
,
654 ("cluster_write: no buffer offset"));
656 /* Initialize vnode to beginning of file. */
658 vp
->v_lasta
= vp
->v_clen
= vp
->v_cstart
= vp
->v_lastw
= 0;
660 if (vp
->v_clen
== 0 || loffset
!= vp
->v_lastw
+ blksize
||
661 bp
->b_bio2
.bio_offset
== NOOFFSET
||
662 (bp
->b_bio2
.bio_offset
!= vp
->v_lasta
+ blksize
)) {
663 maxclen
= vmaxiosize(vp
);
664 if (vp
->v_clen
!= 0) {
666 * Next block is not sequential.
668 * If we are not writing at end of file, the process
669 * seeked to another point in the file since its last
670 * write, or we have reached our maximum cluster size,
671 * then push the previous cluster. Otherwise try
672 * reallocating to make it sequential.
674 * Change to algorithm: only push previous cluster if
675 * it was sequential from the point of view of the
676 * seqcount heuristic, otherwise leave the buffer
677 * intact so we can potentially optimize the I/O
678 * later on in the buf_daemon or update daemon
681 cursize
= vp
->v_lastw
- vp
->v_cstart
+ blksize
;
682 if (bp
->b_loffset
+ blksize
!= filesize
||
683 loffset
!= vp
->v_lastw
+ blksize
|| vp
->v_clen
<= cursize
) {
684 if (!async
&& seqcount
> 0) {
685 cluster_wbuild_wb(vp
, blksize
,
686 vp
->v_cstart
, cursize
);
689 struct buf
**bpp
, **endbp
;
690 struct cluster_save
*buflist
;
692 buflist
= cluster_collectbufs(vp
, bp
, blksize
);
693 endbp
= &buflist
->bs_children
694 [buflist
->bs_nchildren
- 1];
695 if (VOP_REALLOCBLKS(vp
, buflist
)) {
697 * Failed, push the previous cluster
698 * if *really* writing sequentially
699 * in the logical file (seqcount > 1),
700 * otherwise delay it in the hopes that
701 * the low level disk driver can
702 * optimize the write ordering.
704 for (bpp
= buflist
->bs_children
;
707 kfree(buflist
, M_SEGMENT
);
709 cluster_wbuild_wb(vp
,
710 blksize
, vp
->v_cstart
,
715 * Succeeded, keep building cluster.
717 for (bpp
= buflist
->bs_children
;
720 kfree(buflist
, M_SEGMENT
);
721 vp
->v_lastw
= loffset
;
722 vp
->v_lasta
= bp
->b_bio2
.bio_offset
;
728 * Consider beginning a cluster. If at end of file, make
729 * cluster as large as possible, otherwise find size of
732 if ((vp
->v_type
== VREG
) &&
733 bp
->b_loffset
+ blksize
!= filesize
&&
734 (bp
->b_bio2
.bio_offset
== NOOFFSET
) &&
735 (VOP_BMAP(vp
, loffset
, &bp
->b_bio2
.bio_offset
, &maxclen
, NULL
, BUF_CMD_WRITE
) ||
736 bp
->b_bio2
.bio_offset
== NOOFFSET
)) {
739 vp
->v_lasta
= bp
->b_bio2
.bio_offset
;
740 vp
->v_cstart
= loffset
+ blksize
;
741 vp
->v_lastw
= loffset
;
744 if (maxclen
> blksize
)
745 vp
->v_clen
= maxclen
- blksize
;
748 if (!async
&& vp
->v_clen
== 0) { /* I/O not contiguous */
749 vp
->v_cstart
= loffset
+ blksize
;
751 } else { /* Wait for rest of cluster */
752 vp
->v_cstart
= loffset
;
755 } else if (loffset
== vp
->v_cstart
+ vp
->v_clen
) {
757 * At end of cluster, write it out if seqcount tells us we
758 * are operating sequentially, otherwise let the buf or
759 * update daemon handle it.
763 cluster_wbuild_wb(vp
, blksize
, vp
->v_cstart
,
764 vp
->v_clen
+ blksize
);
766 vp
->v_cstart
= loffset
+ blksize
;
767 } else if (vm_page_count_severe()) {
769 * We are low on memory, get it going NOW
774 * In the middle of a cluster, so just delay the I/O for now.
778 vp
->v_lastw
= loffset
;
779 vp
->v_lasta
= bp
->b_bio2
.bio_offset
;
784 * This is an awful lot like cluster_rbuild...wish they could be combined.
785 * The last lbn argument is the current block on which I/O is being
786 * performed. Check to see that it doesn't fall in the middle of
787 * the current block (if last_bp == NULL).
790 cluster_wbuild(struct vnode
*vp
, int blksize
, off_t start_loffset
, int bytes
)
792 struct buf
*bp
, *tbp
;
794 int totalwritten
= 0;
795 int maxiosize
= vmaxiosize(vp
);
799 * If the buffer is not delayed-write (i.e. dirty), or it
800 * is delayed-write but either locked or inval, it cannot
801 * partake in the clustered write.
803 tbp
= findblk(vp
, start_loffset
, FINDBLK_NBLOCK
);
805 (tbp
->b_flags
& (B_LOCKED
| B_INVAL
| B_DELWRI
)) != B_DELWRI
||
806 (LIST_FIRST(&tbp
->b_dep
) && buf_checkwrite(tbp
))) {
809 start_loffset
+= blksize
;
814 KKASSERT(tbp
->b_cmd
== BUF_CMD_DONE
);
817 * Extra memory in the buffer, punt on this buffer.
818 * XXX we could handle this in most cases, but we would
819 * have to push the extra memory down to after our max
820 * possible cluster size and then potentially pull it back
821 * up if the cluster was terminated prematurely--too much
824 if (((tbp
->b_flags
& (B_CLUSTEROK
|B_MALLOC
)) != B_CLUSTEROK
) ||
825 (tbp
->b_bcount
!= tbp
->b_bufsize
) ||
826 (tbp
->b_bcount
!= blksize
) ||
827 (bytes
== blksize
) ||
828 ((bp
= getpbuf(&cluster_pbuf_freecnt
)) == NULL
)) {
829 totalwritten
+= tbp
->b_bufsize
;
831 start_loffset
+= blksize
;
837 * Set up the pbuf. Track our append point with b_bcount
838 * and b_bufsize. b_bufsize is not used by the device but
839 * our caller uses it to loop clusters and we use it to
840 * detect a premature EOF on the block device.
844 bp
->b_xio
.xio_npages
= 0;
845 bp
->b_loffset
= tbp
->b_loffset
;
846 bp
->b_bio2
.bio_offset
= tbp
->b_bio2
.bio_offset
;
849 * We are synthesizing a buffer out of vm_page_t's, but
850 * if the block size is not page aligned then the starting
851 * address may not be either. Inherit the b_data offset
852 * from the original buffer.
854 bp
->b_data
= (char *)((vm_offset_t
)bp
->b_data
|
855 ((vm_offset_t
)tbp
->b_data
& PAGE_MASK
));
856 bp
->b_flags
&= ~B_ERROR
;
857 bp
->b_flags
|= B_CLUSTER
| B_BNOCLIP
|
858 (tbp
->b_flags
& (B_VMIO
| B_NEEDCOMMIT
));
859 bp
->b_bio1
.bio_caller_info1
.cluster_head
= NULL
;
860 bp
->b_bio1
.bio_caller_info2
.cluster_tail
= NULL
;
863 * From this location in the file, scan forward to see
864 * if there are buffers with adjacent data that need to
865 * be written as well.
867 for (i
= 0; i
< bytes
; (i
+= blksize
), (start_loffset
+= blksize
)) {
868 if (i
!= 0) { /* If not the first buffer */
869 tbp
= findblk(vp
, start_loffset
,
872 * Buffer not found or could not be locked
879 * If it IS in core, but has different
880 * characteristics, then don't cluster
883 if ((tbp
->b_flags
& (B_VMIO
| B_CLUSTEROK
|
884 B_INVAL
| B_DELWRI
| B_NEEDCOMMIT
))
885 != (B_DELWRI
| B_CLUSTEROK
|
886 (bp
->b_flags
& (B_VMIO
| B_NEEDCOMMIT
))) ||
887 (tbp
->b_flags
& B_LOCKED
) ||
888 (LIST_FIRST(&tbp
->b_dep
) &&
896 * Check that the combined cluster
897 * would make sense with regard to pages
898 * and would not be too large
900 if ((tbp
->b_bcount
!= blksize
) ||
901 ((bp
->b_bio2
.bio_offset
+ i
) !=
902 tbp
->b_bio2
.bio_offset
) ||
903 ((tbp
->b_xio
.xio_npages
+ bp
->b_xio
.xio_npages
) >
904 (maxiosize
/ PAGE_SIZE
))) {
909 * Ok, it's passed all the tests,
910 * so remove it from the free list
911 * and mark it busy. We will use it.
914 KKASSERT(tbp
->b_cmd
== BUF_CMD_DONE
);
915 } /* end of code for non-first buffers only */
918 * If the IO is via the VM then we do some
919 * special VM hackery (yuck). Since the buffer's
920 * block size may not be page-aligned it is possible
921 * for a page to be shared between two buffers. We
922 * have to get rid of the duplication when building
925 if (tbp
->b_flags
& B_VMIO
) {
928 if (i
!= 0) { /* if not first buffer */
929 for (j
= 0; j
< tbp
->b_xio
.xio_npages
; ++j
) {
930 m
= tbp
->b_xio
.xio_pages
[j
];
931 if (m
->flags
& PG_BUSY
) {
938 for (j
= 0; j
< tbp
->b_xio
.xio_npages
; ++j
) {
939 m
= tbp
->b_xio
.xio_pages
[j
];
941 vm_object_pip_add(m
->object
, 1);
942 if ((bp
->b_xio
.xio_npages
== 0) ||
943 (bp
->b_xio
.xio_pages
[bp
->b_xio
.xio_npages
- 1] != m
)) {
944 bp
->b_xio
.xio_pages
[bp
->b_xio
.xio_npages
] = m
;
945 bp
->b_xio
.xio_npages
++;
949 bp
->b_bcount
+= blksize
;
950 bp
->b_bufsize
+= blksize
;
953 tbp
->b_flags
&= ~B_ERROR
;
954 tbp
->b_cmd
= BUF_CMD_WRITE
;
956 cluster_append(&bp
->b_bio1
, tbp
);
959 * check for latent dependencies to be handled
961 if (LIST_FIRST(&tbp
->b_dep
) != NULL
)
965 pmap_qenter(trunc_page((vm_offset_t
) bp
->b_data
),
966 (vm_page_t
*) bp
->b_xio
.xio_pages
, bp
->b_xio
.xio_npages
);
967 if (bp
->b_bufsize
> bp
->b_kvasize
) {
969 "cluster_wbuild: b_bufsize(%d) > b_kvasize(%d)\n",
970 bp
->b_bufsize
, bp
->b_kvasize
);
972 totalwritten
+= bp
->b_bufsize
;
974 bp
->b_dirtyend
= bp
->b_bufsize
;
975 bp
->b_bio1
.bio_done
= cluster_callback
;
976 bp
->b_cmd
= BUF_CMD_WRITE
;
978 vfs_busy_pages(vp
, bp
);
979 bp
->b_runningbufspace
= bp
->b_bufsize
;
980 if (bp
->b_runningbufspace
) {
981 runningbufspace
+= bp
->b_runningbufspace
;
985 vn_strategy(vp
, &bp
->b_bio1
);
993 * Collect together all the buffers in a cluster.
994 * Plus add one additional buffer.
996 static struct cluster_save
*
997 cluster_collectbufs(struct vnode
*vp
, struct buf
*last_bp
, int blksize
)
999 struct cluster_save
*buflist
;
1004 len
= (int)(vp
->v_lastw
- vp
->v_cstart
+ blksize
) / blksize
;
1005 buflist
= kmalloc(sizeof(struct buf
*) * (len
+ 1) + sizeof(*buflist
),
1006 M_SEGMENT
, M_WAITOK
);
1007 buflist
->bs_nchildren
= 0;
1008 buflist
->bs_children
= (struct buf
**) (buflist
+ 1);
1009 for (loffset
= vp
->v_cstart
, i
= 0; i
< len
; (loffset
+= blksize
), i
++) {
1010 (void) bread(vp
, loffset
, last_bp
->b_bcount
, &bp
);
1011 buflist
->bs_children
[i
] = bp
;
1012 if (bp
->b_bio2
.bio_offset
== NOOFFSET
) {
1013 VOP_BMAP(bp
->b_vp
, bp
->b_loffset
,
1014 &bp
->b_bio2
.bio_offset
,
1015 NULL
, NULL
, BUF_CMD_WRITE
);
1018 buflist
->bs_children
[i
] = bp
= last_bp
;
1019 if (bp
->b_bio2
.bio_offset
== NOOFFSET
) {
1020 VOP_BMAP(bp
->b_vp
, bp
->b_loffset
, &bp
->b_bio2
.bio_offset
,
1021 NULL
, NULL
, BUF_CMD_WRITE
);
1023 buflist
->bs_nchildren
= i
+ 1;
1028 cluster_append(struct bio
*bio
, struct buf
*tbp
)
1030 tbp
->b_cluster_next
= NULL
;
1031 if (bio
->bio_caller_info1
.cluster_head
== NULL
) {
1032 bio
->bio_caller_info1
.cluster_head
= tbp
;
1033 bio
->bio_caller_info2
.cluster_tail
= tbp
;
1035 bio
->bio_caller_info2
.cluster_tail
->b_cluster_next
= tbp
;
1036 bio
->bio_caller_info2
.cluster_tail
= tbp
;
1042 cluster_setram (struct buf
*bp
)
1044 bp
->b_flags
|= B_RAM
;
1045 if (bp
->b_xio
.xio_npages
)
1046 vm_page_flag_set(bp
->b_xio
.xio_pages
[0], PG_RAM
);