4 * Copyright (C) 2013 Proxmox Server Solutions
7 * Dietmar Maurer (dietmar@proxmox.com)
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
17 #include "block/block.h"
18 #include "block/block_int.h"
19 #include "block/blockjob_int.h"
20 #include "block/block_backup.h"
21 #include "qapi/error.h"
22 #include "qapi/qmp/qerror.h"
23 #include "qemu/ratelimit.h"
24 #include "qemu/cutils.h"
25 #include "sysemu/block-backend.h"
26 #include "qemu/bitmap.h"
27 #include "qemu/error-report.h"
29 #define BACKUP_CLUSTER_SIZE_DEFAULT (1 << 16)
30 #define SLICE_TIME 100000000ULL /* ns */
32 typedef struct BackupBlockJob
{
35 /* bitmap for sync=incremental */
36 BdrvDirtyBitmap
*sync_bitmap
;
37 MirrorSyncMode sync_mode
;
39 BlockdevOnError on_source_error
;
40 BlockdevOnError on_target_error
;
41 CoRwlock flush_rwlock
;
43 unsigned long *done_bitmap
;
46 NotifierWithReturn before_write
;
47 QLIST_HEAD(, CowRequest
) inflight_reqs
;
50 /* See if in-flight requests overlap and wait for them to complete */
51 static void coroutine_fn
wait_for_overlapping_requests(BackupBlockJob
*job
,
60 QLIST_FOREACH(req
, &job
->inflight_reqs
, list
) {
61 if (end
> req
->start_byte
&& start
< req
->end_byte
) {
62 qemu_co_queue_wait(&req
->wait_queue
, NULL
);
70 /* Keep track of an in-flight request */
71 static void cow_request_begin(CowRequest
*req
, BackupBlockJob
*job
,
72 int64_t start
, int64_t end
)
74 req
->start_byte
= start
;
76 qemu_co_queue_init(&req
->wait_queue
);
77 QLIST_INSERT_HEAD(&job
->inflight_reqs
, req
, list
);
80 /* Forget about a completed request */
81 static void cow_request_end(CowRequest
*req
)
83 QLIST_REMOVE(req
, list
);
84 qemu_co_queue_restart_all(&req
->wait_queue
);
87 static int coroutine_fn
backup_do_cow(BackupBlockJob
*job
,
88 int64_t offset
, uint64_t bytes
,
90 bool is_write_notifier
)
92 BlockBackend
*blk
= job
->common
.blk
;
93 CowRequest cow_request
;
95 QEMUIOVector bounce_qiov
;
96 void *bounce_buffer
= NULL
;
98 int64_t start
, end
; /* bytes */
101 qemu_co_rwlock_rdlock(&job
->flush_rwlock
);
103 start
= QEMU_ALIGN_DOWN(offset
, job
->cluster_size
);
104 end
= QEMU_ALIGN_UP(bytes
+ offset
, job
->cluster_size
);
106 trace_backup_do_cow_enter(job
, start
, offset
, bytes
);
108 wait_for_overlapping_requests(job
, start
, end
);
109 cow_request_begin(&cow_request
, job
, start
, end
);
111 for (; start
< end
; start
+= job
->cluster_size
) {
112 if (test_bit(start
/ job
->cluster_size
, job
->done_bitmap
)) {
113 trace_backup_do_cow_skip(job
, start
);
114 continue; /* already copied */
117 trace_backup_do_cow_process(job
, start
);
119 n
= MIN(job
->cluster_size
, job
->common
.len
- start
);
121 if (!bounce_buffer
) {
122 bounce_buffer
= blk_blockalign(blk
, job
->cluster_size
);
124 iov
.iov_base
= bounce_buffer
;
126 qemu_iovec_init_external(&bounce_qiov
, &iov
, 1);
128 ret
= blk_co_preadv(blk
, start
, bounce_qiov
.size
, &bounce_qiov
,
129 is_write_notifier
? BDRV_REQ_NO_SERIALISING
: 0);
131 trace_backup_do_cow_read_fail(job
, start
, ret
);
133 *error_is_read
= true;
138 if (buffer_is_zero(iov
.iov_base
, iov
.iov_len
)) {
139 ret
= blk_co_pwrite_zeroes(job
->target
, start
,
140 bounce_qiov
.size
, BDRV_REQ_MAY_UNMAP
);
142 ret
= blk_co_pwritev(job
->target
, start
,
143 bounce_qiov
.size
, &bounce_qiov
,
144 job
->compress
? BDRV_REQ_WRITE_COMPRESSED
: 0);
147 trace_backup_do_cow_write_fail(job
, start
, ret
);
149 *error_is_read
= false;
154 set_bit(start
/ job
->cluster_size
, job
->done_bitmap
);
156 /* Publish progress, guest I/O counts as progress too. Note that the
157 * offset field is an opaque progress value, it is not a disk offset.
159 job
->bytes_read
+= n
;
160 job
->common
.offset
+= n
;
165 qemu_vfree(bounce_buffer
);
168 cow_request_end(&cow_request
);
170 trace_backup_do_cow_return(job
, offset
, bytes
, ret
);
172 qemu_co_rwlock_unlock(&job
->flush_rwlock
);
177 static int coroutine_fn
backup_before_write_notify(
178 NotifierWithReturn
*notifier
,
181 BackupBlockJob
*job
= container_of(notifier
, BackupBlockJob
, before_write
);
182 BdrvTrackedRequest
*req
= opaque
;
184 assert(req
->bs
== blk_bs(job
->common
.blk
));
185 assert(QEMU_IS_ALIGNED(req
->offset
, BDRV_SECTOR_SIZE
));
186 assert(QEMU_IS_ALIGNED(req
->bytes
, BDRV_SECTOR_SIZE
));
188 return backup_do_cow(job
, req
->offset
, req
->bytes
, NULL
, true);
191 static void backup_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
193 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
196 error_setg(errp
, QERR_INVALID_PARAMETER
, "speed");
199 ratelimit_set_speed(&s
->limit
, speed
, SLICE_TIME
);
202 static void backup_cleanup_sync_bitmap(BackupBlockJob
*job
, int ret
)
205 BlockDriverState
*bs
= blk_bs(job
->common
.blk
);
207 if (ret
< 0 || block_job_is_cancelled(&job
->common
)) {
208 /* Merge the successor back into the parent, delete nothing. */
209 bm
= bdrv_reclaim_dirty_bitmap(bs
, job
->sync_bitmap
, NULL
);
212 /* Everything is fine, delete this bitmap and install the backup. */
213 bm
= bdrv_dirty_bitmap_abdicate(bs
, job
->sync_bitmap
, NULL
);
218 static void backup_commit(BlockJob
*job
)
220 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
221 if (s
->sync_bitmap
) {
222 backup_cleanup_sync_bitmap(s
, 0);
226 static void backup_abort(BlockJob
*job
)
228 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
229 if (s
->sync_bitmap
) {
230 backup_cleanup_sync_bitmap(s
, -1);
234 static void backup_clean(BlockJob
*job
)
236 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
238 blk_unref(s
->target
);
242 static void backup_attached_aio_context(BlockJob
*job
, AioContext
*aio_context
)
244 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
246 blk_set_aio_context(s
->target
, aio_context
);
249 void backup_do_checkpoint(BlockJob
*job
, Error
**errp
)
251 BackupBlockJob
*backup_job
= container_of(job
, BackupBlockJob
, common
);
254 assert(job
->driver
->job_type
== BLOCK_JOB_TYPE_BACKUP
);
256 if (backup_job
->sync_mode
!= MIRROR_SYNC_MODE_NONE
) {
257 error_setg(errp
, "The backup job only supports block checkpoint in"
262 len
= DIV_ROUND_UP(backup_job
->common
.len
, backup_job
->cluster_size
);
263 bitmap_zero(backup_job
->done_bitmap
, len
);
266 void backup_wait_for_overlapping_requests(BlockJob
*job
, int64_t offset
,
269 BackupBlockJob
*backup_job
= container_of(job
, BackupBlockJob
, common
);
272 assert(job
->driver
->job_type
== BLOCK_JOB_TYPE_BACKUP
);
274 start
= QEMU_ALIGN_DOWN(offset
, backup_job
->cluster_size
);
275 end
= QEMU_ALIGN_UP(offset
+ bytes
, backup_job
->cluster_size
);
276 wait_for_overlapping_requests(backup_job
, start
, end
);
279 void backup_cow_request_begin(CowRequest
*req
, BlockJob
*job
,
280 int64_t offset
, uint64_t bytes
)
282 BackupBlockJob
*backup_job
= container_of(job
, BackupBlockJob
, common
);
285 assert(job
->driver
->job_type
== BLOCK_JOB_TYPE_BACKUP
);
287 start
= QEMU_ALIGN_DOWN(offset
, backup_job
->cluster_size
);
288 end
= QEMU_ALIGN_UP(offset
+ bytes
, backup_job
->cluster_size
);
289 cow_request_begin(req
, backup_job
, start
, end
);
292 void backup_cow_request_end(CowRequest
*req
)
294 cow_request_end(req
);
297 static void backup_drain(BlockJob
*job
)
299 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
301 /* Need to keep a reference in case blk_drain triggers execution
302 * of backup_complete...
305 BlockBackend
*target
= s
->target
;
312 static BlockErrorAction
backup_error_action(BackupBlockJob
*job
,
313 bool read
, int error
)
316 return block_job_error_action(&job
->common
, job
->on_source_error
,
319 return block_job_error_action(&job
->common
, job
->on_target_error
,
326 } BackupCompleteData
;
328 static void backup_complete(BlockJob
*job
, void *opaque
)
330 BackupCompleteData
*data
= opaque
;
332 block_job_completed(job
, data
->ret
);
336 static bool coroutine_fn
yield_and_check(BackupBlockJob
*job
)
338 if (block_job_is_cancelled(&job
->common
)) {
342 /* we need to yield so that bdrv_drain_all() returns.
343 * (without, VM does not reboot)
345 if (job
->common
.speed
) {
346 uint64_t delay_ns
= ratelimit_calculate_delay(&job
->limit
,
349 block_job_sleep_ns(&job
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
351 block_job_sleep_ns(&job
->common
, QEMU_CLOCK_REALTIME
, 0);
354 if (block_job_is_cancelled(&job
->common
)) {
361 static int coroutine_fn
backup_run_incremental(BackupBlockJob
*job
)
365 int clusters_per_iter
;
366 uint32_t granularity
;
370 int64_t last_cluster
= -1;
371 BdrvDirtyBitmapIter
*dbi
;
373 granularity
= bdrv_dirty_bitmap_granularity(job
->sync_bitmap
);
374 clusters_per_iter
= MAX((granularity
/ job
->cluster_size
), 1);
375 dbi
= bdrv_dirty_iter_new(job
->sync_bitmap
);
377 /* Find the next dirty sector(s) */
378 while ((offset
= bdrv_dirty_iter_next(dbi
)) >= 0) {
379 cluster
= offset
/ job
->cluster_size
;
381 /* Fake progress updates for any clusters we skipped */
382 if (cluster
!= last_cluster
+ 1) {
383 job
->common
.offset
+= ((cluster
- last_cluster
- 1) *
387 for (end
= cluster
+ clusters_per_iter
; cluster
< end
; cluster
++) {
389 if (yield_and_check(job
)) {
392 ret
= backup_do_cow(job
, cluster
* job
->cluster_size
,
393 job
->cluster_size
, &error_is_read
,
396 backup_error_action(job
, error_is_read
, -ret
) ==
397 BLOCK_ERROR_ACTION_REPORT
) {
403 /* If the bitmap granularity is smaller than the backup granularity,
404 * we need to advance the iterator pointer to the next cluster. */
405 if (granularity
< job
->cluster_size
) {
406 bdrv_set_dirty_iter(dbi
, cluster
* job
->cluster_size
);
409 last_cluster
= cluster
- 1;
412 /* Play some final catchup with the progress meter */
413 end
= DIV_ROUND_UP(job
->common
.len
, job
->cluster_size
);
414 if (last_cluster
+ 1 < end
) {
415 job
->common
.offset
+= ((end
- last_cluster
- 1) * job
->cluster_size
);
419 bdrv_dirty_iter_free(dbi
);
423 static void coroutine_fn
backup_run(void *opaque
)
425 BackupBlockJob
*job
= opaque
;
426 BackupCompleteData
*data
;
427 BlockDriverState
*bs
= blk_bs(job
->common
.blk
);
431 QLIST_INIT(&job
->inflight_reqs
);
432 qemu_co_rwlock_init(&job
->flush_rwlock
);
434 job
->done_bitmap
= bitmap_new(DIV_ROUND_UP(job
->common
.len
,
437 job
->before_write
.notify
= backup_before_write_notify
;
438 bdrv_add_before_write_notifier(bs
, &job
->before_write
);
440 if (job
->sync_mode
== MIRROR_SYNC_MODE_NONE
) {
441 while (!block_job_is_cancelled(&job
->common
)) {
442 /* Yield until the job is cancelled. We just let our before_write
443 * notify callback service CoW requests. */
444 block_job_yield(&job
->common
);
446 } else if (job
->sync_mode
== MIRROR_SYNC_MODE_INCREMENTAL
) {
447 ret
= backup_run_incremental(job
);
449 /* Both FULL and TOP SYNC_MODE's require copying.. */
450 for (offset
= 0; offset
< job
->common
.len
;
451 offset
+= job
->cluster_size
) {
455 if (yield_and_check(job
)) {
459 if (job
->sync_mode
== MIRROR_SYNC_MODE_TOP
) {
463 /* Check to see if these blocks are already in the
466 for (i
= 0; i
< job
->cluster_size
;) {
467 /* bdrv_is_allocated() only returns true/false based
468 * on the first set of sectors it comes across that
469 * are are all in the same state.
470 * For that reason we must verify each sector in the
471 * backup cluster length. We end up copying more than
472 * needed but at some point that is always the case. */
474 bdrv_is_allocated(bs
, offset
+ i
,
475 job
->cluster_size
- i
, &n
);
478 if (alloced
|| n
== 0) {
483 /* If the above loop never found any sectors that are in
484 * the topmost image, skip this backup. */
489 /* FULL sync mode we copy the whole drive. */
493 ret
= backup_do_cow(job
, offset
, job
->cluster_size
,
494 &error_is_read
, false);
497 /* Depending on error action, fail now or retry cluster */
498 BlockErrorAction action
=
499 backup_error_action(job
, error_is_read
, -ret
);
500 if (action
== BLOCK_ERROR_ACTION_REPORT
) {
503 offset
-= job
->cluster_size
;
510 notifier_with_return_remove(&job
->before_write
);
512 /* wait until pending backup_do_cow() calls have completed */
513 qemu_co_rwlock_wrlock(&job
->flush_rwlock
);
514 qemu_co_rwlock_unlock(&job
->flush_rwlock
);
515 g_free(job
->done_bitmap
);
517 data
= g_malloc(sizeof(*data
));
519 block_job_defer_to_main_loop(&job
->common
, backup_complete
, data
);
522 static const BlockJobDriver backup_job_driver
= {
523 .instance_size
= sizeof(BackupBlockJob
),
524 .job_type
= BLOCK_JOB_TYPE_BACKUP
,
526 .set_speed
= backup_set_speed
,
527 .commit
= backup_commit
,
528 .abort
= backup_abort
,
529 .clean
= backup_clean
,
530 .attached_aio_context
= backup_attached_aio_context
,
531 .drain
= backup_drain
,
534 BlockJob
*backup_job_create(const char *job_id
, BlockDriverState
*bs
,
535 BlockDriverState
*target
, int64_t speed
,
536 MirrorSyncMode sync_mode
, BdrvDirtyBitmap
*sync_bitmap
,
538 BlockdevOnError on_source_error
,
539 BlockdevOnError on_target_error
,
541 BlockCompletionFunc
*cb
, void *opaque
,
542 BlockJobTxn
*txn
, Error
**errp
)
546 BackupBlockJob
*job
= NULL
;
553 error_setg(errp
, "Source and target cannot be the same");
557 if (!bdrv_is_inserted(bs
)) {
558 error_setg(errp
, "Device is not inserted: %s",
559 bdrv_get_device_name(bs
));
563 if (!bdrv_is_inserted(target
)) {
564 error_setg(errp
, "Device is not inserted: %s",
565 bdrv_get_device_name(target
));
569 if (compress
&& target
->drv
->bdrv_co_pwritev_compressed
== NULL
) {
570 error_setg(errp
, "Compression is not supported for this drive %s",
571 bdrv_get_device_name(target
));
575 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
579 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_BACKUP_TARGET
, errp
)) {
583 if (sync_mode
== MIRROR_SYNC_MODE_INCREMENTAL
) {
585 error_setg(errp
, "must provide a valid bitmap name for "
586 "\"incremental\" sync mode");
590 /* Create a new bitmap, and freeze/disable this one. */
591 if (bdrv_dirty_bitmap_create_successor(bs
, sync_bitmap
, errp
) < 0) {
594 } else if (sync_bitmap
) {
596 "a sync_bitmap was provided to backup_run, "
597 "but received an incompatible sync_mode (%s)",
598 MirrorSyncMode_str(sync_mode
));
602 len
= bdrv_getlength(bs
);
604 error_setg_errno(errp
, -len
, "unable to get length for '%s'",
605 bdrv_get_device_name(bs
));
609 /* job->common.len is fixed, so we can't allow resize */
610 job
= block_job_create(job_id
, &backup_job_driver
, bs
,
611 BLK_PERM_CONSISTENT_READ
,
612 BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
|
613 BLK_PERM_WRITE_UNCHANGED
| BLK_PERM_GRAPH_MOD
,
614 speed
, creation_flags
, cb
, opaque
, errp
);
619 /* The target must match the source in size, so no resize here either */
620 job
->target
= blk_new(BLK_PERM_WRITE
,
621 BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
|
622 BLK_PERM_WRITE_UNCHANGED
| BLK_PERM_GRAPH_MOD
);
623 ret
= blk_insert_bs(job
->target
, target
, errp
);
628 job
->on_source_error
= on_source_error
;
629 job
->on_target_error
= on_target_error
;
630 job
->sync_mode
= sync_mode
;
631 job
->sync_bitmap
= sync_mode
== MIRROR_SYNC_MODE_INCREMENTAL
?
633 job
->compress
= compress
;
635 /* If there is no backing file on the target, we cannot rely on COW if our
636 * backup cluster size is smaller than the target cluster size. Even for
637 * targets with a backing file, try to avoid COW if possible. */
638 ret
= bdrv_get_info(target
, &bdi
);
639 if (ret
== -ENOTSUP
&& !target
->backing
) {
640 /* Cluster size is not defined */
641 warn_report("The target block device doesn't provide "
642 "information about the block size and it doesn't have a "
643 "backing file. The default block size of %u bytes is "
644 "used. If the actual block size of the target exceeds "
645 "this default, the backup may be unusable",
646 BACKUP_CLUSTER_SIZE_DEFAULT
);
647 job
->cluster_size
= BACKUP_CLUSTER_SIZE_DEFAULT
;
648 } else if (ret
< 0 && !target
->backing
) {
649 error_setg_errno(errp
, -ret
,
650 "Couldn't determine the cluster size of the target image, "
651 "which has no backing file");
652 error_append_hint(errp
,
653 "Aborting, since this may create an unusable destination image\n");
655 } else if (ret
< 0 && target
->backing
) {
656 /* Not fatal; just trudge on ahead. */
657 job
->cluster_size
= BACKUP_CLUSTER_SIZE_DEFAULT
;
659 job
->cluster_size
= MAX(BACKUP_CLUSTER_SIZE_DEFAULT
, bdi
.cluster_size
);
662 /* Required permissions are already taken with target's blk_new() */
663 block_job_add_bdrv(&job
->common
, "target", target
, 0, BLK_PERM_ALL
,
665 job
->common
.len
= len
;
666 block_job_txn_add_job(txn
, &job
->common
);
672 bdrv_reclaim_dirty_bitmap(bs
, sync_bitmap
, NULL
);
675 backup_clean(&job
->common
);
676 block_job_early_fail(&job
->common
);