Skip to contentSkip to conversation

blockdev: Fix 'change' for slot devices

PATCH Superseded Max Reitz 4 participants 11 messages Jan 20, 2016 – Jan 22, 2016 Block layer

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

v2 cover blockdev: Fix 'change' for slot devices

The series "BlockBackend and media" intended all block devices with removable media to implement a tray model; if the devices does not have a tray, it should emulate one.

While this may make sense from a technical perspective (blockdev-*-tray are guest device controlling operations, invoking blk_dev_change_media_cb(); blockdev-*-medium are operations concerning the block layer, controlling the BB-BDS link), it is (probably) unintuitive to users, and it requires said implementation of an emulated tray for each of the slot devices (floppy disk drives and SD card readers).

We can get rid of those virtual trays by special-casing tray-less devices in blockdev-*-tray (those operations are no-ops there) and in blockdev-*-medium (those operations then have to invoke blk_dev_change_media_cb()). With this change, changing the medium inserted into a slot device will no longer emit TRAY_MOVED events (which seems like a bugfix to me, because slot devices actually do not have trays).

Patches 1 and 2 are CC'd to qemu-stable because they fix 'change' for SD card readers. Patch 3 does not fix it for floppy disk drives, it just changes the behavior when used on them (no TRAY_MOVED events, no tray_open status, and blockdev-{open,close}-tray are optional), which is why it is not CC'd to qemu-stable; and patch 4 is more of a change in behavior than a stable-worthy fix.

v2:
- Patch 2:
Invoke blk_dev_change_media_cb() after blk_{remove,insert}_bs() have been called, not before; with this change, the value returned by blk_is_inserted() when invoked in the CB will be equal to the @load parameter passed to the CB.
[Peter]

- Patch 4:
Change BlockInfo's @tray_open documentation so it fits the functional changes of that patch.
[Berto]

git-backport-diff against v1:

Key:
[----] : patches are identical
[####] : number of functional differences between upstream/downstream patch [down] : patch is downstream-only
The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively

001/4:[----] [--] 'block: Add blk_dev_has_tray()' 002/4:[0008] [FC] 'blockdev: Fix 'change' for slot devices' 003/4:[----] [--] 'Revert "hw/block/fdc: Implement tray status"' 004/4:[0004] [FC] 'block/qapi: Emit tray_open only if there is a tray'

Max Reitz (4):
block: Add blk_dev_has_tray()
blockdev: Fix 'change' for slot devices Revert "hw/block/fdc: Implement tray status" block/qapi: Emit tray_open only if there is a tray

 block/block-backend.c      |  10 +++-
 block/qapi.c               |   2 +-
 blockdev.c                 |  31 +++++++++++-
 hw/block/fdc.c             |  20 ++------
 include/block/block_int.h  |   1 +
 qapi/block-core.json       |   7 ++-
 tests/fdc-test.c           |   2 -
 tests/qemu-iotests/067.out |   4 --
 tests/qemu-iotests/118     | 117 ++++++++++++++-------------------------------
 9 files changed, 83 insertions(+), 111 deletions(-)
Signature · 1 line
2.7.0
author
#1 lore raw

PATCH v2 1/4 block: Add blk_dev_has_tray()

Pull out the check whether a block device has a tray from blk_dev_is_tray_open() into its own function so both attributes (whether there is a tray vs. whether that tray is open) can be queried independently.

---
 block/block-backend.c     | 10 +++++++++-
 include/block/block_int.h |  1 +
 2 files changed, 10 insertions(+), 1 deletion(-)
block/block-backend.c +9 -1
index e813759..31fe717 100644
@@ -458,6 +458,14 @@ bool blk_dev_has_removable_media(BlockBackend *blk)
 }
 
 /*
+ * Does @blk's attached device model have a tray?
+ */
+bool blk_dev_has_tray(BlockBackend *blk)
+{
+    return blk->dev_ops && blk->dev_ops->is_tray_open;
+}
+
+/*
  * Notify @blk's attached device model of a media eject request.
  * If @force is true, the medium is about to be yanked out forcefully.
  */
