Skip to contentSkip to conversation

block: Extend QMP events for anonymous BlockBackends

PATCH In review Kevin Wolf 3 participants 9 messages Oct 5, 2016 – Oct 6, 2016 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

cover block: Extend QMP events for anonymous BlockBackends

We have two QMP events that include only a BlockBackend name and that are therefore useless with anonymous BBs. This series adds a node-name/qdev ID to them.

Kevin Wolf (3):
block: Add node name to BLOCK_IO_ERROR event
block-backend: Remember if attached device is non-qdev
block: Add qdev ID to DEVICE_TRAY_MOVED

 block.c                        |  7 -----
 block/block-backend.c          | 64 +++++++++++++++++++++++++++++++++++-------
 docs/qmp-commands.txt          |  3 ++
 docs/qmp-events.txt            | 12 ++++++--
 hw/block/xen_disk.c            |  2 +-
 include/sysemu/block-backend.h |  4 +--
 qapi/block-core.json           |  8 ++++--
 qapi/block.json                |  8 ++++--
 8 files changed, 82 insertions(+), 26 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 1/3 block: Add node name to BLOCK_IO_ERROR event

The event currently only contains the BlockBackend name. However, with anonymous BlockBackends, this is always the empty string. Add the node name so that the user can still see which block device caused the event.

---
 block/block-backend.c | 5 +++--
 docs/qmp-events.txt   | 6 +++++-
 qapi/block-core.json  | 8 ++++++--
 3 files changed, 14 insertions(+), 5 deletions(-)
block/block-backend.c +3 -2
index 639294b..911254a 100644
@@ -1206,8 +1206,9 @@ static void send_qmp_error_event(BlockBackend *blk,
     IoOperationType optype;
 
     optype = is_read ? IO_OPERATION_TYPE_READ : IO_OPERATION_TYPE_WRITE;
-    qapi_event_send_block_io_error(blk_name(blk), optype, action,
-                                   blk_iostatus_is_enabled(blk),
+    qapi_event_send_block_io_error(blk_name(blk),
+                                   bdrv_get_node_name(blk_bs(blk)), optype,
+                                   action, blk_iostatus_is_enabled(blk),
                                    error == ENOSPC, strerror(error),
                                    &error_abort);
 }
docs/qmp-events.txt +5 -1
index 7967ec4..25307ab 100644
@@ -65,7 +65,10 @@ Emitted when a disk I/O error occurs.
 
 Data:
 
-- "device": device name (json-string)
+- "device": device name. This is always present for compatibility
+            reasons, but it can be empty ("") if the image does not
+            have a device name associated. (json-string)
+- "node-name": node name (json-string)
 - "operation": I/O operation (json-string, "read" or "write")
 - "action": action that has been taken, it's one of the following (json-string):
     "ignore": error has been ignored
@@ -76,6 +79,7 @@ Example:
 
 { "event": "BLOCK_IO_ERROR",
     "data": { "device": "ide0-hd1",
+              "node-name": "#block212",
               "operation": "write",
               "action": "stop" },
     "timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
qapi/block-core.json +6 -2
index 9d797b8..c4538d6 100644
@@ -2545,7 +2545,11 @@
 #
 # Emitted when a disk I/O error occurs
 #
-# @device: device name
+# @device: device name. This is always present for compatibility
+#          reasons, but it can be empty ("") if the image does not
+#          have a device name associated.
+#
+# @node-name: node name (Since: 2.8)
 #
 # @operation: I/O operation
 #
@@ -2566,7 +2570,7 @@
 # Since: 0.13.0
 ##
 { 'event': 'BLOCK_IO_ERROR',
-  'data': { 'device': 'str', 'operation': 'IoOperationType',
+  'data': { 'device': 'str', 'node-name': 'str', 'operation': 'IoOperationType',
             'action': 'BlockErrorAction', '*nospace': 'bool',
             'reason': 'str' } }
 
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] block: Add node name to BLOCK_IO_ERROR event

Quoted 10 lines · Kevin Wolf

The event currently only contains the BlockBackend name. However, with anonymous BlockBackends, this is always the empty string. Add the node name so that the user can still see which block device caused the event.

---
 block/block-backend.c | 5 +++--
 docs/qmp-events.txt   | 6 +++++-
 qapi/block-core.json  | 8 ++++++--
 3 files changed, 14 insertions(+), 5 deletions(-)

Although it is a bit weird to report the root node's name, because I'd expect the I/O error to occur on the protocol layer and thus the event to report the protocol node's name.

OTOH, I think it'd be rather difficult to either keep the information from which node the error originated or to trace it back when we want to report it (and the decision to report it is generally made on the root node, so...).

Therefore, reporting it on the root node is probably the best we can do.

Would it make sense to add a short note to the documentation (qmp-events.txt and block-core.json) that the node name reported is not necessarily the node the I/O error originated from but may (and generally is) instead be some node above it?

Max

author
#3 lore raw

PATCH 2/3 block-backend: Remember if attached device is non-qdev

In reply to Kevin Wolf

Almost all block devices are qdevified by now. This allows us to go back from the BlockBackend to the DeviceState. xen_disk is the last device that is missing. We'll remember in the BlockBackend if a xen_disk is attached and can then disable any features that require going from a BB to the DeviceState.

While at it, clearly mark the function used by xen_disk as legacy even in its name, not just in TODO comments.

---
 block/block-backend.c          | 26 +++++++++++++++++++-------
 hw/block/xen_disk.c            |  2 +-
 include/sysemu/block-backend.h |  4 ++--
 3 files changed, 22 insertions(+), 10 deletions(-)
block/block-backend.c +19 -7
index 911254a..39c5613 100644
@@ -38,6 +38,7 @@ struct BlockBackend {
     BlockBackendPublic public;
 
     void *dev;                  /* attached device model, if any */
+    bool legacy_dev;            /* true if dev is not a DeviceState */
     /* TODO change to DeviceState when all users are qdevified */
     const BlockDevOps *dev_ops;
     void *dev_opaque;
@@ -507,32 +508,38 @@ void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs)
     }
 }
 
