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