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