Add support to enable dynamic removable block devices
PATCH 1/1 Add support to enable dynamic removable block devices
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
Signature · 8 lines
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
Index: block.c
===================================================================
RCS file: /cvsroot/qemu/qemu/block.c,v
retrieving revision 1.25
block.c +22 -3
@@ -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
block_int.h +3 -0
@@ -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; };