qemu-iotests: Test setting WCE with qdev
[qemu.git] / block / backup.c
blob2c0532314f5f796fe6b70897736647dc8da55683
1 /*
2 * QEMU backup
4 * Copyright (C) 2013 Proxmox Server Solutions
6 * Authors:
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"
16 #include "trace.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 {
31 int64_t start;
32 int64_t end;
33 QLIST_ENTRY(CowRequest) list;
34 CoQueue wait_queue; /* coroutines blocked on this request */
35 } CowRequest;
37 typedef struct BackupBlockJob {
38 BlockJob common;
39 BlockBackend *target;
40 /* bitmap for sync=incremental */
41 BdrvDirtyBitmap *sync_bitmap;
42 MirrorSyncMode sync_mode;
43 RateLimit limit;
44 BlockdevOnError on_source_error;
45 BlockdevOnError on_target_error;
46 CoRwlock flush_rwlock;
47 uint64_t sectors_read;
48 unsigned long *done_bitmap;
49 int64_t cluster_size;
50 NotifierWithReturn before_write;
51 QLIST_HEAD(, CowRequest) inflight_reqs;
52 } BackupBlockJob;
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,
62 int64_t start,
63 int64_t end)
65 CowRequest *req;
66 bool retry;
68 do {
69 retry = false;
70 QLIST_FOREACH(req, &job->inflight_reqs, list) {
71 if (end > req->start && start < req->end) {
72 qemu_co_queue_wait(&req->wait_queue);
73 retry = true;
74 break;
77 } while (retry);
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)
84 req->start = start;
85 req->end = 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,
99 bool *error_is_read,
100 bool is_write_notifier)
102 BlockBackend *blk = job->common.blk;
103 CowRequest cow_request;
104 struct iovec iov;
105 QEMUIOVector bounce_qiov;
106 void *bounce_buffer = NULL;
107 int ret = 0;
108 int64_t sectors_per_cluster = cluster_size_sectors(job);
109 int64_t start, end;
110 int n;
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);
144 if (ret < 0) {
145 trace_backup_do_cow_read_fail(job, start, ret);
146 if (error_is_read) {
147 *error_is_read = true;
149 goto out;
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);
155 } else {
156 ret = blk_co_pwritev(job->target, start * job->cluster_size,
157 bounce_qiov.size, &bounce_qiov, 0);
159 if (ret < 0) {
160 trace_backup_do_cow_write_fail(job, start, ret);
161 if (error_is_read) {
162 *error_is_read = false;
164 goto out;
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;
176 out:
177 if (bounce_buffer) {
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);
187 return ret;
190 static int coroutine_fn backup_before_write_notify(
191 NotifierWithReturn *notifier,
192 void *opaque)
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);
210 if (speed < 0) {
211 error_setg(errp, QERR_INVALID_PARAMETER, "speed");
212 return;
214 ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
217 static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret)
219 BdrvDirtyBitmap *bm;
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);
225 assert(bm);
226 } else {
227 /* Everything is fine, delete this bitmap and install the backup. */
228 bm = bdrv_dirty_bitmap_abdicate(bs, job->sync_bitmap, NULL);
229 assert(bm);
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)
268 if (read) {
269 return block_job_error_action(&job->common, job->on_source_error,
270 true, error);
271 } else {
272 return block_job_error_action(&job->common, job->on_target_error,
273 false, error);
277 typedef struct {
278 int ret;
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);
289 g_free(data);
292 static bool coroutine_fn yield_and_check(BackupBlockJob *job)
294 if (block_job_is_cancelled(&job->common)) {
295 return true;
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,
303 job->sectors_read);
304 job->sectors_read = 0;
305 block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, delay_ns);
306 } else {
307 block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, 0);
310 if (block_job_is_cancelled(&job->common)) {
311 return true;
314 return false;
317 static int coroutine_fn backup_run_incremental(BackupBlockJob *job)
319 bool error_is_read;
320 int ret = 0;
321 int clusters_per_iter;
322 uint32_t granularity;
323 int64_t sector;
324 int64_t cluster;
325 int64_t end;
326 int64_t last_cluster = -1;
327 int64_t sectors_per_cluster = cluster_size_sectors(job);
328 HBitmapIter hbi;
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) *
341 job->cluster_size);
344 for (end = cluster + clusters_per_iter; cluster < end; cluster++) {
345 do {
346 if (yield_and_check(job)) {
347 return ret;
349 ret = backup_do_cow(job, cluster * sectors_per_cluster,
350 sectors_per_cluster, &error_is_read,
351 false);
352 if ((ret < 0) &&
353 backup_error_action(job, error_is_read, -ret) ==
354 BLOCK_ERROR_ACTION_REPORT) {
355 return ret;
357 } while (ret < 0);
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);
375 return ret;
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;
384 int64_t start, end;
385 int64_t sectors_per_cluster = cluster_size_sectors(job);
386 int ret = 0;
388 QLIST_INIT(&job->inflight_reqs);
389 qemu_co_rwlock_init(&job->flush_rwlock);
391 start = 0;
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);
407 } else {
408 /* Both FULL and TOP SYNC_MODE's require copying.. */
409 for (; start < end; start++) {
410 bool error_is_read;
411 if (yield_and_check(job)) {
412 break;
415 if (job->sync_mode == MIRROR_SYNC_MODE_TOP) {
416 int i, n;
417 int alloced = 0;
419 /* Check to see if these blocks are already in the
420 * backing file. */
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. */
429 alloced =
430 bdrv_is_allocated(bs,
431 start * sectors_per_cluster + i,
432 sectors_per_cluster - i, &n);
433 i += n;
435 if (alloced == 1 || n == 0) {
436 break;
440 /* If the above loop never found any sectors that are in
441 * the topmost image, skip this backup. */
442 if (alloced == 0) {
443 continue;
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);
449 if (ret < 0) {
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) {
454 break;
455 } else {
456 start--;
457 continue;
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));
473 data->ret = ret;
474 block_job_defer_to_main_loop(&job->common, backup_complete, data);
477 void backup_start(const char *job_id, BlockDriverState *bs,
478 BlockDriverState *target, int64_t speed,
479 MirrorSyncMode sync_mode, BdrvDirtyBitmap *sync_bitmap,
480 BlockdevOnError on_source_error,
481 BlockdevOnError on_target_error,
482 BlockCompletionFunc *cb, void *opaque,
483 BlockJobTxn *txn, Error **errp)
485 int64_t len;
486 BlockDriverInfo bdi;
487 BackupBlockJob *job = NULL;
488 int ret;
490 assert(bs);
491 assert(target);
493 if (bs == target) {
494 error_setg(errp, "Source and target cannot be the same");
495 return;
498 if (!bdrv_is_inserted(bs)) {
499 error_setg(errp, "Device is not inserted: %s",
500 bdrv_get_device_name(bs));
501 return;
504 if (!bdrv_is_inserted(target)) {
505 error_setg(errp, "Device is not inserted: %s",
506 bdrv_get_device_name(target));
507 return;
510 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
511 return;
514 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) {
515 return;
518 if (sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
519 if (!sync_bitmap) {
520 error_setg(errp, "must provide a valid bitmap name for "
521 "\"incremental\" sync mode");
522 return;
525 /* Create a new bitmap, and freeze/disable this one. */
526 if (bdrv_dirty_bitmap_create_successor(bs, sync_bitmap, errp) < 0) {
527 return;
529 } else if (sync_bitmap) {
530 error_setg(errp,
531 "a sync_bitmap was provided to backup_run, "
532 "but received an incompatible sync_mode (%s)",
533 MirrorSyncMode_lookup[sync_mode]);
534 return;
537 len = bdrv_getlength(bs);
538 if (len < 0) {
539 error_setg_errno(errp, -len, "unable to get length for '%s'",
540 bdrv_get_device_name(bs));
541 goto error;
544 job = block_job_create(job_id, &backup_job_driver, bs, speed,
545 cb, opaque, errp);
546 if (!job) {
547 goto error;
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 ?
557 sync_bitmap : NULL;
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");
569 goto error;
570 } else if (ret < 0 && target->backing) {
571 /* Not fatal; just trudge on ahead. */
572 job->cluster_size = BACKUP_CLUSTER_SIZE_DEFAULT;
573 } else {
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, job);
580 block_job_txn_add_job(txn, &job->common);
581 qemu_coroutine_enter(job->common.co);
582 return;
584 error:
585 if (sync_bitmap) {
586 bdrv_reclaim_dirty_bitmap(bs, sync_bitmap, NULL);
588 if (job) {
589 blk_unref(job->target);
590 block_job_unref(&job->common);