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
25 #include "qemu-common.h"
26 #include "block_int.h"
27 #include "block/qcow2.h"
29 static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size);
30 static int update_refcount(BlockDriverState *bs,
31 int64_t offset, int64_t length,
35 static int cache_refcount_updates = 0;
37 static int write_refcount_block(BDRVQcowState *s)
39 size_t size = s->cluster_size;
41 if (s->refcount_block_cache_offset == 0) {
45 if (bdrv_pwrite(s->hd, s->refcount_block_cache_offset,
46 s->refcount_block_cache, size) != size)
54 /*********************************************************/
55 /* refcount handling */
57 int qcow2_refcount_init(BlockDriverState *bs)
59 BDRVQcowState *s = bs->opaque;
60 int ret, refcount_table_size2, i;
62 s->refcount_block_cache = qemu_malloc(s->cluster_size);
63 refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
64 s->refcount_table = qemu_malloc(refcount_table_size2);
65 if (s->refcount_table_size > 0) {
66 ret = bdrv_pread(s->hd, s->refcount_table_offset,
67 s->refcount_table, refcount_table_size2);
68 if (ret != refcount_table_size2)
70 for(i = 0; i < s->refcount_table_size; i++)
71 be64_to_cpus(&s->refcount_table[i]);
78 void qcow2_refcount_close(BlockDriverState *bs)
80 BDRVQcowState *s = bs->opaque;
81 qemu_free(s->refcount_block_cache);
82 qemu_free(s->refcount_table);
86 static int load_refcount_block(BlockDriverState *bs,
87 int64_t refcount_block_offset)
89 BDRVQcowState *s = bs->opaque;
92 if (cache_refcount_updates) {
93 write_refcount_block(s);
96 ret = bdrv_pread(s->hd, refcount_block_offset, s->refcount_block_cache,
98 if (ret != s->cluster_size)
100 s->refcount_block_cache_offset = refcount_block_offset;
104 static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
106 BDRVQcowState *s = bs->opaque;
107 int refcount_table_index, block_index;
108 int64_t refcount_block_offset;
110 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
111 if (refcount_table_index >= s->refcount_table_size)
113 refcount_block_offset = s->refcount_table[refcount_table_index];
114 if (!refcount_block_offset)
116 if (refcount_block_offset != s->refcount_block_cache_offset) {
117 /* better than nothing: return allocated if read error */
118 if (load_refcount_block(bs, refcount_block_offset) < 0)
121 block_index = cluster_index &
122 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
123 return be16_to_cpu(s->refcount_block_cache[block_index]);
126 static int grow_refcount_table(BlockDriverState *bs, int min_size)
128 BDRVQcowState *s = bs->opaque;
129 int new_table_size, new_table_size2, refcount_table_clusters, i, ret;
131 int64_t table_offset;
134 int64_t old_table_offset;
136 if (min_size <= s->refcount_table_size)
138 /* compute new table size */
139 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
141 if (refcount_table_clusters == 0) {
142 refcount_table_clusters = 1;
144 refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
146 new_table_size = refcount_table_clusters << (s->cluster_bits - 3);
147 if (min_size <= new_table_size)
151 printf("grow_refcount_table from %d to %d\n",
152 s->refcount_table_size,
155 new_table_size2 = new_table_size * sizeof(uint64_t);
156 new_table = qemu_mallocz(new_table_size2);
157 memcpy(new_table, s->refcount_table,
158 s->refcount_table_size * sizeof(uint64_t));
159 for(i = 0; i < s->refcount_table_size; i++)
160 cpu_to_be64s(&new_table[i]);
161 /* Note: we cannot update the refcount now to avoid recursion */
162 table_offset = alloc_clusters_noref(bs, new_table_size2);
163 ret = bdrv_pwrite(s->hd, table_offset, new_table, new_table_size2);
164 if (ret != new_table_size2)
166 for(i = 0; i < s->refcount_table_size; i++)
167 be64_to_cpus(&new_table[i]);
169 cpu_to_be64w((uint64_t*)data, table_offset);
170 cpu_to_be32w((uint32_t*)(data + 8), refcount_table_clusters);
171 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_offset),
172 data, sizeof(data)) != sizeof(data))
174 qemu_free(s->refcount_table);
175 old_table_offset = s->refcount_table_offset;
176 old_table_size = s->refcount_table_size;
177 s->refcount_table = new_table;
178 s->refcount_table_size = new_table_size;
179 s->refcount_table_offset = table_offset;
181 update_refcount(bs, table_offset, new_table_size2, 1);
182 qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
185 qcow2_free_clusters(bs, table_offset, new_table_size2);
186 qemu_free(new_table);
191 static int64_t alloc_refcount_block(BlockDriverState *bs, int64_t cluster_index)
193 BDRVQcowState *s = bs->opaque;
194 int64_t offset, refcount_block_offset;
195 int ret, refcount_table_index;
197 int cache = cache_refcount_updates;
199 /* Find L1 index and grow refcount table if needed */
200 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
201 if (refcount_table_index >= s->refcount_table_size) {
202 ret = grow_refcount_table(bs, refcount_table_index + 1);
207 /* Load or allocate the refcount block */
208 refcount_block_offset = s->refcount_table[refcount_table_index];
209 if (!refcount_block_offset) {
210 if (cache_refcount_updates) {
211 write_refcount_block(s);
212 cache_refcount_updates = 0;
214 /* create a new refcount block */
215 /* Note: we cannot update the refcount now to avoid recursion */
216 offset = alloc_clusters_noref(bs, s->cluster_size);
217 memset(s->refcount_block_cache, 0, s->cluster_size);
218 ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->cluster_size);
219 if (ret != s->cluster_size)
221 s->refcount_table[refcount_table_index] = offset;
222 data64 = cpu_to_be64(offset);
223 ret = bdrv_pwrite(s->hd, s->refcount_table_offset +
224 refcount_table_index * sizeof(uint64_t),
225 &data64, sizeof(data64));
226 if (ret != sizeof(data64))
229 refcount_block_offset = offset;
230 s->refcount_block_cache_offset = offset;
231 update_refcount(bs, offset, s->cluster_size, 1);
232 cache_refcount_updates = cache;
234 if (refcount_block_offset != s->refcount_block_cache_offset) {
235 if (load_refcount_block(bs, refcount_block_offset) < 0)
240 return refcount_block_offset;
243 #define REFCOUNTS_PER_SECTOR (512 >> REFCOUNT_SHIFT)
244 static int write_refcount_block_entries(BDRVQcowState *s,
245 int64_t refcount_block_offset, int first_index, int last_index)
249 if (cache_refcount_updates) {
253 first_index &= ~(REFCOUNTS_PER_SECTOR - 1);
254 last_index = (last_index + REFCOUNTS_PER_SECTOR)
255 & ~(REFCOUNTS_PER_SECTOR - 1);
257 size = (last_index - first_index) << REFCOUNT_SHIFT;
258 if (bdrv_pwrite(s->hd,
259 refcount_block_offset + (first_index << REFCOUNT_SHIFT),
260 &s->refcount_block_cache[first_index], size) != size)
268 /* XXX: cache several refcount block clusters ? */
269 static int update_refcount(BlockDriverState *bs,
270 int64_t offset, int64_t length,
273 BDRVQcowState *s = bs->opaque;
274 int64_t start, last, cluster_offset;
275 int64_t refcount_block_offset = 0;
276 int64_t table_index = -1, old_table_index;
277 int first_index = -1, last_index = -1;
280 printf("update_refcount: offset=%" PRId64 " size=%" PRId64 " addend=%d\n",
281 offset, length, addend);
285 start = offset & ~(s->cluster_size - 1);
286 last = (offset + length - 1) & ~(s->cluster_size - 1);
287 for(cluster_offset = start; cluster_offset <= last;
288 cluster_offset += s->cluster_size)
290 int block_index, refcount;
291 int64_t cluster_index = cluster_offset >> s->cluster_bits;
293 /* Only write refcount block to disk when we are done with it */
294 old_table_index = table_index;
295 table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
296 if ((old_table_index >= 0) && (table_index != old_table_index)) {
298 if (write_refcount_block_entries(s, refcount_block_offset,
299 first_index, last_index) < 0)
308 /* Load the refcount block and allocate it if needed */
309 refcount_block_offset = alloc_refcount_block(bs, cluster_index);
310 if (refcount_block_offset < 0) {
311 return refcount_block_offset;
314 /* we can update the count and save it */
315 block_index = cluster_index &
316 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
317 if (first_index == -1 || block_index < first_index) {
318 first_index = block_index;
320 if (block_index > last_index) {
321 last_index = block_index;
324 refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
326 if (refcount < 0 || refcount > 0xffff)
328 if (refcount == 0 && cluster_index < s->free_cluster_index) {
329 s->free_cluster_index = cluster_index;
331 s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
334 /* Write last changed block to disk */
335 if (refcount_block_offset != 0) {
336 if (write_refcount_block_entries(s, refcount_block_offset,
337 first_index, last_index) < 0)
346 /* addend must be 1 or -1 */
347 static int update_cluster_refcount(BlockDriverState *bs,
348 int64_t cluster_index,
351 BDRVQcowState *s = bs->opaque;
354 ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend);
359 return get_refcount(bs, cluster_index);
364 /*********************************************************/
365 /* cluster allocation functions */
369 /* return < 0 if error */
370 static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
372 BDRVQcowState *s = bs->opaque;
375 nb_clusters = size_to_clusters(s, size);
377 for(i = 0; i < nb_clusters; i++) {
378 int64_t i = s->free_cluster_index++;
379 if (get_refcount(bs, i) != 0)
383 printf("alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
385 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
387 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
390 int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size)
394 offset = alloc_clusters_noref(bs, size);
395 update_refcount(bs, offset, size, 1);
399 /* only used to allocate compressed sectors. We try to allocate
400 contiguous sectors. size must be <= cluster_size */
401 int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
403 BDRVQcowState *s = bs->opaque;
404 int64_t offset, cluster_offset;
407 assert(size > 0 && size <= s->cluster_size);
408 if (s->free_byte_offset == 0) {
409 s->free_byte_offset = qcow2_alloc_clusters(bs, s->cluster_size);
412 free_in_cluster = s->cluster_size -
413 (s->free_byte_offset & (s->cluster_size - 1));
414 if (size <= free_in_cluster) {
415 /* enough space in current cluster */
416 offset = s->free_byte_offset;
417 s->free_byte_offset += size;
418 free_in_cluster -= size;
419 if (free_in_cluster == 0)
420 s->free_byte_offset = 0;
421 if ((offset & (s->cluster_size - 1)) != 0)
422 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
424 offset = qcow2_alloc_clusters(bs, s->cluster_size);
425 cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
426 if ((cluster_offset + s->cluster_size) == offset) {
427 /* we are lucky: contiguous data */
428 offset = s->free_byte_offset;
429 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
430 s->free_byte_offset += size;
432 s->free_byte_offset = offset;
439 void qcow2_free_clusters(BlockDriverState *bs,
440 int64_t offset, int64_t size)
442 update_refcount(bs, offset, size, -1);
448 * free clusters according to its type: compressed or not
452 void qcow2_free_any_clusters(BlockDriverState *bs,
453 uint64_t cluster_offset, int nb_clusters)
455 BDRVQcowState *s = bs->opaque;
457 /* free the cluster */
459 if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
461 nb_csectors = ((cluster_offset >> s->csize_shift) &
463 qcow2_free_clusters(bs,
464 (cluster_offset & s->cluster_offset_mask) & ~511,
469 qcow2_free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
476 /*********************************************************/
477 /* snapshots and image creation */
481 void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
485 int64_t start, last, cluster_offset;
488 start = offset & ~(s->cluster_size - 1);
489 last = (offset + size - 1) & ~(s->cluster_size - 1);
490 for(cluster_offset = start; cluster_offset <= last;
491 cluster_offset += s->cluster_size) {
492 p = &s->refcount_block[cluster_offset >> s->cluster_bits];
493 refcount = be16_to_cpu(*p);
495 *p = cpu_to_be16(refcount);
499 /* update the refcounts of snapshots and the copied flag */
500 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
501 int64_t l1_table_offset, int l1_size, int addend)
503 BDRVQcowState *s = bs->opaque;
504 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
505 int64_t old_offset, old_l2_offset;
506 int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
508 qcow2_l2_cache_reset(bs);
509 cache_refcount_updates = 1;
513 l1_size2 = l1_size * sizeof(uint64_t);
515 if (l1_table_offset != s->l1_table_offset) {
516 l1_table = qemu_mallocz(align_offset(l1_size2, 512));
518 if (bdrv_pread(s->hd, l1_table_offset,
519 l1_table, l1_size2) != l1_size2)
521 for(i = 0;i < l1_size; i++)
522 be64_to_cpus(&l1_table[i]);
524 assert(l1_size == s->l1_size);
525 l1_table = s->l1_table;
529 l2_size = s->l2_size * sizeof(uint64_t);
530 l2_table = qemu_malloc(l2_size);
532 for(i = 0; i < l1_size; i++) {
533 l2_offset = l1_table[i];
535 old_l2_offset = l2_offset;
536 l2_offset &= ~QCOW_OFLAG_COPIED;
538 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
540 for(j = 0; j < s->l2_size; j++) {
541 offset = be64_to_cpu(l2_table[j]);
544 offset &= ~QCOW_OFLAG_COPIED;
545 if (offset & QCOW_OFLAG_COMPRESSED) {
546 nb_csectors = ((offset >> s->csize_shift) &
549 update_refcount(bs, (offset & s->cluster_offset_mask) & ~511,
550 nb_csectors * 512, addend);
551 /* compressed clusters are never modified */
555 refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
557 refcount = get_refcount(bs, offset >> s->cluster_bits);
562 offset |= QCOW_OFLAG_COPIED;
564 if (offset != old_offset) {
565 l2_table[j] = cpu_to_be64(offset);
571 if (bdrv_pwrite(s->hd,
572 l2_offset, l2_table, l2_size) != l2_size)
577 refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
579 refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
582 l2_offset |= QCOW_OFLAG_COPIED;
584 if (l2_offset != old_l2_offset) {
585 l1_table[i] = l2_offset;
591 for(i = 0; i < l1_size; i++)
592 cpu_to_be64s(&l1_table[i]);
593 if (bdrv_pwrite(s->hd, l1_table_offset, l1_table,
594 l1_size2) != l1_size2)
596 for(i = 0; i < l1_size; i++)
597 be64_to_cpus(&l1_table[i]);
602 cache_refcount_updates = 0;
603 write_refcount_block(s);
609 cache_refcount_updates = 0;
610 write_refcount_block(s);
617 /*********************************************************/
618 /* refcount checking functions */
623 * Increases the refcount for a range of clusters in a given refcount table.
624 * This is used to construct a temporary refcount table out of L1 and L2 tables
625 * which can be compared the the refcount table saved in the image.
627 * Returns the number of errors in the image that were found
629 static int inc_refcounts(BlockDriverState *bs,
630 uint16_t *refcount_table,
631 int refcount_table_size,
632 int64_t offset, int64_t size)
634 BDRVQcowState *s = bs->opaque;
635 int64_t start, last, cluster_offset;
642 start = offset & ~(s->cluster_size - 1);
643 last = (offset + size - 1) & ~(s->cluster_size - 1);
644 for(cluster_offset = start; cluster_offset <= last;
645 cluster_offset += s->cluster_size) {
646 k = cluster_offset >> s->cluster_bits;
647 if (k < 0 || k >= refcount_table_size) {
648 fprintf(stderr, "ERROR: invalid cluster offset=0x%" PRIx64 "\n",
652 if (++refcount_table[k] == 0) {
653 fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
654 "\n", cluster_offset);
664 * Increases the refcount in the given refcount table for the all clusters
665 * referenced in the L2 table. While doing so, performs some checks on L2
668 * Returns the number of errors found by the checks or -errno if an internal
671 static int check_refcounts_l2(BlockDriverState *bs,
672 uint16_t *refcount_table, int refcount_table_size, int64_t l2_offset,
675 BDRVQcowState *s = bs->opaque;
676 uint64_t *l2_table, offset;
677 int i, l2_size, nb_csectors, refcount;
680 /* Read L2 table from disk */
681 l2_size = s->l2_size * sizeof(uint64_t);
682 l2_table = qemu_malloc(l2_size);
684 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
687 /* Do the actual checks */
688 for(i = 0; i < s->l2_size; i++) {
689 offset = be64_to_cpu(l2_table[i]);
691 if (offset & QCOW_OFLAG_COMPRESSED) {
692 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
693 if (offset & QCOW_OFLAG_COPIED) {
694 fprintf(stderr, "ERROR: cluster %" PRId64 ": "
695 "copied flag must never be set for compressed "
696 "clusters\n", offset >> s->cluster_bits);
697 offset &= ~QCOW_OFLAG_COPIED;
701 /* Mark cluster as used */
702 nb_csectors = ((offset >> s->csize_shift) &
704 offset &= s->cluster_offset_mask;
705 errors += inc_refcounts(bs, refcount_table,
707 offset & ~511, nb_csectors * 512);
709 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
711 uint64_t entry = offset;
712 offset &= ~QCOW_OFLAG_COPIED;
713 refcount = get_refcount(bs, offset >> s->cluster_bits);
714 if ((refcount == 1) != ((entry & QCOW_OFLAG_COPIED) != 0)) {
715 fprintf(stderr, "ERROR OFLAG_COPIED: offset=%"
716 PRIx64 " refcount=%d\n", entry, refcount);
721 /* Mark cluster as used */
722 offset &= ~QCOW_OFLAG_COPIED;
723 errors += inc_refcounts(bs, refcount_table,
725 offset, s->cluster_size);
727 /* Correct offsets are cluster aligned */
728 if (offset & (s->cluster_size - 1)) {
729 fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
730 "properly aligned; L2 entry corrupted.\n", offset);
741 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
747 * Increases the refcount for the L1 table, its L2 tables and all referenced
748 * clusters in the given refcount table. While doing so, performs some checks
749 * on L1 and L2 entries.
751 * Returns the number of errors found by the checks or -errno if an internal
754 static int check_refcounts_l1(BlockDriverState *bs,
755 uint16_t *refcount_table,
756 int refcount_table_size,
757 int64_t l1_table_offset, int l1_size,
760 BDRVQcowState *s = bs->opaque;
761 uint64_t *l1_table, l2_offset, l1_size2;
762 int i, refcount, ret;
765 l1_size2 = l1_size * sizeof(uint64_t);
767 /* Mark L1 table as used */
768 errors += inc_refcounts(bs, refcount_table, refcount_table_size,
769 l1_table_offset, l1_size2);
771 /* Read L1 table entries from disk */
772 l1_table = qemu_malloc(l1_size2);
773 if (bdrv_pread(s->hd, l1_table_offset,
774 l1_table, l1_size2) != l1_size2)
776 for(i = 0;i < l1_size; i++)
777 be64_to_cpus(&l1_table[i]);
779 /* Do the actual checks */
780 for(i = 0; i < l1_size; i++) {
781 l2_offset = l1_table[i];
783 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
785 refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
787 if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
788 fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
789 " refcount=%d\n", l2_offset, refcount);
794 /* Mark L2 table as used */
795 l2_offset &= ~QCOW_OFLAG_COPIED;
796 errors += inc_refcounts(bs, refcount_table,
801 /* L2 tables are cluster aligned */
802 if (l2_offset & (s->cluster_size - 1)) {
803 fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
804 "cluster aligned; L1 entry corrupted\n", l2_offset);
808 /* Process and check L2 entries */
809 ret = check_refcounts_l2(bs, refcount_table, refcount_table_size,
810 l2_offset, check_copied);
821 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
827 * Checks an image for refcount consistency.
829 * Returns 0 if no errors are found, the number of errors in case the image is
830 * detected as corrupted, and -errno when an internal error occured.
832 int qcow2_check_refcounts(BlockDriverState *bs)
834 BDRVQcowState *s = bs->opaque;
836 int nb_clusters, refcount1, refcount2, i;
838 uint16_t *refcount_table;
841 size = bdrv_getlength(s->hd);
842 nb_clusters = size_to_clusters(s, size);
843 refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
846 errors += inc_refcounts(bs, refcount_table, nb_clusters,
849 /* current L1 table */
850 ret = check_refcounts_l1(bs, refcount_table, nb_clusters,
851 s->l1_table_offset, s->l1_size, 1);
858 for(i = 0; i < s->nb_snapshots; i++) {
859 sn = s->snapshots + i;
860 check_refcounts_l1(bs, refcount_table, nb_clusters,
861 sn->l1_table_offset, sn->l1_size, 0);
863 errors += inc_refcounts(bs, refcount_table, nb_clusters,
864 s->snapshots_offset, s->snapshots_size);
867 errors += inc_refcounts(bs, refcount_table, nb_clusters,
868 s->refcount_table_offset,
869 s->refcount_table_size * sizeof(uint64_t));
870 for(i = 0; i < s->refcount_table_size; i++) {
872 offset = s->refcount_table[i];
874 errors += inc_refcounts(bs, refcount_table, nb_clusters,
875 offset, s->cluster_size);
879 /* compare ref counts */
880 for(i = 0; i < nb_clusters; i++) {
881 refcount1 = get_refcount(bs, i);
882 refcount2 = refcount_table[i];
883 if (refcount1 != refcount2) {
884 fprintf(stderr, "ERROR cluster %d refcount=%d reference=%d\n",
885 i, refcount1, refcount2);
890 qemu_free(refcount_table);