-/*
- * Attach device model @dev to @blk.
- * Return 0 on success, -EBUSY when a device model is attached already.
- */
-int blk_attach_dev(BlockBackend *blk, void *dev)
-/* TODO change to DeviceState *dev when all users are qdevified */
+static int blk_do_attach_dev(BlockBackend *blk, void *dev)
 {
     if (blk->dev) {
         return -EBUSY;
     }
     blk_ref(blk);
     blk->dev = dev;
+    blk->legacy_dev = false;
     blk_iostatus_reset(blk);
     return 0;
 }
 
 /*
  * Attach device model @dev to @blk.
+ * Return 0 on success, -EBUSY when a device model is attached already.
+ */
+int blk_attach_dev(BlockBackend *blk, DeviceState *dev)
+{
+    return blk_do_attach_dev(blk, dev);
+}
+
+/*
+ * Attach device model @dev to @blk.
  * @blk must not have a device model attached already.
  * TODO qdevified devices don't use this, remove when devices are qdevified
  */
-void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
+void blk_attach_dev_legacy(BlockBackend *blk, void *dev)
 {
     if (blk_attach_dev(blk, dev) < 0) {
         abort();
     }
+    blk->legacy_dev = true;
 }
 
 /*
@@ -586,6 +593,11 @@ BlockBackend *blk_by_dev(void *dev)
 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
                      void *opaque)
 {
+    /* All drivers that use blk_set_dev_ops() are qdevified and we want to keep
+     * it that way, so we can assume blk->dev is a DeviceState if blk->dev_ops
+     * is set. */
+    assert(!blk->legacy_dev);
+
     blk->dev_ops = ops;
     blk->dev_opaque = opaque;
 }
hw/block/xen_disk.c +1 -1
index 5aa350a..1292a4b 100644
@@ -1079,7 +1079,7 @@ static int blk_connect(struct XenDevice *xendev)
          * so we can blk_unref() unconditionally */
         blk_ref(blkdev->blk);
     }
