]> git.proxmox.com Git - mirror_qemu.git/blame - block/stream.c
qemu-img: Update documentation for compressed images
[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
80c71a24 14#include "qemu/osdep.h"
4f1043b4 15#include "trace.h"
737e150e 16#include "block/block_int.h"
c87621ea 17#include "block/blockjob_int.h"
da34e65c 18#include "qapi/error.h"
205736f4 19#include "qapi/qmp/qdict.h"
6ef228fc 20#include "qemu/ratelimit.h"
373340b2 21#include "sysemu/block-backend.h"
205736f4 22#include "block/copy-on-read.h"
4f1043b4
SH
23
24enum {
25 /*
99136607
VSO
26 * Maximum chunk size to feed to copy-on-read. This should be
27 * large enough to process multiple clusters in a single call, so
28 * that populating contiguous regions of the image is efficient.
4f1043b4 29 */
99136607 30 STREAM_CHUNK = 512 * 1024, /* in bytes */
4f1043b4
SH
31};
32
33typedef struct StreamBlockJob {
34 BlockJob common;
048954e2 35 BlockBackend *blk;
67acfd21
HR
36 BlockDriverState *base_overlay; /* COW overlay (stream from this) */
37 BlockDriverState *above_base; /* Node directly above the base */
205736f4 38 BlockDriverState *cor_filter_bs;
0f6c9498 39 BlockDriverState *target_bs;
1d809098 40 BlockdevOnError on_error;
13d8cc51 41 char *backing_file_str;
e7d22f8b 42 bool bs_read_only;
4f1043b4
SH
43} StreamBlockJob;
44
03e35d82 45static int coroutine_fn stream_populate(BlockBackend *blk,
99136607 46 int64_t offset, uint64_t bytes)
4f1043b4 47{
8493211c 48 assert(bytes < SIZE_MAX);
4f1043b4 49
205736f4 50 return blk_co_preadv(blk, offset, bytes, NULL, BDRV_REQ_PREFETCH);
65854933
AG
51}
52
1b57488a 53static int stream_prepare(Job *job)
f3e69beb 54{
1908a559 55 StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
0f6c9498 56 BlockDriverState *unfiltered_bs = bdrv_skip_filters(s->target_bs);
8d3dd037
HR
57 BlockDriverState *base;
58 BlockDriverState *unfiltered_base;
12fa4af6 59 Error *local_err = NULL;
1b57488a 60 int ret = 0;
f3e69beb 61
205736f4
AS
62 /* We should drop filter at this point, as filter hold the backing chain */
63 bdrv_cor_filter_drop(s->cor_filter_bs);
64 s->cor_filter_bs = NULL;
65854933 65
92140b9f
KW
66 /*
67 * bdrv_set_backing_hd() requires that unfiltered_bs is drained. Drain
68 * already here and use bdrv_set_backing_hd_drained() instead because
69 * the polling during drained_begin() might change the graph, and if we do
70 * this only later, we may end up working with the wrong base node (or it
71 * might even have gone away by the time we want to use it).
72 */
73 bdrv_drained_begin(unfiltered_bs);
b1e1af39 74
8d3dd037
HR
75 base = bdrv_filter_or_cow_bs(s->above_base);
76 unfiltered_base = bdrv_skip_filters(base);
77
67acfd21 78 if (bdrv_cow_child(unfiltered_bs)) {
f3e69beb 79 const char *base_id = NULL, *base_fmt = NULL;
000e5a1c
AS
80 if (unfiltered_base) {
81 base_id = s->backing_file_str ?: unfiltered_base->filename;
82 if (unfiltered_base->drv) {
83 base_fmt = unfiltered_base->drv->format_name;
f3e69beb
SH
84 }
85 }
b1e1af39 86
92140b9f
KW
87 bdrv_set_backing_hd_drained(unfiltered_bs, base, &local_err);
88
89 /*
90 * This call will do I/O, so the graph can change again from here on.
91 * We have already completed the graph change, so we are not in danger
92 * of operating on the wrong node any more if this happens.
93 */
67acfd21 94 ret = bdrv_change_backing_file(unfiltered_bs, base_id, base_fmt, false);
12fa4af6
KW
95 if (local_err) {
96 error_report_err(local_err);
b1e1af39
HR
97 ret = -EPERM;
98 goto out;
12fa4af6 99 }
f3e69beb
SH
100 }
101
b1e1af39 102out:
92140b9f 103 bdrv_drained_end(unfiltered_bs);
1b57488a
JS
104 return ret;
105}
106
107static void stream_clean(Job *job)
108{
109 StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
1b57488a 110
205736f4
AS
111 if (s->cor_filter_bs) {
112 bdrv_cor_filter_drop(s->cor_filter_bs);
113 s->cor_filter_bs = NULL;
114 }
115
048954e2
VSO
116 blk_unref(s->blk);
117 s->blk = NULL;
118
61b49e48 119 /* Reopen the image back in read-only mode if necessary */
e7d22f8b 120 if (s->bs_read_only) {
a170a91f 121 /* Give up write permissions before making it read-only */
0f6c9498 122 bdrv_reopen_set_read_only(s->target_bs, true, NULL);
61b49e48
AG
123 }
124
f3e69beb 125 g_free(s->backing_file_str);
f3e69beb
SH
126}
127
f67432a2 128static int coroutine_fn stream_run(Job *job, Error **errp)
4f1043b4 129{
f67432a2 130 StreamBlockJob *s = container_of(job, StreamBlockJob, common.job);
0f6c9498 131 BlockDriverState *unfiltered_bs = bdrv_skip_filters(s->target_bs);
05df8a6a 132 int64_t len;
d535435f 133 int64_t offset = 0;
1d809098 134 int error = 0;
51b0a488 135 int64_t n = 0; /* bytes */
4f1043b4 136
67acfd21 137 if (unfiltered_bs == s->base_overlay) {
c624b015 138 /* Nothing to stream */
96a07d5b 139 return 0;
f4a193e7
HR
140 }
141
8ab8140a
KW
142 WITH_GRAPH_RDLOCK_GUARD() {
143 len = bdrv_co_getlength(s->target_bs);
144 if (len < 0) {
145 return len;
146 }
4f1043b4 147 }
30a5c887 148 job_progress_set_remaining(&s->common.job, len);
4f1043b4 149
05df8a6a 150 for ( ; offset < len; offset += n) {
f9749f28 151 bool copy;
35c94535 152 int ret;
4513eafe 153
4513eafe 154 /* Note that even when no rate limit is applied we need to yield
c57b6656 155 * with no pending I/O here so that bdrv_drain_all() returns.
4513eafe 156 */
018e5987 157 block_job_ratelimit_sleep(&s->common);
daa7f2f9 158 if (job_is_cancelled(&s->common.job)) {
4f1043b4
SH
159 break;
160 }
161
c3e4f43a
SW
162 copy = false;
163
7ff9579e
KW
164 WITH_GRAPH_RDLOCK_GUARD() {
165 ret = bdrv_is_allocated(unfiltered_bs, offset, STREAM_CHUNK, &n);
166 if (ret == 1) {
167 /* Allocated in the top, no need to copy. */
168 } else if (ret >= 0) {
169 /*
170 * Copy if allocated in the intermediate images. Limit to the
171 * known-unallocated area [offset, offset+n*BDRV_SECTOR_SIZE).
172 */
173 ret = bdrv_is_allocated_above(bdrv_cow_bs(unfiltered_bs),
174 s->base_overlay, true,
175 offset, n, &n);
176 /* Finish early if end of backing file has been reached */
177 if (ret == 0 && n == 0) {
178 n = len - offset;
179 }
180
181 copy = (ret > 0);
571cd9dc 182 }
f9749f28 183 }
51b0a488 184 trace_stream_one_iteration(s, offset, n, ret);
c3e4f43a 185 if (copy) {
048954e2 186 ret = stream_populate(s->blk, offset, n);
4f1043b4
SH
187 }
188 if (ret < 0) {
1d809098 189 BlockErrorAction action =
81e254dc 190 block_job_error_action(&s->common, s->on_error, true, -ret);
a589569f 191 if (action == BLOCK_ERROR_ACTION_STOP) {
1d809098
PB
192 n = 0;
193 continue;
194 }
195 if (error == 0) {
196 error = ret;
197 }
a589569f 198 if (action == BLOCK_ERROR_ACTION_REPORT) {
1d809098
PB
199 break;
200 }
4f1043b4
SH
201 }
202
203 /* Publish progress */
30a5c887 204 job_progress_update(&s->common.job, n);
dee81d51 205 if (copy) {
018e5987 206 block_job_ratelimit_processed_bytes(&s->common, n);
f14a39cc 207 }
4f1043b4
SH
208 }
209
96a07d5b
AS
210 /* Do not remove the backing file if an error was there but ignored. */
211 return error;
4f1043b4
SH
212}
213
3fc4b10a 214static const BlockJobDriver stream_job_driver = {
33e9e9bd
KW
215 .job_driver = {
216 .instance_size = sizeof(StreamBlockJob),
252291ea 217 .job_type = JOB_TYPE_STREAM,
80fa2c75 218 .free = block_job_free,
f67432a2 219 .run = stream_run,
1b57488a
JS
220 .prepare = stream_prepare,
221 .clean = stream_clean,
b15de828 222 .user_resume = block_job_user_resume,
33e9e9bd 223 },
4f1043b4
SH
224};
225
2323322e
AG
226void stream_start(const char *job_id, BlockDriverState *bs,
227 BlockDriverState *base, const char *backing_file_str,
7f4a396d 228 BlockDriverState *bottom,
cf6320df 229 int creation_flags, int64_t speed,
880747a8
AS
230 BlockdevOnError on_error,
231 const char *filter_node_name,
232 Error **errp)
4f1043b4 233{
1bf26076 234 StreamBlockJob *s = NULL;
61b49e48 235 BlockDriverState *iter;
e7d22f8b 236 bool bs_read_only;
c624b015 237 int basic_flags = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED;
7f4a396d 238 BlockDriverState *base_overlay;
205736f4 239 BlockDriverState *cor_filter_bs = NULL;
67acfd21 240 BlockDriverState *above_base;
205736f4 241 QDict *opts;
1bf26076 242 int ret;
4f1043b4 243
b4ad82aa
EGE
244 GLOBAL_STATE_CODE();
245
7f4a396d
VSO
246 assert(!(base && bottom));
247 assert(!(backing_file_str && bottom));
248
249 if (bottom) {
250 /*
251 * New simple interface. The code is written in terms of old interface
252 * with @base parameter (still, it doesn't freeze link to base, so in
253 * this mean old code is correct for new interface). So, for now, just
254 * emulate base_overlay and above_base. Still, when old interface
255 * finally removed, we should refactor code to use only "bottom", but
256 * not "*base*" things.
257 */
258 assert(!bottom->drv->is_filter);
259 base_overlay = above_base = bottom;
260 } else {
261 base_overlay = bdrv_find_overlay(bs, base);
262 if (!base_overlay) {
263 error_setg(errp, "'%s' is not in the backing chain of '%s'",
264 base->node_name, bs->node_name);
265 return;
266 }
67acfd21 267
7f4a396d
VSO
268 /*
269 * Find the node directly above @base. @base_overlay is a COW overlay,
270 * so it must have a bdrv_cow_child(), but it is the immediate overlay
271 * of @base, so between the two there can only be filters.
272 */
273 above_base = base_overlay;
274 if (bdrv_cow_bs(above_base) != base) {
275 above_base = bdrv_cow_bs(above_base);
276 while (bdrv_filter_bs(above_base) != base) {
277 above_base = bdrv_filter_bs(above_base);
278 }
67acfd21
HR
279 }
280 }
281
61b49e48 282 /* Make sure that the image is opened in read-write mode */
e7d22f8b
AG
283 bs_read_only = bdrv_is_read_only(bs);
284 if (bs_read_only) {
205736f4
AS
285 int ret;
286 /* Hold the chain during reopen */
287 if (bdrv_freeze_backing_chain(bs, above_base, errp) < 0) {
288 return;
289 }
290
291 ret = bdrv_reopen_set_read_only(bs, false, errp);
292
293 /* failure, or cor-filter will hold the chain */
294 bdrv_unfreeze_backing_chain(bs, above_base);
295
296 if (ret < 0) {
297 return;
61b49e48
AG
298 }
299 }
300
205736f4
AS
301 opts = qdict_new();
302
303 qdict_put_str(opts, "driver", "copy-on-read");
304 qdict_put_str(opts, "file", bdrv_get_node_name(bs));
305 /* Pass the base_overlay node name as 'bottom' to COR driver */
306 qdict_put_str(opts, "bottom", base_overlay->node_name);
307 if (filter_node_name) {
308 qdict_put_str(opts, "node-name", filter_node_name);
309 }
310
311 cor_filter_bs = bdrv_insert_node(bs, opts, BDRV_O_RDWR, errp);
312 if (!cor_filter_bs) {
313 goto fail;
314 }
315
316 if (!filter_node_name) {
317 cor_filter_bs->implicit = true;
318 }
319
320 s = block_job_create(job_id, &stream_job_driver, NULL, cor_filter_bs,
048954e2 321 0, BLK_PERM_ALL,
cf6320df 322 speed, creation_flags, NULL, NULL, errp);
a170a91f
KW
323 if (!s) {
324 goto fail;
325 }
326
048954e2
VSO
327 s->blk = blk_new_with_bs(cor_filter_bs, BLK_PERM_CONSISTENT_READ,
328 basic_flags | BLK_PERM_WRITE, errp);
329 if (!s->blk) {
330 goto fail;
331 }
332 /*
333 * Disable request queuing in the BlockBackend to avoid deadlocks on drain:
334 * The job reports that it's busy until it reaches a pause point.
335 */
336 blk_set_disable_request_queuing(s->blk, true);
337 blk_set_allow_aio_context_change(s->blk, true);
338
205736f4
AS
339 /*
340 * Prevent concurrent jobs trying to modify the graph structure here, we
341 * already have our own plans. Also don't allow resize as the image size is
342 * queried only at the job start and then cached.
343 */
344 if (block_job_add_bdrv(&s->common, "active node", bs, 0,
1bf26076 345 basic_flags | BLK_PERM_WRITE, errp)) {
205736f4
AS
346 goto fail;
347 }
348
a170a91f
KW
349 /* Block all intermediate nodes between bs and base, because they will
350 * disappear from the chain after this operation. The streaming job reads
c624b015
AS
351 * every block only once, assuming that it doesn't change, so forbid writes
352 * and resizes. Reassign the base node pointer because the backing BS of the
353 * bottom node might change after the call to bdrv_reopen_set_read_only()
354 * due to parallel block jobs running.
67acfd21
HR
355 * above_base node might change after the call to
356 * bdrv_reopen_set_read_only() due to parallel block jobs running.
c624b015 357 */
67acfd21
HR
358 base = bdrv_filter_or_cow_bs(above_base);
359 for (iter = bdrv_filter_or_cow_bs(bs); iter != base;
360 iter = bdrv_filter_or_cow_bs(iter))
361 {
1bf26076
KW
362 ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
363 basic_flags, errp);
364 if (ret < 0) {
365 goto fail;
366 }
61b49e48
AG
367 }
368
67acfd21
HR
369 s->base_overlay = base_overlay;
370 s->above_base = above_base;
13d8cc51 371 s->backing_file_str = g_strdup(backing_file_str);
205736f4 372 s->cor_filter_bs = cor_filter_bs;
0f6c9498 373 s->target_bs = bs;
e7d22f8b 374 s->bs_read_only = bs_read_only;
4f1043b4 375
1d809098 376 s->on_error = on_error;
5ccac6f1 377 trace_stream_start(bs, base, s);
da01ff7f 378 job_start(&s->common.job);
a170a91f
KW
379 return;
380
381fail:
1bf26076
KW
382 if (s) {
383 job_early_fail(&s->common.job);
384 }
205736f4
AS
385 if (cor_filter_bs) {
386 bdrv_cor_filter_drop(cor_filter_bs);
387 }
e7d22f8b
AG
388 if (bs_read_only) {
389 bdrv_reopen_set_read_only(bs, true, NULL);
a170a91f 390 }
4f1043b4 391}