@@ -473,7 +481,7 @@ void blk_dev_eject_request(BlockBackend *blk, bool force)
  */
 bool blk_dev_is_tray_open(BlockBackend *blk)
 {
-    if (blk->dev_ops && blk->dev_ops->is_tray_open) {
+    if (blk_dev_has_tray(blk)) {
         return blk->dev_ops->is_tray_open(blk->dev_opaque);
     }
     return false;
include/block/block_int.h +1 -0
index 256609d..14bf022 100644
@@ -694,6 +694,7 @@ void blk_set_bs(BlockBackend *blk, BlockDriverState *bs);
 
 void blk_dev_change_media_cb(BlockBackend *blk, bool load);
 bool blk_dev_has_removable_media(BlockBackend *blk);
+bool blk_dev_has_tray(BlockBackend *blk);
 void blk_dev_eject_request(BlockBackend *blk, bool force);
 bool blk_dev_is_tray_open(BlockBackend *blk);
 bool blk_dev_is_medium_locked(BlockBackend *blk);
Signature · 1 line
2.7.0
author
#2 lore raw

PATCH v2 2/4 blockdev: Fix 'change' for slot devices

In reply to Max Reitz

'change' and related operations did not work when used on guest devices featuring removable media but no actual tray, because blk_dev_is_tray_open() always returned false for them and the blockdev-{insert,remove}-medium commands required it to return true.

Fix this by making blockdev-{insert,remove}-medium work on tray-less devices. Also, blockdev-{open,close}-tray are now explicitly no-ops when invoked on such devices, and blk_dev_change_media_cb() is instead called by blockdev-{insert,remove}-medium (for tray-less devices only).

---
 blockdev.c           | 31 +++++++++++++++++++++++++++++--
 qapi/block-core.json |  3 +--
 2 files changed, 30 insertions(+), 4 deletions(-)
blockdev.c +29 -2
index 1392fff..33d01cd 100644
@@ -2303,6 +2303,11 @@ void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
         return;
     }
 
+    if (!blk_dev_has_tray(blk)) {
+        /* Ignore this command on tray-less devices */
+        return;
+    }
+
     if (blk_dev_is_tray_open(blk)) {
         return;
     }
@@ -2333,6 +2338,11 @@ void qmp_blockdev_close_tray(const char *device, Error **errp)
         return;
     }
 
+    if (!blk_dev_has_tray(blk)) {
+        /* Ignore this command on tray-less devices */
+        return;
+    }
+
     if (!blk_dev_is_tray_open(blk)) {
         return;
     }
@@ -2362,7 +2372,7 @@ void qmp_x_blockdev_remove_medium(const char *device, Error **errp)
         return;
     }
 
-    if (has_device && !blk_dev_is_tray_open(blk)) {
+    if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
         error_setg(errp, "Tray of device '%s' is not open", device);
         return;
     }
@@ -2387,6 +2397,14 @@ void qmp_x_blockdev_remove_medium(const char *device, Error **errp)
 
     blk_remove_bs(blk);
 
+    if (!blk_dev_has_tray(blk)) {
+        /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
+         * called at all); therefore, the medium needs to be ejected here.
+         * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
+         * value passed here (i.e. false). */
+        blk_dev_change_media_cb(blk, false);
+    }
+
 out:
     aio_context_release(aio_context);
 }
@@ -2412,7 +2430,7 @@ static void qmp_blockdev_insert_anon_medium(const char *device,
         return;
     }
 
-    if (has_device && !blk_dev_is_tray_open(blk)) {
+    if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
         error_setg(errp, "Tray of device '%s' is not open", device);
         return;
     }
@@ -2424,6 +2442,15 @@ static void qmp_blockdev_insert_anon_medium(const char *device,
 
     blk_insert_bs(blk, bs);
 
+    if (!blk_dev_has_tray(blk)) {
+        /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
+         * called at all); therefore, the medium needs to be pushed into the
+         * slot here.
+         * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
+         * value passed here (i.e. true). */
+        blk_dev_change_media_cb(blk, true);
+    }
+
     QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
 }
 
