Skip to contentSkip to conversation

Block layer cleanup & fixes

PATCH Applied Markus Armbruster 7 participants 145 messages Jul 20, 2011 – Aug 9, 2011 3 messages not in the archive Block layer

v1 of 3 Applied Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Block layer cleanup & fixes

This patch series looks bigger than it is. All the patches are small and hopefully easy to review.

Objectives:

* Push BlockDriverState members locked, tray_open, media_changed into device models, where they belong.

* BlockDriverState member removable is a confusing mess, replace it.

* Improve eject -f.

Also clean up minor messes as they get in the way.

Part I: Preliminaries
PATCH 01-09: Assorted block layer fixes and cleanup

Part II: Move tray state to device models
PATCH 10-18: IDE tray open/closed
PATCH 19-22: SCSI tray open/closed
PATCH 23-25: block layer kill tray_open
PATCH 26-27: IDE tray lock
PATCH 28-29: SCSI tray lock
PATCH 30-32: block layer kill locked
PATCH 33-35: IDE & SCSI synchronize physical tray on init PATCH 36-38: IDE & SCSI migrate tray state
PATCH 39-41: block layer & fdc media_changed

Part III: Replace removable
PATCH 42-44 clean up inappopriate uses of removable PATCH 45-46 replace remaining users

Part IV: Reduce unclean use of block_int.h
PATCH 47-53

PART V: Improve eject -f
PATCH 54-55

Naturally, I want all parts applied. But I did my best to make applying only a prefix workable, too.

Review invited from:

* Kevin, of course.

* Amit, you might be interested in the ATAPI stuff.

* Luiz, the following patches affect QMP's query-block: PATCH 23 "block: Show whether the guest ejected the medium in info block" and PATCH 45 "block: Clean up remaining users of "removable".

* Juan, please review PATCH 37 "ide/atapi: Preserve tray state on migration", PATCH 38 "scsi-disk: Preserve tray state on migration", and PATCH 43 "savevm: Include writable devices with removable media".

* Dimitry, have a look at PATCH 44 "spitz tosa: Simplify "drive is suitable for microdrive" test".

* Stefano, have a look at PATCH 45, it affects "-drive if=xen".

Testing

* Linux boots, both from HD and CD.

* For both IDE and SCSI:

- info block reports tray state correctly

- Guest locking the tray stops eject (without -f) and change

- eject -f; change works even while tray is locked by guest

- Physical tray state tracks the virtual one (file=/dev/sr0)

- Reading /dev/sr0 with tray open behaves as before: IDE closes the tray and reads (matches bare metal), SCSI reports no medium

- Tray state is migrated correctly (tested with savevm/loadvm)

* Guest still notices CD media change (IDE only, SCSI doesn't work before or after my patches because GESN isn't implemented)

* Migrating ide-cd to older version works when tray is closed and unlocked, else fails (tested with savevm/loadvm)

* Migrating scsi-cd to older version fails

* Linux detects fdc media change (from monitor)

* savevm and loadvm snapshot read/write floppy

* spitz and tosa initialize their microdrive as before (PATCH 44; tested with -S -M {spitz,tosa}, didn't actually try to boot)

Not tested:

* BlockDriver "host_floppy"

Lack the hardware. Like pretty much everybody not running a computer museum.

Is keeping BlockDriver "host_floppy" around still worth our while?

If the answer is no, I'm happy to respin with PATCH 39-41 replaced by a single patch to remove it, and simplify block.c and hw/fdc.c.

Markus Armbruster (55):
blockdev: Make eject fail for non-removable drives even with -f
block: Reset device model callbacks on detach
block: Attach non-qdev devices as well
block: Generalize change_cb() to BlockDevOps
block: Split change_cb() into change_media_cb(), resize_cb() block/raw-win32: Drop disabled code for removable host devices
block: Make BlockDriver method bdrv_set_locked() return void
block: Make BlockDriver method bdrv_eject() return void
block: Don't let locked flag prevent medium load
ide: Update command code definitions as per ACS-2 Table B.2
ide: Clean up case label indentation in ide_exec_cmd()
ide: Fix ATA command READ to set ATAPI signature for CD-ROM
ide: Use a table to declare which drive kinds accept each command
ide: Reject ATA commands specific to drive kinds ide/atapi: Clean up misleading name in cmd_start_stop_unit() ide/atapi: Track tray open/close state
ide/atapi: Switch from BlockDriverState's tray_open to own
scsi-disk: Reject CD-specific SCSI commands to disks
scsi-disk: Factor out scsi_disk_emulate_start_stop()
scsi-disk: Track tray open/close state
block: Revert entanglement of bdrv_is_inserted() with tray status
block: Drop tray status tracking, no longer used
block: Show whether the guest ejected the medium in info block ide/atapi: Track tray locked state
ide/atapi: Switch from BlockDriverState's locked to own tray_locked
scsi-disk: Track tray locked state
scsi-disk: Switch from BlockDriverState's locked to own tray_locked
block: Leave enforcing tray lock to device models
block: Drop medium lock tracking, ask device models instead
block: Rename bdrv_set_locked() to bdrv_lock_medium()
ide: Provide IDEDeviceInfo method exit()
ide/atapi: Don't fail eject when tray is already open ide/atapi: Avoid physical/virtual tray state mismatch
scsi-disk: Fix START_STOP to fail when it can't eject
scsi-disk: Avoid physical/virtual tray state mismatch
ide: Give vmstate structs internal linkage where possible ide/atapi: Preserve tray state on migration
scsi-disk: Preserve tray state on migration
block/raw: Fix to forward method bdrv_media_changed()
block: Leave tracking media change to device models
fdc: Make media change detection more robust
block: Clean up bdrv_flush_all()
savevm: Include writable devices with removable media spitz tosa: Simplify "drive is suitable for microdrive" test
block: Clean up remaining users of "removable"
block: Drop BlockDriverState member removable
block: Move BlockConf & friends from block_int.h to block.h
hw: Trim superfluous #include "block_int.h"
block: Declare qemu_blockalign() in block.h, not block_int.h
block: New bdrv_set_buffer_alignment()
block: Reset buffer alignment on detach
block: Move BlockDriverAIOCB & friends from block_int.h to block.h
nbd: Clean up use of block_int.h
block: New change_media_cb() parameter load
ide/atapi scsi-disk: Make monitor eject -f, then change work

 block.c              |  205 ++++++++++++++++++++++++++------------------------
 block.h              |  104 +++++++++++++++++++++++---
 block/nbd.c          |    1 +
 block/raw-posix.c    |   30 +++-----
 block/raw-win32.c    |   35 ---------
 block/raw.c          |   18 +++--
 block_int.h          |   74 ++-----------------
 blockdev.c           |   28 +++-----
 hw/fdc.c             |   48 ++++++------
 hw/ide/atapi.c       |   58 +++++++--------
 hw/ide/cmd646.c      |    1 -
 hw/ide/core.c        |  195 ++++++++++++++++++++++++++++++++++++-----------
 hw/ide/ich.c         |    1 -
 hw/ide/internal.h    |  199 +++++++++++++++++++++++++-----------------------
 hw/ide/isa.c         |    1 -
 hw/ide/macio.c       |    1 -
 hw/ide/microdrive.c  |    1 -
 hw/ide/mmio.c        |    1 -
 hw/ide/pci.c         |    1 -
 hw/ide/piix.c        |    1 -
 hw/ide/qdev.c        |   30 +++++++
 hw/ide/via.c         |    1 -
 hw/lsi53c895a.c      |    1 -
 hw/nand.c            |    4 +-
 hw/onenand.c         |    6 +-
 hw/pflash_cfi01.c    |    1 +
 hw/pflash_cfi02.c    |    1 +
 hw/qdev-properties.c |    6 +-
 hw/scsi-bus.c        |   10 +++
 hw/scsi-disk.c       |  175 ++++++++++++++++++++++++++++++++++++++++---
 hw/scsi-generic.c    |    1 -
 hw/scsi.h            |    5 +-
 hw/sd.c              |   14 ++--
 hw/spitz.c           |   10 +--
 hw/tosa.c            |   10 +--
 hw/usb-msd.c         |    2 +-
 hw/virtio-blk.c      |   15 ++--
 hw/virtio.h          |    2 +-
 hw/xen_disk.c        |    1 +
 nbd.c                |    1 +
 nbd.h                |    2 -
 qmp-commands.hx      |    1 +
 savevm.c             |    4 +-
 trace-events         |    2 +-
 44 files changed, 792 insertions(+), 516 deletions(-)
Signature · 1 line
1.7.2.3
#1 lore raw

PATCH 1/55 blockdev: Make eject fail for non-removable drives even with -f

Ejecting hard disk platters can only end in tears.

If you need to revoke access to an image, use drive_del, not eject -f.

---
 blockdev.c |   17 +++++++----------
 1 files changed, 7 insertions(+), 10 deletions(-)
blockdev.c +7 -10
index a97a801..fcab8da 100644
@@ -646,16 +646,13 @@ out:
 
 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
 {
-    if (!force) {
-        if (!bdrv_is_removable(bs)) {
-            qerror_report(QERR_DEVICE_NOT_REMOVABLE,
-                           bdrv_get_device_name(bs));
-            return -1;
-        }
-        if (bdrv_is_locked(bs)) {
-            qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
-            return -1;
-        }
+    if (!bdrv_is_removable(bs)) {
+        qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
+        return -1;
+    }
+    if (!force && bdrv_is_locked(bs)) {
+        qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
+        return -1;
     }
     bdrv_close(bs);
     return 0;
Signature · 1 line
1.7.2.3
#3 lore raw

PATCH 2/55 block: Reset device model callbacks on detach

In reply to Markus Armbruster

BlockDriverState members change_cb and change_opaque are initially null. The device model may set them, with bdrv_set_change_cb(). If the device model gets detached (hot unplug), they're left dangling. Only safe because device hot unplug automatically destroys the BlockDriverState. But that's a questionable feature, best not to rely on it.

---
 block.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
block.c +2 -0
index 24a25d5..c49a825 100644
@@ -730,6 +730,8 @@ void bdrv_detach(BlockDriverState *bs, DeviceState *qdev)
 {
     assert(bs->peer == qdev);
     bs->peer = NULL;
+    bs->change_cb = NULL;
+    bs->change_opaque = NULL;
 }
 
 DeviceState *bdrv_get_attached(BlockDriverState *bs)
Signature · 1 line
1.7.2.3
Reviewed-by on v1 2/55
#4 lore raw

Re: [Qemu-devel] [PATCH 02/55] block: Reset device model callbacks on detach

Quoted 6 lines · Markus Armbruster

BlockDriverState members change_cb and change_opaque are initially null. The device model may set them, with bdrv_set_change_cb(). If the device model gets detached (hot unplug), they're left dangling. Only safe because device hot unplug automatically destroys the BlockDriverState. But that's a questionable feature, best not to rely on it.

Looks good,

#5 lore raw

PATCH 3/55 block: Attach non-qdev devices as well

In reply to Markus Armbruster

For now, this just protects against programming errors like having the same drive back multiple non-qdev devices, or untimely bdrv_delete(). Later commits will add other interesting uses.

While there, rename BlockDriverState member peer to dev, bdrv_attach() to bdrv_attach_dev(), bdrv_detach() to bdrv_detach_dev(), and bdrv_get_attached() to bdrv_get_attached_dev().

---
 block.c              |   29 ++++++++++++++++++++---------
 block.h              |    7 ++++---
 block_int.h          |    3 ++-
 blockdev.c           |    5 ++---
 hw/ide/core.c        |    1 +
 hw/nand.c            |    4 +++-
 hw/onenand.c         |    6 ++++--
 hw/pflash_cfi01.c    |    1 +
 hw/pflash_cfi02.c    |    1 +
 hw/qdev-properties.c |    6 +++---
 hw/sd.c              |    1 +
 hw/usb-msd.c         |    2 +-
 hw/xen_disk.c        |    1 +
 13 files changed, 44 insertions(+), 23 deletions(-)
block.c +20 -9
index c49a825..1429028 100644
@@ -703,7 +703,7 @@ void bdrv_make_anon(BlockDriverState *bs)
 
 void bdrv_delete(BlockDriverState *bs)
 {
-    assert(!bs->peer);
+    assert(!bs->dev);
 
     /* remove from list, if necessary */
     bdrv_make_anon(bs);
@@ -717,26 +717,37 @@ void bdrv_delete(BlockDriverState *bs)
     qemu_free(bs);
 }
 
-int bdrv_attach(BlockDriverState *bs, DeviceState *qdev)
+int bdrv_attach_dev(BlockDriverState *bs, void *dev)
+/* TODO change to DeviceState *dev when all users are qdevified */
 {
-    if (bs->peer) {
+    if (bs->dev) {
         return -EBUSY;
     }
-    bs->peer = qdev;
+    bs->dev = dev;
     return 0;
 }
 
-void bdrv_detach(BlockDriverState *bs, DeviceState *qdev)
+/* TODO qdevified devices don't use this, remove when devices are qdevified */
+void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev)
 {
-    assert(bs->peer == qdev);
-    bs->peer = NULL;
+    if (bdrv_attach_dev(bs, dev) < 0) {
+        abort();
+    }
+}
+
+void bdrv_detach_dev(BlockDriverState *bs, void *dev)
+/* TODO change to DeviceState *dev when all users are qdevified */
+{
+    assert(bs->dev == dev);
+    bs->dev = NULL;
     bs->change_cb = NULL;
     bs->change_opaque = NULL;
 }
 
-DeviceState *bdrv_get_attached(BlockDriverState *bs)
+/* TODO change to return DeviceState * when all users are qdevified */
+void *bdrv_get_attached_dev(BlockDriverState *bs)
 {
-    return bs->peer;
+    return bs->dev;
 }
 
 /*
block.h +4 -3
index 859d1d9..8d8172c 100644
@@ -72,9 +72,10 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags);
 int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
               BlockDriver *drv);
 void bdrv_close(BlockDriverState *bs);
-int bdrv_attach(BlockDriverState *bs, DeviceState *qdev);
-void bdrv_detach(BlockDriverState *bs, DeviceState *qdev);
-DeviceState *bdrv_get_attached(BlockDriverState *bs);
+int bdrv_attach_dev(BlockDriverState *bs, void *dev);
+void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev);
+void bdrv_detach_dev(BlockDriverState *bs, void *dev);
+void *bdrv_get_attached_dev(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,
block_int.h +2 -1
index 1e265d2..1a23d8a 100644
@@ -159,7 +159,8 @@ struct BlockDriverState {
     BlockDriver *drv; /* NULL means no media */
     void *opaque;
 
-    DeviceState *peer;
+    void *dev;                  /* attached device model, if any */
+    /* TODO change to DeviceState when all users are qdevified */
 
     char filename[1024];
     char backing_file[1024]; /* if non zero, the image is a diff of
blockdev.c +2 -3
index fcab8da..13a9b41 100644
@@ -14,7 +14,6 @@
 #include "qemu-option.h"
 #include "qemu-config.h"
 #include "sysemu.h"
-#include "hw/qdev.h"
 #include "block_int.h"
 
 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
@@ -747,12 +746,12 @@ int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
     bdrv_flush(bs);
     bdrv_close(bs);
 
-    /* if we have a device associated with this BlockDriverState (bs->peer)
+    /* if we have a device attached to this BlockDriverState
      * then we need to make the drive anonymous until the device
      * can be removed.  If this is a drive with no device backing
      * then we can just get rid of the block driver state right here.
      */
-    if (bs->peer) {
+    if (bdrv_get_attached_dev(bs)) {
         bdrv_make_anon(bs);
     } else {
         drive_uninit(drive_get_by_blockdev(bs));
hw/ide/core.c +1 -0
index d145b19..bcef8bc 100644
@@ -1865,6 +1865,7 @@ void ide_init2_with_non_qdev_drives(IDEBus *bus, DriveInfo *hd0,
                 error_report("Can't set up IDE drive %s", dinfo->id);
                 exit(1);
             }
+            bdrv_attach_dev_nofail(dinfo->bdrv, &bus->ifs[i]);
         } else {
             ide_reset(&bus->ifs[i]);
         }
hw/nand.c +3 -1
index 37e51d7..242cc02 100644
@@ -463,8 +463,10 @@ NANDFlashState *nand_init(int manf_id, int chip_id)
 
     s = (NANDFlashState *) qemu_mallocz(sizeof(NANDFlashState));
     dinfo = drive_get(IF_MTD, 0, 0);
-    if (dinfo)
+    if (dinfo) {
         s->bdrv = dinfo->bdrv;
+        bdrv_attach_dev_nofail(s->bdrv, s);
+    }
     s->manf_id = manf_id;
     s->chip_id = chip_id;
     s->size = nand_flash_ids[s->chip_id].size << 20;
hw/onenand.c +4 -2
index 71c1ab4..8382a6f 100644
@@ -632,11 +632,13 @@ void *onenand_init(uint32_t id, int regshift, qemu_irq irq)
     s->density_mask = (id & (1 << 11)) ? (1 << (6 + ((id >> 12) & 7))) : 0;
     s->iomemtype = cpu_register_io_memory(onenand_readfn,
                     onenand_writefn, s, DEVICE_NATIVE_ENDIAN);
-    if (!dinfo)
+    if (!dinfo) {
         s->image = memset(qemu_malloc(size + (size >> 5)),
                         0xff, size + (size >> 5));
-    else
+    } else {
         s->bdrv = dinfo->bdrv;
+        bdrv_attach_dev_nofail(s->bdrv, s);
+    }
     s->otp = memset(qemu_malloc((64 + 2) << PAGE_SHIFT),
                     0xff, (64 + 2) << PAGE_SHIFT);
     s->ram = qemu_ram_alloc(NULL, "onenand.ram", 0xc000 << s->shift);
hw/pflash_cfi01.c +1 -0
index 90fdc84..285e385 100644
@@ -620,6 +620,7 @@ pflash_t *pflash_cfi01_register(target_phys_addr_t base, ram_addr_t off,
             qemu_free(pfl);
             return NULL;
         }
+        bdrv_attach_dev_nofail(pfl->bs, pfl);
     }
 #if 0 /* XXX: there should be a bit to set up read-only,
        *      the same way the hardware does (with WP pin).
hw/pflash_cfi02.c +1 -0
index 725cd1e..059824d 100644
@@ -643,6 +643,7 @@ pflash_t *pflash_cfi02_register(target_phys_addr_t base, ram_addr_t off,
             qemu_free(pfl);
             return NULL;
         }
+        bdrv_attach_dev_nofail(pfl->bs, pfl);
     }
 #if 0 /* XXX: there should be a bit to set up read-only,
        *      the same way the hardware does (with WP pin).
hw/qdev-properties.c +3 -3
index eff2d24..f06b317 100644
@@ -312,7 +312,7 @@ static int parse_drive(DeviceState *dev, Property *prop, const char *str)
     bs = bdrv_find(str);
     if (bs == NULL)
         return -ENOENT;
-    if (bdrv_attach(bs, dev) < 0)
+    if (bdrv_attach_dev(bs, dev) < 0)
         return -EEXIST;
     *ptr = bs;
     return 0;
@@ -323,7 +323,7 @@ static void free_drive(DeviceState *dev, Property *prop)
     BlockDriverState **ptr = qdev_get_prop_ptr(dev, prop);
 
     if (*ptr) {
-        bdrv_detach(*ptr, dev);
+        bdrv_detach_dev(*ptr, dev);
         blockdev_auto_del(*ptr);
     }
 }
@@ -678,7 +678,7 @@ int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *va
 {
     int res;
 
-    res = bdrv_attach(value, dev);
+    res = bdrv_attach_dev(value, dev);
     if (res < 0) {
         error_report("Can't attach drive %s to %s.%s: %s",
                      bdrv_get_device_name(value),
hw/sd.c +1 -0
index cedfb20..3fdd5b0 100644
@@ -451,6 +451,7 @@ SDState *sd_init(BlockDriverState *bs, int is_spi)
     sd->enable = 1;
     sd_reset(sd, bs);
     if (sd->bdrv) {
+        bdrv_attach_dev_nofail(sd->bdrv, sd);
         bdrv_set_change_cb(sd->bdrv, sd_cardchange, sd);
     }
     return sd;
hw/usb-msd.c +1 -1
index 86582cc..8a82204 100644
@@ -530,7 +530,7 @@ static int usb_msd_initfn(USBDevice *dev)
      *
      * The hack is probably a bad idea.
      */
-    bdrv_detach(bs, &s->dev.qdev);
+    bdrv_detach_dev(bs, &s->dev.qdev);
     s->conf.bs = NULL;
 
     if (!s->serial) {
hw/xen_disk.c +1 -0
index 0c298af..7d4144c 100644
@@ -682,6 +682,7 @@ static int blk_init(struct XenDevice *xendev)
         xen_be_printf(&blkdev->xendev, 2, "get configured bdrv (cmdline setup)\n");
         blkdev->bs = blkdev->dinfo->bdrv;
     }
+    bdrv_attach_dev_nofail(blkdev->bs, blkdev);
     blkdev->file_blk  = BLOCK_SIZE;
     blkdev->file_size = bdrv_getlength(blkdev->bs);
     if (blkdev->file_size < 0) {
Signature · 1 line
1.7.2.3
#6 lore raw

PATCH 4/55 block: Generalize change_cb() to BlockDevOps

In reply to Markus Armbruster

So we can more easily add device model callbacks.

---
 block.c         |   43 +++++++++++++++++++++----------------------
 block.h         |    9 ++++++---
 block_int.h     |    5 ++---
 hw/ide/core.c   |    6 +++++-
 hw/sd.c         |    6 +++++-
 hw/virtio-blk.c |    6 +++++-
 6 files changed, 44 insertions(+), 31 deletions(-)
block.c +21 -22
index 1429028..2a1f7af 100644
@@ -43,6 +43,7 @@
 #include <windows.h>
 #endif
 
+static void bdrv_dev_change_cb(BlockDriverState *bs, int reason);
 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque);
@@ -636,10 +637,8 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
     }
 
     if (!bdrv_key_required(bs)) {
-        /* call the change callback */
         bs->media_changed = 1;
-        if (bs->change_cb)
-            bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
+        bdrv_dev_change_cb(bs, CHANGE_MEDIA);
     }
 
     return 0;
@@ -675,10 +674,8 @@ void bdrv_close(BlockDriverState *bs)
             bdrv_close(bs->file);
         }
 
-        /* call the change callback */
         bs->media_changed = 1;
-        if (bs->change_cb)
-            bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
+        bdrv_dev_change_cb(bs, CHANGE_MEDIA);
     }
 }
 
@@ -740,8 +737,8 @@ void bdrv_detach_dev(BlockDriverState *bs, void *dev)
 {
     assert(bs->dev == dev);
     bs->dev = NULL;
-    bs->change_cb = NULL;
-    bs->change_opaque = NULL;
+    bs->dev_ops = NULL;
+    bs->dev_opaque = NULL;
 }
 
 /* TODO change to return DeviceState * when all users are qdevified */
@@ -750,6 +747,20 @@ void *bdrv_get_attached_dev(BlockDriverState *bs)
     return bs->dev;
 }
 
+void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
+                      void *opaque)
+{
+    bs->dev_ops = ops;
+    bs->dev_opaque = opaque;
+}
+
+static void bdrv_dev_change_cb(BlockDriverState *bs, int reason)
+{
+    if (bs->dev_ops && bs->dev_ops->change_cb) {
+        bs->dev_ops->change_cb(bs->dev_opaque, reason);
+    }
+}
+
 /*
  * Run consistency checks on an image
  *
@@ -1152,9 +1163,7 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset)
     ret = drv->bdrv_truncate(bs, offset);
     if (ret == 0) {
         ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
-        if (bs->change_cb) {
-            bs->change_cb(bs->change_opaque, CHANGE_SIZE);
-        }
+        bdrv_dev_change_cb(bs, CHANGE_SIZE);
     }
     return ret;
 }
@@ -1473,15 +1482,6 @@ int bdrv_enable_write_cache(BlockDriverState *bs)
     return bs->enable_write_cache;
 }
 
-/* XXX: no longer used */
-void bdrv_set_change_cb(BlockDriverState *bs,
-                        void (*change_cb)(void *opaque, int reason),
-                        void *opaque)
-{
-    bs->change_cb = change_cb;
-    bs->change_opaque = opaque;
-}
-
 int bdrv_is_encrypted(BlockDriverState *bs)
 {
     if (bs->backing_hd && bs->backing_hd->encrypted)
@@ -1520,8 +1520,7 @@ int bdrv_set_key(BlockDriverState *bs, const char *key)
         bs->valid_key = 1;
         /* call the change callback now, we skipped it on open */
         bs->media_changed = 1;
-        if (bs->change_cb)
-            bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
+        bdrv_dev_change_cb(bs, CHANGE_MEDIA);
     }
     return ret;
 }
block.h +6 -3
index 8d8172c..623c488 100644
@@ -27,6 +27,10 @@ typedef struct QEMUSnapshotInfo {
     uint64_t vm_clock_nsec; /* VM clock relative to boot */
 } QEMUSnapshotInfo;
 
+typedef struct BlockDevOps {
+    void (*change_cb)(void *opaque, int reason);
+} BlockDevOps;
+
 #define BDRV_O_RDWR        0x0002
 #define BDRV_O_SNAPSHOT    0x0008 /* open the file read only and save writes in a snapshot */
 #define BDRV_O_NOCACHE     0x0020 /* do not use the host page cache */
@@ -76,6 +80,8 @@ int bdrv_attach_dev(BlockDriverState *bs, void *dev);
 void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev);
 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);
 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
               uint8_t *buf, int nb_sectors);
 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
@@ -188,9 +194,6 @@ int bdrv_media_changed(BlockDriverState *bs);
 int bdrv_is_locked(BlockDriverState *bs);
 void bdrv_set_locked(BlockDriverState *bs, int locked);
 int bdrv_eject(BlockDriverState *bs, int eject_flag);
-void bdrv_set_change_cb(BlockDriverState *bs,
-                        void (*change_cb)(void *opaque, int reason),
-                        void *opaque);
 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size);
 BlockDriverState *bdrv_find(const char *name);
 BlockDriverState *bdrv_next(BlockDriverState *bs);
block_int.h +2 -3
index 1a23d8a..e2fa254 100644
@@ -152,15 +152,14 @@ struct BlockDriverState {
     int encrypted; /* if true, the media is encrypted */
     int valid_key; /* if true, a valid encryption key has been set */
     int sg;        /* if true, the device is a /dev/sg* */
-    /* event callback when inserting/removing */
-    void (*change_cb)(void *opaque, int reason);
-    void *change_opaque;
 
     BlockDriver *drv; /* NULL means no media */
     void *opaque;
 
     void *dev;                  /* attached device model, if any */
     /* TODO change to DeviceState when all users are qdevified */
+    const BlockDevOps *dev_ops;
+    void *dev_opaque;
 
     char filename[1024];
     char backing_file[1024]; /* if non zero, the image is a diff of
hw/ide/core.c +5 -1
index bcef8bc..773b7cf 100644
@@ -1717,6 +1717,10 @@ void ide_bus_reset(IDEBus *bus)
     bus->dma->ops->reset(bus->dma);
 }
 
+static const BlockDevOps ide_cd_block_ops = {
+    .change_cb = cdrom_change_cb,
+};
+
 int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind,
                    const char *version, const char *serial)
 {
@@ -1751,7 +1755,7 @@ int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind,
     s->smart_errors = 0;
     s->smart_selftest_count = 0;
     if (kind == IDE_CD) {
-        bdrv_set_change_cb(bs, cdrom_change_cb, s);
+        bdrv_set_dev_ops(bs, &ide_cd_block_ops, s);
         bs->buffer_alignment = 2048;
     } else {
         if (!bdrv_is_inserted(s->bs)) {
hw/sd.c +5 -1
index 3fdd5b0..94cea3d 100644
@@ -437,6 +437,10 @@ static void sd_cardchange(void *opaque, int reason)
     }
 }
 
+static const BlockDevOps sd_block_ops = {
+    .change_cb = sd_cardchange,
+};
+
 /* We do not model the chip select pin, so allow the board to select
    whether card should be in SSI or MMC/SD mode.  It is also up to the
    board to ensure that ssi transfers only occur when the chip select
@@ -452,7 +456,7 @@ SDState *sd_init(BlockDriverState *bs, int is_spi)
     sd_reset(sd, bs);
     if (sd->bdrv) {
         bdrv_attach_dev_nofail(sd->bdrv, sd);
-        bdrv_set_change_cb(sd->bdrv, sd_cardchange, sd);
+        bdrv_set_dev_ops(sd->bdrv, &sd_block_ops, sd);
     }
     return sd;
 }
hw/virtio-blk.c +5 -1
index 6471ac8..9e5ef88 100644
@@ -536,6 +536,10 @@ static void virtio_blk_change_cb(void *opaque, int reason)
     }
 }
 
+static const BlockDevOps virtio_block_ops = {
+    .change_cb = virtio_blk_change_cb,
+};
+
 VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf,
                               char **serial)
 {
@@ -582,7 +586,7 @@ VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf,
     register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
                     virtio_blk_save, virtio_blk_load, s);
     bdrv_set_removable(s->bs, 0);
-    bdrv_set_change_cb(s->bs, virtio_blk_change_cb, s);
+    bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
     s->bs->buffer_alignment = conf->logical_block_size;
 
     add_boot_device_path(conf->bootindex, dev, "/disk@0,0");
Signature · 1 line
1.7.2.3
#8 lore raw

PATCH 5/55 block: Split change_cb() into change_media_cb(), resize_cb()

In reply to Markus Armbruster

Multiplexing callbacks complicates matters needlessly.

---
 block.c         |   23 +++++++++++++++--------
 block.h         |   12 +++++++++++-
 block_int.h     |    3 ---
 hw/ide/core.c   |    8 ++------
 hw/sd.c         |    8 ++------
 hw/virtio-blk.c |    8 +++-----
 6 files changed, 33 insertions(+), 29 deletions(-)
block.c +15 -8
index 2a1f7af..fab766a 100644
@@ -43,7 +43,7 @@
 #include <windows.h>
 #endif
 
-static void bdrv_dev_change_cb(BlockDriverState *bs, int reason);
+static void bdrv_dev_change_media_cb(BlockDriverState *bs);
 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque);
@@ -638,7 +638,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
 
     if (!bdrv_key_required(bs)) {
         bs->media_changed = 1;
-        bdrv_dev_change_cb(bs, CHANGE_MEDIA);
+        bdrv_dev_change_media_cb(bs);
     }
 
     return 0;
@@ -675,7 +675,7 @@ void bdrv_close(BlockDriverState *bs)
         }
 
         bs->media_changed = 1;
-        bdrv_dev_change_cb(bs, CHANGE_MEDIA);
+        bdrv_dev_change_media_cb(bs);
     }
 }
 
@@ -754,10 +754,17 @@ void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
     bs->dev_opaque = opaque;
 }
 
-static void bdrv_dev_change_cb(BlockDriverState *bs, int reason)
+static void bdrv_dev_change_media_cb(BlockDriverState *bs)
 {
-    if (bs->dev_ops && bs->dev_ops->change_cb) {
-        bs->dev_ops->change_cb(bs->dev_opaque, reason);
+    if (bs->dev_ops && bs->dev_ops->change_media_cb) {
+        bs->dev_ops->change_media_cb(bs->dev_opaque);
+    }
+}
+
+static void bdrv_dev_resize_cb(BlockDriverState *bs)
+{
+    if (bs->dev_ops && bs->dev_ops->resize_cb) {
+        bs->dev_ops->resize_cb(bs->dev_opaque);
     }
 }
 
@@ -1163,7 +1170,7 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset)
     ret = drv->bdrv_truncate(bs, offset);
     if (ret == 0) {
         ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
-        bdrv_dev_change_cb(bs, CHANGE_SIZE);
+        bdrv_dev_resize_cb(bs);
     }
     return ret;
 }
@@ -1520,7 +1527,7 @@ int bdrv_set_key(BlockDriverState *bs, const char *key)
         bs->valid_key = 1;
         /* call the change callback now, we skipped it on open */
         bs->media_changed = 1;
-        bdrv_dev_change_cb(bs, CHANGE_MEDIA);
+        bdrv_dev_change_media_cb(bs);
     }
     return ret;
 }
