1 #include "kvm/disk-image.h"
6 struct disk_image
*disk_image__new(int fd
, u64 size
, struct disk_image_operations
*ops
, int use_mmap
)
8 struct disk_image
*disk
;
10 disk
= malloc(sizeof *disk
);
18 if (use_mmap
== DISK_IMAGE_MMAP
) {
20 * The write to disk image will be discarded
22 disk
->priv
= mmap(NULL
, size
, PROT_RW
, MAP_PRIVATE
| MAP_NORESERVE
, fd
, 0);
23 if (disk
->priv
== MAP_FAILED
)
30 struct disk_image
*disk_image__open(const char *filename
, bool readonly
)
32 struct disk_image
*disk
;
36 if (stat(filename
, &st
) < 0)
40 disk
= blkdev__probe(filename
, &st
);
44 fd
= open(filename
, readonly
? O_RDONLY
: O_RDWR
);
49 disk
= qcow_probe(fd
, true);
51 pr_warning("Forcing read-only support for QCOW");
56 disk
= raw_image__probe(fd
, &st
, readonly
);
61 pr_warning("close() failed");
66 struct disk_image
**disk_image__open_all(const char **filenames
, bool *readonly
, int count
)
68 struct disk_image
**disks
;
71 if (!count
|| count
> MAX_DISK_IMAGES
)
74 disks
= calloc(count
, sizeof(*disks
));
78 for (i
= 0; i
< count
; i
++) {
82 disks
[i
] = disk_image__open(filenames
[i
], readonly
[i
]);
84 pr_error("Loading disk image '%s' failed", filenames
[i
]);
90 for (i
= 0; i
< count
; i
++)
91 disk_image__close(disks
[i
]);
97 int disk_image__flush(struct disk_image
*disk
)
100 return disk
->ops
->flush(disk
);
102 return fsync(disk
->fd
);
105 int disk_image__close(struct disk_image
*disk
)
107 /* If there was no disk image then there's nothing to do: */
111 if (disk
->ops
->close
)
112 return disk
->ops
->close(disk
);
114 if (close(disk
->fd
) < 0)
115 pr_warning("close() failed");
122 void disk_image__close_all(struct disk_image
**disks
, int count
)
125 disk_image__close(disks
[--count
]);
131 * Fill iov with disk data, starting from sector 'sector'.
132 * Return amount of bytes read.
134 ssize_t
disk_image__read(struct disk_image
*disk
, u64 sector
, const struct iovec
*iov
, int iovcount
)
140 msleep(debug_iodelay
);
142 if (disk
->ops
->read_sector_iov
) {
144 * Try mulitple buffer based operation first
146 total
= disk
->ops
->read_sector_iov(disk
, sector
, iov
, iovcount
);
148 pr_info("disk_image__read error: total=%ld\n", (long)total
);
151 } else if (disk
->ops
->read_sector
) {
153 * Fallback to single buffer based operation
156 nr
= disk
->ops
->read_sector(disk
, sector
, iov
->iov_base
, iov
->iov_len
);
157 if (nr
!= (ssize_t
)iov
->iov_len
) {
158 pr_info("disk_image__read error: nr = %ld iov_len=%ld\n", (long)nr
, (long)iov
->iov_len
);
161 sector
+= iov
->iov_len
>> SECTOR_SHIFT
;
166 die("No disk image operation for read\n");
172 * Write iov to disk, starting from sector 'sector'.
173 * Return amount of bytes written.
175 ssize_t
disk_image__write(struct disk_image
*disk
, u64 sector
, const struct iovec
*iov
, int iovcount
)
181 msleep(debug_iodelay
);
183 if (disk
->ops
->write_sector_iov
) {
185 * Try writev based operation first
187 total
= disk
->ops
->write_sector_iov(disk
, sector
, iov
, iovcount
);
189 pr_info("disk_image__write error: total=%ld\n", (long)total
);
192 } else if (disk
->ops
->write_sector
) {
194 * Fallback to single buffer based operation
197 nr
= disk
->ops
->write_sector(disk
, sector
, iov
->iov_base
, iov
->iov_len
);
198 if (nr
!= (ssize_t
)iov
->iov_len
) {
199 pr_info("disk_image__write error: nr=%ld iov_len=%ld\n", (long)nr
, (long)iov
->iov_len
);
203 sector
+= iov
->iov_len
>> SECTOR_SHIFT
;
208 die("No disk image operation for read\n");
213 ssize_t
disk_image__get_serial(struct disk_image
*disk
, void *buffer
, ssize_t
*len
)
217 if (fstat(disk
->fd
, &st
) != 0)
220 *len
= snprintf(buffer
, *len
, "%llu%llu%llu", (u64
)st
.st_dev
, (u64
)st
.st_rdev
, (u64
)st
.st_ino
);