qapi/block-core.json +1 -2
index 0a915ed..40239bf 100644
@@ -2098,8 +2098,7 @@
 #   respond to the eject request
 # - if the BlockBackend denoted by @device does not have a guest device attached
 #   to it
-# - if the guest device does not have an actual tray and is empty, for instance
-#   for floppy disk drives
+# - if the guest device does not have an actual tray
 #
 # @device: block device name
 #
Signature · 1 line
2.7.0
Reviewed-by on v2 2/4
#3 lore raw
Quoted 10 lines · Max Reitz

'change' and related operations did not work when used on guest devices featuring removable media but no actual tray, because blk_dev_is_tray_open() always returned false for them and the blockdev-{insert,remove}-medium commands required it to return true.

Fix this by making blockdev-{insert,remove}-medium work on tray-less devices. Also, blockdev-{open,close}-tray are now explicitly no-ops when invoked on such devices, and blk_dev_change_media_cb() is instead called by blockdev-{insert,remove}-medium (for tray-less devices only).

Quoted 8 lines
---
 blockdev.c           | 31 +++++++++++++++++++++++++++++--
 qapi/block-core.json |  3 +--
 2 files changed, 30 insertions(+), 4 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#4 lore raw
In reply to Max Reitz
Quoted 5 lines · Max Reitz
blockdev.c +1 -0
@@ -2424,6 +2442,15 @@ static void qmp_blockdev_insert_anon_medium(const char *device,
 
     blk_insert_bs(blk, bs);
 
+    if (!blk_dev_has_tray(blk)) {

blockdev.c static void qmp_blockdev_insert_anon_medium(const char *device, · line 2455

Quoted 10 lines
blockdev.c +8 -0
+        /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
+         * called at all); therefore, the medium needs to be pushed into the
+         * slot here.
+         * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
+         * value passed here (i.e. true). */
+        blk_dev_change_media_cb(blk, true);
+    }
+
     QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
 }

Any reason why you do this before updating bdrv_states ?

If the device has a tray this would happen afterwards, in qmp_blockdev_close_tray().

Berto

author
#5 lore raw
Quoted 5 lines · Alberto Garcia
Quoted 4 lines · Max Reitz
blockdev.c +0 -0
@@ -2424,6 +2442,15 @@ static void qmp_blockdev_insert_anon_medium(const char *device,
 
     blk_insert_bs(blk, bs);
 
Quoted 13 lines

blockdev.c static void qmp_blockdev_insert_anon_medium(const char *device, · line 2455

Quoted 11 lines
blockdev.c +9 -0
+    if (!blk_dev_has_tray(blk)) {
+        /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
+         * called at all); therefore, the medium needs to be pushed into the
+         * slot here.
+         * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
+         * value passed here (i.e. true). */
+        blk_dev_change_media_cb(blk, true);
+    }
+
     QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
 }

Any reason why you do this before updating bdrv_states ?

The reason is that I just moved it after blk_insert_bs(). But that's wrong, QTAILQ_INSERT_TAIL() should be paired with blk_insert_bs() without anything in between, thanks!

