]> git.proxmox.com Git - pve-qemu-kvm.git/blame - debian/patches/0002-add-basic-backup-support-to-block-driver.patch
include backup patches
[pve-qemu-kvm.git] / debian / patches / 0002-add-basic-backup-support-to-block-driver.patch
CommitLineData
5ad5891c
DM
1From 4a8d3de07ce2989390f2b0a42a81884d3300d516 Mon Sep 17 00:00:00 2001
2From: Dietmar Maurer <dietmar@proxmox.com>
3Date: Tue, 13 Nov 2012 10:03:52 +0100
4Subject: [PATCH v3 2/6] add basic backup support to block driver
5
6Function backup_job_start() creates a block job to backup a block device.
7
8We call backup_do_cow() for each write during backup. That function
9reads the original data and pass it to backup_dump_cb().
10
11The tracked_request infrastructure is used to serialize access.
12
13Currently backup cluster size is hardcoded to 65536 bytes.
14
15Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
16---
17 Makefile.objs | 1 +
18 backup.c | 302 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19 backup.h | 30 ++++++
20 block.c | 71 ++++++++++++-
21 block.h | 2 +
22 blockjob.h | 10 ++
23 6 files changed, 410 insertions(+), 6 deletions(-)
24 create mode 100644 backup.c
25 create mode 100644 backup.h
26
27diff --git a/Makefile.objs b/Makefile.objs
28index 3c7abca..cb46be5 100644
29--- a/Makefile.objs
30+++ b/Makefile.objs
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
39diff --git a/backup.c b/backup.c
40new file mode 100644
41index 0000000..6a44974
42--- /dev/null
43+++ b/backup.c
44@@ -0,0 +1,302 @@
45+/*
46+ * QEMU backup
47+ *
48+ * Copyright (C) 2012 Proxmox Server Solutions
49+ *
50+ * Authors:
51+ * Dietmar Maurer (dietmar@proxmox.com)
52+ *
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.
55+ *
56+ */
57+
58+#include <stdio.h>
59+#include <errno.h>
60+#include <unistd.h>
61+
62+#include "block.h"
63+#include "block_int.h"
64+#include "blockjob.h"
65+#include "backup.h"
66+
67+#define DEBUG_BACKUP 0
68+
69+#define DPRINTF(fmt, ...) \
70+ do { if (DEBUG_BACKUP) { printf("backup: " fmt, ## __VA_ARGS__); } } \
71+ while (0)
72+
73+
74+#define BITS_PER_LONG (sizeof(unsigned long) * 8)
75+
76+typedef struct BackupBlockJob {
77+ BlockJob common;
78+ unsigned long *bitmap;
79+ int bitmap_size;
80+ BackupDumpFunc *backup_dump_cb;
81+ BlockDriverCompletionFunc *backup_complete_cb;
82+ void *opaque;
83+} BackupBlockJob;
84+
85+static int backup_get_bitmap(BlockDriverState *bs, int64_t cluster_num)
86+{
87+ assert(bs);
88+ BackupBlockJob *job = (BackupBlockJob *)bs->job;
89+ assert(job);
90+ assert(job->bitmap);
91+
92+ unsigned long val, idx, bit;
93+
94+ idx = cluster_num / BITS_PER_LONG;
95+
96+ assert(job->bitmap_size > idx);
97+
98+ bit = cluster_num % BITS_PER_LONG;
99+ val = job->bitmap[idx];
100+
101+ return !!(val & (1UL << bit));
102+}
103+
104+static void backup_set_bitmap(BlockDriverState *bs, int64_t cluster_num,
105+ int dirty)
106+{
107+ assert(bs);
108+ BackupBlockJob *job = (BackupBlockJob *)bs->job;
109+ assert(job);
110+ assert(job->bitmap);
111+
112+ unsigned long val, idx, bit;
113+
114+ idx = cluster_num / BITS_PER_LONG;
115+
116+ assert(job->bitmap_size > idx);
117+
118+ bit = cluster_num % BITS_PER_LONG;
119+ val = job->bitmap[idx];
120+ if (dirty) {
121+ if (!(val & (1UL << bit))) {
122+ val |= 1UL << bit;
123+ }
124+ } else {
125+ if (val & (1UL << bit)) {
126+ val &= ~(1UL << bit);
127+ }
128+ }
129+ job->bitmap[idx] = val;
130+}
131+
132+static int backup_in_progress_count;
133+
134+static int coroutine_fn backup_do_cow(BlockDriverState *bs,
135+ int64_t sector_num, int nb_sectors)
136+{
137+ assert(bs);
138+ BackupBlockJob *job = (BackupBlockJob *)bs->job;
139+ assert(job);
140+
141+ BlockDriver *drv = bs->drv;
142+ struct iovec iov;
143+ QEMUIOVector bounce_qiov;
144+ void *bounce_buffer = NULL;
145+ int ret = 0;
146+
147+ backup_in_progress_count++;
148+
149+ int64_t start, end;
150+
151+ start = sector_num / BACKUP_BLOCKS_PER_CLUSTER;
152+ end = (sector_num + nb_sectors + BACKUP_BLOCKS_PER_CLUSTER - 1) /
153+ BACKUP_BLOCKS_PER_CLUSTER;
154+
155+ DPRINTF("brdv_co_backup_cow enter %s C%zd %zd %d\n",
156+ bdrv_get_device_name(bs), start, sector_num, nb_sectors);
157+
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 */
162+ }
163+
164+ /* immediately set bitmap (avoid coroutine race) */
165+ backup_set_bitmap(bs, start, 1);
166+
167+ DPRINTF("brdv_co_backup_cow C%zd\n", start);
168+
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);
173+ }
174+
175+ ret = drv->bdrv_co_readv(bs, start * BACKUP_BLOCKS_PER_CLUSTER,
176+ BACKUP_BLOCKS_PER_CLUSTER,
177+ &bounce_qiov);
178+ if (ret < 0) {
179+ DPRINTF("brdv_co_backup_cow bdrv_read C%zd failed\n", start);
180+ goto out;
181+ }
182+
183+ ret = job->backup_dump_cb(job->opaque, bs, start, bounce_buffer);
184+ if (ret < 0) {
185+ DPRINTF("brdv_co_backup_cow dump_cluster_cb C%zd failed\n", start);
186+ goto out;
187+ }
188+
189+ DPRINTF("brdv_co_backup_cow done C%zd\n", start);
190+ }
191+
192+out:
193+ if (bounce_buffer) {
194+ qemu_vfree(bounce_buffer);
195+ }
196+
197+ backup_in_progress_count--;
198+
199+ return ret;
200+}
201+
202+static int coroutine_fn backup_before_read(BlockDriverState *bs,
203+ int64_t sector_num,
204+ int nb_sectors, QEMUIOVector *qiov)
205+{
206+ return backup_do_cow(bs, sector_num, nb_sectors);
207+}
208+
209+static int coroutine_fn backup_before_write(BlockDriverState *bs,
210+ int64_t sector_num,
211+ int nb_sectors, QEMUIOVector *qiov)
212+{
213+ return backup_do_cow(bs, sector_num, nb_sectors);
214+}
215+
216+
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",
222+};
223+
224+static void coroutine_fn backup_run(void *opaque)
225+{
226+ BackupBlockJob *job = opaque;
227+ BlockDriverState *bs = job->common.bs;
228+ assert(bs);
229+
230+ int64_t start, end;
231+
232+ start = 0;
233+ end = (bs->total_sectors + BACKUP_BLOCKS_PER_CLUSTER - 1) /
234+ BACKUP_BLOCKS_PER_CLUSTER;
235+
236+ DPRINTF("backup_run start %s %zd %zd\n", bdrv_get_device_name(bs),
237+ start, end);
238+
239+ int ret = 0;
240+
241+ for (; start < end; start++) {
242+ if (block_job_is_cancelled(&job->common)) {
243+ ret = -1;
244+ break;
245+ }
246+
247+ if (backup_get_bitmap(bs, start)) {
248+ continue; /* already copied */
249+ }
250+
251+ /* we need to yield so that qemu_aio_flush() returns.
252+ * (without, VM does not reboot)
253+ * todo: can we avoid that?
254+ */
255+ co_sleep_ns(rt_clock, 0);
256+ if (block_job_is_cancelled(&job->common)) {
257+ ret = -1;
258+ break;
259+ }
260+ DPRINTF("backup_run loop C%zd\n", start);
261+
262+ /**
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()
266+ */
267+ ret = bdrv_co_backup(bs, start*BACKUP_BLOCKS_PER_CLUSTER, 1);
268+ if (ret < 0) {
269+ break;
270+ }
271+ /* Publish progress */
272+ job->common.offset += BACKUP_CLUSTER_SIZE;
273+ }
274+
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);
279+ }
280+
281+ DPRINTF("backup_run complete %d\n", ret);
282+ block_job_completed(&job->common, ret);
283+}
284+
285+static void backup_job_cleanup_cb(void *opaque, int ret)
286+{
287+ BlockDriverState *bs = opaque;
288+ assert(bs);
289+ BackupBlockJob *job = (BackupBlockJob *)bs->job;
290+ assert(job);
291+
292+ DPRINTF("backup_job_cleanup_cb start %d\n", ret);
293+
294+ job->backup_complete_cb(job->opaque, ret);
295+
296+ DPRINTF("backup_job_cleanup_cb end\n");
297+
298+ g_free(job->bitmap);
299+}
300+
301+int
302+backup_job_start(BlockDriverState *bs, BackupDumpFunc *backup_dump_cb,
303+ BlockDriverCompletionFunc *backup_complete_cb,
304+ void *opaque)
305+{
306+ assert(bs);
307+ assert(backup_dump_cb);
308+ assert(backup_complete_cb);
309+
310+ if (bs->job) {
311+ DPRINTF("bdrv_backup_init failed - running job on %s\n",
312+ bdrv_get_device_name(bs));
313+ return -1;
314+ }
315+
316+ int64_t bitmap_size;
317+ const char *devname = bdrv_get_device_name(bs);
318+
319+ if (!devname || !devname[0]) {
320+ return -1;
321+ }
322+
323+ DPRINTF("bdrv_backup_init %s\n", bdrv_get_device_name(bs));
324+
325+ Error *errp;
326+ BackupBlockJob *job = block_job_create(&backup_job_type, bs, 0,
327+ backup_job_cleanup_cb, bs, &errp);
328+
329+ job->common.cluster_size = BACKUP_CLUSTER_SIZE;
330+
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;
334+
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);
340+
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);
344+
345+ return 0;
346+}
347diff --git a/backup.h b/backup.h
348new file mode 100644
349index 0000000..e1f0290
350--- /dev/null
351+++ b/backup.h
352@@ -0,0 +1,30 @@
353+/*
354+ * QEMU backup related definitions
355+ *
356+ * Copyright (C) Proxmox Server Solutions
357+ *
358+ * Authors:
359+ * Dietmar Maurer (dietmar@proxmox.com)
360+ *
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.
363+ *
364+ */
365+
366+#ifndef QEMU_BACKUP_H
367+#define QEMU_BACKUP_H
368+
369+#include <uuid/uuid.h>
370+
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)
374+
375+typedef int BackupDumpFunc(void *opaque, BlockDriverState *bs,
376+ int64_t cluster_num, unsigned char *buf);
377+
378+int backup_job_start(BlockDriverState *bs, BackupDumpFunc *backup_dump_cb,
379+ BlockDriverCompletionFunc *backup_complete_cb,
380+ void *opaque);
381+
382+#endif /* QEMU_BACKUP_H */
383diff --git a/block.c b/block.c
384index c05875f..2f7c2eb 100644
385--- a/block.c
386+++ b/block.c
387@@ -54,6 +54,7 @@
388 typedef enum {
389 BDRV_REQ_COPY_ON_READ = 0x1,
390 BDRV_REQ_ZERO_WRITE = 0x2,
391+ BDRV_REQ_BACKUP_ONLY = 0x4,
392 } BdrvRequestFlags;
393
394 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
395@@ -1542,7 +1543,7 @@ int bdrv_commit(BlockDriverState *bs)
396
397 if (!drv)
398 return -ENOMEDIUM;
399-
400+
401 if (!bs->backing_hd) {
402 return -ENOTSUP;
403 }
404@@ -1679,6 +1680,22 @@ static void round_to_clusters(BlockDriverState *bs,
405 }
406 }
407
408+/**
409+ * Round a region to job cluster boundaries
410+ */
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)
416+{
417+ int64_t c = job_cluster_size/BDRV_SECTOR_SIZE;
418+
419+ *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c);
420+ *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num +
421+ nb_sectors, c);
422+}
423+
424 static bool tracked_request_overlaps(BdrvTrackedRequest *req,
425 int64_t sector_num, int nb_sectors) {
426 /* aaaa bbbb */
427@@ -1693,7 +1710,9 @@ static bool tracked_request_overlaps(BdrvTrackedRequest *req,
428 }
429
430 static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
431- int64_t sector_num, int nb_sectors)
432+ int64_t sector_num,
433+ int nb_sectors,
434+ int job_cluster_size)
435 {
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);
441
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);
445+ }
446+
447 do {
448 retry = false;
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++;
452 }
453
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;
458+
459+ if (bs->copy_on_read_in_flight || job_cluster_size) {
460+ wait_for_overlapping_requests(bs, sector_num, nb_sectors,
461+ job_cluster_size);
462 }
463
464 tracked_request_begin(&req, bs, sector_num, nb_sectors, false);
465
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 */
470+ goto out;
471+ }
472+ }
473+
474 if (flags & BDRV_REQ_COPY_ON_READ) {
475 int pnum;
476
477@@ -2327,6 +2363,17 @@ int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
478 BDRV_REQ_COPY_ON_READ);
479 }
480
481+int coroutine_fn bdrv_co_backup(BlockDriverState *bs,
482+ int64_t sector_num, int nb_sectors)
483+{
484+ if (!bs->job) {
485+ return -ENOTSUP;
486+ }
487+
488+ return bdrv_co_do_readv(bs, sector_num, nb_sectors, NULL,
489+ BDRV_REQ_BACKUP_ONLY);
490+}
491+
492 static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
493 int64_t sector_num, int nb_sectors)
494 {
495@@ -2384,12 +2431,23 @@ static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
496 bdrv_io_limits_intercept(bs, true, nb_sectors);
497 }
498
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;
503+
504+ if (bs->copy_on_read_in_flight || job_cluster_size) {
505+ wait_for_overlapping_requests(bs, sector_num, nb_sectors,
506+ job_cluster_size);
507 }
508
509 tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
510
511+ if (bs->job && bs->job->job_type->before_write) {
512+ ret = bs->job->job_type->before_write(bs, sector_num, nb_sectors, qiov);
513+ if (ret < 0) {
514+ goto out;
515+ }
516+ }
517+
518 if (flags & BDRV_REQ_ZERO_WRITE) {
519 ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors);
520 } else {
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;
523 }
524
525+out:
526 tracked_request_end(&req);
527
528 return ret;
529diff --git a/block.h b/block.h
530index 722c620..94e5903 100644
531--- a/block.h
532+++ b/block.h
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);
541 /*
542diff --git a/blockjob.h b/blockjob.h
543index 3792b73..6621173 100644
544--- a/blockjob.h
545+++ b/blockjob.h
546@@ -50,6 +50,13 @@ typedef struct BlockJobType {
547 * manually.
548 */
549 void (*complete)(BlockJob *job, Error **errp);
550+
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);
556+
557 } BlockJobType;
558
559 /**
560@@ -103,6 +110,9 @@ struct BlockJob {
561 /** Speed that was set with @block_job_set_speed. */
562 int64_t speed;
563
564+ /** tracked requests */
565+ int cluster_size;
566+
567 /** The completion function that will be called when the job completes. */
568 BlockDriverCompletionFunc *cb;
569
570--
5711.7.2.5
572