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