1 From 26891c19f68a14927e8c52417112d80668be1b3a 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 | 335 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 block.c | 71 +++++++++++-
24 6 files changed, 445 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 3c7abca..cb46be5 100644
32 @@ -48,6 +48,7 @@ coroutine-obj-$(CONFIG_WIN32) += coroutine-win32.o
33 block-obj-y = iov.o cache-utils.o qemu-option.o module.o async.o
34 block-obj-y += nbd.o block.o blockjob.o aes.o qemu-config.o
35 block-obj-y += thread-pool.o qemu-progress.o qemu-sockets.o uri.o notify.o
36 +block-obj-y += backup.o
37 block-obj-y += $(coroutine-obj-y) $(qobject-obj-y) $(version-obj-y)
38 block-obj-$(CONFIG_POSIX) += event_notifier-posix.o aio-posix.o
39 block-obj-$(CONFIG_WIN32) += event_notifier-win32.o aio-win32.o
40 diff --git a/backup.c b/backup.c
42 index 0000000..3ccb74c
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.
64 +#include "block_int.h"
65 +#include "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 BITS_PER_LONG (sizeof(unsigned long) * 8)
77 +#define SLICE_TIME 100000000ULL /* ns */
79 +typedef struct BackupBlockJob {
82 + uint64_t sectors_read;
83 + unsigned long *bitmap;
85 + BackupDumpFunc *backup_dump_cb;
86 + BlockDriverCompletionFunc *backup_complete_cb;
90 +static int backup_get_bitmap(BackupBlockJob *job, int64_t cluster_num)
93 + assert(job->bitmap);
95 + unsigned long val, idx, bit;
97 + idx = cluster_num / BITS_PER_LONG;
99 + assert(job->bitmap_size > idx);
101 + bit = cluster_num % BITS_PER_LONG;
102 + val = job->bitmap[idx];
104 + return !!(val & (1UL << bit));
107 +static void backup_set_bitmap(BackupBlockJob *job, int64_t cluster_num,
111 + assert(job->bitmap);
113 + unsigned long val, idx, bit;
115 + idx = cluster_num / BITS_PER_LONG;
117 + assert(job->bitmap_size > idx);
119 + bit = cluster_num % BITS_PER_LONG;
120 + val = job->bitmap[idx];
122 + if (!(val & (1UL << bit))) {
126 + if (val & (1UL << bit)) {
127 + val &= ~(1UL << bit);
130 + job->bitmap[idx] = val;
133 +static int backup_in_progress_count;
135 +static int coroutine_fn backup_do_cow(BlockDriverState *bs,
136 + int64_t sector_num, int nb_sectors)
139 + BackupBlockJob *job = (BackupBlockJob *)bs->job;
142 + BlockDriver *drv = bs->drv;
144 + QEMUIOVector bounce_qiov;
145 + void *bounce_buffer = NULL;
148 + backup_in_progress_count++;
150 + int64_t start, end;
152 + start = sector_num / BACKUP_BLOCKS_PER_CLUSTER;
153 + end = (sector_num + nb_sectors + BACKUP_BLOCKS_PER_CLUSTER - 1) /
154 + BACKUP_BLOCKS_PER_CLUSTER;
156 + DPRINTF("brdv_co_backup_cow enter %s C%zd %zd %d\n",
157 + bdrv_get_device_name(bs), start, sector_num, nb_sectors);
159 + for (; start < end; start++) {
160 + if (backup_get_bitmap(job, start)) {
161 + DPRINTF("brdv_co_backup_cow skip C%zd\n", start);
162 + continue; /* already copied */
165 + /* immediately set bitmap (avoid coroutine race) */
166 + backup_set_bitmap(job, start, 1);
168 + DPRINTF("brdv_co_backup_cow C%zd\n", start);
170 + if (!bounce_buffer) {
171 + iov.iov_len = BACKUP_CLUSTER_SIZE;
172 + iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len);
173 + qemu_iovec_init_external(&bounce_qiov, &iov, 1);
176 + ret = drv->bdrv_co_readv(bs, start * BACKUP_BLOCKS_PER_CLUSTER,
177 + BACKUP_BLOCKS_PER_CLUSTER,
180 + job->sectors_read += BACKUP_BLOCKS_PER_CLUSTER;
183 + DPRINTF("brdv_co_backup_cow bdrv_read C%zd failed\n", start);
187 + ret = job->backup_dump_cb(job->opaque, bs, start, bounce_buffer);
189 + DPRINTF("brdv_co_backup_cow dump_cluster_cb C%zd failed\n", start);
193 + DPRINTF("brdv_co_backup_cow done C%zd\n", start);
197 + if (bounce_buffer) {
198 + qemu_vfree(bounce_buffer);
201 + backup_in_progress_count--;
206 +static int coroutine_fn backup_before_read(BlockDriverState *bs,
207 + int64_t sector_num,
208 + int nb_sectors, QEMUIOVector *qiov)
210 + return backup_do_cow(bs, sector_num, nb_sectors);
213 +static int coroutine_fn backup_before_write(BlockDriverState *bs,
214 + int64_t sector_num,
215 + int nb_sectors, QEMUIOVector *qiov)
217 + return backup_do_cow(bs, sector_num, nb_sectors);
220 +static void backup_set_speed(BlockJob *job, int64_t speed, Error **errp)
222 + BackupBlockJob *s = container_of(job, BackupBlockJob, common);
225 + error_set(errp, QERR_INVALID_PARAMETER, "speed");
228 + ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
231 +static BlockJobType backup_job_type = {
232 + .instance_size = sizeof(BackupBlockJob),
233 + .before_read = backup_before_read,
234 + .before_write = backup_before_write,
235 + .job_type = "backup",
236 + .set_speed = backup_set_speed,
239 +static void coroutine_fn backup_run(void *opaque)
241 + BackupBlockJob *job = opaque;
242 + BlockDriverState *bs = job->common.bs;
245 + int64_t start, end;
248 + end = (bs->total_sectors + BACKUP_BLOCKS_PER_CLUSTER - 1) /
249 + BACKUP_BLOCKS_PER_CLUSTER;
251 + DPRINTF("backup_run start %s %zd %zd\n", bdrv_get_device_name(bs),
256 + for (; start < end; start++) {
257 + if (block_job_is_cancelled(&job->common)) {
262 + /* we need to yield so that qemu_aio_flush() returns.
263 + * (without, VM does not reboot)
264 + * Note: use 1000 instead of 0 (0 priorize this task too much)
266 + if (job->common.speed) {
267 + uint64_t delay_ns = ratelimit_calculate_delay(
268 + &job->limit, job->sectors_read);
269 + job->sectors_read = 0;
270 + block_job_sleep_ns(&job->common, rt_clock, delay_ns);
272 + block_job_sleep_ns(&job->common, rt_clock, 1000);
275 + if (block_job_is_cancelled(&job->common)) {
280 + if (backup_get_bitmap(job, start)) {
281 + continue; /* already copied */
284 + DPRINTF("backup_run loop C%zd\n", start);
287 + * This triggers a cluster copy
288 + * Note: avoid direct call to brdv_co_backup_cow, because
289 + * this does not call tracked_request_begin()
291 + ret = bdrv_co_backup(bs, start*BACKUP_BLOCKS_PER_CLUSTER, 1);
295 + /* Publish progress */
296 + job->common.offset += BACKUP_CLUSTER_SIZE;
299 + while (backup_in_progress_count > 0) {
300 + DPRINTF("backup_run backup_in_progress_count != 0 (%d)",
301 + backup_in_progress_count);
302 + block_job_sleep_ns(&job->common, rt_clock, 10000);
306 + DPRINTF("backup_run complete %d\n", ret);
307 + block_job_completed(&job->common, ret);
310 +static void backup_job_cleanup_cb(void *opaque, int ret)
312 + BlockDriverState *bs = opaque;
314 + BackupBlockJob *job = (BackupBlockJob *)bs->job;
317 + DPRINTF("backup_job_cleanup_cb start %d\n", ret);
319 + job->backup_complete_cb(job->opaque, ret);
321 + DPRINTF("backup_job_cleanup_cb end\n");
323 + g_free(job->bitmap);
327 +backup_job_start(BlockDriverState *bs)
331 + assert(bs->job->co == NULL);
333 + bs->job->co = qemu_coroutine_create(backup_run);
334 + qemu_coroutine_enter(bs->job->co, bs->job);
338 +backup_job_create(BlockDriverState *bs, BackupDumpFunc *backup_dump_cb,
339 + BlockDriverCompletionFunc *backup_complete_cb,
340 + void *opaque, int64_t speed)
343 + assert(backup_dump_cb);
344 + assert(backup_complete_cb);
347 + DPRINTF("bdrv_backup_init failed - running job on %s\n",
348 + bdrv_get_device_name(bs));
352 + int64_t bitmap_size;
353 + const char *devname = bdrv_get_device_name(bs);
355 + if (!devname || !devname[0]) {
359 + DPRINTF("bdrv_backup_init %s\n", bdrv_get_device_name(bs));
362 + BackupBlockJob *job = block_job_create(&backup_job_type, bs, speed,
363 + backup_job_cleanup_cb, bs, &errp);
365 + job->common.cluster_size = BACKUP_CLUSTER_SIZE;
367 + bitmap_size = bs->total_sectors +
368 + BACKUP_BLOCKS_PER_CLUSTER * BITS_PER_LONG - 1;
369 + bitmap_size /= BACKUP_BLOCKS_PER_CLUSTER * BITS_PER_LONG;
371 + job->backup_dump_cb = backup_dump_cb;
372 + job->backup_complete_cb = backup_complete_cb;
373 + job->opaque = opaque;
374 + job->bitmap_size = bitmap_size;
375 + job->bitmap = g_new0(unsigned long, bitmap_size);
377 + job->common.len = bs->total_sectors*BDRV_SECTOR_SIZE;
381 diff --git a/backup.h b/backup.h
383 index 0000000..a5f85e6
388 + * QEMU backup related definitions
390 + * Copyright (C) Proxmox Server Solutions
393 + * Dietmar Maurer (dietmar@proxmox.com)
395 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
396 + * See the COPYING file in the top-level directory.
400 +#ifndef QEMU_BACKUP_H
401 +#define QEMU_BACKUP_H
403 +#include <uuid/uuid.h>
405 +#define BACKUP_CLUSTER_BITS 16
406 +#define BACKUP_CLUSTER_SIZE (1<<BACKUP_CLUSTER_BITS)
407 +#define BACKUP_BLOCKS_PER_CLUSTER (BACKUP_CLUSTER_SIZE/BDRV_SECTOR_SIZE)
409 +typedef int BackupDumpFunc(void *opaque, BlockDriverState *bs,
410 + int64_t cluster_num, unsigned char *buf);
412 +void backup_job_start(BlockDriverState *bs);
414 +int backup_job_create(BlockDriverState *bs, BackupDumpFunc *backup_dump_cb,
415 + BlockDriverCompletionFunc *backup_complete_cb,
416 + void *opaque, int64_t speed);
418 +#endif /* QEMU_BACKUP_H */
419 diff --git a/block.c b/block.c
420 index c05875f..4de7fbd 100644
425 BDRV_REQ_COPY_ON_READ = 0x1,
426 BDRV_REQ_ZERO_WRITE = 0x2,
427 + BDRV_REQ_BACKUP_ONLY = 0x4,
430 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
431 @@ -1542,7 +1543,7 @@ int bdrv_commit(BlockDriverState *bs)
437 if (!bs->backing_hd) {
440 @@ -1679,6 +1680,22 @@ static void round_to_clusters(BlockDriverState *bs,
445 + * Round a region to job cluster boundaries
447 +static void round_to_job_clusters(BlockDriverState *bs,
448 + int64_t sector_num, int nb_sectors,
449 + int job_cluster_size,
450 + int64_t *cluster_sector_num,
451 + int *cluster_nb_sectors)
453 + int64_t c = job_cluster_size/BDRV_SECTOR_SIZE;
455 + *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c);
456 + *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num +
460 static bool tracked_request_overlaps(BdrvTrackedRequest *req,
461 int64_t sector_num, int nb_sectors) {
463 @@ -1693,7 +1710,9 @@ static bool tracked_request_overlaps(BdrvTrackedRequest *req,
466 static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
467 - int64_t sector_num, int nb_sectors)
468 + int64_t sector_num,
470 + int job_cluster_size)
472 BdrvTrackedRequest *req;
473 int64_t cluster_sector_num;
474 @@ -1709,6 +1728,11 @@ static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
475 round_to_clusters(bs, sector_num, nb_sectors,
476 &cluster_sector_num, &cluster_nb_sectors);
478 + if (job_cluster_size) {
479 + round_to_job_clusters(bs, sector_num, nb_sectors, job_cluster_size,
480 + &cluster_sector_num, &cluster_nb_sectors);
485 QLIST_FOREACH(req, &bs->tracked_requests, list) {
486 @@ -2278,12 +2302,24 @@ static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
487 bs->copy_on_read_in_flight++;
490 - if (bs->copy_on_read_in_flight) {
491 - wait_for_overlapping_requests(bs, sector_num, nb_sectors);
492 + int job_cluster_size = bs->job && bs->job->cluster_size ?
493 + bs->job->cluster_size : 0;
495 + if (bs->copy_on_read_in_flight || job_cluster_size) {
496 + wait_for_overlapping_requests(bs, sector_num, nb_sectors,
500 tracked_request_begin(&req, bs, sector_num, nb_sectors, false);
502 + if (bs->job && bs->job->job_type->before_read) {
503 + ret = bs->job->job_type->before_read(bs, sector_num, nb_sectors, qiov);
504 + if ((ret < 0) || (flags & BDRV_REQ_BACKUP_ONLY)) {
505 + /* Note: We do not return any data to the caller */
510 if (flags & BDRV_REQ_COPY_ON_READ) {
513 @@ -2327,6 +2363,17 @@ int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
514 BDRV_REQ_COPY_ON_READ);
517 +int coroutine_fn bdrv_co_backup(BlockDriverState *bs,
518 + int64_t sector_num, int nb_sectors)
524 + return bdrv_co_do_readv(bs, sector_num, nb_sectors, NULL,
525 + BDRV_REQ_BACKUP_ONLY);
528 static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
529 int64_t sector_num, int nb_sectors)
531 @@ -2384,12 +2431,23 @@ static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
532 bdrv_io_limits_intercept(bs, true, nb_sectors);
535 - if (bs->copy_on_read_in_flight) {
536 - wait_for_overlapping_requests(bs, sector_num, nb_sectors);
537 + int job_cluster_size = bs->job && bs->job->cluster_size ?
538 + bs->job->cluster_size : 0;
540 + if (bs->copy_on_read_in_flight || job_cluster_size) {
541 + wait_for_overlapping_requests(bs, sector_num, nb_sectors,
545 tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
547 + if (bs->job && bs->job->job_type->before_write) {
548 + ret = bs->job->job_type->before_write(bs, sector_num, nb_sectors, qiov);
554 if (flags & BDRV_REQ_ZERO_WRITE) {
555 ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors);
557 @@ -2408,6 +2466,7 @@ static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
558 bs->wr_highest_sector = sector_num + nb_sectors - 1;
562 tracked_request_end(&req);
565 diff --git a/block.h b/block.h
566 index 722c620..94e5903 100644
569 @@ -172,6 +172,8 @@ int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
570 int nb_sectors, QEMUIOVector *qiov);
571 int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
572 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
573 +int coroutine_fn bdrv_co_backup(BlockDriverState *bs,
574 + int64_t sector_num, int nb_sectors);
575 int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
576 int nb_sectors, QEMUIOVector *qiov);
578 diff --git a/blockjob.h b/blockjob.h
579 index 3792b73..6621173 100644
582 @@ -50,6 +50,13 @@ typedef struct BlockJobType {
585 void (*complete)(BlockJob *job, Error **errp);
587 + /** tracked requests */
588 + int coroutine_fn (*before_read)(BlockDriverState *bs, int64_t sector_num,
589 + int nb_sectors, QEMUIOVector *qiov);
590 + int coroutine_fn (*before_write)(BlockDriverState *bs, int64_t sector_num,
591 + int nb_sectors, QEMUIOVector *qiov);
596 @@ -103,6 +110,9 @@ struct BlockJob {
597 /** Speed that was set with @block_job_set_speed. */
600 + /** tracked requests */
603 /** The completion function that will be called when the job completes. */
604 BlockDriverCompletionFunc *cb;