From patchwork Tue Mar 29 19:04:42 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 88835 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 4BC3AB6F1E for ; Wed, 30 Mar 2011 06:25:16 +1100 (EST) Received: from localhost ([127.0.0.1]:48082 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q4eXR-0000Vt-DU for incoming@patchwork.ozlabs.org; Tue, 29 Mar 2011 15:25:13 -0400 Received: from [140.186.70.92] (port=56148 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q4eQn-00076J-E9 for qemu-devel@nongnu.org; Tue, 29 Mar 2011 15:18:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Q4eFD-0001A4-HP for qemu-devel@nongnu.org; Tue, 29 Mar 2011 15:06:24 -0400 Received: from mtagate6.uk.ibm.com ([194.196.100.166]:34636) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Q4eFD-00019i-A7 for qemu-devel@nongnu.org; Tue, 29 Mar 2011 15:06:23 -0400 Received: from d06nrmr1507.portsmouth.uk.ibm.com (d06nrmr1507.portsmouth.uk.ibm.com [9.149.38.233]) by mtagate6.uk.ibm.com (8.13.1/8.13.1) with ESMTP id p2TJ6MMO014203 for ; Tue, 29 Mar 2011 19:06:22 GMT Received: from d06av11.portsmouth.uk.ibm.com (d06av11.portsmouth.uk.ibm.com [9.149.37.252]) by d06nrmr1507.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p2TJ6u4G1675418 for ; Tue, 29 Mar 2011 20:06:56 +0100 Received: from d06av11.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av11.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p2TJ6ML3008155 for ; Tue, 29 Mar 2011 13:06:22 -0600 Received: from stefanha-thinkpad.ibm.com (sig-9-146-150-201.uk.ibm.com [9.146.150.201]) by d06av11.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p2TJ6Gpr008044; Tue, 29 Mar 2011 13:06:21 -0600 From: Stefan Hajnoczi To: Date: Tue, 29 Mar 2011 20:04:42 +0100 Message-Id: <1301425482-8722-4-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1301425482-8722-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1301425482-8722-1-git-send-email-stefanha@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 194.196.100.166 Cc: Kevin Wolf , Anthony Liguori , Stefan Hajnoczi , Juan Quintela , Ryan Harper , Amit Shah Subject: [Qemu-devel] [PATCH v2 3/3] raw-posix: Re-open host CD-ROM after media change X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- block/raw-posix.c | 26 ++++++++++++++++++++++---- 1 files changed, 22 insertions(+), 4 deletions(-) diff --git a/block/raw-posix.c b/block/raw-posix.c index 6b72470..8b5205c 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -1238,10 +1238,28 @@ 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; + /* + * Close the file descriptor if no medium is present and open it to poll + * again. This 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)