]>
Commit | Line | Data |
---|---|---|
23102ed6 | 1 | From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
95259824 WB |
2 | From: Wolfgang Bumiller <w.bumiller@proxmox.com> |
3 | Date: Thu, 17 Mar 2016 11:33:37 +0100 | |
23102ed6 | 4 | Subject: [PATCH] block: add the zeroinit block driver filter |
95259824 WB |
5 | |
6 | --- | |
7 | block/Makefile.objs | 1 + | |
6838f038 WB |
8 | block/zeroinit.c | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++++ |
9 | 2 files changed, 203 insertions(+) | |
95259824 WB |
10 | create mode 100644 block/zeroinit.c |
11 | ||
12 | diff --git a/block/Makefile.objs b/block/Makefile.objs | |
6838f038 | 13 | index 6eaf78a046..823e60cda6 100644 |
95259824 WB |
14 | --- a/block/Makefile.objs |
15 | +++ b/block/Makefile.objs | |
6838f038 | 16 | @@ -4,6 +4,7 @@ block-obj-y += qed.o qed-l2-cache.o qed-table.o qed-cluster.o |
95259824 | 17 | block-obj-y += qed-check.o |
a544966d | 18 | block-obj-y += vhdx.o vhdx-endian.o vhdx-log.o |
95259824 WB |
19 | block-obj-y += quorum.o |
20 | +block-obj-y += zeroinit.o | |
21 | block-obj-y += parallels.o blkdebug.o blkverify.o blkreplay.o | |
22 | block-obj-y += block-backend.o snapshot.o qapi.o | |
a544966d | 23 | block-obj-$(CONFIG_WIN32) += file-win32.o win32-aio.o |
95259824 WB |
24 | diff --git a/block/zeroinit.c b/block/zeroinit.c |
25 | new file mode 100644 | |
6838f038 | 26 | index 0000000000..37f588f75c |
95259824 WB |
27 | --- /dev/null |
28 | +++ b/block/zeroinit.c | |
6838f038 | 29 | @@ -0,0 +1,202 @@ |
95259824 WB |
30 | +/* |
31 | + * Filter to fake a zero-initialized block device. | |
32 | + * | |
33 | + * Copyright (c) 2016 Wolfgang Bumiller <w.bumiller@proxmox.com> | |
34 | + * Copyright (c) 2016 Proxmox Server Solutions GmbH | |
35 | + * | |
36 | + * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
37 | + * See the COPYING file in the top-level directory. | |
38 | + */ | |
39 | + | |
40 | +#include "qemu/osdep.h" | |
41 | +#include "qapi/error.h" | |
42 | +#include "block/block_int.h" | |
43 | +#include "qapi/qmp/qdict.h" | |
44 | +#include "qapi/qmp/qstring.h" | |
45 | +#include "qemu/cutils.h" | |
46 | + | |
47 | +typedef struct { | |
48 | + bool has_zero_init; | |
49 | + int64_t extents; | |
50 | +} BDRVZeroinitState; | |
51 | + | |
52 | +/* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */ | |
53 | +static void zeroinit_parse_filename(const char *filename, QDict *options, | |
54 | + Error **errp) | |
55 | +{ | |
56 | + QString *raw_path; | |
57 | + | |
58 | + /* Parse the blkverify: prefix */ | |
59 | + if (!strstart(filename, "zeroinit:", &filename)) { | |
60 | + /* There was no prefix; therefore, all options have to be already | |
61 | + present in the QDict (except for the filename) */ | |
62 | + return; | |
63 | + } | |
64 | + | |
65 | + raw_path = qstring_from_str(filename); | |
66 | + qdict_put(options, "x-next", raw_path); | |
67 | +} | |
68 | + | |
69 | +static QemuOptsList runtime_opts = { | |
70 | + .name = "zeroinit", | |
71 | + .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
72 | + .desc = { | |
73 | + { | |
74 | + .name = "x-next", | |
75 | + .type = QEMU_OPT_STRING, | |
76 | + .help = "[internal use only, will be removed]", | |
77 | + }, | |
78 | + { | |
79 | + .name = "x-zeroinit", | |
80 | + .type = QEMU_OPT_BOOL, | |
81 | + .help = "set has_initialized_zero flag", | |
82 | + }, | |
83 | + { /* end of list */ } | |
84 | + }, | |
85 | +}; | |
86 | + | |
87 | +static int zeroinit_open(BlockDriverState *bs, QDict *options, int flags, | |
88 | + Error **errp) | |
89 | +{ | |
90 | + BDRVZeroinitState *s = bs->opaque; | |
91 | + QemuOpts *opts; | |
92 | + Error *local_err = NULL; | |
93 | + int ret; | |
94 | + | |
95 | + s->extents = 0; | |
96 | + | |
97 | + opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); | |
98 | + qemu_opts_absorb_qdict(opts, options, &local_err); | |
99 | + if (local_err) { | |
100 | + error_propagate(errp, local_err); | |
101 | + ret = -EINVAL; | |
102 | + goto fail; | |
103 | + } | |
104 | + | |
105 | + /* Open the raw file */ | |
106 | + bs->file = bdrv_open_child(qemu_opt_get(opts, "x-next"), options, "next", | |
107 | + bs, &child_file, false, &local_err); | |
108 | + if (local_err) { | |
109 | + ret = -EINVAL; | |
110 | + error_propagate(errp, local_err); | |
111 | + goto fail; | |
112 | + } | |
113 | + | |
114 | + /* set the options */ | |
115 | + s->has_zero_init = qemu_opt_get_bool(opts, "x-zeroinit", true); | |
116 | + | |
117 | + ret = 0; | |
118 | +fail: | |
119 | + if (ret < 0) { | |
120 | + bdrv_unref_child(bs, bs->file); | |
121 | + } | |
122 | + qemu_opts_del(opts); | |
123 | + return ret; | |
124 | +} | |
125 | + | |
126 | +static void zeroinit_close(BlockDriverState *bs) | |
127 | +{ | |
128 | + BDRVZeroinitState *s = bs->opaque; | |
129 | + (void)s; | |
130 | +} | |
131 | + | |
132 | +static int64_t zeroinit_getlength(BlockDriverState *bs) | |
133 | +{ | |
134 | + return bdrv_getlength(bs->file->bs); | |
135 | +} | |
136 | + | |
6838f038 WB |
137 | +static int coroutine_fn zeroinit_co_preadv(BlockDriverState *bs, |
138 | + uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) | |
95259824 | 139 | +{ |
6838f038 | 140 | + return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags); |
95259824 WB |
141 | +} |
142 | + | |
143 | +static int coroutine_fn zeroinit_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, | |
144 | + int count, BdrvRequestFlags flags) | |
145 | +{ | |
146 | + BDRVZeroinitState *s = bs->opaque; | |
147 | + if (offset >= s->extents) | |
148 | + return 0; | |
149 | + return bdrv_pwrite_zeroes(bs->file, offset, count, flags); | |
150 | +} | |
151 | + | |
6838f038 WB |
152 | +static int coroutine_fn zeroinit_co_pwritev(BlockDriverState *bs, |
153 | + uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) | |
95259824 WB |
154 | +{ |
155 | + BDRVZeroinitState *s = bs->opaque; | |
6838f038 | 156 | + int64_t extents = offset + bytes; |
95259824 WB |
157 | + if (extents > s->extents) |
158 | + s->extents = extents; | |
6838f038 | 159 | + return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); |
95259824 WB |
160 | +} |
161 | + | |
162 | +static bool zeroinit_recurse_is_first_non_filter(BlockDriverState *bs, | |
163 | + BlockDriverState *candidate) | |
164 | +{ | |
165 | + return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate); | |
166 | +} | |
167 | + | |
168 | +static coroutine_fn int zeroinit_co_flush(BlockDriverState *bs) | |
169 | +{ | |
170 | + return bdrv_co_flush(bs->file->bs); | |
171 | +} | |
172 | + | |
173 | +static int zeroinit_has_zero_init(BlockDriverState *bs) | |
174 | +{ | |
175 | + BDRVZeroinitState *s = bs->opaque; | |
176 | + return s->has_zero_init; | |
177 | +} | |
178 | + | |
a544966d WB |
179 | +static int coroutine_fn zeroinit_co_pdiscard(BlockDriverState *bs, |
180 | + int64_t offset, int count) | |
95259824 | 181 | +{ |
a544966d | 182 | + return bdrv_co_pdiscard(bs->file->bs, offset, count); |
95259824 WB |
183 | +} |
184 | + | |
6838f038 WB |
185 | +static int zeroinit_truncate(BlockDriverState *bs, int64_t offset, |
186 | + PreallocMode prealloc, Error **errp) | |
95259824 | 187 | +{ |
6838f038 | 188 | + return bdrv_truncate(bs->file, offset, prealloc, 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, | |
67af0fa4 | 205 | + .bdrv_child_perm = bdrv_filter_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, | |
214 | + .bdrv_recurse_is_first_non_filter = zeroinit_recurse_is_first_non_filter, | |
215 | + | |
216 | + .bdrv_has_zero_init = zeroinit_has_zero_init, | |
217 | + | |
6838f038 | 218 | + .bdrv_co_get_block_status = bdrv_co_get_block_status_from_file, |
95259824 | 219 | + |
a544966d | 220 | + .bdrv_co_pdiscard = zeroinit_co_pdiscard, |
95259824 WB |
221 | + |
222 | + .bdrv_truncate = zeroinit_truncate, | |
223 | + .bdrv_get_info = zeroinit_get_info, | |
224 | +}; | |
225 | + | |
226 | +static void bdrv_zeroinit_init(void) | |
227 | +{ | |
228 | + bdrv_register(&bdrv_zeroinit); | |
229 | +} | |
230 | + | |
231 | +block_init(bdrv_zeroinit_init); | |
232 | -- | |
45169293 | 233 | 2.11.0 |
95259824 | 234 |