]> git.proxmox.com Git - pve-qemu.git/blame - debian/patches/pve/0019-PVE-block-add-the-zeroinit-block-driver-filter.patch
backup: improve error when copy-before-write fails for fleecing
[pve-qemu.git] / debian / patches / pve / 0019-PVE-block-add-the-zeroinit-block-driver-filter.patch
CommitLineData
23102ed6 1From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
95259824 2From: Wolfgang Bumiller <w.bumiller@proxmox.com>
83faa3fe
TL
3Date: Mon, 6 Apr 2020 12:16:47 +0200
4Subject: [PATCH] PVE: block: add the zeroinit block driver filter
95259824 5
b855dce7 6Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
f1eed34a
FE
7[FE: adapt to changed function signatures
8 adhere to block graph lock requirements]
bf251437 9Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
95259824 10---
817b7667 11 block/meson.build | 1 +
f1eed34a
FE
12 block/zeroinit.c | 214 ++++++++++++++++++++++++++++++++++++++++++++++
13 2 files changed, 215 insertions(+)
95259824
WB
14 create mode 100644 block/zeroinit.c
15
817b7667 16diff --git a/block/meson.build b/block/meson.build
4fbd50e2 17index e1f03fd773..b530e117b5 100644
817b7667
SR
18--- a/block/meson.build
19+++ b/block/meson.build
f1eed34a 20@@ -39,6 +39,7 @@ block_ss.add(files(
10e10933 21 'throttle.c',
f1eed34a 22 'throttle-groups.c',
817b7667
SR
23 'write-threshold.c',
24+ 'zeroinit.c',
8dca018b 25 ), zstd, zlib, gnutls)
817b7667 26
10e10933 27 system_ss.add(when: 'CONFIG_TCG', if_true: files('blkreplay.c'))
95259824
WB
28diff --git a/block/zeroinit.c b/block/zeroinit.c
29new file mode 100644
4fbd50e2 30index 0000000000..696558d8d6
95259824
WB
31--- /dev/null
32+++ b/block/zeroinit.c
f1eed34a 33@@ -0,0 +1,214 @@
95259824
WB
34+/*
35+ * Filter to fake a zero-initialized block device.
36+ *
37+ * Copyright (c) 2016 Wolfgang Bumiller <w.bumiller@proxmox.com>
38+ * Copyright (c) 2016 Proxmox Server Solutions GmbH
39+ *
40+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
41+ * See the COPYING file in the top-level directory.
42+ */
43+
44+#include "qemu/osdep.h"
45+#include "qapi/error.h"
46+#include "block/block_int.h"
bf251437 47+#include "block/block-io.h"
f1eed34a 48+#include "block/graph-lock.h"
95259824
WB
49+#include "qapi/qmp/qdict.h"
50+#include "qapi/qmp/qstring.h"
51+#include "qemu/cutils.h"
53e83913 52+#include "qemu/option.h"
be901f66 53+#include "qemu/module.h"
95259824
WB
54+
55+typedef struct {
56+ bool has_zero_init;
57+ int64_t extents;
58+} BDRVZeroinitState;
59+
60+/* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
61+static void zeroinit_parse_filename(const char *filename, QDict *options,
62+ Error **errp)
63+{
64+ QString *raw_path;
65+
66+ /* Parse the blkverify: prefix */
67+ if (!strstart(filename, "zeroinit:", &filename)) {
68+ /* There was no prefix; therefore, all options have to be already
69+ present in the QDict (except for the filename) */
70+ return;
71+ }
72+
73+ raw_path = qstring_from_str(filename);
74+ qdict_put(options, "x-next", raw_path);
75+}
76+
77+static QemuOptsList runtime_opts = {
78+ .name = "zeroinit",
79+ .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
80+ .desc = {
81+ {
82+ .name = "x-next",
83+ .type = QEMU_OPT_STRING,
84+ .help = "[internal use only, will be removed]",
85+ },
86+ {
87+ .name = "x-zeroinit",
88+ .type = QEMU_OPT_BOOL,
89+ .help = "set has_initialized_zero flag",
90+ },
91+ { /* end of list */ }
92+ },
93+};
94+
95+static int zeroinit_open(BlockDriverState *bs, QDict *options, int flags,
96+ Error **errp)
97+{
98+ BDRVZeroinitState *s = bs->opaque;
f1eed34a 99+ BdrvChild *file = NULL;
95259824
WB
100+ QemuOpts *opts;
101+ Error *local_err = NULL;
102+ int ret;
103+
104+ s->extents = 0;
105+
106+ opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
107+ qemu_opts_absorb_qdict(opts, options, &local_err);
108+ if (local_err) {
109+ error_propagate(errp, local_err);
110+ ret = -EINVAL;
111+ goto fail;
112+ }
113+
114+ /* Open the raw file */
f1eed34a
FE
115+ file = bdrv_open_child(qemu_opt_get(opts, "x-next"), options, "next", bs,
116+ &child_of_bds,
117+ BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, false,
118+ &local_err);
4fbd50e2 119+ bdrv_graph_wrlock();
f1eed34a 120+ bs->file = file;
4fbd50e2 121+ bdrv_graph_wrunlock();
95259824
WB
122+ if (local_err) {
123+ ret = -EINVAL;
124+ error_propagate(errp, local_err);
125+ goto fail;
126+ }
127+
128+ /* set the options */
129+ s->has_zero_init = qemu_opt_get_bool(opts, "x-zeroinit", true);
130+
131+ ret = 0;
132+fail:
133+ if (ret < 0) {
4fbd50e2 134+ bdrv_graph_wrlock();
95259824 135+ bdrv_unref_child(bs, bs->file);
4fbd50e2 136+ bdrv_graph_wrunlock();
95259824
WB
137+ }
138+ qemu_opts_del(opts);
139+ return ret;
140+}
141+
142+static void zeroinit_close(BlockDriverState *bs)
143+{
144+ BDRVZeroinitState *s = bs->opaque;
145+ (void)s;
146+}
147+
f1eed34a
FE
148+static coroutine_fn int64_t GRAPH_RDLOCK
149+zeroinit_co_getlength(BlockDriverState *bs)
95259824 150+{
bf251437 151+ return bdrv_co_getlength(bs->file->bs);
95259824
WB
152+}
153+
f1eed34a
FE
154+static int coroutine_fn GRAPH_RDLOCK
155+zeroinit_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
156+ QEMUIOVector *qiov, BdrvRequestFlags flags)
95259824 157+{
6838f038 158+ return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
95259824
WB
159+}
160+
f1eed34a
FE
161+static int coroutine_fn GRAPH_RDLOCK
162+zeroinit_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
163+ BdrvRequestFlags flags)
95259824
WB
164+{
165+ BDRVZeroinitState *s = bs->opaque;
166+ if (offset >= s->extents)
167+ return 0;
4567474e 168+ return bdrv_pwrite_zeroes(bs->file, offset, bytes, flags);
95259824
WB
169+}
170+
f1eed34a
FE
171+static int coroutine_fn GRAPH_RDLOCK
172+zeroinit_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
173+ QEMUIOVector *qiov, BdrvRequestFlags flags)
95259824
WB
174+{
175+ BDRVZeroinitState *s = bs->opaque;
6838f038 176+ int64_t extents = offset + bytes;
95259824
WB
177+ if (extents > s->extents)
178+ s->extents = extents;
6838f038 179+ return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
95259824
WB
180+}
181+
f1eed34a
FE
182+static coroutine_fn int GRAPH_RDLOCK
183+zeroinit_co_flush(BlockDriverState *bs)
95259824
WB
184+{
185+ return bdrv_co_flush(bs->file->bs);
186+}
187+
f1eed34a
FE
188+static int GRAPH_RDLOCK
189+zeroinit_has_zero_init(BlockDriverState *bs)
95259824
WB
190+{
191+ BDRVZeroinitState *s = bs->opaque;
192+ return s->has_zero_init;
193+}
194+
f1eed34a
FE
195+static int coroutine_fn GRAPH_RDLOCK
196+zeroinit_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
95259824 197+{
4567474e 198+ return bdrv_co_pdiscard(bs->file, offset, bytes);
95259824
WB
199+}
200+
f1eed34a
FE
201+static int GRAPH_RDLOCK
202+zeroinit_co_truncate(BlockDriverState *bs, int64_t offset, _Bool exact,
203+ PreallocMode prealloc, BdrvRequestFlags req_flags,
204+ Error **errp)
95259824 205+{
60ae3775 206+ return bdrv_co_truncate(bs->file, offset, exact, prealloc, req_flags, errp);
95259824
WB
207+}
208+
f1eed34a
FE
209+static coroutine_fn int GRAPH_RDLOCK
210+zeroinit_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
95259824 211+{
bf251437 212+ return bdrv_co_get_info(bs->file->bs, bdi);
95259824
WB
213+}
214+
215+static BlockDriver bdrv_zeroinit = {
216+ .format_name = "zeroinit",
217+ .protocol_name = "zeroinit",
218+ .instance_size = sizeof(BDRVZeroinitState),
219+
220+ .bdrv_parse_filename = zeroinit_parse_filename,
221+ .bdrv_file_open = zeroinit_open,
222+ .bdrv_close = zeroinit_close,
bf251437 223+ .bdrv_co_getlength = zeroinit_co_getlength,
60ae3775 224+ .bdrv_child_perm = bdrv_default_perms,
95259824
WB
225+ .bdrv_co_flush_to_disk = zeroinit_co_flush,
226+
227+ .bdrv_co_pwrite_zeroes = zeroinit_co_pwrite_zeroes,
6838f038
WB
228+ .bdrv_co_pwritev = zeroinit_co_pwritev,
229+ .bdrv_co_preadv = zeroinit_co_preadv,
230+ .bdrv_co_flush = zeroinit_co_flush,
95259824
WB
231+
232+ .is_filter = true,
95259824 233+
53e83913 234+ .bdrv_has_zero_init = zeroinit_has_zero_init,
95259824 235+
53e83913 236+ .bdrv_co_pdiscard = zeroinit_co_pdiscard,
95259824 237+
53e83913 238+ .bdrv_co_truncate = zeroinit_co_truncate,
bf251437 239+ .bdrv_co_get_info = zeroinit_co_get_info,
95259824
WB
240+};
241+
242+static void bdrv_zeroinit_init(void)
243+{
244+ bdrv_register(&bdrv_zeroinit);
245+}
246+
247+block_init(bdrv_zeroinit_init);