block.h +11 -1
index 623c488..89f75e3 100644
@@ -27,8 +27,18 @@ typedef struct QEMUSnapshotInfo {
     uint64_t vm_clock_nsec; /* VM clock relative to boot */
 } QEMUSnapshotInfo;
 
+/* Callbacks for block device models */
 typedef struct BlockDevOps {
-    void (*change_cb)(void *opaque, int reason);
+    /*
+     * Runs when virtual media changed (monitor commands eject, change)
+     * Beware: doesn't run when a host device's physical media
+     * changes.  Sure would be useful if it did.
+     */
+    void (*change_media_cb)(void *opaque);
+    /*
+     * Runs when the size changed (e.g. monitor command block_resize)
+     */
+    void (*resize_cb)(void *opaque);
 } BlockDevOps;
 
 #define BDRV_O_RDWR        0x0002
block_int.h +0 -3
index e2fa254..f5d67c1 100644
@@ -203,9 +203,6 @@ struct BlockDriverState {
     void *private;
 };
 
-#define CHANGE_MEDIA    0x01
-#define CHANGE_SIZE     0x02
-
 struct BlockDriverAIOCB {
     AIOPool *pool;
     BlockDriverState *bs;
hw/ide/core.c +2 -6
index 773b7cf..ec043a3 100644
@@ -758,15 +758,11 @@ static void ide_cfata_metadata_write(IDEState *s)
 }
 
 /* called when the inserted state of the media has changed */
-static void cdrom_change_cb(void *opaque, int reason)
+static void ide_cd_change_cb(void *opaque)
 {
     IDEState *s = opaque;
     uint64_t nb_sectors;
 
-    if (!(reason & CHANGE_MEDIA)) {
-        return;
-    }
-
     bdrv_get_geometry(s->bs, &nb_sectors);
     s->nb_sectors = nb_sectors;
 
@@ -1718,7 +1714,7 @@ void ide_bus_reset(IDEBus *bus)
 }
 
 static const BlockDevOps ide_cd_block_ops = {
-    .change_cb = cdrom_change_cb,
+    .change_media_cb = ide_cd_change_cb,
 };
 
 int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind,
hw/sd.c +2 -6
index 94cea3d..4dc9047 100644
@@ -422,14 +422,10 @@ static void sd_reset(SDState *sd, BlockDriverState *bdrv)
     sd->pwd_len = 0;
 }
 
-static void sd_cardchange(void *opaque, int reason)
+static void sd_cardchange(void *opaque)
 {
     SDState *sd = opaque;
 
-    if (!(reason & CHANGE_MEDIA)) {
-        return;
-    }
-
     qemu_set_irq(sd->inserted_cb, bdrv_is_inserted(sd->bdrv));
     if (bdrv_is_inserted(sd->bdrv)) {
         sd_reset(sd, sd->bdrv);
@@ -438,7 +434,7 @@ static void sd_cardchange(void *opaque, int reason)
 }
 
 static const BlockDevOps sd_block_ops = {
-    .change_cb = sd_cardchange,
+    .change_media_cb = sd_cardchange,
 };
 
 /* We do not model the chip select pin, so allow the board to select
hw/virtio-blk.c +3 -5
index 9e5ef88..dc0b692 100644
@@ -527,17 +527,15 @@ static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
     return 0;
 }
 
-static void virtio_blk_change_cb(void *opaque, int reason)
+static void virtio_blk_resize(void *opaque)
 {
     VirtIOBlock *s = opaque;
 
-    if (reason & CHANGE_SIZE) {
-        virtio_notify_config(&s->vdev);
-    }
+    virtio_notify_config(&s->vdev);
 }
 
 static const BlockDevOps virtio_block_ops = {
-    .change_cb = virtio_blk_change_cb,
+    .resize_cb = virtio_blk_resize,
 };
 
 VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf,
Signature · 1 line
1.7.2.3
#10 lore raw

PATCH 6/55 block/raw-win32: Drop disabled code for removable host devices

In reply to Markus Armbruster

It's been disabled since the start (commit 19cb3738, Aug 2006), and has been untouched except for spelling fixes and such. I don't feel like dragging it along any further.

---
 block/raw-win32.c |   35 -----------------------------------
 1 files changed, 0 insertions(+), 35 deletions(-)
block/raw-win32.c +0 -35
index 56bd719..17cc540 100644
@@ -366,41 +366,6 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
     return 0;
 }
 
-#if 0
-/***********************************************/
-/* removable device additional commands */
-
-static int raw_is_inserted(BlockDriverState *bs)
-{
-    return 1;
-}
-
-static int raw_media_changed(BlockDriverState *bs)
-{
-    return -ENOTSUP;
-}
-
-static int raw_eject(BlockDriverState *bs, int eject_flag)
-{
-    DWORD ret_count;
-
-    if (s->type == FTYPE_FILE)
-        return -ENOTSUP;
-    if (eject_flag) {
-        DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
-                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
-    } else {
-        DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
-                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
-    }
-}
-
-static int raw_set_locked(BlockDriverState *bs, int locked)
-{
-    return -ENOTSUP;
-}
-#endif
-
 static int hdev_has_zero_init(BlockDriverState *bs)
 {
     return 0;
Signature · 1 line
1.7.2.3
#11 lore raw

Re: [Qemu-devel] [PATCH 06/55] block/raw-win32: Drop disabled code for removable host devices

Quoted 5 lines · Markus Armbruster

It's been disabled since the start (commit 19cb3738, Aug 2006), and has been untouched except for spelling fixes and such. I don't feel like dragging it along any further.

Ok, let's nuke it.

#12 lore raw

PATCH 7/55 block: Make BlockDriver method bdrv_set_locked() return void

In reply to Markus Armbruster

The only caller is bdrv_set_locked(), and it ignores the value.

Callees always return 0, except for FreeBSD's cdrom_set_locked(), which returns -ENOTSUP when the device is in a terminally wedged state.

---
 block/raw-posix.c |   10 +++-------
 block/raw.c       |    3 +--
 block_int.h       |    2 +-
 3 files changed, 5 insertions(+), 10 deletions(-)
block/raw-posix.c +3 -7
index 34b64aa..14f8f62 100644
@@ -1346,7 +1346,7 @@ static int cdrom_eject(BlockDriverState *bs, int eject_flag)
     return 0;
 }
 
-static int cdrom_set_locked(BlockDriverState *bs, int locked)
+static void cdrom_set_locked(BlockDriverState *bs, int locked)
 {
     BDRVRawState *s = bs->opaque;
 
@@ -1357,8 +1357,6 @@ static int cdrom_set_locked(BlockDriverState *bs, int locked)
          */
         /* perror("CDROM_LOCKDOOR"); */
     }
-
-    return 0;
 }
 
 static BlockDriver bdrv_host_cdrom = {
@@ -1467,12 +1465,12 @@ static int cdrom_eject(BlockDriverState *bs, int eject_flag)
     return 0;
 }
 
-static int cdrom_set_locked(BlockDriverState *bs, int locked)
+static void cdrom_set_locked(BlockDriverState *bs, int locked)
 {
     BDRVRawState *s = bs->opaque;
 
     if (s->fd < 0)
-        return -ENOTSUP;
+        return;
     if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
         /*
          * Note: an error can happen if the distribution automatically
@@ -1480,8 +1478,6 @@ static int cdrom_set_locked(BlockDriverState *bs, int locked)
          */
         /* perror("CDROM_LOCKDOOR"); */
     }
-
-    return 0;
 }
 
 static BlockDriver bdrv_host_cdrom = {
block/raw.c +1 -2
index b0f72d6..1398a9c 100644
@@ -80,10 +80,9 @@ static int raw_eject(BlockDriverState *bs, int eject_flag)
     return bdrv_eject(bs->file, eject_flag);
 }
 
-static int raw_set_locked(BlockDriverState *bs, int locked)
+static void raw_set_locked(BlockDriverState *bs, int locked)
 {
     bdrv_set_locked(bs->file, locked);
-    return 0;
 }
 
 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
block_int.h +1 -1
index f5d67c1..69cf222 100644
@@ -111,7 +111,7 @@ struct BlockDriver {
     int (*bdrv_is_inserted)(BlockDriverState *bs);
     int (*bdrv_media_changed)(BlockDriverState *bs);
     int (*bdrv_eject)(BlockDriverState *bs, int eject_flag);
-    int (*bdrv_set_locked)(BlockDriverState *bs, int locked);
+    void (*bdrv_set_locked)(BlockDriverState *bs, int locked);
 
     /* to control generic scsi devices */
     int (*bdrv_ioctl)(BlockDriverState *bs, unsigned long int req, void *buf);
Signature · 1 line
1.7.2.3

A message that is not in the archive (sent off-list or not yet received): 20110721111134.787a83af@doriath

#14 lore raw

Re: [Qemu-devel] [PATCH 07/55] block: Make BlockDriver method bdrv_set_locked() return void

Quoted 9 lines · Luiz Capitulino
Quoted 5 lines · On Wed, 20 Jul 2011 18:23:41 +0200 Markus Armbruster <[email protected]> wrote:

The only caller is bdrv_set_locked(), and it ignores the value.

Callees always return 0, except for FreeBSD's cdrom_set_locked(), which returns -ENOTSUP when the device is in a terminally wedged state.

Quoted 5 lines

The fact that we're ignoring ioctl() failures caught my attention. Is it because the state we store in bs->locked is what really matters?

Otherwise the right thing to do would be to propagate the error and change callers to handle it.

The only caller is bdrv_set_locked(). Which can't handle it, so it would have to pass it on.

The purpose of bdrv_set_locked() is to synchronize the physical lock (if any) with the virtual lock. Worst that can happen is the physical lock fails to track the virtual one.

bdrv_set_locked()'s callers are device models: ide-cd and scsi-cd. Each one calls it in four places (with all my patches applied):

* Device init: can return failure. Failure makes QEMU refuse to start (-device cold plug), or hot plug fail (device_add).

* Device exit: can't return failure.

* Device post migration: can return failure, makes migration fail. Given the choice, I figure I'd pick an incorrect physical tray lock over a failed migration.

* Guest PREVENT ALLOW MEDIUM REMOVAL: can send error reply to guest. Recommended errors according to MMC-5 are "unit attention errors" (not a good match), "CDB or parameter list validation errors" (worse) and "hardware failures" (no good choices there either).

I doubt adding the error handling is worth our while, but feel free to send patches.

#15 lore raw

Re: [Qemu-devel] [PATCH 07/55] block: Make BlockDriver method bdrv_set_locked() return void

Quoted 36 lines · On Thu, 21 Jul 2011 17:07:17 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 9 lines · Luiz Capitulino
Quoted 5 lines · On Wed, 20 Jul 2011 18:23:41 +0200 Markus Armbruster <[email protected]> wrote:

The only caller is bdrv_set_locked(), and it ignores the value.

Callees always return 0, except for FreeBSD's cdrom_set_locked(), which returns -ENOTSUP when the device is in a terminally wedged state.

Quoted 5 lines

The fact that we're ignoring ioctl() failures caught my attention. Is it because the state we store in bs->locked is what really matters?

Otherwise the right thing to do would be to propagate the error and change callers to handle it.

The only caller is bdrv_set_locked(). Which can't handle it, so it would have to pass it on.

The purpose of bdrv_set_locked() is to synchronize the physical lock (if any) with the virtual lock. Worst that can happen is the physical lock fails to track the virtual one.

bdrv_set_locked()'s callers are device models: ide-cd and scsi-cd. Each one calls it in four places (with all my patches applied):

* Device init: can return failure. Failure makes QEMU refuse to start (-device cold plug), or hot plug fail (device_add).

* Device exit: can't return failure.

* Device post migration: can return failure, makes migration fail. Given the choice, I figure I'd pick an incorrect physical tray lock over a failed migration.

Quoted 7 lines

* Guest PREVENT ALLOW MEDIUM REMOVAL: can send error reply to guest. Recommended errors according to MMC-5 are "unit attention errors" (not a good match), "CDB or parameter list validation errors" (worse) and "hardware failures" (no good choices there either).

I doubt adding the error handling is worth our while, but feel free to send patches.

I can't tell if the problems you mention above (physical lock out of sync with the virtual one and the post migration) will turn out to be real issues. If they do, we'll have to fix it.

#16 lore raw

Re: [Qemu-devel] [PATCH 07/55] block: Make BlockDriver method bdrv_set_locked() return void

In reply to Markus Armbruster

Am 21.07.2011 17:07, schrieb Markus Armbruster:

Quoted 36 lines
Quoted 9 lines · Luiz Capitulino
Quoted 5 lines · On Wed, 20 Jul 2011 18:23:41 +0200 Markus Armbruster <[email protected]> wrote:

The only caller is bdrv_set_locked(), and it ignores the value.

Callees always return 0, except for FreeBSD's cdrom_set_locked(), which returns -ENOTSUP when the device is in a terminally wedged state.

Quoted 5 lines

The fact that we're ignoring ioctl() failures caught my attention. Is it because the state we store in bs->locked is what really matters?

Otherwise the right thing to do would be to propagate the error and change callers to handle it.

The only caller is bdrv_set_locked(). Which can't handle it, so it would have to pass it on.

The purpose of bdrv_set_locked() is to synchronize the physical lock (if any) with the virtual lock. Worst that can happen is the physical lock fails to track the virtual one.

bdrv_set_locked()'s callers are device models: ide-cd and scsi-cd. Each one calls it in four places (with all my patches applied):

* Device init: can return failure. Failure makes QEMU refuse to start (-device cold plug), or hot plug fail (device_add).

* Device exit: can't return failure.

* Device post migration: can return failure, makes migration fail. Given the choice, I figure I'd pick an incorrect physical tray lock over a failed migration.

Quoted 7 lines

* Guest PREVENT ALLOW MEDIUM REMOVAL: can send error reply to guest. Recommended errors according to MMC-5 are "unit attention errors" (not a good match), "CDB or parameter list validation errors" (worse) and "hardware failures" (no good choices there either).

I doubt adding the error handling is worth our while, but feel free to send patches.

I agree with Luiz that it feels a bit strange to remove one of the rare places in qemu that actually return errors, but if you say that the callers can't handle it anyway, well... We can always add it back if we find a device that could make use of it.

Kevin

#17 lore raw

PATCH 8/55 block: Make BlockDriver method bdrv_eject() return void

In reply to Markus Armbruster

Callees always return 0, except for FreeBSD's cdrom_eject(), which returns -ENOTSUP when the device is in a terminally wedged state.

The only caller is bdrv_eject(), and it maps -ENOTSUP to 0 since commit 4be9762a.

---
 block.c           |   17 ++++-------------
 block/raw-posix.c |   16 +++++-----------
 block/raw.c       |    4 ++--
 block_int.h       |    2 +-
 4 files changed, 12 insertions(+), 27 deletions(-)
block.c +4 -13
index fab766a..594bcab 100644
@@ -2768,25 +2768,16 @@ int bdrv_media_changed(BlockDriverState *bs)
 int bdrv_eject(BlockDriverState *bs, int eject_flag)
 {
     BlockDriver *drv = bs->drv;
-    int ret;
 
     if (bs->locked) {
         return -EBUSY;
     }
 
-    if (!drv || !drv->bdrv_eject) {
-        ret = -ENOTSUP;
-    } else {
-        ret = drv->bdrv_eject(bs, eject_flag);
-    }
-    if (ret == -ENOTSUP) {
-        ret = 0;
+    if (drv && drv->bdrv_eject) {
+        drv->bdrv_eject(bs, eject_flag);
     }
-    if (ret >= 0) {
-        bs->tray_open = eject_flag;
-    }
-
-    return ret;
+    bs->tray_open = eject_flag;
+    return 0;
 }
 
 int bdrv_is_locked(BlockDriverState *bs)
block/raw-posix.c +5 -11
index 14f8f62..e8a2fc2 100644
@@ -1239,7 +1239,7 @@ static int floppy_media_changed(BlockDriverState *bs)
     return ret;
 }
 
-static int floppy_eject(BlockDriverState *bs, int eject_flag)
+static void floppy_eject(BlockDriverState *bs, int eject_flag)
 {
     BDRVRawState *s = bs->opaque;
     int fd;
@@ -1254,8 +1254,6 @@ static int floppy_eject(BlockDriverState *bs, int eject_flag)
             perror("FDEJECT");
         close(fd);
     }
-
-    return 0;
 }
 
 static BlockDriver bdrv_host_floppy = {
@@ -1331,7 +1329,7 @@ static int cdrom_is_inserted(BlockDriverState *bs)
     return 0;
 }
 
-static int cdrom_eject(BlockDriverState *bs, int eject_flag)
+static void cdrom_eject(BlockDriverState *bs, int eject_flag)
 {
     BDRVRawState *s = bs->opaque;
 
@@ -1342,8 +1340,6 @@ static int cdrom_eject(BlockDriverState *bs, int eject_flag)
         if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
             perror("CDROMEJECT");
     }
-
-    return 0;
 }
 
 static void cdrom_set_locked(BlockDriverState *bs, int locked)
@@ -1443,12 +1439,12 @@ static int cdrom_is_inserted(BlockDriverState *bs)
     return raw_getlength(bs) > 0;
 }
 
-static int cdrom_eject(BlockDriverState *bs, int eject_flag)
+static void cdrom_eject(BlockDriverState *bs, int eject_flag)
 {
     BDRVRawState *s = bs->opaque;
 
     if (s->fd < 0)
-        return -ENOTSUP;
+        return;
 
     (void) ioctl(s->fd, CDIOCALLOW);
 
@@ -1460,9 +1456,7 @@ static int cdrom_eject(BlockDriverState *bs, int eject_flag)
             perror("CDIOCCLOSE");
     }
 
-    if (cdrom_reopen(bs) < 0)
-        return -ENOTSUP;
-    return 0;
+    cdrom_reopen(bs);
 }
 
 static void cdrom_set_locked(BlockDriverState *bs, int locked)
block/raw.c +2 -2
index 1398a9c..cb6203e 100644
@@ -75,9 +75,9 @@ static int raw_is_inserted(BlockDriverState *bs)
     return bdrv_is_inserted(bs->file);
 }
 
-static int raw_eject(BlockDriverState *bs, int eject_flag)
+static void raw_eject(BlockDriverState *bs, int eject_flag)
 {
-    return bdrv_eject(bs->file, eject_flag);
+    bdrv_eject(bs->file, eject_flag);
 }
 
 static void raw_set_locked(BlockDriverState *bs, int locked)
