hw/rdma: PVRDMA commands and data-path ops
[qemu.git] / block / qcow2-refcount.c
blobd46b69d7f348f7492448fb3590ef6c955291b153
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 "qapi/error.h"
27 #include "qemu-common.h"
28 #include "block/block_int.h"
29 #include "block/qcow2.h"
30 #include "qemu/range.h"
31 #include "qemu/bswap.h"
32 #include "qemu/cutils.h"
34 static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size);
35 static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
36 int64_t offset, int64_t length, uint64_t addend,
37 bool decrease, enum qcow2_discard_type type);
39 static uint64_t get_refcount_ro0(const void *refcount_array, uint64_t index);
40 static uint64_t get_refcount_ro1(const void *refcount_array, uint64_t index);
41 static uint64_t get_refcount_ro2(const void *refcount_array, uint64_t index);
42 static uint64_t get_refcount_ro3(const void *refcount_array, uint64_t index);
43 static uint64_t get_refcount_ro4(const void *refcount_array, uint64_t index);
44 static uint64_t get_refcount_ro5(const void *refcount_array, uint64_t index);
45 static uint64_t get_refcount_ro6(const void *refcount_array, uint64_t index);
47 static void set_refcount_ro0(void *refcount_array, uint64_t index,
48 uint64_t value);
49 static void set_refcount_ro1(void *refcount_array, uint64_t index,
50 uint64_t value);
51 static void set_refcount_ro2(void *refcount_array, uint64_t index,
52 uint64_t value);
53 static void set_refcount_ro3(void *refcount_array, uint64_t index,
54 uint64_t value);
55 static void set_refcount_ro4(void *refcount_array, uint64_t index,
56 uint64_t value);
57 static void set_refcount_ro5(void *refcount_array, uint64_t index,
58 uint64_t value);
59 static void set_refcount_ro6(void *refcount_array, uint64_t index,
60 uint64_t value);
63 static Qcow2GetRefcountFunc *const get_refcount_funcs[] = {
64 &get_refcount_ro0,
65 &get_refcount_ro1,
66 &get_refcount_ro2,
67 &get_refcount_ro3,
68 &get_refcount_ro4,
69 &get_refcount_ro5,
70 &get_refcount_ro6
73 static Qcow2SetRefcountFunc *const set_refcount_funcs[] = {
74 &set_refcount_ro0,
75 &set_refcount_ro1,
76 &set_refcount_ro2,
77 &set_refcount_ro3,
78 &set_refcount_ro4,
79 &set_refcount_ro5,
80 &set_refcount_ro6
84 /*********************************************************/
85 /* refcount handling */
87 static void update_max_refcount_table_index(BDRVQcow2State *s)
89 unsigned i = s->refcount_table_size - 1;
90 while (i > 0 && (s->refcount_table[i] & REFT_OFFSET_MASK) == 0) {
91 i--;
93 /* Set s->max_refcount_table_index to the index of the last used entry */
94 s->max_refcount_table_index = i;
97 int qcow2_refcount_init(BlockDriverState *bs)
99 BDRVQcow2State *s = bs->opaque;
100 unsigned int refcount_table_size2, i;
101 int ret;
103 assert(s->refcount_order >= 0 && s->refcount_order <= 6);
105 s->get_refcount = get_refcount_funcs[s->refcount_order];
106 s->set_refcount = set_refcount_funcs[s->refcount_order];
108 assert(s->refcount_table_size <= INT_MAX / sizeof(uint64_t));
109 refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
110 s->refcount_table = g_try_malloc(refcount_table_size2);
112 if (s->refcount_table_size > 0) {
113 if (s->refcount_table == NULL) {
114 ret = -ENOMEM;
115 goto fail;
117 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_LOAD);
118 ret = bdrv_pread(bs->file, s->refcount_table_offset,
119 s->refcount_table, refcount_table_size2);
120 if (ret < 0) {
121 goto fail;
123 for(i = 0; i < s->refcount_table_size; i++)
124 be64_to_cpus(&s->refcount_table[i]);
125 update_max_refcount_table_index(s);
127 return 0;
128 fail:
129 return ret;
132 void qcow2_refcount_close(BlockDriverState *bs)
134 BDRVQcow2State *s = bs->opaque;
135 g_free(s->refcount_table);
139 static uint64_t get_refcount_ro0(const void *refcount_array, uint64_t index)
141 return (((const uint8_t *)refcount_array)[index / 8] >> (index % 8)) & 0x1;
144 static void set_refcount_ro0(void *refcount_array, uint64_t index,
145 uint64_t value)
147 assert(!(value >> 1));
148 ((uint8_t *)refcount_array)[index / 8] &= ~(0x1 << (index % 8));
149 ((uint8_t *)refcount_array)[index / 8] |= value << (index % 8);
152 static uint64_t get_refcount_ro1(const void *refcount_array, uint64_t index)
154 return (((const uint8_t *)refcount_array)[index / 4] >> (2 * (index % 4)))
155 & 0x3;
158 static void set_refcount_ro1(void *refcount_array, uint64_t index,
159 uint64_t value)
161 assert(!(value >> 2));
162 ((uint8_t *)refcount_array)[index / 4] &= ~(0x3 << (2 * (index % 4)));
163 ((uint8_t *)refcount_array)[index / 4] |= value << (2 * (index % 4));
166 static uint64_t get_refcount_ro2(const void *refcount_array, uint64_t index)
168 return (((const uint8_t *)refcount_array)[index / 2] >> (4 * (index % 2)))
169 & 0xf;
172 static void set_refcount_ro2(void *refcount_array, uint64_t index,
173 uint64_t value)
175 assert(!(value >> 4));
176 ((uint8_t *)refcount_array)[index / 2] &= ~(0xf << (4 * (index % 2)));
177 ((uint8_t *)refcount_array)[index / 2] |= value << (4 * (index % 2));
180 static uint64_t get_refcount_ro3(const void *refcount_array, uint64_t index)
182 return ((const uint8_t *)refcount_array)[index];
185 static void set_refcount_ro3(void *refcount_array, uint64_t index,
186 uint64_t value)
188 assert(!(value >> 8));
189 ((uint8_t *)refcount_array)[index] = value;
192 static uint64_t get_refcount_ro4(const void *refcount_array, uint64_t index)
194 return be16_to_cpu(((const uint16_t *)refcount_array)[index]);
197 static void set_refcount_ro4(void *refcount_array, uint64_t index,
198 uint64_t value)
200 assert(!(value >> 16));
201 ((uint16_t *)refcount_array)[index] = cpu_to_be16(value);
204 static uint64_t get_refcount_ro5(const void *refcount_array, uint64_t index)
206 return be32_to_cpu(((const uint32_t *)refcount_array)[index]);
209 static void set_refcount_ro5(void *refcount_array, uint64_t index,
210 uint64_t value)
212 assert(!(value >> 32));
213 ((uint32_t *)refcount_array)[index] = cpu_to_be32(value);
216 static uint64_t get_refcount_ro6(const void *refcount_array, uint64_t index)
218 return be64_to_cpu(((const uint64_t *)refcount_array)[index]);
221 static void set_refcount_ro6(void *refcount_array, uint64_t index,
222 uint64_t value)
224 ((uint64_t *)refcount_array)[index] = cpu_to_be64(value);
228 static int load_refcount_block(BlockDriverState *bs,
229 int64_t refcount_block_offset,
230 void **refcount_block)
232 BDRVQcow2State *s = bs->opaque;
234 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_LOAD);
235 return qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset,
236 refcount_block);
240 * Retrieves the refcount of the cluster given by its index and stores it in
241 * *refcount. Returns 0 on success and -errno on failure.
243 int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
244 uint64_t *refcount)
246 BDRVQcow2State *s = bs->opaque;
247 uint64_t refcount_table_index, block_index;
248 int64_t refcount_block_offset;
249 int ret;
250 void *refcount_block;
252 refcount_table_index = cluster_index >> s->refcount_block_bits;
253 if (refcount_table_index >= s->refcount_table_size) {
254 *refcount = 0;
255 return 0;
257 refcount_block_offset =
258 s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK;
259 if (!refcount_block_offset) {
260 *refcount = 0;
261 return 0;
264 if (offset_into_cluster(s, refcount_block_offset)) {
265 qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#" PRIx64
266 " unaligned (reftable index: %#" PRIx64 ")",
267 refcount_block_offset, refcount_table_index);
268 return -EIO;
271 ret = qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset,
272 &refcount_block);
273 if (ret < 0) {
274 return ret;
277 block_index = cluster_index & (s->refcount_block_size - 1);
278 *refcount = s->get_refcount(refcount_block, block_index);
280 qcow2_cache_put(s->refcount_block_cache, &refcount_block);
282 return 0;
285 /* Checks if two offsets are described by the same refcount block */
286 static int in_same_refcount_block(BDRVQcow2State *s, uint64_t offset_a,
287 uint64_t offset_b)
289 uint64_t block_a = offset_a >> (s->cluster_bits + s->refcount_block_bits);
290 uint64_t block_b = offset_b >> (s->cluster_bits + s->refcount_block_bits);
292 return (block_a == block_b);
296 * Loads a refcount block. If it doesn't exist yet, it is allocated first
297 * (including growing the refcount table if needed).
299 * Returns 0 on success or -errno in error case
301 static int alloc_refcount_block(BlockDriverState *bs,
302 int64_t cluster_index, void **refcount_block)
304 BDRVQcow2State *s = bs->opaque;
305 unsigned int refcount_table_index;
306 int64_t ret;
308 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
310 /* Find the refcount block for the given cluster */
311 refcount_table_index = cluster_index >> s->refcount_block_bits;
313 if (refcount_table_index < s->refcount_table_size) {
315 uint64_t refcount_block_offset =
316 s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK;
318 /* If it's already there, we're done */
319 if (refcount_block_offset) {
320 if (offset_into_cluster(s, refcount_block_offset)) {
321 qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#"
322 PRIx64 " unaligned (reftable index: "
323 "%#x)", refcount_block_offset,
324 refcount_table_index);
325 return -EIO;
328 return load_refcount_block(bs, refcount_block_offset,
329 refcount_block);
334 * If we came here, we need to allocate something. Something is at least
335 * a cluster for the new refcount block. It may also include a new refcount
336 * table if the old refcount table is too small.
338 * Note that allocating clusters here needs some special care:
340 * - We can't use the normal qcow2_alloc_clusters(), it would try to
341 * increase the refcount and very likely we would end up with an endless
342 * recursion. Instead we must place the refcount blocks in a way that
343 * they can describe them themselves.
345 * - We need to consider that at this point we are inside update_refcounts
346 * and potentially doing an initial refcount increase. This means that
347 * some clusters have already been allocated by the caller, but their
348 * refcount isn't accurate yet. If we allocate clusters for metadata, we
349 * need to return -EAGAIN to signal the caller that it needs to restart
350 * the search for free clusters.
352 * - alloc_clusters_noref and qcow2_free_clusters may load a different
353 * refcount block into the cache
356 *refcount_block = NULL;
358 /* We write to the refcount table, so we might depend on L2 tables */
359 ret = qcow2_cache_flush(bs, s->l2_table_cache);
360 if (ret < 0) {
361 return ret;
364 /* Allocate the refcount block itself and mark it as used */
365 int64_t new_block = alloc_clusters_noref(bs, s->cluster_size);
366 if (new_block < 0) {
367 return new_block;
370 /* If we're allocating the block at offset 0 then something is wrong */
371 if (new_block == 0) {
372 qcow2_signal_corruption(bs, true, -1, -1, "Preventing invalid "
373 "allocation of refcount block at offset 0");
374 return -EIO;
377 #ifdef DEBUG_ALLOC2
378 fprintf(stderr, "qcow2: Allocate refcount block %d for %" PRIx64
379 " at %" PRIx64 "\n",
380 refcount_table_index, cluster_index << s->cluster_bits, new_block);
381 #endif
383 if (in_same_refcount_block(s, new_block, cluster_index << s->cluster_bits)) {
384 /* Zero the new refcount block before updating it */
385 ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block,
386 refcount_block);
387 if (ret < 0) {
388 goto fail;
391 memset(*refcount_block, 0, s->cluster_size);
393 /* The block describes itself, need to update the cache */
394 int block_index = (new_block >> s->cluster_bits) &
395 (s->refcount_block_size - 1);
396 s->set_refcount(*refcount_block, block_index, 1);
397 } else {
398 /* Described somewhere else. This can recurse at most twice before we
399 * arrive at a block that describes itself. */
400 ret = update_refcount(bs, new_block, s->cluster_size, 1, false,
401 QCOW2_DISCARD_NEVER);
402 if (ret < 0) {
403 goto fail;
406 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
407 if (ret < 0) {
408 goto fail;
411 /* Initialize the new refcount block only after updating its refcount,
412 * update_refcount uses the refcount cache itself */
413 ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block,
414 refcount_block);
415 if (ret < 0) {
416 goto fail;
419 memset(*refcount_block, 0, s->cluster_size);
422 /* Now the new refcount block needs to be written to disk */
423 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE);
424 qcow2_cache_entry_mark_dirty(s->refcount_block_cache, *refcount_block);
425 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
426 if (ret < 0) {
427 goto fail;
430 /* If the refcount table is big enough, just hook the block up there */
431 if (refcount_table_index < s->refcount_table_size) {
432 uint64_t data64 = cpu_to_be64(new_block);
433 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_HOOKUP);
434 ret = bdrv_pwrite_sync(bs->file,
435 s->refcount_table_offset + refcount_table_index * sizeof(uint64_t),
436 &data64, sizeof(data64));
437 if (ret < 0) {
438 goto fail;
441 s->refcount_table[refcount_table_index] = new_block;
442 /* If there's a hole in s->refcount_table then it can happen
443 * that refcount_table_index < s->max_refcount_table_index */
444 s->max_refcount_table_index =
445 MAX(s->max_refcount_table_index, refcount_table_index);
447 /* The new refcount block may be where the caller intended to put its
448 * data, so let it restart the search. */
449 return -EAGAIN;
452 qcow2_cache_put(s->refcount_block_cache, refcount_block);
455 * If we come here, we need to grow the refcount table. Again, a new
456 * refcount table needs some space and we can't simply allocate to avoid
457 * endless recursion.
459 * Therefore let's grab new refcount blocks at the end of the image, which
460 * will describe themselves and the new refcount table. This way we can
461 * reference them only in the new table and do the switch to the new
462 * refcount table at once without producing an inconsistent state in
463 * between.
465 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW);
467 /* Calculate the number of refcount blocks needed so far; this will be the
468 * basis for calculating the index of the first cluster used for the
469 * self-describing refcount structures which we are about to create.
471 * Because we reached this point, there cannot be any refcount entries for
472 * cluster_index or higher indices yet. However, because new_block has been
473 * allocated to describe that cluster (and it will assume this role later
474 * on), we cannot use that index; also, new_block may actually have a higher
475 * cluster index than cluster_index, so it needs to be taken into account
476 * here (and 1 needs to be added to its value because that cluster is used).
478 uint64_t blocks_used = DIV_ROUND_UP(MAX(cluster_index + 1,
479 (new_block >> s->cluster_bits) + 1),
480 s->refcount_block_size);
482 /* Create the new refcount table and blocks */
483 uint64_t meta_offset = (blocks_used * s->refcount_block_size) *
484 s->cluster_size;
486 ret = qcow2_refcount_area(bs, meta_offset, 0, false,
487 refcount_table_index, new_block);
488 if (ret < 0) {
489 return ret;
492 ret = load_refcount_block(bs, new_block, refcount_block);
493 if (ret < 0) {
494 return ret;
497 /* If we were trying to do the initial refcount update for some cluster
498 * allocation, we might have used the same clusters to store newly
499 * allocated metadata. Make the caller search some new space. */
500 return -EAGAIN;
502 fail:
503 if (*refcount_block != NULL) {
504 qcow2_cache_put(s->refcount_block_cache, refcount_block);
506 return ret;
510 * Starting at @start_offset, this function creates new self-covering refcount
511 * structures: A new refcount table and refcount blocks which cover all of
512 * themselves, and a number of @additional_clusters beyond their end.
513 * @start_offset must be at the end of the image file, that is, there must be
514 * only empty space beyond it.
515 * If @exact_size is false, the refcount table will have 50 % more entries than
516 * necessary so it will not need to grow again soon.
517 * If @new_refblock_offset is not zero, it contains the offset of a refcount
518 * block that should be entered into the new refcount table at index
519 * @new_refblock_index.
521 * Returns: The offset after the new refcount structures (i.e. where the
522 * @additional_clusters may be placed) on success, -errno on error.
524 int64_t qcow2_refcount_area(BlockDriverState *bs, uint64_t start_offset,
525 uint64_t additional_clusters, bool exact_size,
526 int new_refblock_index,
527 uint64_t new_refblock_offset)
529 BDRVQcow2State *s = bs->opaque;
530 uint64_t total_refblock_count_u64, additional_refblock_count;
531 int total_refblock_count, table_size, area_reftable_index, table_clusters;
532 int i;
533 uint64_t table_offset, block_offset, end_offset;
534 int ret;
535 uint64_t *new_table;
537 assert(!(start_offset % s->cluster_size));
539 qcow2_refcount_metadata_size(start_offset / s->cluster_size +
540 additional_clusters,
541 s->cluster_size, s->refcount_order,
542 !exact_size, &total_refblock_count_u64);
543 if (total_refblock_count_u64 > QCOW_MAX_REFTABLE_SIZE) {
544 return -EFBIG;
546 total_refblock_count = total_refblock_count_u64;
548 /* Index in the refcount table of the first refcount block to cover the area
549 * of refcount structures we are about to create; we know that
550 * @total_refblock_count can cover @start_offset, so this will definitely
551 * fit into an int. */
552 area_reftable_index = (start_offset / s->cluster_size) /
553 s->refcount_block_size;
555 if (exact_size) {
556 table_size = total_refblock_count;
557 } else {
558 table_size = total_refblock_count +
559 DIV_ROUND_UP(total_refblock_count, 2);
561 /* The qcow2 file can only store the reftable size in number of clusters */
562 table_size = ROUND_UP(table_size, s->cluster_size / sizeof(uint64_t));
563 table_clusters = (table_size * sizeof(uint64_t)) / s->cluster_size;
565 if (table_size > QCOW_MAX_REFTABLE_SIZE) {
566 return -EFBIG;
569 new_table = g_try_new0(uint64_t, table_size);
571 assert(table_size > 0);
572 if (new_table == NULL) {
573 ret = -ENOMEM;
574 goto fail;
577 /* Fill the new refcount table */
578 if (table_size > s->max_refcount_table_index) {
579 /* We're actually growing the reftable */
580 memcpy(new_table, s->refcount_table,
581 (s->max_refcount_table_index + 1) * sizeof(uint64_t));
582 } else {
583 /* Improbable case: We're shrinking the reftable. However, the caller
584 * has assured us that there is only empty space beyond @start_offset,
585 * so we can simply drop all of the refblocks that won't fit into the
586 * new reftable. */
587 memcpy(new_table, s->refcount_table, table_size * sizeof(uint64_t));
590 if (new_refblock_offset) {
591 assert(new_refblock_index < total_refblock_count);
592 new_table[new_refblock_index] = new_refblock_offset;
595 /* Count how many new refblocks we have to create */
596 additional_refblock_count = 0;
597 for (i = area_reftable_index; i < total_refblock_count; i++) {
598 if (!new_table[i]) {
599 additional_refblock_count++;
603 table_offset = start_offset + additional_refblock_count * s->cluster_size;
604 end_offset = table_offset + table_clusters * s->cluster_size;
606 /* Fill the refcount blocks, and create new ones, if necessary */
607 block_offset = start_offset;
608 for (i = area_reftable_index; i < total_refblock_count; i++) {
609 void *refblock_data;
610 uint64_t first_offset_covered;
612 /* Reuse an existing refblock if possible, create a new one otherwise */
613 if (new_table[i]) {
614 ret = qcow2_cache_get(bs, s->refcount_block_cache, new_table[i],
615 &refblock_data);
616 if (ret < 0) {
617 goto fail;
619 } else {
620 ret = qcow2_cache_get_empty(bs, s->refcount_block_cache,
621 block_offset, &refblock_data);
622 if (ret < 0) {
623 goto fail;
625 memset(refblock_data, 0, s->cluster_size);
626 qcow2_cache_entry_mark_dirty(s->refcount_block_cache,
627 refblock_data);
629 new_table[i] = block_offset;
630 block_offset += s->cluster_size;
633 /* First host offset covered by this refblock */
634 first_offset_covered = (uint64_t)i * s->refcount_block_size *
635 s->cluster_size;
636 if (first_offset_covered < end_offset) {
637 int j, end_index;
639 /* Set the refcount of all of the new refcount structures to 1 */
641 if (first_offset_covered < start_offset) {
642 assert(i == area_reftable_index);
643 j = (start_offset - first_offset_covered) / s->cluster_size;
644 assert(j < s->refcount_block_size);
645 } else {
646 j = 0;
649 end_index = MIN((end_offset - first_offset_covered) /
650 s->cluster_size,
651 s->refcount_block_size);
653 for (; j < end_index; j++) {
654 /* The caller guaranteed us this space would be empty */
655 assert(s->get_refcount(refblock_data, j) == 0);
656 s->set_refcount(refblock_data, j, 1);
659 qcow2_cache_entry_mark_dirty(s->refcount_block_cache,
660 refblock_data);
663 qcow2_cache_put(s->refcount_block_cache, &refblock_data);
666 assert(block_offset == table_offset);
668 /* Write refcount blocks to disk */
669 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS);
670 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
671 if (ret < 0) {
672 goto fail;
675 /* Write refcount table to disk */
676 for (i = 0; i < total_refblock_count; i++) {
677 cpu_to_be64s(&new_table[i]);
680 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE);
681 ret = bdrv_pwrite_sync(bs->file, table_offset, new_table,
682 table_size * sizeof(uint64_t));
683 if (ret < 0) {
684 goto fail;
687 for (i = 0; i < total_refblock_count; i++) {
688 be64_to_cpus(&new_table[i]);
691 /* Hook up the new refcount table in the qcow2 header */
692 struct QEMU_PACKED {
693 uint64_t d64;
694 uint32_t d32;
695 } data;
696 data.d64 = cpu_to_be64(table_offset);
697 data.d32 = cpu_to_be32(table_clusters);
698 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE);
699 ret = bdrv_pwrite_sync(bs->file,
700 offsetof(QCowHeader, refcount_table_offset),
701 &data, sizeof(data));
702 if (ret < 0) {
703 goto fail;
706 /* And switch it in memory */
707 uint64_t old_table_offset = s->refcount_table_offset;
708 uint64_t old_table_size = s->refcount_table_size;
710 g_free(s->refcount_table);
711 s->refcount_table = new_table;
712 s->refcount_table_size = table_size;
713 s->refcount_table_offset = table_offset;
714 update_max_refcount_table_index(s);
716 /* Free old table. */
717 qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t),
718 QCOW2_DISCARD_OTHER);
720 return end_offset;
722 fail:
723 g_free(new_table);
724 return ret;
727 void qcow2_process_discards(BlockDriverState *bs, int ret)
729 BDRVQcow2State *s = bs->opaque;
730 Qcow2DiscardRegion *d, *next;
732 QTAILQ_FOREACH_SAFE(d, &s->discards, next, next) {
733 QTAILQ_REMOVE(&s->discards, d, next);
735 /* Discard is optional, ignore the return value */
736 if (ret >= 0) {
737 bdrv_pdiscard(bs->file->bs, d->offset, d->bytes);
740 g_free(d);
744 static void update_refcount_discard(BlockDriverState *bs,
745 uint64_t offset, uint64_t length)
747 BDRVQcow2State *s = bs->opaque;
748 Qcow2DiscardRegion *d, *p, *next;
750 QTAILQ_FOREACH(d, &s->discards, next) {
751 uint64_t new_start = MIN(offset, d->offset);
752 uint64_t new_end = MAX(offset + length, d->offset + d->bytes);
754 if (new_end - new_start <= length + d->bytes) {
755 /* There can't be any overlap, areas ending up here have no
756 * references any more and therefore shouldn't get freed another
757 * time. */
758 assert(d->bytes + length == new_end - new_start);
759 d->offset = new_start;
760 d->bytes = new_end - new_start;
761 goto found;
765 d = g_malloc(sizeof(*d));
766 *d = (Qcow2DiscardRegion) {
767 .bs = bs,
768 .offset = offset,
769 .bytes = length,
771 QTAILQ_INSERT_TAIL(&s->discards, d, next);
773 found:
774 /* Merge discard requests if they are adjacent now */
775 QTAILQ_FOREACH_SAFE(p, &s->discards, next, next) {
776 if (p == d
777 || p->offset > d->offset + d->bytes
778 || d->offset > p->offset + p->bytes)
780 continue;
783 /* Still no overlap possible */
784 assert(p->offset == d->offset + d->bytes
785 || d->offset == p->offset + p->bytes);
787 QTAILQ_REMOVE(&s->discards, p, next);
788 d->offset = MIN(d->offset, p->offset);
789 d->bytes += p->bytes;
790 g_free(p);
794 /* XXX: cache several refcount block clusters ? */
795 /* @addend is the absolute value of the addend; if @decrease is set, @addend
796 * will be subtracted from the current refcount, otherwise it will be added */
797 static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
798 int64_t offset,
799 int64_t length,
800 uint64_t addend,
801 bool decrease,
802 enum qcow2_discard_type type)
804 BDRVQcow2State *s = bs->opaque;
805 int64_t start, last, cluster_offset;
806 void *refcount_block = NULL;
807 int64_t old_table_index = -1;
808 int ret;
810 #ifdef DEBUG_ALLOC2
811 fprintf(stderr, "update_refcount: offset=%" PRId64 " size=%" PRId64
812 " addend=%s%" PRIu64 "\n", offset, length, decrease ? "-" : "",
813 addend);
814 #endif
815 if (length < 0) {
816 return -EINVAL;
817 } else if (length == 0) {
818 return 0;
821 if (decrease) {
822 qcow2_cache_set_dependency(bs, s->refcount_block_cache,
823 s->l2_table_cache);
826 start = start_of_cluster(s, offset);
827 last = start_of_cluster(s, offset + length - 1);
828 for(cluster_offset = start; cluster_offset <= last;
829 cluster_offset += s->cluster_size)
831 int block_index;
832 uint64_t refcount;
833 int64_t cluster_index = cluster_offset >> s->cluster_bits;
834 int64_t table_index = cluster_index >> s->refcount_block_bits;
836 /* Load the refcount block and allocate it if needed */
837 if (table_index != old_table_index) {
838 if (refcount_block) {
839 qcow2_cache_put(s->refcount_block_cache, &refcount_block);
841 ret = alloc_refcount_block(bs, cluster_index, &refcount_block);
842 if (ret < 0) {
843 goto fail;
846 old_table_index = table_index;
848 qcow2_cache_entry_mark_dirty(s->refcount_block_cache, refcount_block);
850 /* we can update the count and save it */
851 block_index = cluster_index & (s->refcount_block_size - 1);
853 refcount = s->get_refcount(refcount_block, block_index);
854 if (decrease ? (refcount - addend > refcount)
855 : (refcount + addend < refcount ||
856 refcount + addend > s->refcount_max))
858 ret = -EINVAL;
859 goto fail;
861 if (decrease) {
862 refcount -= addend;
863 } else {
864 refcount += addend;
866 if (refcount == 0 && cluster_index < s->free_cluster_index) {
867 s->free_cluster_index = cluster_index;
869 s->set_refcount(refcount_block, block_index, refcount);
871 if (refcount == 0) {
872 void *table;
874 table = qcow2_cache_is_table_offset(s->refcount_block_cache,
875 offset);
876 if (table != NULL) {
877 qcow2_cache_put(s->refcount_block_cache, &refcount_block);
878 qcow2_cache_discard(s->refcount_block_cache, table);
881 table = qcow2_cache_is_table_offset(s->l2_table_cache, offset);
882 if (table != NULL) {
883 qcow2_cache_discard(s->l2_table_cache, table);
886 if (s->discard_passthrough[type]) {
887 update_refcount_discard(bs, cluster_offset, s->cluster_size);
892 ret = 0;
893 fail:
894 if (!s->cache_discards) {
895 qcow2_process_discards(bs, ret);
898 /* Write last changed block to disk */
899 if (refcount_block) {
900 qcow2_cache_put(s->refcount_block_cache, &refcount_block);
904 * Try do undo any updates if an error is returned (This may succeed in
905 * some cases like ENOSPC for allocating a new refcount block)
907 if (ret < 0) {
908 int dummy;
909 dummy = update_refcount(bs, offset, cluster_offset - offset, addend,
910 !decrease, QCOW2_DISCARD_NEVER);
911 (void)dummy;
914 return ret;
918 * Increases or decreases the refcount of a given cluster.
920 * @addend is the absolute value of the addend; if @decrease is set, @addend
921 * will be subtracted from the current refcount, otherwise it will be added.
923 * On success 0 is returned; on failure -errno is returned.
925 int qcow2_update_cluster_refcount(BlockDriverState *bs,
926 int64_t cluster_index,
927 uint64_t addend, bool decrease,
928 enum qcow2_discard_type type)
930 BDRVQcow2State *s = bs->opaque;
931 int ret;
933 ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend,
934 decrease, type);
935 if (ret < 0) {
936 return ret;
939 return 0;
944 /*********************************************************/
945 /* cluster allocation functions */
949 /* return < 0 if error */
950 static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size)
952 BDRVQcow2State *s = bs->opaque;
953 uint64_t i, nb_clusters, refcount;
954 int ret;
956 /* We can't allocate clusters if they may still be queued for discard. */
957 if (s->cache_discards) {
958 qcow2_process_discards(bs, 0);
961 nb_clusters = size_to_clusters(s, size);
962 retry:
963 for(i = 0; i < nb_clusters; i++) {
964 uint64_t next_cluster_index = s->free_cluster_index++;
965 ret = qcow2_get_refcount(bs, next_cluster_index, &refcount);
967 if (ret < 0) {
968 return ret;
969 } else if (refcount != 0) {
970 goto retry;
974 /* Make sure that all offsets in the "allocated" range are representable
975 * in an int64_t */
976 if (s->free_cluster_index > 0 &&
977 s->free_cluster_index - 1 > (INT64_MAX >> s->cluster_bits))
979 return -EFBIG;
982 #ifdef DEBUG_ALLOC2
983 fprintf(stderr, "alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
984 size,
985 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
986 #endif
987 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
990 int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size)
992 int64_t offset;
993 int ret;
995 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC);
996 do {
997 offset = alloc_clusters_noref(bs, size);
998 if (offset < 0) {
999 return offset;
1002 ret = update_refcount(bs, offset, size, 1, false, QCOW2_DISCARD_NEVER);
1003 } while (ret == -EAGAIN);
1005 if (ret < 0) {
1006 return ret;
1009 return offset;
1012 int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
1013 int64_t nb_clusters)
1015 BDRVQcow2State *s = bs->opaque;
1016 uint64_t cluster_index, refcount;
1017 uint64_t i;
1018 int ret;
1020 assert(nb_clusters >= 0);
1021 if (nb_clusters == 0) {
1022 return 0;
1025 do {
1026 /* Check how many clusters there are free */
1027 cluster_index = offset >> s->cluster_bits;
1028 for(i = 0; i < nb_clusters; i++) {
1029 ret = qcow2_get_refcount(bs, cluster_index++, &refcount);
1030 if (ret < 0) {
1031 return ret;
1032 } else if (refcount != 0) {
1033 break;
1037 /* And then allocate them */
1038 ret = update_refcount(bs, offset, i << s->cluster_bits, 1, false,
1039 QCOW2_DISCARD_NEVER);
1040 } while (ret == -EAGAIN);
1042 if (ret < 0) {
1043 return ret;
1046 return i;
1049 /* only used to allocate compressed sectors. We try to allocate
1050 contiguous sectors. size must be <= cluster_size */
1051 int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
1053 BDRVQcow2State *s = bs->opaque;
1054 int64_t offset;
1055 size_t free_in_cluster;
1056 int ret;
1058 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_BYTES);
1059 assert(size > 0 && size <= s->cluster_size);
1060 assert(!s->free_byte_offset || offset_into_cluster(s, s->free_byte_offset));
1062 offset = s->free_byte_offset;
1064 if (offset) {
1065 uint64_t refcount;
1066 ret = qcow2_get_refcount(bs, offset >> s->cluster_bits, &refcount);
1067 if (ret < 0) {
1068 return ret;
1071 if (refcount == s->refcount_max) {
1072 offset = 0;
1076 free_in_cluster = s->cluster_size - offset_into_cluster(s, offset);
1077 do {
1078 if (!offset || free_in_cluster < size) {
1079 int64_t new_cluster = alloc_clusters_noref(bs, s->cluster_size);
1080 if (new_cluster < 0) {
1081 return new_cluster;
1084 if (new_cluster == 0) {
1085 qcow2_signal_corruption(bs, true, -1, -1, "Preventing invalid "
1086 "allocation of compressed cluster "
1087 "at offset 0");
1088 return -EIO;
1091 if (!offset || ROUND_UP(offset, s->cluster_size) != new_cluster) {
1092 offset = new_cluster;
1093 free_in_cluster = s->cluster_size;
1094 } else {
1095 free_in_cluster += s->cluster_size;
1099 assert(offset);
1100 ret = update_refcount(bs, offset, size, 1, false, QCOW2_DISCARD_NEVER);
1101 if (ret < 0) {
1102 offset = 0;
1104 } while (ret == -EAGAIN);
1105 if (ret < 0) {
1106 return ret;
1109 /* The cluster refcount was incremented; refcount blocks must be flushed
1110 * before the caller's L2 table updates. */
1111 qcow2_cache_set_dependency(bs, s->l2_table_cache, s->refcount_block_cache);
1113 s->free_byte_offset = offset + size;
1114 if (!offset_into_cluster(s, s->free_byte_offset)) {
1115 s->free_byte_offset = 0;
1118 return offset;
1121 void qcow2_free_clusters(BlockDriverState *bs,
1122 int64_t offset, int64_t size,
1123 enum qcow2_discard_type type)
1125 int ret;
1127 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE);
1128 ret = update_refcount(bs, offset, size, 1, true, type);
1129 if (ret < 0) {
1130 fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
1131 /* TODO Remember the clusters to free them later and avoid leaking */
1136 * Free a cluster using its L2 entry (handles clusters of all types, e.g.
1137 * normal cluster, compressed cluster, etc.)
1139 void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
1140 int nb_clusters, enum qcow2_discard_type type)
1142 BDRVQcow2State *s = bs->opaque;
1144 switch (qcow2_get_cluster_type(l2_entry)) {
1145 case QCOW2_CLUSTER_COMPRESSED:
1147 int nb_csectors;
1148 nb_csectors = ((l2_entry >> s->csize_shift) &
1149 s->csize_mask) + 1;
1150 qcow2_free_clusters(bs,
1151 (l2_entry & s->cluster_offset_mask) & ~511,
1152 nb_csectors * 512, type);
1154 break;
1155 case QCOW2_CLUSTER_NORMAL:
1156 case QCOW2_CLUSTER_ZERO_ALLOC:
1157 if (offset_into_cluster(s, l2_entry & L2E_OFFSET_MASK)) {
1158 qcow2_signal_corruption(bs, false, -1, -1,
1159 "Cannot free unaligned cluster %#llx",
1160 l2_entry & L2E_OFFSET_MASK);
1161 } else {
1162 qcow2_free_clusters(bs, l2_entry & L2E_OFFSET_MASK,
1163 nb_clusters << s->cluster_bits, type);
1165 break;
1166 case QCOW2_CLUSTER_ZERO_PLAIN:
1167 case QCOW2_CLUSTER_UNALLOCATED:
1168 break;
1169 default:
1170 abort();
1176 /*********************************************************/
1177 /* snapshots and image creation */
1181 /* update the refcounts of snapshots and the copied flag */
1182 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
1183 int64_t l1_table_offset, int l1_size, int addend)
1185 BDRVQcow2State *s = bs->opaque;
1186 uint64_t *l1_table, *l2_slice, l2_offset, entry, l1_size2, refcount;
1187 bool l1_allocated = false;
1188 int64_t old_entry, old_l2_offset;
1189 unsigned slice, slice_size2, n_slices;
1190 int i, j, l1_modified = 0, nb_csectors;
1191 int ret;
1193 assert(addend >= -1 && addend <= 1);
1195 l2_slice = NULL;
1196 l1_table = NULL;
1197 l1_size2 = l1_size * sizeof(uint64_t);
1198 slice_size2 = s->l2_slice_size * sizeof(uint64_t);
1199 n_slices = s->cluster_size / slice_size2;
1201 s->cache_discards = true;
1203 /* WARNING: qcow2_snapshot_goto relies on this function not using the
1204 * l1_table_offset when it is the current s->l1_table_offset! Be careful
1205 * when changing this! */
1206 if (l1_table_offset != s->l1_table_offset) {
1207 l1_table = g_try_malloc0(align_offset(l1_size2, 512));
1208 if (l1_size2 && l1_table == NULL) {
1209 ret = -ENOMEM;
1210 goto fail;
1212 l1_allocated = true;
1214 ret = bdrv_pread(bs->file, l1_table_offset, l1_table, l1_size2);
1215 if (ret < 0) {
1216 goto fail;
1219 for (i = 0; i < l1_size; i++) {
1220 be64_to_cpus(&l1_table[i]);
1222 } else {
1223 assert(l1_size == s->l1_size);
1224 l1_table = s->l1_table;
1225 l1_allocated = false;
1228 for (i = 0; i < l1_size; i++) {
1229 l2_offset = l1_table[i];
1230 if (l2_offset) {
1231 old_l2_offset = l2_offset;
1232 l2_offset &= L1E_OFFSET_MASK;
1234 if (offset_into_cluster(s, l2_offset)) {
1235 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#"
1236 PRIx64 " unaligned (L1 index: %#x)",
1237 l2_offset, i);
1238 ret = -EIO;
1239 goto fail;
1242 for (slice = 0; slice < n_slices; slice++) {
1243 ret = qcow2_cache_get(bs, s->l2_table_cache,
1244 l2_offset + slice * slice_size2,
1245 (void **) &l2_slice);
1246 if (ret < 0) {
1247 goto fail;
1250 for (j = 0; j < s->l2_slice_size; j++) {
1251 uint64_t cluster_index;
1252 uint64_t offset;
1254 entry = be64_to_cpu(l2_slice[j]);
1255 old_entry = entry;
1256 entry &= ~QCOW_OFLAG_COPIED;
1257 offset = entry & L2E_OFFSET_MASK;
1259 switch (qcow2_get_cluster_type(entry)) {
1260 case QCOW2_CLUSTER_COMPRESSED:
1261 nb_csectors = ((entry >> s->csize_shift) &
1262 s->csize_mask) + 1;
1263 if (addend != 0) {
1264 ret = update_refcount(
1265 bs, (entry & s->cluster_offset_mask) & ~511,
1266 nb_csectors * 512, abs(addend), addend < 0,
1267 QCOW2_DISCARD_SNAPSHOT);
1268 if (ret < 0) {
1269 goto fail;
1272 /* compressed clusters are never modified */
1273 refcount = 2;
1274 break;
1276 case QCOW2_CLUSTER_NORMAL:
1277 case QCOW2_CLUSTER_ZERO_ALLOC:
1278 if (offset_into_cluster(s, offset)) {
1279 /* Here l2_index means table (not slice) index */
1280 int l2_index = slice * s->l2_slice_size + j;
1281 qcow2_signal_corruption(
1282 bs, true, -1, -1, "Cluster "
1283 "allocation offset %#" PRIx64
1284 " unaligned (L2 offset: %#"
1285 PRIx64 ", L2 index: %#x)",
1286 offset, l2_offset, l2_index);
1287 ret = -EIO;
1288 goto fail;
1291 cluster_index = offset >> s->cluster_bits;
1292 assert(cluster_index);
1293 if (addend != 0) {
1294 ret = qcow2_update_cluster_refcount(
1295 bs, cluster_index, abs(addend), addend < 0,
1296 QCOW2_DISCARD_SNAPSHOT);
1297 if (ret < 0) {
1298 goto fail;
1302 ret = qcow2_get_refcount(bs, cluster_index, &refcount);
1303 if (ret < 0) {
1304 goto fail;
1306 break;
1308 case QCOW2_CLUSTER_ZERO_PLAIN:
1309 case QCOW2_CLUSTER_UNALLOCATED:
1310 refcount = 0;
1311 break;
1313 default:
1314 abort();
1317 if (refcount == 1) {
1318 entry |= QCOW_OFLAG_COPIED;
1320 if (entry != old_entry) {
1321 if (addend > 0) {
1322 qcow2_cache_set_dependency(bs, s->l2_table_cache,
1323 s->refcount_block_cache);
1325 l2_slice[j] = cpu_to_be64(entry);
1326 qcow2_cache_entry_mark_dirty(s->l2_table_cache,
1327 l2_slice);
1331 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1334 if (addend != 0) {
1335 ret = qcow2_update_cluster_refcount(bs, l2_offset >>
1336 s->cluster_bits,
1337 abs(addend), addend < 0,
1338 QCOW2_DISCARD_SNAPSHOT);
1339 if (ret < 0) {
1340 goto fail;
1343 ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
1344 &refcount);
1345 if (ret < 0) {
1346 goto fail;
1347 } else if (refcount == 1) {
1348 l2_offset |= QCOW_OFLAG_COPIED;
1350 if (l2_offset != old_l2_offset) {
1351 l1_table[i] = l2_offset;
1352 l1_modified = 1;
1357 ret = bdrv_flush(bs);
1358 fail:
1359 if (l2_slice) {
1360 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
1363 s->cache_discards = false;
1364 qcow2_process_discards(bs, ret);
1366 /* Update L1 only if it isn't deleted anyway (addend = -1) */
1367 if (ret == 0 && addend >= 0 && l1_modified) {
1368 for (i = 0; i < l1_size; i++) {
1369 cpu_to_be64s(&l1_table[i]);
1372 ret = bdrv_pwrite_sync(bs->file, l1_table_offset,
1373 l1_table, l1_size2);
1375 for (i = 0; i < l1_size; i++) {
1376 be64_to_cpus(&l1_table[i]);
1379 if (l1_allocated)
1380 g_free(l1_table);
1381 return ret;
1387 /*********************************************************/
1388 /* refcount checking functions */
1391 static uint64_t refcount_array_byte_size(BDRVQcow2State *s, uint64_t entries)
1393 /* This assertion holds because there is no way we can address more than
1394 * 2^(64 - 9) clusters at once (with cluster size 512 = 2^9, and because
1395 * offsets have to be representable in bytes); due to every cluster
1396 * corresponding to one refcount entry, we are well below that limit */
1397 assert(entries < (UINT64_C(1) << (64 - 9)));
1399 /* Thanks to the assertion this will not overflow, because
1400 * s->refcount_order < 7.
1401 * (note: x << s->refcount_order == x * s->refcount_bits) */
1402 return DIV_ROUND_UP(entries << s->refcount_order, 8);
1406 * Reallocates *array so that it can hold new_size entries. *size must contain
1407 * the current number of entries in *array. If the reallocation fails, *array
1408 * and *size will not be modified and -errno will be returned. If the
1409 * reallocation is successful, *array will be set to the new buffer, *size
1410 * will be set to new_size and 0 will be returned. The size of the reallocated
1411 * refcount array buffer will be aligned to a cluster boundary, and the newly
1412 * allocated area will be zeroed.
1414 static int realloc_refcount_array(BDRVQcow2State *s, void **array,
1415 int64_t *size, int64_t new_size)
1417 int64_t old_byte_size, new_byte_size;
1418 void *new_ptr;
1420 /* Round to clusters so the array can be directly written to disk */
1421 old_byte_size = size_to_clusters(s, refcount_array_byte_size(s, *size))
1422 * s->cluster_size;
1423 new_byte_size = size_to_clusters(s, refcount_array_byte_size(s, new_size))
1424 * s->cluster_size;
1426 if (new_byte_size == old_byte_size) {
1427 *size = new_size;
1428 return 0;
1431 assert(new_byte_size > 0);
1433 if (new_byte_size > SIZE_MAX) {
1434 return -ENOMEM;
1437 new_ptr = g_try_realloc(*array, new_byte_size);
1438 if (!new_ptr) {
1439 return -ENOMEM;
1442 if (new_byte_size > old_byte_size) {
1443 memset((char *)new_ptr + old_byte_size, 0,
1444 new_byte_size - old_byte_size);
1447 *array = new_ptr;
1448 *size = new_size;
1450 return 0;
1454 * Increases the refcount for a range of clusters in a given refcount table.
1455 * This is used to construct a temporary refcount table out of L1 and L2 tables
1456 * which can be compared to the refcount table saved in the image.
1458 * Modifies the number of errors in res.
1460 int qcow2_inc_refcounts_imrt(BlockDriverState *bs, BdrvCheckResult *res,
1461 void **refcount_table,
1462 int64_t *refcount_table_size,
1463 int64_t offset, int64_t size)
1465 BDRVQcow2State *s = bs->opaque;
1466 uint64_t start, last, cluster_offset, k, refcount;
1467 int ret;
1469 if (size <= 0) {
1470 return 0;
1473 start = start_of_cluster(s, offset);
1474 last = start_of_cluster(s, offset + size - 1);
1475 for(cluster_offset = start; cluster_offset <= last;
1476 cluster_offset += s->cluster_size) {
1477 k = cluster_offset >> s->cluster_bits;
1478 if (k >= *refcount_table_size) {
1479 ret = realloc_refcount_array(s, refcount_table,
1480 refcount_table_size, k + 1);
1481 if (ret < 0) {
1482 res->check_errors++;
1483 return ret;
1487 refcount = s->get_refcount(*refcount_table, k);
1488 if (refcount == s->refcount_max) {
1489 fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
1490 "\n", cluster_offset);
1491 fprintf(stderr, "Use qemu-img amend to increase the refcount entry "
1492 "width or qemu-img convert to create a clean copy if the "
1493 "image cannot be opened for writing\n");
1494 res->corruptions++;
1495 continue;
1497 s->set_refcount(*refcount_table, k, refcount + 1);
1500 return 0;
1503 /* Flags for check_refcounts_l1() and check_refcounts_l2() */
1504 enum {
1505 CHECK_FRAG_INFO = 0x2, /* update BlockFragInfo counters */
1509 * Increases the refcount in the given refcount table for the all clusters
1510 * referenced in the L2 table. While doing so, performs some checks on L2
1511 * entries.
1513 * Returns the number of errors found by the checks or -errno if an internal
1514 * error occurred.
1516 static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
1517 void **refcount_table,
1518 int64_t *refcount_table_size, int64_t l2_offset,
1519 int flags, BdrvCheckMode fix)
1521 BDRVQcow2State *s = bs->opaque;
1522 uint64_t *l2_table, l2_entry;
1523 uint64_t next_contiguous_offset = 0;
1524 int i, l2_size, nb_csectors, ret;
1526 /* Read L2 table from disk */
1527 l2_size = s->l2_size * sizeof(uint64_t);
1528 l2_table = g_malloc(l2_size);
1530 ret = bdrv_pread(bs->file, l2_offset, l2_table, l2_size);
1531 if (ret < 0) {
1532 fprintf(stderr, "ERROR: I/O error in check_refcounts_l2\n");
1533 res->check_errors++;
1534 goto fail;
1537 /* Do the actual checks */
1538 for(i = 0; i < s->l2_size; i++) {
1539 l2_entry = be64_to_cpu(l2_table[i]);
1541 switch (qcow2_get_cluster_type(l2_entry)) {
1542 case QCOW2_CLUSTER_COMPRESSED:
1543 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
1544 if (l2_entry & QCOW_OFLAG_COPIED) {
1545 fprintf(stderr, "ERROR: cluster %" PRId64 ": "
1546 "copied flag must never be set for compressed "
1547 "clusters\n", l2_entry >> s->cluster_bits);
1548 l2_entry &= ~QCOW_OFLAG_COPIED;
1549 res->corruptions++;
1552 /* Mark cluster as used */
1553 nb_csectors = ((l2_entry >> s->csize_shift) &
1554 s->csize_mask) + 1;
1555 l2_entry &= s->cluster_offset_mask;
1556 ret = qcow2_inc_refcounts_imrt(bs, res,
1557 refcount_table, refcount_table_size,
1558 l2_entry & ~511, nb_csectors * 512);
1559 if (ret < 0) {
1560 goto fail;
1563 if (flags & CHECK_FRAG_INFO) {
1564 res->bfi.allocated_clusters++;
1565 res->bfi.compressed_clusters++;
1567 /* Compressed clusters are fragmented by nature. Since they
1568 * take up sub-sector space but we only have sector granularity
1569 * I/O we need to re-read the same sectors even for adjacent
1570 * compressed clusters.
1572 res->bfi.fragmented_clusters++;
1574 break;
1576 case QCOW2_CLUSTER_ZERO_ALLOC:
1577 case QCOW2_CLUSTER_NORMAL:
1579 uint64_t offset = l2_entry & L2E_OFFSET_MASK;
1581 if (flags & CHECK_FRAG_INFO) {
1582 res->bfi.allocated_clusters++;
1583 if (next_contiguous_offset &&
1584 offset != next_contiguous_offset) {
1585 res->bfi.fragmented_clusters++;
1587 next_contiguous_offset = offset + s->cluster_size;
1590 /* Correct offsets are cluster aligned */
1591 if (offset_into_cluster(s, offset)) {
1592 if (qcow2_get_cluster_type(l2_entry) ==
1593 QCOW2_CLUSTER_ZERO_ALLOC)
1595 fprintf(stderr, "%s offset=%" PRIx64 ": Preallocated zero "
1596 "cluster is not properly aligned; L2 entry "
1597 "corrupted.\n",
1598 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR",
1599 offset);
1600 if (fix & BDRV_FIX_ERRORS) {
1601 uint64_t l2e_offset =
1602 l2_offset + (uint64_t)i * sizeof(uint64_t);
1604 l2_entry = QCOW_OFLAG_ZERO;
1605 l2_table[i] = cpu_to_be64(l2_entry);
1606 ret = qcow2_pre_write_overlap_check(bs,
1607 QCOW2_OL_ACTIVE_L2 | QCOW2_OL_INACTIVE_L2,
1608 l2e_offset, sizeof(uint64_t));
1609 if (ret < 0) {
1610 fprintf(stderr, "ERROR: Overlap check failed\n");
1611 res->check_errors++;
1612 /* Something is seriously wrong, so abort checking
1613 * this L2 table */
1614 goto fail;
1617 ret = bdrv_pwrite_sync(bs->file, l2e_offset,
1618 &l2_table[i], sizeof(uint64_t));
1619 if (ret < 0) {
1620 fprintf(stderr, "ERROR: Failed to overwrite L2 "
1621 "table entry: %s\n", strerror(-ret));
1622 res->check_errors++;
1623 /* Do not abort, continue checking the rest of this
1624 * L2 table's entries */
1625 } else {
1626 res->corruptions_fixed++;
1627 /* Skip marking the cluster as used
1628 * (it is unused now) */
1629 continue;
1631 } else {
1632 res->corruptions++;
1634 } else {
1635 fprintf(stderr, "ERROR offset=%" PRIx64 ": Data cluster is "
1636 "not properly aligned; L2 entry corrupted.\n", offset);
1637 res->corruptions++;
1641 /* Mark cluster as used */
1642 ret = qcow2_inc_refcounts_imrt(bs, res,
1643 refcount_table, refcount_table_size,
1644 offset, s->cluster_size);
1645 if (ret < 0) {
1646 goto fail;
1648 break;
1651 case QCOW2_CLUSTER_ZERO_PLAIN:
1652 case QCOW2_CLUSTER_UNALLOCATED:
1653 break;
1655 default:
1656 abort();
1660 g_free(l2_table);
1661 return 0;
1663 fail:
1664 g_free(l2_table);
1665 return ret;
1669 * Increases the refcount for the L1 table, its L2 tables and all referenced
1670 * clusters in the given refcount table. While doing so, performs some checks
1671 * on L1 and L2 entries.
1673 * Returns the number of errors found by the checks or -errno if an internal
1674 * error occurred.
1676 static int check_refcounts_l1(BlockDriverState *bs,
1677 BdrvCheckResult *res,
1678 void **refcount_table,
1679 int64_t *refcount_table_size,
1680 int64_t l1_table_offset, int l1_size,
1681 int flags, BdrvCheckMode fix)
1683 BDRVQcow2State *s = bs->opaque;
1684 uint64_t *l1_table = NULL, l2_offset, l1_size2;
1685 int i, ret;
1687 l1_size2 = l1_size * sizeof(uint64_t);
1689 /* Mark L1 table as used */
1690 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_table_size,
1691 l1_table_offset, l1_size2);
1692 if (ret < 0) {
1693 goto fail;
1696 /* Read L1 table entries from disk */
1697 if (l1_size2 > 0) {
1698 l1_table = g_try_malloc(l1_size2);
1699 if (l1_table == NULL) {
1700 ret = -ENOMEM;
1701 res->check_errors++;
1702 goto fail;
1704 ret = bdrv_pread(bs->file, l1_table_offset, l1_table, l1_size2);
1705 if (ret < 0) {
1706 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
1707 res->check_errors++;
1708 goto fail;
1710 for(i = 0;i < l1_size; i++)
1711 be64_to_cpus(&l1_table[i]);
1714 /* Do the actual checks */
1715 for(i = 0; i < l1_size; i++) {
1716 l2_offset = l1_table[i];
1717 if (l2_offset) {
1718 /* Mark L2 table as used */
1719 l2_offset &= L1E_OFFSET_MASK;
1720 ret = qcow2_inc_refcounts_imrt(bs, res,
1721 refcount_table, refcount_table_size,
1722 l2_offset, s->cluster_size);
1723 if (ret < 0) {
1724 goto fail;
1727 /* L2 tables are cluster aligned */
1728 if (offset_into_cluster(s, l2_offset)) {
1729 fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
1730 "cluster aligned; L1 entry corrupted\n", l2_offset);
1731 res->corruptions++;
1734 /* Process and check L2 entries */
1735 ret = check_refcounts_l2(bs, res, refcount_table,
1736 refcount_table_size, l2_offset, flags,
1737 fix);
1738 if (ret < 0) {
1739 goto fail;
1743 g_free(l1_table);
1744 return 0;
1746 fail:
1747 g_free(l1_table);
1748 return ret;
1752 * Checks the OFLAG_COPIED flag for all L1 and L2 entries.
1754 * This function does not print an error message nor does it increment
1755 * check_errors if qcow2_get_refcount fails (this is because such an error will
1756 * have been already detected and sufficiently signaled by the calling function
1757 * (qcow2_check_refcounts) by the time this function is called).
1759 static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res,
1760 BdrvCheckMode fix)
1762 BDRVQcow2State *s = bs->opaque;
1763 uint64_t *l2_table = qemu_blockalign(bs, s->cluster_size);
1764 int ret;
1765 uint64_t refcount;
1766 int i, j;
1768 for (i = 0; i < s->l1_size; i++) {
1769 uint64_t l1_entry = s->l1_table[i];
1770 uint64_t l2_offset = l1_entry & L1E_OFFSET_MASK;
1771 bool l2_dirty = false;
1773 if (!l2_offset) {
1774 continue;
1777 ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
1778 &refcount);
1779 if (ret < 0) {
1780 /* don't print message nor increment check_errors */
1781 continue;
1783 if ((refcount == 1) != ((l1_entry & QCOW_OFLAG_COPIED) != 0)) {
1784 fprintf(stderr, "%s OFLAG_COPIED L2 cluster: l1_index=%d "
1785 "l1_entry=%" PRIx64 " refcount=%" PRIu64 "\n",
1786 fix & BDRV_FIX_ERRORS ? "Repairing" :
1787 "ERROR",
1788 i, l1_entry, refcount);
1789 if (fix & BDRV_FIX_ERRORS) {
1790 s->l1_table[i] = refcount == 1
1791 ? l1_entry | QCOW_OFLAG_COPIED
1792 : l1_entry & ~QCOW_OFLAG_COPIED;
1793 ret = qcow2_write_l1_entry(bs, i);
1794 if (ret < 0) {
1795 res->check_errors++;
1796 goto fail;
1798 res->corruptions_fixed++;
1799 } else {
1800 res->corruptions++;
1804 ret = bdrv_pread(bs->file, l2_offset, l2_table,
1805 s->l2_size * sizeof(uint64_t));
1806 if (ret < 0) {
1807 fprintf(stderr, "ERROR: Could not read L2 table: %s\n",
1808 strerror(-ret));
1809 res->check_errors++;
1810 goto fail;
1813 for (j = 0; j < s->l2_size; j++) {
1814 uint64_t l2_entry = be64_to_cpu(l2_table[j]);
1815 uint64_t data_offset = l2_entry & L2E_OFFSET_MASK;
1816 QCow2ClusterType cluster_type = qcow2_get_cluster_type(l2_entry);
1818 if (cluster_type == QCOW2_CLUSTER_NORMAL ||
1819 cluster_type == QCOW2_CLUSTER_ZERO_ALLOC) {
1820 ret = qcow2_get_refcount(bs,
1821 data_offset >> s->cluster_bits,
1822 &refcount);
1823 if (ret < 0) {
1824 /* don't print message nor increment check_errors */
1825 continue;
1827 if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) {
1828 fprintf(stderr, "%s OFLAG_COPIED data cluster: "
1829 "l2_entry=%" PRIx64 " refcount=%" PRIu64 "\n",
1830 fix & BDRV_FIX_ERRORS ? "Repairing" :
1831 "ERROR",
1832 l2_entry, refcount);
1833 if (fix & BDRV_FIX_ERRORS) {
1834 l2_table[j] = cpu_to_be64(refcount == 1
1835 ? l2_entry | QCOW_OFLAG_COPIED
1836 : l2_entry & ~QCOW_OFLAG_COPIED);
1837 l2_dirty = true;
1838 res->corruptions_fixed++;
1839 } else {
1840 res->corruptions++;
1846 if (l2_dirty) {
1847 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2,
1848 l2_offset, s->cluster_size);
1849 if (ret < 0) {
1850 fprintf(stderr, "ERROR: Could not write L2 table; metadata "
1851 "overlap check failed: %s\n", strerror(-ret));
1852 res->check_errors++;
1853 goto fail;
1856 ret = bdrv_pwrite(bs->file, l2_offset, l2_table,
1857 s->cluster_size);
1858 if (ret < 0) {
1859 fprintf(stderr, "ERROR: Could not write L2 table: %s\n",
1860 strerror(-ret));
1861 res->check_errors++;
1862 goto fail;
1867 ret = 0;
1869 fail:
1870 qemu_vfree(l2_table);
1871 return ret;
1875 * Checks consistency of refblocks and accounts for each refblock in
1876 * *refcount_table.
1878 static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res,
1879 BdrvCheckMode fix, bool *rebuild,
1880 void **refcount_table, int64_t *nb_clusters)
1882 BDRVQcow2State *s = bs->opaque;
1883 int64_t i, size;
1884 int ret;
1886 for(i = 0; i < s->refcount_table_size; i++) {
1887 uint64_t offset, cluster;
1888 offset = s->refcount_table[i];
1889 cluster = offset >> s->cluster_bits;
1891 /* Refcount blocks are cluster aligned */
1892 if (offset_into_cluster(s, offset)) {
1893 fprintf(stderr, "ERROR refcount block %" PRId64 " is not "
1894 "cluster aligned; refcount table entry corrupted\n", i);
1895 res->corruptions++;
1896 *rebuild = true;
1897 continue;
1900 if (cluster >= *nb_clusters) {
1901 fprintf(stderr, "%s refcount block %" PRId64 " is outside image\n",
1902 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
1904 if (fix & BDRV_FIX_ERRORS) {
1905 int64_t new_nb_clusters;
1906 Error *local_err = NULL;
1908 if (offset > INT64_MAX - s->cluster_size) {
1909 ret = -EINVAL;
1910 goto resize_fail;
1913 ret = bdrv_truncate(bs->file, offset + s->cluster_size,
1914 PREALLOC_MODE_OFF, &local_err);
1915 if (ret < 0) {
1916 error_report_err(local_err);
1917 goto resize_fail;
1919 size = bdrv_getlength(bs->file->bs);
1920 if (size < 0) {
1921 ret = size;
1922 goto resize_fail;
1925 new_nb_clusters = size_to_clusters(s, size);
1926 assert(new_nb_clusters >= *nb_clusters);
1928 ret = realloc_refcount_array(s, refcount_table,
1929 nb_clusters, new_nb_clusters);
1930 if (ret < 0) {
1931 res->check_errors++;
1932 return ret;
1935 if (cluster >= *nb_clusters) {
1936 ret = -EINVAL;
1937 goto resize_fail;
1940 res->corruptions_fixed++;
1941 ret = qcow2_inc_refcounts_imrt(bs, res,
1942 refcount_table, nb_clusters,
1943 offset, s->cluster_size);
1944 if (ret < 0) {
1945 return ret;
1947 /* No need to check whether the refcount is now greater than 1:
1948 * This area was just allocated and zeroed, so it can only be
1949 * exactly 1 after qcow2_inc_refcounts_imrt() */
1950 continue;
1952 resize_fail:
1953 res->corruptions++;
1954 *rebuild = true;
1955 fprintf(stderr, "ERROR could not resize image: %s\n",
1956 strerror(-ret));
1957 } else {
1958 res->corruptions++;
1960 continue;
1963 if (offset != 0) {
1964 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters,
1965 offset, s->cluster_size);
1966 if (ret < 0) {
1967 return ret;
1969 if (s->get_refcount(*refcount_table, cluster) != 1) {
1970 fprintf(stderr, "ERROR refcount block %" PRId64
1971 " refcount=%" PRIu64 "\n", i,
1972 s->get_refcount(*refcount_table, cluster));
1973 res->corruptions++;
1974 *rebuild = true;
1979 return 0;
1983 * Calculates an in-memory refcount table.
1985 static int calculate_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
1986 BdrvCheckMode fix, bool *rebuild,
1987 void **refcount_table, int64_t *nb_clusters)
1989 BDRVQcow2State *s = bs->opaque;
1990 int64_t i;
1991 QCowSnapshot *sn;
1992 int ret;
1994 if (!*refcount_table) {
1995 int64_t old_size = 0;
1996 ret = realloc_refcount_array(s, refcount_table,
1997 &old_size, *nb_clusters);
1998 if (ret < 0) {
1999 res->check_errors++;
2000 return ret;
2004 /* header */
2005 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters,
2006 0, s->cluster_size);
2007 if (ret < 0) {
2008 return ret;
2011 /* current L1 table */
2012 ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
2013 s->l1_table_offset, s->l1_size, CHECK_FRAG_INFO,
2014 fix);
2015 if (ret < 0) {
2016 return ret;
2019 /* snapshots */
2020 for (i = 0; i < s->nb_snapshots; i++) {
2021 sn = s->snapshots + i;
2022 ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
2023 sn->l1_table_offset, sn->l1_size, 0, fix);
2024 if (ret < 0) {
2025 return ret;
2028 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters,
2029 s->snapshots_offset, s->snapshots_size);
2030 if (ret < 0) {
2031 return ret;
2034 /* refcount data */
2035 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters,
2036 s->refcount_table_offset,
2037 s->refcount_table_size * sizeof(uint64_t));
2038 if (ret < 0) {
2039 return ret;
2042 /* encryption */
2043 if (s->crypto_header.length) {
2044 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters,
2045 s->crypto_header.offset,
2046 s->crypto_header.length);
2047 if (ret < 0) {
2048 return ret;
2052 /* bitmaps */
2053 ret = qcow2_check_bitmaps_refcounts(bs, res, refcount_table, nb_clusters);
2054 if (ret < 0) {
2055 return ret;
2058 return check_refblocks(bs, res, fix, rebuild, refcount_table, nb_clusters);
2062 * Compares the actual reference count for each cluster in the image against the
2063 * refcount as reported by the refcount structures on-disk.
2065 static void compare_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
2066 BdrvCheckMode fix, bool *rebuild,
2067 int64_t *highest_cluster,
2068 void *refcount_table, int64_t nb_clusters)
2070 BDRVQcow2State *s = bs->opaque;
2071 int64_t i;
2072 uint64_t refcount1, refcount2;
2073 int ret;
2075 for (i = 0, *highest_cluster = 0; i < nb_clusters; i++) {
2076 ret = qcow2_get_refcount(bs, i, &refcount1);
2077 if (ret < 0) {
2078 fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n",
2079 i, strerror(-ret));
2080 res->check_errors++;
2081 continue;
2084 refcount2 = s->get_refcount(refcount_table, i);
2086 if (refcount1 > 0 || refcount2 > 0) {
2087 *highest_cluster = i;
2090 if (refcount1 != refcount2) {
2091 /* Check if we're allowed to fix the mismatch */
2092 int *num_fixed = NULL;
2093 if (refcount1 == 0) {
2094 *rebuild = true;
2095 } else if (refcount1 > refcount2 && (fix & BDRV_FIX_LEAKS)) {
2096 num_fixed = &res->leaks_fixed;
2097 } else if (refcount1 < refcount2 && (fix & BDRV_FIX_ERRORS)) {
2098 num_fixed = &res->corruptions_fixed;
2101 fprintf(stderr, "%s cluster %" PRId64 " refcount=%" PRIu64
2102 " reference=%" PRIu64 "\n",
2103 num_fixed != NULL ? "Repairing" :
2104 refcount1 < refcount2 ? "ERROR" :
2105 "Leaked",
2106 i, refcount1, refcount2);
2108 if (num_fixed) {
2109 ret = update_refcount(bs, i << s->cluster_bits, 1,
2110 refcount_diff(refcount1, refcount2),
2111 refcount1 > refcount2,
2112 QCOW2_DISCARD_ALWAYS);
2113 if (ret >= 0) {
2114 (*num_fixed)++;
2115 continue;
2119 /* And if we couldn't, print an error */
2120 if (refcount1 < refcount2) {
2121 res->corruptions++;
2122 } else {
2123 res->leaks++;
2130 * Allocates clusters using an in-memory refcount table (IMRT) in contrast to
2131 * the on-disk refcount structures.
2133 * On input, *first_free_cluster tells where to start looking, and need not
2134 * actually be a free cluster; the returned offset will not be before that
2135 * cluster. On output, *first_free_cluster points to the first gap found, even
2136 * if that gap was too small to be used as the returned offset.
2138 * Note that *first_free_cluster is a cluster index whereas the return value is
2139 * an offset.
2141 static int64_t alloc_clusters_imrt(BlockDriverState *bs,
2142 int cluster_count,
2143 void **refcount_table,
2144 int64_t *imrt_nb_clusters,
2145 int64_t *first_free_cluster)
2147 BDRVQcow2State *s = bs->opaque;
2148 int64_t cluster = *first_free_cluster, i;
2149 bool first_gap = true;
2150 int contiguous_free_clusters;
2151 int ret;
2153 /* Starting at *first_free_cluster, find a range of at least cluster_count
2154 * continuously free clusters */
2155 for (contiguous_free_clusters = 0;
2156 cluster < *imrt_nb_clusters &&
2157 contiguous_free_clusters < cluster_count;
2158 cluster++)
2160 if (!s->get_refcount(*refcount_table, cluster)) {
2161 contiguous_free_clusters++;
2162 if (first_gap) {
2163 /* If this is the first free cluster found, update
2164 * *first_free_cluster accordingly */
2165 *first_free_cluster = cluster;
2166 first_gap = false;
2168 } else if (contiguous_free_clusters) {
2169 contiguous_free_clusters = 0;
2173 /* If contiguous_free_clusters is greater than zero, it contains the number
2174 * of continuously free clusters until the current cluster; the first free
2175 * cluster in the current "gap" is therefore
2176 * cluster - contiguous_free_clusters */
2178 /* If no such range could be found, grow the in-memory refcount table
2179 * accordingly to append free clusters at the end of the image */
2180 if (contiguous_free_clusters < cluster_count) {
2181 /* contiguous_free_clusters clusters are already empty at the image end;
2182 * we need cluster_count clusters; therefore, we have to allocate
2183 * cluster_count - contiguous_free_clusters new clusters at the end of
2184 * the image (which is the current value of cluster; note that cluster
2185 * may exceed old_imrt_nb_clusters if *first_free_cluster pointed beyond
2186 * the image end) */
2187 ret = realloc_refcount_array(s, refcount_table, imrt_nb_clusters,
2188 cluster + cluster_count
2189 - contiguous_free_clusters);
2190 if (ret < 0) {
2191 return ret;
2195 /* Go back to the first free cluster */
2196 cluster -= contiguous_free_clusters;
2197 for (i = 0; i < cluster_count; i++) {
2198 s->set_refcount(*refcount_table, cluster + i, 1);
2201 return cluster << s->cluster_bits;
2205 * Creates a new refcount structure based solely on the in-memory information
2206 * given through *refcount_table. All necessary allocations will be reflected
2207 * in that array.
2209 * On success, the old refcount structure is leaked (it will be covered by the
2210 * new refcount structure).
2212 static int rebuild_refcount_structure(BlockDriverState *bs,
2213 BdrvCheckResult *res,
2214 void **refcount_table,
2215 int64_t *nb_clusters)
2217 BDRVQcow2State *s = bs->opaque;
2218 int64_t first_free_cluster = 0, reftable_offset = -1, cluster = 0;
2219 int64_t refblock_offset, refblock_start, refblock_index;
2220 uint32_t reftable_size = 0;
2221 uint64_t *on_disk_reftable = NULL;
2222 void *on_disk_refblock;
2223 int ret = 0;
2224 struct {
2225 uint64_t reftable_offset;
2226 uint32_t reftable_clusters;
2227 } QEMU_PACKED reftable_offset_and_clusters;
2229 qcow2_cache_empty(bs, s->refcount_block_cache);
2231 write_refblocks:
2232 for (; cluster < *nb_clusters; cluster++) {
2233 if (!s->get_refcount(*refcount_table, cluster)) {
2234 continue;
2237 refblock_index = cluster >> s->refcount_block_bits;
2238 refblock_start = refblock_index << s->refcount_block_bits;
2240 /* Don't allocate a cluster in a refblock already written to disk */
2241 if (first_free_cluster < refblock_start) {
2242 first_free_cluster = refblock_start;
2244 refblock_offset = alloc_clusters_imrt(bs, 1, refcount_table,
2245 nb_clusters, &first_free_cluster);
2246 if (refblock_offset < 0) {
2247 fprintf(stderr, "ERROR allocating refblock: %s\n",
2248 strerror(-refblock_offset));
2249 res->check_errors++;
2250 ret = refblock_offset;
2251 goto fail;
2254 if (reftable_size <= refblock_index) {
2255 uint32_t old_reftable_size = reftable_size;
2256 uint64_t *new_on_disk_reftable;
2258 reftable_size = ROUND_UP((refblock_index + 1) * sizeof(uint64_t),
2259 s->cluster_size) / sizeof(uint64_t);
2260 new_on_disk_reftable = g_try_realloc(on_disk_reftable,
2261 reftable_size *
2262 sizeof(uint64_t));
2263 if (!new_on_disk_reftable) {
2264 res->check_errors++;
2265 ret = -ENOMEM;
2266 goto fail;
2268 on_disk_reftable = new_on_disk_reftable;
2270 memset(on_disk_reftable + old_reftable_size, 0,
2271 (reftable_size - old_reftable_size) * sizeof(uint64_t));
2273 /* The offset we have for the reftable is now no longer valid;
2274 * this will leak that range, but we can easily fix that by running
2275 * a leak-fixing check after this rebuild operation */
2276 reftable_offset = -1;
2277 } else {
2278 assert(on_disk_reftable);
2280 on_disk_reftable[refblock_index] = refblock_offset;
2282 /* If this is apparently the last refblock (for now), try to squeeze the
2283 * reftable in */
2284 if (refblock_index == (*nb_clusters - 1) >> s->refcount_block_bits &&
2285 reftable_offset < 0)
2287 uint64_t reftable_clusters = size_to_clusters(s, reftable_size *
2288 sizeof(uint64_t));
2289 reftable_offset = alloc_clusters_imrt(bs, reftable_clusters,
2290 refcount_table, nb_clusters,
2291 &first_free_cluster);
2292 if (reftable_offset < 0) {
2293 fprintf(stderr, "ERROR allocating reftable: %s\n",
2294 strerror(-reftable_offset));
2295 res->check_errors++;
2296 ret = reftable_offset;
2297 goto fail;
2301 ret = qcow2_pre_write_overlap_check(bs, 0, refblock_offset,
2302 s->cluster_size);
2303 if (ret < 0) {
2304 fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret));
2305 goto fail;
2308 /* The size of *refcount_table is always cluster-aligned, therefore the
2309 * write operation will not overflow */
2310 on_disk_refblock = (void *)((char *) *refcount_table +
2311 refblock_index * s->cluster_size);
2313 ret = bdrv_write(bs->file, refblock_offset / BDRV_SECTOR_SIZE,
2314 on_disk_refblock, s->cluster_sectors);
2315 if (ret < 0) {
2316 fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret));
2317 goto fail;
2320 /* Go to the end of this refblock */
2321 cluster = refblock_start + s->refcount_block_size - 1;
2324 if (reftable_offset < 0) {
2325 uint64_t post_refblock_start, reftable_clusters;
2327 post_refblock_start = ROUND_UP(*nb_clusters, s->refcount_block_size);
2328 reftable_clusters = size_to_clusters(s,
2329 reftable_size * sizeof(uint64_t));
2330 /* Not pretty but simple */
2331 if (first_free_cluster < post_refblock_start) {
2332 first_free_cluster = post_refblock_start;
2334 reftable_offset = alloc_clusters_imrt(bs, reftable_clusters,
2335 refcount_table, nb_clusters,
2336 &first_free_cluster);
2337 if (reftable_offset < 0) {
2338 fprintf(stderr, "ERROR allocating reftable: %s\n",
2339 strerror(-reftable_offset));
2340 res->check_errors++;
2341 ret = reftable_offset;
2342 goto fail;
2345 goto write_refblocks;
2348 for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) {
2349 cpu_to_be64s(&on_disk_reftable[refblock_index]);
2352 ret = qcow2_pre_write_overlap_check(bs, 0, reftable_offset,
2353 reftable_size * sizeof(uint64_t));
2354 if (ret < 0) {
2355 fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret));
2356 goto fail;
2359 assert(reftable_size < INT_MAX / sizeof(uint64_t));
2360 ret = bdrv_pwrite(bs->file, reftable_offset, on_disk_reftable,
2361 reftable_size * sizeof(uint64_t));
2362 if (ret < 0) {
2363 fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret));
2364 goto fail;
2367 /* Enter new reftable into the image header */
2368 reftable_offset_and_clusters.reftable_offset = cpu_to_be64(reftable_offset);
2369 reftable_offset_and_clusters.reftable_clusters =
2370 cpu_to_be32(size_to_clusters(s, reftable_size * sizeof(uint64_t)));
2371 ret = bdrv_pwrite_sync(bs->file,
2372 offsetof(QCowHeader, refcount_table_offset),
2373 &reftable_offset_and_clusters,
2374 sizeof(reftable_offset_and_clusters));
2375 if (ret < 0) {
2376 fprintf(stderr, "ERROR setting reftable: %s\n", strerror(-ret));
2377 goto fail;
2380 for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) {
2381 be64_to_cpus(&on_disk_reftable[refblock_index]);
2383 s->refcount_table = on_disk_reftable;
2384 s->refcount_table_offset = reftable_offset;
2385 s->refcount_table_size = reftable_size;
2386 update_max_refcount_table_index(s);
2388 return 0;
2390 fail:
2391 g_free(on_disk_reftable);
2392 return ret;
2396 * Checks an image for refcount consistency.
2398 * Returns 0 if no errors are found, the number of errors in case the image is
2399 * detected as corrupted, and -errno when an internal error occurred.
2401 int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
2402 BdrvCheckMode fix)
2404 BDRVQcow2State *s = bs->opaque;
2405 BdrvCheckResult pre_compare_res;
2406 int64_t size, highest_cluster, nb_clusters;
2407 void *refcount_table = NULL;
2408 bool rebuild = false;
2409 int ret;
2411 size = bdrv_getlength(bs->file->bs);
2412 if (size < 0) {
2413 res->check_errors++;
2414 return size;
2417 nb_clusters = size_to_clusters(s, size);
2418 if (nb_clusters > INT_MAX) {
2419 res->check_errors++;
2420 return -EFBIG;
2423 res->bfi.total_clusters =
2424 size_to_clusters(s, bs->total_sectors * BDRV_SECTOR_SIZE);
2426 ret = calculate_refcounts(bs, res, fix, &rebuild, &refcount_table,
2427 &nb_clusters);
2428 if (ret < 0) {
2429 goto fail;
2432 /* In case we don't need to rebuild the refcount structure (but want to fix
2433 * something), this function is immediately called again, in which case the
2434 * result should be ignored */
2435 pre_compare_res = *res;
2436 compare_refcounts(bs, res, 0, &rebuild, &highest_cluster, refcount_table,
2437 nb_clusters);
2439 if (rebuild && (fix & BDRV_FIX_ERRORS)) {
2440 BdrvCheckResult old_res = *res;
2441 int fresh_leaks = 0;
2443 fprintf(stderr, "Rebuilding refcount structure\n");
2444 ret = rebuild_refcount_structure(bs, res, &refcount_table,
2445 &nb_clusters);
2446 if (ret < 0) {
2447 goto fail;
2450 res->corruptions = 0;
2451 res->leaks = 0;
2453 /* Because the old reftable has been exchanged for a new one the
2454 * references have to be recalculated */
2455 rebuild = false;
2456 memset(refcount_table, 0, refcount_array_byte_size(s, nb_clusters));
2457 ret = calculate_refcounts(bs, res, 0, &rebuild, &refcount_table,
2458 &nb_clusters);
2459 if (ret < 0) {
2460 goto fail;
2463 if (fix & BDRV_FIX_LEAKS) {
2464 /* The old refcount structures are now leaked, fix it; the result
2465 * can be ignored, aside from leaks which were introduced by
2466 * rebuild_refcount_structure() that could not be fixed */
2467 BdrvCheckResult saved_res = *res;
2468 *res = (BdrvCheckResult){ 0 };
2470 compare_refcounts(bs, res, BDRV_FIX_LEAKS, &rebuild,
2471 &highest_cluster, refcount_table, nb_clusters);
2472 if (rebuild) {
2473 fprintf(stderr, "ERROR rebuilt refcount structure is still "
2474 "broken\n");
2477 /* Any leaks accounted for here were introduced by
2478 * rebuild_refcount_structure() because that function has created a
2479 * new refcount structure from scratch */
2480 fresh_leaks = res->leaks;
2481 *res = saved_res;
2484 if (res->corruptions < old_res.corruptions) {
2485 res->corruptions_fixed += old_res.corruptions - res->corruptions;
2487 if (res->leaks < old_res.leaks) {
2488 res->leaks_fixed += old_res.leaks - res->leaks;
2490 res->leaks += fresh_leaks;
2491 } else if (fix) {
2492 if (rebuild) {
2493 fprintf(stderr, "ERROR need to rebuild refcount structures\n");
2494 res->check_errors++;
2495 ret = -EIO;
2496 goto fail;
2499 if (res->leaks || res->corruptions) {
2500 *res = pre_compare_res;
2501 compare_refcounts(bs, res, fix, &rebuild, &highest_cluster,
2502 refcount_table, nb_clusters);
2506 /* check OFLAG_COPIED */
2507 ret = check_oflag_copied(bs, res, fix);
2508 if (ret < 0) {
2509 goto fail;
2512 res->image_end_offset = (highest_cluster + 1) * s->cluster_size;
2513 ret = 0;
2515 fail:
2516 g_free(refcount_table);
2518 return ret;
2521 #define overlaps_with(ofs, sz) \
2522 ranges_overlap(offset, size, ofs, sz)
2525 * Checks if the given offset into the image file is actually free to use by
2526 * looking for overlaps with important metadata sections (L1/L2 tables etc.),
2527 * i.e. a sanity check without relying on the refcount tables.
2529 * The ign parameter specifies what checks not to perform (being a bitmask of
2530 * QCow2MetadataOverlap values), i.e., what sections to ignore.
2532 * Returns:
2533 * - 0 if writing to this offset will not affect the mentioned metadata
2534 * - a positive QCow2MetadataOverlap value indicating one overlapping section
2535 * - a negative value (-errno) indicating an error while performing a check,
2536 * e.g. when bdrv_read failed on QCOW2_OL_INACTIVE_L2
2538 int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
2539 int64_t size)
2541 BDRVQcow2State *s = bs->opaque;
2542 int chk = s->overlap_check & ~ign;
2543 int i, j;
2545 if (!size) {
2546 return 0;
2549 if (chk & QCOW2_OL_MAIN_HEADER) {
2550 if (offset < s->cluster_size) {
2551 return QCOW2_OL_MAIN_HEADER;
2555 /* align range to test to cluster boundaries */
2556 size = align_offset(offset_into_cluster(s, offset) + size, s->cluster_size);
2557 offset = start_of_cluster(s, offset);
2559 if ((chk & QCOW2_OL_ACTIVE_L1) && s->l1_size) {
2560 if (overlaps_with(s->l1_table_offset, s->l1_size * sizeof(uint64_t))) {
2561 return QCOW2_OL_ACTIVE_L1;
2565 if ((chk & QCOW2_OL_REFCOUNT_TABLE) && s->refcount_table_size) {
2566 if (overlaps_with(s->refcount_table_offset,
2567 s->refcount_table_size * sizeof(uint64_t))) {
2568 return QCOW2_OL_REFCOUNT_TABLE;
2572 if ((chk & QCOW2_OL_SNAPSHOT_TABLE) && s->snapshots_size) {
2573 if (overlaps_with(s->snapshots_offset, s->snapshots_size)) {
2574 return QCOW2_OL_SNAPSHOT_TABLE;
2578 if ((chk & QCOW2_OL_INACTIVE_L1) && s->snapshots) {
2579 for (i = 0; i < s->nb_snapshots; i++) {
2580 if (s->snapshots[i].l1_size &&
2581 overlaps_with(s->snapshots[i].l1_table_offset,
2582 s->snapshots[i].l1_size * sizeof(uint64_t))) {
2583 return QCOW2_OL_INACTIVE_L1;
2588 if ((chk & QCOW2_OL_ACTIVE_L2) && s->l1_table) {
2589 for (i = 0; i < s->l1_size; i++) {
2590 if ((s->l1_table[i] & L1E_OFFSET_MASK) &&
2591 overlaps_with(s->l1_table[i] & L1E_OFFSET_MASK,
2592 s->cluster_size)) {
2593 return QCOW2_OL_ACTIVE_L2;
2598 if ((chk & QCOW2_OL_REFCOUNT_BLOCK) && s->refcount_table) {
2599 unsigned last_entry = s->max_refcount_table_index;
2600 assert(last_entry < s->refcount_table_size);
2601 assert(last_entry + 1 == s->refcount_table_size ||
2602 (s->refcount_table[last_entry + 1] & REFT_OFFSET_MASK) == 0);
2603 for (i = 0; i <= last_entry; i++) {
2604 if ((s->refcount_table[i] & REFT_OFFSET_MASK) &&
2605 overlaps_with(s->refcount_table[i] & REFT_OFFSET_MASK,
2606 s->cluster_size)) {
2607 return QCOW2_OL_REFCOUNT_BLOCK;
2612 if ((chk & QCOW2_OL_INACTIVE_L2) && s->snapshots) {
2613 for (i = 0; i < s->nb_snapshots; i++) {
2614 uint64_t l1_ofs = s->snapshots[i].l1_table_offset;
2615 uint32_t l1_sz = s->snapshots[i].l1_size;
2616 uint64_t l1_sz2 = l1_sz * sizeof(uint64_t);
2617 uint64_t *l1 = g_try_malloc(l1_sz2);
2618 int ret;
2620 if (l1_sz2 && l1 == NULL) {
2621 return -ENOMEM;
2624 ret = bdrv_pread(bs->file, l1_ofs, l1, l1_sz2);
2625 if (ret < 0) {
2626 g_free(l1);
2627 return ret;
2630 for (j = 0; j < l1_sz; j++) {
2631 uint64_t l2_ofs = be64_to_cpu(l1[j]) & L1E_OFFSET_MASK;
2632 if (l2_ofs && overlaps_with(l2_ofs, s->cluster_size)) {
2633 g_free(l1);
2634 return QCOW2_OL_INACTIVE_L2;
2638 g_free(l1);
2642 return 0;
2645 static const char *metadata_ol_names[] = {
2646 [QCOW2_OL_MAIN_HEADER_BITNR] = "qcow2_header",
2647 [QCOW2_OL_ACTIVE_L1_BITNR] = "active L1 table",
2648 [QCOW2_OL_ACTIVE_L2_BITNR] = "active L2 table",
2649 [QCOW2_OL_REFCOUNT_TABLE_BITNR] = "refcount table",
2650 [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = "refcount block",
2651 [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = "snapshot table",
2652 [QCOW2_OL_INACTIVE_L1_BITNR] = "inactive L1 table",
2653 [QCOW2_OL_INACTIVE_L2_BITNR] = "inactive L2 table",
2657 * First performs a check for metadata overlaps (through
2658 * qcow2_check_metadata_overlap); if that fails with a negative value (error
2659 * while performing a check), that value is returned. If an impending overlap
2660 * is detected, the BDS will be made unusable, the qcow2 file marked corrupt
2661 * and -EIO returned.
2663 * Returns 0 if there were neither overlaps nor errors while checking for
2664 * overlaps; or a negative value (-errno) on error.
2666 int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
2667 int64_t size)
2669 int ret = qcow2_check_metadata_overlap(bs, ign, offset, size);
2671 if (ret < 0) {
2672 return ret;
2673 } else if (ret > 0) {
2674 int metadata_ol_bitnr = ctz32(ret);
2675 assert(metadata_ol_bitnr < QCOW2_OL_MAX_BITNR);
2677 qcow2_signal_corruption(bs, true, offset, size, "Preventing invalid "
2678 "write on metadata (overlaps with %s)",
2679 metadata_ol_names[metadata_ol_bitnr]);
2680 return -EIO;
2683 return 0;
2686 /* A pointer to a function of this type is given to walk_over_reftable(). That
2687 * function will create refblocks and pass them to a RefblockFinishOp once they
2688 * are completed (@refblock). @refblock_empty is set if the refblock is
2689 * completely empty.
2691 * Along with the refblock, a corresponding reftable entry is passed, in the
2692 * reftable @reftable (which may be reallocated) at @reftable_index.
2694 * @allocated should be set to true if a new cluster has been allocated.
2696 typedef int (RefblockFinishOp)(BlockDriverState *bs, uint64_t **reftable,
2697 uint64_t reftable_index, uint64_t *reftable_size,
2698 void *refblock, bool refblock_empty,
2699 bool *allocated, Error **errp);
2702 * This "operation" for walk_over_reftable() allocates the refblock on disk (if
2703 * it is not empty) and inserts its offset into the new reftable. The size of
2704 * this new reftable is increased as required.
2706 static int alloc_refblock(BlockDriverState *bs, uint64_t **reftable,
2707 uint64_t reftable_index, uint64_t *reftable_size,
2708 void *refblock, bool refblock_empty, bool *allocated,
2709 Error **errp)
2711 BDRVQcow2State *s = bs->opaque;
2712 int64_t offset;
2714 if (!refblock_empty && reftable_index >= *reftable_size) {
2715 uint64_t *new_reftable;
2716 uint64_t new_reftable_size;
2718 new_reftable_size = ROUND_UP(reftable_index + 1,
2719 s->cluster_size / sizeof(uint64_t));
2720 if (new_reftable_size > QCOW_MAX_REFTABLE_SIZE / sizeof(uint64_t)) {
2721 error_setg(errp,
2722 "This operation would make the refcount table grow "
2723 "beyond the maximum size supported by QEMU, aborting");
2724 return -ENOTSUP;
2727 new_reftable = g_try_realloc(*reftable, new_reftable_size *
2728 sizeof(uint64_t));
2729 if (!new_reftable) {
2730 error_setg(errp, "Failed to increase reftable buffer size");
2731 return -ENOMEM;
2734 memset(new_reftable + *reftable_size, 0,
2735 (new_reftable_size - *reftable_size) * sizeof(uint64_t));
2737 *reftable = new_reftable;
2738 *reftable_size = new_reftable_size;
2741 if (!refblock_empty && !(*reftable)[reftable_index]) {
2742 offset = qcow2_alloc_clusters(bs, s->cluster_size);
2743 if (offset < 0) {
2744 error_setg_errno(errp, -offset, "Failed to allocate refblock");
2745 return offset;
2747 (*reftable)[reftable_index] = offset;
2748 *allocated = true;
2751 return 0;
2755 * This "operation" for walk_over_reftable() writes the refblock to disk at the
2756 * offset specified by the new reftable's entry. It does not modify the new
2757 * reftable or change any refcounts.
2759 static int flush_refblock(BlockDriverState *bs, uint64_t **reftable,
2760 uint64_t reftable_index, uint64_t *reftable_size,
2761 void *refblock, bool refblock_empty, bool *allocated,
2762 Error **errp)
2764 BDRVQcow2State *s = bs->opaque;
2765 int64_t offset;
2766 int ret;
2768 if (reftable_index < *reftable_size && (*reftable)[reftable_index]) {
2769 offset = (*reftable)[reftable_index];
2771 ret = qcow2_pre_write_overlap_check(bs, 0, offset, s->cluster_size);
2772 if (ret < 0) {
2773 error_setg_errno(errp, -ret, "Overlap check failed");
2774 return ret;
2777 ret = bdrv_pwrite(bs->file, offset, refblock, s->cluster_size);
2778 if (ret < 0) {
2779 error_setg_errno(errp, -ret, "Failed to write refblock");
2780 return ret;
2782 } else {
2783 assert(refblock_empty);
2786 return 0;
2790 * This function walks over the existing reftable and every referenced refblock;
2791 * if @new_set_refcount is non-NULL, it is called for every refcount entry to
2792 * create an equal new entry in the passed @new_refblock. Once that
2793 * @new_refblock is completely filled, @operation will be called.
2795 * @status_cb and @cb_opaque are used for the amend operation's status callback.
2796 * @index is the index of the walk_over_reftable() calls and @total is the total
2797 * number of walk_over_reftable() calls per amend operation. Both are used for
2798 * calculating the parameters for the status callback.
2800 * @allocated is set to true if a new cluster has been allocated.
2802 static int walk_over_reftable(BlockDriverState *bs, uint64_t **new_reftable,
2803 uint64_t *new_reftable_index,
2804 uint64_t *new_reftable_size,
2805 void *new_refblock, int new_refblock_size,
2806 int new_refcount_bits,
2807 RefblockFinishOp *operation, bool *allocated,
2808 Qcow2SetRefcountFunc *new_set_refcount,
2809 BlockDriverAmendStatusCB *status_cb,
2810 void *cb_opaque, int index, int total,
2811 Error **errp)
2813 BDRVQcow2State *s = bs->opaque;
2814 uint64_t reftable_index;
2815 bool new_refblock_empty = true;
2816 int refblock_index;
2817 int new_refblock_index = 0;
2818 int ret;
2820 for (reftable_index = 0; reftable_index < s->refcount_table_size;
2821 reftable_index++)
2823 uint64_t refblock_offset = s->refcount_table[reftable_index]
2824 & REFT_OFFSET_MASK;
2826 status_cb(bs, (uint64_t)index * s->refcount_table_size + reftable_index,
2827 (uint64_t)total * s->refcount_table_size, cb_opaque);
2829 if (refblock_offset) {
2830 void *refblock;
2832 if (offset_into_cluster(s, refblock_offset)) {
2833 qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#"
2834 PRIx64 " unaligned (reftable index: %#"
2835 PRIx64 ")", refblock_offset,
2836 reftable_index);
2837 error_setg(errp,
2838 "Image is corrupt (unaligned refblock offset)");
2839 return -EIO;
2842 ret = qcow2_cache_get(bs, s->refcount_block_cache, refblock_offset,
2843 &refblock);
2844 if (ret < 0) {
2845 error_setg_errno(errp, -ret, "Failed to retrieve refblock");
2846 return ret;
2849 for (refblock_index = 0; refblock_index < s->refcount_block_size;
2850 refblock_index++)
2852 uint64_t refcount;
2854 if (new_refblock_index >= new_refblock_size) {
2855 /* new_refblock is now complete */
2856 ret = operation(bs, new_reftable, *new_reftable_index,
2857 new_reftable_size, new_refblock,
2858 new_refblock_empty, allocated, errp);
2859 if (ret < 0) {
2860 qcow2_cache_put(s->refcount_block_cache, &refblock);
2861 return ret;
2864 (*new_reftable_index)++;
2865 new_refblock_index = 0;
2866 new_refblock_empty = true;
2869 refcount = s->get_refcount(refblock, refblock_index);
2870 if (new_refcount_bits < 64 && refcount >> new_refcount_bits) {
2871 uint64_t offset;
2873 qcow2_cache_put(s->refcount_block_cache, &refblock);
2875 offset = ((reftable_index << s->refcount_block_bits)
2876 + refblock_index) << s->cluster_bits;
2878 error_setg(errp, "Cannot decrease refcount entry width to "
2879 "%i bits: Cluster at offset %#" PRIx64 " has a "
2880 "refcount of %" PRIu64, new_refcount_bits,
2881 offset, refcount);
2882 return -EINVAL;
2885 if (new_set_refcount) {
2886 new_set_refcount(new_refblock, new_refblock_index++,
2887 refcount);
2888 } else {
2889 new_refblock_index++;
2891 new_refblock_empty = new_refblock_empty && refcount == 0;
2894 qcow2_cache_put(s->refcount_block_cache, &refblock);
2895 } else {
2896 /* No refblock means every refcount is 0 */
2897 for (refblock_index = 0; refblock_index < s->refcount_block_size;
2898 refblock_index++)
2900 if (new_refblock_index >= new_refblock_size) {
2901 /* new_refblock is now complete */
2902 ret = operation(bs, new_reftable, *new_reftable_index,
2903 new_reftable_size, new_refblock,
2904 new_refblock_empty, allocated, errp);
2905 if (ret < 0) {
2906 return ret;
2909 (*new_reftable_index)++;
2910 new_refblock_index = 0;
2911 new_refblock_empty = true;
2914 if (new_set_refcount) {
2915 new_set_refcount(new_refblock, new_refblock_index++, 0);
2916 } else {
2917 new_refblock_index++;
2923 if (new_refblock_index > 0) {
2924 /* Complete the potentially existing partially filled final refblock */
2925 if (new_set_refcount) {
2926 for (; new_refblock_index < new_refblock_size;
2927 new_refblock_index++)
2929 new_set_refcount(new_refblock, new_refblock_index, 0);
2933 ret = operation(bs, new_reftable, *new_reftable_index,
2934 new_reftable_size, new_refblock, new_refblock_empty,
2935 allocated, errp);
2936 if (ret < 0) {
2937 return ret;
2940 (*new_reftable_index)++;
2943 status_cb(bs, (uint64_t)(index + 1) * s->refcount_table_size,
2944 (uint64_t)total * s->refcount_table_size, cb_opaque);
2946 return 0;
2949 int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,
2950 BlockDriverAmendStatusCB *status_cb,
2951 void *cb_opaque, Error **errp)
2953 BDRVQcow2State *s = bs->opaque;
2954 Qcow2GetRefcountFunc *new_get_refcount;
2955 Qcow2SetRefcountFunc *new_set_refcount;
2956 void *new_refblock = qemu_blockalign(bs->file->bs, s->cluster_size);
2957 uint64_t *new_reftable = NULL, new_reftable_size = 0;
2958 uint64_t *old_reftable, old_reftable_size, old_reftable_offset;
2959 uint64_t new_reftable_index = 0;
2960 uint64_t i;
2961 int64_t new_reftable_offset = 0, allocated_reftable_size = 0;
2962 int new_refblock_size, new_refcount_bits = 1 << refcount_order;
2963 int old_refcount_order;
2964 int walk_index = 0;
2965 int ret;
2966 bool new_allocation;
2968 assert(s->qcow_version >= 3);
2969 assert(refcount_order >= 0 && refcount_order <= 6);
2971 /* see qcow2_open() */
2972 new_refblock_size = 1 << (s->cluster_bits - (refcount_order - 3));
2974 new_get_refcount = get_refcount_funcs[refcount_order];
2975 new_set_refcount = set_refcount_funcs[refcount_order];
2978 do {
2979 int total_walks;
2981 new_allocation = false;
2983 /* At least we have to do this walk and the one which writes the
2984 * refblocks; also, at least we have to do this loop here at least
2985 * twice (normally), first to do the allocations, and second to
2986 * determine that everything is correctly allocated, this then makes
2987 * three walks in total */
2988 total_walks = MAX(walk_index + 2, 3);
2990 /* First, allocate the structures so they are present in the refcount
2991 * structures */
2992 ret = walk_over_reftable(bs, &new_reftable, &new_reftable_index,
2993 &new_reftable_size, NULL, new_refblock_size,
2994 new_refcount_bits, &alloc_refblock,
2995 &new_allocation, NULL, status_cb, cb_opaque,
2996 walk_index++, total_walks, errp);
2997 if (ret < 0) {
2998 goto done;
3001 new_reftable_index = 0;
3003 if (new_allocation) {
3004 if (new_reftable_offset) {
3005 qcow2_free_clusters(bs, new_reftable_offset,
3006 allocated_reftable_size * sizeof(uint64_t),
3007 QCOW2_DISCARD_NEVER);
3010 new_reftable_offset = qcow2_alloc_clusters(bs, new_reftable_size *
3011 sizeof(uint64_t));
3012 if (new_reftable_offset < 0) {
3013 error_setg_errno(errp, -new_reftable_offset,
3014 "Failed to allocate the new reftable");
3015 ret = new_reftable_offset;
3016 goto done;
3018 allocated_reftable_size = new_reftable_size;
3020 } while (new_allocation);
3022 /* Second, write the new refblocks */
3023 ret = walk_over_reftable(bs, &new_reftable, &new_reftable_index,
3024 &new_reftable_size, new_refblock,
3025 new_refblock_size, new_refcount_bits,
3026 &flush_refblock, &new_allocation, new_set_refcount,
3027 status_cb, cb_opaque, walk_index, walk_index + 1,
3028 errp);
3029 if (ret < 0) {
3030 goto done;
3032 assert(!new_allocation);
3035 /* Write the new reftable */
3036 ret = qcow2_pre_write_overlap_check(bs, 0, new_reftable_offset,
3037 new_reftable_size * sizeof(uint64_t));
3038 if (ret < 0) {
3039 error_setg_errno(errp, -ret, "Overlap check failed");
3040 goto done;
3043 for (i = 0; i < new_reftable_size; i++) {
3044 cpu_to_be64s(&new_reftable[i]);
3047 ret = bdrv_pwrite(bs->file, new_reftable_offset, new_reftable,
3048 new_reftable_size * sizeof(uint64_t));
3050 for (i = 0; i < new_reftable_size; i++) {
3051 be64_to_cpus(&new_reftable[i]);
3054 if (ret < 0) {
3055 error_setg_errno(errp, -ret, "Failed to write the new reftable");
3056 goto done;
3060 /* Empty the refcount cache */
3061 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
3062 if (ret < 0) {
3063 error_setg_errno(errp, -ret, "Failed to flush the refblock cache");
3064 goto done;
3067 /* Update the image header to point to the new reftable; this only updates
3068 * the fields which are relevant to qcow2_update_header(); other fields
3069 * such as s->refcount_table or s->refcount_bits stay stale for now
3070 * (because we have to restore everything if qcow2_update_header() fails) */
3071 old_refcount_order = s->refcount_order;
3072 old_reftable_size = s->refcount_table_size;
3073 old_reftable_offset = s->refcount_table_offset;
3075 s->refcount_order = refcount_order;
3076 s->refcount_table_size = new_reftable_size;
3077 s->refcount_table_offset = new_reftable_offset;
3079 ret = qcow2_update_header(bs);
3080 if (ret < 0) {
3081 s->refcount_order = old_refcount_order;
3082 s->refcount_table_size = old_reftable_size;
3083 s->refcount_table_offset = old_reftable_offset;
3084 error_setg_errno(errp, -ret, "Failed to update the qcow2 header");
3085 goto done;
3088 /* Now update the rest of the in-memory information */
3089 old_reftable = s->refcount_table;
3090 s->refcount_table = new_reftable;
3091 update_max_refcount_table_index(s);
3093 s->refcount_bits = 1 << refcount_order;
3094 s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1);
3095 s->refcount_max += s->refcount_max - 1;
3097 s->refcount_block_bits = s->cluster_bits - (refcount_order - 3);
3098 s->refcount_block_size = 1 << s->refcount_block_bits;
3100 s->get_refcount = new_get_refcount;
3101 s->set_refcount = new_set_refcount;
3103 /* For cleaning up all old refblocks and the old reftable below the "done"
3104 * label */
3105 new_reftable = old_reftable;
3106 new_reftable_size = old_reftable_size;
3107 new_reftable_offset = old_reftable_offset;
3109 done:
3110 if (new_reftable) {
3111 /* On success, new_reftable actually points to the old reftable (and
3112 * new_reftable_size is the old reftable's size); but that is just
3113 * fine */
3114 for (i = 0; i < new_reftable_size; i++) {
3115 uint64_t offset = new_reftable[i] & REFT_OFFSET_MASK;
3116 if (offset) {
3117 qcow2_free_clusters(bs, offset, s->cluster_size,
3118 QCOW2_DISCARD_OTHER);
3121 g_free(new_reftable);
3123 if (new_reftable_offset > 0) {
3124 qcow2_free_clusters(bs, new_reftable_offset,
3125 new_reftable_size * sizeof(uint64_t),
3126 QCOW2_DISCARD_OTHER);
3130 qemu_vfree(new_refblock);
3131 return ret;
3134 static int64_t get_refblock_offset(BlockDriverState *bs, uint64_t offset)
3136 BDRVQcow2State *s = bs->opaque;
3137 uint32_t index = offset_to_reftable_index(s, offset);
3138 int64_t covering_refblock_offset = 0;
3140 if (index < s->refcount_table_size) {
3141 covering_refblock_offset = s->refcount_table[index] & REFT_OFFSET_MASK;
3143 if (!covering_refblock_offset) {
3144 qcow2_signal_corruption(bs, true, -1, -1, "Refblock at %#" PRIx64 " is "
3145 "not covered by the refcount structures",
3146 offset);
3147 return -EIO;
3150 return covering_refblock_offset;
3153 static int qcow2_discard_refcount_block(BlockDriverState *bs,
3154 uint64_t discard_block_offs)
3156 BDRVQcow2State *s = bs->opaque;
3157 int64_t refblock_offs;
3158 uint64_t cluster_index = discard_block_offs >> s->cluster_bits;
3159 uint32_t block_index = cluster_index & (s->refcount_block_size - 1);
3160 void *refblock;
3161 int ret;
3163 refblock_offs = get_refblock_offset(bs, discard_block_offs);
3164 if (refblock_offs < 0) {
3165 return refblock_offs;
3168 assert(discard_block_offs != 0);
3170 ret = qcow2_cache_get(bs, s->refcount_block_cache, refblock_offs,
3171 &refblock);
3172 if (ret < 0) {
3173 return ret;
3176 if (s->get_refcount(refblock, block_index) != 1) {
3177 qcow2_signal_corruption(bs, true, -1, -1, "Invalid refcount:"
3178 " refblock offset %#" PRIx64
3179 ", reftable index %u"
3180 ", block offset %#" PRIx64
3181 ", refcount %#" PRIx64,
3182 refblock_offs,
3183 offset_to_reftable_index(s, discard_block_offs),
3184 discard_block_offs,
3185 s->get_refcount(refblock, block_index));
3186 qcow2_cache_put(s->refcount_block_cache, &refblock);
3187 return -EINVAL;
3189 s->set_refcount(refblock, block_index, 0);
3191 qcow2_cache_entry_mark_dirty(s->refcount_block_cache, refblock);
3193 qcow2_cache_put(s->refcount_block_cache, &refblock);
3195 if (cluster_index < s->free_cluster_index) {
3196 s->free_cluster_index = cluster_index;
3199 refblock = qcow2_cache_is_table_offset(s->refcount_block_cache,
3200 discard_block_offs);
3201 if (refblock) {
3202 /* discard refblock from the cache if refblock is cached */
3203 qcow2_cache_discard(s->refcount_block_cache, refblock);
3205 update_refcount_discard(bs, discard_block_offs, s->cluster_size);
3207 return 0;
3210 int qcow2_shrink_reftable(BlockDriverState *bs)
3212 BDRVQcow2State *s = bs->opaque;
3213 uint64_t *reftable_tmp =
3214 g_malloc(s->refcount_table_size * sizeof(uint64_t));
3215 int i, ret;
3217 for (i = 0; i < s->refcount_table_size; i++) {
3218 int64_t refblock_offs = s->refcount_table[i] & REFT_OFFSET_MASK;
3219 void *refblock;
3220 bool unused_block;
3222 if (refblock_offs == 0) {
3223 reftable_tmp[i] = 0;
3224 continue;
3226 ret = qcow2_cache_get(bs, s->refcount_block_cache, refblock_offs,
3227 &refblock);
3228 if (ret < 0) {
3229 goto out;
3232 /* the refblock has own reference */
3233 if (i == offset_to_reftable_index(s, refblock_offs)) {
3234 uint64_t block_index = (refblock_offs >> s->cluster_bits) &
3235 (s->refcount_block_size - 1);
3236 uint64_t refcount = s->get_refcount(refblock, block_index);
3238 s->set_refcount(refblock, block_index, 0);
3240 unused_block = buffer_is_zero(refblock, s->cluster_size);
3242 s->set_refcount(refblock, block_index, refcount);
3243 } else {
3244 unused_block = buffer_is_zero(refblock, s->cluster_size);
3246 qcow2_cache_put(s->refcount_block_cache, &refblock);
3248 reftable_tmp[i] = unused_block ? 0 : cpu_to_be64(s->refcount_table[i]);
3251 ret = bdrv_pwrite_sync(bs->file, s->refcount_table_offset, reftable_tmp,
3252 s->refcount_table_size * sizeof(uint64_t));
3254 * If the write in the reftable failed the image may contain a partially
3255 * overwritten reftable. In this case it would be better to clear the
3256 * reftable in memory to avoid possible image corruption.
3258 for (i = 0; i < s->refcount_table_size; i++) {
3259 if (s->refcount_table[i] && !reftable_tmp[i]) {
3260 if (ret == 0) {
3261 ret = qcow2_discard_refcount_block(bs, s->refcount_table[i] &
3262 REFT_OFFSET_MASK);
3264 s->refcount_table[i] = 0;
3268 if (!s->cache_discards) {
3269 qcow2_process_discards(bs, ret);
3272 out:
3273 g_free(reftable_tmp);
3274 return ret;
3277 int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size)
3279 BDRVQcow2State *s = bs->opaque;
3280 int64_t i;
3282 for (i = size_to_clusters(s, size) - 1; i >= 0; i--) {
3283 uint64_t refcount;
3284 int ret = qcow2_get_refcount(bs, i, &refcount);
3285 if (ret < 0) {
3286 fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n",
3287 i, strerror(-ret));
3288 return ret;
3290 if (refcount > 0) {
3291 return i;
3294 qcow2_signal_corruption(bs, true, -1, -1,
3295 "There are no references in the refcount table.");
3296 return -EIO;