Implement device tree support needed for Bamboo emulation
[qemu/mini2440/sniper_sniper_test.git] / block-qcow2.c
blobb3b5f8f090d87378c75f32862c8c35ddd0e0f6b5
1 /*
2 * Block driver for the QCOW version 2 format
4 * Copyright (c) 2004-2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu-common.h"
25 #include "block_int.h"
26 #include <zlib.h>
27 #include "aes.h"
28 #include <assert.h>
31 Differences with QCOW:
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.
37 - Size of compressed clusters is stored in sectors to reduce bit usage
38 in the cluster offsets.
39 - Support for storing additional data (such as the VM state) in the
40 snapshots.
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.
46 //#define DEBUG_ALLOC
47 //#define DEBUG_ALLOC2
49 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
50 #define QCOW_VERSION 2
52 #define QCOW_CRYPT_NONE 0
53 #define QCOW_CRYPT_AES 1
55 #define QCOW_MAX_CRYPT_CLUSTERS 32
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)
62 #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
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;
80 typedef struct __attribute__((packed)) QCowSnapshotHeader {
81 /* header is 8 byte aligned */
82 uint64_t l1_table_offset;
84 uint32_t l1_size;
85 uint16_t id_str_size;
86 uint16_t name_size;
88 uint32_t date_sec;
89 uint32_t date_nsec;
91 uint64_t vm_clock_nsec;
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;
100 #define L2_CACHE_SIZE 16
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;
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;
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;
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;
152 static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
153 static int qcow_read(BlockDriverState *bs, int64_t sector_num,
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);
160 static int update_cluster_refcount(BlockDriverState *bs,
161 int64_t cluster_index,
162 int addend);
163 static void update_refcount(BlockDriverState *bs,
164 int64_t offset, int64_t length,
165 int addend);
166 static int64_t alloc_clusters(BlockDriverState *bs, int64_t size);
167 static int64_t alloc_bytes(BlockDriverState *bs, int size);
168 static void free_clusters(BlockDriverState *bs,
169 int64_t offset, int64_t size);
170 #ifdef DEBUG_ALLOC
171 static void check_refcounts(BlockDriverState *bs);
172 #endif
174 static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
176 const QCowHeader *cow_header = (const void *)buf;
178 if (buf_size >= sizeof(QCowHeader) &&
179 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
180 be32_to_cpu(cow_header->version) == QCOW_VERSION)
181 return 100;
182 else
183 return 0;
186 static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
188 BDRVQcowState *s = bs->opaque;
189 int len, i, shift, ret;
190 QCowHeader header;
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.
196 if ((flags & BDRV_O_CACHE_DEF)) {
197 flags |= BDRV_O_CACHE_WB;
198 flags &= ~BDRV_O_CACHE_DEF;
200 ret = bdrv_file_open(&s->hd, filename, flags);
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);
219 if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
220 goto fail;
221 if (header.size <= 1 ||
222 header.cluster_bits < 9 ||
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;
240 s->refcount_table_size =
241 header.refcount_table_clusters << (s->cluster_bits - 3);
243 s->snapshots_offset = header.snapshots_offset;
244 s->nb_snapshots = header.nb_snapshots;
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));
256 if (!s->l1_table)
257 goto fail;
258 if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
259 s->l1_size * sizeof(uint64_t))
260 goto fail;
261 for(i = 0;i < s->l1_size; i++) {
262 be64_to_cpus(&s->l1_table[i]);
264 /* alloc L2 cache */
265 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
266 if (!s->l2_cache)
267 goto fail;
268 s->cluster_cache = qemu_malloc(s->cluster_size);
269 if (!s->cluster_cache)
270 goto fail;
271 /* one more sector for decompressed data alignment */
272 s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
273 + 512);
274 if (!s->cluster_data)
275 goto fail;
276 s->cluster_cache_offset = -1;
278 if (refcount_init(bs) < 0)
279 goto fail;
281 /* read the backing file name */
282 if (header.backing_file_offset != 0) {
283 len = header.backing_file_size;
284 if (len > 1023)
285 len = 1023;
286 if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
287 goto fail;
288 bs->backing_file[len] = '\0';
290 if (qcow_read_snapshots(bs) < 0)
291 goto fail;
293 #ifdef DEBUG_ALLOC
294 check_refcounts(bs);
295 #endif
296 return 0;
298 fail:
299 qcow_free_snapshots(bs);
300 refcount_close(bs);
301 qemu_free(s->l1_table);
302 qemu_free(s->l2_cache);
303 qemu_free(s->cluster_cache);
304 qemu_free(s->cluster_data);
305 bdrv_delete(s->hd);
306 return -1;
309 static int qcow_set_key(BlockDriverState *bs, const char *key)
311 BDRVQcowState *s = bs->opaque;
312 uint8_t keybuf[16];
313 int len, i;
315 memset(keybuf, 0, 16);
316 len = strlen(key);
317 if (len > 16)
318 len = 16;
319 /* XXX: we could compress the chars to 7 bits to increase
320 entropy */
321 for(i = 0;i < len;i++) {
322 keybuf[i] = key[i];
324 s->crypt_method = s->crypt_method_header;
326 if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
327 return -1;
328 if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
329 return -1;
330 #if 0
331 /* test */
333 uint8_t in[16];
334 uint8_t out[16];
335 uint8_t tmp[16];
336 for(i=0;i<16;i++)
337 in[i] = i;
338 AES_encrypt(in, tmp, &s->aes_encrypt_key);
339 AES_decrypt(tmp, out, &s->aes_decrypt_key);
340 for(i = 0; i < 16; i++)
341 printf(" %02x", tmp[i]);
342 printf("\n");
343 for(i = 0; i < 16; i++)
344 printf(" %02x", out[i]);
345 printf("\n");
347 #endif
348 return 0;
351 /* The crypt function is compatible with the linux cryptoloop
352 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
353 supported */
354 static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
355 uint8_t *out_buf, const uint8_t *in_buf,
356 int nb_sectors, int enc,
357 const AES_KEY *key)
359 union {
360 uint64_t ll[2];
361 uint8_t b[16];
362 } ivec;
363 int i;
365 for(i = 0; i < nb_sectors; i++) {
366 ivec.ll[0] = cpu_to_le64(sector_num);
367 ivec.ll[1] = 0;
368 AES_cbc_encrypt(in_buf, out_buf, 512, key,
369 ivec.b, enc);
370 sector_num++;
371 in_buf += 512;
372 out_buf += 512;
376 static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
377 uint64_t cluster_offset, int n_start, int n_end)
379 BDRVQcowState *s = bs->opaque;
380 int n, ret;
382 n = n_end - n_start;
383 if (n <= 0)
384 return 0;
385 ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
386 if (ret < 0)
387 return ret;
388 if (s->crypt_method) {
389 encrypt_sectors(s, start_sect + n_start,
390 s->cluster_data,
391 s->cluster_data, n, 1,
392 &s->aes_encrypt_key);
394 ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start,
395 s->cluster_data, n);
396 if (ret < 0)
397 return ret;
398 return 0;
401 static void l2_cache_reset(BlockDriverState *bs)
403 BDRVQcowState *s = bs->opaque;
405 memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
406 memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
407 memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
410 static inline int l2_cache_new_entry(BlockDriverState *bs)
412 BDRVQcowState *s = bs->opaque;
413 uint32_t min_count;
414 int min_index, i;
416 /* find a new entry in the least used one */
417 min_index = 0;
418 min_count = 0xffffffff;
419 for(i = 0; i < L2_CACHE_SIZE; i++) {
420 if (s->l2_cache_counts[i] < min_count) {
421 min_count = s->l2_cache_counts[i];
422 min_index = i;
425 return min_index;
428 static int64_t align_offset(int64_t offset, int n)
430 offset = (offset + n - 1) & ~(n - 1);
431 return offset;
434 static int grow_l1_table(BlockDriverState *bs, int min_size)
436 BDRVQcowState *s = bs->opaque;
437 int new_l1_size, new_l1_size2, ret, i;
438 uint64_t *new_l1_table;
439 uint64_t new_l1_table_offset;
440 uint8_t data[12];
442 new_l1_size = s->l1_size;
443 if (min_size <= new_l1_size)
444 return 0;
445 while (min_size > new_l1_size) {
446 new_l1_size = (new_l1_size * 3 + 1) / 2;
448 #ifdef DEBUG_ALLOC2
449 printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size);
450 #endif
452 new_l1_size2 = sizeof(uint64_t) * new_l1_size;
453 new_l1_table = qemu_mallocz(new_l1_size2);
454 if (!new_l1_table)
455 return -ENOMEM;
456 memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
458 /* write new table (align to cluster) */
459 new_l1_table_offset = alloc_clusters(bs, new_l1_size2);
461 for(i = 0; i < s->l1_size; i++)
462 new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
463 ret = bdrv_pwrite(s->hd, new_l1_table_offset, new_l1_table, new_l1_size2);
464 if (ret != new_l1_size2)
465 goto fail;
466 for(i = 0; i < s->l1_size; i++)
467 new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
469 /* set new table */
470 cpu_to_be32w((uint32_t*)data, new_l1_size);
471 cpu_to_be64w((uint64_t*)(data + 4), new_l1_table_offset);
472 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_size), data,
473 sizeof(data)) != sizeof(data))
474 goto fail;
475 qemu_free(s->l1_table);
476 free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
477 s->l1_table_offset = new_l1_table_offset;
478 s->l1_table = new_l1_table;
479 s->l1_size = new_l1_size;
480 return 0;
481 fail:
482 qemu_free(s->l1_table);
483 return -EIO;
487 * seek_l2_table
489 * seek l2_offset in the l2_cache table
490 * if not found, return NULL,
491 * if found,
492 * increments the l2 cache hit count of the entry,
493 * if counter overflow, divide by two all counters
494 * return the pointer to the l2 cache entry
498 static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset)
500 int i, j;
502 for(i = 0; i < L2_CACHE_SIZE; i++) {
503 if (l2_offset == s->l2_cache_offsets[i]) {
504 /* increment the hit count */
505 if (++s->l2_cache_counts[i] == 0xffffffff) {
506 for(j = 0; j < L2_CACHE_SIZE; j++) {
507 s->l2_cache_counts[j] >>= 1;
510 return s->l2_cache + (i << s->l2_bits);
513 return NULL;
517 * l2_load
519 * Loads a L2 table into memory. If the table is in the cache, the cache
520 * is used; otherwise the L2 table is loaded from the image file.
522 * Returns a pointer to the L2 table on success, or NULL if the read from
523 * the image file failed.
526 static uint64_t *l2_load(BlockDriverState *bs, uint64_t l2_offset)
528 BDRVQcowState *s = bs->opaque;
529 int min_index;
530 uint64_t *l2_table;
532 /* seek if the table for the given offset is in the cache */
534 l2_table = seek_l2_table(s, l2_offset);
535 if (l2_table != NULL)
536 return l2_table;
538 /* not found: load a new entry in the least used one */
540 min_index = l2_cache_new_entry(bs);
541 l2_table = s->l2_cache + (min_index << s->l2_bits);
542 if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
543 s->l2_size * sizeof(uint64_t))
544 return NULL;
545 s->l2_cache_offsets[min_index] = l2_offset;
546 s->l2_cache_counts[min_index] = 1;
548 return l2_table;
552 * l2_allocate
554 * Allocate a new l2 entry in the file. If l1_index points to an already
555 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
556 * table) copy the contents of the old L2 table into the newly allocated one.
557 * Otherwise the new table is initialized with zeros.
561 static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index)
563 BDRVQcowState *s = bs->opaque;
564 int min_index;
565 uint64_t old_l2_offset, tmp;
566 uint64_t *l2_table, l2_offset;
568 old_l2_offset = s->l1_table[l1_index];
570 /* allocate a new l2 entry */
572 l2_offset = alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
574 /* update the L1 entry */
576 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
578 tmp = cpu_to_be64(l2_offset | QCOW_OFLAG_COPIED);
579 if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp),
580 &tmp, sizeof(tmp)) != sizeof(tmp))
581 return NULL;
583 /* allocate a new entry in the l2 cache */
585 min_index = l2_cache_new_entry(bs);
586 l2_table = s->l2_cache + (min_index << s->l2_bits);
588 if (old_l2_offset == 0) {
589 /* if there was no old l2 table, clear the new table */
590 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
591 } else {
592 /* if there was an old l2 table, read it from the disk */
593 if (bdrv_pread(s->hd, old_l2_offset,
594 l2_table, s->l2_size * sizeof(uint64_t)) !=
595 s->l2_size * sizeof(uint64_t))
596 return NULL;
598 /* write the l2 table to the file */
599 if (bdrv_pwrite(s->hd, l2_offset,
600 l2_table, s->l2_size * sizeof(uint64_t)) !=
601 s->l2_size * sizeof(uint64_t))
602 return NULL;
604 /* update the l2 cache entry */
606 s->l2_cache_offsets[min_index] = l2_offset;
607 s->l2_cache_counts[min_index] = 1;
609 return l2_table;
612 static int size_to_clusters(BDRVQcowState *s, int64_t size)
614 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
617 static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size,
618 uint64_t *l2_table, uint64_t mask)
620 int i;
621 uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask;
623 for (i = 0; i < nb_clusters; i++)
624 if (offset + i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask))
625 break;
627 return i;
630 static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)
632 int i = 0;
634 while(nb_clusters-- && l2_table[i] == 0)
635 i++;
637 return i;
641 * get_cluster_offset
643 * For a given offset of the disk image, return cluster offset in
644 * qcow2 file.
646 * on entry, *num is the number of contiguous clusters we'd like to
647 * access following offset.
649 * on exit, *num is the number of contiguous clusters we can read.
651 * Return 1, if the offset is found
652 * Return 0, otherwise.
656 static uint64_t get_cluster_offset(BlockDriverState *bs,
657 uint64_t offset, int *num)
659 BDRVQcowState *s = bs->opaque;
660 int l1_index, l2_index;
661 uint64_t l2_offset, *l2_table, cluster_offset;
662 int l1_bits, c;
663 int index_in_cluster, nb_available, nb_needed, nb_clusters;
665 index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
666 nb_needed = *num + index_in_cluster;
668 l1_bits = s->l2_bits + s->cluster_bits;
670 /* compute how many bytes there are between the offset and
671 * the end of the l1 entry
674 nb_available = (1 << l1_bits) - (offset & ((1 << l1_bits) - 1));
676 /* compute the number of available sectors */
678 nb_available = (nb_available >> 9) + index_in_cluster;
680 cluster_offset = 0;
682 /* seek the the l2 offset in the l1 table */
684 l1_index = offset >> l1_bits;
685 if (l1_index >= s->l1_size)
686 goto out;
688 l2_offset = s->l1_table[l1_index];
690 /* seek the l2 table of the given l2 offset */
692 if (!l2_offset)
693 goto out;
695 /* load the l2 table in memory */
697 l2_offset &= ~QCOW_OFLAG_COPIED;
698 l2_table = l2_load(bs, l2_offset);
699 if (l2_table == NULL)
700 return 0;
702 /* find the cluster offset for the given disk offset */
704 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
705 cluster_offset = be64_to_cpu(l2_table[l2_index]);
706 nb_clusters = size_to_clusters(s, nb_needed << 9);
708 if (!cluster_offset) {
709 /* how many empty clusters ? */
710 c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]);
711 } else {
712 /* how many allocated clusters ? */
713 c = count_contiguous_clusters(nb_clusters, s->cluster_size,
714 &l2_table[l2_index], QCOW_OFLAG_COPIED);
717 nb_available = (c * s->cluster_sectors);
718 out:
719 if (nb_available > nb_needed)
720 nb_available = nb_needed;
722 *num = nb_available - index_in_cluster;
724 return cluster_offset & ~QCOW_OFLAG_COPIED;
728 * free_any_clusters
730 * free clusters according to its type: compressed or not
734 static void free_any_clusters(BlockDriverState *bs,
735 uint64_t cluster_offset, int nb_clusters)
737 BDRVQcowState *s = bs->opaque;
739 /* free the cluster */
741 if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
742 int nb_csectors;
743 nb_csectors = ((cluster_offset >> s->csize_shift) &
744 s->csize_mask) + 1;
745 free_clusters(bs, (cluster_offset & s->cluster_offset_mask) & ~511,
746 nb_csectors * 512);
747 return;
750 free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
752 return;
756 * get_cluster_table
758 * for a given disk offset, load (and allocate if needed)
759 * the l2 table.
761 * the l2 table offset in the qcow2 file and the cluster index
762 * in the l2 table are given to the caller.
766 static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
767 uint64_t **new_l2_table,
768 uint64_t *new_l2_offset,
769 int *new_l2_index)
771 BDRVQcowState *s = bs->opaque;
772 int l1_index, l2_index, ret;
773 uint64_t l2_offset, *l2_table;
775 /* seek the the l2 offset in the l1 table */
777 l1_index = offset >> (s->l2_bits + s->cluster_bits);
778 if (l1_index >= s->l1_size) {
779 ret = grow_l1_table(bs, l1_index + 1);
780 if (ret < 0)
781 return 0;
783 l2_offset = s->l1_table[l1_index];
785 /* seek the l2 table of the given l2 offset */
787 if (l2_offset & QCOW_OFLAG_COPIED) {
788 /* load the l2 table in memory */
789 l2_offset &= ~QCOW_OFLAG_COPIED;
790 l2_table = l2_load(bs, l2_offset);
791 if (l2_table == NULL)
792 return 0;
793 } else {
794 if (l2_offset)
795 free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
796 l2_table = l2_allocate(bs, l1_index);
797 if (l2_table == NULL)
798 return 0;
799 l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
802 /* find the cluster offset for the given disk offset */
804 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
806 *new_l2_table = l2_table;
807 *new_l2_offset = l2_offset;
808 *new_l2_index = l2_index;
810 return 1;
814 * alloc_compressed_cluster_offset
816 * For a given offset of the disk image, return cluster offset in
817 * qcow2 file.
819 * If the offset is not found, allocate a new compressed cluster.
821 * Return the cluster offset if successful,
822 * Return 0, otherwise.
826 static uint64_t alloc_compressed_cluster_offset(BlockDriverState *bs,
827 uint64_t offset,
828 int compressed_size)
830 BDRVQcowState *s = bs->opaque;
831 int l2_index, ret;
832 uint64_t l2_offset, *l2_table, cluster_offset;
833 int nb_csectors;
835 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
836 if (ret == 0)
837 return 0;
839 cluster_offset = be64_to_cpu(l2_table[l2_index]);
840 if (cluster_offset & QCOW_OFLAG_COPIED)
841 return cluster_offset & ~QCOW_OFLAG_COPIED;
843 if (cluster_offset)
844 free_any_clusters(bs, cluster_offset, 1);
846 cluster_offset = alloc_bytes(bs, compressed_size);
847 nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
848 (cluster_offset >> 9);
850 cluster_offset |= QCOW_OFLAG_COMPRESSED |
851 ((uint64_t)nb_csectors << s->csize_shift);
853 /* update L2 table */
855 /* compressed clusters never have the copied flag */
857 l2_table[l2_index] = cpu_to_be64(cluster_offset);
858 if (bdrv_pwrite(s->hd,
859 l2_offset + l2_index * sizeof(uint64_t),
860 l2_table + l2_index,
861 sizeof(uint64_t)) != sizeof(uint64_t))
862 return 0;
864 return cluster_offset;
867 typedef struct QCowL2Meta
869 uint64_t offset;
870 int n_start;
871 int nb_available;
872 int nb_clusters;
873 } QCowL2Meta;
875 static int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
876 QCowL2Meta *m)
878 BDRVQcowState *s = bs->opaque;
879 int i, j = 0, l2_index, ret;
880 uint64_t *old_cluster, start_sect, l2_offset, *l2_table;
882 if (m->nb_clusters == 0)
883 return 0;
885 if (!(old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t))))
886 return -ENOMEM;
888 /* copy content of unmodified sectors */
889 start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9;
890 if (m->n_start) {
891 ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);
892 if (ret < 0)
893 goto err;
896 if (m->nb_available & (s->cluster_sectors - 1)) {
897 uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
898 ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
899 m->nb_available - end, s->cluster_sectors);
900 if (ret < 0)
901 goto err;
904 ret = -EIO;
905 /* update L2 table */
906 if (!get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index))
907 goto err;
909 for (i = 0; i < m->nb_clusters; i++) {
910 if(l2_table[l2_index + i] != 0)
911 old_cluster[j++] = l2_table[l2_index + i];
913 l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
914 (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
917 if (bdrv_pwrite(s->hd, l2_offset + l2_index * sizeof(uint64_t),
918 l2_table + l2_index, m->nb_clusters * sizeof(uint64_t)) !=
919 m->nb_clusters * sizeof(uint64_t))
920 goto err;
922 for (i = 0; i < j; i++)
923 free_any_clusters(bs, old_cluster[i], 1);
925 ret = 0;
926 err:
927 qemu_free(old_cluster);
928 return ret;
932 * alloc_cluster_offset
934 * For a given offset of the disk image, return cluster offset in
935 * qcow2 file.
937 * If the offset is not found, allocate a new cluster.
939 * Return the cluster offset if successful,
940 * Return 0, otherwise.
944 static uint64_t alloc_cluster_offset(BlockDriverState *bs,
945 uint64_t offset,
946 int n_start, int n_end,
947 int *num, QCowL2Meta *m)
949 BDRVQcowState *s = bs->opaque;
950 int l2_index, ret;
951 uint64_t l2_offset, *l2_table, cluster_offset;
952 int nb_clusters, i = 0;
954 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
955 if (ret == 0)
956 return 0;
958 nb_clusters = size_to_clusters(s, n_end << 9);
960 nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
962 cluster_offset = be64_to_cpu(l2_table[l2_index]);
964 /* We keep all QCOW_OFLAG_COPIED clusters */
966 if (cluster_offset & QCOW_OFLAG_COPIED) {
967 nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size,
968 &l2_table[l2_index], 0);
970 cluster_offset &= ~QCOW_OFLAG_COPIED;
971 m->nb_clusters = 0;
973 goto out;
976 /* for the moment, multiple compressed clusters are not managed */
978 if (cluster_offset & QCOW_OFLAG_COMPRESSED)
979 nb_clusters = 1;
981 /* how many available clusters ? */
983 while (i < nb_clusters) {
984 i += count_contiguous_free_clusters(nb_clusters - i,
985 &l2_table[l2_index + i]);
987 cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
989 if ((cluster_offset & QCOW_OFLAG_COPIED) ||
990 (cluster_offset & QCOW_OFLAG_COMPRESSED))
991 break;
993 i += count_contiguous_clusters(nb_clusters - i, s->cluster_size,
994 &l2_table[l2_index + i], 0);
996 if(be64_to_cpu(l2_table[l2_index + i]))
997 break;
999 nb_clusters = i;
1001 /* allocate a new cluster */
1003 cluster_offset = alloc_clusters(bs, nb_clusters * s->cluster_size);
1005 /* save info needed for meta data update */
1006 m->offset = offset;
1007 m->n_start = n_start;
1008 m->nb_clusters = nb_clusters;
1010 out:
1011 m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
1013 *num = m->nb_available - n_start;
1015 return cluster_offset;
1018 static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
1019 int nb_sectors, int *pnum)
1021 uint64_t cluster_offset;
1023 *pnum = nb_sectors;
1024 cluster_offset = get_cluster_offset(bs, sector_num << 9, pnum);
1026 return (cluster_offset != 0);
1029 static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
1030 const uint8_t *buf, int buf_size)
1032 z_stream strm1, *strm = &strm1;
1033 int ret, out_len;
1035 memset(strm, 0, sizeof(*strm));
1037 strm->next_in = (uint8_t *)buf;
1038 strm->avail_in = buf_size;
1039 strm->next_out = out_buf;
1040 strm->avail_out = out_buf_size;
1042 ret = inflateInit2(strm, -12);
1043 if (ret != Z_OK)
1044 return -1;
1045 ret = inflate(strm, Z_FINISH);
1046 out_len = strm->next_out - out_buf;
1047 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
1048 out_len != out_buf_size) {
1049 inflateEnd(strm);
1050 return -1;
1052 inflateEnd(strm);
1053 return 0;
1056 static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
1058 int ret, csize, nb_csectors, sector_offset;
1059 uint64_t coffset;
1061 coffset = cluster_offset & s->cluster_offset_mask;
1062 if (s->cluster_cache_offset != coffset) {
1063 nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
1064 sector_offset = coffset & 511;
1065 csize = nb_csectors * 512 - sector_offset;
1066 ret = bdrv_read(s->hd, coffset >> 9, s->cluster_data, nb_csectors);
1067 if (ret < 0) {
1068 return -1;
1070 if (decompress_buffer(s->cluster_cache, s->cluster_size,
1071 s->cluster_data + sector_offset, csize) < 0) {
1072 return -1;
1074 s->cluster_cache_offset = coffset;
1076 return 0;
1079 /* handle reading after the end of the backing file */
1080 static int backing_read1(BlockDriverState *bs,
1081 int64_t sector_num, uint8_t *buf, int nb_sectors)
1083 int n1;
1084 if ((sector_num + nb_sectors) <= bs->total_sectors)
1085 return nb_sectors;
1086 if (sector_num >= bs->total_sectors)
1087 n1 = 0;
1088 else
1089 n1 = bs->total_sectors - sector_num;
1090 memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1));
1091 return n1;
1094 static int qcow_read(BlockDriverState *bs, int64_t sector_num,
1095 uint8_t *buf, int nb_sectors)
1097 BDRVQcowState *s = bs->opaque;
1098 int ret, index_in_cluster, n, n1;
1099 uint64_t cluster_offset;
1101 while (nb_sectors > 0) {
1102 n = nb_sectors;
1103 cluster_offset = get_cluster_offset(bs, sector_num << 9, &n);
1104 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1105 if (!cluster_offset) {
1106 if (bs->backing_hd) {
1107 /* read from the base image */
1108 n1 = backing_read1(bs->backing_hd, sector_num, buf, n);
1109 if (n1 > 0) {
1110 ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
1111 if (ret < 0)
1112 return -1;
1114 } else {
1115 memset(buf, 0, 512 * n);
1117 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
1118 if (decompress_cluster(s, cluster_offset) < 0)
1119 return -1;
1120 memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
1121 } else {
1122 ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
1123 if (ret != n * 512)
1124 return -1;
1125 if (s->crypt_method) {
1126 encrypt_sectors(s, sector_num, buf, buf, n, 0,
1127 &s->aes_decrypt_key);
1130 nb_sectors -= n;
1131 sector_num += n;
1132 buf += n * 512;
1134 return 0;
1137 static int qcow_write(BlockDriverState *bs, int64_t sector_num,
1138 const uint8_t *buf, int nb_sectors)
1140 BDRVQcowState *s = bs->opaque;
1141 int ret, index_in_cluster, n;
1142 uint64_t cluster_offset;
1143 int n_end;
1144 QCowL2Meta l2meta;
1146 while (nb_sectors > 0) {
1147 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1148 n_end = index_in_cluster + nb_sectors;
1149 if (s->crypt_method &&
1150 n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
1151 n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
1152 cluster_offset = alloc_cluster_offset(bs, sector_num << 9,
1153 index_in_cluster,
1154 n_end, &n, &l2meta);
1155 if (!cluster_offset)
1156 return -1;
1157 if (s->crypt_method) {
1158 encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,
1159 &s->aes_encrypt_key);
1160 ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512,
1161 s->cluster_data, n * 512);
1162 } else {
1163 ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
1165 if (ret != n * 512 || alloc_cluster_link_l2(bs, cluster_offset, &l2meta) < 0) {
1166 free_any_clusters(bs, cluster_offset, l2meta.nb_clusters);
1167 return -1;
1169 nb_sectors -= n;
1170 sector_num += n;
1171 buf += n * 512;
1173 s->cluster_cache_offset = -1; /* disable compressed cache */
1174 return 0;
1177 typedef struct QCowAIOCB {
1178 BlockDriverAIOCB common;
1179 int64_t sector_num;
1180 uint8_t *buf;
1181 int nb_sectors;
1182 int n;
1183 uint64_t cluster_offset;
1184 uint8_t *cluster_data;
1185 BlockDriverAIOCB *hd_aiocb;
1186 QEMUBH *bh;
1187 QCowL2Meta l2meta;
1188 } QCowAIOCB;
1190 static void qcow_aio_read_cb(void *opaque, int ret);
1191 static void qcow_aio_read_bh(void *opaque)
1193 QCowAIOCB *acb = opaque;
1194 qemu_bh_delete(acb->bh);
1195 acb->bh = NULL;
1196 qcow_aio_read_cb(opaque, 0);
1199 static int qcow_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb)
1201 if (acb->bh)
1202 return -EIO;
1204 acb->bh = qemu_bh_new(cb, acb);
1205 if (!acb->bh)
1206 return -EIO;
1208 qemu_bh_schedule(acb->bh);
1210 return 0;
1213 static void qcow_aio_read_cb(void *opaque, int ret)
1215 QCowAIOCB *acb = opaque;
1216 BlockDriverState *bs = acb->common.bs;
1217 BDRVQcowState *s = bs->opaque;
1218 int index_in_cluster, n1;
1220 acb->hd_aiocb = NULL;
1221 if (ret < 0) {
1222 fail:
1223 acb->common.cb(acb->common.opaque, ret);
1224 qemu_aio_release(acb);
1225 return;
1228 /* post process the read buffer */
1229 if (!acb->cluster_offset) {
1230 /* nothing to do */
1231 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
1232 /* nothing to do */
1233 } else {
1234 if (s->crypt_method) {
1235 encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
1236 acb->n, 0,
1237 &s->aes_decrypt_key);
1241 acb->nb_sectors -= acb->n;
1242 acb->sector_num += acb->n;
1243 acb->buf += acb->n * 512;
1245 if (acb->nb_sectors == 0) {
1246 /* request completed */
1247 acb->common.cb(acb->common.opaque, 0);
1248 qemu_aio_release(acb);
1249 return;
1252 /* prepare next AIO request */
1253 acb->n = acb->nb_sectors;
1254 acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, &acb->n);
1255 index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
1257 if (!acb->cluster_offset) {
1258 if (bs->backing_hd) {
1259 /* read from the base image */
1260 n1 = backing_read1(bs->backing_hd, acb->sector_num,
1261 acb->buf, acb->n);
1262 if (n1 > 0) {
1263 acb->hd_aiocb = bdrv_aio_read(bs->backing_hd, acb->sector_num,
1264 acb->buf, acb->n, qcow_aio_read_cb, acb);
1265 if (acb->hd_aiocb == NULL)
1266 goto fail;
1267 } else {
1268 ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
1269 if (ret < 0)
1270 goto fail;
1272 } else {
1273 /* Note: in this case, no need to wait */
1274 memset(acb->buf, 0, 512 * acb->n);
1275 ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
1276 if (ret < 0)
1277 goto fail;
1279 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
1280 /* add AIO support for compressed blocks ? */
1281 if (decompress_cluster(s, acb->cluster_offset) < 0)
1282 goto fail;
1283 memcpy(acb->buf,
1284 s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
1285 ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
1286 if (ret < 0)
1287 goto fail;
1288 } else {
1289 if ((acb->cluster_offset & 511) != 0) {
1290 ret = -EIO;
1291 goto fail;
1293 acb->hd_aiocb = bdrv_aio_read(s->hd,
1294 (acb->cluster_offset >> 9) + index_in_cluster,
1295 acb->buf, acb->n, qcow_aio_read_cb, acb);
1296 if (acb->hd_aiocb == NULL)
1297 goto fail;
1301 static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
1302 int64_t sector_num, uint8_t *buf, int nb_sectors,
1303 BlockDriverCompletionFunc *cb, void *opaque)
1305 QCowAIOCB *acb;
1307 acb = qemu_aio_get(bs, cb, opaque);
1308 if (!acb)
1309 return NULL;
1310 acb->hd_aiocb = NULL;
1311 acb->sector_num = sector_num;
1312 acb->buf = buf;
1313 acb->nb_sectors = nb_sectors;
1314 acb->n = 0;
1315 acb->cluster_offset = 0;
1316 acb->l2meta.nb_clusters = 0;
1317 return acb;
1320 static BlockDriverAIOCB *qcow_aio_read(BlockDriverState *bs,
1321 int64_t sector_num, uint8_t *buf, int nb_sectors,
1322 BlockDriverCompletionFunc *cb, void *opaque)
1324 QCowAIOCB *acb;
1326 acb = qcow_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1327 if (!acb)
1328 return NULL;
1330 qcow_aio_read_cb(acb, 0);
1331 return &acb->common;
1334 static void qcow_aio_write_cb(void *opaque, int ret)
1336 QCowAIOCB *acb = opaque;
1337 BlockDriverState *bs = acb->common.bs;
1338 BDRVQcowState *s = bs->opaque;
1339 int index_in_cluster;
1340 const uint8_t *src_buf;
1341 int n_end;
1343 acb->hd_aiocb = NULL;
1345 if (ret < 0) {
1346 fail:
1347 acb->common.cb(acb->common.opaque, ret);
1348 qemu_aio_release(acb);
1349 return;
1352 if (alloc_cluster_link_l2(bs, acb->cluster_offset, &acb->l2meta) < 0) {
1353 free_any_clusters(bs, acb->cluster_offset, acb->l2meta.nb_clusters);
1354 goto fail;
1357 acb->nb_sectors -= acb->n;
1358 acb->sector_num += acb->n;
1359 acb->buf += acb->n * 512;
1361 if (acb->nb_sectors == 0) {
1362 /* request completed */
1363 acb->common.cb(acb->common.opaque, 0);
1364 qemu_aio_release(acb);
1365 return;
1368 index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
1369 n_end = index_in_cluster + acb->nb_sectors;
1370 if (s->crypt_method &&
1371 n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
1372 n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
1374 acb->cluster_offset = alloc_cluster_offset(bs, acb->sector_num << 9,
1375 index_in_cluster,
1376 n_end, &acb->n, &acb->l2meta);
1377 if (!acb->cluster_offset || (acb->cluster_offset & 511) != 0) {
1378 ret = -EIO;
1379 goto fail;
1381 if (s->crypt_method) {
1382 if (!acb->cluster_data) {
1383 acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS *
1384 s->cluster_size);
1385 if (!acb->cluster_data) {
1386 ret = -ENOMEM;
1387 goto fail;
1390 encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
1391 acb->n, 1, &s->aes_encrypt_key);
1392 src_buf = acb->cluster_data;
1393 } else {
1394 src_buf = acb->buf;
1396 acb->hd_aiocb = bdrv_aio_write(s->hd,
1397 (acb->cluster_offset >> 9) + index_in_cluster,
1398 src_buf, acb->n,
1399 qcow_aio_write_cb, acb);
1400 if (acb->hd_aiocb == NULL)
1401 goto fail;
1404 static BlockDriverAIOCB *qcow_aio_write(BlockDriverState *bs,
1405 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1406 BlockDriverCompletionFunc *cb, void *opaque)
1408 BDRVQcowState *s = bs->opaque;
1409 QCowAIOCB *acb;
1411 s->cluster_cache_offset = -1; /* disable compressed cache */
1413 acb = qcow_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
1414 if (!acb)
1415 return NULL;
1417 qcow_aio_write_cb(acb, 0);
1418 return &acb->common;
1421 static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
1423 QCowAIOCB *acb = (QCowAIOCB *)blockacb;
1424 if (acb->hd_aiocb)
1425 bdrv_aio_cancel(acb->hd_aiocb);
1426 qemu_aio_release(acb);
1429 static void qcow_close(BlockDriverState *bs)
1431 BDRVQcowState *s = bs->opaque;
1432 qemu_free(s->l1_table);
1433 qemu_free(s->l2_cache);
1434 qemu_free(s->cluster_cache);
1435 qemu_free(s->cluster_data);
1436 refcount_close(bs);
1437 bdrv_delete(s->hd);
1440 /* XXX: use std qcow open function ? */
1441 typedef struct QCowCreateState {
1442 int cluster_size;
1443 int cluster_bits;
1444 uint16_t *refcount_block;
1445 uint64_t *refcount_table;
1446 int64_t l1_table_offset;
1447 int64_t refcount_table_offset;
1448 int64_t refcount_block_offset;
1449 } QCowCreateState;
1451 static void create_refcount_update(QCowCreateState *s,
1452 int64_t offset, int64_t size)
1454 int refcount;
1455 int64_t start, last, cluster_offset;
1456 uint16_t *p;
1458 start = offset & ~(s->cluster_size - 1);
1459 last = (offset + size - 1) & ~(s->cluster_size - 1);
1460 for(cluster_offset = start; cluster_offset <= last;
1461 cluster_offset += s->cluster_size) {
1462 p = &s->refcount_block[cluster_offset >> s->cluster_bits];
1463 refcount = be16_to_cpu(*p);
1464 refcount++;
1465 *p = cpu_to_be16(refcount);
1469 static int qcow_create(const char *filename, int64_t total_size,
1470 const char *backing_file, int flags)
1472 int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
1473 QCowHeader header;
1474 uint64_t tmp, offset;
1475 QCowCreateState s1, *s = &s1;
1477 memset(s, 0, sizeof(*s));
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;
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;
1498 if (flags & BLOCK_FLAG_ENCRYPT) {
1499 header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
1500 } else {
1501 header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
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);
1510 offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
1512 s->refcount_table = qemu_mallocz(s->cluster_size);
1513 if (!s->refcount_table)
1514 goto fail;
1515 s->refcount_block = qemu_mallocz(s->cluster_size);
1516 if (!s->refcount_block)
1517 goto fail;
1519 s->refcount_table_offset = offset;
1520 header.refcount_table_offset = cpu_to_be64(offset);
1521 header.refcount_table_clusters = cpu_to_be32(1);
1522 offset += s->cluster_size;
1524 s->refcount_table[0] = cpu_to_be64(offset);
1525 s->refcount_block_offset = offset;
1526 offset += s->cluster_size;
1528 /* update refcounts */
1529 create_refcount_update(s, 0, header_size);
1530 create_refcount_update(s, s->l1_table_offset, l1_size * sizeof(uint64_t));
1531 create_refcount_update(s, s->refcount_table_offset, s->cluster_size);
1532 create_refcount_update(s, s->refcount_block_offset, s->cluster_size);
1534 /* write all the data */
1535 write(fd, &header, sizeof(header));
1536 if (backing_file) {
1537 write(fd, backing_file, backing_filename_len);
1539 lseek(fd, s->l1_table_offset, SEEK_SET);
1540 tmp = 0;
1541 for(i = 0;i < l1_size; i++) {
1542 write(fd, &tmp, sizeof(tmp));
1544 lseek(fd, s->refcount_table_offset, SEEK_SET);
1545 write(fd, s->refcount_table, s->cluster_size);
1547 lseek(fd, s->refcount_block_offset, SEEK_SET);
1548 write(fd, s->refcount_block, s->cluster_size);
1550 qemu_free(s->refcount_table);
1551 qemu_free(s->refcount_block);
1552 close(fd);
1553 return 0;
1554 fail:
1555 qemu_free(s->refcount_table);
1556 qemu_free(s->refcount_block);
1557 close(fd);
1558 return -ENOMEM;
1561 static int qcow_make_empty(BlockDriverState *bs)
1563 #if 0
1564 /* XXX: not correct */
1565 BDRVQcowState *s = bs->opaque;
1566 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1567 int ret;
1569 memset(s->l1_table, 0, l1_length);
1570 if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
1571 return -1;
1572 ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
1573 if (ret < 0)
1574 return ret;
1576 l2_cache_reset(bs);
1577 #endif
1578 return 0;
1581 /* XXX: put compressed sectors first, then all the cluster aligned
1582 tables to avoid losing bytes in alignment */
1583 static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
1584 const uint8_t *buf, int nb_sectors)
1586 BDRVQcowState *s = bs->opaque;
1587 z_stream strm;
1588 int ret, out_len;
1589 uint8_t *out_buf;
1590 uint64_t cluster_offset;
1592 if (nb_sectors == 0) {
1593 /* align end of file to a sector boundary to ease reading with
1594 sector based I/Os */
1595 cluster_offset = bdrv_getlength(s->hd);
1596 cluster_offset = (cluster_offset + 511) & ~511;
1597 bdrv_truncate(s->hd, cluster_offset);
1598 return 0;
1601 if (nb_sectors != s->cluster_sectors)
1602 return -EINVAL;
1604 out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1605 if (!out_buf)
1606 return -ENOMEM;
1608 /* best compression, small window, no zlib header */
1609 memset(&strm, 0, sizeof(strm));
1610 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1611 Z_DEFLATED, -12,
1612 9, Z_DEFAULT_STRATEGY);
1613 if (ret != 0) {
1614 qemu_free(out_buf);
1615 return -1;
1618 strm.avail_in = s->cluster_size;
1619 strm.next_in = (uint8_t *)buf;
1620 strm.avail_out = s->cluster_size;
1621 strm.next_out = out_buf;
1623 ret = deflate(&strm, Z_FINISH);
1624 if (ret != Z_STREAM_END && ret != Z_OK) {
1625 qemu_free(out_buf);
1626 deflateEnd(&strm);
1627 return -1;
1629 out_len = strm.next_out - out_buf;
1631 deflateEnd(&strm);
1633 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1634 /* could not compress: write normal cluster */
1635 qcow_write(bs, sector_num, buf, s->cluster_sectors);
1636 } else {
1637 cluster_offset = alloc_compressed_cluster_offset(bs, sector_num << 9,
1638 out_len);
1639 if (!cluster_offset)
1640 return -1;
1641 cluster_offset &= s->cluster_offset_mask;
1642 if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
1643 qemu_free(out_buf);
1644 return -1;
1648 qemu_free(out_buf);
1649 return 0;
1652 static void qcow_flush(BlockDriverState *bs)
1654 BDRVQcowState *s = bs->opaque;
1655 bdrv_flush(s->hd);
1658 static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1660 BDRVQcowState *s = bs->opaque;
1661 bdi->cluster_size = s->cluster_size;
1662 bdi->vm_state_offset = (int64_t)s->l1_vm_state_index <<
1663 (s->cluster_bits + s->l2_bits);
1664 return 0;
1667 /*********************************************************/
1668 /* snapshot support */
1670 /* update the refcounts of snapshots and the copied flag */
1671 static int update_snapshot_refcount(BlockDriverState *bs,
1672 int64_t l1_table_offset,
1673 int l1_size,
1674 int addend)
1676 BDRVQcowState *s = bs->opaque;
1677 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
1678 int64_t old_offset, old_l2_offset;
1679 int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
1681 l2_cache_reset(bs);
1683 l2_table = NULL;
1684 l1_table = NULL;
1685 l1_size2 = l1_size * sizeof(uint64_t);
1686 l1_allocated = 0;
1687 if (l1_table_offset != s->l1_table_offset) {
1688 l1_table = qemu_malloc(l1_size2);
1689 if (!l1_table)
1690 goto fail;
1691 l1_allocated = 1;
1692 if (bdrv_pread(s->hd, l1_table_offset,
1693 l1_table, l1_size2) != l1_size2)
1694 goto fail;
1695 for(i = 0;i < l1_size; i++)
1696 be64_to_cpus(&l1_table[i]);
1697 } else {
1698 assert(l1_size == s->l1_size);
1699 l1_table = s->l1_table;
1700 l1_allocated = 0;
1703 l2_size = s->l2_size * sizeof(uint64_t);
1704 l2_table = qemu_malloc(l2_size);
1705 if (!l2_table)
1706 goto fail;
1707 l1_modified = 0;
1708 for(i = 0; i < l1_size; i++) {
1709 l2_offset = l1_table[i];
1710 if (l2_offset) {
1711 old_l2_offset = l2_offset;
1712 l2_offset &= ~QCOW_OFLAG_COPIED;
1713 l2_modified = 0;
1714 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
1715 goto fail;
1716 for(j = 0; j < s->l2_size; j++) {
1717 offset = be64_to_cpu(l2_table[j]);
1718 if (offset != 0) {
1719 old_offset = offset;
1720 offset &= ~QCOW_OFLAG_COPIED;
1721 if (offset & QCOW_OFLAG_COMPRESSED) {
1722 nb_csectors = ((offset >> s->csize_shift) &
1723 s->csize_mask) + 1;
1724 if (addend != 0)
1725 update_refcount(bs, (offset & s->cluster_offset_mask) & ~511,
1726 nb_csectors * 512, addend);
1727 /* compressed clusters are never modified */
1728 refcount = 2;
1729 } else {
1730 if (addend != 0) {
1731 refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
1732 } else {
1733 refcount = get_refcount(bs, offset >> s->cluster_bits);
1737 if (refcount == 1) {
1738 offset |= QCOW_OFLAG_COPIED;
1740 if (offset != old_offset) {
1741 l2_table[j] = cpu_to_be64(offset);
1742 l2_modified = 1;
1746 if (l2_modified) {
1747 if (bdrv_pwrite(s->hd,
1748 l2_offset, l2_table, l2_size) != l2_size)
1749 goto fail;
1752 if (addend != 0) {
1753 refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
1754 } else {
1755 refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
1757 if (refcount == 1) {
1758 l2_offset |= QCOW_OFLAG_COPIED;
1760 if (l2_offset != old_l2_offset) {
1761 l1_table[i] = l2_offset;
1762 l1_modified = 1;
1766 if (l1_modified) {
1767 for(i = 0; i < l1_size; i++)
1768 cpu_to_be64s(&l1_table[i]);
1769 if (bdrv_pwrite(s->hd, l1_table_offset, l1_table,
1770 l1_size2) != l1_size2)
1771 goto fail;
1772 for(i = 0; i < l1_size; i++)
1773 be64_to_cpus(&l1_table[i]);
1775 if (l1_allocated)
1776 qemu_free(l1_table);
1777 qemu_free(l2_table);
1778 return 0;
1779 fail:
1780 if (l1_allocated)
1781 qemu_free(l1_table);
1782 qemu_free(l2_table);
1783 return -EIO;
1786 static void qcow_free_snapshots(BlockDriverState *bs)
1788 BDRVQcowState *s = bs->opaque;
1789 int i;
1791 for(i = 0; i < s->nb_snapshots; i++) {
1792 qemu_free(s->snapshots[i].name);
1793 qemu_free(s->snapshots[i].id_str);
1795 qemu_free(s->snapshots);
1796 s->snapshots = NULL;
1797 s->nb_snapshots = 0;
1800 static int qcow_read_snapshots(BlockDriverState *bs)
1802 BDRVQcowState *s = bs->opaque;
1803 QCowSnapshotHeader h;
1804 QCowSnapshot *sn;
1805 int i, id_str_size, name_size;
1806 int64_t offset;
1807 uint32_t extra_data_size;
1809 offset = s->snapshots_offset;
1810 s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot));
1811 if (!s->snapshots)
1812 goto fail;
1813 for(i = 0; i < s->nb_snapshots; i++) {
1814 offset = align_offset(offset, 8);
1815 if (bdrv_pread(s->hd, offset, &h, sizeof(h)) != sizeof(h))
1816 goto fail;
1817 offset += sizeof(h);
1818 sn = s->snapshots + i;
1819 sn->l1_table_offset = be64_to_cpu(h.l1_table_offset);
1820 sn->l1_size = be32_to_cpu(h.l1_size);
1821 sn->vm_state_size = be32_to_cpu(h.vm_state_size);
1822 sn->date_sec = be32_to_cpu(h.date_sec);
1823 sn->date_nsec = be32_to_cpu(h.date_nsec);
1824 sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec);
1825 extra_data_size = be32_to_cpu(h.extra_data_size);
1827 id_str_size = be16_to_cpu(h.id_str_size);
1828 name_size = be16_to_cpu(h.name_size);
1830 offset += extra_data_size;
1832 sn->id_str = qemu_malloc(id_str_size + 1);
1833 if (!sn->id_str)
1834 goto fail;
1835 if (bdrv_pread(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
1836 goto fail;
1837 offset += id_str_size;
1838 sn->id_str[id_str_size] = '\0';
1840 sn->name = qemu_malloc(name_size + 1);
1841 if (!sn->name)
1842 goto fail;
1843 if (bdrv_pread(s->hd, offset, sn->name, name_size) != name_size)
1844 goto fail;
1845 offset += name_size;
1846 sn->name[name_size] = '\0';
1848 s->snapshots_size = offset - s->snapshots_offset;
1849 return 0;
1850 fail:
1851 qcow_free_snapshots(bs);
1852 return -1;
1855 /* add at the end of the file a new list of snapshots */
1856 static int qcow_write_snapshots(BlockDriverState *bs)
1858 BDRVQcowState *s = bs->opaque;
1859 QCowSnapshot *sn;
1860 QCowSnapshotHeader h;
1861 int i, name_size, id_str_size, snapshots_size;
1862 uint64_t data64;
1863 uint32_t data32;
1864 int64_t offset, snapshots_offset;
1866 /* compute the size of the snapshots */
1867 offset = 0;
1868 for(i = 0; i < s->nb_snapshots; i++) {
1869 sn = s->snapshots + i;
1870 offset = align_offset(offset, 8);
1871 offset += sizeof(h);
1872 offset += strlen(sn->id_str);
1873 offset += strlen(sn->name);
1875 snapshots_size = offset;
1877 snapshots_offset = alloc_clusters(bs, snapshots_size);
1878 offset = snapshots_offset;
1880 for(i = 0; i < s->nb_snapshots; i++) {
1881 sn = s->snapshots + i;
1882 memset(&h, 0, sizeof(h));
1883 h.l1_table_offset = cpu_to_be64(sn->l1_table_offset);
1884 h.l1_size = cpu_to_be32(sn->l1_size);
1885 h.vm_state_size = cpu_to_be32(sn->vm_state_size);
1886 h.date_sec = cpu_to_be32(sn->date_sec);
1887 h.date_nsec = cpu_to_be32(sn->date_nsec);
1888 h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
1890 id_str_size = strlen(sn->id_str);
1891 name_size = strlen(sn->name);
1892 h.id_str_size = cpu_to_be16(id_str_size);
1893 h.name_size = cpu_to_be16(name_size);
1894 offset = align_offset(offset, 8);
1895 if (bdrv_pwrite(s->hd, offset, &h, sizeof(h)) != sizeof(h))
1896 goto fail;
1897 offset += sizeof(h);
1898 if (bdrv_pwrite(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
1899 goto fail;
1900 offset += id_str_size;
1901 if (bdrv_pwrite(s->hd, offset, sn->name, name_size) != name_size)
1902 goto fail;
1903 offset += name_size;
1906 /* update the various header fields */
1907 data64 = cpu_to_be64(snapshots_offset);
1908 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, snapshots_offset),
1909 &data64, sizeof(data64)) != sizeof(data64))
1910 goto fail;
1911 data32 = cpu_to_be32(s->nb_snapshots);
1912 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, nb_snapshots),
1913 &data32, sizeof(data32)) != sizeof(data32))
1914 goto fail;
1916 /* free the old snapshot table */
1917 free_clusters(bs, s->snapshots_offset, s->snapshots_size);
1918 s->snapshots_offset = snapshots_offset;
1919 s->snapshots_size = snapshots_size;
1920 return 0;
1921 fail:
1922 return -1;
1925 static void find_new_snapshot_id(BlockDriverState *bs,
1926 char *id_str, int id_str_size)
1928 BDRVQcowState *s = bs->opaque;
1929 QCowSnapshot *sn;
1930 int i, id, id_max = 0;
1932 for(i = 0; i < s->nb_snapshots; i++) {
1933 sn = s->snapshots + i;
1934 id = strtoul(sn->id_str, NULL, 10);
1935 if (id > id_max)
1936 id_max = id;
1938 snprintf(id_str, id_str_size, "%d", id_max + 1);
1941 static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str)
1943 BDRVQcowState *s = bs->opaque;
1944 int i;
1946 for(i = 0; i < s->nb_snapshots; i++) {
1947 if (!strcmp(s->snapshots[i].id_str, id_str))
1948 return i;
1950 return -1;
1953 static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
1955 BDRVQcowState *s = bs->opaque;
1956 int i, ret;
1958 ret = find_snapshot_by_id(bs, name);
1959 if (ret >= 0)
1960 return ret;
1961 for(i = 0; i < s->nb_snapshots; i++) {
1962 if (!strcmp(s->snapshots[i].name, name))
1963 return i;
1965 return -1;
1968 /* if no id is provided, a new one is constructed */
1969 static int qcow_snapshot_create(BlockDriverState *bs,
1970 QEMUSnapshotInfo *sn_info)
1972 BDRVQcowState *s = bs->opaque;
1973 QCowSnapshot *snapshots1, sn1, *sn = &sn1;
1974 int i, ret;
1975 uint64_t *l1_table = NULL;
1977 memset(sn, 0, sizeof(*sn));
1979 if (sn_info->id_str[0] == '\0') {
1980 /* compute a new id */
1981 find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
1984 /* check that the ID is unique */
1985 if (find_snapshot_by_id(bs, sn_info->id_str) >= 0)
1986 return -ENOENT;
1988 sn->id_str = qemu_strdup(sn_info->id_str);
1989 if (!sn->id_str)
1990 goto fail;
1991 sn->name = qemu_strdup(sn_info->name);
1992 if (!sn->name)
1993 goto fail;
1994 sn->vm_state_size = sn_info->vm_state_size;
1995 sn->date_sec = sn_info->date_sec;
1996 sn->date_nsec = sn_info->date_nsec;
1997 sn->vm_clock_nsec = sn_info->vm_clock_nsec;
1999 ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
2000 if (ret < 0)
2001 goto fail;
2003 /* create the L1 table of the snapshot */
2004 sn->l1_table_offset = alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
2005 sn->l1_size = s->l1_size;
2007 l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
2008 if (!l1_table)
2009 goto fail;
2010 for(i = 0; i < s->l1_size; i++) {
2011 l1_table[i] = cpu_to_be64(s->l1_table[i]);
2013 if (bdrv_pwrite(s->hd, sn->l1_table_offset,
2014 l1_table, s->l1_size * sizeof(uint64_t)) !=
2015 (s->l1_size * sizeof(uint64_t)))
2016 goto fail;
2017 qemu_free(l1_table);
2018 l1_table = NULL;
2020 snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot));
2021 if (!snapshots1)
2022 goto fail;
2023 memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot));
2024 s->snapshots = snapshots1;
2025 s->snapshots[s->nb_snapshots++] = *sn;
2027 if (qcow_write_snapshots(bs) < 0)
2028 goto fail;
2029 #ifdef DEBUG_ALLOC
2030 check_refcounts(bs);
2031 #endif
2032 return 0;
2033 fail:
2034 qemu_free(sn->name);
2035 qemu_free(l1_table);
2036 return -1;
2039 /* copy the snapshot 'snapshot_name' into the current disk image */
2040 static int qcow_snapshot_goto(BlockDriverState *bs,
2041 const char *snapshot_id)
2043 BDRVQcowState *s = bs->opaque;
2044 QCowSnapshot *sn;
2045 int i, snapshot_index, l1_size2;
2047 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
2048 if (snapshot_index < 0)
2049 return -ENOENT;
2050 sn = &s->snapshots[snapshot_index];
2052 if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, -1) < 0)
2053 goto fail;
2055 if (grow_l1_table(bs, sn->l1_size) < 0)
2056 goto fail;
2058 s->l1_size = sn->l1_size;
2059 l1_size2 = s->l1_size * sizeof(uint64_t);
2060 /* copy the snapshot l1 table to the current l1 table */
2061 if (bdrv_pread(s->hd, sn->l1_table_offset,
2062 s->l1_table, l1_size2) != l1_size2)
2063 goto fail;
2064 if (bdrv_pwrite(s->hd, s->l1_table_offset,
2065 s->l1_table, l1_size2) != l1_size2)
2066 goto fail;
2067 for(i = 0;i < s->l1_size; i++) {
2068 be64_to_cpus(&s->l1_table[i]);
2071 if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0)
2072 goto fail;
2074 #ifdef DEBUG_ALLOC
2075 check_refcounts(bs);
2076 #endif
2077 return 0;
2078 fail:
2079 return -EIO;
2082 static int qcow_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
2084 BDRVQcowState *s = bs->opaque;
2085 QCowSnapshot *sn;
2086 int snapshot_index, ret;
2088 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
2089 if (snapshot_index < 0)
2090 return -ENOENT;
2091 sn = &s->snapshots[snapshot_index];
2093 ret = update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1);
2094 if (ret < 0)
2095 return ret;
2096 /* must update the copied flag on the current cluster offsets */
2097 ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
2098 if (ret < 0)
2099 return ret;
2100 free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t));
2102 qemu_free(sn->id_str);
2103 qemu_free(sn->name);
2104 memmove(sn, sn + 1, (s->nb_snapshots - snapshot_index - 1) * sizeof(*sn));
2105 s->nb_snapshots--;
2106 ret = qcow_write_snapshots(bs);
2107 if (ret < 0) {
2108 /* XXX: restore snapshot if error ? */
2109 return ret;
2111 #ifdef DEBUG_ALLOC
2112 check_refcounts(bs);
2113 #endif
2114 return 0;
2117 static int qcow_snapshot_list(BlockDriverState *bs,
2118 QEMUSnapshotInfo **psn_tab)
2120 BDRVQcowState *s = bs->opaque;
2121 QEMUSnapshotInfo *sn_tab, *sn_info;
2122 QCowSnapshot *sn;
2123 int i;
2125 sn_tab = qemu_mallocz(s->nb_snapshots * sizeof(QEMUSnapshotInfo));
2126 if (!sn_tab)
2127 goto fail;
2128 for(i = 0; i < s->nb_snapshots; i++) {
2129 sn_info = sn_tab + i;
2130 sn = s->snapshots + i;
2131 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
2132 sn->id_str);
2133 pstrcpy(sn_info->name, sizeof(sn_info->name),
2134 sn->name);
2135 sn_info->vm_state_size = sn->vm_state_size;
2136 sn_info->date_sec = sn->date_sec;
2137 sn_info->date_nsec = sn->date_nsec;
2138 sn_info->vm_clock_nsec = sn->vm_clock_nsec;
2140 *psn_tab = sn_tab;
2141 return s->nb_snapshots;
2142 fail:
2143 qemu_free(sn_tab);
2144 *psn_tab = NULL;
2145 return -ENOMEM;
2148 /*********************************************************/
2149 /* refcount handling */
2151 static int refcount_init(BlockDriverState *bs)
2153 BDRVQcowState *s = bs->opaque;
2154 int ret, refcount_table_size2, i;
2156 s->refcount_block_cache = qemu_malloc(s->cluster_size);
2157 if (!s->refcount_block_cache)
2158 goto fail;
2159 refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
2160 s->refcount_table = qemu_malloc(refcount_table_size2);
2161 if (!s->refcount_table)
2162 goto fail;
2163 if (s->refcount_table_size > 0) {
2164 ret = bdrv_pread(s->hd, s->refcount_table_offset,
2165 s->refcount_table, refcount_table_size2);
2166 if (ret != refcount_table_size2)
2167 goto fail;
2168 for(i = 0; i < s->refcount_table_size; i++)
2169 be64_to_cpus(&s->refcount_table[i]);
2171 return 0;
2172 fail:
2173 return -ENOMEM;
2176 static void refcount_close(BlockDriverState *bs)
2178 BDRVQcowState *s = bs->opaque;
2179 qemu_free(s->refcount_block_cache);
2180 qemu_free(s->refcount_table);
2184 static int load_refcount_block(BlockDriverState *bs,
2185 int64_t refcount_block_offset)
2187 BDRVQcowState *s = bs->opaque;
2188 int ret;
2189 ret = bdrv_pread(s->hd, refcount_block_offset, s->refcount_block_cache,
2190 s->cluster_size);
2191 if (ret != s->cluster_size)
2192 return -EIO;
2193 s->refcount_block_cache_offset = refcount_block_offset;
2194 return 0;
2197 static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
2199 BDRVQcowState *s = bs->opaque;
2200 int refcount_table_index, block_index;
2201 int64_t refcount_block_offset;
2203 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
2204 if (refcount_table_index >= s->refcount_table_size)
2205 return 0;
2206 refcount_block_offset = s->refcount_table[refcount_table_index];
2207 if (!refcount_block_offset)
2208 return 0;
2209 if (refcount_block_offset != s->refcount_block_cache_offset) {
2210 /* better than nothing: return allocated if read error */
2211 if (load_refcount_block(bs, refcount_block_offset) < 0)
2212 return 1;
2214 block_index = cluster_index &
2215 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
2216 return be16_to_cpu(s->refcount_block_cache[block_index]);
2219 /* return < 0 if error */
2220 static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
2222 BDRVQcowState *s = bs->opaque;
2223 int i, nb_clusters;
2225 nb_clusters = size_to_clusters(s, size);
2226 retry:
2227 for(i = 0; i < nb_clusters; i++) {
2228 int64_t i = s->free_cluster_index++;
2229 if (get_refcount(bs, i) != 0)
2230 goto retry;
2232 #ifdef DEBUG_ALLOC2
2233 printf("alloc_clusters: size=%lld -> %lld\n",
2234 size,
2235 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
2236 #endif
2237 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
2240 static int64_t alloc_clusters(BlockDriverState *bs, int64_t size)
2242 int64_t offset;
2244 offset = alloc_clusters_noref(bs, size);
2245 update_refcount(bs, offset, size, 1);
2246 return offset;
2249 /* only used to allocate compressed sectors. We try to allocate
2250 contiguous sectors. size must be <= cluster_size */
2251 static int64_t alloc_bytes(BlockDriverState *bs, int size)
2253 BDRVQcowState *s = bs->opaque;
2254 int64_t offset, cluster_offset;
2255 int free_in_cluster;
2257 assert(size > 0 && size <= s->cluster_size);
2258 if (s->free_byte_offset == 0) {
2259 s->free_byte_offset = alloc_clusters(bs, s->cluster_size);
2261 redo:
2262 free_in_cluster = s->cluster_size -
2263 (s->free_byte_offset & (s->cluster_size - 1));
2264 if (size <= free_in_cluster) {
2265 /* enough space in current cluster */
2266 offset = s->free_byte_offset;
2267 s->free_byte_offset += size;
2268 free_in_cluster -= size;
2269 if (free_in_cluster == 0)
2270 s->free_byte_offset = 0;
2271 if ((offset & (s->cluster_size - 1)) != 0)
2272 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
2273 } else {
2274 offset = alloc_clusters(bs, s->cluster_size);
2275 cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
2276 if ((cluster_offset + s->cluster_size) == offset) {
2277 /* we are lucky: contiguous data */
2278 offset = s->free_byte_offset;
2279 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
2280 s->free_byte_offset += size;
2281 } else {
2282 s->free_byte_offset = offset;
2283 goto redo;
2286 return offset;
2289 static void free_clusters(BlockDriverState *bs,
2290 int64_t offset, int64_t size)
2292 update_refcount(bs, offset, size, -1);
2295 static int grow_refcount_table(BlockDriverState *bs, int min_size)
2297 BDRVQcowState *s = bs->opaque;
2298 int new_table_size, new_table_size2, refcount_table_clusters, i, ret;
2299 uint64_t *new_table;
2300 int64_t table_offset;
2301 uint8_t data[12];
2302 int old_table_size;
2303 int64_t old_table_offset;
2305 if (min_size <= s->refcount_table_size)
2306 return 0;
2307 /* compute new table size */
2308 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
2309 for(;;) {
2310 if (refcount_table_clusters == 0) {
2311 refcount_table_clusters = 1;
2312 } else {
2313 refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
2315 new_table_size = refcount_table_clusters << (s->cluster_bits - 3);
2316 if (min_size <= new_table_size)
2317 break;
2319 #ifdef DEBUG_ALLOC2
2320 printf("grow_refcount_table from %d to %d\n",
2321 s->refcount_table_size,
2322 new_table_size);
2323 #endif
2324 new_table_size2 = new_table_size * sizeof(uint64_t);
2325 new_table = qemu_mallocz(new_table_size2);
2326 if (!new_table)
2327 return -ENOMEM;
2328 memcpy(new_table, s->refcount_table,
2329 s->refcount_table_size * sizeof(uint64_t));
2330 for(i = 0; i < s->refcount_table_size; i++)
2331 cpu_to_be64s(&new_table[i]);
2332 /* Note: we cannot update the refcount now to avoid recursion */
2333 table_offset = alloc_clusters_noref(bs, new_table_size2);
2334 ret = bdrv_pwrite(s->hd, table_offset, new_table, new_table_size2);
2335 if (ret != new_table_size2)
2336 goto fail;
2337 for(i = 0; i < s->refcount_table_size; i++)
2338 be64_to_cpus(&new_table[i]);
2340 cpu_to_be64w((uint64_t*)data, table_offset);
2341 cpu_to_be32w((uint32_t*)(data + 8), refcount_table_clusters);
2342 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_offset),
2343 data, sizeof(data)) != sizeof(data))
2344 goto fail;
2345 qemu_free(s->refcount_table);
2346 old_table_offset = s->refcount_table_offset;
2347 old_table_size = s->refcount_table_size;
2348 s->refcount_table = new_table;
2349 s->refcount_table_size = new_table_size;
2350 s->refcount_table_offset = table_offset;
2352 update_refcount(bs, table_offset, new_table_size2, 1);
2353 free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
2354 return 0;
2355 fail:
2356 free_clusters(bs, table_offset, new_table_size2);
2357 qemu_free(new_table);
2358 return -EIO;
2361 /* addend must be 1 or -1 */
2362 /* XXX: cache several refcount block clusters ? */
2363 static int update_cluster_refcount(BlockDriverState *bs,
2364 int64_t cluster_index,
2365 int addend)
2367 BDRVQcowState *s = bs->opaque;
2368 int64_t offset, refcount_block_offset;
2369 int ret, refcount_table_index, block_index, refcount;
2370 uint64_t data64;
2372 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
2373 if (refcount_table_index >= s->refcount_table_size) {
2374 if (addend < 0)
2375 return -EINVAL;
2376 ret = grow_refcount_table(bs, refcount_table_index + 1);
2377 if (ret < 0)
2378 return ret;
2380 refcount_block_offset = s->refcount_table[refcount_table_index];
2381 if (!refcount_block_offset) {
2382 if (addend < 0)
2383 return -EINVAL;
2384 /* create a new refcount block */
2385 /* Note: we cannot update the refcount now to avoid recursion */
2386 offset = alloc_clusters_noref(bs, s->cluster_size);
2387 memset(s->refcount_block_cache, 0, s->cluster_size);
2388 ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->cluster_size);
2389 if (ret != s->cluster_size)
2390 return -EINVAL;
2391 s->refcount_table[refcount_table_index] = offset;
2392 data64 = cpu_to_be64(offset);
2393 ret = bdrv_pwrite(s->hd, s->refcount_table_offset +
2394 refcount_table_index * sizeof(uint64_t),
2395 &data64, sizeof(data64));
2396 if (ret != sizeof(data64))
2397 return -EINVAL;
2399 refcount_block_offset = offset;
2400 s->refcount_block_cache_offset = offset;
2401 update_refcount(bs, offset, s->cluster_size, 1);
2402 } else {
2403 if (refcount_block_offset != s->refcount_block_cache_offset) {
2404 if (load_refcount_block(bs, refcount_block_offset) < 0)
2405 return -EIO;
2408 /* we can update the count and save it */
2409 block_index = cluster_index &
2410 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
2411 refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
2412 refcount += addend;
2413 if (refcount < 0 || refcount > 0xffff)
2414 return -EINVAL;
2415 if (refcount == 0 && cluster_index < s->free_cluster_index) {
2416 s->free_cluster_index = cluster_index;
2418 s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
2419 if (bdrv_pwrite(s->hd,
2420 refcount_block_offset + (block_index << REFCOUNT_SHIFT),
2421 &s->refcount_block_cache[block_index], 2) != 2)
2422 return -EIO;
2423 return refcount;
2426 static void update_refcount(BlockDriverState *bs,
2427 int64_t offset, int64_t length,
2428 int addend)
2430 BDRVQcowState *s = bs->opaque;
2431 int64_t start, last, cluster_offset;
2433 #ifdef DEBUG_ALLOC2
2434 printf("update_refcount: offset=%lld size=%lld addend=%d\n",
2435 offset, length, addend);
2436 #endif
2437 if (length <= 0)
2438 return;
2439 start = offset & ~(s->cluster_size - 1);
2440 last = (offset + length - 1) & ~(s->cluster_size - 1);
2441 for(cluster_offset = start; cluster_offset <= last;
2442 cluster_offset += s->cluster_size) {
2443 update_cluster_refcount(bs, cluster_offset >> s->cluster_bits, addend);
2447 #ifdef DEBUG_ALLOC
2448 static void inc_refcounts(BlockDriverState *bs,
2449 uint16_t *refcount_table,
2450 int refcount_table_size,
2451 int64_t offset, int64_t size)
2453 BDRVQcowState *s = bs->opaque;
2454 int64_t start, last, cluster_offset;
2455 int k;
2457 if (size <= 0)
2458 return;
2460 start = offset & ~(s->cluster_size - 1);
2461 last = (offset + size - 1) & ~(s->cluster_size - 1);
2462 for(cluster_offset = start; cluster_offset <= last;
2463 cluster_offset += s->cluster_size) {
2464 k = cluster_offset >> s->cluster_bits;
2465 if (k < 0 || k >= refcount_table_size) {
2466 printf("ERROR: invalid cluster offset=0x%llx\n", cluster_offset);
2467 } else {
2468 if (++refcount_table[k] == 0) {
2469 printf("ERROR: overflow cluster offset=0x%llx\n", cluster_offset);
2475 static int check_refcounts_l1(BlockDriverState *bs,
2476 uint16_t *refcount_table,
2477 int refcount_table_size,
2478 int64_t l1_table_offset, int l1_size,
2479 int check_copied)
2481 BDRVQcowState *s = bs->opaque;
2482 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2;
2483 int l2_size, i, j, nb_csectors, refcount;
2485 l2_table = NULL;
2486 l1_size2 = l1_size * sizeof(uint64_t);
2488 inc_refcounts(bs, refcount_table, refcount_table_size,
2489 l1_table_offset, l1_size2);
2491 l1_table = qemu_malloc(l1_size2);
2492 if (!l1_table)
2493 goto fail;
2494 if (bdrv_pread(s->hd, l1_table_offset,
2495 l1_table, l1_size2) != l1_size2)
2496 goto fail;
2497 for(i = 0;i < l1_size; i++)
2498 be64_to_cpus(&l1_table[i]);
2500 l2_size = s->l2_size * sizeof(uint64_t);
2501 l2_table = qemu_malloc(l2_size);
2502 if (!l2_table)
2503 goto fail;
2504 for(i = 0; i < l1_size; i++) {
2505 l2_offset = l1_table[i];
2506 if (l2_offset) {
2507 if (check_copied) {
2508 refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
2509 if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
2510 printf("ERROR OFLAG_COPIED: l2_offset=%llx refcount=%d\n",
2511 l2_offset, refcount);
2514 l2_offset &= ~QCOW_OFLAG_COPIED;
2515 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
2516 goto fail;
2517 for(j = 0; j < s->l2_size; j++) {
2518 offset = be64_to_cpu(l2_table[j]);
2519 if (offset != 0) {
2520 if (offset & QCOW_OFLAG_COMPRESSED) {
2521 if (offset & QCOW_OFLAG_COPIED) {
2522 printf("ERROR: cluster %lld: copied flag must never be set for compressed clusters\n",
2523 offset >> s->cluster_bits);
2524 offset &= ~QCOW_OFLAG_COPIED;
2526 nb_csectors = ((offset >> s->csize_shift) &
2527 s->csize_mask) + 1;
2528 offset &= s->cluster_offset_mask;
2529 inc_refcounts(bs, refcount_table,
2530 refcount_table_size,
2531 offset & ~511, nb_csectors * 512);
2532 } else {
2533 if (check_copied) {
2534 refcount = get_refcount(bs, (offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
2535 if ((refcount == 1) != ((offset & QCOW_OFLAG_COPIED) != 0)) {
2536 printf("ERROR OFLAG_COPIED: offset=%llx refcount=%d\n",
2537 offset, refcount);
2540 offset &= ~QCOW_OFLAG_COPIED;
2541 inc_refcounts(bs, refcount_table,
2542 refcount_table_size,
2543 offset, s->cluster_size);
2547 inc_refcounts(bs, refcount_table,
2548 refcount_table_size,
2549 l2_offset,
2550 s->cluster_size);
2553 qemu_free(l1_table);
2554 qemu_free(l2_table);
2555 return 0;
2556 fail:
2557 printf("ERROR: I/O error in check_refcounts_l1\n");
2558 qemu_free(l1_table);
2559 qemu_free(l2_table);
2560 return -EIO;
2563 static void check_refcounts(BlockDriverState *bs)
2565 BDRVQcowState *s = bs->opaque;
2566 int64_t size;
2567 int nb_clusters, refcount1, refcount2, i;
2568 QCowSnapshot *sn;
2569 uint16_t *refcount_table;
2571 size = bdrv_getlength(s->hd);
2572 nb_clusters = size_to_clusters(s, size);
2573 refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
2575 /* header */
2576 inc_refcounts(bs, refcount_table, nb_clusters,
2577 0, s->cluster_size);
2579 check_refcounts_l1(bs, refcount_table, nb_clusters,
2580 s->l1_table_offset, s->l1_size, 1);
2582 /* snapshots */
2583 for(i = 0; i < s->nb_snapshots; i++) {
2584 sn = s->snapshots + i;
2585 check_refcounts_l1(bs, refcount_table, nb_clusters,
2586 sn->l1_table_offset, sn->l1_size, 0);
2588 inc_refcounts(bs, refcount_table, nb_clusters,
2589 s->snapshots_offset, s->snapshots_size);
2591 /* refcount data */
2592 inc_refcounts(bs, refcount_table, nb_clusters,
2593 s->refcount_table_offset,
2594 s->refcount_table_size * sizeof(uint64_t));
2595 for(i = 0; i < s->refcount_table_size; i++) {
2596 int64_t offset;
2597 offset = s->refcount_table[i];
2598 if (offset != 0) {
2599 inc_refcounts(bs, refcount_table, nb_clusters,
2600 offset, s->cluster_size);
2604 /* compare ref counts */
2605 for(i = 0; i < nb_clusters; i++) {
2606 refcount1 = get_refcount(bs, i);
2607 refcount2 = refcount_table[i];
2608 if (refcount1 != refcount2)
2609 printf("ERROR cluster %d refcount=%d reference=%d\n",
2610 i, refcount1, refcount2);
2613 qemu_free(refcount_table);
2616 #if 0
2617 static void dump_refcounts(BlockDriverState *bs)
2619 BDRVQcowState *s = bs->opaque;
2620 int64_t nb_clusters, k, k1, size;
2621 int refcount;
2623 size = bdrv_getlength(s->hd);
2624 nb_clusters = size_to_clusters(s, size);
2625 for(k = 0; k < nb_clusters;) {
2626 k1 = k;
2627 refcount = get_refcount(bs, k);
2628 k++;
2629 while (k < nb_clusters && get_refcount(bs, k) == refcount)
2630 k++;
2631 printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1);
2634 #endif
2635 #endif
2637 BlockDriver bdrv_qcow2 = {
2638 "qcow2",
2639 sizeof(BDRVQcowState),
2640 qcow_probe,
2641 qcow_open,
2642 NULL,
2643 NULL,
2644 qcow_close,
2645 qcow_create,
2646 qcow_flush,
2647 qcow_is_allocated,
2648 qcow_set_key,
2649 qcow_make_empty,
2651 .bdrv_aio_read = qcow_aio_read,
2652 .bdrv_aio_write = qcow_aio_write,
2653 .bdrv_aio_cancel = qcow_aio_cancel,
2654 .aiocb_size = sizeof(QCowAIOCB),
2655 .bdrv_write_compressed = qcow_write_compressed,
2657 .bdrv_snapshot_create = qcow_snapshot_create,
2658 .bdrv_snapshot_goto = qcow_snapshot_goto,
2659 .bdrv_snapshot_delete = qcow_snapshot_delete,
2660 .bdrv_snapshot_list = qcow_snapshot_list,
2661 .bdrv_get_info = qcow_get_info,