block_int.h +1 -1
index 69cf222..2d9edcf 100644
@@ -110,7 +110,7 @@ struct BlockDriver {
     /* removable device specific */
     int (*bdrv_is_inserted)(BlockDriverState *bs);
     int (*bdrv_media_changed)(BlockDriverState *bs);
-    int (*bdrv_eject)(BlockDriverState *bs, int eject_flag);
+    void (*bdrv_eject)(BlockDriverState *bs, int eject_flag);
     void (*bdrv_set_locked)(BlockDriverState *bs, int locked);
 
     /* to control generic scsi devices */
Signature · 1 line
1.7.2.3
#18 lore raw

Re: [Qemu-devel] [PATCH 08/55] block: Make BlockDriver method bdrv_eject() return void

Am 20.07.2011 18:23, schrieb Markus Armbruster:

Quoted 7 lines

Callees always return 0, except for FreeBSD's cdrom_eject(), which returns -ENOTSUP when the device is in a terminally wedged state.

The only caller is bdrv_eject(), and it maps -ENOTSUP to 0 since commit 4be9762a.

What about failed ioctls? Currently we only print an error message but still return 0. Is this the right behaviour? Could callers make use of an error return here or would we end up like with bdrv_set_locked() where we can't really communicate the error to the guest?

Kevin

#19 lore raw

Re: [Qemu-devel] [PATCH 08/55] block: Make BlockDriver method bdrv_eject() return void

Quoted 7 lines · Kevin Wolf

Am 20.07.2011 18:23, schrieb Markus Armbruster:

Quoted 6 lines

Callees always return 0, except for FreeBSD's cdrom_eject(), which returns -ENOTSUP when the device is in a terminally wedged state.

The only caller is bdrv_eject(), and it maps -ENOTSUP to 0 since commit 4be9762a.

Quoted 6 lines
Quoted 1 line

What about failed ioctls? Currently we only print an error message but still return 0. Is this the right behaviour? Could callers make use of an error return here or would we end up like with bdrv_set_locked() where we can't really communicate the error to the guest?

Note that I'm not removing any error handling. I don't add any either. The series is long enough...

bdrv_eject()'s purpose is similar to bdrv_set_locked()'s: synchronize the physical tray to the virtual one. Unsurprisingly, our error handling options are also similar.

All bdrv_eject() can do with errors from the BlockDriver method is pass them on.

At this point in the series, bdrv_eject() still returns an error of its
own: -EBUSY when bs->locked. 28/55 moves that check into callers.

bdrv_eject()'s callers are device models: ide-cd and scsi-cd. Each one calls it in three places (with all my patches applied):

* Device init: can return failure. Failure makes QEMU refuse to start (-device cold plug), or hot plug fail (device_add).

* Device post migration: can return failure, makes migration fail. Given the choice, I figure I'd pick an incorrect physical tray lock over a failed migration.

* Guest PREVENT ALLOW MEDIUM REMOVAL: can send error reply to guest.

Does send the appropriate error when the virtual tray is locked.

What sense to send when the ioctl() fails?

How can the ioctl() fail? When the tray is locked, but for that to happen some other process must have interfered with the lock (Bad process! No treat for you! Go away!). We could synchronize the lock again right before the eject, but that merely shrinks the race's window.

I doubt adding the error handling is worth our while, but feel free to send patches.

#20 lore raw

PATCH 9/55 block: Don't let locked flag prevent medium load

In reply to Markus Armbruster

Commit aea2a33c made bdrv_eject() obey the locked flag. Correct for medium eject (eject_flag set), incorrect for medium load (eject_flag clear). See MMC-5 Table 341 "Actions for Lock/Unlock/Eject".

---
 block.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
block.c +1 -1
index 594bcab..6727274 100644
@@ -2769,7 +2769,7 @@ int bdrv_eject(BlockDriverState *bs, int eject_flag)
 {
     BlockDriver *drv = bs->drv;
 
-    if (bs->locked) {
+    if (eject_flag && bs->locked) {
         return -EBUSY;
     }
 
Signature · 1 line
1.7.2.3
#21 lore raw

Re: [Qemu-devel] [PATCH 09/55] block: Don't let locked flag prevent medium load

Am 20.07.2011 18:23, schrieb Markus Armbruster:

Quoted 5 lines

Commit aea2a33c made bdrv_eject() obey the locked flag. Correct for medium eject (eject_flag set), incorrect for medium load (eject_flag clear). See MMC-5 Table 341 "Actions for Lock/Unlock/Eject".

SCSI doesn't really matter here, this is about host state. The comment for bdrv_set_locked only talks about eject, so it's probably right anyway.

Kevin

Quoted 17 lines
---
 block.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
block.c +1 -1
index 594bcab..6727274 100644
@@ -2769,7 +2769,7 @@ int bdrv_eject(BlockDriverState *bs, int eject_flag)
 {
     BlockDriver *drv = bs->drv;
 
-    if (bs->locked) {
+    if (eject_flag && bs->locked) {
         return -EBUSY;
     }
 
#22 lore raw

Re: [Qemu-devel] [PATCH 09/55] block: Don't let locked flag prevent medium load

Quoted 9 lines · Kevin Wolf

Am 20.07.2011 18:23, schrieb Markus Armbruster:

Quoted 5 lines

Commit aea2a33c made bdrv_eject() obey the locked flag. Correct for medium eject (eject_flag set), incorrect for medium load (eject_flag clear). See MMC-5 Table 341 "Actions for Lock/Unlock/Eject".

SCSI doesn't really matter here, this is about host state. The comment for bdrv_set_locked only talks about eject, so it's probably right anyway.

For what it's worth, PATCH 28/55 moves this check to the device models, where MMC-5 clearly applies.

#23 lore raw

PATCH 10/55 ide: Update command code definitions as per ACS-2 Table B.2

In reply to Markus Armbruster

Drop WIN_SRST, it has same value as WIN_DEVICE_RESET.

CFA_IDLEIMMEDIATE isn't specific to CFATA. ACS-2 shows it as a defined command in ATA-1, -2 and -3. Rename to WIN_IDLEIMMEDIATE2.

Turn unused macros into comments. Mark vendor specific, retired, obsolete.

---
 hw/ide/core.c     |    4 +-
 hw/ide/internal.h |  195 +++++++++++++++++++++++++++-------------------------
 2 files changed, 103 insertions(+), 96 deletions(-)
hw/ide/core.c +2 -2
index ec043a3..88ee768 100644
@@ -1104,7 +1104,7 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
     case WIN_STANDBYNOW1:
     case WIN_STANDBYNOW2:
     case WIN_IDLEIMMEDIATE:
-    case CFA_IDLEIMMEDIATE:
+    case WIN_IDLEIMMEDIATE2:
     case WIN_SETIDLE1:
     case WIN_SETIDLE2:
     case WIN_SLEEPNOW1:
@@ -1143,7 +1143,7 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
                           */
         ide_set_irq(s->bus);
         break;
-    case WIN_SRST:
+    case WIN_DEVICE_RESET:
         if (s->drive_kind != IDE_CD)
             goto abort_cmd;
         ide_set_signature(s);
hw/ide/internal.h +101 -94

240 lines not on this page · raw message

Signature · 1 line
1.7.2.3
#24 lore raw

Re: [Qemu-devel] [PATCH 10/55] ide: Update command code definitions as per ACS-2 Table B.2

Quoted 6 lines · Markus Armbruster

Drop WIN_SRST, it has same value as WIN_DEVICE_RESET.

CFA_IDLEIMMEDIATE isn't specific to CFATA. ACS-2 shows it as a defined command in ATA-1, -2 and -3. Rename to WIN_IDLEIMMEDIATE2.

Turn unused macros into comments.

I don't think that part is overly helpful. In general you'd want to keep all defines for protocol headers, just to make it easier to actually use them later on.

#25 lore raw

Re: [Qemu-devel] [PATCH 10/55] ide: Update command code definitions as per ACS-2 Table B.2

Quoted 11 lines · Christoph Hellwig
Quoted 6 lines · Markus Armbruster

Drop WIN_SRST, it has same value as WIN_DEVICE_RESET.

CFA_IDLEIMMEDIATE isn't specific to CFATA. ACS-2 shows it as a defined command in ATA-1, -2 and -3. Rename to WIN_IDLEIMMEDIATE2.

Turn unused macros into comments.

I don't think that part is overly helpful. In general you'd want to keep all defines for protocol headers, just to make it easier to actually use them later on.

Which ones to keep? Just non-obsolete ones? According to which version of the standard?

#26 lore raw

PATCH 11/55 ide: Clean up case label indentation in ide_exec_cmd()

In reply to Markus Armbruster
---
 hw/ide/core.c |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
hw/ide/core.c +6 -6
index 88ee768..fa50692 100644
@@ -954,7 +954,7 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
         s->status = READY_STAT | SEEK_STAT;
         ide_set_irq(s->bus);
         break;
-	case WIN_READ_EXT:
+    case WIN_READ_EXT:
 	lba48 = 1;
     case WIN_READ:
     case WIN_READ_ONCE:
@@ -964,7 +964,7 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
         s->req_nb_sectors = 1;
         ide_sector_read(s);
         break;
-	case WIN_WRITE_EXT:
+    case WIN_WRITE_EXT:
 	lba48 = 1;
     case WIN_WRITE:
     case WIN_WRITE_ONCE:
@@ -977,7 +977,7 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
         ide_transfer_start(s, s->io_buffer, 512, ide_sector_write);
         s->media_changed = 1;
         break;
-	case WIN_MULTREAD_EXT:
+    case WIN_MULTREAD_EXT:
 	lba48 = 1;
     case WIN_MULTREAD:
         if (!s->mult_sectors)
@@ -1002,7 +1002,7 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
         ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_write);
         s->media_changed = 1;
         break;
-	case WIN_READDMA_EXT:
+    case WIN_READDMA_EXT:
 	lba48 = 1;
     case WIN_READDMA:
     case WIN_READDMA_ONCE:
@@ -1011,7 +1011,7 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
 	ide_cmd_lba48_transform(s, lba48);
         ide_sector_start_dma(s, IDE_DMA_READ);
         break;
-	case WIN_WRITEDMA_EXT:
+    case WIN_WRITEDMA_EXT:
 	lba48 = 1;
     case WIN_WRITEDMA:
     case WIN_WRITEDMA_ONCE:
@@ -1236,7 +1236,7 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
         ide_set_irq(s->bus);
         break;
 
-	case WIN_SMART:
+    case WIN_SMART:
 	if (s->drive_kind == IDE_CD)
 		goto abort_cmd;
 	if (s->hcyl != 0xc2 || s->lcyl != 0x4f)
Signature · 1 line
1.7.2.3
#28 lore raw

PATCH 12/55 ide: Fix ATA command READ to set ATAPI signature for CD-ROM

In reply to Markus Armbruster

Must set the ATAPI device signature, see ACS-2 7.36.6 Outputs for PACKET feature set devices.

---
 hw/ide/core.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
hw/ide/core.c +3 -1
index fa50692..f96ebf2 100644
@@ -958,8 +958,10 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
 	lba48 = 1;
     case WIN_READ:
     case WIN_READ_ONCE:
-        if (!s->bs)
+        if (s->drive_kind == IDE_CD) {
+            ide_set_signature(s);
             goto abort_cmd;
+        }
 	ide_cmd_lba48_transform(s, lba48);
         s->req_nb_sectors = 1;
         ide_sector_read(s);
Signature · 1 line
1.7.2.3
#29 lore raw

Re: [Qemu-devel] [PATCH 12/55] ide: Fix ATA command READ to set ATAPI signature for CD-ROM

Quoted 2 lines · Markus Armbruster

Must set the ATAPI device signature, see ACS-2 7.36.6 Outputs for PACKET feature set devices.

Odd but true, even if it's 7.38.2 in my local copy of the ACS spec.

It defintively should be documented in a comment next to the code, given how odd the behaviour is.

#30 lore raw

Re: [Qemu-devel] [PATCH 12/55] ide: Fix ATA command READ to set ATAPI signature for CD-ROM

Quoted 8 lines · Christoph Hellwig
Quoted 2 lines · Markus Armbruster

Must set the ATAPI device signature, see ACS-2 7.36.6 Outputs for PACKET feature set devices.

Odd but true, even if it's 7.38.2 in my local copy of the ACS spec.

It defintively should be documented in a comment next to the code, given how odd the behaviour is.

I can stick in a suitable comment when I respin.

#31 lore raw

PATCH 13/55 ide: Use a table to declare which drive kinds accept each command

In reply to Markus Armbruster

No functional change.

It would be nice to have handler functions in the table, like commit e1a064f9 did for ATAPI. Left for another day.

---
 hw/ide/core.c |  104 +++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 79 insertions(+), 25 deletions(-)
hw/ide/core.c +79 -25
index f96ebf2..d1f7489 100644
@@ -876,6 +876,77 @@ void ide_ioport_write(void *opaque, uint32_t addr, uint32_t val)
     }
 }
 
+#define HD_OK (1u << IDE_HD)
+#define CD_OK (1u << IDE_CD)
+#define CFA_OK (1u << IDE_CFATA)
+#define HD_CFA_OK (HD_OK | CFA_OK)
+#define ALL_OK (HD_OK | CD_OK | CFA_OK)
+
+/* See ACS-2 T13/2015-D Table B.2 Command codes */
+uint8_t ide_cmd_table[0x100] = {
+    /* NOP not implemented, mandatory for CD */
+    [CFA_REQ_EXT_ERROR_CODE]            = CFA_OK,
+    [WIN_DSM]                           = ALL_OK,
+    [WIN_DEVICE_RESET]                  = CD_OK,
+    [WIN_RECAL]                         = ALL_OK,
+    [WIN_READ]                          = ALL_OK,
+    [WIN_READ_ONCE]                     = ALL_OK,
+    [WIN_READ_EXT]                      = ALL_OK,
+    [WIN_READDMA_EXT]                   = ALL_OK,
+    [WIN_READ_NATIVE_MAX_EXT]           = ALL_OK,
+    [WIN_MULTREAD_EXT]                  = ALL_OK,
+    [WIN_WRITE]                         = ALL_OK,
+    [WIN_WRITE_ONCE]                    = ALL_OK,
+    [WIN_WRITE_EXT]                     = ALL_OK,
+    [WIN_WRITEDMA_EXT]                  = ALL_OK,
+    [CFA_WRITE_SECT_WO_ERASE]           = ALL_OK,
+    [WIN_MULTWRITE_EXT]                 = ALL_OK,
+    [WIN_WRITE_VERIFY]                  = ALL_OK,
+    [WIN_VERIFY]                        = ALL_OK,
+    [WIN_VERIFY_ONCE]                   = ALL_OK,
+    [WIN_VERIFY_EXT]                    = ALL_OK,
+    [WIN_SEEK]                          = HD_CFA_OK,
+    [CFA_TRANSLATE_SECTOR]              = CFA_OK,
+    [WIN_DIAGNOSE]                      = ALL_OK,
+    [WIN_SPECIFY]                       = ALL_OK,
+    [WIN_STANDBYNOW2]                   = ALL_OK,
+    [WIN_IDLEIMMEDIATE2]                = ALL_OK,
+    [WIN_STANDBY2]                      = ALL_OK,
+    [WIN_SETIDLE2]                      = ALL_OK,
+    [WIN_CHECKPOWERMODE2]               = ALL_OK,
+    [WIN_SLEEPNOW2]                     = ALL_OK,
+    [WIN_PACKETCMD]                     = CD_OK,
+    [WIN_PIDENTIFY]                     = CD_OK,
+    [WIN_SMART]                         = HD_CFA_OK,
+    [CFA_ACCESS_METADATA_STORAGE]       = CFA_OK,
+    [CFA_ERASE_SECTORS]                 = CFA_OK,
+    [WIN_MULTREAD]                      = ALL_OK,
+    [WIN_MULTWRITE]                     = ALL_OK,
+    [WIN_SETMULT]                       = ALL_OK,
+    [WIN_READDMA]                       = ALL_OK,
+    [WIN_READDMA_ONCE]                  = ALL_OK,
+    [WIN_WRITEDMA]                      = ALL_OK,
+    [WIN_WRITEDMA_ONCE]                 = ALL_OK,
+    [CFA_WRITE_MULTI_WO_ERASE]          = ALL_OK,
+    [WIN_STANDBYNOW1]                   = ALL_OK,
+    [WIN_IDLEIMMEDIATE]                 = ALL_OK,
+    [WIN_STANDBY]                       = ALL_OK,
+    [WIN_SETIDLE1]                      = ALL_OK,
+    [WIN_CHECKPOWERMODE1]               = ALL_OK,
+    [WIN_SLEEPNOW1]                     = ALL_OK,
+    [WIN_FLUSH_CACHE]                   = ALL_OK,
+    [WIN_FLUSH_CACHE_EXT]               = ALL_OK,
+    [WIN_IDENTIFY]                      = ALL_OK,
+    [WIN_SETFEATURES]                   = ALL_OK,
+    [IBM_SENSE_CONDITION]               = CFA_OK,
+    [CFA_WEAR_LEVEL]                    = CFA_OK,
+    [WIN_READ_NATIVE_MAX]               = ALL_OK,
+};
+
+static bool ide_cmd_permitted(IDEState *s, uint32_t cmd)
+{
+    return cmd <= 0xff && (ide_cmd_table[cmd] & (1u << s->drive_kind));
+}
 
 void ide_exec_cmd(IDEBus *bus, uint32_t val)
 {
@@ -895,6 +966,10 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
     if ((s->status & (BUSY_STAT|DRQ_STAT)) && val != WIN_DEVICE_RESET)
         return;
 
+    if (!ide_cmd_permitted(s, val)) {
+        goto abort_cmd;
+    }
+
     switch(val) {
     case WIN_DSM:
         switch (s->feature) {
@@ -1115,21 +1190,15 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
         ide_set_irq(s->bus);
         break;
     case WIN_SEEK:
-        if(s->drive_kind == IDE_CD)
-            goto abort_cmd;
         /* XXX: Check that seek is within bounds */
         s->status = READY_STAT | SEEK_STAT;
         ide_set_irq(s->bus);
         break;
         /* ATAPI commands */
     case WIN_PIDENTIFY:
-        if (s->drive_kind == IDE_CD) {
-            ide_atapi_identify(s);
-            s->status = READY_STAT | SEEK_STAT;
-            ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
-        } else {
-            ide_abort_command(s);
-        }
+        ide_atapi_identify(s);
+        s->status = READY_STAT | SEEK_STAT;
+        ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
         ide_set_irq(s->bus);
         break;
     case WIN_DIAGNOSE:
@@ -1146,15 +1215,11 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
         ide_set_irq(s->bus);
         break;
     case WIN_DEVICE_RESET:
-        if (s->drive_kind != IDE_CD)
-            goto abort_cmd;
         ide_set_signature(s);
         s->status = 0x00; /* NOTE: READY is _not_ set */
         s->error = 0x01;
         break;
     case WIN_PACKETCMD:
-        if (s->drive_kind != IDE_CD)
-            goto abort_cmd;
         /* overlapping commands not supported */
         if (s->feature & 0x02)
             goto abort_cmd;
@@ -1166,16 +1231,12 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
         break;
     /* CF-ATA commands */
     case CFA_REQ_EXT_ERROR_CODE:
-        if (s->drive_kind != IDE_CFATA)
-            goto abort_cmd;
         s->error = 0x09;    /* miscellaneous error */
         s->status = READY_STAT | SEEK_STAT;
         ide_set_irq(s->bus);
         break;
     case CFA_ERASE_SECTORS:
     case CFA_WEAR_LEVEL:
-        if (s->drive_kind != IDE_CFATA)
-            goto abort_cmd;
         if (val == CFA_WEAR_LEVEL)
             s->nsector = 0;
         if (val == CFA_ERASE_SECTORS)
@@ -1185,8 +1246,6 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
         ide_set_irq(s->bus);
         break;
     case CFA_TRANSLATE_SECTOR:
-        if (s->drive_kind != IDE_CFATA)
-            goto abort_cmd;
         s->error = 0x00;
         s->status = READY_STAT | SEEK_STAT;
         memset(s->io_buffer, 0, 0x200);
@@ -1205,8 +1264,6 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
         ide_set_irq(s->bus);
         break;
     case CFA_ACCESS_METADATA_STORAGE:
-        if (s->drive_kind != IDE_CFATA)
-            goto abort_cmd;
         switch (s->feature) {
         case 0x02:	/* Inquiry Metadata Storage */
             ide_cfata_metadata_inquiry(s);
@@ -1225,8 +1282,6 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
         ide_set_irq(s->bus);
         break;
     case IBM_SENSE_CONDITION:
-        if (s->drive_kind != IDE_CFATA)
-            goto abort_cmd;
         switch (s->feature) {
         case 0x01:  /* sense temperature in device */
             s->nsector = 0x50;      /* +20 C */
@@ -1239,8 +1294,6 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
         break;
 
     case WIN_SMART:
-	if (s->drive_kind == IDE_CD)
-		goto abort_cmd;
 	if (s->hcyl != 0xc2 || s->lcyl != 0x4f)
 		goto abort_cmd;
 	if (!s->smart_enabled && s->feature != SMART_ENABLE)
@@ -1395,6 +1448,7 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
 	}
 	break;
     default:
+        /* should not be reachable */
     abort_cmd:
         ide_abort_command(s);
         ide_set_irq(s->bus);
Signature · 1 line
1.7.2.3
Reviewed-by on v1 13/55
#32 lore raw

Re: [Qemu-devel] [PATCH 13/55] ide: Use a table to declare which drive kinds accept each command

Quoted 4 lines · Markus Armbruster

No functional change.

It would be nice to have handler functions in the table, like commit e1a064f9 did for ATAPI. Left for another day.

Looks good,

#33 lore raw

PATCH 14/55 ide: Reject ATA commands specific to drive kinds

In reply to Markus Armbruster

ACS-2 Table B.2 explicitly prohibits ATAPI devices from implementing WIN_RECAL, WIN_READ_EXT, WIN_READDMA_EXT, WIN_READ_NATIVE_MAX, WIN_MULTREAD_EXT, WIN_WRITE, WIN_WRITE_ONCE, WIN_WRITE_EXT, WIN_WRITEDMA_EXT, WIN_MULTWRITE_EXT, WIN_WRITE_VERIFY, WIN_VERIFY, WIN_VERIFY_ONCE, WIN_VERIFY_EXT, WIN_SPECIFY, WIN_MULTREAD, WIN_MULTWRITE, WIN_SETMULT, WIN_READDMA, WIN_READDMA_ONCE, WIN_WRITEDMA, WIN_WRITEDMA_ONCE, WIN_FLUSH_CACHE_EXT. Restrict them to IDE_HD and IDE_CFATA.

Same for CFA_WRITE_SECT_WO_ERASE, CFA_WRITE_MULTI_WO_ERASE. Restrict them to IDE_CFATA, like the other CFA_ commands.

---
 hw/ide/core.c |   50 +++++++++++++++++++++++++-------------------------
 1 files changed, 25 insertions(+), 25 deletions(-)
hw/ide/core.c +25 -25
index d1f7489..c8cfd67 100644
@@ -888,27 +888,27 @@ uint8_t ide_cmd_table[0x100] = {
     [CFA_REQ_EXT_ERROR_CODE]            = CFA_OK,
     [WIN_DSM]                           = ALL_OK,
     [WIN_DEVICE_RESET]                  = CD_OK,
-    [WIN_RECAL]                         = ALL_OK,
+    [WIN_RECAL]                         = HD_CFA_OK,
     [WIN_READ]                          = ALL_OK,
     [WIN_READ_ONCE]                     = ALL_OK,
-    [WIN_READ_EXT]                      = ALL_OK,
-    [WIN_READDMA_EXT]                   = ALL_OK,
-    [WIN_READ_NATIVE_MAX_EXT]           = ALL_OK,
-    [WIN_MULTREAD_EXT]                  = ALL_OK,
-    [WIN_WRITE]                         = ALL_OK,
-    [WIN_WRITE_ONCE]                    = ALL_OK,
-    [WIN_WRITE_EXT]                     = ALL_OK,
-    [WIN_WRITEDMA_EXT]                  = ALL_OK,
-    [CFA_WRITE_SECT_WO_ERASE]           = ALL_OK,
-    [WIN_MULTWRITE_EXT]                 = ALL_OK,
-    [WIN_WRITE_VERIFY]                  = ALL_OK,
-    [WIN_VERIFY]                        = ALL_OK,
-    [WIN_VERIFY_ONCE]                   = ALL_OK,
-    [WIN_VERIFY_EXT]                    = ALL_OK,
+    [WIN_READ_EXT]                      = HD_CFA_OK,
+    [WIN_READDMA_EXT]                   = HD_CFA_OK,
+    [WIN_READ_NATIVE_MAX_EXT]           = HD_CFA_OK,
+    [WIN_MULTREAD_EXT]                  = HD_CFA_OK,
+    [WIN_WRITE]                         = HD_CFA_OK,
+    [WIN_WRITE_ONCE]                    = HD_CFA_OK,
+    [WIN_WRITE_EXT]                     = HD_CFA_OK,
+    [WIN_WRITEDMA_EXT]                  = HD_CFA_OK,
+    [CFA_WRITE_SECT_WO_ERASE]           = CFA_OK,
+    [WIN_MULTWRITE_EXT]                 = HD_CFA_OK,
+    [WIN_WRITE_VERIFY]                  = HD_CFA_OK,
+    [WIN_VERIFY]                        = HD_CFA_OK,
+    [WIN_VERIFY_ONCE]                   = HD_CFA_OK,
+    [WIN_VERIFY_EXT]                    = HD_CFA_OK,
     [WIN_SEEK]                          = HD_CFA_OK,
     [CFA_TRANSLATE_SECTOR]              = CFA_OK,
     [WIN_DIAGNOSE]                      = ALL_OK,
-    [WIN_SPECIFY]                       = ALL_OK,
+    [WIN_SPECIFY]                       = HD_CFA_OK,
     [WIN_STANDBYNOW2]                   = ALL_OK,
     [WIN_IDLEIMMEDIATE2]                = ALL_OK,
     [WIN_STANDBY2]                      = ALL_OK,
@@ -920,14 +920,14 @@ uint8_t ide_cmd_table[0x100] = {
     [WIN_SMART]                         = HD_CFA_OK,
     [CFA_ACCESS_METADATA_STORAGE]       = CFA_OK,
     [CFA_ERASE_SECTORS]                 = CFA_OK,
-    [WIN_MULTREAD]                      = ALL_OK,
-    [WIN_MULTWRITE]                     = ALL_OK,
-    [WIN_SETMULT]                       = ALL_OK,
-    [WIN_READDMA]                       = ALL_OK,
-    [WIN_READDMA_ONCE]                  = ALL_OK,
-    [WIN_WRITEDMA]                      = ALL_OK,
-    [WIN_WRITEDMA_ONCE]                 = ALL_OK,
-    [CFA_WRITE_MULTI_WO_ERASE]          = ALL_OK,
+    [WIN_MULTREAD]                      = HD_CFA_OK,
+    [WIN_MULTWRITE]                     = HD_CFA_OK,
+    [WIN_SETMULT]                       = HD_CFA_OK,
+    [WIN_READDMA]                       = HD_CFA_OK,
+    [WIN_READDMA_ONCE]                  = HD_CFA_OK,
+    [WIN_WRITEDMA]                      = HD_CFA_OK,
+    [WIN_WRITEDMA_ONCE]                 = HD_CFA_OK,
+    [CFA_WRITE_MULTI_WO_ERASE]          = CFA_OK,
     [WIN_STANDBYNOW1]                   = ALL_OK,
     [WIN_IDLEIMMEDIATE]                 = ALL_OK,
     [WIN_STANDBY]                       = ALL_OK,
@@ -935,7 +935,7 @@ uint8_t ide_cmd_table[0x100] = {
     [WIN_CHECKPOWERMODE1]               = ALL_OK,
     [WIN_SLEEPNOW1]                     = ALL_OK,
     [WIN_FLUSH_CACHE]                   = ALL_OK,
-    [WIN_FLUSH_CACHE_EXT]               = ALL_OK,
+    [WIN_FLUSH_CACHE_EXT]               = HD_CFA_OK,
     [WIN_IDENTIFY]                      = ALL_OK,
     [WIN_SETFEATURES]                   = ALL_OK,
     [IBM_SENSE_CONDITION]               = CFA_OK,
Signature · 1 line
1.7.2.3
Reviewed-by on v1 14/55
#34 lore raw

Re: [Qemu-devel] [PATCH 14/55] ide: Reject ATA commands specific to drive kinds

Quoted 11 lines · Markus Armbruster

ACS-2 Table B.2 explicitly prohibits ATAPI devices from implementing WIN_RECAL, WIN_READ_EXT, WIN_READDMA_EXT, WIN_READ_NATIVE_MAX, WIN_MULTREAD_EXT, WIN_WRITE, WIN_WRITE_ONCE, WIN_WRITE_EXT, WIN_WRITEDMA_EXT, WIN_MULTWRITE_EXT, WIN_WRITE_VERIFY, WIN_VERIFY, WIN_VERIFY_ONCE, WIN_VERIFY_EXT, WIN_SPECIFY, WIN_MULTREAD, WIN_MULTWRITE, WIN_SETMULT, WIN_READDMA, WIN_READDMA_ONCE, WIN_WRITEDMA, WIN_WRITEDMA_ONCE, WIN_FLUSH_CACHE_EXT. Restrict them to IDE_HD and IDE_CFATA.

Same for CFA_WRITE_SECT_WO_ERASE, CFA_WRITE_MULTI_WO_ERASE. Restrict them to IDE_CFATA, like the other CFA_ commands.

Looks good,

#35 lore raw

PATCH 15/55 ide/atapi: Clean up misleading name in cmd_start_stop_unit()

In reply to Markus Armbruster

"eject" is misleading; it means "eject" when start is clear, but "load" when start is set. Rename to loej, because that's how MMC-5 calls it, in section 6.40.

---
 hw/ide/atapi.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
hw/ide/atapi.c +4 -4
index fe2fb0b..17fbef8 100644
@@ -892,11 +892,11 @@ static void cmd_seek(IDEState *s, uint8_t* buf)
 
 static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
 {
-    int start, eject, sense, err = 0;
-    start = buf[4] & 1;
-    eject = (buf[4] >> 1) & 1;
+    int sense, err = 0;
+    bool start = buf[4] & 1;
+    bool loej = buf[4] & 2;
 
-    if (eject) {
+    if (loej) {
         err = bdrv_eject(s->bs, !start);
     }
 
Signature · 1 line
1.7.2.3
Reviewed-by on v1 15/55
#36 lore raw

Re: [Qemu-devel] [PATCH 15/55] ide/atapi: Clean up misleading name in cmd_start_stop_unit()

Quoted 3 lines · Markus Armbruster

"eject" is misleading; it means "eject" when start is clear, but "load" when start is set. Rename to loej, because that's how MMC-5 calls it, in section 6.40.

Just as clear as the bdrv_eject name :)

Looks fine anyway,

#37 lore raw

PATCH 16/55 ide/atapi: Track tray open/close state

In reply to Markus Armbruster

We already track it in BlockDriverState since commit 4be9762a. As discussed in that commit's message, we should track it in the device device models instead, because it's device state.

---
 hw/ide/atapi.c    |    4 ++++
 hw/ide/internal.h |    1 +
 2 files changed, 5 insertions(+), 0 deletions(-)
hw/ide/atapi.c +4 -0
index 17fbef8..fee567b 100644
@@ -915,6 +915,10 @@ static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
         ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
         break;
     }
+
+    if (loej && !err) {
+        s->tray_open = !start;
+    }
 }
 
 static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
hw/ide/internal.h +1 -0
index 1f807d4..5cf95c0 100644
@@ -440,6 +440,7 @@ struct IDEState {
     struct unreported_events events;
     uint8_t sense_key;
     uint8_t asc;
+    bool tray_open;
     uint8_t cdrom_changed;
     int packet_transfer_size;
     int elementary_transfer_size;
Signature · 1 line
1.7.2.3
Reviewed-by on v1 16/55
#38 lore raw

Re: [Qemu-devel] [PATCH 16/55] ide/atapi: Track tray open/close state

Quoted 3 lines · Markus Armbruster

We already track it in BlockDriverState since commit 4be9762a. As discussed in that commit's message, we should track it in the device device models instead, because it's device state.

Looks good,

#39 lore raw

PATCH 17/55 ide/atapi: Switch from BlockDriverState's tray_open to own

In reply to Markus Armbruster
---
 hw/ide/atapi.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/ide/atapi.c +1 -1
index fee567b..96f7c31 100644
@@ -510,7 +510,7 @@ static unsigned int event_status_media(IDEState *s,
     uint8_t event_code, media_status;
 
     media_status = 0;
-    if (s->bs->tray_open) {
+    if (s->tray_open) {
         media_status = MS_TRAY_OPEN;
     } else if (bdrv_is_inserted(s->bs)) {
         media_status = MS_MEDIA_PRESENT;
Signature · 1 line
1.7.2.3
#41 lore raw

Re: [Qemu-devel] [PATCH 17/55] ide/atapi: Switch from BlockDriverState's tray_open to own

Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Markus Armbruster

Looks good, although I would have folded this patch into the previous one.

I'll consider squashing when I respin.

#42 lore raw

PATCH 18/55 scsi-disk: Reject CD-specific SCSI commands to disks

In reply to Markus Armbruster

Use a table to declare which drive kinds accept each command.

Limit READ_CAPACITY, READ_TOC, GET_CONFIGURATION to SCSI_CD, as per SPC-4.

It would be nice to have handler functions in the table, like commit e1a064f9 did for ATAPI. Left for another day.

---
 hw/scsi-disk.c |   63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 62 insertions(+), 1 deletions(-)
hw/scsi-disk.c +62 -1
index a8c7372..c4643d0 100644
@@ -991,6 +991,59 @@ illegal_request:
     return -1;
 }
 
+#define HD_OK (1u << SCSI_HD)
+#define CD_OK (1u << SCSI_CD)
+#define ALL_OK (HD_OK | CD_OK)
+
+/* See SPC-4 T10/1731-D Table D.2 Operation codes */
+uint8_t scsi_cmd_table[0x100] = {
+    [TEST_UNIT_READY]           = ALL_OK,
+    [REZERO_UNIT]               = ALL_OK,
+    [REQUEST_SENSE]             = ALL_OK,
+    /* FORMAT_UNIT not implemented, SPC-4 mandatory for HD */
+    [READ_6]                    = ALL_OK,
+    [WRITE_6]                   = ALL_OK,
+    [SEEK_6]                    = ALL_OK,
+    [INQUIRY]                   = ALL_OK,
+    [MODE_SELECT]               = ALL_OK,
+    [RESERVE]                   = ALL_OK,
+    [RELEASE]                   = ALL_OK,
+    [MODE_SENSE]                = ALL_OK,
+    [START_STOP]                = ALL_OK,
+    /* SEND DIAGNOSTIC not implemented, SPC-4 mandatory for HD */
+    [ALLOW_MEDIUM_REMOVAL]      = ALL_OK,
+    [READ_CAPACITY]             = CD_OK,
+    [READ_10]                   = ALL_OK,
+    [WRITE_10]                  = ALL_OK,
+    [SEEK_10]                   = ALL_OK,
+    [WRITE_VERIFY]              = ALL_OK,
+    [VERIFY]                    = ALL_OK,
+    [SYNCHRONIZE_CACHE]         = ALL_OK,
+    [READ_TOC]                  = CD_OK,
+    [GET_CONFIGURATION]         = CD_OK,
+    /* GET EVENT STATUS NOTIFICATION not implemented, SPC-4 mandatory for CD */
+    [MODE_SELECT_10]            = ALL_OK,
+    [RESERVE_10]                = ALL_OK,
+    [RELEASE_10]                = ALL_OK,
+    [MODE_SENSE_10]             = ALL_OK,
+    [READ_16]                   = ALL_OK,
+    [WRITE_16]                  = ALL_OK,
+    [WRITE_VERIFY_16]           = ALL_OK,
+    /* VERIFY(16) not implemented, WRITE_VERIFY(16) is, odd */
+    [WRITE_SAME_16]             = ALL_OK,
+    [SERVICE_ACTION_IN]         = ALL_OK,
+    [REPORT_LUNS]               = ALL_OK,
+    [READ_12]                   = ALL_OK,
+    [WRITE_12]                  = ALL_OK,
+    [WRITE_VERIFY_12]           = ALL_OK,
+    /* VERIFY(12) not implemented, WRITE_VERIFY(12) is, odd */
+};
+
+static bool scsi_cmd_permitted(SCSIDiskState *s, uint8_t cmd)
+{
+    return scsi_cmd_table[cmd] & (1u << s->drive_kind);
+}
+
 /* Execute a scsi command.  Returns the length of the data expected by the
    command.  This will be Positive for data transfers from the device
    (eg. disk reads), negative for transfers to the device (eg. disk writes),
@@ -1033,6 +1086,13 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
             return 0;
         }
     }
+
+    if (!scsi_cmd_permitted(s, command)) {
+        DPRINTF("SCSI command (%2.2x) not valid for device\n", command);
+        scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(INVALID_OPCODE));
+        return 0;
+    }
+
     switch (command) {
     case TEST_UNIT_READY:
     case REQUEST_SENSE:
@@ -1137,7 +1197,8 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
 
         break;
     default:
-        DPRINTF("Unknown SCSI command (%2.2x)\n", buf[0]);
+        /* should not be reachable */
+        BADF("Unknown SCSI command (%2.2x)\n", buf[0]);
         scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(INVALID_OPCODE));
         return 0;
     fail:
Signature · 1 line
1.7.2.3
#43 lore raw

Re: [Qemu-devel] [PATCH 18/55] scsi-disk: Reject CD-specific SCSI commands to disks

Quoted 4 lines · Markus Armbruster

Use a table to declare which drive kinds accept each command.

Limit READ_CAPACITY, READ_TOC, GET_CONFIGURATION to SCSI_CD, as per SPC-4.

READ CAPACITY is defined in SBC, and absolutely required for proper operation with disks.

#44 lore raw

Re: [Qemu-devel] [PATCH 18/55] scsi-disk: Reject CD-specific SCSI commands to disks

Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Markus Armbruster

Use a table to declare which drive kinds accept each command.

Limit READ_CAPACITY, READ_TOC, GET_CONFIGURATION to SCSI_CD, as per SPC-4.

READ CAPACITY is defined in SBC, and absolutely required for proper operation with disks.

Will fix.

#45 lore raw

Re: [Qemu-devel] [PATCH 18/55] scsi-disk: Reject CD-specific SCSI commands to disks

Quoted 5 lines · Markus Armbruster
Quoted 3 lines · Christoph Hellwig
Quoted 2 lines · Markus Armbruster

Use a table to declare which drive kinds accept each command.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Limit READ_CAPACITY, READ_TOC, GET_CONFIGURATION to SCSI_CD, as per SPC-4.

READ CAPACITY is defined in SBC, and absolutely required for proper operation with disks.

Will fix.

Second thoughts: dropping this bug fix patch for now, because it clashes with Hannes's work in progress[*], and the rest of my series doesn't depend on it.

[*] [PATCH 6/6] scsi-disk: Check for supported commands
http://lists.gnu.org/archive/html/qemu-devel/2011-07/msg03082.html

#46 lore raw

PATCH 19/55 scsi-disk: Factor out scsi_disk_emulate_start_stop()

In reply to Markus Armbruster
---
 hw/scsi-disk.c |   17 +++++++++++++----
 1 files changed, 13 insertions(+), 4 deletions(-)
hw/scsi-disk.c +13 -4
index c4643d0..544ecef 100644
@@ -817,6 +817,18 @@ static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
     return toclen;
 }
 
+static void scsi_disk_emulate_start_stop(SCSIDiskReq *r)
+{
+    SCSIRequest *req = &r->req;
+    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
+    bool start = req->cmd.buf[4] & 1;
+    bool loej = req->cmd.buf[4] & 2;
+
+    if (s->drive_kind == SCSI_CD && loej) {
+        bdrv_eject(s->bs, !start);
+    }
+}
+
 static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
 {
     SCSIRequest *req = &r->req;
@@ -870,10 +882,7 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
             goto illegal_request;
         break;
     case START_STOP:
-        if (s->drive_kind == SCSI_CD && (req->cmd.buf[4] & 2)) {
-            /* load/eject medium */
-            bdrv_eject(s->bs, !(req->cmd.buf[4] & 1));
-        }
+        scsi_disk_emulate_start_stop(r);
 	break;
     case ALLOW_MEDIUM_REMOVAL:
         bdrv_set_locked(s->bs, req->cmd.buf[4] & 1);
Signature · 1 line
1.7.2.3
#48 lore raw

PATCH 20/55 scsi-disk: Track tray open/close state

In reply to Markus Armbruster

We already track it in BlockDriverState since commit 4be9762a. As discussed in that commit's message, we should track it in the device device models instead, because it's device state.

---
 hw/scsi-disk.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
hw/scsi-disk.c +2 -0
index 544ecef..fc36327 100644
@@ -74,6 +74,7 @@ struct SCSIDiskState
     char *version;
     char *serial;
     SCSISense sense;
+    bool tray_open;
     SCSIDriveKind drive_kind;
 };
 
@@ -826,6 +827,7 @@ static void scsi_disk_emulate_start_stop(SCSIDiskReq *r)
 
     if (s->drive_kind == SCSI_CD && loej) {
         bdrv_eject(s->bs, !start);
+        s->tray_open = !start;
     }
 }
 
Signature · 1 line
1.7.2.3
Reviewed-by on v1 20/55
#49 lore raw

Re: [Qemu-devel] [PATCH 20/55] scsi-disk: Track tray open/close state

Quoted 3 lines · Markus Armbruster

We already track it in BlockDriverState since commit 4be9762a. As discussed in that commit's message, we should track it in the device device models instead, because it's device state.

Looks good,

#50 lore raw

PATCH 21/55 block: Revert entanglement of bdrv_is_inserted() with tray status

In reply to Markus Armbruster

Commit 4be9762a changed bdrv_is_inserted() to fail when the tray is open. Unfortunately, there are two different kinds of users, with conflicting needs.

1. Device models using bdrv_eject(), currently ide-cd and scsi-cd. They expect bdrv_is_inserted() to reflect the tray status. Commit 4be9762a makes them happy.

2. Code that wants to know whether a BlockDriverState has media, such as find_image_format(), bdrv_flush_all(). Commit 4be9762a makes them unhappy. In particular, it breaks flush on VM stop for media ejected by the guest.

Revert the change to bdrv_is_inserted(). Check the tray status in the device models instead.

Note on IDE: Since only ATAPI devices have a tray, and they don't accept ATA commands since the recent commit "ide: Reject ATA commands specific to drive kinds", checking in atapi.c suffices.

---
 block.c        |    7 +++----
 hw/ide/atapi.c |   15 ++++++++-------
 hw/scsi-disk.c |   12 +++++++++---
 3 files changed, 20 insertions(+), 14 deletions(-)
block.c +3 -4
index 6727274..2b1eb4d 100644
@@ -2734,13 +2734,12 @@ void qemu_aio_release(void *p)
 int bdrv_is_inserted(BlockDriverState *bs)
 {
     BlockDriver *drv = bs->drv;
-    int ret;
+
     if (!drv)
         return 0;
     if (!drv->bdrv_is_inserted)
-        return !bs->tray_open;
-    ret = drv->bdrv_is_inserted(bs);
-    return ret;
+        return 1;
+    return drv->bdrv_is_inserted(bs);
 }
 
 /**
hw/ide/atapi.c +8 -7
index 96f7c31..08e1a7a 100644
@@ -73,7 +73,7 @@ static void lba_to_msf(uint8_t *buf, int lba)
 
 static inline int media_present(IDEState *s)
 {
-    return (s->nb_sectors > 0);
+    return !s->tray_open && s->nb_sectors > 0;
 }
 
 /* XXX: DVDs that could fit on a CD will be reported as a CD */
@@ -1066,20 +1066,21 @@ static const struct {
     [ 0x03 ] = { cmd_request_sense,                 ALLOW_UA },
     [ 0x12 ] = { cmd_inquiry,                       ALLOW_UA },
     [ 0x1a ] = { cmd_mode_sense, /* (6) */          0 },
-    [ 0x1b ] = { cmd_start_stop_unit,               0 },
+    [ 0x1b ] = { cmd_start_stop_unit,               0 }, /* [1] */
     [ 0x1e ] = { cmd_prevent_allow_medium_removal,  0 },
     [ 0x25 ] = { cmd_read_cdvd_capacity,            CHECK_READY },
-    [ 0x28 ] = { cmd_read, /* (10) */               0 },
+    [ 0x28 ] = { cmd_read, /* (10) */               CHECK_READY },
     [ 0x2b ] = { cmd_seek,                          CHECK_READY },
     [ 0x43 ] = { cmd_read_toc_pma_atip,             CHECK_READY },
     [ 0x46 ] = { cmd_get_configuration,             ALLOW_UA },
     [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
     [ 0x5a ] = { cmd_mode_sense, /* (10) */         0 },
-    [ 0xa8 ] = { cmd_read, /* (12) */               0 },
-    [ 0xad ] = { cmd_read_dvd_structure,            0 },
+    [ 0xa8 ] = { cmd_read, /* (12) */               CHECK_READY },
+    [ 0xad ] = { cmd_read_dvd_structure,            CHECK_READY },
     [ 0xbb ] = { cmd_set_speed,                     0 },
     [ 0xbd ] = { cmd_mechanism_status,              0 },
-    [ 0xbe ] = { cmd_read_cd,                       0 },
+    [ 0xbe ] = { cmd_read_cd,                       CHECK_READY },
+    /* [1] handler detects and reports not ready condition itself */
 };
 
 void ide_atapi_cmd(IDEState *s)
@@ -1115,7 +1116,7 @@ void ide_atapi_cmd(IDEState *s)
      * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
      * states rely on this behavior.
      */
-    if (bdrv_is_inserted(s->bs) && s->cdrom_changed) {
+    if (!s->tray_open && bdrv_is_inserted(s->bs) && s->cdrom_changed) {
         ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
 
         s->cdrom_changed = 0;
hw/scsi-disk.c +9 -3
index fc36327..207a868 100644
@@ -189,6 +189,9 @@ static void scsi_read_data(SCSIRequest *req)
     if (n > SCSI_DMA_BUF_SIZE / 512)
         n = SCSI_DMA_BUF_SIZE / 512;
 
+    if (s->tray_open) {
+        scsi_read_complete(r, -ENOMEDIUM);
+    }
     r->iov.iov_len = n * 512;
     qemu_iovec_init_external(&r->qiov, &r->iov, 1);
     r->req.aiocb = bdrv_aio_readv(s->bs, r->sector, &r->qiov, n,
@@ -287,6 +290,9 @@ static void scsi_write_data(SCSIRequest *req)
 
     n = r->iov.iov_len / 512;
     if (n) {
+        if (s->tray_open) {
+            scsi_write_complete(r, -ENOMEDIUM);
+        }
         qemu_iovec_init_external(&r->qiov, &r->iov, 1);
         r->req.aiocb = bdrv_aio_writev(s->bs, r->sector, &r->qiov, n,
                                    scsi_write_complete, r);
@@ -841,7 +847,7 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
 
     switch (req->cmd.buf[0]) {
     case TEST_UNIT_READY:
-        if (!bdrv_is_inserted(s->bs))
+        if (s->tray_open || !bdrv_is_inserted(s->bs))
             goto not_ready;
 	break;
     case REQUEST_SENSE:
@@ -978,7 +984,7 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
         break;
     case REZERO_UNIT:
         DPRINTF("Rezero Unit\n");
-        if (!bdrv_is_inserted(s->bs)) {
+        if (s->tray_open || !bdrv_is_inserted(s->bs)) {
             goto not_ready;
         }
         break;
@@ -990,7 +996,7 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
     return buflen;
 
 not_ready:
-    if (!bdrv_is_inserted(s->bs)) {
+    if (s->tray_open || !bdrv_is_inserted(s->bs)) {
         scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(NO_MEDIUM));
     } else {
         scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(LUN_NOT_READY));
Signature · 1 line
1.7.2.3
Reviewed-by on v1 21/55
#51 lore raw

Re: [Qemu-devel] [PATCH 21/55] block: Revert entanglement of bdrv_is_inserted() with tray status

hw/ide/atapi.c static const struct { · line 1073

Quoted 10 lines
hw/ide/atapi.c +2 -1
@@ -1066,20 +1066,21 @@ static const struct {
     [ 0x03 ] = { cmd_request_sense,                 ALLOW_UA },
     [ 0x12 ] = { cmd_inquiry,                       ALLOW_UA },
     [ 0x1a ] = { cmd_mode_sense, /* (6) */          0 },
-    [ 0x1b ] = { cmd_start_stop_unit,               0 },
+    [ 0x1b ] = { cmd_start_stop_unit,               0 }, /* [1] */
     [ 0x1e ] = { cmd_prevent_allow_medium_removal,  0 },
     [ 0x25 ] = { cmd_read_cdvd_capacity,            CHECK_READY },
 
+    /* [1] handler detects and reports not ready condition itself */

Why not comment this near the actual code? The footnote scheme is pretty odd and I've not seen it anywhere else in the code.

Otherwise looks fine,

#52 lore raw

PATCH 22/55 block: Drop tray status tracking, no longer used

In reply to Markus Armbruster

Commit 4be9762a is now completely redone.

---
 block.c     |    1 -
 block_int.h |    1 -
 2 files changed, 0 insertions(+), 2 deletions(-)
block.c +0 -1
index 2b1eb4d..ba24bf1 100644
@@ -2775,7 +2775,6 @@ int bdrv_eject(BlockDriverState *bs, int eject_flag)
     if (drv && drv->bdrv_eject) {
         drv->bdrv_eject(bs, eject_flag);
     }
-    bs->tray_open = eject_flag;
     return 0;
 }
 
block_int.h +0 -1
index 2d9edcf..b142dee 100644
@@ -148,7 +148,6 @@ struct BlockDriverState {
     int open_flags; /* flags used to open the file, re-used for re-open */
     int removable; /* if true, the media can be removed */
     int locked;    /* if true, the media cannot temporarily be ejected */
-    int tray_open; /* if true, the virtual tray is open */
     int encrypted; /* if true, the media is encrypted */
     int valid_key; /* if true, a valid encryption key has been set */
     int sg;        /* if true, the device is a /dev/sg* */
Signature · 1 line
1.7.2.3
#54 lore raw

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

In reply to Markus Armbruster

Need to ask the device, so this requires new BlockDevOps member is_medium_ejected().

---
 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(-)
block.c +16 -2
index ba24bf1..6759066 100644
@@ -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 }",
block.h +6 -0
index 89f75e3..65a0115 100644
@@ -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,
hw/ide/core.c +6 -0
index c8cfd67..b07a4d7 100644
@@ -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,
hw/scsi-disk.c +10 -0
index 207a868..ff52d87 100644
@@ -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;
qmp-commands.hx +1 -0
index 92c5c3a..cc5d30f 100644
@@ -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)
Signature · 1 line
1.7.2.3

A message that is not in the archive (sent off-list or not yet received): 20110721112232.28405a1c@doriath

#56 lore raw

Re: [Qemu-devel] [PATCH 23/55] block: Show whether the guest ejected the medium in info block

Quoted 64 lines · Luiz Capitulino
Quoted 47 lines · On Wed, 20 Jul 2011 18:23:57 +0200 Markus Armbruster <[email protected]> wrote:

Need to ask the device, so this requires new BlockDevOps member is_medium_ejected().

---
 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(-)
block.c +12 -1
index ba24bf1..6759066 100644
@@ -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;

block.c void bdrv_info(Monitor *mon, QObject **ret_data) · line 1770

Quoted 10 lines
block.c +4 -0
 
         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.

Quoted 2 lines

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

Quoted 1 line

Also misses QMP documentation.

Last patch hunk.

Quoted 6 lines
Quoted 6 lines
block.c +0 -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 }",

[...]

Quoted 12 lines
Quoted 12 lines
qmp-commands.hx +1 -0
index 92c5c3a..cc5d30f 100644
@@ -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)
#57 lore raw

Re: [Qemu-devel] [PATCH 23/55] block: Show whether the guest ejected the medium in info block

Quoted 66 lines · On Thu, 21 Jul 2011 17:08:56 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 64 lines · Luiz Capitulino
Quoted 47 lines · On Wed, 20 Jul 2011 18:23:57 +0200 Markus Armbruster <[email protected]> wrote:

Need to ask the device, so this requires new BlockDevOps member is_medium_ejected().

---
 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(-)
block.c +12 -1
index ba24bf1..6759066 100644
@@ -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;

block.c void bdrv_info(Monitor *mon, QObject **ret_data) · line 1770

Quoted 10 lines
block.c +4 -0
 
         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.

Quoted 5 lines
Quoted 2 lines

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.

Quoted 4 lines
Quoted 2 lines

Also misses QMP documentation.

Last patch hunk.

Ah, thanks, missed it.

Quoted 21 lines
Quoted 7 lines
Quoted 7 lines
block.c +0 -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 }",

[...]

Quoted 12 lines
Quoted 12 lines
qmp-commands.hx +1 -0
index 92c5c3a..cc5d30f 100644
@@ -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)
#58 lore raw

Re: [Qemu-devel] [PATCH 23/55] block: Show whether the guest ejected the medium in info block

Quoted 69 lines · On Thu, 21 Jul 2011 13:38:46 -0300 Luiz Capitulino <[email protected]>…
Quoted 66 lines · On Thu, 21 Jul 2011 17:08:56 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 64 lines · Luiz Capitulino
Quoted 47 lines · On Wed, 20 Jul 2011 18:23:57 +0200 Markus Armbruster <[email protected]> wrote:

Need to ask the device, so this requires new BlockDevOps member is_medium_ejected().

---
 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(-)
block.c +12 -1
index ba24bf1..6759066 100644
@@ -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;

block.c void bdrv_info(Monitor *mon, QObject **ret_data) · line 1770

Quoted 10 lines
block.c +4 -0
 
         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.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

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.

#59 lore raw

PATCH 24/55 ide/atapi: Track tray locked state

In reply to Markus Armbruster

We already track it in BlockDriverState. Just like tray open/close state, we should track it in the device models instead, because it's device state.

---
 hw/ide/atapi.c    |    1 +
 hw/ide/internal.h |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)
hw/ide/atapi.c +1 -0
index 08e1a7a..8634e6b 100644
@@ -820,6 +820,7 @@ static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
 
 static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
 {
+    s->tray_locked = buf[4] & 1;
     bdrv_set_locked(s->bs, buf[4] & 1);
     ide_atapi_cmd_ok(s);
 }
hw/ide/internal.h +1 -0
index 5cf95c0..12f80de 100644
@@ -441,6 +441,7 @@ struct IDEState {
     uint8_t sense_key;
     uint8_t asc;
     bool tray_open;
+    bool tray_locked;
     uint8_t cdrom_changed;
     int packet_transfer_size;
     int elementary_transfer_size;
Signature · 1 line
1.7.2.3
#61 lore raw

PATCH 25/55 ide/atapi: Switch from BlockDriverState's locked to own tray_locked

In reply to Markus Armbruster
---
 hw/ide/atapi.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
hw/ide/atapi.c +2 -1
index 8634e6b..237657f 100644
@@ -777,8 +777,9 @@ static void cmd_mode_sense(IDEState *s, uint8_t *buf)
             buf[12] = 0x71;
             buf[13] = 3 << 5;
             buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
-            if (bdrv_is_locked(s->bs))
+            if (s->tray_locked) {
                 buf[6] |= 1 << 1;
+            }
             buf[15] = 0x00;
             cpu_to_ube16(&buf[16], 706);
             buf[18] = 0;
Signature · 1 line
1.7.2.3
#63 lore raw

PATCH 26/55 scsi-disk: Track tray locked state

In reply to Markus Armbruster

We already track it in BlockDriverState. Just like tray open/close state, we should track it in the device models instead, because it's device state.

---
 hw/scsi-disk.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
hw/scsi-disk.c +2 -0
index ff52d87..a220426 100644
@@ -75,6 +75,7 @@ struct SCSIDiskState
     char *serial;
     SCSISense sense;
     bool tray_open;
+    bool tray_locked;
     SCSIDriveKind drive_kind;
 };
 
@@ -893,6 +894,7 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
         scsi_disk_emulate_start_stop(r);
 	break;
     case ALLOW_MEDIUM_REMOVAL:
+        s->tray_locked = req->cmd.buf[4] & 1;
         bdrv_set_locked(s->bs, req->cmd.buf[4] & 1);
 	break;
     case READ_CAPACITY:
Signature · 1 line
1.7.2.3
#65 lore raw

PATCH 27/55 scsi-disk: Switch from BlockDriverState's locked to own tray_locked

In reply to Markus Armbruster
---
 hw/scsi-disk.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/scsi-disk.c +1 -1
index a220426..aac63b6 100644
@@ -676,7 +676,7 @@ static int mode_sense_page(SCSIRequest *req, int page, uint8_t *p,
         p[5] = 0xff; /* CD DA, DA accurate, RW supported,
                         RW corrected, C2 errors, ISRC,
                         UPC, Bar code */
-        p[6] = 0x2d | (bdrv_is_locked(s->bs)? 2 : 0);
+        p[6] = 0x2d | (s->tray_locked ? 2 : 0);
         /* Locking supported, jumper present, eject, tray */
         p[7] = 0; /* no volume & mute control, no
                      changer */
Signature · 1 line
1.7.2.3
#67 lore raw

Re: [Qemu-devel] [PATCH 27/55] scsi-disk: Switch from BlockDriverState's locked to own tray_locked

Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Markus Armbruster

Looks good, although I would have merged it into the previous one.

I'll consider squashing when I respin.

#68 lore raw

PATCH 28/55 block: Leave enforcing tray lock to device models

In reply to Markus Armbruster

The device model knows best when to accept the guest's eject command. No need to detour through the block layer.

bdrv_eject() can't fail anymore. Make it void.

---
 block.c        |    7 +------
 block.h        |    2 +-
 hw/ide/atapi.c |   29 +++++++++--------------------
 hw/scsi-disk.c |    3 +++
 4 files changed, 14 insertions(+), 27 deletions(-)
block.c +1 -6
index 6759066..70928af 100644
@@ -2778,18 +2778,13 @@ int bdrv_media_changed(BlockDriverState *bs)
 /**
  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
  */
-int bdrv_eject(BlockDriverState *bs, int eject_flag)
+void bdrv_eject(BlockDriverState *bs, int eject_flag)
 {
     BlockDriver *drv = bs->drv;
 
-    if (eject_flag && bs->locked) {
-        return -EBUSY;
-    }
-
     if (drv && drv->bdrv_eject) {
         drv->bdrv_eject(bs, eject_flag);
     }
-    return 0;
 }
 
 int bdrv_is_locked(BlockDriverState *bs)
block.h +1 -1
index 65a0115..7cc7919 100644
@@ -209,7 +209,7 @@ int bdrv_is_inserted(BlockDriverState *bs);
 int bdrv_media_changed(BlockDriverState *bs);
 int bdrv_is_locked(BlockDriverState *bs);
 void bdrv_set_locked(BlockDriverState *bs, int locked);
-int bdrv_eject(BlockDriverState *bs, int eject_flag);
+void bdrv_eject(BlockDriverState *bs, int eject_flag);
 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size);
 BlockDriverState *bdrv_find(const char *name);
 BlockDriverState *bdrv_next(BlockDriverState *bs);
hw/ide/atapi.c +9 -20
index 237657f..6cb8f0e 100644
@@ -894,33 +894,22 @@ static void cmd_seek(IDEState *s, uint8_t* buf)
 
 static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
 {
-    int sense, err = 0;
+    int sense;
     bool start = buf[4] & 1;
     bool loej = buf[4] & 2;
 
     if (loej) {
-        err = bdrv_eject(s->bs, !start);
-    }
-
-    switch (err) {
-    case 0:
-        ide_atapi_cmd_ok(s);
-        break;
-    case -EBUSY:
-        sense = SENSE_NOT_READY;
-        if (bdrv_is_inserted(s->bs)) {
-            sense = SENSE_ILLEGAL_REQUEST;
+        if (!start && s->tray_locked) {
+            sense = bdrv_is_inserted(s->bs)
+                ? SENSE_NOT_READY : SENSE_ILLEGAL_REQUEST;
+            ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
+            return;
         }
-        ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
-        break;
-    default:
-        ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
-        break;
-    }
-
-    if (loej && !err) {
+        bdrv_eject(s->bs, !start);
         s->tray_open = !start;
     }
+
+    ide_atapi_cmd_ok(s);
 }
 
 static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
hw/scsi-disk.c +3 -0
index aac63b6..a4ed499 100644
@@ -833,6 +833,9 @@ static void scsi_disk_emulate_start_stop(SCSIDiskReq *r)
     bool loej = req->cmd.buf[4] & 2;
 
     if (s->drive_kind == SCSI_CD && loej) {
+        if (!start && s->tray_locked) {
+            return;
+        }
         bdrv_eject(s->bs, !start);
         s->tray_open = !start;
     }
Signature · 1 line
1.7.2.3

A message that is not in the archive (sent off-list or not yet received): 20110721113051.4ea99b04@doriath

#70 lore raw

Re: [Qemu-devel] [PATCH 28/55] block: Leave enforcing tray lock to device models

Quoted 10 lines · Luiz Capitulino
Quoted 4 lines · On Wed, 20 Jul 2011 18:24:02 +0200 Markus Armbruster <[email protected]> wrote:

The device model knows best when to accept the guest's eject command. No need to detour through the block layer.

bdrv_eject() can't fail anymore. Make it void.

But we're supposed to return an error to the user/client if we're unable to eject, aren't we?

Same story as for bdrv_set_locked() [PATCH 07/55]: the purpose is to synchronize the physical tray with the virtual one. Errors would have to be propagated up into device model init, post migration and guest START STOP UNIT. What error to report to the guest? Hardware failure?

As with bdrv_set_locked(), I'm not removing any error handling. I don't add any either. The series is long enough...

Quoted 1 line

(one more question below)

Where?

Quoted 106 lines
Quoted 106 lines
---
 block.c        |    7 +------
 block.h        |    2 +-
 hw/ide/atapi.c |   29 +++++++++--------------------
 hw/scsi-disk.c |    3 +++
 4 files changed, 14 insertions(+), 27 deletions(-)
block.c +1 -6
index 6759066..70928af 100644
@@ -2778,18 +2778,13 @@ int bdrv_media_changed(BlockDriverState *bs)
 /**
  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
  */
-int bdrv_eject(BlockDriverState *bs, int eject_flag)
+void bdrv_eject(BlockDriverState *bs, int eject_flag)
 {
     BlockDriver *drv = bs->drv;
 
-    if (eject_flag && bs->locked) {
-        return -EBUSY;
-    }
-
     if (drv && drv->bdrv_eject) {
         drv->bdrv_eject(bs, eject_flag);
     }
-    return 0;
 }
 
 int bdrv_is_locked(BlockDriverState *bs)
block.h +1 -1
index 65a0115..7cc7919 100644
@@ -209,7 +209,7 @@ int bdrv_is_inserted(BlockDriverState *bs);
 int bdrv_media_changed(BlockDriverState *bs);
 int bdrv_is_locked(BlockDriverState *bs);
 void bdrv_set_locked(BlockDriverState *bs, int locked);
-int bdrv_eject(BlockDriverState *bs, int eject_flag);
+void bdrv_eject(BlockDriverState *bs, int eject_flag);
 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size);
 BlockDriverState *bdrv_find(const char *name);
 BlockDriverState *bdrv_next(BlockDriverState *bs);
hw/ide/atapi.c +9 -20
index 237657f..6cb8f0e 100644
@@ -894,33 +894,22 @@ static void cmd_seek(IDEState *s, uint8_t* buf)
 
 static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
 {
-    int sense, err = 0;
+    int sense;
     bool start = buf[4] & 1;
     bool loej = buf[4] & 2;
 
     if (loej) {
-        err = bdrv_eject(s->bs, !start);
-    }
-
-    switch (err) {
-    case 0:
-        ide_atapi_cmd_ok(s);
-        break;
-    case -EBUSY:
-        sense = SENSE_NOT_READY;
-        if (bdrv_is_inserted(s->bs)) {
-            sense = SENSE_ILLEGAL_REQUEST;
+        if (!start && s->tray_locked) {
+            sense = bdrv_is_inserted(s->bs)
+                ? SENSE_NOT_READY : SENSE_ILLEGAL_REQUEST;
+            ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
+            return;
         }
-        ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
-        break;
-    default:
-        ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
-        break;
-    }
-
-    if (loej && !err) {
+        bdrv_eject(s->bs, !start);
         s->tray_open = !start;
     }
+
+    ide_atapi_cmd_ok(s);
 }
 
 static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
hw/scsi-disk.c +3 -0
index aac63b6..a4ed499 100644
@@ -833,6 +833,9 @@ static void scsi_disk_emulate_start_stop(SCSIDiskReq *r)
     bool loej = req->cmd.buf[4] & 2;
 
     if (s->drive_kind == SCSI_CD && loej) {
+        if (!start && s->tray_locked) {
+            return;
+        }
         bdrv_eject(s->bs, !start);
         s->tray_open = !start;
     }
#71 lore raw

Re: [Qemu-devel] [PATCH 28/55] block: Leave enforcing tray lock to device models

Quoted 13 lines · On Thu, 21 Jul 2011 17:16:13 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 10 lines · Luiz Capitulino
Quoted 4 lines · On Wed, 20 Jul 2011 18:24:02 +0200 Markus Armbruster <[email protected]> wrote:

The device model knows best when to accept the guest's eject command. No need to detour through the block layer.

bdrv_eject() can't fail anymore. Make it void.

But we're supposed to return an error to the user/client if we're unable to eject, aren't we?

Quoted 7 lines

Same story as for bdrv_set_locked() [PATCH 07/55]: the purpose is to synchronize the physical tray with the virtual one. Errors would have to be propagated up into device model init, post migration and guest START STOP UNIT. What error to report to the guest? Hardware failure?

As with bdrv_set_locked(), I'm not removing any error handling. I don't add any either. The series is long enough...

I see.

Quoted 4 lines
Quoted 2 lines

(one more question below)

Where?

This was to check if you'd find it where it's hidden.

Kidding :) The question I had was answered by a later patch, but I forgot to remove the comment.

Quoted 108 lines
Quoted 107 lines
Quoted 107 lines
---
 block.c        |    7 +------
 block.h        |    2 +-
 hw/ide/atapi.c |   29 +++++++++--------------------
 hw/scsi-disk.c |    3 +++
 4 files changed, 14 insertions(+), 27 deletions(-)
block.c +1 -6
index 6759066..70928af 100644
@@ -2778,18 +2778,13 @@ int bdrv_media_changed(BlockDriverState *bs)
 /**
  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
  */
-int bdrv_eject(BlockDriverState *bs, int eject_flag)
+void bdrv_eject(BlockDriverState *bs, int eject_flag)
 {
     BlockDriver *drv = bs->drv;
 
-    if (eject_flag && bs->locked) {
-        return -EBUSY;
-    }
-
     if (drv && drv->bdrv_eject) {
         drv->bdrv_eject(bs, eject_flag);
     }
-    return 0;
 }
 
 int bdrv_is_locked(BlockDriverState *bs)
block.h +1 -1
index 65a0115..7cc7919 100644
@@ -209,7 +209,7 @@ int bdrv_is_inserted(BlockDriverState *bs);
 int bdrv_media_changed(BlockDriverState *bs);
 int bdrv_is_locked(BlockDriverState *bs);
 void bdrv_set_locked(BlockDriverState *bs, int locked);
-int bdrv_eject(BlockDriverState *bs, int eject_flag);
+void bdrv_eject(BlockDriverState *bs, int eject_flag);
 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size);
 BlockDriverState *bdrv_find(const char *name);
 BlockDriverState *bdrv_next(BlockDriverState *bs);
hw/ide/atapi.c +9 -20
index 237657f..6cb8f0e 100644
@@ -894,33 +894,22 @@ static void cmd_seek(IDEState *s, uint8_t* buf)
 
 static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
 {
-    int sense, err = 0;
+    int sense;
     bool start = buf[4] & 1;
     bool loej = buf[4] & 2;
 
     if (loej) {
-        err = bdrv_eject(s->bs, !start);
-    }
-
-    switch (err) {
-    case 0:
-        ide_atapi_cmd_ok(s);
-        break;
-    case -EBUSY:
-        sense = SENSE_NOT_READY;
-        if (bdrv_is_inserted(s->bs)) {
-            sense = SENSE_ILLEGAL_REQUEST;
+        if (!start && s->tray_locked) {
+            sense = bdrv_is_inserted(s->bs)
+                ? SENSE_NOT_READY : SENSE_ILLEGAL_REQUEST;
+            ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
+            return;
         }
-        ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
-        break;
-    default:
-        ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
-        break;
-    }
-
-    if (loej && !err) {
+        bdrv_eject(s->bs, !start);
         s->tray_open = !start;
     }
+
+    ide_atapi_cmd_ok(s);
 }
 
 static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
hw/scsi-disk.c +3 -0
index aac63b6..a4ed499 100644
@@ -833,6 +833,9 @@ static void scsi_disk_emulate_start_stop(SCSIDiskReq *r)
     bool loej = req->cmd.buf[4] & 2;
 
     if (s->drive_kind == SCSI_CD && loej) {
+        if (!start && s->tray_locked) {
+            return;
+        }
         bdrv_eject(s->bs, !start);
         s->tray_open = !start;
     }
#72 lore raw

PATCH 29/55 block: Drop medium lock tracking, ask device models instead

In reply to Markus Armbruster

Requires new BlockDevOps member is_medium_locked(). Implement for IDE and SCSI CD-ROMs.

---
 block.c        |   16 +++++++++-------
 block.h        |    7 ++++++-
 block_int.h    |    1 -
 blockdev.c     |    2 +-
 hw/ide/core.c  |    6 ++++++
 hw/scsi-disk.c |    6 ++++++
 6 files changed, 28 insertions(+), 10 deletions(-)
block.c +9 -7
index 70928af..af9bbb2 100644
@@ -776,6 +776,14 @@ static void bdrv_dev_resize_cb(BlockDriverState *bs)
     }
 }
 
+int bdrv_dev_is_medium_locked(BlockDriverState *bs)
+{
+    if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
+        return bs->dev_ops->is_medium_locked(bs->dev_opaque);
+    }
+    return 0;
+}
+
 /*
  * Run consistency checks on an image
  *
@@ -1762,7 +1770,7 @@ void bdrv_info(Monitor *mon, QObject **ret_data)
         bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
                                     "'removable': %i, 'locked': %i }",
                                     bs->device_name, bs->removable,
-                                    bs->locked);
+                                    bdrv_dev_is_medium_locked(bs));
         bs_dict = qobject_to_qdict(bs_obj);
 
         if (bdrv_dev_is_medium_ejected(bs)) {
@@ -2787,11 +2795,6 @@ void bdrv_eject(BlockDriverState *bs, int eject_flag)
     }
 }
 
-int bdrv_is_locked(BlockDriverState *bs)
-{
-    return bs->locked;
-}
-
 /**
  * Lock or unlock the media (if it is locked, the user won't be able
  * to eject it manually).
@@ -2802,7 +2805,6 @@ void bdrv_set_locked(BlockDriverState *bs, int locked)
 
     trace_bdrv_set_locked(bs, locked);
 
-    bs->locked = locked;
     if (drv && drv->bdrv_set_locked) {
         drv->bdrv_set_locked(bs, locked);
     }
block.h +6 -1
index 7cc7919..f2f8dad 100644
@@ -41,6 +41,11 @@ typedef struct BlockDevOps {
      */
     bool (*is_medium_ejected)(void *opaque);
     /*
+     * Is the virtual medium locked into the device?
+     * Null means device doesn't have such a lock.
+     */
+    bool (*is_medium_locked)(void *opaque);
+    /*
      * Runs when the size changed (e.g. monitor command block_resize)
      */
     void (*resize_cb)(void *opaque);
@@ -98,6 +103,7 @@ 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_dev_is_medium_locked(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,
@@ -207,7 +213,6 @@ int bdrv_is_sg(BlockDriverState *bs);
 int bdrv_enable_write_cache(BlockDriverState *bs);
 int bdrv_is_inserted(BlockDriverState *bs);
 int bdrv_media_changed(BlockDriverState *bs);
-int bdrv_is_locked(BlockDriverState *bs);
 void bdrv_set_locked(BlockDriverState *bs, int locked);
 void bdrv_eject(BlockDriverState *bs, int eject_flag);
 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size);
block_int.h +0 -1
index b142dee..a049f1a 100644
@@ -147,7 +147,6 @@ struct BlockDriverState {
     int keep_read_only; /* if true, the media was requested to stay read only */
     int open_flags; /* flags used to open the file, re-used for re-open */
     int removable; /* if true, the media can be removed */
-    int locked;    /* if true, the media cannot temporarily be ejected */
     int encrypted; /* if true, the media is encrypted */
     int valid_key; /* if true, a valid encryption key has been set */
     int sg;        /* if true, the device is a /dev/sg* */
blockdev.c +1 -1
index 13a9b41..4c58128 100644
@@ -649,7 +649,7 @@ static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
         qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
         return -1;
     }
-    if (!force && bdrv_is_locked(bs)) {
+    if (!force && bdrv_dev_is_medium_locked(bs)) {
         qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
         return -1;
     }
hw/ide/core.c +6 -0
index b07a4d7..35beb93 100644
@@ -1774,9 +1774,15 @@ static bool ide_cd_medium_ejected(void *opaque)
     return ((IDEState *)opaque)->tray_open;
 }
 
+static bool ide_cd_is_medium_locked(void *opaque)
+{
+    return ((IDEState *)opaque)->tray_locked;
+}
+
 static const BlockDevOps ide_cd_block_ops = {
     .change_media_cb = ide_cd_change_cb,
     .is_medium_ejected = ide_cd_medium_ejected,
+    .is_medium_locked = ide_cd_is_medium_locked,
 };
 
 int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind,
hw/scsi-disk.c +6 -0
index a4ed499..f804739 100644
@@ -1271,8 +1271,14 @@ static bool scsi_cd_is_medium_ejected(void *opaque)
     return ((SCSIDiskState *)opaque)->tray_open;
 }
 
+static bool scsi_cd_is_medium_locked(void *opaque)
+{
+    return ((SCSIDiskState *)opaque)->tray_locked;
+}
+
 static const BlockDevOps scsi_cd_block_ops = {
     .is_medium_ejected = scsi_cd_is_medium_ejected,
+    .is_medium_locked = scsi_cd_is_medium_locked,
 };
 
 static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)
Signature · 1 line
1.7.2.3
#73 lore raw

PATCH 30/55 block: Rename bdrv_set_locked() to bdrv_lock_medium()

In reply to Markus Armbruster
---
 block.c           |    8 ++++----
 block.h           |    2 +-
 block/raw-posix.c |    8 ++++----
 block/raw.c       |    6 +++---
 block_int.h       |    2 +-
 hw/ide/atapi.c    |    2 +-
 hw/scsi-disk.c    |    2 +-
 trace-events      |    2 +-
 8 files changed, 16 insertions(+), 16 deletions(-)
block.c +4 -4
index af9bbb2..c12f037 100644
@@ -2799,14 +2799,14 @@ void bdrv_eject(BlockDriverState *bs, int eject_flag)
  * Lock or unlock the media (if it is locked, the user won't be able
  * to eject it manually).
  */
-void bdrv_set_locked(BlockDriverState *bs, int locked)
+void bdrv_lock_medium(BlockDriverState *bs, int locked)
 {
     BlockDriver *drv = bs->drv;
 
-    trace_bdrv_set_locked(bs, locked);
+    trace_bdrv_lock_medium(bs, locked);
 
-    if (drv && drv->bdrv_set_locked) {
-        drv->bdrv_set_locked(bs, locked);
+    if (drv && drv->bdrv_lock_medium) {
+        drv->bdrv_lock_medium(bs, locked);
     }
 }
 
block.h +1 -1
index f2f8dad..b034d51 100644
@@ -213,7 +213,7 @@ int bdrv_is_sg(BlockDriverState *bs);
 int bdrv_enable_write_cache(BlockDriverState *bs);
 int bdrv_is_inserted(BlockDriverState *bs);
 int bdrv_media_changed(BlockDriverState *bs);
-void bdrv_set_locked(BlockDriverState *bs, int locked);
+void bdrv_lock_medium(BlockDriverState *bs, int locked);
 void bdrv_eject(BlockDriverState *bs, int eject_flag);
 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size);
 BlockDriverState *bdrv_find(const char *name);
block/raw-posix.c +4 -4
index e8a2fc2..c18778f 100644
@@ -1342,7 +1342,7 @@ static void cdrom_eject(BlockDriverState *bs, int eject_flag)
     }
 }
 
-static void cdrom_set_locked(BlockDriverState *bs, int locked)
+static void cdrom_lock_medium(BlockDriverState *bs, int locked)
 {
     BDRVRawState *s = bs->opaque;
 
@@ -1378,7 +1378,7 @@ static BlockDriver bdrv_host_cdrom = {
     /* removable device support */
     .bdrv_is_inserted   = cdrom_is_inserted,
     .bdrv_eject         = cdrom_eject,
-    .bdrv_set_locked    = cdrom_set_locked,
+    .bdrv_lock_medium   = cdrom_lock_medium,
 
     /* generic scsi device */
     .bdrv_ioctl         = hdev_ioctl,
@@ -1459,7 +1459,7 @@ static void cdrom_eject(BlockDriverState *bs, int eject_flag)
     cdrom_reopen(bs);
 }
 
-static void cdrom_set_locked(BlockDriverState *bs, int locked)
+static void cdrom_lock_medium(BlockDriverState *bs, int locked)
 {
     BDRVRawState *s = bs->opaque;
 
@@ -1497,7 +1497,7 @@ static BlockDriver bdrv_host_cdrom = {
     /* removable device support */
     .bdrv_is_inserted   = cdrom_is_inserted,
     .bdrv_eject         = cdrom_eject,
-    .bdrv_set_locked    = cdrom_set_locked,
+    .bdrv_lock_medium   = cdrom_lock_medium,
 };
 #endif /* __FreeBSD__ */
 
block/raw.c +3 -3
index cb6203e..fa74ef1 100644
@@ -80,9 +80,9 @@ static void raw_eject(BlockDriverState *bs, int eject_flag)
     bdrv_eject(bs->file, eject_flag);
 }
 
-static void raw_set_locked(BlockDriverState *bs, int locked)
+static void raw_lock_medium(BlockDriverState *bs, int locked)
 {
-    bdrv_set_locked(bs->file, locked);
+    bdrv_lock_medium(bs->file, locked);
 }
 
 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
@@ -138,7 +138,7 @@ static BlockDriver bdrv_raw = {
 
     .bdrv_is_inserted   = raw_is_inserted,
     .bdrv_eject         = raw_eject,
-    .bdrv_set_locked    = raw_set_locked,
+    .bdrv_lock_medium   = raw_lock_medium,
     .bdrv_ioctl         = raw_ioctl,
     .bdrv_aio_ioctl     = raw_aio_ioctl,
 
block_int.h +1 -1
index a049f1a..7d49a6e 100644
@@ -111,7 +111,7 @@ struct BlockDriver {
     int (*bdrv_is_inserted)(BlockDriverState *bs);
     int (*bdrv_media_changed)(BlockDriverState *bs);
     void (*bdrv_eject)(BlockDriverState *bs, int eject_flag);
-    void (*bdrv_set_locked)(BlockDriverState *bs, int locked);
+    void (*bdrv_lock_medium)(BlockDriverState *bs, int locked);
 
     /* to control generic scsi devices */
     int (*bdrv_ioctl)(BlockDriverState *bs, unsigned long int req, void *buf);
hw/ide/atapi.c +1 -1
index 6cb8f0e..1be6cbf 100644
@@ -822,7 +822,7 @@ static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
 static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
 {
     s->tray_locked = buf[4] & 1;
-    bdrv_set_locked(s->bs, buf[4] & 1);
+    bdrv_lock_medium(s->bs, buf[4] & 1);
     ide_atapi_cmd_ok(s);
 }
 
hw/scsi-disk.c +1 -1
index f804739..97f6d41 100644
@@ -898,7 +898,7 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
 	break;
     case ALLOW_MEDIUM_REMOVAL:
         s->tray_locked = req->cmd.buf[4] & 1;
-        bdrv_set_locked(s->bs, req->cmd.buf[4] & 1);
+        bdrv_lock_medium(s->bs, req->cmd.buf[4] & 1);
 	break;
     case READ_CAPACITY:
         /* The normal LEN field for this command is zero.  */
trace-events +1 -1
index bebf612..9be7242 100644
@@ -54,7 +54,7 @@ disable bdrv_aio_multiwrite_latefail(void *mcb, int i) "mcb %p i %d"
 disable bdrv_aio_flush(void *bs, void *opaque) "bs %p opaque %p"
 disable bdrv_aio_readv(void *bs, int64_t sector_num, int nb_sectors, void *opaque) "bs %p sector_num %"PRId64" nb_sectors %d opaque %p"
 disable bdrv_aio_writev(void *bs, int64_t sector_num, int nb_sectors, void *opaque) "bs %p sector_num %"PRId64" nb_sectors %d opaque %p"
-disable bdrv_set_locked(void *bs, int locked) "bs %p locked %d"
+disable bdrv_lock_medium(void *bs, int locked) "bs %p locked %d"
 
 # hw/virtio-blk.c
 disable virtio_blk_req_complete(void *req, int status) "req %p status %d"
Signature · 1 line
1.7.2.3
#74 lore raw

PATCH 31/55 ide: Provide IDEDeviceInfo method exit()

In reply to Markus Armbruster
---
 hw/ide/internal.h |    1 +
 hw/ide/qdev.c     |   12 ++++++++++++
 2 files changed, 13 insertions(+), 0 deletions(-)
hw/ide/internal.h +1 -0
index 12f80de..df8887a 100644
@@ -527,6 +527,7 @@ typedef int (*ide_qdev_initfn)(IDEDevice *dev);
 struct IDEDeviceInfo {
     DeviceInfo qdev;
     ide_qdev_initfn init;
+    ide_qdev_initfn exit;
 };
 
 #define BM_STATUS_DMAING 0x01
hw/ide/qdev.c +12 -0
index d9b8f24..97ccbe8 100644
@@ -87,9 +87,21 @@ err:
     return -1;
 }
 
+static int ide_qdev_exit(DeviceState *qdev)
+{
+    IDEDevice *dev = DO_UPCAST(IDEDevice, qdev, qdev);
+    IDEDeviceInfo *info = DO_UPCAST(IDEDeviceInfo, qdev, qdev->info);
+
+    if (info->exit) {
+        return info->exit(dev);
+    }
+    return 0;
+}
+
 static void ide_qdev_register(IDEDeviceInfo *info)
 {
     info->qdev.init = ide_qdev_init;
+    info->qdev.exit = ide_qdev_exit;
     info->qdev.bus_info = &ide_bus_info;
     qdev_register(&info->qdev);
 }
Signature · 1 line
1.7.2.3
#76 lore raw

PATCH 32/55 ide/atapi: Don't fail eject when tray is already open

In reply to Markus Armbruster

MMC-5 6.40.2.6 specifies that START STOP UNIT succeeds when the drive already has the requested state. cmd_start_stop_unit() fails when asked to eject while the tray is open and locked. Fix that.

---
 hw/ide/atapi.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/ide/atapi.c +1 -1
index 1be6cbf..9f384c8 100644
@@ -899,7 +899,7 @@ static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
     bool loej = buf[4] & 2;
 
     if (loej) {
-        if (!start && s->tray_locked) {
+        if (!start && !s->tray_open && s->tray_locked) {
             sense = bdrv_is_inserted(s->bs)
                 ? SENSE_NOT_READY : SENSE_ILLEGAL_REQUEST;
             ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
Signature · 1 line
1.7.2.3
Reviewed-by on v1 32/55
#77 lore raw

Re: [Qemu-devel] [PATCH 32/55] ide/atapi: Don't fail eject when tray is already open

Quoted 5 lines · Markus Armbruster

MMC-5 6.40.2.6 specifies that START STOP UNIT succeeds when the drive already has the requested state. cmd_start_stop_unit() fails when asked to eject while the tray is open and locked. Fix that.

Looks good,

#78 lore raw

PATCH 33/55 ide/atapi: Avoid physical/virtual tray state mismatch

In reply to Markus Armbruster

When ide-cd is backed by a physical drive, we want the physical tray match the virtual one. To that end, we call bdrv_eject() on guest's load/eject, and bdrv_lock_medium() on guest's prevent/allow removal. But we don't set the initial state on device model init. Fix that.

While there, also unlock on device model exit.

---
 hw/ide/core.c |    4 ++++
 hw/ide/qdev.c |   18 ++++++++++++++++++
 2 files changed, 22 insertions(+), 0 deletions(-)
hw/ide/core.c +4 -0
index 35beb93..1c84b68 100644
@@ -1845,6 +1845,10 @@ int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind,
 
     ide_reset(s);
     bdrv_set_removable(bs, s->drive_kind == IDE_CD);
+    if (s->drive_kind == IDE_CD) {
+        bdrv_lock_medium(bs, s->tray_locked);
+        bdrv_eject(bs, s->tray_open);
+    }
     return 0;
 }
 
hw/ide/qdev.c +18 -0
index 97ccbe8..50188a9 100644
@@ -178,6 +178,12 @@ static int ide_cd_initfn(IDEDevice *dev)
     return ide_dev_initfn(dev, IDE_CD);
 }
 
+static int ide_cd_exitfn(IDEDevice *dev)
+{
+    bdrv_lock_medium(dev->conf.bs, 0);
+    return 0;
+}
+
 static int ide_drive_initfn(IDEDevice *dev)
 {
     DriveInfo *dinfo = drive_get_by_blockdev(dev->conf.bs);
@@ -185,6 +191,16 @@ static int ide_drive_initfn(IDEDevice *dev)
     return ide_dev_initfn(dev, dinfo->media_cd ? IDE_CD : IDE_HD);
 }
 
+static int ide_drive_exitfn(IDEDevice *dev)
+{
+    DriveInfo *dinfo = drive_get_by_blockdev(dev->conf.bs);
+
+    if (dinfo->media_cd) {
+        return ide_cd_exitfn(dev);
+    }
+    return 0;
+}
+
 #define DEFINE_IDE_DEV_PROPERTIES()                     \
     DEFINE_PROP_UINT32("unit", IDEDrive, dev.unit, -1), \
     DEFINE_BLOCK_PROPERTIES(IDEDrive, dev.conf),        \
@@ -208,6 +224,7 @@ static IDEDeviceInfo ide_dev_info[] = {
         .qdev.desc    = "virtual IDE CD-ROM",
         .qdev.size    = sizeof(IDEDrive),
         .init         = ide_cd_initfn,
+        .exit         = ide_cd_exitfn,
         .qdev.props   = (Property[]) {
             DEFINE_IDE_DEV_PROPERTIES(),
             DEFINE_PROP_END_OF_LIST(),
@@ -218,6 +235,7 @@ static IDEDeviceInfo ide_dev_info[] = {
         .qdev.desc    = "virtual IDE disk or CD-ROM (legacy)",
         .qdev.size    = sizeof(IDEDrive),
         .init         = ide_drive_initfn,
+        .exit         = ide_drive_exitfn,
         .qdev.props   = (Property[]) {
             DEFINE_IDE_DEV_PROPERTIES(),
             DEFINE_PROP_END_OF_LIST(),
Signature · 1 line
1.7.2.3
Reviewed-by on v1 33/55
#79 lore raw

Re: [Qemu-devel] [PATCH 33/55] ide/atapi: Avoid physical/virtual tray state mismatch

Quoted 6 lines · Markus Armbruster

When ide-cd is backed by a physical drive, we want the physical tray match the virtual one. To that end, we call bdrv_eject() on guest's load/eject, and bdrv_lock_medium() on guest's prevent/allow removal. But we don't set the initial state on device model init. Fix that.

While there, also unlock on device model exit.

Looks good,

But wondering: do you actually use the physical CDROM passthrough?

#80 lore raw

Re: [Qemu-devel] [PATCH 33/55] ide/atapi: Avoid physical/virtual tray state mismatch

Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Markus Armbruster

When ide-cd is backed by a physical drive, we want the physical tray match the virtual one. To that end, we call bdrv_eject() on guest's load/eject, and bdrv_lock_medium() on guest's prevent/allow removal. But we don't set the initial state on device model init. Fix that.

Quoted 7 lines
Quoted 1 line

While there, also unlock on device model exit.

Looks good,

But wondering: do you actually use the physical CDROM passthrough?

In my opinion, it's one of the QEMU features friends don't let friends use. But it's there, I can fix it a little, so I fix it a little. A wiser man would perhaps close his eyes and move on to fix something more useful :)

#81 lore raw

PATCH 34/55 scsi-disk: Fix START_STOP to fail when it can't eject

In reply to Markus Armbruster

Don't fail when tray is already open.

---
 hw/scsi-bus.c  |   10 ++++++++++
 hw/scsi-disk.c |   15 +++++++++++----
 hw/scsi.h      |    4 ++++
 3 files changed, 25 insertions(+), 4 deletions(-)
hw/scsi-bus.c +10 -0
index ad6a730..169a022 100644
@@ -440,6 +440,11 @@ const struct SCSISense sense_code_NO_MEDIUM = {
     .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
 };
 
+/* LUN not ready, medium removal prevented */
+const struct SCSISense sense_code_MEDIUM_REMOVAL_PREVENTED_NR = {
+    .key = NOT_READY, .asc = 0x53, .ascq = 0x00
+};
+
 /* Hardware error, internal target failure */
 const struct SCSISense sense_code_TARGET_FAILURE = {
     .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
@@ -465,6 +470,11 @@ const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
     .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
 };
 
+/* Illegal request, medium removal prevented */
+const struct SCSISense sense_code_MEDIUM_REMOVAL_PREVENTED_ILL = {
+    .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x00
+};
+
 /* Command aborted, I/O process terminated */
 const struct SCSISense sense_code_IO_ERROR = {
     .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
hw/scsi-disk.c +11 -4
index 97f6d41..b9de4df 100644
@@ -825,7 +825,7 @@ static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf)
     return toclen;
 }
 
-static void scsi_disk_emulate_start_stop(SCSIDiskReq *r)
+static int scsi_disk_emulate_start_stop(SCSIDiskReq *r)
 {
     SCSIRequest *req = &r->req;
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
@@ -833,12 +833,17 @@ static void scsi_disk_emulate_start_stop(SCSIDiskReq *r)
     bool loej = req->cmd.buf[4] & 2;
 
     if (s->drive_kind == SCSI_CD && loej) {
-        if (!start && s->tray_locked) {
-            return;
+        if (!start && !s->tray_open && s->tray_locked) {
+            scsi_command_complete(r, CHECK_CONDITION,
+                                  bdrv_is_inserted(s->bs)
+                                  ? SENSE_CODE(MEDIUM_REMOVAL_PREVENTED_ILL)
+                                  : SENSE_CODE(MEDIUM_REMOVAL_PREVENTED_NR));
+            return -1;
         }
         bdrv_eject(s->bs, !start);
         s->tray_open = !start;
     }
+    return 0;
 }
 
 static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
@@ -894,7 +899,9 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
             goto illegal_request;
         break;
     case START_STOP:
-        scsi_disk_emulate_start_stop(r);
+        if (scsi_disk_emulate_start_stop(r) < 0) {
+            return -1;
+        }
 	break;
     case ALLOW_MEDIUM_REMOVAL:
         s->tray_locked = req->cmd.buf[4] & 1;
hw/scsi.h +4 -0
index c1dca35..f6b6bc1 100644
@@ -116,6 +116,8 @@ extern const struct SCSISense sense_code_NO_SENSE;
 extern const struct SCSISense sense_code_LUN_NOT_READY;
 /* LUN not ready, Medium not present */
 extern const struct SCSISense sense_code_NO_MEDIUM;
+/* LUN not readt, medium removal prevented */
+extern const struct SCSISense sense_code_MEDIUM_REMOVAL_PREVENTED_NR;
 /* Hardware error, internal target failure */
 extern const struct SCSISense sense_code_TARGET_FAILURE;
 /* Illegal request, invalid command operation code */
@@ -126,6 +128,8 @@ extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
 extern const struct SCSISense sense_code_INVALID_FIELD;
 /* Illegal request, LUN not supported */
 extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
+/* Illegal request, medium removal prevented */
+extern const struct SCSISense sense_code_MEDIUM_REMOVAL_PREVENTED_ILL;
 /* Command aborted, I/O process terminated */
 extern const struct SCSISense sense_code_IO_ERROR;
 /* Command aborted, I_T Nexus loss occurred */
Signature · 1 line
1.7.2.3
#83 lore raw

PATCH 35/55 scsi-disk: Avoid physical/virtual tray state mismatch

In reply to Markus Armbruster

When scsi-cd is backed by a physical drive, we want the physical tray match the virtual one. To that end, we call bdrv_eject() on guest's load/eject, and bdrv_lock_medium() on guest's prevent/allow removal. But we don't set the initial state on device model init. Fix that.

While there, also unlock on device model exit.

---
 hw/scsi-disk.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
hw/scsi-disk.c +7 -0
index b9de4df..553d8c6 100644
@@ -1270,6 +1270,9 @@ static void scsi_destroy(SCSIDevice *dev)
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
 
     scsi_device_purge_requests(&s->qdev);
+    if (s->drive_kind == SCSI_CD) {
+        bdrv_lock_medium(s->qdev.conf.bs, 0);
+    }
     blockdev_mark_auto_del(s->qdev.conf.bs);
 }
 
@@ -1332,6 +1335,10 @@ static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)
     s->qdev.type = TYPE_DISK;
     qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s);
     bdrv_set_removable(s->bs, kind == SCSI_CD);
+    if (kind == SCSI_CD) {
+        bdrv_lock_medium(s->bs, s->tray_locked);
+        bdrv_eject(s->bs, s->tray_open);
+    }
     add_boot_device_path(s->qdev.conf.bootindex, &dev->qdev, ",0");
     return 0;
 }
Signature · 1 line
1.7.2.3
Reviewed-by on v1 35/55
#84 lore raw

Re: [Qemu-devel] [PATCH 35/55] scsi-disk: Avoid physical/virtual tray state mismatch

Quoted 6 lines · Markus Armbruster

When scsi-cd is backed by a physical drive, we want the physical tray match the virtual one. To that end, we call bdrv_eject() on guest's load/eject, and bdrv_lock_medium() on guest's prevent/allow removal. But we don't set the initial state on device model init. Fix that.

While there, also unlock on device model exit.

Looks good,

#85 lore raw

PATCH 36/55 ide: Give vmstate structs internal linkage where possible

In reply to Markus Armbruster
---
 hw/ide/core.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
hw/ide/core.c +3 -3
index 1c84b68..709645e 100644
@@ -2057,7 +2057,7 @@ static bool ide_error_needed(void *opaque)
 }
 
 /* Fields for GET_EVENT_STATUS_NOTIFICATION ATAPI command */
-const VMStateDescription vmstate_ide_atapi_gesn_state = {
+static const VMStateDescription vmstate_ide_atapi_gesn_state = {
     .name ="ide_drive/atapi/gesn_state",
     .version_id = 1,
     .minimum_version_id = 1,
@@ -2069,7 +2069,7 @@ const VMStateDescription vmstate_ide_atapi_gesn_state = {
     }
 };
 
-const VMStateDescription vmstate_ide_drive_pio_state = {
+static const VMStateDescription vmstate_ide_drive_pio_state = {
     .name = "ide_drive/pio_state",
     .version_id = 1,
     .minimum_version_id = 1,
@@ -2131,7 +2131,7 @@ const VMStateDescription vmstate_ide_drive = {
     }
 };
 
-const VMStateDescription vmstate_ide_error_status = {
+static const VMStateDescription vmstate_ide_error_status = {
     .name ="ide_bus/error",
     .version_id = 1,
     .minimum_version_id = 1,
Signature · 1 line
1.7.2.3
#86 lore raw

PATCH 37/55 ide/atapi: Preserve tray state on migration

In reply to Markus Armbruster

Use a subsection, so that migration to older version still works, provided the tray is closed and unlocked.

---
 hw/ide/core.c |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)
hw/ide/core.c +32 -0
index 709645e..90ad497 100644
@@ -2042,6 +2042,22 @@ static bool ide_drive_pio_state_needed(void *opaque)
         || (s->bus->error_status & BM_STATUS_PIO_RETRY);
 }
 
+static int ide_tray_state_post_load(void *opaque, int version_id)
+{
+    IDEState *s = opaque;
+
+    bdrv_eject(s->bs, s->tray_open);
+    bdrv_lock_medium(s->bs, s->tray_locked);
+    return 0;
+}
+
+static bool ide_tray_state_needed(void *opaque)
+{
+    IDEState *s = opaque;
+
+    return s->tray_open || s->tray_locked;
+}
+
 static bool ide_atapi_gesn_needed(void *opaque)
 {
     IDEState *s = opaque;
@@ -2069,6 +2085,19 @@ static const VMStateDescription vmstate_ide_atapi_gesn_state = {
     }
 };
 
+static const VMStateDescription vmstate_ide_tray_state = {
+    .name = "ide_drive/tray_state",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .post_load = ide_tray_state_post_load,
+    .fields = (VMStateField[]) {
+        VMSTATE_BOOL(tray_open, IDEState),
+        VMSTATE_BOOL(tray_locked, IDEState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 static const VMStateDescription vmstate_ide_drive_pio_state = {
     .name = "ide_drive/pio_state",
     .version_id = 1,
@@ -2123,6 +2152,9 @@ const VMStateDescription vmstate_ide_drive = {
             .vmsd = &vmstate_ide_drive_pio_state,
             .needed = ide_drive_pio_state_needed,
         }, {
+            .vmsd = &vmstate_ide_tray_state,
+            .needed = ide_tray_state_needed,
+        }, {
             .vmsd = &vmstate_ide_atapi_gesn_state,
             .needed = ide_atapi_gesn_needed,
         }, {
Signature · 1 line
1.7.2.3
#87 lore raw

PATCH 38/55 scsi-disk: Preserve tray state on migration

In reply to Markus Armbruster

Breaks migration of qdevs "scsi-cd" and legacy "scsi-disk" to older versions. We normally use subsections to avoid that. Not possible here, because we don't have a section to begin with. Too bad.

---
 hw/scsi-disk.c |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)
hw/scsi-disk.c +35 -0
index 553d8c6..ad63814 100644
@@ -1368,6 +1368,39 @@ static int scsi_disk_initfn(SCSIDevice *dev)
     return scsi_initfn(dev, kind);
 }
 
+static int scsi_cd_post_load(void *opaque, int version_id)
+{
+    SCSIDiskState *s = opaque;
+
+    bdrv_eject(s->bs, s->tray_open);
+    bdrv_lock_medium(s->bs, s->tray_locked);
+    return 0;
+}
+
+static const VMStateDescription vmstate_scsi_cd = {
+    .name = "scsi-cd",
+    .version_id = 0,
+    .minimum_version_id = 0,
+    .post_load = scsi_cd_post_load,
+    .fields = (VMStateField[]) {
+        VMSTATE_BOOL(tray_open, SCSIDiskState),
+        VMSTATE_BOOL(tray_locked, SCSIDiskState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static const VMStateDescription vmstate_scsi_disk = {
+    .name = "scsi-disk",
+    .version_id = 0,
+    .minimum_version_id = 0,
+    .post_load = scsi_cd_post_load,
+    .fields = (VMStateField[]) {
+        VMSTATE_BOOL(tray_open, SCSIDiskState),
+        VMSTATE_BOOL(tray_locked, SCSIDiskState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 #define DEFINE_SCSI_DISK_PROPERTIES()                           \
     DEFINE_BLOCK_PROPERTIES(SCSIDiskState, qdev.conf),          \
     DEFINE_PROP_STRING("ver",  SCSIDiskState, version),         \
@@ -1400,6 +1433,7 @@ static SCSIDeviceInfo scsi_disk_info[] = {
         .qdev.fw_name = "disk",
         .qdev.desc    = "virtual SCSI CD-ROM",
         .qdev.size    = sizeof(SCSIDiskState),
+        .qdev.vmsd    = &vmstate_scsi_cd,
         .qdev.reset   = scsi_disk_reset,
         .init         = scsi_cd_initfn,
         .destroy      = scsi_destroy,
@@ -1420,6 +1454,7 @@ static SCSIDeviceInfo scsi_disk_info[] = {
         .qdev.fw_name = "disk",
         .qdev.desc    = "virtual SCSI disk or CD-ROM (legacy)",
         .qdev.size    = sizeof(SCSIDiskState),
+        .qdev.vmsd    = &vmstate_scsi_disk,
         .qdev.reset   = scsi_disk_reset,
         .init         = scsi_disk_initfn,
         .destroy      = scsi_destroy,
Signature · 1 line
1.7.2.3
#88 lore raw

PATCH 39/55 block/raw: Fix to forward method bdrv_media_changed()

In reply to Markus Armbruster

Block driver "raw" forwards most methods to the underlying block driver. However, it doesn't implement method bdrv_media_changed(). Makes bdrv_media_changed() always return -ENOTSUP.

I believe -fda /dev/fd0 gives you raw over host_floppy, and disk change detection (fdc register 7 bit 7) is broken. Testing my theory requires a computer museum, though.

---
 block/raw.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
block/raw.c +7 -0
index fa74ef1..934f439 100644
@@ -75,6 +75,11 @@ static int raw_is_inserted(BlockDriverState *bs)
     return bdrv_is_inserted(bs->file);
 }
 
+static int raw_media_changed(BlockDriverState *bs)
+{
+    return bdrv_media_changed(bs->file);
+}
+
 static void raw_eject(BlockDriverState *bs, int eject_flag)
 {
     bdrv_eject(bs->file, eject_flag);
@@ -137,8 +142,10 @@ static BlockDriver bdrv_raw = {
     .bdrv_discard       = raw_discard,
 
     .bdrv_is_inserted   = raw_is_inserted,
+    .bdrv_media_changed = raw_media_changed,
     .bdrv_eject         = raw_eject,
     .bdrv_lock_medium   = raw_lock_medium,
+
     .bdrv_ioctl         = raw_ioctl,
     .bdrv_aio_ioctl     = raw_aio_ioctl,
 
Signature · 1 line
1.7.2.3
Reviewed-by on v1 39/55
#89 lore raw

Re: [Qemu-devel] [PATCH 39/55] block/raw: Fix to forward method bdrv_media_changed()

Quoted 7 lines · Markus Armbruster

Block driver "raw" forwards most methods to the underlying block driver. However, it doesn't implement method bdrv_media_changed(). Makes bdrv_media_changed() always return -ENOTSUP.

I believe -fda /dev/fd0 gives you raw over host_floppy, and disk change detection (fdc register 7 bit 7) is broken. Testing my theory requires a computer museum, though.

Haha. The patch looks fine to me anyway,

#90 lore raw

PATCH 40/55 block: Leave tracking media change to device models

In reply to Markus Armbruster

hw/fdc.c is the only one that cares.

---
 block.c     |   20 ++++++--------------
 block_int.h |    1 -
 hw/fdc.c    |   40 ++++++++++++++++++----------------------
 3 files changed, 24 insertions(+), 37 deletions(-)
block.c +6 -14
index c12f037..2225be5 100644
@@ -637,7 +637,6 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
     }
 
     if (!bdrv_key_required(bs)) {
-        bs->media_changed = 1;
         bdrv_dev_change_media_cb(bs);
     }
 
@@ -674,7 +673,6 @@ void bdrv_close(BlockDriverState *bs)
             bdrv_close(bs->file);
         }
 
-        bs->media_changed = 1;
         bdrv_dev_change_media_cb(bs);
     }
 }
@@ -1542,7 +1540,6 @@ int bdrv_set_key(BlockDriverState *bs, const char *key)
     } else if (!bs->valid_key) {
         bs->valid_key = 1;
         /* call the change callback now, we skipped it on open */
-        bs->media_changed = 1;
         bdrv_dev_change_media_cb(bs);
     }
     return ret;
@@ -2765,22 +2762,17 @@ int bdrv_is_inserted(BlockDriverState *bs)
 }
 
 /**
- * Return TRUE if the media changed since the last call to this
- * function. It is currently only used for floppy disks
+ * Return whether the media changed since the last call to this
+ * function, or -ENOTSUP if we don't know.  Most drivers don't know.
  */
 int bdrv_media_changed(BlockDriverState *bs)
 {
     BlockDriver *drv = bs->drv;
-    int ret;
 
-    if (!drv || !drv->bdrv_media_changed)
-        ret = -ENOTSUP;
-    else
-        ret = drv->bdrv_media_changed(bs);
-    if (ret == -ENOTSUP)
-        ret = bs->media_changed;
-    bs->media_changed = 0;
-    return ret;
+    if (drv && drv->bdrv_media_changed) {
+        return drv->bdrv_media_changed(bs);
+    }
+    return -ENOTSUP;
 }
 
 /**
block_int.h +0 -1
index 7d49a6e..d8688b8 100644
@@ -164,7 +164,6 @@ struct BlockDriverState {
                                 this file image */
     char backing_format[16]; /* if non-zero and backing_file exists */
     int is_temporary;
-    int media_changed;
 
     BlockDriverState *backing_hd;
     BlockDriverState *file;
hw/fdc.c +18 -22
index edf0360..a2d8aae 100644
@@ -535,30 +535,11 @@ static CPUWriteMemoryFunc * const fdctrl_mem_write_strict[3] = {
     NULL,
 };
 
-static void fdrive_media_changed_pre_save(void *opaque)
-{
-    FDrive *drive = opaque;
-
-    drive->media_changed = drive->bs->media_changed;
-}
-
-static int fdrive_media_changed_post_load(void *opaque, int version_id)
-{
-    FDrive *drive = opaque;
-
-    if (drive->bs != NULL) {
-        drive->bs->media_changed = drive->media_changed;
-    }
-
-    /* User ejected the floppy when drive->bs == NULL */
-    return 0;
-}
-
 static bool fdrive_media_changed_needed(void *opaque)
 {
     FDrive *drive = opaque;
 
-    return (drive->bs != NULL && drive->bs->media_changed != 1);
+    return (drive->bs != NULL && drive->media_changed != 1);
 }
 
 static const VMStateDescription vmstate_fdrive_media_changed = {
@@ -566,8 +547,6 @@ static const VMStateDescription vmstate_fdrive_media_changed = {
     .version_id = 1,
     .minimum_version_id = 1,
     .minimum_version_id_old = 1,
-    .pre_save = fdrive_media_changed_pre_save,
-    .post_load = fdrive_media_changed_post_load,
     .fields      = (VMStateField[]) {
         VMSTATE_UINT8(media_changed, FDrive),
         VMSTATE_END_OF_LIST()
@@ -920,6 +899,10 @@ static int fdctrl_media_changed(FDrive *drv)
     if (!drv->bs)
         return 0;
     ret = bdrv_media_changed(drv->bs);
+    if (ret < 0) {
+        ret = drv->media_changed;
+    }
+    drv->media_changed = 0;
     if (ret) {
         fd_revalidate(drv);
     }
@@ -1791,6 +1774,17 @@ static void fdctrl_result_timer(void *opaque)
     fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
 }
 
+static void fdctrl_change_cb(void *opaque)
+{
+    FDrive *drive = opaque;
+
+    drive->media_changed = 1;
+}
+
+static const BlockDevOps fdctrl_block_ops = {
+    .change_media_cb = fdctrl_change_cb,
+};
+
 /* Init functions */
 static int fdctrl_connect_drives(FDCtrl *fdctrl)
 {
@@ -1814,7 +1808,9 @@ static int fdctrl_connect_drives(FDCtrl *fdctrl)
         fd_init(drive);
         fd_revalidate(drive);
         if (drive->bs) {
+            drive->media_changed = 1;
             bdrv_set_removable(drive->bs, 1);
+            bdrv_set_dev_ops(drive->bs, &fdctrl_block_ops, drive);
         }
     }
     return 0;
Signature · 1 line
1.7.2.3
#92 lore raw

PATCH 41/55 fdc: Make media change detection more robust

In reply to Markus Armbruster

fdctrl_change_cb() gets called on a virtual media change via monitor. It would be nice if host device block drivers called it on physical media change, but they don't.

bdrv_media_changed() lets you poll for media change, but it returns "don't know" except with block driver "host_floppy".

FDrive member media_changed gets set on device initialization and by fdctrl_change_cb(), and cleared by fdctrl_media_changed(). Thus, it's set on first entry to fdctrl_media_changed() since device initialization or virtual media change.

fdctrl_media_changed() ignores media_changed unless bdrv_media_changed() returns "don't know". If we change media via monitor (setting media_changed), and the new media's block driver returns 0, we lose. Fortunately, "host_floppy" always returns 1 on first call. Brittle. Clean it up not to rely on it.

---
 hw/fdc.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)
hw/fdc.c +8 -4
index a2d8aae..6854e15 100644
@@ -898,11 +898,15 @@ static int fdctrl_media_changed(FDrive *drv)
 
     if (!drv->bs)
         return 0;
-    ret = bdrv_media_changed(drv->bs);
-    if (ret < 0) {
-        ret = drv->media_changed;
+    if (drv->media_changed) {
+        drv->media_changed = 0;
+        ret = 1;
+    } else {
+        ret = bdrv_media_changed(drv->bs);
+        if (ret < 0) {
+            ret = 0;            /* we don't know, assume no */
+        }
     }
-    drv->media_changed = 0;
     if (ret) {
         fd_revalidate(drv);
     }
Signature · 1 line
1.7.2.3
#93 lore raw

PATCH 42/55 block: Clean up bdrv_flush_all()

In reply to Markus Armbruster

Change (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)) to just bdrv_is_inserted(). Rationale:

The value of bdrv_is_removable(bs) matters only when bdrv_is_inserted(bs) is false.

bdrv_is_inserted(bs) is true when bs is open (bs->drv != NULL) and not an empty host drive (CD-ROM or floppy).

Therefore, bdrv_is_removable(bs) matters only when:

1. bs is not open
old: may call bdrv_flush(bs), which does nothing
new: won't call

2. bs is an empty host drive
old: may call bdrv_flush(bs), which calls driver method
raw_flush(), which calls fdatasync() or equivalent, which can't do anything useful while the drive is empty
new: won't call

Result is bs->drv && !bdrv_is_read_only(bs) && bdrv_is_inserted(bs). bdrv_is_inserted(bs) implies bs->drv. Drop the redundant test.

---
 block.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
block.c +1 -2
index 2225be5..f233062 100644
@@ -1626,8 +1626,7 @@ void bdrv_flush_all(void)
     BlockDriverState *bs;
 
     QTAILQ_FOREACH(bs, &bdrv_states, list) {
-        if (bs->drv && !bdrv_is_read_only(bs) &&
-            (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
+        if (!bdrv_is_read_only(bs) && bdrv_is_inserted(bs)) {
             bdrv_flush(bs);
         }
     }
Signature · 1 line
1.7.2.3
#95 lore raw

PATCH 43/55 savevm: Include writable devices with removable media

In reply to Markus Armbruster

savevm and loadvm silently ignore block devices with removable media, such as floppies and SD cards. Rolling back a VM to a previous checkpoint will *not* roll back writes to block devices with removable media.

Moreover, bdrv_is_removable() is a confused mess, and wrong in at least one case: it considers "-drive if=xen,media=cdrom -M xenpv" removable. It'll be cleaned up later in this series.

Read-only block devices are also ignored, but that's okay.

Fix by ignoring only read-only block devices and empty block devices.

---
 block.c  |    2 +-
 savevm.c |    4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
block.c +1 -1
index f233062..9591c8a 100644
@@ -1958,7 +1958,7 @@ void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
 int bdrv_can_snapshot(BlockDriverState *bs)
 {
     BlockDriver *drv = bs->drv;
-    if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
+    if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
         return 0;
     }
 
savevm.c +2 -2
index 8139bc7..1f03987 100644
@@ -1913,7 +1913,7 @@ void do_savevm(Monitor *mon, const QDict *qdict)
     bs = NULL;
     while ((bs = bdrv_next(bs))) {
 
-        if (bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
+        if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
             continue;
         }
 
@@ -2033,7 +2033,7 @@ int load_vmstate(const char *name)
     bs = NULL;
     while ((bs = bdrv_next(bs))) {
 
-        if (bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
+        if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
             continue;
         }
 
Signature · 1 line
1.7.2.3
#96 lore raw

PATCH 44/55 spitz tosa: Simplify "drive is suitable for microdrive" test

In reply to Markus Armbruster

We try the drive defined with -drive if=ide,index=0 (or equivalent sugar). We use it only if (dinfo && bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)). This is a convoluted way to test for "drive media can't be removed".

The only way to create such a drive with -drive if=ide is media=cdrom. And that sets dinfo->media_cd, so just test that.

---
 hw/spitz.c |   10 +++-------
 hw/tosa.c  |   10 +++-------
 2 files changed, 6 insertions(+), 14 deletions(-)
hw/spitz.c +3 -7
index 006f7a9..c743ae0 100644
@@ -706,17 +706,13 @@ static void spitz_ssp_attach(PXA2xxState *cpu)
 static void spitz_microdrive_attach(PXA2xxState *cpu, int slot)
 {
     PCMCIACardState *md;
-    BlockDriverState *bs;
     DriveInfo *dinfo;
 
     dinfo = drive_get(IF_IDE, 0, 0);
-    if (!dinfo)
+    if (!dinfo || dinfo->media_cd)
         return;
-    bs = dinfo->bdrv;
-    if (bdrv_is_inserted(bs) && !bdrv_is_removable(bs)) {
-        md = dscm1xxxx_init(dinfo);
-        pxa2xx_pcmcia_attach(cpu->pcmcia[slot], md);
-    }
+    md = dscm1xxxx_init(dinfo);
+    pxa2xx_pcmcia_attach(cpu->pcmcia[slot], md);
 }
 
 /* Wm8750 and Max7310 on I2C */
hw/tosa.c +3 -7
index a7967a2..7b407f4 100644
@@ -51,17 +51,13 @@
 static void tosa_microdrive_attach(PXA2xxState *cpu)
 {
     PCMCIACardState *md;
-    BlockDriverState *bs;
     DriveInfo *dinfo;
 
     dinfo = drive_get(IF_IDE, 0, 0);
-    if (!dinfo)
+    if (!dinfo || dinfo->media_cd)
         return;
-    bs = dinfo->bdrv;
-    if (bdrv_is_inserted(bs) && !bdrv_is_removable(bs)) {
-        md = dscm1xxxx_init(dinfo);
-        pxa2xx_pcmcia_attach(cpu->pcmcia[0], md);
-    }
+    md = dscm1xxxx_init(dinfo);
+    pxa2xx_pcmcia_attach(cpu->pcmcia[0], md);
 }
 
 static void tosa_out_switch(void *opaque, int line, int level)
Signature · 1 line
1.7.2.3
#97 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Quoted 7 lines · Markus Armbruster

We try the drive defined with -drive if=ide,index=0 (or equivalent sugar).  We use it only if (dinfo && bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)).  This is a convoluted way to test for "drive media can't be removed".

The only way to create such a drive with -drive if=ide is media=cdrom. And that sets dinfo->media_cd, so just test that.

This is a less generic test and more prone to be broken inadvertently, so it seems like a step back. What's the argument against the convoluted and explicit test?

Cheers

#98 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Quoted 6 lines · andrzej zaborowski
Quoted 5 lines · Markus Armbruster

We try the drive defined with -drive if=ide,index=0 (or equivalent sugar).  We use it only if (dinfo && bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)).  This is a convoluted way to test for "drive media can't be removed".

Quoted 6 lines
Quoted 2 lines

The only way to create such a drive with -drive if=ide is media=cdrom. And that sets dinfo->media_cd, so just test that.

This is a less generic test and more prone to be broken inadvertently, so it seems like a step back. What's the argument against the convoluted and explicit test?

My motivation for changing it was to reduce the uses of BlockDriverState member removable prior to nuking it from orbit [PATCH 45/55].

I consider my change an improvement, because I find "dinfo->media_cd" clearer than
"bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)".

Moreover, it's the only place that uses bdrv_is_removable() to test whether the block driver supports media eject. All the other places use dinfo->media_cd: hw/scsi-disk.c hw/xen_devconfig.c hw/ide/core.c hw/ide/qdev.c. Can't see why spitz & tosa need to check the same thing differently, and why it's worth keeping bdrv_is_removable() around just for that.

#99 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Quoted 14 lines · Markus Armbruster
Quoted 6 lines · andrzej zaborowski
Quoted 5 lines · Markus Armbruster

We try the drive defined with -drive if=ide,index=0 (or equivalent sugar).  We use it only if (dinfo && bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)).  This is a convoluted way to test for "drive media can't be removed".

Quoted 6 lines
Quoted 2 lines

The only way to create such a drive with -drive if=ide is media=cdrom. And that sets dinfo->media_cd, so just test that.

This is a less generic test and more prone to be broken inadvertently, so it seems like a step back.  What's the argument against the convoluted and explicit test?

Quoted 6 lines

My motivation for changing it was to reduce the uses of BlockDriverState member removable prior to nuking it from orbit [PATCH 45/55].

I consider my change an improvement, because I find "dinfo->media_cd" clearer than
"bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)".

This seems like an argument for providing a bdrv_supports_eject() or whatever we're actually trying to test for here. I kind of felt the same way as Andrzej when I saw this patch going past but it got lost in my mail folder...

-- PMM

#100 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Quoted 18 lines · Peter Maydell
Quoted 17 lines · Markus Armbruster
Quoted 6 lines · andrzej zaborowski
Quoted 5 lines · Markus Armbruster

We try the drive defined with -drive if=ide,index=0 (or equivalent sugar).  We use it only if (dinfo && bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)).  This is a convoluted way to test for "drive media can't be removed".

Quoted 6 lines
Quoted 2 lines

The only way to create such a drive with -drive if=ide is media=cdrom. And that sets dinfo->media_cd, so just test that.

This is a less generic test and more prone to be broken inadvertently, so it seems like a step back.  What's the argument against the convoluted and explicit test?

My motivation for changing it was to reduce the uses of BlockDriverState member removable prior to nuking it from orbit [PATCH 45/55].

Quoted 8 lines
Quoted 3 lines

I consider my change an improvement, because I find "dinfo->media_cd" clearer than
"bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)".

This seems like an argument for providing a bdrv_supports_eject() or whatever we're actually trying to test for here. I kind of felt the same way as Andrzej when I saw this patch going past but it got lost in my mail folder...

Well, what are you trying to test for here?

Let's start with figuring out what we actually test for right now (may not be what you *want* to test for, but it's a start). The test code is "bdrv_is_inserted(bs) && !bdrv_is_removable(bs)".

bdrv_is_removable() is a confused mess. It is true when an ide-cd, scsi-cd or floppy qdev is attached, or when the BlockDriverState was created with -drive if={floppy,sd} or -drive if={ide,scsi,xen,none},media=cdrom, except when an ide-hd, scsi-hd, scsi-generic or virtio-blk qdev is attached.

Since we're about to attach a device here, no other device can be attached, and the mess simplifies into "when the BlockDriverState was created with -drive if={floppy,sd} or -drive if={ide,scsi,xen,none},media=cdrom".

Since we're getting IF_IDE, it further simplifies into "when the BlockDriverState was created with -drive if=ide,media=cdrom". Which is what my patch tests.

The bdrv_is_inserted() part is actually redundant, because it can only be false if bdrv_is_removable() is true: the only way to create an IDE drive empty is with media=cdrom (makes bdrv_is_removable() true), and media is ejectable only when bdrv_is_removable() is true.

Therefore, my patch preserves behavior.

Testing for "block driver supports eject" is something else entirely. Block drivers "host_cdrom" and "host_floppy" implement method bdrv_eject()[*]. Is that what you want?

Note that "block driver supports eject" is completely independent of "monitor command eject works". You can back a removable device such as a CD-ROM with a block driver that doesn't support eject, say "file". Monitor command eject works fine then. Conversely, you can back a non-removable device such as a disk with a block driver that does support eject, such as "host_cdrom". It may not be the wisest thing to do, but it works. Ejecting the physical media while the device model is using it will result in I/O errors, to nobody's surprise.

Anyway. bdrv_is_removable() must die. You're using it. Please tell me what exactly you're trying to accomplish, so I can get it done without bdrv_is_removable(). Best way to tell me is with a patch.

[*] "raw" implements bdrv_eject() as well, but it merely forwards, which doesn't count.

#101 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Quoted 41 lines · Markus Armbruster
Quoted 18 lines · Peter Maydell
Quoted 17 lines · Markus Armbruster
Quoted 6 lines · andrzej zaborowski
Quoted 5 lines · Markus Armbruster

We try the drive defined with -drive if=ide,index=0 (or equivalent sugar).  We use it only if (dinfo && bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)).  This is a convoluted way to test for "drive media can't be removed".

Quoted 6 lines
Quoted 2 lines

The only way to create such a drive with -drive if=ide is media=cdrom. And that sets dinfo->media_cd, so just test that.

This is a less generic test and more prone to be broken inadvertently, so it seems like a step back.  What's the argument against the convoluted and explicit test?

My motivation for changing it was to reduce the uses of BlockDriverState member removable prior to nuking it from orbit [PATCH 45/55].

Quoted 8 lines
Quoted 3 lines

I consider my change an improvement, because I find "dinfo->media_cd" clearer than
"bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)".

This seems like an argument for providing a bdrv_supports_eject() or whatever we're actually trying to test for here. I kind of felt the same way as Andrzej when I saw this patch going past but it got lost in my mail folder...

Well, what are you trying to test for here?

Let's start with figuring out what we actually test for right now (may not be what you *want* to test for, but it's a start).  The test code is "bdrv_is_inserted(bs) && !bdrv_is_removable(bs)".

bdrv_is_removable() is a confused mess.  It is true when an ide-cd, scsi-cd or floppy qdev is attached, or when the BlockDriverState was created with -drive if={floppy,sd} or -drive if={ide,scsi,xen,none},media=cdrom, except when an ide-hd, scsi-hd, scsi-generic or virtio-blk qdev is attached.

Quoted 7 lines

Since we're about to attach a device here, no other device can be attached, and the mess simplifies into "when the BlockDriverState was created with -drive if={floppy,sd} or -drive if={ide,scsi,xen,none},media=cdrom".

Since we're getting IF_IDE, it further simplifies into "when the BlockDriverState was created with -drive if=ide,media=cdrom".

What's wrong with that again? All sounds sensible to me.

Quoted 2 lines

Which is
what my patch tests.

It's like if you changed the named constants/#defines in a project for their preprocessed values... Behaviour is preserved but it makes worse code, and its easier to break too, when someone updates the value of some constant.

Quoted 6 lines

The bdrv_is_inserted() part is actually redundant, because it can only be false if bdrv_is_removable() is true: the only way to create an IDE drive empty is with media=cdrom (makes bdrv_is_removable() true), and media is ejectable only when bdrv_is_removable() is true.

Quoted 6 lines

Therefore, my patch preserves behavior.

Testing for "block driver supports eject" is something else entirely. Block drivers "host_cdrom" and "host_floppy" implement method bdrv_eject()[*].  Is that what you want?

No, from your description of ejectable it looks like we want to check if the device supports the monitor command "eject", although additionally testing for "block driver supports eject" may make sense.

Cheers

#102 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Quoted 47 lines · andrzej zaborowski
Quoted 46 lines · Markus Armbruster
Quoted 18 lines · Peter Maydell
Quoted 17 lines · Markus Armbruster
Quoted 6 lines · andrzej zaborowski
Quoted 5 lines · Markus Armbruster

We try the drive defined with -drive if=ide,index=0 (or equivalent sugar).  We use it only if (dinfo && bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)).  This is a convoluted way to test for "drive media can't be removed".

Quoted 6 lines
Quoted 2 lines

The only way to create such a drive with -drive if=ide is media=cdrom. And that sets dinfo->media_cd, so just test that.

This is a less generic test and more prone to be broken inadvertently, so it seems like a step back.  What's the argument against the convoluted and explicit test?

My motivation for changing it was to reduce the uses of BlockDriverState member removable prior to nuking it from orbit [PATCH 45/55].

Quoted 8 lines
Quoted 3 lines

I consider my change an improvement, because I find "dinfo->media_cd" clearer than
"bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)".

This seems like an argument for providing a bdrv_supports_eject() or whatever we're actually trying to test for here. I kind of felt the same way as Andrzej when I saw this patch going past but it got lost in my mail folder...

Well, what are you trying to test for here?

Let's start with figuring out what we actually test for right now (may not be what you *want* to test for, but it's a start).  The test code is "bdrv_is_inserted(bs) && !bdrv_is_removable(bs)".

bdrv_is_removable() is a confused mess.  It is true when an ide-cd, scsi-cd or floppy qdev is attached, or when the BlockDriverState was created with -drive if={floppy,sd} or -drive if={ide,scsi,xen,none},media=cdrom, except when an ide-hd, scsi-hd, scsi-generic or virtio-blk qdev is attached.

Since we're about to attach a device here, no other device can be attached, and the mess simplifies into "when the BlockDriverState was created with -drive if={floppy,sd} or -drive if={ide,scsi,xen,none},media=cdrom".

Quoted 4 lines
Quoted 2 lines

Since we're getting IF_IDE, it further simplifies into "when the BlockDriverState was created with -drive if=ide,media=cdrom".

What's wrong with that again? All sounds sensible to me.

I'm not claiming otherwise, just double-checking this is what you want.

Quoted 7 lines
Quoted 2 lines

Which is
what my patch tests.

It's like if you changed the named constants/#defines in a project for their preprocessed values... Behaviour is preserved but it makes worse code, and its easier to break too, when someone updates the value of some constant.

Well, you just said you want to check whether the drive was created as CD-ROM. The most direct way to check precisely that is drive->media_cd, and I can't see how that could break.

If you can see how, please tell me.

If you want to check for something else, please explain what you want.

"Keep it the way it was" isn't an explanation. And keeping bdrv_is_removable() mess alive just for this one use would be silly.

[...]

#103 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Quoted 59 lines · Markus Armbruster
Quoted 47 lines · andrzej zaborowski
Quoted 46 lines · Markus Armbruster
Quoted 18 lines · Peter Maydell
Quoted 17 lines · Markus Armbruster
Quoted 6 lines · andrzej zaborowski
Quoted 5 lines · Markus Armbruster

We try the drive defined with -drive if=ide,index=0 (or equivalent sugar).  We use it only if (dinfo && bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)).  This is a convoluted way to test for "drive media can't be removed".

Quoted 6 lines
Quoted 2 lines

The only way to create such a drive with -drive if=ide is media=cdrom. And that sets dinfo->media_cd, so just test that.

This is a less generic test and more prone to be broken inadvertently, so it seems like a step back.  What's the argument against the convoluted and explicit test?

My motivation for changing it was to reduce the uses of BlockDriverState member removable prior to nuking it from orbit [PATCH 45/55].

Quoted 8 lines
Quoted 3 lines

I consider my change an improvement, because I find "dinfo->media_cd" clearer than
"bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)".

This seems like an argument for providing a bdrv_supports_eject() or whatever we're actually trying to test for here. I kind of felt the same way as Andrzej when I saw this patch going past but it got lost in my mail folder...

Well, what are you trying to test for here?

Let's start with figuring out what we actually test for right now (may not be what you *want* to test for, but it's a start).  The test code is "bdrv_is_inserted(bs) && !bdrv_is_removable(bs)".

bdrv_is_removable() is a confused mess.  It is true when an ide-cd, scsi-cd or floppy qdev is attached, or when the BlockDriverState was created with -drive if={floppy,sd} or -drive if={ide,scsi,xen,none},media=cdrom, except when an ide-hd, scsi-hd, scsi-generic or virtio-blk qdev is attached.

Since we're about to attach a device here, no other device can be attached, and the mess simplifies into "when the BlockDriverState was created with -drive if={floppy,sd} or -drive if={ide,scsi,xen,none},media=cdrom".

Quoted 4 lines
Quoted 2 lines

Since we're getting IF_IDE, it further simplifies into "when the BlockDriverState was created with -drive if=ide,media=cdrom".

What's wrong with that again?  All sounds sensible to me.

I'm not claiming otherwise, just double-checking this is what you want.

Quoted 3 lines
Quoted 2 lines

Which is
what my patch tests.

Quoted 7 lines
Quoted 4 lines

It's like if you changed the named constants/#defines in a project for their preprocessed values... Behaviour is preserved but it makes worse code, and its easier to break too, when someone updates the value of some constant.

Well, you just said you want to check whether the drive was created as CD-ROM.

I didn't say that, I want to check 1. if the underlaying device is shown as removable (support monitor eject, I guess), 2. if the underlaying storage can disappear for any other reason if that's possible to check.

And I said that it looks like the inner implementation of the _is_removable function is probably correct, but inlining it makes no sense to me. And the implementation shouldn't even bother us here. Reread the part you quoted.

Cheers

#104 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Quoted 65 lines · andrzej zaborowski
Quoted 64 lines · Markus Armbruster
Quoted 47 lines · andrzej zaborowski
Quoted 46 lines · Markus Armbruster
Quoted 18 lines · Peter Maydell
Quoted 17 lines · Markus Armbruster
Quoted 6 lines · andrzej zaborowski
Quoted 5 lines · Markus Armbruster

We try the drive defined with -drive if=ide,index=0 (or equivalent sugar).  We use it only if (dinfo && bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)).  This is a convoluted way to test for "drive media can't be removed".

Quoted 6 lines
Quoted 2 lines

The only way to create such a drive with -drive if=ide is media=cdrom. And that sets dinfo->media_cd, so just test that.

This is a less generic test and more prone to be broken inadvertently, so it seems like a step back.  What's the argument against the convoluted and explicit test?

My motivation for changing it was to reduce the uses of BlockDriverState member removable prior to nuking it from orbit [PATCH 45/55].

Quoted 8 lines
Quoted 3 lines

I consider my change an improvement, because I find "dinfo->media_cd" clearer than
"bdrv_is_inserted(dinfo->bdrv) && !bdrv_is_removable(dinfo->bdrv)".

This seems like an argument for providing a bdrv_supports_eject() or whatever we're actually trying to test for here. I kind of felt the same way as Andrzej when I saw this patch going past but it got lost in my mail folder...

Well, what are you trying to test for here?

Let's start with figuring out what we actually test for right now (may not be what you *want* to test for, but it's a start).  The test code is "bdrv_is_inserted(bs) && !bdrv_is_removable(bs)".

bdrv_is_removable() is a confused mess.  It is true when an ide-cd, scsi-cd or floppy qdev is attached, or when the BlockDriverState was created with -drive if={floppy,sd} or -drive if={ide,scsi,xen,none},media=cdrom, except when an ide-hd, scsi-hd, scsi-generic or virtio-blk qdev is attached.

Since we're about to attach a device here, no other device can be attached, and the mess simplifies into "when the BlockDriverState was created with -drive if={floppy,sd} or -drive if={ide,scsi,xen,none},media=cdrom".

Quoted 4 lines
Quoted 2 lines

Since we're getting IF_IDE, it further simplifies into "when the BlockDriverState was created with -drive if=ide,media=cdrom".

What's wrong with that again?  All sounds sensible to me.

I'm not claiming otherwise, just double-checking this is what you want.

Quoted 7 lines
Quoted 2 lines

Which is
what my patch tests.

It's like if you changed the named constants/#defines in a project for their preprocessed values... Behaviour is preserved but it makes worse code, and its easier to break too, when someone updates the value of some constant.

Quoted 5 lines
Quoted 2 lines

Well, you just said you want to check whether the drive was created as CD-ROM.

I didn't say that, I want to check 1. if the underlaying device is shown as removable (support monitor eject, I guess),

After PATCH 45 all of them do, without exception.

"eject" works unless a device model is attached that doesn't want its medium removed.

Before PATCH 45, "eject" sometimes refuses to work when no device model is attached (arbitrary, but harmless), and sometimes worked even when the attached device model doesn't want its medium removed (bug). See the patch description for details.

Quoted 3 lines

2. if the
underlaying storage can disappear for any other reason if that's possible to check.

bdrv_is_removable() *isn't* such a check. It happily returns false for -device if=ide,file=/dev/sr0. tosa and spitz will happily use it, as long as there's a CD in the drive during startup.

The block layer currently doesn't provide such a check. For what it's worth, IDE disks, SCSI disks and all the other non-removable decive models other than microdrives happily work without it.

If you want such a check, what about a patch?

Quoted 4 lines

And I said that it looks like the inner implementation of the _is_removable function is probably correct, but inlining it makes no sense to me. And the implementation shouldn't even bother us here. Reread the part you quoted.

#105 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Quoted 6 lines · Markus Armbruster
Quoted 3 lines · andrzej zaborowski

                                                     2. if the underlaying storage can disappear for any other reason if that's possible to check.

bdrv_is_removable() *isn't* such a check.

Obviously I wasn't claiming it is, just that it might be useful, but not necessrily possible. After all pretty much any storage can be "ejected" with enough force, depending on how far you want to go.

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

What's wrong with that again? All sounds sensible to me.

I'm not claiming otherwise, just double-checking this is what you want.

So first you said you had a problem with _is_removable, and then you said nothing was wrong with the implementation you outlined, plase make up your mind.

Cheers

Cheers

#106 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Quoted 12 lines · andrzej zaborowski
Quoted 6 lines · Markus Armbruster
Quoted 3 lines · andrzej zaborowski

                                                     2. if the underlaying storage can disappear for any other reason if that's possible to check.

bdrv_is_removable() *isn't* such a check.

Obviously I wasn't claiming it is, just that it might be useful, but not necessrily possible. After all pretty much any storage can be "ejected" with enough force, depending on how far you want to go.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

What's wrong with that again? All sounds sensible to me.

I'm not claiming otherwise, just double-checking this is what you want.

So first you said you had a problem with _is_removable, and then you said nothing was wrong with the implementation you outlined, plase make up your mind.

I don't appreciate you quoting me out of context like that.

The sentence you quoted was in the middle of my attempt to get you to explain what you're trying to accomplish there. I started with analyzing what your code does. You asked "what's wrong with that", and I replied that I'm not passing judgement on whether your code is right or wrong, I just want to know what you're trying to do.

I'm getting tired of our misunderstandings, but let me try one more time, and tiresomely verbose:

1. bdrv_is_removable() is ill-defined junk, and needs to go away.

It is true when an ide-cd, scsi-cd or floppy qdev is attached, or when the BlockDriverState was created with -drive if={floppy,sd} or -drive if={ide,scsi,xen,none},media=cdrom ("created removable"), except when an ide-hd, scsi-hd, scsi-generic or virtio-blk qdev is attached.

That's not semantics, that's an embarrassment.

2. I don't have an opinion on what should be done in tosa_microdrive_attach() and spitz_microdrive_attach(). Heck, I wouldn't touch it with a 9ft pole if I could help it. But I have to, because it uses bdrv_is_removable().

3. My first try was to preserve the current behavior. You don't like the result. Fine.

4. For my second try, I tried to understand what you're trying to accomplish there. Turns out you want to check whether "the underlaying storage can disappear for any other reason if that's possible to check." Thanks for explaining that.

5. Sorry, that's not what the code there does.

6. The block layer does not offer an API to check that. You're quite welcome to create it.

7. For what it's worth, IDE disks, SCSI disks and all the other non-removable decive models other than microdrives happily work without such a check.

What now?

#107 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Quoted 15 lines · Markus Armbruster
Quoted 14 lines · andrzej zaborowski
Quoted 6 lines · Markus Armbruster
Quoted 3 lines · andrzej zaborowski

                                                     2. if the underlaying storage can disappear for any other reason if that's possible to check.

bdrv_is_removable() *isn't* such a check.

Obviously I wasn't claiming it is, just that it might be useful, but not necessrily possible.  After all pretty much any storage can be "ejected" with enough force, depending on how far you want to go.

Quoted 2 lines
Quoted 2 lines
Quoted 1 line

What's wrong with that again?  All sounds sensible to me.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

I'm not claiming otherwise, just double-checking this is what you want.

So first you said you had a problem with _is_removable, and then you said nothing was wrong with the implementation you outlined, plase make up your mind.

I don't appreciate you quoting me out of context like that.

I got quite annoyed when you started putting words in my mouth by saying I said anything about CD-ROM.. the code in spitz/tosa is not concerned with CD-ROMs even if downstream it boils down to that, it is concerned with whether the device is removable or not, and that's what the check does. It doesn't help readability or anything by inlining that check. If you're trying to check for A then don't spell it out as B, be explicit. It's not a big deal but I just don't see the point, sorry.

Quoted 3 lines

The sentence you quoted was in the middle of my attempt to get you to explain what you're trying to accomplish there.

I already said about 3 times what it's trying to acomplish. You also have used the word "removable" so I'm sure you know what it means and don't need further explanation. But let's define it this way: if a GUI is going to display an "eject" button next to a drive in the qemu device tree, that's a removable device. CD-ROM is an example of that.
An IDE HDD is an example of something that's not going to have that
button (I assume).

Quoted 7 lines

 I started with
analyzing what your code does.  You asked "what's wrong with that", and I replied that I'm not passing judgement on whether your code is right or wrong, I just want to know what you're trying to do.

I'm getting tired of our misunderstandings, but let me try one more time, and tiresomely verbose:

Yes, same here.

Quoted 2 lines

1. bdrv_is_removable() is ill-defined junk, and needs to go away.

Then replace it with something else, I don't know. Your original argument was it was too convoluted.

Quoted 8 lines

  It is true when an ide-cd, scsi-cd or floppy qdev is attached, or when the BlockDriverState was created with -drive if={floppy,sd} or -drive if={ide,scsi,xen,none},media=cdrom ("created removable"), except when an ide-hd, scsi-hd, scsi-generic or virtio-blk qdev is attached.

  That's not semantics, that's an embarrassment.

Certainly if it can be made simpler then by all means let's make it simpler, but world is complicated like that.

Quoted 6 lines

2. I don't have an opinion on what should be done in tosa_microdrive_attach() and spitz_microdrive_attach().  Heck, I wouldn't touch it with a 9ft pole if I could help it.  But I have to, because it uses bdrv_is_removable().

Quoted 7 lines

3. My first try was to preserve the current behavior.  You don't like the result.  Fine.

4. For my second try, I tried to understand what you're trying to accomplish there.  Turns out you want to check whether "the underlaying storage can disappear for any other reason if that's possible to check."  Thanks for explaining that.

No, I shouldn't have mentioned that, it's a lateral "would be nice to have" issue and as I said it's likely not worth it.

Quoted 5 lines

5. Sorry, that's not what the code there does.

6. The block layer does not offer an API to check that.  You're quite welcome to create it.

See above.

Quoted 4 lines

7. For what it's worth, IDE disks, SCSI disks and all the other non-removable decive models other than microdrives happily work without such a check.

Yes, removing the check would probably make more sense than you current approach, but then I don't see a particular need for it either. (You can remove other checks like writing beyond the end of device and stuff will keep working.)

And you have used the word "non-removable" so you must know what it means, why then are you asking about the intention of that if ()?

Cheers

#108 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Am 03.08.2011 22:20, schrieb andrzej zaborowski:

Quoted 37 lines · Markus Armbruster
Quoted 15 lines
Quoted 14 lines · andrzej zaborowski
Quoted 6 lines · Markus Armbruster
Quoted 3 lines · andrzej zaborowski

2. if the
underlaying storage can disappear for any other reason if that's possible to check.

bdrv_is_removable() *isn't* such a check.

Obviously I wasn't claiming it is, just that it might be useful, but not necessrily possible. After all pretty much any storage can be "ejected" with enough force, depending on how far you want to go.

Quoted 2 lines
Quoted 2 lines
Quoted 1 line

What's wrong with that again? All sounds sensible to me.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

I'm not claiming otherwise, just double-checking this is what you want.

So first you said you had a problem with _is_removable, and then you said nothing was wrong with the implementation you outlined, plase make up your mind.

I don't appreciate you quoting me out of context like that.

I got quite annoyed when you started putting words in my mouth by saying I said anything about CD-ROM.. the code in spitz/tosa is not concerned with CD-ROMs even if downstream it boils down to that, it is concerned with whether the device is removable or not, and that's what the check does. It doesn't help readability or anything by inlining that check. If you're trying to check for A then don't spell it out as B, be explicit. It's not a big deal but I just don't see the point, sorry.

Quoted 3 lines

The sentence you quoted was in the middle of my attempt to get you to explain what you're trying to accomplish there.

Quoted 7 lines

I already said about 3 times what it's trying to acomplish. You also have used the word "removable" so I'm sure you know what it means and don't need further explanation. But let's define it this way: if a GUI is going to display an "eject" button next to a drive in the qemu device tree, that's a removable device. CD-ROM is an example of that.
An IDE HDD is an example of something that's not going to have that
button (I assume).

But this is a property of the device, not of the backend. This means that it belongs in the device emulation and not in block.c.

If you want to have a function spitz_microdrive_is_removable() or similar in the device model I don't really mind (even though I don't see the point), but the block layer is the wrong place for it.

Kevin

#109 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Quoted 46 lines · Kevin Wolf

Am 03.08.2011 22:20, schrieb andrzej zaborowski:

Quoted 37 lines · Markus Armbruster
Quoted 15 lines
Quoted 14 lines · andrzej zaborowski
Quoted 6 lines · Markus Armbruster
Quoted 3 lines · andrzej zaborowski

                                                     2. if the underlaying storage can disappear for any other reason if that's possible to check.

bdrv_is_removable() *isn't* such a check.

Obviously I wasn't claiming it is, just that it might be useful, but not necessrily possible.  After all pretty much any storage can be "ejected" with enough force, depending on how far you want to go.

Quoted 2 lines
Quoted 2 lines
Quoted 1 line

What's wrong with that again?  All sounds sensible to me.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

I'm not claiming otherwise, just double-checking this is what you want.

So first you said you had a problem with _is_removable, and then you said nothing was wrong with the implementation you outlined, plase make up your mind.

I don't appreciate you quoting me out of context like that.

I got quite annoyed when you started putting words in my mouth by saying I said anything about CD-ROM.. the code in spitz/tosa is not concerned with CD-ROMs even if downstream it boils down to that, it is concerned with whether the device is removable or not, and that's what the check does.  It doesn't help readability or anything by inlining that check.  If you're trying to check for A then don't spell it out as B, be explicit.  It's not a big deal but I just don't see the point, sorry.

Quoted 3 lines

The sentence you quoted was in the middle of my attempt to get you to explain what you're trying to accomplish there.

Quoted 7 lines

I already said about 3 times what it's trying to acomplish.  You also have used the word "removable" so I'm sure you know what it means and don't need further explanation.  But let's define it this way: if a GUI is going to display an "eject" button next to a drive in the qemu device tree, that's a removable device.  CD-ROM is an example of that. An IDE HDD is an example of something that's not going to have that button (I assume).

Quoted 2 lines

But this is a property of the device, not of the backend. This means that it belongs in the device emulation and not in block.c.

By device do you mean hw/ide/microdrive.c? I'm not saying it belongs in block.c, but logically it belongs in the same place as bdrv_is_inserted, bdrv_is_locked, bdrv_eject etc. no? So it is a property of whatever "media" is property of.

Quoted 4 lines

If you want to have a function spitz_microdrive_is_removable() or similar in the device model I don't really mind (even though I don't see the point), but the block layer is the wrong place for it.

Cheers
(I'm slow to reply because I'm on travel)

#110 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Am 09.08.2011 06:32, schrieb andrzej zaborowski:

Quoted 47 lines · Kevin Wolf
Quoted 46 lines

Am 03.08.2011 22:20, schrieb andrzej zaborowski:

Quoted 44 lines · Markus Armbruster
Quoted 15 lines
Quoted 14 lines · andrzej zaborowski
Quoted 6 lines · Markus Armbruster
Quoted 3 lines · andrzej zaborowski

2. if the
underlaying storage can disappear for any other reason if that's possible to check.

bdrv_is_removable() *isn't* such a check.

Obviously I wasn't claiming it is, just that it might be useful, but not necessrily possible. After all pretty much any storage can be "ejected" with enough force, depending on how far you want to go.

Quoted 2 lines
Quoted 2 lines
Quoted 1 line

What's wrong with that again? All sounds sensible to me.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

I'm not claiming otherwise, just double-checking this is what you want.

So first you said you had a problem with _is_removable, and then you said nothing was wrong with the implementation you outlined, plase make up your mind.

I don't appreciate you quoting me out of context like that.

I got quite annoyed when you started putting words in my mouth by saying I said anything about CD-ROM.. the code in spitz/tosa is not concerned with CD-ROMs even if downstream it boils down to that, it is concerned with whether the device is removable or not, and that's what the check does. It doesn't help readability or anything by inlining that check. If you're trying to check for A then don't spell it out as B, be explicit. It's not a big deal but I just don't see the point, sorry.

Quoted 3 lines

The sentence you quoted was in the middle of my attempt to get you to explain what you're trying to accomplish there.

I already said about 3 times what it's trying to acomplish. You also have used the word "removable" so I'm sure you know what it means and don't need further explanation. But let's define it this way: if a GUI is going to display an "eject" button next to a drive in the qemu device tree, that's a removable device. CD-ROM is an example of that.
An IDE HDD is an example of something that's not going to have that
button (I assume).

Quoted 4 lines
Quoted 2 lines

But this is a property of the device, not of the backend. This means that it belongs in the device emulation and not in block.c.

By device do you mean hw/ide/microdrive.c?

I mean just anything in hw/. I'm not familiar with how these devices work internally, so if hw/ide/microdrive.c is the right place, fine.

Quoted 4 lines

I'm not saying it belongs
in block.c, but logically it belongs in the same place as bdrv_is_inserted, bdrv_is_locked, bdrv_eject etc. no? So it is a property of whatever "media" is property of.

Depends. As long as you're talking about purely virtual device state, I would say yes, they belong there, too. However, we have things like CD-ROM passthrough where bdrv_is_inserted etc. are supposed to return the status of the physical host device. These are host state and need to be in the block layer.

Kevin

#111 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Quoted 48 lines · Kevin Wolf

Am 09.08.2011 06:32, schrieb andrzej zaborowski:

Quoted 47 lines · Kevin Wolf
Quoted 46 lines

Am 03.08.2011 22:20, schrieb andrzej zaborowski:

Quoted 44 lines · Markus Armbruster
Quoted 15 lines
Quoted 14 lines · andrzej zaborowski
Quoted 6 lines · Markus Armbruster
Quoted 3 lines · andrzej zaborowski

2. if the
underlaying storage can disappear for any other reason if that's possible to check.

bdrv_is_removable() *isn't* such a check.

Obviously I wasn't claiming it is, just that it might be useful, but not necessrily possible. After all pretty much any storage can be "ejected" with enough force, depending on how far you want to go.

Quoted 2 lines
Quoted 2 lines
Quoted 1 line

What's wrong with that again? All sounds sensible to me.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

I'm not claiming otherwise, just double-checking this is what you want.

So first you said you had a problem with _is_removable, and then you said nothing was wrong with the implementation you outlined, plase make up your mind.

I don't appreciate you quoting me out of context like that.

I got quite annoyed when you started putting words in my mouth by saying I said anything about CD-ROM.. the code in spitz/tosa is not concerned with CD-ROMs even if downstream it boils down to that, it is concerned with whether the device is removable or not, and that's what the check does. It doesn't help readability or anything by inlining that check. If you're trying to check for A then don't spell it out as B, be explicit. It's not a big deal but I just don't see the point, sorry.

Quoted 3 lines

The sentence you quoted was in the middle of my attempt to get you to explain what you're trying to accomplish there.

I already said about 3 times what it's trying to acomplish. You also have used the word "removable" so I'm sure you know what it means and don't need further explanation. But let's define it this way: if a GUI is going to display an "eject" button next to a drive in the qemu device tree, that's a removable device. CD-ROM is an example of that.
An IDE HDD is an example of something that's not going to have that
button (I assume).

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

But this is a property of the device, not of the backend. This means that it belongs in the device emulation and not in block.c.

By device do you mean hw/ide/microdrive.c?

I mean just anything in hw/. I'm not familiar with how these devices work internally, so if hw/ide/microdrive.c is the right place, fine.

We have way too many things we call "device", or "driver"... Let me try to dispell the confusion.

Machines spitz, borzoi, terrier (hw/spitz.c) have a spitz microdrive device. Machine tosa (hw/tosa.c) has a tosa microdrive device.

Like all block devices, they consist of a frontend and a backend.

Our block device backend abstraction is BlockDriverState. Internally, a block device backend is the block layer sitting on top of an optional BlockDriver. No block driver is interpreted as no medium.

Monitor command "eject" drops the block driver.

Monitor command "change" first drops the block driver, then attaches a new one.

A BlockDriver may support removable media. Host device block drivers do, the others don't. Doesn't affect monitor commands eject and change at all, because these *drop* the block driver.

A block device frontend is attached to a backend on initialization. In other words, a backend may exist alone, but a frontend is always attached to a backend.

A block device frontend may support removable media. If it does, monitor commands eject and change work while the frontend is attached. They always work while no frontend is attached.

Note: we got *two* different "removables" here, frontend and backend. It is possible to back a removable frontend, say ide-cd, with a non-removable backend, say file. The opposite is also possible (whether it's wise is a different question).

The frontend code is in hw/ide/microdrive.c. It does *not* support removable media. Monitor commands eject and change refuse to touch this device.

The property "monitor command eject is applicable to the device" is a property of the frontend, because the frontend and only the frontend decide whether eject is okay.

The property 'GUI shall display an "eject" button next to a drive in the qemu device tree' is just the same.

Kevin wrote "this is a property of the device, not of the backend. This means that it belongs in the device emulation and not in block.c." Kevin is correct. The code is also correct: microdrive.c defines a non-removable device (by not implementing callback change_media_cb()). Therefore, monitor commands eject and change will not touch microdrives.

The checks in spitz_microdrive_attach() and tosa_microdrive_attach() do *not* contribute to that. All they accomplish is silently refrain from creating the device if the user created the first IDE drive (if=ide,index=0) with media=cd. No more, no less.

I have no opinion on whether that's useful.

If you want to keep it, take my PATCH 44/55 as is.

If you want to create the drive regardless of media=cd, tell me, and I'll do exactly that in my respin.

Quoted 10 lines
Quoted 4 lines

I'm not saying it belongs
in block.c, but logically it belongs in the same place as bdrv_is_inserted, bdrv_is_locked, bdrv_eject etc. no? So it is a property of whatever "media" is property of.

Depends. As long as you're talking about purely virtual device state, I would say yes, they belong there, too. However, we have things like CD-ROM passthrough where bdrv_is_inserted etc. are supposed to return the status of the physical host device. These are host state and need to be in the block layer.

bdrv_is_inserted() is about the backend's media. At least it is after my PATCH 21/55 cleans up the mess I made in commit 4be9762a.

bdrv_eject() is a function frontends supporting removable media call to notify backends of medium eject/load. If the backend happens to use a host device block driver, it'll eject/load the physical medium.

Likewise, bdrv_set_locked() is a function frontends supporting removable media call to notify backends of medium lock/unlock. If the backend happens to use a host device block driver, it'll lock/unlock the physical medium.

bdrv_is_locked() is about the frontend's media lock. To make this more obvious, my PATCH 29/55 replaces it by bdrv_dev_is_medium_locked(). It does *not* query the backend's lock (which may not even exist!) set by bdrv_set_locked().

#112 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Am 09.08.2011 13:56, schrieb Markus Armbruster:

Quoted 4 lines

bdrv_is_locked() is about the frontend's media lock. To make this more obvious, my PATCH 29/55 replaces it by bdrv_dev_is_medium_locked(). It does *not* query the backend's lock (which may not even exist!) set by bdrv_set_locked().

This sounds wrong (the behaviour, not your analysis). Do you plan to remove bdrv_dev_is_medium_locked() as well? It is used by IDE and scsi-disk (easy to replace) and in eject_device() in blockdev.c. Maybe the 'eject' monitor command should be handled by another callback into the device.

Kevin

#113 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Quoted 11 lines · Kevin Wolf

Am 09.08.2011 13:56, schrieb Markus Armbruster:

Quoted 4 lines

bdrv_is_locked() is about the frontend's media lock. To make this more obvious, my PATCH 29/55 replaces it by bdrv_dev_is_medium_locked(). It does *not* query the backend's lock (which may not even exist!) set by bdrv_set_locked().

This sounds wrong (the behaviour, not your analysis). Do you plan to remove bdrv_dev_is_medium_locked() as well? It is used by IDE and scsi-disk (easy to replace) and in eject_device() in blockdev.c. Maybe the 'eject' monitor command should be handled by another callback into the device.

Just two users remain after my series:

* bdrv_info()

It wants to show the frontend's lock state, and uses bdrv_dev_is_medium_locked() to get it from the frontend.

* eject_device()

It needs to fail if the frontend has its medium locked. It uses bdrv_dev_is_medium_locked() to get the lock state from the frontend. Pseudo code (-f glossed over for simplicity):

unless frontend has removable media
fail
if frontend medium is not already open
and frontend medium is locked
fail
drop the block driver

I considered replacing this with "ask the frontend to eject", and then let the frontend decide whether to order the backend to drop the block driver. I decided against it, since I need bdrv_dev_is_medium_locked() anyway for bdrv_info(). Besides, the series is long enough already.

#114 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

Am 09.08.2011 14:36, schrieb Markus Armbruster:

Quoted 14 lines
Quoted 11 lines · Kevin Wolf

Am 09.08.2011 13:56, schrieb Markus Armbruster:

Quoted 4 lines

bdrv_is_locked() is about the frontend's media lock. To make this more obvious, my PATCH 29/55 replaces it by bdrv_dev_is_medium_locked(). It does *not* query the backend's lock (which may not even exist!) set by bdrv_set_locked().

This sounds wrong (the behaviour, not your analysis). Do you plan to remove bdrv_dev_is_medium_locked() as well? It is used by IDE and scsi-disk (easy to replace) and in eject_device() in blockdev.c. Maybe the 'eject' monitor command should be handled by another callback into the device.

Quoted 6 lines

Just two users remain after my series:

* bdrv_info()

It wants to show the frontend's lock state, and uses bdrv_dev_is_medium_locked() to get it from the frontend.

bdrv_info() is a nasty reason for keeping this in the block layer.

Quoted 13 lines

* eject_device()

It needs to fail if the frontend has its medium locked. It uses bdrv_dev_is_medium_locked() to get the lock state from the frontend. Pseudo code (-f glossed over for simplicity):

unless frontend has removable media
fail
if frontend medium is not already open
and frontend medium is locked
fail
drop the block driver

Quoted 5 lines

I considered replacing this with "ask the frontend to eject", and then let the frontend decide whether to order the backend to drop the block driver. I decided against it, since I need bdrv_dev_is_medium_locked() anyway for bdrv_info(). Besides, the series is long enough already.

I'm not asking to change it in this series, but in general I think it would be a good change to make.

Kevin

#115 lore raw

Re: [Qemu-devel] [PATCH 44/55] spitz tosa: Simplify "drive is suitable for microdrive" test

In reply to andrzej zaborowski
Quoted 18 lines · andrzej zaborowski
Quoted 17 lines · Markus Armbruster
Quoted 16 lines · andrzej zaborowski
Quoted 6 lines · Markus Armbruster
Quoted 3 lines · andrzej zaborowski

                                                     2. if the underlaying storage can disappear for any other reason if that's possible to check.

bdrv_is_removable() *isn't* such a check.

Obviously I wasn't claiming it is, just that it might be useful, but not necessrily possible.  After all pretty much any storage can be "ejected" with enough force, depending on how far you want to go.

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

What's wrong with that again?  All sounds sensible to me.

I'm not claiming otherwise, just double-checking this is what you want.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

So first you said you had a problem with _is_removable, and then you said nothing was wrong with the implementation you outlined, plase make up your mind.

I don't appreciate you quoting me out of context like that.

I got quite annoyed when you started putting words in my mouth by saying I said anything about CD-ROM.. the code in spitz/tosa is not

I didn't intend to annoy. It was an honest attempt to figure out what you want to accomplish there. Sorry it came out the wrong way.

[...]

#116 lore raw

PATCH 45/55 block: Clean up remaining users of "removable"

In reply to Markus Armbruster

BlockDriverState member removable is a confused mess. It is true when an ide-cd, scsi-cd or floppy qdev is attached, or when the BlockDriverState was created with -drive if={floppy,sd} or -drive if={ide,scsi,xen,none},media=cdrom ("created removable"), except when an ide-hd, scsi-hd, scsi-generic or virtio-blk qdev is attached.

Three users remain:

1. eject_device(), via bdrv_is_removable() uses it to determine whether a block device can eject media.

2. bdrv_info() is monitor command "info block". QMP documentation says "true if the device is removable, false otherwise". From the monitor user's point of view, the only sensible interpretation of "is removable" is "can eject media with monitor commands eject and change".

A block device can eject media unless a device is attached that doesn't support it. Switch the two users over to new bdrv_dev_has_removable_media() that returns exactly that.

3. bdrv_getlength() uses to suppress its length cache when media can change (see commit 46a4e4e6). Media change is either monitor command change (updates the length cache), monitor command eject (doesn't update the length cache, easily fixable), or physical media change (invalidates length cache, not so easily fixable).

I'm refraining from improving anything here, because this series is long enough already. Instead, I simply switch it over to bdrv_dev_has_removable_media() as well.

This changes the behavior of the length cache and of monitor commands eject and change in two cases:

a. drive not created removable, no device attached

The commit makes the drive removable, and defeats the length cache.

Example: -drive if=none

b. drive created removable, but the attached drive is non-removable,
and doesn't call bdrv_set_removable(..., 0) (most devices don't)

The commit makes the drive non-removable, and enables the length cache.

Example: -drive if=xen,media=cdrom -M xenpv

The other non-removable devices that don't call bdrv_set_removable() can't currently use a drive created removable, either because they aren't qdevified, or because they lack a drive property. Won't stay that way.

---
 block.c        |   18 +++++++++++-------
 block.h        |    3 ++-
 blockdev.c     |    2 +-
 hw/scsi-disk.c |    5 +++++
 4 files changed, 19 insertions(+), 9 deletions(-)
block.c +11 -7
index 9591c8a..b394f17 100644
@@ -750,6 +750,9 @@ void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
 {
     bs->dev_ops = ops;
     bs->dev_opaque = opaque;
+    if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) {
+        bs_snapshots = NULL;
+    }
 }
 
 static void bdrv_dev_change_media_cb(BlockDriverState *bs)
@@ -759,6 +762,11 @@ static void bdrv_dev_change_media_cb(BlockDriverState *bs)
     }
 }
 
