xref: /kvmtool/include/kvm/qcow.h (revision d3476f7d3bb7cee0b620cf207c168cb4f5b5c41e)
1 #ifndef KVM__QCOW_H
2 #define KVM__QCOW_H
3 
4 #include "kvm/mutex.h"
5 
6 #include <linux/types.h>
7 #include <stdbool.h>
8 #include <linux/rbtree.h>
9 #include <linux/list.h>
10 
11 #define QCOW_MAGIC		(('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
12 
13 #define QCOW1_VERSION		1
14 #define QCOW2_VERSION		2
15 
16 #define QCOW1_OFLAG_COMPRESSED	(1ULL << 63)
17 
18 #define QCOW2_OFLAG_COPIED	(1ULL << 63)
19 #define QCOW2_OFLAG_COMPRESSED	(1ULL << 62)
20 
21 #define QCOW2_OFLAGS_MASK	(QCOW2_OFLAG_COPIED|QCOW2_OFLAG_COMPRESSED)
22 
23 #define QCOW2_OFFSET_MASK	(~QCOW2_OFLAGS_MASK)
24 
25 #define MAX_CACHE_NODES         32
26 
27 struct qcow_l2_table {
28 	u64				offset;
29 	struct rb_node			node;
30 	struct list_head		list;
31 	u8				dirty;
32 	u64				table[];
33 };
34 
35 struct qcow_l1_table {
36 	u32				table_size;
37 	u64				*l1_table;
38 
39 	/* Level2 caching data structures */
40 	struct rb_root			root;
41 	struct list_head		lru_list;
42 	int				nr_cached;
43 };
44 
45 #define QCOW_REFCOUNT_BLOCK_SHIFT	1
46 
47 struct qcow_refcount_block {
48 	u64				offset;
49 	struct rb_node			node;
50 	struct list_head		list;
51 	u64				size;
52 	u8				dirty;
53 	u16				entries[];
54 };
55 
56 struct qcow_refcount_table {
57 	u32				rf_size;
58 	u64				*rf_table;
59 
60 	/* Refcount block caching data structures */
61 	struct rb_root			root;
62 	struct list_head		lru_list;
63 	int				nr_cached;
64 };
65 
66 struct qcow_header {
67 	u64				size;	/* in bytes */
68 	u64				l1_table_offset;
69 	u32				l1_size;
70 	u8				cluster_bits;
71 	u8				l2_bits;
72 	u64				refcount_table_offset;
73 	u32				refcount_table_size;
74 };
75 
76 struct qcow {
77 	struct mutex			mutex;
78 	struct qcow_header		*header;
79 	struct qcow_l1_table		table;
80 	struct qcow_refcount_table	refcount_table;
81 	int				fd;
82 	int				csize_shift;
83 	int				csize_mask;
84 	u32				version;
85 	u64				cluster_size;
86 	u64				cluster_offset_mask;
87 	u64				free_clust_idx;
88 	void				*cluster_cache;
89 	void				*cluster_data;
90 	void				*copy_buff;
91 };
92 
93 struct qcow1_header_disk {
94 	u32				magic;
95 	u32				version;
96 
97 	u64				backing_file_offset;
98 	u32 				backing_file_size;
99 	u32				mtime;
100 
101 	u64				size;	/* in bytes */
102 
103 	u8				cluster_bits;
104 	u8				l2_bits;
105 	u32				crypt_method;
106 
107 	u64				l1_table_offset;
108 };
109 
110 struct qcow2_header_disk {
111 	u32				magic;
112 	u32				version;
113 
114 	u64				backing_file_offset;
115 	u32				backing_file_size;
116 
117 	u32				cluster_bits;
118 	u64				size;	/* in bytes */
119 	u32				crypt_method;
120 
121 	u32				l1_size;
122 	u64				l1_table_offset;
123 
124 	u64				refcount_table_offset;
125 	u32				refcount_table_clusters;
126 
127 	u32				nb_snapshots;
128 	u64				snapshots_offset;
129 };
130 
131 struct disk_image *qcow_probe(int fd, bool readonly);
132 
133 #endif /* KVM__QCOW_H */
134