-    blk_attach_dev_nofail(blkdev->blk, blkdev);
+    blk_attach_dev_legacy(blkdev->blk, blkdev);
     blkdev->file_size = blk_getlength(blkdev->blk);
     if (blkdev->file_size < 0) {
         BlockDriverState *bs = blk_bs(blkdev->blk);
include/sysemu/block-backend.h +2 -2
index a7993af..b07159b 100644
@@ -107,8 +107,8 @@ BlockDeviceIoStatus blk_iostatus(const BlockBackend *blk);
 void blk_iostatus_disable(BlockBackend *blk);
 void blk_iostatus_reset(BlockBackend *blk);
 void blk_iostatus_set_err(BlockBackend *blk, int error);
-int blk_attach_dev(BlockBackend *blk, void *dev);
-void blk_attach_dev_nofail(BlockBackend *blk, void *dev);
+int blk_attach_dev(BlockBackend *blk, DeviceState *dev);
+void blk_attach_dev_legacy(BlockBackend *blk, void *dev);
 void blk_detach_dev(BlockBackend *blk, void *dev);
 void *blk_get_attached_dev(BlockBackend *blk);
 BlockBackend *blk_by_dev(void *dev);
Signature · 1 line
1.8.3.1
Reviewed-by on v1 2/3
#4 lore raw

Re: [Qemu-devel] [PATCH 2/3] block-backend: Remember if attached device is non-qdev

Quoted 20 lines · Kevin Wolf

Almost all block devices are qdevified by now. This allows us to go back from the BlockBackend to the DeviceState. xen_disk is the last device that is missing. We'll remember in the BlockBackend if a xen_disk is attached and can then disable any features that require going from a BB to the DeviceState.

While at it, clearly mark the function used by xen_disk as legacy even in its name, not just in TODO comments.

---
 block/block-backend.c          | 26 +++++++++++++++++++-------
 hw/block/xen_disk.c            |  2 +-
 include/sysemu/block-backend.h |  4 ++--
 3 files changed, 22 insertions(+), 10 deletions(-)
block/block-backend.c +0 -0
index 911254a..39c5613 100644

[...]

Quoted 30 lines
block/block-backend.c +8 -6
@@ -507,32 +508,38 @@ void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs)
     }
 }
 
-/*
- * Attach device model @dev to @blk.
- * Return 0 on success, -EBUSY when a device model is attached already.
- */
-int blk_attach_dev(BlockBackend *blk, void *dev)
-/* TODO change to DeviceState *dev when all users are qdevified */
+static int blk_do_attach_dev(BlockBackend *blk, void *dev)
 {
     if (blk->dev) {
         return -EBUSY;
     }
     blk_ref(blk);
     blk->dev = dev;
+    blk->legacy_dev = false;
     blk_iostatus_reset(blk);
     return 0;
 }
 
 /*
  * Attach device model @dev to @blk.
+ * Return 0 on success, -EBUSY when a device model is attached already.
+ */
+int blk_attach_dev(BlockBackend *blk, DeviceState *dev)
+{
+    return blk_do_attach_dev(blk, dev);
+}

block/block-backend.c void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs) · line 539

Quoted 10 lines
block/block-backend.c +4 -1
+
+/*
+ * Attach device model @dev to @blk.
  * @blk must not have a device model attached already.
  * TODO qdevified devices don't use this, remove when devices are qdevified
  */
