]>
Commit | Line | Data |
---|---|---|
f7d0fe02 KW |
1 | /* |
2 | * Block driver for the QCOW version 2 format | |
3 | * | |
4 | * Copyright (c) 2004-2006 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #ifndef BLOCK_QCOW2_H | |
26 | #define BLOCK_QCOW2_H | |
27 | ||
28 | #include "aes.h" | |
68d100e9 | 29 | #include "qemu-coroutine.h" |
f7d0fe02 | 30 | |
14899cdf FN |
31 | //#define DEBUG_ALLOC |
32 | //#define DEBUG_ALLOC2 | |
33 | //#define DEBUG_EXT | |
34 | ||
f7d0fe02 KW |
35 | #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb) |
36 | #define QCOW_VERSION 2 | |
37 | ||
38 | #define QCOW_CRYPT_NONE 0 | |
39 | #define QCOW_CRYPT_AES 1 | |
40 | ||
41 | #define QCOW_MAX_CRYPT_CLUSTERS 32 | |
42 | ||
43 | /* indicate that the refcount of the referenced cluster is exactly one. */ | |
44 | #define QCOW_OFLAG_COPIED (1LL << 63) | |
45 | /* indicate that the cluster is compressed (they never have the copied flag) */ | |
46 | #define QCOW_OFLAG_COMPRESSED (1LL << 62) | |
47 | ||
48 | #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */ | |
49 | ||
50 | #define MIN_CLUSTER_BITS 9 | |
80ee15a6 | 51 | #define MAX_CLUSTER_BITS 21 |
f7d0fe02 KW |
52 | |
53 | #define L2_CACHE_SIZE 16 | |
54 | ||
29c1a730 KW |
55 | /* Must be at least 4 to cover all cases of refcount table growth */ |
56 | #define REFCOUNT_CACHE_SIZE 4 | |
57 | ||
99cce9fa KW |
58 | #define DEFAULT_CLUSTER_SIZE 65536 |
59 | ||
f7d0fe02 KW |
60 | typedef struct QCowHeader { |
61 | uint32_t magic; | |
62 | uint32_t version; | |
63 | uint64_t backing_file_offset; | |
64 | uint32_t backing_file_size; | |
65 | uint32_t cluster_bits; | |
66 | uint64_t size; /* in bytes */ | |
67 | uint32_t crypt_method; | |
68 | uint32_t l1_size; /* XXX: save number of clusters instead ? */ | |
69 | uint64_t l1_table_offset; | |
70 | uint64_t refcount_table_offset; | |
71 | uint32_t refcount_table_clusters; | |
72 | uint32_t nb_snapshots; | |
73 | uint64_t snapshots_offset; | |
74 | } QCowHeader; | |
75 | ||
76 | typedef struct QCowSnapshot { | |
77 | uint64_t l1_table_offset; | |
78 | uint32_t l1_size; | |
79 | char *id_str; | |
80 | char *name; | |
90b27759 | 81 | uint64_t disk_size; |
c2c9a466 | 82 | uint64_t vm_state_size; |
f7d0fe02 KW |
83 | uint32_t date_sec; |
84 | uint32_t date_nsec; | |
85 | uint64_t vm_clock_nsec; | |
86 | } QCowSnapshot; | |
87 | ||
49381094 KW |
88 | struct Qcow2Cache; |
89 | typedef struct Qcow2Cache Qcow2Cache; | |
90 | ||
75bab85c KW |
91 | typedef struct Qcow2UnknownHeaderExtension { |
92 | uint32_t magic; | |
93 | uint32_t len; | |
94 | QLIST_ENTRY(Qcow2UnknownHeaderExtension) next; | |
95 | uint8_t data[]; | |
96 | } Qcow2UnknownHeaderExtension; | |
97 | ||
f7d0fe02 | 98 | typedef struct BDRVQcowState { |
f7d0fe02 KW |
99 | int cluster_bits; |
100 | int cluster_size; | |
101 | int cluster_sectors; | |
102 | int l2_bits; | |
103 | int l2_size; | |
104 | int l1_size; | |
105 | int l1_vm_state_index; | |
106 | int csize_shift; | |
107 | int csize_mask; | |
108 | uint64_t cluster_offset_mask; | |
109 | uint64_t l1_table_offset; | |
110 | uint64_t *l1_table; | |
29c1a730 KW |
111 | |
112 | Qcow2Cache* l2_table_cache; | |
113 | Qcow2Cache* refcount_block_cache; | |
114 | ||
f7d0fe02 KW |
115 | uint8_t *cluster_cache; |
116 | uint8_t *cluster_data; | |
117 | uint64_t cluster_cache_offset; | |
72cf2d4f | 118 | QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs; |
f7d0fe02 KW |
119 | |
120 | uint64_t *refcount_table; | |
121 | uint64_t refcount_table_offset; | |
122 | uint32_t refcount_table_size; | |
f7d0fe02 KW |
123 | int64_t free_cluster_index; |
124 | int64_t free_byte_offset; | |
125 | ||
68d100e9 KW |
126 | CoMutex lock; |
127 | ||
f7d0fe02 KW |
128 | uint32_t crypt_method; /* current crypt method, 0 if no key yet */ |
129 | uint32_t crypt_method_header; | |
130 | AES_KEY aes_encrypt_key; | |
131 | AES_KEY aes_decrypt_key; | |
132 | uint64_t snapshots_offset; | |
133 | int snapshots_size; | |
134 | int nb_snapshots; | |
135 | QCowSnapshot *snapshots; | |
06d9260f AL |
136 | |
137 | int flags; | |
75bab85c | 138 | QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext; |
f7d0fe02 KW |
139 | } BDRVQcowState; |
140 | ||
141 | /* XXX: use std qcow open function ? */ | |
142 | typedef struct QCowCreateState { | |
143 | int cluster_size; | |
144 | int cluster_bits; | |
145 | uint16_t *refcount_block; | |
146 | uint64_t *refcount_table; | |
147 | int64_t l1_table_offset; | |
148 | int64_t refcount_table_offset; | |
149 | int64_t refcount_block_offset; | |
150 | } QCowCreateState; | |
151 | ||
f214978a KW |
152 | struct QCowAIOCB; |
153 | ||
45aba42f KW |
154 | /* XXX This could be private for qcow2-cluster.c */ |
155 | typedef struct QCowL2Meta | |
156 | { | |
157 | uint64_t offset; | |
148da7ea | 158 | uint64_t cluster_offset; |
250196f1 | 159 | uint64_t alloc_offset; |
45aba42f KW |
160 | int n_start; |
161 | int nb_available; | |
162 | int nb_clusters; | |
68d100e9 | 163 | CoQueue dependent_requests; |
f214978a | 164 | |
72cf2d4f | 165 | QLIST_ENTRY(QCowL2Meta) next_in_flight; |
45aba42f KW |
166 | } QCowL2Meta; |
167 | ||
68d000a3 KW |
168 | enum { |
169 | QCOW2_CLUSTER_UNALLOCATED, | |
170 | QCOW2_CLUSTER_NORMAL, | |
171 | QCOW2_CLUSTER_COMPRESSED, | |
172 | }; | |
173 | ||
174 | #define L1E_OFFSET_MASK 0x00ffffffffffff00ULL | |
175 | #define L2E_OFFSET_MASK 0x00ffffffffffff00ULL | |
176 | #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL | |
177 | ||
76dc9e0c KW |
178 | #define REFT_OFFSET_MASK 0xffffffffffffff00ULL |
179 | ||
45aba42f | 180 | static inline int size_to_clusters(BDRVQcowState *s, int64_t size) |
f7d0fe02 KW |
181 | { |
182 | return (size + (s->cluster_size - 1)) >> s->cluster_bits; | |
183 | } | |
184 | ||
419b19d9 SH |
185 | static inline int size_to_l1(BDRVQcowState *s, int64_t size) |
186 | { | |
187 | int shift = s->cluster_bits + s->l2_bits; | |
188 | return (size + (1ULL << shift) - 1) >> shift; | |
189 | } | |
190 | ||
c142442b KW |
191 | static inline int64_t align_offset(int64_t offset, int n) |
192 | { | |
193 | offset = (offset + n - 1) & ~(n - 1); | |
194 | return offset; | |
195 | } | |
196 | ||
68d000a3 KW |
197 | static inline int qcow2_get_cluster_type(uint64_t l2_entry) |
198 | { | |
199 | if (l2_entry & QCOW_OFLAG_COMPRESSED) { | |
200 | return QCOW2_CLUSTER_COMPRESSED; | |
201 | } else if (!(l2_entry & L2E_OFFSET_MASK)) { | |
202 | return QCOW2_CLUSTER_UNALLOCATED; | |
203 | } else { | |
204 | return QCOW2_CLUSTER_NORMAL; | |
205 | } | |
206 | } | |
207 | ||
c142442b | 208 | |
f7d0fe02 KW |
209 | // FIXME Need qcow2_ prefix to global functions |
210 | ||
211 | /* qcow2.c functions */ | |
bd28f835 KW |
212 | int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov, |
213 | int64_t sector_num, int nb_sectors); | |
e24e49e6 | 214 | int qcow2_update_header(BlockDriverState *bs); |
f7d0fe02 KW |
215 | |
216 | /* qcow2-refcount.c functions */ | |
ed6ccf0f KW |
217 | int qcow2_refcount_init(BlockDriverState *bs); |
218 | void qcow2_refcount_close(BlockDriverState *bs); | |
f7d0fe02 | 219 | |
ed6ccf0f | 220 | int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size); |
256900b1 KW |
221 | int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset, |
222 | int nb_clusters); | |
ed6ccf0f KW |
223 | int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size); |
224 | void qcow2_free_clusters(BlockDriverState *bs, | |
45aba42f | 225 | int64_t offset, int64_t size); |
ed6ccf0f | 226 | void qcow2_free_any_clusters(BlockDriverState *bs, |
45aba42f | 227 | uint64_t cluster_offset, int nb_clusters); |
f7d0fe02 | 228 | |
ed6ccf0f KW |
229 | int qcow2_update_snapshot_refcount(BlockDriverState *bs, |
230 | int64_t l1_table_offset, int l1_size, int addend); | |
f7d0fe02 | 231 | |
9ac228e0 | 232 | int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res); |
f7d0fe02 | 233 | |
45aba42f | 234 | /* qcow2-cluster.c functions */ |
72893756 | 235 | int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size); |
ed6ccf0f | 236 | void qcow2_l2_cache_reset(BlockDriverState *bs); |
66f82cee | 237 | int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset); |
ed6ccf0f | 238 | void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num, |
45aba42f KW |
239 | uint8_t *out_buf, const uint8_t *in_buf, |
240 | int nb_sectors, int enc, | |
241 | const AES_KEY *key); | |
242 | ||
1c46efaa KW |
243 | int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset, |
244 | int *num, uint64_t *cluster_offset); | |
f4f0d391 KW |
245 | int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset, |
246 | int n_start, int n_end, int *num, QCowL2Meta *m); | |
ed6ccf0f | 247 | uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, |
45aba42f KW |
248 | uint64_t offset, |
249 | int compressed_size); | |
250 | ||
148da7ea | 251 | int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m); |
5ea929e3 KW |
252 | int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset, |
253 | int nb_sectors); | |
45aba42f | 254 | |
c142442b | 255 | /* qcow2-snapshot.c functions */ |
ed6ccf0f KW |
256 | int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info); |
257 | int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id); | |
258 | int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id); | |
259 | int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab); | |
51ef6727 | 260 | int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name); |
c142442b | 261 | |
ed6ccf0f KW |
262 | void qcow2_free_snapshots(BlockDriverState *bs); |
263 | int qcow2_read_snapshots(BlockDriverState *bs); | |
c142442b | 264 | |
49381094 KW |
265 | /* qcow2-cache.c functions */ |
266 | Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables, | |
267 | bool writethrough); | |
268 | int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c); | |
93913dfd KW |
269 | bool qcow2_cache_set_writethrough(BlockDriverState *bs, Qcow2Cache *c, |
270 | bool enable); | |
49381094 KW |
271 | |
272 | void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table); | |
273 | int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c); | |
274 | int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c, | |
275 | Qcow2Cache *dependency); | |
3de0a294 | 276 | void qcow2_cache_depends_on_flush(Qcow2Cache *c); |
49381094 KW |
277 | |
278 | int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, | |
279 | void **table); | |
280 | int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, | |
281 | void **table); | |
282 | int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table); | |
283 | ||
f7d0fe02 | 284 | #endif |