+int bdrv_dev_has_removable_media(BlockDriverState *bs)
+{
+    return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
+}
+
 bool bdrv_dev_is_medium_ejected(BlockDriverState *bs)
 {
     if (bs->dev_ops && bs->dev_ops->is_medium_ejected) {
@@ -1198,7 +1206,7 @@ int64_t bdrv_getlength(BlockDriverState *bs)
     if (!drv)
         return -ENOMEDIUM;
 
-    if (bs->growable || bs->removable) {
+    if (bs->growable || bdrv_dev_has_removable_media(bs)) {
         if (drv->bdrv_getlength) {
             return drv->bdrv_getlength(bs);
         }
@@ -1483,11 +1491,6 @@ void bdrv_set_removable(BlockDriverState *bs, int removable)
     }
 }
 
-int bdrv_is_removable(BlockDriverState *bs)
-{
-    return bs->removable;
-}
-
 int bdrv_is_read_only(BlockDriverState *bs)
 {
     return bs->read_only;
@@ -1765,7 +1768,8 @@ void bdrv_info(Monitor *mon, QObject **ret_data)
 
         bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
                                     "'removable': %i, 'locked': %i }",
-                                    bs->device_name, bs->removable,
+                                    bs->device_name,
+                                    bdrv_dev_has_removable_media(bs),
                                     bdrv_dev_is_medium_locked(bs));
         bs_dict = qobject_to_qdict(bs_obj);
 
block.h +2 -1
index b034d51..f6fa3d0 100644
@@ -33,6 +33,7 @@ typedef struct BlockDevOps {
      * Runs when virtual media changed (monitor commands eject, change)
      * Beware: doesn't run when a host device's physical media
      * changes.  Sure would be useful if it did.
+     * Device models with removable media must implement this callback.
      */
     void (*change_media_cb)(void *opaque);
     /*
@@ -102,6 +103,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);
+int bdrv_dev_has_removable_media(BlockDriverState *bs);
 bool bdrv_dev_is_medium_ejected(BlockDriverState *bs);
 int bdrv_dev_is_medium_locked(BlockDriverState *bs);
 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
@@ -207,7 +209,6 @@ void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
                        BlockErrorAction on_write_error);
 BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read);
 void bdrv_set_removable(BlockDriverState *bs, int removable);
-int bdrv_is_removable(BlockDriverState *bs);
 int bdrv_is_read_only(BlockDriverState *bs);
 int bdrv_is_sg(BlockDriverState *bs);
 int bdrv_enable_write_cache(BlockDriverState *bs);
blockdev.c +1 -1
index 4c58128..06a82d3 100644
@@ -645,7 +645,7 @@ out:
 
 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
 {
-    if (!bdrv_is_removable(bs)) {
+    if (!bdrv_dev_has_removable_media(bs)) {
         qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
         return -1;
     }
hw/scsi-disk.c +5 -0
index ad63814..ae194c5 100644
@@ -1276,6 +1276,10 @@ static void scsi_destroy(SCSIDevice *dev)
     blockdev_mark_auto_del(s->qdev.conf.bs);
 }
 
+static void scsi_cd_change_media_cb(void *opaque)
+{
+}
+
 static bool scsi_cd_is_medium_ejected(void *opaque)
 {
     return ((SCSIDiskState *)opaque)->tray_open;
@@ -1287,6 +1291,7 @@ static bool scsi_cd_is_medium_locked(void *opaque)
 }
 
 static const BlockDevOps scsi_cd_block_ops = {
+    .change_media_cb = scsi_cd_change_media_cb,
     .is_medium_ejected = scsi_cd_is_medium_ejected,
     .is_medium_locked = scsi_cd_is_medium_locked,
 };
Signature · 1 line
1.7.2.3
#117 lore raw

Re: [Qemu-devel] [PATCH 45/55] block: Clean up remaining users of "removable"

Quoted 46 lines · On Wed, 20 Jul 2011 18:24:19 +0200 Markus Armbruster <[email protected]> wrote:

BlockDriverState member removable is a confused mess. It is true when an ide-cd, scsi-cd or floppy qdev is attached, or when the BlockDriverState was created with -drive if={floppy,sd} or -drive if={ide,scsi,xen,none},media=cdrom ("created removable"), except when an ide-hd, scsi-hd, scsi-generic or virtio-blk qdev is attached.

Three users remain:

1. eject_device(), via bdrv_is_removable() uses it to determine whether a block device can eject media.

2. bdrv_info() is monitor command "info block". QMP documentation says "true if the device is removable, false otherwise". From the monitor user's point of view, the only sensible interpretation of "is removable" is "can eject media with monitor commands eject and change".

A block device can eject media unless a device is attached that doesn't support it. Switch the two users over to new bdrv_dev_has_removable_media() that returns exactly that.

3. bdrv_getlength() uses to suppress its length cache when media can change (see commit 46a4e4e6). Media change is either monitor command change (updates the length cache), monitor command eject (doesn't update the length cache, easily fixable), or physical media change (invalidates length cache, not so easily fixable).

I'm refraining from improving anything here, because this series is long enough already. Instead, I simply switch it over to bdrv_dev_has_removable_media() as well.

This changes the behavior of the length cache and of monitor commands eject and change in two cases:

a. drive not created removable, no device attached

The commit makes the drive removable, and defeats the length cache.

Example: -drive if=none

b. drive created removable, but the attached drive is non-removable,
and doesn't call bdrv_set_removable(..., 0) (most devices don't)

The commit makes the drive non-removable, and enables the length cache.

Quoted 8 lines

Example: -drive if=xen,media=cdrom -M xenpv

The other non-removable devices that don't call bdrv_set_removable() can't currently use a drive created removable, either because they aren't qdevified, or because they lack a drive property. Won't stay that way.

Looks good to me.

Quoted 128 lines
---
 block.c        |   18 +++++++++++-------
 block.h        |    3 ++-
 blockdev.c     |    2 +-
 hw/scsi-disk.c |    5 +++++
 4 files changed, 19 insertions(+), 9 deletions(-)
block.c +11 -7
index 9591c8a..b394f17 100644
@@ -750,6 +750,9 @@ void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
 {
     bs->dev_ops = ops;
     bs->dev_opaque = opaque;
+    if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) {
+        bs_snapshots = NULL;
+    }
 }
 
 static void bdrv_dev_change_media_cb(BlockDriverState *bs)
@@ -759,6 +762,11 @@ static void bdrv_dev_change_media_cb(BlockDriverState *bs)
     }
 }
 
+int bdrv_dev_has_removable_media(BlockDriverState *bs)
+{
+    return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
+}
+
 bool bdrv_dev_is_medium_ejected(BlockDriverState *bs)
 {
     if (bs->dev_ops && bs->dev_ops->is_medium_ejected) {
@@ -1198,7 +1206,7 @@ int64_t bdrv_getlength(BlockDriverState *bs)
     if (!drv)
         return -ENOMEDIUM;
 
-    if (bs->growable || bs->removable) {
+    if (bs->growable || bdrv_dev_has_removable_media(bs)) {
         if (drv->bdrv_getlength) {
             return drv->bdrv_getlength(bs);
         }
@@ -1483,11 +1491,6 @@ void bdrv_set_removable(BlockDriverState *bs, int removable)
     }
 }
 
-int bdrv_is_removable(BlockDriverState *bs)
-{
-    return bs->removable;
-}
-
 int bdrv_is_read_only(BlockDriverState *bs)
 {
     return bs->read_only;
@@ -1765,7 +1768,8 @@ void bdrv_info(Monitor *mon, QObject **ret_data)
 
         bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
                                     "'removable': %i, 'locked': %i }",
-                                    bs->device_name, bs->removable,
+                                    bs->device_name,
+                                    bdrv_dev_has_removable_media(bs),
                                     bdrv_dev_is_medium_locked(bs));
         bs_dict = qobject_to_qdict(bs_obj);
 
block.h +2 -1
index b034d51..f6fa3d0 100644
@@ -33,6 +33,7 @@ typedef struct BlockDevOps {
      * Runs when virtual media changed (monitor commands eject, change)
      * Beware: doesn't run when a host device's physical media
      * changes.  Sure would be useful if it did.
+     * Device models with removable media must implement this callback.
      */
     void (*change_media_cb)(void *opaque);
     /*
@@ -102,6 +103,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);
+int bdrv_dev_has_removable_media(BlockDriverState *bs);
 bool bdrv_dev_is_medium_ejected(BlockDriverState *bs);
 int bdrv_dev_is_medium_locked(BlockDriverState *bs);
 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
@@ -207,7 +209,6 @@ void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
                        BlockErrorAction on_write_error);
 BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read);
 void bdrv_set_removable(BlockDriverState *bs, int removable);
-int bdrv_is_removable(BlockDriverState *bs);
 int bdrv_is_read_only(BlockDriverState *bs);
 int bdrv_is_sg(BlockDriverState *bs);
 int bdrv_enable_write_cache(BlockDriverState *bs);
blockdev.c +1 -1
index 4c58128..06a82d3 100644
@@ -645,7 +645,7 @@ out:
 
 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
 {
-    if (!bdrv_is_removable(bs)) {
+    if (!bdrv_dev_has_removable_media(bs)) {
         qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
         return -1;
     }
hw/scsi-disk.c +5 -0
index ad63814..ae194c5 100644
@@ -1276,6 +1276,10 @@ static void scsi_destroy(SCSIDevice *dev)
     blockdev_mark_auto_del(s->qdev.conf.bs);
 }
 
+static void scsi_cd_change_media_cb(void *opaque)
+{
+}
+
 static bool scsi_cd_is_medium_ejected(void *opaque)
 {
     return ((SCSIDiskState *)opaque)->tray_open;
@@ -1287,6 +1291,7 @@ static bool scsi_cd_is_medium_locked(void *opaque)
 }
 
 static const BlockDevOps scsi_cd_block_ops = {
+    .change_media_cb = scsi_cd_change_media_cb,
     .is_medium_ejected = scsi_cd_is_medium_ejected,
     .is_medium_locked = scsi_cd_is_medium_locked,
 };
#119 lore raw

PATCH 46/55 block: Drop BlockDriverState member removable

In reply to Markus Armbruster

It's a confused mess (see previous commit). No users remain.

---
 block.c           |    8 --------
 block.h           |    1 -
 block_int.h       |    1 -
 blockdev.c        |    5 -----
 hw/fdc.c          |    1 -
 hw/ide/core.c     |    1 -
 hw/scsi-disk.c    |    1 -
 hw/scsi-generic.c |    1 -
 hw/virtio-blk.c   |    1 -
 9 files changed, 0 insertions(+), 20 deletions(-)
block.c +0 -8
index b394f17..925861f 100644
@@ -1483,14 +1483,6 @@ BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
     return is_read ? bs->on_read_error : bs->on_write_error;
 }
 
-void bdrv_set_removable(BlockDriverState *bs, int removable)
-{
-    bs->removable = removable;
-    if (removable && bs == bs_snapshots) {
-        bs_snapshots = NULL;
-    }
-}
-
 int bdrv_is_read_only(BlockDriverState *bs)
 {
     return bs->read_only;
block.h +0 -1
index f6fa3d0..b56dae6 100644
@@ -208,7 +208,6 @@ int bdrv_get_translation_hint(BlockDriverState *bs);
 void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
                        BlockErrorAction on_write_error);
 BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read);
-void bdrv_set_removable(BlockDriverState *bs, int removable);
 int bdrv_is_read_only(BlockDriverState *bs);
 int bdrv_is_sg(BlockDriverState *bs);
 int bdrv_enable_write_cache(BlockDriverState *bs);
block_int.h +0 -1
index d8688b8..4bc91cb 100644
@@ -146,7 +146,6 @@ struct BlockDriverState {
     int read_only; /* if true, the media is read only */
     int keep_read_only; /* if true, the media was requested to stay read only */
     int open_flags; /* flags used to open the file, re-used for re-open */
-    int removable; /* if true, the media can be removed */
     int encrypted; /* if true, the media is encrypted */
     int valid_key; /* if true, a valid encryption key has been set */
     int sg;        /* if true, the device is a /dev/sg* */
blockdev.c +0 -5
index 06a82d3..9284afd 100644
@@ -482,17 +482,12 @@ DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
             }
 	    break;
 	case MEDIA_CDROM:
-            bdrv_set_removable(dinfo->bdrv, 1);
             dinfo->media_cd = 1;
 	    break;
 	}
         break;
     case IF_SD:
-        /* FIXME: This isn't really a floppy, but it's a reasonable
-           approximation.  */
     case IF_FLOPPY:
-        bdrv_set_removable(dinfo->bdrv, 1);
-        break;
     case IF_PFLASH:
     case IF_MTD:
         break;
hw/fdc.c +0 -1
index 6854e15..debcd89 100644
@@ -1813,7 +1813,6 @@ static int fdctrl_connect_drives(FDCtrl *fdctrl)
         fd_revalidate(drive);
         if (drive->bs) {
             drive->media_changed = 1;
-            bdrv_set_removable(drive->bs, 1);
             bdrv_set_dev_ops(drive->bs, &fdctrl_block_ops, drive);
         }
     }
