9ae3a8
From 90321b7139eb0c078f2dffa2d78a121852389e40 Mon Sep 17 00:00:00 2001
9ae3a8
From: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Date: Tue, 5 Nov 2013 14:09:04 +0100
9ae3a8
Subject: [PATCH 51/87] blockdev: Move parsing of 'media' option to drive_init
9ae3a8
9ae3a8
RH-Author: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Message-id: <1383660558-32096-11-git-send-email-kwolf@redhat.com>
9ae3a8
Patchwork-id: 55389
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH 10/24] blockdev: Move parsing of 'media' option to drive_init
9ae3a8
Bugzilla: 978402
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
RH-Acked-by: Max Reitz <mreitz@redhat.com>
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
9ae3a8
This moves as much as possible of the processing of the 'media' option
9ae3a8
to drive_init so that it can only be accessed using legacy functions,
9ae3a8
but never with anything blockdev-add related.
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Reviewed-by: Benoit Canet <benoit@irqsave.net>
9ae3a8
Reviewed-by: Eric Blake <eblake@redhat.com>
9ae3a8
(cherry picked from commit 33cb7dc8b7a26ccdff2f054056d3f2e487cbb4cd)
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
---
9ae3a8
 blockdev.c | 73 ++++++++++++++++++++++++++++++++++++++++++--------------------
9ae3a8
 1 file changed, 50 insertions(+), 23 deletions(-)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 blockdev.c |   73 +++++++++++++++++++++++++++++++++++++++++-------------------
9ae3a8
 1 files changed, 50 insertions(+), 23 deletions(-)
9ae3a8
9ae3a8
diff --git a/blockdev.c b/blockdev.c
9ae3a8
index 8dd9fd7..e6d3c15 100644
9ae3a8
--- a/blockdev.c
9ae3a8
+++ b/blockdev.c
9ae3a8
@@ -324,16 +324,18 @@ static bool do_check_io_limits(BlockIOLimit *io_limits, Error **errp)
9ae3a8
     return true;
9ae3a8
 }
9ae3a8
 
9ae3a8
+typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
9ae3a8
+
9ae3a8
 /* Takes the ownership of bs_opts */
9ae3a8
 static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
-                                BlockInterfaceType block_default_type)
9ae3a8
+                                BlockInterfaceType block_default_type,
9ae3a8
+                                DriveMediaType media)
9ae3a8
 {
9ae3a8
     const char *buf;
9ae3a8
     const char *file = NULL;
9ae3a8
     const char *serial;
9ae3a8
     const char *mediastr = "";
9ae3a8
     BlockInterfaceType type;
9ae3a8
-    enum { MEDIA_DISK, MEDIA_CDROM } media;
9ae3a8
     int bus_id, unit_id;
9ae3a8
     int cyls, heads, secs, translation;
9ae3a8
     int max_devs;
9ae3a8
@@ -354,7 +356,6 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
     BlockDriver *drv = NULL;
9ae3a8
 
9ae3a8
     translation = BIOS_ATA_TRANSLATION_AUTO;
9ae3a8
-    media = MEDIA_DISK;
9ae3a8
 
9ae3a8
     /* Check common options by copying from bs_opts to opts, all other options
9ae3a8
      * stay in bs_opts for processing by bdrv_open(). */
9ae3a8
@@ -441,19 +442,11 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
 	}
9ae3a8
     }
9ae3a8
 
9ae3a8
-    if ((buf = qemu_opt_get(opts, "media")) != NULL) {
9ae3a8
-        if (!strcmp(buf, "disk")) {
9ae3a8
-	    media = MEDIA_DISK;
9ae3a8
-	} else if (!strcmp(buf, "cdrom")) {
9ae3a8
-            if (cyls || secs || heads) {
9ae3a8
-                error_report("CHS can't be set with media=%s", buf);
9ae3a8
-	        return NULL;
9ae3a8
-            }
9ae3a8
-	    media = MEDIA_CDROM;
9ae3a8
-	} else {
9ae3a8
-	    error_report("'%s' invalid media", buf);
9ae3a8
-	    return NULL;
9ae3a8
-	}
9ae3a8
+    if (media == MEDIA_CDROM) {
9ae3a8
+        if (cyls || secs || heads) {
9ae3a8
+            error_report("CHS can't be set with media=cdrom");
9ae3a8
+            return NULL;
9ae3a8
+        }
9ae3a8
     }
9ae3a8
 
9ae3a8
     if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
9ae3a8
@@ -752,11 +745,27 @@ static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to)
9ae3a8
     }
