[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 04/55] block: Generalize change_cb() to BlockDevOps
From: |
Markus Armbruster |
Subject: |
[Qemu-devel] [PATCH 04/55] block: Generalize change_cb() to BlockDevOps |
Date: |
Wed, 20 Jul 2011 18:23:38 +0200 |
So we can more easily add device model callbacks.
Signed-off-by: Markus Armbruster <address@hidden>
---
block.c | 43 +++++++++++++++++++++----------------------
block.h | 9 ++++++---
block_int.h | 5 ++---
hw/ide/core.c | 6 +++++-
hw/sd.c | 6 +++++-
hw/virtio-blk.c | 6 +++++-
6 files changed, 44 insertions(+), 31 deletions(-)
diff --git a/block.c b/block.c
index 1429028..2a1f7af 100644
--- a/block.c
+++ b/block.c
@@ -43,6 +43,7 @@
#include <windows.h>
#endif
+static void bdrv_dev_change_cb(BlockDriverState *bs, int reason);
static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque);
@@ -636,10 +637,8 @@ int bdrv_open(BlockDriverState *bs, const char *filename,
int flags,
}
if (!bdrv_key_required(bs)) {
- /* call the change callback */
bs->media_changed = 1;
- if (bs->change_cb)
- bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
+ bdrv_dev_change_cb(bs, CHANGE_MEDIA);
}
return 0;
@@ -675,10 +674,8 @@ void bdrv_close(BlockDriverState *bs)
bdrv_close(bs->file);
}
- /* call the change callback */
bs->media_changed = 1;
- if (bs->change_cb)
- bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
+ bdrv_dev_change_cb(bs, CHANGE_MEDIA);
}
}
@@ -740,8 +737,8 @@ void bdrv_detach_dev(BlockDriverState *bs, void *dev)
{
assert(bs->dev == dev);
bs->dev = NULL;
- bs->change_cb = NULL;
- bs->change_opaque = NULL;
+ bs->dev_ops = NULL;
+ bs->dev_opaque = NULL;
}
/* TODO change to return DeviceState * when all users are qdevified */
@@ -750,6 +747,20 @@ void *bdrv_get_attached_dev(BlockDriverState *bs)
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
*
@@ -1152,9 +1163,7 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset)
ret = drv->bdrv_truncate(bs, offset);
if (ret == 0) {
ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
- if (bs->change_cb) {
- bs->change_cb(bs->change_opaque, CHANGE_SIZE);
- }
+ bdrv_dev_change_cb(bs, CHANGE_SIZE);
}
return ret;
}
@@ -1473,15 +1482,6 @@ int bdrv_enable_write_cache(BlockDriverState *bs)
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)
{
if (bs->backing_hd && bs->backing_hd->encrypted)
@@ -1520,8 +1520,7 @@ int bdrv_set_key(BlockDriverState *bs, const char *key)
bs->valid_key = 1;
/* call the change callback now, we skipped it on open */
bs->media_changed = 1;
- if (bs->change_cb)
- bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
+ bdrv_dev_change_cb(bs, CHANGE_MEDIA);
}
return ret;
}
diff --git a/block.h b/block.h
index 8d8172c..623c488 100644
--- a/block.h
+++ b/block.h
@@ -27,6 +27,10 @@ typedef struct QEMUSnapshotInfo {
uint64_t vm_clock_nsec; /* VM clock relative to boot */
} QEMUSnapshotInfo;
+typedef struct BlockDevOps {
+ void (*change_cb)(void *opaque, int reason);
+} BlockDevOps;
+
#define BDRV_O_RDWR 0x0002
#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 */
@@ -76,6 +80,8 @@ int bdrv_attach_dev(BlockDriverState *bs, void *dev);
void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev);
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);
int bdrv_read(BlockDriverState *bs, int64_t sector_num,
uint8_t *buf, int nb_sectors);
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
@@ -188,9 +194,6 @@ int bdrv_media_changed(BlockDriverState *bs);
int bdrv_is_locked(BlockDriverState *bs);
void bdrv_set_locked(BlockDriverState *bs, int locked);
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);
BlockDriverState *bdrv_find(const char *name);
BlockDriverState *bdrv_next(BlockDriverState *bs);
diff --git a/block_int.h b/block_int.h
index 1a23d8a..e2fa254 100644
--- a/block_int.h
+++ b/block_int.h
@@ -152,15 +152,14 @@ struct BlockDriverState {
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* */
- /* event callback when inserting/removing */
- void (*change_cb)(void *opaque, int reason);
- void *change_opaque;
BlockDriver *drv; /* NULL means no media */
void *opaque;
void *dev; /* attached device model, if any */
/* TODO change to DeviceState when all users are qdevified */
+ const BlockDevOps *dev_ops;
+ void *dev_opaque;
char filename[1024];
char backing_file[1024]; /* if non zero, the image is a diff of
diff --git a/hw/ide/core.c b/hw/ide/core.c
index bcef8bc..773b7cf 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -1717,6 +1717,10 @@ void ide_bus_reset(IDEBus *bus)
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,
const char *version, const char *serial)
{
@@ -1751,7 +1755,7 @@ int ide_init_drive(IDEState *s, BlockDriverState *bs,
IDEDriveKind kind,
s->smart_errors = 0;
s->smart_selftest_count = 0;
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;
} else {
if (!bdrv_is_inserted(s->bs)) {
diff --git a/hw/sd.c b/hw/sd.c
index 3fdd5b0..94cea3d 100644
--- a/hw/sd.c
+++ b/hw/sd.c
@@ -437,6 +437,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
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
@@ -452,7 +456,7 @@ SDState *sd_init(BlockDriverState *bs, int is_spi)
sd_reset(sd, bs);
if (sd->bdrv) {
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;
}
diff --git a/hw/virtio-blk.c b/hw/virtio-blk.c
index 6471ac8..9e5ef88 100644
--- a/hw/virtio-blk.c
+++ b/hw/virtio-blk.c
@@ -536,6 +536,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,
char **serial)
{
@@ -582,7 +586,7 @@ VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf
*conf,
register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
virtio_blk_save, virtio_blk_load, s);
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;
add_boot_device_path(conf->bootindex, dev, "/address@hidden,0");
--
1.7.2.3
- Re: [Qemu-devel] [PATCH 08/55] block: Make BlockDriver method bdrv_eject() return void, (continued)
- [Qemu-devel] [PATCH 09/55] block: Don't let locked flag prevent medium load, Markus Armbruster, 2011/07/20
- [Qemu-devel] [PATCH 21/55] block: Revert entanglement of bdrv_is_inserted() with tray status, Markus Armbruster, 2011/07/20
- [Qemu-devel] [PATCH 27/55] scsi-disk: Switch from BlockDriverState's locked to own tray_locked, Markus Armbruster, 2011/07/20
- [Qemu-devel] [PATCH 04/55] block: Generalize change_cb() to BlockDevOps,
Markus Armbruster <=
- [Qemu-devel] [PATCH 13/55] ide: Use a table to declare which drive kinds accept each command, Markus Armbruster, 2011/07/20
- [Qemu-devel] [PATCH 12/55] ide: Fix ATA command READ to set ATAPI signature for CD-ROM, Markus Armbruster, 2011/07/20
- [Qemu-devel] [PATCH 32/55] ide/atapi: Don't fail eject when tray is already open, Markus Armbruster, 2011/07/20
- [Qemu-devel] [PATCH 05/55] block: Split change_cb() into change_media_cb(), resize_cb(), Markus Armbruster, 2011/07/20