clean: remove buildinfo/changes
[pve-qemu-kvm.git] / debian / patches / old / 0002-add-basic-backup-support-to-block-driver.patch
bloba178c591a7cd2130fbf356ea377296d19c9e393a
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>
17 ---
18 Makefile.objs | 1 +
19 backup.c | 355 ++++++++++++++++++++++++++++++++++++++++++++++
20 backup.h | 30 ++++
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
30 --- a/Makefile.objs
31 +++ b/Makefile.objs
32 @@ -13,6 +13,7 @@ block-obj-$(CONFIG_POSIX) += aio-posix.o
33 block-obj-$(CONFIG_WIN32) += aio-win32.o
34 block-obj-y += block/
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
41 new file mode 100644
42 index 0000000..8955e1a
43 --- /dev/null
44 +++ b/backup.c
45 @@ -0,0 +1,355 @@
46 +/*
47 + * QEMU backup
48 + *
49 + * Copyright (C) 2013 Proxmox Server Solutions
50 + *
51 + * Authors:
52 + * Dietmar Maurer (dietmar@proxmox.com)
53 + *
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.
56 + *
57 + */
59 +#include <stdio.h>
60 +#include <errno.h>
61 +#include <unistd.h>
63 +#include "block/block.h"
64 +#include "block/block_int.h"
65 +#include "block/blockjob.h"
66 +#include "qemu/ratelimit.h"
67 +#include "backup.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__); } } \
75 + while (0)
78 +#define SLICE_TIME 100000000ULL /* ns */
80 +typedef struct BackupBlockJob {
81 + BlockJob common;
82 + RateLimit limit;
83 + CoRwlock rwlock;
84 + uint64_t sectors_read;
85 + unsigned long *bitmap;
86 + int bitmap_size;
87 + BackupDumpFunc *backup_dump_cb;
88 + BlockDriverCompletionFunc *backup_complete_cb;
89 + void *opaque;
90 +} BackupBlockJob;
92 +static bool backup_get_bitmap(BackupBlockJob *job, int64_t cluster_num)
94 + assert(job);
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,
110 + bool dirty)
112 + assert(job);
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];
123 + if (dirty) {
124 + val |= 1UL << bit;
125 + } else {
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)
134 + assert(bs);
135 + BackupBlockJob *job = (BackupBlockJob *)bs->job;
136 + assert(job);
138 + BlockDriver *drv = bs->drv;
139 + struct iovec iov;
140 + QEMUIOVector bounce_qiov;
141 + void *bounce_buffer = NULL;
142 + int ret = 0;
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++) {
156 + bool zero = 0;
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
175 + int n = 0;
176 + ret = bdrv_co_is_allocated_above(bs, NULL,
177 + start * BACKUP_BLOCKS_PER_CLUSTER,
178 + BACKUP_BLOCKS_PER_CLUSTER, &n);
179 + if (ret < 0) {
180 + DPRINTF("brdv_co_backup_cow is_allocated C%" PRId64 " failed\n",
181 + start);
182 + goto out;
185 + zero = (ret == 0) && (n == BACKUP_BLOCKS_PER_CLUSTER);
187 + if (!zero) {
188 +#endif
189 + ret = drv->bdrv_co_readv(bs, start * BACKUP_BLOCKS_PER_CLUSTER,
190 + BACKUP_BLOCKS_PER_CLUSTER,
191 + &bounce_qiov);
192 + if (ret < 0) {
193 + DPRINTF("brdv_co_backup_cow bdrv_read C%" PRId64 " failed\n",
194 + start);
195 + goto out;
197 +#if USE_ALLOCATION_CHECK
199 +#endif
200 + job->sectors_read += BACKUP_BLOCKS_PER_CLUSTER;
202 + ret = job->backup_dump_cb(job->opaque, bs, start,
203 + zero ? NULL : bounce_buffer);
204 + if (ret < 0) {
205 + DPRINTF("brdv_co_backup_cow dump_cluster_cb C%" PRId64 " failed\n",
206 + start);
207 + goto out;
210 + DPRINTF("brdv_co_backup_cow done C%" PRId64 "\n", start);
213 +out:
214 + if (bounce_buffer) {
215 + qemu_vfree(bounce_buffer);
218 + qemu_co_rwlock_unlock(&job->rwlock);
220 + return ret;
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);
241 + if (speed < 0) {
242 + error_set(errp, QERR_INVALID_PARAMETER, "speed");
243 + return;
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;
260 + assert(bs);
262 + int64_t start, end;
264 + start = 0;
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);
271 + int ret = 0;
273 + for (; start < end; start++) {
274 + if (block_job_is_cancelled(&job->common)) {
275 + ret = -1;
276 + break;
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)
282 + */
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);
288 + } else {
289 + block_job_sleep_ns(&job->common, rt_clock, 1000);
292 + if (block_job_is_cancelled(&job->common)) {
293 + ret = -1;
294 + break;
297 + if (backup_get_bitmap(job, start)) {
298 + continue; /* already copied */
301 + DPRINTF("backup_run loop C%" PRId64 "\n", start);
303 + /**
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()
307 + */
308 + ret = bdrv_co_backup(bs, start*BACKUP_BLOCKS_PER_CLUSTER, 1);
309 + if (ret < 0) {
310 + break;
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;
327 + assert(bs);
328 + BackupBlockJob *job = (BackupBlockJob *)bs->job;
329 + assert(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);
340 +void
341 +backup_job_start(BlockDriverState *bs, bool cancel)
343 + assert(bs);
344 + assert(bs->job);
345 + assert(bs->job->co == NULL);
347 + if (cancel) {
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);
355 +int
356 +backup_job_create(BlockDriverState *bs, BackupDumpFunc *backup_dump_cb,
357 + BlockDriverCompletionFunc *backup_complete_cb,
358 + void *opaque, int64_t speed)
360 + assert(bs);
361 + assert(backup_dump_cb);
362 + assert(backup_complete_cb);
364 + if (bs->job) {
365 + DPRINTF("bdrv_backup_init failed - running job on %s\n",
366 + bdrv_get_device_name(bs));
367 + return -1;
370 + int64_t bitmap_size;
371 + const char *devname = bdrv_get_device_name(bs);
373 + if (!devname || !devname[0]) {
374 + return -1;
377 + DPRINTF("bdrv_backup_init %s\n", bdrv_get_device_name(bs));
379 + Error *errp;
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;
399 + return 0;
401 diff --git a/backup.h b/backup.h
402 new file mode 100644
403 index 0000000..9b1ea1c
404 --- /dev/null
405 +++ b/backup.h
406 @@ -0,0 +1,30 @@
408 + * QEMU backup related definitions
410 + * Copyright (C) 2013 Proxmox Server Solutions
412 + * Authors:
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.
418 + */
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
439 --- a/block.c
440 +++ b/block.c
441 @@ -54,6 +54,7 @@
442 typedef enum {
443 BDRV_REQ_COPY_ON_READ = 0x1,
444 BDRV_REQ_ZERO_WRITE = 0x2,
445 + BDRV_REQ_BACKUP_ONLY = 0x4,
446 } BdrvRequestFlags;
448 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
449 @@ -1554,7 +1555,7 @@ int bdrv_commit(BlockDriverState *bs)
451 if (!drv)
452 return -ENOMEDIUM;
455 if (!bs->backing_hd) {
456 return -ENOTSUP;
458 @@ -1691,6 +1692,22 @@ void bdrv_round_to_clusters(BlockDriverState *bs,
462 +/**
463 + * Round a region to job cluster boundaries
464 + */
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 +
475 + nb_sectors, c);
478 static bool tracked_request_overlaps(BdrvTrackedRequest *req,
479 int64_t sector_num, int nb_sectors) {
480 /* aaaa bbbb */
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,
487 + int nb_sectors,
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);
501 do {
502 retry = false;
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,
515 + job_cluster_size);
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 */
524 + goto out;
528 if (flags & BDRV_REQ_COPY_ON_READ) {
529 int pnum;
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)
538 + if (!bs->job) {
539 + return -ENOTSUP;
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,
560 + job_cluster_size);
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);
567 + if (ret < 0) {
568 + goto out;
572 if (flags & BDRV_REQ_ZERO_WRITE) {
573 ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors);
574 } else {
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;
579 +out:
580 tracked_request_end(&req);
582 return ret;
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 {
601 * manually.
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);
611 } BlockJobType;
614 @@ -103,6 +110,9 @@ struct BlockJob {
615 /** Speed that was set with @block_job_set_speed. */
616 int64_t speed;
618 + /** tracked requests */
619 + int cluster_size;
621 /** The completion function that will be called when the job completes. */
622 BlockDriverCompletionFunc *cb;
625 1.7.2.5