9ae3a8
 }
9ae3a8
 
9ae3a8
+QemuOptsList qemu_legacy_drive_opts = {
9ae3a8
+    .name = "drive",
9ae3a8
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
9ae3a8
+    .desc = {
9ae3a8
+        {
9ae3a8
+            .name = "media",
9ae3a8
+            .type = QEMU_OPT_STRING,
9ae3a8
+            .help = "media type (disk, cdrom)",
9ae3a8
+        },
9ae3a8
+        { /* end of list */ }
9ae3a8
+    },
9ae3a8
+};
9ae3a8
+
9ae3a8
 DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
9ae3a8
 {
9ae3a8
     const char *value;
9ae3a8
-    DriveInfo *dinfo;
9ae3a8
+    DriveInfo *dinfo = NULL;
9ae3a8
     QDict *bs_opts;
9ae3a8
+    QemuOpts *legacy_opts;
9ae3a8
+    DriveMediaType media = MEDIA_DISK;
9ae3a8
+    Error *local_err = NULL;
9ae3a8
 
9ae3a8
     /* Change legacy command line options into QMP ones */
9ae3a8
     qemu_opt_rename(all_opts, "iops", "throttling.iops-total");
9ae3a8
@@ -798,8 +807,29 @@ DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
9ae3a8
     bs_opts = qdict_new();
9ae3a8
     qemu_opts_to_qdict(all_opts, bs_opts);
9ae3a8
 
9ae3a8
+    legacy_opts = qemu_opts_create_nofail(&qemu_legacy_drive_opts);
9ae3a8
+    qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
9ae3a8
+    if (error_is_set(&local_err)) {
9ae3a8
+        qerror_report_err(local_err);
9ae3a8
+        error_free(local_err);
9ae3a8
+        goto fail;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    /* Media type */
9ae3a8
+    value = qemu_opt_get(legacy_opts, "media");
9ae3a8
+    if (value) {
9ae3a8
+        if (!strcmp(value, "disk")) {
9ae3a8
+            media = MEDIA_DISK;
9ae3a8
+        } else if (!strcmp(value, "cdrom")) {
9ae3a8
+            media = MEDIA_CDROM;
9ae3a8
+        } else {
9ae3a8
+            error_report("'%s' invalid media", value);
9ae3a8
+            goto fail;
9ae3a8
+        }
9ae3a8
+    }
9ae3a8
+
9ae3a8
     /* Actual block device init: Functionality shared with blockdev-add */
9ae3a8
-    dinfo = blockdev_init(bs_opts, block_default_type);
9ae3a8
+    dinfo = blockdev_init(bs_opts, block_default_type, media);
9ae3a8
     if (dinfo == NULL) {
9ae3a8
         goto fail;
9ae3a8
     }
9ae3a8
@@ -809,6 +839,7 @@ DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
9ae3a8
     dinfo->opts = all_opts;
9ae3a8
 
9ae3a8
 fail:
9ae3a8
+    qemu_opts_del(legacy_opts);
9ae3a8
     return dinfo;
9ae3a8
 }
9ae3a8
 
9ae3a8
@@ -1705,7 +1736,7 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
9ae3a8
 
9ae3a8
     qdict_flatten(qdict);
9ae3a8
 
9ae3a8
-    dinfo = blockdev_init(qdict, IF_NONE);
9ae3a8
+    dinfo = blockdev_init(qdict, IF_NONE, MEDIA_DISK);
9ae3a8
     if (!dinfo) {
9ae3a8
         error_setg(errp, "Could not open image");
9ae3a8
         goto fail;
9ae3a8
@@ -1774,10 +1805,6 @@ QemuOptsList qemu_common_drive_opts = {
9ae3a8
             .type = QEMU_OPT_STRING,
9ae3a8
             .help = "chs translation (auto, lba. none)",
9ae3a8
         },{
9ae3a8
-            .name = "media",
9ae3a8
-            .type = QEMU_OPT_STRING,
9ae3a8
-            .help = "media type (disk, cdrom)",
9ae3a8
-        },{
9ae3a8
             .name = "snapshot",
9ae3a8
             .type = QEMU_OPT_BOOL,
9ae3a8
             .help = "enable/disable snapshot mode",
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8