(I don't think it changes anything in practice, but it still is wrong.)

Quoted 2 lines

If the device has a tray this would happen afterwards, in qmp_blockdev_close_tray().

Well, tray devices are no longer really a good comparison, because in the opposite case (ejecting a medium), for them we open the tray and then eject the medium; however, for trayless devices we now eject the medium and only then "open the tray" (invoke blk_dev_change_media_cb()).

Anyway, will fix, thanks.

Max

author
#6 lore raw

PATCH v2 3/4 Revert "hw/block/fdc: Implement tray status"

In reply to Max Reitz

This reverts the changes that commit 2e1280e8ff95b3145bc6262accc9d447718e5318 applied to hw/block/fdc.c.

That commit changed tests/fdc-test.c, too, because after it, one less TRAY_MOVED event would be emitted when executing 'change' on an empty drive. However, now, no TRAY_MOVED events will be emitted at all, and the tray_open status returned by query-block will always be false, necessitating (different) changes to tests/fdc-test.c and iotest 118, which is why this patch is not a pure revert of said commit.

---
 hw/block/fdc.c         |  20 ++-------
 tests/fdc-test.c       |   2 -
 tests/qemu-iotests/118 | 117 +++++++++++++++----------------------------------
 3 files changed, 40 insertions(+), 99 deletions(-)
hw/block/fdc.c +4 -16
index 858f5f7..621fd37 100644
@@ -192,8 +192,6 @@ typedef struct FDrive {
     uint8_t ro;               /* Is read-only           */
     uint8_t media_changed;    /* Is media changed       */
     uint8_t media_rate;       /* Data rate of medium    */
-
-    bool media_inserted;      /* Is there a medium in the tray */
 } FDrive;
 
 static void fd_init(FDrive *drv)
@@ -263,7 +261,7 @@ static int fd_seek(FDrive *drv, uint8_t head, uint8_t track, uint8_t sect,
 #endif
         drv->head = head;
         if (drv->track != track) {
-            if (drv->media_inserted) {
+            if (drv->blk != NULL && blk_is_inserted(drv->blk)) {
                 drv->media_changed = 0;
             }
             ret = 1;
@@ -272,7 +270,7 @@ static int fd_seek(FDrive *drv, uint8_t head, uint8_t track, uint8_t sect,
         drv->sect = sect;
     }
 
-    if (!drv->media_inserted) {
+    if (drv->blk == NULL || !blk_is_inserted(drv->blk)) {
         ret = 2;
     }
 
@@ -298,7 +296,7 @@ static void fd_revalidate(FDrive *drv)
         ro = blk_is_read_only(drv->blk);
         pick_geometry(drv->blk, &nb_heads, &max_track,
                       &last_sect, drv->drive, &drive, &rate);
-        if (!drv->media_inserted) {
+        if (!blk_is_inserted(drv->blk)) {
             FLOPPY_DPRINTF("No disk in drive\n");
         } else {
             FLOPPY_DPRINTF("Floppy disk (%d h %d t %d s) %s\n", nb_heads,
@@ -694,7 +692,7 @@ static bool fdrive_media_changed_needed(void *opaque)
 {
     FDrive *drive = opaque;
 
-    return (drive->media_inserted && drive->media_changed != 1);
+    return (drive->blk != NULL && drive->media_changed != 1);
 }
 
 static const VMStateDescription vmstate_fdrive_media_changed = {
@@ -2186,21 +2184,12 @@ static void fdctrl_change_cb(void *opaque, bool load)
 {
     FDrive *drive = opaque;
 
-    drive->media_inserted = load && drive->blk && blk_is_inserted(drive->blk);
-
     drive->media_changed = 1;
     fd_revalidate(drive);
 }
 
-static bool fdctrl_is_tray_open(void *opaque)
-{
-    FDrive *drive = opaque;
-    return !drive->media_inserted;
-}
-
 static const BlockDevOps fdctrl_block_ops = {
     .change_media_cb = fdctrl_change_cb,
-    .is_tray_open = fdctrl_is_tray_open,
 };
 
 /* Init functions */
@@ -2228,7 +2217,6 @@ static void fdctrl_connect_drives(FDCtrl *fdctrl, Error **errp)
         fdctrl_change_cb(drive, 0);
         if (drive->blk) {
             blk_set_dev_ops(drive->blk, &fdctrl_block_ops, drive);
-            drive->media_inserted = blk_is_inserted(drive->blk);
         }
     }
 }
tests/fdc-test.c +0 -2
index b5a4696..7f0e215 100644
@@ -304,7 +304,6 @@ static void test_media_insert(void)
     qmp_discard_response("{'execute':'change', 'arguments':{"
                          " 'device':'floppy0', 'target': %s, 'arg': 'raw' }}",
                          test_image);
-    qmp_discard_response(""); /* ignore event (open -> close) */
 
     dir = inb(FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
@@ -335,7 +334,6 @@ static void test_media_change(void)
      * reset the bit. */
     qmp_discard_response("{'execute':'eject', 'arguments':{"
                          " 'device':'floppy0' }}");
-    qmp_discard_response(""); /* ignore event */
 
     dir = inb(FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
tests/qemu-iotests/118 +36 -81

392 lines not on this page · raw message

Signature · 1 line
2.7.0
author
#7 lore raw

PATCH v2 4/4 block/qapi: Emit tray_open only if there is a tray

In reply to Max Reitz
---
 block/qapi.c               | 2 +-
 qapi/block-core.json       | 4 ++--
 tests/qemu-iotests/067.out | 4 ----
 3 files changed, 3 insertions(+), 7 deletions(-)
block/qapi.c +1 -1
index 58d3975..12a0f25 100644
@@ -299,7 +299,7 @@ static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
     info->locked = blk_dev_is_medium_locked(blk);
     info->removable = blk_dev_has_removable_media(blk);
 
-    if (blk_dev_has_removable_media(blk)) {
+    if (blk_dev_has_tray(blk)) {
         info->has_tray_open = true;
         info->tray_open = blk_dev_is_tray_open(blk);
     }
qapi/block-core.json +2 -2
index 40239bf..628a41d 100644
@@ -382,8 +382,8 @@
 # @locked: True if the guest has locked this device from having its media
 #          removed
 #
-# @tray_open: #optional True if the device has a tray and it is open
-#             (only present if removable is true)
+# @tray_open: #optional True if the device's tray is open
+#             (only present if it has a tray)
 #
 # @dirty-bitmaps: #optional dirty bitmaps information (only present if the
 #                 driver has one or more dirty bitmaps) (Since 2.0)
tests/qemu-iotests/067.out +0 -4
index 27ad56f..ae3fccb 100644
@@ -169,7 +169,6 @@ Testing: -drive file=TEST_DIR/t.qcow2,format=qcow2,if=none,id=disk
                 "file": "TEST_DIR/t.qcow2",
                 "encryption_key_missing": false
             },
-            "tray_open": false,
             "type": "unknown"
         }
     ]
@@ -289,7 +288,6 @@ Testing:
                 "file": "TEST_DIR/t.qcow2",
                 "encryption_key_missing": false
             },
-            "tray_open": false,
             "type": "unknown"
         }
     ]
@@ -410,7 +408,6 @@ Testing:
                 "file": "TEST_DIR/t.qcow2",
                 "encryption_key_missing": false
             },
-            "tray_open": false,
             "type": "unknown"
         }
     ]
@@ -501,7 +498,6 @@ Testing:
                 "file": "TEST_DIR/t.qcow2",
                 "encryption_key_missing": false
             },
