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
24 #include "qemu-common.h"
25 #include "block_int.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
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.
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 */
65 #define offsetof(type, field) ((size_t) &((type *)0)->field)
68 typedef struct QCowHeader {
71 uint64_t backing_file_offset;
72 uint32_t backing_file_size;
73 uint32_t cluster_bits;
74 uint64_t size; /* in bytes */
75 uint32_t crypt_method;
76 uint32_t l1_size; /* XXX: save number of clusters instead ? */
77 uint64_t l1_table_offset;
78 uint64_t refcount_table_offset;
79 uint32_t refcount_table_clusters;
80 uint32_t nb_snapshots;
81 uint64_t snapshots_offset;
84 typedef struct __attribute__((packed)) QCowSnapshotHeader {
85 /* header is 8 byte aligned */
86 uint64_t l1_table_offset;
95 uint64_t vm_clock_nsec;
97 uint32_t vm_state_size;
98 uint32_t extra_data_size; /* for extension */
99 /* extra data follows */
102 } QCowSnapshotHeader;
104 #define L2_CACHE_SIZE 16
106 typedef struct QCowSnapshot {
107 uint64_t l1_table_offset;
111 uint32_t vm_state_size;
114 uint64_t vm_clock_nsec;
117 typedef struct BDRVQcowState {
118 BlockDriverState *hd;
125 int l1_vm_state_index;
128 uint64_t cluster_offset_mask;
129 uint64_t l1_table_offset;
132 uint64_t l2_cache_offsets[L2_CACHE_SIZE];
133 uint32_t l2_cache_counts[L2_CACHE_SIZE];
134 uint8_t *cluster_cache;
135 uint8_t *cluster_data;
136 uint64_t cluster_cache_offset;
138 uint64_t *refcount_table;
139 uint64_t refcount_table_offset;
140 uint32_t refcount_table_size;
141 uint64_t refcount_block_cache_offset;
142 uint16_t *refcount_block_cache;
143 int64_t free_cluster_index;
144 int64_t free_byte_offset;
146 uint32_t crypt_method; /* current crypt method, 0 if no key yet */
147 uint32_t crypt_method_header;
148 AES_KEY aes_encrypt_key;
149 AES_KEY aes_decrypt_key;
150 uint64_t snapshots_offset;
153 QCowSnapshot *snapshots;
156 static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
157 static int qcow_read(BlockDriverState *bs, int64_t sector_num,
158 uint8_t *buf, int nb_sectors);
159 static int qcow_read_snapshots(BlockDriverState *bs);
160 static void qcow_free_snapshots(BlockDriverState *bs);
161 static int refcount_init(BlockDriverState *bs);
162 static void refcount_close(BlockDriverState *bs);
163 static int get_refcount(BlockDriverState *bs, int64_t cluster_index);
164 static int update_cluster_refcount(BlockDriverState *bs,
165 int64_t cluster_index,
167 static void update_refcount(BlockDriverState *bs,
168 int64_t offset, int64_t length,
170 static int64_t alloc_clusters(BlockDriverState *bs, int64_t size);
171 static int64_t alloc_bytes(BlockDriverState *bs, int size);
172 static void free_clusters(BlockDriverState *bs,
173 int64_t offset, int64_t size);
175 static void check_refcounts(BlockDriverState *bs);
178 static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
180 const QCowHeader *cow_header = (const void *)buf;
182 if (buf_size >= sizeof(QCowHeader) &&
183 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
184 be32_to_cpu(cow_header->version) == QCOW_VERSION)
190 static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
192 BDRVQcowState *s = bs->opaque;
193 int len, i, shift, ret;
196 ret = bdrv_file_open(&s->hd, filename, flags);
199 if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header))
201 be32_to_cpus(&header.magic);
202 be32_to_cpus(&header.version);
203 be64_to_cpus(&header.backing_file_offset);
204 be32_to_cpus(&header.backing_file_size);
205 be64_to_cpus(&header.size);
206 be32_to_cpus(&header.cluster_bits);
207 be32_to_cpus(&header.crypt_method);
208 be64_to_cpus(&header.l1_table_offset);
209 be32_to_cpus(&header.l1_size);
210 be64_to_cpus(&header.refcount_table_offset);
211 be32_to_cpus(&header.refcount_table_clusters);
212 be64_to_cpus(&header.snapshots_offset);
213 be32_to_cpus(&header.nb_snapshots);
215 if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
217 if (header.size <= 1 ||
218 header.cluster_bits < 9 ||
219 header.cluster_bits > 16)
221 if (header.crypt_method > QCOW_CRYPT_AES)
223 s->crypt_method_header = header.crypt_method;
224 if (s->crypt_method_header)
226 s->cluster_bits = header.cluster_bits;
227 s->cluster_size = 1 << s->cluster_bits;
228 s->cluster_sectors = 1 << (s->cluster_bits - 9);
229 s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
230 s->l2_size = 1 << s->l2_bits;
231 bs->total_sectors = header.size / 512;
232 s->csize_shift = (62 - (s->cluster_bits - 8));
233 s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
234 s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
235 s->refcount_table_offset = header.refcount_table_offset;
236 s->refcount_table_size =
237 header.refcount_table_clusters << (s->cluster_bits - 3);
239 s->snapshots_offset = header.snapshots_offset;
240 s->nb_snapshots = header.nb_snapshots;
242 /* read the level 1 table */
243 s->l1_size = header.l1_size;
244 shift = s->cluster_bits + s->l2_bits;
245 s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift;
246 /* the L1 table must contain at least enough entries to put
248 if (s->l1_size < s->l1_vm_state_index)
250 s->l1_table_offset = header.l1_table_offset;
251 s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
254 if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
255 s->l1_size * sizeof(uint64_t))
257 for(i = 0;i < s->l1_size; i++) {
258 be64_to_cpus(&s->l1_table[i]);
261 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
264 s->cluster_cache = qemu_malloc(s->cluster_size);
265 if (!s->cluster_cache)
267 /* one more sector for decompressed data alignment */
268 s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
270 if (!s->cluster_data)
272 s->cluster_cache_offset = -1;
274 if (refcount_init(bs) < 0)
277 /* read the backing file name */
278 if (header.backing_file_offset != 0) {
279 len = header.backing_file_size;
282 if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
284 bs->backing_file[len] = '\0';
286 if (qcow_read_snapshots(bs) < 0)
295 qcow_free_snapshots(bs);
297 qemu_free(s->l1_table);
298 qemu_free(s->l2_cache);
299 qemu_free(s->cluster_cache);
300 qemu_free(s->cluster_data);
305 static int qcow_set_key(BlockDriverState *bs, const char *key)
307 BDRVQcowState *s = bs->opaque;
311 memset(keybuf, 0, 16);
315 /* XXX: we could compress the chars to 7 bits to increase
317 for(i = 0;i < len;i++) {
320 s->crypt_method = s->crypt_method_header;
322 if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
324 if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
334 AES_encrypt(in, tmp, &s->aes_encrypt_key);
335 AES_decrypt(tmp, out, &s->aes_decrypt_key);
336 for(i = 0; i < 16; i++)
337 printf(" %02x", tmp[i]);
339 for(i = 0; i < 16; i++)
340 printf(" %02x", out[i]);
347 /* The crypt function is compatible with the linux cryptoloop
348 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
350 static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
351 uint8_t *out_buf, const uint8_t *in_buf,
352 int nb_sectors, int enc,
361 for(i = 0; i < nb_sectors; i++) {
362 ivec.ll[0] = cpu_to_le64(sector_num);
364 AES_cbc_encrypt(in_buf, out_buf, 512, key,
372 static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
373 uint64_t cluster_offset, int n_start, int n_end)
375 BDRVQcowState *s = bs->opaque;
381 ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
384 if (s->crypt_method) {
385 encrypt_sectors(s, start_sect + n_start,
387 s->cluster_data, n, 1,
388 &s->aes_encrypt_key);
390 ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start,
397 static void l2_cache_reset(BlockDriverState *bs)
399 BDRVQcowState *s = bs->opaque;
401 memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
402 memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
403 memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
406 static inline int l2_cache_new_entry(BlockDriverState *bs)
408 BDRVQcowState *s = bs->opaque;
412 /* find a new entry in the least used one */
414 min_count = 0xffffffff;
415 for(i = 0; i < L2_CACHE_SIZE; i++) {
416 if (s->l2_cache_counts[i] < min_count) {
417 min_count = s->l2_cache_counts[i];
424 static int64_t align_offset(int64_t offset, int n)
426 offset = (offset + n - 1) & ~(n - 1);
430 static int grow_l1_table(BlockDriverState *bs, int min_size)
432 BDRVQcowState *s = bs->opaque;
433 int new_l1_size, new_l1_size2, ret, i;
434 uint64_t *new_l1_table;
435 uint64_t new_l1_table_offset;
439 new_l1_size = s->l1_size;
440 if (min_size <= new_l1_size)
442 while (min_size > new_l1_size) {
443 new_l1_size = (new_l1_size * 3 + 1) / 2;
446 printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size);
449 new_l1_size2 = sizeof(uint64_t) * new_l1_size;
450 new_l1_table = qemu_mallocz(new_l1_size2);
453 memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
455 /* write new table (align to cluster) */
456 new_l1_table_offset = alloc_clusters(bs, new_l1_size2);
458 for(i = 0; i < s->l1_size; i++)
459 new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
460 ret = bdrv_pwrite(s->hd, new_l1_table_offset, new_l1_table, new_l1_size2);
461 if (ret != new_l1_size2)
463 for(i = 0; i < s->l1_size; i++)
464 new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
467 data64 = cpu_to_be64(new_l1_table_offset);
468 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_table_offset),
469 &data64, sizeof(data64)) != sizeof(data64))
471 data32 = cpu_to_be32(new_l1_size);
472 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_size),
473 &data32, sizeof(data32)) != sizeof(data32))
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;
482 qemu_free(s->l1_table);
489 * seek l2_offset in the l2_cache table
490 * if not found, return NULL,
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)
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);
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;
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)
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))
545 s->l2_cache_offsets[min_index] = l2_offset;
546 s->l2_cache_counts[min_index] = 1;
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;
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))
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));
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))
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))
604 /* update the l2 cache entry */
606 s->l2_cache_offsets[min_index] = l2_offset;
607 s->l2_cache_counts[min_index] = 1;
615 * For a given offset of the disk image, return cluster offset in
618 * on entry, *num is the number of contiguous clusters we'd like to
619 * access following offset.
621 * on exit, *num is the number of contiguous clusters we can read.
623 * Return 1, if the offset is found
624 * Return 0, otherwise.
628 static uint64_t get_cluster_offset(BlockDriverState *bs,
629 uint64_t offset, int *num)
631 BDRVQcowState *s = bs->opaque;
632 int l1_index, l2_index;
633 uint64_t l2_offset, *l2_table, cluster_offset, next;
635 int index_in_cluster, nb_available, nb_needed;
637 index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
638 nb_needed = *num + index_in_cluster;
640 l1_bits = s->l2_bits + s->cluster_bits;
642 /* compute how many bytes there are between the offset and
643 * and the end of the l1 entry
646 nb_available = (1 << l1_bits) - (offset & ((1 << l1_bits) - 1));
648 /* compute the number of available sectors */
650 nb_available = (nb_available >> 9) + index_in_cluster;
654 /* seek the the l2 offset in the l1 table */
656 l1_index = offset >> l1_bits;
657 if (l1_index >= s->l1_size)
660 l2_offset = s->l1_table[l1_index];
662 /* seek the l2 table of the given l2 offset */
667 /* load the l2 table in memory */
669 l2_offset &= ~QCOW_OFLAG_COPIED;
670 l2_table = l2_load(bs, l2_offset);
671 if (l2_table == NULL)
674 /* find the cluster offset for the given disk offset */
676 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
677 cluster_offset = be64_to_cpu(l2_table[l2_index]);
678 nb_available = s->cluster_sectors;
681 if (!cluster_offset) {
683 /* how many empty clusters ? */
685 while (nb_available < nb_needed && !l2_table[l2_index]) {
687 nb_available += s->cluster_sectors;
691 /* how many allocated clusters ? */
693 cluster_offset &= ~QCOW_OFLAG_COPIED;
694 while (nb_available < nb_needed) {
695 next = be64_to_cpu(l2_table[l2_index]) & ~QCOW_OFLAG_COPIED;
696 if (next != cluster_offset + (nb_available << 9))
699 nb_available += s->cluster_sectors;
704 if (nb_available > nb_needed)
705 nb_available = nb_needed;
707 *num = nb_available - index_in_cluster;
709 return cluster_offset;
715 * free clusters according to its type: compressed or not
719 static void free_any_clusters(BlockDriverState *bs,
720 uint64_t cluster_offset, int nb_clusters)
722 BDRVQcowState *s = bs->opaque;
724 /* free the cluster */
726 if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
728 nb_csectors = ((cluster_offset >> s->csize_shift) &
730 free_clusters(bs, (cluster_offset & s->cluster_offset_mask) & ~511,
735 free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
743 * for a given disk offset, load (and allocate if needed)
746 * the l2 table offset in the qcow2 file and the cluster index
747 * in the l2 table are given to the caller.
751 static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
752 uint64_t **new_l2_table,
753 uint64_t *new_l2_offset,
756 BDRVQcowState *s = bs->opaque;
757 int l1_index, l2_index, ret;
758 uint64_t l2_offset, *l2_table;
760 /* seek the the l2 offset in the l1 table */
762 l1_index = offset >> (s->l2_bits + s->cluster_bits);
763 if (l1_index >= s->l1_size) {
764 ret = grow_l1_table(bs, l1_index + 1);
768 l2_offset = s->l1_table[l1_index];
770 /* seek the l2 table of the given l2 offset */
772 if (l2_offset & QCOW_OFLAG_COPIED) {
773 /* load the l2 table in memory */
774 l2_offset &= ~QCOW_OFLAG_COPIED;
775 l2_table = l2_load(bs, l2_offset);
776 if (l2_table == NULL)
780 free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
781 l2_table = l2_allocate(bs, l1_index);
782 if (l2_table == NULL)
784 l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
787 /* find the cluster offset for the given disk offset */
789 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
791 *new_l2_table = l2_table;
792 *new_l2_offset = l2_offset;
793 *new_l2_index = l2_index;
799 * alloc_compressed_cluster_offset
801 * For a given offset of the disk image, return cluster offset in
804 * If the offset is not found, allocate a new compressed cluster.
806 * Return the cluster offset if successful,
807 * Return 0, otherwise.
811 static uint64_t alloc_compressed_cluster_offset(BlockDriverState *bs,
815 BDRVQcowState *s = bs->opaque;
817 uint64_t l2_offset, *l2_table, cluster_offset;
820 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
824 cluster_offset = be64_to_cpu(l2_table[l2_index]);
825 if (cluster_offset & QCOW_OFLAG_COPIED)
826 return cluster_offset & ~QCOW_OFLAG_COPIED;
829 free_any_clusters(bs, cluster_offset, 1);
831 cluster_offset = alloc_bytes(bs, compressed_size);
832 nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
833 (cluster_offset >> 9);
835 cluster_offset |= QCOW_OFLAG_COMPRESSED |
836 ((uint64_t)nb_csectors << s->csize_shift);
838 /* update L2 table */
840 /* compressed clusters never have the copied flag */
842 l2_table[l2_index] = cpu_to_be64(cluster_offset);
843 if (bdrv_pwrite(s->hd,
844 l2_offset + l2_index * sizeof(uint64_t),
846 sizeof(uint64_t)) != sizeof(uint64_t))
849 return cluster_offset;
853 * alloc_cluster_offset
855 * For a given offset of the disk image, return cluster offset in
858 * If the offset is not found, allocate a new cluster.
860 * Return the cluster offset if successful,
861 * Return 0, otherwise.
865 static uint64_t alloc_cluster_offset(BlockDriverState *bs,
867 int n_start, int n_end,
870 BDRVQcowState *s = bs->opaque;
872 uint64_t l2_offset, *l2_table, cluster_offset;
873 int nb_available, nb_clusters, i;
874 uint64_t start_sect, current;
876 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
880 nb_clusters = ((n_end << 9) + s->cluster_size - 1) >>
882 if (nb_clusters > s->l2_size - l2_index)
883 nb_clusters = s->l2_size - l2_index;
885 cluster_offset = be64_to_cpu(l2_table[l2_index]);
887 /* We keep all QCOW_OFLAG_COPIED clusters */
889 if (cluster_offset & QCOW_OFLAG_COPIED) {
891 for (i = 1; i < nb_clusters; i++) {
892 current = be64_to_cpu(l2_table[l2_index + i]);
893 if (cluster_offset + (i << s->cluster_bits) != current)
898 nb_available = nb_clusters << (s->cluster_bits - 9);
899 if (nb_available > n_end)
900 nb_available = n_end;
902 cluster_offset &= ~QCOW_OFLAG_COPIED;
907 /* for the moment, multiple compressed clusters are not managed */
909 if (cluster_offset & QCOW_OFLAG_COMPRESSED)
912 /* how many empty or how many to free ? */
914 if (!cluster_offset) {
916 /* how many free clusters ? */
919 while (i < nb_clusters &&
920 l2_table[l2_index + i] == 0) {
927 /* how many contiguous clusters ? */
929 for (i = 1; i < nb_clusters; i++) {
930 current = be64_to_cpu(l2_table[l2_index + i]);
931 if (cluster_offset + (i << s->cluster_bits) != current)
936 free_any_clusters(bs, cluster_offset, i);
939 /* allocate a new cluster */
941 cluster_offset = alloc_clusters(bs, nb_clusters * s->cluster_size);
943 /* we must initialize the cluster content which won't be
946 nb_available = nb_clusters << (s->cluster_bits - 9);
947 if (nb_available > n_end)
948 nb_available = n_end;
950 /* copy content of unmodified sectors */
952 start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
954 ret = copy_sectors(bs, start_sect, cluster_offset, 0, n_start);
959 if (nb_available & (s->cluster_sectors - 1)) {
960 uint64_t end = nb_available & ~(uint64_t)(s->cluster_sectors - 1);
961 ret = copy_sectors(bs, start_sect + end,
962 cluster_offset + (end << 9),
969 /* update L2 table */
971 for (i = 0; i < nb_clusters; i++)
972 l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
973 (i << s->cluster_bits)) |
976 if (bdrv_pwrite(s->hd,
977 l2_offset + l2_index * sizeof(uint64_t),
979 nb_clusters * sizeof(uint64_t)) !=
980 nb_clusters * sizeof(uint64_t))
984 *num = nb_available - n_start;
986 return cluster_offset;
989 static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
990 int nb_sectors, int *pnum)
992 uint64_t cluster_offset;
995 cluster_offset = get_cluster_offset(bs, sector_num << 9, pnum);
997 return (cluster_offset != 0);
1000 static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
1001 const uint8_t *buf, int buf_size)
1003 z_stream strm1, *strm = &strm1;
1006 memset(strm, 0, sizeof(*strm));
1008 strm->next_in = (uint8_t *)buf;
1009 strm->avail_in = buf_size;
1010 strm->next_out = out_buf;
1011 strm->avail_out = out_buf_size;
1013 ret = inflateInit2(strm, -12);
1016 ret = inflate(strm, Z_FINISH);
1017 out_len = strm->next_out - out_buf;
1018 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
1019 out_len != out_buf_size) {
1027 static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
1029 int ret, csize, nb_csectors, sector_offset;
1032 coffset = cluster_offset & s->cluster_offset_mask;
1033 if (s->cluster_cache_offset != coffset) {
1034 nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
1035 sector_offset = coffset & 511;
1036 csize = nb_csectors * 512 - sector_offset;
1037 ret = bdrv_read(s->hd, coffset >> 9, s->cluster_data, nb_csectors);
1041 if (decompress_buffer(s->cluster_cache, s->cluster_size,
1042 s->cluster_data + sector_offset, csize) < 0) {
1045 s->cluster_cache_offset = coffset;
1050 /* handle reading after the end of the backing file */
1051 static int backing_read1(BlockDriverState *bs,
1052 int64_t sector_num, uint8_t *buf, int nb_sectors)
1055 if ((sector_num + nb_sectors) <= bs->total_sectors)
1057 if (sector_num >= bs->total_sectors)
1060 n1 = bs->total_sectors - sector_num;
1061 memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1));
1065 static int qcow_read(BlockDriverState *bs, int64_t sector_num,
1066 uint8_t *buf, int nb_sectors)
1068 BDRVQcowState *s = bs->opaque;
1069 int ret, index_in_cluster, n, n1;
1070 uint64_t cluster_offset;
1072 while (nb_sectors > 0) {
1074 cluster_offset = get_cluster_offset(bs, sector_num << 9, &n);
1075 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1076 if (!cluster_offset) {
1077 if (bs->backing_hd) {
1078 /* read from the base image */
1079 n1 = backing_read1(bs->backing_hd, sector_num, buf, n);
1081 ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
1086 memset(buf, 0, 512 * n);
1088 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
1089 if (decompress_cluster(s, cluster_offset) < 0)
1091 memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
1093 ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
1096 if (s->crypt_method) {
1097 encrypt_sectors(s, sector_num, buf, buf, n, 0,
1098 &s->aes_decrypt_key);
1108 static int qcow_write(BlockDriverState *bs, int64_t sector_num,
1109 const uint8_t *buf, int nb_sectors)
1111 BDRVQcowState *s = bs->opaque;
1112 int ret, index_in_cluster, n;
1113 uint64_t cluster_offset;
1116 while (nb_sectors > 0) {
1117 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1118 n_end = index_in_cluster + nb_sectors;
1119 if (s->crypt_method &&
1120 n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
1121 n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
1122 cluster_offset = alloc_cluster_offset(bs, sector_num << 9,
1125 if (!cluster_offset)
1127 if (s->crypt_method) {
1128 encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,
1129 &s->aes_encrypt_key);
1130 ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512,
1131 s->cluster_data, n * 512);
1133 ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
1141 s->cluster_cache_offset = -1; /* disable compressed cache */
1145 typedef struct QCowAIOCB {
1146 BlockDriverAIOCB common;
1151 uint64_t cluster_offset;
1152 uint8_t *cluster_data;
1153 BlockDriverAIOCB *hd_aiocb;
1156 static void qcow_aio_read_cb(void *opaque, int ret)
1158 QCowAIOCB *acb = opaque;
1159 BlockDriverState *bs = acb->common.bs;
1160 BDRVQcowState *s = bs->opaque;
1161 int index_in_cluster, n1;
1163 acb->hd_aiocb = NULL;
1166 acb->common.cb(acb->common.opaque, ret);
1167 qemu_aio_release(acb);
1172 /* post process the read buffer */
1173 if (!acb->cluster_offset) {
1175 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
1178 if (s->crypt_method) {
1179 encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
1181 &s->aes_decrypt_key);
1185 acb->nb_sectors -= acb->n;
1186 acb->sector_num += acb->n;
1187 acb->buf += acb->n * 512;
1189 if (acb->nb_sectors == 0) {
1190 /* request completed */
1191 acb->common.cb(acb->common.opaque, 0);
1192 qemu_aio_release(acb);
1196 /* prepare next AIO request */
1197 acb->n = acb->nb_sectors;
1198 acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, &acb->n);
1199 index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
1201 if (!acb->cluster_offset) {
1202 if (bs->backing_hd) {
1203 /* read from the base image */
1204 n1 = backing_read1(bs->backing_hd, acb->sector_num,
1207 acb->hd_aiocb = bdrv_aio_read(bs->backing_hd, acb->sector_num,
1208 acb->buf, acb->n, qcow_aio_read_cb, acb);
1209 if (acb->hd_aiocb == NULL)
1215 /* Note: in this case, no need to wait */
1216 memset(acb->buf, 0, 512 * acb->n);
1219 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
1220 /* add AIO support for compressed blocks ? */
1221 if (decompress_cluster(s, acb->cluster_offset) < 0)
1224 s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
1227 if ((acb->cluster_offset & 511) != 0) {
1231 acb->hd_aiocb = bdrv_aio_read(s->hd,
1232 (acb->cluster_offset >> 9) + index_in_cluster,
1233 acb->buf, acb->n, qcow_aio_read_cb, acb);
1234 if (acb->hd_aiocb == NULL)
1239 static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
1240 int64_t sector_num, uint8_t *buf, int nb_sectors,
1241 BlockDriverCompletionFunc *cb, void *opaque)
1245 acb = qemu_aio_get(bs, cb, opaque);
1248 acb->hd_aiocb = NULL;
1249 acb->sector_num = sector_num;
1251 acb->nb_sectors = nb_sectors;
1253 acb->cluster_offset = 0;
1257 static BlockDriverAIOCB *qcow_aio_read(BlockDriverState *bs,
1258 int64_t sector_num, uint8_t *buf, int nb_sectors,
1259 BlockDriverCompletionFunc *cb, void *opaque)
1263 acb = qcow_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1267 qcow_aio_read_cb(acb, 0);
1268 return &acb->common;
1271 static void qcow_aio_write_cb(void *opaque, int ret)
1273 QCowAIOCB *acb = opaque;
1274 BlockDriverState *bs = acb->common.bs;
1275 BDRVQcowState *s = bs->opaque;
1276 int index_in_cluster;
1277 uint64_t cluster_offset;
1278 const uint8_t *src_buf;
1281 acb->hd_aiocb = NULL;
1285 acb->common.cb(acb->common.opaque, ret);
1286 qemu_aio_release(acb);
1290 acb->nb_sectors -= acb->n;
1291 acb->sector_num += acb->n;
1292 acb->buf += acb->n * 512;
1294 if (acb->nb_sectors == 0) {
1295 /* request completed */
1296 acb->common.cb(acb->common.opaque, 0);
1297 qemu_aio_release(acb);
1301 index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
1302 n_end = index_in_cluster + acb->nb_sectors;
1303 if (s->crypt_method &&
1304 n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
1305 n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
1307 cluster_offset = alloc_cluster_offset(bs, acb->sector_num << 9,
1310 if (!cluster_offset || (cluster_offset & 511) != 0) {
1314 if (s->crypt_method) {
1315 if (!acb->cluster_data) {
1316 acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS *
1318 if (!acb->cluster_data) {
1323 encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
1324 acb->n, 1, &s->aes_encrypt_key);
1325 src_buf = acb->cluster_data;
1329 acb->hd_aiocb = bdrv_aio_write(s->hd,
1330 (cluster_offset >> 9) + index_in_cluster,
1332 qcow_aio_write_cb, acb);
1333 if (acb->hd_aiocb == NULL)
1337 static BlockDriverAIOCB *qcow_aio_write(BlockDriverState *bs,
1338 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1339 BlockDriverCompletionFunc *cb, void *opaque)
1341 BDRVQcowState *s = bs->opaque;
1344 s->cluster_cache_offset = -1; /* disable compressed cache */
1346 acb = qcow_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
1350 qcow_aio_write_cb(acb, 0);
1351 return &acb->common;
1354 static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
1356 QCowAIOCB *acb = (QCowAIOCB *)blockacb;
1358 bdrv_aio_cancel(acb->hd_aiocb);
1359 qemu_aio_release(acb);
1362 static void qcow_close(BlockDriverState *bs)
1364 BDRVQcowState *s = bs->opaque;
1365 qemu_free(s->l1_table);
1366 qemu_free(s->l2_cache);
1367 qemu_free(s->cluster_cache);
1368 qemu_free(s->cluster_data);
1373 /* XXX: use std qcow open function ? */
1374 typedef struct QCowCreateState {
1377 uint16_t *refcount_block;
1378 uint64_t *refcount_table;
1379 int64_t l1_table_offset;
1380 int64_t refcount_table_offset;
1381 int64_t refcount_block_offset;
1384 static void create_refcount_update(QCowCreateState *s,
1385 int64_t offset, int64_t size)
1388 int64_t start, last, cluster_offset;
1391 start = offset & ~(s->cluster_size - 1);
1392 last = (offset + size - 1) & ~(s->cluster_size - 1);
1393 for(cluster_offset = start; cluster_offset <= last;
1394 cluster_offset += s->cluster_size) {
1395 p = &s->refcount_block[cluster_offset >> s->cluster_bits];
1396 refcount = be16_to_cpu(*p);
1398 *p = cpu_to_be16(refcount);
1402 static int qcow_create(const char *filename, int64_t total_size,
1403 const char *backing_file, int flags)
1405 int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
1407 uint64_t tmp, offset;
1408 QCowCreateState s1, *s = &s1;
1410 memset(s, 0, sizeof(*s));
1412 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
1415 memset(&header, 0, sizeof(header));
1416 header.magic = cpu_to_be32(QCOW_MAGIC);
1417 header.version = cpu_to_be32(QCOW_VERSION);
1418 header.size = cpu_to_be64(total_size * 512);
1419 header_size = sizeof(header);
1420 backing_filename_len = 0;
1422 header.backing_file_offset = cpu_to_be64(header_size);
1423 backing_filename_len = strlen(backing_file);
1424 header.backing_file_size = cpu_to_be32(backing_filename_len);
1425 header_size += backing_filename_len;
1427 s->cluster_bits = 12; /* 4 KB clusters */
1428 s->cluster_size = 1 << s->cluster_bits;
1429 header.cluster_bits = cpu_to_be32(s->cluster_bits);
1430 header_size = (header_size + 7) & ~7;
1431 if (flags & BLOCK_FLAG_ENCRYPT) {
1432 header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
1434 header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
1436 l2_bits = s->cluster_bits - 3;
1437 shift = s->cluster_bits + l2_bits;
1438 l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift);
1439 offset = align_offset(header_size, s->cluster_size);
1440 s->l1_table_offset = offset;
1441 header.l1_table_offset = cpu_to_be64(s->l1_table_offset);
1442 header.l1_size = cpu_to_be32(l1_size);
1443 offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
1445 s->refcount_table = qemu_mallocz(s->cluster_size);
1446 if (!s->refcount_table)
1448 s->refcount_block = qemu_mallocz(s->cluster_size);
1449 if (!s->refcount_block)
1452 s->refcount_table_offset = offset;
1453 header.refcount_table_offset = cpu_to_be64(offset);
1454 header.refcount_table_clusters = cpu_to_be32(1);
1455 offset += s->cluster_size;
1457 s->refcount_table[0] = cpu_to_be64(offset);
1458 s->refcount_block_offset = offset;
1459 offset += s->cluster_size;
1461 /* update refcounts */
1462 create_refcount_update(s, 0, header_size);
1463 create_refcount_update(s, s->l1_table_offset, l1_size * sizeof(uint64_t));
1464 create_refcount_update(s, s->refcount_table_offset, s->cluster_size);
1465 create_refcount_update(s, s->refcount_block_offset, s->cluster_size);
1467 /* write all the data */
1468 write(fd, &header, sizeof(header));
1470 write(fd, backing_file, backing_filename_len);
1472 lseek(fd, s->l1_table_offset, SEEK_SET);
1474 for(i = 0;i < l1_size; i++) {
1475 write(fd, &tmp, sizeof(tmp));
1477 lseek(fd, s->refcount_table_offset, SEEK_SET);
1478 write(fd, s->refcount_table, s->cluster_size);
1480 lseek(fd, s->refcount_block_offset, SEEK_SET);
1481 write(fd, s->refcount_block, s->cluster_size);
1483 qemu_free(s->refcount_table);
1484 qemu_free(s->refcount_block);
1488 qemu_free(s->refcount_table);
1489 qemu_free(s->refcount_block);
1494 static int qcow_make_empty(BlockDriverState *bs)
1497 /* XXX: not correct */
1498 BDRVQcowState *s = bs->opaque;
1499 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1502 memset(s->l1_table, 0, l1_length);
1503 if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
1505 ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
1514 /* XXX: put compressed sectors first, then all the cluster aligned
1515 tables to avoid losing bytes in alignment */
1516 static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
1517 const uint8_t *buf, int nb_sectors)
1519 BDRVQcowState *s = bs->opaque;
1523 uint64_t cluster_offset;
1525 if (nb_sectors == 0) {
1526 /* align end of file to a sector boundary to ease reading with
1527 sector based I/Os */
1528 cluster_offset = bdrv_getlength(s->hd);
1529 cluster_offset = (cluster_offset + 511) & ~511;
1530 bdrv_truncate(s->hd, cluster_offset);
1534 if (nb_sectors != s->cluster_sectors)
1537 out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1541 /* best compression, small window, no zlib header */
1542 memset(&strm, 0, sizeof(strm));
1543 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1545 9, Z_DEFAULT_STRATEGY);
1551 strm.avail_in = s->cluster_size;
1552 strm.next_in = (uint8_t *)buf;
1553 strm.avail_out = s->cluster_size;
1554 strm.next_out = out_buf;
1556 ret = deflate(&strm, Z_FINISH);
1557 if (ret != Z_STREAM_END && ret != Z_OK) {
1562 out_len = strm.next_out - out_buf;
1566 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1567 /* could not compress: write normal cluster */
1568 qcow_write(bs, sector_num, buf, s->cluster_sectors);
1570 cluster_offset = alloc_compressed_cluster_offset(bs, sector_num << 9,
1572 if (!cluster_offset)
1574 cluster_offset &= s->cluster_offset_mask;
1575 if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
1585 static void qcow_flush(BlockDriverState *bs)
1587 BDRVQcowState *s = bs->opaque;
1591 static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1593 BDRVQcowState *s = bs->opaque;
1594 bdi->cluster_size = s->cluster_size;
1595 bdi->vm_state_offset = (int64_t)s->l1_vm_state_index <<
1596 (s->cluster_bits + s->l2_bits);
1600 /*********************************************************/
1601 /* snapshot support */
1603 /* update the refcounts of snapshots and the copied flag */
1604 static int update_snapshot_refcount(BlockDriverState *bs,
1605 int64_t l1_table_offset,
1609 BDRVQcowState *s = bs->opaque;
1610 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
1611 int64_t old_offset, old_l2_offset;
1612 int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
1618 l1_size2 = l1_size * sizeof(uint64_t);
1620 if (l1_table_offset != s->l1_table_offset) {
1621 l1_table = qemu_malloc(l1_size2);
1625 if (bdrv_pread(s->hd, l1_table_offset,
1626 l1_table, l1_size2) != l1_size2)
1628 for(i = 0;i < l1_size; i++)
1629 be64_to_cpus(&l1_table[i]);
1631 assert(l1_size == s->l1_size);
1632 l1_table = s->l1_table;
1636 l2_size = s->l2_size * sizeof(uint64_t);
1637 l2_table = qemu_malloc(l2_size);
1641 for(i = 0; i < l1_size; i++) {
1642 l2_offset = l1_table[i];
1644 old_l2_offset = l2_offset;
1645 l2_offset &= ~QCOW_OFLAG_COPIED;
1647 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
1649 for(j = 0; j < s->l2_size; j++) {
1650 offset = be64_to_cpu(l2_table[j]);
1652 old_offset = offset;
1653 offset &= ~QCOW_OFLAG_COPIED;
1654 if (offset & QCOW_OFLAG_COMPRESSED) {
1655 nb_csectors = ((offset >> s->csize_shift) &
1658 update_refcount(bs, (offset & s->cluster_offset_mask) & ~511,
1659 nb_csectors * 512, addend);
1660 /* compressed clusters are never modified */
1664 refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
1666 refcount = get_refcount(bs, offset >> s->cluster_bits);
1670 if (refcount == 1) {
1671 offset |= QCOW_OFLAG_COPIED;
1673 if (offset != old_offset) {
1674 l2_table[j] = cpu_to_be64(offset);
1680 if (bdrv_pwrite(s->hd,
1681 l2_offset, l2_table, l2_size) != l2_size)
1686 refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
1688 refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
1690 if (refcount == 1) {
1691 l2_offset |= QCOW_OFLAG_COPIED;
1693 if (l2_offset != old_l2_offset) {
1694 l1_table[i] = l2_offset;
1700 for(i = 0; i < l1_size; i++)
1701 cpu_to_be64s(&l1_table[i]);
1702 if (bdrv_pwrite(s->hd, l1_table_offset, l1_table,
1703 l1_size2) != l1_size2)
1705 for(i = 0; i < l1_size; i++)
1706 be64_to_cpus(&l1_table[i]);
1709 qemu_free(l1_table);
1710 qemu_free(l2_table);
1714 qemu_free(l1_table);
1715 qemu_free(l2_table);
1719 static void qcow_free_snapshots(BlockDriverState *bs)
1721 BDRVQcowState *s = bs->opaque;
1724 for(i = 0; i < s->nb_snapshots; i++) {
1725 qemu_free(s->snapshots[i].name);
1726 qemu_free(s->snapshots[i].id_str);
1728 qemu_free(s->snapshots);
1729 s->snapshots = NULL;
1730 s->nb_snapshots = 0;
1733 static int qcow_read_snapshots(BlockDriverState *bs)
1735 BDRVQcowState *s = bs->opaque;
1736 QCowSnapshotHeader h;
1738 int i, id_str_size, name_size;
1740 uint32_t extra_data_size;
1742 offset = s->snapshots_offset;
1743 s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot));
1746 for(i = 0; i < s->nb_snapshots; i++) {
1747 offset = align_offset(offset, 8);
1748 if (bdrv_pread(s->hd, offset, &h, sizeof(h)) != sizeof(h))
1750 offset += sizeof(h);
1751 sn = s->snapshots + i;
1752 sn->l1_table_offset = be64_to_cpu(h.l1_table_offset);
1753 sn->l1_size = be32_to_cpu(h.l1_size);
1754 sn->vm_state_size = be32_to_cpu(h.vm_state_size);
1755 sn->date_sec = be32_to_cpu(h.date_sec);
1756 sn->date_nsec = be32_to_cpu(h.date_nsec);
1757 sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec);
1758 extra_data_size = be32_to_cpu(h.extra_data_size);
1760 id_str_size = be16_to_cpu(h.id_str_size);
1761 name_size = be16_to_cpu(h.name_size);
1763 offset += extra_data_size;
1765 sn->id_str = qemu_malloc(id_str_size + 1);
1768 if (bdrv_pread(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
1770 offset += id_str_size;
1771 sn->id_str[id_str_size] = '\0';
1773 sn->name = qemu_malloc(name_size + 1);
1776 if (bdrv_pread(s->hd, offset, sn->name, name_size) != name_size)
1778 offset += name_size;
1779 sn->name[name_size] = '\0';
1781 s->snapshots_size = offset - s->snapshots_offset;
1784 qcow_free_snapshots(bs);
1788 /* add at the end of the file a new list of snapshots */
1789 static int qcow_write_snapshots(BlockDriverState *bs)
1791 BDRVQcowState *s = bs->opaque;
1793 QCowSnapshotHeader h;
1794 int i, name_size, id_str_size, snapshots_size;
1797 int64_t offset, snapshots_offset;
1799 /* compute the size of the snapshots */
1801 for(i = 0; i < s->nb_snapshots; i++) {
1802 sn = s->snapshots + i;
1803 offset = align_offset(offset, 8);
1804 offset += sizeof(h);
1805 offset += strlen(sn->id_str);
1806 offset += strlen(sn->name);
1808 snapshots_size = offset;
1810 snapshots_offset = alloc_clusters(bs, snapshots_size);
1811 offset = snapshots_offset;
1813 for(i = 0; i < s->nb_snapshots; i++) {
1814 sn = s->snapshots + i;
1815 memset(&h, 0, sizeof(h));
1816 h.l1_table_offset = cpu_to_be64(sn->l1_table_offset);
1817 h.l1_size = cpu_to_be32(sn->l1_size);
1818 h.vm_state_size = cpu_to_be32(sn->vm_state_size);
1819 h.date_sec = cpu_to_be32(sn->date_sec);
1820 h.date_nsec = cpu_to_be32(sn->date_nsec);
1821 h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
1823 id_str_size = strlen(sn->id_str);
1824 name_size = strlen(sn->name);
1825 h.id_str_size = cpu_to_be16(id_str_size);
1826 h.name_size = cpu_to_be16(name_size);
1827 offset = align_offset(offset, 8);
1828 if (bdrv_pwrite(s->hd, offset, &h, sizeof(h)) != sizeof(h))
1830 offset += sizeof(h);
1831 if (bdrv_pwrite(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
1833 offset += id_str_size;
1834 if (bdrv_pwrite(s->hd, offset, sn->name, name_size) != name_size)
1836 offset += name_size;
1839 /* update the various header fields */
1840 data64 = cpu_to_be64(snapshots_offset);
1841 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, snapshots_offset),
1842 &data64, sizeof(data64)) != sizeof(data64))
1844 data32 = cpu_to_be32(s->nb_snapshots);
1845 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, nb_snapshots),
1846 &data32, sizeof(data32)) != sizeof(data32))
1849 /* free the old snapshot table */
1850 free_clusters(bs, s->snapshots_offset, s->snapshots_size);
1851 s->snapshots_offset = snapshots_offset;
1852 s->snapshots_size = snapshots_size;
1858 static void find_new_snapshot_id(BlockDriverState *bs,
1859 char *id_str, int id_str_size)
1861 BDRVQcowState *s = bs->opaque;
1863 int i, id, id_max = 0;
1865 for(i = 0; i < s->nb_snapshots; i++) {
1866 sn = s->snapshots + i;
1867 id = strtoul(sn->id_str, NULL, 10);
1871 snprintf(id_str, id_str_size, "%d", id_max + 1);
1874 static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str)
1876 BDRVQcowState *s = bs->opaque;
1879 for(i = 0; i < s->nb_snapshots; i++) {
1880 if (!strcmp(s->snapshots[i].id_str, id_str))
1886 static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
1888 BDRVQcowState *s = bs->opaque;
1891 ret = find_snapshot_by_id(bs, name);
1894 for(i = 0; i < s->nb_snapshots; i++) {
1895 if (!strcmp(s->snapshots[i].name, name))
1901 /* if no id is provided, a new one is constructed */
1902 static int qcow_snapshot_create(BlockDriverState *bs,
1903 QEMUSnapshotInfo *sn_info)
1905 BDRVQcowState *s = bs->opaque;
1906 QCowSnapshot *snapshots1, sn1, *sn = &sn1;
1908 uint64_t *l1_table = NULL;
1910 memset(sn, 0, sizeof(*sn));
1912 if (sn_info->id_str[0] == '\0') {
1913 /* compute a new id */
1914 find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
1917 /* check that the ID is unique */
1918 if (find_snapshot_by_id(bs, sn_info->id_str) >= 0)
1921 sn->id_str = qemu_strdup(sn_info->id_str);
1924 sn->name = qemu_strdup(sn_info->name);
1927 sn->vm_state_size = sn_info->vm_state_size;
1928 sn->date_sec = sn_info->date_sec;
1929 sn->date_nsec = sn_info->date_nsec;
1930 sn->vm_clock_nsec = sn_info->vm_clock_nsec;
1932 ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
1936 /* create the L1 table of the snapshot */
1937 sn->l1_table_offset = alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
1938 sn->l1_size = s->l1_size;
1940 l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
1943 for(i = 0; i < s->l1_size; i++) {
1944 l1_table[i] = cpu_to_be64(s->l1_table[i]);
1946 if (bdrv_pwrite(s->hd, sn->l1_table_offset,
1947 l1_table, s->l1_size * sizeof(uint64_t)) !=
1948 (s->l1_size * sizeof(uint64_t)))
1950 qemu_free(l1_table);
1953 snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot));
1956 memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot));
1957 s->snapshots = snapshots1;
1958 s->snapshots[s->nb_snapshots++] = *sn;
1960 if (qcow_write_snapshots(bs) < 0)
1963 check_refcounts(bs);
1967 qemu_free(sn->name);
1968 qemu_free(l1_table);
1972 /* copy the snapshot 'snapshot_name' into the current disk image */
1973 static int qcow_snapshot_goto(BlockDriverState *bs,
1974 const char *snapshot_id)
1976 BDRVQcowState *s = bs->opaque;
1978 int i, snapshot_index, l1_size2;
1980 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
1981 if (snapshot_index < 0)
1983 sn = &s->snapshots[snapshot_index];
1985 if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, -1) < 0)
1988 if (grow_l1_table(bs, sn->l1_size) < 0)
1991 s->l1_size = sn->l1_size;
1992 l1_size2 = s->l1_size * sizeof(uint64_t);
1993 /* copy the snapshot l1 table to the current l1 table */
1994 if (bdrv_pread(s->hd, sn->l1_table_offset,
1995 s->l1_table, l1_size2) != l1_size2)
1997 if (bdrv_pwrite(s->hd, s->l1_table_offset,
1998 s->l1_table, l1_size2) != l1_size2)
2000 for(i = 0;i < s->l1_size; i++) {
2001 be64_to_cpus(&s->l1_table[i]);
2004 if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0)
2008 check_refcounts(bs);
2015 static int qcow_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
2017 BDRVQcowState *s = bs->opaque;
2019 int snapshot_index, ret;
2021 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
2022 if (snapshot_index < 0)
2024 sn = &s->snapshots[snapshot_index];
2026 ret = update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1);
2029 /* must update the copied flag on the current cluster offsets */
2030 ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
2033 free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t));
2035 qemu_free(sn->id_str);
2036 qemu_free(sn->name);
2037 memmove(sn, sn + 1, (s->nb_snapshots - snapshot_index - 1) * sizeof(*sn));
2039 ret = qcow_write_snapshots(bs);
2041 /* XXX: restore snapshot if error ? */
2045 check_refcounts(bs);
2050 static int qcow_snapshot_list(BlockDriverState *bs,
2051 QEMUSnapshotInfo **psn_tab)
2053 BDRVQcowState *s = bs->opaque;
2054 QEMUSnapshotInfo *sn_tab, *sn_info;
2058 sn_tab = qemu_mallocz(s->nb_snapshots * sizeof(QEMUSnapshotInfo));
2061 for(i = 0; i < s->nb_snapshots; i++) {
2062 sn_info = sn_tab + i;
2063 sn = s->snapshots + i;
2064 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
2066 pstrcpy(sn_info->name, sizeof(sn_info->name),
2068 sn_info->vm_state_size = sn->vm_state_size;
2069 sn_info->date_sec = sn->date_sec;
2070 sn_info->date_nsec = sn->date_nsec;
2071 sn_info->vm_clock_nsec = sn->vm_clock_nsec;
2074 return s->nb_snapshots;
2081 /*********************************************************/
2082 /* refcount handling */
2084 static int refcount_init(BlockDriverState *bs)
2086 BDRVQcowState *s = bs->opaque;
2087 int ret, refcount_table_size2, i;
2089 s->refcount_block_cache = qemu_malloc(s->cluster_size);
2090 if (!s->refcount_block_cache)
2092 refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
2093 s->refcount_table = qemu_malloc(refcount_table_size2);
2094 if (!s->refcount_table)
2096 if (s->refcount_table_size > 0) {
2097 ret = bdrv_pread(s->hd, s->refcount_table_offset,
2098 s->refcount_table, refcount_table_size2);
2099 if (ret != refcount_table_size2)
2101 for(i = 0; i < s->refcount_table_size; i++)
2102 be64_to_cpus(&s->refcount_table[i]);
2109 static void refcount_close(BlockDriverState *bs)
2111 BDRVQcowState *s = bs->opaque;
2112 qemu_free(s->refcount_block_cache);
2113 qemu_free(s->refcount_table);
2117 static int load_refcount_block(BlockDriverState *bs,
2118 int64_t refcount_block_offset)
2120 BDRVQcowState *s = bs->opaque;
2122 ret = bdrv_pread(s->hd, refcount_block_offset, s->refcount_block_cache,
2124 if (ret != s->cluster_size)
2126 s->refcount_block_cache_offset = refcount_block_offset;
2130 static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
2132 BDRVQcowState *s = bs->opaque;
2133 int refcount_table_index, block_index;
2134 int64_t refcount_block_offset;
2136 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
2137 if (refcount_table_index >= s->refcount_table_size)
2139 refcount_block_offset = s->refcount_table[refcount_table_index];
2140 if (!refcount_block_offset)
2142 if (refcount_block_offset != s->refcount_block_cache_offset) {
2143 /* better than nothing: return allocated if read error */
2144 if (load_refcount_block(bs, refcount_block_offset) < 0)
2147 block_index = cluster_index &
2148 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
2149 return be16_to_cpu(s->refcount_block_cache[block_index]);
2152 /* return < 0 if error */
2153 static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
2155 BDRVQcowState *s = bs->opaque;
2158 nb_clusters = (size + s->cluster_size - 1) >> s->cluster_bits;
2160 if (get_refcount(bs, s->free_cluster_index) == 0) {
2161 s->free_cluster_index++;
2162 for(i = 1; i < nb_clusters; i++) {
2163 if (get_refcount(bs, s->free_cluster_index) != 0)
2165 s->free_cluster_index++;
2168 printf("alloc_clusters: size=%lld -> %lld\n",
2170 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
2172 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
2175 s->free_cluster_index++;
2180 static int64_t alloc_clusters(BlockDriverState *bs, int64_t size)
2184 offset = alloc_clusters_noref(bs, size);
2185 update_refcount(bs, offset, size, 1);
2189 /* only used to allocate compressed sectors. We try to allocate
2190 contiguous sectors. size must be <= cluster_size */
2191 static int64_t alloc_bytes(BlockDriverState *bs, int size)
2193 BDRVQcowState *s = bs->opaque;
2194 int64_t offset, cluster_offset;
2195 int free_in_cluster;
2197 assert(size > 0 && size <= s->cluster_size);
2198 if (s->free_byte_offset == 0) {
2199 s->free_byte_offset = alloc_clusters(bs, s->cluster_size);
2202 free_in_cluster = s->cluster_size -
2203 (s->free_byte_offset & (s->cluster_size - 1));
2204 if (size <= free_in_cluster) {
2205 /* enough space in current cluster */
2206 offset = s->free_byte_offset;
2207 s->free_byte_offset += size;
2208 free_in_cluster -= size;
2209 if (free_in_cluster == 0)
2210 s->free_byte_offset = 0;
2211 if ((offset & (s->cluster_size - 1)) != 0)
2212 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
2214 offset = alloc_clusters(bs, s->cluster_size);
2215 cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
2216 if ((cluster_offset + s->cluster_size) == offset) {
2217 /* we are lucky: contiguous data */
2218 offset = s->free_byte_offset;
2219 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
2220 s->free_byte_offset += size;
2222 s->free_byte_offset = offset;
2229 static void free_clusters(BlockDriverState *bs,
2230 int64_t offset, int64_t size)
2232 update_refcount(bs, offset, size, -1);
2235 static int grow_refcount_table(BlockDriverState *bs, int min_size)
2237 BDRVQcowState *s = bs->opaque;
2238 int new_table_size, new_table_size2, refcount_table_clusters, i, ret;
2239 uint64_t *new_table;
2240 int64_t table_offset;
2244 int64_t old_table_offset;
2246 if (min_size <= s->refcount_table_size)
2248 /* compute new table size */
2249 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
2251 if (refcount_table_clusters == 0) {
2252 refcount_table_clusters = 1;
2254 refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
2256 new_table_size = refcount_table_clusters << (s->cluster_bits - 3);
2257 if (min_size <= new_table_size)
2261 printf("grow_refcount_table from %d to %d\n",
2262 s->refcount_table_size,
2265 new_table_size2 = new_table_size * sizeof(uint64_t);
2266 new_table = qemu_mallocz(new_table_size2);
2269 memcpy(new_table, s->refcount_table,
2270 s->refcount_table_size * sizeof(uint64_t));
2271 for(i = 0; i < s->refcount_table_size; i++)
2272 cpu_to_be64s(&new_table[i]);
2273 /* Note: we cannot update the refcount now to avoid recursion */
2274 table_offset = alloc_clusters_noref(bs, new_table_size2);
2275 ret = bdrv_pwrite(s->hd, table_offset, new_table, new_table_size2);
2276 if (ret != new_table_size2)
2278 for(i = 0; i < s->refcount_table_size; i++)
2279 be64_to_cpus(&new_table[i]);
2281 data64 = cpu_to_be64(table_offset);
2282 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_offset),
2283 &data64, sizeof(data64)) != sizeof(data64))
2285 data32 = cpu_to_be32(refcount_table_clusters);
2286 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_clusters),
2287 &data32, sizeof(data32)) != sizeof(data32))
2289 qemu_free(s->refcount_table);
2290 old_table_offset = s->refcount_table_offset;
2291 old_table_size = s->refcount_table_size;
2292 s->refcount_table = new_table;
2293 s->refcount_table_size = new_table_size;
2294 s->refcount_table_offset = table_offset;
2296 update_refcount(bs, table_offset, new_table_size2, 1);
2297 free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
2300 free_clusters(bs, table_offset, new_table_size2);
2301 qemu_free(new_table);
2305 /* addend must be 1 or -1 */
2306 /* XXX: cache several refcount block clusters ? */
2307 static int update_cluster_refcount(BlockDriverState *bs,
2308 int64_t cluster_index,
2311 BDRVQcowState *s = bs->opaque;
2312 int64_t offset, refcount_block_offset;
2313 int ret, refcount_table_index, block_index, refcount;
2316 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
2317 if (refcount_table_index >= s->refcount_table_size) {
2320 ret = grow_refcount_table(bs, refcount_table_index + 1);
2324 refcount_block_offset = s->refcount_table[refcount_table_index];
2325 if (!refcount_block_offset) {
2328 /* create a new refcount block */
2329 /* Note: we cannot update the refcount now to avoid recursion */
2330 offset = alloc_clusters_noref(bs, s->cluster_size);
2331 memset(s->refcount_block_cache, 0, s->cluster_size);
2332 ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->cluster_size);
2333 if (ret != s->cluster_size)
2335 s->refcount_table[refcount_table_index] = offset;
2336 data64 = cpu_to_be64(offset);
2337 ret = bdrv_pwrite(s->hd, s->refcount_table_offset +
2338 refcount_table_index * sizeof(uint64_t),
2339 &data64, sizeof(data64));
2340 if (ret != sizeof(data64))
2343 refcount_block_offset = offset;
2344 s->refcount_block_cache_offset = offset;
2345 update_refcount(bs, offset, s->cluster_size, 1);
2347 if (refcount_block_offset != s->refcount_block_cache_offset) {
2348 if (load_refcount_block(bs, refcount_block_offset) < 0)
2352 /* we can update the count and save it */
2353 block_index = cluster_index &
2354 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
2355 refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
2357 if (refcount < 0 || refcount > 0xffff)
2359 if (refcount == 0 && cluster_index < s->free_cluster_index) {
2360 s->free_cluster_index = cluster_index;
2362 s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
2363 if (bdrv_pwrite(s->hd,
2364 refcount_block_offset + (block_index << REFCOUNT_SHIFT),
2365 &s->refcount_block_cache[block_index], 2) != 2)
2370 static void update_refcount(BlockDriverState *bs,
2371 int64_t offset, int64_t length,
2374 BDRVQcowState *s = bs->opaque;
2375 int64_t start, last, cluster_offset;
2378 printf("update_refcount: offset=%lld size=%lld addend=%d\n",
2379 offset, length, addend);
2383 start = offset & ~(s->cluster_size - 1);
2384 last = (offset + length - 1) & ~(s->cluster_size - 1);
2385 for(cluster_offset = start; cluster_offset <= last;
2386 cluster_offset += s->cluster_size) {
2387 update_cluster_refcount(bs, cluster_offset >> s->cluster_bits, addend);
2392 static void inc_refcounts(BlockDriverState *bs,
2393 uint16_t *refcount_table,
2394 int refcount_table_size,
2395 int64_t offset, int64_t size)
2397 BDRVQcowState *s = bs->opaque;
2398 int64_t start, last, cluster_offset;
2404 start = offset & ~(s->cluster_size - 1);
2405 last = (offset + size - 1) & ~(s->cluster_size - 1);
2406 for(cluster_offset = start; cluster_offset <= last;
2407 cluster_offset += s->cluster_size) {
2408 k = cluster_offset >> s->cluster_bits;
2409 if (k < 0 || k >= refcount_table_size) {
2410 printf("ERROR: invalid cluster offset=0x%llx\n", cluster_offset);
2412 if (++refcount_table[k] == 0) {
2413 printf("ERROR: overflow cluster offset=0x%llx\n", cluster_offset);
2419 static int check_refcounts_l1(BlockDriverState *bs,
2420 uint16_t *refcount_table,
2421 int refcount_table_size,
2422 int64_t l1_table_offset, int l1_size,
2425 BDRVQcowState *s = bs->opaque;
2426 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2;
2427 int l2_size, i, j, nb_csectors, refcount;
2430 l1_size2 = l1_size * sizeof(uint64_t);
2432 inc_refcounts(bs, refcount_table, refcount_table_size,
2433 l1_table_offset, l1_size2);
2435 l1_table = qemu_malloc(l1_size2);
2438 if (bdrv_pread(s->hd, l1_table_offset,
2439 l1_table, l1_size2) != l1_size2)
2441 for(i = 0;i < l1_size; i++)
2442 be64_to_cpus(&l1_table[i]);
2444 l2_size = s->l2_size * sizeof(uint64_t);
2445 l2_table = qemu_malloc(l2_size);
2448 for(i = 0; i < l1_size; i++) {
2449 l2_offset = l1_table[i];
2452 refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
2453 if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
2454 printf("ERROR OFLAG_COPIED: l2_offset=%llx refcount=%d\n",
2455 l2_offset, refcount);
2458 l2_offset &= ~QCOW_OFLAG_COPIED;
2459 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
2461 for(j = 0; j < s->l2_size; j++) {
2462 offset = be64_to_cpu(l2_table[j]);
2464 if (offset & QCOW_OFLAG_COMPRESSED) {
2465 if (offset & QCOW_OFLAG_COPIED) {
2466 printf("ERROR: cluster %lld: copied flag must never be set for compressed clusters\n",
2467 offset >> s->cluster_bits);
2468 offset &= ~QCOW_OFLAG_COPIED;
2470 nb_csectors = ((offset >> s->csize_shift) &
2472 offset &= s->cluster_offset_mask;
2473 inc_refcounts(bs, refcount_table,
2474 refcount_table_size,
2475 offset & ~511, nb_csectors * 512);
2478 refcount = get_refcount(bs, (offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
2479 if ((refcount == 1) != ((offset & QCOW_OFLAG_COPIED) != 0)) {
2480 printf("ERROR OFLAG_COPIED: offset=%llx refcount=%d\n",
2484 offset &= ~QCOW_OFLAG_COPIED;
2485 inc_refcounts(bs, refcount_table,
2486 refcount_table_size,
2487 offset, s->cluster_size);
2491 inc_refcounts(bs, refcount_table,
2492 refcount_table_size,
2497 qemu_free(l1_table);
2498 qemu_free(l2_table);
2501 printf("ERROR: I/O error in check_refcounts_l1\n");
2502 qemu_free(l1_table);
2503 qemu_free(l2_table);
2507 static void check_refcounts(BlockDriverState *bs)
2509 BDRVQcowState *s = bs->opaque;
2511 int nb_clusters, refcount1, refcount2, i;
2513 uint16_t *refcount_table;
2515 size = bdrv_getlength(s->hd);
2516 nb_clusters = (size + s->cluster_size - 1) >> s->cluster_bits;
2517 refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
2520 inc_refcounts(bs, refcount_table, nb_clusters,
2521 0, s->cluster_size);
2523 check_refcounts_l1(bs, refcount_table, nb_clusters,
2524 s->l1_table_offset, s->l1_size, 1);
2527 for(i = 0; i < s->nb_snapshots; i++) {
2528 sn = s->snapshots + i;
2529 check_refcounts_l1(bs, refcount_table, nb_clusters,
2530 sn->l1_table_offset, sn->l1_size, 0);
2532 inc_refcounts(bs, refcount_table, nb_clusters,
2533 s->snapshots_offset, s->snapshots_size);
2536 inc_refcounts(bs, refcount_table, nb_clusters,
2537 s->refcount_table_offset,
2538 s->refcount_table_size * sizeof(uint64_t));
2539 for(i = 0; i < s->refcount_table_size; i++) {
2541 offset = s->refcount_table[i];
2543 inc_refcounts(bs, refcount_table, nb_clusters,
2544 offset, s->cluster_size);
2548 /* compare ref counts */
2549 for(i = 0; i < nb_clusters; i++) {
2550 refcount1 = get_refcount(bs, i);
2551 refcount2 = refcount_table[i];
2552 if (refcount1 != refcount2)
2553 printf("ERROR cluster %d refcount=%d reference=%d\n",
2554 i, refcount1, refcount2);
2557 qemu_free(refcount_table);
2561 static void dump_refcounts(BlockDriverState *bs)
2563 BDRVQcowState *s = bs->opaque;
2564 int64_t nb_clusters, k, k1, size;
2567 size = bdrv_getlength(s->hd);
2568 nb_clusters = (size + s->cluster_size - 1) >> s->cluster_bits;
2569 for(k = 0; k < nb_clusters;) {
2571 refcount = get_refcount(bs, k);
2573 while (k < nb_clusters && get_refcount(bs, k) == refcount)
2575 printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1);
2581 BlockDriver bdrv_qcow2 = {
2583 sizeof(BDRVQcowState),
2595 .bdrv_aio_read = qcow_aio_read,
2596 .bdrv_aio_write = qcow_aio_write,
2597 .bdrv_aio_cancel = qcow_aio_cancel,
2598 .aiocb_size = sizeof(QCowAIOCB),
2599 .bdrv_write_compressed = qcow_write_compressed,
2601 .bdrv_snapshot_create = qcow_snapshot_create,
2602 .bdrv_snapshot_goto = qcow_snapshot_goto,
2603 .bdrv_snapshot_delete = qcow_snapshot_delete,
2604 .bdrv_snapshot_list = qcow_snapshot_list,
2605 .bdrv_get_info = qcow_get_info,