diff mbox

[23/55] block: Show whether the guest ejected the medium in info block

Message ID 1311179069-27882-24-git-send-email-armbru@redhat.com
State New
Headers show

Commit Message

Markus Armbruster July 20, 2011, 4:23 p.m. UTC
Need to ask the device, so this requires new BlockDevOps member
is_medium_ejected().

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 block.c         |   18 ++++++++++++++++--
 block.h         |    6 ++++++
 hw/ide/core.c   |    6 ++++++
 hw/scsi-disk.c  |   10 ++++++++++
 qmp-commands.hx |    1 +
 5 files changed, 39 insertions(+), 2 deletions(-)

Comments

Markus Armbruster July 21, 2011, 3:08 p.m. UTC | #1
Luiz Capitulino <lcapitulino@redhat.com> writes:

> On Wed, 20 Jul 2011 18:23:57 +0200
> Markus Armbruster <armbru@redhat.com> wrote:
>
>> Need to ask the device, so this requires new BlockDevOps member
>> is_medium_ejected().
>> 
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> ---
>>  block.c         |   18 ++++++++++++++++--
>>  block.h         |    6 ++++++
>>  hw/ide/core.c   |    6 ++++++
>>  hw/scsi-disk.c  |   10 ++++++++++
>>  qmp-commands.hx |    1 +
>>  5 files changed, 39 insertions(+), 2 deletions(-)
>> 
>> diff --git a/block.c b/block.c
>> index ba24bf1..6759066 100644
>> --- a/block.c
>> +++ b/block.c
>> @@ -761,6 +761,14 @@ static void bdrv_dev_change_media_cb(BlockDriverState *bs)
>>      }
>>  }
>>  
>> +bool bdrv_dev_is_medium_ejected(BlockDriverState *bs)
>> +{
>> +    if (bs->dev_ops && bs->dev_ops->is_medium_ejected) {
>> +        return bs->dev_ops->is_medium_ejected(bs->dev_opaque);
>> +    }
>> +    return false;
>> +}
>> +
>>  static void bdrv_dev_resize_cb(BlockDriverState *bs)
>>  {
>>      if (bs->dev_ops && bs->dev_ops->resize_cb) {
>> @@ -1712,7 +1720,9 @@ static void bdrv_print_dict(QObject *obj, void *opaque)
>>      if (qdict_get_bool(bs_dict, "removable")) {
>>          monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
>>      }
>> -
>> +    if (qdict_get_try_bool(bs_dict, "ejected", false)) {
>> +        monitor_printf(mon, " ejected");
>> +    }
>>      if (qdict_haskey(bs_dict, "inserted")) {
>>          QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
>>  
>> @@ -1747,15 +1757,19 @@ void bdrv_info(Monitor *mon, QObject **ret_data)
>>  
>>      QTAILQ_FOREACH(bs, &bdrv_states, list) {
>>          QObject *bs_obj;
>> +        QDict *bs_dict;
>>  
>>          bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
>>                                      "'removable': %i, 'locked': %i }",
>>                                      bs->device_name, bs->removable,
>>                                      bs->locked);
>> +        bs_dict = qobject_to_qdict(bs_obj);
>>  
>> +        if (bdrv_dev_is_medium_ejected(bs)) {
>> +            qdict_put(bs_dict, "ejected", qbool_from_int(1));
>> +        }
>
> I'd expect 'ejected' to be always present. It's a bool afterall, so it's
> either true or false.
>
> But I see that "ejected=false" might not be ideal. What about calling it
> "inserted" then?

"inserted=true" for disks looks odd to me, but whatever floats your boat
:)

> Also misses QMP documentation.

Last patch hunk.

>>          if (bs->drv) {
>>              QObject *obj;
>> -            QDict *bs_dict = qobject_to_qdict(bs_obj);
>>  
>>              obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
>>                                       "'encrypted': %i }",
[...]
>> diff --git a/qmp-commands.hx b/qmp-commands.hx
>> index 92c5c3a..cc5d30f 100644
>> --- a/qmp-commands.hx
>> +++ b/qmp-commands.hx
>> @@ -1070,6 +1070,7 @@ Each json-object contain the following:
>>           - Possible values: "unknown"
>>  - "removable": true if the device is removable, false otherwise (json-bool)
>>  - "locked": true if the device is locked, false otherwise (json-bool)
>> +- "ejected": present and true if the device ejected its media (json-bool)
>>  - "inserted": only present if the device is inserted, it is a json-object
>>     containing the following:
>>           - "file": device file name (json-string)
Luiz Capitulino July 21, 2011, 4:38 p.m. UTC | #2
On Thu, 21 Jul 2011 17:08:56 +0200
Markus Armbruster <armbru@redhat.com> wrote:

> Luiz Capitulino <lcapitulino@redhat.com> writes:
> 
> > On Wed, 20 Jul 2011 18:23:57 +0200
> > Markus Armbruster <armbru@redhat.com> wrote:
> >
> >> Need to ask the device, so this requires new BlockDevOps member
> >> is_medium_ejected().
> >> 
> >> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> >> ---
> >>  block.c         |   18 ++++++++++++++++--
> >>  block.h         |    6 ++++++
> >>  hw/ide/core.c   |    6 ++++++
> >>  hw/scsi-disk.c  |   10 ++++++++++
> >>  qmp-commands.hx |    1 +
> >>  5 files changed, 39 insertions(+), 2 deletions(-)
> >> 
> >> diff --git a/block.c b/block.c
> >> index ba24bf1..6759066 100644
> >> --- a/block.c
> >> +++ b/block.c
> >> @@ -761,6 +761,14 @@ static void bdrv_dev_change_media_cb(BlockDriverState *bs)
> >>      }
> >>  }
> >>  
> >> +bool bdrv_dev_is_medium_ejected(BlockDriverState *bs)
> >> +{
> >> +    if (bs->dev_ops && bs->dev_ops->is_medium_ejected) {
> >> +        return bs->dev_ops->is_medium_ejected(bs->dev_opaque);
> >> +    }
> >> +    return false;
> >> +}
> >> +
> >>  static void bdrv_dev_resize_cb(BlockDriverState *bs)
> >>  {
> >>      if (bs->dev_ops && bs->dev_ops->resize_cb) {
> >> @@ -1712,7 +1720,9 @@ static void bdrv_print_dict(QObject *obj, void *opaque)
> >>      if (qdict_get_bool(bs_dict, "removable")) {
> >>          monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
> >>      }
> >> -
> >> +    if (qdict_get_try_bool(bs_dict, "ejected", false)) {
> >> +        monitor_printf(mon, " ejected");
> >> +    }
> >>      if (qdict_haskey(bs_dict, "inserted")) {
> >>          QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
> >>  
> >> @@ -1747,15 +1757,19 @@ void bdrv_info(Monitor *mon, QObject **ret_data)
> >>  
> >>      QTAILQ_FOREACH(bs, &bdrv_states, list) {
> >>          QObject *bs_obj;
> >> +        QDict *bs_dict;
> >>  
> >>          bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
> >>                                      "'removable': %i, 'locked': %i }",
> >>                                      bs->device_name, bs->removable,
> >>                                      bs->locked);
> >> +        bs_dict = qobject_to_qdict(bs_obj);
> >>  
> >> +        if (bdrv_dev_is_medium_ejected(bs)) {
> >> +            qdict_put(bs_dict, "ejected", qbool_from_int(1));
> >> +        }
> >
> > I'd expect 'ejected' to be always present. It's a bool afterall, so it's
> > either true or false.
> >
> > But I see that "ejected=false" might not be ideal. What about calling it
> > "inserted" then?
> 
> "inserted=true" for disks looks odd to me, but whatever floats your boat
> :)

I meant always present for removable media. Maybe "media-inserted" is even
better.

> 
> > Also misses QMP documentation.
> 
> Last patch hunk.

Ah, thanks, missed it.

> 
> >>          if (bs->drv) {
> >>              QObject *obj;
> >> -            QDict *bs_dict = qobject_to_qdict(bs_obj);
> >>  
> >>              obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
> >>                                       "'encrypted': %i }",
> [...]
> >> diff --git a/qmp-commands.hx b/qmp-commands.hx
> >> index 92c5c3a..cc5d30f 100644
> >> --- a/qmp-commands.hx
> >> +++ b/qmp-commands.hx
> >> @@ -1070,6 +1070,7 @@ Each json-object contain the following:
> >>           - Possible values: "unknown"
> >>  - "removable": true if the device is removable, false otherwise (json-bool)
> >>  - "locked": true if the device is locked, false otherwise (json-bool)
> >> +- "ejected": present and true if the device ejected its media (json-bool)
> >>  - "inserted": only present if the device is inserted, it is a json-object
> >>     containing the following:
> >>           - "file": device file name (json-string)
>
Luiz Capitulino July 21, 2011, 5:30 p.m. UTC | #3
On Thu, 21 Jul 2011 13:38:46 -0300
Luiz Capitulino <lcapitulino@redhat.com> wrote:

> On Thu, 21 Jul 2011 17:08:56 +0200
> Markus Armbruster <armbru@redhat.com> wrote:
> 
> > Luiz Capitulino <lcapitulino@redhat.com> writes:
> > 
> > > On Wed, 20 Jul 2011 18:23:57 +0200
> > > Markus Armbruster <armbru@redhat.com> wrote:
> > >
> > >> Need to ask the device, so this requires new BlockDevOps member
> > >> is_medium_ejected().
> > >> 
> > >> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> > >> ---
> > >>  block.c         |   18 ++++++++++++++++--
> > >>  block.h         |    6 ++++++
> > >>  hw/ide/core.c   |    6 ++++++
> > >>  hw/scsi-disk.c  |   10 ++++++++++
> > >>  qmp-commands.hx |    1 +
> > >>  5 files changed, 39 insertions(+), 2 deletions(-)
> > >> 
> > >> diff --git a/block.c b/block.c
> > >> index ba24bf1..6759066 100644
> > >> --- a/block.c
> > >> +++ b/block.c
> > >> @@ -761,6 +761,14 @@ static void bdrv_dev_change_media_cb(BlockDriverState *bs)
> > >>      }
> > >>  }
> > >>  
> > >> +bool bdrv_dev_is_medium_ejected(BlockDriverState *bs)
> > >> +{
> > >> +    if (bs->dev_ops && bs->dev_ops->is_medium_ejected) {
> > >> +        return bs->dev_ops->is_medium_ejected(bs->dev_opaque);
> > >> +    }
> > >> +    return false;
> > >> +}
> > >> +
> > >>  static void bdrv_dev_resize_cb(BlockDriverState *bs)
> > >>  {
> > >>      if (bs->dev_ops && bs->dev_ops->resize_cb) {
> > >> @@ -1712,7 +1720,9 @@ static void bdrv_print_dict(QObject *obj, void *opaque)
> > >>      if (qdict_get_bool(bs_dict, "removable")) {
> > >>          monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
> > >>      }
> > >> -
> > >> +    if (qdict_get_try_bool(bs_dict, "ejected", false)) {
> > >> +        monitor_printf(mon, " ejected");
> > >> +    }
> > >>      if (qdict_haskey(bs_dict, "inserted")) {
> > >>          QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
> > >>  
> > >> @@ -1747,15 +1757,19 @@ void bdrv_info(Monitor *mon, QObject **ret_data)
> > >>  
> > >>      QTAILQ_FOREACH(bs, &bdrv_states, list) {
> > >>          QObject *bs_obj;
> > >> +        QDict *bs_dict;
> > >>  
> > >>          bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
> > >>                                      "'removable': %i, 'locked': %i }",
> > >>                                      bs->device_name, bs->removable,
> > >>                                      bs->locked);
> > >> +        bs_dict = qobject_to_qdict(bs_obj);
> > >>  
> > >> +        if (bdrv_dev_is_medium_ejected(bs)) {
> > >> +            qdict_put(bs_dict, "ejected", qbool_from_int(1));
> > >> +        }
> > >
> > > I'd expect 'ejected' to be always present. It's a bool afterall, so it's
> > > either true or false.
> > >
> > > But I see that "ejected=false" might not be ideal. What about calling it
> > > "inserted" then?
> > 
> > "inserted=true" for disks looks odd to me, but whatever floats your boat
> > :)
> 
> I meant always present for removable media. Maybe "media-inserted" is even
> better.

Given we already have a 'inserted' key, we could call it "media-ejected" which
is always present for devices that support it.
diff mbox

Patch

diff --git a/block.c b/block.c
index ba24bf1..6759066 100644
--- a/block.c
+++ b/block.c
@@ -761,6 +761,14 @@  static void bdrv_dev_change_media_cb(BlockDriverState *bs)
     }
 }
 
+bool bdrv_dev_is_medium_ejected(BlockDriverState *bs)
+{
+    if (bs->dev_ops && bs->dev_ops->is_medium_ejected) {
+        return bs->dev_ops->is_medium_ejected(bs->dev_opaque);
+    }
+    return false;
+}
+
 static void bdrv_dev_resize_cb(BlockDriverState *bs)
 {
     if (bs->dev_ops && bs->dev_ops->resize_cb) {
@@ -1712,7 +1720,9 @@  static void bdrv_print_dict(QObject *obj, void *opaque)
     if (qdict_get_bool(bs_dict, "removable")) {
         monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
     }
-
+    if (qdict_get_try_bool(bs_dict, "ejected", false)) {
+        monitor_printf(mon, " ejected");
+    }
     if (qdict_haskey(bs_dict, "inserted")) {
         QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
 
@@ -1747,15 +1757,19 @@  void bdrv_info(Monitor *mon, QObject **ret_data)
 
     QTAILQ_FOREACH(bs, &bdrv_states, list) {
         QObject *bs_obj;
+        QDict *bs_dict;
 
         bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
                                     "'removable': %i, 'locked': %i }",
                                     bs->device_name, bs->removable,
                                     bs->locked);
+        bs_dict = qobject_to_qdict(bs_obj);
 
+        if (bdrv_dev_is_medium_ejected(bs)) {
+            qdict_put(bs_dict, "ejected", qbool_from_int(1));
+        }
         if (bs->drv) {
             QObject *obj;
-            QDict *bs_dict = qobject_to_qdict(bs_obj);
 
             obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
                                      "'encrypted': %i }",
diff --git a/block.h b/block.h
index 89f75e3..65a0115 100644
--- a/block.h
+++ b/block.h
@@ -36,6 +36,11 @@  typedef struct BlockDevOps {
      */
     void (*change_media_cb)(void *opaque);
     /*
+     * Is medium ejected from the device?
+     * Device models calling bdrv_eject() should implement this.
+     */
+    bool (*is_medium_ejected)(void *opaque);
+    /*
      * Runs when the size changed (e.g. monitor command block_resize)
      */
     void (*resize_cb)(void *opaque);
@@ -92,6 +97,7 @@  void bdrv_detach_dev(BlockDriverState *bs, void *dev);
 void *bdrv_get_attached_dev(BlockDriverState *bs);
 void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
                       void *opaque);
+bool bdrv_dev_is_medium_ejected(BlockDriverState *bs);
 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
               uint8_t *buf, int nb_sectors);
 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
diff --git a/hw/ide/core.c b/hw/ide/core.c
index c8cfd67..b07a4d7 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -1769,8 +1769,14 @@  void ide_bus_reset(IDEBus *bus)
     bus->dma->ops->reset(bus->dma);
 }
 
+static bool ide_cd_medium_ejected(void *opaque)
+{
+    return ((IDEState *)opaque)->tray_open;
+}
+
 static const BlockDevOps ide_cd_block_ops = {
     .change_media_cb = ide_cd_change_cb,
+    .is_medium_ejected = ide_cd_medium_ejected,
 };
 
 int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind,
diff --git a/hw/scsi-disk.c b/hw/scsi-disk.c
index 207a868..ff52d87 100644
--- a/hw/scsi-disk.c
+++ b/hw/scsi-disk.c
@@ -1261,6 +1261,15 @@  static void scsi_destroy(SCSIDevice *dev)
     blockdev_mark_auto_del(s->qdev.conf.bs);
 }
 
+static bool scsi_cd_is_medium_ejected(void *opaque)
+{
+    return ((SCSIDiskState *)opaque)->tray_open;
+}
+
+static const BlockDevOps scsi_cd_block_ops = {
+    .is_medium_ejected = scsi_cd_is_medium_ejected,
+};
+
 static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)
 {
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
@@ -1294,6 +1303,7 @@  static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)
     }
 
     if (kind == SCSI_CD) {
+        bdrv_set_dev_ops(s->bs, &scsi_cd_block_ops, s);
         s->qdev.blocksize = 2048;
     } else {
         s->qdev.blocksize = s->qdev.conf.logical_block_size;
diff --git a/qmp-commands.hx b/qmp-commands.hx
index 92c5c3a..cc5d30f 100644
--- a/qmp-commands.hx
+++ b/qmp-commands.hx
@@ -1070,6 +1070,7 @@  Each json-object contain the following:
          - Possible values: "unknown"
 - "removable": true if the device is removable, false otherwise (json-bool)
 - "locked": true if the device is locked, false otherwise (json-bool)
+- "ejected": present and true if the device ejected its media (json-bool)
 - "inserted": only present if the device is inserted, it is a json-object
    containing the following:
          - "file": device file name (json-string)