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