9ae3a8
From 9ab9b4d966cf80520ad0864bc61b1d3a255ce39c Mon Sep 17 00:00:00 2001
9ae3a8
From: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Date: Tue, 5 Nov 2013 14:09:08 +0100
9ae3a8
Subject: [PATCH 55/87] blockdev: Move bus/unit/index processing to drive_init
9ae3a8
9ae3a8
RH-Author: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Message-id: <1383660558-32096-15-git-send-email-kwolf@redhat.com>
9ae3a8
Patchwork-id: 55393
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH 14/24] blockdev: Move bus/unit/index processing 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 requires moving the automatic ID generation at the same time, so
9ae3a8
let's do that as well.
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Reviewed-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Reviewed-by: Eric Blake <eblake@redhat.com>
9ae3a8
(cherry picked from commit 87a899c5090c7864fc7dcff3ea0ac34153ea621b)
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
---
9ae3a8
 blockdev.c | 157 ++++++++++++++++++++++++++++---------------------------------
9ae3a8
 1 file changed, 73 insertions(+), 84 deletions(-)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 blockdev.c |  157 ++++++++++++++++++++++++++++--------------------------------
9ae3a8
 1 files changed, 73 insertions(+), 84 deletions(-)
9ae3a8
9ae3a8
diff --git a/blockdev.c b/blockdev.c
9ae3a8
index 9f9cbba..b6dff50 100644
9ae3a8
--- a/blockdev.c
9ae3a8
+++ b/blockdev.c
9ae3a8
@@ -334,10 +334,6 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
     const char *buf;
9ae3a8
     const char *file = NULL;
9ae3a8
     const char *serial;
9ae3a8
-    const char *mediastr = "";
9ae3a8
-    int bus_id, unit_id;
9ae3a8
-    int max_devs;
9ae3a8
-    int index;
9ae3a8
     int ro = 0;
9ae3a8
     int bdrv_flags = 0;
9ae3a8
     int on_read_error, on_write_error;
9ae3a8
@@ -377,10 +373,6 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
     has_driver_specific_opts = !!qdict_size(bs_opts);
9ae3a8
 
9ae3a8
     /* extract parameters */
9ae3a8
-    bus_id  = qemu_opt_get_number(opts, "bus", 0);
9ae3a8
-    unit_id = qemu_opt_get_number(opts, "unit", -1);
9ae3a8
-    index   = qemu_opt_get_number(opts, "index", -1);
9ae3a8
-
9ae3a8
     snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
9ae3a8
     ro = qemu_opt_get_bool(opts, "read-only", 0);
9ae3a8
     copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
9ae3a8
@@ -388,8 +380,6 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
     file = qemu_opt_get(opts, "file");
9ae3a8
     serial = qemu_opt_get(opts, "serial");
9ae3a8
 
9ae3a8
-    max_devs = if_max_devs[type];
9ae3a8
-
9ae3a8
     if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
9ae3a8
         if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
9ae3a8
             error_report("invalid discard option");
9ae3a8
@@ -489,66 +479,6 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
         }
9ae3a8
     }
9ae3a8
 
