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