4 * Copyright IBM, Corp. 2011
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
15 #include "block/block_int.h"
16 #include "block/blockjob.h"
17 #include "qemu/ratelimit.h"
21 * Size of data buffer for populating the image file. This should be large
22 * enough to process multiple clusters in a single call, so that populating
23 * contiguous regions of the image is efficient.
25 STREAM_BUFFER_SIZE
= 512 * 1024, /* in bytes */
28 #define SLICE_TIME 100000000ULL /* ns */
30 typedef struct StreamBlockJob
{
33 BlockDriverState
*base
;
34 BlockdevOnError on_error
;
35 char backing_file_id
[1024];
38 static int coroutine_fn
stream_populate(BlockDriverState
*bs
,
39 int64_t sector_num
, int nb_sectors
,
44 .iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
,
48 qemu_iovec_init_external(&qiov
, &iov
, 1);
50 /* Copy-on-read the unallocated clusters */
51 return bdrv_co_copy_on_readv(bs
, sector_num
, nb_sectors
, &qiov
);
54 static void close_unused_images(BlockDriverState
*top
, BlockDriverState
*base
,
57 BlockDriverState
*intermediate
;
58 intermediate
= top
->backing_hd
;
60 /* Must assign before bdrv_delete() to prevent traversing dangling pointer
61 * while we delete backing image instances.
63 top
->backing_hd
= base
;
65 while (intermediate
) {
66 BlockDriverState
*unused
;
69 if (intermediate
== base
) {
73 unused
= intermediate
;
74 intermediate
= intermediate
->backing_hd
;
75 unused
->backing_hd
= NULL
;
80 static void coroutine_fn
stream_run(void *opaque
)
82 StreamBlockJob
*s
= opaque
;
83 BlockDriverState
*bs
= s
->common
.bs
;
84 BlockDriverState
*base
= s
->base
;
85 int64_t sector_num
, end
;
91 s
->common
.len
= bdrv_getlength(bs
);
92 if (s
->common
.len
< 0) {
93 block_job_completed(&s
->common
, s
->common
.len
);
97 end
= s
->common
.len
>> BDRV_SECTOR_BITS
;
98 buf
= qemu_blockalign(bs
, STREAM_BUFFER_SIZE
);
100 /* Turn on copy-on-read for the whole block device so that guest read
101 * requests help us make progress. Only do this when copying the entire
102 * backing chain since the copy-on-read operation does not take base into
106 bdrv_enable_copy_on_read(bs
);
109 for (sector_num
= 0; sector_num
< end
; sector_num
+= n
) {
110 uint64_t delay_ns
= 0;
114 /* Note that even when no rate limit is applied we need to yield
115 * with no pending I/O here so that bdrv_drain_all() returns.
117 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
118 if (block_job_is_cancelled(&s
->common
)) {
122 ret
= bdrv_co_is_allocated(bs
, sector_num
,
123 STREAM_BUFFER_SIZE
/ BDRV_SECTOR_SIZE
, &n
);
125 /* Allocated in the top, no need to copy. */
128 /* Copy if allocated in the intermediate images. Limit to the
129 * known-unallocated area [sector_num, sector_num+n). */
130 ret
= bdrv_co_is_allocated_above(bs
->backing_hd
, base
,
133 /* Finish early if end of backing file has been reached */
134 if (ret
== 0 && n
== 0) {
135 n
= end
- sector_num
;
140 trace_stream_one_iteration(s
, sector_num
, n
, ret
);
141 if (ret
>= 0 && copy
) {
142 if (s
->common
.speed
) {
143 delay_ns
= ratelimit_calculate_delay(&s
->limit
, n
);
148 ret
= stream_populate(bs
, sector_num
, n
, buf
);
151 BlockErrorAction action
=
152 block_job_error_action(&s
->common
, s
->common
.bs
, s
->on_error
,
154 if (action
== BDRV_ACTION_STOP
) {
161 if (action
== BDRV_ACTION_REPORT
) {
167 /* Publish progress */
168 s
->common
.offset
+= n
* BDRV_SECTOR_SIZE
;
172 bdrv_disable_copy_on_read(bs
);
175 /* Do not remove the backing file if an error was there but ignored. */
178 if (!block_job_is_cancelled(&s
->common
) && sector_num
== end
&& ret
== 0) {
179 const char *base_id
= NULL
, *base_fmt
= NULL
;
181 base_id
= s
->backing_file_id
;
183 base_fmt
= base
->drv
->format_name
;
186 ret
= bdrv_change_backing_file(bs
, base_id
, base_fmt
);
187 close_unused_images(bs
, base
, base_id
);
191 block_job_completed(&s
->common
, ret
);
194 static void stream_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
196 StreamBlockJob
*s
= container_of(job
, StreamBlockJob
, common
);
199 error_set(errp
, QERR_INVALID_PARAMETER
, "speed");
202 ratelimit_set_speed(&s
->limit
, speed
/ BDRV_SECTOR_SIZE
, SLICE_TIME
);
205 static const BlockJobType stream_job_type
= {
206 .instance_size
= sizeof(StreamBlockJob
),
207 .job_type
= "stream",
208 .set_speed
= stream_set_speed
,
211 void stream_start(BlockDriverState
*bs
, BlockDriverState
*base
,
212 const char *base_id
, int64_t speed
,
213 BlockdevOnError on_error
,
214 BlockDriverCompletionFunc
*cb
,
215 void *opaque
, Error
**errp
)
219 if ((on_error
== BLOCKDEV_ON_ERROR_STOP
||
220 on_error
== BLOCKDEV_ON_ERROR_ENOSPC
) &&
221 !bdrv_iostatus_is_enabled(bs
)) {
222 error_set(errp
, QERR_INVALID_PARAMETER
, "on-error");
226 s
= block_job_create(&stream_job_type
, bs
, speed
, cb
, opaque
, errp
);
233 pstrcpy(s
->backing_file_id
, sizeof(s
->backing_file_id
), base_id
);
236 s
->on_error
= on_error
;
237 s
->common
.co
= qemu_coroutine_create(stream_run
);
238 trace_stream_start(bs
, base
, s
, s
->common
.co
, opaque
);
239 qemu_coroutine_enter(s
->common
.co
, s
);