[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v5 24/38] blockdev: Pull out blockdev option extract
From: |
Max Reitz |
Subject: |
[Qemu-devel] [PATCH v5 24/38] blockdev: Pull out blockdev option extraction |
Date: |
Fri, 18 Sep 2015 17:22:59 +0200 |
Extract some of the blockdev option extraction code from blockdev_init()
into its own function. This simplifies blockdev_init() and will allow
reusing the code in a different function added in a follow-up patch.
Signed-off-by: Max Reitz <address@hidden>
---
blockdev.c | 209 +++++++++++++++++++++++++++++++++----------------------------
1 file changed, 113 insertions(+), 96 deletions(-)
diff --git a/blockdev.c b/blockdev.c
index e76a7de..688ee5f 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -350,25 +350,128 @@ static bool check_throttle_config(ThrottleConfig *cfg,
Error **errp)
typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
+static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
+ ThrottleConfig *throttle_cfg, BlockdevDetectZeroesOptions *detect_zeroes,
+ const char **throttling_group, Error **errp)
+{
+ const char *discard;
+ Error *local_error = NULL;
+#ifdef CONFIG_LINUX_AIO
+ const char *aio;
+#endif
+
+ if (!qemu_opt_get_bool(opts, "read-only", false)) {
+ *bdrv_flags |= BDRV_O_RDWR;
+ }
+ if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
+ *bdrv_flags |= BDRV_O_COPY_ON_READ;
+ }
+
+ if ((discard = qemu_opt_get(opts, "discard")) != NULL) {
+ if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) {
+ error_setg(errp, "Invalid discard option");
+ return;
+ }
+ }
+
+ if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true)) {
+ *bdrv_flags |= BDRV_O_CACHE_WB;
+ }
+ if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
+ *bdrv_flags |= BDRV_O_NOCACHE;
+ }
+ if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
+ *bdrv_flags |= BDRV_O_NO_FLUSH;
+ }
+
+#ifdef CONFIG_LINUX_AIO
+ if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
+ if (!strcmp(aio, "native")) {
+ *bdrv_flags |= BDRV_O_NATIVE_AIO;
+ } else if (!strcmp(aio, "threads")) {
+ /* this is the default */
+ } else {
+ error_setg(errp, "invalid aio option");
+ return;
+ }
+ }
+#endif
+
+ /* disk I/O throttling */
+ memset(throttle_cfg, 0, sizeof(*throttle_cfg));
+ throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
+ qemu_opt_get_number(opts, "throttling.bps-total", 0);
+ throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
+ qemu_opt_get_number(opts, "throttling.bps-read", 0);
+ throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
+ qemu_opt_get_number(opts, "throttling.bps-write", 0);
+ throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
+ qemu_opt_get_number(opts, "throttling.iops-total", 0);
+ throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
+ qemu_opt_get_number(opts, "throttling.iops-read", 0);
+ throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
+ qemu_opt_get_number(opts, "throttling.iops-write", 0);
+
+ throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
+ qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
+ throttle_cfg->buckets[THROTTLE_BPS_READ].max =
+ qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
+ throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
+ qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
+ throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
+ qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
+ throttle_cfg->buckets[THROTTLE_OPS_READ].max =
+ qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
+ throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
+ qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
+
+ throttle_cfg->op_size =
+ qemu_opt_get_number(opts, "throttling.iops-size", 0);
+
+ *throttling_group = qemu_opt_get(opts, "throttling.group");
+
+ if (!check_throttle_config(throttle_cfg, errp)) {
+ return;
+ }
+
+ *detect_zeroes =
+ qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
+ qemu_opt_get(opts, "detect-zeroes"),
+ BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX,
+ BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
+ &local_error);
+ if (local_error) {
+ error_propagate(errp, local_error);
+ return;
+ }
+
+ if (*detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
+ !(*bdrv_flags & BDRV_O_UNMAP))
+ {
+ error_setg(errp, "setting detect-zeroes to unmap is not allowed "
+ "without setting discard operation to unmap");
+ return;
+ }
+}
+
/* Takes the ownership of bs_opts */
static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
Error **errp)
{
const char *buf;
- int ro = 0;
int bdrv_flags = 0;
int on_read_error, on_write_error;
BlockBackend *blk;
BlockDriverState *bs;
ThrottleConfig cfg;
int snapshot = 0;
- bool copy_on_read;
Error *error = NULL;
QemuOpts *opts;
const char *id;
bool has_driver_specific_opts;
- BlockdevDetectZeroesOptions detect_zeroes;
- const char *throttling_group;
+ BlockdevDetectZeroesOptions detect_zeroes =
+ BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
+ const char *throttling_group = NULL;
/* Check common options by copying from bs_opts to opts, all other options
* stay in bs_opts for processing by bdrv_open(). */
@@ -393,38 +496,13 @@ static BlockBackend *blockdev_init(const char *file,
QDict *bs_opts,
/* extract parameters */
snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
- ro = qemu_opt_get_bool(opts, "read-only", 0);
- copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
-
- if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
- if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
- error_setg(errp, "invalid discard option");
- goto early_err;
- }
- }
- if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true)) {
- bdrv_flags |= BDRV_O_CACHE_WB;
- }
- if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
- bdrv_flags |= BDRV_O_NOCACHE;
- }
- if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
- bdrv_flags |= BDRV_O_NO_FLUSH;
- }
-
-#ifdef CONFIG_LINUX_AIO
- if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
- if (!strcmp(buf, "native")) {
- bdrv_flags |= BDRV_O_NATIVE_AIO;
- } else if (!strcmp(buf, "threads")) {
- /* this is the default */
- } else {
- error_setg(errp, "invalid aio option");
- goto early_err;
- }
+ extract_common_blockdev_options(opts, &bdrv_flags, &cfg, &detect_zeroes,
+ &throttling_group, &error);
+ if (error) {
+ error_propagate(errp, error);
+ goto early_err;
}
-#endif
if ((buf = qemu_opt_get(opts, "format")) != NULL) {
if (is_help_option(buf)) {
@@ -441,43 +519,6 @@ static BlockBackend *blockdev_init(const char *file, QDict
*bs_opts,
qdict_put(bs_opts, "driver", qstring_from_str(buf));
}
- /* disk I/O throttling */
- memset(&cfg, 0, sizeof(cfg));
- cfg.buckets[THROTTLE_BPS_TOTAL].avg =
- qemu_opt_get_number(opts, "throttling.bps-total", 0);
- cfg.buckets[THROTTLE_BPS_READ].avg =
- qemu_opt_get_number(opts, "throttling.bps-read", 0);
- cfg.buckets[THROTTLE_BPS_WRITE].avg =
- qemu_opt_get_number(opts, "throttling.bps-write", 0);
- cfg.buckets[THROTTLE_OPS_TOTAL].avg =
- qemu_opt_get_number(opts, "throttling.iops-total", 0);
- cfg.buckets[THROTTLE_OPS_READ].avg =
- qemu_opt_get_number(opts, "throttling.iops-read", 0);
- cfg.buckets[THROTTLE_OPS_WRITE].avg =
- qemu_opt_get_number(opts, "throttling.iops-write", 0);
-
- cfg.buckets[THROTTLE_BPS_TOTAL].max =
- qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
- cfg.buckets[THROTTLE_BPS_READ].max =
- qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
- cfg.buckets[THROTTLE_BPS_WRITE].max =
- qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
- cfg.buckets[THROTTLE_OPS_TOTAL].max =
- qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
- cfg.buckets[THROTTLE_OPS_READ].max =
- qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
- cfg.buckets[THROTTLE_OPS_WRITE].max =
- qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
-
- cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
-
- throttling_group = qemu_opt_get(opts, "throttling.group");
-
- if (!check_throttle_config(&cfg, &error)) {
- error_propagate(errp, error);
- goto early_err;
- }
-
on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
on_write_error = parse_block_error_action(buf, 0, &error);
@@ -496,40 +537,16 @@ static BlockBackend *blockdev_init(const char *file,
QDict *bs_opts,
}
}
- detect_zeroes =
- qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
- qemu_opt_get(opts, "detect-zeroes"),
- BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX,
- BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
- &error);
- if (error) {
- error_propagate(errp, error);
- goto early_err;
- }
-
- if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
- !(bdrv_flags & BDRV_O_UNMAP)) {
- error_setg(errp, "setting detect-zeroes to unmap is not allowed "
- "without setting discard operation to unmap");
- goto early_err;
- }
-
if (snapshot) {
/* always use cache=unsafe with snapshot */
bdrv_flags &= ~BDRV_O_CACHE_MASK;
bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
}
- if (copy_on_read) {
- bdrv_flags |= BDRV_O_COPY_ON_READ;
- }
-
if (runstate_check(RUN_STATE_INMIGRATE)) {
bdrv_flags |= BDRV_O_INCOMING;
}
- bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
-
/* init */
if ((!file || !*file) && !has_driver_specific_opts) {
BlockBackendRootState *blk_rs;
@@ -541,7 +558,7 @@ static BlockBackend *blockdev_init(const char *file, QDict
*bs_opts,
blk_rs = blk_get_root_state(blk);
blk_rs->open_flags = bdrv_flags;
- blk_rs->read_only = ro;
+ blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR);
blk_rs->detect_zeroes = detect_zeroes;
if (throttle_enabled(&cfg)) {
--
2.5.2
- [Qemu-devel] [PATCH v5 15/38] block: Move BlockAcctStats into BlockBackend, (continued)
- [Qemu-devel] [PATCH v5 15/38] block: Move BlockAcctStats into BlockBackend, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 16/38] block: Move I/O status and error actions into BB, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 17/38] block: Add BlockBackendRootState, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 18/38] block: Make some BB functions fall back to BBRS, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 19/38] block: Fail requests to empty BlockBackend, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 20/38] block: Prepare remaining BB functions for NULL BDS, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 21/38] block: Add blk_insert_bs(), Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 22/38] block: Prepare for NULL BDS, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 23/38] blockdev: Do not create BDS for empty drive, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 26/38] block: Add blk_remove_bs(), Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 24/38] blockdev: Pull out blockdev option extraction,
Max Reitz <=
- [Qemu-devel] [PATCH v5 25/38] blockdev: Allow more options for BB-less BDS tree, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 27/38] blockdev: Add blockdev-open-tray, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 28/38] blockdev: Add blockdev-close-tray, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 29/38] blockdev: Add blockdev-remove-medium, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 31/38] blockdev: Implement eject with basic operations, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 32/38] blockdev: Implement change with basic operations, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 33/38] block: Inquire tray state before tray-moved events, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 30/38] blockdev: Add blockdev-insert-medium, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 35/38] hmp: Use blockdev-change-medium for change command, Max Reitz, 2015/09/18
- [Qemu-devel] [PATCH v5 34/38] qmp: Introduce blockdev-change-medium, Max Reitz, 2015/09/18