]> git.proxmox.com Git - qemu.git/blame - block/stream.c
block: make bdrv_co_is_allocated static
[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"
737e150e
PB
15#include "block/block_int.h"
16#include "block/blockjob.h"
6ef228fc 17#include "qemu/ratelimit.h"
4f1043b4
SH
18
19enum {
20 /*
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.
24 */
25 STREAM_BUFFER_SIZE = 512 * 1024, /* in bytes */
26};
27
5094a6c0
SH
28#define SLICE_TIME 100000000ULL /* ns */
29
4f1043b4
SH
30typedef struct StreamBlockJob {
31 BlockJob common;
5094a6c0 32 RateLimit limit;
4f1043b4 33 BlockDriverState *base;
1d809098 34 BlockdevOnError on_error;
c8c3080f 35 char backing_file_id[1024];
4f1043b4
SH
36} StreamBlockJob;
37
38static int coroutine_fn stream_populate(BlockDriverState *bs,
39 int64_t sector_num, int nb_sectors,
40 void *buf)
41{
42 struct iovec iov = {
43 .iov_base = buf,
44 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
45 };
46 QEMUIOVector qiov;
47
48 qemu_iovec_init_external(&qiov, &iov, 1);
49
50 /* Copy-on-read the unallocated clusters */
51 return bdrv_co_copy_on_readv(bs, sector_num, nb_sectors, &qiov);
52}
53
5a67a104
MT
54static void close_unused_images(BlockDriverState *top, BlockDriverState *base,
55 const char *base_id)
56{
57 BlockDriverState *intermediate;
58 intermediate = top->backing_hd;
59
88266f5a
SH
60 /* Must assign before bdrv_delete() to prevent traversing dangling pointer
61 * while we delete backing image instances.
62 */
63 top->backing_hd = base;
64
5a67a104
MT
65 while (intermediate) {
66 BlockDriverState *unused;
67
68 /* reached base */
69 if (intermediate == base) {
70 break;
71 }
72
73 unused = intermediate;
74 intermediate = intermediate->backing_hd;
75 unused->backing_hd = NULL;
4f6fd349 76 bdrv_unref(unused);
5a67a104 77 }
5a67a104
MT
78}
79
4f1043b4
SH
80static void coroutine_fn stream_run(void *opaque)
81{
82 StreamBlockJob *s = opaque;
83 BlockDriverState *bs = s->common.bs;
c8c3080f 84 BlockDriverState *base = s->base;
4f1043b4 85 int64_t sector_num, end;
1d809098 86 int error = 0;
4f1043b4 87 int ret = 0;
04120e3b 88 int n = 0;
4f1043b4
SH
89 void *buf;
90
91 s->common.len = bdrv_getlength(bs);
92 if (s->common.len < 0) {
65f46322 93 block_job_completed(&s->common, s->common.len);
4f1043b4
SH
94 return;
95 }
96
97 end = s->common.len >> BDRV_SECTOR_BITS;
98 buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE);
99
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
103 * account.
104 */
105 if (!base) {
106 bdrv_enable_copy_on_read(bs);
107 }
108
109 for (sector_num = 0; sector_num < end; sector_num += n) {
4513eafe 110 uint64_t delay_ns = 0;
f9749f28 111 bool copy;
4513eafe
PB
112
113wait:
114 /* Note that even when no rate limit is applied we need to yield
c57b6656 115 * with no pending I/O here so that bdrv_drain_all() returns.
4513eafe 116 */
7483d1e5 117 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
4f1043b4
SH
118 if (block_job_is_cancelled(&s->common)) {
119 break;
120 }
121
bdad13b9
PB
122 ret = bdrv_is_allocated(bs, sector_num,
123 STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
f9749f28
PB
124 if (ret == 1) {
125 /* Allocated in the top, no need to copy. */
126 copy = false;
127 } else {
128 /* Copy if allocated in the intermediate images. Limit to the
129 * known-unallocated area [sector_num, sector_num+n). */
188a7bbf
PB
130 ret = bdrv_co_is_allocated_above(bs->backing_hd, base,
131 sector_num, n, &n);
571cd9dc
SH
132
133 /* Finish early if end of backing file has been reached */
134 if (ret == 0 && n == 0) {
135 n = end - sector_num;
136 }
137
f9749f28
PB
138 copy = (ret == 1);
139 }
4f1043b4 140 trace_stream_one_iteration(s, sector_num, n, ret);
f9749f28 141 if (ret >= 0 && copy) {
5094a6c0 142 if (s->common.speed) {
4513eafe 143 delay_ns = ratelimit_calculate_delay(&s->limit, n);
5094a6c0 144 if (delay_ns > 0) {
4513eafe 145 goto wait;
5094a6c0
SH
146 }
147 }
4f1043b4
SH
148 ret = stream_populate(bs, sector_num, n, buf);
149 }
150 if (ret < 0) {
1d809098
PB
151 BlockErrorAction action =
152 block_job_error_action(&s->common, s->common.bs, s->on_error,
153 true, -ret);
154 if (action == BDRV_ACTION_STOP) {
155 n = 0;
156 continue;
157 }
158 if (error == 0) {
159 error = ret;
160 }
161 if (action == BDRV_ACTION_REPORT) {
162 break;
163 }
4f1043b4 164 }
c8c3080f 165 ret = 0;
4f1043b4
SH
166
167 /* Publish progress */
168 s->common.offset += n * BDRV_SECTOR_SIZE;
169 }
170
171 if (!base) {
172 bdrv_disable_copy_on_read(bs);
173 }
174
1d809098
PB
175 /* Do not remove the backing file if an error was there but ignored. */
176 ret = error;
177
3e914655 178 if (!block_job_is_cancelled(&s->common) && sector_num == end && ret == 0) {
f6133def 179 const char *base_id = NULL, *base_fmt = NULL;
c8c3080f
MT
180 if (base) {
181 base_id = s->backing_file_id;
f6133def
PB
182 if (base->drv) {
183 base_fmt = base->drv->format_name;
184 }
c8c3080f 185 }
f6133def 186 ret = bdrv_change_backing_file(bs, base_id, base_fmt);
5a67a104 187 close_unused_images(bs, base, base_id);
4f1043b4
SH
188 }
189
190 qemu_vfree(buf);
65f46322 191 block_job_completed(&s->common, ret);
4f1043b4
SH
192}
193
882ec7ce 194static void stream_set_speed(BlockJob *job, int64_t speed, Error **errp)
5094a6c0
SH
195{
196 StreamBlockJob *s = container_of(job, StreamBlockJob, common);
197
882ec7ce
SH
198 if (speed < 0) {
199 error_set(errp, QERR_INVALID_PARAMETER, "speed");
9e6636c7 200 return;
5094a6c0 201 }
6ef228fc 202 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
5094a6c0
SH
203}
204
f59fee8d 205static const BlockJobType stream_job_type = {
4f1043b4
SH
206 .instance_size = sizeof(StreamBlockJob),
207 .job_type = "stream",
5094a6c0 208 .set_speed = stream_set_speed,
4f1043b4
SH
209};
210
fd7f8c65 211void stream_start(BlockDriverState *bs, BlockDriverState *base,
c83c66c3 212 const char *base_id, int64_t speed,
1d809098 213 BlockdevOnError on_error,
c83c66c3 214 BlockDriverCompletionFunc *cb,
fd7f8c65 215 void *opaque, Error **errp)
4f1043b4
SH
216{
217 StreamBlockJob *s;
4f1043b4 218
1d809098
PB
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");
223 return;
224 }
225
c83c66c3 226 s = block_job_create(&stream_job_type, bs, speed, cb, opaque, errp);
4f1043b4 227 if (!s) {
fd7f8c65 228 return;
4f1043b4
SH
229 }
230
231 s->base = base;
c8c3080f
MT
232 if (base_id) {
233 pstrcpy(s->backing_file_id, sizeof(s->backing_file_id), base_id);
234 }
4f1043b4 235
1d809098 236 s->on_error = on_error;
fa4478d5
PB
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);
4f1043b4 240}