diff mbox

[06/14] kvm tools: Consolidate disk_image__{new, new_readonly}

Message ID 1305706755-2816-6-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
This patch simplifies the disk image API.

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

Patch

diff --git a/tools/kvm/disk/core.c b/tools/kvm/disk/core.c
index 3b12fff..2990caa 100644
--- a/tools/kvm/disk/core.c
+++ b/tools/kvm/disk/core.c
@@ -1,7 +1,7 @@ 
 #include "kvm/disk-image.h"
 #include "kvm/qcow.h"
 
-struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operations *ops)
+struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operations *ops, int use_mmap)
 {
 	struct disk_image *disk;
 
@@ -12,20 +12,16 @@  struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operation
 	disk->fd	= fd;
 	disk->size	= size;
 	disk->ops	= ops;
-	return disk;
-}
-
-struct disk_image *disk_image__new_readonly(int fd, u64 size, struct disk_image_operations *ops)
-{
-	struct disk_image *disk;
 
-	disk = disk_image__new(fd, size, ops);
-	if (!disk)
-		return NULL;
+	if (use_mmap == DISK_IMAGE_MMAP) {
+		/*
+		 * The write to disk image will be discarded
+		 */
+		disk->priv = mmap(NULL, size, PROT_RW, MAP_PRIVATE | MAP_NORESERVE, fd, 0);
+		if (disk->priv == MAP_FAILED)
+			die("mmap() failed");
+	}
 
-	disk->priv = mmap(NULL, size, PROT_RW, MAP_PRIVATE | MAP_NORESERVE, fd, 0);
-	if (disk->priv == MAP_FAILED)
-		die("mmap() failed");
 	return disk;
 }
 
diff --git a/tools/kvm/disk/qcow.c b/tools/kvm/disk/qcow.c
index bb2345c..b29efb4 100644
--- a/tools/kvm/disk/qcow.c
+++ b/tools/kvm/disk/qcow.c
@@ -433,10 +433,13 @@  static struct disk_image *qcow2_probe(int fd, bool readonly)
 	if (qcow_read_l1_table(q) < 0)
 		goto error;
 
+	/*
+	 * Do not use mmap use read/write instead
+	 */
 	if (readonly)
-		disk_image = disk_image__new(fd, h->size, &qcow1_disk_readonly_ops);
+		disk_image = disk_image__new(fd, h->size, &qcow1_disk_readonly_ops, DISK_IMAGE_NOMMAP);
 	else
-		disk_image = disk_image__new(fd, h->size, &qcow1_disk_ops);
+		disk_image = disk_image__new(fd, h->size, &qcow1_disk_ops, DISK_IMAGE_NOMMAP);
 
 	if (!disk_image)
 		goto error;
@@ -527,10 +530,13 @@  static struct disk_image *qcow1_probe(int fd, bool readonly)
 	if (qcow_read_l1_table(q) < 0)
 		goto error;
 
+	/*
+	 * Do not use mmap use read/write instead
+	 */
 	if (readonly)
-		disk_image = disk_image__new(fd, h->size, &qcow1_disk_readonly_ops);
+		disk_image = disk_image__new(fd, h->size, &qcow1_disk_readonly_ops, DISK_IMAGE_NOMMAP);
 	else
-		disk_image = disk_image__new(fd, h->size, &qcow1_disk_ops);
+		disk_image = disk_image__new(fd, h->size, &qcow1_disk_ops, DISK_IMAGE_NOMMAP);
 
 	if (!disk_image)
 		goto error;
diff --git a/tools/kvm/disk/raw.c b/tools/kvm/disk/raw.c
index b1a484d..a419c32 100644
--- a/tools/kvm/disk/raw.c
+++ b/tools/kvm/disk/raw.c
@@ -57,10 +57,18 @@  static struct disk_image_operations raw_image_ro_mmap_ops = {
 
 struct disk_image *raw_image__probe(int fd, struct stat *st, bool readonly)
 {
+
 	if (readonly)
-		return disk_image__new_readonly(fd, st->st_size, &raw_image_ro_mmap_ops);
+		/*
+		 * Use mmap's MAP_PRIVATE to implement non-persistent write
+		 * FIXME: This does not work on 32-bit host.
+		 */
+		return disk_image__new(fd, st->st_size, &raw_image_ro_mmap_ops, DISK_IMAGE_MMAP);
 	else
-		return disk_image__new(fd, st->st_size, &raw_image_ops);
+		/*
+		 * Use read/write instead of mmap
+		 */
+		return disk_image__new(fd, st->st_size, &raw_image_ops, DISK_IMAGE_NOMMAP);
 }
 
 struct disk_image *blkdev__probe(const char *filename, struct stat *st)
@@ -80,5 +88,5 @@  struct disk_image *blkdev__probe(const char *filename, struct stat *st)
 		return NULL;
 	}
 
-	return disk_image__new_readonly(fd, size, &raw_image_ro_mmap_ops);
+	return disk_image__new(fd, size, &raw_image_ro_mmap_ops, DISK_IMAGE_MMAP);
 }
diff --git a/tools/kvm/include/kvm/disk-image.h b/tools/kvm/include/kvm/disk-image.h
index e6d9e8e..efa3e42 100644
--- a/tools/kvm/include/kvm/disk-image.h
+++ b/tools/kvm/include/kvm/disk-image.h
@@ -21,6 +21,9 @@ 
 #define SECTOR_SHIFT		9
 #define SECTOR_SIZE		(1UL << SECTOR_SHIFT)
 
+#define DISK_IMAGE_MMAP		0
+#define DISK_IMAGE_NOMMAP	1
+
 struct disk_image;
 
 struct disk_image_operations {
@@ -40,8 +43,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);
-struct disk_image *disk_image__new_readonly(int fd, u64 size, struct disk_image_operations *ops);
+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);
 
 ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);