qcow2: Factor next_refcount_table_size out
[qemu.git] / block / qcow2-refcount.c
blob933311c5ec309f4473d4abe224b3647f5cec6781
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]);
127 * Rounds the refcount table size up to avoid growing the table for each single
128 * refcount block that is allocated.
130 static unsigned int next_refcount_table_size(BDRVQcowState *s,
131 unsigned int min_size)
133 unsigned int min_clusters = (min_size >> (s->cluster_bits - 3)) + 1;
134 unsigned int refcount_table_clusters =
135 MAX(1, s->refcount_table_size >> (s->cluster_bits - 3));
137 while (min_clusters > refcount_table_clusters) {
138 refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
141 return refcount_table_clusters << (s->cluster_bits - 3);
144 static int grow_refcount_table(BlockDriverState *bs, int min_size)
146 BDRVQcowState *s = bs->opaque;
147 int new_table_size, new_table_size2, refcount_table_clusters, i, ret;
148 uint64_t *new_table;
149 int64_t table_offset;
150 uint8_t data[12];
151 int old_table_size;
152 int64_t old_table_offset;
154 if (min_size <= s->refcount_table_size)
155 return 0;
156 /* compute new table size */
157 new_table_size = next_refcount_table_size(s, min_size);
158 #ifdef DEBUG_ALLOC2
159 printf("grow_refcount_table from %d to %d\n",
160 s->refcount_table_size,
161 new_table_size);
162 #endif
163 new_table_size2 = new_table_size * sizeof(uint64_t);
164 new_table = qemu_mallocz(new_table_size2);
165 memcpy(new_table, s->refcount_table,
166 s->refcount_table_size * sizeof(uint64_t));
167 for(i = 0; i < s->refcount_table_size; i++)
168 cpu_to_be64s(&new_table[i]);
169 /* Note: we cannot update the refcount now to avoid recursion */
170 table_offset = alloc_clusters_noref(bs, new_table_size2);
171 ret = bdrv_pwrite(s->hd, table_offset, new_table, new_table_size2);
172 if (ret != new_table_size2)
173 goto fail;
174 for(i = 0; i < s->refcount_table_size; i++)
175 be64_to_cpus(&new_table[i]);
177 cpu_to_be64w((uint64_t*)data, table_offset);
178 cpu_to_be32w((uint32_t*)(data + 8), refcount_table_clusters);
179 ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_offset),
180 data, sizeof(data));
181 if (ret != sizeof(data)) {
182 goto fail;
185 qemu_free(s->refcount_table);
186 old_table_offset = s->refcount_table_offset;
187 old_table_size = s->refcount_table_size;
188 s->refcount_table = new_table;
189 s->refcount_table_size = new_table_size;
190 s->refcount_table_offset = table_offset;
192 update_refcount(bs, table_offset, new_table_size2, 1);
193 qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
194 return 0;
195 fail:
196 qemu_free(new_table);
197 return ret < 0 ? ret : -EIO;
201 static int64_t alloc_refcount_block(BlockDriverState *bs, int64_t cluster_index)
203 BDRVQcowState *s = bs->opaque;
204 int64_t offset, refcount_block_offset;
205 unsigned int refcount_table_index;
206 int ret;
207 uint64_t data64;
208 int cache = cache_refcount_updates;
210 /* Find L1 index and grow refcount table if needed */
211 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
212 if (refcount_table_index >= s->refcount_table_size) {
213 ret = grow_refcount_table(bs, refcount_table_index + 1);
214 if (ret < 0)
215 return ret;
218 /* Load or allocate the refcount block */
219 refcount_block_offset = s->refcount_table[refcount_table_index];
220 if (!refcount_block_offset) {
221 if (cache_refcount_updates) {
222 write_refcount_block(s);
223 cache_refcount_updates = 0;
225 /* create a new refcount block */
226 /* Note: we cannot update the refcount now to avoid recursion */
227 offset = alloc_clusters_noref(bs, s->cluster_size);
228 memset(s->refcount_block_cache, 0, s->cluster_size);
229 ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->cluster_size);
230 if (ret != s->cluster_size)
231 return -EINVAL;
232 s->refcount_table[refcount_table_index] = offset;
233 data64 = cpu_to_be64(offset);
234 ret = bdrv_pwrite(s->hd, s->refcount_table_offset +
235 refcount_table_index * sizeof(uint64_t),
236 &data64, sizeof(data64));
237 if (ret != sizeof(data64))
238 return -EINVAL;
240 refcount_block_offset = offset;
241 s->refcount_block_cache_offset = offset;
242 update_refcount(bs, offset, s->cluster_size, 1);
243 cache_refcount_updates = cache;
244 } else {
245 if (refcount_block_offset != s->refcount_block_cache_offset) {
246 if (load_refcount_block(bs, refcount_block_offset) < 0)
247 return -EIO;
251 return refcount_block_offset;
254 #define REFCOUNTS_PER_SECTOR (512 >> REFCOUNT_SHIFT)
255 static int write_refcount_block_entries(BDRVQcowState *s,
256 int64_t refcount_block_offset, int first_index, int last_index)
258 size_t size;
260 if (cache_refcount_updates) {
261 return 0;
264 first_index &= ~(REFCOUNTS_PER_SECTOR - 1);
265 last_index = (last_index + REFCOUNTS_PER_SECTOR)
266 & ~(REFCOUNTS_PER_SECTOR - 1);
268 size = (last_index - first_index) << REFCOUNT_SHIFT;
269 if (bdrv_pwrite(s->hd,
270 refcount_block_offset + (first_index << REFCOUNT_SHIFT),
271 &s->refcount_block_cache[first_index], size) != size)
273 return -EIO;
276 return 0;
279 /* XXX: cache several refcount block clusters ? */
280 static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
281 int64_t offset, int64_t length, int addend)
283 BDRVQcowState *s = bs->opaque;
284 int64_t start, last, cluster_offset;
285 int64_t refcount_block_offset = 0;
286 int64_t table_index = -1, old_table_index;
287 int first_index = -1, last_index = -1;
288 int ret;
290 #ifdef DEBUG_ALLOC2
291 printf("update_refcount: offset=%" PRId64 " size=%" PRId64 " addend=%d\n",
292 offset, length, addend);
293 #endif
294 if (length < 0) {
295 return -EINVAL;
296 } else if (length == 0) {
297 return 0;
300 start = offset & ~(s->cluster_size - 1);
301 last = (offset + length - 1) & ~(s->cluster_size - 1);
302 for(cluster_offset = start; cluster_offset <= last;
303 cluster_offset += s->cluster_size)
305 int block_index, refcount;
306 int64_t cluster_index = cluster_offset >> s->cluster_bits;
307 int64_t new_block;
309 /* Only write refcount block to disk when we are done with it */
310 old_table_index = table_index;
311 table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
312 if ((old_table_index >= 0) && (table_index != old_table_index)) {
314 if (write_refcount_block_entries(s, refcount_block_offset,
315 first_index, last_index) < 0)
317 return -EIO;
320 first_index = -1;
321 last_index = -1;
324 /* Load the refcount block and allocate it if needed */
325 new_block = alloc_refcount_block(bs, cluster_index);
326 if (new_block < 0) {
327 ret = new_block;
328 goto fail;
330 refcount_block_offset = new_block;
332 /* we can update the count and save it */
333 block_index = cluster_index &
334 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
335 if (first_index == -1 || block_index < first_index) {
336 first_index = block_index;
338 if (block_index > last_index) {
339 last_index = block_index;
342 refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
343 refcount += addend;
344 if (refcount < 0 || refcount > 0xffff) {
345 ret = -EINVAL;
346 goto fail;
348 if (refcount == 0 && cluster_index < s->free_cluster_index) {
349 s->free_cluster_index = cluster_index;
351 s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
354 ret = 0;
355 fail:
357 /* Write last changed block to disk */
358 if (refcount_block_offset != 0) {
359 if (write_refcount_block_entries(s, refcount_block_offset,
360 first_index, last_index) < 0)
362 return ret < 0 ? ret : -EIO;
367 * Try do undo any updates if an error is returned (This may succeed in
368 * some cases like ENOSPC for allocating a new refcount block)
370 if (ret < 0) {
371 int dummy;
372 dummy = update_refcount(bs, offset, cluster_offset - offset, -addend);
375 return ret;
378 /* addend must be 1 or -1 */
379 static int update_cluster_refcount(BlockDriverState *bs,
380 int64_t cluster_index,
381 int addend)
383 BDRVQcowState *s = bs->opaque;
384 int ret;
386 ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend);
387 if (ret < 0) {
388 return ret;
391 return get_refcount(bs, cluster_index);
396 /*********************************************************/
397 /* cluster allocation functions */
401 /* return < 0 if error */
402 static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
404 BDRVQcowState *s = bs->opaque;
405 int i, nb_clusters;
407 nb_clusters = size_to_clusters(s, size);
408 retry:
409 for(i = 0; i < nb_clusters; i++) {
410 int64_t i = s->free_cluster_index++;
411 if (get_refcount(bs, i) != 0)
412 goto retry;
414 #ifdef DEBUG_ALLOC2
415 printf("alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
416 size,
417 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
418 #endif
419 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
422 int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size)
424 int64_t offset;
425 int ret;
427 offset = alloc_clusters_noref(bs, size);
428 ret = update_refcount(bs, offset, size, 1);
429 if (ret < 0) {
430 return ret;
432 return offset;
435 /* only used to allocate compressed sectors. We try to allocate
436 contiguous sectors. size must be <= cluster_size */
437 int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
439 BDRVQcowState *s = bs->opaque;
440 int64_t offset, cluster_offset;
441 int free_in_cluster;
443 assert(size > 0 && size <= s->cluster_size);
444 if (s->free_byte_offset == 0) {
445 s->free_byte_offset = qcow2_alloc_clusters(bs, s->cluster_size);
446 if (s->free_byte_offset < 0) {
447 return s->free_byte_offset;
450 redo:
451 free_in_cluster = s->cluster_size -
452 (s->free_byte_offset & (s->cluster_size - 1));
453 if (size <= free_in_cluster) {
454 /* enough space in current cluster */
455 offset = s->free_byte_offset;
456 s->free_byte_offset += size;
457 free_in_cluster -= size;
458 if (free_in_cluster == 0)
459 s->free_byte_offset = 0;
460 if ((offset & (s->cluster_size - 1)) != 0)
461 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
462 } else {
463 offset = qcow2_alloc_clusters(bs, s->cluster_size);
464 if (offset < 0) {
465 return offset;
467 cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
468 if ((cluster_offset + s->cluster_size) == offset) {
469 /* we are lucky: contiguous data */
470 offset = s->free_byte_offset;
471 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
472 s->free_byte_offset += size;
473 } else {
474 s->free_byte_offset = offset;
475 goto redo;
478 return offset;
481 void qcow2_free_clusters(BlockDriverState *bs,
482 int64_t offset, int64_t size)
484 int ret;
486 ret = update_refcount(bs, offset, size, -1);
487 if (ret < 0) {
488 fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
489 abort();
494 * free_any_clusters
496 * free clusters according to its type: compressed or not
500 void qcow2_free_any_clusters(BlockDriverState *bs,
501 uint64_t cluster_offset, int nb_clusters)
503 BDRVQcowState *s = bs->opaque;
505 /* free the cluster */
507 if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
508 int nb_csectors;
509 nb_csectors = ((cluster_offset >> s->csize_shift) &
510 s->csize_mask) + 1;
511 qcow2_free_clusters(bs,
512 (cluster_offset & s->cluster_offset_mask) & ~511,
513 nb_csectors * 512);
514 return;
517 qcow2_free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
519 return;
524 /*********************************************************/
525 /* snapshots and image creation */
529 void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
530 int64_t size)
532 int refcount;
533 int64_t start, last, cluster_offset;
534 uint16_t *p;
536 start = offset & ~(s->cluster_size - 1);
537 last = (offset + size - 1) & ~(s->cluster_size - 1);
538 for(cluster_offset = start; cluster_offset <= last;
539 cluster_offset += s->cluster_size) {
540 p = &s->refcount_block[cluster_offset >> s->cluster_bits];
541 refcount = be16_to_cpu(*p);
542 refcount++;
543 *p = cpu_to_be16(refcount);
547 /* update the refcounts of snapshots and the copied flag */
548 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
549 int64_t l1_table_offset, int l1_size, int addend)
551 BDRVQcowState *s = bs->opaque;
552 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
553 int64_t old_offset, old_l2_offset;
554 int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
556 qcow2_l2_cache_reset(bs);
557 cache_refcount_updates = 1;
559 l2_table = NULL;
560 l1_table = NULL;
561 l1_size2 = l1_size * sizeof(uint64_t);
562 if (l1_table_offset != s->l1_table_offset) {
563 if (l1_size2 != 0) {
564 l1_table = qemu_mallocz(align_offset(l1_size2, 512));
565 } else {
566 l1_table = NULL;
568 l1_allocated = 1;
569 if (bdrv_pread(s->hd, l1_table_offset,
570 l1_table, l1_size2) != l1_size2)
571 goto fail;
572 for(i = 0;i < l1_size; i++)
573 be64_to_cpus(&l1_table[i]);
574 } else {
575 assert(l1_size == s->l1_size);
576 l1_table = s->l1_table;
577 l1_allocated = 0;
580 l2_size = s->l2_size * sizeof(uint64_t);
581 l2_table = qemu_malloc(l2_size);
582 l1_modified = 0;
583 for(i = 0; i < l1_size; i++) {
584 l2_offset = l1_table[i];
585 if (l2_offset) {
586 old_l2_offset = l2_offset;
587 l2_offset &= ~QCOW_OFLAG_COPIED;
588 l2_modified = 0;
589 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
590 goto fail;
591 for(j = 0; j < s->l2_size; j++) {
592 offset = be64_to_cpu(l2_table[j]);
593 if (offset != 0) {
594 old_offset = offset;
595 offset &= ~QCOW_OFLAG_COPIED;
596 if (offset & QCOW_OFLAG_COMPRESSED) {
597 nb_csectors = ((offset >> s->csize_shift) &
598 s->csize_mask) + 1;
599 if (addend != 0) {
600 int ret;
601 ret = update_refcount(bs,
602 (offset & s->cluster_offset_mask) & ~511,
603 nb_csectors * 512, addend);
604 if (ret < 0) {
605 goto fail;
608 /* compressed clusters are never modified */
609 refcount = 2;
610 } else {
611 if (addend != 0) {
612 refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
613 } else {
614 refcount = get_refcount(bs, offset >> s->cluster_bits);
618 if (refcount == 1) {
619 offset |= QCOW_OFLAG_COPIED;
621 if (offset != old_offset) {
622 l2_table[j] = cpu_to_be64(offset);
623 l2_modified = 1;
627 if (l2_modified) {
628 if (bdrv_pwrite(s->hd,
629 l2_offset, l2_table, l2_size) != l2_size)
630 goto fail;
633 if (addend != 0) {
634 refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
635 } else {
636 refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
638 if (refcount == 1) {
639 l2_offset |= QCOW_OFLAG_COPIED;
641 if (l2_offset != old_l2_offset) {
642 l1_table[i] = l2_offset;
643 l1_modified = 1;
647 if (l1_modified) {
648 for(i = 0; i < l1_size; i++)
649 cpu_to_be64s(&l1_table[i]);
650 if (bdrv_pwrite(s->hd, l1_table_offset, l1_table,
651 l1_size2) != l1_size2)
652 goto fail;
653 for(i = 0; i < l1_size; i++)
654 be64_to_cpus(&l1_table[i]);
656 if (l1_allocated)
657 qemu_free(l1_table);
658 qemu_free(l2_table);
659 cache_refcount_updates = 0;
660 write_refcount_block(s);
661 return 0;
662 fail:
663 if (l1_allocated)
664 qemu_free(l1_table);
665 qemu_free(l2_table);
666 cache_refcount_updates = 0;
667 write_refcount_block(s);
668 return -EIO;
674 /*********************************************************/
675 /* refcount checking functions */
680 * Increases the refcount for a range of clusters in a given refcount table.
681 * This is used to construct a temporary refcount table out of L1 and L2 tables
682 * which can be compared the the refcount table saved in the image.
684 * Returns the number of errors in the image that were found
686 static int inc_refcounts(BlockDriverState *bs,
687 uint16_t *refcount_table,
688 int refcount_table_size,
689 int64_t offset, int64_t size)
691 BDRVQcowState *s = bs->opaque;
692 int64_t start, last, cluster_offset;
693 int k;
694 int errors = 0;
696 if (size <= 0)
697 return 0;
699 start = offset & ~(s->cluster_size - 1);
700 last = (offset + size - 1) & ~(s->cluster_size - 1);
701 for(cluster_offset = start; cluster_offset <= last;
702 cluster_offset += s->cluster_size) {
703 k = cluster_offset >> s->cluster_bits;
704 if (k < 0 || k >= refcount_table_size) {
705 fprintf(stderr, "ERROR: invalid cluster offset=0x%" PRIx64 "\n",
706 cluster_offset);
707 errors++;
708 } else {
709 if (++refcount_table[k] == 0) {
710 fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
711 "\n", cluster_offset);
712 errors++;
717 return errors;
721 * Increases the refcount in the given refcount table for the all clusters
722 * referenced in the L2 table. While doing so, performs some checks on L2
723 * entries.
725 * Returns the number of errors found by the checks or -errno if an internal
726 * error occurred.
728 static int check_refcounts_l2(BlockDriverState *bs,
729 uint16_t *refcount_table, int refcount_table_size, int64_t l2_offset,
730 int check_copied)
732 BDRVQcowState *s = bs->opaque;
733 uint64_t *l2_table, offset;
734 int i, l2_size, nb_csectors, refcount;
735 int errors = 0;
737 /* Read L2 table from disk */
738 l2_size = s->l2_size * sizeof(uint64_t);
739 l2_table = qemu_malloc(l2_size);
741 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
742 goto fail;
744 /* Do the actual checks */
745 for(i = 0; i < s->l2_size; i++) {
746 offset = be64_to_cpu(l2_table[i]);
747 if (offset != 0) {
748 if (offset & QCOW_OFLAG_COMPRESSED) {
749 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
750 if (offset & QCOW_OFLAG_COPIED) {
751 fprintf(stderr, "ERROR: cluster %" PRId64 ": "
752 "copied flag must never be set for compressed "
753 "clusters\n", offset >> s->cluster_bits);
754 offset &= ~QCOW_OFLAG_COPIED;
755 errors++;
758 /* Mark cluster as used */
759 nb_csectors = ((offset >> s->csize_shift) &
760 s->csize_mask) + 1;
761 offset &= s->cluster_offset_mask;
762 errors += inc_refcounts(bs, refcount_table,
763 refcount_table_size,
764 offset & ~511, nb_csectors * 512);
765 } else {
766 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
767 if (check_copied) {
768 uint64_t entry = offset;
769 offset &= ~QCOW_OFLAG_COPIED;
770 refcount = get_refcount(bs, offset >> s->cluster_bits);
771 if ((refcount == 1) != ((entry & QCOW_OFLAG_COPIED) != 0)) {
772 fprintf(stderr, "ERROR OFLAG_COPIED: offset=%"
773 PRIx64 " refcount=%d\n", entry, refcount);
774 errors++;
778 /* Mark cluster as used */
779 offset &= ~QCOW_OFLAG_COPIED;
780 errors += inc_refcounts(bs, refcount_table,
781 refcount_table_size,
782 offset, s->cluster_size);
784 /* Correct offsets are cluster aligned */
785 if (offset & (s->cluster_size - 1)) {
786 fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
787 "properly aligned; L2 entry corrupted.\n", offset);
788 errors++;
794 qemu_free(l2_table);
795 return errors;
797 fail:
798 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
799 qemu_free(l2_table);
800 return -EIO;
804 * Increases the refcount for the L1 table, its L2 tables and all referenced
805 * clusters in the given refcount table. While doing so, performs some checks
806 * on L1 and L2 entries.
808 * Returns the number of errors found by the checks or -errno if an internal
809 * error occurred.
811 static int check_refcounts_l1(BlockDriverState *bs,
812 uint16_t *refcount_table,
813 int refcount_table_size,
814 int64_t l1_table_offset, int l1_size,
815 int check_copied)
817 BDRVQcowState *s = bs->opaque;
818 uint64_t *l1_table, l2_offset, l1_size2;
819 int i, refcount, ret;
820 int errors = 0;
822 l1_size2 = l1_size * sizeof(uint64_t);
824 /* Mark L1 table as used */
825 errors += inc_refcounts(bs, refcount_table, refcount_table_size,
826 l1_table_offset, l1_size2);
828 /* Read L1 table entries from disk */
829 if (l1_size2 == 0) {
830 l1_table = NULL;
831 } else {
832 l1_table = qemu_malloc(l1_size2);
833 if (bdrv_pread(s->hd, l1_table_offset,
834 l1_table, l1_size2) != l1_size2)
835 goto fail;
836 for(i = 0;i < l1_size; i++)
837 be64_to_cpus(&l1_table[i]);
840 /* Do the actual checks */
841 for(i = 0; i < l1_size; i++) {
842 l2_offset = l1_table[i];
843 if (l2_offset) {
844 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
845 if (check_copied) {
846 refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
847 >> s->cluster_bits);
848 if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
849 fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
850 " refcount=%d\n", l2_offset, refcount);
851 errors++;
855 /* Mark L2 table as used */
856 l2_offset &= ~QCOW_OFLAG_COPIED;
857 errors += inc_refcounts(bs, refcount_table,
858 refcount_table_size,
859 l2_offset,
860 s->cluster_size);
862 /* L2 tables are cluster aligned */
863 if (l2_offset & (s->cluster_size - 1)) {
864 fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
865 "cluster aligned; L1 entry corrupted\n", l2_offset);
866 errors++;
869 /* Process and check L2 entries */
870 ret = check_refcounts_l2(bs, refcount_table, refcount_table_size,
871 l2_offset, check_copied);
872 if (ret < 0) {
873 goto fail;
875 errors += ret;
878 qemu_free(l1_table);
879 return errors;
881 fail:
882 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
883 qemu_free(l1_table);
884 return -EIO;
888 * Checks an image for refcount consistency.
890 * Returns 0 if no errors are found, the number of errors in case the image is
891 * detected as corrupted, and -errno when an internal error occured.
893 int qcow2_check_refcounts(BlockDriverState *bs)
895 BDRVQcowState *s = bs->opaque;
896 int64_t size;
897 int nb_clusters, refcount1, refcount2, i;
898 QCowSnapshot *sn;
899 uint16_t *refcount_table;
900 int ret, errors = 0;
902 size = bdrv_getlength(s->hd);
903 nb_clusters = size_to_clusters(s, size);
904 refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
906 /* header */
907 errors += inc_refcounts(bs, refcount_table, nb_clusters,
908 0, s->cluster_size);
910 /* current L1 table */
911 ret = check_refcounts_l1(bs, refcount_table, nb_clusters,
912 s->l1_table_offset, s->l1_size, 1);
913 if (ret < 0) {
914 return ret;
916 errors += ret;
918 /* snapshots */
919 for(i = 0; i < s->nb_snapshots; i++) {
920 sn = s->snapshots + i;
921 check_refcounts_l1(bs, refcount_table, nb_clusters,
922 sn->l1_table_offset, sn->l1_size, 0);
924 errors += inc_refcounts(bs, refcount_table, nb_clusters,
925 s->snapshots_offset, s->snapshots_size);
927 /* refcount data */
928 errors += inc_refcounts(bs, refcount_table, nb_clusters,
929 s->refcount_table_offset,
930 s->refcount_table_size * sizeof(uint64_t));
931 for(i = 0; i < s->refcount_table_size; i++) {
932 int64_t offset;
933 offset = s->refcount_table[i];
934 if (offset != 0) {
935 errors += inc_refcounts(bs, refcount_table, nb_clusters,
936 offset, s->cluster_size);
940 /* compare ref counts */
941 for(i = 0; i < nb_clusters; i++) {
942 refcount1 = get_refcount(bs, i);
943 refcount2 = refcount_table[i];
944 if (refcount1 != refcount2) {
945 fprintf(stderr, "ERROR cluster %d refcount=%d reference=%d\n",
946 i, refcount1, refcount2);
947 errors++;
951 qemu_free(refcount_table);
953 return errors;