1 From 290368c30f966c3e74d6cd926c0d044f7ba2a3ea 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_start() creates a block job to backup a block device.
8 We call backup_do_cow() for each write during backup. That function
9 reads the original data and pass it to backup_dump_cb().
11 The tracked_request infrastructure is used to serialize access.
13 Currently backup cluster size is hardcoded to 65536 bytes.
15 Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
18 backup.c | 302 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20 block.c | 71 ++++++++++++-
23 6 files changed, 410 insertions(+), 6 deletions(-)
24 create mode 100644 backup.c
25 create mode 100644 backup.h
27 diff --git a/Makefile.objs b/Makefile.objs
28 index 3c7abca..cb46be5 100644
31 @@ -48,6 +48,7 @@ coroutine-obj-$(CONFIG_WIN32) += coroutine-win32.o
32 block-obj-y = iov.o cache-utils.o qemu-option.o module.o async.o
33 block-obj-y += nbd.o block.o blockjob.o aes.o qemu-config.o
34 block-obj-y += thread-pool.o qemu-progress.o qemu-sockets.o uri.o notify.o
35 +block-obj-y += backup.o
36 block-obj-y += $(coroutine-obj-y) $(qobject-obj-y) $(version-obj-y)
37 block-obj-$(CONFIG_POSIX) += event_notifier-posix.o aio-posix.o
38 block-obj-$(CONFIG_WIN32) += event_notifier-win32.o aio-win32.o
39 diff --git a/backup.c b/backup.c
41 index 0000000..6a44974
48 + * Copyright (C) 2012 Proxmox Server Solutions
51 + * Dietmar Maurer (dietmar@proxmox.com)
53 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
54 + * See the COPYING file in the top-level directory.
63 +#include "block_int.h"
64 +#include "blockjob.h"
67 +#define DEBUG_BACKUP 0
69 +#define DPRINTF(fmt, ...) \
70 + do { if (DEBUG_BACKUP) { printf("backup: " fmt, ## __VA_ARGS__); } } \
74 +#define BITS_PER_LONG (sizeof(unsigned long) * 8)
76 +typedef struct BackupBlockJob {
78 + unsigned long *bitmap;
80 + BackupDumpFunc *backup_dump_cb;
81 + BlockDriverCompletionFunc *backup_complete_cb;
85 +static int backup_get_bitmap(BlockDriverState *bs, int64_t cluster_num)
88 + BackupBlockJob *job = (BackupBlockJob *)bs->job;
90 + assert(job->bitmap);
92 + unsigned long val, idx, bit;
94 + idx = cluster_num / BITS_PER_LONG;
96 + assert(job->bitmap_size > idx);
98 + bit = cluster_num % BITS_PER_LONG;
99 + val = job->bitmap[idx];
101 + return !!(val & (1UL << bit));
104 +static void backup_set_bitmap(BlockDriverState *bs, int64_t cluster_num,
108 + BackupBlockJob *job = (BackupBlockJob *)bs->job;
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(bs, 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(bs, 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 + DPRINTF("brdv_co_backup_cow bdrv_read C%zd failed\n", start);
183 + ret = job->backup_dump_cb(job->opaque, bs, start, bounce_buffer);
185 + DPRINTF("brdv_co_backup_cow dump_cluster_cb C%zd failed\n", start);
189 + DPRINTF("brdv_co_backup_cow done C%zd\n", start);
193 + if (bounce_buffer) {
194 + qemu_vfree(bounce_buffer);
197 + backup_in_progress_count--;
202 +static int coroutine_fn backup_before_read(BlockDriverState *bs,
203 + int64_t sector_num,
204 + int nb_sectors, QEMUIOVector *qiov)
206 + return backup_do_cow(bs, sector_num, nb_sectors);
209 +static int coroutine_fn backup_before_write(BlockDriverState *bs,
210 + int64_t sector_num,
211 + int nb_sectors, QEMUIOVector *qiov)
213 + return backup_do_cow(bs, sector_num, nb_sectors);
217 +static BlockJobType backup_job_type = {
218 + .instance_size = sizeof(BackupBlockJob),
219 + .before_read = backup_before_read,
220 + .before_write = backup_before_write,
221 + .job_type = "backup",
224 +static void coroutine_fn backup_run(void *opaque)
226 + BackupBlockJob *job = opaque;
227 + BlockDriverState *bs = job->common.bs;
230 + int64_t start, end;
233 + end = (bs->total_sectors + BACKUP_BLOCKS_PER_CLUSTER - 1) /
234 + BACKUP_BLOCKS_PER_CLUSTER;
236 + DPRINTF("backup_run start %s %zd %zd\n", bdrv_get_device_name(bs),
241 + for (; start < end; start++) {
242 + if (block_job_is_cancelled(&job->common)) {
247 + if (backup_get_bitmap(bs, start)) {
248 + continue; /* already copied */
251 + /* we need to yield so that qemu_aio_flush() returns.
252 + * (without, VM does not reboot)
253 + * todo: can we avoid that?
255 + co_sleep_ns(rt_clock, 0);
256 + if (block_job_is_cancelled(&job->common)) {
260 + DPRINTF("backup_run loop C%zd\n", start);
263 + * This triggers a cluster copy
264 + * Note: avoid direct call to brdv_co_backup_cow, because
265 + * this does not call tracked_request_begin()
267 + ret = bdrv_co_backup(bs, start*BACKUP_BLOCKS_PER_CLUSTER, 1);
271 + /* Publish progress */
272 + job->common.offset += BACKUP_CLUSTER_SIZE;
275 + while (backup_in_progress_count > 0) {
276 + DPRINTF("backup_run backup_in_progress_count != 0 (%d)",
277 + backup_in_progress_count);
278 + co_sleep_ns(rt_clock, 10000);
281 + DPRINTF("backup_run complete %d\n", ret);
282 + block_job_completed(&job->common, ret);
285 +static void backup_job_cleanup_cb(void *opaque, int ret)
287 + BlockDriverState *bs = opaque;
289 + BackupBlockJob *job = (BackupBlockJob *)bs->job;
292 + DPRINTF("backup_job_cleanup_cb start %d\n", ret);
294 + job->backup_complete_cb(job->opaque, ret);
296 + DPRINTF("backup_job_cleanup_cb end\n");
298 + g_free(job->bitmap);
302 +backup_job_start(BlockDriverState *bs, BackupDumpFunc *backup_dump_cb,
303 + BlockDriverCompletionFunc *backup_complete_cb,
307 + assert(backup_dump_cb);
308 + assert(backup_complete_cb);
311 + DPRINTF("bdrv_backup_init failed - running job on %s\n",
312 + bdrv_get_device_name(bs));
316 + int64_t bitmap_size;
317 + const char *devname = bdrv_get_device_name(bs);
319 + if (!devname || !devname[0]) {
323 + DPRINTF("bdrv_backup_init %s\n", bdrv_get_device_name(bs));
326 + BackupBlockJob *job = block_job_create(&backup_job_type, bs, 0,
327 + backup_job_cleanup_cb, bs, &errp);
329 + job->common.cluster_size = BACKUP_CLUSTER_SIZE;
331 + bitmap_size = bs->total_sectors +
332 + BACKUP_BLOCKS_PER_CLUSTER * BITS_PER_LONG - 1;
333 + bitmap_size /= BACKUP_BLOCKS_PER_CLUSTER * BITS_PER_LONG;
335 + job->backup_dump_cb = backup_dump_cb;
336 + job->backup_complete_cb = backup_complete_cb;
337 + job->opaque = opaque;
338 + job->bitmap_size = bitmap_size;
339 + job->bitmap = g_new0(unsigned long, bitmap_size);
341 + job->common.len = bs->total_sectors*BDRV_SECTOR_SIZE;
342 + job->common.co = qemu_coroutine_create(backup_run);
343 + qemu_coroutine_enter(job->common.co, job);
347 diff --git a/backup.h b/backup.h
349 index 0000000..e1f0290
354 + * QEMU backup related definitions
356 + * Copyright (C) Proxmox Server Solutions
359 + * Dietmar Maurer (dietmar@proxmox.com)
361 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
362 + * See the COPYING file in the top-level directory.
366 +#ifndef QEMU_BACKUP_H
367 +#define QEMU_BACKUP_H
369 +#include <uuid/uuid.h>
371 +#define BACKUP_CLUSTER_BITS 16
372 +#define BACKUP_CLUSTER_SIZE (1<<BACKUP_CLUSTER_BITS)
373 +#define BACKUP_BLOCKS_PER_CLUSTER (BACKUP_CLUSTER_SIZE/BDRV_SECTOR_SIZE)
375 +typedef int BackupDumpFunc(void *opaque, BlockDriverState *bs,
376 + int64_t cluster_num, unsigned char *buf);
378 +int backup_job_start(BlockDriverState *bs, BackupDumpFunc *backup_dump_cb,
379 + BlockDriverCompletionFunc *backup_complete_cb,
382 +#endif /* QEMU_BACKUP_H */
383 diff --git a/block.c b/block.c
384 index c05875f..2f7c2eb 100644
389 BDRV_REQ_COPY_ON_READ = 0x1,
390 BDRV_REQ_ZERO_WRITE = 0x2,
391 + BDRV_REQ_BACKUP_ONLY = 0x4,
394 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
395 @@ -1542,7 +1543,7 @@ int bdrv_commit(BlockDriverState *bs)
401 if (!bs->backing_hd) {
404 @@ -1679,6 +1680,22 @@ static void round_to_clusters(BlockDriverState *bs,
409 + * Round a region to job cluster boundaries
411 +static void round_to_job_clusters(BlockDriverState *bs,
412 + int64_t sector_num, int nb_sectors,
413 + int job_cluster_size,
414 + int64_t *cluster_sector_num,
415 + int *cluster_nb_sectors)
417 + int64_t c = job_cluster_size/BDRV_SECTOR_SIZE;
419 + *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c);
420 + *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num +
424 static bool tracked_request_overlaps(BdrvTrackedRequest *req,
425 int64_t sector_num, int nb_sectors) {
427 @@ -1693,7 +1710,9 @@ static bool tracked_request_overlaps(BdrvTrackedRequest *req,
430 static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
431 - int64_t sector_num, int nb_sectors)
432 + int64_t sector_num,
434 + int job_cluster_size)
436 BdrvTrackedRequest *req;
437 int64_t cluster_sector_num;
438 @@ -1709,6 +1728,11 @@ static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
439 round_to_clusters(bs, sector_num, nb_sectors,
440 &cluster_sector_num, &cluster_nb_sectors);
442 + if (job_cluster_size) {
443 + round_to_job_clusters(bs, sector_num, nb_sectors, job_cluster_size,
444 + &cluster_sector_num, &cluster_nb_sectors);
449 QLIST_FOREACH(req, &bs->tracked_requests, list) {
450 @@ -2278,12 +2302,24 @@ static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
451 bs->copy_on_read_in_flight++;
454 - if (bs->copy_on_read_in_flight) {
455 - wait_for_overlapping_requests(bs, sector_num, nb_sectors);
456 + int job_cluster_size = bs->job && bs->job->cluster_size ?
457 + bs->job->cluster_size : 0;
459 + if (bs->copy_on_read_in_flight || job_cluster_size) {
460 + wait_for_overlapping_requests(bs, sector_num, nb_sectors,
464 tracked_request_begin(&req, bs, sector_num, nb_sectors, false);
466 + if (bs->job && bs->job->job_type->before_read) {
467 + ret = bs->job->job_type->before_read(bs, sector_num, nb_sectors, qiov);
468 + if (flags & BDRV_REQ_BACKUP_ONLY) {
469 + /* Note: We do not return any data to the caller */
474 if (flags & BDRV_REQ_COPY_ON_READ) {
477 @@ -2327,6 +2363,17 @@ int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
478 BDRV_REQ_COPY_ON_READ);
481 +int coroutine_fn bdrv_co_backup(BlockDriverState *bs,
482 + int64_t sector_num, int nb_sectors)
488 + return bdrv_co_do_readv(bs, sector_num, nb_sectors, NULL,
489 + BDRV_REQ_BACKUP_ONLY);
492 static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
493 int64_t sector_num, int nb_sectors)
495 @@ -2384,12 +2431,23 @@ static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
496 bdrv_io_limits_intercept(bs, true, nb_sectors);
499 - if (bs->copy_on_read_in_flight) {
500 - wait_for_overlapping_requests(bs, sector_num, nb_sectors);
501 + int job_cluster_size = bs->job && bs->job->cluster_size ?
502 + bs->job->cluster_size : 0;
504 + if (bs->copy_on_read_in_flight || job_cluster_size) {
505 + wait_for_overlapping_requests(bs, sector_num, nb_sectors,
509 tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
511 + if (bs->job && bs->job->job_type->before_write) {
512 + ret = bs->job->job_type->before_write(bs, sector_num, nb_sectors, qiov);
518 if (flags & BDRV_REQ_ZERO_WRITE) {
519 ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors);
521 @@ -2408,6 +2466,7 @@ static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
522 bs->wr_highest_sector = sector_num + nb_sectors - 1;
526 tracked_request_end(&req);
529 diff --git a/block.h b/block.h
530 index 722c620..94e5903 100644
533 @@ -172,6 +172,8 @@ int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
534 int nb_sectors, QEMUIOVector *qiov);
535 int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
536 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
537 +int coroutine_fn bdrv_co_backup(BlockDriverState *bs,
538 + int64_t sector_num, int nb_sectors);
539 int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
540 int nb_sectors, QEMUIOVector *qiov);
542 diff --git a/blockjob.h b/blockjob.h
543 index 3792b73..6621173 100644
546 @@ -50,6 +50,13 @@ typedef struct BlockJobType {
549 void (*complete)(BlockJob *job, Error **errp);
551 + /** tracked requests */
552 + int coroutine_fn (*before_read)(BlockDriverState *bs, int64_t sector_num,
553 + int nb_sectors, QEMUIOVector *qiov);
554 + int coroutine_fn (*before_write)(BlockDriverState *bs, int64_t sector_num,
555 + int nb_sectors, QEMUIOVector *qiov);
560 @@ -103,6 +110,9 @@ struct BlockJob {
561 /** Speed that was set with @block_job_set_speed. */
564 + /** tracked requests */
567 /** The completion function that will be called when the job completes. */
568 BlockDriverCompletionFunc *cb;