hw/ide/core.c +0 -1
index 90ad497..2bde2b8 100644
@@ -1844,7 +1844,6 @@ int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind,
     }
 
     ide_reset(s);
-    bdrv_set_removable(bs, s->drive_kind == IDE_CD);
     if (s->drive_kind == IDE_CD) {
         bdrv_lock_medium(bs, s->tray_locked);
         bdrv_eject(bs, s->tray_open);
hw/scsi-disk.c +0 -1
index ae194c5..f36f529 100644
@@ -1339,7 +1339,6 @@ static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)
 
     s->qdev.type = TYPE_DISK;
     qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s);
-    bdrv_set_removable(s->bs, kind == SCSI_CD);
     if (kind == SCSI_CD) {
         bdrv_lock_medium(s->bs, s->tray_locked);
         bdrv_eject(s->bs, s->tray_open);
hw/scsi-generic.c +0 -1
index 8e59c7e..24ca9ff 100644
@@ -533,7 +533,6 @@ static int scsi_generic_initfn(SCSIDevice *dev)
     DPRINTF("block size %d\n", s->qdev.blocksize);
     s->driver_status = 0;
     memset(s->sensebuf, 0, sizeof(s->sensebuf));
-    bdrv_set_removable(s->bs, 0);
     return 0;
 }
 