-void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
+void blk_attach_dev_legacy(BlockBackend *blk, void *dev)
 {
     if (blk_attach_dev(blk, dev) < 0) {

I'd make this a blk_do_attach_dev(), but it's not syntactically wrong, so the choice is up to you.

Thus, either way:

Quoted 6 lines
block/block-backend.c +1 -0
         abort();
     }
+    blk->legacy_dev = true;
 }
 
 /*
#5 lore raw

Re: [Qemu-devel] [PATCH 2/3] block-backend: Remember if attached device is non-qdev

Quoted 7 lines · Max Reitz
Quoted 6 lines
block/block-backend.c +5 -0
+static int blk_do_attach_dev(BlockBackend *blk, void *dev)
 
+ */
+int blk_attach_dev(BlockBackend *blk, DeviceState *dev)
+{
+    return blk_do_attach_dev(blk, dev);
Quoted 6 lines
Quoted 3 lines

+void blk_attach_dev_legacy(BlockBackend *blk, void *dev)

 {
     if (blk_attach_dev(blk, dev) < 0) {

I'd make this a blk_do_attach_dev(), but it's not syntactically wrong, so the choice is up to you.

It's technically undefined C behavior to cast from void* to an unrelated pointer and back to void* (you are only guaranteed a round trip from type to void* and back to original type, not with unrelated types). So syntactically valid but semantically undefined.

On the other hand, ABI-wise, I'd be shocked if our compilers are able to exploit our use of undefined behavior.

At any rate, using blk_do_attach_dev() would avoid the definedness problem, so I'd go ahead and make the change.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#6 lore raw

Re: [Qemu-devel] [PATCH 2/3] block-backend: Remember if attached device is non-qdev

In reply to Max Reitz

Am 05.10.2016 um 20:01 hat Max Reitz geschrieben:

Quoted 12 lines · Kevin Wolf
Quoted 10 lines

Almost all block devices are qdevified by now. This allows us to go back from the BlockBackend to the DeviceState. xen_disk is the last device that is missing. We'll remember in the BlockBackend if a xen_disk is attached and can then disable any features that require going from a BB to the DeviceState.

While at it, clearly mark the function used by xen_disk as legacy even in its name, not just in TODO comments.

Quoted 12 lines

block/block-backend.c void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs) · line 539

Quoted 9 lines
block/block-backend.c +3 -1
+/*
+ * Attach device model @dev to @blk.
  * @blk must not have a device model attached already.
  * TODO qdevified devices don't use this, remove when devices are qdevified
  */
-void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
+void blk_attach_dev_legacy(BlockBackend *blk, void *dev)
 {
     if (blk_attach_dev(blk, dev) < 0) {

I'd make this a blk_do_attach_dev(), but it's not syntactically wrong, so the choice is up to you.

Thanks for catching this, what you suggest is what I intended. Maybe we should just enable the gcc --do-what-i-mean option.

Kevin

author
#7 lore raw

PATCH 3/3 block: Add qdev ID to DEVICE_TRAY_MOVED

In reply to Kevin Wolf

The event currently only contains the BlockBackend name. However, with anonymous BlockBackends, this is always the empty string. Add the qdev ID (or if none was given, the QOM path) so that the user can still see which device caused the event.

Event generation has to be moved from bdrv_eject() to the BlockBackend because the BDS doesn't know the attached device, but that's easy because blk_eject() is the only user of it.

---
 block.c               |  7 -------
 block/block-backend.c | 33 ++++++++++++++++++++++++++++++++-
 docs/qmp-commands.txt |  3 +++
 docs/qmp-events.txt   |  6 +++++-
 qapi/block.json       |  8 ++++++--
 5 files changed, 46 insertions(+), 11 deletions(-)
block.c +0 -7
index bb1f1ec..b0bdad6 100644
@@ -3360,17 +3360,10 @@ int bdrv_media_changed(BlockDriverState *bs)
 void bdrv_eject(BlockDriverState *bs, bool eject_flag)
 {
     BlockDriver *drv = bs->drv;
-    const char *device_name;
 
     if (drv && drv->bdrv_eject) {
         drv->bdrv_eject(bs, eject_flag);
     }
-
-    device_name = bdrv_get_device_name(bs);
-    if (device_name[0] != '\0') {
-        qapi_event_send_device_tray_moved(device_name,
-                                          eject_flag, &error_abort);
-    }
 }
 
 /**
block/block-backend.c +32 -1
index 39c5613..6859791 100644
@@ -566,6 +566,23 @@ void *blk_get_attached_dev(BlockBackend *blk)
     return blk->dev;
 }
 
+/* Return the qdev ID, or if no ID is assigned the QOM path, of the block
+ * device attached to the BlockBackend. */
+static char *blk_get_attached_dev_id(BlockBackend *blk)
+{
+    DeviceState *dev;
+
+    assert(!blk->legacy_dev);
+    dev = blk->dev;
+
+    if (!dev) {
+        return g_strdup("");
+    } else if (dev->id) {
+        return g_strdup(dev->id);
+    }
+    return object_get_canonical_path(OBJECT(dev));
+}
+
 /*
  * Return the BlockBackend which has the device model @dev attached if it
  * exists, else null.
@@ -613,13 +630,17 @@ void blk_dev_change_media_cb(BlockBackend *blk, bool load)
     if (blk->dev_ops && blk->dev_ops->change_media_cb) {
         bool tray_was_open, tray_is_open;
 
+        assert(!blk->legacy_dev);
+
         tray_was_open = blk_dev_is_tray_open(blk);
         blk->dev_ops->change_media_cb(blk->dev_opaque, load);
         tray_is_open = blk_dev_is_tray_open(blk);
 
         if (tray_was_open != tray_is_open) {
-            qapi_event_send_device_tray_moved(blk_name(blk), tray_is_open,
+            char *id = blk_get_attached_dev_id(blk);
+            qapi_event_send_device_tray_moved(blk_name(blk), id, tray_is_open,
                                               &error_abort);
+            g_free(id);
         }
     }
 }
@@ -1325,9 +1346,19 @@ void blk_lock_medium(BlockBackend *blk, bool locked)
 void blk_eject(BlockBackend *blk, bool eject_flag)
 {
     BlockDriverState *bs = blk_bs(blk);
+    char *id;
+
+    /* blk_eject is only called by qdevified devices */
+    assert(!blk->legacy_dev);
 
     if (bs) {
         bdrv_eject(bs, eject_flag);
+
+        id = blk_get_attached_dev_id(blk);
+        qapi_event_send_device_tray_moved(blk_name(blk), id,
+                                          eject_flag, &error_abort);
+        g_free(id);
+
     }
 }
 
docs/qmp-commands.txt +3 -0
index e0adceb..e044029 100644
@@ -3239,6 +3239,7 @@ Example:
                     "microseconds": 716996 },
      "event": "DEVICE_TRAY_MOVED",
      "data": { "device": "ide1-cd0",
+               "id": "ide0-1-0",
                "tray-open": true } }
 
 <- { "return": {} }
@@ -3267,6 +3268,7 @@ Example:
                     "microseconds": 272147 },
      "event": "DEVICE_TRAY_MOVED",
      "data": { "device": "ide1-cd0",
+               "id": "ide0-1-0",
                "tray-open": false } }
 
 <- { "return": {} }
