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; };