hw/virtio-blk.c +0 -1
index dc0b692..4ed1975 100644
@@ -583,7 +583,6 @@ VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf,
     s->qdev = dev;
     register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
                     virtio_blk_save, virtio_blk_load, s);
-    bdrv_set_removable(s->bs, 0);
     bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
     s->bs->buffer_alignment = conf->logical_block_size;
 
Signature · 1 line
1.7.2.3
#121 lore raw

PATCH 47/55 block: Move BlockConf & friends from block_int.h to block.h

In reply to Markus Armbruster

It's convenience stuff for block device models, so block.h isn't the ideal home either, but better than block_int.h.

Permits moving some #include "block_int.h" from device model .h into .c.

---
 block.h           |   38 ++++++++++++++++++++++++++++++++++++++
 block_int.h       |   35 -----------------------------------
 hw/ide/core.c     |    1 +
 hw/ide/internal.h |    1 -
 hw/scsi-disk.c    |    1 +
 hw/scsi.h         |    1 -
 hw/virtio-blk.c   |    1 +
 hw/virtio.h       |    2 +-
 8 files changed, 42 insertions(+), 38 deletions(-)
block.h +38 -0
index b56dae6..b6eaaba 100644
@@ -326,4 +326,42 @@ typedef enum {
 #define BLKDBG_EVENT(bs, evt) bdrv_debug_event(bs, evt)
 void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event);
 
