Add linux-next specific files for 20110831
[linux-2.6/next.git] / tools / kvm / disk / raw.c
blob7f3f8db5014bd3bc05e0c09177469987b404f864
1 #include "kvm/disk-image.h"
3 ssize_t raw_image__read_sector_iov(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
5 u64 offset = sector << SECTOR_SHIFT;
7 return preadv_in_full(disk->fd, iov, iovcount, offset);
10 ssize_t raw_image__write_sector_iov(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
12 u64 offset = sector << SECTOR_SHIFT;
14 return pwritev_in_full(disk->fd, iov, iovcount, offset);
17 ssize_t raw_image__read_sector(struct disk_image *disk, u64 sector, void *dst, u32 dst_len)
19 u64 offset = sector << SECTOR_SHIFT;
21 if (offset + dst_len > disk->size)
22 return -1;
24 memcpy(dst, disk->priv + offset, dst_len);
26 return dst_len;
29 ssize_t raw_image__write_sector(struct disk_image *disk, u64 sector, void *src, u32 src_len)
31 u64 offset = sector << SECTOR_SHIFT;
33 if (offset + src_len > disk->size)
34 return -1;
36 memcpy(disk->priv + offset, src, src_len);
38 return src_len;
41 int raw_image__close(struct disk_image *disk)
43 int ret = 0;
45 if (disk->priv != MAP_FAILED)
46 ret = munmap(disk->priv, disk->size);
48 return ret;
52 * multiple buffer based disk image operations
54 static struct disk_image_operations raw_image_iov_ops = {
55 .read_sector_iov = raw_image__read_sector_iov,
56 .write_sector_iov = raw_image__write_sector_iov,
60 * single buffer based disk image operations
62 static struct disk_image_operations raw_image_ops = {
63 .read_sector = raw_image__read_sector,
64 .write_sector = raw_image__write_sector,
65 .close = raw_image__close,
68 struct disk_image *raw_image__probe(int fd, struct stat *st, bool readonly)
71 if (readonly)
73 * Use mmap's MAP_PRIVATE to implement non-persistent write
74 * FIXME: This does not work on 32-bit host.
76 return disk_image__new(fd, st->st_size, &raw_image_ops, DISK_IMAGE_MMAP);
77 else
79 * Use read/write instead of mmap
81 return disk_image__new(fd, st->st_size, &raw_image_iov_ops, DISK_IMAGE_NOMMAP);