From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1FVAvx-0001iM-8o for qemu-devel@nongnu.org; Sun, 16 Apr 2006 13:21:13 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1FVAvr-0001hD-LY for qemu-devel@nongnu.org; Sun, 16 Apr 2006 13:21:13 -0400 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FVAvr-0001hA-IY for qemu-devel@nongnu.org; Sun, 16 Apr 2006 13:21:07 -0400 Received: from [63.240.77.82] (helo=sccrmhc12.comcast.net) by monty-python.gnu.org with esmtp (Exim 4.52) id 1FVAwK-00076l-Pu for qemu-devel@nongnu.org; Sun, 16 Apr 2006 13:21:36 -0400 Message-ID: <44427D01.7060804@win4lin.com> Date: Sun, 16 Apr 2006 13:21:05 -0400 From: "Leonardo E. Reiter" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------030808000009060903020305" Subject: [Qemu-devel] [PATCH] Add support to enable dynamic removable block devices Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org This is a multi-part message in MIME format. --------------030808000009060903020305 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hello, attached is a patch that modifies the block driver infrastructure to support optional "dynamic" removable block devices. For example, today, you can attach a CDROM block device, but it has to be manually attached and ejected from the monitor, even if this block device points to a real physical driver (i.e. /dev/cdrom on Linux), which itself supports really removable media. The patch does not change any current functionality. But, it can be used to build a block driver which has dynamic state for the media, even though the block device itself is attached statically. For example, this will enable [in the future - new block driver needed] attaching a real CDROM device, and having the ability to actually eject the media and insert new media without having to use the monitor. Regards, Leo Reiter -- Leonardo E. Reiter Vice President of Product Development, CTO Win4Lin, Inc. Virtual Computing from Desktop to Data Center Main: +1 512 339 7979 Fax: +1 512 532 6501 http://www.win4lin.com --------------030808000009060903020305 Content-Type: text/x-patch; name="qemu-blk-rmv.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="qemu-blk-rmv.patch" Index: block.c =================================================================== RCS file: /cvsroot/qemu/qemu/block.c,v retrieving revision 1.25 diff -a -u -r1.25 block.c --- block.c 18 Dec 2005 18:28:15 -0000 1.25 +++ block.c 16 Apr 2006 17:14:59 -0000 @@ -446,13 +446,21 @@ return 0; } +/* this function is needed to avoid checking for/calling dynamic methods for + * removable devices if the device is not designated as removable */ +static inline int bdrv_can_write(BlockDriverState *bs) +{ + if (!bs->removable) + return (bs->inserted) && (!bs->read_only); + else + return (bdrv_is_inserted(bs)) && (!bdrv_is_read_only(bs)); +} + /* return -1 if error */ int bdrv_write(BlockDriverState *bs, int64_t sector_num, const uint8_t *buf, int nb_sectors) { - if (!bs->inserted) - return -1; - if (bs->read_only) + if (!bdrv_can_write(bs)) return -1; if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { memcpy(bs->boot_sector_data, buf, 512); @@ -520,11 +528,15 @@ int bdrv_is_read_only(BlockDriverState *bs) { + if (bs->drv && bs->drv->bdrv_is_read_only) + bs->read_only = bs->drv->bdrv_is_read_only(bs); return bs->read_only; } int bdrv_is_inserted(BlockDriverState *bs) { + if (bs->drv && bs->drv->bdrv_is_inserted) + bs->inserted = bs->drv->bdrv_is_inserted(bs); return bs->inserted; } @@ -535,6 +547,13 @@ void bdrv_set_locked(BlockDriverState *bs, int locked) { + /* XXX: bs->drv->bdrv_set_locked() has no need to set bs->locked; if this + * function gets called from the disk controller, it has to always set the + * locked state to whatever the disk controller says, regardless of whether + * or not this is implemented by the block driver itself or whether or not + * the block driver succeeds in setting the locked state. */ + if (bs->drv && bs->drv->bdrv_set_locked) + bs->drv->bdrv_set_locked(bs, locked); bs->locked = locked; } Index: block_int.h =================================================================== RCS file: /cvsroot/qemu/qemu/block_int.h,v retrieving revision 1.4 diff -a -u -r1.4 block_int.h --- block_int.h 18 Dec 2005 18:28:15 -0000 1.4 +++ block_int.h 16 Apr 2006 17:14:59 -0000 @@ -40,6 +40,9 @@ int nb_sectors, int *pnum); int (*bdrv_set_key)(BlockDriverState *bs, const char *key); int (*bdrv_make_empty)(BlockDriverState *bs); + int (*bdrv_is_read_only)(BlockDriverState *bs); + int (*bdrv_is_inserted)(BlockDriverState *bs); + void (*bdrv_set_locked)(BlockDriverState *bs, int locked); struct BlockDriver *next; }; --------------030808000009060903020305--