]>
Commit | Line | Data |
---|---|---|
585f8587 FB |
1 | /* |
2 | * Block driver for the QCOW version 2 format | |
5fafdf24 | 3 | * |
585f8587 | 4 | * Copyright (c) 2004-2006 Fabrice Bellard |
5fafdf24 | 5 | * |
585f8587 FB |
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 | */ | |
faf07963 | 24 | #include "qemu-common.h" |
585f8587 FB |
25 | #include "block_int.h" |
26 | #include <zlib.h> | |
27 | #include "aes.h" | |
28 | #include <assert.h> | |
29 | ||
30 | /* | |
31 | Differences with QCOW: | |
32 | ||
33 | - Support for multiple incremental snapshots. | |
34 | - Memory management by reference counts. | |
35 | - Clusters which have a reference count of one have the bit | |
36 | QCOW_OFLAG_COPIED to optimize write performance. | |
5fafdf24 | 37 | - Size of compressed clusters is stored in sectors to reduce bit usage |
585f8587 FB |
38 | in the cluster offsets. |
39 | - Support for storing additional data (such as the VM state) in the | |
3b46e624 | 40 | snapshots. |
585f8587 FB |
41 | - If a backing store is used, the cluster size is not constrained |
42 | (could be backported to QCOW). | |
43 | - L2 tables have always a size of one cluster. | |
44 | */ | |
45 | ||
46 | //#define DEBUG_ALLOC | |
47 | //#define DEBUG_ALLOC2 | |
5fafdf24 | 48 | |
585f8587 FB |
49 | #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb) |
50 | #define QCOW_VERSION 2 | |
51 | ||
52 | #define QCOW_CRYPT_NONE 0 | |
53 | #define QCOW_CRYPT_AES 1 | |
54 | ||
095a9c58 AL |
55 | #define QCOW_MAX_CRYPT_CLUSTERS 32 |
56 | ||
585f8587 FB |
57 | /* indicate that the refcount of the referenced cluster is exactly one. */ |
58 | #define QCOW_OFLAG_COPIED (1LL << 63) | |
59 | /* indicate that the cluster is compressed (they never have the copied flag) */ | |
60 | #define QCOW_OFLAG_COMPRESSED (1LL << 62) | |
61 | ||
62 | #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */ | |
63 | ||
585f8587 FB |
64 | typedef struct QCowHeader { |
65 | uint32_t magic; | |
66 | uint32_t version; | |
67 | uint64_t backing_file_offset; | |
68 | uint32_t backing_file_size; | |
69 | uint32_t cluster_bits; | |
70 | uint64_t size; /* in bytes */ | |
71 | uint32_t crypt_method; | |
72 | uint32_t l1_size; /* XXX: save number of clusters instead ? */ | |
73 | uint64_t l1_table_offset; | |
74 | uint64_t refcount_table_offset; | |
75 | uint32_t refcount_table_clusters; | |
76 | uint32_t nb_snapshots; | |
77 | uint64_t snapshots_offset; | |
78 | } QCowHeader; | |
79 | ||
80 | typedef struct __attribute__((packed)) QCowSnapshotHeader { | |
81 | /* header is 8 byte aligned */ | |
82 | uint64_t l1_table_offset; | |
83 | ||
84 | uint32_t l1_size; | |
85 | uint16_t id_str_size; | |
86 | uint16_t name_size; | |
87 | ||
88 | uint32_t date_sec; | |
89 | uint32_t date_nsec; | |
90 | ||
91 | uint64_t vm_clock_nsec; | |
92 | ||
93 | uint32_t vm_state_size; | |
94 | uint32_t extra_data_size; /* for extension */ | |
95 | /* extra data follows */ | |
96 | /* id_str follows */ | |
97 | /* name follows */ | |
98 | } QCowSnapshotHeader; | |
99 | ||
100 | #define L2_CACHE_SIZE 16 | |
101 | ||
102 | typedef struct QCowSnapshot { | |
103 | uint64_t l1_table_offset; | |
104 | uint32_t l1_size; | |
105 | char *id_str; | |
106 | char *name; | |
107 | uint32_t vm_state_size; | |
108 | uint32_t date_sec; | |
109 | uint32_t date_nsec; | |
110 | uint64_t vm_clock_nsec; | |
111 | } QCowSnapshot; | |
112 | ||
113 | typedef struct BDRVQcowState { | |
114 | BlockDriverState *hd; | |
115 | int cluster_bits; | |
116 | int cluster_size; | |
117 | int cluster_sectors; | |
118 | int l2_bits; | |
119 | int l2_size; | |
120 | int l1_size; | |
121 | int l1_vm_state_index; | |
122 | int csize_shift; | |
123 | int csize_mask; | |
124 | uint64_t cluster_offset_mask; | |
125 | uint64_t l1_table_offset; | |
126 | uint64_t *l1_table; | |
127 | uint64_t *l2_cache; | |
128 | uint64_t l2_cache_offsets[L2_CACHE_SIZE]; | |
129 | uint32_t l2_cache_counts[L2_CACHE_SIZE]; | |
130 | uint8_t *cluster_cache; | |
131 | uint8_t *cluster_data; | |
132 | uint64_t cluster_cache_offset; | |
133 | ||
134 | uint64_t *refcount_table; | |
135 | uint64_t refcount_table_offset; | |
136 | uint32_t refcount_table_size; | |
137 | uint64_t refcount_block_cache_offset; | |
138 | uint16_t *refcount_block_cache; | |
139 | int64_t free_cluster_index; | |
140 | int64_t free_byte_offset; | |
141 | ||
142 | uint32_t crypt_method; /* current crypt method, 0 if no key yet */ | |
143 | uint32_t crypt_method_header; | |
144 | AES_KEY aes_encrypt_key; | |
145 | AES_KEY aes_decrypt_key; | |
146 | uint64_t snapshots_offset; | |
147 | int snapshots_size; | |
148 | int nb_snapshots; | |
149 | QCowSnapshot *snapshots; | |
150 | } BDRVQcowState; | |
151 | ||
152 | static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset); | |
5fafdf24 | 153 | static int qcow_read(BlockDriverState *bs, int64_t sector_num, |
585f8587 FB |
154 | uint8_t *buf, int nb_sectors); |
155 | static int qcow_read_snapshots(BlockDriverState *bs); | |
156 | static void qcow_free_snapshots(BlockDriverState *bs); | |
157 | static int refcount_init(BlockDriverState *bs); | |
158 | static void refcount_close(BlockDriverState *bs); | |
159 | static int get_refcount(BlockDriverState *bs, int64_t cluster_index); | |
5fafdf24 | 160 | static int update_cluster_refcount(BlockDriverState *bs, |
585f8587 FB |
161 | int64_t cluster_index, |
162 | int addend); | |
5fafdf24 TS |
163 | static void update_refcount(BlockDriverState *bs, |
164 | int64_t offset, int64_t length, | |
585f8587 FB |
165 | int addend); |
166 | static int64_t alloc_clusters(BlockDriverState *bs, int64_t size); | |
167 | static int64_t alloc_bytes(BlockDriverState *bs, int size); | |
5fafdf24 | 168 | static void free_clusters(BlockDriverState *bs, |
585f8587 FB |
169 | int64_t offset, int64_t size); |
170 | #ifdef DEBUG_ALLOC | |
171 | static void check_refcounts(BlockDriverState *bs); | |
172 | #endif | |
173 | ||
174 | static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) | |
175 | { | |
176 | const QCowHeader *cow_header = (const void *)buf; | |
3b46e624 | 177 | |
585f8587 FB |
178 | if (buf_size >= sizeof(QCowHeader) && |
179 | be32_to_cpu(cow_header->magic) == QCOW_MAGIC && | |
5fafdf24 | 180 | be32_to_cpu(cow_header->version) == QCOW_VERSION) |
585f8587 FB |
181 | return 100; |
182 | else | |
183 | return 0; | |
184 | } | |
185 | ||
186 | static int qcow_open(BlockDriverState *bs, const char *filename, int flags) | |
187 | { | |
188 | BDRVQcowState *s = bs->opaque; | |
189 | int len, i, shift, ret; | |
190 | QCowHeader header; | |
191 | ||
4dc822d7 AL |
192 | /* Performance is terrible right now with cache=writethrough due mainly |
193 | * to reference count updates. If the user does not explicitly specify | |
194 | * a caching type, force to writeback caching. | |
195 | */ | |
196 | if ((flags & BDRV_O_CACHE_DEF)) { | |
197 | flags |= BDRV_O_CACHE_WB; | |
198 | flags &= ~BDRV_O_CACHE_DEF; | |
199 | } | |
b5eff355 | 200 | ret = bdrv_file_open(&s->hd, filename, flags); |
585f8587 FB |
201 | if (ret < 0) |
202 | return ret; | |
203 | if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header)) | |
204 | goto fail; | |
205 | be32_to_cpus(&header.magic); | |
206 | be32_to_cpus(&header.version); | |
207 | be64_to_cpus(&header.backing_file_offset); | |
208 | be32_to_cpus(&header.backing_file_size); | |
209 | be64_to_cpus(&header.size); | |
210 | be32_to_cpus(&header.cluster_bits); | |
211 | be32_to_cpus(&header.crypt_method); | |
212 | be64_to_cpus(&header.l1_table_offset); | |
213 | be32_to_cpus(&header.l1_size); | |
214 | be64_to_cpus(&header.refcount_table_offset); | |
215 | be32_to_cpus(&header.refcount_table_clusters); | |
216 | be64_to_cpus(&header.snapshots_offset); | |
217 | be32_to_cpus(&header.nb_snapshots); | |
3b46e624 | 218 | |
585f8587 FB |
219 | if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION) |
220 | goto fail; | |
5fafdf24 TS |
221 | if (header.size <= 1 || |
222 | header.cluster_bits < 9 || | |
585f8587 FB |
223 | header.cluster_bits > 16) |
224 | goto fail; | |
225 | if (header.crypt_method > QCOW_CRYPT_AES) | |
226 | goto fail; | |
227 | s->crypt_method_header = header.crypt_method; | |
228 | if (s->crypt_method_header) | |
229 | bs->encrypted = 1; | |
230 | s->cluster_bits = header.cluster_bits; | |
231 | s->cluster_size = 1 << s->cluster_bits; | |
232 | s->cluster_sectors = 1 << (s->cluster_bits - 9); | |
233 | s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */ | |
234 | s->l2_size = 1 << s->l2_bits; | |
235 | bs->total_sectors = header.size / 512; | |
236 | s->csize_shift = (62 - (s->cluster_bits - 8)); | |
237 | s->csize_mask = (1 << (s->cluster_bits - 8)) - 1; | |
238 | s->cluster_offset_mask = (1LL << s->csize_shift) - 1; | |
239 | s->refcount_table_offset = header.refcount_table_offset; | |
5fafdf24 | 240 | s->refcount_table_size = |
585f8587 FB |
241 | header.refcount_table_clusters << (s->cluster_bits - 3); |
242 | ||
243 | s->snapshots_offset = header.snapshots_offset; | |
244 | s->nb_snapshots = header.nb_snapshots; | |
245 | ||
246 | /* read the level 1 table */ | |
247 | s->l1_size = header.l1_size; | |
248 | shift = s->cluster_bits + s->l2_bits; | |
249 | s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift; | |
250 | /* the L1 table must contain at least enough entries to put | |
251 | header.size bytes */ | |
252 | if (s->l1_size < s->l1_vm_state_index) | |
253 | goto fail; | |
254 | s->l1_table_offset = header.l1_table_offset; | |
255 | s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t)); | |
5fafdf24 | 256 | if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != |
585f8587 FB |
257 | s->l1_size * sizeof(uint64_t)) |
258 | goto fail; | |
259 | for(i = 0;i < s->l1_size; i++) { | |
260 | be64_to_cpus(&s->l1_table[i]); | |
261 | } | |
262 | /* alloc L2 cache */ | |
263 | s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); | |
585f8587 | 264 | s->cluster_cache = qemu_malloc(s->cluster_size); |
585f8587 | 265 | /* one more sector for decompressed data alignment */ |
095a9c58 AL |
266 | s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size |
267 | + 512); | |
585f8587 | 268 | s->cluster_cache_offset = -1; |
3b46e624 | 269 | |
585f8587 FB |
270 | if (refcount_init(bs) < 0) |
271 | goto fail; | |
272 | ||
273 | /* read the backing file name */ | |
274 | if (header.backing_file_offset != 0) { | |
275 | len = header.backing_file_size; | |
276 | if (len > 1023) | |
277 | len = 1023; | |
278 | if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len) | |
279 | goto fail; | |
280 | bs->backing_file[len] = '\0'; | |
281 | } | |
282 | if (qcow_read_snapshots(bs) < 0) | |
283 | goto fail; | |
284 | ||
285 | #ifdef DEBUG_ALLOC | |
286 | check_refcounts(bs); | |
287 | #endif | |
288 | return 0; | |
289 | ||
290 | fail: | |
291 | qcow_free_snapshots(bs); | |
292 | refcount_close(bs); | |
293 | qemu_free(s->l1_table); | |
294 | qemu_free(s->l2_cache); | |
295 | qemu_free(s->cluster_cache); | |
296 | qemu_free(s->cluster_data); | |
297 | bdrv_delete(s->hd); | |
298 | return -1; | |
299 | } | |
300 | ||
301 | static int qcow_set_key(BlockDriverState *bs, const char *key) | |
302 | { | |
303 | BDRVQcowState *s = bs->opaque; | |
304 | uint8_t keybuf[16]; | |
305 | int len, i; | |
3b46e624 | 306 | |
585f8587 FB |
307 | memset(keybuf, 0, 16); |
308 | len = strlen(key); | |
309 | if (len > 16) | |
310 | len = 16; | |
311 | /* XXX: we could compress the chars to 7 bits to increase | |
312 | entropy */ | |
313 | for(i = 0;i < len;i++) { | |
314 | keybuf[i] = key[i]; | |
315 | } | |
316 | s->crypt_method = s->crypt_method_header; | |
317 | ||
318 | if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) | |
319 | return -1; | |
320 | if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) | |
321 | return -1; | |
322 | #if 0 | |
323 | /* test */ | |
324 | { | |
325 | uint8_t in[16]; | |
326 | uint8_t out[16]; | |
327 | uint8_t tmp[16]; | |
328 | for(i=0;i<16;i++) | |
329 | in[i] = i; | |
330 | AES_encrypt(in, tmp, &s->aes_encrypt_key); | |
331 | AES_decrypt(tmp, out, &s->aes_decrypt_key); | |
332 | for(i = 0; i < 16; i++) | |
333 | printf(" %02x", tmp[i]); | |
334 | printf("\n"); | |
335 | for(i = 0; i < 16; i++) | |
336 | printf(" %02x", out[i]); | |
337 | printf("\n"); | |
338 | } | |
339 | #endif | |
340 | return 0; | |
341 | } | |
342 | ||
343 | /* The crypt function is compatible with the linux cryptoloop | |
344 | algorithm for < 4 GB images. NOTE: out_buf == in_buf is | |
345 | supported */ | |
346 | static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num, | |
347 | uint8_t *out_buf, const uint8_t *in_buf, | |
348 | int nb_sectors, int enc, | |
349 | const AES_KEY *key) | |
350 | { | |
351 | union { | |
352 | uint64_t ll[2]; | |
353 | uint8_t b[16]; | |
354 | } ivec; | |
355 | int i; | |
356 | ||
357 | for(i = 0; i < nb_sectors; i++) { | |
358 | ivec.ll[0] = cpu_to_le64(sector_num); | |
359 | ivec.ll[1] = 0; | |
5fafdf24 | 360 | AES_cbc_encrypt(in_buf, out_buf, 512, key, |
585f8587 FB |
361 | ivec.b, enc); |
362 | sector_num++; | |
363 | in_buf += 512; | |
364 | out_buf += 512; | |
365 | } | |
366 | } | |
367 | ||
368 | static int copy_sectors(BlockDriverState *bs, uint64_t start_sect, | |
369 | uint64_t cluster_offset, int n_start, int n_end) | |
370 | { | |
371 | BDRVQcowState *s = bs->opaque; | |
372 | int n, ret; | |
373 | ||
374 | n = n_end - n_start; | |
375 | if (n <= 0) | |
376 | return 0; | |
377 | ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n); | |
378 | if (ret < 0) | |
379 | return ret; | |
380 | if (s->crypt_method) { | |
5fafdf24 TS |
381 | encrypt_sectors(s, start_sect + n_start, |
382 | s->cluster_data, | |
585f8587 FB |
383 | s->cluster_data, n, 1, |
384 | &s->aes_encrypt_key); | |
385 | } | |
5fafdf24 | 386 | ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start, |
585f8587 FB |
387 | s->cluster_data, n); |
388 | if (ret < 0) | |
389 | return ret; | |
390 | return 0; | |
391 | } | |
392 | ||
393 | static void l2_cache_reset(BlockDriverState *bs) | |
394 | { | |
395 | BDRVQcowState *s = bs->opaque; | |
396 | ||
397 | memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); | |
398 | memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t)); | |
399 | memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t)); | |
400 | } | |
401 | ||
402 | static inline int l2_cache_new_entry(BlockDriverState *bs) | |
403 | { | |
404 | BDRVQcowState *s = bs->opaque; | |
405 | uint32_t min_count; | |
406 | int min_index, i; | |
407 | ||
408 | /* find a new entry in the least used one */ | |
409 | min_index = 0; | |
410 | min_count = 0xffffffff; | |
411 | for(i = 0; i < L2_CACHE_SIZE; i++) { | |
412 | if (s->l2_cache_counts[i] < min_count) { | |
413 | min_count = s->l2_cache_counts[i]; | |
414 | min_index = i; | |
415 | } | |
416 | } | |
417 | return min_index; | |
418 | } | |
419 | ||
420 | static int64_t align_offset(int64_t offset, int n) | |
421 | { | |
422 | offset = (offset + n - 1) & ~(n - 1); | |
423 | return offset; | |
424 | } | |
425 | ||
426 | static int grow_l1_table(BlockDriverState *bs, int min_size) | |
427 | { | |
428 | BDRVQcowState *s = bs->opaque; | |
429 | int new_l1_size, new_l1_size2, ret, i; | |
430 | uint64_t *new_l1_table; | |
431 | uint64_t new_l1_table_offset; | |
643e5399 | 432 | uint8_t data[12]; |
585f8587 FB |
433 | |
434 | new_l1_size = s->l1_size; | |
435 | if (min_size <= new_l1_size) | |
436 | return 0; | |
437 | while (min_size > new_l1_size) { | |
438 | new_l1_size = (new_l1_size * 3 + 1) / 2; | |
439 | } | |
440 | #ifdef DEBUG_ALLOC2 | |
441 | printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size); | |
442 | #endif | |
443 | ||
444 | new_l1_size2 = sizeof(uint64_t) * new_l1_size; | |
445 | new_l1_table = qemu_mallocz(new_l1_size2); | |
585f8587 FB |
446 | memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t)); |
447 | ||
448 | /* write new table (align to cluster) */ | |
449 | new_l1_table_offset = alloc_clusters(bs, new_l1_size2); | |
3b46e624 | 450 | |
585f8587 FB |
451 | for(i = 0; i < s->l1_size; i++) |
452 | new_l1_table[i] = cpu_to_be64(new_l1_table[i]); | |
453 | ret = bdrv_pwrite(s->hd, new_l1_table_offset, new_l1_table, new_l1_size2); | |
454 | if (ret != new_l1_size2) | |
455 | goto fail; | |
456 | for(i = 0; i < s->l1_size; i++) | |
457 | new_l1_table[i] = be64_to_cpu(new_l1_table[i]); | |
3b46e624 | 458 | |
585f8587 | 459 | /* set new table */ |
643e5399 AL |
460 | cpu_to_be32w((uint32_t*)data, new_l1_size); |
461 | cpu_to_be64w((uint64_t*)(data + 4), new_l1_table_offset); | |
462 | if (bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_size), data, | |
463 | sizeof(data)) != sizeof(data)) | |
585f8587 FB |
464 | goto fail; |
465 | qemu_free(s->l1_table); | |
466 | free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t)); | |
467 | s->l1_table_offset = new_l1_table_offset; | |
468 | s->l1_table = new_l1_table; | |
469 | s->l1_size = new_l1_size; | |
470 | return 0; | |
471 | fail: | |
472 | qemu_free(s->l1_table); | |
473 | return -EIO; | |
474 | } | |
475 | ||
108534b9 AL |
476 | /* |
477 | * seek_l2_table | |
585f8587 | 478 | * |
108534b9 AL |
479 | * seek l2_offset in the l2_cache table |
480 | * if not found, return NULL, | |
481 | * if found, | |
482 | * increments the l2 cache hit count of the entry, | |
483 | * if counter overflow, divide by two all counters | |
484 | * return the pointer to the l2 cache entry | |
585f8587 | 485 | * |
108534b9 AL |
486 | */ |
487 | ||
488 | static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset) | |
489 | { | |
490 | int i, j; | |
491 | ||
492 | for(i = 0; i < L2_CACHE_SIZE; i++) { | |
493 | if (l2_offset == s->l2_cache_offsets[i]) { | |
494 | /* increment the hit count */ | |
495 | if (++s->l2_cache_counts[i] == 0xffffffff) { | |
496 | for(j = 0; j < L2_CACHE_SIZE; j++) { | |
497 | s->l2_cache_counts[j] >>= 1; | |
498 | } | |
499 | } | |
500 | return s->l2_cache + (i << s->l2_bits); | |
501 | } | |
502 | } | |
503 | return NULL; | |
504 | } | |
505 | ||
506 | /* | |
507 | * l2_load | |
508 | * | |
509 | * Loads a L2 table into memory. If the table is in the cache, the cache | |
510 | * is used; otherwise the L2 table is loaded from the image file. | |
511 | * | |
512 | * Returns a pointer to the L2 table on success, or NULL if the read from | |
513 | * the image file failed. | |
514 | */ | |
515 | ||
516 | static uint64_t *l2_load(BlockDriverState *bs, uint64_t l2_offset) | |
517 | { | |
518 | BDRVQcowState *s = bs->opaque; | |
519 | int min_index; | |
520 | uint64_t *l2_table; | |
521 | ||
522 | /* seek if the table for the given offset is in the cache */ | |
523 | ||
524 | l2_table = seek_l2_table(s, l2_offset); | |
525 | if (l2_table != NULL) | |
526 | return l2_table; | |
527 | ||
528 | /* not found: load a new entry in the least used one */ | |
529 | ||
530 | min_index = l2_cache_new_entry(bs); | |
531 | l2_table = s->l2_cache + (min_index << s->l2_bits); | |
532 | if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) != | |
533 | s->l2_size * sizeof(uint64_t)) | |
534 | return NULL; | |
535 | s->l2_cache_offsets[min_index] = l2_offset; | |
536 | s->l2_cache_counts[min_index] = 1; | |
537 | ||
538 | return l2_table; | |
539 | } | |
540 | ||
541 | /* | |
542 | * l2_allocate | |
585f8587 | 543 | * |
108534b9 AL |
544 | * Allocate a new l2 entry in the file. If l1_index points to an already |
545 | * used entry in the L2 table (i.e. we are doing a copy on write for the L2 | |
546 | * table) copy the contents of the old L2 table into the newly allocated one. | |
547 | * Otherwise the new table is initialized with zeros. | |
585f8587 | 548 | * |
585f8587 | 549 | */ |
108534b9 AL |
550 | |
551 | static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index) | |
552 | { | |
553 | BDRVQcowState *s = bs->opaque; | |
554 | int min_index; | |
555 | uint64_t old_l2_offset, tmp; | |
556 | uint64_t *l2_table, l2_offset; | |
557 | ||
558 | old_l2_offset = s->l1_table[l1_index]; | |
559 | ||
560 | /* allocate a new l2 entry */ | |
561 | ||
562 | l2_offset = alloc_clusters(bs, s->l2_size * sizeof(uint64_t)); | |
563 | ||
564 | /* update the L1 entry */ | |
565 | ||
566 | s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED; | |
567 | ||
568 | tmp = cpu_to_be64(l2_offset | QCOW_OFLAG_COPIED); | |
569 | if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp), | |
570 | &tmp, sizeof(tmp)) != sizeof(tmp)) | |
571 | return NULL; | |
572 | ||
573 | /* allocate a new entry in the l2 cache */ | |
574 | ||
575 | min_index = l2_cache_new_entry(bs); | |
576 | l2_table = s->l2_cache + (min_index << s->l2_bits); | |
577 | ||
578 | if (old_l2_offset == 0) { | |
579 | /* if there was no old l2 table, clear the new table */ | |
580 | memset(l2_table, 0, s->l2_size * sizeof(uint64_t)); | |
581 | } else { | |
582 | /* if there was an old l2 table, read it from the disk */ | |
583 | if (bdrv_pread(s->hd, old_l2_offset, | |
584 | l2_table, s->l2_size * sizeof(uint64_t)) != | |
585 | s->l2_size * sizeof(uint64_t)) | |
586 | return NULL; | |
587 | } | |
588 | /* write the l2 table to the file */ | |
589 | if (bdrv_pwrite(s->hd, l2_offset, | |
590 | l2_table, s->l2_size * sizeof(uint64_t)) != | |
591 | s->l2_size * sizeof(uint64_t)) | |
592 | return NULL; | |
593 | ||
594 | /* update the l2 cache entry */ | |
595 | ||
596 | s->l2_cache_offsets[min_index] = l2_offset; | |
597 | s->l2_cache_counts[min_index] = 1; | |
598 | ||
599 | return l2_table; | |
600 | } | |
601 | ||
6db6c638 AL |
602 | static int size_to_clusters(BDRVQcowState *s, int64_t size) |
603 | { | |
604 | return (size + (s->cluster_size - 1)) >> s->cluster_bits; | |
605 | } | |
606 | ||
607 | static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size, | |
ff4b91c2 | 608 | uint64_t *l2_table, uint64_t start, uint64_t mask) |
6db6c638 AL |
609 | { |
610 | int i; | |
611 | uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask; | |
612 | ||
ab5ccbd6 AL |
613 | if (!offset) |
614 | return 0; | |
615 | ||
ff4b91c2 | 616 | for (i = start; i < start + nb_clusters; i++) |
6db6c638 AL |
617 | if (offset + i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask)) |
618 | break; | |
619 | ||
ff4b91c2 | 620 | return (i - start); |
6db6c638 AL |
621 | } |
622 | ||
623 | static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table) | |
624 | { | |
625 | int i = 0; | |
626 | ||
627 | while(nb_clusters-- && l2_table[i] == 0) | |
628 | i++; | |
629 | ||
630 | return i; | |
631 | } | |
632 | ||
05203524 AL |
633 | /* |
634 | * get_cluster_offset | |
635 | * | |
636 | * For a given offset of the disk image, return cluster offset in | |
637 | * qcow2 file. | |
638 | * | |
095a9c58 AL |
639 | * on entry, *num is the number of contiguous clusters we'd like to |
640 | * access following offset. | |
641 | * | |
642 | * on exit, *num is the number of contiguous clusters we can read. | |
643 | * | |
05203524 AL |
644 | * Return 1, if the offset is found |
645 | * Return 0, otherwise. | |
646 | * | |
647 | */ | |
648 | ||
095a9c58 AL |
649 | static uint64_t get_cluster_offset(BlockDriverState *bs, |
650 | uint64_t offset, int *num) | |
05203524 AL |
651 | { |
652 | BDRVQcowState *s = bs->opaque; | |
653 | int l1_index, l2_index; | |
6db6c638 AL |
654 | uint64_t l2_offset, *l2_table, cluster_offset; |
655 | int l1_bits, c; | |
656 | int index_in_cluster, nb_available, nb_needed, nb_clusters; | |
095a9c58 AL |
657 | |
658 | index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1); | |
659 | nb_needed = *num + index_in_cluster; | |
660 | ||
661 | l1_bits = s->l2_bits + s->cluster_bits; | |
662 | ||
663 | /* compute how many bytes there are between the offset and | |
6db6c638 | 664 | * the end of the l1 entry |
095a9c58 AL |
665 | */ |
666 | ||
667 | nb_available = (1 << l1_bits) - (offset & ((1 << l1_bits) - 1)); | |
668 | ||
669 | /* compute the number of available sectors */ | |
670 | ||
671 | nb_available = (nb_available >> 9) + index_in_cluster; | |
672 | ||
44988194 AL |
673 | if (nb_needed > nb_available) { |
674 | nb_needed = nb_available; | |
675 | } | |
676 | ||
095a9c58 | 677 | cluster_offset = 0; |
05203524 AL |
678 | |
679 | /* seek the the l2 offset in the l1 table */ | |
680 | ||
095a9c58 | 681 | l1_index = offset >> l1_bits; |
05203524 | 682 | if (l1_index >= s->l1_size) |
095a9c58 | 683 | goto out; |
05203524 AL |
684 | |
685 | l2_offset = s->l1_table[l1_index]; | |
686 | ||
687 | /* seek the l2 table of the given l2 offset */ | |
688 | ||
689 | if (!l2_offset) | |
095a9c58 | 690 | goto out; |
05203524 AL |
691 | |
692 | /* load the l2 table in memory */ | |
693 | ||
694 | l2_offset &= ~QCOW_OFLAG_COPIED; | |
695 | l2_table = l2_load(bs, l2_offset); | |
696 | if (l2_table == NULL) | |
768706a5 | 697 | return 0; |
05203524 AL |
698 | |
699 | /* find the cluster offset for the given disk offset */ | |
700 | ||
701 | l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1); | |
702 | cluster_offset = be64_to_cpu(l2_table[l2_index]); | |
6db6c638 | 703 | nb_clusters = size_to_clusters(s, nb_needed << 9); |
095a9c58 AL |
704 | |
705 | if (!cluster_offset) { | |
6db6c638 AL |
706 | /* how many empty clusters ? */ |
707 | c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]); | |
095a9c58 | 708 | } else { |
6db6c638 AL |
709 | /* how many allocated clusters ? */ |
710 | c = count_contiguous_clusters(nb_clusters, s->cluster_size, | |
ff4b91c2 | 711 | &l2_table[l2_index], 0, QCOW_OFLAG_COPIED); |
6db6c638 | 712 | } |
095a9c58 | 713 | |
6db6c638 | 714 | nb_available = (c * s->cluster_sectors); |
095a9c58 AL |
715 | out: |
716 | if (nb_available > nb_needed) | |
717 | nb_available = nb_needed; | |
718 | ||
719 | *num = nb_available - index_in_cluster; | |
720 | ||
6db6c638 | 721 | return cluster_offset & ~QCOW_OFLAG_COPIED; |
05203524 AL |
722 | } |
723 | ||
724 | /* | |
52d893ec | 725 | * free_any_clusters |
05203524 | 726 | * |
52d893ec | 727 | * free clusters according to its type: compressed or not |
05203524 | 728 | * |
52d893ec AL |
729 | */ |
730 | ||
731 | static void free_any_clusters(BlockDriverState *bs, | |
095a9c58 | 732 | uint64_t cluster_offset, int nb_clusters) |
52d893ec AL |
733 | { |
734 | BDRVQcowState *s = bs->opaque; | |
735 | ||
52d893ec AL |
736 | /* free the cluster */ |
737 | ||
738 | if (cluster_offset & QCOW_OFLAG_COMPRESSED) { | |
739 | int nb_csectors; | |
740 | nb_csectors = ((cluster_offset >> s->csize_shift) & | |
741 | s->csize_mask) + 1; | |
742 | free_clusters(bs, (cluster_offset & s->cluster_offset_mask) & ~511, | |
743 | nb_csectors * 512); | |
744 | return; | |
745 | } | |
746 | ||
095a9c58 AL |
747 | free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits); |
748 | ||
749 | return; | |
52d893ec AL |
750 | } |
751 | ||
752 | /* | |
753 | * get_cluster_table | |
05203524 | 754 | * |
52d893ec AL |
755 | * for a given disk offset, load (and allocate if needed) |
756 | * the l2 table. | |
757 | * | |
758 | * the l2 table offset in the qcow2 file and the cluster index | |
759 | * in the l2 table are given to the caller. | |
05203524 AL |
760 | * |
761 | */ | |
762 | ||
52d893ec AL |
763 | static int get_cluster_table(BlockDriverState *bs, uint64_t offset, |
764 | uint64_t **new_l2_table, | |
765 | uint64_t *new_l2_offset, | |
766 | int *new_l2_index) | |
585f8587 FB |
767 | { |
768 | BDRVQcowState *s = bs->opaque; | |
108534b9 | 769 | int l1_index, l2_index, ret; |
52d893ec | 770 | uint64_t l2_offset, *l2_table; |
108534b9 AL |
771 | |
772 | /* seek the the l2 offset in the l1 table */ | |
3b46e624 | 773 | |
585f8587 FB |
774 | l1_index = offset >> (s->l2_bits + s->cluster_bits); |
775 | if (l1_index >= s->l1_size) { | |
108534b9 AL |
776 | ret = grow_l1_table(bs, l1_index + 1); |
777 | if (ret < 0) | |
585f8587 FB |
778 | return 0; |
779 | } | |
780 | l2_offset = s->l1_table[l1_index]; | |
108534b9 AL |
781 | |
782 | /* seek the l2 table of the given l2 offset */ | |
783 | ||
05203524 AL |
784 | if (l2_offset & QCOW_OFLAG_COPIED) { |
785 | /* load the l2 table in memory */ | |
786 | l2_offset &= ~QCOW_OFLAG_COPIED; | |
787 | l2_table = l2_load(bs, l2_offset); | |
788 | if (l2_table == NULL) | |
585f8587 | 789 | return 0; |
05203524 AL |
790 | } else { |
791 | if (l2_offset) | |
792 | free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t)); | |
108534b9 AL |
793 | l2_table = l2_allocate(bs, l1_index); |
794 | if (l2_table == NULL) | |
585f8587 | 795 | return 0; |
108534b9 | 796 | l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED; |
585f8587 | 797 | } |
108534b9 AL |
798 | |
799 | /* find the cluster offset for the given disk offset */ | |
800 | ||
585f8587 | 801 | l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1); |
05203524 | 802 | |
52d893ec AL |
803 | *new_l2_table = l2_table; |
804 | *new_l2_offset = l2_offset; | |
805 | *new_l2_index = l2_index; | |
806 | ||
807 | return 1; | |
808 | } | |
809 | ||
810 | /* | |
811 | * alloc_compressed_cluster_offset | |
812 | * | |
813 | * For a given offset of the disk image, return cluster offset in | |
814 | * qcow2 file. | |
815 | * | |
816 | * If the offset is not found, allocate a new compressed cluster. | |
817 | * | |
818 | * Return the cluster offset if successful, | |
819 | * Return 0, otherwise. | |
820 | * | |
821 | */ | |
822 | ||
823 | static uint64_t alloc_compressed_cluster_offset(BlockDriverState *bs, | |
824 | uint64_t offset, | |
825 | int compressed_size) | |
826 | { | |
827 | BDRVQcowState *s = bs->opaque; | |
828 | int l2_index, ret; | |
829 | uint64_t l2_offset, *l2_table, cluster_offset; | |
830 | int nb_csectors; | |
831 | ||
832 | ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index); | |
833 | if (ret == 0) | |
834 | return 0; | |
835 | ||
836 | cluster_offset = be64_to_cpu(l2_table[l2_index]); | |
05203524 AL |
837 | if (cluster_offset & QCOW_OFLAG_COPIED) |
838 | return cluster_offset & ~QCOW_OFLAG_COPIED; | |
839 | ||
095a9c58 AL |
840 | if (cluster_offset) |
841 | free_any_clusters(bs, cluster_offset, 1); | |
108534b9 | 842 | |
52d893ec AL |
843 | cluster_offset = alloc_bytes(bs, compressed_size); |
844 | nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) - | |
845 | (cluster_offset >> 9); | |
05203524 | 846 | |
52d893ec AL |
847 | cluster_offset |= QCOW_OFLAG_COMPRESSED | |
848 | ((uint64_t)nb_csectors << s->csize_shift); | |
05203524 | 849 | |
52d893ec | 850 | /* update L2 table */ |
05203524 | 851 | |
52d893ec | 852 | /* compressed clusters never have the copied flag */ |
05203524 | 853 | |
52d893ec AL |
854 | l2_table[l2_index] = cpu_to_be64(cluster_offset); |
855 | if (bdrv_pwrite(s->hd, | |
856 | l2_offset + l2_index * sizeof(uint64_t), | |
857 | l2_table + l2_index, | |
858 | sizeof(uint64_t)) != sizeof(uint64_t)) | |
859 | return 0; | |
05203524 | 860 | |
52d893ec AL |
861 | return cluster_offset; |
862 | } | |
05203524 | 863 | |
e976c6a1 AL |
864 | typedef struct QCowL2Meta |
865 | { | |
866 | uint64_t offset; | |
867 | int n_start; | |
868 | int nb_available; | |
869 | int nb_clusters; | |
870 | } QCowL2Meta; | |
871 | ||
872 | static int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset, | |
873 | QCowL2Meta *m) | |
874 | { | |
875 | BDRVQcowState *s = bs->opaque; | |
876 | int i, j = 0, l2_index, ret; | |
877 | uint64_t *old_cluster, start_sect, l2_offset, *l2_table; | |
878 | ||
879 | if (m->nb_clusters == 0) | |
880 | return 0; | |
881 | ||
3ec88e80 | 882 | old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t)); |
e976c6a1 AL |
883 | |
884 | /* copy content of unmodified sectors */ | |
885 | start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9; | |
886 | if (m->n_start) { | |
887 | ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start); | |
888 | if (ret < 0) | |
889 | goto err; | |
890 | } | |
891 | ||
892 | if (m->nb_available & (s->cluster_sectors - 1)) { | |
893 | uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1); | |
894 | ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9), | |
895 | m->nb_available - end, s->cluster_sectors); | |
896 | if (ret < 0) | |
897 | goto err; | |
898 | } | |
899 | ||
900 | ret = -EIO; | |
901 | /* update L2 table */ | |
902 | if (!get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index)) | |
903 | goto err; | |
904 | ||
905 | for (i = 0; i < m->nb_clusters; i++) { | |
8bc2ad6a GN |
906 | /* if two concurrent writes happen to the same unallocated cluster |
907 | * each write allocates separate cluster and writes data concurrently. | |
908 | * The first one to complete updates l2 table with pointer to its | |
909 | * cluster the second one has to do RMW (which is done above by | |
910 | * copy_sectors()), update l2 table with its cluster pointer and free | |
911 | * old cluster. This is what this loop does */ | |
e976c6a1 AL |
912 | if(l2_table[l2_index + i] != 0) |
913 | old_cluster[j++] = l2_table[l2_index + i]; | |
914 | ||
915 | l2_table[l2_index + i] = cpu_to_be64((cluster_offset + | |
916 | (i << s->cluster_bits)) | QCOW_OFLAG_COPIED); | |
917 | } | |
918 | ||
919 | if (bdrv_pwrite(s->hd, l2_offset + l2_index * sizeof(uint64_t), | |
920 | l2_table + l2_index, m->nb_clusters * sizeof(uint64_t)) != | |
921 | m->nb_clusters * sizeof(uint64_t)) | |
922 | goto err; | |
923 | ||
924 | for (i = 0; i < j; i++) | |
8bc2ad6a GN |
925 | free_any_clusters(bs, be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, |
926 | 1); | |
e976c6a1 AL |
927 | |
928 | ret = 0; | |
929 | err: | |
930 | qemu_free(old_cluster); | |
931 | return ret; | |
932 | } | |
933 | ||
52d893ec AL |
934 | /* |
935 | * alloc_cluster_offset | |
936 | * | |
937 | * For a given offset of the disk image, return cluster offset in | |
938 | * qcow2 file. | |
939 | * | |
940 | * If the offset is not found, allocate a new cluster. | |
941 | * | |
942 | * Return the cluster offset if successful, | |
943 | * Return 0, otherwise. | |
944 | * | |
945 | */ | |
946 | ||
947 | static uint64_t alloc_cluster_offset(BlockDriverState *bs, | |
948 | uint64_t offset, | |
095a9c58 | 949 | int n_start, int n_end, |
e976c6a1 | 950 | int *num, QCowL2Meta *m) |
52d893ec AL |
951 | { |
952 | BDRVQcowState *s = bs->opaque; | |
953 | int l2_index, ret; | |
954 | uint64_t l2_offset, *l2_table, cluster_offset; | |
e976c6a1 | 955 | int nb_clusters, i = 0; |
52d893ec AL |
956 | |
957 | ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index); | |
958 | if (ret == 0) | |
959 | return 0; | |
960 | ||
6db6c638 AL |
961 | nb_clusters = size_to_clusters(s, n_end << 9); |
962 | ||
e976c6a1 | 963 | nb_clusters = MIN(nb_clusters, s->l2_size - l2_index); |
095a9c58 | 964 | |
52d893ec | 965 | cluster_offset = be64_to_cpu(l2_table[l2_index]); |
52d893ec | 966 | |
095a9c58 AL |
967 | /* We keep all QCOW_OFLAG_COPIED clusters */ |
968 | ||
969 | if (cluster_offset & QCOW_OFLAG_COPIED) { | |
6db6c638 | 970 | nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size, |
ff4b91c2 | 971 | &l2_table[l2_index], 0, 0); |
095a9c58 | 972 | |
095a9c58 | 973 | cluster_offset &= ~QCOW_OFLAG_COPIED; |
e976c6a1 | 974 | m->nb_clusters = 0; |
095a9c58 AL |
975 | |
976 | goto out; | |
977 | } | |
978 | ||
979 | /* for the moment, multiple compressed clusters are not managed */ | |
980 | ||
981 | if (cluster_offset & QCOW_OFLAG_COMPRESSED) | |
982 | nb_clusters = 1; | |
983 | ||
bc352085 | 984 | /* how many available clusters ? */ |
095a9c58 | 985 | |
bc352085 | 986 | while (i < nb_clusters) { |
ab5ccbd6 | 987 | i += count_contiguous_clusters(nb_clusters - i, s->cluster_size, |
ff4b91c2 | 988 | &l2_table[l2_index], i, 0); |
ab5ccbd6 AL |
989 | |
990 | if(be64_to_cpu(l2_table[l2_index + i])) | |
991 | break; | |
992 | ||
6db6c638 AL |
993 | i += count_contiguous_free_clusters(nb_clusters - i, |
994 | &l2_table[l2_index + i]); | |
095a9c58 | 995 | |
6db6c638 | 996 | cluster_offset = be64_to_cpu(l2_table[l2_index + i]); |
095a9c58 | 997 | |
6db6c638 | 998 | if ((cluster_offset & QCOW_OFLAG_COPIED) || |
bc352085 | 999 | (cluster_offset & QCOW_OFLAG_COMPRESSED)) |
6db6c638 | 1000 | break; |
095a9c58 | 1001 | } |
bc352085 | 1002 | nb_clusters = i; |
05203524 AL |
1003 | |
1004 | /* allocate a new cluster */ | |
1005 | ||
095a9c58 | 1006 | cluster_offset = alloc_clusters(bs, nb_clusters * s->cluster_size); |
05203524 | 1007 | |
e976c6a1 AL |
1008 | /* save info needed for meta data update */ |
1009 | m->offset = offset; | |
1010 | m->n_start = n_start; | |
1011 | m->nb_clusters = nb_clusters; | |
05203524 | 1012 | |
095a9c58 | 1013 | out: |
e976c6a1 AL |
1014 | m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end); |
1015 | ||
1016 | *num = m->nb_available - n_start; | |
095a9c58 | 1017 | |
585f8587 FB |
1018 | return cluster_offset; |
1019 | } | |
1020 | ||
5fafdf24 | 1021 | static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
585f8587 FB |
1022 | int nb_sectors, int *pnum) |
1023 | { | |
585f8587 FB |
1024 | uint64_t cluster_offset; |
1025 | ||
095a9c58 AL |
1026 | *pnum = nb_sectors; |
1027 | cluster_offset = get_cluster_offset(bs, sector_num << 9, pnum); | |
1028 | ||
585f8587 FB |
1029 | return (cluster_offset != 0); |
1030 | } | |
1031 | ||
1032 | static int decompress_buffer(uint8_t *out_buf, int out_buf_size, | |
1033 | const uint8_t *buf, int buf_size) | |
1034 | { | |
1035 | z_stream strm1, *strm = &strm1; | |
1036 | int ret, out_len; | |
1037 | ||
1038 | memset(strm, 0, sizeof(*strm)); | |
1039 | ||
1040 | strm->next_in = (uint8_t *)buf; | |
1041 | strm->avail_in = buf_size; | |
1042 | strm->next_out = out_buf; | |
1043 | strm->avail_out = out_buf_size; | |
1044 | ||
1045 | ret = inflateInit2(strm, -12); | |
1046 | if (ret != Z_OK) | |
1047 | return -1; | |
1048 | ret = inflate(strm, Z_FINISH); | |
1049 | out_len = strm->next_out - out_buf; | |
1050 | if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) || | |
1051 | out_len != out_buf_size) { | |
1052 | inflateEnd(strm); | |
1053 | return -1; | |
1054 | } | |
1055 | inflateEnd(strm); | |
1056 | return 0; | |
1057 | } | |
3b46e624 | 1058 | |
585f8587 FB |
1059 | static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset) |
1060 | { | |
1061 | int ret, csize, nb_csectors, sector_offset; | |
1062 | uint64_t coffset; | |
1063 | ||
1064 | coffset = cluster_offset & s->cluster_offset_mask; | |
1065 | if (s->cluster_cache_offset != coffset) { | |
1066 | nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1; | |
1067 | sector_offset = coffset & 511; | |
1068 | csize = nb_csectors * 512 - sector_offset; | |
1069 | ret = bdrv_read(s->hd, coffset >> 9, s->cluster_data, nb_csectors); | |
1070 | if (ret < 0) { | |
1071 | return -1; | |
1072 | } | |
1073 | if (decompress_buffer(s->cluster_cache, s->cluster_size, | |
1074 | s->cluster_data + sector_offset, csize) < 0) { | |
1075 | return -1; | |
1076 | } | |
1077 | s->cluster_cache_offset = coffset; | |
1078 | } | |
1079 | return 0; | |
1080 | } | |
1081 | ||
a9465922 | 1082 | /* handle reading after the end of the backing file */ |
5fafdf24 | 1083 | static int backing_read1(BlockDriverState *bs, |
a9465922 FB |
1084 | int64_t sector_num, uint8_t *buf, int nb_sectors) |
1085 | { | |
1086 | int n1; | |
1087 | if ((sector_num + nb_sectors) <= bs->total_sectors) | |
1088 | return nb_sectors; | |
1089 | if (sector_num >= bs->total_sectors) | |
1090 | n1 = 0; | |
1091 | else | |
1092 | n1 = bs->total_sectors - sector_num; | |
1093 | memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1)); | |
1094 | return n1; | |
1095 | } | |
1096 | ||
5fafdf24 | 1097 | static int qcow_read(BlockDriverState *bs, int64_t sector_num, |
585f8587 FB |
1098 | uint8_t *buf, int nb_sectors) |
1099 | { | |
1100 | BDRVQcowState *s = bs->opaque; | |
a9465922 | 1101 | int ret, index_in_cluster, n, n1; |
585f8587 | 1102 | uint64_t cluster_offset; |
3b46e624 | 1103 | |
585f8587 | 1104 | while (nb_sectors > 0) { |
095a9c58 AL |
1105 | n = nb_sectors; |
1106 | cluster_offset = get_cluster_offset(bs, sector_num << 9, &n); | |
585f8587 | 1107 | index_in_cluster = sector_num & (s->cluster_sectors - 1); |
585f8587 FB |
1108 | if (!cluster_offset) { |
1109 | if (bs->backing_hd) { | |
1110 | /* read from the base image */ | |
a9465922 FB |
1111 | n1 = backing_read1(bs->backing_hd, sector_num, buf, n); |
1112 | if (n1 > 0) { | |
1113 | ret = bdrv_read(bs->backing_hd, sector_num, buf, n1); | |
1114 | if (ret < 0) | |
1115 | return -1; | |
1116 | } | |
585f8587 FB |
1117 | } else { |
1118 | memset(buf, 0, 512 * n); | |
1119 | } | |
1120 | } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) { | |
1121 | if (decompress_cluster(s, cluster_offset) < 0) | |
1122 | return -1; | |
1123 | memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n); | |
1124 | } else { | |
1125 | ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512); | |
5fafdf24 | 1126 | if (ret != n * 512) |
585f8587 FB |
1127 | return -1; |
1128 | if (s->crypt_method) { | |
5fafdf24 | 1129 | encrypt_sectors(s, sector_num, buf, buf, n, 0, |
585f8587 FB |
1130 | &s->aes_decrypt_key); |
1131 | } | |
1132 | } | |
1133 | nb_sectors -= n; | |
1134 | sector_num += n; | |
1135 | buf += n * 512; | |
1136 | } | |
1137 | return 0; | |
1138 | } | |
1139 | ||
5fafdf24 | 1140 | static int qcow_write(BlockDriverState *bs, int64_t sector_num, |
585f8587 FB |
1141 | const uint8_t *buf, int nb_sectors) |
1142 | { | |
1143 | BDRVQcowState *s = bs->opaque; | |
1144 | int ret, index_in_cluster, n; | |
1145 | uint64_t cluster_offset; | |
095a9c58 | 1146 | int n_end; |
e976c6a1 | 1147 | QCowL2Meta l2meta; |
3b46e624 | 1148 | |
585f8587 FB |
1149 | while (nb_sectors > 0) { |
1150 | index_in_cluster = sector_num & (s->cluster_sectors - 1); | |
095a9c58 AL |
1151 | n_end = index_in_cluster + nb_sectors; |
1152 | if (s->crypt_method && | |
1153 | n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) | |
1154 | n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors; | |
52d893ec | 1155 | cluster_offset = alloc_cluster_offset(bs, sector_num << 9, |
05203524 | 1156 | index_in_cluster, |
e976c6a1 | 1157 | n_end, &n, &l2meta); |
585f8587 FB |
1158 | if (!cluster_offset) |
1159 | return -1; | |
1160 | if (s->crypt_method) { | |
1161 | encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1, | |
1162 | &s->aes_encrypt_key); | |
5fafdf24 | 1163 | ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, |
585f8587 FB |
1164 | s->cluster_data, n * 512); |
1165 | } else { | |
1166 | ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512); | |
1167 | } | |
e976c6a1 AL |
1168 | if (ret != n * 512 || alloc_cluster_link_l2(bs, cluster_offset, &l2meta) < 0) { |
1169 | free_any_clusters(bs, cluster_offset, l2meta.nb_clusters); | |
585f8587 | 1170 | return -1; |
e976c6a1 | 1171 | } |
585f8587 FB |
1172 | nb_sectors -= n; |
1173 | sector_num += n; | |
1174 | buf += n * 512; | |
1175 | } | |
1176 | s->cluster_cache_offset = -1; /* disable compressed cache */ | |
1177 | return 0; | |
1178 | } | |
1179 | ||
ce1a14dc PB |
1180 | typedef struct QCowAIOCB { |
1181 | BlockDriverAIOCB common; | |
585f8587 FB |
1182 | int64_t sector_num; |
1183 | uint8_t *buf; | |
1184 | int nb_sectors; | |
1185 | int n; | |
1186 | uint64_t cluster_offset; | |
5fafdf24 | 1187 | uint8_t *cluster_data; |
585f8587 | 1188 | BlockDriverAIOCB *hd_aiocb; |
1490791f | 1189 | QEMUBH *bh; |
e976c6a1 | 1190 | QCowL2Meta l2meta; |
585f8587 FB |
1191 | } QCowAIOCB; |
1192 | ||
1490791f AL |
1193 | static void qcow_aio_read_cb(void *opaque, int ret); |
1194 | static void qcow_aio_read_bh(void *opaque) | |
1195 | { | |
1196 | QCowAIOCB *acb = opaque; | |
1197 | qemu_bh_delete(acb->bh); | |
1198 | acb->bh = NULL; | |
1199 | qcow_aio_read_cb(opaque, 0); | |
1200 | } | |
1201 | ||
a32ef786 AL |
1202 | static int qcow_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb) |
1203 | { | |
1204 | if (acb->bh) | |
1205 | return -EIO; | |
1206 | ||
1207 | acb->bh = qemu_bh_new(cb, acb); | |
1208 | if (!acb->bh) | |
1209 | return -EIO; | |
1210 | ||
1211 | qemu_bh_schedule(acb->bh); | |
1212 | ||
1213 | return 0; | |
1214 | } | |
1215 | ||
585f8587 FB |
1216 | static void qcow_aio_read_cb(void *opaque, int ret) |
1217 | { | |
ce1a14dc PB |
1218 | QCowAIOCB *acb = opaque; |
1219 | BlockDriverState *bs = acb->common.bs; | |
585f8587 | 1220 | BDRVQcowState *s = bs->opaque; |
a9465922 | 1221 | int index_in_cluster, n1; |
585f8587 | 1222 | |
ce1a14dc | 1223 | acb->hd_aiocb = NULL; |
585f8587 | 1224 | if (ret < 0) { |
ac674887 | 1225 | fail: |
ce1a14dc PB |
1226 | acb->common.cb(acb->common.opaque, ret); |
1227 | qemu_aio_release(acb); | |
585f8587 FB |
1228 | return; |
1229 | } | |
1230 | ||
585f8587 | 1231 | /* post process the read buffer */ |
ce1a14dc | 1232 | if (!acb->cluster_offset) { |
585f8587 | 1233 | /* nothing to do */ |
ce1a14dc | 1234 | } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
585f8587 FB |
1235 | /* nothing to do */ |
1236 | } else { | |
1237 | if (s->crypt_method) { | |
5fafdf24 TS |
1238 | encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf, |
1239 | acb->n, 0, | |
585f8587 FB |
1240 | &s->aes_decrypt_key); |
1241 | } | |
1242 | } | |
1243 | ||
ce1a14dc PB |
1244 | acb->nb_sectors -= acb->n; |
1245 | acb->sector_num += acb->n; | |
1246 | acb->buf += acb->n * 512; | |
585f8587 | 1247 | |
ce1a14dc | 1248 | if (acb->nb_sectors == 0) { |
585f8587 | 1249 | /* request completed */ |
ce1a14dc PB |
1250 | acb->common.cb(acb->common.opaque, 0); |
1251 | qemu_aio_release(acb); | |
585f8587 FB |
1252 | return; |
1253 | } | |
3b46e624 | 1254 | |
585f8587 | 1255 | /* prepare next AIO request */ |
095a9c58 AL |
1256 | acb->n = acb->nb_sectors; |
1257 | acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, &acb->n); | |
ce1a14dc | 1258 | index_in_cluster = acb->sector_num & (s->cluster_sectors - 1); |
ce1a14dc PB |
1259 | |
1260 | if (!acb->cluster_offset) { | |
585f8587 FB |
1261 | if (bs->backing_hd) { |
1262 | /* read from the base image */ | |
5fafdf24 | 1263 | n1 = backing_read1(bs->backing_hd, acb->sector_num, |
ce1a14dc | 1264 | acb->buf, acb->n); |
a9465922 | 1265 | if (n1 > 0) { |
5fafdf24 | 1266 | acb->hd_aiocb = bdrv_aio_read(bs->backing_hd, acb->sector_num, |
ce1a14dc PB |
1267 | acb->buf, acb->n, qcow_aio_read_cb, acb); |
1268 | if (acb->hd_aiocb == NULL) | |
a9465922 FB |
1269 | goto fail; |
1270 | } else { | |
a32ef786 AL |
1271 | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
1272 | if (ret < 0) | |
ac674887 | 1273 | goto fail; |
a9465922 | 1274 | } |
585f8587 FB |
1275 | } else { |
1276 | /* Note: in this case, no need to wait */ | |
ce1a14dc | 1277 | memset(acb->buf, 0, 512 * acb->n); |
a32ef786 AL |
1278 | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
1279 | if (ret < 0) | |
ac674887 | 1280 | goto fail; |
585f8587 | 1281 | } |
ce1a14dc | 1282 | } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
585f8587 | 1283 | /* add AIO support for compressed blocks ? */ |
ce1a14dc | 1284 | if (decompress_cluster(s, acb->cluster_offset) < 0) |
585f8587 | 1285 | goto fail; |
5fafdf24 | 1286 | memcpy(acb->buf, |
ce1a14dc | 1287 | s->cluster_cache + index_in_cluster * 512, 512 * acb->n); |
a32ef786 AL |
1288 | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
1289 | if (ret < 0) | |
ac674887 | 1290 | goto fail; |
585f8587 | 1291 | } else { |
ce1a14dc | 1292 | if ((acb->cluster_offset & 511) != 0) { |
585f8587 FB |
1293 | ret = -EIO; |
1294 | goto fail; | |
1295 | } | |
ce1a14dc | 1296 | acb->hd_aiocb = bdrv_aio_read(s->hd, |
5fafdf24 | 1297 | (acb->cluster_offset >> 9) + index_in_cluster, |
ce1a14dc PB |
1298 | acb->buf, acb->n, qcow_aio_read_cb, acb); |
1299 | if (acb->hd_aiocb == NULL) | |
585f8587 FB |
1300 | goto fail; |
1301 | } | |
1302 | } | |
1303 | ||
ce1a14dc PB |
1304 | static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs, |
1305 | int64_t sector_num, uint8_t *buf, int nb_sectors, | |
1306 | BlockDriverCompletionFunc *cb, void *opaque) | |
585f8587 | 1307 | { |
ce1a14dc PB |
1308 | QCowAIOCB *acb; |
1309 | ||
1310 | acb = qemu_aio_get(bs, cb, opaque); | |
1311 | if (!acb) | |
1312 | return NULL; | |
1313 | acb->hd_aiocb = NULL; | |
1314 | acb->sector_num = sector_num; | |
1315 | acb->buf = buf; | |
1316 | acb->nb_sectors = nb_sectors; | |
1317 | acb->n = 0; | |
1318 | acb->cluster_offset = 0; | |
e976c6a1 | 1319 | acb->l2meta.nb_clusters = 0; |
ce1a14dc PB |
1320 | return acb; |
1321 | } | |
1322 | ||
1323 | static BlockDriverAIOCB *qcow_aio_read(BlockDriverState *bs, | |
1324 | int64_t sector_num, uint8_t *buf, int nb_sectors, | |
1325 | BlockDriverCompletionFunc *cb, void *opaque) | |
1326 | { | |
1327 | QCowAIOCB *acb; | |
1328 | ||
1329 | acb = qcow_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque); | |
1330 | if (!acb) | |
1331 | return NULL; | |
585f8587 FB |
1332 | |
1333 | qcow_aio_read_cb(acb, 0); | |
ce1a14dc | 1334 | return &acb->common; |
585f8587 FB |
1335 | } |
1336 | ||
1337 | static void qcow_aio_write_cb(void *opaque, int ret) | |
1338 | { | |
ce1a14dc PB |
1339 | QCowAIOCB *acb = opaque; |
1340 | BlockDriverState *bs = acb->common.bs; | |
585f8587 | 1341 | BDRVQcowState *s = bs->opaque; |
585f8587 | 1342 | int index_in_cluster; |
585f8587 | 1343 | const uint8_t *src_buf; |
095a9c58 | 1344 | int n_end; |
ce1a14dc PB |
1345 | |
1346 | acb->hd_aiocb = NULL; | |
1347 | ||
585f8587 FB |
1348 | if (ret < 0) { |
1349 | fail: | |
ce1a14dc PB |
1350 | acb->common.cb(acb->common.opaque, ret); |
1351 | qemu_aio_release(acb); | |
585f8587 FB |
1352 | return; |
1353 | } | |
1354 | ||
e976c6a1 AL |
1355 | if (alloc_cluster_link_l2(bs, acb->cluster_offset, &acb->l2meta) < 0) { |
1356 | free_any_clusters(bs, acb->cluster_offset, acb->l2meta.nb_clusters); | |
1357 | goto fail; | |
1358 | } | |
1359 | ||
ce1a14dc PB |
1360 | acb->nb_sectors -= acb->n; |
1361 | acb->sector_num += acb->n; | |
1362 | acb->buf += acb->n * 512; | |
585f8587 | 1363 | |
ce1a14dc | 1364 | if (acb->nb_sectors == 0) { |
585f8587 | 1365 | /* request completed */ |
ce1a14dc PB |
1366 | acb->common.cb(acb->common.opaque, 0); |
1367 | qemu_aio_release(acb); | |
585f8587 FB |
1368 | return; |
1369 | } | |
3b46e624 | 1370 | |
ce1a14dc | 1371 | index_in_cluster = acb->sector_num & (s->cluster_sectors - 1); |
095a9c58 AL |
1372 | n_end = index_in_cluster + acb->nb_sectors; |
1373 | if (s->crypt_method && | |
1374 | n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) | |
1375 | n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors; | |
1376 | ||
e976c6a1 | 1377 | acb->cluster_offset = alloc_cluster_offset(bs, acb->sector_num << 9, |
05203524 | 1378 | index_in_cluster, |
e976c6a1 AL |
1379 | n_end, &acb->n, &acb->l2meta); |
1380 | if (!acb->cluster_offset || (acb->cluster_offset & 511) != 0) { | |
585f8587 FB |
1381 | ret = -EIO; |
1382 | goto fail; | |
1383 | } | |
1384 | if (s->crypt_method) { | |
ce1a14dc | 1385 | if (!acb->cluster_data) { |
095a9c58 AL |
1386 | acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS * |
1387 | s->cluster_size); | |
585f8587 | 1388 | } |
5fafdf24 | 1389 | encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf, |
ce1a14dc PB |
1390 | acb->n, 1, &s->aes_encrypt_key); |
1391 | src_buf = acb->cluster_data; | |
585f8587 | 1392 | } else { |
ce1a14dc | 1393 | src_buf = acb->buf; |
585f8587 | 1394 | } |
ce1a14dc | 1395 | acb->hd_aiocb = bdrv_aio_write(s->hd, |
e976c6a1 | 1396 | (acb->cluster_offset >> 9) + index_in_cluster, |
5fafdf24 | 1397 | src_buf, acb->n, |
ce1a14dc PB |
1398 | qcow_aio_write_cb, acb); |
1399 | if (acb->hd_aiocb == NULL) | |
585f8587 FB |
1400 | goto fail; |
1401 | } | |
1402 | ||
ce1a14dc PB |
1403 | static BlockDriverAIOCB *qcow_aio_write(BlockDriverState *bs, |
1404 | int64_t sector_num, const uint8_t *buf, int nb_sectors, | |
1405 | BlockDriverCompletionFunc *cb, void *opaque) | |
585f8587 | 1406 | { |
585f8587 | 1407 | BDRVQcowState *s = bs->opaque; |
ce1a14dc | 1408 | QCowAIOCB *acb; |
3b46e624 | 1409 | |
585f8587 FB |
1410 | s->cluster_cache_offset = -1; /* disable compressed cache */ |
1411 | ||
ce1a14dc PB |
1412 | acb = qcow_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque); |
1413 | if (!acb) | |
1414 | return NULL; | |
3b46e624 | 1415 | |
585f8587 | 1416 | qcow_aio_write_cb(acb, 0); |
ce1a14dc | 1417 | return &acb->common; |
585f8587 FB |
1418 | } |
1419 | ||
ce1a14dc | 1420 | static void qcow_aio_cancel(BlockDriverAIOCB *blockacb) |
585f8587 | 1421 | { |
ce1a14dc PB |
1422 | QCowAIOCB *acb = (QCowAIOCB *)blockacb; |
1423 | if (acb->hd_aiocb) | |
1424 | bdrv_aio_cancel(acb->hd_aiocb); | |
1425 | qemu_aio_release(acb); | |
585f8587 FB |
1426 | } |
1427 | ||
1428 | static void qcow_close(BlockDriverState *bs) | |
1429 | { | |
1430 | BDRVQcowState *s = bs->opaque; | |
1431 | qemu_free(s->l1_table); | |
1432 | qemu_free(s->l2_cache); | |
1433 | qemu_free(s->cluster_cache); | |
1434 | qemu_free(s->cluster_data); | |
1435 | refcount_close(bs); | |
1436 | bdrv_delete(s->hd); | |
1437 | } | |
1438 | ||
1439 | /* XXX: use std qcow open function ? */ | |
1440 | typedef struct QCowCreateState { | |
1441 | int cluster_size; | |
1442 | int cluster_bits; | |
1443 | uint16_t *refcount_block; | |
1444 | uint64_t *refcount_table; | |
1445 | int64_t l1_table_offset; | |
1446 | int64_t refcount_table_offset; | |
1447 | int64_t refcount_block_offset; | |
1448 | } QCowCreateState; | |
1449 | ||
1450 | static void create_refcount_update(QCowCreateState *s, | |
1451 | int64_t offset, int64_t size) | |
1452 | { | |
1453 | int refcount; | |
1454 | int64_t start, last, cluster_offset; | |
1455 | uint16_t *p; | |
1456 | ||
1457 | start = offset & ~(s->cluster_size - 1); | |
1458 | last = (offset + size - 1) & ~(s->cluster_size - 1); | |
5fafdf24 | 1459 | for(cluster_offset = start; cluster_offset <= last; |
585f8587 FB |
1460 | cluster_offset += s->cluster_size) { |
1461 | p = &s->refcount_block[cluster_offset >> s->cluster_bits]; | |
1462 | refcount = be16_to_cpu(*p); | |
1463 | refcount++; | |
1464 | *p = cpu_to_be16(refcount); | |
1465 | } | |
1466 | } | |
1467 | ||
1468 | static int qcow_create(const char *filename, int64_t total_size, | |
1469 | const char *backing_file, int flags) | |
1470 | { | |
1471 | int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits; | |
7cc73776 | 1472 | int ref_clusters; |
585f8587 FB |
1473 | QCowHeader header; |
1474 | uint64_t tmp, offset; | |
1475 | QCowCreateState s1, *s = &s1; | |
3b46e624 | 1476 | |
585f8587 FB |
1477 | memset(s, 0, sizeof(*s)); |
1478 | ||
1479 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); | |
1480 | if (fd < 0) | |
1481 | return -1; | |
1482 | memset(&header, 0, sizeof(header)); | |
1483 | header.magic = cpu_to_be32(QCOW_MAGIC); | |
1484 | header.version = cpu_to_be32(QCOW_VERSION); | |
1485 | header.size = cpu_to_be64(total_size * 512); | |
1486 | header_size = sizeof(header); | |
1487 | backing_filename_len = 0; | |
1488 | if (backing_file) { | |
1489 | header.backing_file_offset = cpu_to_be64(header_size); | |
1490 | backing_filename_len = strlen(backing_file); | |
1491 | header.backing_file_size = cpu_to_be32(backing_filename_len); | |
1492 | header_size += backing_filename_len; | |
1493 | } | |
1494 | s->cluster_bits = 12; /* 4 KB clusters */ | |
1495 | s->cluster_size = 1 << s->cluster_bits; | |
1496 | header.cluster_bits = cpu_to_be32(s->cluster_bits); | |
1497 | header_size = (header_size + 7) & ~7; | |
ec36ba14 | 1498 | if (flags & BLOCK_FLAG_ENCRYPT) { |
585f8587 FB |
1499 | header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); |
1500 | } else { | |
1501 | header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); | |
1502 | } | |
1503 | l2_bits = s->cluster_bits - 3; | |
1504 | shift = s->cluster_bits + l2_bits; | |
1505 | l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift); | |
1506 | offset = align_offset(header_size, s->cluster_size); | |
1507 | s->l1_table_offset = offset; | |
1508 | header.l1_table_offset = cpu_to_be64(s->l1_table_offset); | |
1509 | header.l1_size = cpu_to_be32(l1_size); | |
15e6690a | 1510 | offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size); |
585f8587 FB |
1511 | |
1512 | s->refcount_table = qemu_mallocz(s->cluster_size); | |
3b46e624 | 1513 | |
585f8587 FB |
1514 | s->refcount_table_offset = offset; |
1515 | header.refcount_table_offset = cpu_to_be64(offset); | |
1516 | header.refcount_table_clusters = cpu_to_be32(1); | |
1517 | offset += s->cluster_size; | |
585f8587 | 1518 | s->refcount_block_offset = offset; |
7cc73776 AL |
1519 | |
1520 | /* count how many refcount blocks needed */ | |
1521 | tmp = offset >> s->cluster_bits; | |
1522 | ref_clusters = (tmp >> (s->cluster_bits - REFCOUNT_SHIFT)) + 1; | |
1523 | for (i=0; i < ref_clusters; i++) { | |
1524 | s->refcount_table[i] = cpu_to_be64(offset); | |
1525 | offset += s->cluster_size; | |
1526 | } | |
1527 | ||
1528 | s->refcount_block = qemu_mallocz(ref_clusters * s->cluster_size); | |
585f8587 FB |
1529 | |
1530 | /* update refcounts */ | |
1531 | create_refcount_update(s, 0, header_size); | |
15e6690a | 1532 | create_refcount_update(s, s->l1_table_offset, l1_size * sizeof(uint64_t)); |
585f8587 | 1533 | create_refcount_update(s, s->refcount_table_offset, s->cluster_size); |
7cc73776 | 1534 | create_refcount_update(s, s->refcount_block_offset, ref_clusters * s->cluster_size); |
3b46e624 | 1535 | |
585f8587 FB |
1536 | /* write all the data */ |
1537 | write(fd, &header, sizeof(header)); | |
1538 | if (backing_file) { | |
1539 | write(fd, backing_file, backing_filename_len); | |
1540 | } | |
1541 | lseek(fd, s->l1_table_offset, SEEK_SET); | |
1542 | tmp = 0; | |
1543 | for(i = 0;i < l1_size; i++) { | |
1544 | write(fd, &tmp, sizeof(tmp)); | |
1545 | } | |
1546 | lseek(fd, s->refcount_table_offset, SEEK_SET); | |
1547 | write(fd, s->refcount_table, s->cluster_size); | |
3b46e624 | 1548 | |
585f8587 | 1549 | lseek(fd, s->refcount_block_offset, SEEK_SET); |
7cc73776 | 1550 | write(fd, s->refcount_block, ref_clusters * s->cluster_size); |
585f8587 FB |
1551 | |
1552 | qemu_free(s->refcount_table); | |
1553 | qemu_free(s->refcount_block); | |
1554 | close(fd); | |
1555 | return 0; | |
585f8587 FB |
1556 | } |
1557 | ||
1558 | static int qcow_make_empty(BlockDriverState *bs) | |
1559 | { | |
1560 | #if 0 | |
1561 | /* XXX: not correct */ | |
1562 | BDRVQcowState *s = bs->opaque; | |
1563 | uint32_t l1_length = s->l1_size * sizeof(uint64_t); | |
1564 | int ret; | |
1565 | ||
1566 | memset(s->l1_table, 0, l1_length); | |
1567 | if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0) | |
ac674887 | 1568 | return -1; |
585f8587 FB |
1569 | ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length); |
1570 | if (ret < 0) | |
1571 | return ret; | |
3b46e624 | 1572 | |
585f8587 FB |
1573 | l2_cache_reset(bs); |
1574 | #endif | |
1575 | return 0; | |
1576 | } | |
1577 | ||
1578 | /* XXX: put compressed sectors first, then all the cluster aligned | |
1579 | tables to avoid losing bytes in alignment */ | |
5fafdf24 | 1580 | static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num, |
585f8587 FB |
1581 | const uint8_t *buf, int nb_sectors) |
1582 | { | |
1583 | BDRVQcowState *s = bs->opaque; | |
1584 | z_stream strm; | |
1585 | int ret, out_len; | |
1586 | uint8_t *out_buf; | |
1587 | uint64_t cluster_offset; | |
1588 | ||
1589 | if (nb_sectors == 0) { | |
1590 | /* align end of file to a sector boundary to ease reading with | |
1591 | sector based I/Os */ | |
1592 | cluster_offset = bdrv_getlength(s->hd); | |
1593 | cluster_offset = (cluster_offset + 511) & ~511; | |
1594 | bdrv_truncate(s->hd, cluster_offset); | |
1595 | return 0; | |
1596 | } | |
1597 | ||
1598 | if (nb_sectors != s->cluster_sectors) | |
1599 | return -EINVAL; | |
1600 | ||
1601 | out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); | |
585f8587 FB |
1602 | |
1603 | /* best compression, small window, no zlib header */ | |
1604 | memset(&strm, 0, sizeof(strm)); | |
1605 | ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, | |
5fafdf24 | 1606 | Z_DEFLATED, -12, |
585f8587 FB |
1607 | 9, Z_DEFAULT_STRATEGY); |
1608 | if (ret != 0) { | |
1609 | qemu_free(out_buf); | |
1610 | return -1; | |
1611 | } | |
1612 | ||
1613 | strm.avail_in = s->cluster_size; | |
1614 | strm.next_in = (uint8_t *)buf; | |
1615 | strm.avail_out = s->cluster_size; | |
1616 | strm.next_out = out_buf; | |
1617 | ||
1618 | ret = deflate(&strm, Z_FINISH); | |
1619 | if (ret != Z_STREAM_END && ret != Z_OK) { | |
1620 | qemu_free(out_buf); | |
1621 | deflateEnd(&strm); | |
1622 | return -1; | |
1623 | } | |
1624 | out_len = strm.next_out - out_buf; | |
1625 | ||
1626 | deflateEnd(&strm); | |
1627 | ||
1628 | if (ret != Z_STREAM_END || out_len >= s->cluster_size) { | |
1629 | /* could not compress: write normal cluster */ | |
1630 | qcow_write(bs, sector_num, buf, s->cluster_sectors); | |
1631 | } else { | |
52d893ec AL |
1632 | cluster_offset = alloc_compressed_cluster_offset(bs, sector_num << 9, |
1633 | out_len); | |
1634 | if (!cluster_offset) | |
1635 | return -1; | |
585f8587 FB |
1636 | cluster_offset &= s->cluster_offset_mask; |
1637 | if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) { | |
1638 | qemu_free(out_buf); | |
1639 | return -1; | |
1640 | } | |
1641 | } | |
3b46e624 | 1642 | |
585f8587 FB |
1643 | qemu_free(out_buf); |
1644 | return 0; | |
1645 | } | |
1646 | ||
1647 | static void qcow_flush(BlockDriverState *bs) | |
1648 | { | |
1649 | BDRVQcowState *s = bs->opaque; | |
1650 | bdrv_flush(s->hd); | |
1651 | } | |
1652 | ||
1653 | static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) | |
1654 | { | |
1655 | BDRVQcowState *s = bs->opaque; | |
1656 | bdi->cluster_size = s->cluster_size; | |
5fafdf24 | 1657 | bdi->vm_state_offset = (int64_t)s->l1_vm_state_index << |
585f8587 FB |
1658 | (s->cluster_bits + s->l2_bits); |
1659 | return 0; | |
1660 | } | |
1661 | ||
1662 | /*********************************************************/ | |
1663 | /* snapshot support */ | |
1664 | ||
1665 | /* update the refcounts of snapshots and the copied flag */ | |
5fafdf24 | 1666 | static int update_snapshot_refcount(BlockDriverState *bs, |
585f8587 FB |
1667 | int64_t l1_table_offset, |
1668 | int l1_size, | |
1669 | int addend) | |
1670 | { | |
1671 | BDRVQcowState *s = bs->opaque; | |
1672 | uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated; | |
1673 | int64_t old_offset, old_l2_offset; | |
1674 | int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount; | |
3b46e624 | 1675 | |
585f8587 FB |
1676 | l2_cache_reset(bs); |
1677 | ||
1678 | l2_table = NULL; | |
1679 | l1_table = NULL; | |
1680 | l1_size2 = l1_size * sizeof(uint64_t); | |
1681 | l1_allocated = 0; | |
1682 | if (l1_table_offset != s->l1_table_offset) { | |
1683 | l1_table = qemu_malloc(l1_size2); | |
585f8587 | 1684 | l1_allocated = 1; |
5fafdf24 | 1685 | if (bdrv_pread(s->hd, l1_table_offset, |
585f8587 FB |
1686 | l1_table, l1_size2) != l1_size2) |
1687 | goto fail; | |
1688 | for(i = 0;i < l1_size; i++) | |
1689 | be64_to_cpus(&l1_table[i]); | |
1690 | } else { | |
1691 | assert(l1_size == s->l1_size); | |
1692 | l1_table = s->l1_table; | |
1693 | l1_allocated = 0; | |
1694 | } | |
3b46e624 | 1695 | |
585f8587 FB |
1696 | l2_size = s->l2_size * sizeof(uint64_t); |
1697 | l2_table = qemu_malloc(l2_size); | |
585f8587 FB |
1698 | l1_modified = 0; |
1699 | for(i = 0; i < l1_size; i++) { | |
1700 | l2_offset = l1_table[i]; | |
1701 | if (l2_offset) { | |
1702 | old_l2_offset = l2_offset; | |
1703 | l2_offset &= ~QCOW_OFLAG_COPIED; | |
1704 | l2_modified = 0; | |
1705 | if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size) | |
1706 | goto fail; | |
1707 | for(j = 0; j < s->l2_size; j++) { | |
1708 | offset = be64_to_cpu(l2_table[j]); | |
1709 | if (offset != 0) { | |
1710 | old_offset = offset; | |
1711 | offset &= ~QCOW_OFLAG_COPIED; | |
1712 | if (offset & QCOW_OFLAG_COMPRESSED) { | |
5fafdf24 | 1713 | nb_csectors = ((offset >> s->csize_shift) & |
585f8587 FB |
1714 | s->csize_mask) + 1; |
1715 | if (addend != 0) | |
1716 | update_refcount(bs, (offset & s->cluster_offset_mask) & ~511, | |
1717 | nb_csectors * 512, addend); | |
1718 | /* compressed clusters are never modified */ | |
5fafdf24 | 1719 | refcount = 2; |
585f8587 FB |
1720 | } else { |
1721 | if (addend != 0) { | |
1722 | refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend); | |
1723 | } else { | |
1724 | refcount = get_refcount(bs, offset >> s->cluster_bits); | |
1725 | } | |
1726 | } | |
1727 | ||
1728 | if (refcount == 1) { | |
1729 | offset |= QCOW_OFLAG_COPIED; | |
1730 | } | |
1731 | if (offset != old_offset) { | |
1732 | l2_table[j] = cpu_to_be64(offset); | |
1733 | l2_modified = 1; | |
1734 | } | |
1735 | } | |
1736 | } | |
1737 | if (l2_modified) { | |
5fafdf24 | 1738 | if (bdrv_pwrite(s->hd, |
585f8587 FB |
1739 | l2_offset, l2_table, l2_size) != l2_size) |
1740 | goto fail; | |
1741 | } | |
1742 | ||
1743 | if (addend != 0) { | |
1744 | refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend); | |
1745 | } else { | |
1746 | refcount = get_refcount(bs, l2_offset >> s->cluster_bits); | |
1747 | } | |
1748 | if (refcount == 1) { | |
1749 | l2_offset |= QCOW_OFLAG_COPIED; | |
1750 | } | |
1751 | if (l2_offset != old_l2_offset) { | |
1752 | l1_table[i] = l2_offset; | |
1753 | l1_modified = 1; | |
1754 | } | |
1755 | } | |
1756 | } | |
1757 | if (l1_modified) { | |
1758 | for(i = 0; i < l1_size; i++) | |
1759 | cpu_to_be64s(&l1_table[i]); | |
5fafdf24 | 1760 | if (bdrv_pwrite(s->hd, l1_table_offset, l1_table, |
585f8587 FB |
1761 | l1_size2) != l1_size2) |
1762 | goto fail; | |
1763 | for(i = 0; i < l1_size; i++) | |
1764 | be64_to_cpus(&l1_table[i]); | |
1765 | } | |
1766 | if (l1_allocated) | |
1767 | qemu_free(l1_table); | |
1768 | qemu_free(l2_table); | |
1769 | return 0; | |
1770 | fail: | |
1771 | if (l1_allocated) | |
1772 | qemu_free(l1_table); | |
1773 | qemu_free(l2_table); | |
1774 | return -EIO; | |
1775 | } | |
1776 | ||
1777 | static void qcow_free_snapshots(BlockDriverState *bs) | |
1778 | { | |
1779 | BDRVQcowState *s = bs->opaque; | |
1780 | int i; | |
1781 | ||
1782 | for(i = 0; i < s->nb_snapshots; i++) { | |
1783 | qemu_free(s->snapshots[i].name); | |
1784 | qemu_free(s->snapshots[i].id_str); | |
1785 | } | |
1786 | qemu_free(s->snapshots); | |
1787 | s->snapshots = NULL; | |
1788 | s->nb_snapshots = 0; | |
1789 | } | |
1790 | ||
1791 | static int qcow_read_snapshots(BlockDriverState *bs) | |
1792 | { | |
1793 | BDRVQcowState *s = bs->opaque; | |
1794 | QCowSnapshotHeader h; | |
1795 | QCowSnapshot *sn; | |
1796 | int i, id_str_size, name_size; | |
1797 | int64_t offset; | |
1798 | uint32_t extra_data_size; | |
1799 | ||
63c75dcd | 1800 | if (!s->nb_snapshots) { |
1801 | s->snapshots = NULL; | |
1802 | s->snapshots_size = 0; | |
1803 | return 0; | |
1804 | } | |
1805 | ||
585f8587 FB |
1806 | offset = s->snapshots_offset; |
1807 | s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot)); | |
585f8587 FB |
1808 | for(i = 0; i < s->nb_snapshots; i++) { |
1809 | offset = align_offset(offset, 8); | |
1810 | if (bdrv_pread(s->hd, offset, &h, sizeof(h)) != sizeof(h)) | |
1811 | goto fail; | |
1812 | offset += sizeof(h); | |
1813 | sn = s->snapshots + i; | |
1814 | sn->l1_table_offset = be64_to_cpu(h.l1_table_offset); | |
1815 | sn->l1_size = be32_to_cpu(h.l1_size); | |
1816 | sn->vm_state_size = be32_to_cpu(h.vm_state_size); | |
1817 | sn->date_sec = be32_to_cpu(h.date_sec); | |
1818 | sn->date_nsec = be32_to_cpu(h.date_nsec); | |
1819 | sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec); | |
1820 | extra_data_size = be32_to_cpu(h.extra_data_size); | |
1821 | ||
1822 | id_str_size = be16_to_cpu(h.id_str_size); | |
1823 | name_size = be16_to_cpu(h.name_size); | |
1824 | ||
1825 | offset += extra_data_size; | |
1826 | ||
1827 | sn->id_str = qemu_malloc(id_str_size + 1); | |
585f8587 FB |
1828 | if (bdrv_pread(s->hd, offset, sn->id_str, id_str_size) != id_str_size) |
1829 | goto fail; | |
1830 | offset += id_str_size; | |
1831 | sn->id_str[id_str_size] = '\0'; | |
1832 | ||
1833 | sn->name = qemu_malloc(name_size + 1); | |
585f8587 FB |
1834 | if (bdrv_pread(s->hd, offset, sn->name, name_size) != name_size) |
1835 | goto fail; | |
1836 | offset += name_size; | |
1837 | sn->name[name_size] = '\0'; | |
1838 | } | |
1839 | s->snapshots_size = offset - s->snapshots_offset; | |
1840 | return 0; | |
1841 | fail: | |
1842 | qcow_free_snapshots(bs); | |
1843 | return -1; | |
1844 | } | |
1845 | ||
1846 | /* add at the end of the file a new list of snapshots */ | |
1847 | static int qcow_write_snapshots(BlockDriverState *bs) | |
1848 | { | |
1849 | BDRVQcowState *s = bs->opaque; | |
1850 | QCowSnapshot *sn; | |
1851 | QCowSnapshotHeader h; | |
1852 | int i, name_size, id_str_size, snapshots_size; | |
1853 | uint64_t data64; | |
1854 | uint32_t data32; | |
1855 | int64_t offset, snapshots_offset; | |
1856 | ||
1857 | /* compute the size of the snapshots */ | |
1858 | offset = 0; | |
1859 | for(i = 0; i < s->nb_snapshots; i++) { | |
1860 | sn = s->snapshots + i; | |
1861 | offset = align_offset(offset, 8); | |
1862 | offset += sizeof(h); | |
1863 | offset += strlen(sn->id_str); | |
1864 | offset += strlen(sn->name); | |
1865 | } | |
1866 | snapshots_size = offset; | |
1867 | ||
1868 | snapshots_offset = alloc_clusters(bs, snapshots_size); | |
1869 | offset = snapshots_offset; | |
3b46e624 | 1870 | |
585f8587 FB |
1871 | for(i = 0; i < s->nb_snapshots; i++) { |
1872 | sn = s->snapshots + i; | |
1873 | memset(&h, 0, sizeof(h)); | |
1874 | h.l1_table_offset = cpu_to_be64(sn->l1_table_offset); | |
1875 | h.l1_size = cpu_to_be32(sn->l1_size); | |
1876 | h.vm_state_size = cpu_to_be32(sn->vm_state_size); | |
1877 | h.date_sec = cpu_to_be32(sn->date_sec); | |
1878 | h.date_nsec = cpu_to_be32(sn->date_nsec); | |
1879 | h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec); | |
3b46e624 | 1880 | |
585f8587 FB |
1881 | id_str_size = strlen(sn->id_str); |
1882 | name_size = strlen(sn->name); | |
1883 | h.id_str_size = cpu_to_be16(id_str_size); | |
1884 | h.name_size = cpu_to_be16(name_size); | |
1885 | offset = align_offset(offset, 8); | |
1886 | if (bdrv_pwrite(s->hd, offset, &h, sizeof(h)) != sizeof(h)) | |
1887 | goto fail; | |
1888 | offset += sizeof(h); | |
1889 | if (bdrv_pwrite(s->hd, offset, sn->id_str, id_str_size) != id_str_size) | |
1890 | goto fail; | |
1891 | offset += id_str_size; | |
1892 | if (bdrv_pwrite(s->hd, offset, sn->name, name_size) != name_size) | |
1893 | goto fail; | |
1894 | offset += name_size; | |
1895 | } | |
1896 | ||
1897 | /* update the various header fields */ | |
1898 | data64 = cpu_to_be64(snapshots_offset); | |
1899 | if (bdrv_pwrite(s->hd, offsetof(QCowHeader, snapshots_offset), | |
1900 | &data64, sizeof(data64)) != sizeof(data64)) | |
1901 | goto fail; | |
1902 | data32 = cpu_to_be32(s->nb_snapshots); | |
1903 | if (bdrv_pwrite(s->hd, offsetof(QCowHeader, nb_snapshots), | |
1904 | &data32, sizeof(data32)) != sizeof(data32)) | |
1905 | goto fail; | |
1906 | ||
1907 | /* free the old snapshot table */ | |
1908 | free_clusters(bs, s->snapshots_offset, s->snapshots_size); | |
1909 | s->snapshots_offset = snapshots_offset; | |
1910 | s->snapshots_size = snapshots_size; | |
1911 | return 0; | |
1912 | fail: | |
1913 | return -1; | |
1914 | } | |
1915 | ||
1916 | static void find_new_snapshot_id(BlockDriverState *bs, | |
1917 | char *id_str, int id_str_size) | |
1918 | { | |
1919 | BDRVQcowState *s = bs->opaque; | |
1920 | QCowSnapshot *sn; | |
1921 | int i, id, id_max = 0; | |
1922 | ||
1923 | for(i = 0; i < s->nb_snapshots; i++) { | |
1924 | sn = s->snapshots + i; | |
1925 | id = strtoul(sn->id_str, NULL, 10); | |
1926 | if (id > id_max) | |
1927 | id_max = id; | |
1928 | } | |
1929 | snprintf(id_str, id_str_size, "%d", id_max + 1); | |
1930 | } | |
1931 | ||
1932 | static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str) | |
1933 | { | |
1934 | BDRVQcowState *s = bs->opaque; | |
1935 | int i; | |
1936 | ||
1937 | for(i = 0; i < s->nb_snapshots; i++) { | |
1938 | if (!strcmp(s->snapshots[i].id_str, id_str)) | |
1939 | return i; | |
1940 | } | |
1941 | return -1; | |
1942 | } | |
1943 | ||
1944 | static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name) | |
1945 | { | |
1946 | BDRVQcowState *s = bs->opaque; | |
1947 | int i, ret; | |
3b46e624 | 1948 | |
585f8587 FB |
1949 | ret = find_snapshot_by_id(bs, name); |
1950 | if (ret >= 0) | |
1951 | return ret; | |
1952 | for(i = 0; i < s->nb_snapshots; i++) { | |
1953 | if (!strcmp(s->snapshots[i].name, name)) | |
1954 | return i; | |
1955 | } | |
1956 | return -1; | |
1957 | } | |
1958 | ||
1959 | /* if no id is provided, a new one is constructed */ | |
5fafdf24 | 1960 | static int qcow_snapshot_create(BlockDriverState *bs, |
585f8587 FB |
1961 | QEMUSnapshotInfo *sn_info) |
1962 | { | |
1963 | BDRVQcowState *s = bs->opaque; | |
1964 | QCowSnapshot *snapshots1, sn1, *sn = &sn1; | |
1965 | int i, ret; | |
1966 | uint64_t *l1_table = NULL; | |
3b46e624 | 1967 | |
585f8587 FB |
1968 | memset(sn, 0, sizeof(*sn)); |
1969 | ||
1970 | if (sn_info->id_str[0] == '\0') { | |
1971 | /* compute a new id */ | |
1972 | find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str)); | |
1973 | } | |
1974 | ||
1975 | /* check that the ID is unique */ | |
1976 | if (find_snapshot_by_id(bs, sn_info->id_str) >= 0) | |
1977 | return -ENOENT; | |
1978 | ||
1979 | sn->id_str = qemu_strdup(sn_info->id_str); | |
1980 | if (!sn->id_str) | |
1981 | goto fail; | |
1982 | sn->name = qemu_strdup(sn_info->name); | |
1983 | if (!sn->name) | |
1984 | goto fail; | |
1985 | sn->vm_state_size = sn_info->vm_state_size; | |
1986 | sn->date_sec = sn_info->date_sec; | |
1987 | sn->date_nsec = sn_info->date_nsec; | |
1988 | sn->vm_clock_nsec = sn_info->vm_clock_nsec; | |
1989 | ||
1990 | ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1); | |
1991 | if (ret < 0) | |
1992 | goto fail; | |
1993 | ||
1994 | /* create the L1 table of the snapshot */ | |
1995 | sn->l1_table_offset = alloc_clusters(bs, s->l1_size * sizeof(uint64_t)); | |
1996 | sn->l1_size = s->l1_size; | |
1997 | ||
1998 | l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t)); | |
585f8587 FB |
1999 | for(i = 0; i < s->l1_size; i++) { |
2000 | l1_table[i] = cpu_to_be64(s->l1_table[i]); | |
2001 | } | |
2002 | if (bdrv_pwrite(s->hd, sn->l1_table_offset, | |
5fafdf24 | 2003 | l1_table, s->l1_size * sizeof(uint64_t)) != |
585f8587 FB |
2004 | (s->l1_size * sizeof(uint64_t))) |
2005 | goto fail; | |
2006 | qemu_free(l1_table); | |
2007 | l1_table = NULL; | |
2008 | ||
2009 | snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot)); | |
63c75dcd | 2010 | if (s->snapshots) { |
2011 | memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot)); | |
2012 | qemu_free(s->snapshots); | |
2013 | } | |
585f8587 FB |
2014 | s->snapshots = snapshots1; |
2015 | s->snapshots[s->nb_snapshots++] = *sn; | |
2016 | ||
2017 | if (qcow_write_snapshots(bs) < 0) | |
2018 | goto fail; | |
2019 | #ifdef DEBUG_ALLOC | |
2020 | check_refcounts(bs); | |
2021 | #endif | |
2022 | return 0; | |
2023 | fail: | |
2024 | qemu_free(sn->name); | |
2025 | qemu_free(l1_table); | |
2026 | return -1; | |
2027 | } | |
2028 | ||
2029 | /* copy the snapshot 'snapshot_name' into the current disk image */ | |
5fafdf24 | 2030 | static int qcow_snapshot_goto(BlockDriverState *bs, |
585f8587 FB |
2031 | const char *snapshot_id) |
2032 | { | |
2033 | BDRVQcowState *s = bs->opaque; | |
2034 | QCowSnapshot *sn; | |
2035 | int i, snapshot_index, l1_size2; | |
2036 | ||
2037 | snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id); | |
2038 | if (snapshot_index < 0) | |
2039 | return -ENOENT; | |
2040 | sn = &s->snapshots[snapshot_index]; | |
2041 | ||
2042 | if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, -1) < 0) | |
2043 | goto fail; | |
2044 | ||
2045 | if (grow_l1_table(bs, sn->l1_size) < 0) | |
2046 | goto fail; | |
2047 | ||
2048 | s->l1_size = sn->l1_size; | |
2049 | l1_size2 = s->l1_size * sizeof(uint64_t); | |
2050 | /* copy the snapshot l1 table to the current l1 table */ | |
5fafdf24 | 2051 | if (bdrv_pread(s->hd, sn->l1_table_offset, |
585f8587 FB |
2052 | s->l1_table, l1_size2) != l1_size2) |
2053 | goto fail; | |
2054 | if (bdrv_pwrite(s->hd, s->l1_table_offset, | |
2055 | s->l1_table, l1_size2) != l1_size2) | |
2056 | goto fail; | |
2057 | for(i = 0;i < s->l1_size; i++) { | |
2058 | be64_to_cpus(&s->l1_table[i]); | |
2059 | } | |
2060 | ||
2061 | if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0) | |
2062 | goto fail; | |
2063 | ||
2064 | #ifdef DEBUG_ALLOC | |
2065 | check_refcounts(bs); | |
2066 | #endif | |
2067 | return 0; | |
2068 | fail: | |
2069 | return -EIO; | |
2070 | } | |
2071 | ||
2072 | static int qcow_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) | |
2073 | { | |
2074 | BDRVQcowState *s = bs->opaque; | |
2075 | QCowSnapshot *sn; | |
2076 | int snapshot_index, ret; | |
3b46e624 | 2077 | |
585f8587 FB |
2078 | snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id); |
2079 | if (snapshot_index < 0) | |
2080 | return -ENOENT; | |
2081 | sn = &s->snapshots[snapshot_index]; | |
2082 | ||
2083 | ret = update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1); | |
2084 | if (ret < 0) | |
2085 | return ret; | |
2086 | /* must update the copied flag on the current cluster offsets */ | |
2087 | ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0); | |
2088 | if (ret < 0) | |
2089 | return ret; | |
2090 | free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t)); | |
2091 | ||
2092 | qemu_free(sn->id_str); | |
2093 | qemu_free(sn->name); | |
2094 | memmove(sn, sn + 1, (s->nb_snapshots - snapshot_index - 1) * sizeof(*sn)); | |
2095 | s->nb_snapshots--; | |
2096 | ret = qcow_write_snapshots(bs); | |
2097 | if (ret < 0) { | |
2098 | /* XXX: restore snapshot if error ? */ | |
2099 | return ret; | |
2100 | } | |
2101 | #ifdef DEBUG_ALLOC | |
2102 | check_refcounts(bs); | |
2103 | #endif | |
2104 | return 0; | |
2105 | } | |
2106 | ||
5fafdf24 | 2107 | static int qcow_snapshot_list(BlockDriverState *bs, |
585f8587 FB |
2108 | QEMUSnapshotInfo **psn_tab) |
2109 | { | |
2110 | BDRVQcowState *s = bs->opaque; | |
2111 | QEMUSnapshotInfo *sn_tab, *sn_info; | |
2112 | QCowSnapshot *sn; | |
2113 | int i; | |
2114 | ||
2115 | sn_tab = qemu_mallocz(s->nb_snapshots * sizeof(QEMUSnapshotInfo)); | |
585f8587 FB |
2116 | for(i = 0; i < s->nb_snapshots; i++) { |
2117 | sn_info = sn_tab + i; | |
2118 | sn = s->snapshots + i; | |
2119 | pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), | |
2120 | sn->id_str); | |
2121 | pstrcpy(sn_info->name, sizeof(sn_info->name), | |
2122 | sn->name); | |
2123 | sn_info->vm_state_size = sn->vm_state_size; | |
2124 | sn_info->date_sec = sn->date_sec; | |
2125 | sn_info->date_nsec = sn->date_nsec; | |
2126 | sn_info->vm_clock_nsec = sn->vm_clock_nsec; | |
2127 | } | |
2128 | *psn_tab = sn_tab; | |
2129 | return s->nb_snapshots; | |
585f8587 FB |
2130 | } |
2131 | ||
2132 | /*********************************************************/ | |
2133 | /* refcount handling */ | |
2134 | ||
2135 | static int refcount_init(BlockDriverState *bs) | |
2136 | { | |
2137 | BDRVQcowState *s = bs->opaque; | |
2138 | int ret, refcount_table_size2, i; | |
3b46e624 | 2139 | |
585f8587 | 2140 | s->refcount_block_cache = qemu_malloc(s->cluster_size); |
585f8587 FB |
2141 | refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t); |
2142 | s->refcount_table = qemu_malloc(refcount_table_size2); | |
585f8587 FB |
2143 | if (s->refcount_table_size > 0) { |
2144 | ret = bdrv_pread(s->hd, s->refcount_table_offset, | |
2145 | s->refcount_table, refcount_table_size2); | |
2146 | if (ret != refcount_table_size2) | |
2147 | goto fail; | |
2148 | for(i = 0; i < s->refcount_table_size; i++) | |
2149 | be64_to_cpus(&s->refcount_table[i]); | |
2150 | } | |
2151 | return 0; | |
2152 | fail: | |
2153 | return -ENOMEM; | |
2154 | } | |
2155 | ||
2156 | static void refcount_close(BlockDriverState *bs) | |
2157 | { | |
2158 | BDRVQcowState *s = bs->opaque; | |
2159 | qemu_free(s->refcount_block_cache); | |
2160 | qemu_free(s->refcount_table); | |
2161 | } | |
2162 | ||
2163 | ||
5fafdf24 | 2164 | static int load_refcount_block(BlockDriverState *bs, |
585f8587 FB |
2165 | int64_t refcount_block_offset) |
2166 | { | |
2167 | BDRVQcowState *s = bs->opaque; | |
2168 | int ret; | |
5fafdf24 | 2169 | ret = bdrv_pread(s->hd, refcount_block_offset, s->refcount_block_cache, |
585f8587 FB |
2170 | s->cluster_size); |
2171 | if (ret != s->cluster_size) | |
2172 | return -EIO; | |
2173 | s->refcount_block_cache_offset = refcount_block_offset; | |
2174 | return 0; | |
2175 | } | |
2176 | ||
2177 | static int get_refcount(BlockDriverState *bs, int64_t cluster_index) | |
2178 | { | |
2179 | BDRVQcowState *s = bs->opaque; | |
2180 | int refcount_table_index, block_index; | |
2181 | int64_t refcount_block_offset; | |
2182 | ||
2183 | refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT); | |
2184 | if (refcount_table_index >= s->refcount_table_size) | |
2185 | return 0; | |
2186 | refcount_block_offset = s->refcount_table[refcount_table_index]; | |
2187 | if (!refcount_block_offset) | |
2188 | return 0; | |
2189 | if (refcount_block_offset != s->refcount_block_cache_offset) { | |
2190 | /* better than nothing: return allocated if read error */ | |
2191 | if (load_refcount_block(bs, refcount_block_offset) < 0) | |
2192 | return 1; | |
2193 | } | |
5fafdf24 | 2194 | block_index = cluster_index & |
585f8587 FB |
2195 | ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1); |
2196 | return be16_to_cpu(s->refcount_block_cache[block_index]); | |
2197 | } | |
2198 | ||
2199 | /* return < 0 if error */ | |
2200 | static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size) | |
2201 | { | |
2202 | BDRVQcowState *s = bs->opaque; | |
2203 | int i, nb_clusters; | |
2204 | ||
6db6c638 AL |
2205 | nb_clusters = size_to_clusters(s, size); |
2206 | retry: | |
2207 | for(i = 0; i < nb_clusters; i++) { | |
2208 | int64_t i = s->free_cluster_index++; | |
2209 | if (get_refcount(bs, i) != 0) | |
2210 | goto retry; | |
2211 | } | |
585f8587 | 2212 | #ifdef DEBUG_ALLOC2 |
6db6c638 AL |
2213 | printf("alloc_clusters: size=%lld -> %lld\n", |
2214 | size, | |
2215 | (s->free_cluster_index - nb_clusters) << s->cluster_bits); | |
585f8587 | 2216 | #endif |
6db6c638 | 2217 | return (s->free_cluster_index - nb_clusters) << s->cluster_bits; |
585f8587 FB |
2218 | } |
2219 | ||
2220 | static int64_t alloc_clusters(BlockDriverState *bs, int64_t size) | |
2221 | { | |
2222 | int64_t offset; | |
2223 | ||
2224 | offset = alloc_clusters_noref(bs, size); | |
2225 | update_refcount(bs, offset, size, 1); | |
2226 | return offset; | |
2227 | } | |
2228 | ||
2229 | /* only used to allocate compressed sectors. We try to allocate | |
2230 | contiguous sectors. size must be <= cluster_size */ | |
2231 | static int64_t alloc_bytes(BlockDriverState *bs, int size) | |
2232 | { | |
2233 | BDRVQcowState *s = bs->opaque; | |
2234 | int64_t offset, cluster_offset; | |
2235 | int free_in_cluster; | |
3b46e624 | 2236 | |
585f8587 FB |
2237 | assert(size > 0 && size <= s->cluster_size); |
2238 | if (s->free_byte_offset == 0) { | |
2239 | s->free_byte_offset = alloc_clusters(bs, s->cluster_size); | |
2240 | } | |
2241 | redo: | |
5fafdf24 | 2242 | free_in_cluster = s->cluster_size - |
585f8587 FB |
2243 | (s->free_byte_offset & (s->cluster_size - 1)); |
2244 | if (size <= free_in_cluster) { | |
2245 | /* enough space in current cluster */ | |
2246 | offset = s->free_byte_offset; | |
2247 | s->free_byte_offset += size; | |
2248 | free_in_cluster -= size; | |
2249 | if (free_in_cluster == 0) | |
2250 | s->free_byte_offset = 0; | |
2251 | if ((offset & (s->cluster_size - 1)) != 0) | |
2252 | update_cluster_refcount(bs, offset >> s->cluster_bits, 1); | |
2253 | } else { | |
2254 | offset = alloc_clusters(bs, s->cluster_size); | |
2255 | cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1); | |
2256 | if ((cluster_offset + s->cluster_size) == offset) { | |
2257 | /* we are lucky: contiguous data */ | |
2258 | offset = s->free_byte_offset; | |
2259 | update_cluster_refcount(bs, offset >> s->cluster_bits, 1); | |
2260 | s->free_byte_offset += size; | |
2261 | } else { | |
2262 | s->free_byte_offset = offset; | |
2263 | goto redo; | |
2264 | } | |
2265 | } | |
2266 | return offset; | |
2267 | } | |
2268 | ||
5fafdf24 | 2269 | static void free_clusters(BlockDriverState *bs, |
585f8587 FB |
2270 | int64_t offset, int64_t size) |
2271 | { | |
2272 | update_refcount(bs, offset, size, -1); | |
2273 | } | |
2274 | ||
2275 | static int grow_refcount_table(BlockDriverState *bs, int min_size) | |
2276 | { | |
2277 | BDRVQcowState *s = bs->opaque; | |
2278 | int new_table_size, new_table_size2, refcount_table_clusters, i, ret; | |
2279 | uint64_t *new_table; | |
2280 | int64_t table_offset; | |
643e5399 | 2281 | uint8_t data[12]; |
23be50f1 TS |
2282 | int old_table_size; |
2283 | int64_t old_table_offset; | |
585f8587 FB |
2284 | |
2285 | if (min_size <= s->refcount_table_size) | |
2286 | return 0; | |
2287 | /* compute new table size */ | |
2288 | refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3); | |
2289 | for(;;) { | |
2290 | if (refcount_table_clusters == 0) { | |
2291 | refcount_table_clusters = 1; | |
2292 | } else { | |
2293 | refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2; | |
2294 | } | |
2295 | new_table_size = refcount_table_clusters << (s->cluster_bits - 3); | |
2296 | if (min_size <= new_table_size) | |
2297 | break; | |
2298 | } | |
15e6690a FB |
2299 | #ifdef DEBUG_ALLOC2 |
2300 | printf("grow_refcount_table from %d to %d\n", | |
2301 | s->refcount_table_size, | |
2302 | new_table_size); | |
2303 | #endif | |
585f8587 FB |
2304 | new_table_size2 = new_table_size * sizeof(uint64_t); |
2305 | new_table = qemu_mallocz(new_table_size2); | |
5fafdf24 | 2306 | memcpy(new_table, s->refcount_table, |
585f8587 FB |
2307 | s->refcount_table_size * sizeof(uint64_t)); |
2308 | for(i = 0; i < s->refcount_table_size; i++) | |
2309 | cpu_to_be64s(&new_table[i]); | |
2310 | /* Note: we cannot update the refcount now to avoid recursion */ | |
2311 | table_offset = alloc_clusters_noref(bs, new_table_size2); | |
2312 | ret = bdrv_pwrite(s->hd, table_offset, new_table, new_table_size2); | |
5fafdf24 | 2313 | if (ret != new_table_size2) |
585f8587 FB |
2314 | goto fail; |
2315 | for(i = 0; i < s->refcount_table_size; i++) | |
2316 | be64_to_cpus(&new_table[i]); | |
2317 | ||
643e5399 AL |
2318 | cpu_to_be64w((uint64_t*)data, table_offset); |
2319 | cpu_to_be32w((uint32_t*)(data + 8), refcount_table_clusters); | |
585f8587 | 2320 | if (bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_offset), |
643e5399 | 2321 | data, sizeof(data)) != sizeof(data)) |
585f8587 FB |
2322 | goto fail; |
2323 | qemu_free(s->refcount_table); | |
23be50f1 TS |
2324 | old_table_offset = s->refcount_table_offset; |
2325 | old_table_size = s->refcount_table_size; | |
585f8587 FB |
2326 | s->refcount_table = new_table; |
2327 | s->refcount_table_size = new_table_size; | |
a4080ece | 2328 | s->refcount_table_offset = table_offset; |
585f8587 FB |
2329 | |
2330 | update_refcount(bs, table_offset, new_table_size2, 1); | |
23be50f1 | 2331 | free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t)); |
585f8587 FB |
2332 | return 0; |
2333 | fail: | |
2334 | free_clusters(bs, table_offset, new_table_size2); | |
2335 | qemu_free(new_table); | |
2336 | return -EIO; | |
2337 | } | |
2338 | ||
2339 | /* addend must be 1 or -1 */ | |
2340 | /* XXX: cache several refcount block clusters ? */ | |
5fafdf24 | 2341 | static int update_cluster_refcount(BlockDriverState *bs, |
585f8587 FB |
2342 | int64_t cluster_index, |
2343 | int addend) | |
2344 | { | |
2345 | BDRVQcowState *s = bs->opaque; | |
2346 | int64_t offset, refcount_block_offset; | |
2347 | int ret, refcount_table_index, block_index, refcount; | |
2348 | uint64_t data64; | |
2349 | ||
2350 | refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT); | |
2351 | if (refcount_table_index >= s->refcount_table_size) { | |
2352 | if (addend < 0) | |
2353 | return -EINVAL; | |
2354 | ret = grow_refcount_table(bs, refcount_table_index + 1); | |
2355 | if (ret < 0) | |
2356 | return ret; | |
2357 | } | |
2358 | refcount_block_offset = s->refcount_table[refcount_table_index]; | |
2359 | if (!refcount_block_offset) { | |
2360 | if (addend < 0) | |
2361 | return -EINVAL; | |
2362 | /* create a new refcount block */ | |
2363 | /* Note: we cannot update the refcount now to avoid recursion */ | |
2364 | offset = alloc_clusters_noref(bs, s->cluster_size); | |
2365 | memset(s->refcount_block_cache, 0, s->cluster_size); | |
2366 | ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->cluster_size); | |
2367 | if (ret != s->cluster_size) | |
2368 | return -EINVAL; | |
2369 | s->refcount_table[refcount_table_index] = offset; | |
2370 | data64 = cpu_to_be64(offset); | |
5fafdf24 TS |
2371 | ret = bdrv_pwrite(s->hd, s->refcount_table_offset + |
2372 | refcount_table_index * sizeof(uint64_t), | |
585f8587 FB |
2373 | &data64, sizeof(data64)); |
2374 | if (ret != sizeof(data64)) | |
2375 | return -EINVAL; | |
2376 | ||
2377 | refcount_block_offset = offset; | |
2378 | s->refcount_block_cache_offset = offset; | |
2379 | update_refcount(bs, offset, s->cluster_size, 1); | |
2380 | } else { | |
2381 | if (refcount_block_offset != s->refcount_block_cache_offset) { | |
2382 | if (load_refcount_block(bs, refcount_block_offset) < 0) | |
2383 | return -EIO; | |
2384 | } | |
2385 | } | |
2386 | /* we can update the count and save it */ | |
5fafdf24 | 2387 | block_index = cluster_index & |
585f8587 FB |
2388 | ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1); |
2389 | refcount = be16_to_cpu(s->refcount_block_cache[block_index]); | |
2390 | refcount += addend; | |
2391 | if (refcount < 0 || refcount > 0xffff) | |
2392 | return -EINVAL; | |
2393 | if (refcount == 0 && cluster_index < s->free_cluster_index) { | |
2394 | s->free_cluster_index = cluster_index; | |
2395 | } | |
2396 | s->refcount_block_cache[block_index] = cpu_to_be16(refcount); | |
5fafdf24 TS |
2397 | if (bdrv_pwrite(s->hd, |
2398 | refcount_block_offset + (block_index << REFCOUNT_SHIFT), | |
585f8587 FB |
2399 | &s->refcount_block_cache[block_index], 2) != 2) |
2400 | return -EIO; | |
2401 | return refcount; | |
2402 | } | |
2403 | ||
5fafdf24 TS |
2404 | static void update_refcount(BlockDriverState *bs, |
2405 | int64_t offset, int64_t length, | |
585f8587 FB |
2406 | int addend) |
2407 | { | |
2408 | BDRVQcowState *s = bs->opaque; | |
2409 | int64_t start, last, cluster_offset; | |
2410 | ||
2411 | #ifdef DEBUG_ALLOC2 | |
5fafdf24 | 2412 | printf("update_refcount: offset=%lld size=%lld addend=%d\n", |
585f8587 FB |
2413 | offset, length, addend); |
2414 | #endif | |
2415 | if (length <= 0) | |
2416 | return; | |
2417 | start = offset & ~(s->cluster_size - 1); | |
2418 | last = (offset + length - 1) & ~(s->cluster_size - 1); | |
5fafdf24 | 2419 | for(cluster_offset = start; cluster_offset <= last; |
585f8587 FB |
2420 | cluster_offset += s->cluster_size) { |
2421 | update_cluster_refcount(bs, cluster_offset >> s->cluster_bits, addend); | |
2422 | } | |
2423 | } | |
2424 | ||
2425 | #ifdef DEBUG_ALLOC | |
5fafdf24 TS |
2426 | static void inc_refcounts(BlockDriverState *bs, |
2427 | uint16_t *refcount_table, | |
585f8587 FB |
2428 | int refcount_table_size, |
2429 | int64_t offset, int64_t size) | |
2430 | { | |
2431 | BDRVQcowState *s = bs->opaque; | |
2432 | int64_t start, last, cluster_offset; | |
2433 | int k; | |
3b46e624 | 2434 | |
585f8587 FB |
2435 | if (size <= 0) |
2436 | return; | |
2437 | ||
2438 | start = offset & ~(s->cluster_size - 1); | |
2439 | last = (offset + size - 1) & ~(s->cluster_size - 1); | |
5fafdf24 | 2440 | for(cluster_offset = start; cluster_offset <= last; |
585f8587 FB |
2441 | cluster_offset += s->cluster_size) { |
2442 | k = cluster_offset >> s->cluster_bits; | |
2443 | if (k < 0 || k >= refcount_table_size) { | |
2444 | printf("ERROR: invalid cluster offset=0x%llx\n", cluster_offset); | |
2445 | } else { | |
2446 | if (++refcount_table[k] == 0) { | |
2447 | printf("ERROR: overflow cluster offset=0x%llx\n", cluster_offset); | |
2448 | } | |
2449 | } | |
2450 | } | |
2451 | } | |
2452 | ||
5fafdf24 TS |
2453 | static int check_refcounts_l1(BlockDriverState *bs, |
2454 | uint16_t *refcount_table, | |
585f8587 FB |
2455 | int refcount_table_size, |
2456 | int64_t l1_table_offset, int l1_size, | |
2457 | int check_copied) | |
2458 | { | |
2459 | BDRVQcowState *s = bs->opaque; | |
2460 | uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2; | |
2461 | int l2_size, i, j, nb_csectors, refcount; | |
2462 | ||
2463 | l2_table = NULL; | |
2464 | l1_size2 = l1_size * sizeof(uint64_t); | |
2465 | ||
2466 | inc_refcounts(bs, refcount_table, refcount_table_size, | |
2467 | l1_table_offset, l1_size2); | |
2468 | ||
2469 | l1_table = qemu_malloc(l1_size2); | |
5fafdf24 | 2470 | if (bdrv_pread(s->hd, l1_table_offset, |
585f8587 FB |
2471 | l1_table, l1_size2) != l1_size2) |
2472 | goto fail; | |
2473 | for(i = 0;i < l1_size; i++) | |
2474 | be64_to_cpus(&l1_table[i]); | |
3b46e624 | 2475 | |
585f8587 FB |
2476 | l2_size = s->l2_size * sizeof(uint64_t); |
2477 | l2_table = qemu_malloc(l2_size); | |
585f8587 FB |
2478 | for(i = 0; i < l1_size; i++) { |
2479 | l2_offset = l1_table[i]; | |
2480 | if (l2_offset) { | |
2481 | if (check_copied) { | |
2482 | refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits); | |
2483 | if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) { | |
2484 | printf("ERROR OFLAG_COPIED: l2_offset=%llx refcount=%d\n", | |
2485 | l2_offset, refcount); | |
2486 | } | |
2487 | } | |
2488 | l2_offset &= ~QCOW_OFLAG_COPIED; | |
2489 | if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size) | |
2490 | goto fail; | |
2491 | for(j = 0; j < s->l2_size; j++) { | |
2492 | offset = be64_to_cpu(l2_table[j]); | |
2493 | if (offset != 0) { | |
2494 | if (offset & QCOW_OFLAG_COMPRESSED) { | |
2495 | if (offset & QCOW_OFLAG_COPIED) { | |
2496 | printf("ERROR: cluster %lld: copied flag must never be set for compressed clusters\n", | |
2497 | offset >> s->cluster_bits); | |
2498 | offset &= ~QCOW_OFLAG_COPIED; | |
2499 | } | |
5fafdf24 | 2500 | nb_csectors = ((offset >> s->csize_shift) & |
585f8587 FB |
2501 | s->csize_mask) + 1; |
2502 | offset &= s->cluster_offset_mask; | |
5fafdf24 | 2503 | inc_refcounts(bs, refcount_table, |
585f8587 FB |
2504 | refcount_table_size, |
2505 | offset & ~511, nb_csectors * 512); | |
2506 | } else { | |
2507 | if (check_copied) { | |
2508 | refcount = get_refcount(bs, (offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits); | |
2509 | if ((refcount == 1) != ((offset & QCOW_OFLAG_COPIED) != 0)) { | |
2510 | printf("ERROR OFLAG_COPIED: offset=%llx refcount=%d\n", | |
2511 | offset, refcount); | |
2512 | } | |
2513 | } | |
2514 | offset &= ~QCOW_OFLAG_COPIED; | |
5fafdf24 | 2515 | inc_refcounts(bs, refcount_table, |
585f8587 FB |
2516 | refcount_table_size, |
2517 | offset, s->cluster_size); | |
2518 | } | |
2519 | } | |
2520 | } | |
5fafdf24 | 2521 | inc_refcounts(bs, refcount_table, |
585f8587 FB |
2522 | refcount_table_size, |
2523 | l2_offset, | |
2524 | s->cluster_size); | |
2525 | } | |
2526 | } | |
2527 | qemu_free(l1_table); | |
2528 | qemu_free(l2_table); | |
2529 | return 0; | |
2530 | fail: | |
2531 | printf("ERROR: I/O error in check_refcounts_l1\n"); | |
2532 | qemu_free(l1_table); | |
2533 | qemu_free(l2_table); | |
2534 | return -EIO; | |
2535 | } | |
2536 | ||
2537 | static void check_refcounts(BlockDriverState *bs) | |
2538 | { | |
2539 | BDRVQcowState *s = bs->opaque; | |
2540 | int64_t size; | |
2541 | int nb_clusters, refcount1, refcount2, i; | |
2542 | QCowSnapshot *sn; | |
2543 | uint16_t *refcount_table; | |
2544 | ||
2545 | size = bdrv_getlength(s->hd); | |
6db6c638 | 2546 | nb_clusters = size_to_clusters(s, size); |
585f8587 | 2547 | refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t)); |
15e6690a | 2548 | |
585f8587 FB |
2549 | /* header */ |
2550 | inc_refcounts(bs, refcount_table, nb_clusters, | |
2551 | 0, s->cluster_size); | |
3b46e624 | 2552 | |
585f8587 FB |
2553 | check_refcounts_l1(bs, refcount_table, nb_clusters, |
2554 | s->l1_table_offset, s->l1_size, 1); | |
2555 | ||
2556 | /* snapshots */ | |
2557 | for(i = 0; i < s->nb_snapshots; i++) { | |
2558 | sn = s->snapshots + i; | |
2559 | check_refcounts_l1(bs, refcount_table, nb_clusters, | |
2560 | sn->l1_table_offset, sn->l1_size, 0); | |
2561 | } | |
2562 | inc_refcounts(bs, refcount_table, nb_clusters, | |
2563 | s->snapshots_offset, s->snapshots_size); | |
2564 | ||
2565 | /* refcount data */ | |
2566 | inc_refcounts(bs, refcount_table, nb_clusters, | |
5fafdf24 | 2567 | s->refcount_table_offset, |
585f8587 FB |
2568 | s->refcount_table_size * sizeof(uint64_t)); |
2569 | for(i = 0; i < s->refcount_table_size; i++) { | |
2570 | int64_t offset; | |
2571 | offset = s->refcount_table[i]; | |
2572 | if (offset != 0) { | |
2573 | inc_refcounts(bs, refcount_table, nb_clusters, | |
2574 | offset, s->cluster_size); | |
2575 | } | |
2576 | } | |
2577 | ||
2578 | /* compare ref counts */ | |
2579 | for(i = 0; i < nb_clusters; i++) { | |
2580 | refcount1 = get_refcount(bs, i); | |
2581 | refcount2 = refcount_table[i]; | |
2582 | if (refcount1 != refcount2) | |
2583 | printf("ERROR cluster %d refcount=%d reference=%d\n", | |
2584 | i, refcount1, refcount2); | |
2585 | } | |
2586 | ||
2587 | qemu_free(refcount_table); | |
2588 | } | |
2589 | ||
2590 | #if 0 | |
2591 | static void dump_refcounts(BlockDriverState *bs) | |
2592 | { | |
2593 | BDRVQcowState *s = bs->opaque; | |
2594 | int64_t nb_clusters, k, k1, size; | |
2595 | int refcount; | |
2596 | ||
2597 | size = bdrv_getlength(s->hd); | |
6db6c638 | 2598 | nb_clusters = size_to_clusters(s, size); |
585f8587 FB |
2599 | for(k = 0; k < nb_clusters;) { |
2600 | k1 = k; | |
2601 | refcount = get_refcount(bs, k); | |
2602 | k++; | |
2603 | while (k < nb_clusters && get_refcount(bs, k) == refcount) | |
2604 | k++; | |
2605 | printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1); | |
2606 | } | |
2607 | } | |
2608 | #endif | |
2609 | #endif | |
2610 | ||
8d47bb61 AL |
2611 | static int qcow_put_buffer(BlockDriverState *bs, const uint8_t *buf, |
2612 | int64_t pos, int size) | |
2613 | { | |
2614 | int growable = bs->growable; | |
2615 | ||
2616 | bs->growable = 1; | |
2617 | bdrv_pwrite(bs, pos, buf, size); | |
2618 | bs->growable = growable; | |
2619 | ||
2620 | return size; | |
2621 | } | |
2622 | ||
2623 | static int qcow_get_buffer(BlockDriverState *bs, uint8_t *buf, | |
2624 | int64_t pos, int size) | |
2625 | { | |
2626 | int growable = bs->growable; | |
2627 | int ret; | |
2628 | ||
2629 | bs->growable = 1; | |
2630 | ret = bdrv_pread(bs, pos, buf, size); | |
2631 | bs->growable = growable; | |
2632 | ||
2633 | return ret; | |
2634 | } | |
2635 | ||
585f8587 FB |
2636 | BlockDriver bdrv_qcow2 = { |
2637 | "qcow2", | |
2638 | sizeof(BDRVQcowState), | |
2639 | qcow_probe, | |
2640 | qcow_open, | |
2641 | NULL, | |
2642 | NULL, | |
2643 | qcow_close, | |
2644 | qcow_create, | |
2645 | qcow_flush, | |
2646 | qcow_is_allocated, | |
2647 | qcow_set_key, | |
2648 | qcow_make_empty, | |
2649 | ||
585f8587 FB |
2650 | .bdrv_aio_read = qcow_aio_read, |
2651 | .bdrv_aio_write = qcow_aio_write, | |
2652 | .bdrv_aio_cancel = qcow_aio_cancel, | |
ce1a14dc | 2653 | .aiocb_size = sizeof(QCowAIOCB), |
585f8587 FB |
2654 | .bdrv_write_compressed = qcow_write_compressed, |
2655 | ||
2656 | .bdrv_snapshot_create = qcow_snapshot_create, | |
2657 | .bdrv_snapshot_goto = qcow_snapshot_goto, | |
2658 | .bdrv_snapshot_delete = qcow_snapshot_delete, | |
2659 | .bdrv_snapshot_list = qcow_snapshot_list, | |
2660 | .bdrv_get_info = qcow_get_info, | |
8d47bb61 AL |
2661 | |
2662 | .bdrv_put_buffer = qcow_put_buffer, | |
2663 | .bdrv_get_buffer = qcow_get_buffer, | |
585f8587 | 2664 | }; |