qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com
Subject: [Qemu-devel] [PULL 24/28] blockdev: Fix blockdev-add not to create DriveInfo
Date: Mon, 20 Oct 2014 15:35:50 +0200	[thread overview]
Message-ID: <1413812154-11153-25-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1413812154-11153-1-git-send-email-kwolf@redhat.com>

From: Markus Armbruster <armbru@redhat.com>

blockdev_init() always creates a DriveInfo, but only drive_new() fills
it in.  qmp_blockdev_add() leaves it blank.  This results in a drive
with type = IF_IDE, bus = 0, unit = 0.  Screwed up in commit ee13ed1c.

Board initialization code looking for IDE drive (0,0) can pick up one
of these bogus drives.  The QMP command has to execute really early to
be visible.  Not sure how likely that is in practice.

Fix by creating DriveInfo in drive_new().  Block backends created by
blockdev-add don't get one.

Breaks the test for "has been created by qmp_blockdev_add()" in
blockdev_mark_auto_del() and do_drive_del(), because it changes the
value of dinfo && !dinfo->enable_auto_del from true to false.  Simply
test !dinfo instead.

Leaves DriveInfo member enable_auto_del unused.  Drop it.

A few places assume a block backend always has a DriveInfo.  Fix them
up.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/block-backend.c     |  2 +-
 blockdev.c                | 22 +++++++---------------
 hw/block/block.c          | 16 ++++++++++------
 hw/ide/qdev.c             |  2 +-
 hw/scsi/scsi-disk.c       |  2 +-
 include/sysemu/blockdev.h |  1 -
 6 files changed, 20 insertions(+), 25 deletions(-)

diff --git a/block/block-backend.c b/block/block-backend.c
index a3e613b..ae30873 100644
--- a/block/block-backend.c
+++ b/block/block-backend.c
@@ -18,7 +18,7 @@ struct BlockBackend {
     char *name;
     int refcnt;
     BlockDriverState *bs;
-    DriveInfo *legacy_dinfo;
+    DriveInfo *legacy_dinfo;    /* null unless created by drive_new() */
     QTAILQ_ENTRY(BlockBackend) link; /* for blk_backends */
 };
 
diff --git a/blockdev.c b/blockdev.c
index cdefbb0..2a3d908 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -116,16 +116,14 @@ void blockdev_mark_auto_del(BlockBackend *blk)
     DriveInfo *dinfo = blk_legacy_dinfo(blk);
     BlockDriverState *bs = blk_bs(blk);
 
-    if (dinfo && !dinfo->enable_auto_del) {
+    if (!dinfo) {
         return;
     }
 
     if (bs->job) {
         block_job_cancel(bs->job);
     }
-    if (dinfo) {
-        dinfo->auto_del = 1;
-    }
+    dinfo->auto_del = 1;
 }
 
 void blockdev_auto_del(BlockBackend *blk)
@@ -346,7 +344,6 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
     int on_read_error, on_write_error;
     BlockBackend *blk;
     BlockDriverState *bs;
-    DriveInfo *dinfo;
     ThrottleConfig cfg;
     int snapshot = 0;
     bool copy_on_read;
@@ -518,9 +515,6 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
         bdrv_set_io_limits(bs, &cfg);
     }
 
