From patchwork Mon Sep 12 14:19:21 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 114367 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 78B6AB71CC for ; Tue, 13 Sep 2011 00:51:44 +1000 (EST) Received: from localhost ([::1]:38278 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R37KI-00025u-Mu for incoming@patchwork.ozlabs.org; Mon, 12 Sep 2011 10:17:34 -0400 Received: from eggs.gnu.org ([140.186.70.92]:38525) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R37Jv-0001fJ-OJ for qemu-devel@nongnu.org; Mon, 12 Sep 2011 10:17:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1R37Jt-00077r-Hb for qemu-devel@nongnu.org; Mon, 12 Sep 2011 10:17:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:63808) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R37Jt-00077d-8v for qemu-devel@nongnu.org; Mon, 12 Sep 2011 10:17:09 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p8CEH7NF030475 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 12 Sep 2011 10:17:07 -0400 Received: from dhcp-5-188.str.redhat.com (dhcp-5-175.str.redhat.com [10.32.5.175]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p8CEGa2r030698; Mon, 12 Sep 2011 10:17:06 -0400 From: Kevin Wolf To: anthony@codemonkey.ws Date: Mon, 12 Sep 2011 16:19:21 +0200 Message-Id: <1315837174-15327-23-git-send-email-kwolf@redhat.com> In-Reply-To: <1315837174-15327-1-git-send-email-kwolf@redhat.com> References: <1315837174-15327-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [Qemu-devel] [PATCH 22/35] block: Clean up remaining users of "removable" X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: 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. Signed-off-by: Markus Armbruster Signed-off-by: Kevin Wolf --- block.c | 18 +++++++++++------- block.h | 3 ++- blockdev.c | 2 +- hw/scsi-disk.c | 5 +++++ 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/block.c b/block.c index 7225b15..fee45bf 100644 --- a/block.c +++ b/block.c @@ -802,6 +802,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) @@ -811,6 +814,11 @@ static void bdrv_dev_change_media_cb(BlockDriverState *bs) } } +bool bdrv_dev_has_removable_media(BlockDriverState *bs) +{ + return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); +} + static void bdrv_dev_resize_cb(BlockDriverState *bs) { if (bs->dev_ops && bs->dev_ops->resize_cb) { @@ -1329,7 +1337,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); } @@ -1614,11 +1622,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; @@ -1897,7 +1900,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)); if (bs->drv) { diff --git a/block.h b/block.h index 4691090..860e30d 100644 --- a/block.h +++ b/block.h @@ -34,6 +34,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); /* @@ -99,6 +100,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_has_removable_media(BlockDriverState *bs); bool bdrv_dev_is_medium_locked(BlockDriverState *bs); int bdrv_read(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sectors); @@ -206,7 +208,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); diff --git a/blockdev.c b/blockdev.c index 3f00b2e..ddf1f8f 100644 --- a/blockdev.c +++ b/blockdev.c @@ -636,7 +636,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; } diff --git a/hw/scsi-disk.c b/hw/scsi-disk.c index 1a49217..d6e838c 100644 --- a/hw/scsi-disk.c +++ b/hw/scsi-disk.c @@ -1172,12 +1172,17 @@ 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_locked(void *opaque) { return ((SCSIDiskState *)opaque)->tray_locked; } static const BlockDevOps scsi_cd_block_ops = { + .change_media_cb = scsi_cd_change_media_cb, .is_medium_locked = scsi_cd_is_medium_locked, };