diff mbox

[08/14] kvm tools: Tune up ops in 'struct disk_image_operations'

Message ID 1305706755-2816-8-git-send-email-asias.hejun@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Asias He May 18, 2011, 8:19 a.m. UTC
Make read/write ops in 'struct disk_image_operations'
always return the number of bytes read/written and close/flush
ops return int.

Signed-off-by: Asias He <asias.hejun@gmail.com>
---
 tools/kvm/disk/core.c              |    8 +++++---
 tools/kvm/disk/qcow.c              |   18 ++++++++++--------
 tools/kvm/disk/raw.c               |   16 ++++++++++------
 tools/kvm/include/kvm/disk-image.h |   22 +++++++++++++++-------
 4 files changed, 40 insertions(+), 24 deletions(-)
diff mbox

Patch

diff --git a/tools/kvm/disk/core.c b/tools/kvm/disk/core.c
index 2990caa..a0ccdc5 100644
--- a/tools/kvm/disk/core.c
+++ b/tools/kvm/disk/core.c
@@ -55,19 +55,21 @@  struct disk_image *disk_image__open(const char *filename, bool readonly)
 	return NULL;
 }
 
-void disk_image__close(struct disk_image *disk)
+int disk_image__close(struct disk_image *disk)
 {
 	/* If there was no disk image then there's nothing to do: */
 	if (!disk)
-		return;
+		return 0;
 
 	if (disk->ops->close)
-		disk->ops->close(disk);
+		return disk->ops->close(disk);
 
 	if (close(disk->fd) < 0)
 		warning("close() failed");
 
 	free(disk);
+
+	return 0;
 }
 
 /* Fill iov with disk data, starting from sector 'sector'. Return amount of bytes read. */
diff --git a/tools/kvm/disk/qcow.c b/tools/kvm/disk/qcow.c
index b29efb4..956775e 100644
--- a/tools/kvm/disk/qcow.c
+++ b/tools/kvm/disk/qcow.c
@@ -103,7 +103,7 @@  out_error:
 	goto out;
 }
 
-static int qcow1_read_sector(struct disk_image *disk, u64 sector, void *dst, u32 dst_len)
+static ssize_t qcow1_read_sector(struct disk_image *disk, u64 sector, void *dst, u32 dst_len)
 {
 	struct qcow *q = disk->priv;
 	struct qcow_header *header = q->header;
@@ -129,7 +129,7 @@  static int qcow1_read_sector(struct disk_image *disk, u64 sector, void *dst, u32
 		sector		+= (nr >> SECTOR_SHIFT);
 	}
 
-	return 0;
+	return dst_len;
 }
 
 static inline u64 file_size(int fd)
@@ -291,7 +291,7 @@  error:
 	return -1;
 }
 
-static int qcow1_write_sector(struct disk_image *disk, u64 sector, void *src, u32 src_len)
+static ssize_t qcow1_write_sector(struct disk_image *disk, u64 sector, void *src, u32 src_len)
 {
 	struct qcow *q = disk->priv;
 	struct qcow_header *header = q->header;
@@ -317,33 +317,35 @@  static int qcow1_write_sector(struct disk_image *disk, u64 sector, void *src, u3
 		offset		+= nr;
 	}
 
-	return 0;
+	return nr_written;
 }
 
-static int qcow1_nowrite_sector(struct disk_image *disk, u64 sector, void *src, u32 src_len)
+static ssize_t qcow1_nowrite_sector(struct disk_image *disk, u64 sector, void *src, u32 src_len)
 {
 	/* I/O error */
 	return -1;
 }
 
