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/cutils.h"
26 #include "sysemu/block-backend.h"
27 #include "qemu/bitmap.h"
28 #include "qemu/error-report.h"
30 #include "block/copy-before-write.h"
32 typedef struct BackupBlockJob
{
34 BlockDriverState
*cbw
;
35 BlockDriverState
*source_bs
;
36 BlockDriverState
*target_bs
;
38 BdrvDirtyBitmap
*sync_bitmap
;
40 MirrorSyncMode sync_mode
;
41 BitmapSyncMode bitmap_mode
;
42 BlockdevOnError on_source_error
;
43 BlockdevOnError on_target_error
;
51 BlockCopyCallState
*bg_bcs_call
;
54 static const BlockJobDriver backup_job_driver
;
56 static void backup_cleanup_sync_bitmap(BackupBlockJob
*job
, int ret
)
59 bool sync
= (((ret
== 0) || (job
->bitmap_mode
== BITMAP_SYNC_MODE_ALWAYS
)) \
60 && (job
->bitmap_mode
!= BITMAP_SYNC_MODE_NEVER
));
64 * We succeeded, or we always intended to sync the bitmap.
65 * Delete this bitmap and install the child.
67 bm
= bdrv_dirty_bitmap_abdicate(job
->sync_bitmap
, NULL
);
70 * We failed, or we never intended to sync the bitmap anyway.
71 * Merge the successor back into the parent, keeping all data.
73 bm
= bdrv_reclaim_dirty_bitmap(job
->sync_bitmap
, NULL
);
78 if (ret
< 0 && job
->bitmap_mode
== BITMAP_SYNC_MODE_ALWAYS
) {
79 /* If we failed and synced, merge in the bits we didn't copy: */
80 bdrv_dirty_bitmap_merge_internal(bm
, block_copy_dirty_bitmap(job
->bcs
),
85 static void backup_commit(Job
*job
)
87 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
.job
);
89 backup_cleanup_sync_bitmap(s
, 0);
93 static void backup_abort(Job
*job
)
95 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
.job
);
97 backup_cleanup_sync_bitmap(s
, -1);
101 static void backup_clean(Job
*job
)
103 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
.job
);
104 block_job_remove_all_bdrv(&s
->common
);
105 bdrv_cbw_drop(s
->cbw
);
108 void backup_do_checkpoint(BlockJob
*job
, Error
**errp
)
110 BackupBlockJob
*backup_job
= container_of(job
, BackupBlockJob
, common
);
112 assert(block_job_driver(job
) == &backup_job_driver
);
114 if (backup_job
->sync_mode
!= MIRROR_SYNC_MODE_NONE
) {
115 error_setg(errp
, "The backup job only supports block checkpoint in"
120 bdrv_set_dirty_bitmap(block_copy_dirty_bitmap(backup_job
->bcs
), 0,
124 static BlockErrorAction
backup_error_action(BackupBlockJob
*job
,
125 bool read
, int error
)
128 return block_job_error_action(&job
->common
, job
->on_source_error
,
131 return block_job_error_action(&job
->common
, job
->on_target_error
,
136 static void coroutine_fn
backup_block_copy_callback(void *opaque
)
138 BackupBlockJob
*s
= opaque
;
142 aio_co_wake(s
->common
.job
.co
);
144 job_enter(&s
->common
.job
);
148 static int coroutine_fn
backup_loop(BackupBlockJob
*job
)
150 BlockCopyCallState
*s
= NULL
;
153 BlockErrorAction act
;
155 while (true) { /* retry loop */
156 job
->bg_bcs_call
= s
= block_copy_async(job
->bcs
, 0,
157 QEMU_ALIGN_UP(job
->len
, job
->cluster_size
),
158 job
->perf
.max_workers
, job
->perf
.max_chunk
,
159 backup_block_copy_callback
, job
);
161 while (!block_copy_call_finished(s
) &&
162 !job_is_cancelled(&job
->common
.job
))
164 job_yield(&job
->common
.job
);
167 if (!block_copy_call_finished(s
)) {
168 assert(job_is_cancelled(&job
->common
.job
));
170 * Note that we can't use job_yield() here, as it doesn't work for
173 block_copy_call_cancel(s
);
175 qemu_coroutine_yield();
176 assert(block_copy_call_finished(s
));
181 if (job_is_cancelled(&job
->common
.job
) ||
182 block_copy_call_succeeded(s
))
188 if (block_copy_call_cancelled(s
)) {
190 * Job is not cancelled but only block-copy call. This is possible
191 * after job pause. Now the pause is finished, start new block-copy
194 block_copy_call_free(s
);
198 /* The only remaining case is failed block-copy call. */
199 assert(block_copy_call_failed(s
));
201 ret
= block_copy_call_status(s
, &error_is_read
);
202 act
= backup_error_action(job
, error_is_read
, -ret
);
204 case BLOCK_ERROR_ACTION_REPORT
:
206 case BLOCK_ERROR_ACTION_STOP
:
208 * Go to pause prior to starting new block-copy call on the next
211 job_pause_point(&job
->common
.job
);
213 case BLOCK_ERROR_ACTION_IGNORE
:
214 /* Proceed to new block-copy call to retry. */
220 block_copy_call_free(s
);
224 block_copy_call_free(s
);
225 job
->bg_bcs_call
= NULL
;
229 static void backup_init_bcs_bitmap(BackupBlockJob
*job
)
233 BdrvDirtyBitmap
*bcs_bitmap
= block_copy_dirty_bitmap(job
->bcs
);
235 if (job
->sync_mode
== MIRROR_SYNC_MODE_BITMAP
) {
236 bdrv_clear_dirty_bitmap(bcs_bitmap
, NULL
);
237 ret
= bdrv_dirty_bitmap_merge_internal(bcs_bitmap
, job
->sync_bitmap
,
240 } else if (job
->sync_mode
== MIRROR_SYNC_MODE_TOP
) {
242 * We can't hog the coroutine to initialize this thoroughly.
243 * Set a flag and resume work when we are able to yield safely.
245 block_copy_set_skip_unallocated(job
->bcs
, true);
248 estimate
= bdrv_get_dirty_count(bcs_bitmap
);
249 job_progress_set_remaining(&job
->common
.job
, estimate
);
252 static int coroutine_fn
backup_run(Job
*job
, Error
**errp
)
254 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
.job
);
257 backup_init_bcs_bitmap(s
);
259 if (s
->sync_mode
== MIRROR_SYNC_MODE_TOP
) {
263 for (offset
= 0; offset
< s
->len
; ) {
264 if (job_is_cancelled(job
)) {
268 job_pause_point(job
);
270 if (job_is_cancelled(job
)) {
274 ret
= block_copy_reset_unallocated(s
->bcs
, offset
, &count
);
281 block_copy_set_skip_unallocated(s
->bcs
, false);
284 if (s
->sync_mode
== MIRROR_SYNC_MODE_NONE
) {
286 * All bits are set in bcs bitmap to allow any cluster to be copied.
287 * This does not actually require them to be copied.
289 while (!job_is_cancelled(job
)) {
291 * Yield until the job is cancelled. We just let our before_write
292 * notify callback service CoW requests.
297 return backup_loop(s
);
303 static void coroutine_fn
backup_pause(Job
*job
)
305 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
.job
);
307 if (s
->bg_bcs_call
&& !block_copy_call_finished(s
->bg_bcs_call
)) {
308 block_copy_call_cancel(s
->bg_bcs_call
);
310 qemu_coroutine_yield();
314 static void coroutine_fn
backup_set_speed(BlockJob
*job
, int64_t speed
)
316 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
);
319 * block_job_set_speed() is called first from block_job_create(), when we
320 * don't yet have s->bcs.
323 block_copy_set_speed(s
->bcs
, speed
);
324 if (s
->bg_bcs_call
) {
325 block_copy_kick(s
->bg_bcs_call
);
330 static void backup_cancel(Job
*job
, bool force
)
332 BackupBlockJob
*s
= container_of(job
, BackupBlockJob
, common
.job
);
334 bdrv_cancel_in_flight(s
->target_bs
);
337 static const BlockJobDriver backup_job_driver
= {
339 .instance_size
= sizeof(BackupBlockJob
),
340 .job_type
= JOB_TYPE_BACKUP
,
341 .free
= block_job_free
,
342 .user_resume
= block_job_user_resume
,
344 .commit
= backup_commit
,
345 .abort
= backup_abort
,
346 .clean
= backup_clean
,
347 .pause
= backup_pause
,
348 .cancel
= backup_cancel
,
350 .set_speed
= backup_set_speed
,
353 BlockJob
*backup_job_create(const char *job_id
, BlockDriverState
*bs
,
354 BlockDriverState
*target
, int64_t speed
,
355 MirrorSyncMode sync_mode
, BdrvDirtyBitmap
*sync_bitmap
,
356 BitmapSyncMode bitmap_mode
,
358 const char *filter_node_name
,
360 BlockdevOnError on_source_error
,
361 BlockdevOnError on_target_error
,
363 BlockCompletionFunc
*cb
, void *opaque
,
364 JobTxn
*txn
, Error
**errp
)
366 int64_t len
, target_len
;
367 BackupBlockJob
*job
= NULL
;
368 int64_t cluster_size
;
369 BlockDriverState
*cbw
= NULL
;
370 BlockCopyState
*bcs
= NULL
;
375 /* QMP interface protects us from these cases */
376 assert(sync_mode
!= MIRROR_SYNC_MODE_INCREMENTAL
);
377 assert(sync_bitmap
|| sync_mode
!= MIRROR_SYNC_MODE_BITMAP
);
380 error_setg(errp
, "Source and target cannot be the same");
384 if (!bdrv_is_inserted(bs
)) {
385 error_setg(errp
, "Device is not inserted: %s",
386 bdrv_get_device_name(bs
));
390 if (!bdrv_is_inserted(target
)) {
391 error_setg(errp
, "Device is not inserted: %s",
392 bdrv_get_device_name(target
));
396 if (compress
&& !bdrv_supports_compressed_writes(target
)) {
397 error_setg(errp
, "Compression is not supported for this drive %s",
398 bdrv_get_device_name(target
));
402 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
406 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_BACKUP_TARGET
, errp
)) {
410 if (perf
->max_workers
< 1) {
411 error_setg(errp
, "max-workers must be greater than zero");
415 if (perf
->max_chunk
< 0) {
416 error_setg(errp
, "max-chunk must be zero (which means no limit) or "
422 /* If we need to write to this bitmap, check that we can: */
423 if (bitmap_mode
!= BITMAP_SYNC_MODE_NEVER
&&
424 bdrv_dirty_bitmap_check(sync_bitmap
, BDRV_BITMAP_DEFAULT
, errp
)) {
428 /* Create a new bitmap, and freeze/disable this one. */
429 if (bdrv_dirty_bitmap_create_successor(sync_bitmap
, errp
) < 0) {
434 len
= bdrv_getlength(bs
);
436 error_setg_errno(errp
, -len
, "Unable to get length for '%s'",
437 bdrv_get_device_or_node_name(bs
));
441 target_len
= bdrv_getlength(target
);
442 if (target_len
< 0) {
443 error_setg_errno(errp
, -target_len
, "Unable to get length for '%s'",
444 bdrv_get_device_or_node_name(bs
));
448 if (target_len
!= len
) {
449 error_setg(errp
, "Source and target image have different sizes");
453 cbw
= bdrv_cbw_append(bs
, target
, filter_node_name
, &bcs
, errp
);
458 cluster_size
= block_copy_cluster_size(bcs
);
460 if (perf
->max_chunk
&& perf
->max_chunk
< cluster_size
) {
461 error_setg(errp
, "Required max-chunk (%" PRIi64
") is less than backup "
462 "cluster size (%" PRIi64
")", perf
->max_chunk
, cluster_size
);
466 /* job->len is fixed, so we can't allow resize */
467 job
= block_job_create(job_id
, &backup_job_driver
, txn
, cbw
,
469 speed
, creation_flags
, cb
, opaque
, errp
);
476 job
->target_bs
= target
;
477 job
->on_source_error
= on_source_error
;
478 job
->on_target_error
= on_target_error
;
479 job
->sync_mode
= sync_mode
;
480 job
->sync_bitmap
= sync_bitmap
;
481 job
->bitmap_mode
= bitmap_mode
;
483 job
->cluster_size
= cluster_size
;
487 block_copy_set_copy_opts(bcs
, perf
->use_copy_range
, compress
);
488 block_copy_set_progress_meter(bcs
, &job
->common
.job
.progress
);
489 block_copy_set_speed(bcs
, speed
);
491 /* Required permissions are taken by copy-before-write filter target */
492 block_job_add_bdrv(&job
->common
, "target", target
, 0, BLK_PERM_ALL
,
499 bdrv_reclaim_dirty_bitmap(sync_bitmap
, NULL
);