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.h"
20 #include "qapi/error.h"
21 #include "qapi/qmp/qerror.h"
22 #include "qemu/ratelimit.h"
23 #include "qemu/cutils.h"
24 #include "sysemu/block-backend.h"
25 #include "qemu/bitmap.h"
27 #define BACKUP_CLUSTER_SIZE_DEFAULT (1 << 16)
28 #define SLICE_TIME 100000000ULL /* ns */
30 typedef struct CowRequest
{
33 QLIST_ENTRY(CowRequest
) list
;
34 CoQueue wait_queue
; /* coroutines blocked on this request */
37 typedef struct BackupBlockJob
{
40 /* bitmap for sync=incremental */
41 BdrvDirtyBitmap
*sync_bitmap
;
42 MirrorSyncMode sync_mode
;
44 BlockdevOnError on_source_error
;
45 BlockdevOnError on_target_error
;
46 CoRwlock flush_rwlock
;
47 uint64_t sectors_read
;
48 unsigned long *done_bitmap
;
50 NotifierWithReturn before_write
;
51 QLIST_HEAD(, CowRequest
) inflight_reqs
;
54 /* Size of a cluster in sectors, instead of bytes. */
55 static inline int64_t cluster_size_sectors(BackupBlockJob
*job
)
57 return job
->cluster_size
/ BDRV_SECTOR_SIZE
;
60 /* See if in-flight requests overlap and wait for them to complete */
61 static void coroutine_fn
wait_for_overlapping_requests(BackupBlockJob
*job
,
70 QLIST_FOREACH(req
, &job
->inflight_reqs
, list
) {
71 if (end
> req
->start
&& start
< req
->end
) {
72 qemu_co_queue_wait(&req
->wait_queue
);
80 /* Keep track of an in-flight request */
81 static void cow_request_begin(CowRequest
*req
, BackupBlockJob
*job
,
82 int64_t start
, int64_t end
)
86 qemu_co_queue_init(&req
->wait_queue
);
87 QLIST_INSERT_HEAD(&job
->inflight_reqs
, req
, list
);
90 /* Forget about a completed request */
91 static void cow_request_end(CowRequest
*req
)
93 QLIST_REMOVE(req
, list
);
94 qemu_co_queue_restart_all(&req
->wait_queue
);
97 static int coroutine_fn
backup_do_cow(BackupBlockJob
*job
,
98 int64_t sector_num
, int nb_sectors
,
100 bool is_write_notifier
)
102 BlockBackend
*blk
= job
->common
.blk
;
103 CowRequest cow_request
;
105 QEMUIOVector bounce_qiov
;
106 void *bounce_buffer
= NULL
;
108 int64_t sectors_per_cluster
= cluster_size_sectors(job
);
112 qemu_co_rwlock_rdlock(&job
->flush_rwlock
);
114 start
= sector_num
/ sectors_per_cluster
;
115 end
= DIV_ROUND_UP(sector_num
+ nb_sectors
, sectors_per_cluster
);
117 trace_backup_do_cow_enter(job
, start
, sector_num
, nb_sectors
);
119 wait_for_overlapping_requests(job
, start
, end
);
120 cow_request_begin(&cow_request
, job
, start
, end
);
122 for (; start
< end
; start
++) {
123 if (test_bit(start
, job
->done_bitmap
)) {
124 trace_backup_do_cow_skip(job
, start
);
125 continue; /* already copied */
128 trace_backup_do_cow_process(job
, start
);
130 n
= MIN(sectors_per_cluster
,
131 job
->common
.len
/ BDRV_SECTOR_SIZE
-
132 start
* sectors_per_cluster
);
134 if (!bounce_buffer
) {
135 bounce_buffer
= blk_blockalign(blk
, job
->cluster_size
);
137 iov
.iov_base
= bounce_buffer
;
138 iov
.iov_len
= n
* BDRV_SECTOR_SIZE
;
139 qemu_iovec_init_external(&bounce_qiov
, &iov
, 1);
141 ret
= blk_co_preadv(blk
, start
* job
->cluster_size
,
142 bounce_qiov
.size
, &bounce_qiov
,
143 is_write_notifier
? BDRV_REQ_NO_SERIALISING
: 0);
145 trace_backup_do_cow_read_fail(job
, start
, ret
);
147 *error_is_read
= true;
152 if (buffer_is_zero(iov
.iov_base
, iov
.iov_len
)) {
153 ret
= blk_co_pwrite_zeroes(job
->target
, start
* job
->cluster_size
,
154 bounce_qiov
.size
, BDRV_REQ_MAY_UNMAP
);
156 ret
= blk_co_pwritev(job
->target
, start
* job
->cluster_size
,
157 bounce_qiov
.size
, &bounce_qiov
, 0);
160 trace_backup_do_cow_write_fail(job
, start
, ret
);
162 *error_is_read
= false;
167 set_bit(start
, job
->done_bitmap
);
169 /* Publish progress, guest I/O counts as progress too. Note that the
170 * offset field is an opaque progress value, it is not a disk offset.
172 job
->sectors_read
+= n
;
173 job
->common
.offset
+= n
* BDRV_SECTOR_SIZE
;
178 qemu_vfree(bounce_buffer
);
181 cow_request_end(&cow_request
);
183 trace_backup_do_cow_return(job
, sector_num
, nb_sectors
, ret
);
185 qemu_co_rwlock_unlock(&job
->flush_rwlock
);
190 static int coroutine_fn
backup_before_write_notify(
191 NotifierWithReturn
*notifier
,
194 BackupBlockJob
*job
= container_of(notifier
, BackupBlockJob
, before_write
);
195 BdrvTrackedRequest
*req
= opaque
;
196 int64_t sector_num
= req
->offset
>> BDRV_SECTOR_BITS
;
197 int nb_sectors
= req
->bytes
>> BDRV_SECTOR_BITS
;
199 assert(req
->bs
== blk_bs(job
->common
.blk
));
200 assert((req
->offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
201 assert((req
->bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
203 return backup_do_cow(job
, sector_num
, nb_sectors
, NULL
, true);
206 static void backup_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
208 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
211 error_setg(errp
, QERR_INVALID_PARAMETER
, "speed");
214 ratelimit_set_speed(&s
->limit
, speed
/ BDRV_SECTOR_SIZE
, SLICE_TIME
);
217 static void backup_cleanup_sync_bitmap(BackupBlockJob
*job
, int ret
)
220 BlockDriverState
*bs
= blk_bs(job
->common
.blk
);
222 if (ret
< 0 || block_job_is_cancelled(&job
->common
)) {
223 /* Merge the successor back into the parent, delete nothing. */
224 bm
= bdrv_reclaim_dirty_bitmap(bs
, job
->sync_bitmap
, NULL
);
227 /* Everything is fine, delete this bitmap and install the backup. */
228 bm
= bdrv_dirty_bitmap_abdicate(bs
, job
->sync_bitmap
, NULL
);
233 static void backup_commit(BlockJob
*job
)
235 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
236 if (s
->sync_bitmap
) {
237 backup_cleanup_sync_bitmap(s
, 0);
241 static void backup_abort(BlockJob
*job
)
243 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
244 if (s
->sync_bitmap
) {
245 backup_cleanup_sync_bitmap(s
, -1);
249 static void backup_attached_aio_context(BlockJob
*job
, AioContext
*aio_context
)
251 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
253 blk_set_aio_context(s
->target
, aio_context
);
256 static const BlockJobDriver backup_job_driver
= {
257 .instance_size
= sizeof(BackupBlockJob
),
258 .job_type
= BLOCK_JOB_TYPE_BACKUP
,
259 .set_speed
= backup_set_speed
,
260 .commit
= backup_commit
,
261 .abort
= backup_abort
,
262 .attached_aio_context
= backup_attached_aio_context
,
265 static BlockErrorAction
backup_error_action(BackupBlockJob
*job
,
266 bool read
, int error
)
269 return block_job_error_action(&job
->common
, job
->on_source_error
,
272 return block_job_error_action(&job
->common
, job
->on_target_error
,
279 } BackupCompleteData
;
281 static void backup_complete(BlockJob
*job
, void *opaque
)
283 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
284 BackupCompleteData
*data
= opaque
;
286 blk_unref(s
->target
);
288 block_job_completed(job
, data
->ret
);
292 static bool coroutine_fn
yield_and_check(BackupBlockJob
*job
)
294 if (block_job_is_cancelled(&job
->common
)) {
298 /* we need to yield so that bdrv_drain_all() returns.
299 * (without, VM does not reboot)
301 if (job
->common
.speed
) {
302 uint64_t delay_ns
= ratelimit_calculate_delay(&job
->limit
,
304 job
->sectors_read
= 0;
305 block_job_sleep_ns(&job
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
307 block_job_sleep_ns(&job
->common
, QEMU_CLOCK_REALTIME
, 0);
310 if (block_job_is_cancelled(&job
->common
)) {
317 static int coroutine_fn
backup_run_incremental(BackupBlockJob
*job
)
321 int clusters_per_iter
;
322 uint32_t granularity
;
326 int64_t last_cluster
= -1;
327 int64_t sectors_per_cluster
= cluster_size_sectors(job
);
330 granularity
= bdrv_dirty_bitmap_granularity(job
->sync_bitmap
);
331 clusters_per_iter
= MAX((granularity
/ job
->cluster_size
), 1);
332 bdrv_dirty_iter_init(job
->sync_bitmap
, &hbi
);
334 /* Find the next dirty sector(s) */
335 while ((sector
= hbitmap_iter_next(&hbi
)) != -1) {
336 cluster
= sector
/ sectors_per_cluster
;
338 /* Fake progress updates for any clusters we skipped */
339 if (cluster
!= last_cluster
+ 1) {
340 job
->common
.offset
+= ((cluster
- last_cluster
- 1) *
344 for (end
= cluster
+ clusters_per_iter
; cluster
< end
; cluster
++) {
346 if (yield_and_check(job
)) {
349 ret
= backup_do_cow(job
, cluster
* sectors_per_cluster
,
350 sectors_per_cluster
, &error_is_read
,
353 backup_error_action(job
, error_is_read
, -ret
) ==
354 BLOCK_ERROR_ACTION_REPORT
) {
360 /* If the bitmap granularity is smaller than the backup granularity,
361 * we need to advance the iterator pointer to the next cluster. */
362 if (granularity
< job
->cluster_size
) {
363 bdrv_set_dirty_iter(&hbi
, cluster
* sectors_per_cluster
);
366 last_cluster
= cluster
- 1;
369 /* Play some final catchup with the progress meter */
370 end
= DIV_ROUND_UP(job
->common
.len
, job
->cluster_size
);
371 if (last_cluster
+ 1 < end
) {
372 job
->common
.offset
+= ((end
- last_cluster
- 1) * job
->cluster_size
);
378 static void coroutine_fn
backup_run(void *opaque
)
380 BackupBlockJob
*job
= opaque
;
381 BackupCompleteData
*data
;
382 BlockDriverState
*bs
= blk_bs(job
->common
.blk
);
383 BlockBackend
*target
= job
->target
;
385 int64_t sectors_per_cluster
= cluster_size_sectors(job
);
388 QLIST_INIT(&job
->inflight_reqs
);
389 qemu_co_rwlock_init(&job
->flush_rwlock
);
392 end
= DIV_ROUND_UP(job
->common
.len
, job
->cluster_size
);
394 job
->done_bitmap
= bitmap_new(end
);
396 job
->before_write
.notify
= backup_before_write_notify
;
397 bdrv_add_before_write_notifier(bs
, &job
->before_write
);
399 if (job
->sync_mode
== MIRROR_SYNC_MODE_NONE
) {
400 while (!block_job_is_cancelled(&job
->common
)) {
401 /* Yield until the job is cancelled. We just let our before_write
402 * notify callback service CoW requests. */
403 block_job_yield(&job
->common
);
405 } else if (job
->sync_mode
== MIRROR_SYNC_MODE_INCREMENTAL
) {
406 ret
= backup_run_incremental(job
);
408 /* Both FULL and TOP SYNC_MODE's require copying.. */
409 for (; start
< end
; start
++) {
411 if (yield_and_check(job
)) {
415 if (job
->sync_mode
== MIRROR_SYNC_MODE_TOP
) {
419 /* Check to see if these blocks are already in the
422 for (i
= 0; i
< sectors_per_cluster
;) {
423 /* bdrv_is_allocated() only returns true/false based
424 * on the first set of sectors it comes across that
425 * are are all in the same state.
426 * For that reason we must verify each sector in the
427 * backup cluster length. We end up copying more than
428 * needed but at some point that is always the case. */
430 bdrv_is_allocated(bs
,
431 start
* sectors_per_cluster
+ i
,
432 sectors_per_cluster
- i
, &n
);
435 if (alloced
== 1 || n
== 0) {
440 /* If the above loop never found any sectors that are in
441 * the topmost image, skip this backup. */
446 /* FULL sync mode we copy the whole drive. */
447 ret
= backup_do_cow(job
, start
* sectors_per_cluster
,
448 sectors_per_cluster
, &error_is_read
, false);
450 /* Depending on error action, fail now or retry cluster */
451 BlockErrorAction action
=
452 backup_error_action(job
, error_is_read
, -ret
);
453 if (action
== BLOCK_ERROR_ACTION_REPORT
) {
463 notifier_with_return_remove(&job
->before_write
);
465 /* wait until pending backup_do_cow() calls have completed */
466 qemu_co_rwlock_wrlock(&job
->flush_rwlock
);
467 qemu_co_rwlock_unlock(&job
->flush_rwlock
);
468 g_free(job
->done_bitmap
);
470 bdrv_op_unblock_all(blk_bs(target
), job
->common
.blocker
);
472 data
= g_malloc(sizeof(*data
));
474 block_job_defer_to_main_loop(&job
->common
, backup_complete
, data
);
477 void backup_start(BlockDriverState
*bs
, BlockDriverState
*target
,
478 int64_t speed
, MirrorSyncMode sync_mode
,
479 BdrvDirtyBitmap
*sync_bitmap
,
480 BlockdevOnError on_source_error
,
481 BlockdevOnError on_target_error
,
482 BlockCompletionFunc
*cb
, void *opaque
,
483 BlockJobTxn
*txn
, Error
**errp
)
487 BackupBlockJob
*job
= NULL
;
495 error_setg(errp
, "Source and target cannot be the same");
499 if (!bdrv_is_inserted(bs
)) {
500 error_setg(errp
, "Device is not inserted: %s",
501 bdrv_get_device_name(bs
));
505 if (!bdrv_is_inserted(target
)) {
506 error_setg(errp
, "Device is not inserted: %s",
507 bdrv_get_device_name(target
));
511 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
515 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_BACKUP_TARGET
, errp
)) {
519 if (sync_mode
== MIRROR_SYNC_MODE_INCREMENTAL
) {
521 error_setg(errp
, "must provide a valid bitmap name for "
522 "\"incremental\" sync mode");
526 /* Create a new bitmap, and freeze/disable this one. */
527 if (bdrv_dirty_bitmap_create_successor(bs
, sync_bitmap
, errp
) < 0) {
530 } else if (sync_bitmap
) {
532 "a sync_bitmap was provided to backup_run, "
533 "but received an incompatible sync_mode (%s)",
534 MirrorSyncMode_lookup
[sync_mode
]);
538 len
= bdrv_getlength(bs
);
540 error_setg_errno(errp
, -len
, "unable to get length for '%s'",
541 bdrv_get_device_name(bs
));
545 job
= block_job_create(&backup_job_driver
, bs
, speed
, cb
, opaque
, errp
);
550 job
->target
= blk_new();
551 blk_insert_bs(job
->target
, target
);
553 job
->on_source_error
= on_source_error
;
554 job
->on_target_error
= on_target_error
;
555 job
->sync_mode
= sync_mode
;
556 job
->sync_bitmap
= sync_mode
== MIRROR_SYNC_MODE_INCREMENTAL
?
559 /* If there is no backing file on the target, we cannot rely on COW if our
560 * backup cluster size is smaller than the target cluster size. Even for
561 * targets with a backing file, try to avoid COW if possible. */
562 ret
= bdrv_get_info(target
, &bdi
);
563 if (ret
< 0 && !target
->backing
) {
564 error_setg_errno(errp
, -ret
,
565 "Couldn't determine the cluster size of the target image, "
566 "which has no backing file");
567 error_append_hint(errp
,
568 "Aborting, since this may create an unusable destination image\n");
570 } else if (ret
< 0 && target
->backing
) {
571 /* Not fatal; just trudge on ahead. */
572 job
->cluster_size
= BACKUP_CLUSTER_SIZE_DEFAULT
;
574 job
->cluster_size
= MAX(BACKUP_CLUSTER_SIZE_DEFAULT
, bdi
.cluster_size
);
577 bdrv_op_block_all(target
, job
->common
.blocker
);
578 job
->common
.len
= len
;
579 job
->common
.co
= qemu_coroutine_create(backup_run
);
580 block_job_txn_add_job(txn
, &job
->common
);
581 qemu_coroutine_enter(job
->common
.co
, job
);
586 bdrv_reclaim_dirty_bitmap(bs
, sync_bitmap
, NULL
);
589 blk_unref(job
->target
);
590 block_job_unref(&job
->common
);