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
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
28 #include "qemu/range.h"
29 #include "qemu/bswap.h"
30 #include "qemu/cutils.h"
33 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
,
49 static void set_refcount_ro1(void *refcount_array
, uint64_t index
,
51 static void set_refcount_ro2(void *refcount_array
, uint64_t index
,
53 static void set_refcount_ro3(void *refcount_array
, uint64_t index
,
55 static void set_refcount_ro4(void *refcount_array
, uint64_t index
,
57 static void set_refcount_ro5(void *refcount_array
, uint64_t index
,
59 static void set_refcount_ro6(void *refcount_array
, uint64_t index
,
63 static Qcow2GetRefcountFunc
*const get_refcount_funcs
[] = {
73 static Qcow2SetRefcountFunc
*const set_refcount_funcs
[] = {
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) {
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
;
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
) {
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
);
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
);
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
,
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)))
158 static void set_refcount_ro1(void *refcount_array
, uint64_t index
,
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)))
172 static void set_refcount_ro2(void *refcount_array
, uint64_t index
,
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
,
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
,
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
,
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
,
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
,
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
,
246 BDRVQcow2State
*s
= bs
->opaque
;
247 uint64_t refcount_table_index
, block_index
;
248 int64_t refcount_block_offset
;
250 void *refcount_block
;
252 refcount_table_index
= cluster_index
>> s
->refcount_block_bits
;
253 if (refcount_table_index
>= s
->refcount_table_size
) {
257 refcount_block_offset
=
258 s
->refcount_table
[refcount_table_index
] & REFT_OFFSET_MASK
;
259 if (!refcount_block_offset
) {
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
);
271 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refcount_block_offset
,
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
);
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
,
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
;
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
);
328 return load_refcount_block(bs
, refcount_block_offset
,
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
);
364 /* Allocate the refcount block itself and mark it as used */
365 int64_t new_block
= alloc_clusters_noref(bs
, s
->cluster_size
, INT64_MAX
);
370 /* The offset must fit in the offset field of the refcount table entry */
371 assert((new_block
& REFT_OFFSET_MASK
) == new_block
);
373 /* If we're allocating the block at offset 0 then something is wrong */
374 if (new_block
== 0) {
375 qcow2_signal_corruption(bs
, true, -1, -1, "Preventing invalid "
376 "allocation of refcount block at offset 0");
381 fprintf(stderr
, "qcow2: Allocate refcount block %d for %" PRIx64
383 refcount_table_index
, cluster_index
<< s
->cluster_bits
, new_block
);
386 if (in_same_refcount_block(s
, new_block
, cluster_index
<< s
->cluster_bits
)) {
387 /* Zero the new refcount block before updating it */
388 ret
= qcow2_cache_get_empty(bs
, s
->refcount_block_cache
, new_block
,
394 memset(*refcount_block
, 0, s
->cluster_size
);
396 /* The block describes itself, need to update the cache */
397 int block_index
= (new_block
>> s
->cluster_bits
) &
398 (s
->refcount_block_size
- 1);
399 s
->set_refcount(*refcount_block
, block_index
, 1);
401 /* Described somewhere else. This can recurse at most twice before we
402 * arrive at a block that describes itself. */
403 ret
= update_refcount(bs
, new_block
, s
->cluster_size
, 1, false,
404 QCOW2_DISCARD_NEVER
);
409 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
414 /* Initialize the new refcount block only after updating its refcount,
415 * update_refcount uses the refcount cache itself */
416 ret
= qcow2_cache_get_empty(bs
, s
->refcount_block_cache
, new_block
,
422 memset(*refcount_block
, 0, s
->cluster_size
);
425 /* Now the new refcount block needs to be written to disk */
426 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE
);
427 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
, *refcount_block
);
428 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
433 /* If the refcount table is big enough, just hook the block up there */
434 if (refcount_table_index
< s
->refcount_table_size
) {
435 uint64_t data64
= cpu_to_be64(new_block
);
436 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_HOOKUP
);
437 ret
= bdrv_pwrite_sync(bs
->file
,
438 s
->refcount_table_offset
+ refcount_table_index
* sizeof(uint64_t),
439 &data64
, sizeof(data64
));
444 s
->refcount_table
[refcount_table_index
] = new_block
;
445 /* If there's a hole in s->refcount_table then it can happen
446 * that refcount_table_index < s->max_refcount_table_index */
447 s
->max_refcount_table_index
=
448 MAX(s
->max_refcount_table_index
, refcount_table_index
);
450 /* The new refcount block may be where the caller intended to put its
451 * data, so let it restart the search. */
455 qcow2_cache_put(s
->refcount_block_cache
, refcount_block
);
458 * If we come here, we need to grow the refcount table. Again, a new
459 * refcount table needs some space and we can't simply allocate to avoid
462 * Therefore let's grab new refcount blocks at the end of the image, which
463 * will describe themselves and the new refcount table. This way we can
464 * reference them only in the new table and do the switch to the new
465 * refcount table at once without producing an inconsistent state in
468 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_GROW
);
470 /* Calculate the number of refcount blocks needed so far; this will be the
471 * basis for calculating the index of the first cluster used for the
472 * self-describing refcount structures which we are about to create.
474 * Because we reached this point, there cannot be any refcount entries for
475 * cluster_index or higher indices yet. However, because new_block has been
476 * allocated to describe that cluster (and it will assume this role later
477 * on), we cannot use that index; also, new_block may actually have a higher
478 * cluster index than cluster_index, so it needs to be taken into account
479 * here (and 1 needs to be added to its value because that cluster is used).
481 uint64_t blocks_used
= DIV_ROUND_UP(MAX(cluster_index
+ 1,
482 (new_block
>> s
->cluster_bits
) + 1),
483 s
->refcount_block_size
);
485 /* Create the new refcount table and blocks */
486 uint64_t meta_offset
= (blocks_used
* s
->refcount_block_size
) *
489 ret
= qcow2_refcount_area(bs
, meta_offset
, 0, false,
490 refcount_table_index
, new_block
);
495 ret
= load_refcount_block(bs
, new_block
, refcount_block
);
500 /* If we were trying to do the initial refcount update for some cluster
501 * allocation, we might have used the same clusters to store newly
502 * allocated metadata. Make the caller search some new space. */
506 if (*refcount_block
!= NULL
) {
507 qcow2_cache_put(s
->refcount_block_cache
, refcount_block
);
513 * Starting at @start_offset, this function creates new self-covering refcount
514 * structures: A new refcount table and refcount blocks which cover all of
515 * themselves, and a number of @additional_clusters beyond their end.
516 * @start_offset must be at the end of the image file, that is, there must be
517 * only empty space beyond it.
518 * If @exact_size is false, the refcount table will have 50 % more entries than
519 * necessary so it will not need to grow again soon.
520 * If @new_refblock_offset is not zero, it contains the offset of a refcount
521 * block that should be entered into the new refcount table at index
522 * @new_refblock_index.
524 * Returns: The offset after the new refcount structures (i.e. where the
525 * @additional_clusters may be placed) on success, -errno on error.
527 int64_t qcow2_refcount_area(BlockDriverState
*bs
, uint64_t start_offset
,
528 uint64_t additional_clusters
, bool exact_size
,
529 int new_refblock_index
,
530 uint64_t new_refblock_offset
)
532 BDRVQcow2State
*s
= bs
->opaque
;
533 uint64_t total_refblock_count_u64
, additional_refblock_count
;
534 int total_refblock_count
, table_size
, area_reftable_index
, table_clusters
;
536 uint64_t table_offset
, block_offset
, end_offset
;
540 assert(!(start_offset
% s
->cluster_size
));
542 qcow2_refcount_metadata_size(start_offset
/ s
->cluster_size
+
544 s
->cluster_size
, s
->refcount_order
,
545 !exact_size
, &total_refblock_count_u64
);
546 if (total_refblock_count_u64
> QCOW_MAX_REFTABLE_SIZE
) {
549 total_refblock_count
= total_refblock_count_u64
;
551 /* Index in the refcount table of the first refcount block to cover the area
552 * of refcount structures we are about to create; we know that
553 * @total_refblock_count can cover @start_offset, so this will definitely
554 * fit into an int. */
555 area_reftable_index
= (start_offset
/ s
->cluster_size
) /
556 s
->refcount_block_size
;
559 table_size
= total_refblock_count
;
561 table_size
= total_refblock_count
+
562 DIV_ROUND_UP(total_refblock_count
, 2);
564 /* The qcow2 file can only store the reftable size in number of clusters */
565 table_size
= ROUND_UP(table_size
, s
->cluster_size
/ sizeof(uint64_t));
566 table_clusters
= (table_size
* sizeof(uint64_t)) / s
->cluster_size
;
568 if (table_size
> QCOW_MAX_REFTABLE_SIZE
) {
572 new_table
= g_try_new0(uint64_t, table_size
);
574 assert(table_size
> 0);
575 if (new_table
== NULL
) {
580 /* Fill the new refcount table */
581 if (table_size
> s
->max_refcount_table_index
) {
582 /* We're actually growing the reftable */
583 memcpy(new_table
, s
->refcount_table
,
584 (s
->max_refcount_table_index
+ 1) * sizeof(uint64_t));
586 /* Improbable case: We're shrinking the reftable. However, the caller
587 * has assured us that there is only empty space beyond @start_offset,
588 * so we can simply drop all of the refblocks that won't fit into the
590 memcpy(new_table
, s
->refcount_table
, table_size
* sizeof(uint64_t));
593 if (new_refblock_offset
) {
594 assert(new_refblock_index
< total_refblock_count
);
595 new_table
[new_refblock_index
] = new_refblock_offset
;
598 /* Count how many new refblocks we have to create */
599 additional_refblock_count
= 0;
600 for (i
= area_reftable_index
; i
< total_refblock_count
; i
++) {
602 additional_refblock_count
++;
606 table_offset
= start_offset
+ additional_refblock_count
* s
->cluster_size
;
607 end_offset
= table_offset
+ table_clusters
* s
->cluster_size
;
609 /* Fill the refcount blocks, and create new ones, if necessary */
610 block_offset
= start_offset
;
611 for (i
= area_reftable_index
; i
< total_refblock_count
; i
++) {
613 uint64_t first_offset_covered
;
615 /* Reuse an existing refblock if possible, create a new one otherwise */
617 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, new_table
[i
],
623 ret
= qcow2_cache_get_empty(bs
, s
->refcount_block_cache
,
624 block_offset
, &refblock_data
);
628 memset(refblock_data
, 0, s
->cluster_size
);
629 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
,
632 new_table
[i
] = block_offset
;
633 block_offset
+= s
->cluster_size
;
636 /* First host offset covered by this refblock */
637 first_offset_covered
= (uint64_t)i
* s
->refcount_block_size
*
639 if (first_offset_covered
< end_offset
) {
642 /* Set the refcount of all of the new refcount structures to 1 */
644 if (first_offset_covered
< start_offset
) {
645 assert(i
== area_reftable_index
);
646 j
= (start_offset
- first_offset_covered
) / s
->cluster_size
;
647 assert(j
< s
->refcount_block_size
);
652 end_index
= MIN((end_offset
- first_offset_covered
) /
654 s
->refcount_block_size
);
656 for (; j
< end_index
; j
++) {
657 /* The caller guaranteed us this space would be empty */
658 assert(s
->get_refcount(refblock_data
, j
) == 0);
659 s
->set_refcount(refblock_data
, j
, 1);
662 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
,
666 qcow2_cache_put(s
->refcount_block_cache
, &refblock_data
);
669 assert(block_offset
== table_offset
);
671 /* Write refcount blocks to disk */
672 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS
);
673 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
678 /* Write refcount table to disk */
679 for (i
= 0; i
< total_refblock_count
; i
++) {
680 cpu_to_be64s(&new_table
[i
]);
683 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE
);
684 ret
= bdrv_pwrite_sync(bs
->file
, table_offset
, new_table
,
685 table_size
* sizeof(uint64_t));
690 for (i
= 0; i
< total_refblock_count
; i
++) {
691 be64_to_cpus(&new_table
[i
]);
694 /* Hook up the new refcount table in the qcow2 header */
699 data
.d64
= cpu_to_be64(table_offset
);
700 data
.d32
= cpu_to_be32(table_clusters
);
701 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE
);
702 ret
= bdrv_pwrite_sync(bs
->file
,
703 offsetof(QCowHeader
, refcount_table_offset
),
704 &data
, sizeof(data
));
709 /* And switch it in memory */
710 uint64_t old_table_offset
= s
->refcount_table_offset
;
711 uint64_t old_table_size
= s
->refcount_table_size
;
713 g_free(s
->refcount_table
);
714 s
->refcount_table
= new_table
;
715 s
->refcount_table_size
= table_size
;
716 s
->refcount_table_offset
= table_offset
;
717 update_max_refcount_table_index(s
);
719 /* Free old table. */
720 qcow2_free_clusters(bs
, old_table_offset
, old_table_size
* sizeof(uint64_t),
721 QCOW2_DISCARD_OTHER
);
730 void qcow2_process_discards(BlockDriverState
*bs
, int ret
)
732 BDRVQcow2State
*s
= bs
->opaque
;
733 Qcow2DiscardRegion
*d
, *next
;
735 QTAILQ_FOREACH_SAFE(d
, &s
->discards
, next
, next
) {
736 QTAILQ_REMOVE(&s
->discards
, d
, next
);
738 /* Discard is optional, ignore the return value */
740 int r2
= bdrv_pdiscard(bs
->file
, d
->offset
, d
->bytes
);
742 trace_qcow2_process_discards_failed_region(d
->offset
, d
->bytes
,
751 static void update_refcount_discard(BlockDriverState
*bs
,
752 uint64_t offset
, uint64_t length
)
754 BDRVQcow2State
*s
= bs
->opaque
;
755 Qcow2DiscardRegion
*d
, *p
, *next
;
757 QTAILQ_FOREACH(d
, &s
->discards
, next
) {
758 uint64_t new_start
= MIN(offset
, d
->offset
);
759 uint64_t new_end
= MAX(offset
+ length
, d
->offset
+ d
->bytes
);
761 if (new_end
- new_start
<= length
+ d
->bytes
) {
762 /* There can't be any overlap, areas ending up here have no
763 * references any more and therefore shouldn't get freed another
765 assert(d
->bytes
+ length
== new_end
- new_start
);
766 d
->offset
= new_start
;
767 d
->bytes
= new_end
- new_start
;
772 d
= g_malloc(sizeof(*d
));
773 *d
= (Qcow2DiscardRegion
) {
778 QTAILQ_INSERT_TAIL(&s
->discards
, d
, next
);
781 /* Merge discard requests if they are adjacent now */
782 QTAILQ_FOREACH_SAFE(p
, &s
->discards
, next
, next
) {
784 || p
->offset
> d
->offset
+ d
->bytes
785 || d
->offset
> p
->offset
+ p
->bytes
)
790 /* Still no overlap possible */
791 assert(p
->offset
== d
->offset
+ d
->bytes
792 || d
->offset
== p
->offset
+ p
->bytes
);
794 QTAILQ_REMOVE(&s
->discards
, p
, next
);
795 d
->offset
= MIN(d
->offset
, p
->offset
);
796 d
->bytes
+= p
->bytes
;
801 /* XXX: cache several refcount block clusters ? */
802 /* @addend is the absolute value of the addend; if @decrease is set, @addend
803 * will be subtracted from the current refcount, otherwise it will be added */
804 static int QEMU_WARN_UNUSED_RESULT
update_refcount(BlockDriverState
*bs
,
809 enum qcow2_discard_type type
)
811 BDRVQcow2State
*s
= bs
->opaque
;
812 int64_t start
, last
, cluster_offset
;
813 void *refcount_block
= NULL
;
814 int64_t old_table_index
= -1;
818 fprintf(stderr
, "update_refcount: offset=%" PRId64
" size=%" PRId64
819 " addend=%s%" PRIu64
"\n", offset
, length
, decrease
? "-" : "",
824 } else if (length
== 0) {
829 qcow2_cache_set_dependency(bs
, s
->refcount_block_cache
,
833 start
= start_of_cluster(s
, offset
);
834 last
= start_of_cluster(s
, offset
+ length
- 1);
835 for(cluster_offset
= start
; cluster_offset
<= last
;
836 cluster_offset
+= s
->cluster_size
)
840 int64_t cluster_index
= cluster_offset
>> s
->cluster_bits
;
841 int64_t table_index
= cluster_index
>> s
->refcount_block_bits
;
843 /* Load the refcount block and allocate it if needed */
844 if (table_index
!= old_table_index
) {
845 if (refcount_block
) {
846 qcow2_cache_put(s
->refcount_block_cache
, &refcount_block
);
848 ret
= alloc_refcount_block(bs
, cluster_index
, &refcount_block
);
849 /* If the caller needs to restart the search for free clusters,
850 * try the same ones first to see if they're still free. */
851 if (ret
== -EAGAIN
) {
852 if (s
->free_cluster_index
> (start
>> s
->cluster_bits
)) {
853 s
->free_cluster_index
= (start
>> s
->cluster_bits
);
860 old_table_index
= table_index
;
862 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
, refcount_block
);
864 /* we can update the count and save it */
865 block_index
= cluster_index
& (s
->refcount_block_size
- 1);
867 refcount
= s
->get_refcount(refcount_block
, block_index
);
868 if (decrease
? (refcount
- addend
> refcount
)
869 : (refcount
+ addend
< refcount
||
870 refcount
+ addend
> s
->refcount_max
))
880 if (refcount
== 0 && cluster_index
< s
->free_cluster_index
) {
881 s
->free_cluster_index
= cluster_index
;
883 s
->set_refcount(refcount_block
, block_index
, refcount
);
888 table
= qcow2_cache_is_table_offset(s
->refcount_block_cache
,
891 qcow2_cache_put(s
->refcount_block_cache
, &refcount_block
);
892 old_table_index
= -1;
893 qcow2_cache_discard(s
->refcount_block_cache
, table
);
896 table
= qcow2_cache_is_table_offset(s
->l2_table_cache
, offset
);
898 qcow2_cache_discard(s
->l2_table_cache
, table
);
901 if (s
->discard_passthrough
[type
]) {
902 update_refcount_discard(bs
, cluster_offset
, s
->cluster_size
);
909 if (!s
->cache_discards
) {
910 qcow2_process_discards(bs
, ret
);
913 /* Write last changed block to disk */
914 if (refcount_block
) {
915 qcow2_cache_put(s
->refcount_block_cache
, &refcount_block
);
919 * Try do undo any updates if an error is returned (This may succeed in
920 * some cases like ENOSPC for allocating a new refcount block)
924 dummy
= update_refcount(bs
, offset
, cluster_offset
- offset
, addend
,
925 !decrease
, QCOW2_DISCARD_NEVER
);
933 * Increases or decreases the refcount of a given cluster.
935 * @addend is the absolute value of the addend; if @decrease is set, @addend
936 * will be subtracted from the current refcount, otherwise it will be added.
938 * On success 0 is returned; on failure -errno is returned.
940 int qcow2_update_cluster_refcount(BlockDriverState
*bs
,
941 int64_t cluster_index
,
942 uint64_t addend
, bool decrease
,
943 enum qcow2_discard_type type
)
945 BDRVQcow2State
*s
= bs
->opaque
;
948 ret
= update_refcount(bs
, cluster_index
<< s
->cluster_bits
, 1, addend
,
959 /*********************************************************/
960 /* cluster allocation functions */
964 /* return < 0 if error */
965 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, uint64_t size
,
968 BDRVQcow2State
*s
= bs
->opaque
;
969 uint64_t i
, nb_clusters
, refcount
;
972 /* We can't allocate clusters if they may still be queued for discard. */
973 if (s
->cache_discards
) {
974 qcow2_process_discards(bs
, 0);
977 nb_clusters
= size_to_clusters(s
, size
);
979 for(i
= 0; i
< nb_clusters
; i
++) {
980 uint64_t next_cluster_index
= s
->free_cluster_index
++;
981 ret
= qcow2_get_refcount(bs
, next_cluster_index
, &refcount
);
985 } else if (refcount
!= 0) {
990 /* Make sure that all offsets in the "allocated" range are representable
991 * in the requested max */
992 if (s
->free_cluster_index
> 0 &&
993 s
->free_cluster_index
- 1 > (max
>> s
->cluster_bits
))
999 fprintf(stderr
, "alloc_clusters: size=%" PRId64
" -> %" PRId64
"\n",
1001 (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
);
1003 return (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
;
1006 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, uint64_t size
)
1011 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC
);
1013 offset
= alloc_clusters_noref(bs
, size
, QCOW_MAX_CLUSTER_OFFSET
);
1018 ret
= update_refcount(bs
, offset
, size
, 1, false, QCOW2_DISCARD_NEVER
);
1019 } while (ret
== -EAGAIN
);
1028 int64_t qcow2_alloc_clusters_at(BlockDriverState
*bs
, uint64_t offset
,
1029 int64_t nb_clusters
)
1031 BDRVQcow2State
*s
= bs
->opaque
;
1032 uint64_t cluster_index
, refcount
;
1036 assert(nb_clusters
>= 0);
1037 if (nb_clusters
== 0) {
1042 /* Check how many clusters there are free */
1043 cluster_index
= offset
>> s
->cluster_bits
;
1044 for(i
= 0; i
< nb_clusters
; i
++) {
1045 ret
= qcow2_get_refcount(bs
, cluster_index
++, &refcount
);
1048 } else if (refcount
!= 0) {
1053 /* And then allocate them */
1054 ret
= update_refcount(bs
, offset
, i
<< s
->cluster_bits
, 1, false,
1055 QCOW2_DISCARD_NEVER
);
1056 } while (ret
== -EAGAIN
);
1065 /* only used to allocate compressed sectors. We try to allocate
1066 contiguous sectors. size must be <= cluster_size */
1067 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
)
1069 BDRVQcow2State
*s
= bs
->opaque
;
1071 size_t free_in_cluster
;
1074 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC_BYTES
);
1075 assert(size
> 0 && size
<= s
->cluster_size
);
1076 assert(!s
->free_byte_offset
|| offset_into_cluster(s
, s
->free_byte_offset
));
1078 offset
= s
->free_byte_offset
;
1082 ret
= qcow2_get_refcount(bs
, offset
>> s
->cluster_bits
, &refcount
);
1087 if (refcount
== s
->refcount_max
) {
1092 free_in_cluster
= s
->cluster_size
- offset_into_cluster(s
, offset
);
1094 if (!offset
|| free_in_cluster
< size
) {
1095 int64_t new_cluster
;
1097 new_cluster
= alloc_clusters_noref(bs
, s
->cluster_size
,
1098 MIN(s
->cluster_offset_mask
,
1099 QCOW_MAX_CLUSTER_OFFSET
));
1100 if (new_cluster
< 0) {
1104 if (new_cluster
== 0) {
1105 qcow2_signal_corruption(bs
, true, -1, -1, "Preventing invalid "
1106 "allocation of compressed cluster "
1111 if (!offset
|| ROUND_UP(offset
, s
->cluster_size
) != new_cluster
) {
1112 offset
= new_cluster
;
1113 free_in_cluster
= s
->cluster_size
;
1115 free_in_cluster
+= s
->cluster_size
;
1120 ret
= update_refcount(bs
, offset
, size
, 1, false, QCOW2_DISCARD_NEVER
);
1124 } while (ret
== -EAGAIN
);
1129 /* The cluster refcount was incremented; refcount blocks must be flushed
1130 * before the caller's L2 table updates. */
1131 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
, s
->refcount_block_cache
);
1133 s
->free_byte_offset
= offset
+ size
;
1134 if (!offset_into_cluster(s
, s
->free_byte_offset
)) {
1135 s
->free_byte_offset
= 0;
1141 void qcow2_free_clusters(BlockDriverState
*bs
,
1142 int64_t offset
, int64_t size
,
1143 enum qcow2_discard_type type
)
1147 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_FREE
);
1148 ret
= update_refcount(bs
, offset
, size
, 1, true, type
);
1150 fprintf(stderr
, "qcow2_free_clusters failed: %s\n", strerror(-ret
));
1151 /* TODO Remember the clusters to free them later and avoid leaking */
1156 * Free a cluster using its L2 entry (handles clusters of all types, e.g.
1157 * normal cluster, compressed cluster, etc.)
1159 void qcow2_free_any_clusters(BlockDriverState
*bs
, uint64_t l2_entry
,
1160 int nb_clusters
, enum qcow2_discard_type type
)
1162 BDRVQcow2State
*s
= bs
->opaque
;
1163 QCow2ClusterType ctype
= qcow2_get_cluster_type(bs
, l2_entry
);
1165 if (has_data_file(bs
)) {
1166 if (s
->discard_passthrough
[type
] &&
1167 (ctype
== QCOW2_CLUSTER_NORMAL
||
1168 ctype
== QCOW2_CLUSTER_ZERO_ALLOC
))
1170 bdrv_pdiscard(s
->data_file
, l2_entry
& L2E_OFFSET_MASK
,
1171 nb_clusters
<< s
->cluster_bits
);
1177 case QCOW2_CLUSTER_COMPRESSED
:
1179 int64_t offset
= (l2_entry
& s
->cluster_offset_mask
)
1180 & QCOW2_COMPRESSED_SECTOR_MASK
;
1181 int size
= QCOW2_COMPRESSED_SECTOR_SIZE
*
1182 (((l2_entry
>> s
->csize_shift
) & s
->csize_mask
) + 1);
1183 qcow2_free_clusters(bs
, offset
, size
, type
);
1186 case QCOW2_CLUSTER_NORMAL
:
1187 case QCOW2_CLUSTER_ZERO_ALLOC
:
1188 if (offset_into_cluster(s
, l2_entry
& L2E_OFFSET_MASK
)) {
1189 qcow2_signal_corruption(bs
, false, -1, -1,
1190 "Cannot free unaligned cluster %#llx",
1191 l2_entry
& L2E_OFFSET_MASK
);
1193 qcow2_free_clusters(bs
, l2_entry
& L2E_OFFSET_MASK
,
1194 nb_clusters
<< s
->cluster_bits
, type
);
1197 case QCOW2_CLUSTER_ZERO_PLAIN
:
1198 case QCOW2_CLUSTER_UNALLOCATED
:
1205 int coroutine_fn
qcow2_write_caches(BlockDriverState
*bs
)
1207 BDRVQcow2State
*s
= bs
->opaque
;
1210 ret
= qcow2_cache_write(bs
, s
->l2_table_cache
);
1215 if (qcow2_need_accurate_refcounts(s
)) {
1216 ret
= qcow2_cache_write(bs
, s
->refcount_block_cache
);
1225 int coroutine_fn
qcow2_flush_caches(BlockDriverState
*bs
)
1227 int ret
= qcow2_write_caches(bs
);
1232 return bdrv_flush(bs
->file
->bs
);
1235 /*********************************************************/
1236 /* snapshots and image creation */
1240 /* update the refcounts of snapshots and the copied flag */
1241 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
1242 int64_t l1_table_offset
, int l1_size
, int addend
)
1244 BDRVQcow2State
*s
= bs
->opaque
;
1245 uint64_t *l1_table
, *l2_slice
, l2_offset
, entry
, l1_size2
, refcount
;
1246 bool l1_allocated
= false;
1247 int64_t old_entry
, old_l2_offset
;
1248 unsigned slice
, slice_size2
, n_slices
;
1249 int i
, j
, l1_modified
= 0, nb_csectors
;
1252 assert(addend
>= -1 && addend
<= 1);
1256 l1_size2
= l1_size
* sizeof(uint64_t);
1257 slice_size2
= s
->l2_slice_size
* l2_entry_size(s
);
1258 n_slices
= s
->cluster_size
/ slice_size2
;
1260 s
->cache_discards
= true;
1262 /* WARNING: qcow2_snapshot_goto relies on this function not using the
1263 * l1_table_offset when it is the current s->l1_table_offset! Be careful
1264 * when changing this! */
1265 if (l1_table_offset
!= s
->l1_table_offset
) {
1266 l1_table
= g_try_malloc0(l1_size2
);
1267 if (l1_size2
&& l1_table
== NULL
) {
1271 l1_allocated
= true;
1273 ret
= bdrv_pread(bs
->file
, l1_table_offset
, l1_table
, l1_size2
);
1278 for (i
= 0; i
< l1_size
; i
++) {
1279 be64_to_cpus(&l1_table
[i
]);
1282 assert(l1_size
== s
->l1_size
);
1283 l1_table
= s
->l1_table
;
1284 l1_allocated
= false;
1287 for (i
= 0; i
< l1_size
; i
++) {
1288 l2_offset
= l1_table
[i
];
1290 old_l2_offset
= l2_offset
;
1291 l2_offset
&= L1E_OFFSET_MASK
;
1293 if (offset_into_cluster(s
, l2_offset
)) {
1294 qcow2_signal_corruption(bs
, true, -1, -1, "L2 table offset %#"
1295 PRIx64
" unaligned (L1 index: %#x)",
1301 for (slice
= 0; slice
< n_slices
; slice
++) {
1302 ret
= qcow2_cache_get(bs
, s
->l2_table_cache
,
1303 l2_offset
+ slice
* slice_size2
,
1304 (void **) &l2_slice
);
1309 for (j
= 0; j
< s
->l2_slice_size
; j
++) {
1310 uint64_t cluster_index
;
1313 entry
= get_l2_entry(s
, l2_slice
, j
);
1315 entry
&= ~QCOW_OFLAG_COPIED
;
1316 offset
= entry
& L2E_OFFSET_MASK
;
1318 switch (qcow2_get_cluster_type(bs
, entry
)) {
1319 case QCOW2_CLUSTER_COMPRESSED
:
1320 nb_csectors
= ((entry
>> s
->csize_shift
) &
1323 uint64_t coffset
= (entry
& s
->cluster_offset_mask
)
1324 & QCOW2_COMPRESSED_SECTOR_MASK
;
1325 ret
= update_refcount(
1327 nb_csectors
* QCOW2_COMPRESSED_SECTOR_SIZE
,
1328 abs(addend
), addend
< 0,
1329 QCOW2_DISCARD_SNAPSHOT
);
1334 /* compressed clusters are never modified */
1338 case QCOW2_CLUSTER_NORMAL
:
1339 case QCOW2_CLUSTER_ZERO_ALLOC
:
1340 if (offset_into_cluster(s
, offset
)) {
1341 /* Here l2_index means table (not slice) index */
1342 int l2_index
= slice
* s
->l2_slice_size
+ j
;
1343 qcow2_signal_corruption(
1344 bs
, true, -1, -1, "Cluster "
1345 "allocation offset %#" PRIx64
1346 " unaligned (L2 offset: %#"
1347 PRIx64
", L2 index: %#x)",
1348 offset
, l2_offset
, l2_index
);
1353 cluster_index
= offset
>> s
->cluster_bits
;
1354 assert(cluster_index
);
1356 ret
= qcow2_update_cluster_refcount(
1357 bs
, cluster_index
, abs(addend
), addend
< 0,
1358 QCOW2_DISCARD_SNAPSHOT
);
1364 ret
= qcow2_get_refcount(bs
, cluster_index
, &refcount
);
1370 case QCOW2_CLUSTER_ZERO_PLAIN
:
1371 case QCOW2_CLUSTER_UNALLOCATED
:
1379 if (refcount
== 1) {
1380 entry
|= QCOW_OFLAG_COPIED
;
1382 if (entry
!= old_entry
) {
1384 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
,
1385 s
->refcount_block_cache
);
1387 set_l2_entry(s
, l2_slice
, j
, entry
);
1388 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
,
1393 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
1397 ret
= qcow2_update_cluster_refcount(bs
, l2_offset
>>
1399 abs(addend
), addend
< 0,
1400 QCOW2_DISCARD_SNAPSHOT
);
1405 ret
= qcow2_get_refcount(bs
, l2_offset
>> s
->cluster_bits
,
1409 } else if (refcount
== 1) {
1410 l2_offset
|= QCOW_OFLAG_COPIED
;
1412 if (l2_offset
!= old_l2_offset
) {
1413 l1_table
[i
] = l2_offset
;
1419 ret
= bdrv_flush(bs
);
1422 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
1425 s
->cache_discards
= false;
1426 qcow2_process_discards(bs
, ret
);
1428 /* Update L1 only if it isn't deleted anyway (addend = -1) */
1429 if (ret
== 0 && addend
>= 0 && l1_modified
) {
1430 for (i
= 0; i
< l1_size
; i
++) {
1431 cpu_to_be64s(&l1_table
[i
]);
1434 ret
= bdrv_pwrite_sync(bs
->file
, l1_table_offset
,
1435 l1_table
, l1_size2
);
1437 for (i
= 0; i
< l1_size
; i
++) {
1438 be64_to_cpus(&l1_table
[i
]);
1449 /*********************************************************/
1450 /* refcount checking functions */
1453 static uint64_t refcount_array_byte_size(BDRVQcow2State
*s
, uint64_t entries
)
1455 /* This assertion holds because there is no way we can address more than
1456 * 2^(64 - 9) clusters at once (with cluster size 512 = 2^9, and because
1457 * offsets have to be representable in bytes); due to every cluster
1458 * corresponding to one refcount entry, we are well below that limit */
1459 assert(entries
< (UINT64_C(1) << (64 - 9)));
1461 /* Thanks to the assertion this will not overflow, because
1462 * s->refcount_order < 7.
1463 * (note: x << s->refcount_order == x * s->refcount_bits) */
1464 return DIV_ROUND_UP(entries
<< s
->refcount_order
, 8);
1468 * Reallocates *array so that it can hold new_size entries. *size must contain
1469 * the current number of entries in *array. If the reallocation fails, *array
1470 * and *size will not be modified and -errno will be returned. If the
1471 * reallocation is successful, *array will be set to the new buffer, *size
1472 * will be set to new_size and 0 will be returned. The size of the reallocated
1473 * refcount array buffer will be aligned to a cluster boundary, and the newly
1474 * allocated area will be zeroed.
1476 static int realloc_refcount_array(BDRVQcow2State
*s
, void **array
,
1477 int64_t *size
, int64_t new_size
)
1479 int64_t old_byte_size
, new_byte_size
;
1482 /* Round to clusters so the array can be directly written to disk */
1483 old_byte_size
= size_to_clusters(s
, refcount_array_byte_size(s
, *size
))
1485 new_byte_size
= size_to_clusters(s
, refcount_array_byte_size(s
, new_size
))
1488 if (new_byte_size
== old_byte_size
) {
1493 assert(new_byte_size
> 0);
1495 if (new_byte_size
> SIZE_MAX
) {
1499 new_ptr
= g_try_realloc(*array
, new_byte_size
);
1504 if (new_byte_size
> old_byte_size
) {
1505 memset((char *)new_ptr
+ old_byte_size
, 0,
1506 new_byte_size
- old_byte_size
);
1516 * Increases the refcount for a range of clusters in a given refcount table.
1517 * This is used to construct a temporary refcount table out of L1 and L2 tables
1518 * which can be compared to the refcount table saved in the image.
1520 * Modifies the number of errors in res.
1522 int qcow2_inc_refcounts_imrt(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1523 void **refcount_table
,
1524 int64_t *refcount_table_size
,
1525 int64_t offset
, int64_t size
)
1527 BDRVQcow2State
*s
= bs
->opaque
;
1528 uint64_t start
, last
, cluster_offset
, k
, refcount
;
1536 file_len
= bdrv_getlength(bs
->file
->bs
);
1542 * Last cluster of qcow2 image may be semi-allocated, so it may be OK to
1543 * reference some space after file end but it should be less than one
1546 if (offset
+ size
- file_len
>= s
->cluster_size
) {
1547 fprintf(stderr
, "ERROR: counting reference for region exceeding the "
1548 "end of the file by one cluster or more: offset 0x%" PRIx64
1549 " size 0x%" PRIx64
"\n", offset
, size
);
1554 start
= start_of_cluster(s
, offset
);
1555 last
= start_of_cluster(s
, offset
+ size
- 1);
1556 for(cluster_offset
= start
; cluster_offset
<= last
;
1557 cluster_offset
+= s
->cluster_size
) {
1558 k
= cluster_offset
>> s
->cluster_bits
;
1559 if (k
>= *refcount_table_size
) {
1560 ret
= realloc_refcount_array(s
, refcount_table
,
1561 refcount_table_size
, k
+ 1);
1563 res
->check_errors
++;
1568 refcount
= s
->get_refcount(*refcount_table
, k
);
1569 if (refcount
== s
->refcount_max
) {
1570 fprintf(stderr
, "ERROR: overflow cluster offset=0x%" PRIx64
1571 "\n", cluster_offset
);
1572 fprintf(stderr
, "Use qemu-img amend to increase the refcount entry "
1573 "width or qemu-img convert to create a clean copy if the "
1574 "image cannot be opened for writing\n");
1578 s
->set_refcount(*refcount_table
, k
, refcount
+ 1);
1584 /* Flags for check_refcounts_l1() and check_refcounts_l2() */
1586 CHECK_FRAG_INFO
= 0x2, /* update BlockFragInfo counters */
1590 * Increases the refcount in the given refcount table for the all clusters
1591 * referenced in the L2 table. While doing so, performs some checks on L2
1594 * Returns the number of errors found by the checks or -errno if an internal
1597 static int check_refcounts_l2(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1598 void **refcount_table
,
1599 int64_t *refcount_table_size
, int64_t l2_offset
,
1600 int flags
, BdrvCheckMode fix
, bool active
)
1602 BDRVQcow2State
*s
= bs
->opaque
;
1603 uint64_t *l2_table
, l2_entry
;
1604 uint64_t next_contiguous_offset
= 0;
1605 int i
, l2_size
, nb_csectors
, ret
;
1607 /* Read L2 table from disk */
1608 l2_size
= s
->l2_size
* l2_entry_size(s
);
1609 l2_table
= g_malloc(l2_size
);
1611 ret
= bdrv_pread(bs
->file
, l2_offset
, l2_table
, l2_size
);
1613 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l2\n");
1614 res
->check_errors
++;
1618 /* Do the actual checks */
1619 for(i
= 0; i
< s
->l2_size
; i
++) {
1620 l2_entry
= get_l2_entry(s
, l2_table
, i
);
1622 switch (qcow2_get_cluster_type(bs
, l2_entry
)) {
1623 case QCOW2_CLUSTER_COMPRESSED
:
1624 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
1625 if (l2_entry
& QCOW_OFLAG_COPIED
) {
1626 fprintf(stderr
, "ERROR: coffset=0x%" PRIx64
": "
1627 "copied flag must never be set for compressed "
1628 "clusters\n", l2_entry
& s
->cluster_offset_mask
);
1629 l2_entry
&= ~QCOW_OFLAG_COPIED
;
1633 if (has_data_file(bs
)) {
1634 fprintf(stderr
, "ERROR compressed cluster %d with data file, "
1635 "entry=0x%" PRIx64
"\n", i
, l2_entry
);
1640 /* Mark cluster as used */
1641 nb_csectors
= ((l2_entry
>> s
->csize_shift
) &
1643 l2_entry
&= s
->cluster_offset_mask
;
1644 ret
= qcow2_inc_refcounts_imrt(
1645 bs
, res
, refcount_table
, refcount_table_size
,
1646 l2_entry
& QCOW2_COMPRESSED_SECTOR_MASK
,
1647 nb_csectors
* QCOW2_COMPRESSED_SECTOR_SIZE
);
1652 if (flags
& CHECK_FRAG_INFO
) {
1653 res
->bfi
.allocated_clusters
++;
1654 res
->bfi
.compressed_clusters
++;
1656 /* Compressed clusters are fragmented by nature. Since they
1657 * take up sub-sector space but we only have sector granularity
1658 * I/O we need to re-read the same sectors even for adjacent
1659 * compressed clusters.
1661 res
->bfi
.fragmented_clusters
++;
1665 case QCOW2_CLUSTER_ZERO_ALLOC
:
1666 case QCOW2_CLUSTER_NORMAL
:
1668 uint64_t offset
= l2_entry
& L2E_OFFSET_MASK
;
1670 /* Correct offsets are cluster aligned */
1671 if (offset_into_cluster(s
, offset
)) {
1675 if (has_subclusters(s
)) {
1676 uint64_t l2_bitmap
= get_l2_bitmap(s
, l2_table
, i
);
1677 contains_data
= (l2_bitmap
& QCOW_L2_BITMAP_ALL_ALLOC
);
1679 contains_data
= !(l2_entry
& QCOW_OFLAG_ZERO
);
1682 if (!contains_data
) {
1683 fprintf(stderr
, "%s offset=%" PRIx64
": Preallocated "
1684 "cluster is not properly aligned; L2 entry "
1686 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR",
1688 if (fix
& BDRV_FIX_ERRORS
) {
1689 int idx
= i
* (l2_entry_size(s
) / sizeof(uint64_t));
1690 uint64_t l2e_offset
=
1691 l2_offset
+ (uint64_t)i
* l2_entry_size(s
);
1692 int ign
= active
? QCOW2_OL_ACTIVE_L2
:
1693 QCOW2_OL_INACTIVE_L2
;
1695 l2_entry
= has_subclusters(s
) ? 0 : QCOW_OFLAG_ZERO
;
1696 set_l2_entry(s
, l2_table
, i
, l2_entry
);
1697 ret
= qcow2_pre_write_overlap_check(bs
, ign
,
1698 l2e_offset
, l2_entry_size(s
), false);
1700 fprintf(stderr
, "ERROR: Overlap check failed\n");
1701 res
->check_errors
++;
1702 /* Something is seriously wrong, so abort checking
1707 ret
= bdrv_pwrite_sync(bs
->file
, l2e_offset
,
1711 fprintf(stderr
, "ERROR: Failed to overwrite L2 "
1712 "table entry: %s\n", strerror(-ret
));
1713 res
->check_errors
++;
1714 /* Do not abort, continue checking the rest of this
1715 * L2 table's entries */
1718 res
->corruptions_fixed
++;
1719 /* Skip marking the cluster as used
1720 * (it is unused now) */
1725 fprintf(stderr
, "ERROR offset=%" PRIx64
": Data cluster is "
1726 "not properly aligned; L2 entry corrupted.\n", offset
);
1730 if (flags
& CHECK_FRAG_INFO
) {
1731 res
->bfi
.allocated_clusters
++;
1732 if (next_contiguous_offset
&&
1733 offset
!= next_contiguous_offset
) {
1734 res
->bfi
.fragmented_clusters
++;
1736 next_contiguous_offset
= offset
+ s
->cluster_size
;
1739 /* Mark cluster as used */
1740 if (!has_data_file(bs
)) {
1741 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
,
1742 refcount_table_size
,
1743 offset
, s
->cluster_size
);
1751 case QCOW2_CLUSTER_ZERO_PLAIN
:
1752 case QCOW2_CLUSTER_UNALLOCATED
:
1769 * Increases the refcount for the L1 table, its L2 tables and all referenced
1770 * clusters in the given refcount table. While doing so, performs some checks
1771 * on L1 and L2 entries.
1773 * Returns the number of errors found by the checks or -errno if an internal
1776 static int check_refcounts_l1(BlockDriverState
*bs
,
1777 BdrvCheckResult
*res
,
1778 void **refcount_table
,
1779 int64_t *refcount_table_size
,
1780 int64_t l1_table_offset
, int l1_size
,
1781 int flags
, BdrvCheckMode fix
, bool active
)
1783 BDRVQcow2State
*s
= bs
->opaque
;
1784 uint64_t *l1_table
= NULL
, l2_offset
, l1_size2
;
1787 l1_size2
= l1_size
* sizeof(uint64_t);
1789 /* Mark L1 table as used */
1790 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, refcount_table_size
,
1791 l1_table_offset
, l1_size2
);
1796 /* Read L1 table entries from disk */
1798 l1_table
= g_try_malloc(l1_size2
);
1799 if (l1_table
== NULL
) {
1801 res
->check_errors
++;
1804 ret
= bdrv_pread(bs
->file
, l1_table_offset
, l1_table
, l1_size2
);
1806 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
1807 res
->check_errors
++;
1810 for(i
= 0;i
< l1_size
; i
++)
1811 be64_to_cpus(&l1_table
[i
]);
1814 /* Do the actual checks */
1815 for(i
= 0; i
< l1_size
; i
++) {
1816 l2_offset
= l1_table
[i
];
1818 /* Mark L2 table as used */
1819 l2_offset
&= L1E_OFFSET_MASK
;
1820 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
1821 refcount_table
, refcount_table_size
,
1822 l2_offset
, s
->cluster_size
);
1827 /* L2 tables are cluster aligned */
1828 if (offset_into_cluster(s
, l2_offset
)) {
1829 fprintf(stderr
, "ERROR l2_offset=%" PRIx64
": Table is not "
1830 "cluster aligned; L1 entry corrupted\n", l2_offset
);
1834 /* Process and check L2 entries */
1835 ret
= check_refcounts_l2(bs
, res
, refcount_table
,
1836 refcount_table_size
, l2_offset
, flags
,
1852 * Checks the OFLAG_COPIED flag for all L1 and L2 entries.
1854 * This function does not print an error message nor does it increment
1855 * check_errors if qcow2_get_refcount fails (this is because such an error will
1856 * have been already detected and sufficiently signaled by the calling function
1857 * (qcow2_check_refcounts) by the time this function is called).
1859 static int check_oflag_copied(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1862 BDRVQcow2State
*s
= bs
->opaque
;
1863 uint64_t *l2_table
= qemu_blockalign(bs
, s
->cluster_size
);
1869 if (fix
& BDRV_FIX_ERRORS
) {
1872 } else if (fix
& BDRV_FIX_LEAKS
) {
1873 /* Repair only if that seems safe: This function is always
1874 * called after the refcounts have been fixed, so the refcount
1875 * is accurate if that repair was successful */
1876 repair
= !res
->check_errors
&& !res
->corruptions
&& !res
->leaks
;
1881 for (i
= 0; i
< s
->l1_size
; i
++) {
1882 uint64_t l1_entry
= s
->l1_table
[i
];
1883 uint64_t l2_offset
= l1_entry
& L1E_OFFSET_MASK
;
1890 ret
= qcow2_get_refcount(bs
, l2_offset
>> s
->cluster_bits
,
1893 /* don't print message nor increment check_errors */
1896 if ((refcount
== 1) != ((l1_entry
& QCOW_OFLAG_COPIED
) != 0)) {
1898 fprintf(stderr
, "%s OFLAG_COPIED L2 cluster: l1_index=%d "
1899 "l1_entry=%" PRIx64
" refcount=%" PRIu64
"\n",
1900 repair
? "Repairing" : "ERROR", i
, l1_entry
, refcount
);
1902 s
->l1_table
[i
] = refcount
== 1
1903 ? l1_entry
| QCOW_OFLAG_COPIED
1904 : l1_entry
& ~QCOW_OFLAG_COPIED
;
1905 ret
= qcow2_write_l1_entry(bs
, i
);
1907 res
->check_errors
++;
1911 res
->corruptions_fixed
++;
1915 ret
= bdrv_pread(bs
->file
, l2_offset
, l2_table
,
1916 s
->l2_size
* l2_entry_size(s
));
1918 fprintf(stderr
, "ERROR: Could not read L2 table: %s\n",
1920 res
->check_errors
++;
1924 for (j
= 0; j
< s
->l2_size
; j
++) {
1925 uint64_t l2_entry
= get_l2_entry(s
, l2_table
, j
);
1926 uint64_t data_offset
= l2_entry
& L2E_OFFSET_MASK
;
1927 QCow2ClusterType cluster_type
= qcow2_get_cluster_type(bs
, l2_entry
);
1929 if (cluster_type
== QCOW2_CLUSTER_NORMAL
||
1930 cluster_type
== QCOW2_CLUSTER_ZERO_ALLOC
) {
1931 if (has_data_file(bs
)) {
1934 ret
= qcow2_get_refcount(bs
,
1935 data_offset
>> s
->cluster_bits
,
1938 /* don't print message nor increment check_errors */
1942 if ((refcount
== 1) != ((l2_entry
& QCOW_OFLAG_COPIED
) != 0)) {
1944 fprintf(stderr
, "%s OFLAG_COPIED data cluster: "
1945 "l2_entry=%" PRIx64
" refcount=%" PRIu64
"\n",
1946 repair
? "Repairing" : "ERROR", l2_entry
, refcount
);
1948 set_l2_entry(s
, l2_table
, j
,
1950 l2_entry
| QCOW_OFLAG_COPIED
:
1951 l2_entry
& ~QCOW_OFLAG_COPIED
);
1959 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L2
,
1960 l2_offset
, s
->cluster_size
,
1963 fprintf(stderr
, "ERROR: Could not write L2 table; metadata "
1964 "overlap check failed: %s\n", strerror(-ret
));
1965 res
->check_errors
++;
1969 ret
= bdrv_pwrite(bs
->file
, l2_offset
, l2_table
,
1972 fprintf(stderr
, "ERROR: Could not write L2 table: %s\n",
1974 res
->check_errors
++;
1977 res
->corruptions
-= l2_dirty
;
1978 res
->corruptions_fixed
+= l2_dirty
;
1985 qemu_vfree(l2_table
);
1990 * Checks consistency of refblocks and accounts for each refblock in
1993 static int check_refblocks(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1994 BdrvCheckMode fix
, bool *rebuild
,
1995 void **refcount_table
, int64_t *nb_clusters
)
1997 BDRVQcow2State
*s
= bs
->opaque
;
2001 for(i
= 0; i
< s
->refcount_table_size
; i
++) {
2002 uint64_t offset
, cluster
;
2003 offset
= s
->refcount_table
[i
];
2004 cluster
= offset
>> s
->cluster_bits
;
2006 /* Refcount blocks are cluster aligned */
2007 if (offset_into_cluster(s
, offset
)) {
2008 fprintf(stderr
, "ERROR refcount block %" PRId64
" is not "
2009 "cluster aligned; refcount table entry corrupted\n", i
);
2015 if (cluster
>= *nb_clusters
) {
2017 fprintf(stderr
, "%s refcount block %" PRId64
" is outside image\n",
2018 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR", i
);
2020 if (fix
& BDRV_FIX_ERRORS
) {
2021 int64_t new_nb_clusters
;
2022 Error
*local_err
= NULL
;
2024 if (offset
> INT64_MAX
- s
->cluster_size
) {
2029 ret
= bdrv_truncate(bs
->file
, offset
+ s
->cluster_size
, false,
2030 PREALLOC_MODE_OFF
, 0, &local_err
);
2032 error_report_err(local_err
);
2035 size
= bdrv_getlength(bs
->file
->bs
);
2041 new_nb_clusters
= size_to_clusters(s
, size
);
2042 assert(new_nb_clusters
>= *nb_clusters
);
2044 ret
= realloc_refcount_array(s
, refcount_table
,
2045 nb_clusters
, new_nb_clusters
);
2047 res
->check_errors
++;
2051 if (cluster
>= *nb_clusters
) {
2057 res
->corruptions_fixed
++;
2058 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
2059 refcount_table
, nb_clusters
,
2060 offset
, s
->cluster_size
);
2064 /* No need to check whether the refcount is now greater than 1:
2065 * This area was just allocated and zeroed, so it can only be
2066 * exactly 1 after qcow2_inc_refcounts_imrt() */
2071 fprintf(stderr
, "ERROR could not resize image: %s\n",
2078 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, nb_clusters
,
2079 offset
, s
->cluster_size
);
2083 if (s
->get_refcount(*refcount_table
, cluster
) != 1) {
2084 fprintf(stderr
, "ERROR refcount block %" PRId64
2085 " refcount=%" PRIu64
"\n", i
,
2086 s
->get_refcount(*refcount_table
, cluster
));
2097 * Calculates an in-memory refcount table.
2099 static int calculate_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
2100 BdrvCheckMode fix
, bool *rebuild
,
2101 void **refcount_table
, int64_t *nb_clusters
)
2103 BDRVQcow2State
*s
= bs
->opaque
;
2108 if (!*refcount_table
) {
2109 int64_t old_size
= 0;
2110 ret
= realloc_refcount_array(s
, refcount_table
,
2111 &old_size
, *nb_clusters
);
2113 res
->check_errors
++;
2119 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, nb_clusters
,
2120 0, s
->cluster_size
);
2125 /* current L1 table */
2126 ret
= check_refcounts_l1(bs
, res
, refcount_table
, nb_clusters
,
2127 s
->l1_table_offset
, s
->l1_size
, CHECK_FRAG_INFO
,
2134 if (has_data_file(bs
) && s
->nb_snapshots
) {
2135 fprintf(stderr
, "ERROR %d snapshots in image with data file\n",
2140 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
2141 sn
= s
->snapshots
+ i
;
2142 if (offset_into_cluster(s
, sn
->l1_table_offset
)) {
2143 fprintf(stderr
, "ERROR snapshot %s (%s) l1_offset=%#" PRIx64
": "
2144 "L1 table is not cluster aligned; snapshot table entry "
2145 "corrupted\n", sn
->id_str
, sn
->name
, sn
->l1_table_offset
);
2149 if (sn
->l1_size
> QCOW_MAX_L1_SIZE
/ sizeof(uint64_t)) {
2150 fprintf(stderr
, "ERROR snapshot %s (%s) l1_size=%#" PRIx32
": "
2151 "L1 table is too large; snapshot table entry corrupted\n",
2152 sn
->id_str
, sn
->name
, sn
->l1_size
);
2156 ret
= check_refcounts_l1(bs
, res
, refcount_table
, nb_clusters
,
2157 sn
->l1_table_offset
, sn
->l1_size
, 0, fix
,
2163 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, nb_clusters
,
2164 s
->snapshots_offset
, s
->snapshots_size
);
2170 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, nb_clusters
,
2171 s
->refcount_table_offset
,
2172 s
->refcount_table_size
* sizeof(uint64_t));
2178 if (s
->crypto_header
.length
) {
2179 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, nb_clusters
,
2180 s
->crypto_header
.offset
,
2181 s
->crypto_header
.length
);
2188 ret
= qcow2_check_bitmaps_refcounts(bs
, res
, refcount_table
, nb_clusters
);
2193 return check_refblocks(bs
, res
, fix
, rebuild
, refcount_table
, nb_clusters
);
2197 * Compares the actual reference count for each cluster in the image against the
2198 * refcount as reported by the refcount structures on-disk.
2200 static void compare_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
2201 BdrvCheckMode fix
, bool *rebuild
,
2202 int64_t *highest_cluster
,
2203 void *refcount_table
, int64_t nb_clusters
)
2205 BDRVQcow2State
*s
= bs
->opaque
;
2207 uint64_t refcount1
, refcount2
;
2210 for (i
= 0, *highest_cluster
= 0; i
< nb_clusters
; i
++) {
2211 ret
= qcow2_get_refcount(bs
, i
, &refcount1
);
2213 fprintf(stderr
, "Can't get refcount for cluster %" PRId64
": %s\n",
2215 res
->check_errors
++;
2219 refcount2
= s
->get_refcount(refcount_table
, i
);
2221 if (refcount1
> 0 || refcount2
> 0) {
2222 *highest_cluster
= i
;
2225 if (refcount1
!= refcount2
) {
2226 /* Check if we're allowed to fix the mismatch */
2227 int *num_fixed
= NULL
;
2228 if (refcount1
== 0) {
2230 } else if (refcount1
> refcount2
&& (fix
& BDRV_FIX_LEAKS
)) {
2231 num_fixed
= &res
->leaks_fixed
;
2232 } else if (refcount1
< refcount2
&& (fix
& BDRV_FIX_ERRORS
)) {
2233 num_fixed
= &res
->corruptions_fixed
;
2236 fprintf(stderr
, "%s cluster %" PRId64
" refcount=%" PRIu64
2237 " reference=%" PRIu64
"\n",
2238 num_fixed
!= NULL
? "Repairing" :
2239 refcount1
< refcount2
? "ERROR" :
2241 i
, refcount1
, refcount2
);
2244 ret
= update_refcount(bs
, i
<< s
->cluster_bits
, 1,
2245 refcount_diff(refcount1
, refcount2
),
2246 refcount1
> refcount2
,
2247 QCOW2_DISCARD_ALWAYS
);
2254 /* And if we couldn't, print an error */
2255 if (refcount1
< refcount2
) {
2265 * Allocates clusters using an in-memory refcount table (IMRT) in contrast to
2266 * the on-disk refcount structures.
2268 * On input, *first_free_cluster tells where to start looking, and need not
2269 * actually be a free cluster; the returned offset will not be before that
2270 * cluster. On output, *first_free_cluster points to the first gap found, even
2271 * if that gap was too small to be used as the returned offset.
2273 * Note that *first_free_cluster is a cluster index whereas the return value is
2276 static int64_t alloc_clusters_imrt(BlockDriverState
*bs
,
2278 void **refcount_table
,
2279 int64_t *imrt_nb_clusters
,
2280 int64_t *first_free_cluster
)
2282 BDRVQcow2State
*s
= bs
->opaque
;
2283 int64_t cluster
= *first_free_cluster
, i
;
2284 bool first_gap
= true;
2285 int contiguous_free_clusters
;
2288 /* Starting at *first_free_cluster, find a range of at least cluster_count
2289 * continuously free clusters */
2290 for (contiguous_free_clusters
= 0;
2291 cluster
< *imrt_nb_clusters
&&
2292 contiguous_free_clusters
< cluster_count
;
2295 if (!s
->get_refcount(*refcount_table
, cluster
)) {
2296 contiguous_free_clusters
++;
2298 /* If this is the first free cluster found, update
2299 * *first_free_cluster accordingly */
2300 *first_free_cluster
= cluster
;
2303 } else if (contiguous_free_clusters
) {
2304 contiguous_free_clusters
= 0;
2308 /* If contiguous_free_clusters is greater than zero, it contains the number
2309 * of continuously free clusters until the current cluster; the first free
2310 * cluster in the current "gap" is therefore
2311 * cluster - contiguous_free_clusters */
2313 /* If no such range could be found, grow the in-memory refcount table
2314 * accordingly to append free clusters at the end of the image */
2315 if (contiguous_free_clusters
< cluster_count
) {
2316 /* contiguous_free_clusters clusters are already empty at the image end;
2317 * we need cluster_count clusters; therefore, we have to allocate
2318 * cluster_count - contiguous_free_clusters new clusters at the end of
2319 * the image (which is the current value of cluster; note that cluster
2320 * may exceed old_imrt_nb_clusters if *first_free_cluster pointed beyond
2322 ret
= realloc_refcount_array(s
, refcount_table
, imrt_nb_clusters
,
2323 cluster
+ cluster_count
2324 - contiguous_free_clusters
);
2330 /* Go back to the first free cluster */
2331 cluster
-= contiguous_free_clusters
;
2332 for (i
= 0; i
< cluster_count
; i
++) {
2333 s
->set_refcount(*refcount_table
, cluster
+ i
, 1);
2336 return cluster
<< s
->cluster_bits
;
2340 * Creates a new refcount structure based solely on the in-memory information
2341 * given through *refcount_table. All necessary allocations will be reflected
2344 * On success, the old refcount structure is leaked (it will be covered by the
2345 * new refcount structure).
2347 static int rebuild_refcount_structure(BlockDriverState
*bs
,
2348 BdrvCheckResult
*res
,
2349 void **refcount_table
,
2350 int64_t *nb_clusters
)
2352 BDRVQcow2State
*s
= bs
->opaque
;
2353 int64_t first_free_cluster
= 0, reftable_offset
= -1, cluster
= 0;
2354 int64_t refblock_offset
, refblock_start
, refblock_index
;
2355 uint32_t reftable_size
= 0;
2356 uint64_t *on_disk_reftable
= NULL
;
2357 void *on_disk_refblock
;
2360 uint64_t reftable_offset
;
2361 uint32_t reftable_clusters
;
2362 } QEMU_PACKED reftable_offset_and_clusters
;
2364 qcow2_cache_empty(bs
, s
->refcount_block_cache
);
2367 for (; cluster
< *nb_clusters
; cluster
++) {
2368 if (!s
->get_refcount(*refcount_table
, cluster
)) {
2372 refblock_index
= cluster
>> s
->refcount_block_bits
;
2373 refblock_start
= refblock_index
<< s
->refcount_block_bits
;
2375 /* Don't allocate a cluster in a refblock already written to disk */
2376 if (first_free_cluster
< refblock_start
) {
2377 first_free_cluster
= refblock_start
;
2379 refblock_offset
= alloc_clusters_imrt(bs
, 1, refcount_table
,
2380 nb_clusters
, &first_free_cluster
);
2381 if (refblock_offset
< 0) {
2382 fprintf(stderr
, "ERROR allocating refblock: %s\n",
2383 strerror(-refblock_offset
));
2384 res
->check_errors
++;
2385 ret
= refblock_offset
;
2389 if (reftable_size
<= refblock_index
) {
2390 uint32_t old_reftable_size
= reftable_size
;
2391 uint64_t *new_on_disk_reftable
;
2393 reftable_size
= ROUND_UP((refblock_index
+ 1) * sizeof(uint64_t),
2394 s
->cluster_size
) / sizeof(uint64_t);
2395 new_on_disk_reftable
= g_try_realloc(on_disk_reftable
,
2398 if (!new_on_disk_reftable
) {
2399 res
->check_errors
++;
2403 on_disk_reftable
= new_on_disk_reftable
;
2405 memset(on_disk_reftable
+ old_reftable_size
, 0,
2406 (reftable_size
- old_reftable_size
) * sizeof(uint64_t));
2408 /* The offset we have for the reftable is now no longer valid;
2409 * this will leak that range, but we can easily fix that by running
2410 * a leak-fixing check after this rebuild operation */
2411 reftable_offset
= -1;
2413 assert(on_disk_reftable
);
2415 on_disk_reftable
[refblock_index
] = refblock_offset
;
2417 /* If this is apparently the last refblock (for now), try to squeeze the
2419 if (refblock_index
== (*nb_clusters
- 1) >> s
->refcount_block_bits
&&
2420 reftable_offset
< 0)
2422 uint64_t reftable_clusters
= size_to_clusters(s
, reftable_size
*
2424 reftable_offset
= alloc_clusters_imrt(bs
, reftable_clusters
,
2425 refcount_table
, nb_clusters
,
2426 &first_free_cluster
);
2427 if (reftable_offset
< 0) {
2428 fprintf(stderr
, "ERROR allocating reftable: %s\n",
2429 strerror(-reftable_offset
));
2430 res
->check_errors
++;
2431 ret
= reftable_offset
;
2436 ret
= qcow2_pre_write_overlap_check(bs
, 0, refblock_offset
,
2437 s
->cluster_size
, false);
2439 fprintf(stderr
, "ERROR writing refblock: %s\n", strerror(-ret
));
2443 /* The size of *refcount_table is always cluster-aligned, therefore the
2444 * write operation will not overflow */
2445 on_disk_refblock
= (void *)((char *) *refcount_table
+
2446 refblock_index
* s
->cluster_size
);
2448 ret
= bdrv_pwrite(bs
->file
, refblock_offset
, on_disk_refblock
,
2451 fprintf(stderr
, "ERROR writing refblock: %s\n", strerror(-ret
));
2455 /* Go to the end of this refblock */
2456 cluster
= refblock_start
+ s
->refcount_block_size
- 1;
2459 if (reftable_offset
< 0) {
2460 uint64_t post_refblock_start
, reftable_clusters
;
2462 post_refblock_start
= ROUND_UP(*nb_clusters
, s
->refcount_block_size
);
2463 reftable_clusters
= size_to_clusters(s
,
2464 reftable_size
* sizeof(uint64_t));
2465 /* Not pretty but simple */
2466 if (first_free_cluster
< post_refblock_start
) {
2467 first_free_cluster
= post_refblock_start
;
2469 reftable_offset
= alloc_clusters_imrt(bs
, reftable_clusters
,
2470 refcount_table
, nb_clusters
,
2471 &first_free_cluster
);
2472 if (reftable_offset
< 0) {
2473 fprintf(stderr
, "ERROR allocating reftable: %s\n",
2474 strerror(-reftable_offset
));
2475 res
->check_errors
++;
2476 ret
= reftable_offset
;
2480 goto write_refblocks
;
2483 for (refblock_index
= 0; refblock_index
< reftable_size
; refblock_index
++) {
2484 cpu_to_be64s(&on_disk_reftable
[refblock_index
]);
2487 ret
= qcow2_pre_write_overlap_check(bs
, 0, reftable_offset
,
2488 reftable_size
* sizeof(uint64_t),
2491 fprintf(stderr
, "ERROR writing reftable: %s\n", strerror(-ret
));
2495 assert(reftable_size
< INT_MAX
/ sizeof(uint64_t));
2496 ret
= bdrv_pwrite(bs
->file
, reftable_offset
, on_disk_reftable
,
2497 reftable_size
* sizeof(uint64_t));
2499 fprintf(stderr
, "ERROR writing reftable: %s\n", strerror(-ret
));
2503 /* Enter new reftable into the image header */
2504 reftable_offset_and_clusters
.reftable_offset
= cpu_to_be64(reftable_offset
);
2505 reftable_offset_and_clusters
.reftable_clusters
=
2506 cpu_to_be32(size_to_clusters(s
, reftable_size
* sizeof(uint64_t)));
2507 ret
= bdrv_pwrite_sync(bs
->file
,
2508 offsetof(QCowHeader
, refcount_table_offset
),
2509 &reftable_offset_and_clusters
,
2510 sizeof(reftable_offset_and_clusters
));
2512 fprintf(stderr
, "ERROR setting reftable: %s\n", strerror(-ret
));
2516 for (refblock_index
= 0; refblock_index
< reftable_size
; refblock_index
++) {
2517 be64_to_cpus(&on_disk_reftable
[refblock_index
]);
2519 s
->refcount_table
= on_disk_reftable
;
2520 s
->refcount_table_offset
= reftable_offset
;
2521 s
->refcount_table_size
= reftable_size
;
2522 update_max_refcount_table_index(s
);
2527 g_free(on_disk_reftable
);
2532 * Checks an image for refcount consistency.
2534 * Returns 0 if no errors are found, the number of errors in case the image is
2535 * detected as corrupted, and -errno when an internal error occurred.
2537 int qcow2_check_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
2540 BDRVQcow2State
*s
= bs
->opaque
;
2541 BdrvCheckResult pre_compare_res
;
2542 int64_t size
, highest_cluster
, nb_clusters
;
2543 void *refcount_table
= NULL
;
2544 bool rebuild
= false;
2547 size
= bdrv_getlength(bs
->file
->bs
);
2549 res
->check_errors
++;
2553 nb_clusters
= size_to_clusters(s
, size
);
2554 if (nb_clusters
> INT_MAX
) {
2555 res
->check_errors
++;
2559 res
->bfi
.total_clusters
=
2560 size_to_clusters(s
, bs
->total_sectors
* BDRV_SECTOR_SIZE
);
2562 ret
= calculate_refcounts(bs
, res
, fix
, &rebuild
, &refcount_table
,
2568 /* In case we don't need to rebuild the refcount structure (but want to fix
2569 * something), this function is immediately called again, in which case the
2570 * result should be ignored */
2571 pre_compare_res
= *res
;
2572 compare_refcounts(bs
, res
, 0, &rebuild
, &highest_cluster
, refcount_table
,
2575 if (rebuild
&& (fix
& BDRV_FIX_ERRORS
)) {
2576 BdrvCheckResult old_res
= *res
;
2577 int fresh_leaks
= 0;
2579 fprintf(stderr
, "Rebuilding refcount structure\n");
2580 ret
= rebuild_refcount_structure(bs
, res
, &refcount_table
,
2586 res
->corruptions
= 0;
2589 /* Because the old reftable has been exchanged for a new one the
2590 * references have to be recalculated */
2592 memset(refcount_table
, 0, refcount_array_byte_size(s
, nb_clusters
));
2593 ret
= calculate_refcounts(bs
, res
, 0, &rebuild
, &refcount_table
,
2599 if (fix
& BDRV_FIX_LEAKS
) {
2600 /* The old refcount structures are now leaked, fix it; the result
2601 * can be ignored, aside from leaks which were introduced by
2602 * rebuild_refcount_structure() that could not be fixed */
2603 BdrvCheckResult saved_res
= *res
;
2604 *res
= (BdrvCheckResult
){ 0 };
2606 compare_refcounts(bs
, res
, BDRV_FIX_LEAKS
, &rebuild
,
2607 &highest_cluster
, refcount_table
, nb_clusters
);
2609 fprintf(stderr
, "ERROR rebuilt refcount structure is still "
2613 /* Any leaks accounted for here were introduced by
2614 * rebuild_refcount_structure() because that function has created a
2615 * new refcount structure from scratch */
2616 fresh_leaks
= res
->leaks
;
2620 if (res
->corruptions
< old_res
.corruptions
) {
2621 res
->corruptions_fixed
+= old_res
.corruptions
- res
->corruptions
;
2623 if (res
->leaks
< old_res
.leaks
) {
2624 res
->leaks_fixed
+= old_res
.leaks
- res
->leaks
;
2626 res
->leaks
+= fresh_leaks
;
2629 fprintf(stderr
, "ERROR need to rebuild refcount structures\n");
2630 res
->check_errors
++;
2635 if (res
->leaks
|| res
->corruptions
) {
2636 *res
= pre_compare_res
;
2637 compare_refcounts(bs
, res
, fix
, &rebuild
, &highest_cluster
,
2638 refcount_table
, nb_clusters
);
2642 /* check OFLAG_COPIED */
2643 ret
= check_oflag_copied(bs
, res
, fix
);
2648 res
->image_end_offset
= (highest_cluster
+ 1) * s
->cluster_size
;
2652 g_free(refcount_table
);
2657 #define overlaps_with(ofs, sz) \
2658 ranges_overlap(offset, size, ofs, sz)
2661 * Checks if the given offset into the image file is actually free to use by
2662 * looking for overlaps with important metadata sections (L1/L2 tables etc.),
2663 * i.e. a sanity check without relying on the refcount tables.
2665 * The ign parameter specifies what checks not to perform (being a bitmask of
2666 * QCow2MetadataOverlap values), i.e., what sections to ignore.
2669 * - 0 if writing to this offset will not affect the mentioned metadata
2670 * - a positive QCow2MetadataOverlap value indicating one overlapping section
2671 * - a negative value (-errno) indicating an error while performing a check,
2672 * e.g. when bdrv_pread failed on QCOW2_OL_INACTIVE_L2
2674 int qcow2_check_metadata_overlap(BlockDriverState
*bs
, int ign
, int64_t offset
,
2677 BDRVQcow2State
*s
= bs
->opaque
;
2678 int chk
= s
->overlap_check
& ~ign
;
2685 if (chk
& QCOW2_OL_MAIN_HEADER
) {
2686 if (offset
< s
->cluster_size
) {
2687 return QCOW2_OL_MAIN_HEADER
;
2691 /* align range to test to cluster boundaries */
2692 size
= ROUND_UP(offset_into_cluster(s
, offset
) + size
, s
->cluster_size
);
2693 offset
= start_of_cluster(s
, offset
);
2695 if ((chk
& QCOW2_OL_ACTIVE_L1
) && s
->l1_size
) {
2696 if (overlaps_with(s
->l1_table_offset
, s
->l1_size
* sizeof(uint64_t))) {
2697 return QCOW2_OL_ACTIVE_L1
;
2701 if ((chk
& QCOW2_OL_REFCOUNT_TABLE
) && s
->refcount_table_size
) {
2702 if (overlaps_with(s
->refcount_table_offset
,
2703 s
->refcount_table_size
* sizeof(uint64_t))) {
2704 return QCOW2_OL_REFCOUNT_TABLE
;
2708 if ((chk
& QCOW2_OL_SNAPSHOT_TABLE
) && s
->snapshots_size
) {
2709 if (overlaps_with(s
->snapshots_offset
, s
->snapshots_size
)) {
2710 return QCOW2_OL_SNAPSHOT_TABLE
;
2714 if ((chk
& QCOW2_OL_INACTIVE_L1
) && s
->snapshots
) {
2715 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
2716 if (s
->snapshots
[i
].l1_size
&&
2717 overlaps_with(s
->snapshots
[i
].l1_table_offset
,
2718 s
->snapshots
[i
].l1_size
* sizeof(uint64_t))) {
2719 return QCOW2_OL_INACTIVE_L1
;
2724 if ((chk
& QCOW2_OL_ACTIVE_L2
) && s
->l1_table
) {
2725 for (i
= 0; i
< s
->l1_size
; i
++) {
2726 if ((s
->l1_table
[i
] & L1E_OFFSET_MASK
) &&
2727 overlaps_with(s
->l1_table
[i
] & L1E_OFFSET_MASK
,
2729 return QCOW2_OL_ACTIVE_L2
;
2734 if ((chk
& QCOW2_OL_REFCOUNT_BLOCK
) && s
->refcount_table
) {
2735 unsigned last_entry
= s
->max_refcount_table_index
;
2736 assert(last_entry
< s
->refcount_table_size
);
2737 assert(last_entry
+ 1 == s
->refcount_table_size
||
2738 (s
->refcount_table
[last_entry
+ 1] & REFT_OFFSET_MASK
) == 0);
2739 for (i
= 0; i
<= last_entry
; i
++) {
2740 if ((s
->refcount_table
[i
] & REFT_OFFSET_MASK
) &&
2741 overlaps_with(s
->refcount_table
[i
] & REFT_OFFSET_MASK
,
2743 return QCOW2_OL_REFCOUNT_BLOCK
;
2748 if ((chk
& QCOW2_OL_INACTIVE_L2
) && s
->snapshots
) {
2749 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
2750 uint64_t l1_ofs
= s
->snapshots
[i
].l1_table_offset
;
2751 uint32_t l1_sz
= s
->snapshots
[i
].l1_size
;
2752 uint64_t l1_sz2
= l1_sz
* sizeof(uint64_t);
2756 ret
= qcow2_validate_table(bs
, l1_ofs
, l1_sz
, sizeof(uint64_t),
2757 QCOW_MAX_L1_SIZE
, "", NULL
);
2762 l1
= g_try_malloc(l1_sz2
);
2764 if (l1_sz2
&& l1
== NULL
) {
2768 ret
= bdrv_pread(bs
->file
, l1_ofs
, l1
, l1_sz2
);
2774 for (j
= 0; j
< l1_sz
; j
++) {
2775 uint64_t l2_ofs
= be64_to_cpu(l1
[j
]) & L1E_OFFSET_MASK
;
2776 if (l2_ofs
&& overlaps_with(l2_ofs
, s
->cluster_size
)) {
2778 return QCOW2_OL_INACTIVE_L2
;
2786 if ((chk
& QCOW2_OL_BITMAP_DIRECTORY
) &&
2787 (s
->autoclear_features
& QCOW2_AUTOCLEAR_BITMAPS
))
2789 if (overlaps_with(s
->bitmap_directory_offset
,
2790 s
->bitmap_directory_size
))
2792 return QCOW2_OL_BITMAP_DIRECTORY
;
2799 static const char *metadata_ol_names
[] = {
2800 [QCOW2_OL_MAIN_HEADER_BITNR
] = "qcow2_header",
2801 [QCOW2_OL_ACTIVE_L1_BITNR
] = "active L1 table",
2802 [QCOW2_OL_ACTIVE_L2_BITNR
] = "active L2 table",
2803 [QCOW2_OL_REFCOUNT_TABLE_BITNR
] = "refcount table",
2804 [QCOW2_OL_REFCOUNT_BLOCK_BITNR
] = "refcount block",
2805 [QCOW2_OL_SNAPSHOT_TABLE_BITNR
] = "snapshot table",
2806 [QCOW2_OL_INACTIVE_L1_BITNR
] = "inactive L1 table",
2807 [QCOW2_OL_INACTIVE_L2_BITNR
] = "inactive L2 table",
2808 [QCOW2_OL_BITMAP_DIRECTORY_BITNR
] = "bitmap directory",
2810 QEMU_BUILD_BUG_ON(QCOW2_OL_MAX_BITNR
!= ARRAY_SIZE(metadata_ol_names
));
2813 * First performs a check for metadata overlaps (through
2814 * qcow2_check_metadata_overlap); if that fails with a negative value (error
2815 * while performing a check), that value is returned. If an impending overlap
2816 * is detected, the BDS will be made unusable, the qcow2 file marked corrupt
2817 * and -EIO returned.
2819 * Returns 0 if there were neither overlaps nor errors while checking for
2820 * overlaps; or a negative value (-errno) on error.
2822 int qcow2_pre_write_overlap_check(BlockDriverState
*bs
, int ign
, int64_t offset
,
2823 int64_t size
, bool data_file
)
2827 if (data_file
&& has_data_file(bs
)) {
2831 ret
= qcow2_check_metadata_overlap(bs
, ign
, offset
, size
);
2834 } else if (ret
> 0) {
2835 int metadata_ol_bitnr
= ctz32(ret
);
2836 assert(metadata_ol_bitnr
< QCOW2_OL_MAX_BITNR
);
2838 qcow2_signal_corruption(bs
, true, offset
, size
, "Preventing invalid "
2839 "write on metadata (overlaps with %s)",
2840 metadata_ol_names
[metadata_ol_bitnr
]);
2847 /* A pointer to a function of this type is given to walk_over_reftable(). That
2848 * function will create refblocks and pass them to a RefblockFinishOp once they
2849 * are completed (@refblock). @refblock_empty is set if the refblock is
2852 * Along with the refblock, a corresponding reftable entry is passed, in the
2853 * reftable @reftable (which may be reallocated) at @reftable_index.
2855 * @allocated should be set to true if a new cluster has been allocated.
2857 typedef int (RefblockFinishOp
)(BlockDriverState
*bs
, uint64_t **reftable
,
2858 uint64_t reftable_index
, uint64_t *reftable_size
,
2859 void *refblock
, bool refblock_empty
,
2860 bool *allocated
, Error
**errp
);
2863 * This "operation" for walk_over_reftable() allocates the refblock on disk (if
2864 * it is not empty) and inserts its offset into the new reftable. The size of
2865 * this new reftable is increased as required.
2867 static int alloc_refblock(BlockDriverState
*bs
, uint64_t **reftable
,
2868 uint64_t reftable_index
, uint64_t *reftable_size
,
2869 void *refblock
, bool refblock_empty
, bool *allocated
,
2872 BDRVQcow2State
*s
= bs
->opaque
;
2875 if (!refblock_empty
&& reftable_index
>= *reftable_size
) {
2876 uint64_t *new_reftable
;
2877 uint64_t new_reftable_size
;
2879 new_reftable_size
= ROUND_UP(reftable_index
+ 1,
2880 s
->cluster_size
/ sizeof(uint64_t));
2881 if (new_reftable_size
> QCOW_MAX_REFTABLE_SIZE
/ sizeof(uint64_t)) {
2883 "This operation would make the refcount table grow "
2884 "beyond the maximum size supported by QEMU, aborting");
2888 new_reftable
= g_try_realloc(*reftable
, new_reftable_size
*
2890 if (!new_reftable
) {
2891 error_setg(errp
, "Failed to increase reftable buffer size");
2895 memset(new_reftable
+ *reftable_size
, 0,
2896 (new_reftable_size
- *reftable_size
) * sizeof(uint64_t));
2898 *reftable
= new_reftable
;
2899 *reftable_size
= new_reftable_size
;
2902 if (!refblock_empty
&& !(*reftable
)[reftable_index
]) {
2903 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
2905 error_setg_errno(errp
, -offset
, "Failed to allocate refblock");
2908 (*reftable
)[reftable_index
] = offset
;
2916 * This "operation" for walk_over_reftable() writes the refblock to disk at the
2917 * offset specified by the new reftable's entry. It does not modify the new
2918 * reftable or change any refcounts.
2920 static int flush_refblock(BlockDriverState
*bs
, uint64_t **reftable
,
2921 uint64_t reftable_index
, uint64_t *reftable_size
,
2922 void *refblock
, bool refblock_empty
, bool *allocated
,
2925 BDRVQcow2State
*s
= bs
->opaque
;
2929 if (reftable_index
< *reftable_size
&& (*reftable
)[reftable_index
]) {
2930 offset
= (*reftable
)[reftable_index
];
2932 ret
= qcow2_pre_write_overlap_check(bs
, 0, offset
, s
->cluster_size
,
2935 error_setg_errno(errp
, -ret
, "Overlap check failed");
2939 ret
= bdrv_pwrite(bs
->file
, offset
, refblock
, s
->cluster_size
);
2941 error_setg_errno(errp
, -ret
, "Failed to write refblock");
2945 assert(refblock_empty
);
2952 * This function walks over the existing reftable and every referenced refblock;
2953 * if @new_set_refcount is non-NULL, it is called for every refcount entry to
2954 * create an equal new entry in the passed @new_refblock. Once that
2955 * @new_refblock is completely filled, @operation will be called.
2957 * @status_cb and @cb_opaque are used for the amend operation's status callback.
2958 * @index is the index of the walk_over_reftable() calls and @total is the total
2959 * number of walk_over_reftable() calls per amend operation. Both are used for
2960 * calculating the parameters for the status callback.
2962 * @allocated is set to true if a new cluster has been allocated.
2964 static int walk_over_reftable(BlockDriverState
*bs
, uint64_t **new_reftable
,
2965 uint64_t *new_reftable_index
,
2966 uint64_t *new_reftable_size
,
2967 void *new_refblock
, int new_refblock_size
,
2968 int new_refcount_bits
,
2969 RefblockFinishOp
*operation
, bool *allocated
,
2970 Qcow2SetRefcountFunc
*new_set_refcount
,
2971 BlockDriverAmendStatusCB
*status_cb
,
2972 void *cb_opaque
, int index
, int total
,
2975 BDRVQcow2State
*s
= bs
->opaque
;
2976 uint64_t reftable_index
;
2977 bool new_refblock_empty
= true;
2979 int new_refblock_index
= 0;
2982 for (reftable_index
= 0; reftable_index
< s
->refcount_table_size
;
2985 uint64_t refblock_offset
= s
->refcount_table
[reftable_index
]
2988 status_cb(bs
, (uint64_t)index
* s
->refcount_table_size
+ reftable_index
,
2989 (uint64_t)total
* s
->refcount_table_size
, cb_opaque
);
2991 if (refblock_offset
) {
2994 if (offset_into_cluster(s
, refblock_offset
)) {
2995 qcow2_signal_corruption(bs
, true, -1, -1, "Refblock offset %#"
2996 PRIx64
" unaligned (reftable index: %#"
2997 PRIx64
")", refblock_offset
,
3000 "Image is corrupt (unaligned refblock offset)");
3004 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refblock_offset
,
3007 error_setg_errno(errp
, -ret
, "Failed to retrieve refblock");
3011 for (refblock_index
= 0; refblock_index
< s
->refcount_block_size
;
3016 if (new_refblock_index
>= new_refblock_size
) {
3017 /* new_refblock is now complete */
3018 ret
= operation(bs
, new_reftable
, *new_reftable_index
,
3019 new_reftable_size
, new_refblock
,
3020 new_refblock_empty
, allocated
, errp
);
3022 qcow2_cache_put(s
->refcount_block_cache
, &refblock
);
3026 (*new_reftable_index
)++;
3027 new_refblock_index
= 0;
3028 new_refblock_empty
= true;
3031 refcount
= s
->get_refcount(refblock
, refblock_index
);
3032 if (new_refcount_bits
< 64 && refcount
>> new_refcount_bits
) {
3035 qcow2_cache_put(s
->refcount_block_cache
, &refblock
);
3037 offset
= ((reftable_index
<< s
->refcount_block_bits
)
3038 + refblock_index
) << s
->cluster_bits
;
3040 error_setg(errp
, "Cannot decrease refcount entry width to "
3041 "%i bits: Cluster at offset %#" PRIx64
" has a "
3042 "refcount of %" PRIu64
, new_refcount_bits
,
3047 if (new_set_refcount
) {
3048 new_set_refcount(new_refblock
, new_refblock_index
++,
3051 new_refblock_index
++;
3053 new_refblock_empty
= new_refblock_empty
&& refcount
== 0;
3056 qcow2_cache_put(s
->refcount_block_cache
, &refblock
);
3058 /* No refblock means every refcount is 0 */
3059 for (refblock_index
= 0; refblock_index
< s
->refcount_block_size
;
3062 if (new_refblock_index
>= new_refblock_size
) {
3063 /* new_refblock is now complete */
3064 ret
= operation(bs
, new_reftable
, *new_reftable_index
,
3065 new_reftable_size
, new_refblock
,
3066 new_refblock_empty
, allocated
, errp
);
3071 (*new_reftable_index
)++;
3072 new_refblock_index
= 0;
3073 new_refblock_empty
= true;
3076 if (new_set_refcount
) {
3077 new_set_refcount(new_refblock
, new_refblock_index
++, 0);
3079 new_refblock_index
++;
3085 if (new_refblock_index
> 0) {
3086 /* Complete the potentially existing partially filled final refblock */
3087 if (new_set_refcount
) {
3088 for (; new_refblock_index
< new_refblock_size
;
3089 new_refblock_index
++)
3091 new_set_refcount(new_refblock
, new_refblock_index
, 0);
3095 ret
= operation(bs
, new_reftable
, *new_reftable_index
,
3096 new_reftable_size
, new_refblock
, new_refblock_empty
,
3102 (*new_reftable_index
)++;
3105 status_cb(bs
, (uint64_t)(index
+ 1) * s
->refcount_table_size
,
3106 (uint64_t)total
* s
->refcount_table_size
, cb_opaque
);
3111 int qcow2_change_refcount_order(BlockDriverState
*bs
, int refcount_order
,
3112 BlockDriverAmendStatusCB
*status_cb
,
3113 void *cb_opaque
, Error
**errp
)
3115 BDRVQcow2State
*s
= bs
->opaque
;
3116 Qcow2GetRefcountFunc
*new_get_refcount
;
3117 Qcow2SetRefcountFunc
*new_set_refcount
;
3118 void *new_refblock
= qemu_blockalign(bs
->file
->bs
, s
->cluster_size
);
3119 uint64_t *new_reftable
= NULL
, new_reftable_size
= 0;
3120 uint64_t *old_reftable
, old_reftable_size
, old_reftable_offset
;
3121 uint64_t new_reftable_index
= 0;
3123 int64_t new_reftable_offset
= 0, allocated_reftable_size
= 0;
3124 int new_refblock_size
, new_refcount_bits
= 1 << refcount_order
;
3125 int old_refcount_order
;
3128 bool new_allocation
;
3130 assert(s
->qcow_version
>= 3);
3131 assert(refcount_order
>= 0 && refcount_order
<= 6);
3133 /* see qcow2_open() */
3134 new_refblock_size
= 1 << (s
->cluster_bits
- (refcount_order
- 3));
3136 new_get_refcount
= get_refcount_funcs
[refcount_order
];
3137 new_set_refcount
= set_refcount_funcs
[refcount_order
];
3143 new_allocation
= false;
3145 /* At least we have to do this walk and the one which writes the
3146 * refblocks; also, at least we have to do this loop here at least
3147 * twice (normally), first to do the allocations, and second to
3148 * determine that everything is correctly allocated, this then makes
3149 * three walks in total */
3150 total_walks
= MAX(walk_index
+ 2, 3);
3152 /* First, allocate the structures so they are present in the refcount
3154 ret
= walk_over_reftable(bs
, &new_reftable
, &new_reftable_index
,
3155 &new_reftable_size
, NULL
, new_refblock_size
,
3156 new_refcount_bits
, &alloc_refblock
,
3157 &new_allocation
, NULL
, status_cb
, cb_opaque
,
3158 walk_index
++, total_walks
, errp
);
3163 new_reftable_index
= 0;
3165 if (new_allocation
) {
3166 if (new_reftable_offset
) {
3167 qcow2_free_clusters(bs
, new_reftable_offset
,
3168 allocated_reftable_size
* sizeof(uint64_t),
3169 QCOW2_DISCARD_NEVER
);
3172 new_reftable_offset
= qcow2_alloc_clusters(bs
, new_reftable_size
*
3174 if (new_reftable_offset
< 0) {
3175 error_setg_errno(errp
, -new_reftable_offset
,
3176 "Failed to allocate the new reftable");
3177 ret
= new_reftable_offset
;
3180 allocated_reftable_size
= new_reftable_size
;
3182 } while (new_allocation
);
3184 /* Second, write the new refblocks */
3185 ret
= walk_over_reftable(bs
, &new_reftable
, &new_reftable_index
,
3186 &new_reftable_size
, new_refblock
,
3187 new_refblock_size
, new_refcount_bits
,
3188 &flush_refblock
, &new_allocation
, new_set_refcount
,
3189 status_cb
, cb_opaque
, walk_index
, walk_index
+ 1,
3194 assert(!new_allocation
);
3197 /* Write the new reftable */
3198 ret
= qcow2_pre_write_overlap_check(bs
, 0, new_reftable_offset
,
3199 new_reftable_size
* sizeof(uint64_t),
3202 error_setg_errno(errp
, -ret
, "Overlap check failed");
3206 for (i
= 0; i
< new_reftable_size
; i
++) {
3207 cpu_to_be64s(&new_reftable
[i
]);
3210 ret
= bdrv_pwrite(bs
->file
, new_reftable_offset
, new_reftable
,
3211 new_reftable_size
* sizeof(uint64_t));
3213 for (i
= 0; i
< new_reftable_size
; i
++) {
3214 be64_to_cpus(&new_reftable
[i
]);
3218 error_setg_errno(errp
, -ret
, "Failed to write the new reftable");
3223 /* Empty the refcount cache */
3224 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
3226 error_setg_errno(errp
, -ret
, "Failed to flush the refblock cache");
3230 /* Update the image header to point to the new reftable; this only updates
3231 * the fields which are relevant to qcow2_update_header(); other fields
3232 * such as s->refcount_table or s->refcount_bits stay stale for now
3233 * (because we have to restore everything if qcow2_update_header() fails) */
3234 old_refcount_order
= s
->refcount_order
;
3235 old_reftable_size
= s
->refcount_table_size
;
3236 old_reftable_offset
= s
->refcount_table_offset
;
3238 s
->refcount_order
= refcount_order
;
3239 s
->refcount_table_size
= new_reftable_size
;
3240 s
->refcount_table_offset
= new_reftable_offset
;
3242 ret
= qcow2_update_header(bs
);
3244 s
->refcount_order
= old_refcount_order
;
3245 s
->refcount_table_size
= old_reftable_size
;
3246 s
->refcount_table_offset
= old_reftable_offset
;
3247 error_setg_errno(errp
, -ret
, "Failed to update the qcow2 header");
3251 /* Now update the rest of the in-memory information */
3252 old_reftable
= s
->refcount_table
;
3253 s
->refcount_table
= new_reftable
;
3254 update_max_refcount_table_index(s
);
3256 s
->refcount_bits
= 1 << refcount_order
;
3257 s
->refcount_max
= UINT64_C(1) << (s
->refcount_bits
- 1);
3258 s
->refcount_max
+= s
->refcount_max
- 1;
3260 s
->refcount_block_bits
= s
->cluster_bits
- (refcount_order
- 3);
3261 s
->refcount_block_size
= 1 << s
->refcount_block_bits
;
3263 s
->get_refcount
= new_get_refcount
;
3264 s
->set_refcount
= new_set_refcount
;
3266 /* For cleaning up all old refblocks and the old reftable below the "done"
3268 new_reftable
= old_reftable
;
3269 new_reftable_size
= old_reftable_size
;
3270 new_reftable_offset
= old_reftable_offset
;
3274 /* On success, new_reftable actually points to the old reftable (and
3275 * new_reftable_size is the old reftable's size); but that is just
3277 for (i
= 0; i
< new_reftable_size
; i
++) {
3278 uint64_t offset
= new_reftable
[i
] & REFT_OFFSET_MASK
;
3280 qcow2_free_clusters(bs
, offset
, s
->cluster_size
,
3281 QCOW2_DISCARD_OTHER
);
3284 g_free(new_reftable
);
3286 if (new_reftable_offset
> 0) {
3287 qcow2_free_clusters(bs
, new_reftable_offset
,
3288 new_reftable_size
* sizeof(uint64_t),
3289 QCOW2_DISCARD_OTHER
);
3293 qemu_vfree(new_refblock
);
3297 static int64_t get_refblock_offset(BlockDriverState
*bs
, uint64_t offset
)
3299 BDRVQcow2State
*s
= bs
->opaque
;
3300 uint32_t index
= offset_to_reftable_index(s
, offset
);
3301 int64_t covering_refblock_offset
= 0;
3303 if (index
< s
->refcount_table_size
) {
3304 covering_refblock_offset
= s
->refcount_table
[index
] & REFT_OFFSET_MASK
;
3306 if (!covering_refblock_offset
) {
3307 qcow2_signal_corruption(bs
, true, -1, -1, "Refblock at %#" PRIx64
" is "
3308 "not covered by the refcount structures",
3313 return covering_refblock_offset
;
3316 static int qcow2_discard_refcount_block(BlockDriverState
*bs
,
3317 uint64_t discard_block_offs
)
3319 BDRVQcow2State
*s
= bs
->opaque
;
3320 int64_t refblock_offs
;
3321 uint64_t cluster_index
= discard_block_offs
>> s
->cluster_bits
;
3322 uint32_t block_index
= cluster_index
& (s
->refcount_block_size
- 1);
3326 refblock_offs
= get_refblock_offset(bs
, discard_block_offs
);
3327 if (refblock_offs
< 0) {
3328 return refblock_offs
;
3331 assert(discard_block_offs
!= 0);
3333 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refblock_offs
,
3339 if (s
->get_refcount(refblock
, block_index
) != 1) {
3340 qcow2_signal_corruption(bs
, true, -1, -1, "Invalid refcount:"
3341 " refblock offset %#" PRIx64
3342 ", reftable index %u"
3343 ", block offset %#" PRIx64
3344 ", refcount %#" PRIx64
,
3346 offset_to_reftable_index(s
, discard_block_offs
),
3348 s
->get_refcount(refblock
, block_index
));
3349 qcow2_cache_put(s
->refcount_block_cache
, &refblock
);
3352 s
->set_refcount(refblock
, block_index
, 0);
3354 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
, refblock
);
3356 qcow2_cache_put(s
->refcount_block_cache
, &refblock
);
3358 if (cluster_index
< s
->free_cluster_index
) {
3359 s
->free_cluster_index
= cluster_index
;
3362 refblock
= qcow2_cache_is_table_offset(s
->refcount_block_cache
,
3363 discard_block_offs
);
3365 /* discard refblock from the cache if refblock is cached */
3366 qcow2_cache_discard(s
->refcount_block_cache
, refblock
);
3368 update_refcount_discard(bs
, discard_block_offs
, s
->cluster_size
);
3373 int qcow2_shrink_reftable(BlockDriverState
*bs
)
3375 BDRVQcow2State
*s
= bs
->opaque
;
3376 uint64_t *reftable_tmp
=
3377 g_malloc(s
->refcount_table_size
* sizeof(uint64_t));
3380 for (i
= 0; i
< s
->refcount_table_size
; i
++) {
3381 int64_t refblock_offs
= s
->refcount_table
[i
] & REFT_OFFSET_MASK
;
3385 if (refblock_offs
== 0) {
3386 reftable_tmp
[i
] = 0;
3389 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refblock_offs
,
3395 /* the refblock has own reference */
3396 if (i
== offset_to_reftable_index(s
, refblock_offs
)) {
3397 uint64_t block_index
= (refblock_offs
>> s
->cluster_bits
) &
3398 (s
->refcount_block_size
- 1);
3399 uint64_t refcount
= s
->get_refcount(refblock
, block_index
);
3401 s
->set_refcount(refblock
, block_index
, 0);
3403 unused_block
= buffer_is_zero(refblock
, s
->cluster_size
);
3405 s
->set_refcount(refblock
, block_index
, refcount
);
3407 unused_block
= buffer_is_zero(refblock
, s
->cluster_size
);
3409 qcow2_cache_put(s
->refcount_block_cache
, &refblock
);
3411 reftable_tmp
[i
] = unused_block
? 0 : cpu_to_be64(s
->refcount_table
[i
]);
3414 ret
= bdrv_pwrite_sync(bs
->file
, s
->refcount_table_offset
, reftable_tmp
,
3415 s
->refcount_table_size
* sizeof(uint64_t));
3417 * If the write in the reftable failed the image may contain a partially
3418 * overwritten reftable. In this case it would be better to clear the
3419 * reftable in memory to avoid possible image corruption.
3421 for (i
= 0; i
< s
->refcount_table_size
; i
++) {
3422 if (s
->refcount_table
[i
] && !reftable_tmp
[i
]) {
3424 ret
= qcow2_discard_refcount_block(bs
, s
->refcount_table
[i
] &
3427 s
->refcount_table
[i
] = 0;
3431 if (!s
->cache_discards
) {
3432 qcow2_process_discards(bs
, ret
);
3436 g_free(reftable_tmp
);
3440 int64_t qcow2_get_last_cluster(BlockDriverState
*bs
, int64_t size
)
3442 BDRVQcow2State
*s
= bs
->opaque
;
3445 for (i
= size_to_clusters(s
, size
) - 1; i
>= 0; i
--) {
3447 int ret
= qcow2_get_refcount(bs
, i
, &refcount
);
3449 fprintf(stderr
, "Can't get refcount for cluster %" PRId64
": %s\n",
3457 qcow2_signal_corruption(bs
, true, -1, -1,
3458 "There are no references in the refcount table.");
3462 int qcow2_detect_metadata_preallocation(BlockDriverState
*bs
)
3464 BDRVQcow2State
*s
= bs
->opaque
;
3465 int64_t i
, end_cluster
, cluster_count
= 0, threshold
;
3466 int64_t file_length
, real_allocation
, real_clusters
;
3468 qemu_co_mutex_assert_locked(&s
->lock
);
3470 file_length
= bdrv_getlength(bs
->file
->bs
);
3471 if (file_length
< 0) {
3475 real_allocation
= bdrv_get_allocated_file_size(bs
->file
->bs
);
3476 if (real_allocation
< 0) {
3477 return real_allocation
;
3480 real_clusters
= real_allocation
/ s
->cluster_size
;
3481 threshold
= MAX(real_clusters
* 10 / 9, real_clusters
+ 2);
3483 end_cluster
= size_to_clusters(s
, file_length
);
3484 for (i
= 0; i
< end_cluster
&& cluster_count
< threshold
; i
++) {
3486 int ret
= qcow2_get_refcount(bs
, i
, &refcount
);
3490 cluster_count
+= !!refcount
;
3493 return cluster_count
>= threshold
;