@@ -3303,6 +3305,7 @@ Example:
                     "microseconds": 549958 },
      "event": "DEVICE_TRAY_MOVED",
      "data": { "device": "ide1-cd0",
+               "id": "ide0-1-0",
                "tray-open": true } }
 
 <- { "return": {} }
docs/qmp-events.txt +5 -1
index 25307ab..4d56022 100644
@@ -218,12 +218,16 @@ or by HMP/QMP commands.
 
 Data:
 
-- "device": device name (json-string)
+- "device": Block device name. This is always present for compatibility
+            reasons, but it can be empty ("") if the image does not have a
+            device name associated. (json-string)
+- "id": The name or QOM path of the guest device (json-string)
 - "tray-open": true if the tray has been opened or false if it has been closed
                (json-bool)
 
 { "event": "DEVICE_TRAY_MOVED",
   "data": { "device": "ide1-cd0",
+            "id": "/machine/unattached/device[22]",
             "tray-open": true
   },
   "timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
qapi/block.json +6 -2
index c896bd1..4661fc9 100644
@@ -195,14 +195,18 @@
 # Emitted whenever the tray of a removable device is moved by the guest or by
 # HMP/QMP commands
 #
-# @device: device name
+# @device: Block device name. This is always present for compatibility
+#          reasons, but it can be empty ("") if the image does not
+#          have a device name associated.
+#
+# @id: The name or QOM path of the guest device
 #
 # @tray-open: true if the tray has been opened or false if it has been closed
 #
 # Since: 1.1
 ##
 { 'event': 'DEVICE_TRAY_MOVED',
-  'data': { 'device': 'str', 'tray-open': 'bool' } }
+  'data': { 'device': 'str', 'id': 'str', 'tray-open': 'bool' } }
 
 ##
 # @QuorumOpType
Signature · 1 line
1.8.3.1
Reviewed-by on v1 3/3
#8 lore raw

Re: [Qemu-devel] [PATCH 3/3] block: Add qdev ID to DEVICE_TRAY_MOVED

Quoted 9 lines · Kevin Wolf

The event currently only contains the BlockBackend name. However, with anonymous BlockBackends, this is always the empty string. Add the qdev ID (or if none was given, the QOM path) so that the user can still see which device caused the event.

Event generation has to be moved from bdrv_eject() to the BlockBackend because the BDS doesn't know the attached device, but that's easy because blk_eject() is the only user of it.

Quoted 8 lines
---
 block.c               |  7 -------
 block/block-backend.c | 33 ++++++++++++++++++++++++++++++++-
 docs/qmp-commands.txt |  3 +++
 docs/qmp-events.txt   |  6 +++++-
 qapi/block.json       |  8 ++++++--
 5 files changed, 46 insertions(+), 11 deletions(-)

I didn't even know we use the same event for both virtual and physical trays. Fun, fun.

Even more fun in that blk_eject() always issues the event, regardless of whether there actually is a physical tray. Therefore, I assume it's effectively just an event for the virtual tray which has to hook up in blk_eject() because a guest eject won't go through blk_dev_change_media_cb(), but it will go through blk_eject() (which QMP ejects do not). Totally makes sense.