Skip to content

Commit

Permalink
block: Parse 'detect-zeroes' in bdrv_open_common()
Browse files Browse the repository at this point in the history
Amongst others, this means that you can now use the 'detect-zeroes'
option for non-top-level nodes in blockdev-add, like the QAPI schema
promises.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
  • Loading branch information
kevmw committed Sep 29, 2016
1 parent 0a4279d commit 692e01a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
33 changes: 33 additions & 0 deletions block.c
Expand Up @@ -42,6 +42,7 @@
#include "qapi-event.h"
#include "qemu/cutils.h"
#include "qemu/id.h"
#include "qapi/util.h"

#ifdef CONFIG_BSD
#include <sys/ioctl.h>
Expand Down Expand Up @@ -954,6 +955,11 @@ static QemuOptsList bdrv_runtime_opts = {
.type = QEMU_OPT_BOOL,
.help = "Node is opened in read-only mode",
},
{
.name = "detect-zeroes",
.type = QEMU_OPT_STRING,
.help = "try to optimize zero writes (off, on, unmap)",
},
{ /* end of list */ }
},
};
Expand All @@ -970,6 +976,7 @@ static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file,
const char *filename;
const char *driver_name = NULL;
const char *node_name = NULL;
const char *detect_zeroes;
QemuOpts *opts;
BlockDriver *drv;
Error *local_err = NULL;
Expand Down Expand Up @@ -1038,6 +1045,32 @@ static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file,
}
}

detect_zeroes = qemu_opt_get(opts, "detect-zeroes");
if (detect_zeroes) {
BlockdevDetectZeroesOptions value =
qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
detect_zeroes,
BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
&local_err);
if (local_err) {
error_propagate(errp, local_err);
ret = -EINVAL;
goto fail_opts;
}

if (value == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
!(bs->open_flags & BDRV_O_UNMAP))
{
error_setg(errp, "setting detect-zeroes to unmap is not allowed "
"without setting discard operation to unmap");
ret = -EINVAL;
goto fail_opts;
}

bs->detect_zeroes = value;
}

if (filename != NULL) {
pstrcpy(bs->filename, sizeof(bs->filename), filename);
} else {
Expand Down
9 changes: 1 addition & 8 deletions blockdev.c
Expand Up @@ -658,7 +658,6 @@ static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
BlockDriverState *bs;
QemuOpts *opts;
Error *local_error = NULL;
BlockdevDetectZeroesOptions detect_zeroes;
int bdrv_flags = 0;

opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
Expand All @@ -673,7 +672,7 @@ static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
}

extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
&detect_zeroes, &local_error);
NULL, &local_error);
if (local_error) {
error_propagate(errp, local_error);
goto fail;
Expand All @@ -695,8 +694,6 @@ static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
goto fail_no_bs_opts;
}

bs->detect_zeroes = detect_zeroes;

fail_no_bs_opts:
qemu_opts_del(opts);
return bs;
Expand Down Expand Up @@ -4136,10 +4133,6 @@ static QemuOptsList qemu_root_bds_opts = {
.name = "copy-on-read",
.type = QEMU_OPT_BOOL,
.help = "copy read data from backing file into image file",
},{
.name = "detect-zeroes",
.type = QEMU_OPT_STRING,
.help = "try to optimize zero writes (off, on, unmap)",
},
{ /* end of list */ }
},
Expand Down

0 comments on commit 692e01a

Please sign in to comment.