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"
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
,
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(bs
, 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
);
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");
378 fprintf(stderr
, "qcow2: Allocate refcount block %d for %" PRIx64
380 refcount_table_index
, cluster_index
<< s
->cluster_bits
, new_block
);
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
,
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);
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
);
406 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
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
,
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(bs
, s
->refcount_block_cache
, *refcount_block
);
425 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
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
));
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. */
452 qcow2_cache_put(bs
, 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
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
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
) *
486 ret
= qcow2_refcount_area(bs
, meta_offset
, 0, false,
487 refcount_table_index
, new_block
);
492 ret
= load_refcount_block(bs
, new_block
, refcount_block
);
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. */
503 if (*refcount_block
!= NULL
) {
504 qcow2_cache_put(bs
, s
->refcount_block_cache
, refcount_block
);
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
;
533 uint64_t table_offset
, block_offset
, end_offset
;
537 assert(!(start_offset
% s
->cluster_size
));
539 qcow2_refcount_metadata_size(start_offset
/ s
->cluster_size
+
541 s
->cluster_size
, s
->refcount_order
,
542 !exact_size
, &total_refblock_count_u64
);
543 if (total_refblock_count_u64
> QCOW_MAX_REFTABLE_SIZE
) {
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
;
556 table_size
= total_refblock_count
;
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
) {
569 new_table
= g_try_new0(uint64_t, table_size
);
571 assert(table_size
> 0);
572 if (new_table
== NULL
) {
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));
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
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
++) {
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
++) {
610 uint64_t first_offset_covered
;
612 /* Reuse an existing refblock if possible, create a new one otherwise */
614 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, new_table
[i
],
620 ret
= qcow2_cache_get_empty(bs
, s
->refcount_block_cache
,
621 block_offset
, &refblock_data
);
625 memset(refblock_data
, 0, s
->cluster_size
);
626 qcow2_cache_entry_mark_dirty(bs
, s
->refcount_block_cache
,
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
*
636 if (first_offset_covered
< end_offset
) {
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
);
649 end_index
= MIN((end_offset
- first_offset_covered
) /
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(bs
, s
->refcount_block_cache
,
663 qcow2_cache_put(bs
, 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
);
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));
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 */
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
));
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
);
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 */
737 bdrv_pdiscard(bs
->file
->bs
, d
->offset
, d
->bytes
);
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
758 assert(d
->bytes
+ length
== new_end
- new_start
);
759 d
->offset
= new_start
;
760 d
->bytes
= new_end
- new_start
;
765 d
= g_malloc(sizeof(*d
));
766 *d
= (Qcow2DiscardRegion
) {
771 QTAILQ_INSERT_TAIL(&s
->discards
, d
, next
);
774 /* Merge discard requests if they are adjacent now */
775 QTAILQ_FOREACH_SAFE(p
, &s
->discards
, next
, next
) {
777 || p
->offset
> d
->offset
+ d
->bytes
778 || d
->offset
> p
->offset
+ p
->bytes
)
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
;
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
,
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;
811 fprintf(stderr
, "update_refcount: offset=%" PRId64
" size=%" PRId64
812 " addend=%s%" PRIu64
"\n", offset
, length
, decrease
? "-" : "",
817 } else if (length
== 0) {
822 qcow2_cache_set_dependency(bs
, s
->refcount_block_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
)
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(bs
, s
->refcount_block_cache
, &refcount_block
);
841 ret
= alloc_refcount_block(bs
, cluster_index
, &refcount_block
);
846 old_table_index
= table_index
;
848 qcow2_cache_entry_mark_dirty(bs
, s
->refcount_block_cache
,
851 /* we can update the count and save it */
852 block_index
= cluster_index
& (s
->refcount_block_size
- 1);
854 refcount
= s
->get_refcount(refcount_block
, block_index
);
855 if (decrease
? (refcount
- addend
> refcount
)
856 : (refcount
+ addend
< refcount
||
857 refcount
+ addend
> s
->refcount_max
))
867 if (refcount
== 0 && cluster_index
< s
->free_cluster_index
) {
868 s
->free_cluster_index
= cluster_index
;
870 s
->set_refcount(refcount_block
, block_index
, refcount
);
875 table
= qcow2_cache_is_table_offset(bs
, s
->refcount_block_cache
,
878 qcow2_cache_put(bs
, s
->refcount_block_cache
, &refcount_block
);
879 qcow2_cache_discard(bs
, s
->refcount_block_cache
, table
);
882 table
= qcow2_cache_is_table_offset(bs
, s
->l2_table_cache
, offset
);
884 qcow2_cache_discard(bs
, s
->l2_table_cache
, table
);
887 if (s
->discard_passthrough
[type
]) {
888 update_refcount_discard(bs
, cluster_offset
, s
->cluster_size
);
895 if (!s
->cache_discards
) {
896 qcow2_process_discards(bs
, ret
);
899 /* Write last changed block to disk */
900 if (refcount_block
) {
901 qcow2_cache_put(bs
, s
->refcount_block_cache
, &refcount_block
);
905 * Try do undo any updates if an error is returned (This may succeed in
906 * some cases like ENOSPC for allocating a new refcount block)
910 dummy
= update_refcount(bs
, offset
, cluster_offset
- offset
, addend
,
911 !decrease
, QCOW2_DISCARD_NEVER
);
919 * Increases or decreases the refcount of a given cluster.
921 * @addend is the absolute value of the addend; if @decrease is set, @addend
922 * will be subtracted from the current refcount, otherwise it will be added.
924 * On success 0 is returned; on failure -errno is returned.
926 int qcow2_update_cluster_refcount(BlockDriverState
*bs
,
927 int64_t cluster_index
,
928 uint64_t addend
, bool decrease
,
929 enum qcow2_discard_type type
)
931 BDRVQcow2State
*s
= bs
->opaque
;
934 ret
= update_refcount(bs
, cluster_index
<< s
->cluster_bits
, 1, addend
,
945 /*********************************************************/
946 /* cluster allocation functions */
950 /* return < 0 if error */
951 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, uint64_t size
)
953 BDRVQcow2State
*s
= bs
->opaque
;
954 uint64_t i
, nb_clusters
, refcount
;
957 /* We can't allocate clusters if they may still be queued for discard. */
958 if (s
->cache_discards
) {
959 qcow2_process_discards(bs
, 0);
962 nb_clusters
= size_to_clusters(s
, size
);
964 for(i
= 0; i
< nb_clusters
; i
++) {
965 uint64_t next_cluster_index
= s
->free_cluster_index
++;
966 ret
= qcow2_get_refcount(bs
, next_cluster_index
, &refcount
);
970 } else if (refcount
!= 0) {
975 /* Make sure that all offsets in the "allocated" range are representable
977 if (s
->free_cluster_index
> 0 &&
978 s
->free_cluster_index
- 1 > (INT64_MAX
>> s
->cluster_bits
))
984 fprintf(stderr
, "alloc_clusters: size=%" PRId64
" -> %" PRId64
"\n",
986 (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
);
988 return (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
;
991 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, uint64_t size
)
996 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC
);
998 offset
= alloc_clusters_noref(bs
, size
);
1003 ret
= update_refcount(bs
, offset
, size
, 1, false, QCOW2_DISCARD_NEVER
);
1004 } while (ret
== -EAGAIN
);
1013 int64_t qcow2_alloc_clusters_at(BlockDriverState
*bs
, uint64_t offset
,
1014 int64_t nb_clusters
)
1016 BDRVQcow2State
*s
= bs
->opaque
;
1017 uint64_t cluster_index
, refcount
;
1021 assert(nb_clusters
>= 0);
1022 if (nb_clusters
== 0) {
1027 /* Check how many clusters there are free */
1028 cluster_index
= offset
>> s
->cluster_bits
;
1029 for(i
= 0; i
< nb_clusters
; i
++) {
1030 ret
= qcow2_get_refcount(bs
, cluster_index
++, &refcount
);
1033 } else if (refcount
!= 0) {
1038 /* And then allocate them */
1039 ret
= update_refcount(bs
, offset
, i
<< s
->cluster_bits
, 1, false,
1040 QCOW2_DISCARD_NEVER
);
1041 } while (ret
== -EAGAIN
);
1050 /* only used to allocate compressed sectors. We try to allocate
1051 contiguous sectors. size must be <= cluster_size */
1052 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
)
1054 BDRVQcow2State
*s
= bs
->opaque
;
1056 size_t free_in_cluster
;
1059 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC_BYTES
);
1060 assert(size
> 0 && size
<= s
->cluster_size
);
1061 assert(!s
->free_byte_offset
|| offset_into_cluster(s
, s
->free_byte_offset
));
1063 offset
= s
->free_byte_offset
;
1067 ret
= qcow2_get_refcount(bs
, offset
>> s
->cluster_bits
, &refcount
);
1072 if (refcount
== s
->refcount_max
) {
1077 free_in_cluster
= s
->cluster_size
- offset_into_cluster(s
, offset
);
1079 if (!offset
|| free_in_cluster
< size
) {
1080 int64_t new_cluster
= alloc_clusters_noref(bs
, s
->cluster_size
);
1081 if (new_cluster
< 0) {
1085 if (new_cluster
== 0) {
1086 qcow2_signal_corruption(bs
, true, -1, -1, "Preventing invalid "
1087 "allocation of compressed cluster "
1092 if (!offset
|| ROUND_UP(offset
, s
->cluster_size
) != new_cluster
) {
1093 offset
= new_cluster
;
1094 free_in_cluster
= s
->cluster_size
;
1096 free_in_cluster
+= s
->cluster_size
;
1101 ret
= update_refcount(bs
, offset
, size
, 1, false, QCOW2_DISCARD_NEVER
);
1105 } while (ret
== -EAGAIN
);
1110 /* The cluster refcount was incremented; refcount blocks must be flushed
1111 * before the caller's L2 table updates. */
1112 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
, s
->refcount_block_cache
);
1114 s
->free_byte_offset
= offset
+ size
;
1115 if (!offset_into_cluster(s
, s
->free_byte_offset
)) {
1116 s
->free_byte_offset
= 0;
1122 void qcow2_free_clusters(BlockDriverState
*bs
,
1123 int64_t offset
, int64_t size
,
1124 enum qcow2_discard_type type
)
1128 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_FREE
);
1129 ret
= update_refcount(bs
, offset
, size
, 1, true, type
);
1131 fprintf(stderr
, "qcow2_free_clusters failed: %s\n", strerror(-ret
));
1132 /* TODO Remember the clusters to free them later and avoid leaking */
1137 * Free a cluster using its L2 entry (handles clusters of all types, e.g.
1138 * normal cluster, compressed cluster, etc.)
1140 void qcow2_free_any_clusters(BlockDriverState
*bs
, uint64_t l2_entry
,
1141 int nb_clusters
, enum qcow2_discard_type type
)
1143 BDRVQcow2State
*s
= bs
->opaque
;
1145 switch (qcow2_get_cluster_type(l2_entry
)) {
1146 case QCOW2_CLUSTER_COMPRESSED
:
1149 nb_csectors
= ((l2_entry
>> s
->csize_shift
) &
1151 qcow2_free_clusters(bs
,
1152 (l2_entry
& s
->cluster_offset_mask
) & ~511,
1153 nb_csectors
* 512, type
);
1156 case QCOW2_CLUSTER_NORMAL
:
1157 case QCOW2_CLUSTER_ZERO_ALLOC
:
1158 if (offset_into_cluster(s
, l2_entry
& L2E_OFFSET_MASK
)) {
1159 qcow2_signal_corruption(bs
, false, -1, -1,
1160 "Cannot free unaligned cluster %#llx",
1161 l2_entry
& L2E_OFFSET_MASK
);
1163 qcow2_free_clusters(bs
, l2_entry
& L2E_OFFSET_MASK
,
1164 nb_clusters
<< s
->cluster_bits
, type
);
1167 case QCOW2_CLUSTER_ZERO_PLAIN
:
1168 case QCOW2_CLUSTER_UNALLOCATED
:
1177 /*********************************************************/
1178 /* snapshots and image creation */
1182 /* update the refcounts of snapshots and the copied flag */
1183 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
1184 int64_t l1_table_offset
, int l1_size
, int addend
)
1186 BDRVQcow2State
*s
= bs
->opaque
;
1187 uint64_t *l1_table
, *l2_table
, l2_offset
, entry
, l1_size2
, refcount
;
1188 bool l1_allocated
= false;
1189 int64_t old_entry
, old_l2_offset
;
1190 int i
, j
, l1_modified
= 0, nb_csectors
;
1193 assert(addend
>= -1 && addend
<= 1);
1197 l1_size2
= l1_size
* sizeof(uint64_t);
1199 s
->cache_discards
= true;
1201 /* WARNING: qcow2_snapshot_goto relies on this function not using the
1202 * l1_table_offset when it is the current s->l1_table_offset! Be careful
1203 * when changing this! */
1204 if (l1_table_offset
!= s
->l1_table_offset
) {
1205 l1_table
= g_try_malloc0(align_offset(l1_size2
, 512));
1206 if (l1_size2
&& l1_table
== NULL
) {
1210 l1_allocated
= true;
1212 ret
= bdrv_pread(bs
->file
, l1_table_offset
, l1_table
, l1_size2
);
1217 for (i
= 0; i
< l1_size
; i
++) {
1218 be64_to_cpus(&l1_table
[i
]);
1221 assert(l1_size
== s
->l1_size
);
1222 l1_table
= s
->l1_table
;
1223 l1_allocated
= false;
1226 for (i
= 0; i
< l1_size
; i
++) {
1227 l2_offset
= l1_table
[i
];
1229 old_l2_offset
= l2_offset
;
1230 l2_offset
&= L1E_OFFSET_MASK
;
1232 if (offset_into_cluster(s
, l2_offset
)) {
1233 qcow2_signal_corruption(bs
, true, -1, -1, "L2 table offset %#"
1234 PRIx64
" unaligned (L1 index: %#x)",
1240 ret
= qcow2_cache_get(bs
, s
->l2_table_cache
, l2_offset
,
1241 (void**) &l2_table
);
1246 for (j
= 0; j
< s
->l2_size
; j
++) {
1247 uint64_t cluster_index
;
1250 entry
= be64_to_cpu(l2_table
[j
]);
1252 entry
&= ~QCOW_OFLAG_COPIED
;
1253 offset
= entry
& L2E_OFFSET_MASK
;
1255 switch (qcow2_get_cluster_type(entry
)) {
1256 case QCOW2_CLUSTER_COMPRESSED
:
1257 nb_csectors
= ((entry
>> s
->csize_shift
) &
1260 ret
= update_refcount(bs
,
1261 (entry
& s
->cluster_offset_mask
) & ~511,
1262 nb_csectors
* 512, abs(addend
), addend
< 0,
1263 QCOW2_DISCARD_SNAPSHOT
);
1268 /* compressed clusters are never modified */
1272 case QCOW2_CLUSTER_NORMAL
:
1273 case QCOW2_CLUSTER_ZERO_ALLOC
:
1274 if (offset_into_cluster(s
, offset
)) {
1275 qcow2_signal_corruption(bs
, true, -1, -1, "Cluster "
1276 "allocation offset %#" PRIx64
1277 " unaligned (L2 offset: %#"
1278 PRIx64
", L2 index: %#x)",
1279 offset
, l2_offset
, j
);
1284 cluster_index
= offset
>> s
->cluster_bits
;
1285 assert(cluster_index
);
1287 ret
= qcow2_update_cluster_refcount(bs
,
1288 cluster_index
, abs(addend
), addend
< 0,
1289 QCOW2_DISCARD_SNAPSHOT
);
1295 ret
= qcow2_get_refcount(bs
, cluster_index
, &refcount
);
1301 case QCOW2_CLUSTER_ZERO_PLAIN
:
1302 case QCOW2_CLUSTER_UNALLOCATED
:
1310 if (refcount
== 1) {
1311 entry
|= QCOW_OFLAG_COPIED
;
1313 if (entry
!= old_entry
) {
1315 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
,
1316 s
->refcount_block_cache
);
1318 l2_table
[j
] = cpu_to_be64(entry
);
1319 qcow2_cache_entry_mark_dirty(bs
, s
->l2_table_cache
,
1324 qcow2_cache_put(bs
, s
->l2_table_cache
, (void **) &l2_table
);
1327 ret
= qcow2_update_cluster_refcount(bs
, l2_offset
>>
1329 abs(addend
), addend
< 0,
1330 QCOW2_DISCARD_SNAPSHOT
);
1335 ret
= qcow2_get_refcount(bs
, l2_offset
>> s
->cluster_bits
,
1339 } else if (refcount
== 1) {
1340 l2_offset
|= QCOW_OFLAG_COPIED
;
1342 if (l2_offset
!= old_l2_offset
) {
1343 l1_table
[i
] = l2_offset
;
1349 ret
= bdrv_flush(bs
);
1352 qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
1355 s
->cache_discards
= false;
1356 qcow2_process_discards(bs
, ret
);
1358 /* Update L1 only if it isn't deleted anyway (addend = -1) */
1359 if (ret
== 0 && addend
>= 0 && l1_modified
) {
1360 for (i
= 0; i
< l1_size
; i
++) {
1361 cpu_to_be64s(&l1_table
[i
]);
1364 ret
= bdrv_pwrite_sync(bs
->file
, l1_table_offset
,
1365 l1_table
, l1_size2
);
1367 for (i
= 0; i
< l1_size
; i
++) {
1368 be64_to_cpus(&l1_table
[i
]);
1379 /*********************************************************/
1380 /* refcount checking functions */
1383 static uint64_t refcount_array_byte_size(BDRVQcow2State
*s
, uint64_t entries
)
1385 /* This assertion holds because there is no way we can address more than
1386 * 2^(64 - 9) clusters at once (with cluster size 512 = 2^9, and because
1387 * offsets have to be representable in bytes); due to every cluster
1388 * corresponding to one refcount entry, we are well below that limit */
1389 assert(entries
< (UINT64_C(1) << (64 - 9)));
1391 /* Thanks to the assertion this will not overflow, because
1392 * s->refcount_order < 7.
1393 * (note: x << s->refcount_order == x * s->refcount_bits) */
1394 return DIV_ROUND_UP(entries
<< s
->refcount_order
, 8);
1398 * Reallocates *array so that it can hold new_size entries. *size must contain
1399 * the current number of entries in *array. If the reallocation fails, *array
1400 * and *size will not be modified and -errno will be returned. If the
1401 * reallocation is successful, *array will be set to the new buffer, *size
1402 * will be set to new_size and 0 will be returned. The size of the reallocated
1403 * refcount array buffer will be aligned to a cluster boundary, and the newly
1404 * allocated area will be zeroed.
1406 static int realloc_refcount_array(BDRVQcow2State
*s
, void **array
,
1407 int64_t *size
, int64_t new_size
)
1409 int64_t old_byte_size
, new_byte_size
;
1412 /* Round to clusters so the array can be directly written to disk */
1413 old_byte_size
= size_to_clusters(s
, refcount_array_byte_size(s
, *size
))
1415 new_byte_size
= size_to_clusters(s
, refcount_array_byte_size(s
, new_size
))
1418 if (new_byte_size
== old_byte_size
) {
1423 assert(new_byte_size
> 0);
1425 if (new_byte_size
> SIZE_MAX
) {
1429 new_ptr
= g_try_realloc(*array
, new_byte_size
);
1434 if (new_byte_size
> old_byte_size
) {
1435 memset((char *)new_ptr
+ old_byte_size
, 0,
1436 new_byte_size
- old_byte_size
);
1446 * Increases the refcount for a range of clusters in a given refcount table.
1447 * This is used to construct a temporary refcount table out of L1 and L2 tables
1448 * which can be compared to the refcount table saved in the image.
1450 * Modifies the number of errors in res.
1452 int qcow2_inc_refcounts_imrt(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1453 void **refcount_table
,
1454 int64_t *refcount_table_size
,
1455 int64_t offset
, int64_t size
)
1457 BDRVQcow2State
*s
= bs
->opaque
;
1458 uint64_t start
, last
, cluster_offset
, k
, refcount
;
1465 start
= start_of_cluster(s
, offset
);
1466 last
= start_of_cluster(s
, offset
+ size
- 1);
1467 for(cluster_offset
= start
; cluster_offset
<= last
;
1468 cluster_offset
+= s
->cluster_size
) {
1469 k
= cluster_offset
>> s
->cluster_bits
;
1470 if (k
>= *refcount_table_size
) {
1471 ret
= realloc_refcount_array(s
, refcount_table
,
1472 refcount_table_size
, k
+ 1);
1474 res
->check_errors
++;
1479 refcount
= s
->get_refcount(*refcount_table
, k
);
1480 if (refcount
== s
->refcount_max
) {
1481 fprintf(stderr
, "ERROR: overflow cluster offset=0x%" PRIx64
1482 "\n", cluster_offset
);
1483 fprintf(stderr
, "Use qemu-img amend to increase the refcount entry "
1484 "width or qemu-img convert to create a clean copy if the "
1485 "image cannot be opened for writing\n");
1489 s
->set_refcount(*refcount_table
, k
, refcount
+ 1);
1495 /* Flags for check_refcounts_l1() and check_refcounts_l2() */
1497 CHECK_FRAG_INFO
= 0x2, /* update BlockFragInfo counters */
1501 * Increases the refcount in the given refcount table for the all clusters
1502 * referenced in the L2 table. While doing so, performs some checks on L2
1505 * Returns the number of errors found by the checks or -errno if an internal
1508 static int check_refcounts_l2(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1509 void **refcount_table
,
1510 int64_t *refcount_table_size
, int64_t l2_offset
,
1513 BDRVQcow2State
*s
= bs
->opaque
;
1514 uint64_t *l2_table
, l2_entry
;
1515 uint64_t next_contiguous_offset
= 0;
1516 int i
, l2_size
, nb_csectors
, ret
;
1518 /* Read L2 table from disk */
1519 l2_size
= s
->l2_size
* sizeof(uint64_t);
1520 l2_table
= g_malloc(l2_size
);
1522 ret
= bdrv_pread(bs
->file
, l2_offset
, l2_table
, l2_size
);
1524 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l2\n");
1525 res
->check_errors
++;
1529 /* Do the actual checks */
1530 for(i
= 0; i
< s
->l2_size
; i
++) {
1531 l2_entry
= be64_to_cpu(l2_table
[i
]);
1533 switch (qcow2_get_cluster_type(l2_entry
)) {
1534 case QCOW2_CLUSTER_COMPRESSED
:
1535 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
1536 if (l2_entry
& QCOW_OFLAG_COPIED
) {
1537 fprintf(stderr
, "ERROR: cluster %" PRId64
": "
1538 "copied flag must never be set for compressed "
1539 "clusters\n", l2_entry
>> s
->cluster_bits
);
1540 l2_entry
&= ~QCOW_OFLAG_COPIED
;
1544 /* Mark cluster as used */
1545 nb_csectors
= ((l2_entry
>> s
->csize_shift
) &
1547 l2_entry
&= s
->cluster_offset_mask
;
1548 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
1549 refcount_table
, refcount_table_size
,
1550 l2_entry
& ~511, nb_csectors
* 512);
1555 if (flags
& CHECK_FRAG_INFO
) {
1556 res
->bfi
.allocated_clusters
++;
1557 res
->bfi
.compressed_clusters
++;
1559 /* Compressed clusters are fragmented by nature. Since they
1560 * take up sub-sector space but we only have sector granularity
1561 * I/O we need to re-read the same sectors even for adjacent
1562 * compressed clusters.
1564 res
->bfi
.fragmented_clusters
++;
1568 case QCOW2_CLUSTER_ZERO_ALLOC
:
1569 case QCOW2_CLUSTER_NORMAL
:
1571 uint64_t offset
= l2_entry
& L2E_OFFSET_MASK
;
1573 if (flags
& CHECK_FRAG_INFO
) {
1574 res
->bfi
.allocated_clusters
++;
1575 if (next_contiguous_offset
&&
1576 offset
!= next_contiguous_offset
) {
1577 res
->bfi
.fragmented_clusters
++;
1579 next_contiguous_offset
= offset
+ s
->cluster_size
;
1582 /* Mark cluster as used */
1583 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
1584 refcount_table
, refcount_table_size
,
1585 offset
, s
->cluster_size
);
1590 /* Correct offsets are cluster aligned */
1591 if (offset_into_cluster(s
, offset
)) {
1592 fprintf(stderr
, "ERROR offset=%" PRIx64
": Cluster is not "
1593 "properly aligned; L2 entry corrupted.\n", offset
);
1599 case QCOW2_CLUSTER_ZERO_PLAIN
:
1600 case QCOW2_CLUSTER_UNALLOCATED
:
1617 * Increases the refcount for the L1 table, its L2 tables and all referenced
1618 * clusters in the given refcount table. While doing so, performs some checks
1619 * on L1 and L2 entries.
1621 * Returns the number of errors found by the checks or -errno if an internal
1624 static int check_refcounts_l1(BlockDriverState
*bs
,
1625 BdrvCheckResult
*res
,
1626 void **refcount_table
,
1627 int64_t *refcount_table_size
,
1628 int64_t l1_table_offset
, int l1_size
,
1631 BDRVQcow2State
*s
= bs
->opaque
;
1632 uint64_t *l1_table
= NULL
, l2_offset
, l1_size2
;
1635 l1_size2
= l1_size
* sizeof(uint64_t);
1637 /* Mark L1 table as used */
1638 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, refcount_table_size
,
1639 l1_table_offset
, l1_size2
);
1644 /* Read L1 table entries from disk */
1646 l1_table
= g_try_malloc(l1_size2
);
1647 if (l1_table
== NULL
) {
1649 res
->check_errors
++;
1652 ret
= bdrv_pread(bs
->file
, l1_table_offset
, l1_table
, l1_size2
);
1654 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
1655 res
->check_errors
++;
1658 for(i
= 0;i
< l1_size
; i
++)
1659 be64_to_cpus(&l1_table
[i
]);
1662 /* Do the actual checks */
1663 for(i
= 0; i
< l1_size
; i
++) {
1664 l2_offset
= l1_table
[i
];
1666 /* Mark L2 table as used */
1667 l2_offset
&= L1E_OFFSET_MASK
;
1668 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
1669 refcount_table
, refcount_table_size
,
1670 l2_offset
, s
->cluster_size
);
1675 /* L2 tables are cluster aligned */
1676 if (offset_into_cluster(s
, l2_offset
)) {
1677 fprintf(stderr
, "ERROR l2_offset=%" PRIx64
": Table is not "
1678 "cluster aligned; L1 entry corrupted\n", l2_offset
);
1682 /* Process and check L2 entries */
1683 ret
= check_refcounts_l2(bs
, res
, refcount_table
,
1684 refcount_table_size
, l2_offset
, flags
);
1699 * Checks the OFLAG_COPIED flag for all L1 and L2 entries.
1701 * This function does not print an error message nor does it increment
1702 * check_errors if qcow2_get_refcount fails (this is because such an error will
1703 * have been already detected and sufficiently signaled by the calling function
1704 * (qcow2_check_refcounts) by the time this function is called).
1706 static int check_oflag_copied(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1709 BDRVQcow2State
*s
= bs
->opaque
;
1710 uint64_t *l2_table
= qemu_blockalign(bs
, s
->cluster_size
);
1715 for (i
= 0; i
< s
->l1_size
; i
++) {
1716 uint64_t l1_entry
= s
->l1_table
[i
];
1717 uint64_t l2_offset
= l1_entry
& L1E_OFFSET_MASK
;
1718 bool l2_dirty
= false;
1724 ret
= qcow2_get_refcount(bs
, l2_offset
>> s
->cluster_bits
,
1727 /* don't print message nor increment check_errors */
1730 if ((refcount
== 1) != ((l1_entry
& QCOW_OFLAG_COPIED
) != 0)) {
1731 fprintf(stderr
, "%s OFLAG_COPIED L2 cluster: l1_index=%d "
1732 "l1_entry=%" PRIx64
" refcount=%" PRIu64
"\n",
1733 fix
& BDRV_FIX_ERRORS
? "Repairing" :
1735 i
, l1_entry
, refcount
);
1736 if (fix
& BDRV_FIX_ERRORS
) {
1737 s
->l1_table
[i
] = refcount
== 1
1738 ? l1_entry
| QCOW_OFLAG_COPIED
1739 : l1_entry
& ~QCOW_OFLAG_COPIED
;
1740 ret
= qcow2_write_l1_entry(bs
, i
);
1742 res
->check_errors
++;
1745 res
->corruptions_fixed
++;
1751 ret
= bdrv_pread(bs
->file
, l2_offset
, l2_table
,
1752 s
->l2_size
* sizeof(uint64_t));
1754 fprintf(stderr
, "ERROR: Could not read L2 table: %s\n",
1756 res
->check_errors
++;
1760 for (j
= 0; j
< s
->l2_size
; j
++) {
1761 uint64_t l2_entry
= be64_to_cpu(l2_table
[j
]);
1762 uint64_t data_offset
= l2_entry
& L2E_OFFSET_MASK
;
1763 QCow2ClusterType cluster_type
= qcow2_get_cluster_type(l2_entry
);
1765 if (cluster_type
== QCOW2_CLUSTER_NORMAL
||
1766 cluster_type
== QCOW2_CLUSTER_ZERO_ALLOC
) {
1767 ret
= qcow2_get_refcount(bs
,
1768 data_offset
>> s
->cluster_bits
,
1771 /* don't print message nor increment check_errors */
1774 if ((refcount
== 1) != ((l2_entry
& QCOW_OFLAG_COPIED
) != 0)) {
1775 fprintf(stderr
, "%s OFLAG_COPIED data cluster: "
1776 "l2_entry=%" PRIx64
" refcount=%" PRIu64
"\n",
1777 fix
& BDRV_FIX_ERRORS
? "Repairing" :
1779 l2_entry
, refcount
);
1780 if (fix
& BDRV_FIX_ERRORS
) {
1781 l2_table
[j
] = cpu_to_be64(refcount
== 1
1782 ? l2_entry
| QCOW_OFLAG_COPIED
1783 : l2_entry
& ~QCOW_OFLAG_COPIED
);
1785 res
->corruptions_fixed
++;
1794 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L2
,
1795 l2_offset
, s
->cluster_size
);
1797 fprintf(stderr
, "ERROR: Could not write L2 table; metadata "
1798 "overlap check failed: %s\n", strerror(-ret
));
1799 res
->check_errors
++;
1803 ret
= bdrv_pwrite(bs
->file
, l2_offset
, l2_table
,
1806 fprintf(stderr
, "ERROR: Could not write L2 table: %s\n",
1808 res
->check_errors
++;
1817 qemu_vfree(l2_table
);
1822 * Checks consistency of refblocks and accounts for each refblock in
1825 static int check_refblocks(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1826 BdrvCheckMode fix
, bool *rebuild
,
1827 void **refcount_table
, int64_t *nb_clusters
)
1829 BDRVQcow2State
*s
= bs
->opaque
;
1833 for(i
= 0; i
< s
->refcount_table_size
; i
++) {
1834 uint64_t offset
, cluster
;
1835 offset
= s
->refcount_table
[i
];
1836 cluster
= offset
>> s
->cluster_bits
;
1838 /* Refcount blocks are cluster aligned */
1839 if (offset_into_cluster(s
, offset
)) {
1840 fprintf(stderr
, "ERROR refcount block %" PRId64
" is not "
1841 "cluster aligned; refcount table entry corrupted\n", i
);
1847 if (cluster
>= *nb_clusters
) {
1848 fprintf(stderr
, "%s refcount block %" PRId64
" is outside image\n",
1849 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR", i
);
1851 if (fix
& BDRV_FIX_ERRORS
) {
1852 int64_t new_nb_clusters
;
1853 Error
*local_err
= NULL
;
1855 if (offset
> INT64_MAX
- s
->cluster_size
) {
1860 ret
= bdrv_truncate(bs
->file
, offset
+ s
->cluster_size
,
1861 PREALLOC_MODE_OFF
, &local_err
);
1863 error_report_err(local_err
);
1866 size
= bdrv_getlength(bs
->file
->bs
);
1872 new_nb_clusters
= size_to_clusters(s
, size
);
1873 assert(new_nb_clusters
>= *nb_clusters
);
1875 ret
= realloc_refcount_array(s
, refcount_table
,
1876 nb_clusters
, new_nb_clusters
);
1878 res
->check_errors
++;
1882 if (cluster
>= *nb_clusters
) {
1887 res
->corruptions_fixed
++;
1888 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
1889 refcount_table
, nb_clusters
,
1890 offset
, s
->cluster_size
);
1894 /* No need to check whether the refcount is now greater than 1:
1895 * This area was just allocated and zeroed, so it can only be
1896 * exactly 1 after qcow2_inc_refcounts_imrt() */
1902 fprintf(stderr
, "ERROR could not resize image: %s\n",
1911 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, nb_clusters
,
1912 offset
, s
->cluster_size
);
1916 if (s
->get_refcount(*refcount_table
, cluster
) != 1) {
1917 fprintf(stderr
, "ERROR refcount block %" PRId64
1918 " refcount=%" PRIu64
"\n", i
,
1919 s
->get_refcount(*refcount_table
, cluster
));
1930 * Calculates an in-memory refcount table.
1932 static int calculate_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1933 BdrvCheckMode fix
, bool *rebuild
,
1934 void **refcount_table
, int64_t *nb_clusters
)
1936 BDRVQcow2State
*s
= bs
->opaque
;
1941 if (!*refcount_table
) {
1942 int64_t old_size
= 0;
1943 ret
= realloc_refcount_array(s
, refcount_table
,
1944 &old_size
, *nb_clusters
);
1946 res
->check_errors
++;
1952 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, nb_clusters
,
1953 0, s
->cluster_size
);
1958 /* current L1 table */
1959 ret
= check_refcounts_l1(bs
, res
, refcount_table
, nb_clusters
,
1960 s
->l1_table_offset
, s
->l1_size
, CHECK_FRAG_INFO
);
1966 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
1967 sn
= s
->snapshots
+ i
;
1968 ret
= check_refcounts_l1(bs
, res
, refcount_table
, nb_clusters
,
1969 sn
->l1_table_offset
, sn
->l1_size
, 0);
1974 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, nb_clusters
,
1975 s
->snapshots_offset
, s
->snapshots_size
);
1981 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, nb_clusters
,
1982 s
->refcount_table_offset
,
1983 s
->refcount_table_size
* sizeof(uint64_t));
1989 if (s
->crypto_header
.length
) {
1990 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, nb_clusters
,
1991 s
->crypto_header
.offset
,
1992 s
->crypto_header
.length
);
1999 ret
= qcow2_check_bitmaps_refcounts(bs
, res
, refcount_table
, nb_clusters
);
2004 return check_refblocks(bs
, res
, fix
, rebuild
, refcount_table
, nb_clusters
);
2008 * Compares the actual reference count for each cluster in the image against the
2009 * refcount as reported by the refcount structures on-disk.
2011 static void compare_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
2012 BdrvCheckMode fix
, bool *rebuild
,
2013 int64_t *highest_cluster
,
2014 void *refcount_table
, int64_t nb_clusters
)
2016 BDRVQcow2State
*s
= bs
->opaque
;
2018 uint64_t refcount1
, refcount2
;
2021 for (i
= 0, *highest_cluster
= 0; i
< nb_clusters
; i
++) {
2022 ret
= qcow2_get_refcount(bs
, i
, &refcount1
);
2024 fprintf(stderr
, "Can't get refcount for cluster %" PRId64
": %s\n",
2026 res
->check_errors
++;
2030 refcount2
= s
->get_refcount(refcount_table
, i
);
2032 if (refcount1
> 0 || refcount2
> 0) {
2033 *highest_cluster
= i
;
2036 if (refcount1
!= refcount2
) {
2037 /* Check if we're allowed to fix the mismatch */
2038 int *num_fixed
= NULL
;
2039 if (refcount1
== 0) {
2041 } else if (refcount1
> refcount2
&& (fix
& BDRV_FIX_LEAKS
)) {
2042 num_fixed
= &res
->leaks_fixed
;
2043 } else if (refcount1
< refcount2
&& (fix
& BDRV_FIX_ERRORS
)) {
2044 num_fixed
= &res
->corruptions_fixed
;
2047 fprintf(stderr
, "%s cluster %" PRId64
" refcount=%" PRIu64
2048 " reference=%" PRIu64
"\n",
2049 num_fixed
!= NULL
? "Repairing" :
2050 refcount1
< refcount2
? "ERROR" :
2052 i
, refcount1
, refcount2
);
2055 ret
= update_refcount(bs
, i
<< s
->cluster_bits
, 1,
2056 refcount_diff(refcount1
, refcount2
),
2057 refcount1
> refcount2
,
2058 QCOW2_DISCARD_ALWAYS
);
2065 /* And if we couldn't, print an error */
2066 if (refcount1
< refcount2
) {
2076 * Allocates clusters using an in-memory refcount table (IMRT) in contrast to
2077 * the on-disk refcount structures.
2079 * On input, *first_free_cluster tells where to start looking, and need not
2080 * actually be a free cluster; the returned offset will not be before that
2081 * cluster. On output, *first_free_cluster points to the first gap found, even
2082 * if that gap was too small to be used as the returned offset.
2084 * Note that *first_free_cluster is a cluster index whereas the return value is
2087 static int64_t alloc_clusters_imrt(BlockDriverState
*bs
,
2089 void **refcount_table
,
2090 int64_t *imrt_nb_clusters
,
2091 int64_t *first_free_cluster
)
2093 BDRVQcow2State
*s
= bs
->opaque
;
2094 int64_t cluster
= *first_free_cluster
, i
;
2095 bool first_gap
= true;
2096 int contiguous_free_clusters
;
2099 /* Starting at *first_free_cluster, find a range of at least cluster_count
2100 * continuously free clusters */
2101 for (contiguous_free_clusters
= 0;
2102 cluster
< *imrt_nb_clusters
&&
2103 contiguous_free_clusters
< cluster_count
;
2106 if (!s
->get_refcount(*refcount_table
, cluster
)) {
2107 contiguous_free_clusters
++;
2109 /* If this is the first free cluster found, update
2110 * *first_free_cluster accordingly */
2111 *first_free_cluster
= cluster
;
2114 } else if (contiguous_free_clusters
) {
2115 contiguous_free_clusters
= 0;
2119 /* If contiguous_free_clusters is greater than zero, it contains the number
2120 * of continuously free clusters until the current cluster; the first free
2121 * cluster in the current "gap" is therefore
2122 * cluster - contiguous_free_clusters */
2124 /* If no such range could be found, grow the in-memory refcount table
2125 * accordingly to append free clusters at the end of the image */
2126 if (contiguous_free_clusters
< cluster_count
) {
2127 /* contiguous_free_clusters clusters are already empty at the image end;
2128 * we need cluster_count clusters; therefore, we have to allocate
2129 * cluster_count - contiguous_free_clusters new clusters at the end of
2130 * the image (which is the current value of cluster; note that cluster
2131 * may exceed old_imrt_nb_clusters if *first_free_cluster pointed beyond
2133 ret
= realloc_refcount_array(s
, refcount_table
, imrt_nb_clusters
,
2134 cluster
+ cluster_count
2135 - contiguous_free_clusters
);
2141 /* Go back to the first free cluster */
2142 cluster
-= contiguous_free_clusters
;
2143 for (i
= 0; i
< cluster_count
; i
++) {
2144 s
->set_refcount(*refcount_table
, cluster
+ i
, 1);
2147 return cluster
<< s
->cluster_bits
;
2151 * Creates a new refcount structure based solely on the in-memory information
2152 * given through *refcount_table. All necessary allocations will be reflected
2155 * On success, the old refcount structure is leaked (it will be covered by the
2156 * new refcount structure).
2158 static int rebuild_refcount_structure(BlockDriverState
*bs
,
2159 BdrvCheckResult
*res
,
2160 void **refcount_table
,
2161 int64_t *nb_clusters
)
2163 BDRVQcow2State
*s
= bs
->opaque
;
2164 int64_t first_free_cluster
= 0, reftable_offset
= -1, cluster
= 0;
2165 int64_t refblock_offset
, refblock_start
, refblock_index
;
2166 uint32_t reftable_size
= 0;
2167 uint64_t *on_disk_reftable
= NULL
;
2168 void *on_disk_refblock
;
2171 uint64_t reftable_offset
;
2172 uint32_t reftable_clusters
;
2173 } QEMU_PACKED reftable_offset_and_clusters
;
2175 qcow2_cache_empty(bs
, s
->refcount_block_cache
);
2178 for (; cluster
< *nb_clusters
; cluster
++) {
2179 if (!s
->get_refcount(*refcount_table
, cluster
)) {
2183 refblock_index
= cluster
>> s
->refcount_block_bits
;
2184 refblock_start
= refblock_index
<< s
->refcount_block_bits
;
2186 /* Don't allocate a cluster in a refblock already written to disk */
2187 if (first_free_cluster
< refblock_start
) {
2188 first_free_cluster
= refblock_start
;
2190 refblock_offset
= alloc_clusters_imrt(bs
, 1, refcount_table
,
2191 nb_clusters
, &first_free_cluster
);
2192 if (refblock_offset
< 0) {
2193 fprintf(stderr
, "ERROR allocating refblock: %s\n",
2194 strerror(-refblock_offset
));
2195 res
->check_errors
++;
2196 ret
= refblock_offset
;
2200 if (reftable_size
<= refblock_index
) {
2201 uint32_t old_reftable_size
= reftable_size
;
2202 uint64_t *new_on_disk_reftable
;
2204 reftable_size
= ROUND_UP((refblock_index
+ 1) * sizeof(uint64_t),
2205 s
->cluster_size
) / sizeof(uint64_t);
2206 new_on_disk_reftable
= g_try_realloc(on_disk_reftable
,
2209 if (!new_on_disk_reftable
) {
2210 res
->check_errors
++;
2214 on_disk_reftable
= new_on_disk_reftable
;
2216 memset(on_disk_reftable
+ old_reftable_size
, 0,
2217 (reftable_size
- old_reftable_size
) * sizeof(uint64_t));
2219 /* The offset we have for the reftable is now no longer valid;
2220 * this will leak that range, but we can easily fix that by running
2221 * a leak-fixing check after this rebuild operation */
2222 reftable_offset
= -1;
2224 assert(on_disk_reftable
);
2226 on_disk_reftable
[refblock_index
] = refblock_offset
;
2228 /* If this is apparently the last refblock (for now), try to squeeze the
2230 if (refblock_index
== (*nb_clusters
- 1) >> s
->refcount_block_bits
&&
2231 reftable_offset
< 0)
2233 uint64_t reftable_clusters
= size_to_clusters(s
, reftable_size
*
2235 reftable_offset
= alloc_clusters_imrt(bs
, reftable_clusters
,
2236 refcount_table
, nb_clusters
,
2237 &first_free_cluster
);
2238 if (reftable_offset
< 0) {
2239 fprintf(stderr
, "ERROR allocating reftable: %s\n",
2240 strerror(-reftable_offset
));
2241 res
->check_errors
++;
2242 ret
= reftable_offset
;
2247 ret
= qcow2_pre_write_overlap_check(bs
, 0, refblock_offset
,
2250 fprintf(stderr
, "ERROR writing refblock: %s\n", strerror(-ret
));
2254 /* The size of *refcount_table is always cluster-aligned, therefore the
2255 * write operation will not overflow */
2256 on_disk_refblock
= (void *)((char *) *refcount_table
+
2257 refblock_index
* s
->cluster_size
);
2259 ret
= bdrv_write(bs
->file
, refblock_offset
/ BDRV_SECTOR_SIZE
,
2260 on_disk_refblock
, s
->cluster_sectors
);
2262 fprintf(stderr
, "ERROR writing refblock: %s\n", strerror(-ret
));
2266 /* Go to the end of this refblock */
2267 cluster
= refblock_start
+ s
->refcount_block_size
- 1;
2270 if (reftable_offset
< 0) {
2271 uint64_t post_refblock_start
, reftable_clusters
;
2273 post_refblock_start
= ROUND_UP(*nb_clusters
, s
->refcount_block_size
);
2274 reftable_clusters
= size_to_clusters(s
,
2275 reftable_size
* sizeof(uint64_t));
2276 /* Not pretty but simple */
2277 if (first_free_cluster
< post_refblock_start
) {
2278 first_free_cluster
= post_refblock_start
;
2280 reftable_offset
= alloc_clusters_imrt(bs
, reftable_clusters
,
2281 refcount_table
, nb_clusters
,
2282 &first_free_cluster
);
2283 if (reftable_offset
< 0) {
2284 fprintf(stderr
, "ERROR allocating reftable: %s\n",
2285 strerror(-reftable_offset
));
2286 res
->check_errors
++;
2287 ret
= reftable_offset
;
2291 goto write_refblocks
;
2294 for (refblock_index
= 0; refblock_index
< reftable_size
; refblock_index
++) {
2295 cpu_to_be64s(&on_disk_reftable
[refblock_index
]);
2298 ret
= qcow2_pre_write_overlap_check(bs
, 0, reftable_offset
,
2299 reftable_size
* sizeof(uint64_t));
2301 fprintf(stderr
, "ERROR writing reftable: %s\n", strerror(-ret
));
2305 assert(reftable_size
< INT_MAX
/ sizeof(uint64_t));
2306 ret
= bdrv_pwrite(bs
->file
, reftable_offset
, on_disk_reftable
,
2307 reftable_size
* sizeof(uint64_t));
2309 fprintf(stderr
, "ERROR writing reftable: %s\n", strerror(-ret
));
2313 /* Enter new reftable into the image header */
2314 reftable_offset_and_clusters
.reftable_offset
= cpu_to_be64(reftable_offset
);
2315 reftable_offset_and_clusters
.reftable_clusters
=
2316 cpu_to_be32(size_to_clusters(s
, reftable_size
* sizeof(uint64_t)));
2317 ret
= bdrv_pwrite_sync(bs
->file
,
2318 offsetof(QCowHeader
, refcount_table_offset
),
2319 &reftable_offset_and_clusters
,
2320 sizeof(reftable_offset_and_clusters
));
2322 fprintf(stderr
, "ERROR setting reftable: %s\n", strerror(-ret
));
2326 for (refblock_index
= 0; refblock_index
< reftable_size
; refblock_index
++) {
2327 be64_to_cpus(&on_disk_reftable
[refblock_index
]);
2329 s
->refcount_table
= on_disk_reftable
;
2330 s
->refcount_table_offset
= reftable_offset
;
2331 s
->refcount_table_size
= reftable_size
;
2332 update_max_refcount_table_index(s
);
2337 g_free(on_disk_reftable
);
2342 * Checks an image for refcount consistency.
2344 * Returns 0 if no errors are found, the number of errors in case the image is
2345 * detected as corrupted, and -errno when an internal error occurred.
2347 int qcow2_check_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
2350 BDRVQcow2State
*s
= bs
->opaque
;
2351 BdrvCheckResult pre_compare_res
;
2352 int64_t size
, highest_cluster
, nb_clusters
;
2353 void *refcount_table
= NULL
;
2354 bool rebuild
= false;
2357 size
= bdrv_getlength(bs
->file
->bs
);
2359 res
->check_errors
++;
2363 nb_clusters
= size_to_clusters(s
, size
);
2364 if (nb_clusters
> INT_MAX
) {
2365 res
->check_errors
++;
2369 res
->bfi
.total_clusters
=
2370 size_to_clusters(s
, bs
->total_sectors
* BDRV_SECTOR_SIZE
);
2372 ret
= calculate_refcounts(bs
, res
, fix
, &rebuild
, &refcount_table
,
2378 /* In case we don't need to rebuild the refcount structure (but want to fix
2379 * something), this function is immediately called again, in which case the
2380 * result should be ignored */
2381 pre_compare_res
= *res
;
2382 compare_refcounts(bs
, res
, 0, &rebuild
, &highest_cluster
, refcount_table
,
2385 if (rebuild
&& (fix
& BDRV_FIX_ERRORS
)) {
2386 BdrvCheckResult old_res
= *res
;
2387 int fresh_leaks
= 0;
2389 fprintf(stderr
, "Rebuilding refcount structure\n");
2390 ret
= rebuild_refcount_structure(bs
, res
, &refcount_table
,
2396 res
->corruptions
= 0;
2399 /* Because the old reftable has been exchanged for a new one the
2400 * references have to be recalculated */
2402 memset(refcount_table
, 0, refcount_array_byte_size(s
, nb_clusters
));
2403 ret
= calculate_refcounts(bs
, res
, 0, &rebuild
, &refcount_table
,
2409 if (fix
& BDRV_FIX_LEAKS
) {
2410 /* The old refcount structures are now leaked, fix it; the result
2411 * can be ignored, aside from leaks which were introduced by
2412 * rebuild_refcount_structure() that could not be fixed */
2413 BdrvCheckResult saved_res
= *res
;
2414 *res
= (BdrvCheckResult
){ 0 };
2416 compare_refcounts(bs
, res
, BDRV_FIX_LEAKS
, &rebuild
,
2417 &highest_cluster
, refcount_table
, nb_clusters
);
2419 fprintf(stderr
, "ERROR rebuilt refcount structure is still "
2423 /* Any leaks accounted for here were introduced by
2424 * rebuild_refcount_structure() because that function has created a
2425 * new refcount structure from scratch */
2426 fresh_leaks
= res
->leaks
;
2430 if (res
->corruptions
< old_res
.corruptions
) {
2431 res
->corruptions_fixed
+= old_res
.corruptions
- res
->corruptions
;
2433 if (res
->leaks
< old_res
.leaks
) {
2434 res
->leaks_fixed
+= old_res
.leaks
- res
->leaks
;
2436 res
->leaks
+= fresh_leaks
;
2439 fprintf(stderr
, "ERROR need to rebuild refcount structures\n");
2440 res
->check_errors
++;
2445 if (res
->leaks
|| res
->corruptions
) {
2446 *res
= pre_compare_res
;
2447 compare_refcounts(bs
, res
, fix
, &rebuild
, &highest_cluster
,
2448 refcount_table
, nb_clusters
);
2452 /* check OFLAG_COPIED */
2453 ret
= check_oflag_copied(bs
, res
, fix
);
2458 res
->image_end_offset
= (highest_cluster
+ 1) * s
->cluster_size
;
2462 g_free(refcount_table
);
2467 #define overlaps_with(ofs, sz) \
2468 ranges_overlap(offset, size, ofs, sz)
2471 * Checks if the given offset into the image file is actually free to use by
2472 * looking for overlaps with important metadata sections (L1/L2 tables etc.),
2473 * i.e. a sanity check without relying on the refcount tables.
2475 * The ign parameter specifies what checks not to perform (being a bitmask of
2476 * QCow2MetadataOverlap values), i.e., what sections to ignore.
2479 * - 0 if writing to this offset will not affect the mentioned metadata
2480 * - a positive QCow2MetadataOverlap value indicating one overlapping section
2481 * - a negative value (-errno) indicating an error while performing a check,
2482 * e.g. when bdrv_read failed on QCOW2_OL_INACTIVE_L2
2484 int qcow2_check_metadata_overlap(BlockDriverState
*bs
, int ign
, int64_t offset
,
2487 BDRVQcow2State
*s
= bs
->opaque
;
2488 int chk
= s
->overlap_check
& ~ign
;
2495 if (chk
& QCOW2_OL_MAIN_HEADER
) {
2496 if (offset
< s
->cluster_size
) {
2497 return QCOW2_OL_MAIN_HEADER
;
2501 /* align range to test to cluster boundaries */
2502 size
= align_offset(offset_into_cluster(s
, offset
) + size
, s
->cluster_size
);
2503 offset
= start_of_cluster(s
, offset
);
2505 if ((chk
& QCOW2_OL_ACTIVE_L1
) && s
->l1_size
) {
2506 if (overlaps_with(s
->l1_table_offset
, s
->l1_size
* sizeof(uint64_t))) {
2507 return QCOW2_OL_ACTIVE_L1
;
2511 if ((chk
& QCOW2_OL_REFCOUNT_TABLE
) && s
->refcount_table_size
) {
2512 if (overlaps_with(s
->refcount_table_offset
,
2513 s
->refcount_table_size
* sizeof(uint64_t))) {
2514 return QCOW2_OL_REFCOUNT_TABLE
;
2518 if ((chk
& QCOW2_OL_SNAPSHOT_TABLE
) && s
->snapshots_size
) {
2519 if (overlaps_with(s
->snapshots_offset
, s
->snapshots_size
)) {
2520 return QCOW2_OL_SNAPSHOT_TABLE
;
2524 if ((chk
& QCOW2_OL_INACTIVE_L1
) && s
->snapshots
) {
2525 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
2526 if (s
->snapshots
[i
].l1_size
&&
2527 overlaps_with(s
->snapshots
[i
].l1_table_offset
,
2528 s
->snapshots
[i
].l1_size
* sizeof(uint64_t))) {
2529 return QCOW2_OL_INACTIVE_L1
;
2534 if ((chk
& QCOW2_OL_ACTIVE_L2
) && s
->l1_table
) {
2535 for (i
= 0; i
< s
->l1_size
; i
++) {
2536 if ((s
->l1_table
[i
] & L1E_OFFSET_MASK
) &&
2537 overlaps_with(s
->l1_table
[i
] & L1E_OFFSET_MASK
,
2539 return QCOW2_OL_ACTIVE_L2
;
2544 if ((chk
& QCOW2_OL_REFCOUNT_BLOCK
) && s
->refcount_table
) {
2545 unsigned last_entry
= s
->max_refcount_table_index
;
2546 assert(last_entry
< s
->refcount_table_size
);
2547 assert(last_entry
+ 1 == s
->refcount_table_size
||
2548 (s
->refcount_table
[last_entry
+ 1] & REFT_OFFSET_MASK
) == 0);
2549 for (i
= 0; i
<= last_entry
; i
++) {
2550 if ((s
->refcount_table
[i
] & REFT_OFFSET_MASK
) &&
2551 overlaps_with(s
->refcount_table
[i
] & REFT_OFFSET_MASK
,
2553 return QCOW2_OL_REFCOUNT_BLOCK
;
2558 if ((chk
& QCOW2_OL_INACTIVE_L2
) && s
->snapshots
) {
2559 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
2560 uint64_t l1_ofs
= s
->snapshots
[i
].l1_table_offset
;
2561 uint32_t l1_sz
= s
->snapshots
[i
].l1_size
;
2562 uint64_t l1_sz2
= l1_sz
* sizeof(uint64_t);
2563 uint64_t *l1
= g_try_malloc(l1_sz2
);
2566 if (l1_sz2
&& l1
== NULL
) {
2570 ret
= bdrv_pread(bs
->file
, l1_ofs
, l1
, l1_sz2
);
2576 for (j
= 0; j
< l1_sz
; j
++) {
2577 uint64_t l2_ofs
= be64_to_cpu(l1
[j
]) & L1E_OFFSET_MASK
;
2578 if (l2_ofs
&& overlaps_with(l2_ofs
, s
->cluster_size
)) {
2580 return QCOW2_OL_INACTIVE_L2
;
2591 static const char *metadata_ol_names
[] = {
2592 [QCOW2_OL_MAIN_HEADER_BITNR
] = "qcow2_header",
2593 [QCOW2_OL_ACTIVE_L1_BITNR
] = "active L1 table",
2594 [QCOW2_OL_ACTIVE_L2_BITNR
] = "active L2 table",
2595 [QCOW2_OL_REFCOUNT_TABLE_BITNR
] = "refcount table",
2596 [QCOW2_OL_REFCOUNT_BLOCK_BITNR
] = "refcount block",
2597 [QCOW2_OL_SNAPSHOT_TABLE_BITNR
] = "snapshot table",
2598 [QCOW2_OL_INACTIVE_L1_BITNR
] = "inactive L1 table",
2599 [QCOW2_OL_INACTIVE_L2_BITNR
] = "inactive L2 table",
2603 * First performs a check for metadata overlaps (through
2604 * qcow2_check_metadata_overlap); if that fails with a negative value (error
2605 * while performing a check), that value is returned. If an impending overlap
2606 * is detected, the BDS will be made unusable, the qcow2 file marked corrupt
2607 * and -EIO returned.
2609 * Returns 0 if there were neither overlaps nor errors while checking for
2610 * overlaps; or a negative value (-errno) on error.
2612 int qcow2_pre_write_overlap_check(BlockDriverState
*bs
, int ign
, int64_t offset
,
2615 int ret
= qcow2_check_metadata_overlap(bs
, ign
, offset
, size
);
2619 } else if (ret
> 0) {
2620 int metadata_ol_bitnr
= ctz32(ret
);
2621 assert(metadata_ol_bitnr
< QCOW2_OL_MAX_BITNR
);
2623 qcow2_signal_corruption(bs
, true, offset
, size
, "Preventing invalid "
2624 "write on metadata (overlaps with %s)",
2625 metadata_ol_names
[metadata_ol_bitnr
]);
2632 /* A pointer to a function of this type is given to walk_over_reftable(). That
2633 * function will create refblocks and pass them to a RefblockFinishOp once they
2634 * are completed (@refblock). @refblock_empty is set if the refblock is
2637 * Along with the refblock, a corresponding reftable entry is passed, in the
2638 * reftable @reftable (which may be reallocated) at @reftable_index.
2640 * @allocated should be set to true if a new cluster has been allocated.
2642 typedef int (RefblockFinishOp
)(BlockDriverState
*bs
, uint64_t **reftable
,
2643 uint64_t reftable_index
, uint64_t *reftable_size
,
2644 void *refblock
, bool refblock_empty
,
2645 bool *allocated
, Error
**errp
);
2648 * This "operation" for walk_over_reftable() allocates the refblock on disk (if
2649 * it is not empty) and inserts its offset into the new reftable. The size of
2650 * this new reftable is increased as required.
2652 static int alloc_refblock(BlockDriverState
*bs
, uint64_t **reftable
,
2653 uint64_t reftable_index
, uint64_t *reftable_size
,
2654 void *refblock
, bool refblock_empty
, bool *allocated
,
2657 BDRVQcow2State
*s
= bs
->opaque
;
2660 if (!refblock_empty
&& reftable_index
>= *reftable_size
) {
2661 uint64_t *new_reftable
;
2662 uint64_t new_reftable_size
;
2664 new_reftable_size
= ROUND_UP(reftable_index
+ 1,
2665 s
->cluster_size
/ sizeof(uint64_t));
2666 if (new_reftable_size
> QCOW_MAX_REFTABLE_SIZE
/ sizeof(uint64_t)) {
2668 "This operation would make the refcount table grow "
2669 "beyond the maximum size supported by QEMU, aborting");
2673 new_reftable
= g_try_realloc(*reftable
, new_reftable_size
*
2675 if (!new_reftable
) {
2676 error_setg(errp
, "Failed to increase reftable buffer size");
2680 memset(new_reftable
+ *reftable_size
, 0,
2681 (new_reftable_size
- *reftable_size
) * sizeof(uint64_t));
2683 *reftable
= new_reftable
;
2684 *reftable_size
= new_reftable_size
;
2687 if (!refblock_empty
&& !(*reftable
)[reftable_index
]) {
2688 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
2690 error_setg_errno(errp
, -offset
, "Failed to allocate refblock");
2693 (*reftable
)[reftable_index
] = offset
;
2701 * This "operation" for walk_over_reftable() writes the refblock to disk at the
2702 * offset specified by the new reftable's entry. It does not modify the new
2703 * reftable or change any refcounts.
2705 static int flush_refblock(BlockDriverState
*bs
, uint64_t **reftable
,
2706 uint64_t reftable_index
, uint64_t *reftable_size
,
2707 void *refblock
, bool refblock_empty
, bool *allocated
,
2710 BDRVQcow2State
*s
= bs
->opaque
;
2714 if (reftable_index
< *reftable_size
&& (*reftable
)[reftable_index
]) {
2715 offset
= (*reftable
)[reftable_index
];
2717 ret
= qcow2_pre_write_overlap_check(bs
, 0, offset
, s
->cluster_size
);
2719 error_setg_errno(errp
, -ret
, "Overlap check failed");
2723 ret
= bdrv_pwrite(bs
->file
, offset
, refblock
, s
->cluster_size
);
2725 error_setg_errno(errp
, -ret
, "Failed to write refblock");
2729 assert(refblock_empty
);
2736 * This function walks over the existing reftable and every referenced refblock;
2737 * if @new_set_refcount is non-NULL, it is called for every refcount entry to
2738 * create an equal new entry in the passed @new_refblock. Once that
2739 * @new_refblock is completely filled, @operation will be called.
2741 * @status_cb and @cb_opaque are used for the amend operation's status callback.
2742 * @index is the index of the walk_over_reftable() calls and @total is the total
2743 * number of walk_over_reftable() calls per amend operation. Both are used for
2744 * calculating the parameters for the status callback.
2746 * @allocated is set to true if a new cluster has been allocated.
2748 static int walk_over_reftable(BlockDriverState
*bs
, uint64_t **new_reftable
,
2749 uint64_t *new_reftable_index
,
2750 uint64_t *new_reftable_size
,
2751 void *new_refblock
, int new_refblock_size
,
2752 int new_refcount_bits
,
2753 RefblockFinishOp
*operation
, bool *allocated
,
2754 Qcow2SetRefcountFunc
*new_set_refcount
,
2755 BlockDriverAmendStatusCB
*status_cb
,
2756 void *cb_opaque
, int index
, int total
,
2759 BDRVQcow2State
*s
= bs
->opaque
;
2760 uint64_t reftable_index
;
2761 bool new_refblock_empty
= true;
2763 int new_refblock_index
= 0;
2766 for (reftable_index
= 0; reftable_index
< s
->refcount_table_size
;
2769 uint64_t refblock_offset
= s
->refcount_table
[reftable_index
]
2772 status_cb(bs
, (uint64_t)index
* s
->refcount_table_size
+ reftable_index
,
2773 (uint64_t)total
* s
->refcount_table_size
, cb_opaque
);
2775 if (refblock_offset
) {
2778 if (offset_into_cluster(s
, refblock_offset
)) {
2779 qcow2_signal_corruption(bs
, true, -1, -1, "Refblock offset %#"
2780 PRIx64
" unaligned (reftable index: %#"
2781 PRIx64
")", refblock_offset
,
2784 "Image is corrupt (unaligned refblock offset)");
2788 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refblock_offset
,
2791 error_setg_errno(errp
, -ret
, "Failed to retrieve refblock");
2795 for (refblock_index
= 0; refblock_index
< s
->refcount_block_size
;
2800 if (new_refblock_index
>= new_refblock_size
) {
2801 /* new_refblock is now complete */
2802 ret
= operation(bs
, new_reftable
, *new_reftable_index
,
2803 new_reftable_size
, new_refblock
,
2804 new_refblock_empty
, allocated
, errp
);
2806 qcow2_cache_put(bs
, s
->refcount_block_cache
, &refblock
);
2810 (*new_reftable_index
)++;
2811 new_refblock_index
= 0;
2812 new_refblock_empty
= true;
2815 refcount
= s
->get_refcount(refblock
, refblock_index
);
2816 if (new_refcount_bits
< 64 && refcount
>> new_refcount_bits
) {
2819 qcow2_cache_put(bs
, s
->refcount_block_cache
, &refblock
);
2821 offset
= ((reftable_index
<< s
->refcount_block_bits
)
2822 + refblock_index
) << s
->cluster_bits
;
2824 error_setg(errp
, "Cannot decrease refcount entry width to "
2825 "%i bits: Cluster at offset %#" PRIx64
" has a "
2826 "refcount of %" PRIu64
, new_refcount_bits
,
2831 if (new_set_refcount
) {
2832 new_set_refcount(new_refblock
, new_refblock_index
++,
2835 new_refblock_index
++;
2837 new_refblock_empty
= new_refblock_empty
&& refcount
== 0;
2840 qcow2_cache_put(bs
, s
->refcount_block_cache
, &refblock
);
2842 /* No refblock means every refcount is 0 */
2843 for (refblock_index
= 0; refblock_index
< s
->refcount_block_size
;
2846 if (new_refblock_index
>= new_refblock_size
) {
2847 /* new_refblock is now complete */
2848 ret
= operation(bs
, new_reftable
, *new_reftable_index
,
2849 new_reftable_size
, new_refblock
,
2850 new_refblock_empty
, allocated
, errp
);
2855 (*new_reftable_index
)++;
2856 new_refblock_index
= 0;
2857 new_refblock_empty
= true;
2860 if (new_set_refcount
) {
2861 new_set_refcount(new_refblock
, new_refblock_index
++, 0);
2863 new_refblock_index
++;
2869 if (new_refblock_index
> 0) {
2870 /* Complete the potentially existing partially filled final refblock */
2871 if (new_set_refcount
) {
2872 for (; new_refblock_index
< new_refblock_size
;
2873 new_refblock_index
++)
2875 new_set_refcount(new_refblock
, new_refblock_index
, 0);
2879 ret
= operation(bs
, new_reftable
, *new_reftable_index
,
2880 new_reftable_size
, new_refblock
, new_refblock_empty
,
2886 (*new_reftable_index
)++;
2889 status_cb(bs
, (uint64_t)(index
+ 1) * s
->refcount_table_size
,
2890 (uint64_t)total
* s
->refcount_table_size
, cb_opaque
);
2895 int qcow2_change_refcount_order(BlockDriverState
*bs
, int refcount_order
,
2896 BlockDriverAmendStatusCB
*status_cb
,
2897 void *cb_opaque
, Error
**errp
)
2899 BDRVQcow2State
*s
= bs
->opaque
;
2900 Qcow2GetRefcountFunc
*new_get_refcount
;
2901 Qcow2SetRefcountFunc
*new_set_refcount
;
2902 void *new_refblock
= qemu_blockalign(bs
->file
->bs
, s
->cluster_size
);
2903 uint64_t *new_reftable
= NULL
, new_reftable_size
= 0;
2904 uint64_t *old_reftable
, old_reftable_size
, old_reftable_offset
;
2905 uint64_t new_reftable_index
= 0;
2907 int64_t new_reftable_offset
= 0, allocated_reftable_size
= 0;
2908 int new_refblock_size
, new_refcount_bits
= 1 << refcount_order
;
2909 int old_refcount_order
;
2912 bool new_allocation
;
2914 assert(s
->qcow_version
>= 3);
2915 assert(refcount_order
>= 0 && refcount_order
<= 6);
2917 /* see qcow2_open() */
2918 new_refblock_size
= 1 << (s
->cluster_bits
- (refcount_order
- 3));
2920 new_get_refcount
= get_refcount_funcs
[refcount_order
];
2921 new_set_refcount
= set_refcount_funcs
[refcount_order
];
2927 new_allocation
= false;
2929 /* At least we have to do this walk and the one which writes the
2930 * refblocks; also, at least we have to do this loop here at least
2931 * twice (normally), first to do the allocations, and second to
2932 * determine that everything is correctly allocated, this then makes
2933 * three walks in total */
2934 total_walks
= MAX(walk_index
+ 2, 3);
2936 /* First, allocate the structures so they are present in the refcount
2938 ret
= walk_over_reftable(bs
, &new_reftable
, &new_reftable_index
,
2939 &new_reftable_size
, NULL
, new_refblock_size
,
2940 new_refcount_bits
, &alloc_refblock
,
2941 &new_allocation
, NULL
, status_cb
, cb_opaque
,
2942 walk_index
++, total_walks
, errp
);
2947 new_reftable_index
= 0;
2949 if (new_allocation
) {
2950 if (new_reftable_offset
) {
2951 qcow2_free_clusters(bs
, new_reftable_offset
,
2952 allocated_reftable_size
* sizeof(uint64_t),
2953 QCOW2_DISCARD_NEVER
);
2956 new_reftable_offset
= qcow2_alloc_clusters(bs
, new_reftable_size
*
2958 if (new_reftable_offset
< 0) {
2959 error_setg_errno(errp
, -new_reftable_offset
,
2960 "Failed to allocate the new reftable");
2961 ret
= new_reftable_offset
;
2964 allocated_reftable_size
= new_reftable_size
;
2966 } while (new_allocation
);
2968 /* Second, write the new refblocks */
2969 ret
= walk_over_reftable(bs
, &new_reftable
, &new_reftable_index
,
2970 &new_reftable_size
, new_refblock
,
2971 new_refblock_size
, new_refcount_bits
,
2972 &flush_refblock
, &new_allocation
, new_set_refcount
,
2973 status_cb
, cb_opaque
, walk_index
, walk_index
+ 1,
2978 assert(!new_allocation
);
2981 /* Write the new reftable */
2982 ret
= qcow2_pre_write_overlap_check(bs
, 0, new_reftable_offset
,
2983 new_reftable_size
* sizeof(uint64_t));
2985 error_setg_errno(errp
, -ret
, "Overlap check failed");
2989 for (i
= 0; i
< new_reftable_size
; i
++) {
2990 cpu_to_be64s(&new_reftable
[i
]);
2993 ret
= bdrv_pwrite(bs
->file
, new_reftable_offset
, new_reftable
,
2994 new_reftable_size
* sizeof(uint64_t));
2996 for (i
= 0; i
< new_reftable_size
; i
++) {
2997 be64_to_cpus(&new_reftable
[i
]);
3001 error_setg_errno(errp
, -ret
, "Failed to write the new reftable");
3006 /* Empty the refcount cache */
3007 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
3009 error_setg_errno(errp
, -ret
, "Failed to flush the refblock cache");
3013 /* Update the image header to point to the new reftable; this only updates
3014 * the fields which are relevant to qcow2_update_header(); other fields
3015 * such as s->refcount_table or s->refcount_bits stay stale for now
3016 * (because we have to restore everything if qcow2_update_header() fails) */
3017 old_refcount_order
= s
->refcount_order
;
3018 old_reftable_size
= s
->refcount_table_size
;
3019 old_reftable_offset
= s
->refcount_table_offset
;
3021 s
->refcount_order
= refcount_order
;
3022 s
->refcount_table_size
= new_reftable_size
;
3023 s
->refcount_table_offset
= new_reftable_offset
;
3025 ret
= qcow2_update_header(bs
);
3027 s
->refcount_order
= old_refcount_order
;
3028 s
->refcount_table_size
= old_reftable_size
;
3029 s
->refcount_table_offset
= old_reftable_offset
;
3030 error_setg_errno(errp
, -ret
, "Failed to update the qcow2 header");
3034 /* Now update the rest of the in-memory information */
3035 old_reftable
= s
->refcount_table
;
3036 s
->refcount_table
= new_reftable
;
3037 update_max_refcount_table_index(s
);
3039 s
->refcount_bits
= 1 << refcount_order
;
3040 s
->refcount_max
= UINT64_C(1) << (s
->refcount_bits
- 1);
3041 s
->refcount_max
+= s
->refcount_max
- 1;
3043 s
->refcount_block_bits
= s
->cluster_bits
- (refcount_order
- 3);
3044 s
->refcount_block_size
= 1 << s
->refcount_block_bits
;
3046 s
->get_refcount
= new_get_refcount
;
3047 s
->set_refcount
= new_set_refcount
;
3049 /* For cleaning up all old refblocks and the old reftable below the "done"
3051 new_reftable
= old_reftable
;
3052 new_reftable_size
= old_reftable_size
;
3053 new_reftable_offset
= old_reftable_offset
;
3057 /* On success, new_reftable actually points to the old reftable (and
3058 * new_reftable_size is the old reftable's size); but that is just
3060 for (i
= 0; i
< new_reftable_size
; i
++) {
3061 uint64_t offset
= new_reftable
[i
] & REFT_OFFSET_MASK
;
3063 qcow2_free_clusters(bs
, offset
, s
->cluster_size
,
3064 QCOW2_DISCARD_OTHER
);
3067 g_free(new_reftable
);
3069 if (new_reftable_offset
> 0) {
3070 qcow2_free_clusters(bs
, new_reftable_offset
,
3071 new_reftable_size
* sizeof(uint64_t),
3072 QCOW2_DISCARD_OTHER
);
3076 qemu_vfree(new_refblock
);
3080 static int64_t get_refblock_offset(BlockDriverState
*bs
, uint64_t offset
)
3082 BDRVQcow2State
*s
= bs
->opaque
;
3083 uint32_t index
= offset_to_reftable_index(s
, offset
);
3084 int64_t covering_refblock_offset
= 0;
3086 if (index
< s
->refcount_table_size
) {
3087 covering_refblock_offset
= s
->refcount_table
[index
] & REFT_OFFSET_MASK
;
3089 if (!covering_refblock_offset
) {
3090 qcow2_signal_corruption(bs
, true, -1, -1, "Refblock at %#" PRIx64
" is "
3091 "not covered by the refcount structures",
3096 return covering_refblock_offset
;
3099 static int qcow2_discard_refcount_block(BlockDriverState
*bs
,
3100 uint64_t discard_block_offs
)
3102 BDRVQcow2State
*s
= bs
->opaque
;
3103 int64_t refblock_offs
;
3104 uint64_t cluster_index
= discard_block_offs
>> s
->cluster_bits
;
3105 uint32_t block_index
= cluster_index
& (s
->refcount_block_size
- 1);
3109 refblock_offs
= get_refblock_offset(bs
, discard_block_offs
);
3110 if (refblock_offs
< 0) {
3111 return refblock_offs
;
3114 assert(discard_block_offs
!= 0);
3116 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refblock_offs
,
3122 if (s
->get_refcount(refblock
, block_index
) != 1) {
3123 qcow2_signal_corruption(bs
, true, -1, -1, "Invalid refcount:"
3124 " refblock offset %#" PRIx64
3125 ", reftable index %u"
3126 ", block offset %#" PRIx64
3127 ", refcount %#" PRIx64
,
3129 offset_to_reftable_index(s
, discard_block_offs
),
3131 s
->get_refcount(refblock
, block_index
));
3132 qcow2_cache_put(bs
, s
->refcount_block_cache
, &refblock
);
3135 s
->set_refcount(refblock
, block_index
, 0);
3137 qcow2_cache_entry_mark_dirty(bs
, s
->refcount_block_cache
, refblock
);
3139 qcow2_cache_put(bs
, s
->refcount_block_cache
, &refblock
);
3141 if (cluster_index
< s
->free_cluster_index
) {
3142 s
->free_cluster_index
= cluster_index
;
3145 refblock
= qcow2_cache_is_table_offset(bs
, s
->refcount_block_cache
,
3146 discard_block_offs
);
3148 /* discard refblock from the cache if refblock is cached */
3149 qcow2_cache_discard(bs
, s
->refcount_block_cache
, refblock
);
3151 update_refcount_discard(bs
, discard_block_offs
, s
->cluster_size
);
3156 int qcow2_shrink_reftable(BlockDriverState
*bs
)
3158 BDRVQcow2State
*s
= bs
->opaque
;
3159 uint64_t *reftable_tmp
=
3160 g_malloc(s
->refcount_table_size
* sizeof(uint64_t));
3163 for (i
= 0; i
< s
->refcount_table_size
; i
++) {
3164 int64_t refblock_offs
= s
->refcount_table
[i
] & REFT_OFFSET_MASK
;
3168 if (refblock_offs
== 0) {
3169 reftable_tmp
[i
] = 0;
3172 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refblock_offs
,
3178 /* the refblock has own reference */
3179 if (i
== offset_to_reftable_index(s
, refblock_offs
)) {
3180 uint64_t block_index
= (refblock_offs
>> s
->cluster_bits
) &
3181 (s
->refcount_block_size
- 1);
3182 uint64_t refcount
= s
->get_refcount(refblock
, block_index
);
3184 s
->set_refcount(refblock
, block_index
, 0);
3186 unused_block
= buffer_is_zero(refblock
, s
->cluster_size
);
3188 s
->set_refcount(refblock
, block_index
, refcount
);
3190 unused_block
= buffer_is_zero(refblock
, s
->cluster_size
);
3192 qcow2_cache_put(bs
, s
->refcount_block_cache
, &refblock
);
3194 reftable_tmp
[i
] = unused_block
? 0 : cpu_to_be64(s
->refcount_table
[i
]);
3197 ret
= bdrv_pwrite_sync(bs
->file
, s
->refcount_table_offset
, reftable_tmp
,
3198 s
->refcount_table_size
* sizeof(uint64_t));
3200 * If the write in the reftable failed the image may contain a partially
3201 * overwritten reftable. In this case it would be better to clear the
3202 * reftable in memory to avoid possible image corruption.
3204 for (i
= 0; i
< s
->refcount_table_size
; i
++) {
3205 if (s
->refcount_table
[i
] && !reftable_tmp
[i
]) {
3207 ret
= qcow2_discard_refcount_block(bs
, s
->refcount_table
[i
] &
3210 s
->refcount_table
[i
] = 0;
3214 if (!s
->cache_discards
) {
3215 qcow2_process_discards(bs
, ret
);
3219 g_free(reftable_tmp
);
3223 int64_t qcow2_get_last_cluster(BlockDriverState
*bs
, int64_t size
)
3225 BDRVQcow2State
*s
= bs
->opaque
;
3228 for (i
= size_to_clusters(s
, size
) - 1; i
>= 0; i
--) {
3230 int ret
= qcow2_get_refcount(bs
, i
, &refcount
);
3232 fprintf(stderr
, "Can't get refcount for cluster %" PRId64
": %s\n",
3240 qcow2_signal_corruption(bs
, true, -1, -1,
3241 "There are no references in the refcount table.");