1 From b3bf300710e38fd4692f0d761c75f4ba3a7edd7e 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 44/55] block: add the zeroinit block driver filter
7 block/Makefile.objs | 1 +
8 block/zeroinit.c | 216 ++++++++++++++++++++++++++++++++++++++++++++++++++++
9 2 files changed, 217 insertions(+)
10 create mode 100644 block/zeroinit.c
12 diff --git a/block/Makefile.objs b/block/Makefile.objs
13 index 44a5416..9e2cdbc 100644
14 --- a/block/Makefile.objs
15 +++ b/block/Makefile.objs
16 @@ -4,6 +4,7 @@ block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
17 block-obj-y += qed-check.o
18 block-obj-$(CONFIG_VHDX) += vhdx.o vhdx-endian.o vhdx-log.o
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
23 block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
24 diff --git a/block/zeroinit.c b/block/zeroinit.c
26 index 0000000..04a9bd0
28 +++ b/block/zeroinit.c
31 + * Filter to fake a zero-initialized block device.
33 + * Copyright (c) 2016 Wolfgang Bumiller <w.bumiller@proxmox.com>
34 + * Copyright (c) 2016 Proxmox Server Solutions GmbH
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.
40 +#include "block/block_int.h"
41 +#include "qapi/qmp/qdict.h"
42 +#include "qapi/qmp/qstring.h"
49 +/* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
50 +static void zeroinit_parse_filename(const char *filename, QDict *options,
55 + /* Parse the blkverify: prefix */
56 + if (!strstart(filename, "zeroinit:", &filename)) {
57 + /* There was no prefix; therefore, all options have to be already
58 + present in the QDict (except for the filename) */
62 + raw_path = qstring_from_str(filename);
63 + qdict_put(options, "x-next", raw_path);
66 +static QemuOptsList runtime_opts = {
68 + .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
72 + .type = QEMU_OPT_STRING,
73 + .help = "[internal use only, will be removed]",
76 + .name = "x-zeroinit",
77 + .type = QEMU_OPT_BOOL,
78 + .help = "set has_initialized_zero flag",
80 + { /* end of list */ }
84 +static int zeroinit_open(BlockDriverState *bs, QDict *options, int flags,
87 + BDRVZeroinitState *s = bs->opaque;
89 + Error *local_err = NULL;
94 + opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
95 + qemu_opts_absorb_qdict(opts, options, &local_err);
97 + error_propagate(errp, local_err);
102 + /* Open the raw file */
103 + bs->file = bdrv_open_child(qemu_opt_get(opts, "x-next"), options, "next",
104 + bs, &child_file, false, &local_err);
107 + error_propagate(errp, local_err);
111 + /* set the options */
112 + s->has_zero_init = qemu_opt_get_bool(opts, "x-zeroinit", true);
117 + bdrv_unref_child(bs, bs->file);
119 + qemu_opts_del(opts);
123 +static void zeroinit_close(BlockDriverState *bs)
125 + BDRVZeroinitState *s = bs->opaque;
129 +static int64_t zeroinit_getlength(BlockDriverState *bs)
131 + return bdrv_getlength(bs->file->bs);
134 +static BlockAIOCB *zeroinit_aio_readv(BlockDriverState *bs,
135 + int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
136 + BlockCompletionFunc *cb, void *opaque)
138 + return bdrv_aio_readv(bs->file->bs, sector_num, qiov, nb_sectors,
142 +static int coroutine_fn zeroinit_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
143 + int nb_sectors, BdrvRequestFlags flags)
145 + BDRVZeroinitState *s = bs->opaque;
146 + if (sector_num >= s->extents)
148 + return bdrv_write_zeroes(bs->file->bs, sector_num, nb_sectors, flags);
151 +static BlockAIOCB *zeroinit_aio_writev(BlockDriverState *bs,
152 + int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
153 + BlockCompletionFunc *cb, void *opaque)
155 + BDRVZeroinitState *s = bs->opaque;
156 + int64_t extents = sector_num + nb_sectors + 1;
157 + if (extents > s->extents)
158 + s->extents = extents;
159 + return bdrv_aio_writev(bs->file->bs, sector_num, qiov, nb_sectors,
163 +static BlockAIOCB *zeroinit_aio_flush(BlockDriverState *bs,
164 + BlockCompletionFunc *cb,
167 + return bdrv_aio_flush(bs->file->bs, cb, opaque);
170 +static bool zeroinit_recurse_is_first_non_filter(BlockDriverState *bs,
171 + BlockDriverState *candidate)
173 + return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
176 +static coroutine_fn int zeroinit_co_flush(BlockDriverState *bs)
178 + return bdrv_co_flush(bs->file->bs);
181 +static int zeroinit_has_zero_init(BlockDriverState *bs)
183 + BDRVZeroinitState *s = bs->opaque;
184 + return s->has_zero_init;
187 +static int64_t coroutine_fn zeroinit_co_get_block_status(BlockDriverState *bs,
188 + int64_t sector_num,
189 + int nb_sectors, int *pnum)
191 + return bdrv_get_block_status(bs->file->bs, sector_num, nb_sectors, pnum);
194 +static coroutine_fn BlockAIOCB *zeroinit_aio_discard(BlockDriverState *bs,
195 + int64_t sector_num, int nb_sectors,
196 + BlockCompletionFunc *cb, void *opaque)
198 + return bdrv_aio_discard(bs->file->bs, sector_num, nb_sectors, cb, opaque);
201 +static int zeroinit_truncate(BlockDriverState *bs, int64_t offset)
203 + return bdrv_truncate(bs->file->bs, offset);
206 +static int zeroinit_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
208 + return bdrv_get_info(bs->file->bs, bdi);
211 +static BlockDriver bdrv_zeroinit = {
212 + .format_name = "zeroinit",
213 + .protocol_name = "zeroinit",
214 + .instance_size = sizeof(BDRVZeroinitState),
216 + .bdrv_parse_filename = zeroinit_parse_filename,
217 + .bdrv_file_open = zeroinit_open,
218 + .bdrv_close = zeroinit_close,
219 + .bdrv_getlength = zeroinit_getlength,
220 + .bdrv_co_flush_to_disk = zeroinit_co_flush,
222 + .bdrv_co_write_zeroes = zeroinit_co_write_zeroes,
223 + .bdrv_aio_writev = zeroinit_aio_writev,
224 + .bdrv_aio_readv = zeroinit_aio_readv,
225 + .bdrv_aio_flush = zeroinit_aio_flush,
228 + .bdrv_recurse_is_first_non_filter = zeroinit_recurse_is_first_non_filter,
230 + .bdrv_has_zero_init = zeroinit_has_zero_init,
232 + .bdrv_co_get_block_status = zeroinit_co_get_block_status,
234 + .bdrv_aio_discard = zeroinit_aio_discard,
236 + .bdrv_truncate = zeroinit_truncate,
237 + .bdrv_get_info = zeroinit_get_info,
240 +static void bdrv_zeroinit_init(void)
242 + bdrv_register(&bdrv_zeroinit);
245 +block_init(bdrv_zeroinit_init);