qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, Amit Shah <amit.shah@redhat.com>,
	Anthony Liguori <aliguori@us.ibm.com>,
	Ryan Harper <ryanh@us.ibm.com>,
	Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Subject: [Qemu-devel] [PATCH 3/3] raw-posix: Re-open host CD-ROM after media change
Date: Wed, 23 Mar 2011 17:40:55 +0000	[thread overview]
Message-ID: <1300902055-25850-4-git-send-email-stefanha@linux.vnet.ibm.com> (raw)
In-Reply-To: <1300902055-25850-1-git-send-email-stefanha@linux.vnet.ibm.com>

Piggy-back on the guest CD-ROM polling to poll on the host.  Open and
close the host CD-ROM file descriptor to ensure we read the new size and
not a stale size.

Two things are going on here:

1. If hald/udisks is not already polling CD-ROMs on the host then
   re-opening the CD-ROM causes the host to read the new medium's size.

2. There is a bug in Linux which means the CD-ROM file descriptor must
   be re-opened in order for lseek(2) to see the new size.  The
   inode size gets out of sync with the underlying device (which you can
   confirm by checking that /sys/block/sr0/size and lseek(2) do not
   match after media change).  I have raised this with the
   maintainers but we need a workaround for the foreseeable future.

Note that these changes are all in a #ifdef __linux__ section.

Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
---
 block/raw-posix.c |   25 +++++++++++++++++++++----
 1 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/block/raw-posix.c b/block/raw-posix.c
index a95c8d4..ac95467 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -1240,10 +1240,27 @@ static int cdrom_is_inserted(BlockDriverState *bs)
     BDRVRawState *s = bs->opaque;
     int ret;
 
-    ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
-    if (ret == CDS_DISC_OK)
-        return 1;
-    return 0;
+    /*
+     * Opening and closing on each drive status check ensures the medium size
+     * is refreshed.  If the file descriptor is kept open the size can become
+     * stale.  This is essentially replicating CD-ROM polling but is driven by
+     * the guest.  As the guest polls, we poll the host.
+     */
+
+    if (s->fd == -1) {
+        s->fd = qemu_open(bs->filename, s->open_flags, 0644);
+        if (s->fd < 0) {
+            return 0;
+        }
+    }
+
+    ret = (ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) == CDS_DISC_OK);
+
+    if (!ret) {
+        close(s->fd);
+        s->fd = -1;
+    }
+    return ret;
 }
 
 static int cdrom_eject(BlockDriverState *bs, int eject_flag)
-- 
1.7.4.1

  parent reply	other threads:[~2011-03-23 17:41 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-23 17:40 [Qemu-devel] [PATCH 0/3] block: Correct size across CD-ROM media change Stefan Hajnoczi
2011-03-23 17:40 ` [Qemu-devel] [PATCH 1/3] trace: Trace bdrv_set_locked() Stefan Hajnoczi
2011-03-23 17:40 ` [Qemu-devel] [PATCH 2/3] block: Do not cache device size for removable media Stefan Hajnoczi
2011-03-23 20:18   ` [Qemu-devel] " Juan Quintela
2011-03-23 20:46     ` Stefan Hajnoczi
2011-03-23 17:40 ` Stefan Hajnoczi [this message]
2011-03-23 20:27   ` [Qemu-devel] Re: [PATCH 3/3] raw-posix: Re-open host CD-ROM after media change Juan Quintela
2011-03-23 20:50     ` Stefan Hajnoczi
2011-03-24 12:42       ` Kevin Wolf
2011-03-24 19:00         ` Stefan Hajnoczi
2011-03-24 21:23           ` Juan Quintela

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1300902055-25850-4-git-send-email-stefanha@linux.vnet.ibm.com \
    --to=stefanha@linux.vnet.ibm.com \
    --cc=aliguori@us.ibm.com \
    --cc=amit.shah@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=ryanh@us.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).