[PATCH v2 05/10] kvm tools: Add optional callback on disk op completion

Sasha Levin
2011-11-01T16:08:16Z

This patch adds an optional callback to be called when a disk op completes.

Currently theres not much use for it, but it is the infrastructure for adding
aio support.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
---
tools/kvm/disk/core.c | 22 ++++++++++++++++++----
tools/kvm/disk/qcow.c | 6 +++---
tools/kvm/disk/raw.c | 12 ++++++++----
tools/kvm/include/kvm/disk-image.h | 24 +++++++++++++++---------
tools/kvm/virtio/blk.c | 6 ++++--
5 files changed, 48 insertions(+), 22 deletions(-)

diff --git a/tools/kvm/disk/core.c b/tools/kvm/disk/core.c
index 4f51cbe..c1cb52f 100644
--- a/tools/kvm/disk/core.c
+++ b/tools/kvm/disk/core.c
@@ -133,7 +133,8 @@ void disk_image__close_all(struct disk_image **disks, int count)
* Fill iov with disk data, starting from sector 'sector'.
* Return amount of bytes read.
*/
-ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
+ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec *iov,
+ int iovcount, void *param)
{
ssize_t total = 0;

@@ -141,7 +142,7 @@ ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec
msleep(debug_iodelay);

if (disk->ops->read_sector) {
- total = disk->ops->read_sector(disk, sector, iov, iovcount);
+ total = disk->ops->read_sector(disk, sector, iov, iovcount, param);
if (total < 0) {
pr_info("disk_image__read error: total=%ld\n", (long)total);
return -1;
@@ -150,6 +151,9 @@ ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec
/* Do nothing */
}

+ if (disk->disk_req_cb)
+ disk->disk_req_cb(param);
+
return total;
}

@@ -157,7 +161,8 @@ ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec
* Write iov to disk, starting from sector 'sector'.
* Return amount of bytes written.
*/
-ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
+ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iovec *iov,
+ int iovcount, void *param)
{
ssize_t total = 0;

@@ -168,7 +173,8 @@ ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iove
/*
* Try writev based operation first
*/
- total = disk->ops->write_sector(disk, sector, iov, iovcount);
+
+ total = disk->ops->write_sector(disk, sector, iov, iovcount, param);
if (total < 0) {
pr_info("disk_image__write error: total=%ld\n", (long)total);
return -1;
@@ -177,6 +183,9 @@ ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iove
/* Do nothing */
}

+ if (disk->disk_req_cb)
+ disk->disk_req_cb(param);
+
return total;
}

@@ -190,3 +199,8 @@ ssize_t disk_image__get_serial(struct disk_image *disk, void *buffer, ssize_t *l
*len = snprintf(buffer, *len, "%llu%llu%llu", (u64)st.st_dev, (u64)st.st_rdev, (u64)st.st_ino);
return *len;
}
+
+void disk_image__set_callback(struct disk_image *disk, void (*disk_req_cb)(void *param))
+{
+ disk->disk_req_cb = disk_req_cb;
+}
diff --git a/tools/kvm/disk/qcow.c b/tools/kvm/disk/qcow.c
index 288782e..ba65ab6 100644
--- a/tools/kvm/disk/qcow.c
+++ b/tools/kvm/disk/qcow.c
@@ -489,7 +489,7 @@ static ssize_t qcow_read_sector_single(struct disk_image *disk, u64 sector,
}

