1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Dietmar Maurer <dietmar@proxmox.com>
3 Date: Mon, 6 Apr 2020 12:16:58 +0200
4 Subject: [PATCH] PVE-Backup: add backup-dump block driver
6 - add backup-dump block driver block/backup-dump.c
7 - move BackupBlockJob declaration from block/backup.c to include/block/block_int.h
8 - block/backup.c - backup-job-create: also consider source cluster size
9 - job.c: make job_should_pause non-static
11 block/Makefile.objs | 1 +
12 block/backup-dump.c | 168 ++++++++++++++++++++++++++++++++++++++
13 block/backup.c | 23 ++----
14 include/block/block_int.h | 30 +++++++
16 5 files changed, 206 insertions(+), 19 deletions(-)
17 create mode 100644 block/backup-dump.c
19 diff --git a/block/Makefile.objs b/block/Makefile.objs
20 index d1a9227b8f..9ea0477d0b 100644
21 --- a/block/Makefile.objs
22 +++ b/block/Makefile.objs
23 @@ -33,6 +33,7 @@ block-obj-$(CONFIG_CURL) += curl.o
24 block-obj-$(CONFIG_RBD) += rbd.o
25 block-obj-$(CONFIG_GLUSTERFS) += gluster.o
26 block-obj-$(CONFIG_LIBSSH) += ssh.o
27 +block-obj-y += backup-dump.o
28 block-obj-y += accounting.o dirty-bitmap.o
29 block-obj-y += write-threshold.o
30 block-obj-y += backup.o
31 diff --git a/block/backup-dump.c b/block/backup-dump.c
33 index 0000000000..93d7f46950
35 +++ b/block/backup-dump.c
38 + * BlockDriver to send backup data stream to a callback function
40 + * Copyright (C) 2020 Proxmox Server Solutions GmbH
42 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
43 + * See the COPYING file in the top-level directory.
47 +#include "qemu/osdep.h"
48 +#include "qemu-common.h"
49 +#include "qom/object_interfaces.h"
50 +#include "block/block_int.h"
53 + int dump_cb_block_size;
55 + BackupDumpFunc *dump_cb;
57 +} BDRVBackupDumpState;
59 +static int qemu_backup_dump_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
61 + BDRVBackupDumpState *s = bs->opaque;
63 + bdi->cluster_size = s->dump_cb_block_size;
67 +static int qemu_backup_dump_check_perm(
68 + BlockDriverState *bs,
73 + /* Nothing to do. */
77 +static void qemu_backup_dump_set_perm(
78 + BlockDriverState *bs,
82 + /* Nothing to do. */
85 +static void qemu_backup_dump_abort_perm_update(BlockDriverState *bs)
87 + /* Nothing to do. */
90 +static void qemu_backup_dump_refresh_limits(BlockDriverState *bs, Error **errp)
92 + bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
95 +static void qemu_backup_dump_close(BlockDriverState *bs)
97 + /* Nothing to do. */
100 +static int64_t qemu_backup_dump_getlength(BlockDriverState *bs)
102 + BDRVBackupDumpState *s = bs->opaque;
104 + return s->byte_size;
107 +static coroutine_fn int qemu_backup_dump_co_writev(
108 + BlockDriverState *bs,
109 + int64_t sector_num,
111 + QEMUIOVector *qiov,
114 + /* flags can be only values we set in supported_write_flags */
115 + assert(flags == 0);
117 + BDRVBackupDumpState *s = bs->opaque;
118 + off_t offset = sector_num * BDRV_SECTOR_SIZE;
120 + uint64_t written = 0;
122 + for (int i = 0; i < qiov->niov; ++i) {
123 + const struct iovec *v = &qiov->iov[i];
125 + int rc = s->dump_cb(s->dump_cb_data, offset, v->iov_len, v->iov_base);
130 + if (rc != v->iov_len) {
134 + written += v->iov_len;
135 + offset += v->iov_len;
141 +static void qemu_backup_dump_child_perm(
142 + BlockDriverState *bs,
144 + BdrvChildRole role,
145 + BlockReopenQueue *reopen_queue,
146 + uint64_t perm, uint64_t shared,
147 + uint64_t *nperm, uint64_t *nshared)
149 + *nperm = BLK_PERM_ALL;
150 + *nshared = BLK_PERM_ALL;
153 +static BlockDriver bdrv_backup_dump_drive = {
154 + .format_name = "backup-dump-drive",
155 + .protocol_name = "backup-dump",
156 + .instance_size = sizeof(BDRVBackupDumpState),
158 + .bdrv_close = qemu_backup_dump_close,
159 + .bdrv_has_zero_init = bdrv_has_zero_init_1,
160 + .bdrv_getlength = qemu_backup_dump_getlength,
161 + .bdrv_get_info = qemu_backup_dump_get_info,
163 + .bdrv_co_writev = qemu_backup_dump_co_writev,
165 + .bdrv_refresh_limits = qemu_backup_dump_refresh_limits,
166 + .bdrv_check_perm = qemu_backup_dump_check_perm,
167 + .bdrv_set_perm = qemu_backup_dump_set_perm,
168 + .bdrv_abort_perm_update = qemu_backup_dump_abort_perm_update,
169 + .bdrv_child_perm = qemu_backup_dump_child_perm,
172 +static void bdrv_backup_dump_init(void)
174 + bdrv_register(&bdrv_backup_dump_drive);
177 +block_init(bdrv_backup_dump_init);
180 +BlockDriverState *bdrv_backup_dump_create(
181 + int dump_cb_block_size,
182 + uint64_t byte_size,
183 + BackupDumpFunc *dump_cb,
184 + void *dump_cb_data,
187 + BDRVBackupDumpState *state;
188 + BlockDriverState *bs = bdrv_new_open_driver(
189 + &bdrv_backup_dump_drive, NULL, BDRV_O_RDWR, errp);
195 + bs->total_sectors = byte_size / BDRV_SECTOR_SIZE;
196 + bs->opaque = state = g_new0(BDRVBackupDumpState, 1);
198 + state->dump_cb_block_size = dump_cb_block_size;
199 + state->byte_size = byte_size;
200 + state->dump_cb = dump_cb;
201 + state->dump_cb_data = dump_cb_data;
205 diff --git a/block/backup.c b/block/backup.c
206 index 5f373a4f9b..1bcc7faa32 100644
211 #define BACKUP_CLUSTER_SIZE_DEFAULT (1 << 16)
213 -typedef struct BackupBlockJob {
215 - BlockDriverState *backup_top;
216 - BlockDriverState *source_bs;
218 - BdrvDirtyBitmap *sync_bitmap;
220 - MirrorSyncMode sync_mode;
221 - BitmapSyncMode bitmap_mode;
222 - BlockdevOnError on_source_error;
223 - BlockdevOnError on_target_error;
225 - uint64_t bytes_read;
226 - int64_t cluster_size;
228 - BlockCopyState *bcs;
231 static const BlockJobDriver backup_job_driver;
233 static void backup_progress_bytes_callback(int64_t bytes, void *opaque)
234 @@ -423,6 +405,11 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
238 + BlockDriverInfo bdi;
239 + if (bdrv_get_info(bs, &bdi) == 0) {
240 + cluster_size = MAX(cluster_size, bdi.cluster_size);
244 * If source is in backing chain of target assume that target is going to be
245 * used for "image fleecing", i.e. it should represent a kind of snapshot of
246 diff --git a/include/block/block_int.h b/include/block/block_int.h
247 index 5094ae1e95..dc72197388 100644
248 --- a/include/block/block_int.h
249 +++ b/include/block/block_int.h
252 #define BLOCK_PROBE_BUF_SIZE 512
254 +typedef int BackupDumpFunc(void *opaque, uint64_t offset, uint64_t bytes, const void *buf);
256 +BlockDriverState *bdrv_backuo_dump_create(
257 + int dump_cb_block_size,
258 + uint64_t byte_size,
259 + BackupDumpFunc *dump_cb,
260 + void *dump_cb_data,
263 +// Needs to be defined here, since it's used in blockdev.c to detect PVE backup
264 +// jobs with source_bs
265 +typedef struct BlockCopyState BlockCopyState;
266 +typedef struct BackupBlockJob {
268 + BlockDriverState *backup_top;
269 + BlockDriverState *source_bs;
271 + BdrvDirtyBitmap *sync_bitmap;
273 + MirrorSyncMode sync_mode;
274 + BitmapSyncMode bitmap_mode;
275 + BlockdevOnError on_source_error;
276 + BlockdevOnError on_target_error;
278 + uint64_t bytes_read;
279 + int64_t cluster_size;
281 + BlockCopyState *bcs;
284 enum BdrvTrackedRequestType {
287 diff --git a/job.c b/job.c
288 index e82253e041..bcbbb0be02 100644
291 @@ -269,7 +269,8 @@ static bool job_started(Job *job)
295 -static bool job_should_pause(Job *job)
296 +bool job_should_pause(Job *job);
297 +bool job_should_pause(Job *job)
299 return job->pause_count > 0;