]> git.proxmox.com Git - mirror_qemu.git/blame - block/stream.c
qmp: Add command 'blockdev-backup'
[mirror_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;
13d8cc51 35 char *backing_file_str;
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 */
920beae1 63 bdrv_set_backing_hd(top, base);
88266f5a 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;
920beae1 75 bdrv_set_backing_hd(unused, NULL);
4f6fd349 76 bdrv_unref(unused);
5a67a104 77 }
355ef4ac 78
3baca891 79 bdrv_refresh_limits(top, NULL);
5a67a104
MT
80}
81
f3e69beb
SH
82typedef struct {
83 int ret;
84 bool reached_end;
85} StreamCompleteData;
86
87static void stream_complete(BlockJob *job, void *opaque)
88{
89 StreamBlockJob *s = container_of(job, StreamBlockJob, common);
90 StreamCompleteData *data = opaque;
91 BlockDriverState *base = s->base;
92
93 if (!block_job_is_cancelled(&s->common) && data->reached_end &&
94 data->ret == 0) {
95 const char *base_id = NULL, *base_fmt = NULL;
96 if (base) {
97 base_id = s->backing_file_str;
98 if (base->drv) {
99 base_fmt = base->drv->format_name;
100 }
101 }
102 data->ret = bdrv_change_backing_file(job->bs, base_id, base_fmt);
103 close_unused_images(job->bs, base, base_id);
104 }
105
106 g_free(s->backing_file_str);
107 block_job_completed(&s->common, data->ret);
108 g_free(data);
109}
110
4f1043b4
SH
111static void coroutine_fn stream_run(void *opaque)
112{
113 StreamBlockJob *s = opaque;
f3e69beb 114 StreamCompleteData *data;
4f1043b4 115 BlockDriverState *bs = s->common.bs;
c8c3080f 116 BlockDriverState *base = s->base;
4f1043b4 117 int64_t sector_num, end;
1d809098 118 int error = 0;
4f1043b4 119 int ret = 0;
04120e3b 120 int n = 0;
4f1043b4
SH
121 void *buf;
122
f4a193e7
HR
123 if (!bs->backing_hd) {
124 block_job_completed(&s->common, 0);
125 return;
126 }
127
4f1043b4
SH
128 s->common.len = bdrv_getlength(bs);
129 if (s->common.len < 0) {
65f46322 130 block_job_completed(&s->common, s->common.len);
4f1043b4
SH
131 return;
132 }
133
134 end = s->common.len >> BDRV_SECTOR_BITS;
135 buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE);
136
137 /* Turn on copy-on-read for the whole block device so that guest read
138 * requests help us make progress. Only do this when copying the entire
139 * backing chain since the copy-on-read operation does not take base into
140 * account.
141 */
142 if (!base) {
143 bdrv_enable_copy_on_read(bs);
144 }
145
146 for (sector_num = 0; sector_num < end; sector_num += n) {
4513eafe 147 uint64_t delay_ns = 0;
f9749f28 148 bool copy;
4513eafe
PB
149
150wait:
151 /* Note that even when no rate limit is applied we need to yield
c57b6656 152 * with no pending I/O here so that bdrv_drain_all() returns.
4513eafe 153 */
7483d1e5 154 block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
4f1043b4
SH
155 if (block_job_is_cancelled(&s->common)) {
156 break;
157 }
158
c3e4f43a
SW
159 copy = false;
160
bdad13b9
PB
161 ret = bdrv_is_allocated(bs, sector_num,
162 STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
f9749f28
PB
163 if (ret == 1) {
164 /* Allocated in the top, no need to copy. */
d663640c 165 } else if (ret >= 0) {
f9749f28
PB
166 /* Copy if allocated in the intermediate images. Limit to the
167 * known-unallocated area [sector_num, sector_num+n). */
4f578637
PB
168 ret = bdrv_is_allocated_above(bs->backing_hd, base,
169 sector_num, n, &n);
571cd9dc
SH
170
171 /* Finish early if end of backing file has been reached */
172 if (ret == 0 && n == 0) {
173 n = end - sector_num;
174 }
175
f9749f28
PB
176 copy = (ret == 1);
177 }
4f1043b4 178 trace_stream_one_iteration(s, sector_num, n, ret);
c3e4f43a 179 if (copy) {
5094a6c0 180 if (s->common.speed) {
4513eafe 181 delay_ns = ratelimit_calculate_delay(&s->limit, n);
5094a6c0 182 if (delay_ns > 0) {
4513eafe 183 goto wait;
5094a6c0
SH
184 }
185 }
4f1043b4
SH
186 ret = stream_populate(bs, sector_num, n, buf);
187 }
188 if (ret < 0) {
1d809098
PB
189 BlockErrorAction action =
190 block_job_error_action(&s->common, s->common.bs, s->on_error,
191 true, -ret);
a589569f 192 if (action == BLOCK_ERROR_ACTION_STOP) {
1d809098
PB
193 n = 0;
194 continue;
195 }
196 if (error == 0) {
197 error = ret;
198 }
a589569f 199 if (action == BLOCK_ERROR_ACTION_REPORT) {
1d809098
PB
200 break;
201 }
4f1043b4 202 }
c8c3080f 203 ret = 0;
4f1043b4
SH
204
205 /* Publish progress */
206 s->common.offset += n * BDRV_SECTOR_SIZE;
207 }
208
209 if (!base) {
210 bdrv_disable_copy_on_read(bs);
211 }
212
1d809098
PB
213 /* Do not remove the backing file if an error was there but ignored. */
214 ret = error;
215
4f1043b4 216 qemu_vfree(buf);
f3e69beb
SH
217
218 /* Modify backing chain and close BDSes in main loop */
219 data = g_malloc(sizeof(*data));
220 data->ret = ret;
221 data->reached_end = sector_num == end;
222 block_job_defer_to_main_loop(&s->common, stream_complete, data);
4f1043b4
SH
223}
224
882ec7ce 225static void stream_set_speed(BlockJob *job, int64_t speed, Error **errp)
5094a6c0
SH
226{
227 StreamBlockJob *s = container_of(job, StreamBlockJob, common);
228
882ec7ce
SH
229 if (speed < 0) {
230 error_set(errp, QERR_INVALID_PARAMETER, "speed");
9e6636c7 231 return;
5094a6c0 232 }
6ef228fc 233 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
5094a6c0
SH
234}
235
3fc4b10a 236static const BlockJobDriver stream_job_driver = {
4f1043b4 237 .instance_size = sizeof(StreamBlockJob),
79e14bf7 238 .job_type = BLOCK_JOB_TYPE_STREAM,
5094a6c0 239 .set_speed = stream_set_speed,
4f1043b4
SH
240};
241
fd7f8c65 242void stream_start(BlockDriverState *bs, BlockDriverState *base,
13d8cc51 243 const char *backing_file_str, int64_t speed,
1d809098 244 BlockdevOnError on_error,
097310b5 245 BlockCompletionFunc *cb,
fd7f8c65 246 void *opaque, Error **errp)
4f1043b4
SH
247{
248 StreamBlockJob *s;
4f1043b4 249
1d809098
PB
250 if ((on_error == BLOCKDEV_ON_ERROR_STOP ||
251 on_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
252 !bdrv_iostatus_is_enabled(bs)) {
253 error_set(errp, QERR_INVALID_PARAMETER, "on-error");
254 return;
255 }
256
3fc4b10a 257 s = block_job_create(&stream_job_driver, bs, speed, cb, opaque, errp);
4f1043b4 258 if (!s) {
fd7f8c65 259 return;
4f1043b4
SH
260 }
261
262 s->base = base;
13d8cc51 263 s->backing_file_str = g_strdup(backing_file_str);
4f1043b4 264
1d809098 265 s->on_error = on_error;
fa4478d5
PB
266 s->common.co = qemu_coroutine_create(stream_run);
267 trace_stream_start(bs, base, s, s->common.co, opaque);
268 qemu_coroutine_enter(s->common.co, s);
4f1043b4 269}