提交 0e49de52 编写于 作者: M Markus Armbruster 提交者: Kevin Wolf

block: Generalize change_cb() to BlockDevOps

So we can more easily add device model callbacks.
Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
Signed-off-by: NKevin Wolf <kwolf@redhat.com>
上级 fa879d62
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
#include <windows.h> #include <windows.h>
#endif #endif
static void bdrv_dev_change_cb(BlockDriverState *bs, int reason);
static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque); BlockDriverCompletionFunc *cb, void *opaque);
...@@ -688,10 +689,8 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags, ...@@ -688,10 +689,8 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
} }
if (!bdrv_key_required(bs)) { if (!bdrv_key_required(bs)) {
/* call the change callback */
bs->media_changed = 1; bs->media_changed = 1;
if (bs->change_cb) bdrv_dev_change_cb(bs, CHANGE_MEDIA);
bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
} }
return 0; return 0;
...@@ -727,10 +726,8 @@ void bdrv_close(BlockDriverState *bs) ...@@ -727,10 +726,8 @@ void bdrv_close(BlockDriverState *bs)
bdrv_close(bs->file); bdrv_close(bs->file);
} }
/* call the change callback */
bs->media_changed = 1; bs->media_changed = 1;
if (bs->change_cb) bdrv_dev_change_cb(bs, CHANGE_MEDIA);
bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
} }
} }
...@@ -792,8 +789,8 @@ void bdrv_detach_dev(BlockDriverState *bs, void *dev) ...@@ -792,8 +789,8 @@ void bdrv_detach_dev(BlockDriverState *bs, void *dev)
{ {
assert(bs->dev == dev); assert(bs->dev == dev);
bs->dev = NULL; bs->dev = NULL;
bs->change_cb = NULL; bs->dev_ops = NULL;
bs->change_opaque = NULL; bs->dev_opaque = NULL;
} }
/* TODO change to return DeviceState * when all users are qdevified */ /* TODO change to return DeviceState * when all users are qdevified */
...@@ -802,6 +799,20 @@ void *bdrv_get_attached_dev(BlockDriverState *bs) ...@@ -802,6 +799,20 @@ void *bdrv_get_attached_dev(BlockDriverState *bs)
return bs->dev; return bs->dev;
} }
void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
void *opaque)
{
bs->dev_ops = ops;
bs->dev_opaque = opaque;
}
static void bdrv_dev_change_cb(BlockDriverState *bs, int reason)
{
if (bs->dev_ops && bs->dev_ops->change_cb) {
bs->dev_ops->change_cb(bs->dev_opaque, reason);
}
}
/* /*
* Run consistency checks on an image * Run consistency checks on an image
* *
...@@ -1272,9 +1283,7 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset) ...@@ -1272,9 +1283,7 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset)
ret = drv->bdrv_truncate(bs, offset); ret = drv->bdrv_truncate(bs, offset);
if (ret == 0) { if (ret == 0) {
ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
if (bs->change_cb) { bdrv_dev_change_cb(bs, CHANGE_SIZE);
bs->change_cb(bs->change_opaque, CHANGE_SIZE);
}
} }
return ret; return ret;
} }
...@@ -1612,15 +1621,6 @@ int bdrv_enable_write_cache(BlockDriverState *bs) ...@@ -1612,15 +1621,6 @@ int bdrv_enable_write_cache(BlockDriverState *bs)
return bs->enable_write_cache; return bs->enable_write_cache;
} }
/* XXX: no longer used */
void bdrv_set_change_cb(BlockDriverState *bs,
void (*change_cb)(void *opaque, int reason),
void *opaque)
{
bs->change_cb = change_cb;
bs->change_opaque = opaque;
}
int bdrv_is_encrypted(BlockDriverState *bs) int bdrv_is_encrypted(BlockDriverState *bs)
{ {
if (bs->backing_hd && bs->backing_hd->encrypted) if (bs->backing_hd && bs->backing_hd->encrypted)
...@@ -1659,8 +1659,7 @@ int bdrv_set_key(BlockDriverState *bs, const char *key) ...@@ -1659,8 +1659,7 @@ int bdrv_set_key(BlockDriverState *bs, const char *key)
bs->valid_key = 1; bs->valid_key = 1;
/* call the change callback now, we skipped it on open */ /* call the change callback now, we skipped it on open */
bs->media_changed = 1; bs->media_changed = 1;
if (bs->change_cb) bdrv_dev_change_cb(bs, CHANGE_MEDIA);
bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
} }
return ret; return ret;
} }
......
...@@ -28,6 +28,10 @@ typedef struct QEMUSnapshotInfo { ...@@ -28,6 +28,10 @@ typedef struct QEMUSnapshotInfo {
uint64_t vm_clock_nsec; /* VM clock relative to boot */ uint64_t vm_clock_nsec; /* VM clock relative to boot */
} QEMUSnapshotInfo; } QEMUSnapshotInfo;
typedef struct BlockDevOps {
void (*change_cb)(void *opaque, int reason);
} BlockDevOps;
#define BDRV_O_RDWR 0x0002 #define BDRV_O_RDWR 0x0002
#define BDRV_O_SNAPSHOT 0x0008 /* open the file read only and save writes in a snapshot */ #define BDRV_O_SNAPSHOT 0x0008 /* open the file read only and save writes in a snapshot */
#define BDRV_O_NOCACHE 0x0020 /* do not use the host page cache */ #define BDRV_O_NOCACHE 0x0020 /* do not use the host page cache */
...@@ -78,6 +82,8 @@ int bdrv_attach_dev(BlockDriverState *bs, void *dev); ...@@ -78,6 +82,8 @@ int bdrv_attach_dev(BlockDriverState *bs, void *dev);
void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev); void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev);
void bdrv_detach_dev(BlockDriverState *bs, void *dev); void bdrv_detach_dev(BlockDriverState *bs, void *dev);
void *bdrv_get_attached_dev(BlockDriverState *bs); void *bdrv_get_attached_dev(BlockDriverState *bs);
void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
void *opaque);
int bdrv_read(BlockDriverState *bs, int64_t sector_num, int bdrv_read(BlockDriverState *bs, int64_t sector_num,
uint8_t *buf, int nb_sectors); uint8_t *buf, int nb_sectors);
int bdrv_write(BlockDriverState *bs, int64_t sector_num, int bdrv_write(BlockDriverState *bs, int64_t sector_num,
...@@ -193,9 +199,6 @@ int bdrv_media_changed(BlockDriverState *bs); ...@@ -193,9 +199,6 @@ int bdrv_media_changed(BlockDriverState *bs);
int bdrv_is_locked(BlockDriverState *bs); int bdrv_is_locked(BlockDriverState *bs);
void bdrv_set_locked(BlockDriverState *bs, int locked); void bdrv_set_locked(BlockDriverState *bs, int locked);
int bdrv_eject(BlockDriverState *bs, int eject_flag); int bdrv_eject(BlockDriverState *bs, int eject_flag);
void bdrv_set_change_cb(BlockDriverState *bs,
void (*change_cb)(void *opaque, int reason),
void *opaque);
void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size); void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size);
BlockDriverState *bdrv_find(const char *name); BlockDriverState *bdrv_find(const char *name);
BlockDriverState *bdrv_next(BlockDriverState *bs); BlockDriverState *bdrv_next(BlockDriverState *bs);
......
...@@ -161,15 +161,14 @@ struct BlockDriverState { ...@@ -161,15 +161,14 @@ struct BlockDriverState {
int encrypted; /* if true, the media is encrypted */ int encrypted; /* if true, the media is encrypted */
int valid_key; /* if true, a valid encryption key has been set */ int valid_key; /* if true, a valid encryption key has been set */
int sg; /* if true, the device is a /dev/sg* */ int sg; /* if true, the device is a /dev/sg* */
/* event callback when inserting/removing */
void (*change_cb)(void *opaque, int reason);
void *change_opaque;
BlockDriver *drv; /* NULL means no media */ BlockDriver *drv; /* NULL means no media */
void *opaque; void *opaque;
void *dev; /* attached device model, if any */ void *dev; /* attached device model, if any */
/* TODO change to DeviceState when all users are qdevified */ /* TODO change to DeviceState when all users are qdevified */
const BlockDevOps *dev_ops;
void *dev_opaque;
char filename[1024]; char filename[1024];
char backing_file[1024]; /* if non zero, the image is a diff of char backing_file[1024]; /* if non zero, the image is a diff of
......
...@@ -1742,6 +1742,10 @@ void ide_bus_reset(IDEBus *bus) ...@@ -1742,6 +1742,10 @@ void ide_bus_reset(IDEBus *bus)
bus->dma->ops->reset(bus->dma); bus->dma->ops->reset(bus->dma);
} }
static const BlockDevOps ide_cd_block_ops = {
.change_cb = cdrom_change_cb,
};
int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind, int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind,
const char *version, const char *serial) const char *version, const char *serial)
{ {
...@@ -1776,7 +1780,7 @@ int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind, ...@@ -1776,7 +1780,7 @@ int ide_init_drive(IDEState *s, BlockDriverState *bs, IDEDriveKind kind,
s->smart_errors = 0; s->smart_errors = 0;
s->smart_selftest_count = 0; s->smart_selftest_count = 0;
if (kind == IDE_CD) { if (kind == IDE_CD) {
bdrv_set_change_cb(bs, cdrom_change_cb, s); bdrv_set_dev_ops(bs, &ide_cd_block_ops, s);
bs->buffer_alignment = 2048; bs->buffer_alignment = 2048;
} else { } else {
if (!bdrv_is_inserted(s->bs)) { if (!bdrv_is_inserted(s->bs)) {
......
...@@ -435,6 +435,10 @@ static void sd_cardchange(void *opaque, int reason) ...@@ -435,6 +435,10 @@ static void sd_cardchange(void *opaque, int reason)
} }
} }
static const BlockDevOps sd_block_ops = {
.change_cb = sd_cardchange,
};
/* We do not model the chip select pin, so allow the board to select /* We do not model the chip select pin, so allow the board to select
whether card should be in SSI or MMC/SD mode. It is also up to the whether card should be in SSI or MMC/SD mode. It is also up to the
board to ensure that ssi transfers only occur when the chip select board to ensure that ssi transfers only occur when the chip select
...@@ -450,7 +454,7 @@ SDState *sd_init(BlockDriverState *bs, int is_spi) ...@@ -450,7 +454,7 @@ SDState *sd_init(BlockDriverState *bs, int is_spi)
sd_reset(sd, bs); sd_reset(sd, bs);
if (sd->bdrv) { if (sd->bdrv) {
bdrv_attach_dev_nofail(sd->bdrv, sd); bdrv_attach_dev_nofail(sd->bdrv, sd);
bdrv_set_change_cb(sd->bdrv, sd_cardchange, sd); bdrv_set_dev_ops(sd->bdrv, &sd_block_ops, sd);
} }
return sd; return sd;
} }
......
...@@ -552,6 +552,10 @@ static void virtio_blk_change_cb(void *opaque, int reason) ...@@ -552,6 +552,10 @@ static void virtio_blk_change_cb(void *opaque, int reason)
} }
} }
static const BlockDevOps virtio_block_ops = {
.change_cb = virtio_blk_change_cb,
};
VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf, VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf,
char **serial) char **serial)
{ {
...@@ -598,7 +602,7 @@ VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf, ...@@ -598,7 +602,7 @@ VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf,
register_savevm(dev, "virtio-blk", virtio_blk_id++, 2, register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
virtio_blk_save, virtio_blk_load, s); virtio_blk_save, virtio_blk_load, s);
bdrv_set_removable(s->bs, 0); bdrv_set_removable(s->bs, 0);
bdrv_set_change_cb(s->bs, virtio_blk_change_cb, s); bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
s->bs->buffer_alignment = conf->logical_block_size; s->bs->buffer_alignment = conf->logical_block_size;
add_boot_device_path(conf->bootindex, dev, "/disk@0,0"); add_boot_device_path(conf->bootindex, dev, "/disk@0,0");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册