Patchwork [2/4] qdev: Introduce qdev_force_unplug.

login
register
mail settings
Submitter Anthony PERARD
Date May 15, 2012, 3:26 p.m.
Message ID <1337095599-28836-3-git-send-email-anthony.perard@citrix.com>
Download mbox | patch
Permalink /patch/159366/
State New
Headers show

Comments

Anthony PERARD - May 15, 2012, 3:26 p.m.
This function will be use to force a device to be ejected without the guest
cooperation.

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
---
 hw/qdev.c |   23 ++++++++++++++++++++---
 hw/qdev.h |    3 +++
 2 files changed, 23 insertions(+), 3 deletions(-)
Stefano Stabellini - May 15, 2012, 6:15 p.m.
On Tue, 15 May 2012, Anthony PERARD wrote:
> This function will be use to force a device to be ejected without the guest
> cooperation.
> 
> Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
> ---
>  hw/qdev.c |   23 ++++++++++++++++++++---
>  hw/qdev.h |    3 +++
>  2 files changed, 23 insertions(+), 3 deletions(-)
> 
> diff --git a/hw/qdev.c b/hw/qdev.c
> index 6a8f6bd..c95d4c2 100644
> --- a/hw/qdev.c
> +++ b/hw/qdev.c
> @@ -184,24 +184,41 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
>      dev->alias_required_for_version = required_for_version;
>  }
>  
> -void qdev_unplug(DeviceState *dev, Error **errp)
> +static void qdev_unplug_common(DeviceState *dev, Error **errp, bool force)
>  {
>      DeviceClass *dc = DEVICE_GET_CLASS(dev);
> +    qdev_event unplug;
>  
>      if (!dev->parent_bus->allow_hotplug) {
>          error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
>          return;
>      }
> -    assert(dc->unplug != NULL);
> +
> +    if (force) {
> +        unplug = dc->force_unplug;
> +    } else {
> +        unplug = dc->unplug;
> +    }
> +    assert(unplug != NULL);

unplug needs to be initialized to NULL above
Anthony PERARD - May 15, 2012, 6:22 p.m.
On Tue, May 15, 2012 at 7:15 PM, Stefano Stabellini
<stefano.stabellini@eu.citrix.com> wrote:
> On Tue, 15 May 2012, Anthony PERARD wrote:
>> This function will be use to force a device to be ejected without the guest
>> cooperation.
>>
>> Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
>> ---
>>  hw/qdev.c |   23 ++++++++++++++++++++---
>>  hw/qdev.h |    3 +++
>>  2 files changed, 23 insertions(+), 3 deletions(-)
>>
>> diff --git a/hw/qdev.c b/hw/qdev.c
>> index 6a8f6bd..c95d4c2 100644
>> --- a/hw/qdev.c
>> +++ b/hw/qdev.c
>> @@ -184,24 +184,41 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
>>      dev->alias_required_for_version = required_for_version;
>>  }
>>
>> -void qdev_unplug(DeviceState *dev, Error **errp)
>> +static void qdev_unplug_common(DeviceState *dev, Error **errp, bool force)
>>  {
>>      DeviceClass *dc = DEVICE_GET_CLASS(dev);
>> +    qdev_event unplug;
>>
>>      if (!dev->parent_bus->allow_hotplug) {
>>          error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
>>          return;
>>      }
>> -    assert(dc->unplug != NULL);
>> +
>> +    if (force) {
>> +        unplug = dc->force_unplug;
>> +    } else {
>> +        unplug = dc->unplug;
>> +    }
>> +    assert(unplug != NULL);
>
> unplug needs to be initialized to NULL above

Why? unplug is not used before to be set.

But I can do that for the next version if there is one.

Patch

diff --git a/hw/qdev.c b/hw/qdev.c
index 6a8f6bd..c95d4c2 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -184,24 +184,41 @@  void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
     dev->alias_required_for_version = required_for_version;
 }
 
-void qdev_unplug(DeviceState *dev, Error **errp)
+static void qdev_unplug_common(DeviceState *dev, Error **errp, bool force)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
+    qdev_event unplug;
 
     if (!dev->parent_bus->allow_hotplug) {
         error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
         return;
     }
-    assert(dc->unplug != NULL);
+
+    if (force) {
+        unplug = dc->force_unplug;
+    } else {
+        unplug = dc->unplug;
+    }
+    assert(unplug != NULL);
 
     qdev_hot_removed = true;
 
-    if (dc->unplug(dev) < 0) {
+    if (unplug(dev) < 0) {
         error_set(errp, QERR_UNDEFINED_ERROR);
         return;
     }
 }
 
+void qdev_force_unplug(DeviceState *dev, Error **errp)
+{
+    qdev_unplug_common(dev, errp, true);
+}
+
+void qdev_unplug(DeviceState *dev, Error **errp)
+{
+    qdev_unplug_common(dev, errp, false);
+}
+
 static int qdev_reset_one(DeviceState *dev, void *opaque)
 {
     device_reset(dev);
diff --git a/hw/qdev.h b/hw/qdev.h
index 4e90119..404c560 100644
--- a/hw/qdev.h
+++ b/hw/qdev.h
@@ -54,6 +54,7 @@  typedef struct DeviceClass {
     /* Private to qdev / bus.  */
     qdev_initfn init;
     qdev_event unplug;
+    qdev_event force_unplug;
     qdev_event exit;
     BusInfo *bus_info;
 } DeviceClass;
@@ -150,6 +151,8 @@  void qdev_init_nofail(DeviceState *dev);
 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
                                  int required_for_version);
 void qdev_unplug(DeviceState *dev, Error **errp);
+/* Unplug a device without the guest cooperation. */
+void qdev_force_unplug(DeviceState *dev, Error **errp);
 void qdev_free(DeviceState *dev);
 int qdev_simple_unplug_cb(DeviceState *dev);
 void qdev_machine_creation_done(void);