1 From eb29d6acb8d8e671e292c9f50655bc87d9742282 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 v3 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 | 338 ++++++++++++++++++++++++++++++++++++++++++++++
21 block.c | 71 +++++++++-
22 include/block/block.h | 2 +
23 include/block/blockjob.h | 10 ++
24 6 files changed, 448 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 21e9c91..d72822b 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..61ca668
49 + * Copyright (C) 2012 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 DPRINTF(fmt, ...) \
72 + do { if (DEBUG_BACKUP) { printf("backup: " fmt, ## __VA_ARGS__); } } \
76 +#define SLICE_TIME 100000000ULL /* ns */
78 +typedef struct BackupBlockJob {
81 + uint64_t sectors_read;
82 + unsigned long *bitmap;
84 + BackupDumpFunc *backup_dump_cb;
85 + BlockDriverCompletionFunc *backup_complete_cb;
89 +static int backup_get_bitmap(BackupBlockJob *job, int64_t cluster_num)
92 + assert(job->bitmap);
94 + unsigned long val, idx, bit;
96 + idx = cluster_num / BITS_PER_LONG;
98 + assert(job->bitmap_size > idx);
100 + bit = cluster_num % BITS_PER_LONG;
101 + val = job->bitmap[idx];
103 + return !!(val & (1UL << bit));
106 +static void backup_set_bitmap(BackupBlockJob *job, int64_t cluster_num,
110 + assert(job->bitmap);
112 + unsigned long val, idx, bit;
114 + idx = cluster_num / BITS_PER_LONG;
116 + assert(job->bitmap_size > idx);
118 + bit = cluster_num % BITS_PER_LONG;
119 + val = job->bitmap[idx];
121 + if (!(val & (1UL << bit))) {
125 + if (val & (1UL << bit)) {
126 + val &= ~(1UL << bit);
129 + job->bitmap[idx] = val;
132 +static int backup_in_progress_count;
134 +static int coroutine_fn backup_do_cow(BlockDriverState *bs,
135 + int64_t sector_num, int nb_sectors)
138 + BackupBlockJob *job = (BackupBlockJob *)bs->job;
141 + BlockDriver *drv = bs->drv;
143 + QEMUIOVector bounce_qiov;
144 + void *bounce_buffer = NULL;
147 + backup_in_progress_count++;
149 + int64_t start, end;
151 + start = sector_num / BACKUP_BLOCKS_PER_CLUSTER;
152 + end = (sector_num + nb_sectors + BACKUP_BLOCKS_PER_CLUSTER - 1) /
153 + BACKUP_BLOCKS_PER_CLUSTER;
155 + DPRINTF("brdv_co_backup_cow enter %s C%zd %zd %d\n",
156 + bdrv_get_device_name(bs), start, sector_num, nb_sectors);
158 + for (; start < end; start++) {
159 + if (backup_get_bitmap(job, start)) {
160 + DPRINTF("brdv_co_backup_cow skip C%zd\n", start);
161 + continue; /* already copied */
164 + /* immediately set bitmap (avoid coroutine race) */
165 + backup_set_bitmap(job, start, 1);
167 + DPRINTF("brdv_co_backup_cow C%zd\n", start);
169 + if (!bounce_buffer) {
170 + iov.iov_len = BACKUP_CLUSTER_SIZE;
171 + iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len);
172 + qemu_iovec_init_external(&bounce_qiov, &iov, 1);
175 + ret = drv->bdrv_co_readv(bs, start * BACKUP_BLOCKS_PER_CLUSTER,
176 + BACKUP_BLOCKS_PER_CLUSTER,
179 + job->sectors_read += BACKUP_BLOCKS_PER_CLUSTER;
182 + DPRINTF("brdv_co_backup_cow bdrv_read C%zd failed\n", start);
186 + ret = job->backup_dump_cb(job->opaque, bs, start, bounce_buffer);
188 + DPRINTF("brdv_co_backup_cow dump_cluster_cb C%zd failed\n", start);
192 + DPRINTF("brdv_co_backup_cow done C%zd\n", start);
196 + if (bounce_buffer) {
197 + qemu_vfree(bounce_buffer);
200 + backup_in_progress_count--;
205 +static int coroutine_fn backup_before_read(BlockDriverState *bs,
206 + int64_t sector_num,
207 + int nb_sectors, QEMUIOVector *qiov)
209 + return backup_do_cow(bs, sector_num, nb_sectors);
212 +static int coroutine_fn backup_before_write(BlockDriverState *bs,
213 + int64_t sector_num,
214 + int nb_sectors, QEMUIOVector *qiov)
216 + return backup_do_cow(bs, sector_num, nb_sectors);
219 +static void backup_set_speed(BlockJob *job, int64_t speed, Error **errp)
221 + BackupBlockJob *s = container_of(job, BackupBlockJob, common);
224 + error_set(errp, QERR_INVALID_PARAMETER, "speed");
227 + ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
230 +static BlockJobType backup_job_type = {
231 + .instance_size = sizeof(BackupBlockJob),
232 + .before_read = backup_before_read,
233 + .before_write = backup_before_write,
234 + .job_type = "backup",
235 + .set_speed = backup_set_speed,
238 +static void coroutine_fn backup_run(void *opaque)
240 + BackupBlockJob *job = opaque;
241 + BlockDriverState *bs = job->common.bs;
244 + int64_t start, end;
247 + end = (bs->total_sectors + BACKUP_BLOCKS_PER_CLUSTER - 1) /
248 + BACKUP_BLOCKS_PER_CLUSTER;
250 + DPRINTF("backup_run start %s %zd %zd\n", bdrv_get_device_name(bs),
255 + for (; start < end; start++) {
256 + if (block_job_is_cancelled(&job->common)) {
261 + /* we need to yield so that qemu_aio_flush() returns.
262 + * (without, VM does not reboot)
263 + * Note: use 1000 instead of 0 (0 priorize this task too much)
265 + if (job->common.speed) {
266 + uint64_t delay_ns = ratelimit_calculate_delay(
267 + &job->limit, job->sectors_read);
268 + job->sectors_read = 0;
269 + block_job_sleep_ns(&job->common, rt_clock, delay_ns);
271 + block_job_sleep_ns(&job->common, rt_clock, 1000);
274 + if (block_job_is_cancelled(&job->common)) {
279 + if (backup_get_bitmap(job, start)) {
280 + continue; /* already copied */
283 + DPRINTF("backup_run loop C%zd\n", start);
286 + * This triggers a cluster copy
287 + * Note: avoid direct call to brdv_co_backup_cow, because
288 + * this does not call tracked_request_begin()
290 + ret = bdrv_co_backup(bs, start*BACKUP_BLOCKS_PER_CLUSTER, 1);
294 + /* Publish progress */
295 + job->common.offset += BACKUP_CLUSTER_SIZE;
298 + while (backup_in_progress_count > 0) {
299 + DPRINTF("backup_run backup_in_progress_count != 0 (%d)",
300 + backup_in_progress_count);
301 + block_job_sleep_ns(&job->common, rt_clock, 10000);
305 + DPRINTF("backup_run complete %d\n", ret);
306 + block_job_completed(&job->common, ret);
309 +static void backup_job_cleanup_cb(void *opaque, int ret)
311 + BlockDriverState *bs = opaque;
313 + BackupBlockJob *job = (BackupBlockJob *)bs->job;
316 + DPRINTF("backup_job_cleanup_cb start %d\n", ret);
318 + job->backup_complete_cb(job->opaque, ret);
320 + DPRINTF("backup_job_cleanup_cb end\n");
322 + g_free(job->bitmap);
326 +backup_job_start(BlockDriverState *bs, bool cancel)
330 + assert(bs->job->co == NULL);
333 + block_job_cancel(bs->job); /* set cancel flag */
336 + bs->job->co = qemu_coroutine_create(backup_run);
337 + qemu_coroutine_enter(bs->job->co, bs->job);
341 +backup_job_create(BlockDriverState *bs, BackupDumpFunc *backup_dump_cb,
342 + BlockDriverCompletionFunc *backup_complete_cb,
343 + void *opaque, int64_t speed)
346 + assert(backup_dump_cb);
347 + assert(backup_complete_cb);
350 + DPRINTF("bdrv_backup_init failed - running job on %s\n",
351 + bdrv_get_device_name(bs));
355 + int64_t bitmap_size;
356 + const char *devname = bdrv_get_device_name(bs);
358 + if (!devname || !devname[0]) {
362 + DPRINTF("bdrv_backup_init %s\n", bdrv_get_device_name(bs));
365 + BackupBlockJob *job = block_job_create(&backup_job_type, bs, speed,
366 + backup_job_cleanup_cb, bs, &errp);
368 + job->common.cluster_size = BACKUP_CLUSTER_SIZE;
370 + bitmap_size = bs->total_sectors +
371 + BACKUP_BLOCKS_PER_CLUSTER * BITS_PER_LONG - 1;
372 + bitmap_size /= BACKUP_BLOCKS_PER_CLUSTER * BITS_PER_LONG;
374 + job->backup_dump_cb = backup_dump_cb;
375 + job->backup_complete_cb = backup_complete_cb;
376 + job->opaque = opaque;
377 + job->bitmap_size = bitmap_size;
378 + job->bitmap = g_new0(unsigned long, bitmap_size);
380 + job->common.len = bs->total_sectors*BDRV_SECTOR_SIZE;
384 diff --git a/backup.h b/backup.h
386 index 0000000..20a9016
391 + * QEMU backup related definitions
393 + * Copyright (C) Proxmox Server Solutions
396 + * Dietmar Maurer (dietmar@proxmox.com)
398 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
399 + * See the COPYING file in the top-level directory.
403 +#ifndef QEMU_BACKUP_H
404 +#define QEMU_BACKUP_H
406 +#include <uuid/uuid.h>
408 +#define BACKUP_CLUSTER_BITS 16
409 +#define BACKUP_CLUSTER_SIZE (1<<BACKUP_CLUSTER_BITS)
410 +#define BACKUP_BLOCKS_PER_CLUSTER (BACKUP_CLUSTER_SIZE/BDRV_SECTOR_SIZE)
412 +typedef int BackupDumpFunc(void *opaque, BlockDriverState *bs,
413 + int64_t cluster_num, unsigned char *buf);
415 +void backup_job_start(BlockDriverState *bs, bool cancel);
417 +int backup_job_create(BlockDriverState *bs, BackupDumpFunc *backup_dump_cb,
418 + BlockDriverCompletionFunc *backup_complete_cb,
419 + void *opaque, int64_t speed);
421 +#endif /* QEMU_BACKUP_H */
422 diff --git a/block.c b/block.c
423 index 50dab8e..6e6d08f 100644
428 BDRV_REQ_COPY_ON_READ = 0x1,
429 BDRV_REQ_ZERO_WRITE = 0x2,
430 + BDRV_REQ_BACKUP_ONLY = 0x4,
433 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
434 @@ -1554,7 +1555,7 @@ int bdrv_commit(BlockDriverState *bs)
440 if (!bs->backing_hd) {
443 @@ -1691,6 +1692,22 @@ void bdrv_round_to_clusters(BlockDriverState *bs,
448 + * Round a region to job cluster boundaries
450 +static void round_to_job_clusters(BlockDriverState *bs,
451 + int64_t sector_num, int nb_sectors,
452 + int job_cluster_size,
453 + int64_t *cluster_sector_num,
454 + int *cluster_nb_sectors)
456 + int64_t c = job_cluster_size/BDRV_SECTOR_SIZE;
458 + *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c);
459 + *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num +
463 static bool tracked_request_overlaps(BdrvTrackedRequest *req,
464 int64_t sector_num, int nb_sectors) {
466 @@ -1705,7 +1722,9 @@ static bool tracked_request_overlaps(BdrvTrackedRequest *req,
469 static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
470 - int64_t sector_num, int nb_sectors)
471 + int64_t sector_num,
473 + int job_cluster_size)
475 BdrvTrackedRequest *req;
476 int64_t cluster_sector_num;
477 @@ -1721,6 +1740,11 @@ static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
478 bdrv_round_to_clusters(bs, sector_num, nb_sectors,
479 &cluster_sector_num, &cluster_nb_sectors);
481 + if (job_cluster_size) {
482 + round_to_job_clusters(bs, sector_num, nb_sectors, job_cluster_size,
483 + &cluster_sector_num, &cluster_nb_sectors);
488 QLIST_FOREACH(req, &bs->tracked_requests, list) {
489 @@ -2260,12 +2284,24 @@ static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
490 bs->copy_on_read_in_flight++;
493 - if (bs->copy_on_read_in_flight) {
494 - wait_for_overlapping_requests(bs, sector_num, nb_sectors);
495 + int job_cluster_size = bs->job && bs->job->cluster_size ?
496 + bs->job->cluster_size : 0;
498 + if (bs->copy_on_read_in_flight || job_cluster_size) {
499 + wait_for_overlapping_requests(bs, sector_num, nb_sectors,
503 tracked_request_begin(&req, bs, sector_num, nb_sectors, false);
505 + if (bs->job && bs->job->job_type->before_read) {
506 + ret = bs->job->job_type->before_read(bs, sector_num, nb_sectors, qiov);
507 + if ((ret < 0) || (flags & BDRV_REQ_BACKUP_ONLY)) {
508 + /* Note: We do not return any data to the caller */
513 if (flags & BDRV_REQ_COPY_ON_READ) {
516 @@ -2309,6 +2345,17 @@ int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
517 BDRV_REQ_COPY_ON_READ);
520 +int coroutine_fn bdrv_co_backup(BlockDriverState *bs,
521 + int64_t sector_num, int nb_sectors)
527 + return bdrv_co_do_readv(bs, sector_num, nb_sectors, NULL,
528 + BDRV_REQ_BACKUP_ONLY);
531 static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
532 int64_t sector_num, int nb_sectors)
534 @@ -2366,12 +2413,23 @@ static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
535 bdrv_io_limits_intercept(bs, true, nb_sectors);
538 - if (bs->copy_on_read_in_flight) {
539 - wait_for_overlapping_requests(bs, sector_num, nb_sectors);
540 + int job_cluster_size = bs->job && bs->job->cluster_size ?
541 + bs->job->cluster_size : 0;
543 + if (bs->copy_on_read_in_flight || job_cluster_size) {
544 + wait_for_overlapping_requests(bs, sector_num, nb_sectors,
548 tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
550 + if (bs->job && bs->job->job_type->before_write) {
551 + ret = bs->job->job_type->before_write(bs, sector_num, nb_sectors, qiov);
557 if (flags & BDRV_REQ_ZERO_WRITE) {
558 ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors);
560 @@ -2390,6 +2448,7 @@ static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
561 bs->wr_highest_sector = sector_num + nb_sectors - 1;
565 tracked_request_end(&req);
568 diff --git a/include/block/block.h b/include/block/block.h
569 index 5c3b911..b6144be 100644
570 --- a/include/block/block.h
571 +++ b/include/block/block.h
572 @@ -172,6 +172,8 @@ int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
573 int nb_sectors, QEMUIOVector *qiov);
574 int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
575 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
576 +int coroutine_fn bdrv_co_backup(BlockDriverState *bs,
577 + int64_t sector_num, int nb_sectors);
578 int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
579 int nb_sectors, QEMUIOVector *qiov);
581 diff --git a/include/block/blockjob.h b/include/block/blockjob.h
582 index c290d07..6f42495 100644
583 --- a/include/block/blockjob.h
584 +++ b/include/block/blockjob.h
585 @@ -50,6 +50,13 @@ typedef struct BlockJobType {
588 void (*complete)(BlockJob *job, Error **errp);
590 + /** tracked requests */
591 + int coroutine_fn (*before_read)(BlockDriverState *bs, int64_t sector_num,
592 + int nb_sectors, QEMUIOVector *qiov);
593 + int coroutine_fn (*before_write)(BlockDriverState *bs, int64_t sector_num,
594 + int nb_sectors, QEMUIOVector *qiov);
599 @@ -103,6 +110,9 @@ struct BlockJob {
600 /** Speed that was set with @block_job_set_speed. */
603 + /** tracked requests */
606 /** The completion function that will be called when the job completes. */
607 BlockDriverCompletionFunc *cb;