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_int.h"
16 #include "qemu/ratelimit.h"
20 * Size of data buffer for populating the image file. This should be large
21 * enough to process multiple clusters in a single call, so that populating
22 * contiguous regions of the image is efficient.
24 STREAM_BUFFER_SIZE
= 512 * 1024, /* in bytes */
27 #define SLICE_TIME 100000000ULL /* ns */
29 typedef struct StreamBlockJob
{
32 BlockDriverState
*base
;
33 char backing_file_id
[1024];
36 static int coroutine_fn
stream_populate(BlockDriverState
*bs
,
37 int64_t sector_num
, int nb_sectors
,
42 .iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
,
46 qemu_iovec_init_external(&qiov
, &iov
, 1);
48 /* Copy-on-read the unallocated clusters */
49 return bdrv_co_copy_on_readv(bs
, sector_num
, nb_sectors
, &qiov
);
52 static void close_unused_images(BlockDriverState
*top
, BlockDriverState
*base
,
55 BlockDriverState
*intermediate
;
56 intermediate
= top
->backing_hd
;
58 while (intermediate
) {
59 BlockDriverState
*unused
;
62 if (intermediate
== base
) {
66 unused
= intermediate
;
67 intermediate
= intermediate
->backing_hd
;
68 unused
->backing_hd
= NULL
;
71 top
->backing_hd
= base
;
74 static void coroutine_fn
stream_run(void *opaque
)
76 StreamBlockJob
*s
= opaque
;
77 BlockDriverState
*bs
= s
->common
.bs
;
78 BlockDriverState
*base
= s
->base
;
79 int64_t sector_num
, end
;
84 s
->common
.len
= bdrv_getlength(bs
);
85 if (s
->common
.len
< 0) {
86 block_job_complete(&s
->common
, s
->common
.len
);
90 end
= s
->common
.len
>> BDRV_SECTOR_BITS
;
91 buf
= qemu_blockalign(bs
, STREAM_BUFFER_SIZE
);
93 /* Turn on copy-on-read for the whole block device so that guest read
94 * requests help us make progress. Only do this when copying the entire
95 * backing chain since the copy-on-read operation does not take base into
99 bdrv_enable_copy_on_read(bs
);
102 for (sector_num
= 0; sector_num
< end
; sector_num
+= n
) {
103 uint64_t delay_ns
= 0;
107 /* Note that even when no rate limit is applied we need to yield
108 * with no pending I/O here so that qemu_aio_flush() returns.
110 block_job_sleep_ns(&s
->common
, rt_clock
, delay_ns
);
111 if (block_job_is_cancelled(&s
->common
)) {
115 ret
= bdrv_co_is_allocated(bs
, sector_num
,
116 STREAM_BUFFER_SIZE
/ BDRV_SECTOR_SIZE
, &n
);
118 /* Allocated in the top, no need to copy. */
121 /* Copy if allocated in the intermediate images. Limit to the
122 * known-unallocated area [sector_num, sector_num+n). */
123 ret
= bdrv_co_is_allocated_above(bs
->backing_hd
, base
,
126 /* Finish early if end of backing file has been reached */
127 if (ret
== 0 && n
== 0) {
128 n
= end
- sector_num
;
133 trace_stream_one_iteration(s
, sector_num
, n
, ret
);
134 if (ret
>= 0 && copy
) {
135 if (s
->common
.speed
) {
136 delay_ns
= ratelimit_calculate_delay(&s
->limit
, n
);
141 ret
= stream_populate(bs
, sector_num
, n
, buf
);
148 /* Publish progress */
149 s
->common
.offset
+= n
* BDRV_SECTOR_SIZE
;
153 bdrv_disable_copy_on_read(bs
);
156 if (!block_job_is_cancelled(&s
->common
) && sector_num
== end
&& ret
== 0) {
157 const char *base_id
= NULL
, *base_fmt
= NULL
;
159 base_id
= s
->backing_file_id
;
161 base_fmt
= base
->drv
->format_name
;
164 ret
= bdrv_change_backing_file(bs
, base_id
, base_fmt
);
165 close_unused_images(bs
, base
, base_id
);
169 block_job_complete(&s
->common
, ret
);
172 static void stream_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
174 StreamBlockJob
*s
= container_of(job
, StreamBlockJob
, common
);
177 error_set(errp
, QERR_INVALID_PARAMETER
, "speed");
180 ratelimit_set_speed(&s
->limit
, speed
/ BDRV_SECTOR_SIZE
, SLICE_TIME
);
183 static BlockJobType stream_job_type
= {
184 .instance_size
= sizeof(StreamBlockJob
),
185 .job_type
= "stream",
186 .set_speed
= stream_set_speed
,
189 void stream_start(BlockDriverState
*bs
, BlockDriverState
*base
,
190 const char *base_id
, int64_t speed
,
191 BlockDriverCompletionFunc
*cb
,
192 void *opaque
, Error
**errp
)
196 s
= block_job_create(&stream_job_type
, bs
, speed
, cb
, opaque
, errp
);
203 pstrcpy(s
->backing_file_id
, sizeof(s
->backing_file_id
), base_id
);
206 s
->common
.co
= qemu_coroutine_create(stream_run
);
207 trace_stream_start(bs
, base
, s
, s
->common
.co
, opaque
);
208 qemu_coroutine_enter(s
->common
.co
, s
);