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
6 Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
8 block/Makefile.objs | 1 +
9 block/zeroinit.c | 203 ++++++++++++++++++++++++++++++++++++++++++++
10 2 files changed, 204 insertions(+)
11 create mode 100644 block/zeroinit.c
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
27 index 0000000000..64c49ad0e0
29 +++ b/block/zeroinit.c
32 + * Filter to fake a zero-initialized block device.
34 + * Copyright (c) 2016 Wolfgang Bumiller <w.bumiller@proxmox.com>
35 + * Copyright (c) 2016 Proxmox Server Solutions GmbH
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.
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"
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,
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) */
67 + raw_path = qstring_from_str(filename);
68 + qdict_put(options, "x-next", raw_path);
71 +static QemuOptsList runtime_opts = {
73 + .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
77 + .type = QEMU_OPT_STRING,
78 + .help = "[internal use only, will be removed]",
81 + .name = "x-zeroinit",
82 + .type = QEMU_OPT_BOOL,
83 + .help = "set has_initialized_zero flag",
85 + { /* end of list */ }
89 +static int zeroinit_open(BlockDriverState *bs, QDict *options, int flags,
92 + BDRVZeroinitState *s = bs->opaque;
94 + Error *local_err = NULL;
99 + opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
100 + qemu_opts_absorb_qdict(opts, options, &local_err);
102 + error_propagate(errp, local_err);
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);
112 + error_propagate(errp, local_err);
116 + /* set the options */
117 + s->has_zero_init = qemu_opt_get_bool(opts, "x-zeroinit", true);
122 + bdrv_unref_child(bs, bs->file);
124 + qemu_opts_del(opts);
128 +static void zeroinit_close(BlockDriverState *bs)
130 + BDRVZeroinitState *s = bs->opaque;
134 +static int64_t zeroinit_getlength(BlockDriverState *bs)
136 + return bdrv_getlength(bs->file->bs);
139 +static int coroutine_fn zeroinit_co_preadv(BlockDriverState *bs,
140 + uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
142 + return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
145 +static int coroutine_fn zeroinit_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
146 + int count, BdrvRequestFlags flags)
148 + BDRVZeroinitState *s = bs->opaque;
149 + if (offset >= s->extents)
151 + return bdrv_pwrite_zeroes(bs->file, offset, count, flags);
154 +static int coroutine_fn zeroinit_co_pwritev(BlockDriverState *bs,
155 + uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
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);
164 +static bool zeroinit_recurse_is_first_non_filter(BlockDriverState *bs,
165 + BlockDriverState *candidate)
167 + return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
170 +static coroutine_fn int zeroinit_co_flush(BlockDriverState *bs)
172 + return bdrv_co_flush(bs->file->bs);
175 +static int zeroinit_has_zero_init(BlockDriverState *bs)
177 + BDRVZeroinitState *s = bs->opaque;
178 + return s->has_zero_init;
181 +static int coroutine_fn zeroinit_co_pdiscard(BlockDriverState *bs,
182 + int64_t offset, int count)
184 + return bdrv_co_pdiscard(bs->file, offset, count);
187 +static int zeroinit_co_truncate(BlockDriverState *bs, int64_t offset,
188 + PreallocMode prealloc, Error **errp)
190 + return bdrv_co_truncate(bs->file, offset, prealloc, errp);
193 +static int zeroinit_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
195 + return bdrv_get_info(bs->file->bs, bdi);
198 +static BlockDriver bdrv_zeroinit = {
199 + .format_name = "zeroinit",
200 + .protocol_name = "zeroinit",
201 + .instance_size = sizeof(BDRVZeroinitState),
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,
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,
216 + .bdrv_recurse_is_first_non_filter = zeroinit_recurse_is_first_non_filter,
218 + .bdrv_has_zero_init = zeroinit_has_zero_init,
220 + .bdrv_co_block_status = bdrv_co_block_status_from_file,
222 + .bdrv_co_pdiscard = zeroinit_co_pdiscard,
224 + .bdrv_co_truncate = zeroinit_co_truncate,
225 + .bdrv_get_info = zeroinit_get_info,
228 +static void bdrv_zeroinit_init(void)
230 + bdrv_register(&bdrv_zeroinit);
233 +block_init(bdrv_zeroinit_init);