]>
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 SR |
10 | block/meson.build | 1 + |
11 | block/zeroinit.c | 196 ++++++++++++++++++++++++++++++++++++++++++++++ | |
12 | 2 files changed, 197 insertions(+) | |
95259824 WB |
13 | create mode 100644 block/zeroinit.c |
14 | ||
817b7667 | 15 | diff --git a/block/meson.build b/block/meson.build |
5b15e2ec | 16 | index 60bc305597..ad40c10b6a 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 | |
4567474e | 29 | index 0000000000..20ee611f22 |
95259824 WB |
30 | --- /dev/null |
31 | +++ b/block/zeroinit.c | |
817b7667 | 32 | @@ -0,0 +1,196 @@ |
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", | |
60ae3775 | 112 | + bs, &child_of_bds, BDRV_CHILD_FILTERED, false, &local_err); |
95259824 WB |
113 | + if (local_err) { |
114 | + ret = -EINVAL; | |
115 | + error_propagate(errp, local_err); | |
116 | + goto fail; | |
117 | + } | |
118 | + | |
119 | + /* set the options */ | |
120 | + s->has_zero_init = qemu_opt_get_bool(opts, "x-zeroinit", true); | |
121 | + | |
122 | + ret = 0; | |
123 | +fail: | |
124 | + if (ret < 0) { | |
125 | + bdrv_unref_child(bs, bs->file); | |
126 | + } | |
127 | + qemu_opts_del(opts); | |
128 | + return ret; | |
129 | +} | |
130 | + | |
131 | +static void zeroinit_close(BlockDriverState *bs) | |
132 | +{ | |
133 | + BDRVZeroinitState *s = bs->opaque; | |
134 | + (void)s; | |
135 | +} | |
136 | + | |
137 | +static int64_t zeroinit_getlength(BlockDriverState *bs) | |
138 | +{ | |
139 | + return bdrv_getlength(bs->file->bs); | |
140 | +} | |
141 | + | |
6838f038 | 142 | +static int coroutine_fn zeroinit_co_preadv(BlockDriverState *bs, |
4567474e | 143 | + int64_t offset, int64_t bytes, QEMUIOVector *qiov, BdrvRequestFlags flags) |
95259824 | 144 | +{ |
6838f038 | 145 | + return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags); |
95259824 WB |
146 | +} |
147 | + | |
148 | +static int coroutine_fn zeroinit_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, | |
4567474e | 149 | + int64_t bytes, BdrvRequestFlags flags) |
95259824 WB |
150 | +{ |
151 | + BDRVZeroinitState *s = bs->opaque; | |
152 | + if (offset >= s->extents) | |
153 | + return 0; | |
4567474e | 154 | + return bdrv_pwrite_zeroes(bs->file, offset, bytes, flags); |
95259824 WB |
155 | +} |
156 | + | |
6838f038 | 157 | +static int coroutine_fn zeroinit_co_pwritev(BlockDriverState *bs, |
4567474e | 158 | + int64_t offset, int64_t bytes, QEMUIOVector *qiov, BdrvRequestFlags flags) |
95259824 WB |
159 | +{ |
160 | + BDRVZeroinitState *s = bs->opaque; | |
6838f038 | 161 | + int64_t extents = offset + bytes; |
95259824 WB |
162 | + if (extents > s->extents) |
163 | + s->extents = extents; | |
6838f038 | 164 | + return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); |
95259824 WB |
165 | +} |
166 | + | |
95259824 WB |
167 | +static coroutine_fn int zeroinit_co_flush(BlockDriverState *bs) |
168 | +{ | |
169 | + return bdrv_co_flush(bs->file->bs); | |
170 | +} | |
171 | + | |
172 | +static int zeroinit_has_zero_init(BlockDriverState *bs) | |
173 | +{ | |
174 | + BDRVZeroinitState *s = bs->opaque; | |
175 | + return s->has_zero_init; | |
176 | +} | |
177 | + | |
a544966d | 178 | +static int coroutine_fn zeroinit_co_pdiscard(BlockDriverState *bs, |
4567474e | 179 | + int64_t offset, int64_t bytes) |
95259824 | 180 | +{ |
4567474e | 181 | + return bdrv_co_pdiscard(bs->file, offset, bytes); |
95259824 WB |
182 | +} |
183 | + | |
53e83913 | 184 | +static int zeroinit_co_truncate(BlockDriverState *bs, int64_t offset, |
60ae3775 SR |
185 | + _Bool exact, PreallocMode prealloc, |
186 | + BdrvRequestFlags req_flags, Error **errp) | |
95259824 | 187 | +{ |
60ae3775 | 188 | + return bdrv_co_truncate(bs->file, offset, exact, prealloc, req_flags, errp); |
95259824 WB |
189 | +} |
190 | + | |
191 | +static int zeroinit_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) | |
192 | +{ | |
193 | + return bdrv_get_info(bs->file->bs, bdi); | |
194 | +} | |
195 | + | |
196 | +static BlockDriver bdrv_zeroinit = { | |
197 | + .format_name = "zeroinit", | |
198 | + .protocol_name = "zeroinit", | |
199 | + .instance_size = sizeof(BDRVZeroinitState), | |
200 | + | |
201 | + .bdrv_parse_filename = zeroinit_parse_filename, | |
202 | + .bdrv_file_open = zeroinit_open, | |
203 | + .bdrv_close = zeroinit_close, | |
204 | + .bdrv_getlength = zeroinit_getlength, | |
60ae3775 | 205 | + .bdrv_child_perm = bdrv_default_perms, |
95259824 WB |
206 | + .bdrv_co_flush_to_disk = zeroinit_co_flush, |
207 | + | |
208 | + .bdrv_co_pwrite_zeroes = zeroinit_co_pwrite_zeroes, | |
6838f038 WB |
209 | + .bdrv_co_pwritev = zeroinit_co_pwritev, |
210 | + .bdrv_co_preadv = zeroinit_co_preadv, | |
211 | + .bdrv_co_flush = zeroinit_co_flush, | |
95259824 WB |
212 | + |
213 | + .is_filter = true, | |
95259824 | 214 | + |
53e83913 | 215 | + .bdrv_has_zero_init = zeroinit_has_zero_init, |
95259824 | 216 | + |
53e83913 | 217 | + .bdrv_co_pdiscard = zeroinit_co_pdiscard, |
95259824 | 218 | + |
53e83913 WB |
219 | + .bdrv_co_truncate = zeroinit_co_truncate, |
220 | + .bdrv_get_info = zeroinit_get_info, | |
95259824 WB |
221 | +}; |
222 | + | |
223 | +static void bdrv_zeroinit_init(void) | |
224 | +{ | |
225 | + bdrv_register(&bdrv_zeroinit); | |
226 | +} | |
227 | + | |
228 | +block_init(bdrv_zeroinit_init); |