]>
Commit | Line | Data |
---|---|---|
747ff602 JC |
1 | /* |
2 | * Live block commit | |
3 | * | |
4 | * Copyright Red Hat, Inc. 2012 | |
5 | * | |
6 | * Authors: | |
7 | * Jeff Cody <jcody@redhat.com> | |
8 | * Based on stream.c by Stefan Hajnoczi | |
9 | * | |
10 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. | |
11 | * See the COPYING.LIB file in the top-level directory. | |
12 | * | |
13 | */ | |
14 | ||
80c71a24 | 15 | #include "qemu/osdep.h" |
dcbf37ce | 16 | #include "qemu/cutils.h" |
747ff602 | 17 | #include "trace.h" |
737e150e | 18 | #include "block/block_int.h" |
c87621ea | 19 | #include "block/blockjob_int.h" |
da34e65c | 20 | #include "qapi/error.h" |
cc7a8ea7 | 21 | #include "qapi/qmp/qerror.h" |
747ff602 | 22 | #include "qemu/ratelimit.h" |
373340b2 | 23 | #include "sysemu/block-backend.h" |
747ff602 JC |
24 | |
25 | enum { | |
26 | /* | |
27 | * Size of data buffer for populating the image file. This should be large | |
28 | * enough to process multiple clusters in a single call, so that populating | |
29 | * contiguous regions of the image is efficient. | |
30 | */ | |
31 | COMMIT_BUFFER_SIZE = 512 * 1024, /* in bytes */ | |
32 | }; | |
33 | ||
34 | #define SLICE_TIME 100000000ULL /* ns */ | |
35 | ||
36 | typedef struct CommitBlockJob { | |
37 | BlockJob common; | |
38 | RateLimit limit; | |
39 | BlockDriverState *active; | |
8dfba279 | 40 | BlockDriverState *commit_top_bs; |
4653456a KW |
41 | BlockBackend *top; |
42 | BlockBackend *base; | |
92aa5c6d | 43 | BlockdevOnError on_error; |
747ff602 JC |
44 | int base_flags; |
45 | int orig_overlay_flags; | |
54e26900 | 46 | char *backing_file_str; |
747ff602 JC |
47 | } CommitBlockJob; |
48 | ||
4653456a | 49 | static int coroutine_fn commit_populate(BlockBackend *bs, BlockBackend *base, |
d8a98584 | 50 | int64_t offset, uint64_t bytes, |
747ff602 JC |
51 | void *buf) |
52 | { | |
53 | int ret = 0; | |
4653456a KW |
54 | QEMUIOVector qiov; |
55 | struct iovec iov = { | |
56 | .iov_base = buf, | |
d8a98584 | 57 | .iov_len = bytes, |
4653456a | 58 | }; |
747ff602 | 59 | |
d8a98584 | 60 | assert(bytes < SIZE_MAX); |
4653456a KW |
61 | qemu_iovec_init_external(&qiov, &iov, 1); |
62 | ||
d8a98584 | 63 | ret = blk_co_preadv(bs, offset, qiov.size, &qiov, 0); |
4653456a | 64 | if (ret < 0) { |
747ff602 JC |
65 | return ret; |
66 | } | |
67 | ||
d8a98584 | 68 | ret = blk_co_pwritev(base, offset, qiov.size, &qiov, 0); |
4653456a | 69 | if (ret < 0) { |
747ff602 JC |
70 | return ret; |
71 | } | |
72 | ||
73 | return 0; | |
74 | } | |
75 | ||
9e85cd5c SH |
76 | typedef struct { |
77 | int ret; | |
78 | } CommitCompleteData; | |
79 | ||
80 | static void commit_complete(BlockJob *job, void *opaque) | |
747ff602 | 81 | { |
9e85cd5c SH |
82 | CommitBlockJob *s = container_of(job, CommitBlockJob, common); |
83 | CommitCompleteData *data = opaque; | |
747ff602 | 84 | BlockDriverState *active = s->active; |
4653456a KW |
85 | BlockDriverState *top = blk_bs(s->top); |
86 | BlockDriverState *base = blk_bs(s->base); | |
8dfba279 | 87 | BlockDriverState *overlay_bs = bdrv_find_overlay(active, s->commit_top_bs); |
9e85cd5c | 88 | int ret = data->ret; |
8dfba279 KW |
89 | bool remove_commit_top_bs = false; |
90 | ||
19ebd13e KW |
91 | /* Make sure overlay_bs and top stay around until bdrv_set_backing_hd() */ |
92 | bdrv_ref(top); | |
93 | bdrv_ref(overlay_bs); | |
94 | ||
8dfba279 KW |
95 | /* Remove base node parent that still uses BLK_PERM_WRITE/RESIZE before |
96 | * the normal backing chain can be restored. */ | |
97 | blk_unref(s->base); | |
9e85cd5c SH |
98 | |
99 | if (!block_job_is_cancelled(&s->common) && ret == 0) { | |
100 | /* success */ | |
8dfba279 KW |
101 | ret = bdrv_drop_intermediate(active, s->commit_top_bs, base, |
102 | s->backing_file_str); | |
103 | } else if (overlay_bs) { | |
104 | /* XXX Can (or should) we somehow keep 'consistent read' blocked even | |
105 | * after the failed/cancelled commit job is gone? If we already wrote | |
106 | * something to base, the intermediate images aren't valid any more. */ | |
107 | remove_commit_top_bs = true; | |
9e85cd5c SH |
108 | } |
109 | ||
110 | /* restore base open flags here if appropriate (e.g., change the base back | |
111 | * to r/o). These reopens do not need to be atomic, since we won't abort | |
112 | * even on failure here */ | |
113 | if (s->base_flags != bdrv_get_flags(base)) { | |
114 | bdrv_reopen(base, s->base_flags, NULL); | |
115 | } | |
9e85cd5c SH |
116 | if (overlay_bs && s->orig_overlay_flags != bdrv_get_flags(overlay_bs)) { |
117 | bdrv_reopen(overlay_bs, s->orig_overlay_flags, NULL); | |
118 | } | |
119 | g_free(s->backing_file_str); | |
4653456a | 120 | blk_unref(s->top); |
4f78a16f KW |
121 | |
122 | /* If there is more than one reference to the job (e.g. if called from | |
123 | * block_job_finish_sync()), block_job_completed() won't free it and | |
124 | * therefore the blockers on the intermediate nodes remain. This would | |
125 | * cause bdrv_set_backing_hd() to fail. */ | |
126 | block_job_remove_all_bdrv(job); | |
127 | ||
9e85cd5c SH |
128 | block_job_completed(&s->common, ret); |
129 | g_free(data); | |
8dfba279 KW |
130 | |
131 | /* If bdrv_drop_intermediate() didn't already do that, remove the commit | |
132 | * filter driver from the backing chain. Do this as the final step so that | |
133 | * the 'consistent read' permission can be granted. */ | |
134 | if (remove_commit_top_bs) { | |
12fa4af6 | 135 | bdrv_set_backing_hd(overlay_bs, top, &error_abort); |
8dfba279 | 136 | } |
19ebd13e KW |
137 | |
138 | bdrv_unref(overlay_bs); | |
139 | bdrv_unref(top); | |
9e85cd5c SH |
140 | } |
141 | ||
142 | static void coroutine_fn commit_run(void *opaque) | |
143 | { | |
144 | CommitBlockJob *s = opaque; | |
145 | CommitCompleteData *data; | |
747ff602 | 146 | int64_t sector_num, end; |
f14a39cc | 147 | uint64_t delay_ns = 0; |
747ff602 JC |
148 | int ret = 0; |
149 | int n = 0; | |
9e85cd5c | 150 | void *buf = NULL; |
747ff602 JC |
151 | int bytes_written = 0; |
152 | int64_t base_len; | |
153 | ||
4653456a | 154 | ret = s->common.len = blk_getlength(s->top); |
747ff602 JC |
155 | |
156 | ||
157 | if (s->common.len < 0) { | |
9e85cd5c | 158 | goto out; |
747ff602 JC |
159 | } |
160 | ||
4653456a | 161 | ret = base_len = blk_getlength(s->base); |
747ff602 | 162 | if (base_len < 0) { |
9e85cd5c | 163 | goto out; |
747ff602 JC |
164 | } |
165 | ||
166 | if (base_len < s->common.len) { | |
ed3d2ec9 | 167 | ret = blk_truncate(s->base, s->common.len, NULL); |
747ff602 | 168 | if (ret) { |
9e85cd5c | 169 | goto out; |
747ff602 JC |
170 | } |
171 | } | |
172 | ||
747ff602 | 173 | end = s->common.len >> BDRV_SECTOR_BITS; |
4653456a | 174 | buf = blk_blockalign(s->top, COMMIT_BUFFER_SIZE); |
747ff602 JC |
175 | |
176 | for (sector_num = 0; sector_num < end; sector_num += n) { | |
747ff602 JC |
177 | bool copy; |
178 | ||
747ff602 | 179 | /* Note that even when no rate limit is applied we need to yield |
c57b6656 | 180 | * with no pending I/O here so that bdrv_drain_all() returns. |
747ff602 | 181 | */ |
7483d1e5 | 182 | block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns); |
747ff602 JC |
183 | if (block_job_is_cancelled(&s->common)) { |
184 | break; | |
185 | } | |
186 | /* Copy if allocated above the base */ | |
4653456a KW |
187 | ret = bdrv_is_allocated_above(blk_bs(s->top), blk_bs(s->base), |
188 | sector_num, | |
4f578637 PB |
189 | COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE, |
190 | &n); | |
747ff602 | 191 | copy = (ret == 1); |
5cb1a49e EB |
192 | trace_commit_one_iteration(s, sector_num * BDRV_SECTOR_SIZE, |
193 | n * BDRV_SECTOR_SIZE, ret); | |
747ff602 | 194 | if (copy) { |
d8a98584 EB |
195 | ret = commit_populate(s->top, s->base, |
196 | sector_num * BDRV_SECTOR_SIZE, | |
197 | n * BDRV_SECTOR_SIZE, buf); | |
747ff602 JC |
198 | bytes_written += n * BDRV_SECTOR_SIZE; |
199 | } | |
200 | if (ret < 0) { | |
1e8fb7f1 KW |
201 | BlockErrorAction action = |
202 | block_job_error_action(&s->common, false, s->on_error, -ret); | |
203 | if (action == BLOCK_ERROR_ACTION_REPORT) { | |
9e85cd5c | 204 | goto out; |
747ff602 JC |
205 | } else { |
206 | n = 0; | |
207 | continue; | |
208 | } | |
209 | } | |
210 | /* Publish progress */ | |
211 | s->common.offset += n * BDRV_SECTOR_SIZE; | |
f14a39cc SS |
212 | |
213 | if (copy && s->common.speed) { | |
f3e4ce4a EB |
214 | delay_ns = ratelimit_calculate_delay(&s->limit, |
215 | n * BDRV_SECTOR_SIZE); | |
f14a39cc | 216 | } |
747ff602 JC |
217 | } |
218 | ||
219 | ret = 0; | |
220 | ||
9e85cd5c | 221 | out: |
747ff602 JC |
222 | qemu_vfree(buf); |
223 | ||
9e85cd5c SH |
224 | data = g_malloc(sizeof(*data)); |
225 | data->ret = ret; | |
226 | block_job_defer_to_main_loop(&s->common, commit_complete, data); | |
747ff602 JC |
227 | } |
228 | ||
229 | static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp) | |
230 | { | |
231 | CommitBlockJob *s = container_of(job, CommitBlockJob, common); | |
232 | ||
233 | if (speed < 0) { | |
c6bd8c70 | 234 | error_setg(errp, QERR_INVALID_PARAMETER, "speed"); |
747ff602 JC |
235 | return; |
236 | } | |
f3e4ce4a | 237 | ratelimit_set_speed(&s->limit, speed, SLICE_TIME); |
747ff602 JC |
238 | } |
239 | ||
3fc4b10a | 240 | static const BlockJobDriver commit_job_driver = { |
747ff602 | 241 | .instance_size = sizeof(CommitBlockJob), |
79e14bf7 | 242 | .job_type = BLOCK_JOB_TYPE_COMMIT, |
747ff602 | 243 | .set_speed = commit_set_speed, |
a7815a76 | 244 | .start = commit_run, |
747ff602 JC |
245 | }; |
246 | ||
8dfba279 KW |
247 | static int coroutine_fn bdrv_commit_top_preadv(BlockDriverState *bs, |
248 | uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) | |
249 | { | |
250 | return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags); | |
251 | } | |
252 | ||
91965658 KW |
253 | static int64_t coroutine_fn bdrv_commit_top_get_block_status( |
254 | BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum, | |
255 | BlockDriverState **file) | |
256 | { | |
257 | *pnum = nb_sectors; | |
258 | *file = bs->backing->bs; | |
d5254033 | 259 | return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | |
91965658 KW |
260 | (sector_num << BDRV_SECTOR_BITS); |
261 | } | |
262 | ||
dcbf37ce KW |
263 | static void bdrv_commit_top_refresh_filename(BlockDriverState *bs, QDict *opts) |
264 | { | |
265 | bdrv_refresh_filename(bs->backing->bs); | |
266 | pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), | |
267 | bs->backing->bs->filename); | |
268 | } | |
91965658 | 269 | |
8dfba279 KW |
270 | static void bdrv_commit_top_close(BlockDriverState *bs) |
271 | { | |
272 | } | |
273 | ||
274 | static void bdrv_commit_top_child_perm(BlockDriverState *bs, BdrvChild *c, | |
275 | const BdrvChildRole *role, | |
276 | uint64_t perm, uint64_t shared, | |
277 | uint64_t *nperm, uint64_t *nshared) | |
278 | { | |
279 | *nperm = 0; | |
280 | *nshared = BLK_PERM_ALL; | |
281 | } | |
282 | ||
283 | /* Dummy node that provides consistent read to its users without requiring it | |
284 | * from its backing file and that allows writes on the backing file chain. */ | |
285 | static BlockDriver bdrv_commit_top = { | |
91965658 KW |
286 | .format_name = "commit_top", |
287 | .bdrv_co_preadv = bdrv_commit_top_preadv, | |
288 | .bdrv_co_get_block_status = bdrv_commit_top_get_block_status, | |
dcbf37ce | 289 | .bdrv_refresh_filename = bdrv_commit_top_refresh_filename, |
91965658 KW |
290 | .bdrv_close = bdrv_commit_top_close, |
291 | .bdrv_child_perm = bdrv_commit_top_child_perm, | |
8dfba279 KW |
292 | }; |
293 | ||
fd62c609 AG |
294 | void commit_start(const char *job_id, BlockDriverState *bs, |
295 | BlockDriverState *base, BlockDriverState *top, int64_t speed, | |
8254b6d9 | 296 | BlockdevOnError on_error, const char *backing_file_str, |
0db832f4 | 297 | const char *filter_node_name, Error **errp) |
747ff602 JC |
298 | { |
299 | CommitBlockJob *s; | |
300 | BlockReopenQueue *reopen_queue = NULL; | |
301 | int orig_overlay_flags; | |
302 | int orig_base_flags; | |
3e4c5122 | 303 | BlockDriverState *iter; |
747ff602 | 304 | BlockDriverState *overlay_bs; |
8dfba279 | 305 | BlockDriverState *commit_top_bs = NULL; |
747ff602 | 306 | Error *local_err = NULL; |
d7086422 | 307 | int ret; |
747ff602 | 308 | |
18da7f94 | 309 | assert(top != bs); |
747ff602 JC |
310 | if (top == base) { |
311 | error_setg(errp, "Invalid files for merge: top and base are the same"); | |
312 | return; | |
313 | } | |
314 | ||
747ff602 JC |
315 | overlay_bs = bdrv_find_overlay(bs, top); |
316 | ||
317 | if (overlay_bs == NULL) { | |
318 | error_setg(errp, "Could not find overlay image for %s:", top->filename); | |
319 | return; | |
320 | } | |
321 | ||
c6cc12bf KW |
322 | s = block_job_create(job_id, &commit_job_driver, bs, 0, BLK_PERM_ALL, |
323 | speed, BLOCK_JOB_DEFAULT, NULL, NULL, errp); | |
834fe28d AG |
324 | if (!s) { |
325 | return; | |
326 | } | |
327 | ||
747ff602 JC |
328 | orig_base_flags = bdrv_get_flags(base); |
329 | orig_overlay_flags = bdrv_get_flags(overlay_bs); | |
330 | ||
331 | /* convert base & overlay_bs to r/w, if necessary */ | |
3db2bd55 AG |
332 | if (!(orig_base_flags & BDRV_O_RDWR)) { |
333 | reopen_queue = bdrv_reopen_queue(reopen_queue, base, NULL, | |
334 | orig_base_flags | BDRV_O_RDWR); | |
335 | } | |
0fe282bb AG |
336 | if (!(orig_overlay_flags & BDRV_O_RDWR)) { |
337 | reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs, NULL, | |
338 | orig_overlay_flags | BDRV_O_RDWR); | |
339 | } | |
747ff602 | 340 | if (reopen_queue) { |
720150f3 | 341 | bdrv_reopen_multiple(bdrv_get_aio_context(bs), reopen_queue, &local_err); |
747ff602 JC |
342 | if (local_err != NULL) { |
343 | error_propagate(errp, local_err); | |
d7086422 | 344 | goto fail; |
747ff602 JC |
345 | } |
346 | } | |
347 | ||
8dfba279 KW |
348 | /* Insert commit_top block node above top, so we can block consistent read |
349 | * on the backing chain below it */ | |
0db832f4 KW |
350 | commit_top_bs = bdrv_new_open_driver(&bdrv_commit_top, filter_node_name, 0, |
351 | errp); | |
8dfba279 KW |
352 | if (commit_top_bs == NULL) { |
353 | goto fail; | |
354 | } | |
0d0676a1 | 355 | commit_top_bs->total_sectors = top->total_sectors; |
02be4aeb | 356 | bdrv_set_aio_context(commit_top_bs, bdrv_get_aio_context(top)); |
8dfba279 | 357 | |
b69f00dd FZ |
358 | bdrv_set_backing_hd(commit_top_bs, top, &local_err); |
359 | if (local_err) { | |
360 | bdrv_unref(commit_top_bs); | |
361 | commit_top_bs = NULL; | |
362 | error_propagate(errp, local_err); | |
363 | goto fail; | |
364 | } | |
365 | bdrv_set_backing_hd(overlay_bs, commit_top_bs, &local_err); | |
366 | if (local_err) { | |
367 | bdrv_unref(commit_top_bs); | |
368 | commit_top_bs = NULL; | |
369 | error_propagate(errp, local_err); | |
370 | goto fail; | |
371 | } | |
8dfba279 KW |
372 | |
373 | s->commit_top_bs = commit_top_bs; | |
374 | bdrv_unref(commit_top_bs); | |
747ff602 | 375 | |
3e4c5122 AG |
376 | /* Block all nodes between top and base, because they will |
377 | * disappear from the chain after this operation. */ | |
378 | assert(bdrv_chain_contains(top, base)); | |
8dfba279 KW |
379 | for (iter = top; iter != base; iter = backing_bs(iter)) { |
380 | /* XXX BLK_PERM_WRITE needs to be allowed so we don't block ourselves | |
381 | * at s->base (if writes are blocked for a node, they are also blocked | |
382 | * for its backing file). The other options would be a second filter | |
383 | * driver above s->base. */ | |
384 | ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0, | |
385 | BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE, | |
386 | errp); | |
387 | if (ret < 0) { | |
388 | goto fail; | |
389 | } | |
3e4c5122 | 390 | } |
8dfba279 KW |
391 | |
392 | ret = block_job_add_bdrv(&s->common, "base", base, 0, BLK_PERM_ALL, errp); | |
393 | if (ret < 0) { | |
394 | goto fail; | |
395 | } | |
396 | ||
3e4c5122 | 397 | /* overlay_bs must be blocked because it needs to be modified to |
8dfba279 KW |
398 | * update the backing image string. */ |
399 | ret = block_job_add_bdrv(&s->common, "overlay of top", overlay_bs, | |
400 | BLK_PERM_GRAPH_MOD, BLK_PERM_ALL, errp); | |
401 | if (ret < 0) { | |
402 | goto fail; | |
3e4c5122 AG |
403 | } |
404 | ||
8dfba279 KW |
405 | s->base = blk_new(BLK_PERM_CONSISTENT_READ |
406 | | BLK_PERM_WRITE | |
407 | | BLK_PERM_RESIZE, | |
408 | BLK_PERM_CONSISTENT_READ | |
409 | | BLK_PERM_GRAPH_MOD | |
410 | | BLK_PERM_WRITE_UNCHANGED); | |
d7086422 KW |
411 | ret = blk_insert_bs(s->base, base, errp); |
412 | if (ret < 0) { | |
413 | goto fail; | |
414 | } | |
4653456a | 415 | |
8dfba279 | 416 | /* Required permissions are already taken with block_job_add_bdrv() */ |
6d0eb64d | 417 | s->top = blk_new(0, BLK_PERM_ALL); |
b247767a | 418 | ret = blk_insert_bs(s->top, top, errp); |
d7086422 KW |
419 | if (ret < 0) { |
420 | goto fail; | |
421 | } | |
4653456a | 422 | |
747ff602 JC |
423 | s->active = bs; |
424 | ||
425 | s->base_flags = orig_base_flags; | |
426 | s->orig_overlay_flags = orig_overlay_flags; | |
427 | ||
54e26900 JC |
428 | s->backing_file_str = g_strdup(backing_file_str); |
429 | ||
747ff602 | 430 | s->on_error = on_error; |
747ff602 | 431 | |
5ccac6f1 JS |
432 | trace_commit_start(bs, base, top, s); |
433 | block_job_start(&s->common); | |
d7086422 KW |
434 | return; |
435 | ||
436 | fail: | |
437 | if (s->base) { | |
438 | blk_unref(s->base); | |
439 | } | |
440 | if (s->top) { | |
441 | blk_unref(s->top); | |
442 | } | |
8dfba279 | 443 | if (commit_top_bs) { |
12fa4af6 | 444 | bdrv_set_backing_hd(overlay_bs, top, &error_abort); |
8dfba279 | 445 | } |
05b0d8e3 | 446 | block_job_early_fail(&s->common); |
747ff602 | 447 | } |
83fd6dd3 KW |
448 | |
449 | ||
450 | #define COMMIT_BUF_SECTORS 2048 | |
451 | ||
452 | /* commit COW file into the raw image */ | |
453 | int bdrv_commit(BlockDriverState *bs) | |
454 | { | |
f8e2bd53 | 455 | BlockBackend *src, *backing; |
d3f06759 KW |
456 | BlockDriverState *backing_file_bs = NULL; |
457 | BlockDriverState *commit_top_bs = NULL; | |
83fd6dd3 KW |
458 | BlockDriver *drv = bs->drv; |
459 | int64_t sector, total_sectors, length, backing_length; | |
460 | int n, ro, open_flags; | |
461 | int ret = 0; | |
462 | uint8_t *buf = NULL; | |
d3f06759 | 463 | Error *local_err = NULL; |
83fd6dd3 KW |
464 | |
465 | if (!drv) | |
466 | return -ENOMEDIUM; | |
467 | ||
468 | if (!bs->backing) { | |
469 | return -ENOTSUP; | |
470 | } | |
471 | ||
472 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) || | |
473 | bdrv_op_is_blocked(bs->backing->bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) { | |
474 | return -EBUSY; | |
475 | } | |
476 | ||
477 | ro = bs->backing->bs->read_only; | |
478 | open_flags = bs->backing->bs->open_flags; | |
479 | ||
480 | if (ro) { | |
481 | if (bdrv_reopen(bs->backing->bs, open_flags | BDRV_O_RDWR, NULL)) { | |
482 | return -EACCES; | |
483 | } | |
484 | } | |
485 | ||
d3f06759 KW |
486 | src = blk_new(BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL); |
487 | backing = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); | |
d7086422 | 488 | |
d3f06759 | 489 | ret = blk_insert_bs(src, bs, &local_err); |
d7086422 | 490 | if (ret < 0) { |
d3f06759 KW |
491 | error_report_err(local_err); |
492 | goto ro_cleanup; | |
493 | } | |
494 | ||
495 | /* Insert commit_top block node above backing, so we can write to it */ | |
496 | backing_file_bs = backing_bs(bs); | |
497 | ||
498 | commit_top_bs = bdrv_new_open_driver(&bdrv_commit_top, NULL, BDRV_O_RDWR, | |
499 | &local_err); | |
500 | if (commit_top_bs == NULL) { | |
501 | error_report_err(local_err); | |
d7086422 KW |
502 | goto ro_cleanup; |
503 | } | |
02be4aeb | 504 | bdrv_set_aio_context(commit_top_bs, bdrv_get_aio_context(backing_file_bs)); |
d7086422 | 505 | |
12fa4af6 KW |
506 | bdrv_set_backing_hd(commit_top_bs, backing_file_bs, &error_abort); |
507 | bdrv_set_backing_hd(bs, commit_top_bs, &error_abort); | |
d3f06759 KW |
508 | |
509 | ret = blk_insert_bs(backing, backing_file_bs, &local_err); | |
d7086422 | 510 | if (ret < 0) { |
d3f06759 | 511 | error_report_err(local_err); |
d7086422 KW |
512 | goto ro_cleanup; |
513 | } | |
f8e2bd53 KW |
514 | |
515 | length = blk_getlength(src); | |
83fd6dd3 KW |
516 | if (length < 0) { |
517 | ret = length; | |
518 | goto ro_cleanup; | |
519 | } | |
520 | ||
f8e2bd53 | 521 | backing_length = blk_getlength(backing); |
83fd6dd3 KW |
522 | if (backing_length < 0) { |
523 | ret = backing_length; | |
524 | goto ro_cleanup; | |
525 | } | |
526 | ||
527 | /* If our top snapshot is larger than the backing file image, | |
528 | * grow the backing file image if possible. If not possible, | |
529 | * we must return an error */ | |
530 | if (length > backing_length) { | |
ed3d2ec9 | 531 | ret = blk_truncate(backing, length, &local_err); |
83fd6dd3 | 532 | if (ret < 0) { |
ed3d2ec9 | 533 | error_report_err(local_err); |
83fd6dd3 KW |
534 | goto ro_cleanup; |
535 | } | |
536 | } | |
537 | ||
538 | total_sectors = length >> BDRV_SECTOR_BITS; | |
539 | ||
f8e2bd53 KW |
540 | /* blk_try_blockalign() for src will choose an alignment that works for |
541 | * backing as well, so no need to compare the alignment manually. */ | |
542 | buf = blk_try_blockalign(src, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); | |
83fd6dd3 KW |
543 | if (buf == NULL) { |
544 | ret = -ENOMEM; | |
545 | goto ro_cleanup; | |
546 | } | |
547 | ||
548 | for (sector = 0; sector < total_sectors; sector += n) { | |
549 | ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n); | |
550 | if (ret < 0) { | |
551 | goto ro_cleanup; | |
552 | } | |
553 | if (ret) { | |
f8e2bd53 KW |
554 | ret = blk_pread(src, sector * BDRV_SECTOR_SIZE, buf, |
555 | n * BDRV_SECTOR_SIZE); | |
83fd6dd3 KW |
556 | if (ret < 0) { |
557 | goto ro_cleanup; | |
558 | } | |
559 | ||
f8e2bd53 KW |
560 | ret = blk_pwrite(backing, sector * BDRV_SECTOR_SIZE, buf, |
561 | n * BDRV_SECTOR_SIZE, 0); | |
83fd6dd3 KW |
562 | if (ret < 0) { |
563 | goto ro_cleanup; | |
564 | } | |
565 | } | |
566 | } | |
567 | ||
568 | if (drv->bdrv_make_empty) { | |
569 | ret = drv->bdrv_make_empty(bs); | |
570 | if (ret < 0) { | |
571 | goto ro_cleanup; | |
572 | } | |
f8e2bd53 | 573 | blk_flush(src); |
83fd6dd3 KW |
574 | } |
575 | ||
576 | /* | |
577 | * Make sure all data we wrote to the backing device is actually | |
578 | * stable on disk. | |
579 | */ | |
f8e2bd53 | 580 | blk_flush(backing); |
83fd6dd3 KW |
581 | |
582 | ret = 0; | |
583 | ro_cleanup: | |
584 | qemu_vfree(buf); | |
585 | ||
f8e2bd53 | 586 | blk_unref(backing); |
d3f06759 | 587 | if (backing_file_bs) { |
12fa4af6 | 588 | bdrv_set_backing_hd(bs, backing_file_bs, &error_abort); |
d3f06759 KW |
589 | } |
590 | bdrv_unref(commit_top_bs); | |
591 | blk_unref(src); | |
f8e2bd53 | 592 | |
83fd6dd3 KW |
593 | if (ro) { |
594 | /* ignoring error return here */ | |
595 | bdrv_reopen(bs->backing->bs, open_flags & ~BDRV_O_RDWR, NULL); | |
596 | } | |
597 | ||
598 | return ret; | |
599 | } |