Signed-off-by: Asias He <asias.hejun@xxxxxxxxx> --- 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 */ -- 1.7.5.1 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html