Skip to contentSkip to conversation

blockdev: Fix 'change' for slot devices

PATCH Superseded Max Reitz 4 participants 14 messages Jan 12, 2016 – Jan 18, 2016 Block layer

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

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.

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                 |  27 ++++++++++-
 hw/block/fdc.c             |  20 ++------
 include/block/block_int.h  |   1 +
 qapi/block-core.json       |   3 +-
 tests/fdc-test.c           |   2 -
 tests/qemu-iotests/067.out |   4 --
 tests/qemu-iotests/118     | 117 ++++++++++++++-------------------------------
 9 files changed, 77 insertions(+), 109 deletions(-)
Signature · 1 line
2.7.0
author
#1 lore raw

PATCH 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 an 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 f41d326..60f9588 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
Reviewed-by on v1 1/4
#2 lore raw

Re: [Qemu-devel] [PATCH 1/4] block: Add blk_dev_has_tray()

Quoted 2 lines · Max Reitz

Pull out the check whether a block device has a tray from blk_dev_is_tray_open() into an own function so both attributes (whether

s.an own/its own/

Quoted 9 lines

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(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v1 1/4
#3 lore raw

Re: [Qemu-devel] [Qemu-block] [PATCH 1/4] block: Add blk_dev_has_tray()

In reply to Max Reitz
Quoted 7 lines · Max Reitz

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

Berto

author
#4 lore raw

PATCH 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           | 27 +++++++++++++++++++++++++--
 qapi/block-core.json |  3 +--
 2 files changed, 26 insertions(+), 4 deletions(-)
blockdev.c +25 -2
index 2df0c6d..f053be6 100644
@@ -2305,6 +2305,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;
     }
@@ -2335,6 +2340,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;
     }
@@ -2364,7 +2374,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;
     }
@@ -2381,6 +2391,12 @@ void qmp_x_blockdev_remove_medium(const char *device, Error **errp)
         goto out;
     }
 
+    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 */
+        blk_dev_change_media_cb(blk, false);
+    }
+
     /* This follows the convention established by bdrv_make_anon() */
     if (bs->device_list.tqe_prev) {
         QTAILQ_REMOVE(&bdrv_states, bs, device_list);
@@ -2414,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 +2440,13 @@ static void qmp_blockdev_insert_anon_medium(const char *device,
         return;
     }
 
+    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 */
+        blk_dev_change_media_cb(blk, true);
+    }
+
     blk_insert_bs(blk, bs);
 
     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 v1 2/4
#5 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 7 lines
---
 blockdev.c           | 27 +++++++++++++++++++++++++--
 qapi/block-core.json |  3 +--
 2 files changed, 26 insertions(+), 4 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v1 2/4
#6 lore raw
In reply to Max Reitz
Quoted 5 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.

Quoted 8 lines

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).

Berto

author
#7 lore raw

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

In reply to Max Reitz

This reverts the changes 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 4292ece..6686a72 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
Reviewed-by on v1 3/4
#8 lore raw

Re: [Qemu-devel] [PATCH 3/4] Revert "hw/block/fdc: Implement tray status"

Quoted 3 lines · Max Reitz

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

Might read better as s/changes commit/changes that commit/

Quoted 7 lines

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.

Quoted 7 lines
---
 hw/block/fdc.c         |  20 ++-------
 tests/fdc-test.c       |   2 -
 tests/qemu-iotests/118 | 117 +++++++++++++++----------------------------------
 3 files changed, 40 insertions(+), 99 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#9 lore raw

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

In reply to Max Reitz
---
 block/qapi.c               | 2 +-
 tests/qemu-iotests/067.out | 4 ----
 2 files changed, 1 insertion(+), 5 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);
     }
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 v1 4/4
#10 lore raw

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

Quoted 6 lines · Max Reitz
---
 block/qapi.c               | 2 +-
 tests/qemu-iotests/067.out | 4 ----
 2 files changed, 1 insertion(+), 5 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#11 lore raw

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

In reply to Max Reitz
Quoted 11 lines · Max Reitz
---
 block/qapi.c               | 2 +-
 tests/qemu-iotests/067.out | 4 ----
 2 files changed, 1 insertion(+), 5 deletions(-)
block/qapi.c +0 -0
index 58d3975..12a0f25 100644

block/qapi.c static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, · line 305

Quoted 8 lines
block/qapi.c +1 -1
@@ -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);
     }

It probably makes sense to update the documentation as well:

# @tray_open: #optional True if the device has a tray and it is open
# (only present if removable is true)

Berto

#12 lore raw
In reply to Max Reitz
Quoted 3 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.

I tried this for the zaurus (spitz) board, and although "change sd0 file" now doesn't give an error, it doesn't cause the guest to notice that an SD card has been inserted either.

I start the guest with no SD card plugged in, and then from the monitor issue "change sd0 file.img". The sd_cardchange callback is called (from blk_dev_change_media_cb, with load==true), but when it sd_cardchange() calls blk_is_inserted(sd->blk) this returns false and so the SD card model reports to the controller that no card is present.

If I issue the command again, the callback is called twice, firstly with load=false -- this time blk_is_inserted() returns true and so the sd card says "card has been inserted" (and the guest notices this and prints its usual found-mmc-card logging). Then on the second callback with load==true blk_is_inserted() returns false again and so the card says "card not inserted".

So it looks like the sense of blk_is_inserted() when the loading/unloading callbacks are made is the opposite of what the sd.c code is expecting.

(You can see this behaviour with just the first 2 cc-stable patches.)

thanks
-- PMM

#13 lore raw
Quoted 8 lines · Peter Maydell
Quoted 3 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.

I tried this for the zaurus (spitz) board, and although "change sd0 file" now doesn't give an error, it doesn't cause the guest to notice that an SD card has been inserted either.

PS: my test image for zaurus is here:
http://people.linaro.org/~peter.maydell/zaurus2.tgz

You can run it with
path/to/zaurus2/runme path/to/qemu-system-arm
which by default will boot up with no SD card inserted, serial console output to stdout. Using the 'change sd0 somefile' ought to then make it notice an SD card and print the usual kernel messages.

thanks
-- PMM