+
+/* Convenience for block device models */
+
+typedef struct BlockConf {
+    BlockDriverState *bs;
+    uint16_t physical_block_size;
+    uint16_t logical_block_size;
+    uint16_t min_io_size;
+    uint32_t opt_io_size;
+    int32_t bootindex;
+    uint32_t discard_granularity;
+} BlockConf;
+
+static inline unsigned int get_physical_block_exp(BlockConf *conf)
+{
+    unsigned int exp = 0, size;
+
+    for (size = conf->physical_block_size;
+        size > conf->logical_block_size;
+        size >>= 1) {
+        exp++;
+    }
+
+    return exp;
+}
+
+#define DEFINE_BLOCK_PROPERTIES(_state, _conf)                          \
+    DEFINE_PROP_DRIVE("drive", _state, _conf.bs),                       \
+    DEFINE_PROP_UINT16("logical_block_size", _state,                    \
+                       _conf.logical_block_size, 512),                  \
+    DEFINE_PROP_UINT16("physical_block_size", _state,                   \
+                       _conf.physical_block_size, 512),                 \
+    DEFINE_PROP_UINT16("min_io_size", _state, _conf.min_io_size, 0),  \
+    DEFINE_PROP_UINT32("opt_io_size", _state, _conf.opt_io_size, 0),    \
+    DEFINE_PROP_INT32("bootindex", _state, _conf.bootindex, -1),        \
+    DEFINE_PROP_UINT32("discard_granularity", _state, \
+                       _conf.discard_granularity, 0)
+
 #endif
