diff mbox

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

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

Commit Message

Markus Armbruster July 20, 2011, 4:24 p.m. UTC
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.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 hw/scsi-disk.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)

Comments

Christoph Hellwig July 26, 2011, 12:17 p.m. UTC | #1
On Wed, Jul 20, 2011 at 06:24:09PM +0200, Markus Armbruster wrote:
> 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,

Reviewed-by: Christoph Hellwig <hch@lst.de>
diff mbox

Patch

diff --git a/hw/scsi-disk.c b/hw/scsi-disk.c
index b9de4df..553d8c6 100644
--- a/hw/scsi-disk.c
+++ b/hw/scsi-disk.c
@@ -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;
 }