block: Drop medium lock tracking, ask device models instead

Requires new BlockDevOps member is_medium_locked().  Implement for IDE
and SCSI CD-ROMs.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Markus Armbruster 2011-09-06 18:58:46 +02:00 committed by Kevin Wolf
parent fdec4404dd
commit f107639a6f
6 changed files with 32 additions and 10 deletions

16
block.c
View File

@ -818,6 +818,14 @@ static void bdrv_dev_resize_cb(BlockDriverState *bs)
}
}
bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
{
if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
return bs->dev_ops->is_medium_locked(bs->dev_opaque);
}
return false;
}
/*
* Run consistency checks on an image
*
@ -1890,7 +1898,7 @@ 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->locked);
bdrv_dev_is_medium_locked(bs));
if (bs->drv) {
QObject *obj;
@ -3060,11 +3068,6 @@ void bdrv_eject(BlockDriverState *bs, int eject_flag)
}
}
int bdrv_is_locked(BlockDriverState *bs)
{
return bs->locked;
}
/**
* Lock or unlock the media (if it is locked, the user won't be able
* to eject it manually).
@ -3075,7 +3078,6 @@ void bdrv_set_locked(BlockDriverState *bs, int locked)
trace_bdrv_set_locked(bs, locked);
bs->locked = locked;
if (drv && drv->bdrv_set_locked) {
drv->bdrv_set_locked(bs, locked);
}

View File

@ -36,6 +36,11 @@ typedef struct BlockDevOps {
* changes. Sure would be useful if it did.
*/
void (*change_media_cb)(void *opaque);
/*
* Is the virtual medium locked into the device?
* Device models implement this only when device has such a lock.
*/
bool (*is_medium_locked)(void *opaque);
/*
* Runs when the size changed (e.g. monitor command block_resize)
*/
@ -94,6 +99,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_is_medium_locked(BlockDriverState *bs);
int bdrv_read(BlockDriverState *bs, int64_t sector_num,
uint8_t *buf, int nb_sectors);
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
@ -206,7 +212,6 @@ int bdrv_is_sg(BlockDriverState *bs);
int bdrv_enable_write_cache(BlockDriverState *bs);
int bdrv_is_inserted(BlockDriverState *bs);
int bdrv_media_changed(BlockDriverState *bs);
int bdrv_is_locked(BlockDriverState *bs);
void bdrv_set_locked(BlockDriverState *bs, int locked);
void bdrv_eject(BlockDriverState *bs, int eject_flag);
void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size);

View File

@ -156,7 +156,6 @@ struct BlockDriverState {
int keep_read_only; /* if true, the media was requested to stay read only */
int open_flags; /* flags used to open the file, re-used for re-open */
int removable; /* if true, the media can be removed */
int locked; /* if true, the media cannot temporarily be ejected */
int encrypted; /* if true, the media is encrypted */
int valid_key; /* if true, a valid encryption key has been set */
int sg; /* if true, the device is a /dev/sg* */

View File

@ -640,7 +640,7 @@ static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
return -1;
}
if (!force && bdrv_is_locked(bs)) {
if (!force && bdrv_dev_is_medium_locked(bs)) {
qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
return -1;
}

View File

@ -1795,8 +1795,14 @@ void ide_bus_reset(IDEBus *bus)
bus->dma->ops->reset(bus->dma);
}
static bool ide_cd_is_medium_locked(void *opaque)
{
return ((IDEState *)opaque)->tray_locked;
}
static const BlockDevOps ide_cd_block_ops = {
.change_media_cb = ide_cd_change_cb,
.is_medium_locked = ide_cd_is_medium_locked,
};
int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind,

View File

@ -1165,6 +1165,15 @@ static void scsi_destroy(SCSIDevice *dev)
blockdev_mark_auto_del(s->qdev.conf.bs);
}
static bool scsi_cd_is_medium_locked(void *opaque)
{
return ((SCSIDiskState *)opaque)->tray_locked;
}
static const BlockDevOps scsi_cd_block_ops = {
.is_medium_locked = scsi_cd_is_medium_locked,
};
static int scsi_initfn(SCSIDevice *dev, uint8_t scsi_type)
{
SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
@ -1199,6 +1208,7 @@ static int scsi_initfn(SCSIDevice *dev, uint8_t scsi_type)
}
if (scsi_type == TYPE_ROM) {
bdrv_set_dev_ops(s->bs, &scsi_cd_block_ops, s);
s->qdev.blocksize = 2048;
} else if (scsi_type == TYPE_DISK) {
s->qdev.blocksize = s->qdev.conf.logical_block_size;