From patchwork Wed May 18 08:19:08 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asias He X-Patchwork-Id: 793072 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 p4I8O3wm004358 for ; Wed, 18 May 2011 08:24:06 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755582Ab1ERIXO (ORCPT ); Wed, 18 May 2011 04:23:14 -0400 Received: from mail-pz0-f46.google.com ([209.85.210.46]:37524 "EHLO mail-pz0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755576Ab1ERIXN (ORCPT ); Wed, 18 May 2011 04:23:13 -0400 Received: by mail-pz0-f46.google.com with SMTP id 9so601787pzk.19 for ; Wed, 18 May 2011 01:23:12 -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=m2nYbLIzMfhDDXCvwjsU0LoovT2+wi+wPuv5S8qt7sw=; b=HGGZ8c2Bwjeu6MVx/GS0u6cq6Vy9mEvEQIaLZatWHQenLXy+f51Mb47fi9qBbFmrgw S+nvlx5oyTnbpJ9Fc+wSFNKZKPR4VkhL7nrIuHhvrsL6bvf/Jx2ZiNESOsjJryaI6vKN QG23HrAH3DxVihw0HBFd0hyKxNKFw2m8q2VPk= 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=RkGerLWWjjQhzy0M7EzRPAyodBreawwFYUCPdtWJDX7q226q/o/ldZAhfHd6nml9Kb A9kXzXELZNXPjChJekC7X855caMmkwuG0EIa9CGKwZyS6EJEaR+t4fhLYGVlpmP/x6pj fYyTrXvi5WslBH4hOBw3W7CW6WSnj5hhUUkKE= Received: by 10.68.47.2 with SMTP id z2mr2528897pbm.327.1305706992845; Wed, 18 May 2011 01:23:12 -0700 (PDT) Received: from localhost.localdomain ([219.224.169.130]) by mx.google.com with ESMTPS id q10sm912018pbk.39.2011.05.18.01.23.07 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 18 May 2011 01:23:12 -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 07/14] kvm tools: Split blk device code from raw.c to blk.c Date: Wed, 18 May 2011 16:19:08 +0800 Message-Id: <1305706755-2816-7-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) Signed-off-by: Asias He --- tools/kvm/Makefile | 1 + tools/kvm/disk/blk.c | 30 ++++++++++++++++++++++++++++++ tools/kvm/disk/raw.c | 32 ++++++-------------------------- tools/kvm/include/kvm/disk-image.h | 6 ++++++ 4 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 tools/kvm/disk/blk.c diff --git a/tools/kvm/Makefile b/tools/kvm/Makefile index a19fbe8..ffafcba 100644 --- a/tools/kvm/Makefile +++ b/tools/kvm/Makefile @@ -39,6 +39,7 @@ OBJS += kvm-run.o OBJS += disk/qcow.o OBJS += disk/core.o OBJS += disk/raw.o +OBJS += disk/blk.o OBJS += mptable.o OBJS += threadpool.o OBJS += irq.o diff --git a/tools/kvm/disk/blk.c b/tools/kvm/disk/blk.c new file mode 100644 index 0000000..ece4d2a --- /dev/null +++ b/tools/kvm/disk/blk.c @@ -0,0 +1,30 @@ +#include "kvm/disk-image.h" + +/* + * raw image and blk dev are similar, so reuse raw image ops. + */ +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 *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(fd, size, &raw_image_ro_mmap_ops, DISK_IMAGE_MMAP); +} diff --git a/tools/kvm/disk/raw.c b/tools/kvm/disk/raw.c index a419c32..8ee7e3f 100644 --- a/tools/kvm/disk/raw.c +++ b/tools/kvm/disk/raw.c @@ -1,20 +1,20 @@ #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) +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) +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) +int raw_image__read_sector_ro_mmap(struct disk_image *disk, u64 sector, void *dst, u32 dst_len) { u64 offset = sector << SECTOR_SHIFT; @@ -26,7 +26,7 @@ static int raw_image__read_sector_ro_mmap(struct disk_image *disk, u64 sector, v return 0; } -static int raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, void *src, u32 src_len) +int raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, void *src, u32 src_len) { u64 offset = sector << SECTOR_SHIFT; @@ -38,7 +38,7 @@ static int raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, return 0; } -static void raw_image__close_ro_mmap(struct disk_image *disk) +void raw_image__close_ro_mmap(struct disk_image *disk) { if (disk->priv != MAP_FAILED) munmap(disk->priv, disk->size); @@ -46,7 +46,7 @@ static void raw_image__close_ro_mmap(struct disk_image *disk) static struct disk_image_operations raw_image_ops = { .read_sector_iov = raw_image__read_sector_iov, - .write_sector_iov = raw_image__write_sector_iov + .write_sector_iov = raw_image__write_sector_iov, }; static struct disk_image_operations raw_image_ro_mmap_ops = { @@ -70,23 +70,3 @@ struct disk_image *raw_image__probe(int fd, struct stat *st, bool readonly) */ 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) -{ - 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(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 efa3e42..463801e 100644 --- a/tools/kvm/include/kvm/disk-image.h +++ b/tools/kvm/include/kvm/disk-image.h @@ -59,4 +59,10 @@ static inline int disk_image__flush(struct disk_image *disk) struct disk_image *raw_image__probe(int fd, struct stat *st, bool readonly); struct disk_image *blkdev__probe(const char *filename, struct stat *st); +ssize_t raw_image__read_sector_iov(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount); +ssize_t raw_image__write_sector_iov(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount); +int raw_image__read_sector_ro_mmap(struct disk_image *disk, u64 sector, void *dst, u32 dst_len); +int raw_image__write_sector_ro_mmap(struct disk_image *disk, u64 sector, void *src, u32 src_len); +void raw_image__close_ro_mmap(struct disk_image *disk); + #endif /* KVM__DISK_IMAGE_H */