Clean libhw subdirs as well
[qemu/stefanha.git] / block / qcow2-refcount.c
blob744107cd1da5a30bfbf17c8c7f7f2f2222f5c8c2
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 QEMU_WARN_UNUSED_RESULT 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(BlockDriverState *bs)
39 BDRVQcowState *s = bs->opaque;
40 size_t size = s->cluster_size;
42 if (s->refcount_block_cache_offset == 0) {
43 return 0;
46 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE);
47 if (bdrv_pwrite(bs->file, s->refcount_block_cache_offset,
48 s->refcount_block_cache, size) != size)
50 return -EIO;
53 return 0;
56 /*********************************************************/
57 /* refcount handling */
59 int qcow2_refcount_init(BlockDriverState *bs)
61 BDRVQcowState *s = bs->opaque;
62 int ret, refcount_table_size2, i;
64 s->refcount_block_cache = qemu_malloc(s->cluster_size);
65 refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
66 s->refcount_table = qemu_malloc(refcount_table_size2);
67 if (s->refcount_table_size > 0) {
68 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_LOAD);
69 ret = bdrv_pread(bs->file, s->refcount_table_offset,
70 s->refcount_table, refcount_table_size2);
71 if (ret != refcount_table_size2)
72 goto fail;
73 for(i = 0; i < s->refcount_table_size; i++)
74 be64_to_cpus(&s->refcount_table[i]);
76 return 0;
77 fail:
78 return -ENOMEM;
81 void qcow2_refcount_close(BlockDriverState *bs)
83 BDRVQcowState *s = bs->opaque;
84 qemu_free(s->refcount_block_cache);
85 qemu_free(s->refcount_table);
89 static int load_refcount_block(BlockDriverState *bs,
90 int64_t refcount_block_offset)
92 BDRVQcowState *s = bs->opaque;
93 int ret;
95 if (cache_refcount_updates) {
96 write_refcount_block(bs);
99 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_LOAD);
100 ret = bdrv_pread(bs->file, refcount_block_offset, s->refcount_block_cache,
101 s->cluster_size);
102 if (ret != s->cluster_size)
103 return -EIO;
104 s->refcount_block_cache_offset = refcount_block_offset;
105 return 0;
108 static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
110 BDRVQcowState *s = bs->opaque;
111 int refcount_table_index, block_index;
112 int64_t refcount_block_offset;
114 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
115 if (refcount_table_index >= s->refcount_table_size)
116 return 0;
117 refcount_block_offset = s->refcount_table[refcount_table_index];
118 if (!refcount_block_offset)
119 return 0;
120 if (refcount_block_offset != s->refcount_block_cache_offset) {
121 /* better than nothing: return allocated if read error */
122 if (load_refcount_block(bs, refcount_block_offset) < 0)
123 return 1;
125 block_index = cluster_index &
126 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
127 return be16_to_cpu(s->refcount_block_cache[block_index]);
131 * Rounds the refcount table size up to avoid growing the table for each single
132 * refcount block that is allocated.
134 static unsigned int next_refcount_table_size(BDRVQcowState *s,
135 unsigned int min_size)
137 unsigned int min_clusters = (min_size >> (s->cluster_bits - 3)) + 1;
138 unsigned int refcount_table_clusters =
139 MAX(1, s->refcount_table_size >> (s->cluster_bits - 3));
141 while (min_clusters > refcount_table_clusters) {
142 refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
145 return refcount_table_clusters << (s->cluster_bits - 3);
149 /* Checks if two offsets are described by the same refcount block */
150 static int in_same_refcount_block(BDRVQcowState *s, uint64_t offset_a,
151 uint64_t offset_b)
153 uint64_t block_a = offset_a >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
154 uint64_t block_b = offset_b >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
156 return (block_a == block_b);
160 * Loads a refcount block. If it doesn't exist yet, it is allocated first
161 * (including growing the refcount table if needed).
163 * Returns the offset of the refcount block on success or -errno in error case
165 static int64_t alloc_refcount_block(BlockDriverState *bs, int64_t cluster_index)
167 BDRVQcowState *s = bs->opaque;
168 unsigned int refcount_table_index;
169 int ret;
171 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
173 /* Find the refcount block for the given cluster */
174 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
176 if (refcount_table_index < s->refcount_table_size) {
178 uint64_t refcount_block_offset =
179 s->refcount_table[refcount_table_index];
181 /* If it's already there, we're done */
182 if (refcount_block_offset) {
183 if (refcount_block_offset != s->refcount_block_cache_offset) {
184 ret = load_refcount_block(bs, refcount_block_offset);
185 if (ret < 0) {
186 return ret;
189 return refcount_block_offset;
194 * If we came here, we need to allocate something. Something is at least
195 * a cluster for the new refcount block. It may also include a new refcount
196 * table if the old refcount table is too small.
198 * Note that allocating clusters here needs some special care:
200 * - We can't use the normal qcow2_alloc_clusters(), it would try to
201 * increase the refcount and very likely we would end up with an endless
202 * recursion. Instead we must place the refcount blocks in a way that
203 * they can describe them themselves.
205 * - We need to consider that at this point we are inside update_refcounts
206 * and doing the initial refcount increase. This means that some clusters
207 * have already been allocated by the caller, but their refcount isn't
208 * accurate yet. free_cluster_index tells us where this allocation ends
209 * as long as we don't overwrite it by freeing clusters.
211 * - alloc_clusters_noref and qcow2_free_clusters may load a different
212 * refcount block into the cache
215 if (cache_refcount_updates) {
216 ret = write_refcount_block(bs);
217 if (ret < 0) {
218 return ret;
222 /* Allocate the refcount block itself and mark it as used */
223 uint64_t new_block = alloc_clusters_noref(bs, s->cluster_size);
224 memset(s->refcount_block_cache, 0, s->cluster_size);
225 s->refcount_block_cache_offset = new_block;
227 #ifdef DEBUG_ALLOC2
228 fprintf(stderr, "qcow2: Allocate refcount block %d for %" PRIx64
229 " at %" PRIx64 "\n",
230 refcount_table_index, cluster_index << s->cluster_bits, new_block);
231 #endif
233 if (in_same_refcount_block(s, new_block, cluster_index << s->cluster_bits)) {
234 /* The block describes itself, need to update the cache */
235 int block_index = (new_block >> s->cluster_bits) &
236 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
237 s->refcount_block_cache[block_index] = cpu_to_be16(1);
238 } else {
239 /* Described somewhere else. This can recurse at most twice before we
240 * arrive at a block that describes itself. */
241 ret = update_refcount(bs, new_block, s->cluster_size, 1);
242 if (ret < 0) {
243 goto fail_block;
247 /* Now the new refcount block needs to be written to disk */
248 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE);
249 ret = bdrv_pwrite(bs->file, new_block, s->refcount_block_cache,
250 s->cluster_size);
251 if (ret < 0) {
252 goto fail_block;
255 /* If the refcount table is big enough, just hook the block up there */
256 if (refcount_table_index < s->refcount_table_size) {
257 uint64_t data64 = cpu_to_be64(new_block);
258 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_HOOKUP);
259 ret = bdrv_pwrite(bs->file,
260 s->refcount_table_offset + refcount_table_index * sizeof(uint64_t),
261 &data64, sizeof(data64));
262 if (ret < 0) {
263 goto fail_block;
266 s->refcount_table[refcount_table_index] = new_block;
267 return new_block;
271 * If we come here, we need to grow the refcount table. Again, a new
272 * refcount table needs some space and we can't simply allocate to avoid
273 * endless recursion.
275 * Therefore let's grab new refcount blocks at the end of the image, which
276 * will describe themselves and the new refcount table. This way we can
277 * reference them only in the new table and do the switch to the new
278 * refcount table at once without producing an inconsistent state in
279 * between.
281 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW);
283 /* Calculate the number of refcount blocks needed so far */
284 uint64_t refcount_block_clusters = 1 << (s->cluster_bits - REFCOUNT_SHIFT);
285 uint64_t blocks_used = (s->free_cluster_index +
286 refcount_block_clusters - 1) / refcount_block_clusters;
288 /* And now we need at least one block more for the new metadata */
289 uint64_t table_size = next_refcount_table_size(s, blocks_used + 1);
290 uint64_t last_table_size;
291 uint64_t blocks_clusters;
292 do {
293 uint64_t table_clusters = size_to_clusters(s, table_size);
294 blocks_clusters = 1 +
295 ((table_clusters + refcount_block_clusters - 1)
296 / refcount_block_clusters);
297 uint64_t meta_clusters = table_clusters + blocks_clusters;
299 last_table_size = table_size;
300 table_size = next_refcount_table_size(s, blocks_used +
301 ((meta_clusters + refcount_block_clusters - 1)
302 / refcount_block_clusters));
304 } while (last_table_size != table_size);
306 #ifdef DEBUG_ALLOC2
307 fprintf(stderr, "qcow2: Grow refcount table %" PRId32 " => %" PRId64 "\n",
308 s->refcount_table_size, table_size);
309 #endif
311 /* Create the new refcount table and blocks */
312 uint64_t meta_offset = (blocks_used * refcount_block_clusters) *
313 s->cluster_size;
314 uint64_t table_offset = meta_offset + blocks_clusters * s->cluster_size;
315 uint16_t *new_blocks = qemu_mallocz(blocks_clusters * s->cluster_size);
316 uint64_t *new_table = qemu_mallocz(table_size * sizeof(uint64_t));
318 assert(meta_offset >= (s->free_cluster_index * s->cluster_size));
320 /* Fill the new refcount table */
321 memcpy(new_table, s->refcount_table,
322 s->refcount_table_size * sizeof(uint64_t));
323 new_table[refcount_table_index] = new_block;
325 int i;
326 for (i = 0; i < blocks_clusters; i++) {
327 new_table[blocks_used + i] = meta_offset + (i * s->cluster_size);
330 /* Fill the refcount blocks */
331 uint64_t table_clusters = size_to_clusters(s, table_size * sizeof(uint64_t));
332 int block = 0;
333 for (i = 0; i < table_clusters + blocks_clusters; i++) {
334 new_blocks[block++] = cpu_to_be16(1);
337 /* Write refcount blocks to disk */
338 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS);
339 ret = bdrv_pwrite(bs->file, meta_offset, new_blocks,
340 blocks_clusters * s->cluster_size);
341 qemu_free(new_blocks);
342 if (ret < 0) {
343 goto fail_table;
346 /* Write refcount table to disk */
347 for(i = 0; i < table_size; i++) {
348 cpu_to_be64s(&new_table[i]);
351 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE);
352 ret = bdrv_pwrite(bs->file, table_offset, new_table,
353 table_size * sizeof(uint64_t));
354 if (ret < 0) {
355 goto fail_table;
358 for(i = 0; i < table_size; i++) {
359 cpu_to_be64s(&new_table[i]);
362 /* Hook up the new refcount table in the qcow2 header */
363 uint8_t data[12];
364 cpu_to_be64w((uint64_t*)data, table_offset);
365 cpu_to_be32w((uint32_t*)(data + 8), table_clusters);
366 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE);
367 ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, refcount_table_offset),
368 data, sizeof(data));
369 if (ret < 0) {
370 goto fail_table;
373 /* And switch it in memory */
374 uint64_t old_table_offset = s->refcount_table_offset;
375 uint64_t old_table_size = s->refcount_table_size;
377 qemu_free(s->refcount_table);
378 s->refcount_table = new_table;
379 s->refcount_table_size = table_size;
380 s->refcount_table_offset = table_offset;
382 /* Free old table. Remember, we must not change free_cluster_index */
383 uint64_t old_free_cluster_index = s->free_cluster_index;
384 qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
385 s->free_cluster_index = old_free_cluster_index;
387 ret = load_refcount_block(bs, new_block);
388 if (ret < 0) {
389 goto fail_block;
392 return new_block;
394 fail_table:
395 qemu_free(new_table);
396 fail_block:
397 s->refcount_block_cache_offset = 0;
398 return ret;
401 #define REFCOUNTS_PER_SECTOR (512 >> REFCOUNT_SHIFT)
402 static int write_refcount_block_entries(BlockDriverState *bs,
403 int64_t refcount_block_offset, int first_index, int last_index)
405 BDRVQcowState *s = bs->opaque;
406 size_t size;
408 if (cache_refcount_updates) {
409 return 0;
412 first_index &= ~(REFCOUNTS_PER_SECTOR - 1);
413 last_index = (last_index + REFCOUNTS_PER_SECTOR)
414 & ~(REFCOUNTS_PER_SECTOR - 1);
416 size = (last_index - first_index) << REFCOUNT_SHIFT;
417 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART);
418 if (bdrv_pwrite(bs->file,
419 refcount_block_offset + (first_index << REFCOUNT_SHIFT),
420 &s->refcount_block_cache[first_index], size) != size)
422 return -EIO;
425 return 0;
428 /* XXX: cache several refcount block clusters ? */
429 static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
430 int64_t offset, int64_t length, int addend)
432 BDRVQcowState *s = bs->opaque;
433 int64_t start, last, cluster_offset;
434 int64_t refcount_block_offset = 0;
435 int64_t table_index = -1, old_table_index;
436 int first_index = -1, last_index = -1;
437 int ret;
439 #ifdef DEBUG_ALLOC2
440 printf("update_refcount: offset=%" PRId64 " size=%" PRId64 " addend=%d\n",
441 offset, length, addend);
442 #endif
443 if (length < 0) {
444 return -EINVAL;
445 } else if (length == 0) {
446 return 0;
449 start = offset & ~(s->cluster_size - 1);
450 last = (offset + length - 1) & ~(s->cluster_size - 1);
451 for(cluster_offset = start; cluster_offset <= last;
452 cluster_offset += s->cluster_size)
454 int block_index, refcount;
455 int64_t cluster_index = cluster_offset >> s->cluster_bits;
456 int64_t new_block;
458 /* Only write refcount block to disk when we are done with it */
459 old_table_index = table_index;
460 table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
461 if ((old_table_index >= 0) && (table_index != old_table_index)) {
463 if (write_refcount_block_entries(bs, refcount_block_offset,
464 first_index, last_index) < 0)
466 return -EIO;
469 first_index = -1;
470 last_index = -1;
473 /* Load the refcount block and allocate it if needed */
474 new_block = alloc_refcount_block(bs, cluster_index);
475 if (new_block < 0) {
476 ret = new_block;
477 goto fail;
479 refcount_block_offset = new_block;
481 /* we can update the count and save it */
482 block_index = cluster_index &
483 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
484 if (first_index == -1 || block_index < first_index) {
485 first_index = block_index;
487 if (block_index > last_index) {
488 last_index = block_index;
491 refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
492 refcount += addend;
493 if (refcount < 0 || refcount > 0xffff) {
494 ret = -EINVAL;
495 goto fail;
497 if (refcount == 0 && cluster_index < s->free_cluster_index) {
498 s->free_cluster_index = cluster_index;
500 s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
503 ret = 0;
504 fail:
506 /* Write last changed block to disk */
507 if (refcount_block_offset != 0) {
508 if (write_refcount_block_entries(bs, refcount_block_offset,
509 first_index, last_index) < 0)
511 return ret < 0 ? ret : -EIO;
516 * Try do undo any updates if an error is returned (This may succeed in
517 * some cases like ENOSPC for allocating a new refcount block)
519 if (ret < 0) {
520 int dummy;
521 dummy = update_refcount(bs, offset, cluster_offset - offset, -addend);
524 return ret;
527 /* addend must be 1 or -1 */
528 static int update_cluster_refcount(BlockDriverState *bs,
529 int64_t cluster_index,
530 int addend)
532 BDRVQcowState *s = bs->opaque;
533 int ret;
535 ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend);
536 if (ret < 0) {
537 return ret;
540 return get_refcount(bs, cluster_index);
545 /*********************************************************/
546 /* cluster allocation functions */
550 /* return < 0 if error */
551 static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
553 BDRVQcowState *s = bs->opaque;
554 int i, nb_clusters;
556 nb_clusters = size_to_clusters(s, size);
557 retry:
558 for(i = 0; i < nb_clusters; i++) {
559 int64_t next_cluster_index = s->free_cluster_index++;
560 if (get_refcount(bs, next_cluster_index) != 0)
561 goto retry;
563 #ifdef DEBUG_ALLOC2
564 printf("alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
565 size,
566 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
567 #endif
568 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
571 int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size)
573 int64_t offset;
574 int ret;
576 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC);
577 offset = alloc_clusters_noref(bs, size);
578 ret = update_refcount(bs, offset, size, 1);
579 if (ret < 0) {
580 return ret;
582 return offset;
585 /* only used to allocate compressed sectors. We try to allocate
586 contiguous sectors. size must be <= cluster_size */
587 int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
589 BDRVQcowState *s = bs->opaque;
590 int64_t offset, cluster_offset;
591 int free_in_cluster;
593 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_BYTES);
594 assert(size > 0 && size <= s->cluster_size);
595 if (s->free_byte_offset == 0) {
596 s->free_byte_offset = qcow2_alloc_clusters(bs, s->cluster_size);
597 if (s->free_byte_offset < 0) {
598 return s->free_byte_offset;
601 redo:
602 free_in_cluster = s->cluster_size -
603 (s->free_byte_offset & (s->cluster_size - 1));
604 if (size <= free_in_cluster) {
605 /* enough space in current cluster */
606 offset = s->free_byte_offset;
607 s->free_byte_offset += size;
608 free_in_cluster -= size;
609 if (free_in_cluster == 0)
610 s->free_byte_offset = 0;
611 if ((offset & (s->cluster_size - 1)) != 0)
612 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
613 } else {
614 offset = qcow2_alloc_clusters(bs, s->cluster_size);
615 if (offset < 0) {
616 return offset;
618 cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
619 if ((cluster_offset + s->cluster_size) == offset) {
620 /* we are lucky: contiguous data */
621 offset = s->free_byte_offset;
622 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
623 s->free_byte_offset += size;
624 } else {
625 s->free_byte_offset = offset;
626 goto redo;
629 return offset;
632 void qcow2_free_clusters(BlockDriverState *bs,
633 int64_t offset, int64_t size)
635 int ret;
637 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE);
638 ret = update_refcount(bs, offset, size, -1);
639 if (ret < 0) {
640 fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
641 /* TODO Remember the clusters to free them later and avoid leaking */
646 * free_any_clusters
648 * free clusters according to its type: compressed or not
652 void qcow2_free_any_clusters(BlockDriverState *bs,
653 uint64_t cluster_offset, int nb_clusters)
655 BDRVQcowState *s = bs->opaque;
657 /* free the cluster */
659 if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
660 int nb_csectors;
661 nb_csectors = ((cluster_offset >> s->csize_shift) &
662 s->csize_mask) + 1;
663 qcow2_free_clusters(bs,
664 (cluster_offset & s->cluster_offset_mask) & ~511,
665 nb_csectors * 512);
666 return;
669 qcow2_free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
671 return;
676 /*********************************************************/
677 /* snapshots and image creation */
681 void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
682 int64_t size)
684 int refcount;
685 int64_t start, last, cluster_offset;
686 uint16_t *p;
688 start = offset & ~(s->cluster_size - 1);
689 last = (offset + size - 1) & ~(s->cluster_size - 1);
690 for(cluster_offset = start; cluster_offset <= last;
691 cluster_offset += s->cluster_size) {
692 p = &s->refcount_block[cluster_offset >> s->cluster_bits];
693 refcount = be16_to_cpu(*p);
694 refcount++;
695 *p = cpu_to_be16(refcount);
699 /* update the refcounts of snapshots and the copied flag */
700 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
701 int64_t l1_table_offset, int l1_size, int addend)
703 BDRVQcowState *s = bs->opaque;
704 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
705 int64_t old_offset, old_l2_offset;
706 int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
708 qcow2_l2_cache_reset(bs);
709 cache_refcount_updates = 1;
711 l2_table = NULL;
712 l1_table = NULL;
713 l1_size2 = l1_size * sizeof(uint64_t);
714 if (l1_table_offset != s->l1_table_offset) {
715 if (l1_size2 != 0) {
716 l1_table = qemu_mallocz(align_offset(l1_size2, 512));
717 } else {
718 l1_table = NULL;
720 l1_allocated = 1;
721 if (bdrv_pread(bs->file, l1_table_offset,
722 l1_table, l1_size2) != l1_size2)
723 goto fail;
724 for(i = 0;i < l1_size; i++)
725 be64_to_cpus(&l1_table[i]);
726 } else {
727 assert(l1_size == s->l1_size);
728 l1_table = s->l1_table;
729 l1_allocated = 0;
732 l2_size = s->l2_size * sizeof(uint64_t);
733 l2_table = qemu_malloc(l2_size);
734 l1_modified = 0;
735 for(i = 0; i < l1_size; i++) {
736 l2_offset = l1_table[i];
737 if (l2_offset) {
738 old_l2_offset = l2_offset;
739 l2_offset &= ~QCOW_OFLAG_COPIED;
740 l2_modified = 0;
741 if (bdrv_pread(bs->file, l2_offset, l2_table, l2_size) != l2_size)
742 goto fail;
743 for(j = 0; j < s->l2_size; j++) {
744 offset = be64_to_cpu(l2_table[j]);
745 if (offset != 0) {
746 old_offset = offset;
747 offset &= ~QCOW_OFLAG_COPIED;
748 if (offset & QCOW_OFLAG_COMPRESSED) {
749 nb_csectors = ((offset >> s->csize_shift) &
750 s->csize_mask) + 1;
751 if (addend != 0) {
752 int ret;
753 ret = update_refcount(bs,
754 (offset & s->cluster_offset_mask) & ~511,
755 nb_csectors * 512, addend);
756 if (ret < 0) {
757 goto fail;
760 /* compressed clusters are never modified */
761 refcount = 2;
762 } else {
763 if (addend != 0) {
764 refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
765 } else {
766 refcount = get_refcount(bs, offset >> s->cluster_bits);
770 if (refcount == 1) {
771 offset |= QCOW_OFLAG_COPIED;
773 if (offset != old_offset) {
774 l2_table[j] = cpu_to_be64(offset);
775 l2_modified = 1;
779 if (l2_modified) {
780 if (bdrv_pwrite(bs->file,
781 l2_offset, l2_table, l2_size) != l2_size)
782 goto fail;
785 if (addend != 0) {
786 refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
787 } else {
788 refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
790 if (refcount == 1) {
791 l2_offset |= QCOW_OFLAG_COPIED;
793 if (l2_offset != old_l2_offset) {
794 l1_table[i] = l2_offset;
795 l1_modified = 1;
799 if (l1_modified) {
800 for(i = 0; i < l1_size; i++)
801 cpu_to_be64s(&l1_table[i]);
802 if (bdrv_pwrite(bs->file, l1_table_offset, l1_table,
803 l1_size2) != l1_size2)
804 goto fail;
805 for(i = 0; i < l1_size; i++)
806 be64_to_cpus(&l1_table[i]);
808 if (l1_allocated)
809 qemu_free(l1_table);
810 qemu_free(l2_table);
811 cache_refcount_updates = 0;
812 write_refcount_block(bs);
813 return 0;
814 fail:
815 if (l1_allocated)
816 qemu_free(l1_table);
817 qemu_free(l2_table);
818 cache_refcount_updates = 0;
819 write_refcount_block(bs);
820 return -EIO;
826 /*********************************************************/
827 /* refcount checking functions */
832 * Increases the refcount for a range of clusters in a given refcount table.
833 * This is used to construct a temporary refcount table out of L1 and L2 tables
834 * which can be compared the the refcount table saved in the image.
836 * Returns the number of errors in the image that were found
838 static int inc_refcounts(BlockDriverState *bs,
839 uint16_t *refcount_table,
840 int refcount_table_size,
841 int64_t offset, int64_t size)
843 BDRVQcowState *s = bs->opaque;
844 int64_t start, last, cluster_offset;
845 int k;
846 int errors = 0;
848 if (size <= 0)
849 return 0;
851 start = offset & ~(s->cluster_size - 1);
852 last = (offset + size - 1) & ~(s->cluster_size - 1);
853 for(cluster_offset = start; cluster_offset <= last;
854 cluster_offset += s->cluster_size) {
855 k = cluster_offset >> s->cluster_bits;
856 if (k < 0 || k >= refcount_table_size) {
857 fprintf(stderr, "ERROR: invalid cluster offset=0x%" PRIx64 "\n",
858 cluster_offset);
859 errors++;
860 } else {
861 if (++refcount_table[k] == 0) {
862 fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
863 "\n", cluster_offset);
864 errors++;
869 return errors;
873 * Increases the refcount in the given refcount table for the all clusters
874 * referenced in the L2 table. While doing so, performs some checks on L2
875 * entries.
877 * Returns the number of errors found by the checks or -errno if an internal
878 * error occurred.
880 static int check_refcounts_l2(BlockDriverState *bs,
881 uint16_t *refcount_table, int refcount_table_size, int64_t l2_offset,
882 int check_copied)
884 BDRVQcowState *s = bs->opaque;
885 uint64_t *l2_table, offset;
886 int i, l2_size, nb_csectors, refcount;
887 int errors = 0;
889 /* Read L2 table from disk */
890 l2_size = s->l2_size * sizeof(uint64_t);
891 l2_table = qemu_malloc(l2_size);
893 if (bdrv_pread(bs->file, l2_offset, l2_table, l2_size) != l2_size)
894 goto fail;
896 /* Do the actual checks */
897 for(i = 0; i < s->l2_size; i++) {
898 offset = be64_to_cpu(l2_table[i]);
899 if (offset != 0) {
900 if (offset & QCOW_OFLAG_COMPRESSED) {
901 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
902 if (offset & QCOW_OFLAG_COPIED) {
903 fprintf(stderr, "ERROR: cluster %" PRId64 ": "
904 "copied flag must never be set for compressed "
905 "clusters\n", offset >> s->cluster_bits);
906 offset &= ~QCOW_OFLAG_COPIED;
907 errors++;
910 /* Mark cluster as used */
911 nb_csectors = ((offset >> s->csize_shift) &
912 s->csize_mask) + 1;
913 offset &= s->cluster_offset_mask;
914 errors += inc_refcounts(bs, refcount_table,
915 refcount_table_size,
916 offset & ~511, nb_csectors * 512);
917 } else {
918 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
919 if (check_copied) {
920 uint64_t entry = offset;
921 offset &= ~QCOW_OFLAG_COPIED;
922 refcount = get_refcount(bs, offset >> s->cluster_bits);
923 if ((refcount == 1) != ((entry & QCOW_OFLAG_COPIED) != 0)) {
924 fprintf(stderr, "ERROR OFLAG_COPIED: offset=%"
925 PRIx64 " refcount=%d\n", entry, refcount);
926 errors++;
930 /* Mark cluster as used */
931 offset &= ~QCOW_OFLAG_COPIED;
932 errors += inc_refcounts(bs, refcount_table,
933 refcount_table_size,
934 offset, s->cluster_size);
936 /* Correct offsets are cluster aligned */
937 if (offset & (s->cluster_size - 1)) {
938 fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
939 "properly aligned; L2 entry corrupted.\n", offset);
940 errors++;
946 qemu_free(l2_table);
947 return errors;
949 fail:
950 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
951 qemu_free(l2_table);
952 return -EIO;
956 * Increases the refcount for the L1 table, its L2 tables and all referenced
957 * clusters in the given refcount table. While doing so, performs some checks
958 * on L1 and L2 entries.
960 * Returns the number of errors found by the checks or -errno if an internal
961 * error occurred.
963 static int check_refcounts_l1(BlockDriverState *bs,
964 uint16_t *refcount_table,
965 int refcount_table_size,
966 int64_t l1_table_offset, int l1_size,
967 int check_copied)
969 BDRVQcowState *s = bs->opaque;
970 uint64_t *l1_table, l2_offset, l1_size2;
971 int i, refcount, ret;
972 int errors = 0;
974 l1_size2 = l1_size * sizeof(uint64_t);
976 /* Mark L1 table as used */
977 errors += inc_refcounts(bs, refcount_table, refcount_table_size,
978 l1_table_offset, l1_size2);
980 /* Read L1 table entries from disk */
981 if (l1_size2 == 0) {
982 l1_table = NULL;
983 } else {
984 l1_table = qemu_malloc(l1_size2);
985 if (bdrv_pread(bs->file, l1_table_offset,
986 l1_table, l1_size2) != l1_size2)
987 goto fail;
988 for(i = 0;i < l1_size; i++)
989 be64_to_cpus(&l1_table[i]);
992 /* Do the actual checks */
993 for(i = 0; i < l1_size; i++) {
994 l2_offset = l1_table[i];
995 if (l2_offset) {
996 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
997 if (check_copied) {
998 refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
999 >> s->cluster_bits);
1000 if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
1001 fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
1002 " refcount=%d\n", l2_offset, refcount);
1003 errors++;
1007 /* Mark L2 table as used */
1008 l2_offset &= ~QCOW_OFLAG_COPIED;
1009 errors += inc_refcounts(bs, refcount_table,
1010 refcount_table_size,
1011 l2_offset,
1012 s->cluster_size);
1014 /* L2 tables are cluster aligned */
1015 if (l2_offset & (s->cluster_size - 1)) {
1016 fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
1017 "cluster aligned; L1 entry corrupted\n", l2_offset);
1018 errors++;
1021 /* Process and check L2 entries */
1022 ret = check_refcounts_l2(bs, refcount_table, refcount_table_size,
1023 l2_offset, check_copied);
1024 if (ret < 0) {
1025 goto fail;
1027 errors += ret;
1030 qemu_free(l1_table);
1031 return errors;
1033 fail:
1034 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
1035 qemu_free(l1_table);
1036 return -EIO;
1040 * Checks an image for refcount consistency.
1042 * Returns 0 if no errors are found, the number of errors in case the image is
1043 * detected as corrupted, and -errno when an internal error occured.
1045 int qcow2_check_refcounts(BlockDriverState *bs)
1047 BDRVQcowState *s = bs->opaque;
1048 int64_t size;
1049 int nb_clusters, refcount1, refcount2, i;
1050 QCowSnapshot *sn;
1051 uint16_t *refcount_table;
1052 int ret, errors = 0;
1054 size = bdrv_getlength(bs->file);
1055 nb_clusters = size_to_clusters(s, size);
1056 refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
1058 /* header */
1059 errors += inc_refcounts(bs, refcount_table, nb_clusters,
1060 0, s->cluster_size);
1062 /* current L1 table */
1063 ret = check_refcounts_l1(bs, refcount_table, nb_clusters,
1064 s->l1_table_offset, s->l1_size, 1);
1065 if (ret < 0) {
1066 return ret;
1068 errors += ret;
1070 /* snapshots */
1071 for(i = 0; i < s->nb_snapshots; i++) {
1072 sn = s->snapshots + i;
1073 check_refcounts_l1(bs, refcount_table, nb_clusters,
1074 sn->l1_table_offset, sn->l1_size, 0);
1076 errors += inc_refcounts(bs, refcount_table, nb_clusters,
1077 s->snapshots_offset, s->snapshots_size);
1079 /* refcount data */
1080 errors += inc_refcounts(bs, refcount_table, nb_clusters,
1081 s->refcount_table_offset,
1082 s->refcount_table_size * sizeof(uint64_t));
1083 for(i = 0; i < s->refcount_table_size; i++) {
1084 int64_t offset;
1085 offset = s->refcount_table[i];
1087 /* Refcount blocks are cluster aligned */
1088 if (offset & (s->cluster_size - 1)) {
1089 fprintf(stderr, "ERROR refcount block %d is not "
1090 "cluster aligned; refcount table entry corrupted\n", i);
1091 errors++;
1094 if (offset != 0) {
1095 errors += inc_refcounts(bs, refcount_table, nb_clusters,
1096 offset, s->cluster_size);
1097 if (refcount_table[offset / s->cluster_size] != 1) {
1098 fprintf(stderr, "ERROR refcount block %d refcount=%d\n",
1099 i, refcount_table[offset / s->cluster_size]);
1104 /* compare ref counts */
1105 for(i = 0; i < nb_clusters; i++) {
1106 refcount1 = get_refcount(bs, i);
1107 refcount2 = refcount_table[i];
1108 if (refcount1 != refcount2) {
1109 fprintf(stderr, "ERROR cluster %d refcount=%d reference=%d\n",
1110 i, refcount1, refcount2);
1111 errors++;
1115 qemu_free(refcount_table);
1117 return errors;