Unbreak USB autoconnect filters
[qemu.git] / block / qcow2-refcount.c
blob609eee1891beefcb1f95b7059a8e371d77c7998f
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.
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,
32 int addend);
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) {
42 return 0;
45 if (bdrv_pwrite(s->hd, s->refcount_block_cache_offset,
46 s->refcount_block_cache, size) != size)
48 return -EIO;
51 return 0;
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)
69 goto fail;
70 for(i = 0; i < s->refcount_table_size; i++)
71 be64_to_cpus(&s->refcount_table[i]);
73 return 0;
74 fail:
75 return -ENOMEM;
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;
90 int ret;
92 if (cache_refcount_updates) {
93 write_refcount_block(s);
96 ret = bdrv_pread(s->hd, refcount_block_offset, s->refcount_block_cache,
97 s->cluster_size);
98 if (ret != s->cluster_size)
99 return -EIO;
100 s->refcount_block_cache_offset = refcount_block_offset;
101 return 0;
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)
112 return 0;
113 refcount_block_offset = s->refcount_table[refcount_table_index];
114 if (!refcount_block_offset)
115 return 0;
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)
119 return 1;
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;
130 uint64_t *new_table;
131 int64_t table_offset;
132 uint8_t data[12];
133 int old_table_size;
134 int64_t old_table_offset;
136 if (min_size <= s->refcount_table_size)
137 return 0;
138 /* compute new table size */
139 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
140 for(;;) {
141 if (refcount_table_clusters == 0) {
142 refcount_table_clusters = 1;
143 } else {
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)
148 break;
150 #ifdef DEBUG_ALLOC2
151 printf("grow_refcount_table from %d to %d\n",
152 s->refcount_table_size,
153 new_table_size);
154 #endif
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)
165 goto fail;
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))
173 goto fail;
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));
183 return 0;
184 fail:
185 qcow2_free_clusters(bs, table_offset, new_table_size2);
186 qemu_free(new_table);
187 return -EIO;
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 unsigned int refcount_table_index;
196 int ret;
197 uint64_t data64;
198 int cache = cache_refcount_updates;
200 /* Find L1 index and grow refcount table if needed */
201 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
202 if (refcount_table_index >= s->refcount_table_size) {
203 ret = grow_refcount_table(bs, refcount_table_index + 1);
204 if (ret < 0)
205 return ret;
208 /* Load or allocate the refcount block */
209 refcount_block_offset = s->refcount_table[refcount_table_index];
210 if (!refcount_block_offset) {
211 if (cache_refcount_updates) {
212 write_refcount_block(s);
213 cache_refcount_updates = 0;
215 /* create a new refcount block */
216 /* Note: we cannot update the refcount now to avoid recursion */
217 offset = alloc_clusters_noref(bs, s->cluster_size);
218 memset(s->refcount_block_cache, 0, s->cluster_size);
219 ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->cluster_size);
220 if (ret != s->cluster_size)
221 return -EINVAL;
222 s->refcount_table[refcount_table_index] = offset;
223 data64 = cpu_to_be64(offset);
224 ret = bdrv_pwrite(s->hd, s->refcount_table_offset +
225 refcount_table_index * sizeof(uint64_t),
226 &data64, sizeof(data64));
227 if (ret != sizeof(data64))
228 return -EINVAL;
230 refcount_block_offset = offset;
231 s->refcount_block_cache_offset = offset;
232 update_refcount(bs, offset, s->cluster_size, 1);
233 cache_refcount_updates = cache;
234 } else {
235 if (refcount_block_offset != s->refcount_block_cache_offset) {
236 if (load_refcount_block(bs, refcount_block_offset) < 0)
237 return -EIO;
241 return refcount_block_offset;
244 #define REFCOUNTS_PER_SECTOR (512 >> REFCOUNT_SHIFT)
245 static int write_refcount_block_entries(BDRVQcowState *s,
246 int64_t refcount_block_offset, int first_index, int last_index)
248 size_t size;
250 if (cache_refcount_updates) {
251 return 0;
254 first_index &= ~(REFCOUNTS_PER_SECTOR - 1);
255 last_index = (last_index + REFCOUNTS_PER_SECTOR)
256 & ~(REFCOUNTS_PER_SECTOR - 1);
258 size = (last_index - first_index) << REFCOUNT_SHIFT;
259 if (bdrv_pwrite(s->hd,
260 refcount_block_offset + (first_index << REFCOUNT_SHIFT),
261 &s->refcount_block_cache[first_index], size) != size)
263 return -EIO;
266 return 0;
269 /* XXX: cache several refcount block clusters ? */
270 static int update_refcount(BlockDriverState *bs,
271 int64_t offset, int64_t length,
272 int addend)
274 BDRVQcowState *s = bs->opaque;
275 int64_t start, last, cluster_offset;
276 int64_t refcount_block_offset = 0;
277 int64_t table_index = -1, old_table_index;
278 int first_index = -1, last_index = -1;
280 #ifdef DEBUG_ALLOC2
281 printf("update_refcount: offset=%" PRId64 " size=%" PRId64 " addend=%d\n",
282 offset, length, addend);
283 #endif
284 if (length <= 0)
285 return -EINVAL;
286 start = offset & ~(s->cluster_size - 1);
287 last = (offset + length - 1) & ~(s->cluster_size - 1);
288 for(cluster_offset = start; cluster_offset <= last;
289 cluster_offset += s->cluster_size)
291 int block_index, refcount;
292 int64_t cluster_index = cluster_offset >> s->cluster_bits;
294 /* Only write refcount block to disk when we are done with it */
295 old_table_index = table_index;
296 table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
297 if ((old_table_index >= 0) && (table_index != old_table_index)) {
299 if (write_refcount_block_entries(s, refcount_block_offset,
300 first_index, last_index) < 0)
302 return -EIO;
305 first_index = -1;
306 last_index = -1;
309 /* Load the refcount block and allocate it if needed */
310 refcount_block_offset = alloc_refcount_block(bs, cluster_index);
311 if (refcount_block_offset < 0) {
312 return refcount_block_offset;
315 /* we can update the count and save it */
316 block_index = cluster_index &
317 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
318 if (first_index == -1 || block_index < first_index) {
319 first_index = block_index;
321 if (block_index > last_index) {
322 last_index = block_index;
325 refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
326 refcount += addend;
327 if (refcount < 0 || refcount > 0xffff)
328 return -EINVAL;
329 if (refcount == 0 && cluster_index < s->free_cluster_index) {
330 s->free_cluster_index = cluster_index;
332 s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
335 /* Write last changed block to disk */
336 if (refcount_block_offset != 0) {
337 if (write_refcount_block_entries(s, refcount_block_offset,
338 first_index, last_index) < 0)
340 return -EIO;
344 return 0;
347 /* addend must be 1 or -1 */
348 static int update_cluster_refcount(BlockDriverState *bs,
349 int64_t cluster_index,
350 int addend)
352 BDRVQcowState *s = bs->opaque;
353 int ret;
355 ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend);
356 if (ret < 0) {
357 return ret;
360 return get_refcount(bs, cluster_index);
365 /*********************************************************/
366 /* cluster allocation functions */
370 /* return < 0 if error */
371 static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
373 BDRVQcowState *s = bs->opaque;
374 int i, nb_clusters;
376 nb_clusters = size_to_clusters(s, size);
377 retry:
378 for(i = 0; i < nb_clusters; i++) {
379 int64_t i = s->free_cluster_index++;
380 if (get_refcount(bs, i) != 0)
381 goto retry;
383 #ifdef DEBUG_ALLOC2
384 printf("alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
385 size,
386 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
387 #endif
388 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
391 int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size)
393 int64_t offset;
395 offset = alloc_clusters_noref(bs, size);
396 update_refcount(bs, offset, size, 1);
397 return offset;
400 /* only used to allocate compressed sectors. We try to allocate
401 contiguous sectors. size must be <= cluster_size */
402 int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
404 BDRVQcowState *s = bs->opaque;
405 int64_t offset, cluster_offset;
406 int free_in_cluster;
408 assert(size > 0 && size <= s->cluster_size);
409 if (s->free_byte_offset == 0) {
410 s->free_byte_offset = qcow2_alloc_clusters(bs, s->cluster_size);
412 redo:
413 free_in_cluster = s->cluster_size -
414 (s->free_byte_offset & (s->cluster_size - 1));
415 if (size <= free_in_cluster) {
416 /* enough space in current cluster */
417 offset = s->free_byte_offset;
418 s->free_byte_offset += size;
419 free_in_cluster -= size;
420 if (free_in_cluster == 0)
421 s->free_byte_offset = 0;
422 if ((offset & (s->cluster_size - 1)) != 0)
423 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
424 } else {
425 offset = qcow2_alloc_clusters(bs, s->cluster_size);
426 cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
427 if ((cluster_offset + s->cluster_size) == offset) {
428 /* we are lucky: contiguous data */
429 offset = s->free_byte_offset;
430 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
431 s->free_byte_offset += size;
432 } else {
433 s->free_byte_offset = offset;
434 goto redo;
437 return offset;
440 void qcow2_free_clusters(BlockDriverState *bs,
441 int64_t offset, int64_t size)
443 update_refcount(bs, offset, size, -1);
447 * free_any_clusters
449 * free clusters according to its type: compressed or not
453 void qcow2_free_any_clusters(BlockDriverState *bs,
454 uint64_t cluster_offset, int nb_clusters)
456 BDRVQcowState *s = bs->opaque;
458 /* free the cluster */
460 if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
461 int nb_csectors;
462 nb_csectors = ((cluster_offset >> s->csize_shift) &
463 s->csize_mask) + 1;
464 qcow2_free_clusters(bs,
465 (cluster_offset & s->cluster_offset_mask) & ~511,
466 nb_csectors * 512);
467 return;
470 qcow2_free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
472 return;
477 /*********************************************************/
478 /* snapshots and image creation */
482 void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
483 int64_t size)
485 int refcount;
486 int64_t start, last, cluster_offset;
487 uint16_t *p;
489 start = offset & ~(s->cluster_size - 1);
490 last = (offset + size - 1) & ~(s->cluster_size - 1);
491 for(cluster_offset = start; cluster_offset <= last;
492 cluster_offset += s->cluster_size) {
493 p = &s->refcount_block[cluster_offset >> s->cluster_bits];
494 refcount = be16_to_cpu(*p);
495 refcount++;
496 *p = cpu_to_be16(refcount);
500 /* update the refcounts of snapshots and the copied flag */
501 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
502 int64_t l1_table_offset, int l1_size, int addend)
504 BDRVQcowState *s = bs->opaque;
505 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
506 int64_t old_offset, old_l2_offset;
507 int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
509 qcow2_l2_cache_reset(bs);
510 cache_refcount_updates = 1;
512 l2_table = NULL;
513 l1_table = NULL;
514 l1_size2 = l1_size * sizeof(uint64_t);
515 l1_allocated = 0;
516 if (l1_table_offset != s->l1_table_offset) {
517 l1_table = qemu_mallocz(align_offset(l1_size2, 512));
518 l1_allocated = 1;
519 if (bdrv_pread(s->hd, l1_table_offset,
520 l1_table, l1_size2) != l1_size2)
521 goto fail;
522 for(i = 0;i < l1_size; i++)
523 be64_to_cpus(&l1_table[i]);
524 } else {
525 assert(l1_size == s->l1_size);
526 l1_table = s->l1_table;
527 l1_allocated = 0;
530 l2_size = s->l2_size * sizeof(uint64_t);
531 l2_table = qemu_malloc(l2_size);
532 l1_modified = 0;
533 for(i = 0; i < l1_size; i++) {
534 l2_offset = l1_table[i];
535 if (l2_offset) {
536 old_l2_offset = l2_offset;
537 l2_offset &= ~QCOW_OFLAG_COPIED;
538 l2_modified = 0;
539 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
540 goto fail;
541 for(j = 0; j < s->l2_size; j++) {
542 offset = be64_to_cpu(l2_table[j]);
543 if (offset != 0) {
544 old_offset = offset;
545 offset &= ~QCOW_OFLAG_COPIED;
546 if (offset & QCOW_OFLAG_COMPRESSED) {
547 nb_csectors = ((offset >> s->csize_shift) &
548 s->csize_mask) + 1;
549 if (addend != 0)
550 update_refcount(bs, (offset & s->cluster_offset_mask) & ~511,
551 nb_csectors * 512, addend);
552 /* compressed clusters are never modified */
553 refcount = 2;
554 } else {
555 if (addend != 0) {
556 refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
557 } else {
558 refcount = get_refcount(bs, offset >> s->cluster_bits);
562 if (refcount == 1) {
563 offset |= QCOW_OFLAG_COPIED;
565 if (offset != old_offset) {
566 l2_table[j] = cpu_to_be64(offset);
567 l2_modified = 1;
571 if (l2_modified) {
572 if (bdrv_pwrite(s->hd,
573 l2_offset, l2_table, l2_size) != l2_size)
574 goto fail;
577 if (addend != 0) {
578 refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
579 } else {
580 refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
582 if (refcount == 1) {
583 l2_offset |= QCOW_OFLAG_COPIED;
585 if (l2_offset != old_l2_offset) {
586 l1_table[i] = l2_offset;
587 l1_modified = 1;
591 if (l1_modified) {
592 for(i = 0; i < l1_size; i++)
593 cpu_to_be64s(&l1_table[i]);
594 if (bdrv_pwrite(s->hd, l1_table_offset, l1_table,
595 l1_size2) != l1_size2)
596 goto fail;
597 for(i = 0; i < l1_size; i++)
598 be64_to_cpus(&l1_table[i]);
600 if (l1_allocated)
601 qemu_free(l1_table);
602 qemu_free(l2_table);
603 cache_refcount_updates = 0;
604 write_refcount_block(s);
605 return 0;
606 fail:
607 if (l1_allocated)
608 qemu_free(l1_table);
609 qemu_free(l2_table);
610 cache_refcount_updates = 0;
611 write_refcount_block(s);
612 return -EIO;
618 /*********************************************************/
619 /* refcount checking functions */
624 * Increases the refcount for a range of clusters in a given refcount table.
625 * This is used to construct a temporary refcount table out of L1 and L2 tables
626 * which can be compared the the refcount table saved in the image.
628 * Returns the number of errors in the image that were found
630 static int inc_refcounts(BlockDriverState *bs,
631 uint16_t *refcount_table,
632 int refcount_table_size,
633 int64_t offset, int64_t size)
635 BDRVQcowState *s = bs->opaque;
636 int64_t start, last, cluster_offset;
637 int k;
638 int errors = 0;
640 if (size <= 0)
641 return 0;
643 start = offset & ~(s->cluster_size - 1);
644 last = (offset + size - 1) & ~(s->cluster_size - 1);
645 for(cluster_offset = start; cluster_offset <= last;
646 cluster_offset += s->cluster_size) {
647 k = cluster_offset >> s->cluster_bits;
648 if (k < 0 || k >= refcount_table_size) {
649 fprintf(stderr, "ERROR: invalid cluster offset=0x%" PRIx64 "\n",
650 cluster_offset);
651 errors++;
652 } else {
653 if (++refcount_table[k] == 0) {
654 fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
655 "\n", cluster_offset);
656 errors++;
661 return errors;
665 * Increases the refcount in the given refcount table for the all clusters
666 * referenced in the L2 table. While doing so, performs some checks on L2
667 * entries.
669 * Returns the number of errors found by the checks or -errno if an internal
670 * error occurred.
672 static int check_refcounts_l2(BlockDriverState *bs,
673 uint16_t *refcount_table, int refcount_table_size, int64_t l2_offset,
674 int check_copied)
676 BDRVQcowState *s = bs->opaque;
677 uint64_t *l2_table, offset;
678 int i, l2_size, nb_csectors, refcount;
679 int errors = 0;
681 /* Read L2 table from disk */
682 l2_size = s->l2_size * sizeof(uint64_t);
683 l2_table = qemu_malloc(l2_size);
685 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
686 goto fail;
688 /* Do the actual checks */
689 for(i = 0; i < s->l2_size; i++) {
690 offset = be64_to_cpu(l2_table[i]);
691 if (offset != 0) {
692 if (offset & QCOW_OFLAG_COMPRESSED) {
693 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
694 if (offset & QCOW_OFLAG_COPIED) {
695 fprintf(stderr, "ERROR: cluster %" PRId64 ": "
696 "copied flag must never be set for compressed "
697 "clusters\n", offset >> s->cluster_bits);
698 offset &= ~QCOW_OFLAG_COPIED;
699 errors++;
702 /* Mark cluster as used */
703 nb_csectors = ((offset >> s->csize_shift) &
704 s->csize_mask) + 1;
705 offset &= s->cluster_offset_mask;
706 errors += inc_refcounts(bs, refcount_table,
707 refcount_table_size,
708 offset & ~511, nb_csectors * 512);
709 } else {
710 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
711 if (check_copied) {
712 uint64_t entry = offset;
713 offset &= ~QCOW_OFLAG_COPIED;
714 refcount = get_refcount(bs, offset >> s->cluster_bits);
715 if ((refcount == 1) != ((entry & QCOW_OFLAG_COPIED) != 0)) {
716 fprintf(stderr, "ERROR OFLAG_COPIED: offset=%"
717 PRIx64 " refcount=%d\n", entry, refcount);
718 errors++;
722 /* Mark cluster as used */
723 offset &= ~QCOW_OFLAG_COPIED;
724 errors += inc_refcounts(bs, refcount_table,
725 refcount_table_size,
726 offset, s->cluster_size);
728 /* Correct offsets are cluster aligned */
729 if (offset & (s->cluster_size - 1)) {
730 fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
731 "properly aligned; L2 entry corrupted.\n", offset);
732 errors++;
738 qemu_free(l2_table);
739 return errors;
741 fail:
742 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
743 qemu_free(l2_table);
744 return -EIO;
748 * Increases the refcount for the L1 table, its L2 tables and all referenced
749 * clusters in the given refcount table. While doing so, performs some checks
750 * on L1 and L2 entries.
752 * Returns the number of errors found by the checks or -errno if an internal
753 * error occurred.
755 static int check_refcounts_l1(BlockDriverState *bs,
756 uint16_t *refcount_table,
757 int refcount_table_size,
758 int64_t l1_table_offset, int l1_size,
759 int check_copied)
761 BDRVQcowState *s = bs->opaque;
762 uint64_t *l1_table, l2_offset, l1_size2;
763 int i, refcount, ret;
764 int errors = 0;
766 l1_size2 = l1_size * sizeof(uint64_t);
768 /* Mark L1 table as used */
769 errors += inc_refcounts(bs, refcount_table, refcount_table_size,
770 l1_table_offset, l1_size2);
772 /* Read L1 table entries from disk */
773 l1_table = qemu_malloc(l1_size2);
774 if (bdrv_pread(s->hd, l1_table_offset,
775 l1_table, l1_size2) != l1_size2)
776 goto fail;
777 for(i = 0;i < l1_size; i++)
778 be64_to_cpus(&l1_table[i]);
780 /* Do the actual checks */
781 for(i = 0; i < l1_size; i++) {
782 l2_offset = l1_table[i];
783 if (l2_offset) {
784 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
785 if (check_copied) {
786 refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
787 >> s->cluster_bits);
788 if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
789 fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
790 " refcount=%d\n", l2_offset, refcount);
791 errors++;
795 /* Mark L2 table as used */
796 l2_offset &= ~QCOW_OFLAG_COPIED;
797 errors += inc_refcounts(bs, refcount_table,
798 refcount_table_size,
799 l2_offset,
800 s->cluster_size);
802 /* L2 tables are cluster aligned */
803 if (l2_offset & (s->cluster_size - 1)) {
804 fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
805 "cluster aligned; L1 entry corrupted\n", l2_offset);
806 errors++;
809 /* Process and check L2 entries */
810 ret = check_refcounts_l2(bs, refcount_table, refcount_table_size,
811 l2_offset, check_copied);
812 if (ret < 0) {
813 goto fail;
815 errors += ret;
818 qemu_free(l1_table);
819 return errors;
821 fail:
822 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
823 qemu_free(l1_table);
824 return -EIO;
828 * Checks an image for refcount consistency.
830 * Returns 0 if no errors are found, the number of errors in case the image is
831 * detected as corrupted, and -errno when an internal error occured.
833 int qcow2_check_refcounts(BlockDriverState *bs)
835 BDRVQcowState *s = bs->opaque;
836 int64_t size;
837 int nb_clusters, refcount1, refcount2, i;
838 QCowSnapshot *sn;
839 uint16_t *refcount_table;
840 int ret, errors = 0;
842 size = bdrv_getlength(s->hd);
843 nb_clusters = size_to_clusters(s, size);
844 refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
846 /* header */
847 errors += inc_refcounts(bs, refcount_table, nb_clusters,
848 0, s->cluster_size);
850 /* current L1 table */
851 ret = check_refcounts_l1(bs, refcount_table, nb_clusters,
852 s->l1_table_offset, s->l1_size, 1);
853 if (ret < 0) {
854 return ret;
856 errors += ret;
858 /* snapshots */
859 for(i = 0; i < s->nb_snapshots; i++) {
860 sn = s->snapshots + i;
861 check_refcounts_l1(bs, refcount_table, nb_clusters,
862 sn->l1_table_offset, sn->l1_size, 0);
864 errors += inc_refcounts(bs, refcount_table, nb_clusters,
865 s->snapshots_offset, s->snapshots_size);
867 /* refcount data */
868 errors += inc_refcounts(bs, refcount_table, nb_clusters,
869 s->refcount_table_offset,
870 s->refcount_table_size * sizeof(uint64_t));
871 for(i = 0; i < s->refcount_table_size; i++) {
872 int64_t offset;
873 offset = s->refcount_table[i];
874 if (offset != 0) {
875 errors += inc_refcounts(bs, refcount_table, nb_clusters,
876 offset, s->cluster_size);
880 /* compare ref counts */
881 for(i = 0; i < nb_clusters; i++) {
882 refcount1 = get_refcount(bs, i);
883 refcount2 = refcount_table[i];
884 if (refcount1 != refcount2) {
885 fprintf(stderr, "ERROR cluster %d refcount=%d reference=%d\n",
886 i, refcount1, refcount2);
887 errors++;
891 qemu_free(refcount_table);
893 return errors;