qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Max Reitz <mreitz@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, Fam Zheng <famz@redhat.com>,
	Jeff Cody <jcody@redhat.com>,
	Markus Armbruster <armbru@redhat.com>,
	Max Reitz <mreitz@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	john@redhat.com
Subject: [Qemu-devel] [PATCH 35/50] blockdev: Pull out blockdev option extraction
Date: Mon, 26 Jan 2015 11:03:09 -0500	[thread overview]
Message-ID: <1422288204-29271-36-git-send-email-mreitz@redhat.com> (raw)
In-Reply-To: <1422288204-29271-1-git-send-email-mreitz@redhat.com>

Extract some of the blockdev option extraction code from blockdev_init()
into an 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 <mreitz@redhat.com>
---
 blockdev.c | 201 +++++++++++++++++++++++++++++++++----------------------------
 1 file changed, 108 insertions(+), 93 deletions(-)

diff --git a/blockdev.c b/blockdev.c
index d99edbb..d573c5c 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -341,24 +341,123 @@ 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,
+    Error **errp)
+{
+    const char *discard, *aio;
+    Error *local_error = NULL;
+
+    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, "cache.writeback", true)) {
+        *bdrv_flags |= BDRV_O_CACHE_WB;
+    }
+    if (qemu_opt_get_bool(opts, "cache.direct", false)) {
+        *bdrv_flags |= BDRV_O_NOCACHE;
+    }
+    if (qemu_opt_get_bool(opts, "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);
+
+    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;
+    BlockdevDetectZeroesOptions detect_zeroes =
+        BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
 
     /* Check common options by copying from bs_opts to opts, all other options
      * stay in bs_opts for processing by bdrv_open(). */
@@ -383,38 +482,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, "cache.writeback", true)) {
-        bdrv_flags |= BDRV_O_CACHE_WB;
-    }
-    if (qemu_opt_get_bool(opts, "cache.direct", false)) {
-        bdrv_flags |= BDRV_O_NOCACHE;
-    }
-    if (qemu_opt_get_bool(opts, "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,
+                                    &error);
+    if (error) {
+        error_propagate(errp, error);
+        goto early_err;
     }
-#endif
 
     if ((buf = qemu_opt_get(opts, "format")) != NULL) {
         if (is_help_option(buf)) {
@@ -431,41 +505,6 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
         qdict_put_obj(bs_opts, "driver", QOBJECT(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);
-
-    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);
@@ -484,40 +523,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;
@@ -529,7 +544,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.1.0

  parent reply	other threads:[~2015-01-26 16:04 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-26 16:02 [Qemu-devel] [PATCH 00/50] blockdev: BlockBackend and media Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 01/50] blockdev: Allow creation of BDS trees without BB Max Reitz
2015-01-27 18:22   ` Eric Blake
2015-01-27 18:26     ` Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 02/50] iotests: Only create BB if necessary Max Reitz
2015-01-27 18:49   ` Eric Blake
2015-01-26 16:02 ` [Qemu-devel] [PATCH 03/50] hw/block/fdc: Implement tray status Max Reitz
2015-01-27 18:58   ` Eric Blake
2015-01-26 16:02 ` [Qemu-devel] [PATCH 04/50] hw/usb-storage: Check whether BB is inserted Max Reitz
2015-01-27 19:05   ` Eric Blake
2015-01-26 16:02 ` [Qemu-devel] [PATCH 05/50] block: Fix BB AIOCB AioContext without BDS Max Reitz
2015-01-27 19:12   ` Eric Blake
2015-01-26 16:02 ` [Qemu-devel] [PATCH 06/50] block: Add blk_is_available() Max Reitz
2015-01-27 19:15   ` Eric Blake
2015-01-27 19:16     ` Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 07/50] block: Make bdrv_is_inserted() recursive Max Reitz
2015-01-27 19:18   ` Eric Blake
2015-01-26 16:02 ` [Qemu-devel] [PATCH 08/50] block/quorum: Implement bdrv_is_inserted() Max Reitz
2015-01-27 19:20   ` Eric Blake
2015-01-26 16:02 ` [Qemu-devel] [PATCH 09/50] block: Move guest_block_size into BlockBackend Max Reitz
2015-01-27 19:22   ` Eric Blake
2015-01-27 19:37     ` John Priddy
2015-01-27 19:40       ` Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 10/50] block: Remove wr_highest_offset from BlockAcctStats Max Reitz
2015-01-27 20:01   ` Eric Blake
2015-01-27 20:02     ` Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 11/50] block: Move BlockAcctStats into BlockBackend Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 12/50] block: Move I/O status and error actions into BB Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 13/50] block: Add BlockBackendRootState Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 14/50] block: Make some BB functions fall back to BBRS Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 15/50] block: Fail requests to empty BlockBackend Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 16/50] block: Prepare remaining BB functions for NULL BDS Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 17/50] block: Respect empty BB in bdrv_lookup_bs() Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 18/50] block: Respect empty BB in bdrv_query_info() Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 19/50] blockdev: Use BlockBackend for blockdev-backup TA Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 20/50] blockdev: Check blk_is_available() in sn-del-int-sync Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 21/50] blockdev: Check BB validity in internal snapshot TA Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 22/50] blockdev: Check BB validity in drive-backup TA Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 23/50] blockdev: Catch NULL BDS in block_set_io_throttle Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 24/50] blockdev: Check BB validity in block-stream Max Reitz
2015-01-26 16:02 ` [Qemu-devel] [PATCH 25/50] blockdev: Check BB validity in block-commit Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 26/50] blockdev: Check BB validity in drive-backup Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 27/50] blockdev: Check BB validity in blockdev-backup Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 28/50] blockdev: Check BB validity in drive-mirror Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 29/50] blockdev: Check BB validity in find_block_job() Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 30/50] blockdev: Check BB validity in change-backing-file Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 31/50] block: Add blk_insert_bs() Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 32/50] blockdev: Check BB validity in eject and change Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 33/50] blockdev: Respect NULL BDS in do_drive_del() Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 34/50] blockdev: Do not create BDS for empty drive Max Reitz
2015-01-26 16:03 ` Max Reitz [this message]
2015-01-26 16:03 ` [Qemu-devel] [PATCH 36/50] blockdev: Allow more options for BB-less BDS tree Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 37/50] block: Add blk_remove_bs() Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 38/50] blockdev: Add blockdev-open-tray Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 39/50] blockdev: Add blockdev-close-tray Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 40/50] blockdev: Add blockdev-remove-medium Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 41/50] blockdev: Add blockdev-insert-medium Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 42/50] blockdev: Implement eject with basic operations Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 43/50] blockdev: Implement change " Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 44/50] block: Inquire tray state before tray-moved events Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 45/50] qmp: Introduce blockdev-change-medium Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 46/50] hmp: Use blockdev-change-medium for change command Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 47/50] blockdev: Add read-only option to blockdev-change-medium Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 48/50] hmp: Add read-only option to change command Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 49/50] iotests: More options for VM.add_drive() Max Reitz
2015-01-26 16:03 ` [Qemu-devel] [PATCH 50/50] iotests: Add test for change-related QMP commands Max Reitz
2015-01-27 19:30 ` [Qemu-devel] [PATCH 00/50] blockdev: BlockBackend and media Eric Blake
2015-01-27 19:38   ` Max Reitz
2015-01-27 19:49 ` Max Reitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1422288204-29271-36-git-send-email-mreitz@redhat.com \
    --to=mreitz@redhat.com \
    --cc=armbru@redhat.com \
    --cc=famz@redhat.com \
    --cc=jcody@redhat.com \
    --cc=john@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).