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