1 From 1d0c6dfc9616c0dd17986cab6744c10cb748de1e Mon Sep 17 00:00:00 2001
2 From: Dietmar Maurer <dietmar@proxmox.com>
3 Date: Tue, 13 Nov 2012 10:03:52 +0100
4 Subject: [PATCH v5 2/6] add basic backup support to block driver
6 Function backup_job_create() creates a block job to backup a block device.
7 The coroutine is started with backup_job_start().
9 We call backup_do_cow() for each write during backup. That function
10 reads the original data and pass it to backup_dump_cb().
12 The tracked_request infrastructure is used to serialize access.
14 Currently backup cluster size is hardcoded to 65536 bytes.
16 Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
19 backup.c | 355 ++++++++++++++++++++++++++++++++++++++++++++++
21 block.c | 71 +++++++++-
22 include/block/block.h | 2 +
23 include/block/blockjob.h | 10 ++
24 6 files changed, 463 insertions(+), 6 deletions(-)
25 create mode 100644 backup.c
26 create mode 100644 backup.h
28 diff --git a/Makefile.objs b/Makefile.objs
29 index a68cdac..df64f70 100644
32 @@ -13,6 +13,7 @@ block-obj-$(CONFIG_POSIX) += aio-posix.o
33 block-obj-$(CONFIG_WIN32) += aio-win32.o
35 block-obj-y += qapi-types.o qapi-visit.o
36 +block-obj-y += backup.o
38 block-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o
39 block-obj-y += qemu-coroutine-sleep.o
40 diff --git a/backup.c b/backup.c
42 index 0000000..8955e1a
49 + * Copyright (C) 2013 Proxmox Server Solutions
52 + * Dietmar Maurer (dietmar@proxmox.com)
54 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
55 + * See the COPYING file in the top-level directory.
63 +#include "block/block.h"
64 +#include "block/block_int.h"
65 +#include "block/blockjob.h"
66 +#include "qemu/ratelimit.h"
69 +#define DEBUG_BACKUP 0
71 +#define USE_ALLOCATION_CHECK 0
73 +#define DPRINTF(fmt, ...) \
74 + do { if (DEBUG_BACKUP) { printf("backup: " fmt, ## __VA_ARGS__); } } \
78 +#define SLICE_TIME 100000000ULL /* ns */
80 +typedef struct BackupBlockJob {
84 + uint64_t sectors_read;
85 + unsigned long *bitmap;
87 + BackupDumpFunc *backup_dump_cb;
88 + BlockDriverCompletionFunc *backup_complete_cb;
92 +static bool backup_get_bitmap(BackupBlockJob *job, int64_t cluster_num)
95 + assert(job->bitmap);
97 + unsigned long val, idx, bit;
99 + idx = cluster_num / BITS_PER_LONG;
101 + assert(job->bitmap_size > idx);
103 + bit = cluster_num % BITS_PER_LONG;
104 + val = job->bitmap[idx];
106 + return !!(val & (1UL << bit));
109 +static void backup_set_bitmap(BackupBlockJob *job, int64_t cluster_num,
113 + assert(job->bitmap);
115 + unsigned long val, idx, bit;
117 + idx = cluster_num / BITS_PER_LONG;
119 + assert(job->bitmap_size > idx);
121 + bit = cluster_num % BITS_PER_LONG;
122 + val = job->bitmap[idx];
126 + val &= ~(1UL << bit);
128 + job->bitmap[idx] = val;
131 +static int coroutine_fn backup_do_cow(BlockDriverState *bs,
132 + int64_t sector_num, int nb_sectors)
135 + BackupBlockJob *job = (BackupBlockJob *)bs->job;
138 + BlockDriver *drv = bs->drv;
140 + QEMUIOVector bounce_qiov;
141 + void *bounce_buffer = NULL;
144 + qemu_co_rwlock_rdlock(&job->rwlock);
146 + int64_t start, end;
148 + start = sector_num / BACKUP_BLOCKS_PER_CLUSTER;
149 + end = (sector_num + nb_sectors + BACKUP_BLOCKS_PER_CLUSTER - 1) /
150 + BACKUP_BLOCKS_PER_CLUSTER;
152 + DPRINTF("brdv_co_backup_cow enter %s C%" PRId64 " %" PRId64 " %d\n",
153 + bdrv_get_device_name(bs), start, sector_num, nb_sectors);
155 + for (; start < end; start++) {
158 + if (backup_get_bitmap(job, start)) {
159 + DPRINTF("brdv_co_backup_cow skip C%" PRId64 "\n", start);
160 + continue; /* already copied */
163 + /* immediately set bitmap (avoid coroutine race) */
164 + backup_set_bitmap(job, start, 1);
166 + DPRINTF("brdv_co_backup_cow C%" PRId64 "\n", start);
168 + if (!bounce_buffer) {
169 + iov.iov_len = BACKUP_CLUSTER_SIZE;
170 + iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len);
171 + qemu_iovec_init_external(&bounce_qiov, &iov, 1);
174 +#if USE_ALLOCATION_CHECK
176 + ret = bdrv_co_is_allocated_above(bs, NULL,
177 + start * BACKUP_BLOCKS_PER_CLUSTER,
178 + BACKUP_BLOCKS_PER_CLUSTER, &n);
180 + DPRINTF("brdv_co_backup_cow is_allocated C%" PRId64 " failed\n",
185 + zero = (ret == 0) && (n == BACKUP_BLOCKS_PER_CLUSTER);
189 + ret = drv->bdrv_co_readv(bs, start * BACKUP_BLOCKS_PER_CLUSTER,
190 + BACKUP_BLOCKS_PER_CLUSTER,
193 + DPRINTF("brdv_co_backup_cow bdrv_read C%" PRId64 " failed\n",
197 +#if USE_ALLOCATION_CHECK
200 + job->sectors_read += BACKUP_BLOCKS_PER_CLUSTER;
202 + ret = job->backup_dump_cb(job->opaque, bs, start,
203 + zero ? NULL : bounce_buffer);
205 + DPRINTF("brdv_co_backup_cow dump_cluster_cb C%" PRId64 " failed\n",
210 + DPRINTF("brdv_co_backup_cow done C%" PRId64 "\n", start);
214 + if (bounce_buffer) {
215 + qemu_vfree(bounce_buffer);
218 + qemu_co_rwlock_unlock(&job->rwlock);
223 +static int coroutine_fn backup_before_read(BlockDriverState *bs,
224 + int64_t sector_num,
225 + int nb_sectors, QEMUIOVector *qiov)
227 + return backup_do_cow(bs, sector_num, nb_sectors);
230 +static int coroutine_fn backup_before_write(BlockDriverState *bs,
231 + int64_t sector_num,
232 + int nb_sectors, QEMUIOVector *qiov)
234 + return backup_do_cow(bs, sector_num, nb_sectors);
237 +static void backup_set_speed(BlockJob *job, int64_t speed, Error **errp)
239 + BackupBlockJob *s = container_of(job, BackupBlockJob, common);
242 + error_set(errp, QERR_INVALID_PARAMETER, "speed");
245 + ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
248 +static BlockJobType backup_job_type = {
249 + .instance_size = sizeof(BackupBlockJob),
250 + .before_read = backup_before_read,
251 + .before_write = backup_before_write,
252 + .job_type = "backup",
253 + .set_speed = backup_set_speed,
256 +static void coroutine_fn backup_run(void *opaque)
258 + BackupBlockJob *job = opaque;
259 + BlockDriverState *bs = job->common.bs;
262 + int64_t start, end;
265 + end = (bs->total_sectors + BACKUP_BLOCKS_PER_CLUSTER - 1) /
266 + BACKUP_BLOCKS_PER_CLUSTER;
268 + DPRINTF("backup_run start %s %" PRId64 " %" PRId64 "\n",
269 + bdrv_get_device_name(bs), start, end);
273 + for (; start < end; start++) {
274 + if (block_job_is_cancelled(&job->common)) {
279 + /* we need to yield so that qemu_aio_flush() returns.
280 + * (without, VM does not reboot)
281 + * Note: use 1000 instead of 0 (0 prioritize this task too much)
283 + if (job->common.speed) {
284 + uint64_t delay_ns = ratelimit_calculate_delay(
285 + &job->limit, job->sectors_read);
286 + job->sectors_read = 0;
287 + block_job_sleep_ns(&job->common, rt_clock, delay_ns);
289 + block_job_sleep_ns(&job->common, rt_clock, 1000);
292 + if (block_job_is_cancelled(&job->common)) {
297 + if (backup_get_bitmap(job, start)) {
298 + continue; /* already copied */
301 + DPRINTF("backup_run loop C%" PRId64 "\n", start);
304 + * This triggers a cluster copy
305 + * Note: avoid direct call to brdv_co_backup_cow, because
306 + * this does not call tracked_request_begin()
308 + ret = bdrv_co_backup(bs, start*BACKUP_BLOCKS_PER_CLUSTER, 1);
312 + /* Publish progress */
313 + job->common.offset += BACKUP_CLUSTER_SIZE;
316 + /* wait until pending backup_do_cow()calls have completed */
317 + qemu_co_rwlock_wrlock(&job->rwlock);
318 + qemu_co_rwlock_unlock(&job->rwlock);
320 + DPRINTF("backup_run complete %d\n", ret);
321 + block_job_completed(&job->common, ret);
324 +static void backup_job_cleanup_cb(void *opaque, int ret)
326 + BlockDriverState *bs = opaque;
328 + BackupBlockJob *job = (BackupBlockJob *)bs->job;
331 + DPRINTF("backup_job_cleanup_cb start %d\n", ret);
333 + job->backup_complete_cb(job->opaque, ret);
335 + DPRINTF("backup_job_cleanup_cb end\n");
337 + g_free(job->bitmap);
341 +backup_job_start(BlockDriverState *bs, bool cancel)
345 + assert(bs->job->co == NULL);
348 + block_job_cancel(bs->job); /* set cancel flag */
351 + bs->job->co = qemu_coroutine_create(backup_run);
352 + qemu_coroutine_enter(bs->job->co, bs->job);
356 +backup_job_create(BlockDriverState *bs, BackupDumpFunc *backup_dump_cb,
357 + BlockDriverCompletionFunc *backup_complete_cb,
358 + void *opaque, int64_t speed)
361 + assert(backup_dump_cb);
362 + assert(backup_complete_cb);
365 + DPRINTF("bdrv_backup_init failed - running job on %s\n",
366 + bdrv_get_device_name(bs));
370 + int64_t bitmap_size;
371 + const char *devname = bdrv_get_device_name(bs);
373 + if (!devname || !devname[0]) {
377 + DPRINTF("bdrv_backup_init %s\n", bdrv_get_device_name(bs));
380 + BackupBlockJob *job = block_job_create(&backup_job_type, bs, speed,
381 + backup_job_cleanup_cb, bs, &errp);
383 + qemu_co_rwlock_init(&job->rwlock);
385 + job->common.cluster_size = BACKUP_CLUSTER_SIZE;
387 + bitmap_size = bs->total_sectors +
388 + BACKUP_BLOCKS_PER_CLUSTER * BITS_PER_LONG - 1;
389 + bitmap_size /= BACKUP_BLOCKS_PER_CLUSTER * BITS_PER_LONG;
391 + job->backup_dump_cb = backup_dump_cb;
392 + job->backup_complete_cb = backup_complete_cb;
393 + job->opaque = opaque;
394 + job->bitmap_size = bitmap_size;
395 + job->bitmap = g_new0(unsigned long, bitmap_size);
397 + job->common.len = bs->total_sectors*BDRV_SECTOR_SIZE;
401 diff --git a/backup.h b/backup.h
403 index 0000000..9b1ea1c
408 + * QEMU backup related definitions
410 + * Copyright (C) 2013 Proxmox Server Solutions
413 + * Dietmar Maurer (dietmar@proxmox.com)
415 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
416 + * See the COPYING file in the top-level directory.
420 +#ifndef QEMU_BACKUP_H
421 +#define QEMU_BACKUP_H
423 +#define BACKUP_CLUSTER_BITS 16
424 +#define BACKUP_CLUSTER_SIZE (1<<BACKUP_CLUSTER_BITS)
425 +#define BACKUP_BLOCKS_PER_CLUSTER (BACKUP_CLUSTER_SIZE/BDRV_SECTOR_SIZE)
427 +typedef int BackupDumpFunc(void *opaque, BlockDriverState *bs,
428 + int64_t cluster_num, unsigned char *buf);
430 +void backup_job_start(BlockDriverState *bs, bool cancel);
432 +int backup_job_create(BlockDriverState *bs, BackupDumpFunc *backup_dump_cb,
433 + BlockDriverCompletionFunc *backup_complete_cb,
434 + void *opaque, int64_t speed);
436 +#endif /* QEMU_BACKUP_H */
437 diff --git a/block.c b/block.c
438 index 50dab8e..6e6d08f 100644
443 BDRV_REQ_COPY_ON_READ = 0x1,
444 BDRV_REQ_ZERO_WRITE = 0x2,
445 + BDRV_REQ_BACKUP_ONLY = 0x4,
448 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
449 @@ -1554,7 +1555,7 @@ int bdrv_commit(BlockDriverState *bs)
455 if (!bs->backing_hd) {
458 @@ -1691,6 +1692,22 @@ void bdrv_round_to_clusters(BlockDriverState *bs,
463 + * Round a region to job cluster boundaries
465 +static void round_to_job_clusters(BlockDriverState *bs,
466 + int64_t sector_num, int nb_sectors,
467 + int job_cluster_size,
468 + int64_t *cluster_sector_num,
469 + int *cluster_nb_sectors)
471 + int64_t c = job_cluster_size/BDRV_SECTOR_SIZE;
473 + *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c);
474 + *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num +
478 static bool tracked_request_overlaps(BdrvTrackedRequest *req,
479 int64_t sector_num, int nb_sectors) {
481 @@ -1705,7 +1722,9 @@ static bool tracked_request_overlaps(BdrvTrackedRequest *req,
484 static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
485 - int64_t sector_num, int nb_sectors)
486 + int64_t sector_num,
488 + int job_cluster_size)
490 BdrvTrackedRequest *req;
491 int64_t cluster_sector_num;
492 @@ -1721,6 +1740,11 @@ static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
493 bdrv_round_to_clusters(bs, sector_num, nb_sectors,
494 &cluster_sector_num, &cluster_nb_sectors);
496 + if (job_cluster_size) {
497 + round_to_job_clusters(bs, sector_num, nb_sectors, job_cluster_size,
498 + &cluster_sector_num, &cluster_nb_sectors);
503 QLIST_FOREACH(req, &bs->tracked_requests, list) {
504 @@ -2260,12 +2284,24 @@ static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
505 bs->copy_on_read_in_flight++;
508 - if (bs->copy_on_read_in_flight) {
509 - wait_for_overlapping_requests(bs, sector_num, nb_sectors);
510 + int job_cluster_size = bs->job && bs->job->cluster_size ?
511 + bs->job->cluster_size : 0;
513 + if (bs->copy_on_read_in_flight || job_cluster_size) {
514 + wait_for_overlapping_requests(bs, sector_num, nb_sectors,
518 tracked_request_begin(&req, bs, sector_num, nb_sectors, false);
520 + if (bs->job && bs->job->job_type->before_read) {
521 + ret = bs->job->job_type->before_read(bs, sector_num, nb_sectors, qiov);
522 + if ((ret < 0) || (flags & BDRV_REQ_BACKUP_ONLY)) {
523 + /* Note: We do not return any data to the caller */
528 if (flags & BDRV_REQ_COPY_ON_READ) {
531 @@ -2309,6 +2345,17 @@ int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
532 BDRV_REQ_COPY_ON_READ);
535 +int coroutine_fn bdrv_co_backup(BlockDriverState *bs,
536 + int64_t sector_num, int nb_sectors)
542 + return bdrv_co_do_readv(bs, sector_num, nb_sectors, NULL,
543 + BDRV_REQ_BACKUP_ONLY);
546 static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
547 int64_t sector_num, int nb_sectors)
549 @@ -2366,12 +2413,23 @@ static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
550 bdrv_io_limits_intercept(bs, true, nb_sectors);
553 - if (bs->copy_on_read_in_flight) {
554 - wait_for_overlapping_requests(bs, sector_num, nb_sectors);
555 + int job_cluster_size = bs->job && bs->job->cluster_size ?
556 + bs->job->cluster_size : 0;
558 + if (bs->copy_on_read_in_flight || job_cluster_size) {
559 + wait_for_overlapping_requests(bs, sector_num, nb_sectors,
563 tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
565 + if (bs->job && bs->job->job_type->before_write) {
566 + ret = bs->job->job_type->before_write(bs, sector_num, nb_sectors, qiov);
572 if (flags & BDRV_REQ_ZERO_WRITE) {
573 ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors);
575 @@ -2390,6 +2448,7 @@ static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
576 bs->wr_highest_sector = sector_num + nb_sectors - 1;
580 tracked_request_end(&req);
583 diff --git a/include/block/block.h b/include/block/block.h
584 index 5c3b911..b6144be 100644
585 --- a/include/block/block.h
586 +++ b/include/block/block.h
587 @@ -172,6 +172,8 @@ int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
588 int nb_sectors, QEMUIOVector *qiov);
589 int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
590 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
591 +int coroutine_fn bdrv_co_backup(BlockDriverState *bs,
592 + int64_t sector_num, int nb_sectors);
593 int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
594 int nb_sectors, QEMUIOVector *qiov);
596 diff --git a/include/block/blockjob.h b/include/block/blockjob.h
597 index c290d07..6f42495 100644
598 --- a/include/block/blockjob.h
599 +++ b/include/block/blockjob.h
600 @@ -50,6 +50,13 @@ typedef struct BlockJobType {
603 void (*complete)(BlockJob *job, Error **errp);
605 + /** tracked requests */
606 + int coroutine_fn (*before_read)(BlockDriverState *bs, int64_t sector_num,
607 + int nb_sectors, QEMUIOVector *qiov);
608 + int coroutine_fn (*before_write)(BlockDriverState *bs, int64_t sector_num,
609 + int nb_sectors, QEMUIOVector *qiov);
614 @@ -103,6 +110,9 @@ struct BlockJob {
615 /** Speed that was set with @block_job_set_speed. */
618 + /** tracked requests */
621 /** The completion function that will be called when the job completes. */
622 BlockDriverCompletionFunc *cb;