qcow2: Improve error handling in update_refcount
[qemu/kraxel.git] / block / qcow2-refcount.c
bloba84620ffe2681e141bac2e7b6a8df254100f1cf0
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 ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_offset),
172 data, sizeof(data));
173 if (ret != sizeof(data)) {
174 goto fail;
177 qemu_free(s->refcount_table);
178 old_table_offset = s->refcount_table_offset;
179 old_table_size = s->refcount_table_size;
180 s->refcount_table = new_table;
181 s->refcount_table_size = new_table_size;
182 s->refcount_table_offset = table_offset;
184 update_refcount(bs, table_offset, new_table_size2, 1);
185 qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
186 return 0;
187 fail:
188 qemu_free(new_table);
189 return ret < 0 ? ret : -EIO;
193 static int64_t alloc_refcount_block(BlockDriverState *bs, int64_t cluster_index)
195 BDRVQcowState *s = bs->opaque;
196 int64_t offset, refcount_block_offset;
197 unsigned int refcount_table_index;
198 int ret;
199 uint64_t data64;
200 int cache = cache_refcount_updates;
202 /* Find L1 index and grow refcount table if needed */
203 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
204 if (refcount_table_index >= s->refcount_table_size) {
205 ret = grow_refcount_table(bs, refcount_table_index + 1);
206 if (ret < 0)
207 return ret;
210 /* Load or allocate the refcount block */
211 refcount_block_offset = s->refcount_table[refcount_table_index];
212 if (!refcount_block_offset) {
213 if (cache_refcount_updates) {
214 write_refcount_block(s);
215 cache_refcount_updates = 0;
217 /* create a new refcount block */
218 /* Note: we cannot update the refcount now to avoid recursion */
219 offset = alloc_clusters_noref(bs, s->cluster_size);
220 memset(s->refcount_block_cache, 0, s->cluster_size);
221 ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->cluster_size);
222 if (ret != s->cluster_size)
223 return -EINVAL;
224 s->refcount_table[refcount_table_index] = offset;
225 data64 = cpu_to_be64(offset);
226 ret = bdrv_pwrite(s->hd, s->refcount_table_offset +
227 refcount_table_index * sizeof(uint64_t),
228 &data64, sizeof(data64));
229 if (ret != sizeof(data64))
230 return -EINVAL;
232 refcount_block_offset = offset;
233 s->refcount_block_cache_offset = offset;
234 update_refcount(bs, offset, s->cluster_size, 1);
235 cache_refcount_updates = cache;
236 } else {
237 if (refcount_block_offset != s->refcount_block_cache_offset) {
238 if (load_refcount_block(bs, refcount_block_offset) < 0)
239 return -EIO;
243 return refcount_block_offset;
246 #define REFCOUNTS_PER_SECTOR (512 >> REFCOUNT_SHIFT)
247 static int write_refcount_block_entries(BDRVQcowState *s,
248 int64_t refcount_block_offset, int first_index, int last_index)
250 size_t size;
252 if (cache_refcount_updates) {
253 return 0;
256 first_index &= ~(REFCOUNTS_PER_SECTOR - 1);
257 last_index = (last_index + REFCOUNTS_PER_SECTOR)
258 & ~(REFCOUNTS_PER_SECTOR - 1);
260 size = (last_index - first_index) << REFCOUNT_SHIFT;
261 if (bdrv_pwrite(s->hd,
262 refcount_block_offset + (first_index << REFCOUNT_SHIFT),
263 &s->refcount_block_cache[first_index], size) != size)
265 return -EIO;
268 return 0;
271 /* XXX: cache several refcount block clusters ? */
272 static int update_refcount(BlockDriverState *bs,
273 int64_t offset, int64_t length,
274 int addend)
276 BDRVQcowState *s = bs->opaque;
277 int64_t start, last, cluster_offset;
278 int64_t refcount_block_offset = 0;
279 int64_t table_index = -1, old_table_index;
280 int first_index = -1, last_index = -1;
281 int ret;
283 #ifdef DEBUG_ALLOC2
284 printf("update_refcount: offset=%" PRId64 " size=%" PRId64 " addend=%d\n",
285 offset, length, addend);
286 #endif
287 if (length <= 0)
288 return -EINVAL;
289 start = offset & ~(s->cluster_size - 1);
290 last = (offset + length - 1) & ~(s->cluster_size - 1);
291 for(cluster_offset = start; cluster_offset <= last;
292 cluster_offset += s->cluster_size)
294 int block_index, refcount;
295 int64_t cluster_index = cluster_offset >> s->cluster_bits;
296 int64_t new_block;
298 /* Only write refcount block to disk when we are done with it */
299 old_table_index = table_index;
300 table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
301 if ((old_table_index >= 0) && (table_index != old_table_index)) {
303 if (write_refcount_block_entries(s, refcount_block_offset,
304 first_index, last_index) < 0)
306 return -EIO;
309 first_index = -1;
310 last_index = -1;
313 /* Load the refcount block and allocate it if needed */
314 new_block = alloc_refcount_block(bs, cluster_index);
315 if (new_block < 0) {
316 ret = new_block;
317 goto fail;
319 refcount_block_offset = new_block;
321 /* we can update the count and save it */
322 block_index = cluster_index &
323 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
324 if (first_index == -1 || block_index < first_index) {
325 first_index = block_index;
327 if (block_index > last_index) {
328 last_index = block_index;
331 refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
332 refcount += addend;
333 if (refcount < 0 || refcount > 0xffff) {
334 ret = -EINVAL;
335 goto fail;
337 if (refcount == 0 && cluster_index < s->free_cluster_index) {
338 s->free_cluster_index = cluster_index;
340 s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
343 ret = 0;
344 fail:
346 /* Write last changed block to disk */
347 if (refcount_block_offset != 0) {
348 if (write_refcount_block_entries(s, refcount_block_offset,
349 first_index, last_index) < 0)
351 return ret < 0 ? ret : -EIO;
356 * Try do undo any updates if an error is returned (This may succeed in
357 * some cases like ENOSPC for allocating a new refcount block)
359 if (ret < 0) {
360 int dummy;
361 dummy = update_refcount(bs, offset, cluster_offset - offset, -addend);
364 return ret;
367 /* addend must be 1 or -1 */
368 static int update_cluster_refcount(BlockDriverState *bs,
369 int64_t cluster_index,
370 int addend)
372 BDRVQcowState *s = bs->opaque;
373 int ret;
375 ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend);
376 if (ret < 0) {
377 return ret;
380 return get_refcount(bs, cluster_index);
385 /*********************************************************/
386 /* cluster allocation functions */
390 /* return < 0 if error */
391 static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
393 BDRVQcowState *s = bs->opaque;
394 int i, nb_clusters;
396 nb_clusters = size_to_clusters(s, size);
397 retry:
398 for(i = 0; i < nb_clusters; i++) {
399 int64_t i = s->free_cluster_index++;
400 if (get_refcount(bs, i) != 0)
401 goto retry;
403 #ifdef DEBUG_ALLOC2
404 printf("alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
405 size,
406 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
407 #endif
408 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
411 int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size)
413 int64_t offset;
415 offset = alloc_clusters_noref(bs, size);
416 update_refcount(bs, offset, size, 1);
417 return offset;
420 /* only used to allocate compressed sectors. We try to allocate
421 contiguous sectors. size must be <= cluster_size */
422 int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
424 BDRVQcowState *s = bs->opaque;
425 int64_t offset, cluster_offset;
426 int free_in_cluster;
428 assert(size > 0 && size <= s->cluster_size);
429 if (s->free_byte_offset == 0) {
430 s->free_byte_offset = qcow2_alloc_clusters(bs, s->cluster_size);
432 redo:
433 free_in_cluster = s->cluster_size -
434 (s->free_byte_offset & (s->cluster_size - 1));
435 if (size <= free_in_cluster) {
436 /* enough space in current cluster */
437 offset = s->free_byte_offset;
438 s->free_byte_offset += size;
439 free_in_cluster -= size;
440 if (free_in_cluster == 0)
441 s->free_byte_offset = 0;
442 if ((offset & (s->cluster_size - 1)) != 0)
443 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
444 } else {
445 offset = qcow2_alloc_clusters(bs, s->cluster_size);
446 cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
447 if ((cluster_offset + s->cluster_size) == offset) {
448 /* we are lucky: contiguous data */
449 offset = s->free_byte_offset;
450 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
451 s->free_byte_offset += size;
452 } else {
453 s->free_byte_offset = offset;
454 goto redo;
457 return offset;
460 void qcow2_free_clusters(BlockDriverState *bs,
461 int64_t offset, int64_t size)
463 update_refcount(bs, offset, size, -1);
467 * free_any_clusters
469 * free clusters according to its type: compressed or not
473 void qcow2_free_any_clusters(BlockDriverState *bs,
474 uint64_t cluster_offset, int nb_clusters)
476 BDRVQcowState *s = bs->opaque;
478 /* free the cluster */
480 if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
481 int nb_csectors;
482 nb_csectors = ((cluster_offset >> s->csize_shift) &
483 s->csize_mask) + 1;
484 qcow2_free_clusters(bs,
485 (cluster_offset & s->cluster_offset_mask) & ~511,
486 nb_csectors * 512);
487 return;
490 qcow2_free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
492 return;
497 /*********************************************************/
498 /* snapshots and image creation */
502 void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
503 int64_t size)
505 int refcount;
506 int64_t start, last, cluster_offset;
507 uint16_t *p;
509 start = offset & ~(s->cluster_size - 1);
510 last = (offset + size - 1) & ~(s->cluster_size - 1);
511 for(cluster_offset = start; cluster_offset <= last;
512 cluster_offset += s->cluster_size) {
513 p = &s->refcount_block[cluster_offset >> s->cluster_bits];
514 refcount = be16_to_cpu(*p);
515 refcount++;
516 *p = cpu_to_be16(refcount);
520 /* update the refcounts of snapshots and the copied flag */
521 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
522 int64_t l1_table_offset, int l1_size, int addend)
524 BDRVQcowState *s = bs->opaque;
525 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
526 int64_t old_offset, old_l2_offset;
527 int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
529 qcow2_l2_cache_reset(bs);
530 cache_refcount_updates = 1;
532 l2_table = NULL;
533 l1_table = NULL;
534 l1_size2 = l1_size * sizeof(uint64_t);
535 if (l1_table_offset != s->l1_table_offset) {
536 if (l1_size2 != 0) {
537 l1_table = qemu_mallocz(align_offset(l1_size2, 512));
538 } else {
539 l1_table = NULL;
541 l1_allocated = 1;
542 if (bdrv_pread(s->hd, l1_table_offset,
543 l1_table, l1_size2) != l1_size2)
544 goto fail;
545 for(i = 0;i < l1_size; i++)
546 be64_to_cpus(&l1_table[i]);
547 } else {
548 assert(l1_size == s->l1_size);
549 l1_table = s->l1_table;
550 l1_allocated = 0;
553 l2_size = s->l2_size * sizeof(uint64_t);
554 l2_table = qemu_malloc(l2_size);
555 l1_modified = 0;
556 for(i = 0; i < l1_size; i++) {
557 l2_offset = l1_table[i];
558 if (l2_offset) {
559 old_l2_offset = l2_offset;
560 l2_offset &= ~QCOW_OFLAG_COPIED;
561 l2_modified = 0;
562 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
563 goto fail;
564 for(j = 0; j < s->l2_size; j++) {
565 offset = be64_to_cpu(l2_table[j]);
566 if (offset != 0) {
567 old_offset = offset;
568 offset &= ~QCOW_OFLAG_COPIED;
569 if (offset & QCOW_OFLAG_COMPRESSED) {
570 nb_csectors = ((offset >> s->csize_shift) &
571 s->csize_mask) + 1;
572 if (addend != 0)
573 update_refcount(bs, (offset & s->cluster_offset_mask) & ~511,
574 nb_csectors * 512, addend);
575 /* compressed clusters are never modified */
576 refcount = 2;
577 } else {
578 if (addend != 0) {
579 refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
580 } else {
581 refcount = get_refcount(bs, offset >> s->cluster_bits);
585 if (refcount == 1) {
586 offset |= QCOW_OFLAG_COPIED;
588 if (offset != old_offset) {
589 l2_table[j] = cpu_to_be64(offset);
590 l2_modified = 1;
594 if (l2_modified) {
595 if (bdrv_pwrite(s->hd,
596 l2_offset, l2_table, l2_size) != l2_size)
597 goto fail;
600 if (addend != 0) {
601 refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
602 } else {
603 refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
605 if (refcount == 1) {
606 l2_offset |= QCOW_OFLAG_COPIED;
608 if (l2_offset != old_l2_offset) {
609 l1_table[i] = l2_offset;
610 l1_modified = 1;
614 if (l1_modified) {
615 for(i = 0; i < l1_size; i++)
616 cpu_to_be64s(&l1_table[i]);
617 if (bdrv_pwrite(s->hd, l1_table_offset, l1_table,
618 l1_size2) != l1_size2)
619 goto fail;
620 for(i = 0; i < l1_size; i++)
621 be64_to_cpus(&l1_table[i]);
623 if (l1_allocated)
624 qemu_free(l1_table);
625 qemu_free(l2_table);
626 cache_refcount_updates = 0;
627 write_refcount_block(s);
628 return 0;
629 fail:
630 if (l1_allocated)
631 qemu_free(l1_table);
632 qemu_free(l2_table);
633 cache_refcount_updates = 0;
634 write_refcount_block(s);
635 return -EIO;
641 /*********************************************************/
642 /* refcount checking functions */
647 * Increases the refcount for a range of clusters in a given refcount table.
648 * This is used to construct a temporary refcount table out of L1 and L2 tables
649 * which can be compared the the refcount table saved in the image.
651 * Returns the number of errors in the image that were found
653 static int inc_refcounts(BlockDriverState *bs,
654 uint16_t *refcount_table,
655 int refcount_table_size,
656 int64_t offset, int64_t size)
658 BDRVQcowState *s = bs->opaque;
659 int64_t start, last, cluster_offset;
660 int k;
661 int errors = 0;
663 if (size <= 0)
664 return 0;
666 start = offset & ~(s->cluster_size - 1);
667 last = (offset + size - 1) & ~(s->cluster_size - 1);
668 for(cluster_offset = start; cluster_offset <= last;
669 cluster_offset += s->cluster_size) {
670 k = cluster_offset >> s->cluster_bits;
671 if (k < 0 || k >= refcount_table_size) {
672 fprintf(stderr, "ERROR: invalid cluster offset=0x%" PRIx64 "\n",
673 cluster_offset);
674 errors++;
675 } else {
676 if (++refcount_table[k] == 0) {
677 fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
678 "\n", cluster_offset);
679 errors++;
684 return errors;
688 * Increases the refcount in the given refcount table for the all clusters
689 * referenced in the L2 table. While doing so, performs some checks on L2
690 * entries.
692 * Returns the number of errors found by the checks or -errno if an internal
693 * error occurred.
695 static int check_refcounts_l2(BlockDriverState *bs,
696 uint16_t *refcount_table, int refcount_table_size, int64_t l2_offset,
697 int check_copied)
699 BDRVQcowState *s = bs->opaque;
700 uint64_t *l2_table, offset;
701 int i, l2_size, nb_csectors, refcount;
702 int errors = 0;
704 /* Read L2 table from disk */
705 l2_size = s->l2_size * sizeof(uint64_t);
706 l2_table = qemu_malloc(l2_size);
708 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
709 goto fail;
711 /* Do the actual checks */
712 for(i = 0; i < s->l2_size; i++) {
713 offset = be64_to_cpu(l2_table[i]);
714 if (offset != 0) {
715 if (offset & QCOW_OFLAG_COMPRESSED) {
716 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
717 if (offset & QCOW_OFLAG_COPIED) {
718 fprintf(stderr, "ERROR: cluster %" PRId64 ": "
719 "copied flag must never be set for compressed "
720 "clusters\n", offset >> s->cluster_bits);
721 offset &= ~QCOW_OFLAG_COPIED;
722 errors++;
725 /* Mark cluster as used */
726 nb_csectors = ((offset >> s->csize_shift) &
727 s->csize_mask) + 1;
728 offset &= s->cluster_offset_mask;
729 errors += inc_refcounts(bs, refcount_table,
730 refcount_table_size,
731 offset & ~511, nb_csectors * 512);
732 } else {
733 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
734 if (check_copied) {
735 uint64_t entry = offset;
736 offset &= ~QCOW_OFLAG_COPIED;
737 refcount = get_refcount(bs, offset >> s->cluster_bits);
738 if ((refcount == 1) != ((entry & QCOW_OFLAG_COPIED) != 0)) {
739 fprintf(stderr, "ERROR OFLAG_COPIED: offset=%"
740 PRIx64 " refcount=%d\n", entry, refcount);
741 errors++;
745 /* Mark cluster as used */
746 offset &= ~QCOW_OFLAG_COPIED;
747 errors += inc_refcounts(bs, refcount_table,
748 refcount_table_size,
749 offset, s->cluster_size);
751 /* Correct offsets are cluster aligned */
752 if (offset & (s->cluster_size - 1)) {
753 fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
754 "properly aligned; L2 entry corrupted.\n", offset);
755 errors++;
761 qemu_free(l2_table);
762 return errors;
764 fail:
765 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
766 qemu_free(l2_table);
767 return -EIO;
771 * Increases the refcount for the L1 table, its L2 tables and all referenced
772 * clusters in the given refcount table. While doing so, performs some checks
773 * on L1 and L2 entries.
775 * Returns the number of errors found by the checks or -errno if an internal
776 * error occurred.
778 static int check_refcounts_l1(BlockDriverState *bs,
779 uint16_t *refcount_table,
780 int refcount_table_size,
781 int64_t l1_table_offset, int l1_size,
782 int check_copied)
784 BDRVQcowState *s = bs->opaque;
785 uint64_t *l1_table, l2_offset, l1_size2;
786 int i, refcount, ret;
787 int errors = 0;
789 l1_size2 = l1_size * sizeof(uint64_t);
791 /* Mark L1 table as used */
792 errors += inc_refcounts(bs, refcount_table, refcount_table_size,
793 l1_table_offset, l1_size2);
795 /* Read L1 table entries from disk */
796 if (l1_size2 == 0) {
797 l1_table = NULL;
798 } else {
799 l1_table = qemu_malloc(l1_size2);
800 if (bdrv_pread(s->hd, l1_table_offset,
801 l1_table, l1_size2) != l1_size2)
802 goto fail;
803 for(i = 0;i < l1_size; i++)
804 be64_to_cpus(&l1_table[i]);
807 /* Do the actual checks */
808 for(i = 0; i < l1_size; i++) {
809 l2_offset = l1_table[i];
810 if (l2_offset) {
811 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
812 if (check_copied) {
813 refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
814 >> s->cluster_bits);
815 if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
816 fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
817 " refcount=%d\n", l2_offset, refcount);
818 errors++;
822 /* Mark L2 table as used */
823 l2_offset &= ~QCOW_OFLAG_COPIED;
824 errors += inc_refcounts(bs, refcount_table,
825 refcount_table_size,
826 l2_offset,
827 s->cluster_size);
829 /* L2 tables are cluster aligned */
830 if (l2_offset & (s->cluster_size - 1)) {
831 fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
832 "cluster aligned; L1 entry corrupted\n", l2_offset);
833 errors++;
836 /* Process and check L2 entries */
837 ret = check_refcounts_l2(bs, refcount_table, refcount_table_size,
838 l2_offset, check_copied);
839 if (ret < 0) {
840 goto fail;
842 errors += ret;
845 qemu_free(l1_table);
846 return errors;
848 fail:
849 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
850 qemu_free(l1_table);
851 return -EIO;
855 * Checks an image for refcount consistency.
857 * Returns 0 if no errors are found, the number of errors in case the image is
858 * detected as corrupted, and -errno when an internal error occured.
860 int qcow2_check_refcounts(BlockDriverState *bs)
862 BDRVQcowState *s = bs->opaque;
863 int64_t size;
864 int nb_clusters, refcount1, refcount2, i;
865 QCowSnapshot *sn;
866 uint16_t *refcount_table;
867 int ret, errors = 0;
869 size = bdrv_getlength(s->hd);
870 nb_clusters = size_to_clusters(s, size);
871 refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
873 /* header */
874 errors += inc_refcounts(bs, refcount_table, nb_clusters,
875 0, s->cluster_size);
877 /* current L1 table */
878 ret = check_refcounts_l1(bs, refcount_table, nb_clusters,
879 s->l1_table_offset, s->l1_size, 1);
880 if (ret < 0) {
881 return ret;
883 errors += ret;
885 /* snapshots */
886 for(i = 0; i < s->nb_snapshots; i++) {
887 sn = s->snapshots + i;
888 check_refcounts_l1(bs, refcount_table, nb_clusters,
889 sn->l1_table_offset, sn->l1_size, 0);
891 errors += inc_refcounts(bs, refcount_table, nb_clusters,
892 s->snapshots_offset, s->snapshots_size);
894 /* refcount data */
895 errors += inc_refcounts(bs, refcount_table, nb_clusters,
896 s->refcount_table_offset,
897 s->refcount_table_size * sizeof(uint64_t));
898 for(i = 0; i < s->refcount_table_size; i++) {
899 int64_t offset;
900 offset = s->refcount_table[i];
901 if (offset != 0) {
902 errors += inc_refcounts(bs, refcount_table, nb_clusters,
903 offset, s->cluster_size);
907 /* compare ref counts */
908 for(i = 0; i < nb_clusters; i++) {
909 refcount1 = get_refcount(bs, i);
910 refcount2 = refcount_table[i];
911 if (refcount1 != refcount2) {
912 fprintf(stderr, "ERROR cluster %d refcount=%d reference=%d\n",
913 i, refcount1, refcount2);
914 errors++;
918 qemu_free(refcount_table);
920 return errors;