Patchwork Re: [PATCH] [STABLE] Fix virtio-blk hot add after remove

login
register
mail settings
Submitter Gerd Hoffmann
Date Oct. 15, 2009, 8 a.m.
Message ID <4AD6D681.3050401@redhat.com>
Download mbox | patch
Permalink /patch/36082/
State New
Headers show

Comments

Gerd Hoffmann - Oct. 15, 2009, 8 a.m.
On 10/14/09 18:59, Anthony Liguori wrote:
> qdev_init_bdrv() expects that each drive added is the next logical unit for
> the given interface type.  However, when dealing with hotplug, there may
> be holes in the units.  drive_init reclaims holes in units but qdev_init_bdrv()
> is not smart enough to do this.

Oh, right.

> Suggestions for a less ugly solution are appreciated.

See attached patch.  Isn't exactly pretty.  But at least it is less 
invasive and IMHO the logic is easier to understand as well.

Warning: *untested*.

cheers,
   Gerd
From 572ff4912ed021c7b4bb2076d8d72e497f0c434b Mon Sep 17 00:00:00 2001
From: Gerd Hoffmann <kraxel@redhat.com>
Date: Thu, 15 Oct 2009 09:55:11 +0200
Subject: [PATCH] fix virtio blk hotplugging.


Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 hw/pci-hotplug.c |    2 ++
 hw/qdev.c        |    8 ++++++++
 sysemu.h         |    1 +
 3 files changed, 11 insertions(+), 0 deletions(-)

Patch

diff --git a/hw/pci-hotplug.c b/hw/pci-hotplug.c
index 8bedea2..658564e 100644
--- a/hw/pci-hotplug.c
+++ b/hw/pci-hotplug.c
@@ -147,7 +147,9 @@  static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
                         drives_table[drive_idx].unit);
         break;
     case IF_VIRTIO:
+        drives_table[drive_idx].qdev_pick_this_one_please = 1;
         dev = pci_create("virtio-blk-pci", devaddr);
+        drives_table[drive_idx].qdev_pick_this_one_please = 0;
         break;
     default:
         dev = NULL;
diff --git a/hw/qdev.c b/hw/qdev.c
index faecc76..9dd896b 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -191,6 +191,14 @@  BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
     int unit = next_block_unit[type]++;
     int index;
 
+    for (index = 0; index < MAX_DRIVES; index++) {
+        if (!drives_table[index].used)
+            continue;
+        if (!drives_table[index].qdev_pick_this_one_please)
+            continue;
+        return drives_table[index].bdrv;
+    }
+
     index = drive_get_index(type, 0, unit);
     if (index == -1) {
         return NULL;
diff --git a/sysemu.h b/sysemu.h
index ce25109..6dc2b4f 100644
--- a/sysemu.h
+++ b/sysemu.h
@@ -173,6 +173,7 @@  typedef struct DriveInfo {
     int bus;
     int unit;
     int used;
+    int qdev_pick_this_one_please; /* band-aid for virtio-blk hotplug */
     int drive_opt_idx;
     BlockInterfaceErrorAction onerror;
     char serial[BLOCK_SERIAL_STRLEN + 1];