4 * Copyright (C) 2013 Proxmox Server Solutions
5 * Copyright (c) 2019 Virtuozzo International GmbH.
8 * Dietmar Maurer (dietmar@proxmox.com)
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
18 #include "block/block.h"
19 #include "block/block_int.h"
20 #include "block/blockjob_int.h"
21 #include "block/block_backup.h"
22 #include "block/block-copy.h"
23 #include "qapi/error.h"
24 #include "qapi/qmp/qerror.h"
25 #include "qemu/ratelimit.h"
26 #include "qemu/cutils.h"
27 #include "sysemu/block-backend.h"
28 #include "qemu/bitmap.h"
29 #include "qemu/error-report.h"
31 #include "block/backup-top.h"
33 #define BACKUP_CLUSTER_SIZE_DEFAULT (1 << 16)
35 typedef struct BackupBlockJob
{
37 BlockDriverState
*backup_top
;
38 BlockDriverState
*source_bs
;
40 BdrvDirtyBitmap
*sync_bitmap
;
42 MirrorSyncMode sync_mode
;
43 BitmapSyncMode bitmap_mode
;
44 BlockdevOnError on_source_error
;
45 BlockdevOnError on_target_error
;
53 static const BlockJobDriver backup_job_driver
;
55 static void backup_progress_bytes_callback(int64_t bytes
, void *opaque
)
57 BackupBlockJob
*s
= opaque
;
59 s
->bytes_read
+= bytes
;
60 job_progress_update(&s
->common
.job
, bytes
);
63 static void backup_progress_reset_callback(void *opaque
)
65 BackupBlockJob
*s
= opaque
;
66 uint64_t estimate
= bdrv_get_dirty_count(s
->bcs
->copy_bitmap
);
68 job_progress_set_remaining(&s
->common
.job
, estimate
);
71 static int coroutine_fn
backup_do_cow(BackupBlockJob
*job
,
72 int64_t offset
, uint64_t bytes
,
76 int64_t start
, end
; /* bytes */
78 start
= QEMU_ALIGN_DOWN(offset
, job
->cluster_size
);
79 end
= QEMU_ALIGN_UP(bytes
+ offset
, job
->cluster_size
);
81 trace_backup_do_cow_enter(job
, start
, offset
, bytes
);
83 ret
= block_copy(job
->bcs
, start
, end
- start
, error_is_read
);
85 trace_backup_do_cow_return(job
, offset
, bytes
, ret
);
90 static void backup_cleanup_sync_bitmap(BackupBlockJob
*job
, int ret
)
93 bool sync
= (((ret
== 0) || (job
->bitmap_mode
== BITMAP_SYNC_MODE_ALWAYS
)) \
94 && (job
->bitmap_mode
!= BITMAP_SYNC_MODE_NEVER
));
98 * We succeeded, or we always intended to sync the bitmap.
99 * Delete this bitmap and install the child.
101 bm
= bdrv_dirty_bitmap_abdicate(job
->sync_bitmap
, NULL
);
104 * We failed, or we never intended to sync the bitmap anyway.
105 * Merge the successor back into the parent, keeping all data.
107 bm
= bdrv_reclaim_dirty_bitmap(job
->sync_bitmap
, NULL
);
112 if (ret
< 0 && job
->bitmap_mode
== BITMAP_SYNC_MODE_ALWAYS
) {
113 /* If we failed and synced, merge in the bits we didn't copy: */
114 bdrv_dirty_bitmap_merge_internal(bm
, job
->bcs
->copy_bitmap
,
119 static void backup_commit(Job
*job
)
121 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
.job
);
122 if (s
->sync_bitmap
) {
123 backup_cleanup_sync_bitmap(s
, 0);
127 static void backup_abort(Job
*job
)
129 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
.job
);
130 if (s
->sync_bitmap
) {
131 backup_cleanup_sync_bitmap(s
, -1);
135 static void backup_clean(Job
*job
)
137 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
.job
);
138 AioContext
*aio_context
= bdrv_get_aio_context(s
->backup_top
);
140 aio_context_acquire(aio_context
);
141 bdrv_backup_top_drop(s
->backup_top
);
142 aio_context_release(aio_context
);
145 void backup_do_checkpoint(BlockJob
*job
, Error
**errp
)
147 BackupBlockJob
*backup_job
= container_of(job
, BackupBlockJob
, common
);
149 assert(block_job_driver(job
) == &backup_job_driver
);
151 if (backup_job
->sync_mode
!= MIRROR_SYNC_MODE_NONE
) {
152 error_setg(errp
, "The backup job only supports block checkpoint in"
157 bdrv_set_dirty_bitmap(backup_job
->bcs
->copy_bitmap
, 0, backup_job
->len
);
160 static BlockErrorAction
backup_error_action(BackupBlockJob
*job
,
161 bool read
, int error
)
164 return block_job_error_action(&job
->common
, job
->on_source_error
,
167 return block_job_error_action(&job
->common
, job
->on_target_error
,
172 static bool coroutine_fn
yield_and_check(BackupBlockJob
*job
)
176 if (job_is_cancelled(&job
->common
.job
)) {
181 * We need to yield even for delay_ns = 0 so that bdrv_drain_all() can
182 * return. Without a yield, the VM would not reboot.
184 delay_ns
= block_job_ratelimit_get_delay(&job
->common
, job
->bytes_read
);
186 job_sleep_ns(&job
->common
.job
, delay_ns
);
188 if (job_is_cancelled(&job
->common
.job
)) {
195 static int coroutine_fn
backup_loop(BackupBlockJob
*job
)
199 BdrvDirtyBitmapIter
*bdbi
;
202 bdbi
= bdrv_dirty_iter_new(job
->bcs
->copy_bitmap
);
203 while ((offset
= bdrv_dirty_iter_next(bdbi
)) != -1) {
205 if (yield_and_check(job
)) {
208 ret
= backup_do_cow(job
, offset
, job
->cluster_size
, &error_is_read
);
209 if (ret
< 0 && backup_error_action(job
, error_is_read
, -ret
) ==
210 BLOCK_ERROR_ACTION_REPORT
)
218 bdrv_dirty_iter_free(bdbi
);
222 static void backup_init_copy_bitmap(BackupBlockJob
*job
)
227 if (job
->sync_mode
== MIRROR_SYNC_MODE_BITMAP
) {
228 ret
= bdrv_dirty_bitmap_merge_internal(job
->bcs
->copy_bitmap
,
233 if (job
->sync_mode
== MIRROR_SYNC_MODE_TOP
) {
235 * We can't hog the coroutine to initialize this thoroughly.
236 * Set a flag and resume work when we are able to yield safely.
238 job
->bcs
->skip_unallocated
= true;
240 bdrv_set_dirty_bitmap(job
->bcs
->copy_bitmap
, 0, job
->len
);
243 estimate
= bdrv_get_dirty_count(job
->bcs
->copy_bitmap
);
244 job_progress_set_remaining(&job
->common
.job
, estimate
);
247 static int coroutine_fn
backup_run(Job
*job
, Error
**errp
)
249 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
.job
);
252 backup_init_copy_bitmap(s
);
254 if (s
->sync_mode
== MIRROR_SYNC_MODE_TOP
) {
258 for (offset
= 0; offset
< s
->len
; ) {
259 if (yield_and_check(s
)) {
264 ret
= block_copy_reset_unallocated(s
->bcs
, offset
, &count
);
271 s
->bcs
->skip_unallocated
= false;
274 if (s
->sync_mode
== MIRROR_SYNC_MODE_NONE
) {
276 * All bits are set in copy_bitmap to allow any cluster to be copied.
277 * This does not actually require them to be copied.
279 while (!job_is_cancelled(job
)) {
281 * Yield until the job is cancelled. We just let our before_write
282 * notify callback service CoW requests.
287 ret
= backup_loop(s
);
294 static const BlockJobDriver backup_job_driver
= {
296 .instance_size
= sizeof(BackupBlockJob
),
297 .job_type
= JOB_TYPE_BACKUP
,
298 .free
= block_job_free
,
299 .user_resume
= block_job_user_resume
,
301 .commit
= backup_commit
,
302 .abort
= backup_abort
,
303 .clean
= backup_clean
,
307 static int64_t backup_calculate_cluster_size(BlockDriverState
*target
,
314 * If there is no backing file on the target, we cannot rely on COW if our
315 * backup cluster size is smaller than the target cluster size. Even for
316 * targets with a backing file, try to avoid COW if possible.
318 ret
= bdrv_get_info(target
, &bdi
);
319 if (ret
== -ENOTSUP
&& !target
->backing
) {
320 /* Cluster size is not defined */
321 warn_report("The target block device doesn't provide "
322 "information about the block size and it doesn't have a "
323 "backing file. The default block size of %u bytes is "
324 "used. If the actual block size of the target exceeds "
325 "this default, the backup may be unusable",
326 BACKUP_CLUSTER_SIZE_DEFAULT
);
327 return BACKUP_CLUSTER_SIZE_DEFAULT
;
328 } else if (ret
< 0 && !target
->backing
) {
329 error_setg_errno(errp
, -ret
,
330 "Couldn't determine the cluster size of the target image, "
331 "which has no backing file");
332 error_append_hint(errp
,
333 "Aborting, since this may create an unusable destination image\n");
335 } else if (ret
< 0 && target
->backing
) {
336 /* Not fatal; just trudge on ahead. */
337 return BACKUP_CLUSTER_SIZE_DEFAULT
;
340 return MAX(BACKUP_CLUSTER_SIZE_DEFAULT
, bdi
.cluster_size
);
343 BlockJob
*backup_job_create(const char *job_id
, BlockDriverState
*bs
,
344 BlockDriverState
*target
, int64_t speed
,
345 MirrorSyncMode sync_mode
, BdrvDirtyBitmap
*sync_bitmap
,
346 BitmapSyncMode bitmap_mode
,
348 const char *filter_node_name
,
349 BlockdevOnError on_source_error
,
350 BlockdevOnError on_target_error
,
352 BlockCompletionFunc
*cb
, void *opaque
,
353 JobTxn
*txn
, Error
**errp
)
356 BackupBlockJob
*job
= NULL
;
357 int64_t cluster_size
;
358 BdrvRequestFlags write_flags
;
359 BlockDriverState
*backup_top
= NULL
;
360 BlockCopyState
*bcs
= NULL
;
365 /* QMP interface protects us from these cases */
366 assert(sync_mode
!= MIRROR_SYNC_MODE_INCREMENTAL
);
367 assert(sync_bitmap
|| sync_mode
!= MIRROR_SYNC_MODE_BITMAP
);
370 error_setg(errp
, "Source and target cannot be the same");
374 if (!bdrv_is_inserted(bs
)) {
375 error_setg(errp
, "Device is not inserted: %s",
376 bdrv_get_device_name(bs
));
380 if (!bdrv_is_inserted(target
)) {
381 error_setg(errp
, "Device is not inserted: %s",
382 bdrv_get_device_name(target
));
386 if (compress
&& !block_driver_can_compress(target
->drv
)) {
387 error_setg(errp
, "Compression is not supported for this drive %s",
388 bdrv_get_device_name(target
));
392 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
396 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_BACKUP_TARGET
, errp
)) {
401 /* If we need to write to this bitmap, check that we can: */
402 if (bitmap_mode
!= BITMAP_SYNC_MODE_NEVER
&&
403 bdrv_dirty_bitmap_check(sync_bitmap
, BDRV_BITMAP_DEFAULT
, errp
)) {
407 /* Create a new bitmap, and freeze/disable this one. */
408 if (bdrv_dirty_bitmap_create_successor(sync_bitmap
, errp
) < 0) {
413 len
= bdrv_getlength(bs
);
415 error_setg_errno(errp
, -len
, "unable to get length for '%s'",
416 bdrv_get_device_name(bs
));
420 cluster_size
= backup_calculate_cluster_size(target
, errp
);
421 if (cluster_size
< 0) {
426 * If source is in backing chain of target assume that target is going to be
427 * used for "image fleecing", i.e. it should represent a kind of snapshot of
428 * source at backup-start point in time. And target is going to be read by
429 * somebody (for example, used as NBD export) during backup job.
431 * In this case, we need to add BDRV_REQ_SERIALISING write flag to avoid
432 * intersection of backup writes and third party reads from target,
433 * otherwise reading from target we may occasionally read already updated by
436 * For more information see commit f8d59dfb40bb and test
437 * tests/qemu-iotests/222
439 write_flags
= (bdrv_chain_contains(target
, bs
) ? BDRV_REQ_SERIALISING
: 0) |
440 (compress
? BDRV_REQ_WRITE_COMPRESSED
: 0),
442 backup_top
= bdrv_backup_top_append(bs
, target
, filter_node_name
,
443 cluster_size
, write_flags
, &bcs
, errp
);
448 /* job->len is fixed, so we can't allow resize */
449 job
= block_job_create(job_id
, &backup_job_driver
, txn
, backup_top
,
451 speed
, creation_flags
, cb
, opaque
, errp
);
456 job
->backup_top
= backup_top
;
458 job
->on_source_error
= on_source_error
;
459 job
->on_target_error
= on_target_error
;
460 job
->sync_mode
= sync_mode
;
461 job
->sync_bitmap
= sync_bitmap
;
462 job
->bitmap_mode
= bitmap_mode
;
464 job
->cluster_size
= cluster_size
;
467 block_copy_set_callbacks(bcs
, backup_progress_bytes_callback
,
468 backup_progress_reset_callback
, job
);
470 /* Required permissions are already taken by backup-top target */
471 block_job_add_bdrv(&job
->common
, "target", target
, 0, BLK_PERM_ALL
,
478 bdrv_reclaim_dirty_bitmap(sync_bitmap
, NULL
);
481 bdrv_backup_top_drop(backup_top
);