static ssize_t qcow_read_sector(struct disk_image *disk, u64 sector,
- const struct iovec *iov, int iovcount)
+ const struct iovec *iov, int iovcount, void *param)
{
ssize_t nr, total = 0;

@@ -917,7 +917,7 @@ static ssize_t qcow_write_sector_single(struct disk_image *disk, u64 sector, voi
}

static ssize_t qcow_write_sector(struct disk_image *disk, u64 sector,
- const struct iovec *iov, int iovcount)
+ const struct iovec *iov, int iovcount, void *param)
{
ssize_t nr, total = 0;

@@ -937,7 +937,7 @@ static ssize_t qcow_write_sector(struct disk_image *disk, u64 sector,
}

static ssize_t qcow_nowrite_sector(struct disk_image *disk, u64 sector,
- const struct iovec *iov, int iovcount)
+ const struct iovec *iov, int iovcount, void *param)
{
/* I/O error */
pr_info("%s: no write support\n", __func__);
diff --git a/tools/kvm/disk/raw.c b/tools/kvm/disk/raw.c
index 7255eb4..51e8318 100644
--- a/tools/kvm/disk/raw.c
+++ b/tools/kvm/disk/raw.c
@@ -1,20 +1,23 @@
#include "kvm/disk-image.h"

-ssize_t raw_image__read_sector(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
+ssize_t raw_image__read_sector(struct disk_image *disk, u64 sector, const struct iovec *iov,
+ int iovcount, void *param)
{
u64 offset = sector << SECTOR_SHIFT;

return preadv_in_full(disk->fd, iov, iovcount, offset);
}

-ssize_t raw_image__write_sector(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
+ssize_t raw_image__write_sector(struct disk_image *disk, u64 sector, const struct iovec *iov,
+ int iovcount, void *param)
{
u64 offset = sector << SECTOR_SHIFT;

return pwritev_in_full(disk->fd, iov, iovcount, offset);
}

-ssize_t raw_image__read_sector_mmap(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
+ssize_t raw_image__read_sector_mmap(struct disk_image *disk, u64 sector, const struct iovec *iov,
+ int iovcount, void *param)
{
u64 offset = sector << SECTOR_SHIFT;
ssize_t total = 0;
@@ -31,7 +34,8 @@ ssize_t raw_image__read_sector_mmap(struct disk_image *disk, u64 sector, const s
return total;
}

-ssize_t raw_image__write_sector_mmap(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
+ssize_t raw_image__write_sector_mmap(struct disk_image *disk, u64 sector, const struct iovec *iov,
+ int iovcount, void *param)
{
u64 offset = sector << SECTOR_SHIFT;
ssize_t total = 0;
diff --git a/tools/kvm/include/kvm/disk-image.h b/tools/kvm/include/kvm/disk-image.h
index 6a6ae6f..f22213f 100644
--- a/tools/kvm/include/kvm/disk-image.h
+++ b/tools/kvm/include/kvm/disk-image.h
@@ -31,8 +31,10 @@ enum {
struct disk_image;

struct disk_image_operations {
- ssize_t (*read_sector)(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
- ssize_t (*write_sector)(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
+ ssize_t (*read_sector)(struct disk_image *disk, u64 sector, const struct iovec *iov,
+ int iovcount, void *param);
+ ssize_t (*write_sector)(struct disk_image *disk, u64 sector, const struct iovec *iov,
+ int iovcount, void *param);
int (*flush)(struct disk_image *disk);
int (*close)(struct disk_image *disk);
};
@@ -42,6 +44,8 @@ struct disk_image {
u64 size;
struct disk_image_operations *ops;
void *priv;
+ void *disk_req_cb_param;
+ void (*disk_req_cb)(void *param);
};

struct disk_image *disk_image__open(const char *filename, bool readonly);
@@ -50,21 +54,23 @@ struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operation
int disk_image__close(struct disk_image *disk);
void disk_image__close_all(struct disk_image **disks, int count);
int disk_image__flush(struct disk_image *disk);
-ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
-ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
+ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec *iov,
+ int iovcount, void *param);
+ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iovec *iov,
+ int iovcount, void *param);
ssize_t disk_image__get_serial(struct disk_image *disk, void *buffer, ssize_t *len);

struct disk_image *raw_image__probe(int fd, struct stat *st, bool readonly);
struct disk_image *blkdev__probe(const char *filename, struct stat *st);

ssize_t raw_image__read_sector(struct disk_image *disk, u64 sector,
- const struct iovec *iov, int iovcount);
+ const struct iovec *iov, int iovcount, void *param);
ssize_t raw_image__write_sector(struct disk_image *disk, u64 sector,
- const struct iovec *iov, int iovcount);
+ const struct iovec *iov, int iovcount, void *param);
ssize_t raw_image__read_sector_mmap(struct disk_image *disk, u64 sector,
- const struct iovec *iov, int iovcount);
+ const struct iovec *iov, int iovcount, void *param);
ssize_t raw_image__write_sector_mmap(struct disk_image *disk, u64 sector,
- const struct iovec *iov, int iovcount);
+ const struct iovec *iov, int iovcount, void *param);
int raw_image__close(struct disk_image *disk);
-
+void disk_image__set_callback(struct disk_image *disk, void (*disk_req_cb)(void *param));
#endif /* KVM__DISK_IMAGE_H */
diff --git a/tools/kvm/virtio/blk.c b/tools/kvm/virtio/blk.c
index 6ffa753..9db293e 100644
--- a/tools/kvm/virtio/blk.c
+++ b/tools/kvm/virtio/blk.c
@@ -77,10 +77,12 @@ static void virtio_blk_do_io_request(struct kvm *kvm, void *param)

switch (req->type) {
case VIRTIO_BLK_T_IN:
- block_cnt = disk_image__read(bdev->disk, req->sector, iov + 1, in + out - 2);
+ block_cnt = disk_image__read(bdev->disk, req->sector, iov + 1,
+ in + out - 2, NULL);
break;
case VIRTIO_BLK_T_OUT:
- block_cnt = disk_image__write(bdev->disk, req->sector, iov + 1, in + out - 2);
+ block_cnt = disk_image__write(bdev->disk, req->sector, iov + 1,
+ in + out - 2, NULL);
break;
case VIRTIO_BLK_T_FLUSH:
block_cnt = disk_image__flush(bdev->disk);
--
1.7.7.1