xref: /kvmtool/disk/qcow.c (revision a51948cef188d211fa1e7e0570e8abb265bb0072)
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 
18742fce76SPrasad 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 
25742fce76SPrasad 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 
32742fce76SPrasad 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 
39*a51948ceSPekka Enberg static ssize_t qcow1_read_cluster(struct qcow *q, u64 offset, void *dst, u32 dst_len)
4086835cedSPrasad Joshi {
4186835cedSPrasad Joshi 	struct qcow1_header *header = q->header;
423dac48d4SPrasad Joshi 	struct qcow_table *table  = &q->table;
43179b71f0SPekka Enberg 	u64 *l2_table = NULL;
44742fce76SPrasad Joshi 	u64 l2_table_offset;
45742fce76SPrasad Joshi 	u64 l2_table_size;
463dac48d4SPrasad Joshi 	u64 cluster_size;
47742fce76SPrasad Joshi 	u64 clust_offset;
48742fce76SPrasad Joshi 	u64 clust_start;
49*a51948ceSPekka Enberg 	size_t length;
50742fce76SPrasad Joshi 	u64 l1_idx;
51742fce76SPrasad Joshi 	u64 l2_idx;
5286835cedSPrasad Joshi 
53dae803fbSPekka Enberg 	cluster_size = 1 << header->cluster_bits;
5486835cedSPrasad Joshi 
55c5e0624bSPrasad Joshi 	l1_idx = get_l1_index(q, offset);
563dac48d4SPrasad Joshi 	if (l1_idx >= table->table_size)
5786835cedSPrasad Joshi 		goto out_error;
5886835cedSPrasad Joshi 
593dac48d4SPrasad Joshi 	clust_offset = get_cluster_offset(q, offset);
603dac48d4SPrasad Joshi 	if (clust_offset >= cluster_size)
613dac48d4SPrasad Joshi 		goto out_error;
623dac48d4SPrasad Joshi 
633dac48d4SPrasad Joshi 	length = cluster_size - clust_offset;
643dac48d4SPrasad Joshi 	if (length > dst_len)
653dac48d4SPrasad Joshi 		length = dst_len;
663dac48d4SPrasad Joshi 
673dac48d4SPrasad Joshi 	l2_table_offset = table->l1_table[l1_idx];
6886835cedSPrasad Joshi 	if (!l2_table_offset)
693dac48d4SPrasad Joshi 		goto zero_cluster;
7086835cedSPrasad Joshi 
7186835cedSPrasad Joshi 	l2_table_size = 1 << header->l2_bits;
72742fce76SPrasad Joshi 	l2_table = calloc(l2_table_size, sizeof(u64));
7386835cedSPrasad Joshi 	if (!l2_table)
7486835cedSPrasad Joshi 		goto out_error;
7586835cedSPrasad Joshi 
763dac48d4SPrasad Joshi 	if (pread_in_full(q->fd, l2_table, l2_table_size * sizeof(u64),
773dac48d4SPrasad Joshi 				l2_table_offset) < 0)
78b6edb0ecSSasha Levin 		goto out_error;
7986835cedSPrasad Joshi 
80c5e0624bSPrasad Joshi 	l2_idx = get_l2_index(q, offset);
8186835cedSPrasad Joshi 	if (l2_idx >= l2_table_size)
82b6edb0ecSSasha Levin 		goto out_error;
8386835cedSPrasad Joshi 
8486835cedSPrasad Joshi 	clust_start = be64_to_cpu(l2_table[l2_idx]);
8586835cedSPrasad Joshi 	if (!clust_start)
863dac48d4SPrasad Joshi 		goto zero_cluster;
8786835cedSPrasad Joshi 
883dac48d4SPrasad Joshi 	if (pread_in_full(q->fd, dst, length, clust_start + clust_offset) < 0)
89b6edb0ecSSasha Levin 		goto out_error;
9086835cedSPrasad Joshi 
91179b71f0SPekka Enberg out:
92179b71f0SPekka Enberg 	free(l2_table);
933dac48d4SPrasad Joshi 	return length;
9486835cedSPrasad Joshi 
95179b71f0SPekka Enberg zero_cluster:
96179b71f0SPekka Enberg 	memset(dst, 0, length);
97179b71f0SPekka Enberg 	goto out;
98179b71f0SPekka Enberg 
9986835cedSPrasad Joshi out_error:
100179b71f0SPekka Enberg 	length = -1;
101179b71f0SPekka Enberg 	goto out;
1023dac48d4SPrasad Joshi }
103b6edb0ecSSasha Levin 
1043dac48d4SPrasad Joshi static int qcow1_read_sector(struct disk_image *self, uint64_t sector,
1053dac48d4SPrasad Joshi 		void *dst, uint32_t dst_len)
1063dac48d4SPrasad Joshi {
1073dac48d4SPrasad Joshi 	struct qcow *q = self->priv;
1083dac48d4SPrasad Joshi 	struct qcow1_header *header = q->header;
1093dac48d4SPrasad Joshi 	char *buf = dst;
1103dac48d4SPrasad Joshi 	u64 offset;
111d8eea993SPekka Enberg 	u32 nr_read;
1123dac48d4SPrasad Joshi 	u32 nr;
1133dac48d4SPrasad Joshi 
114d8eea993SPekka Enberg 	nr_read = 0;
115d8eea993SPekka Enberg 	while (nr_read < dst_len) {
1163dac48d4SPrasad Joshi 		offset = sector << SECTOR_SHIFT;
1173dac48d4SPrasad Joshi 		if (offset >= header->size)
1183dac48d4SPrasad Joshi 			goto out_error;
1193dac48d4SPrasad Joshi 
120d8eea993SPekka Enberg 		nr = qcow1_read_cluster(q, offset, buf, dst_len - nr_read);
121*a51948ceSPekka Enberg 		if (nr <= 0)
1223dac48d4SPrasad Joshi 			goto out_error;
1233dac48d4SPrasad Joshi 
124d8eea993SPekka Enberg 		nr_read		+= nr;
1253dac48d4SPrasad Joshi 		buf		+= nr;
1263dac48d4SPrasad Joshi 		sector		+= (nr >> SECTOR_SHIFT);
1273dac48d4SPrasad Joshi 	}
1283dac48d4SPrasad Joshi 	return 0;
1293dac48d4SPrasad Joshi out_error:
13086835cedSPrasad Joshi 	return -1;
13186835cedSPrasad Joshi }
13286835cedSPrasad Joshi 
13386835cedSPrasad Joshi static int qcow1_write_sector(struct disk_image *self, uint64_t sector, void *src, uint32_t src_len)
13486835cedSPrasad Joshi {
13586835cedSPrasad Joshi 	return -1;
13686835cedSPrasad Joshi }
13786835cedSPrasad Joshi 
13886835cedSPrasad Joshi static void qcow1_disk_close(struct disk_image *self)
13986835cedSPrasad Joshi {
14086835cedSPrasad Joshi 	struct qcow *q;
14186835cedSPrasad Joshi 
14286835cedSPrasad Joshi 	if (!self)
14386835cedSPrasad Joshi 		return;
14486835cedSPrasad Joshi 
14586835cedSPrasad Joshi 	q = self->priv;
14686835cedSPrasad Joshi 
1476c6f79b6SPrasad Joshi 	free(q->table.l1_table);
14886835cedSPrasad Joshi 	free(q->header);
14986835cedSPrasad Joshi 	free(q);
15086835cedSPrasad Joshi }
15186835cedSPrasad Joshi 
15286835cedSPrasad Joshi struct disk_image_operations qcow1_disk_ops = {
15386835cedSPrasad Joshi 	.read_sector		= qcow1_read_sector,
15486835cedSPrasad Joshi 	.write_sector		= qcow1_write_sector,
15586835cedSPrasad Joshi 	.close			= qcow1_disk_close
15686835cedSPrasad Joshi };
15786835cedSPrasad Joshi 
15886835cedSPrasad Joshi static int qcow_read_l1_table(struct qcow *q)
15986835cedSPrasad Joshi {
16086835cedSPrasad Joshi 	struct qcow1_header *header = q->header;
16100adcc1bSPrasad Joshi 	struct qcow_table *table = &q->table;
16200adcc1bSPrasad Joshi 	u64 i;
16386835cedSPrasad Joshi 
16400adcc1bSPrasad Joshi 	table->table_size = header->size / ((1 << header->l2_bits) *
16500adcc1bSPrasad Joshi 			(1 << header->cluster_bits));
16686835cedSPrasad Joshi 
16700adcc1bSPrasad Joshi 	table->l1_table	= calloc(table->table_size, sizeof(u64));
16800adcc1bSPrasad Joshi 	if (!table->l1_table)
16986835cedSPrasad Joshi 		return -1;
17086835cedSPrasad Joshi 
17100adcc1bSPrasad Joshi 	if (pread_in_full(q->fd, table->l1_table, sizeof(u64) *
17200adcc1bSPrasad Joshi 				table->table_size, header->l1_table_offset) < 0)
17386835cedSPrasad Joshi 		return -1;
17486835cedSPrasad Joshi 
17500adcc1bSPrasad Joshi 	for (i = 0; i < table->table_size; i++)
17600adcc1bSPrasad Joshi 		be64_to_cpus(&table->l1_table[i]);
17700adcc1bSPrasad Joshi 
17886835cedSPrasad Joshi 	return 0;
17986835cedSPrasad Joshi }
18086835cedSPrasad Joshi 
18186835cedSPrasad Joshi static void *qcow1_read_header(int fd)
18286835cedSPrasad Joshi {
18386835cedSPrasad Joshi 	struct qcow1_header *header;
18486835cedSPrasad Joshi 
18586835cedSPrasad Joshi 	header = malloc(sizeof(struct qcow1_header));
18686835cedSPrasad Joshi 	if (!header)
18786835cedSPrasad Joshi 		return NULL;
18886835cedSPrasad Joshi 
18986835cedSPrasad Joshi 	if (pread_in_full(fd, header, sizeof(struct qcow1_header), 0) < 0)
19086835cedSPrasad Joshi 		return NULL;
19186835cedSPrasad Joshi 
19286835cedSPrasad Joshi 	be32_to_cpus(&header->magic);
19386835cedSPrasad Joshi 	be32_to_cpus(&header->version);
19486835cedSPrasad Joshi 	be64_to_cpus(&header->backing_file_offset);
19586835cedSPrasad Joshi 	be32_to_cpus(&header->backing_file_size);
19686835cedSPrasad Joshi 	be32_to_cpus(&header->mtime);
19786835cedSPrasad Joshi 	be64_to_cpus(&header->size);
19886835cedSPrasad Joshi 	be32_to_cpus(&header->crypt_method);
19986835cedSPrasad Joshi 	be64_to_cpus(&header->l1_table_offset);
20086835cedSPrasad Joshi 
20186835cedSPrasad Joshi 	return header;
20286835cedSPrasad Joshi }
20386835cedSPrasad Joshi 
20486835cedSPrasad Joshi static struct disk_image *qcow1_probe(int fd)
20586835cedSPrasad Joshi {
20686835cedSPrasad Joshi 	struct qcow *q;
20786835cedSPrasad Joshi 	struct qcow1_header *h;
20886835cedSPrasad Joshi 	struct disk_image *disk_image;
20986835cedSPrasad Joshi 
21086835cedSPrasad Joshi 	q = calloc(1, sizeof(struct qcow));
21186835cedSPrasad Joshi 	if (!q)
21286835cedSPrasad Joshi 		goto error;
21386835cedSPrasad Joshi 
21486835cedSPrasad Joshi 	q->fd = fd;
21586835cedSPrasad Joshi 
21686835cedSPrasad Joshi 	h = q->header = qcow1_read_header(fd);
21786835cedSPrasad Joshi 	if (!h)
21886835cedSPrasad Joshi 		goto error;
21986835cedSPrasad Joshi 
22086835cedSPrasad Joshi 	if (qcow_read_l1_table(q) < 0)
22186835cedSPrasad Joshi 		goto error;
22286835cedSPrasad Joshi 
22386835cedSPrasad Joshi 	disk_image = disk_image__new(fd, h->size, &qcow1_disk_ops);
22486835cedSPrasad Joshi 	if (!disk_image)
22586835cedSPrasad Joshi 		goto error;
22686835cedSPrasad Joshi 	disk_image->priv = q;
22786835cedSPrasad Joshi 
22886835cedSPrasad Joshi 	return disk_image;
22986835cedSPrasad Joshi error:
23086835cedSPrasad Joshi 	if (!q)
23186835cedSPrasad Joshi 		return NULL;
23286835cedSPrasad Joshi 
2336c6f79b6SPrasad Joshi 	free(q->table.l1_table);
23486835cedSPrasad Joshi 	free(q->header);
23586835cedSPrasad Joshi 	free(q);
23686835cedSPrasad Joshi 
23786835cedSPrasad Joshi 	return NULL;
23886835cedSPrasad Joshi }
23986835cedSPrasad Joshi 
24086835cedSPrasad Joshi static int qcow_check_image(int fd)
24186835cedSPrasad Joshi {
24286835cedSPrasad Joshi 	struct qcow1_header header;
24386835cedSPrasad Joshi 
24486835cedSPrasad Joshi 	if (pread_in_full(fd, &header, sizeof(struct qcow1_header), 0) < 0)
24586835cedSPrasad Joshi 		return -1;
24686835cedSPrasad Joshi 
24786835cedSPrasad Joshi 	be32_to_cpus(&header.magic);
24886835cedSPrasad Joshi 	be32_to_cpus(&header.version);
24986835cedSPrasad Joshi 
25086835cedSPrasad Joshi 	if (header.magic != QCOW_MAGIC)
25186835cedSPrasad Joshi 		return -1;
25286835cedSPrasad Joshi 
25386835cedSPrasad Joshi 	if (header.version != QCOW1_VERSION)
25486835cedSPrasad Joshi 		return -1;
25586835cedSPrasad Joshi 
25686835cedSPrasad Joshi 	return 0;
25786835cedSPrasad Joshi }
25886835cedSPrasad Joshi 
25986835cedSPrasad Joshi struct disk_image *qcow_probe(int fd)
26086835cedSPrasad Joshi {
26186835cedSPrasad Joshi 	if (qcow_check_image(fd) < 0)
26286835cedSPrasad Joshi 		return NULL;
26386835cedSPrasad Joshi 
26486835cedSPrasad Joshi 	return qcow1_probe(fd);
26586835cedSPrasad Joshi }
266