block_int.h +0 -35
index 4bc91cb..7e74d01 100644
@@ -219,39 +219,4 @@ void *qemu_blockalign(BlockDriverState *bs, size_t size);
 int is_windows_drive(const char *filename);
 #endif
 
-typedef struct BlockConf {
-    BlockDriverState *bs;
-    uint16_t physical_block_size;
-    uint16_t logical_block_size;
-    uint16_t min_io_size;
-    uint32_t opt_io_size;
-    int32_t bootindex;
-    uint32_t discard_granularity;
-} BlockConf;
-
-static inline unsigned int get_physical_block_exp(BlockConf *conf)
-{
-    unsigned int exp = 0, size;
-
-    for (size = conf->physical_block_size;
-        size > conf->logical_block_size;
-        size >>= 1) {
-        exp++;
-    }
-
-    return exp;
-}
-
-#define DEFINE_BLOCK_PROPERTIES(_state, _conf)                          \
-    DEFINE_PROP_DRIVE("drive", _state, _conf.bs),                       \
-    DEFINE_PROP_UINT16("logical_block_size", _state,                    \
-                       _conf.logical_block_size, 512),                  \
-    DEFINE_PROP_UINT16("physical_block_size", _state,                   \
-                       _conf.physical_block_size, 512),                 \
-    DEFINE_PROP_UINT16("min_io_size", _state, _conf.min_io_size, 0),  \
-    DEFINE_PROP_UINT32("opt_io_size", _state, _conf.opt_io_size, 0),    \
-    DEFINE_PROP_INT32("bootindex", _state, _conf.bootindex, -1),        \
-    DEFINE_PROP_UINT32("discard_granularity", _state, \
-                       _conf.discard_granularity, 0)
-
 #endif /* BLOCK_INT_H */
hw/ide/core.c +1 -0
index 2bde2b8..7301108 100644
@@ -30,6 +30,7 @@
 #include "sysemu.h"
 #include "dma.h"
 #include "blockdev.h"
+#include "block_int.h"
 
 #include <hw/ide/internal.h>
 
hw/ide/internal.h +0 -1
index df8887a..d467b04 100644
@@ -7,7 +7,6 @@
  * non-internal declarations are in hw/ide.h
  */
 #include <hw/ide.h>
-#include "block_int.h"
 #include "iorange.h"
 #include "dma.h"
 
hw/scsi-disk.c +1 -0
index f36f529..9d6968c 100644
@@ -37,6 +37,7 @@ do { fprintf(stderr, "scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
 #include "scsi-defs.h"
 #include "sysemu.h"
 #include "blockdev.h"
+#include "block_int.h"
 
 #define SCSI_DMA_BUF_SIZE    131072
 #define SCSI_MAX_INQUIRY_LEN 256
hw/scsi.h +0 -1
index f6b6bc1..aa3aeff 100644
@@ -3,7 +3,6 @@
 
 #include "qdev.h"
 #include "block.h"
-#include "block_int.h"
 
 #define MAX_SCSI_DEVS	255
 
hw/virtio-blk.c +1 -0
index 4ed1975..a517150 100644
@@ -15,6 +15,7 @@
 #include "qemu-error.h"
 #include "trace.h"
 #include "blockdev.h"
+#include "block_int.h"
 #include "virtio-blk.h"
 #ifdef __linux__
 # include <scsi/sg.h>
hw/virtio.h +1 -1
index 0fd0bb0..fd9967d 100644
@@ -18,7 +18,7 @@
 #include "net.h"
 #include "qdev.h"
 #include "sysemu.h"
-#include "block_int.h"
+#include "block.h"
 #include "event_notifier.h"
 #ifdef CONFIG_LINUX
 #include "9p.h"
Signature · 1 line
1.7.2.3
Reviewed-by on v1 47/55
#122 lore raw

Re: [Qemu-devel] [PATCH 47/55] block: Move BlockConf & friends from block_int.h to block.h

Quoted 5 lines · Markus Armbruster

It's convenience stuff for block device models, so block.h isn't the ideal home either, but better than block_int.h.

Permits moving some #include "block_int.h" from device model .h into .c.

Fine with me (and I added this originally).

#123 lore raw

PATCH 48/55 hw: Trim superfluous #include "block_int.h"

In reply to Markus Armbruster

Including it in device models is unclean, including it without a reason adds insult to injury.

---
 hw/fdc.c            |    1 -
 hw/ide/cmd646.c     |    1 -
 hw/ide/ich.c        |    1 -
 hw/ide/isa.c        |    1 -
 hw/ide/macio.c      |    1 -
 hw/ide/microdrive.c |    1 -
 hw/ide/mmio.c       |    1 -
 hw/ide/pci.c        |    1 -
 hw/ide/piix.c       |    1 -
 hw/ide/via.c        |    1 -
 hw/lsi53c895a.c     |    1 -
 11 files changed, 0 insertions(+), 11 deletions(-)
hw/fdc.c +0 -1
index debcd89..e8da6a9 100644
@@ -36,7 +36,6 @@
 #include "qdev-addr.h"
 #include "blockdev.h"
 #include "sysemu.h"
-#include "block_int.h"
 
 /********************************************************/
 /* debug Floppy devices */
hw/ide/cmd646.c +0 -1
index 56302b5..79ceca0 100644
@@ -27,7 +27,6 @@
 #include <hw/pci.h>
 #include <hw/isa.h>
 #include "block.h"
-#include "block_int.h"
 #include "sysemu.h"
 #include "dma.h"
 
hw/ide/ich.c +0 -1
index 054e073..6f8c175 100644
@@ -66,7 +66,6 @@
 #include <hw/pci.h>
 #include <hw/isa.h>
 #include "block.h"
-#include "block_int.h"
 #include "dma.h"
 
 #include <hw/ide/pci.h>
hw/ide/isa.c +0 -1
index 4ac7453..28b69d2 100644
@@ -26,7 +26,6 @@
 #include <hw/pc.h>
 #include <hw/isa.h>
 #include "block.h"
-#include "block_int.h"
 #include "dma.h"
 
 #include <hw/ide/internal.h>
hw/ide/macio.c +0 -1
index 7daeb31..e92c330 100644
@@ -26,7 +26,6 @@
 #include <hw/ppc_mac.h>
 #include <hw/mac_dbdma.h>
 #include "block.h"
-#include "block_int.h"
 #include "dma.h"
 
 #include <hw/ide/internal.h>
hw/ide/microdrive.c +0 -1
index 9fbbf0e..61a6034 100644
@@ -26,7 +26,6 @@
 #include <hw/pc.h>
 #include <hw/pcmcia.h>
 #include "block.h"
-#include "block_int.h"
 #include "dma.h"
 
 #include <hw/ide/internal.h>
hw/ide/mmio.c +0 -1
index 10f6f40..d6bb5e3 100644
@@ -24,7 +24,6 @@
  */
 #include <hw/hw.h>
 #include "block.h"
-#include "block_int.h"
 #include "dma.h"
 
 #include <hw/ide/internal.h>
hw/ide/pci.c +0 -1
index 9f3050a..a5371f4 100644
@@ -27,7 +27,6 @@
 #include <hw/pci.h>
 #include <hw/isa.h>
 #include "block.h"
-#include "block_int.h"
 #include "dma.h"
 
 #include <hw/ide/pci.h>
hw/ide/piix.c +0 -1
index 84f72b0..a0bef42 100644
@@ -27,7 +27,6 @@
 #include <hw/pci.h>
 #include <hw/isa.h>
 #include "block.h"
-#include "block_int.h"
 #include "sysemu.h"
 #include "dma.h"
 
hw/ide/via.c +0 -1
index 3474c37..3c131f6 100644
@@ -28,7 +28,6 @@
 #include <hw/pci.h>
 #include <hw/isa.h>
 #include "block.h"
-#include "block_int.h"
 #include "sysemu.h"
 #include "dma.h"
 
hw/lsi53c895a.c +0 -1
index 940b43a..0930519 100644
@@ -15,7 +15,6 @@
 #include "hw.h"
 #include "pci.h"
 #include "scsi.h"
-#include "block_int.h"
 
 //#define DEBUG_LSI
 //#define DEBUG_LSI_REG
Signature · 1 line
1.7.2.3
#125 lore raw

PATCH 49/55 block: Declare qemu_blockalign() in block.h, not block_int.h

In reply to Markus Armbruster

Device models should be able to use it without an unclean include of block_int.h.

---
 block.h     |    2 ++
 block_int.h |    2 --
 hw/sd.c     |    1 -
 3 files changed, 2 insertions(+), 3 deletions(-)
block.h +2 -0
index b6eaaba..8f52abb 100644
@@ -264,6 +264,8 @@ int bdrv_img_create(const char *filename, const char *fmt,
                     const char *base_filename, const char *base_fmt,
                     char *options, uint64_t img_size, int flags);
 
+void *qemu_blockalign(BlockDriverState *bs, size_t size);
+
 #define BDRV_SECTORS_PER_DIRTY_CHUNK 2048
 
 void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable);
block_int.h +0 -2
index 7e74d01..768c842 100644
@@ -213,8 +213,6 @@ void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
                    BlockDriverCompletionFunc *cb, void *opaque);
 void qemu_aio_release(void *p);
 
-void *qemu_blockalign(BlockDriverState *bs, size_t size);
-
 #ifdef _WIN32
 int is_windows_drive(const char *filename);
 #endif
hw/sd.c +0 -1
index 4dc9047..fdd87b0 100644
@@ -31,7 +31,6 @@
 
 #include "hw.h"
 #include "block.h"
-#include "block_int.h"
 #include "sd.h"
 
 //#define DEBUG_SD 1
Signature · 1 line
1.7.2.3
#126 lore raw

Re: [Qemu-devel] [PATCH 49/55] block: Declare qemu_blockalign() in block.h, not block_int.h

Quoted 2 lines · Markus Armbruster

Device models should be able to use it without an unclean include of block_int.h.

Hmm. We already do proper read-modify-write cycles for actual data transfers, so killing the buffer inside SD sounds like the better idea. qemu_blockalign really should be used only for block-layer internal allocations.

#127 lore raw

Re: [Qemu-devel] [PATCH 49/55] block: Declare qemu_blockalign() in block.h, not block_int.h

Quoted 8 lines · Christoph Hellwig
Quoted 2 lines · Markus Armbruster

Device models should be able to use it without an unclean include of block_int.h.

Hmm. We already do proper read-modify-write cycles for actual data transfers, so killing the buffer inside SD sounds like the better idea. qemu_blockalign really should be used only for block-layer internal allocations.

Ok, dropping the patch.

#128 lore raw

Re: [Qemu-devel] [PATCH 49/55] block: Declare qemu_blockalign() in block.h, not block_int.h

In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig
Quoted 2 lines · Markus Armbruster

Device models should be able to use it without an unclean include of block_int.h.

Hmm. We already do proper read-modify-write cycles for actual data transfers, so killing the buffer inside SD sounds like the better idea. qemu_blockalign really should be used only for block-layer internal allocations.

What about the uses in scsi-disk.c and ide/core.c?

#129 lore raw

Re: [Qemu-devel] [PATCH 49/55] block: Declare qemu_blockalign() in block.h, not block_int.h

Quoted 6 lines · Markus Armbruster
Quoted 4 lines

Hmm. We already do proper read-modify-write cycles for actual data transfers, so killing the buffer inside SD sounds like the better idea. qemu_blockalign really should be used only for block-layer internal allocations.

What about the uses in scsi-disk.c and ide/core.c?

They are allocating internal bounce buffers due to suboptimal I/O models. But given that this is the reality in multiple qemu drivers I think I'll give up and ack the move.

#130 lore raw

PATCH 50/55 block: New bdrv_set_buffer_alignment()

In reply to Markus Armbruster

Device models should be able to set it without an unclean include of block_int.h.

---
 block.c         |    6 ++++--
 block.h         |    1 +
 hw/ide/core.c   |    2 +-
 hw/scsi-disk.c  |    3 +--
 hw/virtio-blk.c |    3 +--
 5 files changed, 8 insertions(+), 7 deletions(-)
block.c +4 -2
index 925861f..e9b911b 100644
@@ -428,7 +428,6 @@ static int bdrv_open_common(BlockDriverState *bs, const char *filename,
     bs->encrypted = 0;
     bs->valid_key = 0;
     bs->open_flags = flags;
-    /* buffer_alignment defaulted to 512, drivers can change this value */
     bs->buffer_alignment = 512;
 
     pstrcpy(bs->filename, sizeof(bs->filename), filename);
@@ -2819,7 +2818,10 @@ BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
     return NULL;
 }
 
-
+void bdrv_set_buffer_alignment(BlockDriverState *bs, int align)
+{
+    bs->buffer_alignment = align;
+}
 
 void *qemu_blockalign(BlockDriverState *bs, size_t size)
 {
block.h +1 -0
index 8f52abb..c596b4c 100644
@@ -264,6 +264,7 @@ int bdrv_img_create(const char *filename, const char *fmt,
                     const char *base_filename, const char *base_fmt,
                     char *options, uint64_t img_size, int flags);
 
+void bdrv_set_buffer_alignment(BlockDriverState *bs, int align);
 void *qemu_blockalign(BlockDriverState *bs, size_t size);
 
 #define BDRV_SECTORS_PER_DIRTY_CHUNK 2048
hw/ide/core.c +1 -1
index 7301108..bfa1162 100644
@@ -1821,7 +1821,7 @@ int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind,
     s->smart_selftest_count = 0;
     if (kind == IDE_CD) {
         bdrv_set_dev_ops(bs, &ide_cd_block_ops, s);
-        bs->buffer_alignment = 2048;
+        bdrv_set_buffer_alignment(bs, 2048);
     } else {
         if (!bdrv_is_inserted(s->bs)) {
             error_report("Device needs media, but drive is empty");
hw/scsi-disk.c +1 -2
index 9d6968c..1bfdfdd 100644
@@ -37,7 +37,6 @@ do { fprintf(stderr, "scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
 #include "scsi-defs.h"
 #include "sysemu.h"
 #include "blockdev.h"
-#include "block_int.h"
 
 #define SCSI_DMA_BUF_SIZE    131072
 #define SCSI_MAX_INQUIRY_LEN 256
@@ -1336,7 +1335,7 @@ static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)
         s->qdev.blocksize = s->qdev.conf.logical_block_size;
     }
     s->cluster_size = s->qdev.blocksize / 512;
-    s->bs->buffer_alignment = s->qdev.blocksize;
+    bdrv_set_buffer_alignment(s->bs, s->qdev.blocksize);
 
     s->qdev.type = TYPE_DISK;
     qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s);
hw/virtio-blk.c +1 -2
index a517150..811d709 100644
@@ -15,7 +15,6 @@
 #include "qemu-error.h"
 #include "trace.h"
 #include "blockdev.h"
-#include "block_int.h"
 #include "virtio-blk.h"
 #ifdef __linux__
 # include <scsi/sg.h>
@@ -585,7 +584,7 @@ VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf,
     register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
                     virtio_blk_save, virtio_blk_load, s);
     bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
-    s->bs->buffer_alignment = conf->logical_block_size;
+    bdrv_set_buffer_alignment(s->bs, conf->logical_block_size);
 
     add_boot_device_path(conf->bootindex, dev, "/disk@0,0");
 
Signature · 1 line
1.7.2.3
#132 lore raw

PATCH 51/55 block: Reset buffer alignment on detach

In reply to Markus Armbruster

BlockDriverState member buffer_alignment is initially 512. The device model may set them, with bdrv_set_buffer_alignment(). If the device model gets detached (hot unplug), the device's alignment is left behind. Only okay because device hot unplug automatically destroys the BlockDriverState. But that's a questionable feature, best not to rely on it.

---
 block.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
block.c +1 -0
index e9b911b..b94864c 100644
@@ -736,6 +736,7 @@ void bdrv_detach_dev(BlockDriverState *bs, void *dev)
     bs->dev = NULL;
     bs->dev_ops = NULL;
     bs->dev_opaque = NULL;
+    bs->buffer_alignment = 512;
 }
 
 /* TODO change to return DeviceState * when all users are qdevified */
Signature · 1 line
1.7.2.3
Reviewed-by on v1 51/55
#133 lore raw

Re: [Qemu-devel] [PATCH 51/55] block: Reset buffer alignment on detach

Quoted 6 lines · Markus Armbruster

BlockDriverState member buffer_alignment is initially 512. The device model may set them, with bdrv_set_buffer_alignment(). If the device model gets detached (hot unplug), the device's alignment is left behind. Only okay because device hot unplug automatically destroys the BlockDriverState. But that's a questionable feature, best not to rely on it.

Looks good,

#134 lore raw

PATCH 52/55 block: Move BlockDriverAIOCB & friends from block_int.h to block.h

In reply to Markus Armbruster

Device models should be able to use them without an unclean include of block_int.h.

---
 block.h       |   15 +++++++++++++++
 block_int.h   |   18 ------------------
 hw/ide/core.c |    1 -
 3 files changed, 15 insertions(+), 19 deletions(-)
block.h +15 -0
index c596b4c..be5ed0a 100644
@@ -140,8 +140,23 @@ int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res);
 /* async block I/O */
 typedef struct BlockDriverAIOCB BlockDriverAIOCB;
 typedef void BlockDriverCompletionFunc(void *opaque, int ret);
+typedef struct AIOPool {
+    void (*cancel)(BlockDriverAIOCB *acb);
+    int aiocb_size;
+    BlockDriverAIOCB *free_aiocb;
+} AIOPool;
+struct BlockDriverAIOCB {
+    AIOPool *pool;
+    BlockDriverState *bs;
+    BlockDriverCompletionFunc *cb;
+    void *opaque;
+    BlockDriverAIOCB *next;
+};
 typedef void BlockDriverDirtyHandler(BlockDriverState *bs, int64_t sector,
                                      int sector_num);
+void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
+                   BlockDriverCompletionFunc *cb, void *opaque);
+void qemu_aio_release(void *p);
 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
                                  QEMUIOVector *iov, int nb_sectors,
                                  BlockDriverCompletionFunc *cb, void *opaque);
block_int.h +0 -18
index 768c842..9f1121d 100644
@@ -40,12 +40,6 @@
 #define BLOCK_OPT_TABLE_SIZE    "table_size"
 #define BLOCK_OPT_PREALLOC      "preallocation"
 
-typedef struct AIOPool {
-    void (*cancel)(BlockDriverAIOCB *acb);
-    int aiocb_size;
-    BlockDriverAIOCB *free_aiocb;
-} AIOPool;
-
 struct BlockDriver {
     const char *format_name;
     int instance_size;
@@ -199,20 +193,8 @@ struct BlockDriverState {
     void *private;
 };
 
-struct BlockDriverAIOCB {
-    AIOPool *pool;
-    BlockDriverState *bs;
-    BlockDriverCompletionFunc *cb;
-    void *opaque;
-    BlockDriverAIOCB *next;
-};
-
 void get_tmp_filename(char *filename, int size);
 
-void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
-                   BlockDriverCompletionFunc *cb, void *opaque);
-void qemu_aio_release(void *p);
-
 #ifdef _WIN32
 int is_windows_drive(const char *filename);
 #endif
hw/ide/core.c +0 -1
index bfa1162..cac3518 100644
@@ -30,7 +30,6 @@
 #include "sysemu.h"
 #include "dma.h"
 #include "blockdev.h"
-#include "block_int.h"
 
 #include <hw/ide/internal.h>
 
Signature · 1 line
1.7.2.3
#135 lore raw

Re: [Qemu-devel] [PATCH 52/55] block: Move BlockDriverAIOCB & friends from block_int.h to block.h

Quoted 2 lines · Markus Armbruster

Device models should be able to use them without an unclean include of block_int.h.

I don't think so. In generall they aren't for use of device models at all. It's just that the IDE trim support with it's ranges is rather "special". If you absolutely want IDE to stop including block_int.h I guess we'll have to lift that code into the block layer somehow.

#136 lore raw

Re: [Qemu-devel] [PATCH 52/55] block: Move BlockDriverAIOCB & friends from block_int.h to block.h

Quoted 8 lines · Christoph Hellwig
Quoted 2 lines · Markus Armbruster

Device models should be able to use them without an unclean include of block_int.h.

I don't think so. In generall they aren't for use of device models at all. It's just that the IDE trim support with it's ranges is rather "special". If you absolutely want IDE to stop including block_int.h I guess we'll have to lift that code into the block layer somehow.

Not "absolutely" enough to do the lifting now. Dropping the patch.

#137 lore raw

PATCH 53/55 nbd: Clean up use of block_int.h

In reply to Markus Armbruster
---
 block/nbd.c |    1 +
 nbd.c       |    1 +
 nbd.h       |    2 --
 3 files changed, 2 insertions(+), 2 deletions(-)
block/nbd.c +1 -0
index 7a52f62..2e365ac 100644
@@ -28,6 +28,7 @@
 
 #include "qemu-common.h"
 #include "nbd.h"
+#include "block_int.h"
 #include "module.h"
 #include "qemu_socket.h"
 
nbd.c +1 -0
index 0dcd86b..c40100d 100644
@@ -17,6 +17,7 @@
  */
 
 #include "nbd.h"
+#include "block.h"
 
 #include <errno.h>
 #include <string.h>
nbd.h +0 -2
index b38d0d0..5f87a7d 100644
@@ -23,8 +23,6 @@
 
 #include <qemu-common.h>
 
-#include "block_int.h"
-
 struct nbd_request {
     uint32_t magic;
     uint32_t type;
Signature · 1 line
1.7.2.3
#139 lore raw

PATCH 54/55 block: New change_media_cb() parameter load

In reply to Markus Armbruster

To let device models distinguish between eject and load.

---
 block.c        |   12 ++++++------
 block.h        |    3 ++-
 hw/fdc.c       |    2 +-
 hw/ide/core.c  |    2 +-
 hw/scsi-disk.c |    2 +-
 hw/sd.c        |    2 +-
 6 files changed, 12 insertions(+), 11 deletions(-)
block.c +6 -6
index b94864c..53a433a 100644
@@ -43,7 +43,7 @@
 #include <windows.h>
 #endif
 
-static void bdrv_dev_change_media_cb(BlockDriverState *bs);
+static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque);
@@ -636,7 +636,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
     }
 
     if (!bdrv_key_required(bs)) {
-        bdrv_dev_change_media_cb(bs);
+        bdrv_dev_change_media_cb(bs, true);
     }
 
     return 0;
@@ -672,7 +672,7 @@ void bdrv_close(BlockDriverState *bs)
             bdrv_close(bs->file);
         }
 
-        bdrv_dev_change_media_cb(bs);
+        bdrv_dev_change_media_cb(bs, false);
     }
 }
 
@@ -755,10 +755,10 @@ void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
     }
 }
 
-static void bdrv_dev_change_media_cb(BlockDriverState *bs)
+static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load)
 {
     if (bs->dev_ops && bs->dev_ops->change_media_cb) {
-        bs->dev_ops->change_media_cb(bs->dev_opaque);
+        bs->dev_ops->change_media_cb(bs->dev_opaque, load);
     }
 }
 
@@ -1535,7 +1535,7 @@ int bdrv_set_key(BlockDriverState *bs, const char *key)
     } else if (!bs->valid_key) {
         bs->valid_key = 1;
         /* call the change callback now, we skipped it on open */
-        bdrv_dev_change_media_cb(bs);
+        bdrv_dev_change_media_cb(bs, 1);
     }
     return ret;
 }
block.h +2 -1
index be5ed0a..c99de24 100644
@@ -31,11 +31,12 @@ typedef struct QEMUSnapshotInfo {
 typedef struct BlockDevOps {
     /*
      * Runs when virtual media changed (monitor commands eject, change)
+     * Argument load is true on load and false on eject.
      * Beware: doesn't run when a host device's physical media
      * changes.  Sure would be useful if it did.
      * Device models with removable media must implement this callback.
      */
-    void (*change_media_cb)(void *opaque);
+    void (*change_media_cb)(void *opaque, bool load);
     /*
      * Is medium ejected from the device?
      * Device models calling bdrv_eject() should implement this.
hw/fdc.c +1 -1
index e8da6a9..1f084ce 100644
@@ -1777,7 +1777,7 @@ static void fdctrl_result_timer(void *opaque)
     fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
 }
 
-static void fdctrl_change_cb(void *opaque)
+static void fdctrl_change_cb(void *opaque, bool load)
 {
     FDrive *drive = opaque;
 
hw/ide/core.c +1 -1
index cac3518..05ed7e9 100644
@@ -758,7 +758,7 @@ static void ide_cfata_metadata_write(IDEState *s)
 }
 
 /* called when the inserted state of the media has changed */
-static void ide_cd_change_cb(void *opaque)
+static void ide_cd_change_cb(void *opaque, bool load)
 {
     IDEState *s = opaque;
     uint64_t nb_sectors;
hw/scsi-disk.c +1 -1
index 1bfdfdd..4f28e9c 100644
@@ -1276,7 +1276,7 @@ static void scsi_destroy(SCSIDevice *dev)
     blockdev_mark_auto_del(s->qdev.conf.bs);
 }
 
-static void scsi_cd_change_media_cb(void *opaque)
+static void scsi_cd_change_media_cb(void *opaque, bool load)
 {
 }
 
hw/sd.c +1 -1
index fdd87b0..31b83de 100644
@@ -421,7 +421,7 @@ static void sd_reset(SDState *sd, BlockDriverState *bdrv)
     sd->pwd_len = 0;
 }
 
-static void sd_cardchange(void *opaque)
+static void sd_cardchange(void *opaque, bool load)
 {
     SDState *sd = opaque;
 
Signature · 1 line
1.7.2.3
#141 lore raw

PATCH 55/55 ide/atapi scsi-disk: Make monitor eject -f, then change work

In reply to Markus Armbruster

change fails while the tray is locked by the guest. eject -f forces it open and removes any media. Unfortunately, the tray closes again instantly. Since the lock remains as it is, there is no way to insert another medium unless the guest voluntarily unlocks.

Fix by leaving the tray open after monitor eject.

---
 blockdev.c     |    3 ++-
 hw/ide/core.c  |    1 +
 hw/scsi-disk.c |    1 +
 3 files changed, 4 insertions(+), 1 deletions(-)
blockdev.c +2 -1
index 9284afd..197f57f 100644
@@ -644,7 +644,8 @@ static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
         qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
         return -1;
     }
-    if (!force && bdrv_dev_is_medium_locked(bs)) {
+    if (!force && !bdrv_dev_is_medium_ejected(bs)
+        && bdrv_dev_is_medium_locked(bs)) {
         qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
         return -1;
     }
hw/ide/core.c +1 -0
index 05ed7e9..77eef58 100644
@@ -763,6 +763,7 @@ static void ide_cd_change_cb(void *opaque, bool load)
     IDEState *s = opaque;
     uint64_t nb_sectors;
 
+    s->tray_open = !load;
     bdrv_get_geometry(s->bs, &nb_sectors);
     s->nb_sectors = nb_sectors;
 
hw/scsi-disk.c +1 -0
index 4f28e9c..3d245fe 100644
@@ -1278,6 +1278,7 @@ static void scsi_destroy(SCSIDevice *dev)
 
 static void scsi_cd_change_media_cb(void *opaque, bool load)
 {
+    ((SCSIDiskState *)opaque)->tray_open = !load;
 }
 
 static bool scsi_cd_is_medium_ejected(void *opaque)
Signature · 1 line
1.7.2.3
Reviewed-by on v1 55/55
#142 lore raw

Re: [Qemu-devel] [PATCH 55/55] ide/atapi scsi-disk: Make monitor eject -f, then change work

Quoted 6 lines · Markus Armbruster

change fails while the tray is locked by the guest. eject -f forces it open and removes any media. Unfortunately, the tray closes again instantly. Since the lock remains as it is, there is no way to insert another medium unless the guest voluntarily unlocks.

Fix by leaving the tray open after monitor eject.

Looks fine,

#143 lore raw
In reply to Markus Armbruster
Quoted 5 lines · Markus Armbruster

This patch series looks bigger than it is. All the patches are small and hopefully easy to review.

Objectives:

Quoted 8 lines

* Push BlockDriverState members locked, tray_open, media_changed into device models, where they belong.

* BlockDriverState member removable is a confusing mess, replace it.

* Improve eject -f.

Also clean up minor messes as they get in the way.

Sorry, haven't seen these patches yet, but a question: will adding things like 'this removable device is a CDROM/CD-RW/DVD/DVD-dual layer/DVD-dual-side/DVD-RAM, etc., need extra cleanups or will this just fit in?

Amit

#144 lore raw
Quoted 11 lines · Amit Shah
Quoted 10 lines · Markus Armbruster

This patch series looks bigger than it is. All the patches are small and hopefully easy to review.

Objectives:

* Push BlockDriverState members locked, tray_open, media_changed into device models, where they belong.

* BlockDriverState member removable is a confusing mess, replace it.

Quoted 8 lines
Quoted 3 lines

* Improve eject -f.

Also clean up minor messes as they get in the way.

Sorry, haven't seen these patches yet, but a question: will adding things like 'this removable device is a CDROM/CD-RW/DVD/DVD-dual layer/DVD-dual-side/DVD-RAM, etc., need extra cleanups or will this just fit in?

I don't know, sorry.

#145 lore raw
Quoted 15 lines · Markus Armbruster
Quoted 13 lines · Amit Shah
Quoted 12 lines · Markus Armbruster

This patch series looks bigger than it is. All the patches are small and hopefully easy to review.

Objectives:

* Push BlockDriverState members locked, tray_open, media_changed into device models, where they belong.

* BlockDriverState member removable is a confusing mess, replace it.

* Improve eject -f.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Also clean up minor messes as they get in the way.

Sorry, haven't seen these patches yet, but a question: will adding things like 'this removable device is a CDROM/CD-RW/DVD/DVD-dual layer/DVD-dual-side/DVD-RAM, etc., need extra cleanups or will this just fit in?

I don't know, sorry.

No problem!

Amit

Applied
#146 lore raw
In reply to Markus Armbruster

Am 20.07.2011 18:23, schrieb Markus Armbruster:

Quoted 8 lines

This patch series looks bigger than it is. All the patches are small and hopefully easy to review.

Objectives:

* Push BlockDriverState members locked, tray_open, media_changed into device models, where they belong.

Quoted 8 lines

* BlockDriverState member removable is a confusing mess, replace it.

* Improve eject -f.

Also clean up minor messes as they get in the way.

Part I: Preliminaries
PATCH 01-09: Assorted block layer fixes and cleanup

Thanks, applied patches 1-9 to the block branch.

Quoted 15 lines

Part II: Move tray state to device models
PATCH 10-18: IDE tray open/closed
PATCH 19-22: SCSI tray open/closed
PATCH 23-25: block layer kill tray_open
PATCH 26-27: IDE tray lock
PATCH 28-29: SCSI tray lock
PATCH 30-32: block layer kill locked
PATCH 33-35: IDE & SCSI synchronize physical tray on init PATCH 36-38: IDE & SCSI migrate tray state
PATCH 39-41: block layer & fdc media_changed

Part III: Replace removable
PATCH 42-44 clean up inappopriate uses of removable PATCH 45-46 replace remaining users

Quoted 5 lines

Part IV: Reduce unclean use of block_int.h
PATCH 47-53

PART V: Improve eject -f
PATCH 54-55

I'll wait for a v2 on top of the block branch for the rest of the series.

Kevin

#147 lore raw
In reply to Markus Armbruster
Quoted 2 lines · Markus Armbruster

This patch series looks bigger than it is. All the patches are small and hopefully easy to review.

Nice series!

ACK the entire series (barring the non-qdev users connecting to bdrv and scsi patches, which I didn't quite follow).

Amit