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