From patchwork Wed May 18 08:19:04 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asias He X-Patchwork-Id: 793022 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 p4I8O3wi004358 for ; Wed, 18 May 2011 08:24:05 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755396Ab1ERIV0 (ORCPT ); Wed, 18 May 2011 04:21:26 -0400 Received: from mail-px0-f173.google.com ([209.85.212.173]:32917 "EHLO mail-px0-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752031Ab1ERIVZ (ORCPT ); Wed, 18 May 2011 04:21:25 -0400 Received: by pxi16 with SMTP id 16so911153pxi.4 for ; Wed, 18 May 2011 01:21:24 -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=45hCP3K9NAMaFGwTaJAQZH6Z/72lWDt3qk8Ns64eq9o=; b=wA248P1N319iwphI5iC+t/yhv8I99H8lpEtCZEkX24v8do6sKl29Os4yYmrr1B6jXn l+2lPHOhXGW3rT5eoaTLV1ROu5ZYCsoC9XaRuwroCct5BrZjzyvsKIgC2sMF7iuH0PFj 9B8WhP0ogMyP93am8M2WV+py63Jsf/H0Awivs= 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=gJJXtQiEpw6CQdCsifHcu50gx4c4qNW237Cr2kDCaZ+rdtaDp1Q1BuhzSBPvpRvrAB C7u3zsyZipBw47Bq1lYTrpsZCy2Yd9PWWCh3WXFYENlx0ScP51+CdmFYVDpyLOuLCCLC NhLjxDyMbsWr3O+0gEM+M4b6b4tpvfMEfZtgc= Received: by 10.68.69.102 with SMTP id d6mr2557103pbu.138.1305706884796; Wed, 18 May 2011 01:21:24 -0700 (PDT) Received: from localhost.localdomain ([219.224.169.130]) by mx.google.com with ESMTPS id q10sm912018pbk.39.2011.05.18.01.21.18 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 18 May 2011 01:21:24 -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 03/14] kvm tools: Split raw image and blk device code from disk/core.c Date: Wed, 18 May 2011 16:19:04 +0800 Message-Id: <1305706755-2816-3-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:05 +0000 (UTC) This patch moves raw image and blk device code into disk/raw.c Signed-off-by: Asias He --- tools/kvm/Makefile | 3 +- tools/kvm/disk/core.c | 101 +----------------------------------- tools/kvm/disk/raw.c | 84 ++++++++++++++++++++++++++++++ tools/kvm/include/kvm/disk-image.h | 15 +++++ 4 files changed, 102 insertions(+), 101 deletions(-) create mode 100644 tools/kvm/disk/raw.c diff --git a/tools/kvm/Makefile b/tools/kvm/Makefile index d7b1233..a19fbe8 100644 --- a/tools/kvm/Makefile +++ b/tools/kvm/Makefile @@ -15,7 +15,6 @@ TAGS = ctags OBJS += 8250-serial.o OBJS += cpuid.o OBJS += read-write.o -OBJS += disk/core.o OBJS += interrupt.o OBJS += ioport.o OBJS += kvm.o @@ -38,6 +37,8 @@ OBJS += kvm-help.o OBJS += kvm-cmd.o OBJS += kvm-run.o OBJS += disk/qcow.o +OBJS += disk/core.o +OBJS += disk/raw.o OBJS += mptable.o OBJS += threadpool.o OBJS += irq.o diff --git a/tools/kvm/disk/core.c b/tools/kvm/disk/core.c index 4172b05..4622e99 100644 --- a/tools/kvm/disk/core.c +++ b/tools/kvm/disk/core.c @@ -1,21 +1,5 @@ #include "kvm/disk-image.h" - -#include "kvm/read-write.h" #include "kvm/qcow.h" -#include "kvm/util.h" - -#include /* for BLKGETSIZE64 */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operations *ops) { @@ -45,89 +29,6 @@ struct disk_image *disk_image__new_readonly(int fd, u64 size, struct disk_image_ return disk; } -static ssize_t raw_image__read_sector_iov(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount) -{ - u64 offset = sector << SECTOR_SHIFT; - - return preadv_in_full(disk->fd, iov, iovcount, offset); -} - -static ssize_t raw_image__write_sector_iov(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount) -{ - u64 offset = sector << SECTOR_SHIFT; - - return pwritev_in_full(disk->fd, iov, iovcount, offset); -} - -static int raw_image__read_sector_ro_mmap(struct disk_image *disk, u64 sector, void *dst, u32 dst_len) -{ - u64 offset = sector << SECTOR_SHIFT; - - if (offset + dst_len > disk->size) - return -1; - - memcpy(dst, disk->priv + offset, dst_len); - - return 0; -} - -static int raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, void *src, u32 src_len) -{ - u64 offset = sector << SECTOR_SHIFT; - - if (offset + src_len > disk->size) - return -1; - - memcpy(disk->priv + offset, src, src_len); - - return 0; -} - -static void raw_image__close_ro_mmap(struct disk_image *disk) -{ - if (disk->priv != MAP_FAILED) - munmap(disk->priv, disk->size); -} - -static struct disk_image_operations raw_image_ops = { - .read_sector_iov = raw_image__read_sector_iov, - .write_sector_iov = raw_image__write_sector_iov -}; - -static struct disk_image_operations raw_image_ro_mmap_ops = { - .read_sector = raw_image__read_sector_ro_mmap, - .write_sector = raw_image__write_sector_ro_mmap, - .close = raw_image__close_ro_mmap, -}; - -static 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); - else - return disk_image__new(fd, st->st_size, &raw_image_ops); -} - -static struct disk_image *blkdev__probe(const char *filename, struct stat *st) -{ - u64 size; - int fd; - - if (!S_ISBLK(st->st_mode)) - return NULL; - - fd = open(filename, O_RDONLY); - if (fd < 0) - return NULL; - - if (ioctl(fd, BLKGETSIZE64, &size) < 0) { - close(fd); - return NULL; - } - - return disk_image__new_readonly(fd, size, &raw_image_ro_mmap_ops); -} - struct disk_image *disk_image__open(const char *filename, bool readonly) { struct disk_image *disk; @@ -209,4 +110,4 @@ ssize_t disk_image__write_sector_iov(struct disk_image *disk, u64 sector, const } return (sector - first_sector) << SECTOR_SHIFT; -} \ No newline at end of file +} diff --git a/tools/kvm/disk/raw.c b/tools/kvm/disk/raw.c new file mode 100644 index 0000000..b1a484d --- /dev/null +++ b/tools/kvm/disk/raw.c @@ -0,0 +1,84 @@ +#include "kvm/disk-image.h" + +static ssize_t raw_image__read_sector_iov(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount) +{ + u64 offset = sector << SECTOR_SHIFT; + + return preadv_in_full(disk->fd, iov, iovcount, offset); +} + +static ssize_t raw_image__write_sector_iov(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount) +{ + u64 offset = sector << SECTOR_SHIFT; + + return pwritev_in_full(disk->fd, iov, iovcount, offset); +} + +static int raw_image__read_sector_ro_mmap(struct disk_image *disk, u64 sector, void *dst, u32 dst_len) +{ + u64 offset = sector << SECTOR_SHIFT; + + if (offset + dst_len > disk->size) + return -1; + + memcpy(dst, disk->priv + offset, dst_len); + + return 0; +} + +static int raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, void *src, u32 src_len) +{ + u64 offset = sector << SECTOR_SHIFT; + + if (offset + src_len > disk->size) + return -1; + + memcpy(disk->priv + offset, src, src_len); + + return 0; +} + +static void raw_image__close_ro_mmap(struct disk_image *disk) +{ + if (disk->priv != MAP_FAILED) + munmap(disk->priv, disk->size); +} + +static struct disk_image_operations raw_image_ops = { + .read_sector_iov = raw_image__read_sector_iov, + .write_sector_iov = raw_image__write_sector_iov +}; + +static struct disk_image_operations raw_image_ro_mmap_ops = { + .read_sector = raw_image__read_sector_ro_mmap, + .write_sector = raw_image__write_sector_ro_mmap, + .close = raw_image__close_ro_mmap, +}; + +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); + else + return disk_image__new(fd, st->st_size, &raw_image_ops); +} + +struct disk_image *blkdev__probe(const char *filename, struct stat *st) +{ + u64 size; + int fd; + + if (!S_ISBLK(st->st_mode)) + return NULL; + + fd = open(filename, O_RDONLY); + if (fd < 0) + return NULL; + + if (ioctl(fd, BLKGETSIZE64, &size) < 0) { + close(fd); + return NULL; + } + + return disk_image__new_readonly(fd, size, &raw_image_ro_mmap_ops); +} diff --git a/tools/kvm/include/kvm/disk-image.h b/tools/kvm/include/kvm/disk-image.h index cc83f38..87bebd3 100644 --- a/tools/kvm/include/kvm/disk-image.h +++ b/tools/kvm/include/kvm/disk-image.h @@ -1,10 +1,22 @@ #ifndef KVM__DISK_IMAGE_H #define KVM__DISK_IMAGE_H +#include "kvm/read-write.h" +#include "kvm/util.h" + #include +#include /* for BLKGETSIZE64 */ +#include +#include +#include +#include #include #include +#include +#include +#include #include +#include #define SECTOR_SHIFT 9 #define SECTOR_SIZE (1UL << SECTOR_SHIFT) @@ -52,4 +64,7 @@ static inline int disk_image__flush(struct disk_image *disk) return fsync(disk->fd); } +struct disk_image *raw_image__probe(int fd, struct stat *st, bool readonly); +struct disk_image *blkdev__probe(const char *filename, struct stat *st); + #endif /* KVM__DISK_IMAGE_H */