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"
17 #include "block/block_int.h"
18 #include "block/blockjob_int.h"
19 #include "qapi/error.h"
20 #include "qapi/qmp/qerror.h"
21 #include "qemu/ratelimit.h"
22 #include "sysemu/block-backend.h"
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.
30 COMMIT_BUFFER_SIZE
= 512 * 1024, /* in bytes */
33 #define SLICE_TIME 100000000ULL /* ns */
35 typedef struct CommitBlockJob
{
38 BlockDriverState
*active
;
39 BlockDriverState
*commit_top_bs
;
42 BlockdevOnError on_error
;
44 int orig_overlay_flags
;
45 char *backing_file_str
;
48 static int coroutine_fn
commit_populate(BlockBackend
*bs
, BlockBackend
*base
,
49 int64_t sector_num
, int nb_sectors
,
56 .iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
,
59 qemu_iovec_init_external(&qiov
, &iov
, 1);
61 ret
= blk_co_preadv(bs
, sector_num
* BDRV_SECTOR_SIZE
,
67 ret
= blk_co_pwritev(base
, sector_num
* BDRV_SECTOR_SIZE
,
80 static void commit_complete(BlockJob
*job
, void *opaque
)
82 CommitBlockJob
*s
= container_of(job
, CommitBlockJob
, common
);
83 CommitCompleteData
*data
= opaque
;
84 BlockDriverState
*active
= s
->active
;
85 BlockDriverState
*top
= blk_bs(s
->top
);
86 BlockDriverState
*base
= blk_bs(s
->base
);
87 BlockDriverState
*overlay_bs
= bdrv_find_overlay(active
, s
->commit_top_bs
);
89 bool remove_commit_top_bs
= false;
91 /* Remove base node parent that still uses BLK_PERM_WRITE/RESIZE before
92 * the normal backing chain can be restored. */
95 if (!block_job_is_cancelled(&s
->common
) && ret
== 0) {
97 ret
= bdrv_drop_intermediate(active
, s
->commit_top_bs
, base
,
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;
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
);
112 if (overlay_bs
&& s
->orig_overlay_flags
!= bdrv_get_flags(overlay_bs
)) {
113 bdrv_reopen(overlay_bs
, s
->orig_overlay_flags
, NULL
);
115 g_free(s
->backing_file_str
);
117 block_job_completed(&s
->common
, ret
);
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
) {
124 bdrv_set_backing_hd(overlay_bs
, top
, &error_abort
);
128 static void coroutine_fn
commit_run(void *opaque
)
130 CommitBlockJob
*s
= opaque
;
131 CommitCompleteData
*data
;
132 int64_t sector_num
, end
;
133 uint64_t delay_ns
= 0;
137 int bytes_written
= 0;
140 ret
= s
->common
.len
= blk_getlength(s
->top
);
143 if (s
->common
.len
< 0) {
147 ret
= base_len
= blk_getlength(s
->base
);
152 if (base_len
< s
->common
.len
) {
153 ret
= blk_truncate(s
->base
, s
->common
.len
);
159 end
= s
->common
.len
>> BDRV_SECTOR_BITS
;
160 buf
= blk_blockalign(s
->top
, COMMIT_BUFFER_SIZE
);
162 for (sector_num
= 0; sector_num
< end
; sector_num
+= n
) {
165 /* Note that even when no rate limit is applied we need to yield
166 * with no pending I/O here so that bdrv_drain_all() returns.
168 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
169 if (block_job_is_cancelled(&s
->common
)) {
172 /* Copy if allocated above the base */
173 ret
= bdrv_is_allocated_above(blk_bs(s
->top
), blk_bs(s
->base
),
175 COMMIT_BUFFER_SIZE
/ BDRV_SECTOR_SIZE
,
178 trace_commit_one_iteration(s
, sector_num
, n
, ret
);
180 ret
= commit_populate(s
->top
, s
->base
, sector_num
, n
, buf
);
181 bytes_written
+= n
* BDRV_SECTOR_SIZE
;
184 BlockErrorAction action
=
185 block_job_error_action(&s
->common
, false, s
->on_error
, -ret
);
186 if (action
== BLOCK_ERROR_ACTION_REPORT
) {
193 /* Publish progress */
194 s
->common
.offset
+= n
* BDRV_SECTOR_SIZE
;
196 if (copy
&& s
->common
.speed
) {
197 delay_ns
= ratelimit_calculate_delay(&s
->limit
, n
);
206 data
= g_malloc(sizeof(*data
));
208 block_job_defer_to_main_loop(&s
->common
, commit_complete
, data
);
211 static void commit_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
213 CommitBlockJob
*s
= container_of(job
, CommitBlockJob
, common
);
216 error_setg(errp
, QERR_INVALID_PARAMETER
, "speed");
219 ratelimit_set_speed(&s
->limit
, speed
/ BDRV_SECTOR_SIZE
, SLICE_TIME
);
222 static const BlockJobDriver commit_job_driver
= {
223 .instance_size
= sizeof(CommitBlockJob
),
224 .job_type
= BLOCK_JOB_TYPE_COMMIT
,
225 .set_speed
= commit_set_speed
,
229 static int coroutine_fn
bdrv_commit_top_preadv(BlockDriverState
*bs
,
230 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
232 return bdrv_co_preadv(bs
->backing
, offset
, bytes
, qiov
, flags
);
235 static void bdrv_commit_top_close(BlockDriverState
*bs
)
239 static void bdrv_commit_top_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
240 const BdrvChildRole
*role
,
241 uint64_t perm
, uint64_t shared
,
242 uint64_t *nperm
, uint64_t *nshared
)
245 *nshared
= BLK_PERM_ALL
;
248 /* Dummy node that provides consistent read to its users without requiring it
249 * from its backing file and that allows writes on the backing file chain. */
250 static BlockDriver bdrv_commit_top
= {
251 .format_name
= "commit_top",
252 .bdrv_co_preadv
= bdrv_commit_top_preadv
,
253 .bdrv_close
= bdrv_commit_top_close
,
254 .bdrv_child_perm
= bdrv_commit_top_child_perm
,
257 void commit_start(const char *job_id
, BlockDriverState
*bs
,
258 BlockDriverState
*base
, BlockDriverState
*top
, int64_t speed
,
259 BlockdevOnError on_error
, const char *backing_file_str
,
260 const char *filter_node_name
, Error
**errp
)
263 BlockReopenQueue
*reopen_queue
= NULL
;
264 int orig_overlay_flags
;
266 BlockDriverState
*iter
;
267 BlockDriverState
*overlay_bs
;
268 BlockDriverState
*commit_top_bs
= NULL
;
269 Error
*local_err
= NULL
;
274 error_setg(errp
, "Invalid files for merge: top and base are the same");
278 overlay_bs
= bdrv_find_overlay(bs
, top
);
280 if (overlay_bs
== NULL
) {
281 error_setg(errp
, "Could not find overlay image for %s:", top
->filename
);
285 s
= block_job_create(job_id
, &commit_job_driver
, bs
, 0, BLK_PERM_ALL
,
286 speed
, BLOCK_JOB_DEFAULT
, NULL
, NULL
, errp
);
291 orig_base_flags
= bdrv_get_flags(base
);
292 orig_overlay_flags
= bdrv_get_flags(overlay_bs
);
294 /* convert base & overlay_bs to r/w, if necessary */
295 if (!(orig_base_flags
& BDRV_O_RDWR
)) {
296 reopen_queue
= bdrv_reopen_queue(reopen_queue
, base
, NULL
,
297 orig_base_flags
| BDRV_O_RDWR
);
299 if (!(orig_overlay_flags
& BDRV_O_RDWR
)) {
300 reopen_queue
= bdrv_reopen_queue(reopen_queue
, overlay_bs
, NULL
,
301 orig_overlay_flags
| BDRV_O_RDWR
);
304 bdrv_reopen_multiple(bdrv_get_aio_context(bs
), reopen_queue
, &local_err
);
305 if (local_err
!= NULL
) {
306 error_propagate(errp
, local_err
);
311 /* Insert commit_top block node above top, so we can block consistent read
312 * on the backing chain below it */
313 commit_top_bs
= bdrv_new_open_driver(&bdrv_commit_top
, filter_node_name
, 0,
315 if (commit_top_bs
== NULL
) {
319 bdrv_set_backing_hd(commit_top_bs
, top
, &error_abort
);
320 bdrv_set_backing_hd(overlay_bs
, commit_top_bs
, &error_abort
);
322 s
->commit_top_bs
= commit_top_bs
;
323 bdrv_unref(commit_top_bs
);
325 /* Block all nodes between top and base, because they will
326 * disappear from the chain after this operation. */
327 assert(bdrv_chain_contains(top
, base
));
328 for (iter
= top
; iter
!= base
; iter
= backing_bs(iter
)) {
329 /* XXX BLK_PERM_WRITE needs to be allowed so we don't block ourselves
330 * at s->base (if writes are blocked for a node, they are also blocked
331 * for its backing file). The other options would be a second filter
332 * driver above s->base. */
333 ret
= block_job_add_bdrv(&s
->common
, "intermediate node", iter
, 0,
334 BLK_PERM_WRITE_UNCHANGED
| BLK_PERM_WRITE
,
341 ret
= block_job_add_bdrv(&s
->common
, "base", base
, 0, BLK_PERM_ALL
, errp
);
346 /* overlay_bs must be blocked because it needs to be modified to
347 * update the backing image string. */
348 ret
= block_job_add_bdrv(&s
->common
, "overlay of top", overlay_bs
,
349 BLK_PERM_GRAPH_MOD
, BLK_PERM_ALL
, errp
);
354 s
->base
= blk_new(BLK_PERM_CONSISTENT_READ
357 BLK_PERM_CONSISTENT_READ
359 | BLK_PERM_WRITE_UNCHANGED
);
360 ret
= blk_insert_bs(s
->base
, base
, errp
);
365 /* Required permissions are already taken with block_job_add_bdrv() */
366 s
->top
= blk_new(0, BLK_PERM_ALL
);
367 blk_insert_bs(s
->top
, top
, errp
);
374 s
->base_flags
= orig_base_flags
;
375 s
->orig_overlay_flags
= orig_overlay_flags
;
377 s
->backing_file_str
= g_strdup(backing_file_str
);
379 s
->on_error
= on_error
;
381 trace_commit_start(bs
, base
, top
, s
);
382 block_job_start(&s
->common
);
393 bdrv_set_backing_hd(overlay_bs
, top
, &error_abort
);
395 block_job_unref(&s
->common
);
399 #define COMMIT_BUF_SECTORS 2048
401 /* commit COW file into the raw image */
402 int bdrv_commit(BlockDriverState
*bs
)
404 BlockBackend
*src
, *backing
;
405 BlockDriverState
*backing_file_bs
= NULL
;
406 BlockDriverState
*commit_top_bs
= NULL
;
407 BlockDriver
*drv
= bs
->drv
;
408 int64_t sector
, total_sectors
, length
, backing_length
;
409 int n
, ro
, open_flags
;
412 Error
*local_err
= NULL
;
421 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, NULL
) ||
422 bdrv_op_is_blocked(bs
->backing
->bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, NULL
)) {
426 ro
= bs
->backing
->bs
->read_only
;
427 open_flags
= bs
->backing
->bs
->open_flags
;
430 if (bdrv_reopen(bs
->backing
->bs
, open_flags
| BDRV_O_RDWR
, NULL
)) {
435 src
= blk_new(BLK_PERM_CONSISTENT_READ
, BLK_PERM_ALL
);
436 backing
= blk_new(BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
);
438 ret
= blk_insert_bs(src
, bs
, &local_err
);
440 error_report_err(local_err
);
444 /* Insert commit_top block node above backing, so we can write to it */
445 backing_file_bs
= backing_bs(bs
);
447 commit_top_bs
= bdrv_new_open_driver(&bdrv_commit_top
, NULL
, BDRV_O_RDWR
,
449 if (commit_top_bs
== NULL
) {
450 error_report_err(local_err
);
454 bdrv_set_backing_hd(commit_top_bs
, backing_file_bs
, &error_abort
);
455 bdrv_set_backing_hd(bs
, commit_top_bs
, &error_abort
);
457 ret
= blk_insert_bs(backing
, backing_file_bs
, &local_err
);
459 error_report_err(local_err
);
463 length
= blk_getlength(src
);
469 backing_length
= blk_getlength(backing
);
470 if (backing_length
< 0) {
471 ret
= backing_length
;
475 /* If our top snapshot is larger than the backing file image,
476 * grow the backing file image if possible. If not possible,
477 * we must return an error */
478 if (length
> backing_length
) {
479 ret
= blk_truncate(backing
, length
);
485 total_sectors
= length
>> BDRV_SECTOR_BITS
;
487 /* blk_try_blockalign() for src will choose an alignment that works for
488 * backing as well, so no need to compare the alignment manually. */
489 buf
= blk_try_blockalign(src
, COMMIT_BUF_SECTORS
* BDRV_SECTOR_SIZE
);
495 for (sector
= 0; sector
< total_sectors
; sector
+= n
) {
496 ret
= bdrv_is_allocated(bs
, sector
, COMMIT_BUF_SECTORS
, &n
);
501 ret
= blk_pread(src
, sector
* BDRV_SECTOR_SIZE
, buf
,
502 n
* BDRV_SECTOR_SIZE
);
507 ret
= blk_pwrite(backing
, sector
* BDRV_SECTOR_SIZE
, buf
,
508 n
* BDRV_SECTOR_SIZE
, 0);
515 if (drv
->bdrv_make_empty
) {
516 ret
= drv
->bdrv_make_empty(bs
);
524 * Make sure all data we wrote to the backing device is actually
534 if (backing_file_bs
) {
535 bdrv_set_backing_hd(bs
, backing_file_bs
, &error_abort
);
537 bdrv_unref(commit_top_bs
);
541 /* ignoring error return here */
542 bdrv_reopen(bs
->backing
->bs
, open_flags
& ~BDRV_O_RDWR
, NULL
);