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 #if defined(CLUSTERDEBUG)
60 #include <sys/sysctl.h>
61 static int rcluster
= 0;
62 SYSCTL_INT(_debug
, OID_AUTO
, rcluster
, CTLFLAG_RW
, &rcluster
, 0, "");
65 static MALLOC_DEFINE(M_SEGMENT
, "cluster_save", "cluster_save buffer");
67 static struct cluster_save
*
68 cluster_collectbufs (struct vnode
*vp
, struct buf
*last_bp
,
71 cluster_rbuild (struct vnode
*vp
, off_t filesize
, off_t loffset
,
72 off_t doffset
, int blksize
, int run
,
73 struct buf
*fbp
, int doasync
);
74 static void cluster_callback (struct bio
*);
77 static int write_behind
= 1;
78 SYSCTL_INT(_vfs
, OID_AUTO
, write_behind
, CTLFLAG_RW
, &write_behind
, 0, "");
80 extern vm_page_t bogus_page
;
82 extern int cluster_pbuf_freecnt
;
85 * Maximum number of blocks for read-ahead.
90 * This replaces bread.
93 cluster_read(struct vnode
*vp
, off_t filesize
, off_t loffset
,
94 int blksize
, int totread
, int seqcount
, struct buf
**bpp
)
96 struct buf
*bp
, *rbp
, *reqbp
;
101 int maxra
, racluster
;
106 * Try to limit the amount of read-ahead by a few
107 * ad-hoc parameters. This needs work!!!
109 racluster
= vmaxiosize(vp
) / blksize
;
110 maxra
= 2 * racluster
+ (totread
/ blksize
);
117 * get the requested block
119 *bpp
= reqbp
= bp
= getblk(vp
, loffset
, blksize
, 0, 0);
120 origoffset
= loffset
;
123 * if it is in the cache, then check to see if the reads have been
124 * sequential. If they have, then try some read-ahead, otherwise
125 * back-off on prospective read-aheads.
127 if (bp
->b_flags
& B_CACHE
) {
130 } else if ((bp
->b_flags
& B_RAM
) == 0) {
134 bp
->b_flags
&= ~B_RAM
;
136 * We do the crit here so that there is no window
137 * between the findblk and the b_usecount increment
138 * below. We opt to keep the crit out of the loop
142 for (i
= 1; i
< maxra
; i
++) {
143 if (!(tbp
= findblk(vp
, loffset
+ i
* blksize
))) {
148 * Set another read-ahead mark so we know
151 if (((i
% racluster
) == (racluster
- 1)) ||
153 tbp
->b_flags
|= B_RAM
;
159 loffset
+= i
* blksize
;
163 off_t firstread
= bp
->b_loffset
;
166 KASSERT(firstread
!= NOOFFSET
,
167 ("cluster_read: no buffer offset"));
168 if (firstread
+ totread
> filesize
)
169 totread
= (int)(filesize
- firstread
);
170 nblks
= totread
/ blksize
;
174 if (nblks
> racluster
)
177 error
= VOP_BMAP(vp
, loffset
, &doffset
,
178 &burstbytes
, NULL
, BUF_CMD_READ
);
180 goto single_block_read
;
181 if (doffset
== NOOFFSET
)
182 goto single_block_read
;
183 if (burstbytes
< blksize
* 2)
184 goto single_block_read
;
185 if (nblks
> burstbytes
/ blksize
)
186 nblks
= burstbytes
/ blksize
;
188 bp
= cluster_rbuild(vp
, filesize
, loffset
,
189 doffset
, blksize
, nblks
, bp
, 0);
190 loffset
+= bp
->b_bufsize
;
194 * if it isn't in the cache, then get a chunk from
195 * disk if sequential, otherwise just get the block.
197 bp
->b_flags
|= B_RAM
;
203 * Handle the synchronous read. This only occurs if B_CACHE was
204 * not set. bp (and rbp) could be either a cluster bp or a normal
205 * bp depending on the what cluster_rbuild() decided to do. If
206 * it is a cluster bp, vfs_busy_pages() has already been called.
209 #if defined(CLUSTERDEBUG)
211 kprintf("S(%lld,%d,%d) ",
212 bp
->b_loffset
, bp
->b_bcount
, seqcount
);
214 bp
->b_cmd
= BUF_CMD_READ
;
215 if ((bp
->b_flags
& B_CLUSTER
) == 0)
216 vfs_busy_pages(vp
, bp
);
217 bp
->b_flags
&= ~(B_ERROR
|B_INVAL
);
218 if ((bp
->b_flags
& B_ASYNC
) || bp
->b_bio1
.bio_done
!= NULL
)
220 vn_strategy(vp
, &bp
->b_bio1
);
221 if (bp
->b_flags
& B_ERROR
) {
222 if ((error
= bp
->b_error
) == 0)
230 * If we have been doing sequential I/O, then do some read-ahead.
232 * Only mess with buffers which we can immediately lock. HAMMER
233 * will do device-readahead irrespective of what the blocks
239 loffset
< origoffset
+ seqcount
* blksize
&&
240 loffset
+ blksize
<= filesize
247 rbp
= getblk(vp
, loffset
, blksize
,
248 GETBLK_SZMATCH
|GETBLK_NOWAIT
, 0);
251 if ((rbp
->b_flags
& B_CACHE
)) {
257 * An error from the read-ahead bmap has nothing to do
258 * with the caller's original request.
260 tmp_error
= VOP_BMAP(vp
, loffset
, &doffset
,
261 &burstbytes
, NULL
, BUF_CMD_READ
);
262 if (tmp_error
|| doffset
== NOOFFSET
) {
263 rbp
->b_flags
|= B_INVAL
;
268 ntoread
= burstbytes
/ blksize
;
269 nblksread
= (totread
+ blksize
- 1) / blksize
;
270 if (seqcount
< nblksread
)
271 seqcount
= nblksread
;
272 if (ntoread
> seqcount
)
275 rbp
->b_flags
|= B_RAM
/* | B_AGE*/;
277 rbp
= cluster_rbuild(vp
, filesize
, loffset
,
281 rbp
->b_bio2
.bio_offset
= doffset
;
283 #if defined(CLUSTERDEBUG)
286 kprintf("A+(%lld,%d,%lld,%d) ",
287 rbp
->b_loffset
, rbp
->b_bcount
,
288 rbp
->b_loffset
- origoffset
,
291 kprintf("A(%lld,%d,%lld,%d) ",
292 rbp
->b_loffset
, rbp
->b_bcount
,
293 rbp
->b_loffset
- origoffset
,
297 rbp
->b_flags
&= ~(B_ERROR
|B_INVAL
);
298 rbp
->b_flags
|= B_ASYNC
;
299 rbp
->b_cmd
= BUF_CMD_READ
;
301 if ((rbp
->b_flags
& B_CLUSTER
) == 0)
302 vfs_busy_pages(vp
, rbp
);
303 BUF_KERNPROC(rbp
); /* B_ASYNC */
304 vn_strategy(vp
, &rbp
->b_bio1
);
309 return (biowait(reqbp
));
315 * If blocks are contiguous on disk, use this to provide clustered
316 * read ahead. We will read as many blocks as possible sequentially
317 * and then parcel them up into logical blocks in the buffer hash table.
320 cluster_rbuild(struct vnode
*vp
, off_t filesize
, off_t loffset
,
321 off_t doffset
, int blksize
, int run
, struct buf
*fbp
, int doasync
)
323 struct buf
*bp
, *tbp
;
326 int maxiosize
= vmaxiosize(vp
);
331 while (loffset
+ run
* blksize
> filesize
) {
336 tbp
->b_bio2
.bio_offset
= doffset
;
337 if((tbp
->b_flags
& B_MALLOC
) ||
338 ((tbp
->b_flags
& B_VMIO
) == 0) || (run
<= 1)) {
342 bp
= trypbuf(&cluster_pbuf_freecnt
);
347 * We are synthesizing a buffer out of vm_page_t's, but
348 * if the block size is not page aligned then the starting
349 * address may not be either. Inherit the b_data offset
350 * from the original buffer.
352 bp
->b_data
= (char *)((vm_offset_t
)bp
->b_data
|
353 ((vm_offset_t
)tbp
->b_data
& PAGE_MASK
));
354 bp
->b_flags
|= B_ASYNC
| B_CLUSTER
| B_VMIO
;
355 bp
->b_cmd
= BUF_CMD_READ
;
356 bp
->b_bio1
.bio_done
= cluster_callback
;
357 bp
->b_bio1
.bio_caller_info1
.cluster_head
= NULL
;
358 bp
->b_bio1
.bio_caller_info2
.cluster_tail
= NULL
;
359 bp
->b_loffset
= loffset
;
360 bp
->b_bio2
.bio_offset
= doffset
;
361 KASSERT(bp
->b_loffset
!= NOOFFSET
,
362 ("cluster_rbuild: no buffer offset"));
366 bp
->b_xio
.xio_npages
= 0;
368 for (boffset
= doffset
, i
= 0; i
< run
; ++i
, boffset
+= blksize
) {
370 if ((bp
->b_xio
.xio_npages
* PAGE_SIZE
) +
371 round_page(blksize
) > maxiosize
) {
376 * Shortcut some checks and try to avoid buffers that
377 * would block in the lock. The same checks have to
378 * be made again after we officially get the buffer.
380 tbp
= getblk(vp
, loffset
+ i
* blksize
, blksize
,
381 GETBLK_SZMATCH
|GETBLK_NOWAIT
, 0);
384 for (j
= 0; j
< tbp
->b_xio
.xio_npages
; j
++) {
385 if (tbp
->b_xio
.xio_pages
[j
]->valid
)
388 if (j
!= tbp
->b_xio
.xio_npages
) {
394 * Stop scanning if the buffer is fuly valid
395 * (marked B_CACHE), or locked (may be doing a
396 * background write), or if the buffer is not
397 * VMIO backed. The clustering code can only deal
398 * with VMIO-backed buffers.
400 if ((tbp
->b_flags
& (B_CACHE
|B_LOCKED
)) ||
401 (tbp
->b_flags
& B_VMIO
) == 0 ||
402 (LIST_FIRST(&tbp
->b_dep
) != NULL
&&
410 * The buffer must be completely invalid in order to
411 * take part in the cluster. If it is partially valid
414 for (j
= 0;j
< tbp
->b_xio
.xio_npages
; j
++) {
415 if (tbp
->b_xio
.xio_pages
[j
]->valid
)
418 if (j
!= tbp
->b_xio
.xio_npages
) {
424 * Set a read-ahead mark as appropriate
426 if (i
== 1 || i
== (run
- 1))
427 tbp
->b_flags
|= B_RAM
;
430 * Depress the priority of buffers not explicitly
433 /* tbp->b_flags |= B_AGE; */
436 * Set the block number if it isn't set, otherwise
437 * if it is make sure it matches the block number we
440 if (tbp
->b_bio2
.bio_offset
== NOOFFSET
) {
441 tbp
->b_bio2
.bio_offset
= boffset
;
442 } else if (tbp
->b_bio2
.bio_offset
!= boffset
) {
448 * The first buffer is setup async if doasync is specified.
449 * All other buffers in the cluster are setup async. This
450 * way the caller can decide how to deal with the requested
454 tbp
->b_flags
|= B_ASYNC
;
455 tbp
->b_cmd
= BUF_CMD_READ
;
457 cluster_append(&bp
->b_bio1
, tbp
);
458 for (j
= 0; j
< tbp
->b_xio
.xio_npages
; ++j
) {
460 m
= tbp
->b_xio
.xio_pages
[j
];
462 vm_object_pip_add(m
->object
, 1);
463 if ((bp
->b_xio
.xio_npages
== 0) ||
464 (bp
->b_xio
.xio_pages
[bp
->b_xio
.xio_npages
-1] != m
)) {
465 bp
->b_xio
.xio_pages
[bp
->b_xio
.xio_npages
] = m
;
466 bp
->b_xio
.xio_npages
++;
468 if ((m
->valid
& VM_PAGE_BITS_ALL
) == VM_PAGE_BITS_ALL
)
469 tbp
->b_xio
.xio_pages
[j
] = bogus_page
;
472 * XXX shouldn't this be += size for both, like in
475 * Don't inherit tbp->b_bufsize as it may be larger due to
476 * a non-page-aligned size. Instead just aggregate using
479 if (tbp
->b_bcount
!= blksize
)
480 kprintf("warning: tbp->b_bcount wrong %d vs %d\n", tbp
->b_bcount
, blksize
);
481 if (tbp
->b_bufsize
!= blksize
)
482 kprintf("warning: tbp->b_bufsize wrong %d vs %d\n", tbp
->b_bufsize
, blksize
);
483 bp
->b_bcount
+= blksize
;
484 bp
->b_bufsize
+= blksize
;
488 * Fully valid pages in the cluster are already good and do not need
489 * to be re-read from disk. Replace the page with bogus_page
491 for (j
= 0; j
< bp
->b_xio
.xio_npages
; j
++) {
492 if ((bp
->b_xio
.xio_pages
[j
]->valid
& VM_PAGE_BITS_ALL
) ==
494 bp
->b_xio
.xio_pages
[j
] = bogus_page
;
497 if (bp
->b_bufsize
> bp
->b_kvasize
) {
498 panic("cluster_rbuild: b_bufsize(%d) > b_kvasize(%d)",
499 bp
->b_bufsize
, bp
->b_kvasize
);
502 pmap_qenter(trunc_page((vm_offset_t
) bp
->b_data
),
503 (vm_page_t
*)bp
->b_xio
.xio_pages
, bp
->b_xio
.xio_npages
);
508 * Cleanup after a clustered read or write.
509 * This is complicated by the fact that any of the buffers might have
510 * extra memory (if there were no empty buffer headers at allocbuf time)
511 * that we will need to shift around.
513 * The returned bio is &bp->b_bio1
516 cluster_callback(struct bio
*bio
)
518 struct buf
*bp
= bio
->bio_buf
;
523 * Must propogate errors to all the components. A short read (EOF)
524 * is a critical error.
526 if (bp
->b_flags
& B_ERROR
) {
528 } else if (bp
->b_bcount
!= bp
->b_bufsize
) {
529 panic("cluster_callback: unexpected EOF on cluster %p!", bio
);
532 pmap_qremove(trunc_page((vm_offset_t
) bp
->b_data
), bp
->b_xio
.xio_npages
);
534 * Move memory from the large cluster buffer into the component
535 * buffers and mark IO as done on these. Since the memory map
536 * is the same, no actual copying is required.
538 while ((tbp
= bio
->bio_caller_info1
.cluster_head
) != NULL
) {
539 bio
->bio_caller_info1
.cluster_head
= tbp
->b_cluster_next
;
541 tbp
->b_flags
|= B_ERROR
;
542 tbp
->b_error
= error
;
544 tbp
->b_dirtyoff
= tbp
->b_dirtyend
= 0;
545 tbp
->b_flags
&= ~(B_ERROR
|B_INVAL
);
547 * XXX the bdwrite()/bqrelse() issued during
548 * cluster building clears B_RELBUF (see bqrelse()
549 * comment). If direct I/O was specified, we have
550 * to restore it here to allow the buffer and VM
553 if (tbp
->b_flags
& B_DIRECT
)
554 tbp
->b_flags
|= B_RELBUF
;
556 biodone(&tbp
->b_bio1
);
558 relpbuf(bp
, &cluster_pbuf_freecnt
);
564 * Implement modified write build for cluster.
566 * write_behind = 0 write behind disabled
567 * write_behind = 1 write behind normal (default)
568 * write_behind = 2 write behind backed-off
572 cluster_wbuild_wb(struct vnode
*vp
, int blksize
, off_t start_loffset
, int len
)
576 switch(write_behind
) {
578 if (start_loffset
< len
)
580 start_loffset
-= len
;
583 r
= cluster_wbuild(vp
, blksize
, start_loffset
, len
);
593 * Do clustered write for FFS.
596 * 1. Write is not sequential (write asynchronously)
597 * Write is sequential:
598 * 2. beginning of cluster - begin cluster
599 * 3. middle of a cluster - add to cluster
600 * 4. end of a cluster - asynchronously write cluster
603 cluster_write(struct buf
*bp
, off_t filesize
, int blksize
, int seqcount
)
607 int maxclen
, cursize
;
611 if (vp
->v_type
== VREG
)
612 async
= vp
->v_mount
->mnt_flag
& MNT_ASYNC
;
615 loffset
= bp
->b_loffset
;
616 KASSERT(bp
->b_loffset
!= NOOFFSET
,
617 ("cluster_write: no buffer offset"));
619 /* Initialize vnode to beginning of file. */
621 vp
->v_lasta
= vp
->v_clen
= vp
->v_cstart
= vp
->v_lastw
= 0;
623 if (vp
->v_clen
== 0 || loffset
!= vp
->v_lastw
+ blksize
||
624 bp
->b_bio2
.bio_offset
== NOOFFSET
||
625 (bp
->b_bio2
.bio_offset
!= vp
->v_lasta
+ blksize
)) {
626 maxclen
= vmaxiosize(vp
);
627 if (vp
->v_clen
!= 0) {
629 * Next block is not sequential.
631 * If we are not writing at end of file, the process
632 * seeked to another point in the file since its last
633 * write, or we have reached our maximum cluster size,
634 * then push the previous cluster. Otherwise try
635 * reallocating to make it sequential.
637 * Change to algorithm: only push previous cluster if
638 * it was sequential from the point of view of the
639 * seqcount heuristic, otherwise leave the buffer
640 * intact so we can potentially optimize the I/O
641 * later on in the buf_daemon or update daemon
644 cursize
= vp
->v_lastw
- vp
->v_cstart
+ blksize
;
645 if (bp
->b_loffset
+ blksize
!= filesize
||
646 loffset
!= vp
->v_lastw
+ blksize
|| vp
->v_clen
<= cursize
) {
647 if (!async
&& seqcount
> 0) {
648 cluster_wbuild_wb(vp
, blksize
,
649 vp
->v_cstart
, cursize
);
652 struct buf
**bpp
, **endbp
;
653 struct cluster_save
*buflist
;
655 buflist
= cluster_collectbufs(vp
, bp
, blksize
);
656 endbp
= &buflist
->bs_children
657 [buflist
->bs_nchildren
- 1];
658 if (VOP_REALLOCBLKS(vp
, buflist
)) {
660 * Failed, push the previous cluster
661 * if *really* writing sequentially
662 * in the logical file (seqcount > 1),
663 * otherwise delay it in the hopes that
664 * the low level disk driver can
665 * optimize the write ordering.
667 for (bpp
= buflist
->bs_children
;
670 kfree(buflist
, M_SEGMENT
);
672 cluster_wbuild_wb(vp
,
673 blksize
, vp
->v_cstart
,
678 * Succeeded, keep building cluster.
680 for (bpp
= buflist
->bs_children
;
683 kfree(buflist
, M_SEGMENT
);
684 vp
->v_lastw
= loffset
;
685 vp
->v_lasta
= bp
->b_bio2
.bio_offset
;
691 * Consider beginning a cluster. If at end of file, make
692 * cluster as large as possible, otherwise find size of
695 if ((vp
->v_type
== VREG
) &&
696 bp
->b_loffset
+ blksize
!= filesize
&&
697 (bp
->b_bio2
.bio_offset
== NOOFFSET
) &&
698 (VOP_BMAP(vp
, loffset
, &bp
->b_bio2
.bio_offset
, &maxclen
, NULL
, BUF_CMD_WRITE
) ||
699 bp
->b_bio2
.bio_offset
== NOOFFSET
)) {
702 vp
->v_lasta
= bp
->b_bio2
.bio_offset
;
703 vp
->v_cstart
= loffset
+ blksize
;
704 vp
->v_lastw
= loffset
;
707 if (maxclen
> blksize
)
708 vp
->v_clen
= maxclen
- blksize
;
711 if (!async
&& vp
->v_clen
== 0) { /* I/O not contiguous */
712 vp
->v_cstart
= loffset
+ blksize
;
714 } else { /* Wait for rest of cluster */
715 vp
->v_cstart
= loffset
;
718 } else if (loffset
== vp
->v_cstart
+ vp
->v_clen
) {
720 * At end of cluster, write it out if seqcount tells us we
721 * are operating sequentially, otherwise let the buf or
722 * update daemon handle it.
726 cluster_wbuild_wb(vp
, blksize
, vp
->v_cstart
,
727 vp
->v_clen
+ blksize
);
729 vp
->v_cstart
= loffset
+ blksize
;
730 } else if (vm_page_count_severe()) {
732 * We are low on memory, get it going NOW
737 * In the middle of a cluster, so just delay the I/O for now.
741 vp
->v_lastw
= loffset
;
742 vp
->v_lasta
= bp
->b_bio2
.bio_offset
;
747 * This is an awful lot like cluster_rbuild...wish they could be combined.
748 * The last lbn argument is the current block on which I/O is being
749 * performed. Check to see that it doesn't fall in the middle of
750 * the current block (if last_bp == NULL).
753 cluster_wbuild(struct vnode
*vp
, int blksize
, off_t start_loffset
, int bytes
)
755 struct buf
*bp
, *tbp
;
757 int totalwritten
= 0;
758 int maxiosize
= vmaxiosize(vp
);
763 * If the buffer is not delayed-write (i.e. dirty), or it
764 * is delayed-write but either locked or inval, it cannot
765 * partake in the clustered write.
767 if (((tbp
= findblk(vp
, start_loffset
)) == NULL
) ||
768 ((tbp
->b_flags
& (B_LOCKED
| B_INVAL
| B_DELWRI
)) != B_DELWRI
) ||
769 (LIST_FIRST(&tbp
->b_dep
) != NULL
&& buf_checkwrite(tbp
)) ||
770 BUF_LOCK(tbp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
771 start_loffset
+= blksize
;
777 KKASSERT(tbp
->b_cmd
== BUF_CMD_DONE
);
781 * Extra memory in the buffer, punt on this buffer.
782 * XXX we could handle this in most cases, but we would
783 * have to push the extra memory down to after our max
784 * possible cluster size and then potentially pull it back
785 * up if the cluster was terminated prematurely--too much
788 if (((tbp
->b_flags
& (B_CLUSTEROK
|B_MALLOC
)) != B_CLUSTEROK
) ||
789 (tbp
->b_bcount
!= tbp
->b_bufsize
) ||
790 (tbp
->b_bcount
!= blksize
) ||
791 (bytes
== blksize
) ||
792 ((bp
= getpbuf(&cluster_pbuf_freecnt
)) == NULL
)) {
793 totalwritten
+= tbp
->b_bufsize
;
795 start_loffset
+= blksize
;
801 * Set up the pbuf. Track our append point with b_bcount
802 * and b_bufsize. b_bufsize is not used by the device but
803 * our caller uses it to loop clusters and we use it to
804 * detect a premature EOF on the block device.
808 bp
->b_xio
.xio_npages
= 0;
809 bp
->b_loffset
= tbp
->b_loffset
;
810 bp
->b_bio2
.bio_offset
= tbp
->b_bio2
.bio_offset
;
813 * We are synthesizing a buffer out of vm_page_t's, but
814 * if the block size is not page aligned then the starting
815 * address may not be either. Inherit the b_data offset
816 * from the original buffer.
818 bp
->b_data
= (char *)((vm_offset_t
)bp
->b_data
|
819 ((vm_offset_t
)tbp
->b_data
& PAGE_MASK
));
820 bp
->b_flags
&= ~B_ERROR
;
821 bp
->b_flags
|= B_CLUSTER
| B_BNOCLIP
|
822 (tbp
->b_flags
& (B_VMIO
| B_NEEDCOMMIT
));
823 bp
->b_bio1
.bio_done
= cluster_callback
;
824 bp
->b_bio1
.bio_caller_info1
.cluster_head
= NULL
;
825 bp
->b_bio1
.bio_caller_info2
.cluster_tail
= NULL
;
827 * From this location in the file, scan forward to see
828 * if there are buffers with adjacent data that need to
829 * be written as well.
831 for (i
= 0; i
< bytes
; (i
+= blksize
), (start_loffset
+= blksize
)) {
832 if (i
!= 0) { /* If not the first buffer */
835 * If the adjacent data is not even in core it
836 * can't need to be written.
838 if ((tbp
= findblk(vp
, start_loffset
)) == NULL
) {
844 * If it IS in core, but has different
845 * characteristics, or is locked (which
846 * means it could be undergoing a background
847 * I/O or be in a weird state), then don't
850 if ((tbp
->b_flags
& (B_VMIO
| B_CLUSTEROK
|
851 B_INVAL
| B_DELWRI
| B_NEEDCOMMIT
))
852 != (B_DELWRI
| B_CLUSTEROK
|
853 (bp
->b_flags
& (B_VMIO
| B_NEEDCOMMIT
))) ||
854 (tbp
->b_flags
& B_LOCKED
) ||
855 (LIST_FIRST(&tbp
->b_dep
) != NULL
&& buf_checkwrite(tbp
)) ||
856 BUF_LOCK(tbp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
862 * Check that the combined cluster
863 * would make sense with regard to pages
864 * and would not be too large
866 if ((tbp
->b_bcount
!= blksize
) ||
867 ((bp
->b_bio2
.bio_offset
+ i
) !=
868 tbp
->b_bio2
.bio_offset
) ||
869 ((tbp
->b_xio
.xio_npages
+ bp
->b_xio
.xio_npages
) >
870 (maxiosize
/ PAGE_SIZE
))) {
876 * Ok, it's passed all the tests,
877 * so remove it from the free list
878 * and mark it busy. We will use it.
881 KKASSERT(tbp
->b_cmd
== BUF_CMD_DONE
);
883 } /* end of code for non-first buffers only */
886 * If the IO is via the VM then we do some
887 * special VM hackery (yuck). Since the buffer's
888 * block size may not be page-aligned it is possible
889 * for a page to be shared between two buffers. We
890 * have to get rid of the duplication when building
893 if (tbp
->b_flags
& B_VMIO
) {
896 if (i
!= 0) { /* if not first buffer */
897 for (j
= 0; j
< tbp
->b_xio
.xio_npages
; ++j
) {
898 m
= tbp
->b_xio
.xio_pages
[j
];
899 if (m
->flags
& PG_BUSY
) {
906 for (j
= 0; j
< tbp
->b_xio
.xio_npages
; ++j
) {
907 m
= tbp
->b_xio
.xio_pages
[j
];
909 vm_object_pip_add(m
->object
, 1);
910 if ((bp
->b_xio
.xio_npages
== 0) ||
911 (bp
->b_xio
.xio_pages
[bp
->b_xio
.xio_npages
- 1] != m
)) {
912 bp
->b_xio
.xio_pages
[bp
->b_xio
.xio_npages
] = m
;
913 bp
->b_xio
.xio_npages
++;
917 bp
->b_bcount
+= blksize
;
918 bp
->b_bufsize
+= blksize
;
922 tbp
->b_flags
&= ~B_ERROR
;
923 tbp
->b_flags
|= B_ASYNC
;
924 tbp
->b_cmd
= BUF_CMD_WRITE
;
927 cluster_append(&bp
->b_bio1
, tbp
);
930 * check for latent dependencies to be handled
932 if (LIST_FIRST(&tbp
->b_dep
) != NULL
)
936 pmap_qenter(trunc_page((vm_offset_t
) bp
->b_data
),
937 (vm_page_t
*) bp
->b_xio
.xio_pages
, bp
->b_xio
.xio_npages
);
938 if (bp
->b_bufsize
> bp
->b_kvasize
) {
940 "cluster_wbuild: b_bufsize(%d) > b_kvasize(%d)\n",
941 bp
->b_bufsize
, bp
->b_kvasize
);
943 totalwritten
+= bp
->b_bufsize
;
945 bp
->b_dirtyend
= bp
->b_bufsize
;
946 bp
->b_flags
|= B_ASYNC
;
947 bp
->b_cmd
= BUF_CMD_WRITE
;
948 vfs_busy_pages(vp
, bp
);
949 bp
->b_runningbufspace
= bp
->b_bufsize
;
950 if (bp
->b_runningbufspace
) {
951 runningbufspace
+= bp
->b_runningbufspace
;
954 BUF_KERNPROC(bp
); /* B_ASYNC */
955 vn_strategy(vp
, &bp
->b_bio1
);
963 * Collect together all the buffers in a cluster.
964 * Plus add one additional buffer.
966 static struct cluster_save
*
967 cluster_collectbufs(struct vnode
*vp
, struct buf
*last_bp
, int blksize
)
969 struct cluster_save
*buflist
;
974 len
= (int)(vp
->v_lastw
- vp
->v_cstart
+ blksize
) / blksize
;
975 buflist
= kmalloc(sizeof(struct buf
*) * (len
+ 1) + sizeof(*buflist
),
976 M_SEGMENT
, M_WAITOK
);
977 buflist
->bs_nchildren
= 0;
978 buflist
->bs_children
= (struct buf
**) (buflist
+ 1);
979 for (loffset
= vp
->v_cstart
, i
= 0; i
< len
; (loffset
+= blksize
), i
++) {
980 (void) bread(vp
, loffset
, last_bp
->b_bcount
, &bp
);
981 buflist
->bs_children
[i
] = bp
;
982 if (bp
->b_bio2
.bio_offset
== NOOFFSET
) {
983 VOP_BMAP(bp
->b_vp
, bp
->b_loffset
,
984 &bp
->b_bio2
.bio_offset
,
985 NULL
, NULL
, BUF_CMD_WRITE
);
988 buflist
->bs_children
[i
] = bp
= last_bp
;
989 if (bp
->b_bio2
.bio_offset
== NOOFFSET
) {
990 VOP_BMAP(bp
->b_vp
, bp
->b_loffset
, &bp
->b_bio2
.bio_offset
,
991 NULL
, NULL
, BUF_CMD_WRITE
);
993 buflist
->bs_nchildren
= i
+ 1;
998 cluster_append(struct bio
*bio
, struct buf
*tbp
)
1000 tbp
->b_cluster_next
= NULL
;
1001 if (bio
->bio_caller_info1
.cluster_head
== NULL
) {
1002 bio
->bio_caller_info1
.cluster_head
= tbp
;
1003 bio
->bio_caller_info2
.cluster_tail
= tbp
;
1005 bio
->bio_caller_info2
.cluster_tail
->b_cluster_next
= tbp
;
1006 bio
->bio_caller_info2
.cluster_tail
= tbp
;