4 * Copyright (C) 2013 Proxmox Server Solutions
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)
31 typedef struct BackupBlockJob {
34 /* bitmap for sync=incremental */
35 BdrvDirtyBitmap *sync_bitmap;
36 MirrorSyncMode sync_mode;
37 BlockdevOnError on_source_error;
38 BlockdevOnError on_target_error;
39 CoRwlock flush_rwlock;
44 NotifierWithReturn before_write;
45 QLIST_HEAD(, CowRequest) inflight_reqs;
49 int64_t copy_range_size;
52 static const BlockJobDriver backup_job_driver;
54 /* See if in-flight requests overlap and wait for them to complete */
55 static void coroutine_fn wait_for_overlapping_requests(BackupBlockJob *job,
64 QLIST_FOREACH(req, &job->inflight_reqs, list) {
65 if (end > req->start_byte && start < req->end_byte) {
66 qemu_co_queue_wait(&req->wait_queue, NULL);
74 /* Keep track of an in-flight request */
75 static void cow_request_begin(CowRequest *req, BackupBlockJob *job,
76 int64_t start, int64_t end)
78 req->start_byte = start;
80 qemu_co_queue_init(&req->wait_queue);
81 QLIST_INSERT_HEAD(&job->inflight_reqs, req, list);
84 /* Forget about a completed request */
85 static void cow_request_end(CowRequest *req)
87 QLIST_REMOVE(req, list);
88 qemu_co_queue_restart_all(&req->wait_queue);
91 /* Copy range to target with a bounce buffer and return the bytes copied. If
92 * error occured, return a negative error number */
93 static int coroutine_fn backup_cow_with_bounce_buffer(BackupBlockJob *job,
96 bool is_write_notifier,
103 BlockBackend *blk = job->common.blk;
106 hbitmap_reset(job->copy_bitmap, start / job->cluster_size, 1);
107 nbytes = MIN(job->cluster_size, job->len - start);
108 if (!*bounce_buffer) {
109 *bounce_buffer = blk_blockalign(blk, job->cluster_size);
111 iov.iov_base = *bounce_buffer;
112 iov.iov_len = nbytes;
113 qemu_iovec_init_external(&qiov, &iov, 1);
115 ret = blk_co_preadv(blk, start, qiov.size, &qiov,
116 is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0);
118 trace_backup_do_cow_read_fail(job, start, ret);
120 *error_is_read = true;
125 if (qemu_iovec_is_zero(&qiov)) {
126 ret = blk_co_pwrite_zeroes(job->target, start,
127 qiov.size, BDRV_REQ_MAY_UNMAP);
129 ret = blk_co_pwritev(job->target, start,
131 job->compress ? BDRV_REQ_WRITE_COMPRESSED : 0);
134 trace_backup_do_cow_write_fail(job, start, ret);
136 *error_is_read = false;
143 hbitmap_set(job->copy_bitmap, start / job->cluster_size, 1);
148 /* Copy range to target and return the bytes copied. If error occured, return a
149 * negative error number. */
150 static int coroutine_fn backup_cow_with_offload(BackupBlockJob *job,
153 bool is_write_notifier)
157 BlockBackend *blk = job->common.blk;
160 assert(QEMU_IS_ALIGNED(job->copy_range_size, job->cluster_size));
161 nbytes = MIN(job->copy_range_size, end - start);
162 nr_clusters = DIV_ROUND_UP(nbytes, job->cluster_size);
163 hbitmap_reset(job->copy_bitmap, start / job->cluster_size,
165 ret = blk_co_copy_range(blk, start, job->target, start, nbytes,
166 is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0);
168 trace_backup_do_cow_copy_range_fail(job, start, ret);
169 hbitmap_set(job->copy_bitmap, start / job->cluster_size,
177 static int coroutine_fn backup_do_cow(BackupBlockJob *job,
178 int64_t offset, uint64_t bytes,
180 bool is_write_notifier)
182 CowRequest cow_request;
184 int64_t start, end; /* bytes */
185 void *bounce_buffer = NULL;
187 qemu_co_rwlock_rdlock(&job->flush_rwlock);
189 start = QEMU_ALIGN_DOWN(offset, job->cluster_size);
190 end = QEMU_ALIGN_UP(bytes + offset, job->cluster_size);
192 trace_backup_do_cow_enter(job, start, offset, bytes);
194 wait_for_overlapping_requests(job, start, end);
195 cow_request_begin(&cow_request, job, start, end);
197 while (start < end) {
198 if (!hbitmap_get(job->copy_bitmap, start / job->cluster_size)) {
199 trace_backup_do_cow_skip(job, start);
200 start += job->cluster_size;
201 continue; /* already copied */
204 trace_backup_do_cow_process(job, start);
206 if (job->use_copy_range) {
207 ret = backup_cow_with_offload(job, start, end, is_write_notifier);
209 job->use_copy_range = false;
212 if (!job->use_copy_range) {
213 ret = backup_cow_with_bounce_buffer(job, start, end, is_write_notifier,
214 error_is_read, &bounce_buffer);
220 /* Publish progress, guest I/O counts as progress too. Note that the
221 * offset field is an opaque progress value, it is not a disk offset.
224 job->bytes_read += ret;
225 job_progress_update(&job->common.job, ret);
230 qemu_vfree(bounce_buffer);
233 cow_request_end(&cow_request);
235 trace_backup_do_cow_return(job, offset, bytes, ret);
237 qemu_co_rwlock_unlock(&job->flush_rwlock);
242 static int coroutine_fn backup_before_write_notify(
243 NotifierWithReturn *notifier,
246 BackupBlockJob *job = container_of(notifier, BackupBlockJob, before_write);
247 BdrvTrackedRequest *req = opaque;
249 assert(req->bs == blk_bs(job->common.blk));
250 assert(QEMU_IS_ALIGNED(req->offset, BDRV_SECTOR_SIZE));
251 assert(QEMU_IS_ALIGNED(req->bytes, BDRV_SECTOR_SIZE));
253 return backup_do_cow(job, req->offset, req->bytes, NULL, true);
256 static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret)
259 BlockDriverState *bs = blk_bs(job->common.blk);
262 /* Merge the successor back into the parent, delete nothing. */
263 bm = bdrv_reclaim_dirty_bitmap(bs, job->sync_bitmap, NULL);
266 /* Everything is fine, delete this bitmap and install the backup. */
267 bm = bdrv_dirty_bitmap_abdicate(bs, job->sync_bitmap, NULL);
272 static void backup_commit(Job *job)
274 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
275 if (s->sync_bitmap) {
276 backup_cleanup_sync_bitmap(s, 0);
280 static void backup_abort(Job *job)
282 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
283 if (s->sync_bitmap) {
284 backup_cleanup_sync_bitmap(s, -1);
288 static void backup_clean(Job *job)
290 BackupBlockJob *s = container_of(job, BackupBlockJob, common.job);
292 blk_unref(s->target);
296 static void backup_attached_aio_context(BlockJob *job, AioContext *aio_context)
298 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
300 blk_set_aio_context(s->target, aio_context);
303 void backup_do_checkpoint(BlockJob *job, Error **errp)
305 BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
308 assert(block_job_driver(job) == &backup_job_driver);
310 if (backup_job->sync_mode != MIRROR_SYNC_MODE_NONE) {
311 error_setg(errp, "The backup job only supports block checkpoint in"
316 len = DIV_ROUND_UP(backup_job->len, backup_job->cluster_size);
317 hbitmap_set(backup_job->copy_bitmap, 0, len);
320 void backup_wait_for_overlapping_requests(BlockJob *job, int64_t offset,
323 BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
326 assert(block_job_driver(job) == &backup_job_driver);
328 start = QEMU_ALIGN_DOWN(offset, backup_job->cluster_size);
329 end = QEMU_ALIGN_UP(offset + bytes, backup_job->cluster_size);
330 wait_for_overlapping_requests(backup_job, start, end);
333 void backup_cow_request_begin(CowRequest *req, BlockJob *job,
334 int64_t offset, uint64_t bytes)
336 BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
339 assert(block_job_driver(job) == &backup_job_driver);
341 start = QEMU_ALIGN_DOWN(offset, backup_job->cluster_size);
342 end = QEMU_ALIGN_UP(offset + bytes, backup_job->cluster_size);
343 cow_request_begin(req, backup_job, start, end);
346 void backup_cow_request_end(CowRequest *req)
348 cow_request_end(req);
351 static void backup_drain(BlockJob *job)
353 BackupBlockJob *s = container_of(job, BackupBlockJob, common);
355 /* Need to keep a reference in case blk_drain triggers execution
356 * of backup_complete...
359 BlockBackend *target = s->target;
366 static BlockErrorAction backup_error_action(BackupBlockJob *job,
367 bool read, int error)
370 return block_job_error_action(&job->common, job->on_source_error,
373 return block_job_error_action(&job->common, job->on_target_error,
380 } BackupCompleteData;
382 static void backup_complete(Job *job, void *opaque)
384 BackupCompleteData *data = opaque;
386 job_completed(job, data->ret, NULL);
390 static bool coroutine_fn yield_and_check(BackupBlockJob *job)
394 if (job_is_cancelled(&job->common.job)) {
398 /* We need to yield even for delay_ns = 0 so that bdrv_drain_all() can
399 * return. Without a yield, the VM would not reboot. */
400 delay_ns = block_job_ratelimit_get_delay(&job->common, job->bytes_read);
402 job_sleep_ns(&job->common.job, delay_ns);
404 if (job_is_cancelled(&job->common.job)) {
411 static int coroutine_fn backup_run_incremental(BackupBlockJob *job)
418 hbitmap_iter_init(&hbi, job->copy_bitmap, 0);
419 while ((cluster = hbitmap_iter_next(&hbi, true)) != -1) {
421 if (yield_and_check(job)) {
424 ret = backup_do_cow(job, cluster * job->cluster_size,
425 job->cluster_size, &error_is_read, false);
426 if (ret < 0 && backup_error_action(job, error_is_read, -ret) ==
427 BLOCK_ERROR_ACTION_REPORT)
437 /* init copy_bitmap from sync_bitmap */
438 static void backup_incremental_init_copy_bitmap(BackupBlockJob *job)
440 BdrvDirtyBitmapIter *dbi;
442 int64_t end = DIV_ROUND_UP(bdrv_dirty_bitmap_size(job->sync_bitmap),
445 dbi = bdrv_dirty_iter_new(job->sync_bitmap);
446 while ((offset = bdrv_dirty_iter_next(dbi)) != -1) {
447 int64_t cluster = offset / job->cluster_size;
448 int64_t next_cluster;
450 offset += bdrv_dirty_bitmap_granularity(job->sync_bitmap);
451 if (offset >= bdrv_dirty_bitmap_size(job->sync_bitmap)) {
452 hbitmap_set(job->copy_bitmap, cluster, end - cluster);
456 offset = bdrv_dirty_bitmap_next_zero(job->sync_bitmap, offset);
458 hbitmap_set(job->copy_bitmap, cluster, end - cluster);
462 next_cluster = DIV_ROUND_UP(offset, job->cluster_size);
463 hbitmap_set(job->copy_bitmap, cluster, next_cluster - cluster);
464 if (next_cluster >= end) {
468 bdrv_set_dirty_iter(dbi, next_cluster * job->cluster_size);
471 /* TODO job_progress_set_remaining() would make more sense */
472 job_progress_update(&job->common.job,
473 job->len - hbitmap_count(job->copy_bitmap) * job->cluster_size);
475 bdrv_dirty_iter_free(dbi);
478 static void coroutine_fn backup_run(void *opaque)
480 BackupBlockJob *job = opaque;
481 BackupCompleteData *data;
482 BlockDriverState *bs = blk_bs(job->common.blk);
483 int64_t offset, nb_clusters;
486 QLIST_INIT(&job->inflight_reqs);
487 qemu_co_rwlock_init(&job->flush_rwlock);
489 nb_clusters = DIV_ROUND_UP(job->len, job->cluster_size);
490 job_progress_set_remaining(&job->common.job, job->len);
492 job->copy_bitmap = hbitmap_alloc(nb_clusters, 0);
493 if (job->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
494 backup_incremental_init_copy_bitmap(job);
496 hbitmap_set(job->copy_bitmap, 0, nb_clusters);
500 job->before_write.notify = backup_before_write_notify;
501 bdrv_add_before_write_notifier(bs, &job->before_write);
503 if (job->sync_mode == MIRROR_SYNC_MODE_NONE) {
504 /* All bits are set in copy_bitmap to allow any cluster to be copied.
505 * This does not actually require them to be copied. */
506 while (!job_is_cancelled(&job->common.job)) {
507 /* Yield until the job is cancelled. We just let our before_write
508 * notify callback service CoW requests. */
509 job_yield(&job->common.job);
511 } else if (job->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
512 ret = backup_run_incremental(job);
514 /* Both FULL and TOP SYNC_MODE's require copying.. */
515 for (offset = 0; offset < job->len;
516 offset += job->cluster_size) {
520 if (yield_and_check(job)) {
524 if (job->sync_mode == MIRROR_SYNC_MODE_TOP) {
528 /* Check to see if these blocks are already in the
531 for (i = 0; i < job->cluster_size;) {
532 /* bdrv_is_allocated() only returns true/false based
533 * on the first set of sectors it comes across that
534 * are are all in the same state.
535 * For that reason we must verify each sector in the
536 * backup cluster length. We end up copying more than
537 * needed but at some point that is always the case. */
539 bdrv_is_allocated(bs, offset + i,
540 job->cluster_size - i, &n);
543 if (alloced || n == 0) {
548 /* If the above loop never found any sectors that are in
549 * the topmost image, skip this backup. */
554 /* FULL sync mode we copy the whole drive. */
558 ret = backup_do_cow(job, offset, job->cluster_size,
559 &error_is_read, false);
562 /* Depending on error action, fail now or retry cluster */
563 BlockErrorAction action =
564 backup_error_action(job, error_is_read, -ret);
565 if (action == BLOCK_ERROR_ACTION_REPORT) {
568 offset -= job->cluster_size;
575 notifier_with_return_remove(&job->before_write);
577 /* wait until pending backup_do_cow() calls have completed */
578 qemu_co_rwlock_wrlock(&job->flush_rwlock);
579 qemu_co_rwlock_unlock(&job->flush_rwlock);
580 hbitmap_free(job->copy_bitmap);
582 data = g_malloc(sizeof(*data));
584 job_defer_to_main_loop(&job->common.job, backup_complete, data);
587 static const BlockJobDriver backup_job_driver = {
589 .instance_size = sizeof(BackupBlockJob),
590 .job_type = JOB_TYPE_BACKUP,
591 .free = block_job_free,
592 .user_resume = block_job_user_resume,
593 .drain = block_job_drain,
595 .commit = backup_commit,
596 .abort = backup_abort,
597 .clean = backup_clean,
599 .attached_aio_context = backup_attached_aio_context,
600 .drain = backup_drain,
603 BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
604 BlockDriverState *target, int64_t speed,
605 MirrorSyncMode sync_mode, BdrvDirtyBitmap *sync_bitmap,
607 BlockdevOnError on_source_error,
608 BlockdevOnError on_target_error,
610 BlockCompletionFunc *cb, void *opaque,
611 JobTxn *txn, Error **errp)
615 BackupBlockJob *job = NULL;
622 error_setg(errp, "Source and target cannot be the same");
626 if (!bdrv_is_inserted(bs)) {
627 error_setg(errp, "Device is not inserted: %s",
628 bdrv_get_device_name(bs));
632 if (!bdrv_is_inserted(target)) {
633 error_setg(errp, "Device is not inserted: %s",
634 bdrv_get_device_name(target));
638 if (compress && target->drv->bdrv_co_pwritev_compressed == NULL) {
639 error_setg(errp, "Compression is not supported for this drive %s",
640 bdrv_get_device_name(target));
644 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
648 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) {
652 if (sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
654 error_setg(errp, "must provide a valid bitmap name for "
655 "\"incremental\" sync mode");
659 /* Create a new bitmap, and freeze/disable this one. */
660 if (bdrv_dirty_bitmap_create_successor(bs, sync_bitmap, errp) < 0) {
663 } else if (sync_bitmap) {
665 "a sync_bitmap was provided to backup_run, "
666 "but received an incompatible sync_mode (%s)",
667 MirrorSyncMode_str(sync_mode));
671 len = bdrv_getlength(bs);
673 error_setg_errno(errp, -len, "unable to get length for '%s'",
674 bdrv_get_device_name(bs));
678 /* job->len is fixed, so we can't allow resize */
679 job = block_job_create(job_id, &backup_job_driver, txn, bs,
680 BLK_PERM_CONSISTENT_READ,
681 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE |
682 BLK_PERM_WRITE_UNCHANGED | BLK_PERM_GRAPH_MOD,
683 speed, creation_flags, cb, opaque, errp);
688 /* The target must match the source in size, so no resize here either */
689 job->target = blk_new(BLK_PERM_WRITE,
690 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE |
691 BLK_PERM_WRITE_UNCHANGED | BLK_PERM_GRAPH_MOD);
692 ret = blk_insert_bs(job->target, target, errp);
697 job->on_source_error = on_source_error;
698 job->on_target_error = on_target_error;
699 job->sync_mode = sync_mode;
700 job->sync_bitmap = sync_mode == MIRROR_SYNC_MODE_INCREMENTAL ?
702 job->compress = compress;
704 /* If there is no backing file on the target, we cannot rely on COW if our
705 * backup cluster size is smaller than the target cluster size. Even for
706 * targets with a backing file, try to avoid COW if possible. */
707 ret = bdrv_get_info(target, &bdi);
708 if (ret == -ENOTSUP && !target->backing) {
709 /* Cluster size is not defined */
710 warn_report("The target block device doesn't provide "
711 "information about the block size and it doesn't have a "
712 "backing file. The default block size of %u bytes is "
713 "used. If the actual block size of the target exceeds "
714 "this default, the backup may be unusable",
715 BACKUP_CLUSTER_SIZE_DEFAULT);
716 job->cluster_size = BACKUP_CLUSTER_SIZE_DEFAULT;
717 } else if (ret < 0 && !target->backing) {
718 error_setg_errno(errp, -ret,
719 "Couldn't determine the cluster size of the target image, "
720 "which has no backing file");
721 error_append_hint(errp,
722 "Aborting, since this may create an unusable destination image\n");
724 } else if (ret < 0 && target->backing) {
725 /* Not fatal; just trudge on ahead. */
726 job->cluster_size = BACKUP_CLUSTER_SIZE_DEFAULT;
728 job->cluster_size = MAX(BACKUP_CLUSTER_SIZE_DEFAULT, bdi.cluster_size);
730 job->use_copy_range = true;
731 job->copy_range_size = MIN_NON_ZERO(blk_get_max_transfer(job->common.blk),
732 blk_get_max_transfer(job->target));
733 job->copy_range_size = MAX(job->cluster_size,
734 QEMU_ALIGN_UP(job->copy_range_size,
737 /* Required permissions are already taken with target's blk_new() */
738 block_job_add_bdrv(&job->common, "target", target, 0, BLK_PERM_ALL,
746 bdrv_reclaim_dirty_bitmap(bs, sync_bitmap, NULL);
749 backup_clean(&job->common.job);
750 job_early_fail(&job->common.job);