-            "tray_open": false,
             "type": "unknown"
         }
     ]
Signature · 1 line
2.7.0
Reviewed-by on v2 4/4
#8 lore raw

Re: [Qemu-devel] [PATCH v2 4/4] block/qapi: Emit tray_open only if there is a tray

Quoted 7 lines · Max Reitz
---
 block/qapi.c               | 2 +-
 qapi/block-core.json       | 4 ++--
 tests/qemu-iotests/067.out | 4 ----
 3 files changed, 3 insertions(+), 7 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Tested-by on the v2 series
#10 lore raw
In reply to Max Reitz
Quoted 10 lines · Max Reitz

The series "BlockBackend and media" intended all block devices with removable media to implement a tray model; if the devices does not have a tray, it should emulate one.

Patches 1 and 2 are CC'd to qemu-stable because they fix 'change' for SD card readers. Patch 3 does not fix it for floppy disk drives, it just changes the behavior when used on them (no TRAY_MOVED events, no tray_open status, and blockdev-{open,close}-tray are optional), which is why it is not CC'd to qemu-stable; and patch 4 is more of a change in behavior than a stable-worthy fix.

Thanks; this fixes the regression in sd card handling for me.

(including testing that just the stable-cc'd patches 1 and 2 are sufficient to fix the regression).

-- PMM