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
{
39 BlockDriverState
*target
;
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 QLIST_HEAD(, CowRequest
) inflight_reqs
;
53 /* Size of a cluster in sectors, instead of bytes. */
54 static inline int64_t cluster_size_sectors(BackupBlockJob
*job
)
56 return job
->cluster_size
/ BDRV_SECTOR_SIZE
;
59 /* See if in-flight requests overlap and wait for them to complete */
60 static void coroutine_fn
wait_for_overlapping_requests(BackupBlockJob
*job
,
69 QLIST_FOREACH(req
, &job
->inflight_reqs
, list
) {
70 if (end
> req
->start
&& start
< req
->end
) {
71 qemu_co_queue_wait(&req
->wait_queue
);
79 /* Keep track of an in-flight request */
80 static void cow_request_begin(CowRequest
*req
, BackupBlockJob
*job
,
81 int64_t start
, int64_t end
)
85 qemu_co_queue_init(&req
->wait_queue
);
86 QLIST_INSERT_HEAD(&job
->inflight_reqs
, req
, list
);
89 /* Forget about a completed request */
90 static void cow_request_end(CowRequest
*req
)
92 QLIST_REMOVE(req
, list
);
93 qemu_co_queue_restart_all(&req
->wait_queue
);
96 static int coroutine_fn
backup_do_cow(BlockDriverState
*bs
,
97 int64_t sector_num
, int nb_sectors
,
99 bool is_write_notifier
)
101 BackupBlockJob
*job
= (BackupBlockJob
*)bs
->job
;
102 CowRequest cow_request
;
104 QEMUIOVector bounce_qiov
;
105 void *bounce_buffer
= NULL
;
107 int64_t sectors_per_cluster
= cluster_size_sectors(job
);
111 qemu_co_rwlock_rdlock(&job
->flush_rwlock
);
113 start
= sector_num
/ sectors_per_cluster
;
114 end
= DIV_ROUND_UP(sector_num
+ nb_sectors
, sectors_per_cluster
);
116 trace_backup_do_cow_enter(job
, start
, sector_num
, nb_sectors
);
118 wait_for_overlapping_requests(job
, start
, end
);
119 cow_request_begin(&cow_request
, job
, start
, end
);
121 for (; start
< end
; start
++) {
122 if (test_bit(start
, job
->done_bitmap
)) {
123 trace_backup_do_cow_skip(job
, start
);
124 continue; /* already copied */
127 trace_backup_do_cow_process(job
, start
);
129 n
= MIN(sectors_per_cluster
,
130 job
->common
.len
/ BDRV_SECTOR_SIZE
-
131 start
* sectors_per_cluster
);
133 if (!bounce_buffer
) {
134 bounce_buffer
= qemu_blockalign(bs
, job
->cluster_size
);
136 iov
.iov_base
= bounce_buffer
;
137 iov
.iov_len
= n
* BDRV_SECTOR_SIZE
;
138 qemu_iovec_init_external(&bounce_qiov
, &iov
, 1);
140 if (is_write_notifier
) {
141 ret
= bdrv_co_readv_no_serialising(bs
,
142 start
* sectors_per_cluster
,
145 ret
= bdrv_co_readv(bs
, start
* sectors_per_cluster
, n
,
149 trace_backup_do_cow_read_fail(job
, start
, ret
);
151 *error_is_read
= true;
156 if (buffer_is_zero(iov
.iov_base
, iov
.iov_len
)) {
157 ret
= bdrv_co_write_zeroes(job
->target
,
158 start
* sectors_per_cluster
,
159 n
, BDRV_REQ_MAY_UNMAP
);
161 ret
= bdrv_co_writev(job
->target
,
162 start
* sectors_per_cluster
, n
,
166 trace_backup_do_cow_write_fail(job
, start
, ret
);
168 *error_is_read
= false;
173 set_bit(start
, job
->done_bitmap
);
175 /* Publish progress, guest I/O counts as progress too. Note that the
176 * offset field is an opaque progress value, it is not a disk offset.
178 job
->sectors_read
+= n
;
179 job
->common
.offset
+= n
* BDRV_SECTOR_SIZE
;
184 qemu_vfree(bounce_buffer
);
187 cow_request_end(&cow_request
);
189 trace_backup_do_cow_return(job
, sector_num
, nb_sectors
, ret
);
191 qemu_co_rwlock_unlock(&job
->flush_rwlock
);
196 static int coroutine_fn
backup_before_write_notify(
197 NotifierWithReturn
*notifier
,
200 BdrvTrackedRequest
*req
= opaque
;
201 int64_t sector_num
= req
->offset
>> BDRV_SECTOR_BITS
;
202 int nb_sectors
= req
->bytes
>> BDRV_SECTOR_BITS
;
204 assert((req
->offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
205 assert((req
->bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
207 return backup_do_cow(req
->bs
, sector_num
, nb_sectors
, NULL
, true);
210 static void backup_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
212 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
215 error_setg(errp
, QERR_INVALID_PARAMETER
, "speed");
218 ratelimit_set_speed(&s
->limit
, speed
/ BDRV_SECTOR_SIZE
, SLICE_TIME
);
221 static void backup_cleanup_sync_bitmap(BackupBlockJob
*job
, int ret
)
224 BlockDriverState
*bs
= job
->common
.bs
;
226 if (ret
< 0 || block_job_is_cancelled(&job
->common
)) {
227 /* Merge the successor back into the parent, delete nothing. */
228 bm
= bdrv_reclaim_dirty_bitmap(bs
, job
->sync_bitmap
, NULL
);
231 /* Everything is fine, delete this bitmap and install the backup. */
232 bm
= bdrv_dirty_bitmap_abdicate(bs
, job
->sync_bitmap
, NULL
);
237 static void backup_commit(BlockJob
*job
)
239 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
240 if (s
->sync_bitmap
) {
241 backup_cleanup_sync_bitmap(s
, 0);
245 static void backup_abort(BlockJob
*job
)
247 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
248 if (s
->sync_bitmap
) {
249 backup_cleanup_sync_bitmap(s
, -1);
253 static const BlockJobDriver backup_job_driver
= {
254 .instance_size
= sizeof(BackupBlockJob
),
255 .job_type
= BLOCK_JOB_TYPE_BACKUP
,
256 .set_speed
= backup_set_speed
,
257 .commit
= backup_commit
,
258 .abort
= backup_abort
,
261 static BlockErrorAction
backup_error_action(BackupBlockJob
*job
,
262 bool read
, int error
)
265 return block_job_error_action(&job
->common
, job
->on_source_error
,
268 return block_job_error_action(&job
->common
, job
->on_target_error
,
275 } BackupCompleteData
;
277 static void backup_complete(BlockJob
*job
, void *opaque
)
279 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
280 BackupCompleteData
*data
= opaque
;
282 bdrv_unref(s
->target
);
284 block_job_completed(job
, data
->ret
);
288 static bool coroutine_fn
yield_and_check(BackupBlockJob
*job
)
290 if (block_job_is_cancelled(&job
->common
)) {
294 /* we need to yield so that bdrv_drain_all() returns.
295 * (without, VM does not reboot)
297 if (job
->common
.speed
) {
298 uint64_t delay_ns
= ratelimit_calculate_delay(&job
->limit
,
300 job
->sectors_read
= 0;
301 block_job_sleep_ns(&job
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
303 block_job_sleep_ns(&job
->common
, QEMU_CLOCK_REALTIME
, 0);
306 if (block_job_is_cancelled(&job
->common
)) {
313 static int coroutine_fn
backup_run_incremental(BackupBlockJob
*job
)
317 int clusters_per_iter
;
318 uint32_t granularity
;
322 int64_t last_cluster
= -1;
323 int64_t sectors_per_cluster
= cluster_size_sectors(job
);
324 BlockDriverState
*bs
= job
->common
.bs
;
327 granularity
= bdrv_dirty_bitmap_granularity(job
->sync_bitmap
);
328 clusters_per_iter
= MAX((granularity
/ job
->cluster_size
), 1);
329 bdrv_dirty_iter_init(job
->sync_bitmap
, &hbi
);
331 /* Find the next dirty sector(s) */
332 while ((sector
= hbitmap_iter_next(&hbi
)) != -1) {
333 cluster
= sector
/ sectors_per_cluster
;
335 /* Fake progress updates for any clusters we skipped */
336 if (cluster
!= last_cluster
+ 1) {
337 job
->common
.offset
+= ((cluster
- last_cluster
- 1) *
341 for (end
= cluster
+ clusters_per_iter
; cluster
< end
; cluster
++) {
343 if (yield_and_check(job
)) {
346 ret
= backup_do_cow(bs
, cluster
* sectors_per_cluster
,
347 sectors_per_cluster
, &error_is_read
,
350 backup_error_action(job
, error_is_read
, -ret
) ==
351 BLOCK_ERROR_ACTION_REPORT
) {
357 /* If the bitmap granularity is smaller than the backup granularity,
358 * we need to advance the iterator pointer to the next cluster. */
359 if (granularity
< job
->cluster_size
) {
360 bdrv_set_dirty_iter(&hbi
, cluster
* sectors_per_cluster
);
363 last_cluster
= cluster
- 1;
366 /* Play some final catchup with the progress meter */
367 end
= DIV_ROUND_UP(job
->common
.len
, job
->cluster_size
);
368 if (last_cluster
+ 1 < end
) {
369 job
->common
.offset
+= ((end
- last_cluster
- 1) * job
->cluster_size
);
375 static void coroutine_fn
backup_run(void *opaque
)
377 BackupBlockJob
*job
= opaque
;
378 BackupCompleteData
*data
;
379 BlockDriverState
*bs
= job
->common
.bs
;
380 BlockDriverState
*target
= job
->target
;
381 NotifierWithReturn before_write
= {
382 .notify
= backup_before_write_notify
,
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 bdrv_add_before_write_notifier(bs
, &before_write
);
398 if (job
->sync_mode
== MIRROR_SYNC_MODE_NONE
) {
399 while (!block_job_is_cancelled(&job
->common
)) {
400 /* Yield until the job is cancelled. We just let our before_write
401 * notify callback service CoW requests. */
402 job
->common
.busy
= false;
403 qemu_coroutine_yield();
404 job
->common
.busy
= true;
406 } else if (job
->sync_mode
== MIRROR_SYNC_MODE_INCREMENTAL
) {
407 ret
= backup_run_incremental(job
);
409 /* Both FULL and TOP SYNC_MODE's require copying.. */
410 for (; start
< end
; start
++) {
412 if (yield_and_check(job
)) {
416 if (job
->sync_mode
== MIRROR_SYNC_MODE_TOP
) {
420 /* Check to see if these blocks are already in the
423 for (i
= 0; i
< sectors_per_cluster
;) {
424 /* bdrv_is_allocated() only returns true/false based
425 * on the first set of sectors it comes across that
426 * are are all in the same state.
427 * For that reason we must verify each sector in the
428 * backup cluster length. We end up copying more than
429 * needed but at some point that is always the case. */
431 bdrv_is_allocated(bs
,
432 start
* sectors_per_cluster
+ i
,
433 sectors_per_cluster
- i
, &n
);
436 if (alloced
== 1 || n
== 0) {
441 /* If the above loop never found any sectors that are in
442 * the topmost image, skip this backup. */
447 /* FULL sync mode we copy the whole drive. */
448 ret
= backup_do_cow(bs
, start
* sectors_per_cluster
,
449 sectors_per_cluster
, &error_is_read
, false);
451 /* Depending on error action, fail now or retry cluster */
452 BlockErrorAction action
=
453 backup_error_action(job
, error_is_read
, -ret
);
454 if (action
== BLOCK_ERROR_ACTION_REPORT
) {
464 notifier_with_return_remove(&before_write
);
466 /* wait until pending backup_do_cow() calls have completed */
467 qemu_co_rwlock_wrlock(&job
->flush_rwlock
);
468 qemu_co_rwlock_unlock(&job
->flush_rwlock
);
469 g_free(job
->done_bitmap
);
471 bdrv_op_unblock_all(target
, job
->common
.blocker
);
473 data
= g_malloc(sizeof(*data
));
475 block_job_defer_to_main_loop(&job
->common
, backup_complete
, data
);
478 void backup_start(BlockDriverState
*bs
, BlockDriverState
*target
,
479 int64_t speed
, MirrorSyncMode sync_mode
,
480 BdrvDirtyBitmap
*sync_bitmap
,
481 BlockdevOnError on_source_error
,
482 BlockdevOnError on_target_error
,
483 BlockCompletionFunc
*cb
, void *opaque
,
484 BlockJobTxn
*txn
, Error
**errp
)
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 BackupBlockJob
*job
= block_job_create(&backup_job_driver
, bs
, speed
,
551 job
->on_source_error
= on_source_error
;
552 job
->on_target_error
= on_target_error
;
553 job
->target
= target
;
554 job
->sync_mode
= sync_mode
;
555 job
->sync_bitmap
= sync_mode
== MIRROR_SYNC_MODE_INCREMENTAL
?
558 /* If there is no backing file on the target, we cannot rely on COW if our
559 * backup cluster size is smaller than the target cluster size. Even for
560 * targets with a backing file, try to avoid COW if possible. */
561 ret
= bdrv_get_info(job
->target
, &bdi
);
562 if (ret
< 0 && !target
->backing
) {
563 error_setg_errno(errp
, -ret
,
564 "Couldn't determine the cluster size of the target image, "
565 "which has no backing file");
566 error_append_hint(errp
,
567 "Aborting, since this may create an unusable destination image\n");
569 } else if (ret
< 0 && target
->backing
) {
570 /* Not fatal; just trudge on ahead. */
571 job
->cluster_size
= BACKUP_CLUSTER_SIZE_DEFAULT
;
573 job
->cluster_size
= MAX(BACKUP_CLUSTER_SIZE_DEFAULT
, bdi
.cluster_size
);
576 bdrv_op_block_all(target
, job
->common
.blocker
);
577 job
->common
.len
= len
;
578 job
->common
.co
= qemu_coroutine_create(backup_run
);
579 block_job_txn_add_job(txn
, &job
->common
);
580 qemu_coroutine_enter(job
->common
.co
, job
);
585 bdrv_reclaim_dirty_bitmap(bs
, sync_bitmap
, NULL
);