From patchwork Wed May 18 08:19:07 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asias He X-Patchwork-Id: 793062 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.3) with ESMTP id p4I8O3wl004358 for ; Wed, 18 May 2011 08:24:06 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755569Ab1ERIXH (ORCPT ); Wed, 18 May 2011 04:23:07 -0400 Received: from mail-px0-f173.google.com ([209.85.212.173]:43960 "EHLO mail-px0-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755554Ab1ERIXG (ORCPT ); Wed, 18 May 2011 04:23:06 -0400 Received: by pxi16 with SMTP id 16so911879pxi.4 for ; Wed, 18 May 2011 01:23:06 -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 :in-reply-to:references; bh=wycFcqPMTGhZhEjufOT5M5tDR93dFmvnPQpmrEW8jgk=; b=lTUso6OElL9wYk1TElpA0ivZ2dQZMFMUqm+8uhdlSCwvMQ1v6wF9+KRuw4MhKruw9h CUgGDaBUTvq/7gBo61H0vt+2VFGUTUE73V8jvSMxIUg0T1k9SfUqjdcqbO5NTYyhXTNy XAXCfJoFnACB1119ufRHrsoEQ03oUYo244+ZU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=AJ1njoKhkuEUZ4bOm1H1SG+C4wYLi/EVbQ6O5Fft04GFjQ9QFQBGwL9NNj81OG0F2I +Ubt9nJSkQ6Qwek1EecklhTFM9pxEbBi2PYwfRV79b8woUptQRlBxVHEyIxLBqZxZbVO 5G9dv3NEgyQIP3bHTXbreGbK8AIRyMAVZDAIY= Received: by 10.68.28.33 with SMTP id y1mr2632437pbg.485.1305706985930; Wed, 18 May 2011 01:23:05 -0700 (PDT) Received: from localhost.localdomain ([219.224.169.130]) by mx.google.com with ESMTPS id q10sm912018pbk.39.2011.05.18.01.22.58 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 18 May 2011 01:23:05 -0700 (PDT) From: Asias He To: Pekka Enberg Cc: Cyrill Gorcunov , Ingo Molnar , Sasha Levin , Prasad Joshi , kvm@vger.kernel.org, Asias He Subject: [PATCH 06/14] kvm tools: Consolidate disk_image__{new, new_readonly} Date: Wed, 18 May 2011 16:19:07 +0800 Message-Id: <1305706755-2816-6-git-send-email-asias.hejun@gmail.com> X-Mailer: git-send-email 1.7.5.1 In-Reply-To: <1305706755-2816-1-git-send-email-asias.hejun@gmail.com> References: <1305706755-2816-1-git-send-email-asias.hejun@gmail.com> 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 (demeter2.kernel.org [140.211.167.43]); Wed, 18 May 2011 08:24:06 +0000 (UTC) This patch simplifies the disk image API. Signed-off-by: Asias He --- 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 --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);