xref: /kvmtool/disk/qcow.c (revision 742fce761c5c93d531d3e49ede45fa28df6e13a9)
186835cedSPrasad Joshi #include "kvm/qcow.h"
286835cedSPrasad Joshi 
386835cedSPrasad Joshi #include "kvm/disk-image.h"
486835cedSPrasad Joshi #include "kvm/read-write.h"
586835cedSPrasad Joshi #include "kvm/util.h"
686835cedSPrasad Joshi 
786835cedSPrasad Joshi #include <sys/types.h>
886835cedSPrasad Joshi #include <sys/stat.h>
986835cedSPrasad Joshi #include <stdbool.h>
1086835cedSPrasad Joshi #include <stdlib.h>
1186835cedSPrasad Joshi #include <string.h>
1286835cedSPrasad Joshi #include <unistd.h>
1386835cedSPrasad Joshi #include <fcntl.h>
1486835cedSPrasad Joshi 
1586835cedSPrasad Joshi #include <linux/byteorder.h>
1686835cedSPrasad Joshi #include <linux/types.h>
1786835cedSPrasad Joshi 
18*742fce76SPrasad Joshi static inline u64 get_l1_index(struct qcow *q, u64 offset)
1986835cedSPrasad Joshi {
2086835cedSPrasad Joshi 	struct qcow1_header *header = q->header;
2186835cedSPrasad Joshi 
2286835cedSPrasad Joshi 	return offset >> (header->l2_bits + header->cluster_bits);
2386835cedSPrasad Joshi }
2486835cedSPrasad Joshi 
25*742fce76SPrasad Joshi static inline u64 get_l2_index(struct qcow *q, u64 offset)
2686835cedSPrasad Joshi {
2786835cedSPrasad Joshi 	struct qcow1_header *header = q->header;
2886835cedSPrasad Joshi 
2986835cedSPrasad Joshi 	return (offset >> (header->cluster_bits)) & ((1 << header->l2_bits)-1);
3086835cedSPrasad Joshi }
3186835cedSPrasad Joshi 
32*742fce76SPrasad Joshi static inline u64 get_cluster_offset(struct qcow *q, u64 offset)
3386835cedSPrasad Joshi {
3486835cedSPrasad Joshi 	struct qcow1_header *header = q->header;
3586835cedSPrasad Joshi 
3686835cedSPrasad Joshi 	return offset & ((1 << header->cluster_bits)-1);
3786835cedSPrasad Joshi }
3886835cedSPrasad Joshi 
3986835cedSPrasad Joshi static int qcow1_read_sector(struct disk_image *self, uint64_t sector, void *dst, uint32_t dst_len)
4086835cedSPrasad Joshi {
4186835cedSPrasad Joshi 	struct qcow *q = self->priv;
4286835cedSPrasad Joshi 	struct qcow1_header *header = q->header;
43*742fce76SPrasad Joshi 	u64 l2_table_offset;
44*742fce76SPrasad Joshi 	u64 l2_table_size;
45*742fce76SPrasad Joshi 	u64 clust_offset;
46*742fce76SPrasad Joshi 	u64 clust_start;
47*742fce76SPrasad Joshi 	u64 *l2_table = NULL;
48*742fce76SPrasad Joshi 	u64 l1_idx;
49*742fce76SPrasad Joshi 	u64 l2_idx;
50*742fce76SPrasad Joshi 	u64 offset;
5186835cedSPrasad Joshi 
5286835cedSPrasad Joshi 	offset		= sector << SECTOR_SHIFT;
5386835cedSPrasad Joshi 	if (offset >= header->size)
5486835cedSPrasad Joshi 		goto out_error;
5586835cedSPrasad Joshi 
56c5e0624bSPrasad Joshi 	l1_idx		= get_l1_index(q, offset);
5786835cedSPrasad Joshi 
5886835cedSPrasad Joshi 	if (l1_idx >= q->table.table_size)
5986835cedSPrasad Joshi 		goto out_error;
6086835cedSPrasad Joshi 
6100adcc1bSPrasad Joshi 	l2_table_offset	= q->table.l1_table[l1_idx];
6286835cedSPrasad Joshi 	if (!l2_table_offset)
6386835cedSPrasad Joshi 		goto zero_sector;
6486835cedSPrasad Joshi 
6586835cedSPrasad Joshi 	l2_table_size	= 1 << header->l2_bits;
6686835cedSPrasad Joshi 
67*742fce76SPrasad Joshi 	l2_table	= calloc(l2_table_size, sizeof(u64));
6886835cedSPrasad Joshi 	if (!l2_table)
6986835cedSPrasad Joshi 		goto out_error;
7086835cedSPrasad Joshi 
71*742fce76SPrasad Joshi 	if (pread_in_full(q->fd, l2_table, sizeof(u64) * l2_table_size, l2_table_offset) < 0)
72b6edb0ecSSasha Levin 		goto out_error;
7386835cedSPrasad Joshi 
74c5e0624bSPrasad Joshi 	l2_idx		= get_l2_index(q, offset);
7586835cedSPrasad Joshi 
7686835cedSPrasad Joshi 	if (l2_idx >= l2_table_size)
77b6edb0ecSSasha Levin 		goto out_error;
7886835cedSPrasad Joshi 
7986835cedSPrasad Joshi 	clust_start	= be64_to_cpu(l2_table[l2_idx]);
8086835cedSPrasad Joshi 
81b6edb0ecSSasha Levin 	free(l2_table);
82b6edb0ecSSasha Levin 
8386835cedSPrasad Joshi 	if (!clust_start)
8486835cedSPrasad Joshi 		goto zero_sector;
8586835cedSPrasad Joshi 
86c5e0624bSPrasad Joshi 	clust_offset	= get_cluster_offset(q, offset);
8786835cedSPrasad Joshi 
8886835cedSPrasad Joshi 	if (pread_in_full(q->fd, dst, dst_len, clust_start + clust_offset) < 0)
89b6edb0ecSSasha Levin 		goto out_error;
9086835cedSPrasad Joshi 
9186835cedSPrasad Joshi 	return 0;
9286835cedSPrasad Joshi 
9386835cedSPrasad Joshi zero_sector:
9486835cedSPrasad Joshi 	memset(dst, 0, dst_len);
9586835cedSPrasad Joshi 
9686835cedSPrasad Joshi 	return 0;
9786835cedSPrasad Joshi 
9886835cedSPrasad Joshi out_error:
99b6edb0ecSSasha Levin 	free(l2_table);
100b6edb0ecSSasha Levin 
10186835cedSPrasad Joshi 	return -1;
10286835cedSPrasad Joshi }
10386835cedSPrasad Joshi 
10486835cedSPrasad Joshi static int qcow1_write_sector(struct disk_image *self, uint64_t sector, void *src, uint32_t src_len)
10586835cedSPrasad Joshi {
10686835cedSPrasad Joshi 	return -1;
10786835cedSPrasad Joshi }
10886835cedSPrasad Joshi 
10986835cedSPrasad Joshi static void qcow1_disk_close(struct disk_image *self)
11086835cedSPrasad Joshi {
11186835cedSPrasad Joshi 	struct qcow *q;
11286835cedSPrasad Joshi 
11386835cedSPrasad Joshi 	if (!self)
11486835cedSPrasad Joshi 		return;
11586835cedSPrasad Joshi 
11686835cedSPrasad Joshi 	q = self->priv;
11786835cedSPrasad Joshi 
1186c6f79b6SPrasad Joshi 	free(q->table.l1_table);
11986835cedSPrasad Joshi 	free(q->header);
12086835cedSPrasad Joshi 	free(q);
12186835cedSPrasad Joshi }
12286835cedSPrasad Joshi 
12386835cedSPrasad Joshi struct disk_image_operations qcow1_disk_ops = {
12486835cedSPrasad Joshi 	.read_sector		= qcow1_read_sector,
12586835cedSPrasad Joshi 	.write_sector		= qcow1_write_sector,
12686835cedSPrasad Joshi 	.close			= qcow1_disk_close
12786835cedSPrasad Joshi };
12886835cedSPrasad Joshi 
12986835cedSPrasad Joshi static int qcow_read_l1_table(struct qcow *q)
13086835cedSPrasad Joshi {
13186835cedSPrasad Joshi 	struct qcow1_header *header = q->header;
13200adcc1bSPrasad Joshi 	struct qcow_table *table = &q->table;
13300adcc1bSPrasad Joshi 	u64 i;
13486835cedSPrasad Joshi 
13500adcc1bSPrasad Joshi 	table->table_size = header->size / ((1 << header->l2_bits) *
13600adcc1bSPrasad Joshi 			(1 << header->cluster_bits));
13786835cedSPrasad Joshi 
13800adcc1bSPrasad Joshi 	table->l1_table	= calloc(table->table_size, sizeof(u64));
13900adcc1bSPrasad Joshi 	if (!table->l1_table)
14086835cedSPrasad Joshi 		return -1;
14186835cedSPrasad Joshi 
14200adcc1bSPrasad Joshi 	if (pread_in_full(q->fd, table->l1_table, sizeof(u64) *
14300adcc1bSPrasad Joshi 				table->table_size, header->l1_table_offset) < 0)
14486835cedSPrasad Joshi 		return -1;
14586835cedSPrasad Joshi 
14600adcc1bSPrasad Joshi 	for (i = 0; i < table->table_size; i++)
14700adcc1bSPrasad Joshi 		be64_to_cpus(&table->l1_table[i]);
14800adcc1bSPrasad Joshi 
14986835cedSPrasad Joshi 	return 0;
15086835cedSPrasad Joshi }
15186835cedSPrasad Joshi 
15286835cedSPrasad Joshi static void *qcow1_read_header(int fd)
15386835cedSPrasad Joshi {
15486835cedSPrasad Joshi 	struct qcow1_header *header;
15586835cedSPrasad Joshi 
15686835cedSPrasad Joshi 	header = malloc(sizeof(struct qcow1_header));
15786835cedSPrasad Joshi 	if (!header)
15886835cedSPrasad Joshi 		return NULL;
15986835cedSPrasad Joshi 
16086835cedSPrasad Joshi 	if (pread_in_full(fd, header, sizeof(struct qcow1_header), 0) < 0)
16186835cedSPrasad Joshi 		return NULL;
16286835cedSPrasad Joshi 
16386835cedSPrasad Joshi 	be32_to_cpus(&header->magic);
16486835cedSPrasad Joshi 	be32_to_cpus(&header->version);
16586835cedSPrasad Joshi 	be64_to_cpus(&header->backing_file_offset);
16686835cedSPrasad Joshi 	be32_to_cpus(&header->backing_file_size);
16786835cedSPrasad Joshi 	be32_to_cpus(&header->mtime);
16886835cedSPrasad Joshi 	be64_to_cpus(&header->size);
16986835cedSPrasad Joshi 	be32_to_cpus(&header->crypt_method);
17086835cedSPrasad Joshi 	be64_to_cpus(&header->l1_table_offset);
17186835cedSPrasad Joshi 
17286835cedSPrasad Joshi 	return header;
17386835cedSPrasad Joshi }
17486835cedSPrasad Joshi 
17586835cedSPrasad Joshi static struct disk_image *qcow1_probe(int fd)
17686835cedSPrasad Joshi {
17786835cedSPrasad Joshi 	struct qcow *q;
17886835cedSPrasad Joshi 	struct qcow1_header *h;
17986835cedSPrasad Joshi 	struct disk_image *disk_image;
18086835cedSPrasad Joshi 
18186835cedSPrasad Joshi 	q = calloc(1, sizeof(struct qcow));
18286835cedSPrasad Joshi 	if (!q)
18386835cedSPrasad Joshi 		goto error;
18486835cedSPrasad Joshi 
18586835cedSPrasad Joshi 	q->fd = fd;
18686835cedSPrasad Joshi 
18786835cedSPrasad Joshi 	h = q->header = qcow1_read_header(fd);
18886835cedSPrasad Joshi 	if (!h)
18986835cedSPrasad Joshi 		goto error;
19086835cedSPrasad Joshi 
19186835cedSPrasad Joshi 	if (qcow_read_l1_table(q) < 0)
19286835cedSPrasad Joshi 		goto error;
19386835cedSPrasad Joshi 
19486835cedSPrasad Joshi 	disk_image = disk_image__new(fd, h->size, &qcow1_disk_ops);
19586835cedSPrasad Joshi 	if (!disk_image)
19686835cedSPrasad Joshi 		goto error;
19786835cedSPrasad Joshi 	disk_image->priv = q;
19886835cedSPrasad Joshi 
19986835cedSPrasad Joshi 	return disk_image;
20086835cedSPrasad Joshi error:
20186835cedSPrasad Joshi 	if (!q)
20286835cedSPrasad Joshi 		return NULL;
20386835cedSPrasad Joshi 
2046c6f79b6SPrasad Joshi 	free(q->table.l1_table);
20586835cedSPrasad Joshi 	free(q->header);
20686835cedSPrasad Joshi 	free(q);
20786835cedSPrasad Joshi 
20886835cedSPrasad Joshi 	return NULL;
20986835cedSPrasad Joshi }
21086835cedSPrasad Joshi 
21186835cedSPrasad Joshi static int qcow_check_image(int fd)
21286835cedSPrasad Joshi {
21386835cedSPrasad Joshi 	struct qcow1_header header;
21486835cedSPrasad Joshi 
21586835cedSPrasad Joshi 	if (pread_in_full(fd, &header, sizeof(struct qcow1_header), 0) < 0)
21686835cedSPrasad Joshi 		return -1;
21786835cedSPrasad Joshi 
21886835cedSPrasad Joshi 	be32_to_cpus(&header.magic);
21986835cedSPrasad Joshi 	be32_to_cpus(&header.version);
22086835cedSPrasad Joshi 
22186835cedSPrasad Joshi 	if (header.magic != QCOW_MAGIC)
22286835cedSPrasad Joshi 		return -1;
22386835cedSPrasad Joshi 
22486835cedSPrasad Joshi 	if (header.version != QCOW1_VERSION)
22586835cedSPrasad Joshi 		return -1;
22686835cedSPrasad Joshi 
22786835cedSPrasad Joshi 	return 0;
22886835cedSPrasad Joshi }
22986835cedSPrasad Joshi 
23086835cedSPrasad Joshi struct disk_image *qcow_probe(int fd)
23186835cedSPrasad Joshi {
23286835cedSPrasad Joshi 	if (qcow_check_image(fd) < 0)
23386835cedSPrasad Joshi 		return NULL;
23486835cedSPrasad Joshi 
23586835cedSPrasad Joshi 	return qcow1_probe(fd);
23686835cedSPrasad Joshi }
237