4 * Copyright Red Hat, Inc. 2012
7 * Jeff Cody <jcody@redhat.com>
8 * Based on stream.c by Stefan Hajnoczi
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.
15 #include "qemu/osdep.h"
16 #include "qemu/cutils.h"
18 #include "block/block_int.h"
19 #include "block/blockjob_int.h"
20 #include "qapi/error.h"
21 #include "qemu/ratelimit.h"
22 #include "qemu/memalign.h"
23 #include "sysemu/block-backend.h"
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.
31 COMMIT_BUFFER_SIZE
= 512 * 1024, /* in bytes */
34 typedef struct CommitBlockJob
{
36 BlockDriverState
*commit_top_bs
;
39 BlockDriverState
*base_bs
;
40 BlockDriverState
*base_overlay
;
41 BlockdevOnError on_error
;
44 char *backing_file_str
;
45 bool backing_mask_protocol
;
48 static int commit_prepare(Job
*job
)
50 CommitBlockJob
*s
= container_of(job
, CommitBlockJob
, common
.job
);
52 bdrv_graph_rdlock_main_loop();
53 bdrv_unfreeze_backing_chain(s
->commit_top_bs
, s
->base_bs
);
54 s
->chain_frozen
= false;
55 bdrv_graph_rdunlock_main_loop();
57 /* Remove base node parent that still uses BLK_PERM_WRITE/RESIZE before
58 * the normal backing chain can be restored. */
62 /* FIXME: bdrv_drop_intermediate treats total failures and partial failures
63 * identically. Further work is needed to disambiguate these cases. */
64 return bdrv_drop_intermediate(s
->commit_top_bs
, s
->base_bs
,
66 s
->backing_mask_protocol
);
69 static void commit_abort(Job
*job
)
71 CommitBlockJob
*s
= container_of(job
, CommitBlockJob
, common
.job
);
72 BlockDriverState
*top_bs
= blk_bs(s
->top
);
73 BlockDriverState
*commit_top_backing_bs
;
75 if (s
->chain_frozen
) {
76 bdrv_graph_rdlock_main_loop();
77 bdrv_unfreeze_backing_chain(s
->commit_top_bs
, s
->base_bs
);
78 bdrv_graph_rdunlock_main_loop();
81 /* Make sure commit_top_bs and top stay around until bdrv_replace_node() */
83 bdrv_ref(s
->commit_top_bs
);
89 /* free the blockers on the intermediate nodes so that bdrv_replace_nodes
91 block_job_remove_all_bdrv(&s
->common
);
93 /* If bdrv_drop_intermediate() failed (or was not invoked), remove the
94 * commit filter driver from the backing chain now. Do this as the final
95 * step so that the 'consistent read' permission can be granted.
97 * XXX Can (or should) we somehow keep 'consistent read' blocked even
98 * after the failed/cancelled commit job is gone? If we already wrote
99 * something to base, the intermediate images aren't valid any more. */
100 bdrv_graph_rdlock_main_loop();
101 commit_top_backing_bs
= s
->commit_top_bs
->backing
->bs
;
102 bdrv_graph_rdunlock_main_loop();
104 bdrv_drained_begin(commit_top_backing_bs
);
106 bdrv_replace_node(s
->commit_top_bs
, commit_top_backing_bs
, &error_abort
);
107 bdrv_graph_wrunlock();
108 bdrv_drained_end(commit_top_backing_bs
);
110 bdrv_unref(s
->commit_top_bs
);
114 static void commit_clean(Job
*job
)
116 CommitBlockJob
*s
= container_of(job
, CommitBlockJob
, common
.job
);
118 /* restore base open flags here if appropriate (e.g., change the base back
119 * to r/o). These reopens do not need to be atomic, since we won't abort
120 * even on failure here */
121 if (s
->base_read_only
) {
122 bdrv_reopen_set_read_only(s
->base_bs
, true, NULL
);
125 g_free(s
->backing_file_str
);
129 static int coroutine_fn
commit_run(Job
*job
, Error
**errp
)
131 CommitBlockJob
*s
= container_of(job
, CommitBlockJob
, common
.job
);
134 int64_t n
= 0; /* bytes */
135 QEMU_AUTO_VFREE
void *buf
= NULL
;
136 int64_t len
, base_len
;
138 len
= blk_co_getlength(s
->top
);
142 job_progress_set_remaining(&s
->common
.job
, len
);
144 base_len
= blk_co_getlength(s
->base
);
149 if (base_len
< len
) {
150 ret
= blk_co_truncate(s
->base
, len
, false, PREALLOC_MODE_OFF
, 0, NULL
);
156 buf
= blk_blockalign(s
->top
, COMMIT_BUFFER_SIZE
);
158 for (offset
= 0; offset
< len
; offset
+= n
) {
160 bool error_in_source
= true;
162 /* Note that even when no rate limit is applied we need to yield
163 * with no pending I/O here so that bdrv_drain_all() returns.
165 block_job_ratelimit_sleep(&s
->common
);
166 if (job_is_cancelled(&s
->common
.job
)) {
169 /* Copy if allocated above the base */
170 ret
= blk_co_is_allocated_above(s
->top
, s
->base_overlay
, true,
171 offset
, COMMIT_BUFFER_SIZE
, &n
);
173 trace_commit_one_iteration(s
, offset
, n
, ret
);
175 assert(n
< SIZE_MAX
);
177 ret
= blk_co_pread(s
->top
, offset
, n
, buf
, 0);
179 ret
= blk_co_pwrite(s
->base
, offset
, n
, buf
, 0);
181 error_in_source
= false;
186 BlockErrorAction action
=
187 block_job_error_action(&s
->common
, s
->on_error
,
188 error_in_source
, -ret
);
189 if (action
== BLOCK_ERROR_ACTION_REPORT
) {
196 /* Publish progress */
197 job_progress_update(&s
->common
.job
, n
);
200 block_job_ratelimit_processed_bytes(&s
->common
, n
);
207 static const BlockJobDriver commit_job_driver
= {
209 .instance_size
= sizeof(CommitBlockJob
),
210 .job_type
= JOB_TYPE_COMMIT
,
211 .free
= block_job_free
,
212 .user_resume
= block_job_user_resume
,
214 .prepare
= commit_prepare
,
215 .abort
= commit_abort
,
216 .clean
= commit_clean
220 static int coroutine_fn GRAPH_RDLOCK
221 bdrv_commit_top_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
222 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
224 return bdrv_co_preadv(bs
->backing
, offset
, bytes
, qiov
, flags
);
227 static GRAPH_RDLOCK
void bdrv_commit_top_refresh_filename(BlockDriverState
*bs
)
229 pstrcpy(bs
->exact_filename
, sizeof(bs
->exact_filename
),
230 bs
->backing
->bs
->filename
);
233 static void bdrv_commit_top_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
235 BlockReopenQueue
*reopen_queue
,
236 uint64_t perm
, uint64_t shared
,
237 uint64_t *nperm
, uint64_t *nshared
)
240 *nshared
= BLK_PERM_ALL
;
243 /* Dummy node that provides consistent read to its users without requiring it
244 * from its backing file and that allows writes on the backing file chain. */
245 static BlockDriver bdrv_commit_top
= {
246 .format_name
= "commit_top",
247 .bdrv_co_preadv
= bdrv_commit_top_preadv
,
248 .bdrv_refresh_filename
= bdrv_commit_top_refresh_filename
,
249 .bdrv_child_perm
= bdrv_commit_top_child_perm
,
252 .filtered_child_is_backing
= true,
255 void commit_start(const char *job_id
, BlockDriverState
*bs
,
256 BlockDriverState
*base
, BlockDriverState
*top
,
257 int creation_flags
, int64_t speed
,
258 BlockdevOnError on_error
, const char *backing_file_str
,
259 bool backing_mask_protocol
,
260 const char *filter_node_name
, Error
**errp
)
263 BlockDriverState
*iter
;
264 BlockDriverState
*commit_top_bs
= NULL
;
265 BlockDriverState
*filtered_base
;
266 int64_t base_size
, top_size
;
267 uint64_t base_perms
, iter_shared_perms
;
273 bdrv_graph_rdlock_main_loop();
274 if (bdrv_skip_filters(top
) == bdrv_skip_filters(base
)) {
275 error_setg(errp
, "Invalid files for merge: top and base are the same");
276 bdrv_graph_rdunlock_main_loop();
279 bdrv_graph_rdunlock_main_loop();
281 base_size
= bdrv_getlength(base
);
283 error_setg_errno(errp
, -base_size
, "Could not inquire base image size");
287 top_size
= bdrv_getlength(top
);
289 error_setg_errno(errp
, -top_size
, "Could not inquire top image size");
293 base_perms
= BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
;
294 if (base_size
< top_size
) {
295 base_perms
|= BLK_PERM_RESIZE
;
298 s
= block_job_create(job_id
, &commit_job_driver
, NULL
, bs
, 0, BLK_PERM_ALL
,
299 speed
, creation_flags
, NULL
, NULL
, errp
);
304 /* convert base to r/w, if necessary */
305 s
->base_read_only
= bdrv_is_read_only(base
);
306 if (s
->base_read_only
) {
307 if (bdrv_reopen_set_read_only(base
, false, errp
) != 0) {
312 /* Insert commit_top block node above top, so we can block consistent read
313 * on the backing chain below it */
314 commit_top_bs
= bdrv_new_open_driver(&bdrv_commit_top
, filter_node_name
, 0,
316 if (commit_top_bs
== NULL
) {
319 if (!filter_node_name
) {
320 commit_top_bs
->implicit
= true;
323 /* So that we can always drop this node */
324 commit_top_bs
->never_freeze
= true;
326 commit_top_bs
->total_sectors
= top
->total_sectors
;
328 ret
= bdrv_append(commit_top_bs
, top
, errp
);
329 bdrv_unref(commit_top_bs
); /* referenced by new parents or failed */
331 commit_top_bs
= NULL
;
335 s
->commit_top_bs
= commit_top_bs
;
338 * Block all nodes between top and base, because they will
339 * disappear from the chain after this operation.
340 * Note that this assumes that the user is fine with removing all
341 * nodes (including R/W filters) between top and base. Assuring
342 * this is the responsibility of the interface (i.e. whoever calls
346 s
->base_overlay
= bdrv_find_overlay(top
, base
);
347 assert(s
->base_overlay
);
350 * The topmost node with
351 * bdrv_skip_filters(filtered_base) == bdrv_skip_filters(base)
353 filtered_base
= bdrv_cow_bs(s
->base_overlay
);
354 assert(bdrv_skip_filters(filtered_base
) == bdrv_skip_filters(base
));
357 * XXX BLK_PERM_WRITE needs to be allowed so we don't block ourselves
358 * at s->base (if writes are blocked for a node, they are also blocked
359 * for its backing file). The other options would be a second filter
360 * driver above s->base.
362 iter_shared_perms
= BLK_PERM_WRITE_UNCHANGED
| BLK_PERM_WRITE
;
364 for (iter
= top
; iter
!= base
; iter
= bdrv_filter_or_cow_bs(iter
)) {
365 if (iter
== filtered_base
) {
367 * From here on, all nodes are filters on the base. This
368 * allows us to share BLK_PERM_CONSISTENT_READ.
370 iter_shared_perms
|= BLK_PERM_CONSISTENT_READ
;
373 ret
= block_job_add_bdrv(&s
->common
, "intermediate node", iter
, 0,
374 iter_shared_perms
, errp
);
376 bdrv_graph_wrunlock();
381 if (bdrv_freeze_backing_chain(commit_top_bs
, base
, errp
) < 0) {
382 bdrv_graph_wrunlock();
385 s
->chain_frozen
= true;
387 ret
= block_job_add_bdrv(&s
->common
, "base", base
, 0, BLK_PERM_ALL
, errp
);
388 bdrv_graph_wrunlock();
394 s
->base
= blk_new(s
->common
.job
.aio_context
,
396 BLK_PERM_CONSISTENT_READ
397 | BLK_PERM_WRITE_UNCHANGED
);
398 ret
= blk_insert_bs(s
->base
, base
, errp
);
402 blk_set_disable_request_queuing(s
->base
, true);
405 /* Required permissions are already taken with block_job_add_bdrv() */
406 s
->top
= blk_new(s
->common
.job
.aio_context
, 0, BLK_PERM_ALL
);
407 ret
= blk_insert_bs(s
->top
, top
, errp
);
411 blk_set_disable_request_queuing(s
->top
, true);
413 s
->backing_file_str
= g_strdup(backing_file_str
);
414 s
->backing_mask_protocol
= backing_mask_protocol
;
415 s
->on_error
= on_error
;
417 trace_commit_start(bs
, base
, top
, s
);
418 job_start(&s
->common
.job
);
422 if (s
->chain_frozen
) {
423 bdrv_graph_rdlock_main_loop();
424 bdrv_unfreeze_backing_chain(commit_top_bs
, base
);
425 bdrv_graph_rdunlock_main_loop();
433 if (s
->base_read_only
) {
434 bdrv_reopen_set_read_only(base
, true, NULL
);
436 job_early_fail(&s
->common
.job
);
437 /* commit_top_bs has to be replaced after deleting the block job,
438 * otherwise this would fail because of lack of permissions. */
440 bdrv_drained_begin(top
);
442 bdrv_replace_node(commit_top_bs
, top
, &error_abort
);
443 bdrv_graph_wrunlock();
444 bdrv_drained_end(top
);
449 #define COMMIT_BUF_SIZE (2048 * BDRV_SECTOR_SIZE)
451 /* commit COW file into the raw image */
452 int bdrv_commit(BlockDriverState
*bs
)
454 BlockBackend
*src
, *backing
;
455 BlockDriverState
*backing_file_bs
= NULL
;
456 BlockDriverState
*commit_top_bs
= NULL
;
457 BlockDriver
*drv
= bs
->drv
;
459 int64_t offset
, length
, backing_length
;
463 QEMU_AUTO_VFREE
uint8_t *buf
= NULL
;
464 Error
*local_err
= NULL
;
467 GRAPH_RDLOCK_GUARD_MAINLOOP();
472 backing_file_bs
= bdrv_cow_bs(bs
);
474 if (!backing_file_bs
) {
478 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, NULL
) ||
479 bdrv_op_is_blocked(backing_file_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, NULL
))
484 ro
= bdrv_is_read_only(backing_file_bs
);
487 if (bdrv_reopen_set_read_only(backing_file_bs
, false, NULL
)) {
492 ctx
= bdrv_get_aio_context(bs
);
493 /* WRITE_UNCHANGED is required for bdrv_make_empty() */
494 src
= blk_new(ctx
, BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE_UNCHANGED
,
496 backing
= blk_new(ctx
, BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
);
498 ret
= blk_insert_bs(src
, bs
, &local_err
);
500 error_report_err(local_err
);
504 /* Insert commit_top block node above backing, so we can write to it */
505 commit_top_bs
= bdrv_new_open_driver(&bdrv_commit_top
, NULL
, BDRV_O_RDWR
,
507 if (commit_top_bs
== NULL
) {
508 error_report_err(local_err
);
512 bdrv_set_backing_hd(commit_top_bs
, backing_file_bs
, &error_abort
);
513 bdrv_set_backing_hd(bs
, commit_top_bs
, &error_abort
);
515 ret
= blk_insert_bs(backing
, backing_file_bs
, &local_err
);
517 error_report_err(local_err
);
521 length
= blk_getlength(src
);
527 backing_length
= blk_getlength(backing
);
528 if (backing_length
< 0) {
529 ret
= backing_length
;
533 /* If our top snapshot is larger than the backing file image,
534 * grow the backing file image if possible. If not possible,
535 * we must return an error */
536 if (length
> backing_length
) {
537 ret
= blk_truncate(backing
, length
, false, PREALLOC_MODE_OFF
, 0,
540 error_report_err(local_err
);
545 /* blk_try_blockalign() for src will choose an alignment that works for
546 * backing as well, so no need to compare the alignment manually. */
547 buf
= blk_try_blockalign(src
, COMMIT_BUF_SIZE
);
553 for (offset
= 0; offset
< length
; offset
+= n
) {
554 ret
= bdrv_is_allocated(bs
, offset
, COMMIT_BUF_SIZE
, &n
);
559 ret
= blk_pread(src
, offset
, n
, buf
, 0);
564 ret
= blk_pwrite(backing
, offset
, n
, buf
, 0);
571 ret
= blk_make_empty(src
, NULL
);
572 /* Ignore -ENOTSUP */
573 if (ret
< 0 && ret
!= -ENOTSUP
) {
580 * Make sure all data we wrote to the backing device is actually
588 if (bdrv_cow_bs(bs
) != backing_file_bs
) {
589 bdrv_set_backing_hd(bs
, backing_file_bs
, &error_abort
);
591 bdrv_unref(commit_top_bs
);
595 /* ignoring error return here */
596 bdrv_reopen_set_read_only(backing_file_bs
, true, NULL
);