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