i8257: rename struct dma_cont to I8257State
[qemu/kevin.git] / block / qcow2-refcount.c
blob52a0a9ffc3596d00a5a0a87fafae9816cab1a380
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/osdep.h"
26 #include "qemu-common.h"
27 #include "block/block_int.h"
28 #include "block/qcow2.h"
29 #include "qemu/range.h"
31 static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size);
32 static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
33 int64_t offset, int64_t length, uint64_t addend,
34 bool decrease, enum qcow2_discard_type type);
36 static uint64_t get_refcount_ro0(const void *refcount_array, uint64_t index);
37 static uint64_t get_refcount_ro1(const void *refcount_array, uint64_t index);
38 static uint64_t get_refcount_ro2(const void *refcount_array, uint64_t index);
39 static uint64_t get_refcount_ro3(const void *refcount_array, uint64_t index);
40 static uint64_t get_refcount_ro4(const void *refcount_array, uint64_t index);
41 static uint64_t get_refcount_ro5(const void *refcount_array, uint64_t index);
42 static uint64_t get_refcount_ro6(const void *refcount_array, uint64_t index);
44 static void set_refcount_ro0(void *refcount_array, uint64_t index,
45 uint64_t value);
46 static void set_refcount_ro1(void *refcount_array, uint64_t index,
47 uint64_t value);
48 static void set_refcount_ro2(void *refcount_array, uint64_t index,
49 uint64_t value);
50 static void set_refcount_ro3(void *refcount_array, uint64_t index,
51 uint64_t value);
52 static void set_refcount_ro4(void *refcount_array, uint64_t index,
53 uint64_t value);
54 static void set_refcount_ro5(void *refcount_array, uint64_t index,
55 uint64_t value);
56 static void set_refcount_ro6(void *refcount_array, uint64_t index,
57 uint64_t value);
60 static Qcow2GetRefcountFunc *const get_refcount_funcs[] = {
61 &get_refcount_ro0,
62 &get_refcount_ro1,
63 &get_refcount_ro2,
64 &get_refcount_ro3,
65 &get_refcount_ro4,
66 &get_refcount_ro5,
67 &get_refcount_ro6
70 static Qcow2SetRefcountFunc *const set_refcount_funcs[] = {
71 &set_refcount_ro0,
72 &set_refcount_ro1,
73 &set_refcount_ro2,
74 &set_refcount_ro3,
75 &set_refcount_ro4,
76 &set_refcount_ro5,
77 &set_refcount_ro6
81 /*********************************************************/
82 /* refcount handling */
84 int qcow2_refcount_init(BlockDriverState *bs)
86 BDRVQcow2State *s = bs->opaque;
87 unsigned int refcount_table_size2, i;
88 int ret;
90 assert(s->refcount_order >= 0 && s->refcount_order <= 6);
92 s->get_refcount = get_refcount_funcs[s->refcount_order];
93 s->set_refcount = set_refcount_funcs[s->refcount_order];
95 assert(s->refcount_table_size <= INT_MAX / sizeof(uint64_t));
96 refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
97 s->refcount_table = g_try_malloc(refcount_table_size2);
99 if (s->refcount_table_size > 0) {
100 if (s->refcount_table == NULL) {
101 ret = -ENOMEM;
102 goto fail;
104 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_LOAD);
105 ret = bdrv_pread(bs->file->bs, s->refcount_table_offset,
106 s->refcount_table, refcount_table_size2);
107 if (ret < 0) {
108 goto fail;
110 for(i = 0; i < s->refcount_table_size; i++)
111 be64_to_cpus(&s->refcount_table[i]);
113 return 0;
114 fail:
115 return ret;
118 void qcow2_refcount_close(BlockDriverState *bs)
120 BDRVQcow2State *s = bs->opaque;
121 g_free(s->refcount_table);
125 static uint64_t get_refcount_ro0(const void *refcount_array, uint64_t index)
127 return (((const uint8_t *)refcount_array)[index / 8] >> (index % 8)) & 0x1;
130 static void set_refcount_ro0(void *refcount_array, uint64_t index,
131 uint64_t value)
133 assert(!(value >> 1));
134 ((uint8_t *)refcount_array)[index / 8] &= ~(0x1 << (index % 8));
135 ((uint8_t *)refcount_array)[index / 8] |= value << (index % 8);
138 static uint64_t get_refcount_ro1(const void *refcount_array, uint64_t index)
140 return (((const uint8_t *)refcount_array)[index / 4] >> (2 * (index % 4)))
141 & 0x3;
144 static void set_refcount_ro1(void *refcount_array, uint64_t index,
145 uint64_t value)
147 assert(!(value >> 2));
148 ((uint8_t *)refcount_array)[index / 4] &= ~(0x3 << (2 * (index % 4)));
149 ((uint8_t *)refcount_array)[index / 4] |= value << (2 * (index % 4));
152 static uint64_t get_refcount_ro2(const void *refcount_array, uint64_t index)
154 return (((const uint8_t *)refcount_array)[index / 2] >> (4 * (index % 2)))
155 & 0xf;
158 static void set_refcount_ro2(void *refcount_array, uint64_t index,
159 uint64_t value)
161 assert(!(value >> 4));
162 ((uint8_t *)refcount_array)[index / 2] &= ~(0xf << (4 * (index % 2)));
163 ((uint8_t *)refcount_array)[index / 2] |= value << (4 * (index % 2));
166 static uint64_t get_refcount_ro3(const void *refcount_array, uint64_t index)
168 return ((const uint8_t *)refcount_array)[index];
171 static void set_refcount_ro3(void *refcount_array, uint64_t index,
172 uint64_t value)
174 assert(!(value >> 8));
175 ((uint8_t *)refcount_array)[index] = value;
178 static uint64_t get_refcount_ro4(const void *refcount_array, uint64_t index)
180 return be16_to_cpu(((const uint16_t *)refcount_array)[index]);
183 static void set_refcount_ro4(void *refcount_array, uint64_t index,
184 uint64_t value)
186 assert(!(value >> 16));
187 ((uint16_t *)refcount_array)[index] = cpu_to_be16(value);
190 static uint64_t get_refcount_ro5(const void *refcount_array, uint64_t index)
192 return be32_to_cpu(((const uint32_t *)refcount_array)[index]);
195 static void set_refcount_ro5(void *refcount_array, uint64_t index,
196 uint64_t value)
198 assert(!(value >> 32));
199 ((uint32_t *)refcount_array)[index] = cpu_to_be32(value);
202 static uint64_t get_refcount_ro6(const void *refcount_array, uint64_t index)
204 return be64_to_cpu(((const uint64_t *)refcount_array)[index]);
207 static void set_refcount_ro6(void *refcount_array, uint64_t index,
208 uint64_t value)
210 ((uint64_t *)refcount_array)[index] = cpu_to_be64(value);
214 static int load_refcount_block(BlockDriverState *bs,
215 int64_t refcount_block_offset,
216 void **refcount_block)
218 BDRVQcow2State *s = bs->opaque;
219 int ret;
221 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_LOAD);
222 ret = qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset,
223 refcount_block);
225 return ret;
229 * Retrieves the refcount of the cluster given by its index and stores it in
230 * *refcount. Returns 0 on success and -errno on failure.
232 int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
233 uint64_t *refcount)
235 BDRVQcow2State *s = bs->opaque;
236 uint64_t refcount_table_index, block_index;
237 int64_t refcount_block_offset;
238 int ret;
239 void *refcount_block;
241 refcount_table_index = cluster_index >> s->refcount_block_bits;
242 if (refcount_table_index >= s->refcount_table_size) {
243 *refcount = 0;
244 return 0;
246 refcount_block_offset =
247 s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK;
248 if (!refcount_block_offset) {
249 *refcount = 0;
250 return 0;
253 if (offset_into_cluster(s, refcount_block_offset)) {
254 qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#" PRIx64
255 " unaligned (reftable index: %#" PRIx64 ")",
256 refcount_block_offset, refcount_table_index);
257 return -EIO;
260 ret = qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset,
261 &refcount_block);
262 if (ret < 0) {
263 return ret;
266 block_index = cluster_index & (s->refcount_block_size - 1);
267 *refcount = s->get_refcount(refcount_block, block_index);
269 qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block);
271 return 0;
275 * Rounds the refcount table size up to avoid growing the table for each single
276 * refcount block that is allocated.
278 static unsigned int next_refcount_table_size(BDRVQcow2State *s,
279 unsigned int min_size)
281 unsigned int min_clusters = (min_size >> (s->cluster_bits - 3)) + 1;
282 unsigned int refcount_table_clusters =
283 MAX(1, s->refcount_table_size >> (s->cluster_bits - 3));
285 while (min_clusters > refcount_table_clusters) {
286 refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
289 return refcount_table_clusters << (s->cluster_bits - 3);
293 /* Checks if two offsets are described by the same refcount block */
294 static int in_same_refcount_block(BDRVQcow2State *s, uint64_t offset_a,
295 uint64_t offset_b)
297 uint64_t block_a = offset_a >> (s->cluster_bits + s->refcount_block_bits);
298 uint64_t block_b = offset_b >> (s->cluster_bits + s->refcount_block_bits);
300 return (block_a == block_b);
304 * Loads a refcount block. If it doesn't exist yet, it is allocated first
305 * (including growing the refcount table if needed).
307 * Returns 0 on success or -errno in error case
309 static int alloc_refcount_block(BlockDriverState *bs,
310 int64_t cluster_index, void **refcount_block)
312 BDRVQcow2State *s = bs->opaque;
313 unsigned int refcount_table_index;
314 int ret;
316 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
318 /* Find the refcount block for the given cluster */
319 refcount_table_index = cluster_index >> s->refcount_block_bits;
321 if (refcount_table_index < s->refcount_table_size) {
323 uint64_t refcount_block_offset =
324 s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK;
326 /* If it's already there, we're done */
327 if (refcount_block_offset) {
328 if (offset_into_cluster(s, refcount_block_offset)) {
329 qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#"
330 PRIx64 " unaligned (reftable index: "
331 "%#x)", refcount_block_offset,
332 refcount_table_index);
333 return -EIO;
336 return load_refcount_block(bs, refcount_block_offset,
337 refcount_block);
342 * If we came here, we need to allocate something. Something is at least
343 * a cluster for the new refcount block. It may also include a new refcount
344 * table if the old refcount table is too small.
346 * Note that allocating clusters here needs some special care:
348 * - We can't use the normal qcow2_alloc_clusters(), it would try to
349 * increase the refcount and very likely we would end up with an endless
350 * recursion. Instead we must place the refcount blocks in a way that
351 * they can describe them themselves.
353 * - We need to consider that at this point we are inside update_refcounts
354 * and potentially doing an initial refcount increase. This means that
355 * some clusters have already been allocated by the caller, but their
356 * refcount isn't accurate yet. If we allocate clusters for metadata, we
357 * need to return -EAGAIN to signal the caller that it needs to restart
358 * the search for free clusters.
360 * - alloc_clusters_noref and qcow2_free_clusters may load a different
361 * refcount block into the cache
364 *refcount_block = NULL;
366 /* We write to the refcount table, so we might depend on L2 tables */
367 ret = qcow2_cache_flush(bs, s->l2_table_cache);
368 if (ret < 0) {
369 return ret;
372 /* Allocate the refcount block itself and mark it as used */
373 int64_t new_block = alloc_clusters_noref(bs, s->cluster_size);
374 if (new_block < 0) {
375 return new_block;
378 #ifdef DEBUG_ALLOC2
379 fprintf(stderr, "qcow2: Allocate refcount block %d for %" PRIx64
380 " at %" PRIx64 "\n",
381 refcount_table_index, cluster_index << s->cluster_bits, new_block);
382 #endif
384 if (in_same_refcount_block(s, new_block, cluster_index << s->cluster_bits)) {
385 /* Zero the new refcount block before updating it */
386 ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block,
387 refcount_block);
388 if (ret < 0) {
389 goto fail_block;
392 memset(*refcount_block, 0, s->cluster_size);
394 /* The block describes itself, need to update the cache */
395 int block_index = (new_block >> s->cluster_bits) &
396 (s->refcount_block_size - 1);
397 s->set_refcount(*refcount_block, block_index, 1);
398 } else {
399 /* Described somewhere else. This can recurse at most twice before we
400 * arrive at a block that describes itself. */
401 ret = update_refcount(bs, new_block, s->cluster_size, 1, false,
402 QCOW2_DISCARD_NEVER);
403 if (ret < 0) {
404 goto fail_block;
407 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
408 if (ret < 0) {
409 goto fail_block;
412 /* Initialize the new refcount block only after updating its refcount,
413 * update_refcount uses the refcount cache itself */
414 ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block,
415 refcount_block);
416 if (ret < 0) {
417 goto fail_block;
420 memset(*refcount_block, 0, s->cluster_size);
423 /* Now the new refcount block needs to be written to disk */
424 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE);
425 qcow2_cache_entry_mark_dirty(bs, s->refcount_block_cache, *refcount_block);
426 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
427 if (ret < 0) {
428 goto fail_block;
431 /* If the refcount table is big enough, just hook the block up there */
432 if (refcount_table_index < s->refcount_table_size) {
433 uint64_t data64 = cpu_to_be64(new_block);
434 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_HOOKUP);
435 ret = bdrv_pwrite_sync(bs->file->bs,
436 s->refcount_table_offset + refcount_table_index * sizeof(uint64_t),
437 &data64, sizeof(data64));
438 if (ret < 0) {
439 goto fail_block;
442 s->refcount_table[refcount_table_index] = new_block;
444 /* The new refcount block may be where the caller intended to put its
445 * data, so let it restart the search. */
446 return -EAGAIN;
449 qcow2_cache_put(bs, s->refcount_block_cache, refcount_block);
452 * If we come here, we need to grow the refcount table. Again, a new
453 * refcount table needs some space and we can't simply allocate to avoid
454 * endless recursion.
456 * Therefore let's grab new refcount blocks at the end of the image, which
457 * will describe themselves and the new refcount table. This way we can
458 * reference them only in the new table and do the switch to the new
459 * refcount table at once without producing an inconsistent state in
460 * between.
462 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW);
464 /* Calculate the number of refcount blocks needed so far; this will be the
465 * basis for calculating the index of the first cluster used for the
466 * self-describing refcount structures which we are about to create.
468 * Because we reached this point, there cannot be any refcount entries for
469 * cluster_index or higher indices yet. However, because new_block has been
470 * allocated to describe that cluster (and it will assume this role later
471 * on), we cannot use that index; also, new_block may actually have a higher
472 * cluster index than cluster_index, so it needs to be taken into account
473 * here (and 1 needs to be added to its value because that cluster is used).
475 uint64_t blocks_used = DIV_ROUND_UP(MAX(cluster_index + 1,
476 (new_block >> s->cluster_bits) + 1),
477 s->refcount_block_size);
479 if (blocks_used > QCOW_MAX_REFTABLE_SIZE / sizeof(uint64_t)) {
480 return -EFBIG;
483 /* And now we need at least one block more for the new metadata */
484 uint64_t table_size = next_refcount_table_size(s, blocks_used + 1);
485 uint64_t last_table_size;
486 uint64_t blocks_clusters;
487 do {
488 uint64_t table_clusters =
489 size_to_clusters(s, table_size * sizeof(uint64_t));
490 blocks_clusters = 1 +
491 ((table_clusters + s->refcount_block_size - 1)
492 / s->refcount_block_size);
493 uint64_t meta_clusters = table_clusters + blocks_clusters;
495 last_table_size = table_size;
496 table_size = next_refcount_table_size(s, blocks_used +
497 ((meta_clusters + s->refcount_block_size - 1)
498 / s->refcount_block_size));
500 } while (last_table_size != table_size);
502 #ifdef DEBUG_ALLOC2
503 fprintf(stderr, "qcow2: Grow refcount table %" PRId32 " => %" PRId64 "\n",
504 s->refcount_table_size, table_size);
505 #endif
507 /* Create the new refcount table and blocks */
508 uint64_t meta_offset = (blocks_used * s->refcount_block_size) *
509 s->cluster_size;
510 uint64_t table_offset = meta_offset + blocks_clusters * s->cluster_size;
511 uint64_t *new_table = g_try_new0(uint64_t, table_size);
512 void *new_blocks = g_try_malloc0(blocks_clusters * s->cluster_size);
514 assert(table_size > 0 && blocks_clusters > 0);
515 if (new_table == NULL || new_blocks == NULL) {
516 ret = -ENOMEM;
517 goto fail_table;
520 /* Fill the new refcount table */
521 memcpy(new_table, s->refcount_table,
522 s->refcount_table_size * sizeof(uint64_t));
523 new_table[refcount_table_index] = new_block;
525 int i;
526 for (i = 0; i < blocks_clusters; i++) {
527 new_table[blocks_used + i] = meta_offset + (i * s->cluster_size);
530 /* Fill the refcount blocks */
531 uint64_t table_clusters = size_to_clusters(s, table_size * sizeof(uint64_t));
532 int block = 0;
533 for (i = 0; i < table_clusters + blocks_clusters; i++) {
534 s->set_refcount(new_blocks, block++, 1);
537 /* Write refcount blocks to disk */
538 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS);
539 ret = bdrv_pwrite_sync(bs->file->bs, meta_offset, new_blocks,
540 blocks_clusters * s->cluster_size);
541 g_free(new_blocks);
542 new_blocks = NULL;
543 if (ret < 0) {
544 goto fail_table;
547 /* Write refcount table to disk */
548 for(i = 0; i < table_size; i++) {
549 cpu_to_be64s(&new_table[i]);
552 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE);
553 ret = bdrv_pwrite_sync(bs->file->bs, table_offset, new_table,
554 table_size * sizeof(uint64_t));
555 if (ret < 0) {
556 goto fail_table;
559 for(i = 0; i < table_size; i++) {
560 be64_to_cpus(&new_table[i]);
563 /* Hook up the new refcount table in the qcow2 header */
564 struct QEMU_PACKED {
565 uint64_t d64;
566 uint32_t d32;
567 } data;
568 cpu_to_be64w(&data.d64, table_offset);
569 cpu_to_be32w(&data.d32, table_clusters);
570 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE);
571 ret = bdrv_pwrite_sync(bs->file->bs,
572 offsetof(QCowHeader, refcount_table_offset),
573 &data, sizeof(data));
574 if (ret < 0) {
575 goto fail_table;
578 /* And switch it in memory */
579 uint64_t old_table_offset = s->refcount_table_offset;
580 uint64_t old_table_size = s->refcount_table_size;
582 g_free(s->refcount_table);
583 s->refcount_table = new_table;
584 s->refcount_table_size = table_size;
585 s->refcount_table_offset = table_offset;
587 /* Free old table. */
588 qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t),
589 QCOW2_DISCARD_OTHER);
591 ret = load_refcount_block(bs, new_block, refcount_block);
592 if (ret < 0) {
593 return ret;
596 /* If we were trying to do the initial refcount update for some cluster
597 * allocation, we might have used the same clusters to store newly
598 * allocated metadata. Make the caller search some new space. */
599 return -EAGAIN;
601 fail_table:
602 g_free(new_blocks);
603 g_free(new_table);
604 fail_block:
605 if (*refcount_block != NULL) {
606 qcow2_cache_put(bs, s->refcount_block_cache, refcount_block);
608 return ret;
611 void qcow2_process_discards(BlockDriverState *bs, int ret)
613 BDRVQcow2State *s = bs->opaque;
614 Qcow2DiscardRegion *d, *next;
616 QTAILQ_FOREACH_SAFE(d, &s->discards, next, next) {
617 QTAILQ_REMOVE(&s->discards, d, next);
619 /* Discard is optional, ignore the return value */
620 if (ret >= 0) {
621 bdrv_discard(bs->file->bs,
622 d->offset >> BDRV_SECTOR_BITS,
623 d->bytes >> BDRV_SECTOR_BITS);
626 g_free(d);
630 static void update_refcount_discard(BlockDriverState *bs,
631 uint64_t offset, uint64_t length)
633 BDRVQcow2State *s = bs->opaque;
634 Qcow2DiscardRegion *d, *p, *next;
636 QTAILQ_FOREACH(d, &s->discards, next) {
637 uint64_t new_start = MIN(offset, d->offset);
638 uint64_t new_end = MAX(offset + length, d->offset + d->bytes);
640 if (new_end - new_start <= length + d->bytes) {
641 /* There can't be any overlap, areas ending up here have no
642 * references any more and therefore shouldn't get freed another
643 * time. */
644 assert(d->bytes + length == new_end - new_start);
645 d->offset = new_start;
646 d->bytes = new_end - new_start;
647 goto found;
651 d = g_malloc(sizeof(*d));
652 *d = (Qcow2DiscardRegion) {
653 .bs = bs,
654 .offset = offset,
655 .bytes = length,
657 QTAILQ_INSERT_TAIL(&s->discards, d, next);
659 found:
660 /* Merge discard requests if they are adjacent now */
661 QTAILQ_FOREACH_SAFE(p, &s->discards, next, next) {
662 if (p == d
663 || p->offset > d->offset + d->bytes
664 || d->offset > p->offset + p->bytes)
666 continue;
669 /* Still no overlap possible */
670 assert(p->offset == d->offset + d->bytes
671 || d->offset == p->offset + p->bytes);
673 QTAILQ_REMOVE(&s->discards, p, next);
674 d->offset = MIN(d->offset, p->offset);
675 d->bytes += p->bytes;
676 g_free(p);
680 /* XXX: cache several refcount block clusters ? */
681 /* @addend is the absolute value of the addend; if @decrease is set, @addend
682 * will be subtracted from the current refcount, otherwise it will be added */
683 static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
684 int64_t offset,
685 int64_t length,
686 uint64_t addend,
687 bool decrease,
688 enum qcow2_discard_type type)
690 BDRVQcow2State *s = bs->opaque;
691 int64_t start, last, cluster_offset;
692 void *refcount_block = NULL;
693 int64_t old_table_index = -1;
694 int ret;
696 #ifdef DEBUG_ALLOC2
697 fprintf(stderr, "update_refcount: offset=%" PRId64 " size=%" PRId64
698 " addend=%s%" PRIu64 "\n", offset, length, decrease ? "-" : "",
699 addend);
700 #endif
701 if (length < 0) {
702 return -EINVAL;
703 } else if (length == 0) {
704 return 0;
707 if (decrease) {
708 qcow2_cache_set_dependency(bs, s->refcount_block_cache,
709 s->l2_table_cache);
712 start = start_of_cluster(s, offset);
713 last = start_of_cluster(s, offset + length - 1);
714 for(cluster_offset = start; cluster_offset <= last;
715 cluster_offset += s->cluster_size)
717 int block_index;
718 uint64_t refcount;
719 int64_t cluster_index = cluster_offset >> s->cluster_bits;
720 int64_t table_index = cluster_index >> s->refcount_block_bits;
722 /* Load the refcount block and allocate it if needed */
723 if (table_index != old_table_index) {
724 if (refcount_block) {
725 qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block);
727 ret = alloc_refcount_block(bs, cluster_index, &refcount_block);
728 if (ret < 0) {
729 goto fail;
732 old_table_index = table_index;
734 qcow2_cache_entry_mark_dirty(bs, s->refcount_block_cache,
735 refcount_block);
737 /* we can update the count and save it */
738 block_index = cluster_index & (s->refcount_block_size - 1);
740 refcount = s->get_refcount(refcount_block, block_index);
741 if (decrease ? (refcount - addend > refcount)
742 : (refcount + addend < refcount ||
743 refcount + addend > s->refcount_max))
745 ret = -EINVAL;
746 goto fail;
748 if (decrease) {
749 refcount -= addend;
750 } else {
751 refcount += addend;
753 if (refcount == 0 && cluster_index < s->free_cluster_index) {
754 s->free_cluster_index = cluster_index;
756 s->set_refcount(refcount_block, block_index, refcount);
758 if (refcount == 0 && s->discard_passthrough[type]) {
759 update_refcount_discard(bs, cluster_offset, s->cluster_size);
763 ret = 0;
764 fail:
765 if (!s->cache_discards) {
766 qcow2_process_discards(bs, ret);
769 /* Write last changed block to disk */
770 if (refcount_block) {
771 qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block);
775 * Try do undo any updates if an error is returned (This may succeed in
776 * some cases like ENOSPC for allocating a new refcount block)
778 if (ret < 0) {
779 int dummy;
780 dummy = update_refcount(bs, offset, cluster_offset - offset, addend,
781 !decrease, QCOW2_DISCARD_NEVER);
782 (void)dummy;
785 return ret;
789 * Increases or decreases the refcount of a given cluster.
791 * @addend is the absolute value of the addend; if @decrease is set, @addend
792 * will be subtracted from the current refcount, otherwise it will be added.
794 * On success 0 is returned; on failure -errno is returned.
796 int qcow2_update_cluster_refcount(BlockDriverState *bs,
797 int64_t cluster_index,
798 uint64_t addend, bool decrease,
799 enum qcow2_discard_type type)
801 BDRVQcow2State *s = bs->opaque;
802 int ret;
804 ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend,
805 decrease, type);
806 if (ret < 0) {
807 return ret;
810 return 0;
815 /*********************************************************/
816 /* cluster allocation functions */
820 /* return < 0 if error */
821 static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size)
823 BDRVQcow2State *s = bs->opaque;
824 uint64_t i, nb_clusters, refcount;
825 int ret;
827 /* We can't allocate clusters if they may still be queued for discard. */
828 if (s->cache_discards) {
829 qcow2_process_discards(bs, 0);
832 nb_clusters = size_to_clusters(s, size);
833 retry:
834 for(i = 0; i < nb_clusters; i++) {
835 uint64_t next_cluster_index = s->free_cluster_index++;
836 ret = qcow2_get_refcount(bs, next_cluster_index, &refcount);
838 if (ret < 0) {
839 return ret;
840 } else if (refcount != 0) {
841 goto retry;
845 /* Make sure that all offsets in the "allocated" range are representable
846 * in an int64_t */
847 if (s->free_cluster_index > 0 &&
848 s->free_cluster_index - 1 > (INT64_MAX >> s->cluster_bits))
850 return -EFBIG;
853 #ifdef DEBUG_ALLOC2
854 fprintf(stderr, "alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
855 size,
856 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
857 #endif
858 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
861 int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size)
863 int64_t offset;
864 int ret;
866 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC);
867 do {
868 offset = alloc_clusters_noref(bs, size);
869 if (offset < 0) {
870 return offset;
873 ret = update_refcount(bs, offset, size, 1, false, QCOW2_DISCARD_NEVER);
874 } while (ret == -EAGAIN);
876 if (ret < 0) {
877 return ret;
880 return offset;
883 int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
884 int64_t nb_clusters)
886 BDRVQcow2State *s = bs->opaque;
887 uint64_t cluster_index, refcount;
888 uint64_t i;
889 int ret;
891 assert(nb_clusters >= 0);
892 if (nb_clusters == 0) {
893 return 0;
896 do {
897 /* Check how many clusters there are free */
898 cluster_index = offset >> s->cluster_bits;
899 for(i = 0; i < nb_clusters; i++) {
900 ret = qcow2_get_refcount(bs, cluster_index++, &refcount);
901 if (ret < 0) {
902 return ret;
903 } else if (refcount != 0) {
904 break;
908 /* And then allocate them */
909 ret = update_refcount(bs, offset, i << s->cluster_bits, 1, false,
910 QCOW2_DISCARD_NEVER);
911 } while (ret == -EAGAIN);
913 if (ret < 0) {
914 return ret;
917 return i;
920 /* only used to allocate compressed sectors. We try to allocate
921 contiguous sectors. size must be <= cluster_size */
922 int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
924 BDRVQcow2State *s = bs->opaque;
925 int64_t offset;
926 size_t free_in_cluster;
927 int ret;
929 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_BYTES);
930 assert(size > 0 && size <= s->cluster_size);
931 assert(!s->free_byte_offset || offset_into_cluster(s, s->free_byte_offset));
933 offset = s->free_byte_offset;
935 if (offset) {
936 uint64_t refcount;
937 ret = qcow2_get_refcount(bs, offset >> s->cluster_bits, &refcount);
938 if (ret < 0) {
939 return ret;
942 if (refcount == s->refcount_max) {
943 offset = 0;
947 free_in_cluster = s->cluster_size - offset_into_cluster(s, offset);
948 do {
949 if (!offset || free_in_cluster < size) {
950 int64_t new_cluster = alloc_clusters_noref(bs, s->cluster_size);
951 if (new_cluster < 0) {
952 return new_cluster;
955 if (!offset || ROUND_UP(offset, s->cluster_size) != new_cluster) {
956 offset = new_cluster;
957 free_in_cluster = s->cluster_size;
958 } else {
959 free_in_cluster += s->cluster_size;
963 assert(offset);
964 ret = update_refcount(bs, offset, size, 1, false, QCOW2_DISCARD_NEVER);
965 if (ret < 0) {
966 offset = 0;
968 } while (ret == -EAGAIN);
969 if (ret < 0) {
970 return ret;
973 /* The cluster refcount was incremented; refcount blocks must be flushed
974 * before the caller's L2 table updates. */
975 qcow2_cache_set_dependency(bs, s->l2_table_cache, s->refcount_block_cache);
977 s->free_byte_offset = offset + size;
978 if (!offset_into_cluster(s, s->free_byte_offset)) {
979 s->free_byte_offset = 0;
982 return offset;
985 void qcow2_free_clusters(BlockDriverState *bs,
986 int64_t offset, int64_t size,
987 enum qcow2_discard_type type)
989 int ret;
991 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE);
992 ret = update_refcount(bs, offset, size, 1, true, type);
993 if (ret < 0) {
994 fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
995 /* TODO Remember the clusters to free them later and avoid leaking */
1000 * Free a cluster using its L2 entry (handles clusters of all types, e.g.
1001 * normal cluster, compressed cluster, etc.)
1003 void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
1004 int nb_clusters, enum qcow2_discard_type type)
1006 BDRVQcow2State *s = bs->opaque;
1008 switch (qcow2_get_cluster_type(l2_entry)) {
1009 case QCOW2_CLUSTER_COMPRESSED:
1011 int nb_csectors;
1012 nb_csectors = ((l2_entry >> s->csize_shift) &
1013 s->csize_mask) + 1;
1014 qcow2_free_clusters(bs,
1015 (l2_entry & s->cluster_offset_mask) & ~511,
1016 nb_csectors * 512, type);
1018 break;
1019 case QCOW2_CLUSTER_NORMAL:
1020 case QCOW2_CLUSTER_ZERO:
1021 if (l2_entry & L2E_OFFSET_MASK) {
1022 if (offset_into_cluster(s, l2_entry & L2E_OFFSET_MASK)) {
1023 qcow2_signal_corruption(bs, false, -1, -1,
1024 "Cannot free unaligned cluster %#llx",
1025 l2_entry & L2E_OFFSET_MASK);
1026 } else {
1027 qcow2_free_clusters(bs, l2_entry & L2E_OFFSET_MASK,
1028 nb_clusters << s->cluster_bits, type);
1031 break;
1032 case QCOW2_CLUSTER_UNALLOCATED:
1033 break;
1034 default:
1035 abort();
1041 /*********************************************************/
1042 /* snapshots and image creation */
1046 /* update the refcounts of snapshots and the copied flag */
1047 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
1048 int64_t l1_table_offset, int l1_size, int addend)
1050 BDRVQcow2State *s = bs->opaque;
1051 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, refcount;
1052 bool l1_allocated = false;
1053 int64_t old_offset, old_l2_offset;
1054 int i, j, l1_modified = 0, nb_csectors;
1055 int ret;
1057 assert(addend >= -1 && addend <= 1);
1059 l2_table = NULL;
1060 l1_table = NULL;
1061 l1_size2 = l1_size * sizeof(uint64_t);
1063 s->cache_discards = true;
1065 /* WARNING: qcow2_snapshot_goto relies on this function not using the
1066 * l1_table_offset when it is the current s->l1_table_offset! Be careful
1067 * when changing this! */
1068 if (l1_table_offset != s->l1_table_offset) {
1069 l1_table = g_try_malloc0(align_offset(l1_size2, 512));
1070 if (l1_size2 && l1_table == NULL) {
1071 ret = -ENOMEM;
1072 goto fail;
1074 l1_allocated = true;
1076 ret = bdrv_pread(bs->file->bs, l1_table_offset, l1_table, l1_size2);
1077 if (ret < 0) {
1078 goto fail;
1081 for(i = 0;i < l1_size; i++)
1082 be64_to_cpus(&l1_table[i]);
1083 } else {
1084 assert(l1_size == s->l1_size);
1085 l1_table = s->l1_table;
1086 l1_allocated = false;
1089 for(i = 0; i < l1_size; i++) {
1090 l2_offset = l1_table[i];
1091 if (l2_offset) {
1092 old_l2_offset = l2_offset;
1093 l2_offset &= L1E_OFFSET_MASK;
1095 if (offset_into_cluster(s, l2_offset)) {
1096 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#"
1097 PRIx64 " unaligned (L1 index: %#x)",
1098 l2_offset, i);
1099 ret = -EIO;
1100 goto fail;
1103 ret = qcow2_cache_get(bs, s->l2_table_cache, l2_offset,
1104 (void**) &l2_table);
1105 if (ret < 0) {
1106 goto fail;
1109 for(j = 0; j < s->l2_size; j++) {
1110 uint64_t cluster_index;
1112 offset = be64_to_cpu(l2_table[j]);
1113 old_offset = offset;
1114 offset &= ~QCOW_OFLAG_COPIED;
1116 switch (qcow2_get_cluster_type(offset)) {
1117 case QCOW2_CLUSTER_COMPRESSED:
1118 nb_csectors = ((offset >> s->csize_shift) &
1119 s->csize_mask) + 1;
1120 if (addend != 0) {
1121 ret = update_refcount(bs,
1122 (offset & s->cluster_offset_mask) & ~511,
1123 nb_csectors * 512, abs(addend), addend < 0,
1124 QCOW2_DISCARD_SNAPSHOT);
1125 if (ret < 0) {
1126 goto fail;
1129 /* compressed clusters are never modified */
1130 refcount = 2;
1131 break;
1133 case QCOW2_CLUSTER_NORMAL:
1134 case QCOW2_CLUSTER_ZERO:
1135 if (offset_into_cluster(s, offset & L2E_OFFSET_MASK)) {
1136 qcow2_signal_corruption(bs, true, -1, -1, "Data "
1137 "cluster offset %#llx "
1138 "unaligned (L2 offset: %#"
1139 PRIx64 ", L2 index: %#x)",
1140 offset & L2E_OFFSET_MASK,
1141 l2_offset, j);
1142 ret = -EIO;
1143 goto fail;
1146 cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
1147 if (!cluster_index) {
1148 /* unallocated */
1149 refcount = 0;
1150 break;
1152 if (addend != 0) {
1153 ret = qcow2_update_cluster_refcount(bs,
1154 cluster_index, abs(addend), addend < 0,
1155 QCOW2_DISCARD_SNAPSHOT);
1156 if (ret < 0) {
1157 goto fail;
1161 ret = qcow2_get_refcount(bs, cluster_index, &refcount);
1162 if (ret < 0) {
1163 goto fail;
1165 break;
1167 case QCOW2_CLUSTER_UNALLOCATED:
1168 refcount = 0;
1169 break;
1171 default:
1172 abort();
1175 if (refcount == 1) {
1176 offset |= QCOW_OFLAG_COPIED;
1178 if (offset != old_offset) {
1179 if (addend > 0) {
1180 qcow2_cache_set_dependency(bs, s->l2_table_cache,
1181 s->refcount_block_cache);
1183 l2_table[j] = cpu_to_be64(offset);
1184 qcow2_cache_entry_mark_dirty(bs, s->l2_table_cache,
1185 l2_table);
1189 qcow2_cache_put(bs, s->l2_table_cache, (void **) &l2_table);
1191 if (addend != 0) {
1192 ret = qcow2_update_cluster_refcount(bs, l2_offset >>
1193 s->cluster_bits,
1194 abs(addend), addend < 0,
1195 QCOW2_DISCARD_SNAPSHOT);
1196 if (ret < 0) {
1197 goto fail;
1200 ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
1201 &refcount);
1202 if (ret < 0) {
1203 goto fail;
1204 } else if (refcount == 1) {
1205 l2_offset |= QCOW_OFLAG_COPIED;
1207 if (l2_offset != old_l2_offset) {
1208 l1_table[i] = l2_offset;
1209 l1_modified = 1;
1214 ret = bdrv_flush(bs);
1215 fail:
1216 if (l2_table) {
1217 qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
1220 s->cache_discards = false;
1221 qcow2_process_discards(bs, ret);
1223 /* Update L1 only if it isn't deleted anyway (addend = -1) */
1224 if (ret == 0 && addend >= 0 && l1_modified) {
1225 for (i = 0; i < l1_size; i++) {
1226 cpu_to_be64s(&l1_table[i]);
1229 ret = bdrv_pwrite_sync(bs->file->bs, l1_table_offset,
1230 l1_table, l1_size2);
1232 for (i = 0; i < l1_size; i++) {
1233 be64_to_cpus(&l1_table[i]);
1236 if (l1_allocated)
1237 g_free(l1_table);
1238 return ret;
1244 /*********************************************************/
1245 /* refcount checking functions */
1248 static uint64_t refcount_array_byte_size(BDRVQcow2State *s, uint64_t entries)
1250 /* This assertion holds because there is no way we can address more than
1251 * 2^(64 - 9) clusters at once (with cluster size 512 = 2^9, and because
1252 * offsets have to be representable in bytes); due to every cluster
1253 * corresponding to one refcount entry, we are well below that limit */
1254 assert(entries < (UINT64_C(1) << (64 - 9)));
1256 /* Thanks to the assertion this will not overflow, because
1257 * s->refcount_order < 7.
1258 * (note: x << s->refcount_order == x * s->refcount_bits) */
1259 return DIV_ROUND_UP(entries << s->refcount_order, 8);
1263 * Reallocates *array so that it can hold new_size entries. *size must contain
1264 * the current number of entries in *array. If the reallocation fails, *array
1265 * and *size will not be modified and -errno will be returned. If the
1266 * reallocation is successful, *array will be set to the new buffer, *size
1267 * will be set to new_size and 0 will be returned. The size of the reallocated
1268 * refcount array buffer will be aligned to a cluster boundary, and the newly
1269 * allocated area will be zeroed.
1271 static int realloc_refcount_array(BDRVQcow2State *s, void **array,
1272 int64_t *size, int64_t new_size)
1274 int64_t old_byte_size, new_byte_size;
1275 void *new_ptr;
1277 /* Round to clusters so the array can be directly written to disk */
1278 old_byte_size = size_to_clusters(s, refcount_array_byte_size(s, *size))
1279 * s->cluster_size;
1280 new_byte_size = size_to_clusters(s, refcount_array_byte_size(s, new_size))
1281 * s->cluster_size;
1283 if (new_byte_size == old_byte_size) {
1284 *size = new_size;
1285 return 0;
1288 assert(new_byte_size > 0);
1290 if (new_byte_size > SIZE_MAX) {
1291 return -ENOMEM;
1294 new_ptr = g_try_realloc(*array, new_byte_size);
1295 if (!new_ptr) {
1296 return -ENOMEM;
1299 if (new_byte_size > old_byte_size) {
1300 memset((char *)new_ptr + old_byte_size, 0,
1301 new_byte_size - old_byte_size);
1304 *array = new_ptr;
1305 *size = new_size;
1307 return 0;
1311 * Increases the refcount for a range of clusters in a given refcount table.
1312 * This is used to construct a temporary refcount table out of L1 and L2 tables
1313 * which can be compared to the refcount table saved in the image.
1315 * Modifies the number of errors in res.
1317 static int inc_refcounts(BlockDriverState *bs,
1318 BdrvCheckResult *res,
1319 void **refcount_table,
1320 int64_t *refcount_table_size,
1321 int64_t offset, int64_t size)
1323 BDRVQcow2State *s = bs->opaque;
1324 uint64_t start, last, cluster_offset, k, refcount;
1325 int ret;
1327 if (size <= 0) {
1328 return 0;
1331 start = start_of_cluster(s, offset);
1332 last = start_of_cluster(s, offset + size - 1);
1333 for(cluster_offset = start; cluster_offset <= last;
1334 cluster_offset += s->cluster_size) {
1335 k = cluster_offset >> s->cluster_bits;
1336 if (k >= *refcount_table_size) {
1337 ret = realloc_refcount_array(s, refcount_table,
1338 refcount_table_size, k + 1);
1339 if (ret < 0) {
1340 res->check_errors++;
1341 return ret;
1345 refcount = s->get_refcount(*refcount_table, k);
1346 if (refcount == s->refcount_max) {
1347 fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
1348 "\n", cluster_offset);
1349 fprintf(stderr, "Use qemu-img amend to increase the refcount entry "
1350 "width or qemu-img convert to create a clean copy if the "
1351 "image cannot be opened for writing\n");
1352 res->corruptions++;
1353 continue;
1355 s->set_refcount(*refcount_table, k, refcount + 1);
1358 return 0;
1361 /* Flags for check_refcounts_l1() and check_refcounts_l2() */
1362 enum {
1363 CHECK_FRAG_INFO = 0x2, /* update BlockFragInfo counters */
1367 * Increases the refcount in the given refcount table for the all clusters
1368 * referenced in the L2 table. While doing so, performs some checks on L2
1369 * entries.
1371 * Returns the number of errors found by the checks or -errno if an internal
1372 * error occurred.
1374 static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
1375 void **refcount_table,
1376 int64_t *refcount_table_size, int64_t l2_offset,
1377 int flags)
1379 BDRVQcow2State *s = bs->opaque;
1380 uint64_t *l2_table, l2_entry;
1381 uint64_t next_contiguous_offset = 0;
1382 int i, l2_size, nb_csectors, ret;
1384 /* Read L2 table from disk */
1385 l2_size = s->l2_size * sizeof(uint64_t);
1386 l2_table = g_malloc(l2_size);
1388 ret = bdrv_pread(bs->file->bs, l2_offset, l2_table, l2_size);
1389 if (ret < 0) {
1390 fprintf(stderr, "ERROR: I/O error in check_refcounts_l2\n");
1391 res->check_errors++;
1392 goto fail;
1395 /* Do the actual checks */
1396 for(i = 0; i < s->l2_size; i++) {
1397 l2_entry = be64_to_cpu(l2_table[i]);
1399 switch (qcow2_get_cluster_type(l2_entry)) {
1400 case QCOW2_CLUSTER_COMPRESSED:
1401 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
1402 if (l2_entry & QCOW_OFLAG_COPIED) {
1403 fprintf(stderr, "ERROR: cluster %" PRId64 ": "
1404 "copied flag must never be set for compressed "
1405 "clusters\n", l2_entry >> s->cluster_bits);
1406 l2_entry &= ~QCOW_OFLAG_COPIED;
1407 res->corruptions++;
1410 /* Mark cluster as used */
1411 nb_csectors = ((l2_entry >> s->csize_shift) &
1412 s->csize_mask) + 1;
1413 l2_entry &= s->cluster_offset_mask;
1414 ret = inc_refcounts(bs, res, refcount_table, refcount_table_size,
1415 l2_entry & ~511, nb_csectors * 512);
1416 if (ret < 0) {
1417 goto fail;
1420 if (flags & CHECK_FRAG_INFO) {
1421 res->bfi.allocated_clusters++;
1422 res->bfi.compressed_clusters++;
1424 /* Compressed clusters are fragmented by nature. Since they
1425 * take up sub-sector space but we only have sector granularity
1426 * I/O we need to re-read the same sectors even for adjacent
1427 * compressed clusters.
1429 res->bfi.fragmented_clusters++;
1431 break;
1433 case QCOW2_CLUSTER_ZERO:
1434 if ((l2_entry & L2E_OFFSET_MASK) == 0) {
1435 break;
1437 /* fall through */
1439 case QCOW2_CLUSTER_NORMAL:
1441 uint64_t offset = l2_entry & L2E_OFFSET_MASK;
1443 if (flags & CHECK_FRAG_INFO) {
1444 res->bfi.allocated_clusters++;
1445 if (next_contiguous_offset &&
1446 offset != next_contiguous_offset) {
1447 res->bfi.fragmented_clusters++;
1449 next_contiguous_offset = offset + s->cluster_size;
1452 /* Mark cluster as used */
1453 ret = inc_refcounts(bs, res, refcount_table, refcount_table_size,
1454 offset, s->cluster_size);
1455 if (ret < 0) {
1456 goto fail;
1459 /* Correct offsets are cluster aligned */
1460 if (offset_into_cluster(s, offset)) {
1461 fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
1462 "properly aligned; L2 entry corrupted.\n", offset);
1463 res->corruptions++;
1465 break;
1468 case QCOW2_CLUSTER_UNALLOCATED:
1469 break;
1471 default:
1472 abort();
1476 g_free(l2_table);
1477 return 0;
1479 fail:
1480 g_free(l2_table);
1481 return ret;
1485 * Increases the refcount for the L1 table, its L2 tables and all referenced
1486 * clusters in the given refcount table. While doing so, performs some checks
1487 * on L1 and L2 entries.
1489 * Returns the number of errors found by the checks or -errno if an internal
1490 * error occurred.
1492 static int check_refcounts_l1(BlockDriverState *bs,
1493 BdrvCheckResult *res,
1494 void **refcount_table,
1495 int64_t *refcount_table_size,
1496 int64_t l1_table_offset, int l1_size,
1497 int flags)
1499 BDRVQcow2State *s = bs->opaque;
1500 uint64_t *l1_table = NULL, l2_offset, l1_size2;
1501 int i, ret;
1503 l1_size2 = l1_size * sizeof(uint64_t);
1505 /* Mark L1 table as used */
1506 ret = inc_refcounts(bs, res, refcount_table, refcount_table_size,
1507 l1_table_offset, l1_size2);
1508 if (ret < 0) {
1509 goto fail;
1512 /* Read L1 table entries from disk */
1513 if (l1_size2 > 0) {
1514 l1_table = g_try_malloc(l1_size2);
1515 if (l1_table == NULL) {
1516 ret = -ENOMEM;
1517 res->check_errors++;
1518 goto fail;
1520 ret = bdrv_pread(bs->file->bs, l1_table_offset, l1_table, l1_size2);
1521 if (ret < 0) {
1522 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
1523 res->check_errors++;
1524 goto fail;
1526 for(i = 0;i < l1_size; i++)
1527 be64_to_cpus(&l1_table[i]);
1530 /* Do the actual checks */
1531 for(i = 0; i < l1_size; i++) {
1532 l2_offset = l1_table[i];
1533 if (l2_offset) {
1534 /* Mark L2 table as used */
1535 l2_offset &= L1E_OFFSET_MASK;
1536 ret = inc_refcounts(bs, res, refcount_table, refcount_table_size,
1537 l2_offset, s->cluster_size);
1538 if (ret < 0) {
1539 goto fail;
1542 /* L2 tables are cluster aligned */
1543 if (offset_into_cluster(s, l2_offset)) {
1544 fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
1545 "cluster aligned; L1 entry corrupted\n", l2_offset);
1546 res->corruptions++;
1549 /* Process and check L2 entries */
1550 ret = check_refcounts_l2(bs, res, refcount_table,
1551 refcount_table_size, l2_offset, flags);
1552 if (ret < 0) {
1553 goto fail;
1557 g_free(l1_table);
1558 return 0;
1560 fail:
1561 g_free(l1_table);
1562 return ret;
1566 * Checks the OFLAG_COPIED flag for all L1 and L2 entries.
1568 * This function does not print an error message nor does it increment
1569 * check_errors if qcow2_get_refcount fails (this is because such an error will
1570 * have been already detected and sufficiently signaled by the calling function
1571 * (qcow2_check_refcounts) by the time this function is called).
1573 static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res,
1574 BdrvCheckMode fix)
1576 BDRVQcow2State *s = bs->opaque;
1577 uint64_t *l2_table = qemu_blockalign(bs, s->cluster_size);
1578 int ret;
1579 uint64_t refcount;
1580 int i, j;
1582 for (i = 0; i < s->l1_size; i++) {
1583 uint64_t l1_entry = s->l1_table[i];
1584 uint64_t l2_offset = l1_entry & L1E_OFFSET_MASK;
1585 bool l2_dirty = false;
1587 if (!l2_offset) {
1588 continue;
1591 ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
1592 &refcount);
1593 if (ret < 0) {
1594 /* don't print message nor increment check_errors */
1595 continue;
1597 if ((refcount == 1) != ((l1_entry & QCOW_OFLAG_COPIED) != 0)) {
1598 fprintf(stderr, "%s OFLAG_COPIED L2 cluster: l1_index=%d "
1599 "l1_entry=%" PRIx64 " refcount=%" PRIu64 "\n",
1600 fix & BDRV_FIX_ERRORS ? "Repairing" :
1601 "ERROR",
1602 i, l1_entry, refcount);
1603 if (fix & BDRV_FIX_ERRORS) {
1604 s->l1_table[i] = refcount == 1
1605 ? l1_entry | QCOW_OFLAG_COPIED
1606 : l1_entry & ~QCOW_OFLAG_COPIED;
1607 ret = qcow2_write_l1_entry(bs, i);
1608 if (ret < 0) {
1609 res->check_errors++;
1610 goto fail;
1612 res->corruptions_fixed++;
1613 } else {
1614 res->corruptions++;
1618 ret = bdrv_pread(bs->file->bs, l2_offset, l2_table,
1619 s->l2_size * sizeof(uint64_t));
1620 if (ret < 0) {
1621 fprintf(stderr, "ERROR: Could not read L2 table: %s\n",
1622 strerror(-ret));
1623 res->check_errors++;
1624 goto fail;
1627 for (j = 0; j < s->l2_size; j++) {
1628 uint64_t l2_entry = be64_to_cpu(l2_table[j]);
1629 uint64_t data_offset = l2_entry & L2E_OFFSET_MASK;
1630 int cluster_type = qcow2_get_cluster_type(l2_entry);
1632 if ((cluster_type == QCOW2_CLUSTER_NORMAL) ||
1633 ((cluster_type == QCOW2_CLUSTER_ZERO) && (data_offset != 0))) {
1634 ret = qcow2_get_refcount(bs,
1635 data_offset >> s->cluster_bits,
1636 &refcount);
1637 if (ret < 0) {
1638 /* don't print message nor increment check_errors */
1639 continue;
1641 if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) {
1642 fprintf(stderr, "%s OFLAG_COPIED data cluster: "
1643 "l2_entry=%" PRIx64 " refcount=%" PRIu64 "\n",
1644 fix & BDRV_FIX_ERRORS ? "Repairing" :
1645 "ERROR",
1646 l2_entry, refcount);
1647 if (fix & BDRV_FIX_ERRORS) {
1648 l2_table[j] = cpu_to_be64(refcount == 1
1649 ? l2_entry | QCOW_OFLAG_COPIED
1650 : l2_entry & ~QCOW_OFLAG_COPIED);
1651 l2_dirty = true;
1652 res->corruptions_fixed++;
1653 } else {
1654 res->corruptions++;
1660 if (l2_dirty) {
1661 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2,
1662 l2_offset, s->cluster_size);
1663 if (ret < 0) {
1664 fprintf(stderr, "ERROR: Could not write L2 table; metadata "
1665 "overlap check failed: %s\n", strerror(-ret));
1666 res->check_errors++;
1667 goto fail;
1670 ret = bdrv_pwrite(bs->file->bs, l2_offset, l2_table,
1671 s->cluster_size);
1672 if (ret < 0) {
1673 fprintf(stderr, "ERROR: Could not write L2 table: %s\n",
1674 strerror(-ret));
1675 res->check_errors++;
1676 goto fail;
1681 ret = 0;
1683 fail:
1684 qemu_vfree(l2_table);
1685 return ret;
1689 * Checks consistency of refblocks and accounts for each refblock in
1690 * *refcount_table.
1692 static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res,
1693 BdrvCheckMode fix, bool *rebuild,
1694 void **refcount_table, int64_t *nb_clusters)
1696 BDRVQcow2State *s = bs->opaque;
1697 int64_t i, size;
1698 int ret;
1700 for(i = 0; i < s->refcount_table_size; i++) {
1701 uint64_t offset, cluster;
1702 offset = s->refcount_table[i];
1703 cluster = offset >> s->cluster_bits;
1705 /* Refcount blocks are cluster aligned */
1706 if (offset_into_cluster(s, offset)) {
1707 fprintf(stderr, "ERROR refcount block %" PRId64 " is not "
1708 "cluster aligned; refcount table entry corrupted\n", i);
1709 res->corruptions++;
1710 *rebuild = true;
1711 continue;
1714 if (cluster >= *nb_clusters) {
1715 fprintf(stderr, "%s refcount block %" PRId64 " is outside image\n",
1716 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
1718 if (fix & BDRV_FIX_ERRORS) {
1719 int64_t new_nb_clusters;
1721 if (offset > INT64_MAX - s->cluster_size) {
1722 ret = -EINVAL;
1723 goto resize_fail;
1726 ret = bdrv_truncate(bs->file->bs, offset + s->cluster_size);
1727 if (ret < 0) {
1728 goto resize_fail;
1730 size = bdrv_getlength(bs->file->bs);
1731 if (size < 0) {
1732 ret = size;
1733 goto resize_fail;
1736 new_nb_clusters = size_to_clusters(s, size);
1737 assert(new_nb_clusters >= *nb_clusters);
1739 ret = realloc_refcount_array(s, refcount_table,
1740 nb_clusters, new_nb_clusters);
1741 if (ret < 0) {
1742 res->check_errors++;
1743 return ret;
1746 if (cluster >= *nb_clusters) {
1747 ret = -EINVAL;
1748 goto resize_fail;
1751 res->corruptions_fixed++;
1752 ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
1753 offset, s->cluster_size);
1754 if (ret < 0) {
1755 return ret;
1757 /* No need to check whether the refcount is now greater than 1:
1758 * This area was just allocated and zeroed, so it can only be
1759 * exactly 1 after inc_refcounts() */
1760 continue;
1762 resize_fail:
1763 res->corruptions++;
1764 *rebuild = true;
1765 fprintf(stderr, "ERROR could not resize image: %s\n",
1766 strerror(-ret));
1767 } else {
1768 res->corruptions++;
1770 continue;
1773 if (offset != 0) {
1774 ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
1775 offset, s->cluster_size);
1776 if (ret < 0) {
1777 return ret;
1779 if (s->get_refcount(*refcount_table, cluster) != 1) {
1780 fprintf(stderr, "ERROR refcount block %" PRId64
1781 " refcount=%" PRIu64 "\n", i,
1782 s->get_refcount(*refcount_table, cluster));
1783 res->corruptions++;
1784 *rebuild = true;
1789 return 0;
1793 * Calculates an in-memory refcount table.
1795 static int calculate_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
1796 BdrvCheckMode fix, bool *rebuild,
1797 void **refcount_table, int64_t *nb_clusters)
1799 BDRVQcow2State *s = bs->opaque;
1800 int64_t i;
1801 QCowSnapshot *sn;
1802 int ret;
1804 if (!*refcount_table) {
1805 int64_t old_size = 0;
1806 ret = realloc_refcount_array(s, refcount_table,
1807 &old_size, *nb_clusters);
1808 if (ret < 0) {
1809 res->check_errors++;
1810 return ret;
1814 /* header */
1815 ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
1816 0, s->cluster_size);
1817 if (ret < 0) {
1818 return ret;
1821 /* current L1 table */
1822 ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
1823 s->l1_table_offset, s->l1_size, CHECK_FRAG_INFO);
1824 if (ret < 0) {
1825 return ret;
1828 /* snapshots */
1829 for (i = 0; i < s->nb_snapshots; i++) {
1830 sn = s->snapshots + i;
1831 ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
1832 sn->l1_table_offset, sn->l1_size, 0);
1833 if (ret < 0) {
1834 return ret;
1837 ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
1838 s->snapshots_offset, s->snapshots_size);
1839 if (ret < 0) {
1840 return ret;
1843 /* refcount data */
1844 ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
1845 s->refcount_table_offset,
1846 s->refcount_table_size * sizeof(uint64_t));
1847 if (ret < 0) {
1848 return ret;
1851 return check_refblocks(bs, res, fix, rebuild, refcount_table, nb_clusters);
1855 * Compares the actual reference count for each cluster in the image against the
1856 * refcount as reported by the refcount structures on-disk.
1858 static void compare_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
1859 BdrvCheckMode fix, bool *rebuild,
1860 int64_t *highest_cluster,
1861 void *refcount_table, int64_t nb_clusters)
1863 BDRVQcow2State *s = bs->opaque;
1864 int64_t i;
1865 uint64_t refcount1, refcount2;
1866 int ret;
1868 for (i = 0, *highest_cluster = 0; i < nb_clusters; i++) {
1869 ret = qcow2_get_refcount(bs, i, &refcount1);
1870 if (ret < 0) {
1871 fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n",
1872 i, strerror(-ret));
1873 res->check_errors++;
1874 continue;
1877 refcount2 = s->get_refcount(refcount_table, i);
1879 if (refcount1 > 0 || refcount2 > 0) {
1880 *highest_cluster = i;
1883 if (refcount1 != refcount2) {
1884 /* Check if we're allowed to fix the mismatch */
1885 int *num_fixed = NULL;
1886 if (refcount1 == 0) {
1887 *rebuild = true;
1888 } else if (refcount1 > refcount2 && (fix & BDRV_FIX_LEAKS)) {
1889 num_fixed = &res->leaks_fixed;
1890 } else if (refcount1 < refcount2 && (fix & BDRV_FIX_ERRORS)) {
1891 num_fixed = &res->corruptions_fixed;
1894 fprintf(stderr, "%s cluster %" PRId64 " refcount=%" PRIu64
1895 " reference=%" PRIu64 "\n",
1896 num_fixed != NULL ? "Repairing" :
1897 refcount1 < refcount2 ? "ERROR" :
1898 "Leaked",
1899 i, refcount1, refcount2);
1901 if (num_fixed) {
1902 ret = update_refcount(bs, i << s->cluster_bits, 1,
1903 refcount_diff(refcount1, refcount2),
1904 refcount1 > refcount2,
1905 QCOW2_DISCARD_ALWAYS);
1906 if (ret >= 0) {
1907 (*num_fixed)++;
1908 continue;
1912 /* And if we couldn't, print an error */
1913 if (refcount1 < refcount2) {
1914 res->corruptions++;
1915 } else {
1916 res->leaks++;
1923 * Allocates clusters using an in-memory refcount table (IMRT) in contrast to
1924 * the on-disk refcount structures.
1926 * On input, *first_free_cluster tells where to start looking, and need not
1927 * actually be a free cluster; the returned offset will not be before that
1928 * cluster. On output, *first_free_cluster points to the first gap found, even
1929 * if that gap was too small to be used as the returned offset.
1931 * Note that *first_free_cluster is a cluster index whereas the return value is
1932 * an offset.
1934 static int64_t alloc_clusters_imrt(BlockDriverState *bs,
1935 int cluster_count,
1936 void **refcount_table,
1937 int64_t *imrt_nb_clusters,
1938 int64_t *first_free_cluster)
1940 BDRVQcow2State *s = bs->opaque;
1941 int64_t cluster = *first_free_cluster, i;
1942 bool first_gap = true;
1943 int contiguous_free_clusters;
1944 int ret;
1946 /* Starting at *first_free_cluster, find a range of at least cluster_count
1947 * continuously free clusters */
1948 for (contiguous_free_clusters = 0;
1949 cluster < *imrt_nb_clusters &&
1950 contiguous_free_clusters < cluster_count;
1951 cluster++)
1953 if (!s->get_refcount(*refcount_table, cluster)) {
1954 contiguous_free_clusters++;
1955 if (first_gap) {
1956 /* If this is the first free cluster found, update
1957 * *first_free_cluster accordingly */
1958 *first_free_cluster = cluster;
1959 first_gap = false;
1961 } else if (contiguous_free_clusters) {
1962 contiguous_free_clusters = 0;
1966 /* If contiguous_free_clusters is greater than zero, it contains the number
1967 * of continuously free clusters until the current cluster; the first free
1968 * cluster in the current "gap" is therefore
1969 * cluster - contiguous_free_clusters */
1971 /* If no such range could be found, grow the in-memory refcount table
1972 * accordingly to append free clusters at the end of the image */
1973 if (contiguous_free_clusters < cluster_count) {
1974 /* contiguous_free_clusters clusters are already empty at the image end;
1975 * we need cluster_count clusters; therefore, we have to allocate
1976 * cluster_count - contiguous_free_clusters new clusters at the end of
1977 * the image (which is the current value of cluster; note that cluster
1978 * may exceed old_imrt_nb_clusters if *first_free_cluster pointed beyond
1979 * the image end) */
1980 ret = realloc_refcount_array(s, refcount_table, imrt_nb_clusters,
1981 cluster + cluster_count
1982 - contiguous_free_clusters);
1983 if (ret < 0) {
1984 return ret;
1988 /* Go back to the first free cluster */
1989 cluster -= contiguous_free_clusters;
1990 for (i = 0; i < cluster_count; i++) {
1991 s->set_refcount(*refcount_table, cluster + i, 1);
1994 return cluster << s->cluster_bits;
1998 * Creates a new refcount structure based solely on the in-memory information
1999 * given through *refcount_table. All necessary allocations will be reflected
2000 * in that array.
2002 * On success, the old refcount structure is leaked (it will be covered by the
2003 * new refcount structure).
2005 static int rebuild_refcount_structure(BlockDriverState *bs,
2006 BdrvCheckResult *res,
2007 void **refcount_table,
2008 int64_t *nb_clusters)
2010 BDRVQcow2State *s = bs->opaque;
2011 int64_t first_free_cluster = 0, reftable_offset = -1, cluster = 0;
2012 int64_t refblock_offset, refblock_start, refblock_index;
2013 uint32_t reftable_size = 0;
2014 uint64_t *on_disk_reftable = NULL;
2015 void *on_disk_refblock;
2016 int ret = 0;
2017 struct {
2018 uint64_t reftable_offset;
2019 uint32_t reftable_clusters;
2020 } QEMU_PACKED reftable_offset_and_clusters;
2022 qcow2_cache_empty(bs, s->refcount_block_cache);
2024 write_refblocks:
2025 for (; cluster < *nb_clusters; cluster++) {
2026 if (!s->get_refcount(*refcount_table, cluster)) {
2027 continue;
2030 refblock_index = cluster >> s->refcount_block_bits;
2031 refblock_start = refblock_index << s->refcount_block_bits;
2033 /* Don't allocate a cluster in a refblock already written to disk */
2034 if (first_free_cluster < refblock_start) {
2035 first_free_cluster = refblock_start;
2037 refblock_offset = alloc_clusters_imrt(bs, 1, refcount_table,
2038 nb_clusters, &first_free_cluster);
2039 if (refblock_offset < 0) {
2040 fprintf(stderr, "ERROR allocating refblock: %s\n",
2041 strerror(-refblock_offset));
2042 res->check_errors++;
2043 ret = refblock_offset;
2044 goto fail;
2047 if (reftable_size <= refblock_index) {
2048 uint32_t old_reftable_size = reftable_size;
2049 uint64_t *new_on_disk_reftable;
2051 reftable_size = ROUND_UP((refblock_index + 1) * sizeof(uint64_t),
2052 s->cluster_size) / sizeof(uint64_t);
2053 new_on_disk_reftable = g_try_realloc(on_disk_reftable,
2054 reftable_size *
2055 sizeof(uint64_t));
2056 if (!new_on_disk_reftable) {
2057 res->check_errors++;
2058 ret = -ENOMEM;
2059 goto fail;
2061 on_disk_reftable = new_on_disk_reftable;
2063 memset(on_disk_reftable + old_reftable_size, 0,
2064 (reftable_size - old_reftable_size) * sizeof(uint64_t));
2066 /* The offset we have for the reftable is now no longer valid;
2067 * this will leak that range, but we can easily fix that by running
2068 * a leak-fixing check after this rebuild operation */
2069 reftable_offset = -1;
2071 on_disk_reftable[refblock_index] = refblock_offset;
2073 /* If this is apparently the last refblock (for now), try to squeeze the
2074 * reftable in */
2075 if (refblock_index == (*nb_clusters - 1) >> s->refcount_block_bits &&
2076 reftable_offset < 0)
2078 uint64_t reftable_clusters = size_to_clusters(s, reftable_size *
2079 sizeof(uint64_t));
2080 reftable_offset = alloc_clusters_imrt(bs, reftable_clusters,
2081 refcount_table, nb_clusters,
2082 &first_free_cluster);
2083 if (reftable_offset < 0) {
2084 fprintf(stderr, "ERROR allocating reftable: %s\n",
2085 strerror(-reftable_offset));
2086 res->check_errors++;
2087 ret = reftable_offset;
2088 goto fail;
2092 ret = qcow2_pre_write_overlap_check(bs, 0, refblock_offset,
2093 s->cluster_size);
2094 if (ret < 0) {
2095 fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret));
2096 goto fail;
2099 /* The size of *refcount_table is always cluster-aligned, therefore the
2100 * write operation will not overflow */
2101 on_disk_refblock = (void *)((char *) *refcount_table +
2102 refblock_index * s->cluster_size);
2104 ret = bdrv_write(bs->file->bs, refblock_offset / BDRV_SECTOR_SIZE,
2105 on_disk_refblock, s->cluster_sectors);
2106 if (ret < 0) {
2107 fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret));
2108 goto fail;
2111 /* Go to the end of this refblock */
2112 cluster = refblock_start + s->refcount_block_size - 1;
2115 if (reftable_offset < 0) {
2116 uint64_t post_refblock_start, reftable_clusters;
2118 post_refblock_start = ROUND_UP(*nb_clusters, s->refcount_block_size);
2119 reftable_clusters = size_to_clusters(s,
2120 reftable_size * sizeof(uint64_t));
2121 /* Not pretty but simple */
2122 if (first_free_cluster < post_refblock_start) {
2123 first_free_cluster = post_refblock_start;
2125 reftable_offset = alloc_clusters_imrt(bs, reftable_clusters,
2126 refcount_table, nb_clusters,
2127 &first_free_cluster);
2128 if (reftable_offset < 0) {
2129 fprintf(stderr, "ERROR allocating reftable: %s\n",
2130 strerror(-reftable_offset));
2131 res->check_errors++;
2132 ret = reftable_offset;
2133 goto fail;
2136 goto write_refblocks;
2139 assert(on_disk_reftable);
2141 for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) {
2142 cpu_to_be64s(&on_disk_reftable[refblock_index]);
2145 ret = qcow2_pre_write_overlap_check(bs, 0, reftable_offset,
2146 reftable_size * sizeof(uint64_t));
2147 if (ret < 0) {
2148 fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret));
2149 goto fail;
2152 assert(reftable_size < INT_MAX / sizeof(uint64_t));
2153 ret = bdrv_pwrite(bs->file->bs, reftable_offset, on_disk_reftable,
2154 reftable_size * sizeof(uint64_t));
2155 if (ret < 0) {
2156 fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret));
2157 goto fail;
2160 /* Enter new reftable into the image header */
2161 cpu_to_be64w(&reftable_offset_and_clusters.reftable_offset,
2162 reftable_offset);
2163 cpu_to_be32w(&reftable_offset_and_clusters.reftable_clusters,
2164 size_to_clusters(s, reftable_size * sizeof(uint64_t)));
2165 ret = bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader,
2166 refcount_table_offset),
2167 &reftable_offset_and_clusters,
2168 sizeof(reftable_offset_and_clusters));
2169 if (ret < 0) {
2170 fprintf(stderr, "ERROR setting reftable: %s\n", strerror(-ret));
2171 goto fail;
2174 for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) {
2175 be64_to_cpus(&on_disk_reftable[refblock_index]);
2177 s->refcount_table = on_disk_reftable;
2178 s->refcount_table_offset = reftable_offset;
2179 s->refcount_table_size = reftable_size;
2181 return 0;
2183 fail:
2184 g_free(on_disk_reftable);
2185 return ret;
2189 * Checks an image for refcount consistency.
2191 * Returns 0 if no errors are found, the number of errors in case the image is
2192 * detected as corrupted, and -errno when an internal error occurred.
2194 int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
2195 BdrvCheckMode fix)
2197 BDRVQcow2State *s = bs->opaque;
2198 BdrvCheckResult pre_compare_res;
2199 int64_t size, highest_cluster, nb_clusters;
2200 void *refcount_table = NULL;
2201 bool rebuild = false;
2202 int ret;
2204 size = bdrv_getlength(bs->file->bs);
2205 if (size < 0) {
2206 res->check_errors++;
2207 return size;
2210 nb_clusters = size_to_clusters(s, size);
2211 if (nb_clusters > INT_MAX) {
2212 res->check_errors++;
2213 return -EFBIG;
2216 res->bfi.total_clusters =
2217 size_to_clusters(s, bs->total_sectors * BDRV_SECTOR_SIZE);
2219 ret = calculate_refcounts(bs, res, fix, &rebuild, &refcount_table,
2220 &nb_clusters);
2221 if (ret < 0) {
2222 goto fail;
2225 /* In case we don't need to rebuild the refcount structure (but want to fix
2226 * something), this function is immediately called again, in which case the
2227 * result should be ignored */
2228 pre_compare_res = *res;
2229 compare_refcounts(bs, res, 0, &rebuild, &highest_cluster, refcount_table,
2230 nb_clusters);
2232 if (rebuild && (fix & BDRV_FIX_ERRORS)) {
2233 BdrvCheckResult old_res = *res;
2234 int fresh_leaks = 0;
2236 fprintf(stderr, "Rebuilding refcount structure\n");
2237 ret = rebuild_refcount_structure(bs, res, &refcount_table,
2238 &nb_clusters);
2239 if (ret < 0) {
2240 goto fail;
2243 res->corruptions = 0;
2244 res->leaks = 0;
2246 /* Because the old reftable has been exchanged for a new one the
2247 * references have to be recalculated */
2248 rebuild = false;
2249 memset(refcount_table, 0, refcount_array_byte_size(s, nb_clusters));
2250 ret = calculate_refcounts(bs, res, 0, &rebuild, &refcount_table,
2251 &nb_clusters);
2252 if (ret < 0) {
2253 goto fail;
2256 if (fix & BDRV_FIX_LEAKS) {
2257 /* The old refcount structures are now leaked, fix it; the result
2258 * can be ignored, aside from leaks which were introduced by
2259 * rebuild_refcount_structure() that could not be fixed */
2260 BdrvCheckResult saved_res = *res;
2261 *res = (BdrvCheckResult){ 0 };
2263 compare_refcounts(bs, res, BDRV_FIX_LEAKS, &rebuild,
2264 &highest_cluster, refcount_table, nb_clusters);
2265 if (rebuild) {
2266 fprintf(stderr, "ERROR rebuilt refcount structure is still "
2267 "broken\n");
2270 /* Any leaks accounted for here were introduced by
2271 * rebuild_refcount_structure() because that function has created a
2272 * new refcount structure from scratch */
2273 fresh_leaks = res->leaks;
2274 *res = saved_res;
2277 if (res->corruptions < old_res.corruptions) {
2278 res->corruptions_fixed += old_res.corruptions - res->corruptions;
2280 if (res->leaks < old_res.leaks) {
2281 res->leaks_fixed += old_res.leaks - res->leaks;
2283 res->leaks += fresh_leaks;
2284 } else if (fix) {
2285 if (rebuild) {
2286 fprintf(stderr, "ERROR need to rebuild refcount structures\n");
2287 res->check_errors++;
2288 ret = -EIO;
2289 goto fail;
2292 if (res->leaks || res->corruptions) {
2293 *res = pre_compare_res;
2294 compare_refcounts(bs, res, fix, &rebuild, &highest_cluster,
2295 refcount_table, nb_clusters);
2299 /* check OFLAG_COPIED */
2300 ret = check_oflag_copied(bs, res, fix);
2301 if (ret < 0) {
2302 goto fail;
2305 res->image_end_offset = (highest_cluster + 1) * s->cluster_size;
2306 ret = 0;
2308 fail:
2309 g_free(refcount_table);
2311 return ret;
2314 #define overlaps_with(ofs, sz) \
2315 ranges_overlap(offset, size, ofs, sz)
2318 * Checks if the given offset into the image file is actually free to use by
2319 * looking for overlaps with important metadata sections (L1/L2 tables etc.),
2320 * i.e. a sanity check without relying on the refcount tables.
2322 * The ign parameter specifies what checks not to perform (being a bitmask of
2323 * QCow2MetadataOverlap values), i.e., what sections to ignore.
2325 * Returns:
2326 * - 0 if writing to this offset will not affect the mentioned metadata
2327 * - a positive QCow2MetadataOverlap value indicating one overlapping section
2328 * - a negative value (-errno) indicating an error while performing a check,
2329 * e.g. when bdrv_read failed on QCOW2_OL_INACTIVE_L2
2331 int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
2332 int64_t size)
2334 BDRVQcow2State *s = bs->opaque;
2335 int chk = s->overlap_check & ~ign;
2336 int i, j;
2338 if (!size) {
2339 return 0;
2342 if (chk & QCOW2_OL_MAIN_HEADER) {
2343 if (offset < s->cluster_size) {
2344 return QCOW2_OL_MAIN_HEADER;
2348 /* align range to test to cluster boundaries */
2349 size = align_offset(offset_into_cluster(s, offset) + size, s->cluster_size);
2350 offset = start_of_cluster(s, offset);
2352 if ((chk & QCOW2_OL_ACTIVE_L1) && s->l1_size) {
2353 if (overlaps_with(s->l1_table_offset, s->l1_size * sizeof(uint64_t))) {
2354 return QCOW2_OL_ACTIVE_L1;
2358 if ((chk & QCOW2_OL_REFCOUNT_TABLE) && s->refcount_table_size) {
2359 if (overlaps_with(s->refcount_table_offset,
2360 s->refcount_table_size * sizeof(uint64_t))) {
2361 return QCOW2_OL_REFCOUNT_TABLE;
2365 if ((chk & QCOW2_OL_SNAPSHOT_TABLE) && s->snapshots_size) {
2366 if (overlaps_with(s->snapshots_offset, s->snapshots_size)) {
2367 return QCOW2_OL_SNAPSHOT_TABLE;
2371 if ((chk & QCOW2_OL_INACTIVE_L1) && s->snapshots) {
2372 for (i = 0; i < s->nb_snapshots; i++) {
2373 if (s->snapshots[i].l1_size &&
2374 overlaps_with(s->snapshots[i].l1_table_offset,
2375 s->snapshots[i].l1_size * sizeof(uint64_t))) {
2376 return QCOW2_OL_INACTIVE_L1;
2381 if ((chk & QCOW2_OL_ACTIVE_L2) && s->l1_table) {
2382 for (i = 0; i < s->l1_size; i++) {
2383 if ((s->l1_table[i] & L1E_OFFSET_MASK) &&
2384 overlaps_with(s->l1_table[i] & L1E_OFFSET_MASK,
2385 s->cluster_size)) {
2386 return QCOW2_OL_ACTIVE_L2;
2391 if ((chk & QCOW2_OL_REFCOUNT_BLOCK) && s->refcount_table) {
2392 for (i = 0; i < s->refcount_table_size; i++) {
2393 if ((s->refcount_table[i] & REFT_OFFSET_MASK) &&
2394 overlaps_with(s->refcount_table[i] & REFT_OFFSET_MASK,
2395 s->cluster_size)) {
2396 return QCOW2_OL_REFCOUNT_BLOCK;
2401 if ((chk & QCOW2_OL_INACTIVE_L2) && s->snapshots) {
2402 for (i = 0; i < s->nb_snapshots; i++) {
2403 uint64_t l1_ofs = s->snapshots[i].l1_table_offset;
2404 uint32_t l1_sz = s->snapshots[i].l1_size;
2405 uint64_t l1_sz2 = l1_sz * sizeof(uint64_t);
2406 uint64_t *l1 = g_try_malloc(l1_sz2);
2407 int ret;
2409 if (l1_sz2 && l1 == NULL) {
2410 return -ENOMEM;
2413 ret = bdrv_pread(bs->file->bs, l1_ofs, l1, l1_sz2);
2414 if (ret < 0) {
2415 g_free(l1);
2416 return ret;
2419 for (j = 0; j < l1_sz; j++) {
2420 uint64_t l2_ofs = be64_to_cpu(l1[j]) & L1E_OFFSET_MASK;
2421 if (l2_ofs && overlaps_with(l2_ofs, s->cluster_size)) {
2422 g_free(l1);
2423 return QCOW2_OL_INACTIVE_L2;
2427 g_free(l1);
2431 return 0;
2434 static const char *metadata_ol_names[] = {
2435 [QCOW2_OL_MAIN_HEADER_BITNR] = "qcow2_header",
2436 [QCOW2_OL_ACTIVE_L1_BITNR] = "active L1 table",
2437 [QCOW2_OL_ACTIVE_L2_BITNR] = "active L2 table",
2438 [QCOW2_OL_REFCOUNT_TABLE_BITNR] = "refcount table",
2439 [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = "refcount block",
2440 [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = "snapshot table",
2441 [QCOW2_OL_INACTIVE_L1_BITNR] = "inactive L1 table",
2442 [QCOW2_OL_INACTIVE_L2_BITNR] = "inactive L2 table",
2446 * First performs a check for metadata overlaps (through
2447 * qcow2_check_metadata_overlap); if that fails with a negative value (error
2448 * while performing a check), that value is returned. If an impending overlap
2449 * is detected, the BDS will be made unusable, the qcow2 file marked corrupt
2450 * and -EIO returned.
2452 * Returns 0 if there were neither overlaps nor errors while checking for
2453 * overlaps; or a negative value (-errno) on error.
2455 int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
2456 int64_t size)
2458 int ret = qcow2_check_metadata_overlap(bs, ign, offset, size);
2460 if (ret < 0) {
2461 return ret;
2462 } else if (ret > 0) {
2463 int metadata_ol_bitnr = ctz32(ret);
2464 assert(metadata_ol_bitnr < QCOW2_OL_MAX_BITNR);
2466 qcow2_signal_corruption(bs, true, offset, size, "Preventing invalid "
2467 "write on metadata (overlaps with %s)",
2468 metadata_ol_names[metadata_ol_bitnr]);
2469 return -EIO;
2472 return 0;
2475 /* A pointer to a function of this type is given to walk_over_reftable(). That
2476 * function will create refblocks and pass them to a RefblockFinishOp once they
2477 * are completed (@refblock). @refblock_empty is set if the refblock is
2478 * completely empty.
2480 * Along with the refblock, a corresponding reftable entry is passed, in the
2481 * reftable @reftable (which may be reallocated) at @reftable_index.
2483 * @allocated should be set to true if a new cluster has been allocated.
2485 typedef int (RefblockFinishOp)(BlockDriverState *bs, uint64_t **reftable,
2486 uint64_t reftable_index, uint64_t *reftable_size,
2487 void *refblock, bool refblock_empty,
2488 bool *allocated, Error **errp);
2491 * This "operation" for walk_over_reftable() allocates the refblock on disk (if
2492 * it is not empty) and inserts its offset into the new reftable. The size of
2493 * this new reftable is increased as required.
2495 static int alloc_refblock(BlockDriverState *bs, uint64_t **reftable,
2496 uint64_t reftable_index, uint64_t *reftable_size,
2497 void *refblock, bool refblock_empty, bool *allocated,
2498 Error **errp)
2500 BDRVQcow2State *s = bs->opaque;
2501 int64_t offset;
2503 if (!refblock_empty && reftable_index >= *reftable_size) {
2504 uint64_t *new_reftable;
2505 uint64_t new_reftable_size;
2507 new_reftable_size = ROUND_UP(reftable_index + 1,
2508 s->cluster_size / sizeof(uint64_t));
2509 if (new_reftable_size > QCOW_MAX_REFTABLE_SIZE / sizeof(uint64_t)) {
2510 error_setg(errp,
2511 "This operation would make the refcount table grow "
2512 "beyond the maximum size supported by QEMU, aborting");
2513 return -ENOTSUP;
2516 new_reftable = g_try_realloc(*reftable, new_reftable_size *
2517 sizeof(uint64_t));
2518 if (!new_reftable) {
2519 error_setg(errp, "Failed to increase reftable buffer size");
2520 return -ENOMEM;
2523 memset(new_reftable + *reftable_size, 0,
2524 (new_reftable_size - *reftable_size) * sizeof(uint64_t));
2526 *reftable = new_reftable;
2527 *reftable_size = new_reftable_size;
2530 if (!refblock_empty && !(*reftable)[reftable_index]) {
2531 offset = qcow2_alloc_clusters(bs, s->cluster_size);
2532 if (offset < 0) {
2533 error_setg_errno(errp, -offset, "Failed to allocate refblock");
2534 return offset;
2536 (*reftable)[reftable_index] = offset;
2537 *allocated = true;
2540 return 0;
2544 * This "operation" for walk_over_reftable() writes the refblock to disk at the
2545 * offset specified by the new reftable's entry. It does not modify the new
2546 * reftable or change any refcounts.
2548 static int flush_refblock(BlockDriverState *bs, uint64_t **reftable,
2549 uint64_t reftable_index, uint64_t *reftable_size,
2550 void *refblock, bool refblock_empty, bool *allocated,
2551 Error **errp)
2553 BDRVQcow2State *s = bs->opaque;
2554 int64_t offset;
2555 int ret;
2557 if (reftable_index < *reftable_size && (*reftable)[reftable_index]) {
2558 offset = (*reftable)[reftable_index];
2560 ret = qcow2_pre_write_overlap_check(bs, 0, offset, s->cluster_size);
2561 if (ret < 0) {
2562 error_setg_errno(errp, -ret, "Overlap check failed");
2563 return ret;
2566 ret = bdrv_pwrite(bs->file->bs, offset, refblock, s->cluster_size);
2567 if (ret < 0) {
2568 error_setg_errno(errp, -ret, "Failed to write refblock");
2569 return ret;
2571 } else {
2572 assert(refblock_empty);
2575 return 0;
2579 * This function walks over the existing reftable and every referenced refblock;
2580 * if @new_set_refcount is non-NULL, it is called for every refcount entry to
2581 * create an equal new entry in the passed @new_refblock. Once that
2582 * @new_refblock is completely filled, @operation will be called.
2584 * @status_cb and @cb_opaque are used for the amend operation's status callback.
2585 * @index is the index of the walk_over_reftable() calls and @total is the total
2586 * number of walk_over_reftable() calls per amend operation. Both are used for
2587 * calculating the parameters for the status callback.
2589 * @allocated is set to true if a new cluster has been allocated.
2591 static int walk_over_reftable(BlockDriverState *bs, uint64_t **new_reftable,
2592 uint64_t *new_reftable_index,
2593 uint64_t *new_reftable_size,
2594 void *new_refblock, int new_refblock_size,
2595 int new_refcount_bits,
2596 RefblockFinishOp *operation, bool *allocated,
2597 Qcow2SetRefcountFunc *new_set_refcount,
2598 BlockDriverAmendStatusCB *status_cb,
2599 void *cb_opaque, int index, int total,
2600 Error **errp)
2602 BDRVQcow2State *s = bs->opaque;
2603 uint64_t reftable_index;
2604 bool new_refblock_empty = true;
2605 int refblock_index;
2606 int new_refblock_index = 0;
2607 int ret;
2609 for (reftable_index = 0; reftable_index < s->refcount_table_size;
2610 reftable_index++)
2612 uint64_t refblock_offset = s->refcount_table[reftable_index]
2613 & REFT_OFFSET_MASK;
2615 status_cb(bs, (uint64_t)index * s->refcount_table_size + reftable_index,
2616 (uint64_t)total * s->refcount_table_size, cb_opaque);
2618 if (refblock_offset) {
2619 void *refblock;
2621 if (offset_into_cluster(s, refblock_offset)) {
2622 qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#"
2623 PRIx64 " unaligned (reftable index: %#"
2624 PRIx64 ")", refblock_offset,
2625 reftable_index);
2626 error_setg(errp,
2627 "Image is corrupt (unaligned refblock offset)");
2628 return -EIO;
2631 ret = qcow2_cache_get(bs, s->refcount_block_cache, refblock_offset,
2632 &refblock);
2633 if (ret < 0) {
2634 error_setg_errno(errp, -ret, "Failed to retrieve refblock");
2635 return ret;
2638 for (refblock_index = 0; refblock_index < s->refcount_block_size;
2639 refblock_index++)
2641 uint64_t refcount;
2643 if (new_refblock_index >= new_refblock_size) {
2644 /* new_refblock is now complete */
2645 ret = operation(bs, new_reftable, *new_reftable_index,
2646 new_reftable_size, new_refblock,
2647 new_refblock_empty, allocated, errp);
2648 if (ret < 0) {
2649 qcow2_cache_put(bs, s->refcount_block_cache, &refblock);
2650 return ret;
2653 (*new_reftable_index)++;
2654 new_refblock_index = 0;
2655 new_refblock_empty = true;
2658 refcount = s->get_refcount(refblock, refblock_index);
2659 if (new_refcount_bits < 64 && refcount >> new_refcount_bits) {
2660 uint64_t offset;
2662 qcow2_cache_put(bs, s->refcount_block_cache, &refblock);
2664 offset = ((reftable_index << s->refcount_block_bits)
2665 + refblock_index) << s->cluster_bits;
2667 error_setg(errp, "Cannot decrease refcount entry width to "
2668 "%i bits: Cluster at offset %#" PRIx64 " has a "
2669 "refcount of %" PRIu64, new_refcount_bits,
2670 offset, refcount);
2671 return -EINVAL;
2674 if (new_set_refcount) {
2675 new_set_refcount(new_refblock, new_refblock_index++,
2676 refcount);
2677 } else {
2678 new_refblock_index++;
2680 new_refblock_empty = new_refblock_empty && refcount == 0;
2683 qcow2_cache_put(bs, s->refcount_block_cache, &refblock);
2684 } else {
2685 /* No refblock means every refcount is 0 */
2686 for (refblock_index = 0; refblock_index < s->refcount_block_size;
2687 refblock_index++)
2689 if (new_refblock_index >= new_refblock_size) {
2690 /* new_refblock is now complete */
2691 ret = operation(bs, new_reftable, *new_reftable_index,
2692 new_reftable_size, new_refblock,
2693 new_refblock_empty, allocated, errp);
2694 if (ret < 0) {
2695 return ret;
2698 (*new_reftable_index)++;
2699 new_refblock_index = 0;
2700 new_refblock_empty = true;
2703 if (new_set_refcount) {
2704 new_set_refcount(new_refblock, new_refblock_index++, 0);
2705 } else {
2706 new_refblock_index++;
2712 if (new_refblock_index > 0) {
2713 /* Complete the potentially existing partially filled final refblock */
2714 if (new_set_refcount) {
2715 for (; new_refblock_index < new_refblock_size;
2716 new_refblock_index++)
2718 new_set_refcount(new_refblock, new_refblock_index, 0);
2722 ret = operation(bs, new_reftable, *new_reftable_index,
2723 new_reftable_size, new_refblock, new_refblock_empty,
2724 allocated, errp);
2725 if (ret < 0) {
2726 return ret;
2729 (*new_reftable_index)++;
2732 status_cb(bs, (uint64_t)(index + 1) * s->refcount_table_size,
2733 (uint64_t)total * s->refcount_table_size, cb_opaque);
2735 return 0;
2738 int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,
2739 BlockDriverAmendStatusCB *status_cb,
2740 void *cb_opaque, Error **errp)
2742 BDRVQcow2State *s = bs->opaque;
2743 Qcow2GetRefcountFunc *new_get_refcount;
2744 Qcow2SetRefcountFunc *new_set_refcount;
2745 void *new_refblock = qemu_blockalign(bs->file->bs, s->cluster_size);
2746 uint64_t *new_reftable = NULL, new_reftable_size = 0;
2747 uint64_t *old_reftable, old_reftable_size, old_reftable_offset;
2748 uint64_t new_reftable_index = 0;
2749 uint64_t i;
2750 int64_t new_reftable_offset = 0, allocated_reftable_size = 0;
2751 int new_refblock_size, new_refcount_bits = 1 << refcount_order;
2752 int old_refcount_order;
2753 int walk_index = 0;
2754 int ret;
2755 bool new_allocation;
2757 assert(s->qcow_version >= 3);
2758 assert(refcount_order >= 0 && refcount_order <= 6);
2760 /* see qcow2_open() */
2761 new_refblock_size = 1 << (s->cluster_bits - (refcount_order - 3));
2763 new_get_refcount = get_refcount_funcs[refcount_order];
2764 new_set_refcount = set_refcount_funcs[refcount_order];
2767 do {
2768 int total_walks;
2770 new_allocation = false;
2772 /* At least we have to do this walk and the one which writes the
2773 * refblocks; also, at least we have to do this loop here at least
2774 * twice (normally), first to do the allocations, and second to
2775 * determine that everything is correctly allocated, this then makes
2776 * three walks in total */
2777 total_walks = MAX(walk_index + 2, 3);
2779 /* First, allocate the structures so they are present in the refcount
2780 * structures */
2781 ret = walk_over_reftable(bs, &new_reftable, &new_reftable_index,
2782 &new_reftable_size, NULL, new_refblock_size,
2783 new_refcount_bits, &alloc_refblock,
2784 &new_allocation, NULL, status_cb, cb_opaque,
2785 walk_index++, total_walks, errp);
2786 if (ret < 0) {
2787 goto done;
2790 new_reftable_index = 0;
2792 if (new_allocation) {
2793 if (new_reftable_offset) {
2794 qcow2_free_clusters(bs, new_reftable_offset,
2795 allocated_reftable_size * sizeof(uint64_t),
2796 QCOW2_DISCARD_NEVER);
2799 new_reftable_offset = qcow2_alloc_clusters(bs, new_reftable_size *
2800 sizeof(uint64_t));
2801 if (new_reftable_offset < 0) {
2802 error_setg_errno(errp, -new_reftable_offset,
2803 "Failed to allocate the new reftable");
2804 ret = new_reftable_offset;
2805 goto done;
2807 allocated_reftable_size = new_reftable_size;
2809 } while (new_allocation);
2811 /* Second, write the new refblocks */
2812 ret = walk_over_reftable(bs, &new_reftable, &new_reftable_index,
2813 &new_reftable_size, new_refblock,
2814 new_refblock_size, new_refcount_bits,
2815 &flush_refblock, &new_allocation, new_set_refcount,
2816 status_cb, cb_opaque, walk_index, walk_index + 1,
2817 errp);
2818 if (ret < 0) {
2819 goto done;
2821 assert(!new_allocation);
2824 /* Write the new reftable */
2825 ret = qcow2_pre_write_overlap_check(bs, 0, new_reftable_offset,
2826 new_reftable_size * sizeof(uint64_t));
2827 if (ret < 0) {
2828 error_setg_errno(errp, -ret, "Overlap check failed");
2829 goto done;
2832 for (i = 0; i < new_reftable_size; i++) {
2833 cpu_to_be64s(&new_reftable[i]);
2836 ret = bdrv_pwrite(bs->file->bs, new_reftable_offset, new_reftable,
2837 new_reftable_size * sizeof(uint64_t));
2839 for (i = 0; i < new_reftable_size; i++) {
2840 be64_to_cpus(&new_reftable[i]);
2843 if (ret < 0) {
2844 error_setg_errno(errp, -ret, "Failed to write the new reftable");
2845 goto done;
2849 /* Empty the refcount cache */
2850 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
2851 if (ret < 0) {
2852 error_setg_errno(errp, -ret, "Failed to flush the refblock cache");
2853 goto done;
2856 /* Update the image header to point to the new reftable; this only updates
2857 * the fields which are relevant to qcow2_update_header(); other fields
2858 * such as s->refcount_table or s->refcount_bits stay stale for now
2859 * (because we have to restore everything if qcow2_update_header() fails) */
2860 old_refcount_order = s->refcount_order;
2861 old_reftable_size = s->refcount_table_size;
2862 old_reftable_offset = s->refcount_table_offset;
2864 s->refcount_order = refcount_order;
2865 s->refcount_table_size = new_reftable_size;
2866 s->refcount_table_offset = new_reftable_offset;
2868 ret = qcow2_update_header(bs);
2869 if (ret < 0) {
2870 s->refcount_order = old_refcount_order;
2871 s->refcount_table_size = old_reftable_size;
2872 s->refcount_table_offset = old_reftable_offset;
2873 error_setg_errno(errp, -ret, "Failed to update the qcow2 header");
2874 goto done;
2877 /* Now update the rest of the in-memory information */
2878 old_reftable = s->refcount_table;
2879 s->refcount_table = new_reftable;
2881 s->refcount_bits = 1 << refcount_order;
2882 s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1);
2883 s->refcount_max += s->refcount_max - 1;
2885 s->refcount_block_bits = s->cluster_bits - (refcount_order - 3);
2886 s->refcount_block_size = 1 << s->refcount_block_bits;
2888 s->get_refcount = new_get_refcount;
2889 s->set_refcount = new_set_refcount;
2891 /* For cleaning up all old refblocks and the old reftable below the "done"
2892 * label */
2893 new_reftable = old_reftable;
2894 new_reftable_size = old_reftable_size;
2895 new_reftable_offset = old_reftable_offset;
2897 done:
2898 if (new_reftable) {
2899 /* On success, new_reftable actually points to the old reftable (and
2900 * new_reftable_size is the old reftable's size); but that is just
2901 * fine */
2902 for (i = 0; i < new_reftable_size; i++) {
2903 uint64_t offset = new_reftable[i] & REFT_OFFSET_MASK;
2904 if (offset) {
2905 qcow2_free_clusters(bs, offset, s->cluster_size,
2906 QCOW2_DISCARD_OTHER);
2909 g_free(new_reftable);
2911 if (new_reftable_offset > 0) {
2912 qcow2_free_clusters(bs, new_reftable_offset,
2913 new_reftable_size * sizeof(uint64_t),
2914 QCOW2_DISCARD_OTHER);
2918 qemu_vfree(new_refblock);
2919 return ret;