diff mbox

kvm tool: Avoid using disk_image->priv member in disk_image__new()

Message ID 1302714801-13425-1-git-send-email-prasadjoshi124@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Prasad Joshi April 13, 2011, 5:13 p.m. UTC
The disk_image->priv is supposed to be a private member for users of
disk_image__new(). The other block device drivers, for example qcow, might need
this pointer to hold their header.

Added a new function disk_image__new_readonly() which calls disk_image__new()
to allocate a new disk and then sets the priv member to mmamped address.

Signed-off-by: Prasad Joshi <prasadjoshi124@gmail.com>
---
 tools/kvm/disk-image.c             |   27 +++++++++++++++++++--------
 tools/kvm/include/kvm/disk-image.h |    3 ++-
 2 files changed, 21 insertions(+), 9 deletions(-)
diff mbox

Patch

diff --git a/tools/kvm/disk-image.c b/tools/kvm/disk-image.c
index 595d407..c666c04 100644
--- a/tools/kvm/disk-image.c
+++ b/tools/kvm/disk-image.c
@@ -13,7 +13,7 @@ 
 #include <unistd.h>
 #include <fcntl.h>
 
-struct disk_image *disk_image__new(int fd, uint64_t size, struct disk_image_operations *ops, bool readonly)
+struct disk_image *disk_image__new(int fd, uint64_t size, struct disk_image_operations *ops)
 {
 	struct disk_image *self;
 
@@ -24,16 +24,24 @@  struct disk_image *disk_image__new(int fd, uint64_t size, struct disk_image_oper
 	self->fd	= fd;
 	self->size	= size;
 	self->ops	= ops;
-	if (readonly) {
-		self->priv = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_NORESERVE, fd, 0);
-		if (self->priv == MAP_FAILED)
-			die("mmap() failed");
-	} else
-		self->priv = MAP_FAILED;
+	return self;
+}
+
+struct disk_image *disk_image__new_readonly(int fd, uint64_t size, struct disk_image_operations *ops)
+{
+	struct disk_image *self;
 
+	self = disk_image__new(fd, size, ops);
+	if (!self)
+		return NULL;
+
+	self->priv = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_NORESERVE, fd, 0);
+	if (self->priv == MAP_FAILED)
+		die("mmap() failed");
 	return self;
 }
 
+
 static int raw_image__read_sector(struct disk_image *self, uint64_t sector, void *dst, uint32_t dst_len)
 {
 	uint64_t offset = sector << SECTOR_SHIFT;
@@ -101,7 +109,10 @@  static struct disk_image *raw_image__probe(int fd, bool readonly)
 	if (fstat(fd, &st) < 0)
 		return NULL;
 
-	return disk_image__new(fd, st.st_size, readonly ? &raw_image_ro_mmap_ops : &raw_image_ops, readonly);
+	if (readonly)
+		return disk_image__new_readonly(fd, st.st_size, &raw_image_ro_mmap_ops);
+	else 
+		return disk_image__new(fd, st.st_size, &raw_image_ops);
 }
 
 struct disk_image *disk_image__open(const char *filename, bool readonly)
diff --git a/tools/kvm/include/kvm/disk-image.h b/tools/kvm/include/kvm/disk-image.h
index 91240c2..33962c6 100644
--- a/tools/kvm/include/kvm/disk-image.h
+++ b/tools/kvm/include/kvm/disk-image.h
@@ -23,7 +23,8 @@  struct disk_image {
 };
 
 struct disk_image *disk_image__open(const char *filename, bool readonly);
-struct disk_image *disk_image__new(int fd, uint64_t size, struct disk_image_operations *ops, bool readonly);
+struct disk_image *disk_image__new(int fd, uint64_t size, struct disk_image_operations *ops);
+struct disk_image *disk_image__new_readonly(int fd, uint64_t size, struct disk_image_operations *ops);
 void disk_image__close(struct disk_image *self);
 
 static inline int disk_image__read_sector(struct disk_image *self, uint64_t sector, void *dst, uint32_t dst_len)