-    dinfo = g_malloc0(sizeof(*dinfo));
-    blk_set_legacy_dinfo(blk, dinfo);
-
     if (!file || !*file) {
         if (has_driver_specific_opts) {
             file = NULL;
@@ -990,9 +984,8 @@ DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
         assert(!local_err);
     }
 
-    /* Set legacy DriveInfo fields */
-    dinfo = blk_legacy_dinfo(blk);
-    dinfo->enable_auto_del = true;
+    /* Create legacy DriveInfo */
+    dinfo = g_malloc0(sizeof(*dinfo));
     dinfo->opts = all_opts;
 
     dinfo->cyls = cyls;
@@ -1004,9 +997,10 @@ DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
     dinfo->bus = bus_id;
     dinfo->unit = unit_id;
     dinfo->devaddr = devaddr;
-
     dinfo->serial = g_strdup(serial);
 
+    blk_set_legacy_dinfo(blk, dinfo);
+
     switch(type) {
     case IF_IDE:
     case IF_SCSI:
@@ -1810,7 +1804,6 @@ int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
     const char *id = qdict_get_str(qdict, "id");
     BlockBackend *blk;
     BlockDriverState *bs;
-    DriveInfo *dinfo;
     AioContext *aio_context;
     Error *local_err = NULL;
 
@@ -1821,8 +1814,7 @@ int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
     }
     bs = blk_bs(blk);
 
-    dinfo = blk_legacy_dinfo(blk);
-    if (dinfo && !dinfo->enable_auto_del) {
+    if (!blk_legacy_dinfo(blk)) {
         error_report("Deleting device added with blockdev-add"
                      " is not supported");
         return -1;
diff --git a/hw/block/block.c b/hw/block/block.c
index 0666dd3..a625773 100644
--- a/hw/block/block.c
+++ b/hw/block/block.c
@@ -19,7 +19,9 @@ void blkconf_serial(BlockConf *conf, char **serial)
     if (!*serial) {
         /* try to fall back to value set with legacy -drive serial=... */
         dinfo = blk_legacy_dinfo(conf->blk);
-        *serial = g_strdup(dinfo->serial);
+        if (dinfo) {
+            *serial = g_strdup(dinfo->serial);
+        }
     }
 }
 
@@ -32,11 +34,13 @@ void blkconf_geometry(BlockConf *conf, int *ptrans,
     if (!conf->cyls && !conf->heads && !conf->secs) {
         /* try to fall back to value set with legacy -drive cyls=... */
         dinfo = blk_legacy_dinfo(conf->blk);
-        conf->cyls  = dinfo->cyls;
-        conf->heads = dinfo->heads;
-        conf->secs  = dinfo->secs;
-        if (ptrans) {
-            *ptrans = dinfo->trans;
+        if (dinfo) {
+            conf->cyls  = dinfo->cyls;
+            conf->heads = dinfo->heads;
+            conf->secs  = dinfo->secs;
+            if (ptrans) {
+                *ptrans = dinfo->trans;
+            }
         }
     }
     if (!conf->cyls && !conf->heads && !conf->secs) {
diff --git a/hw/ide/qdev.c b/hw/ide/qdev.c
index 600e67c..b4f096e 100644
--- a/hw/ide/qdev.c
+++ b/hw/ide/qdev.c
@@ -252,7 +252,7 @@ static int ide_drive_initfn(IDEDevice *dev)
 {
     DriveInfo *dinfo = blk_legacy_dinfo(dev->conf.blk);
 
-    return ide_dev_initfn(dev, dinfo->media_cd ? IDE_CD : IDE_HD);
+    return ide_dev_initfn(dev, dinfo && dinfo->media_cd ? IDE_CD : IDE_HD);
 }
 
 #define DEFINE_IDE_DEV_PROPERTIES()                     \
diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c
index 300ba01..010eefd 100644
--- a/hw/scsi/scsi-disk.c
+++ b/hw/scsi/scsi-disk.c
@@ -2310,7 +2310,7 @@ static void scsi_disk_realize(SCSIDevice *dev, Error **errp)
     }
 
     dinfo = blk_legacy_dinfo(dev->conf.blk);
-    if (dinfo->media_cd) {
+    if (dinfo && dinfo->media_cd) {
         scsi_cd_realize(dev, errp);
     } else {
         scsi_hd_realize(dev, errp);
diff --git a/include/sysemu/blockdev.h b/include/sysemu/blockdev.h
index 0c70c29..09d1e30 100644
--- a/include/sysemu/blockdev.h
+++ b/include/sysemu/blockdev.h
@@ -35,7 +35,6 @@ struct DriveInfo {
     int bus;
     int unit;
     int auto_del;               /* see blockdev_mark_auto_del() */
-    bool enable_auto_del;       /* Only for legacy drive_new() */
     bool is_default;            /* Added by default_drive() ?  */
     int media_cd;
     int cyls, heads, secs, trans;
-- 
1.8.3.1

  parent reply	other threads:[~2014-10-20 13:36 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-20 13:35 [Qemu-devel] [PULL 00/28] Block patches Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 01/28] qcow2: fix leak of Qcow2DiscardRegion in update_refcount_discard Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 02/28] block/raw-posix: Fix disk corruption in try_fiemap Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 03/28] block/raw-posix: use seek_hole ahead of fiemap Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 04/28] nbd: Fix filename generation Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 05/28] block: Split bdrv_new_root() off bdrv_new() Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 06/28] block: New BlockBackend Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 07/28] block: Connect BlockBackend to BlockDriverState Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 08/28] block: Connect BlockBackend and DriveInfo Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 09/28] block: Code motion to get rid of stubs/blockdev.c Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 10/28] block: Make BlockBackend own its BlockDriverState Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 11/28] blockdev: Eliminate drive_del() Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 12/28] block: Eliminate bdrv_iterate(), use bdrv_next() Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 13/28] block: Eliminate BlockDriverState member device_name[] Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 14/28] block: Merge BlockBackend and BlockDriverState name spaces Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 15/28] block: Eliminate DriveInfo member bdrv, use blk_by_legacy_dinfo() Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 16/28] block: Rename BlockDriverAIOCB* to BlockAIOCB* Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 17/28] block: Rename BlockDriverCompletionFunc to BlockCompletionFunc Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 18/28] virtio-blk: Drop redundant VirtIOBlock member conf Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 19/28] virtio-blk: Rename VirtIOBlkConf variables to conf Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 20/28] hw: Convert from BlockDriverState to BlockBackend, mostly Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 21/28] ide: Complete conversion from BlockDriverState to BlockBackend Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 22/28] pc87312: Drop unused members of PC87312State Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 23/28] blockdev: Drop superfluous DriveInfo member id Kevin Wolf
2014-10-20 13:35 ` Kevin Wolf [this message]
2014-10-20 13:35 ` [Qemu-devel] [PULL 25/28] block/qapi: Convert qmp_query_block() to BlockBackend Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 26/28] blockdev: Convert qmp_eject(), qmp_change_blockdev() " Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 27/28] block: Lift device model API into BlockBackend Kevin Wolf
2014-10-20 13:35 ` [Qemu-devel] [PULL 28/28] block: Make device model's references to BlockBackend strong Kevin Wolf
2014-10-22 17:30 ` [Qemu-devel] [PULL 00/28] Block patches Peter Maydell

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=1413812154-11153-25-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /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).