]>
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" |
6ef228fc | 20 | #include "qemu/ratelimit.h" |
373340b2 | 21 | #include "sysemu/block-backend.h" |
4f1043b4 SH |
22 | |
23 | enum { | |
24 | /* | |
99136607 VSO |
25 | * Maximum chunk size to feed to copy-on-read. This should be |
26 | * large enough to process multiple clusters in a single call, so | |
27 | * that populating contiguous regions of the image is efficient. | |
4f1043b4 | 28 | */ |
99136607 | 29 | STREAM_CHUNK = 512 * 1024, /* in bytes */ |
4f1043b4 SH |
30 | }; |
31 | ||
32 | typedef struct StreamBlockJob { | |
33 | BlockJob common; | |
67acfd21 HR |
34 | BlockDriverState *base_overlay; /* COW overlay (stream from this) */ |
35 | BlockDriverState *above_base; /* Node directly above the base */ | |
1d809098 | 36 | BlockdevOnError on_error; |
13d8cc51 | 37 | char *backing_file_str; |
e7d22f8b | 38 | bool bs_read_only; |
65854933 | 39 | bool chain_frozen; |
4f1043b4 SH |
40 | } StreamBlockJob; |
41 | ||
03e35d82 | 42 | static int coroutine_fn stream_populate(BlockBackend *blk, |
99136607 | 43 | int64_t offset, uint64_t bytes) |
4f1043b4 | 44 | { |
8493211c | 45 | assert(bytes < SIZE_MAX); |
4f1043b4 | 46 | |
99136607 VSO |
47 | return blk_co_preadv(blk, offset, bytes, NULL, |
48 | BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH); | |
4f1043b4 SH |
49 | } |
50 | ||
65854933 AG |
51 | static void stream_abort(Job *job) |
52 | { | |
53 | StreamBlockJob *s = container_of(job, StreamBlockJob, common.job); | |
54 | ||
55 | if (s->chain_frozen) { | |
56 | BlockJob *bjob = &s->common; | |
67acfd21 | 57 | bdrv_unfreeze_backing_chain(blk_bs(bjob->blk), s->above_base); |
65854933 AG |
58 | } |
59 | } | |
60 | ||
1b57488a | 61 | static int stream_prepare(Job *job) |
f3e69beb | 62 | { |
1908a559 KW |
63 | StreamBlockJob *s = container_of(job, StreamBlockJob, common.job); |
64 | BlockJob *bjob = &s->common; | |
1908a559 | 65 | BlockDriverState *bs = blk_bs(bjob->blk); |
67acfd21 HR |
66 | BlockDriverState *unfiltered_bs = bdrv_skip_filters(bs); |
67 | BlockDriverState *base = bdrv_filter_or_cow_bs(s->above_base); | |
12fa4af6 | 68 | Error *local_err = NULL; |
1b57488a | 69 | int ret = 0; |
f3e69beb | 70 | |
67acfd21 | 71 | bdrv_unfreeze_backing_chain(bs, s->above_base); |
65854933 AG |
72 | s->chain_frozen = false; |
73 | ||
67acfd21 | 74 | if (bdrv_cow_child(unfiltered_bs)) { |
f3e69beb SH |
75 | const char *base_id = NULL, *base_fmt = NULL; |
76 | if (base) { | |
77 | base_id = s->backing_file_str; | |
78 | if (base->drv) { | |
79 | base_fmt = base->drv->format_name; | |
80 | } | |
81 | } | |
67acfd21 HR |
82 | bdrv_set_backing_hd(unfiltered_bs, base, &local_err); |
83 | ret = bdrv_change_backing_file(unfiltered_bs, base_id, base_fmt, false); | |
12fa4af6 KW |
84 | if (local_err) { |
85 | error_report_err(local_err); | |
1b57488a | 86 | return -EPERM; |
12fa4af6 | 87 | } |
f3e69beb SH |
88 | } |
89 | ||
1b57488a JS |
90 | return ret; |
91 | } | |
92 | ||
93 | static void stream_clean(Job *job) | |
94 | { | |
95 | StreamBlockJob *s = container_of(job, StreamBlockJob, common.job); | |
96 | BlockJob *bjob = &s->common; | |
97 | BlockDriverState *bs = blk_bs(bjob->blk); | |
98 | ||
61b49e48 | 99 | /* Reopen the image back in read-only mode if necessary */ |
e7d22f8b | 100 | if (s->bs_read_only) { |
a170a91f | 101 | /* Give up write permissions before making it read-only */ |
1908a559 | 102 | blk_set_perm(bjob->blk, 0, BLK_PERM_ALL, &error_abort); |
e7d22f8b | 103 | bdrv_reopen_set_read_only(bs, true, NULL); |
61b49e48 AG |
104 | } |
105 | ||
f3e69beb | 106 | g_free(s->backing_file_str); |
f3e69beb SH |
107 | } |
108 | ||
f67432a2 | 109 | static int coroutine_fn stream_run(Job *job, Error **errp) |
4f1043b4 | 110 | { |
f67432a2 | 111 | StreamBlockJob *s = container_of(job, StreamBlockJob, common.job); |
03e35d82 KW |
112 | BlockBackend *blk = s->common.blk; |
113 | BlockDriverState *bs = blk_bs(blk); | |
67acfd21 HR |
114 | BlockDriverState *unfiltered_bs = bdrv_skip_filters(bs); |
115 | bool enable_cor = !bdrv_cow_child(s->base_overlay); | |
05df8a6a | 116 | int64_t len; |
d535435f | 117 | int64_t offset = 0; |
f14a39cc | 118 | uint64_t delay_ns = 0; |
1d809098 | 119 | int error = 0; |
51b0a488 | 120 | int64_t n = 0; /* bytes */ |
4f1043b4 | 121 | |
67acfd21 | 122 | if (unfiltered_bs == s->base_overlay) { |
c624b015 | 123 | /* Nothing to stream */ |
96a07d5b | 124 | return 0; |
f4a193e7 HR |
125 | } |
126 | ||
05df8a6a KW |
127 | len = bdrv_getlength(bs); |
128 | if (len < 0) { | |
96a07d5b | 129 | return len; |
4f1043b4 | 130 | } |
30a5c887 | 131 | job_progress_set_remaining(&s->common.job, len); |
4f1043b4 | 132 | |
4f1043b4 SH |
133 | /* Turn on copy-on-read for the whole block device so that guest read |
134 | * requests help us make progress. Only do this when copying the entire | |
135 | * backing chain since the copy-on-read operation does not take base into | |
136 | * account. | |
137 | */ | |
c624b015 | 138 | if (enable_cor) { |
4f1043b4 SH |
139 | bdrv_enable_copy_on_read(bs); |
140 | } | |
141 | ||
05df8a6a | 142 | for ( ; offset < len; offset += n) { |
f9749f28 | 143 | bool copy; |
35c94535 | 144 | int ret; |
4513eafe | 145 | |
4513eafe | 146 | /* Note that even when no rate limit is applied we need to yield |
c57b6656 | 147 | * with no pending I/O here so that bdrv_drain_all() returns. |
4513eafe | 148 | */ |
5d43e86e | 149 | job_sleep_ns(&s->common.job, delay_ns); |
daa7f2f9 | 150 | if (job_is_cancelled(&s->common.job)) { |
4f1043b4 SH |
151 | break; |
152 | } | |
153 | ||
c3e4f43a SW |
154 | copy = false; |
155 | ||
67acfd21 | 156 | ret = bdrv_is_allocated(unfiltered_bs, offset, STREAM_CHUNK, &n); |
f9749f28 PB |
157 | if (ret == 1) { |
158 | /* Allocated in the top, no need to copy. */ | |
d663640c | 159 | } else if (ret >= 0) { |
f9749f28 | 160 | /* Copy if allocated in the intermediate images. Limit to the |
d535435f | 161 | * known-unallocated area [offset, offset+n*BDRV_SECTOR_SIZE). */ |
67acfd21 HR |
162 | ret = bdrv_is_allocated_above(bdrv_cow_bs(unfiltered_bs), |
163 | s->base_overlay, true, | |
51b0a488 | 164 | offset, n, &n); |
571cd9dc SH |
165 | /* Finish early if end of backing file has been reached */ |
166 | if (ret == 0 && n == 0) { | |
05df8a6a | 167 | n = len - offset; |
571cd9dc SH |
168 | } |
169 | ||
a92b1b06 | 170 | copy = (ret > 0); |
f9749f28 | 171 | } |
51b0a488 | 172 | trace_stream_one_iteration(s, offset, n, ret); |
c3e4f43a | 173 | if (copy) { |
99136607 | 174 | ret = stream_populate(blk, offset, n); |
4f1043b4 SH |
175 | } |
176 | if (ret < 0) { | |
1d809098 | 177 | BlockErrorAction action = |
81e254dc | 178 | block_job_error_action(&s->common, s->on_error, true, -ret); |
a589569f | 179 | if (action == BLOCK_ERROR_ACTION_STOP) { |
1d809098 PB |
180 | n = 0; |
181 | continue; | |
182 | } | |
183 | if (error == 0) { | |
184 | error = ret; | |
185 | } | |
a589569f | 186 | if (action == BLOCK_ERROR_ACTION_REPORT) { |
1d809098 PB |
187 | break; |
188 | } | |
4f1043b4 SH |
189 | } |
190 | ||
191 | /* Publish progress */ | |
30a5c887 | 192 | job_progress_update(&s->common.job, n); |
dee81d51 KW |
193 | if (copy) { |
194 | delay_ns = block_job_ratelimit_get_delay(&s->common, n); | |
2fe4bba1 KW |
195 | } else { |
196 | delay_ns = 0; | |
f14a39cc | 197 | } |
4f1043b4 SH |
198 | } |
199 | ||
c624b015 | 200 | if (enable_cor) { |
4f1043b4 SH |
201 | bdrv_disable_copy_on_read(bs); |
202 | } | |
203 | ||
96a07d5b AS |
204 | /* Do not remove the backing file if an error was there but ignored. */ |
205 | return error; | |
4f1043b4 SH |
206 | } |
207 | ||
3fc4b10a | 208 | static const BlockJobDriver stream_job_driver = { |
33e9e9bd KW |
209 | .job_driver = { |
210 | .instance_size = sizeof(StreamBlockJob), | |
252291ea | 211 | .job_type = JOB_TYPE_STREAM, |
80fa2c75 | 212 | .free = block_job_free, |
f67432a2 | 213 | .run = stream_run, |
1b57488a | 214 | .prepare = stream_prepare, |
65854933 | 215 | .abort = stream_abort, |
1b57488a | 216 | .clean = stream_clean, |
b15de828 | 217 | .user_resume = block_job_user_resume, |
33e9e9bd | 218 | }, |
4f1043b4 SH |
219 | }; |
220 | ||
2323322e AG |
221 | void stream_start(const char *job_id, BlockDriverState *bs, |
222 | BlockDriverState *base, const char *backing_file_str, | |
cf6320df | 223 | int creation_flags, int64_t speed, |
880747a8 AS |
224 | BlockdevOnError on_error, |
225 | const char *filter_node_name, | |
226 | Error **errp) | |
4f1043b4 SH |
227 | { |
228 | StreamBlockJob *s; | |
61b49e48 | 229 | BlockDriverState *iter; |
e7d22f8b | 230 | bool bs_read_only; |
c624b015 | 231 | int basic_flags = BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED; |
67acfd21 HR |
232 | BlockDriverState *base_overlay = bdrv_find_overlay(bs, base); |
233 | BlockDriverState *above_base; | |
4f1043b4 | 234 | |
67acfd21 HR |
235 | if (!base_overlay) { |
236 | error_setg(errp, "'%s' is not in the backing chain of '%s'", | |
237 | base->node_name, bs->node_name); | |
238 | return; | |
239 | } | |
240 | ||
241 | /* | |
242 | * Find the node directly above @base. @base_overlay is a COW overlay, so | |
243 | * it must have a bdrv_cow_child(), but it is the immediate overlay of | |
244 | * @base, so between the two there can only be filters. | |
245 | */ | |
246 | above_base = base_overlay; | |
247 | if (bdrv_cow_bs(above_base) != base) { | |
248 | above_base = bdrv_cow_bs(above_base); | |
249 | while (bdrv_filter_bs(above_base) != base) { | |
250 | above_base = bdrv_filter_bs(above_base); | |
251 | } | |
252 | } | |
253 | ||
254 | if (bdrv_freeze_backing_chain(bs, above_base, errp) < 0) { | |
20509c4b AG |
255 | return; |
256 | } | |
257 | ||
61b49e48 | 258 | /* Make sure that the image is opened in read-write mode */ |
e7d22f8b AG |
259 | bs_read_only = bdrv_is_read_only(bs); |
260 | if (bs_read_only) { | |
261 | if (bdrv_reopen_set_read_only(bs, false, errp) != 0) { | |
20509c4b AG |
262 | bs_read_only = false; |
263 | goto fail; | |
61b49e48 AG |
264 | } |
265 | } | |
266 | ||
a170a91f KW |
267 | /* Prevent concurrent jobs trying to modify the graph structure here, we |
268 | * already have our own plans. Also don't allow resize as the image size is | |
269 | * queried only at the job start and then cached. */ | |
75859b94 | 270 | s = block_job_create(job_id, &stream_job_driver, NULL, bs, |
c624b015 AS |
271 | basic_flags | BLK_PERM_GRAPH_MOD, |
272 | basic_flags | BLK_PERM_WRITE, | |
cf6320df | 273 | speed, creation_flags, NULL, NULL, errp); |
a170a91f KW |
274 | if (!s) { |
275 | goto fail; | |
276 | } | |
277 | ||
278 | /* Block all intermediate nodes between bs and base, because they will | |
279 | * disappear from the chain after this operation. The streaming job reads | |
c624b015 AS |
280 | * every block only once, assuming that it doesn't change, so forbid writes |
281 | * and resizes. Reassign the base node pointer because the backing BS of the | |
282 | * bottom node might change after the call to bdrv_reopen_set_read_only() | |
283 | * due to parallel block jobs running. | |
67acfd21 HR |
284 | * above_base node might change after the call to |
285 | * bdrv_reopen_set_read_only() due to parallel block jobs running. | |
c624b015 | 286 | */ |
67acfd21 HR |
287 | base = bdrv_filter_or_cow_bs(above_base); |
288 | for (iter = bdrv_filter_or_cow_bs(bs); iter != base; | |
289 | iter = bdrv_filter_or_cow_bs(iter)) | |
290 | { | |
76d554e2 | 291 | block_job_add_bdrv(&s->common, "intermediate node", iter, 0, |
c624b015 | 292 | basic_flags, &error_abort); |
61b49e48 AG |
293 | } |
294 | ||
67acfd21 HR |
295 | s->base_overlay = base_overlay; |
296 | s->above_base = above_base; | |
13d8cc51 | 297 | s->backing_file_str = g_strdup(backing_file_str); |
e7d22f8b | 298 | s->bs_read_only = bs_read_only; |
65854933 | 299 | s->chain_frozen = true; |
4f1043b4 | 300 | |
1d809098 | 301 | s->on_error = on_error; |
5ccac6f1 | 302 | trace_stream_start(bs, base, s); |
da01ff7f | 303 | job_start(&s->common.job); |
a170a91f KW |
304 | return; |
305 | ||
306 | fail: | |
e7d22f8b AG |
307 | if (bs_read_only) { |
308 | bdrv_reopen_set_read_only(bs, true, NULL); | |
a170a91f | 309 | } |
67acfd21 | 310 | bdrv_unfreeze_backing_chain(bs, above_base); |
4f1043b4 | 311 | } |