]> git.proxmox.com Git - qemu.git/blame - block/stream.c
stream: tweak usage of bdrv_co_is_allocated
[qemu.git] / block / stream.c
CommitLineData
4f1043b4
SH
1/*
2 * Image streaming
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 *
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.
11 *
12 */
13
14#include "trace.h"
15#include "block_int.h"
16
17enum {
18 /*
19 * Size of data buffer for populating the image file. This should be large
20 * enough to process multiple clusters in a single call, so that populating
21 * contiguous regions of the image is efficient.
22 */
23 STREAM_BUFFER_SIZE = 512 * 1024, /* in bytes */
24};
25
5094a6c0
SH
26#define SLICE_TIME 100000000ULL /* ns */
27
28typedef struct {
29 int64_t next_slice_time;
30 uint64_t slice_quota;
31 uint64_t dispatched;
32} RateLimit;
33
34static int64_t ratelimit_calculate_delay(RateLimit *limit, uint64_t n)
35{
5094a6c0
SH
36 int64_t now = qemu_get_clock_ns(rt_clock);
37
38 if (limit->next_slice_time < now) {
39 limit->next_slice_time = now + SLICE_TIME;
40 limit->dispatched = 0;
41 }
b21d677e 42 if (limit->dispatched == 0 || limit->dispatched + n <= limit->slice_quota) {
5094a6c0 43 limit->dispatched += n;
b21d677e
PB
44 return 0;
45 } else {
46 limit->dispatched = n;
47 return limit->next_slice_time - now;
5094a6c0 48 }
5094a6c0
SH
49}
50
51static void ratelimit_set_speed(RateLimit *limit, uint64_t speed)
52{
53 limit->slice_quota = speed / (1000000000ULL / SLICE_TIME);
54}
55
4f1043b4
SH
56typedef struct StreamBlockJob {
57 BlockJob common;
5094a6c0 58 RateLimit limit;
4f1043b4 59 BlockDriverState *base;
c8c3080f 60 char backing_file_id[1024];
4f1043b4
SH
61} StreamBlockJob;
62
63static int coroutine_fn stream_populate(BlockDriverState *bs,
64 int64_t sector_num, int nb_sectors,
65 void *buf)
66{
67 struct iovec iov = {
68 .iov_base = buf,
69 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
70 };
71 QEMUIOVector qiov;
72
73 qemu_iovec_init_external(&qiov, &iov, 1);
74
75 /* Copy-on-read the unallocated clusters */
76 return bdrv_co_copy_on_readv(bs, sector_num, nb_sectors, &qiov);
77}
78
5a67a104
MT
79static void close_unused_images(BlockDriverState *top, BlockDriverState *base,
80 const char *base_id)
81{
82 BlockDriverState *intermediate;
83 intermediate = top->backing_hd;
84
85 while (intermediate) {
86 BlockDriverState *unused;
87
88 /* reached base */
89 if (intermediate == base) {
90 break;
91 }
92
93 unused = intermediate;
94 intermediate = intermediate->backing_hd;
95 unused->backing_hd = NULL;
96 bdrv_delete(unused);
97 }
98 top->backing_hd = base;
5a67a104
MT
99}
100
c8c3080f
MT
101/*
102 * Given an image chain: [BASE] -> [INTER1] -> [INTER2] -> [TOP]
103 *
f9749f28
PB
104 * Return true if the given sector is allocated in any image between
105 * BASE and TOP (inclusive). BASE can be NULL to check if the given
106 * sector is allocated in any image of the chain. Return false otherwise.
c8c3080f
MT
107 *
108 * 'pnum' is set to the number of sectors (including and immediately following
109 * the specified sector) that are known to be in the same
110 * allocated/unallocated state.
111 *
112 */
f9749f28
PB
113static int coroutine_fn is_allocated_above(BlockDriverState *top,
114 BlockDriverState *base,
115 int64_t sector_num,
116 int nb_sectors, int *pnum)
c8c3080f
MT
117{
118 BlockDriverState *intermediate;
f9749f28 119 int ret, n = nb_sectors;
c8c3080f 120
f9749f28 121 intermediate = top;
efcc7a23 122 while (intermediate != base) {
c8c3080f 123 int pnum_inter;
c8c3080f
MT
124 ret = bdrv_co_is_allocated(intermediate, sector_num, nb_sectors,
125 &pnum_inter);
126 if (ret < 0) {
127 return ret;
128 } else if (ret) {
129 *pnum = pnum_inter;
f9749f28 130 return 1;
c8c3080f
MT
131 }
132
133 /*
134 * [sector_num, nb_sectors] is unallocated on top but intermediate
135 * might have
136 *
137 * [sector_num+x, nr_sectors] allocated.
138 */
139 if (n > pnum_inter) {
140 n = pnum_inter;
141 }
142
143 intermediate = intermediate->backing_hd;
144 }
145
efcc7a23 146 *pnum = n;
f9749f28 147 return 0;
c8c3080f
MT
148}
149
4f1043b4
SH
150static void coroutine_fn stream_run(void *opaque)
151{
152 StreamBlockJob *s = opaque;
153 BlockDriverState *bs = s->common.bs;
c8c3080f 154 BlockDriverState *base = s->base;
4f1043b4
SH
155 int64_t sector_num, end;
156 int ret = 0;
04120e3b 157 int n = 0;
4f1043b4
SH
158 void *buf;
159
160 s->common.len = bdrv_getlength(bs);
161 if (s->common.len < 0) {
162 block_job_complete(&s->common, s->common.len);
163 return;
164 }
165
166 end = s->common.len >> BDRV_SECTOR_BITS;
167 buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE);
168
169 /* Turn on copy-on-read for the whole block device so that guest read
170 * requests help us make progress. Only do this when copying the entire
171 * backing chain since the copy-on-read operation does not take base into
172 * account.
173 */
174 if (!base) {
175 bdrv_enable_copy_on_read(bs);
176 }
177
178 for (sector_num = 0; sector_num < end; sector_num += n) {
4513eafe 179 uint64_t delay_ns = 0;
f9749f28 180 bool copy;
4513eafe
PB
181
182wait:
183 /* Note that even when no rate limit is applied we need to yield
184 * with no pending I/O here so that qemu_aio_flush() returns.
185 */
186 block_job_sleep_ns(&s->common, rt_clock, delay_ns);
4f1043b4
SH
187 if (block_job_is_cancelled(&s->common)) {
188 break;
189 }
190
f9749f28
PB
191 ret = bdrv_co_is_allocated(bs, sector_num,
192 STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
193 if (ret == 1) {
194 /* Allocated in the top, no need to copy. */
195 copy = false;
196 } else {
197 /* Copy if allocated in the intermediate images. Limit to the
198 * known-unallocated area [sector_num, sector_num+n). */
199 ret = is_allocated_above(bs->backing_hd, base, sector_num, n, &n);
200 copy = (ret == 1);
201 }
202
4f1043b4 203 trace_stream_one_iteration(s, sector_num, n, ret);
f9749f28 204 if (ret >= 0 && copy) {
5094a6c0 205 if (s->common.speed) {
4513eafe 206 delay_ns = ratelimit_calculate_delay(&s->limit, n);
5094a6c0 207 if (delay_ns > 0) {
4513eafe 208 goto wait;
5094a6c0
SH
209 }
210 }
4f1043b4
SH
211 ret = stream_populate(bs, sector_num, n, buf);
212 }
213 if (ret < 0) {
214 break;
215 }
c8c3080f 216 ret = 0;
4f1043b4
SH
217
218 /* Publish progress */
219 s->common.offset += n * BDRV_SECTOR_SIZE;
220 }
221
222 if (!base) {
223 bdrv_disable_copy_on_read(bs);
224 }
225
3e914655 226 if (!block_job_is_cancelled(&s->common) && sector_num == end && ret == 0) {
f6133def 227 const char *base_id = NULL, *base_fmt = NULL;
c8c3080f
MT
228 if (base) {
229 base_id = s->backing_file_id;
f6133def
PB
230 if (base->drv) {
231 base_fmt = base->drv->format_name;
232 }
c8c3080f 233 }
f6133def 234 ret = bdrv_change_backing_file(bs, base_id, base_fmt);
5a67a104 235 close_unused_images(bs, base, base_id);
4f1043b4
SH
236 }
237
238 qemu_vfree(buf);
239 block_job_complete(&s->common, ret);
240}
241
882ec7ce 242static void stream_set_speed(BlockJob *job, int64_t speed, Error **errp)
5094a6c0
SH
243{
244 StreamBlockJob *s = container_of(job, StreamBlockJob, common);
245
882ec7ce
SH
246 if (speed < 0) {
247 error_set(errp, QERR_INVALID_PARAMETER, "speed");
9e6636c7 248 return;
5094a6c0 249 }
882ec7ce 250 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE);
5094a6c0
SH
251}
252
4f1043b4
SH
253static BlockJobType stream_job_type = {
254 .instance_size = sizeof(StreamBlockJob),
255 .job_type = "stream",
5094a6c0 256 .set_speed = stream_set_speed,
4f1043b4
SH
257};
258
fd7f8c65 259void stream_start(BlockDriverState *bs, BlockDriverState *base,
c83c66c3
SH
260 const char *base_id, int64_t speed,
261 BlockDriverCompletionFunc *cb,
fd7f8c65 262 void *opaque, Error **errp)
4f1043b4
SH
263{
264 StreamBlockJob *s;
4f1043b4 265
c83c66c3 266 s = block_job_create(&stream_job_type, bs, speed, cb, opaque, errp);
4f1043b4 267 if (!s) {
fd7f8c65 268 return;
4f1043b4
SH
269 }
270
271 s->base = base;
c8c3080f
MT
272 if (base_id) {
273 pstrcpy(s->backing_file_id, sizeof(s->backing_file_id), base_id);
274 }
4f1043b4 275
fa4478d5
PB
276 s->common.co = qemu_coroutine_create(stream_run);
277 trace_stream_start(bs, base, s, s->common.co, opaque);
278 qemu_coroutine_enter(s->common.co, s);
4f1043b4 279}