9ae3a8
-    /* compute bus and unit according index */
9ae3a8
-
9ae3a8
-    if (index != -1) {
9ae3a8
-        if (bus_id != 0 || unit_id != -1) {
9ae3a8
-            error_report("index cannot be used with bus and unit");
9ae3a8
-            return NULL;
9ae3a8
-        }
9ae3a8
-        bus_id = drive_index_to_bus_id(type, index);
9ae3a8
-        unit_id = drive_index_to_unit_id(type, index);
9ae3a8
-    }
9ae3a8
-
9ae3a8
-    /* if user doesn't specify a unit_id,
9ae3a8
-     * try to find the first free
9ae3a8
-     */
9ae3a8
-
9ae3a8
-    if (unit_id == -1) {
9ae3a8
-       unit_id = 0;
9ae3a8
-       while (drive_get(type, bus_id, unit_id) != NULL) {
9ae3a8
-           unit_id++;
9ae3a8
-           if (max_devs && unit_id >= max_devs) {
9ae3a8
-               unit_id -= max_devs;
9ae3a8
-               bus_id++;
9ae3a8
-           }
9ae3a8
-       }
9ae3a8
-    }
9ae3a8
-
9ae3a8
-    /* check unit id */
9ae3a8
-
9ae3a8
-    if (max_devs && unit_id >= max_devs) {
9ae3a8
-        error_report("unit %d too big (max is %d)",
9ae3a8
-                     unit_id, max_devs - 1);
9ae3a8
-        return NULL;
9ae3a8
-    }
9ae3a8
-
9ae3a8
-    /*
9ae3a8
-     * catch multiple definitions
9ae3a8
-     */
9ae3a8
-
9ae3a8
-    if (drive_get(type, bus_id, unit_id) != NULL) {
9ae3a8
-        error_report("drive with bus=%d, unit=%d (index=%d) exists",
9ae3a8
-                     bus_id, unit_id, index);
9ae3a8
-        return NULL;
9ae3a8
-    }
9ae3a8
-
9ae3a8
-    /* no id supplied -> create one */
9ae3a8
-    if (qemu_opts_id(opts) == NULL) {
9ae3a8
-        char *new_id;
9ae3a8
-        if (type == IF_IDE || type == IF_SCSI) {
9ae3a8
-            mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
9ae3a8
-        }
9ae3a8
-        if (max_devs) {
9ae3a8
-            new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
9ae3a8
-                                     mediastr, unit_id);
9ae3a8
-        } else {
9ae3a8
-            new_id = g_strdup_printf("%s%s%i", if_name[type],
9ae3a8
-                                     mediastr, unit_id);
9ae3a8
-        }
9ae3a8
-        qemu_opts_set_id(opts, new_id);
9ae3a8
-    }
9ae3a8
-
9ae3a8
     /* init */
9ae3a8
     dinfo = g_malloc0(sizeof(*dinfo));
9ae3a8
     dinfo->id = g_strdup(qemu_opts_id(opts));
9ae3a8
@@ -557,8 +487,6 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
     dinfo->bdrv->read_only = ro;
9ae3a8
     dinfo->devaddr = devaddr;
9ae3a8
     dinfo->type = type;
9ae3a8
-    dinfo->bus = bus_id;
9ae3a8
-    dinfo->unit = unit_id;
9ae3a8
     dinfo->refcount = 1;
