From patchwork Wed Apr 13 17:13:21 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prasad Joshi X-Patchwork-Id: 705431 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p3DHDNCS032044 for ; Wed, 13 Apr 2011 17:13:24 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757976Ab1DMRNU (ORCPT ); Wed, 13 Apr 2011 13:13:20 -0400 Received: from mail-ww0-f44.google.com ([74.125.82.44]:65222 "EHLO mail-ww0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757890Ab1DMRNU (ORCPT ); Wed, 13 Apr 2011 13:13:20 -0400 Received: by wwa36 with SMTP id 36so991457wwa.1 for ; Wed, 13 Apr 2011 10:13:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer; bh=18j2vWKdOVPEOQJITmQembj7n3pwh5KltS0+5Cx1qUI=; b=WWWDJYq9NNiYxWrwqBIPxfCKWHoPnbUR5dT3EXSWw7VFt1D1/tMhuWJxlbF3iWtGVR +vK3oz/zMSkkDSUGBjklJX99ZL0Eiu/R3COcV0NHEp0OJLfYGBKsDxxmw4emC9z0r1aq rKJ1gIqvlf3FT8xvuFNd+s88ZhICEIcD6qjTY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=PiWdm+tLRn0gZ2aOzMK/1RaF8S7s3i3hgP540xmWNdhNPr8xpoIh+crsFTzDnW/xqP bsPoFMaDrLqSpXUJtdgwhDG3yQ/5Um9I7M6mB9II9oLBX/HM0sC5HBzf6ED/G3jGYEpu FefrqAt9/J28ktYdoKS2qGmeOq9YeALPSEaeY= Received: by 10.227.163.13 with SMTP id y13mr6073314wbx.56.1302714798415; Wed, 13 Apr 2011 10:13:18 -0700 (PDT) Received: from prasad-kvm.localdomain (pineapple.rdg.ac.uk [134.225.206.123]) by mx.google.com with ESMTPS id o6sm466949wbo.37.2011.04.13.10.13.16 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 13 Apr 2011 10:13:17 -0700 (PDT) Received: by prasad-kvm.localdomain (Postfix, from userid 1000) id CC31926E006E; Wed, 13 Apr 2011 18:13:22 +0100 (BST) From: Prasad Joshi To: prasadjoshi124@gmail.com Cc: mingo@elte.hu, kvm@vger.kernel.org, penberg@kernel.org, asias.hejun@gmail.com, gorcunov@gmail.com, levinsasha928@gmail.com Subject: [PATCH] kvm tool: Avoid using disk_image->priv member in disk_image__new() Date: Wed, 13 Apr 2011 18:13:21 +0100 Message-Id: <1302714801-13425-1-git-send-email-prasadjoshi124@gmail.com> X-Mailer: git-send-email 1.7.1 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Wed, 13 Apr 2011 17:13:24 +0000 (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 --- tools/kvm/disk-image.c | 27 +++++++++++++++++++-------- tools/kvm/include/kvm/disk-image.h | 3 ++- 2 files changed, 21 insertions(+), 9 deletions(-) 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 #include -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)