diff mbox

[7/9] scsi-disk: bdrv_attach_dev() for empty CD-ROM

Message ID 1499864249-26305-8-git-send-email-kwolf@redhat.com
State New
Headers show

Commit Message

Kevin Wolf July 12, 2017, 12:57 p.m. UTC
If no drive=... option is passed (for an empty drive), we don't only
lack the BlockBackend normally created by parse_drive(), but we also
need to manually call blk_attach_dev().

This fixes at least a segfault when unplugging such devices, the bug
that they didn't show up in query-block, and probably some more
problems.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 hw/scsi/scsi-disk.c | 5 +++++
 1 file changed, 5 insertions(+)

Comments

Eric Blake July 12, 2017, 3:58 p.m. UTC | #1
On 07/12/2017 07:57 AM, Kevin Wolf wrote:
> If no drive=... option is passed (for an empty drive), we don't only
> lack the BlockBackend normally created by parse_drive(), but we also
> need to manually call blk_attach_dev().
> 
> This fixes at least a segfault when unplugging such devices, the bug
> that they didn't show up in query-block, and probably some more
> problems.
> 
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> ---
>  hw/scsi/scsi-disk.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 

Reviewed-by: Eric Blake <eblake@redhat.com>
diff mbox

Patch

diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c
index a53f058..5f1e5e8 100644
--- a/hw/scsi/scsi-disk.c
+++ b/hw/scsi/scsi-disk.c
@@ -2384,9 +2384,14 @@  static void scsi_hd_realize(SCSIDevice *dev, Error **errp)
 static void scsi_cd_realize(SCSIDevice *dev, Error **errp)
 {
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
+    int ret;
 
     if (!dev->conf.blk) {
+        /* Anonymous BlockBackend for an empty drive. As we put it into
+         * dev->conf, qdev takes care of detaching on unplug. */
         dev->conf.blk = blk_new(0, BLK_PERM_ALL);
+        ret = blk_attach_dev(dev->conf.blk, &dev->qdev);
+        assert(ret == 0);
     }
 
     s->qdev.blocksize = 2048;