9ae3a8
     if (serial != NULL) {
9ae3a8
         dinfo->serial = g_strdup(serial);
9ae3a8
@@ -681,6 +609,18 @@ QemuOptsList qemu_legacy_drive_opts = {
9ae3a8
     .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
9ae3a8
     .desc = {
9ae3a8
         {
9ae3a8
+            .name = "bus",
9ae3a8
+            .type = QEMU_OPT_NUMBER,
9ae3a8
+            .help = "bus number",
9ae3a8
+        },{
9ae3a8
+            .name = "unit",
9ae3a8
+            .type = QEMU_OPT_NUMBER,
9ae3a8
+            .help = "unit number (i.e. lun for scsi)",
9ae3a8
+        },{
9ae3a8
+            .name = "index",
9ae3a8
+            .type = QEMU_OPT_NUMBER,
9ae3a8
+            .help = "index number",
9ae3a8
+        },{
9ae3a8
             .name = "media",
9ae3a8
             .type = QEMU_OPT_STRING,
9ae3a8
             .help = "media type (disk, cdrom)",
9ae3a8
@@ -722,6 +662,7 @@ DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
9ae3a8
     DriveMediaType media = MEDIA_DISK;
9ae3a8
     BlockInterfaceType type;
9ae3a8
     int cyls, heads, secs, translation;
9ae3a8
+    int max_devs, bus_id, unit_id, index;
9ae3a8
     Error *local_err = NULL;
9ae3a8
 
9ae3a8
     /* Change legacy command line options into QMP ones */
9ae3a8
@@ -854,6 +795,63 @@ DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
9ae3a8
         }
9ae3a8
     }
9ae3a8
 
9ae3a8
+    /* Device address specified by bus/unit or index.
9ae3a8
+     * If none was specified, try to find the first free one. */
9ae3a8
+    bus_id  = qemu_opt_get_number(legacy_opts, "bus", 0);
9ae3a8
+    unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
9ae3a8
+    index   = qemu_opt_get_number(legacy_opts, "index", -1);
9ae3a8
+
9ae3a8
+    max_devs = if_max_devs[type];
9ae3a8
+
9ae3a8
+    if (index != -1) {
9ae3a8
+        if (bus_id != 0 || unit_id != -1) {
9ae3a8
+            error_report("index cannot be used with bus and unit");
9ae3a8
+            goto fail;
9ae3a8
+        }
9ae3a8
+        bus_id = drive_index_to_bus_id(type, index);
9ae3a8
+        unit_id = drive_index_to_unit_id(type, index);
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    if (unit_id == -1) {
9ae3a8
+       unit_id = 0;
9ae3a8
+       while (drive_get(type, bus_id, unit_id) != NULL) {
9ae3a8
+           unit_id++;
9ae3a8
+           if (max_devs && unit_id >= max_devs) {
9ae3a8
+               unit_id -= max_devs;
9ae3a8
+               bus_id++;
9ae3a8
+           }
9ae3a8
+       }
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    if (max_devs && unit_id >= max_devs) {
9ae3a8
+        error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
9ae3a8
+        goto fail;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    if (drive_get(type, bus_id, unit_id) != NULL) {
9ae3a8
+        error_report("drive with bus=%d, unit=%d (index=%d) exists",
9ae3a8
+                     bus_id, unit_id, index);
9ae3a8
+        goto fail;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    /* no id supplied -> create one */
9ae3a8
+    if (qemu_opts_id(all_opts) == NULL) {
9ae3a8
+        char *new_id;
9ae3a8
+        const char *mediastr = "";
9ae3a8
+        if (type == IF_IDE || type == IF_SCSI) {
9ae3a8
+            mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
9ae3a8
+        }
9ae3a8
+        if (max_devs) {
9ae3a8
+            new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
9ae3a8
+                                     mediastr, unit_id);
9ae3a8
+        } else {
9ae3a8
+            new_id = g_strdup_printf("%s%s%i", if_name[type],
9ae3a8
+                                     mediastr, unit_id);
9ae3a8
+        }
9ae3a8
+        qdict_put(bs_opts, "id", qstring_from_str(new_id));
9ae3a8
+        g_free(new_id);
9ae3a8
+    }
9ae3a8
+
9ae3a8
     /* Actual block device init: Functionality shared with blockdev-add */
9ae3a8
     dinfo = blockdev_init(bs_opts, type, media);
9ae3a8
     if (dinfo == NULL) {
9ae3a8
@@ -869,6 +867,9 @@ DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
9ae3a8
     dinfo->secs = secs;
9ae3a8
     dinfo->trans = translation;
9ae3a8
 
9ae3a8
+    dinfo->bus = bus_id;
9ae3a8
+    dinfo->unit = unit_id;
9ae3a8
+
9ae3a8
 fail:
9ae3a8
     qemu_opts_del(legacy_opts);
9ae3a8
     return dinfo;
9ae3a8
@@ -1804,18 +1805,6 @@ QemuOptsList qemu_common_drive_opts = {
9ae3a8
     .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
9ae3a8
     .desc = {
9ae3a8
         {
9ae3a8
-            .name = "bus",
9ae3a8
-            .type = QEMU_OPT_NUMBER,
9ae3a8
-            .help = "bus number",
9ae3a8
-        },{
9ae3a8
-            .name = "unit",
9ae3a8
-            .type = QEMU_OPT_NUMBER,
9ae3a8
-            .help = "unit number (i.e. lun for scsi)",
9ae3a8
-        },{
9ae3a8
-            .name = "index",
9ae3a8
-            .type = QEMU_OPT_NUMBER,
9ae3a8
-            .help = "index number",
9ae3a8
-        },{
9ae3a8
             .name = "snapshot",
9ae3a8
             .type = QEMU_OPT_BOOL,
9ae3a8
             .help = "enable/disable snapshot mode",
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8