This patch moves raw image and blk device code into disk/raw.c Signed-off-by: Asias He <asias.hejun@xxxxxxxxx> --- 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 <linux/fs.h> /* for BLKGETSIZE64 */ - -#include <sys/ioctl.h> -#include <sys/types.h> -#include <linux/types.h> -#include <sys/mman.h> -#include <sys/stat.h> -#include <stdbool.h> -#include <stddef.h> -#include <stdlib.h> -#include <unistd.h> -#include <fcntl.h> 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 <linux/types.h> +#include <linux/fs.h> /* for BLKGETSIZE64 */ +#include <sys/ioctl.h> +#include <sys/types.h> +#include <sys/mman.h> +#include <sys/stat.h> #include <stdbool.h> #include <sys/uio.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdlib.h> #include <unistd.h> +#include <fcntl.h> #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 */ -- 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