-static void qcow1_disk_close(struct disk_image *disk)
+static int qcow1_disk_close(struct disk_image *disk)
 {
 	struct qcow *q;
 
 	if (!disk)
-		return;
+		return 0;
 
 	q = disk->priv;
 
 	free(q->table.l1_table);
 	free(q->header);
 	free(q);
+
+	return 0;
 }
 
 static struct disk_image_operations qcow1_disk_readonly_ops = {
 	.read_sector		= qcow1_read_sector,
 	.write_sector		= qcow1_nowrite_sector,
-	.close			= qcow1_disk_close
+	.close			= qcow1_disk_close,
 };
 
 static struct disk_image_operations qcow1_disk_ops = {
diff --git a/tools/kvm/disk/raw.c b/tools/kvm/disk/raw.c
index 8ee7e3f..b58837b 100644
--- a/tools/kvm/disk/raw.c
+++ b/tools/kvm/disk/raw.c
@@ -14,7 +14,7 @@  ssize_t raw_image__write_sector_iov(struct disk_image *disk, u64 sector, const s
 	return pwritev_in_full(disk->fd, iov, iovcount, offset);
 }
 
-int raw_image__read_sector_ro_mmap(struct disk_image *disk, u64 sector, void *dst, u32 dst_len)
+ssize_t raw_image__read_sector_ro_mmap(struct disk_image *disk, u64 sector, void *dst, u32 dst_len)
 {
 	u64 offset = sector << SECTOR_SHIFT;
 
@@ -23,10 +23,10 @@  int raw_image__read_sector_ro_mmap(struct disk_image *disk, u64 sector, void *ds
 
 	memcpy(dst, disk->priv + offset, dst_len);
 
-	return 0;
+	return dst_len;
 }
 
-int raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, void *src, u32 src_len)
+ssize_t raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, void *src, u32 src_len)
 {
 	u64 offset = sector << SECTOR_SHIFT;
 
@@ -35,13 +35,17 @@  int raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, void *s
 
 	memcpy(disk->priv + offset, src, src_len);
 
-	return 0;
+	return src_len;
 }
 
-void raw_image__close_ro_mmap(struct disk_image *disk)
+int raw_image__close_ro_mmap(struct disk_image *disk)
 {
+	int ret = 0;
+
 	if (disk->priv != MAP_FAILED)
-		munmap(disk->priv, disk->size);
+		ret = munmap(disk->priv, disk->size);
+
+	return ret;
 }
 
 static struct disk_image_operations raw_image_ops = {
diff --git a/tools/kvm/include/kvm/disk-image.h b/tools/kvm/include/kvm/disk-image.h
index 463801e..6531a93 100644
--- a/tools/kvm/include/kvm/disk-image.h
+++ b/tools/kvm/include/kvm/disk-image.h
@@ -27,12 +27,20 @@ 
 struct disk_image;
 
 struct disk_image_operations {
-	int (*read_sector)(struct disk_image *disk, u64 sector, void *dst, u32 dst_len);
-	int (*write_sector)(struct disk_image *disk, u64 sector, void *src, u32 src_len);
+	/*
+	 * The following two are used for reading or writing with a single buffer.
+	 * The implentation can use readv/writev or memcpy.
+	 */
+	ssize_t (*read_sector)(struct disk_image *disk, u64 sector, void *dst, u32 dst_len);
+	ssize_t (*write_sector)(struct disk_image *disk, u64 sector, void *src, u32 src_len);
+	/*
+	 * The following two are used for reading or writing with multiple buffers.
+	 * The implentation can use readv/writev or memcpy.
+	 */
 	ssize_t (*read_sector_iov)(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
 	ssize_t (*write_sector_iov)(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
 	int (*flush)(struct disk_image *disk);
-	void (*close)(struct disk_image *disk);
+	int (*close)(struct disk_image *disk);
 };
 
 struct disk_image {
@@ -44,7 +52,7 @@  struct disk_image {
 
 struct disk_image *disk_image__open(const char *filename, bool readonly);
 struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operations *ops, int mmap);
-void disk_image__close(struct disk_image *disk);
+int disk_image__close(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);
@@ -61,8 +69,8 @@  struct disk_image *blkdev__probe(const char *filename, struct stat *st);
 
 ssize_t raw_image__read_sector_iov(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
 ssize_t raw_image__write_sector_iov(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
-int raw_image__read_sector_ro_mmap(struct disk_image *disk, u64 sector, void *dst, u32 dst_len);
-int raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, void *src, u32 src_len);
-void raw_image__close_ro_mmap(struct disk_image *disk);
+ssize_t raw_image__read_sector_ro_mmap(struct disk_image *disk, u64 sector, void *dst, u32 dst_len);
+ssize_t raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, void *src, u32 src_len);
+int raw_image__close_ro_mmap(struct disk_image *disk);
 
 #endif /* KVM__DISK_IMAGE_H */