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"
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
,
36 static int QEMU_WARN_UNUSED_RESULT
update_refcount(BlockDriverState
*bs
,
37 int64_t offset
, int64_t length
, uint64_t addend
,
38 bool decrease
, enum qcow2_discard_type type
);
40 static uint64_t get_refcount_ro0(const void *refcount_array
, uint64_t index
);
41 static uint64_t get_refcount_ro1(const void *refcount_array
, uint64_t index
);
42 static uint64_t get_refcount_ro2(const void *refcount_array
, uint64_t index
);
43 static uint64_t get_refcount_ro3(const void *refcount_array
, uint64_t index
);
44 static uint64_t get_refcount_ro4(const void *refcount_array
, uint64_t index
);
45 static uint64_t get_refcount_ro5(const void *refcount_array
, uint64_t index
);
46 static uint64_t get_refcount_ro6(const void *refcount_array
, uint64_t index
);
48 static void set_refcount_ro0(void *refcount_array
, uint64_t index
,
50 static void set_refcount_ro1(void *refcount_array
, uint64_t index
,
52 static void set_refcount_ro2(void *refcount_array
, uint64_t index
,
54 static void set_refcount_ro3(void *refcount_array
, uint64_t index
,
56 static void set_refcount_ro4(void *refcount_array
, uint64_t index
,
58 static void set_refcount_ro5(void *refcount_array
, uint64_t index
,
60 static void set_refcount_ro6(void *refcount_array
, uint64_t index
,
64 static Qcow2GetRefcountFunc
*const get_refcount_funcs
[] = {
74 static Qcow2SetRefcountFunc
*const set_refcount_funcs
[] = {
85 /*********************************************************/
86 /* refcount handling */
88 static void update_max_refcount_table_index(BDRVQcow2State
*s
)
90 unsigned i
= s
->refcount_table_size
- 1;
91 while (i
> 0 && (s
->refcount_table
[i
] & REFT_OFFSET_MASK
) == 0) {
94 /* Set s->max_refcount_table_index to the index of the last used entry */
95 s
->max_refcount_table_index
= i
;
98 int qcow2_refcount_init(BlockDriverState
*bs
)
100 BDRVQcow2State
*s
= bs
->opaque
;
101 unsigned int refcount_table_size2
, i
;
104 assert(s
->refcount_order
>= 0 && s
->refcount_order
<= 6);
106 s
->get_refcount
= get_refcount_funcs
[s
->refcount_order
];
107 s
->set_refcount
= set_refcount_funcs
[s
->refcount_order
];
109 assert(s
->refcount_table_size
<= INT_MAX
/ sizeof(uint64_t));
110 refcount_table_size2
= s
->refcount_table_size
* sizeof(uint64_t);
111 s
->refcount_table
= g_try_malloc(refcount_table_size2
);
113 if (s
->refcount_table_size
> 0) {
114 if (s
->refcount_table
== NULL
) {
118 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_LOAD
);
119 ret
= bdrv_pread(bs
->file
, s
->refcount_table_offset
,
120 s
->refcount_table
, refcount_table_size2
);
124 for(i
= 0; i
< s
->refcount_table_size
; i
++)
125 be64_to_cpus(&s
->refcount_table
[i
]);
126 update_max_refcount_table_index(s
);
133 void qcow2_refcount_close(BlockDriverState
*bs
)
135 BDRVQcow2State
*s
= bs
->opaque
;
136 g_free(s
->refcount_table
);
140 static uint64_t get_refcount_ro0(const void *refcount_array
, uint64_t index
)
142 return (((const uint8_t *)refcount_array
)[index
/ 8] >> (index
% 8)) & 0x1;
145 static void set_refcount_ro0(void *refcount_array
, uint64_t index
,
148 assert(!(value
>> 1));
149 ((uint8_t *)refcount_array
)[index
/ 8] &= ~(0x1 << (index
% 8));
150 ((uint8_t *)refcount_array
)[index
/ 8] |= value
<< (index
% 8);
153 static uint64_t get_refcount_ro1(const void *refcount_array
, uint64_t index
)
155 return (((const uint8_t *)refcount_array
)[index
/ 4] >> (2 * (index
% 4)))
159 static void set_refcount_ro1(void *refcount_array
, uint64_t index
,
162 assert(!(value
>> 2));
163 ((uint8_t *)refcount_array
)[index
/ 4] &= ~(0x3 << (2 * (index
% 4)));
164 ((uint8_t *)refcount_array
)[index
/ 4] |= value
<< (2 * (index
% 4));
167 static uint64_t get_refcount_ro2(const void *refcount_array
, uint64_t index
)
169 return (((const uint8_t *)refcount_array
)[index
/ 2] >> (4 * (index
% 2)))
173 static void set_refcount_ro2(void *refcount_array
, uint64_t index
,
176 assert(!(value
>> 4));
177 ((uint8_t *)refcount_array
)[index
/ 2] &= ~(0xf << (4 * (index
% 2)));
178 ((uint8_t *)refcount_array
)[index
/ 2] |= value
<< (4 * (index
% 2));
181 static uint64_t get_refcount_ro3(const void *refcount_array
, uint64_t index
)
183 return ((const uint8_t *)refcount_array
)[index
];
186 static void set_refcount_ro3(void *refcount_array
, uint64_t index
,
189 assert(!(value
>> 8));
190 ((uint8_t *)refcount_array
)[index
] = value
;
193 static uint64_t get_refcount_ro4(const void *refcount_array
, uint64_t index
)
195 return be16_to_cpu(((const uint16_t *)refcount_array
)[index
]);
198 static void set_refcount_ro4(void *refcount_array
, uint64_t index
,
201 assert(!(value
>> 16));
202 ((uint16_t *)refcount_array
)[index
] = cpu_to_be16(value
);
205 static uint64_t get_refcount_ro5(const void *refcount_array
, uint64_t index
)
207 return be32_to_cpu(((const uint32_t *)refcount_array
)[index
]);
210 static void set_refcount_ro5(void *refcount_array
, uint64_t index
,
213 assert(!(value
>> 32));
214 ((uint32_t *)refcount_array
)[index
] = cpu_to_be32(value
);
217 static uint64_t get_refcount_ro6(const void *refcount_array
, uint64_t index
)
219 return be64_to_cpu(((const uint64_t *)refcount_array
)[index
]);
222 static void set_refcount_ro6(void *refcount_array
, uint64_t index
,
225 ((uint64_t *)refcount_array
)[index
] = cpu_to_be64(value
);
229 static int load_refcount_block(BlockDriverState
*bs
,
230 int64_t refcount_block_offset
,
231 void **refcount_block
)
233 BDRVQcow2State
*s
= bs
->opaque
;
235 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_LOAD
);
236 return qcow2_cache_get(bs
, s
->refcount_block_cache
, refcount_block_offset
,
241 * Retrieves the refcount of the cluster given by its index and stores it in
242 * *refcount. Returns 0 on success and -errno on failure.
244 int qcow2_get_refcount(BlockDriverState
*bs
, int64_t cluster_index
,
247 BDRVQcow2State
*s
= bs
->opaque
;
248 uint64_t refcount_table_index
, block_index
;
249 int64_t refcount_block_offset
;
251 void *refcount_block
;
253 refcount_table_index
= cluster_index
>> s
->refcount_block_bits
;
254 if (refcount_table_index
>= s
->refcount_table_size
) {
258 refcount_block_offset
=
259 s
->refcount_table
[refcount_table_index
] & REFT_OFFSET_MASK
;
260 if (!refcount_block_offset
) {
265 if (offset_into_cluster(s
, refcount_block_offset
)) {
266 qcow2_signal_corruption(bs
, true, -1, -1, "Refblock offset %#" PRIx64
267 " unaligned (reftable index: %#" PRIx64
")",
268 refcount_block_offset
, refcount_table_index
);
272 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refcount_block_offset
,
278 block_index
= cluster_index
& (s
->refcount_block_size
- 1);
279 *refcount
= s
->get_refcount(refcount_block
, block_index
);
281 qcow2_cache_put(s
->refcount_block_cache
, &refcount_block
);
286 /* Checks if two offsets are described by the same refcount block */
287 static int in_same_refcount_block(BDRVQcow2State
*s
, uint64_t offset_a
,
290 uint64_t block_a
= offset_a
>> (s
->cluster_bits
+ s
->refcount_block_bits
);
291 uint64_t block_b
= offset_b
>> (s
->cluster_bits
+ s
->refcount_block_bits
);
293 return (block_a
== block_b
);
297 * Loads a refcount block. If it doesn't exist yet, it is allocated first
298 * (including growing the refcount table if needed).
300 * Returns 0 on success or -errno in error case
302 static int alloc_refcount_block(BlockDriverState
*bs
,
303 int64_t cluster_index
, void **refcount_block
)
305 BDRVQcow2State
*s
= bs
->opaque
;
306 unsigned int refcount_table_index
;
309 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC
);
311 /* Find the refcount block for the given cluster */
312 refcount_table_index
= cluster_index
>> s
->refcount_block_bits
;
314 if (refcount_table_index
< s
->refcount_table_size
) {
316 uint64_t refcount_block_offset
=
317 s
->refcount_table
[refcount_table_index
] & REFT_OFFSET_MASK
;
319 /* If it's already there, we're done */
320 if (refcount_block_offset
) {
321 if (offset_into_cluster(s
, refcount_block_offset
)) {
322 qcow2_signal_corruption(bs
, true, -1, -1, "Refblock offset %#"
323 PRIx64
" unaligned (reftable index: "
324 "%#x)", refcount_block_offset
,
325 refcount_table_index
);
329 return load_refcount_block(bs
, refcount_block_offset
,
335 * If we came here, we need to allocate something. Something is at least
336 * a cluster for the new refcount block. It may also include a new refcount
337 * table if the old refcount table is too small.
339 * Note that allocating clusters here needs some special care:
341 * - We can't use the normal qcow2_alloc_clusters(), it would try to
342 * increase the refcount and very likely we would end up with an endless
343 * recursion. Instead we must place the refcount blocks in a way that
344 * they can describe them themselves.
346 * - We need to consider that at this point we are inside update_refcounts
347 * and potentially doing an initial refcount increase. This means that
348 * some clusters have already been allocated by the caller, but their
349 * refcount isn't accurate yet. If we allocate clusters for metadata, we
350 * need to return -EAGAIN to signal the caller that it needs to restart
351 * the search for free clusters.
353 * - alloc_clusters_noref and qcow2_free_clusters may load a different
354 * refcount block into the cache
357 *refcount_block
= NULL
;
359 /* We write to the refcount table, so we might depend on L2 tables */
360 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
365 /* Allocate the refcount block itself and mark it as used */
366 int64_t new_block
= alloc_clusters_noref(bs
, s
->cluster_size
, INT64_MAX
);
371 /* If we're allocating the block at offset 0 then something is wrong */
372 if (new_block
== 0) {
373 qcow2_signal_corruption(bs
, true, -1, -1, "Preventing invalid "
374 "allocation of refcount block at offset 0");
379 fprintf(stderr
, "qcow2: Allocate refcount block %d for %" PRIx64
381 refcount_table_index
, cluster_index
<< s
->cluster_bits
, new_block
);
384 if (in_same_refcount_block(s
, new_block
, cluster_index
<< s
->cluster_bits
)) {
385 /* Zero the new refcount block before updating it */
386 ret
= qcow2_cache_get_empty(bs
, s
->refcount_block_cache
, new_block
,
392 memset(*refcount_block
, 0, s
->cluster_size
);
394 /* The block describes itself, need to update the cache */
395 int block_index
= (new_block
>> s
->cluster_bits
) &
396 (s
->refcount_block_size
- 1);
397 s
->set_refcount(*refcount_block
, block_index
, 1);
399 /* Described somewhere else. This can recurse at most twice before we
400 * arrive at a block that describes itself. */
401 ret
= update_refcount(bs
, new_block
, s
->cluster_size
, 1, false,
402 QCOW2_DISCARD_NEVER
);
407 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
412 /* Initialize the new refcount block only after updating its refcount,
413 * update_refcount uses the refcount cache itself */
414 ret
= qcow2_cache_get_empty(bs
, s
->refcount_block_cache
, new_block
,
420 memset(*refcount_block
, 0, s
->cluster_size
);
423 /* Now the new refcount block needs to be written to disk */
424 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE
);
425 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
, *refcount_block
);
426 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
431 /* If the refcount table is big enough, just hook the block up there */
432 if (refcount_table_index
< s
->refcount_table_size
) {
433 uint64_t data64
= cpu_to_be64(new_block
);
434 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_HOOKUP
);
435 ret
= bdrv_pwrite_sync(bs
->file
,
436 s
->refcount_table_offset
+ refcount_table_index
* sizeof(uint64_t),
437 &data64
, sizeof(data64
));
442 s
->refcount_table
[refcount_table_index
] = new_block
;
443 /* If there's a hole in s->refcount_table then it can happen
444 * that refcount_table_index < s->max_refcount_table_index */
445 s
->max_refcount_table_index
=
446 MAX(s
->max_refcount_table_index
, refcount_table_index
);
448 /* The new refcount block may be where the caller intended to put its
449 * data, so let it restart the search. */
453 qcow2_cache_put(s
->refcount_block_cache
, refcount_block
);
456 * If we come here, we need to grow the refcount table. Again, a new
457 * refcount table needs some space and we can't simply allocate to avoid
460 * Therefore let's grab new refcount blocks at the end of the image, which
461 * will describe themselves and the new refcount table. This way we can
462 * reference them only in the new table and do the switch to the new
463 * refcount table at once without producing an inconsistent state in
466 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_GROW
);
468 /* Calculate the number of refcount blocks needed so far; this will be the
469 * basis for calculating the index of the first cluster used for the
470 * self-describing refcount structures which we are about to create.
472 * Because we reached this point, there cannot be any refcount entries for
473 * cluster_index or higher indices yet. However, because new_block has been
474 * allocated to describe that cluster (and it will assume this role later
475 * on), we cannot use that index; also, new_block may actually have a higher
476 * cluster index than cluster_index, so it needs to be taken into account
477 * here (and 1 needs to be added to its value because that cluster is used).
479 uint64_t blocks_used
= DIV_ROUND_UP(MAX(cluster_index
+ 1,
480 (new_block
>> s
->cluster_bits
) + 1),
481 s
->refcount_block_size
);
483 /* Create the new refcount table and blocks */
484 uint64_t meta_offset
= (blocks_used
* s
->refcount_block_size
) *
487 ret
= qcow2_refcount_area(bs
, meta_offset
, 0, false,
488 refcount_table_index
, new_block
);
493 ret
= load_refcount_block(bs
, new_block
, refcount_block
);
498 /* If we were trying to do the initial refcount update for some cluster
499 * allocation, we might have used the same clusters to store newly
500 * allocated metadata. Make the caller search some new space. */
504 if (*refcount_block
!= NULL
) {
505 qcow2_cache_put(s
->refcount_block_cache
, refcount_block
);
511 * Starting at @start_offset, this function creates new self-covering refcount
512 * structures: A new refcount table and refcount blocks which cover all of
513 * themselves, and a number of @additional_clusters beyond their end.
514 * @start_offset must be at the end of the image file, that is, there must be
515 * only empty space beyond it.
516 * If @exact_size is false, the refcount table will have 50 % more entries than
517 * necessary so it will not need to grow again soon.
518 * If @new_refblock_offset is not zero, it contains the offset of a refcount
519 * block that should be entered into the new refcount table at index
520 * @new_refblock_index.
522 * Returns: The offset after the new refcount structures (i.e. where the
523 * @additional_clusters may be placed) on success, -errno on error.
525 int64_t qcow2_refcount_area(BlockDriverState
*bs
, uint64_t start_offset
,
526 uint64_t additional_clusters
, bool exact_size
,
527 int new_refblock_index
,
528 uint64_t new_refblock_offset
)
530 BDRVQcow2State
*s
= bs
->opaque
;
531 uint64_t total_refblock_count_u64
, additional_refblock_count
;
532 int total_refblock_count
, table_size
, area_reftable_index
, table_clusters
;
534 uint64_t table_offset
, block_offset
, end_offset
;
538 assert(!(start_offset
% s
->cluster_size
));
540 qcow2_refcount_metadata_size(start_offset
/ s
->cluster_size
+
542 s
->cluster_size
, s
->refcount_order
,
543 !exact_size
, &total_refblock_count_u64
);
544 if (total_refblock_count_u64
> QCOW_MAX_REFTABLE_SIZE
) {
547 total_refblock_count
= total_refblock_count_u64
;
549 /* Index in the refcount table of the first refcount block to cover the area
550 * of refcount structures we are about to create; we know that
551 * @total_refblock_count can cover @start_offset, so this will definitely
552 * fit into an int. */
553 area_reftable_index
= (start_offset
/ s
->cluster_size
) /
554 s
->refcount_block_size
;
557 table_size
= total_refblock_count
;
559 table_size
= total_refblock_count
+
560 DIV_ROUND_UP(total_refblock_count
, 2);
562 /* The qcow2 file can only store the reftable size in number of clusters */
563 table_size
= ROUND_UP(table_size
, s
->cluster_size
/ sizeof(uint64_t));
564 table_clusters
= (table_size
* sizeof(uint64_t)) / s
->cluster_size
;
566 if (table_size
> QCOW_MAX_REFTABLE_SIZE
) {
570 new_table
= g_try_new0(uint64_t, table_size
);
572 assert(table_size
> 0);
573 if (new_table
== NULL
) {
578 /* Fill the new refcount table */
579 if (table_size
> s
->max_refcount_table_index
) {
580 /* We're actually growing the reftable */
581 memcpy(new_table
, s
->refcount_table
,
582 (s
->max_refcount_table_index
+ 1) * sizeof(uint64_t));
584 /* Improbable case: We're shrinking the reftable. However, the caller
585 * has assured us that there is only empty space beyond @start_offset,
586 * so we can simply drop all of the refblocks that won't fit into the
588 memcpy(new_table
, s
->refcount_table
, table_size
* sizeof(uint64_t));
591 if (new_refblock_offset
) {
592 assert(new_refblock_index
< total_refblock_count
);
593 new_table
[new_refblock_index
] = new_refblock_offset
;
596 /* Count how many new refblocks we have to create */
597 additional_refblock_count
= 0;
598 for (i
= area_reftable_index
; i
< total_refblock_count
; i
++) {
600 additional_refblock_count
++;
604 table_offset
= start_offset
+ additional_refblock_count
* s
->cluster_size
;
605 end_offset
= table_offset
+ table_clusters
* s
->cluster_size
;
607 /* Fill the refcount blocks, and create new ones, if necessary */
608 block_offset
= start_offset
;
609 for (i
= area_reftable_index
; i
< total_refblock_count
; i
++) {
611 uint64_t first_offset_covered
;
613 /* Reuse an existing refblock if possible, create a new one otherwise */
615 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, new_table
[i
],
621 ret
= qcow2_cache_get_empty(bs
, s
->refcount_block_cache
,
622 block_offset
, &refblock_data
);
626 memset(refblock_data
, 0, s
->cluster_size
);
627 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
,
630 new_table
[i
] = block_offset
;
631 block_offset
+= s
->cluster_size
;
634 /* First host offset covered by this refblock */
635 first_offset_covered
= (uint64_t)i
* s
->refcount_block_size
*
637 if (first_offset_covered
< end_offset
) {
640 /* Set the refcount of all of the new refcount structures to 1 */
642 if (first_offset_covered
< start_offset
) {
643 assert(i
== area_reftable_index
);
644 j
= (start_offset
- first_offset_covered
) / s
->cluster_size
;
645 assert(j
< s
->refcount_block_size
);
650 end_index
= MIN((end_offset
- first_offset_covered
) /
652 s
->refcount_block_size
);
654 for (; j
< end_index
; j
++) {
655 /* The caller guaranteed us this space would be empty */
656 assert(s
->get_refcount(refblock_data
, j
) == 0);
657 s
->set_refcount(refblock_data
, j
, 1);
660 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
,
664 qcow2_cache_put(s
->refcount_block_cache
, &refblock_data
);
667 assert(block_offset
== table_offset
);
669 /* Write refcount blocks to disk */
670 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS
);
671 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
676 /* Write refcount table to disk */
677 for (i
= 0; i
< total_refblock_count
; i
++) {
678 cpu_to_be64s(&new_table
[i
]);
681 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE
);
682 ret
= bdrv_pwrite_sync(bs
->file
, table_offset
, new_table
,
683 table_size
* sizeof(uint64_t));
688 for (i
= 0; i
< total_refblock_count
; i
++) {
689 be64_to_cpus(&new_table
[i
]);
692 /* Hook up the new refcount table in the qcow2 header */
697 data
.d64
= cpu_to_be64(table_offset
);
698 data
.d32
= cpu_to_be32(table_clusters
);
699 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE
);
700 ret
= bdrv_pwrite_sync(bs
->file
,
701 offsetof(QCowHeader
, refcount_table_offset
),
702 &data
, sizeof(data
));
707 /* And switch it in memory */
708 uint64_t old_table_offset
= s
->refcount_table_offset
;
709 uint64_t old_table_size
= s
->refcount_table_size
;
711 g_free(s
->refcount_table
);
712 s
->refcount_table
= new_table
;
713 s
->refcount_table_size
= table_size
;
714 s
->refcount_table_offset
= table_offset
;
715 update_max_refcount_table_index(s
);
717 /* Free old table. */
718 qcow2_free_clusters(bs
, old_table_offset
, old_table_size
* sizeof(uint64_t),
719 QCOW2_DISCARD_OTHER
);
728 void qcow2_process_discards(BlockDriverState
*bs
, int ret
)
730 BDRVQcow2State
*s
= bs
->opaque
;
731 Qcow2DiscardRegion
*d
, *next
;
733 QTAILQ_FOREACH_SAFE(d
, &s
->discards
, next
, next
) {
734 QTAILQ_REMOVE(&s
->discards
, d
, next
);
736 /* Discard is optional, ignore the return value */
738 bdrv_pdiscard(bs
->file
, d
->offset
, d
->bytes
);
745 static void update_refcount_discard(BlockDriverState
*bs
,
746 uint64_t offset
, uint64_t length
)
748 BDRVQcow2State
*s
= bs
->opaque
;
749 Qcow2DiscardRegion
*d
, *p
, *next
;
751 QTAILQ_FOREACH(d
, &s
->discards
, next
) {
752 uint64_t new_start
= MIN(offset
, d
->offset
);
753 uint64_t new_end
= MAX(offset
+ length
, d
->offset
+ d
->bytes
);
755 if (new_end
- new_start
<= length
+ d
->bytes
) {
756 /* There can't be any overlap, areas ending up here have no
757 * references any more and therefore shouldn't get freed another
759 assert(d
->bytes
+ length
== new_end
- new_start
);
760 d
->offset
= new_start
;
761 d
->bytes
= new_end
- new_start
;
766 d
= g_malloc(sizeof(*d
));
767 *d
= (Qcow2DiscardRegion
) {
772 QTAILQ_INSERT_TAIL(&s
->discards
, d
, next
);
775 /* Merge discard requests if they are adjacent now */
776 QTAILQ_FOREACH_SAFE(p
, &s
->discards
, next
, next
) {
778 || p
->offset
> d
->offset
+ d
->bytes
779 || d
->offset
> p
->offset
+ p
->bytes
)
784 /* Still no overlap possible */
785 assert(p
->offset
== d
->offset
+ d
->bytes
786 || d
->offset
== p
->offset
+ p
->bytes
);
788 QTAILQ_REMOVE(&s
->discards
, p
, next
);
789 d
->offset
= MIN(d
->offset
, p
->offset
);
790 d
->bytes
+= p
->bytes
;
795 /* XXX: cache several refcount block clusters ? */
796 /* @addend is the absolute value of the addend; if @decrease is set, @addend
797 * will be subtracted from the current refcount, otherwise it will be added */
798 static int QEMU_WARN_UNUSED_RESULT
update_refcount(BlockDriverState
*bs
,
803 enum qcow2_discard_type type
)
805 BDRVQcow2State
*s
= bs
->opaque
;
806 int64_t start
, last
, cluster_offset
;
807 void *refcount_block
= NULL
;
808 int64_t old_table_index
= -1;
812 fprintf(stderr
, "update_refcount: offset=%" PRId64
" size=%" PRId64
813 " addend=%s%" PRIu64
"\n", offset
, length
, decrease
? "-" : "",
818 } else if (length
== 0) {
823 qcow2_cache_set_dependency(bs
, s
->refcount_block_cache
,
827 start
= start_of_cluster(s
, offset
);
828 last
= start_of_cluster(s
, offset
+ length
- 1);
829 for(cluster_offset
= start
; cluster_offset
<= last
;
830 cluster_offset
+= s
->cluster_size
)
834 int64_t cluster_index
= cluster_offset
>> s
->cluster_bits
;
835 int64_t table_index
= cluster_index
>> s
->refcount_block_bits
;
837 /* Load the refcount block and allocate it if needed */
838 if (table_index
!= old_table_index
) {
839 if (refcount_block
) {
840 qcow2_cache_put(s
->refcount_block_cache
, &refcount_block
);
842 ret
= alloc_refcount_block(bs
, cluster_index
, &refcount_block
);
843 /* If the caller needs to restart the search for free clusters,
844 * try the same ones first to see if they're still free. */
845 if (ret
== -EAGAIN
) {
846 if (s
->free_cluster_index
> (start
>> s
->cluster_bits
)) {
847 s
->free_cluster_index
= (start
>> s
->cluster_bits
);
854 old_table_index
= table_index
;
856 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
, refcount_block
);
858 /* we can update the count and save it */
859 block_index
= cluster_index
& (s
->refcount_block_size
- 1);
861 refcount
= s
->get_refcount(refcount_block
, block_index
);
862 if (decrease
? (refcount
- addend
> refcount
)
863 : (refcount
+ addend
< refcount
||
864 refcount
+ addend
> s
->refcount_max
))
874 if (refcount
== 0 && cluster_index
< s
->free_cluster_index
) {
875 s
->free_cluster_index
= cluster_index
;
877 s
->set_refcount(refcount_block
, block_index
, refcount
);
882 table
= qcow2_cache_is_table_offset(s
->refcount_block_cache
,
885 qcow2_cache_put(s
->refcount_block_cache
, &refcount_block
);
886 qcow2_cache_discard(s
->refcount_block_cache
, table
);
889 table
= qcow2_cache_is_table_offset(s
->l2_table_cache
, offset
);
891 qcow2_cache_discard(s
->l2_table_cache
, table
);
894 if (s
->discard_passthrough
[type
]) {
895 update_refcount_discard(bs
, cluster_offset
, s
->cluster_size
);
902 if (!s
->cache_discards
) {
903 qcow2_process_discards(bs
, ret
);
906 /* Write last changed block to disk */
907 if (refcount_block
) {
908 qcow2_cache_put(s
->refcount_block_cache
, &refcount_block
);
912 * Try do undo any updates if an error is returned (This may succeed in
913 * some cases like ENOSPC for allocating a new refcount block)
917 dummy
= update_refcount(bs
, offset
, cluster_offset
- offset
, addend
,
918 !decrease
, QCOW2_DISCARD_NEVER
);
926 * Increases or decreases the refcount of a given cluster.
928 * @addend is the absolute value of the addend; if @decrease is set, @addend
929 * will be subtracted from the current refcount, otherwise it will be added.
931 * On success 0 is returned; on failure -errno is returned.
933 int qcow2_update_cluster_refcount(BlockDriverState
*bs
,
934 int64_t cluster_index
,
935 uint64_t addend
, bool decrease
,
936 enum qcow2_discard_type type
)
938 BDRVQcow2State
*s
= bs
->opaque
;
941 ret
= update_refcount(bs
, cluster_index
<< s
->cluster_bits
, 1, addend
,
952 /*********************************************************/
953 /* cluster allocation functions */
957 /* return < 0 if error */
958 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, uint64_t size
,
961 BDRVQcow2State
*s
= bs
->opaque
;
962 uint64_t i
, nb_clusters
, refcount
;
965 /* We can't allocate clusters if they may still be queued for discard. */
966 if (s
->cache_discards
) {
967 qcow2_process_discards(bs
, 0);
970 nb_clusters
= size_to_clusters(s
, size
);
972 for(i
= 0; i
< nb_clusters
; i
++) {
973 uint64_t next_cluster_index
= s
->free_cluster_index
++;
974 ret
= qcow2_get_refcount(bs
, next_cluster_index
, &refcount
);
978 } else if (refcount
!= 0) {
983 /* Make sure that all offsets in the "allocated" range are representable
984 * in the requested max */
985 if (s
->free_cluster_index
> 0 &&
986 s
->free_cluster_index
- 1 > (max
>> s
->cluster_bits
))
992 fprintf(stderr
, "alloc_clusters: size=%" PRId64
" -> %" PRId64
"\n",
994 (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
);
996 return (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
;
999 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, uint64_t size
)
1004 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC
);
1006 offset
= alloc_clusters_noref(bs
, size
, QCOW_MAX_CLUSTER_OFFSET
);
1011 ret
= update_refcount(bs
, offset
, size
, 1, false, QCOW2_DISCARD_NEVER
);
1012 } while (ret
== -EAGAIN
);
1021 int64_t qcow2_alloc_clusters_at(BlockDriverState
*bs
, uint64_t offset
,
1022 int64_t nb_clusters
)
1024 BDRVQcow2State
*s
= bs
->opaque
;
1025 uint64_t cluster_index
, refcount
;
1029 assert(nb_clusters
>= 0);
1030 if (nb_clusters
== 0) {
1035 /* Check how many clusters there are free */
1036 cluster_index
= offset
>> s
->cluster_bits
;
1037 for(i
= 0; i
< nb_clusters
; i
++) {
1038 ret
= qcow2_get_refcount(bs
, cluster_index
++, &refcount
);
1041 } else if (refcount
!= 0) {
1046 /* And then allocate them */
1047 ret
= update_refcount(bs
, offset
, i
<< s
->cluster_bits
, 1, false,
1048 QCOW2_DISCARD_NEVER
);
1049 } while (ret
== -EAGAIN
);
1058 /* only used to allocate compressed sectors. We try to allocate
1059 contiguous sectors. size must be <= cluster_size */
1060 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
)
1062 BDRVQcow2State
*s
= bs
->opaque
;
1064 size_t free_in_cluster
;
1067 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC_BYTES
);
1068 assert(size
> 0 && size
<= s
->cluster_size
);
1069 assert(!s
->free_byte_offset
|| offset_into_cluster(s
, s
->free_byte_offset
));
1071 offset
= s
->free_byte_offset
;
1075 ret
= qcow2_get_refcount(bs
, offset
>> s
->cluster_bits
, &refcount
);
1080 if (refcount
== s
->refcount_max
) {
1085 free_in_cluster
= s
->cluster_size
- offset_into_cluster(s
, offset
);
1087 if (!offset
|| free_in_cluster
< size
) {
1088 int64_t new_cluster
;
1090 new_cluster
= alloc_clusters_noref(bs
, s
->cluster_size
,
1091 MIN(s
->cluster_offset_mask
,
1092 QCOW_MAX_CLUSTER_OFFSET
));
1093 if (new_cluster
< 0) {
1097 if (new_cluster
== 0) {
1098 qcow2_signal_corruption(bs
, true, -1, -1, "Preventing invalid "
1099 "allocation of compressed cluster "
1104 if (!offset
|| ROUND_UP(offset
, s
->cluster_size
) != new_cluster
) {
1105 offset
= new_cluster
;
1106 free_in_cluster
= s
->cluster_size
;
1108 free_in_cluster
+= s
->cluster_size
;
1113 ret
= update_refcount(bs
, offset
, size
, 1, false, QCOW2_DISCARD_NEVER
);
1117 } while (ret
== -EAGAIN
);
1122 /* The cluster refcount was incremented; refcount blocks must be flushed
1123 * before the caller's L2 table updates. */
1124 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
, s
->refcount_block_cache
);
1126 s
->free_byte_offset
= offset
+ size
;
1127 if (!offset_into_cluster(s
, s
->free_byte_offset
)) {
1128 s
->free_byte_offset
= 0;
1134 void qcow2_free_clusters(BlockDriverState
*bs
,
1135 int64_t offset
, int64_t size
,
1136 enum qcow2_discard_type type
)
1140 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_FREE
);
1141 ret
= update_refcount(bs
, offset
, size
, 1, true, type
);
1143 fprintf(stderr
, "qcow2_free_clusters failed: %s\n", strerror(-ret
));
1144 /* TODO Remember the clusters to free them later and avoid leaking */
1149 * Free a cluster using its L2 entry (handles clusters of all types, e.g.
1150 * normal cluster, compressed cluster, etc.)
1152 void qcow2_free_any_clusters(BlockDriverState
*bs
, uint64_t l2_entry
,
1153 int nb_clusters
, enum qcow2_discard_type type
)
1155 BDRVQcow2State
*s
= bs
->opaque
;
1157 switch (qcow2_get_cluster_type(l2_entry
)) {
1158 case QCOW2_CLUSTER_COMPRESSED
:
1161 nb_csectors
= ((l2_entry
>> s
->csize_shift
) &
1163 qcow2_free_clusters(bs
,
1164 (l2_entry
& s
->cluster_offset_mask
) & ~511,
1165 nb_csectors
* 512, type
);
1168 case QCOW2_CLUSTER_NORMAL
:
1169 case QCOW2_CLUSTER_ZERO_ALLOC
:
1170 if (offset_into_cluster(s
, l2_entry
& L2E_OFFSET_MASK
)) {
1171 qcow2_signal_corruption(bs
, false, -1, -1,
1172 "Cannot free unaligned cluster %#llx",
1173 l2_entry
& L2E_OFFSET_MASK
);
1175 qcow2_free_clusters(bs
, l2_entry
& L2E_OFFSET_MASK
,
1176 nb_clusters
<< s
->cluster_bits
, type
);
1179 case QCOW2_CLUSTER_ZERO_PLAIN
:
1180 case QCOW2_CLUSTER_UNALLOCATED
:
1187 int coroutine_fn
qcow2_write_caches(BlockDriverState
*bs
)
1189 BDRVQcow2State
*s
= bs
->opaque
;
1192 ret
= qcow2_cache_write(bs
, s
->l2_table_cache
);
1197 if (qcow2_need_accurate_refcounts(s
)) {
1198 ret
= qcow2_cache_write(bs
, s
->refcount_block_cache
);
1207 int coroutine_fn
qcow2_flush_caches(BlockDriverState
*bs
)
1209 int ret
= qcow2_write_caches(bs
);
1214 return bdrv_flush(bs
->file
->bs
);
1217 /*********************************************************/
1218 /* snapshots and image creation */
1222 /* update the refcounts of snapshots and the copied flag */
1223 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
1224 int64_t l1_table_offset
, int l1_size
, int addend
)
1226 BDRVQcow2State
*s
= bs
->opaque
;
1227 uint64_t *l1_table
, *l2_slice
, l2_offset
, entry
, l1_size2
, refcount
;
1228 bool l1_allocated
= false;
1229 int64_t old_entry
, old_l2_offset
;
1230 unsigned slice
, slice_size2
, n_slices
;
1231 int i
, j
, l1_modified
= 0, nb_csectors
;
1234 assert(addend
>= -1 && addend
<= 1);
1238 l1_size2
= l1_size
* sizeof(uint64_t);
1239 slice_size2
= s
->l2_slice_size
* sizeof(uint64_t);
1240 n_slices
= s
->cluster_size
/ slice_size2
;
1242 s
->cache_discards
= true;
1244 /* WARNING: qcow2_snapshot_goto relies on this function not using the
1245 * l1_table_offset when it is the current s->l1_table_offset! Be careful
1246 * when changing this! */
1247 if (l1_table_offset
!= s
->l1_table_offset
) {
1248 l1_table
= g_try_malloc0(ROUND_UP(l1_size2
, 512));
1249 if (l1_size2
&& l1_table
== NULL
) {
1253 l1_allocated
= true;
1255 ret
= bdrv_pread(bs
->file
, l1_table_offset
, l1_table
, l1_size2
);
1260 for (i
= 0; i
< l1_size
; i
++) {
1261 be64_to_cpus(&l1_table
[i
]);
1264 assert(l1_size
== s
->l1_size
);
1265 l1_table
= s
->l1_table
;
1266 l1_allocated
= false;
1269 for (i
= 0; i
< l1_size
; i
++) {
1270 l2_offset
= l1_table
[i
];
1272 old_l2_offset
= l2_offset
;
1273 l2_offset
&= L1E_OFFSET_MASK
;
1275 if (offset_into_cluster(s
, l2_offset
)) {
1276 qcow2_signal_corruption(bs
, true, -1, -1, "L2 table offset %#"
1277 PRIx64
" unaligned (L1 index: %#x)",
1283 for (slice
= 0; slice
< n_slices
; slice
++) {
1284 ret
= qcow2_cache_get(bs
, s
->l2_table_cache
,
1285 l2_offset
+ slice
* slice_size2
,
1286 (void **) &l2_slice
);
1291 for (j
= 0; j
< s
->l2_slice_size
; j
++) {
1292 uint64_t cluster_index
;
1295 entry
= be64_to_cpu(l2_slice
[j
]);
1297 entry
&= ~QCOW_OFLAG_COPIED
;
1298 offset
= entry
& L2E_OFFSET_MASK
;
1300 switch (qcow2_get_cluster_type(entry
)) {
1301 case QCOW2_CLUSTER_COMPRESSED
:
1302 nb_csectors
= ((entry
>> s
->csize_shift
) &
1305 ret
= update_refcount(
1306 bs
, (entry
& s
->cluster_offset_mask
) & ~511,
1307 nb_csectors
* 512, abs(addend
), addend
< 0,
1308 QCOW2_DISCARD_SNAPSHOT
);
1313 /* compressed clusters are never modified */
1317 case QCOW2_CLUSTER_NORMAL
:
1318 case QCOW2_CLUSTER_ZERO_ALLOC
:
1319 if (offset_into_cluster(s
, offset
)) {
1320 /* Here l2_index means table (not slice) index */
1321 int l2_index
= slice
* s
->l2_slice_size
+ j
;
1322 qcow2_signal_corruption(
1323 bs
, true, -1, -1, "Cluster "
1324 "allocation offset %#" PRIx64
1325 " unaligned (L2 offset: %#"
1326 PRIx64
", L2 index: %#x)",
1327 offset
, l2_offset
, l2_index
);
1332 cluster_index
= offset
>> s
->cluster_bits
;
1333 assert(cluster_index
);
1335 ret
= qcow2_update_cluster_refcount(
1336 bs
, cluster_index
, abs(addend
), addend
< 0,
1337 QCOW2_DISCARD_SNAPSHOT
);
1343 ret
= qcow2_get_refcount(bs
, cluster_index
, &refcount
);
1349 case QCOW2_CLUSTER_ZERO_PLAIN
:
1350 case QCOW2_CLUSTER_UNALLOCATED
:
1358 if (refcount
== 1) {
1359 entry
|= QCOW_OFLAG_COPIED
;
1361 if (entry
!= old_entry
) {
1363 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
,
1364 s
->refcount_block_cache
);
1366 l2_slice
[j
] = cpu_to_be64(entry
);
1367 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
,
1372 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
1376 ret
= qcow2_update_cluster_refcount(bs
, l2_offset
>>
1378 abs(addend
), addend
< 0,
1379 QCOW2_DISCARD_SNAPSHOT
);
1384 ret
= qcow2_get_refcount(bs
, l2_offset
>> s
->cluster_bits
,
1388 } else if (refcount
== 1) {
1389 l2_offset
|= QCOW_OFLAG_COPIED
;
1391 if (l2_offset
!= old_l2_offset
) {
1392 l1_table
[i
] = l2_offset
;
1398 ret
= bdrv_flush(bs
);
1401 qcow2_cache_put(s
->l2_table_cache
, (void **) &l2_slice
);
1404 s
->cache_discards
= false;
1405 qcow2_process_discards(bs
, ret
);
1407 /* Update L1 only if it isn't deleted anyway (addend = -1) */
1408 if (ret
== 0 && addend
>= 0 && l1_modified
) {
1409 for (i
= 0; i
< l1_size
; i
++) {
1410 cpu_to_be64s(&l1_table
[i
]);
1413 ret
= bdrv_pwrite_sync(bs
->file
, l1_table_offset
,
1414 l1_table
, l1_size2
);
1416 for (i
= 0; i
< l1_size
; i
++) {
1417 be64_to_cpus(&l1_table
[i
]);
1428 /*********************************************************/
1429 /* refcount checking functions */
1432 static uint64_t refcount_array_byte_size(BDRVQcow2State
*s
, uint64_t entries
)
1434 /* This assertion holds because there is no way we can address more than
1435 * 2^(64 - 9) clusters at once (with cluster size 512 = 2^9, and because
1436 * offsets have to be representable in bytes); due to every cluster
1437 * corresponding to one refcount entry, we are well below that limit */
1438 assert(entries
< (UINT64_C(1) << (64 - 9)));
1440 /* Thanks to the assertion this will not overflow, because
1441 * s->refcount_order < 7.
1442 * (note: x << s->refcount_order == x * s->refcount_bits) */
1443 return DIV_ROUND_UP(entries
<< s
->refcount_order
, 8);
1447 * Reallocates *array so that it can hold new_size entries. *size must contain
1448 * the current number of entries in *array. If the reallocation fails, *array
1449 * and *size will not be modified and -errno will be returned. If the
1450 * reallocation is successful, *array will be set to the new buffer, *size
1451 * will be set to new_size and 0 will be returned. The size of the reallocated
1452 * refcount array buffer will be aligned to a cluster boundary, and the newly
1453 * allocated area will be zeroed.
1455 static int realloc_refcount_array(BDRVQcow2State
*s
, void **array
,
1456 int64_t *size
, int64_t new_size
)
1458 int64_t old_byte_size
, new_byte_size
;
1461 /* Round to clusters so the array can be directly written to disk */
1462 old_byte_size
= size_to_clusters(s
, refcount_array_byte_size(s
, *size
))
1464 new_byte_size
= size_to_clusters(s
, refcount_array_byte_size(s
, new_size
))
1467 if (new_byte_size
== old_byte_size
) {
1472 assert(new_byte_size
> 0);
1474 if (new_byte_size
> SIZE_MAX
) {
1478 new_ptr
= g_try_realloc(*array
, new_byte_size
);
1483 if (new_byte_size
> old_byte_size
) {
1484 memset((char *)new_ptr
+ old_byte_size
, 0,
1485 new_byte_size
- old_byte_size
);
1495 * Increases the refcount for a range of clusters in a given refcount table.
1496 * This is used to construct a temporary refcount table out of L1 and L2 tables
1497 * which can be compared to the refcount table saved in the image.
1499 * Modifies the number of errors in res.
1501 int qcow2_inc_refcounts_imrt(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1502 void **refcount_table
,
1503 int64_t *refcount_table_size
,
1504 int64_t offset
, int64_t size
)
1506 BDRVQcow2State
*s
= bs
->opaque
;
1507 uint64_t start
, last
, cluster_offset
, k
, refcount
;
1514 start
= start_of_cluster(s
, offset
);
1515 last
= start_of_cluster(s
, offset
+ size
- 1);
1516 for(cluster_offset
= start
; cluster_offset
<= last
;
1517 cluster_offset
+= s
->cluster_size
) {
1518 k
= cluster_offset
>> s
->cluster_bits
;
1519 if (k
>= *refcount_table_size
) {
1520 ret
= realloc_refcount_array(s
, refcount_table
,
1521 refcount_table_size
, k
+ 1);
1523 res
->check_errors
++;
1528 refcount
= s
->get_refcount(*refcount_table
, k
);
1529 if (refcount
== s
->refcount_max
) {
1530 fprintf(stderr
, "ERROR: overflow cluster offset=0x%" PRIx64
1531 "\n", cluster_offset
);
1532 fprintf(stderr
, "Use qemu-img amend to increase the refcount entry "
1533 "width or qemu-img convert to create a clean copy if the "
1534 "image cannot be opened for writing\n");
1538 s
->set_refcount(*refcount_table
, k
, refcount
+ 1);
1544 /* Flags for check_refcounts_l1() and check_refcounts_l2() */
1546 CHECK_FRAG_INFO
= 0x2, /* update BlockFragInfo counters */
1550 * Increases the refcount in the given refcount table for the all clusters
1551 * referenced in the L2 table. While doing so, performs some checks on L2
1554 * Returns the number of errors found by the checks or -errno if an internal
1557 static int check_refcounts_l2(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1558 void **refcount_table
,
1559 int64_t *refcount_table_size
, int64_t l2_offset
,
1560 int flags
, BdrvCheckMode fix
)
1562 BDRVQcow2State
*s
= bs
->opaque
;
1563 uint64_t *l2_table
, l2_entry
;
1564 uint64_t next_contiguous_offset
= 0;
1565 int i
, l2_size
, nb_csectors
, ret
;
1567 /* Read L2 table from disk */
1568 l2_size
= s
->l2_size
* sizeof(uint64_t);
1569 l2_table
= g_malloc(l2_size
);
1571 ret
= bdrv_pread(bs
->file
, l2_offset
, l2_table
, l2_size
);
1573 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l2\n");
1574 res
->check_errors
++;
1578 /* Do the actual checks */
1579 for(i
= 0; i
< s
->l2_size
; i
++) {
1580 l2_entry
= be64_to_cpu(l2_table
[i
]);
1582 switch (qcow2_get_cluster_type(l2_entry
)) {
1583 case QCOW2_CLUSTER_COMPRESSED
:
1584 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
1585 if (l2_entry
& QCOW_OFLAG_COPIED
) {
1586 fprintf(stderr
, "ERROR: coffset=0x%" PRIx64
": "
1587 "copied flag must never be set for compressed "
1588 "clusters\n", l2_entry
& s
->cluster_offset_mask
);
1589 l2_entry
&= ~QCOW_OFLAG_COPIED
;
1593 /* Mark cluster as used */
1594 nb_csectors
= ((l2_entry
>> s
->csize_shift
) &
1596 l2_entry
&= s
->cluster_offset_mask
;
1597 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
1598 refcount_table
, refcount_table_size
,
1599 l2_entry
& ~511, nb_csectors
* 512);
1604 if (flags
& CHECK_FRAG_INFO
) {
1605 res
->bfi
.allocated_clusters
++;
1606 res
->bfi
.compressed_clusters
++;
1608 /* Compressed clusters are fragmented by nature. Since they
1609 * take up sub-sector space but we only have sector granularity
1610 * I/O we need to re-read the same sectors even for adjacent
1611 * compressed clusters.
1613 res
->bfi
.fragmented_clusters
++;
1617 case QCOW2_CLUSTER_ZERO_ALLOC
:
1618 case QCOW2_CLUSTER_NORMAL
:
1620 uint64_t offset
= l2_entry
& L2E_OFFSET_MASK
;
1622 if (flags
& CHECK_FRAG_INFO
) {
1623 res
->bfi
.allocated_clusters
++;
1624 if (next_contiguous_offset
&&
1625 offset
!= next_contiguous_offset
) {
1626 res
->bfi
.fragmented_clusters
++;
1628 next_contiguous_offset
= offset
+ s
->cluster_size
;
1631 /* Correct offsets are cluster aligned */
1632 if (offset_into_cluster(s
, offset
)) {
1633 if (qcow2_get_cluster_type(l2_entry
) ==
1634 QCOW2_CLUSTER_ZERO_ALLOC
)
1636 fprintf(stderr
, "%s offset=%" PRIx64
": Preallocated zero "
1637 "cluster is not properly aligned; L2 entry "
1639 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR",
1641 if (fix
& BDRV_FIX_ERRORS
) {
1642 uint64_t l2e_offset
=
1643 l2_offset
+ (uint64_t)i
* sizeof(uint64_t);
1645 l2_entry
= QCOW_OFLAG_ZERO
;
1646 l2_table
[i
] = cpu_to_be64(l2_entry
);
1647 ret
= qcow2_pre_write_overlap_check(bs
,
1648 QCOW2_OL_ACTIVE_L2
| QCOW2_OL_INACTIVE_L2
,
1649 l2e_offset
, sizeof(uint64_t));
1651 fprintf(stderr
, "ERROR: Overlap check failed\n");
1652 res
->check_errors
++;
1653 /* Something is seriously wrong, so abort checking
1658 ret
= bdrv_pwrite_sync(bs
->file
, l2e_offset
,
1659 &l2_table
[i
], sizeof(uint64_t));
1661 fprintf(stderr
, "ERROR: Failed to overwrite L2 "
1662 "table entry: %s\n", strerror(-ret
));
1663 res
->check_errors
++;
1664 /* Do not abort, continue checking the rest of this
1665 * L2 table's entries */
1667 res
->corruptions_fixed
++;
1668 /* Skip marking the cluster as used
1669 * (it is unused now) */
1676 fprintf(stderr
, "ERROR offset=%" PRIx64
": Data cluster is "
1677 "not properly aligned; L2 entry corrupted.\n", offset
);
1682 /* Mark cluster as used */
1683 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
1684 refcount_table
, refcount_table_size
,
1685 offset
, s
->cluster_size
);
1692 case QCOW2_CLUSTER_ZERO_PLAIN
:
1693 case QCOW2_CLUSTER_UNALLOCATED
:
1710 * Increases the refcount for the L1 table, its L2 tables and all referenced
1711 * clusters in the given refcount table. While doing so, performs some checks
1712 * on L1 and L2 entries.
1714 * Returns the number of errors found by the checks or -errno if an internal
1717 static int check_refcounts_l1(BlockDriverState
*bs
,
1718 BdrvCheckResult
*res
,
1719 void **refcount_table
,
1720 int64_t *refcount_table_size
,
1721 int64_t l1_table_offset
, int l1_size
,
1722 int flags
, BdrvCheckMode fix
)
1724 BDRVQcow2State
*s
= bs
->opaque
;
1725 uint64_t *l1_table
= NULL
, l2_offset
, l1_size2
;
1728 l1_size2
= l1_size
* sizeof(uint64_t);
1730 /* Mark L1 table as used */
1731 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, refcount_table_size
,
1732 l1_table_offset
, l1_size2
);
1737 /* Read L1 table entries from disk */
1739 l1_table
= g_try_malloc(l1_size2
);
1740 if (l1_table
== NULL
) {
1742 res
->check_errors
++;
1745 ret
= bdrv_pread(bs
->file
, l1_table_offset
, l1_table
, l1_size2
);
1747 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
1748 res
->check_errors
++;
1751 for(i
= 0;i
< l1_size
; i
++)
1752 be64_to_cpus(&l1_table
[i
]);
1755 /* Do the actual checks */
1756 for(i
= 0; i
< l1_size
; i
++) {
1757 l2_offset
= l1_table
[i
];
1759 /* Mark L2 table as used */
1760 l2_offset
&= L1E_OFFSET_MASK
;
1761 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
1762 refcount_table
, refcount_table_size
,
1763 l2_offset
, s
->cluster_size
);
1768 /* L2 tables are cluster aligned */
1769 if (offset_into_cluster(s
, l2_offset
)) {
1770 fprintf(stderr
, "ERROR l2_offset=%" PRIx64
": Table is not "
1771 "cluster aligned; L1 entry corrupted\n", l2_offset
);
1775 /* Process and check L2 entries */
1776 ret
= check_refcounts_l2(bs
, res
, refcount_table
,
1777 refcount_table_size
, l2_offset
, flags
,
1793 * Checks the OFLAG_COPIED flag for all L1 and L2 entries.
1795 * This function does not print an error message nor does it increment
1796 * check_errors if qcow2_get_refcount fails (this is because such an error will
1797 * have been already detected and sufficiently signaled by the calling function
1798 * (qcow2_check_refcounts) by the time this function is called).
1800 static int check_oflag_copied(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1803 BDRVQcow2State
*s
= bs
->opaque
;
1804 uint64_t *l2_table
= qemu_blockalign(bs
, s
->cluster_size
);
1810 if (fix
& BDRV_FIX_ERRORS
) {
1813 } else if (fix
& BDRV_FIX_LEAKS
) {
1814 /* Repair only if that seems safe: This function is always
1815 * called after the refcounts have been fixed, so the refcount
1816 * is accurate if that repair was successful */
1817 repair
= !res
->check_errors
&& !res
->corruptions
&& !res
->leaks
;
1822 for (i
= 0; i
< s
->l1_size
; i
++) {
1823 uint64_t l1_entry
= s
->l1_table
[i
];
1824 uint64_t l2_offset
= l1_entry
& L1E_OFFSET_MASK
;
1825 bool l2_dirty
= false;
1831 ret
= qcow2_get_refcount(bs
, l2_offset
>> s
->cluster_bits
,
1834 /* don't print message nor increment check_errors */
1837 if ((refcount
== 1) != ((l1_entry
& QCOW_OFLAG_COPIED
) != 0)) {
1838 fprintf(stderr
, "%s OFLAG_COPIED L2 cluster: l1_index=%d "
1839 "l1_entry=%" PRIx64
" refcount=%" PRIu64
"\n",
1840 repair
? "Repairing" : "ERROR", i
, l1_entry
, refcount
);
1842 s
->l1_table
[i
] = refcount
== 1
1843 ? l1_entry
| QCOW_OFLAG_COPIED
1844 : l1_entry
& ~QCOW_OFLAG_COPIED
;
1845 ret
= qcow2_write_l1_entry(bs
, i
);
1847 res
->check_errors
++;
1850 res
->corruptions_fixed
++;
1856 ret
= bdrv_pread(bs
->file
, l2_offset
, l2_table
,
1857 s
->l2_size
* sizeof(uint64_t));
1859 fprintf(stderr
, "ERROR: Could not read L2 table: %s\n",
1861 res
->check_errors
++;
1865 for (j
= 0; j
< s
->l2_size
; j
++) {
1866 uint64_t l2_entry
= be64_to_cpu(l2_table
[j
]);
1867 uint64_t data_offset
= l2_entry
& L2E_OFFSET_MASK
;
1868 QCow2ClusterType cluster_type
= qcow2_get_cluster_type(l2_entry
);
1870 if (cluster_type
== QCOW2_CLUSTER_NORMAL
||
1871 cluster_type
== QCOW2_CLUSTER_ZERO_ALLOC
) {
1872 ret
= qcow2_get_refcount(bs
,
1873 data_offset
>> s
->cluster_bits
,
1876 /* don't print message nor increment check_errors */
1879 if ((refcount
== 1) != ((l2_entry
& QCOW_OFLAG_COPIED
) != 0)) {
1880 fprintf(stderr
, "%s OFLAG_COPIED data cluster: "
1881 "l2_entry=%" PRIx64
" refcount=%" PRIu64
"\n",
1882 repair
? "Repairing" : "ERROR", l2_entry
, refcount
);
1884 l2_table
[j
] = cpu_to_be64(refcount
== 1
1885 ? l2_entry
| QCOW_OFLAG_COPIED
1886 : l2_entry
& ~QCOW_OFLAG_COPIED
);
1888 res
->corruptions_fixed
++;
1897 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L2
,
1898 l2_offset
, s
->cluster_size
);
1900 fprintf(stderr
, "ERROR: Could not write L2 table; metadata "
1901 "overlap check failed: %s\n", strerror(-ret
));
1902 res
->check_errors
++;
1906 ret
= bdrv_pwrite(bs
->file
, l2_offset
, l2_table
,
1909 fprintf(stderr
, "ERROR: Could not write L2 table: %s\n",
1911 res
->check_errors
++;
1920 qemu_vfree(l2_table
);
1925 * Checks consistency of refblocks and accounts for each refblock in
1928 static int check_refblocks(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1929 BdrvCheckMode fix
, bool *rebuild
,
1930 void **refcount_table
, int64_t *nb_clusters
)
1932 BDRVQcow2State
*s
= bs
->opaque
;
1936 for(i
= 0; i
< s
->refcount_table_size
; i
++) {
1937 uint64_t offset
, cluster
;
1938 offset
= s
->refcount_table
[i
];
1939 cluster
= offset
>> s
->cluster_bits
;
1941 /* Refcount blocks are cluster aligned */
1942 if (offset_into_cluster(s
, offset
)) {
1943 fprintf(stderr
, "ERROR refcount block %" PRId64
" is not "
1944 "cluster aligned; refcount table entry corrupted\n", i
);
1950 if (cluster
>= *nb_clusters
) {
1951 fprintf(stderr
, "%s refcount block %" PRId64
" is outside image\n",
1952 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR", i
);
1954 if (fix
& BDRV_FIX_ERRORS
) {
1955 int64_t new_nb_clusters
;
1956 Error
*local_err
= NULL
;
1958 if (offset
> INT64_MAX
- s
->cluster_size
) {
1963 ret
= bdrv_truncate(bs
->file
, offset
+ s
->cluster_size
,
1964 PREALLOC_MODE_OFF
, &local_err
);
1966 error_report_err(local_err
);
1969 size
= bdrv_getlength(bs
->file
->bs
);
1975 new_nb_clusters
= size_to_clusters(s
, size
);
1976 assert(new_nb_clusters
>= *nb_clusters
);
1978 ret
= realloc_refcount_array(s
, refcount_table
,
1979 nb_clusters
, new_nb_clusters
);
1981 res
->check_errors
++;
1985 if (cluster
>= *nb_clusters
) {
1990 res
->corruptions_fixed
++;
1991 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
1992 refcount_table
, nb_clusters
,
1993 offset
, s
->cluster_size
);
1997 /* No need to check whether the refcount is now greater than 1:
1998 * This area was just allocated and zeroed, so it can only be
1999 * exactly 1 after qcow2_inc_refcounts_imrt() */
2005 fprintf(stderr
, "ERROR could not resize image: %s\n",
2014 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, nb_clusters
,
2015 offset
, s
->cluster_size
);
2019 if (s
->get_refcount(*refcount_table
, cluster
) != 1) {
2020 fprintf(stderr
, "ERROR refcount block %" PRId64
2021 " refcount=%" PRIu64
"\n", i
,
2022 s
->get_refcount(*refcount_table
, cluster
));
2033 * Calculates an in-memory refcount table.
2035 static int calculate_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
2036 BdrvCheckMode fix
, bool *rebuild
,
2037 void **refcount_table
, int64_t *nb_clusters
)
2039 BDRVQcow2State
*s
= bs
->opaque
;
2044 if (!*refcount_table
) {
2045 int64_t old_size
= 0;
2046 ret
= realloc_refcount_array(s
, refcount_table
,
2047 &old_size
, *nb_clusters
);
2049 res
->check_errors
++;
2055 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, nb_clusters
,
2056 0, s
->cluster_size
);
2061 /* current L1 table */
2062 ret
= check_refcounts_l1(bs
, res
, refcount_table
, nb_clusters
,
2063 s
->l1_table_offset
, s
->l1_size
, CHECK_FRAG_INFO
,
2070 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
2071 sn
= s
->snapshots
+ i
;
2072 if (offset_into_cluster(s
, sn
->l1_table_offset
)) {
2073 fprintf(stderr
, "ERROR snapshot %s (%s) l1_offset=%#" PRIx64
": "
2074 "L1 table is not cluster aligned; snapshot table entry "
2075 "corrupted\n", sn
->id_str
, sn
->name
, sn
->l1_table_offset
);
2079 if (sn
->l1_size
> QCOW_MAX_L1_SIZE
/ sizeof(uint64_t)) {
2080 fprintf(stderr
, "ERROR snapshot %s (%s) l1_size=%#" PRIx32
": "
2081 "L1 table is too large; snapshot table entry corrupted\n",
2082 sn
->id_str
, sn
->name
, sn
->l1_size
);
2086 ret
= check_refcounts_l1(bs
, res
, refcount_table
, nb_clusters
,
2087 sn
->l1_table_offset
, sn
->l1_size
, 0, fix
);
2092 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, nb_clusters
,
2093 s
->snapshots_offset
, s
->snapshots_size
);
2099 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, nb_clusters
,
2100 s
->refcount_table_offset
,
2101 s
->refcount_table_size
* sizeof(uint64_t));
2107 if (s
->crypto_header
.length
) {
2108 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, nb_clusters
,
2109 s
->crypto_header
.offset
,
2110 s
->crypto_header
.length
);
2117 ret
= qcow2_check_bitmaps_refcounts(bs
, res
, refcount_table
, nb_clusters
);
2122 return check_refblocks(bs
, res
, fix
, rebuild
, refcount_table
, nb_clusters
);
2126 * Compares the actual reference count for each cluster in the image against the
2127 * refcount as reported by the refcount structures on-disk.
2129 static void compare_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
2130 BdrvCheckMode fix
, bool *rebuild
,
2131 int64_t *highest_cluster
,
2132 void *refcount_table
, int64_t nb_clusters
)
2134 BDRVQcow2State
*s
= bs
->opaque
;
2136 uint64_t refcount1
, refcount2
;
2139 for (i
= 0, *highest_cluster
= 0; i
< nb_clusters
; i
++) {
2140 ret
= qcow2_get_refcount(bs
, i
, &refcount1
);
2142 fprintf(stderr
, "Can't get refcount for cluster %" PRId64
": %s\n",
2144 res
->check_errors
++;
2148 refcount2
= s
->get_refcount(refcount_table
, i
);
2150 if (refcount1
> 0 || refcount2
> 0) {
2151 *highest_cluster
= i
;
2154 if (refcount1
!= refcount2
) {
2155 /* Check if we're allowed to fix the mismatch */
2156 int *num_fixed
= NULL
;
2157 if (refcount1
== 0) {
2159 } else if (refcount1
> refcount2
&& (fix
& BDRV_FIX_LEAKS
)) {
2160 num_fixed
= &res
->leaks_fixed
;
2161 } else if (refcount1
< refcount2
&& (fix
& BDRV_FIX_ERRORS
)) {
2162 num_fixed
= &res
->corruptions_fixed
;
2165 fprintf(stderr
, "%s cluster %" PRId64
" refcount=%" PRIu64
2166 " reference=%" PRIu64
"\n",
2167 num_fixed
!= NULL
? "Repairing" :
2168 refcount1
< refcount2
? "ERROR" :
2170 i
, refcount1
, refcount2
);
2173 ret
= update_refcount(bs
, i
<< s
->cluster_bits
, 1,
2174 refcount_diff(refcount1
, refcount2
),
2175 refcount1
> refcount2
,
2176 QCOW2_DISCARD_ALWAYS
);
2183 /* And if we couldn't, print an error */
2184 if (refcount1
< refcount2
) {
2194 * Allocates clusters using an in-memory refcount table (IMRT) in contrast to
2195 * the on-disk refcount structures.
2197 * On input, *first_free_cluster tells where to start looking, and need not
2198 * actually be a free cluster; the returned offset will not be before that
2199 * cluster. On output, *first_free_cluster points to the first gap found, even
2200 * if that gap was too small to be used as the returned offset.
2202 * Note that *first_free_cluster is a cluster index whereas the return value is
2205 static int64_t alloc_clusters_imrt(BlockDriverState
*bs
,
2207 void **refcount_table
,
2208 int64_t *imrt_nb_clusters
,
2209 int64_t *first_free_cluster
)
2211 BDRVQcow2State
*s
= bs
->opaque
;
2212 int64_t cluster
= *first_free_cluster
, i
;
2213 bool first_gap
= true;
2214 int contiguous_free_clusters
;
2217 /* Starting at *first_free_cluster, find a range of at least cluster_count
2218 * continuously free clusters */
2219 for (contiguous_free_clusters
= 0;
2220 cluster
< *imrt_nb_clusters
&&
2221 contiguous_free_clusters
< cluster_count
;
2224 if (!s
->get_refcount(*refcount_table
, cluster
)) {
2225 contiguous_free_clusters
++;
2227 /* If this is the first free cluster found, update
2228 * *first_free_cluster accordingly */
2229 *first_free_cluster
= cluster
;
2232 } else if (contiguous_free_clusters
) {
2233 contiguous_free_clusters
= 0;
2237 /* If contiguous_free_clusters is greater than zero, it contains the number
2238 * of continuously free clusters until the current cluster; the first free
2239 * cluster in the current "gap" is therefore
2240 * cluster - contiguous_free_clusters */
2242 /* If no such range could be found, grow the in-memory refcount table
2243 * accordingly to append free clusters at the end of the image */
2244 if (contiguous_free_clusters
< cluster_count
) {
2245 /* contiguous_free_clusters clusters are already empty at the image end;
2246 * we need cluster_count clusters; therefore, we have to allocate
2247 * cluster_count - contiguous_free_clusters new clusters at the end of
2248 * the image (which is the current value of cluster; note that cluster
2249 * may exceed old_imrt_nb_clusters if *first_free_cluster pointed beyond
2251 ret
= realloc_refcount_array(s
, refcount_table
, imrt_nb_clusters
,
2252 cluster
+ cluster_count
2253 - contiguous_free_clusters
);
2259 /* Go back to the first free cluster */
2260 cluster
-= contiguous_free_clusters
;
2261 for (i
= 0; i
< cluster_count
; i
++) {
2262 s
->set_refcount(*refcount_table
, cluster
+ i
, 1);
2265 return cluster
<< s
->cluster_bits
;
2269 * Creates a new refcount structure based solely on the in-memory information
2270 * given through *refcount_table. All necessary allocations will be reflected
2273 * On success, the old refcount structure is leaked (it will be covered by the
2274 * new refcount structure).
2276 static int rebuild_refcount_structure(BlockDriverState
*bs
,
2277 BdrvCheckResult
*res
,
2278 void **refcount_table
,
2279 int64_t *nb_clusters
)
2281 BDRVQcow2State
*s
= bs
->opaque
;
2282 int64_t first_free_cluster
= 0, reftable_offset
= -1, cluster
= 0;
2283 int64_t refblock_offset
, refblock_start
, refblock_index
;
2284 uint32_t reftable_size
= 0;
2285 uint64_t *on_disk_reftable
= NULL
;
2286 void *on_disk_refblock
;
2289 uint64_t reftable_offset
;
2290 uint32_t reftable_clusters
;
2291 } QEMU_PACKED reftable_offset_and_clusters
;
2293 qcow2_cache_empty(bs
, s
->refcount_block_cache
);
2296 for (; cluster
< *nb_clusters
; cluster
++) {
2297 if (!s
->get_refcount(*refcount_table
, cluster
)) {
2301 refblock_index
= cluster
>> s
->refcount_block_bits
;
2302 refblock_start
= refblock_index
<< s
->refcount_block_bits
;
2304 /* Don't allocate a cluster in a refblock already written to disk */
2305 if (first_free_cluster
< refblock_start
) {
2306 first_free_cluster
= refblock_start
;
2308 refblock_offset
= alloc_clusters_imrt(bs
, 1, refcount_table
,
2309 nb_clusters
, &first_free_cluster
);
2310 if (refblock_offset
< 0) {
2311 fprintf(stderr
, "ERROR allocating refblock: %s\n",
2312 strerror(-refblock_offset
));
2313 res
->check_errors
++;
2314 ret
= refblock_offset
;
2318 if (reftable_size
<= refblock_index
) {
2319 uint32_t old_reftable_size
= reftable_size
;
2320 uint64_t *new_on_disk_reftable
;
2322 reftable_size
= ROUND_UP((refblock_index
+ 1) * sizeof(uint64_t),
2323 s
->cluster_size
) / sizeof(uint64_t);
2324 new_on_disk_reftable
= g_try_realloc(on_disk_reftable
,
2327 if (!new_on_disk_reftable
) {
2328 res
->check_errors
++;
2332 on_disk_reftable
= new_on_disk_reftable
;
2334 memset(on_disk_reftable
+ old_reftable_size
, 0,
2335 (reftable_size
- old_reftable_size
) * sizeof(uint64_t));
2337 /* The offset we have for the reftable is now no longer valid;
2338 * this will leak that range, but we can easily fix that by running
2339 * a leak-fixing check after this rebuild operation */
2340 reftable_offset
= -1;
2342 assert(on_disk_reftable
);
2344 on_disk_reftable
[refblock_index
] = refblock_offset
;
2346 /* If this is apparently the last refblock (for now), try to squeeze the
2348 if (refblock_index
== (*nb_clusters
- 1) >> s
->refcount_block_bits
&&
2349 reftable_offset
< 0)
2351 uint64_t reftable_clusters
= size_to_clusters(s
, reftable_size
*
2353 reftable_offset
= alloc_clusters_imrt(bs
, reftable_clusters
,
2354 refcount_table
, nb_clusters
,
2355 &first_free_cluster
);
2356 if (reftable_offset
< 0) {
2357 fprintf(stderr
, "ERROR allocating reftable: %s\n",
2358 strerror(-reftable_offset
));
2359 res
->check_errors
++;
2360 ret
= reftable_offset
;
2365 ret
= qcow2_pre_write_overlap_check(bs
, 0, refblock_offset
,
2368 fprintf(stderr
, "ERROR writing refblock: %s\n", strerror(-ret
));
2372 /* The size of *refcount_table is always cluster-aligned, therefore the
2373 * write operation will not overflow */
2374 on_disk_refblock
= (void *)((char *) *refcount_table
+
2375 refblock_index
* s
->cluster_size
);
2377 ret
= bdrv_write(bs
->file
, refblock_offset
/ BDRV_SECTOR_SIZE
,
2378 on_disk_refblock
, s
->cluster_sectors
);
2380 fprintf(stderr
, "ERROR writing refblock: %s\n", strerror(-ret
));
2384 /* Go to the end of this refblock */
2385 cluster
= refblock_start
+ s
->refcount_block_size
- 1;
2388 if (reftable_offset
< 0) {
2389 uint64_t post_refblock_start
, reftable_clusters
;
2391 post_refblock_start
= ROUND_UP(*nb_clusters
, s
->refcount_block_size
);
2392 reftable_clusters
= size_to_clusters(s
,
2393 reftable_size
* sizeof(uint64_t));
2394 /* Not pretty but simple */
2395 if (first_free_cluster
< post_refblock_start
) {
2396 first_free_cluster
= post_refblock_start
;
2398 reftable_offset
= alloc_clusters_imrt(bs
, reftable_clusters
,
2399 refcount_table
, nb_clusters
,
2400 &first_free_cluster
);
2401 if (reftable_offset
< 0) {
2402 fprintf(stderr
, "ERROR allocating reftable: %s\n",
2403 strerror(-reftable_offset
));
2404 res
->check_errors
++;
2405 ret
= reftable_offset
;
2409 goto write_refblocks
;
2412 for (refblock_index
= 0; refblock_index
< reftable_size
; refblock_index
++) {
2413 cpu_to_be64s(&on_disk_reftable
[refblock_index
]);
2416 ret
= qcow2_pre_write_overlap_check(bs
, 0, reftable_offset
,
2417 reftable_size
* sizeof(uint64_t));
2419 fprintf(stderr
, "ERROR writing reftable: %s\n", strerror(-ret
));
2423 assert(reftable_size
< INT_MAX
/ sizeof(uint64_t));
2424 ret
= bdrv_pwrite(bs
->file
, reftable_offset
, on_disk_reftable
,
2425 reftable_size
* sizeof(uint64_t));
2427 fprintf(stderr
, "ERROR writing reftable: %s\n", strerror(-ret
));
2431 /* Enter new reftable into the image header */
2432 reftable_offset_and_clusters
.reftable_offset
= cpu_to_be64(reftable_offset
);
2433 reftable_offset_and_clusters
.reftable_clusters
=
2434 cpu_to_be32(size_to_clusters(s
, reftable_size
* sizeof(uint64_t)));
2435 ret
= bdrv_pwrite_sync(bs
->file
,
2436 offsetof(QCowHeader
, refcount_table_offset
),
2437 &reftable_offset_and_clusters
,
2438 sizeof(reftable_offset_and_clusters
));
2440 fprintf(stderr
, "ERROR setting reftable: %s\n", strerror(-ret
));
2444 for (refblock_index
= 0; refblock_index
< reftable_size
; refblock_index
++) {
2445 be64_to_cpus(&on_disk_reftable
[refblock_index
]);
2447 s
->refcount_table
= on_disk_reftable
;
2448 s
->refcount_table_offset
= reftable_offset
;
2449 s
->refcount_table_size
= reftable_size
;
2450 update_max_refcount_table_index(s
);
2455 g_free(on_disk_reftable
);
2460 * Checks an image for refcount consistency.
2462 * Returns 0 if no errors are found, the number of errors in case the image is
2463 * detected as corrupted, and -errno when an internal error occurred.
2465 int qcow2_check_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
2468 BDRVQcow2State
*s
= bs
->opaque
;
2469 BdrvCheckResult pre_compare_res
;
2470 int64_t size
, highest_cluster
, nb_clusters
;
2471 void *refcount_table
= NULL
;
2472 bool rebuild
= false;
2475 size
= bdrv_getlength(bs
->file
->bs
);
2477 res
->check_errors
++;
2481 nb_clusters
= size_to_clusters(s
, size
);
2482 if (nb_clusters
> INT_MAX
) {
2483 res
->check_errors
++;
2487 res
->bfi
.total_clusters
=
2488 size_to_clusters(s
, bs
->total_sectors
* BDRV_SECTOR_SIZE
);
2490 ret
= calculate_refcounts(bs
, res
, fix
, &rebuild
, &refcount_table
,
2496 /* In case we don't need to rebuild the refcount structure (but want to fix
2497 * something), this function is immediately called again, in which case the
2498 * result should be ignored */
2499 pre_compare_res
= *res
;
2500 compare_refcounts(bs
, res
, 0, &rebuild
, &highest_cluster
, refcount_table
,
2503 if (rebuild
&& (fix
& BDRV_FIX_ERRORS
)) {
2504 BdrvCheckResult old_res
= *res
;
2505 int fresh_leaks
= 0;
2507 fprintf(stderr
, "Rebuilding refcount structure\n");
2508 ret
= rebuild_refcount_structure(bs
, res
, &refcount_table
,
2514 res
->corruptions
= 0;
2517 /* Because the old reftable has been exchanged for a new one the
2518 * references have to be recalculated */
2520 memset(refcount_table
, 0, refcount_array_byte_size(s
, nb_clusters
));
2521 ret
= calculate_refcounts(bs
, res
, 0, &rebuild
, &refcount_table
,
2527 if (fix
& BDRV_FIX_LEAKS
) {
2528 /* The old refcount structures are now leaked, fix it; the result
2529 * can be ignored, aside from leaks which were introduced by
2530 * rebuild_refcount_structure() that could not be fixed */
2531 BdrvCheckResult saved_res
= *res
;
2532 *res
= (BdrvCheckResult
){ 0 };
2534 compare_refcounts(bs
, res
, BDRV_FIX_LEAKS
, &rebuild
,
2535 &highest_cluster
, refcount_table
, nb_clusters
);
2537 fprintf(stderr
, "ERROR rebuilt refcount structure is still "
2541 /* Any leaks accounted for here were introduced by
2542 * rebuild_refcount_structure() because that function has created a
2543 * new refcount structure from scratch */
2544 fresh_leaks
= res
->leaks
;
2548 if (res
->corruptions
< old_res
.corruptions
) {
2549 res
->corruptions_fixed
+= old_res
.corruptions
- res
->corruptions
;
2551 if (res
->leaks
< old_res
.leaks
) {
2552 res
->leaks_fixed
+= old_res
.leaks
- res
->leaks
;
2554 res
->leaks
+= fresh_leaks
;
2557 fprintf(stderr
, "ERROR need to rebuild refcount structures\n");
2558 res
->check_errors
++;
2563 if (res
->leaks
|| res
->corruptions
) {
2564 *res
= pre_compare_res
;
2565 compare_refcounts(bs
, res
, fix
, &rebuild
, &highest_cluster
,
2566 refcount_table
, nb_clusters
);
2570 /* check OFLAG_COPIED */
2571 ret
= check_oflag_copied(bs
, res
, fix
);
2576 res
->image_end_offset
= (highest_cluster
+ 1) * s
->cluster_size
;
2580 g_free(refcount_table
);
2585 #define overlaps_with(ofs, sz) \
2586 ranges_overlap(offset, size, ofs, sz)
2589 * Checks if the given offset into the image file is actually free to use by
2590 * looking for overlaps with important metadata sections (L1/L2 tables etc.),
2591 * i.e. a sanity check without relying on the refcount tables.
2593 * The ign parameter specifies what checks not to perform (being a bitmask of
2594 * QCow2MetadataOverlap values), i.e., what sections to ignore.
2597 * - 0 if writing to this offset will not affect the mentioned metadata
2598 * - a positive QCow2MetadataOverlap value indicating one overlapping section
2599 * - a negative value (-errno) indicating an error while performing a check,
2600 * e.g. when bdrv_read failed on QCOW2_OL_INACTIVE_L2
2602 int qcow2_check_metadata_overlap(BlockDriverState
*bs
, int ign
, int64_t offset
,
2605 BDRVQcow2State
*s
= bs
->opaque
;
2606 int chk
= s
->overlap_check
& ~ign
;
2613 if (chk
& QCOW2_OL_MAIN_HEADER
) {
2614 if (offset
< s
->cluster_size
) {
2615 return QCOW2_OL_MAIN_HEADER
;
2619 /* align range to test to cluster boundaries */
2620 size
= ROUND_UP(offset_into_cluster(s
, offset
) + size
, s
->cluster_size
);
2621 offset
= start_of_cluster(s
, offset
);
2623 if ((chk
& QCOW2_OL_ACTIVE_L1
) && s
->l1_size
) {
2624 if (overlaps_with(s
->l1_table_offset
, s
->l1_size
* sizeof(uint64_t))) {
2625 return QCOW2_OL_ACTIVE_L1
;
2629 if ((chk
& QCOW2_OL_REFCOUNT_TABLE
) && s
->refcount_table_size
) {
2630 if (overlaps_with(s
->refcount_table_offset
,
2631 s
->refcount_table_size
* sizeof(uint64_t))) {
2632 return QCOW2_OL_REFCOUNT_TABLE
;
2636 if ((chk
& QCOW2_OL_SNAPSHOT_TABLE
) && s
->snapshots_size
) {
2637 if (overlaps_with(s
->snapshots_offset
, s
->snapshots_size
)) {
2638 return QCOW2_OL_SNAPSHOT_TABLE
;
2642 if ((chk
& QCOW2_OL_INACTIVE_L1
) && s
->snapshots
) {
2643 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
2644 if (s
->snapshots
[i
].l1_size
&&
2645 overlaps_with(s
->snapshots
[i
].l1_table_offset
,
2646 s
->snapshots
[i
].l1_size
* sizeof(uint64_t))) {
2647 return QCOW2_OL_INACTIVE_L1
;
2652 if ((chk
& QCOW2_OL_ACTIVE_L2
) && s
->l1_table
) {
2653 for (i
= 0; i
< s
->l1_size
; i
++) {
2654 if ((s
->l1_table
[i
] & L1E_OFFSET_MASK
) &&
2655 overlaps_with(s
->l1_table
[i
] & L1E_OFFSET_MASK
,
2657 return QCOW2_OL_ACTIVE_L2
;
2662 if ((chk
& QCOW2_OL_REFCOUNT_BLOCK
) && s
->refcount_table
) {
2663 unsigned last_entry
= s
->max_refcount_table_index
;
2664 assert(last_entry
< s
->refcount_table_size
);
2665 assert(last_entry
+ 1 == s
->refcount_table_size
||
2666 (s
->refcount_table
[last_entry
+ 1] & REFT_OFFSET_MASK
) == 0);
2667 for (i
= 0; i
<= last_entry
; i
++) {
2668 if ((s
->refcount_table
[i
] & REFT_OFFSET_MASK
) &&
2669 overlaps_with(s
->refcount_table
[i
] & REFT_OFFSET_MASK
,
2671 return QCOW2_OL_REFCOUNT_BLOCK
;
2676 if ((chk
& QCOW2_OL_INACTIVE_L2
) && s
->snapshots
) {
2677 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
2678 uint64_t l1_ofs
= s
->snapshots
[i
].l1_table_offset
;
2679 uint32_t l1_sz
= s
->snapshots
[i
].l1_size
;
2680 uint64_t l1_sz2
= l1_sz
* sizeof(uint64_t);
2684 ret
= qcow2_validate_table(bs
, l1_ofs
, l1_sz
, sizeof(uint64_t),
2685 QCOW_MAX_L1_SIZE
, "", NULL
);
2690 l1
= g_try_malloc(l1_sz2
);
2692 if (l1_sz2
&& l1
== NULL
) {
2696 ret
= bdrv_pread(bs
->file
, l1_ofs
, l1
, l1_sz2
);
2702 for (j
= 0; j
< l1_sz
; j
++) {
2703 uint64_t l2_ofs
= be64_to_cpu(l1
[j
]) & L1E_OFFSET_MASK
;
2704 if (l2_ofs
&& overlaps_with(l2_ofs
, s
->cluster_size
)) {
2706 return QCOW2_OL_INACTIVE_L2
;
2714 if ((chk
& QCOW2_OL_BITMAP_DIRECTORY
) &&
2715 (s
->autoclear_features
& QCOW2_AUTOCLEAR_BITMAPS
))
2717 if (overlaps_with(s
->bitmap_directory_offset
,
2718 s
->bitmap_directory_size
))
2720 return QCOW2_OL_BITMAP_DIRECTORY
;
2727 static const char *metadata_ol_names
[] = {
2728 [QCOW2_OL_MAIN_HEADER_BITNR
] = "qcow2_header",
2729 [QCOW2_OL_ACTIVE_L1_BITNR
] = "active L1 table",
2730 [QCOW2_OL_ACTIVE_L2_BITNR
] = "active L2 table",
2731 [QCOW2_OL_REFCOUNT_TABLE_BITNR
] = "refcount table",
2732 [QCOW2_OL_REFCOUNT_BLOCK_BITNR
] = "refcount block",
2733 [QCOW2_OL_SNAPSHOT_TABLE_BITNR
] = "snapshot table",
2734 [QCOW2_OL_INACTIVE_L1_BITNR
] = "inactive L1 table",
2735 [QCOW2_OL_INACTIVE_L2_BITNR
] = "inactive L2 table",
2736 [QCOW2_OL_BITMAP_DIRECTORY_BITNR
] = "bitmap directory",
2738 QEMU_BUILD_BUG_ON(QCOW2_OL_MAX_BITNR
!= ARRAY_SIZE(metadata_ol_names
));
2741 * First performs a check for metadata overlaps (through
2742 * qcow2_check_metadata_overlap); if that fails with a negative value (error
2743 * while performing a check), that value is returned. If an impending overlap
2744 * is detected, the BDS will be made unusable, the qcow2 file marked corrupt
2745 * and -EIO returned.
2747 * Returns 0 if there were neither overlaps nor errors while checking for
2748 * overlaps; or a negative value (-errno) on error.
2750 int qcow2_pre_write_overlap_check(BlockDriverState
*bs
, int ign
, int64_t offset
,
2753 int ret
= qcow2_check_metadata_overlap(bs
, ign
, offset
, size
);
2757 } else if (ret
> 0) {
2758 int metadata_ol_bitnr
= ctz32(ret
);
2759 assert(metadata_ol_bitnr
< QCOW2_OL_MAX_BITNR
);
2761 qcow2_signal_corruption(bs
, true, offset
, size
, "Preventing invalid "
2762 "write on metadata (overlaps with %s)",
2763 metadata_ol_names
[metadata_ol_bitnr
]);
2770 /* A pointer to a function of this type is given to walk_over_reftable(). That
2771 * function will create refblocks and pass them to a RefblockFinishOp once they
2772 * are completed (@refblock). @refblock_empty is set if the refblock is
2775 * Along with the refblock, a corresponding reftable entry is passed, in the
2776 * reftable @reftable (which may be reallocated) at @reftable_index.
2778 * @allocated should be set to true if a new cluster has been allocated.
2780 typedef int (RefblockFinishOp
)(BlockDriverState
*bs
, uint64_t **reftable
,
2781 uint64_t reftable_index
, uint64_t *reftable_size
,
2782 void *refblock
, bool refblock_empty
,
2783 bool *allocated
, Error
**errp
);
2786 * This "operation" for walk_over_reftable() allocates the refblock on disk (if
2787 * it is not empty) and inserts its offset into the new reftable. The size of
2788 * this new reftable is increased as required.
2790 static int alloc_refblock(BlockDriverState
*bs
, uint64_t **reftable
,
2791 uint64_t reftable_index
, uint64_t *reftable_size
,
2792 void *refblock
, bool refblock_empty
, bool *allocated
,
2795 BDRVQcow2State
*s
= bs
->opaque
;
2798 if (!refblock_empty
&& reftable_index
>= *reftable_size
) {
2799 uint64_t *new_reftable
;
2800 uint64_t new_reftable_size
;
2802 new_reftable_size
= ROUND_UP(reftable_index
+ 1,
2803 s
->cluster_size
/ sizeof(uint64_t));
2804 if (new_reftable_size
> QCOW_MAX_REFTABLE_SIZE
/ sizeof(uint64_t)) {
2806 "This operation would make the refcount table grow "
2807 "beyond the maximum size supported by QEMU, aborting");
2811 new_reftable
= g_try_realloc(*reftable
, new_reftable_size
*
2813 if (!new_reftable
) {
2814 error_setg(errp
, "Failed to increase reftable buffer size");
2818 memset(new_reftable
+ *reftable_size
, 0,
2819 (new_reftable_size
- *reftable_size
) * sizeof(uint64_t));
2821 *reftable
= new_reftable
;
2822 *reftable_size
= new_reftable_size
;
2825 if (!refblock_empty
&& !(*reftable
)[reftable_index
]) {
2826 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
2828 error_setg_errno(errp
, -offset
, "Failed to allocate refblock");
2831 (*reftable
)[reftable_index
] = offset
;
2839 * This "operation" for walk_over_reftable() writes the refblock to disk at the
2840 * offset specified by the new reftable's entry. It does not modify the new
2841 * reftable or change any refcounts.
2843 static int flush_refblock(BlockDriverState
*bs
, uint64_t **reftable
,
2844 uint64_t reftable_index
, uint64_t *reftable_size
,
2845 void *refblock
, bool refblock_empty
, bool *allocated
,
2848 BDRVQcow2State
*s
= bs
->opaque
;
2852 if (reftable_index
< *reftable_size
&& (*reftable
)[reftable_index
]) {
2853 offset
= (*reftable
)[reftable_index
];
2855 ret
= qcow2_pre_write_overlap_check(bs
, 0, offset
, s
->cluster_size
);
2857 error_setg_errno(errp
, -ret
, "Overlap check failed");
2861 ret
= bdrv_pwrite(bs
->file
, offset
, refblock
, s
->cluster_size
);
2863 error_setg_errno(errp
, -ret
, "Failed to write refblock");
2867 assert(refblock_empty
);
2874 * This function walks over the existing reftable and every referenced refblock;
2875 * if @new_set_refcount is non-NULL, it is called for every refcount entry to
2876 * create an equal new entry in the passed @new_refblock. Once that
2877 * @new_refblock is completely filled, @operation will be called.
2879 * @status_cb and @cb_opaque are used for the amend operation's status callback.
2880 * @index is the index of the walk_over_reftable() calls and @total is the total
2881 * number of walk_over_reftable() calls per amend operation. Both are used for
2882 * calculating the parameters for the status callback.
2884 * @allocated is set to true if a new cluster has been allocated.
2886 static int walk_over_reftable(BlockDriverState
*bs
, uint64_t **new_reftable
,
2887 uint64_t *new_reftable_index
,
2888 uint64_t *new_reftable_size
,
2889 void *new_refblock
, int new_refblock_size
,
2890 int new_refcount_bits
,
2891 RefblockFinishOp
*operation
, bool *allocated
,
2892 Qcow2SetRefcountFunc
*new_set_refcount
,
2893 BlockDriverAmendStatusCB
*status_cb
,
2894 void *cb_opaque
, int index
, int total
,
2897 BDRVQcow2State
*s
= bs
->opaque
;
2898 uint64_t reftable_index
;
2899 bool new_refblock_empty
= true;
2901 int new_refblock_index
= 0;
2904 for (reftable_index
= 0; reftable_index
< s
->refcount_table_size
;
2907 uint64_t refblock_offset
= s
->refcount_table
[reftable_index
]
2910 status_cb(bs
, (uint64_t)index
* s
->refcount_table_size
+ reftable_index
,
2911 (uint64_t)total
* s
->refcount_table_size
, cb_opaque
);
2913 if (refblock_offset
) {
2916 if (offset_into_cluster(s
, refblock_offset
)) {
2917 qcow2_signal_corruption(bs
, true, -1, -1, "Refblock offset %#"
2918 PRIx64
" unaligned (reftable index: %#"
2919 PRIx64
")", refblock_offset
,
2922 "Image is corrupt (unaligned refblock offset)");
2926 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refblock_offset
,
2929 error_setg_errno(errp
, -ret
, "Failed to retrieve refblock");
2933 for (refblock_index
= 0; refblock_index
< s
->refcount_block_size
;
2938 if (new_refblock_index
>= new_refblock_size
) {
2939 /* new_refblock is now complete */
2940 ret
= operation(bs
, new_reftable
, *new_reftable_index
,
2941 new_reftable_size
, new_refblock
,
2942 new_refblock_empty
, allocated
, errp
);
2944 qcow2_cache_put(s
->refcount_block_cache
, &refblock
);
2948 (*new_reftable_index
)++;
2949 new_refblock_index
= 0;
2950 new_refblock_empty
= true;
2953 refcount
= s
->get_refcount(refblock
, refblock_index
);
2954 if (new_refcount_bits
< 64 && refcount
>> new_refcount_bits
) {
2957 qcow2_cache_put(s
->refcount_block_cache
, &refblock
);
2959 offset
= ((reftable_index
<< s
->refcount_block_bits
)
2960 + refblock_index
) << s
->cluster_bits
;
2962 error_setg(errp
, "Cannot decrease refcount entry width to "
2963 "%i bits: Cluster at offset %#" PRIx64
" has a "
2964 "refcount of %" PRIu64
, new_refcount_bits
,
2969 if (new_set_refcount
) {
2970 new_set_refcount(new_refblock
, new_refblock_index
++,
2973 new_refblock_index
++;
2975 new_refblock_empty
= new_refblock_empty
&& refcount
== 0;
2978 qcow2_cache_put(s
->refcount_block_cache
, &refblock
);
2980 /* No refblock means every refcount is 0 */
2981 for (refblock_index
= 0; refblock_index
< s
->refcount_block_size
;
2984 if (new_refblock_index
>= new_refblock_size
) {
2985 /* new_refblock is now complete */
2986 ret
= operation(bs
, new_reftable
, *new_reftable_index
,
2987 new_reftable_size
, new_refblock
,
2988 new_refblock_empty
, allocated
, errp
);
2993 (*new_reftable_index
)++;
2994 new_refblock_index
= 0;
2995 new_refblock_empty
= true;
2998 if (new_set_refcount
) {
2999 new_set_refcount(new_refblock
, new_refblock_index
++, 0);
3001 new_refblock_index
++;
3007 if (new_refblock_index
> 0) {
3008 /* Complete the potentially existing partially filled final refblock */
3009 if (new_set_refcount
) {
3010 for (; new_refblock_index
< new_refblock_size
;
3011 new_refblock_index
++)
3013 new_set_refcount(new_refblock
, new_refblock_index
, 0);
3017 ret
= operation(bs
, new_reftable
, *new_reftable_index
,
3018 new_reftable_size
, new_refblock
, new_refblock_empty
,
3024 (*new_reftable_index
)++;
3027 status_cb(bs
, (uint64_t)(index
+ 1) * s
->refcount_table_size
,
3028 (uint64_t)total
* s
->refcount_table_size
, cb_opaque
);
3033 int qcow2_change_refcount_order(BlockDriverState
*bs
, int refcount_order
,
3034 BlockDriverAmendStatusCB
*status_cb
,
3035 void *cb_opaque
, Error
**errp
)
3037 BDRVQcow2State
*s
= bs
->opaque
;
3038 Qcow2GetRefcountFunc
*new_get_refcount
;
3039 Qcow2SetRefcountFunc
*new_set_refcount
;
3040 void *new_refblock
= qemu_blockalign(bs
->file
->bs
, s
->cluster_size
);
3041 uint64_t *new_reftable
= NULL
, new_reftable_size
= 0;
3042 uint64_t *old_reftable
, old_reftable_size
, old_reftable_offset
;
3043 uint64_t new_reftable_index
= 0;
3045 int64_t new_reftable_offset
= 0, allocated_reftable_size
= 0;
3046 int new_refblock_size
, new_refcount_bits
= 1 << refcount_order
;
3047 int old_refcount_order
;
3050 bool new_allocation
;
3052 assert(s
->qcow_version
>= 3);
3053 assert(refcount_order
>= 0 && refcount_order
<= 6);
3055 /* see qcow2_open() */
3056 new_refblock_size
= 1 << (s
->cluster_bits
- (refcount_order
- 3));
3058 new_get_refcount
= get_refcount_funcs
[refcount_order
];
3059 new_set_refcount
= set_refcount_funcs
[refcount_order
];
3065 new_allocation
= false;
3067 /* At least we have to do this walk and the one which writes the
3068 * refblocks; also, at least we have to do this loop here at least
3069 * twice (normally), first to do the allocations, and second to
3070 * determine that everything is correctly allocated, this then makes
3071 * three walks in total */
3072 total_walks
= MAX(walk_index
+ 2, 3);
3074 /* First, allocate the structures so they are present in the refcount
3076 ret
= walk_over_reftable(bs
, &new_reftable
, &new_reftable_index
,
3077 &new_reftable_size
, NULL
, new_refblock_size
,
3078 new_refcount_bits
, &alloc_refblock
,
3079 &new_allocation
, NULL
, status_cb
, cb_opaque
,
3080 walk_index
++, total_walks
, errp
);
3085 new_reftable_index
= 0;
3087 if (new_allocation
) {
3088 if (new_reftable_offset
) {
3089 qcow2_free_clusters(bs
, new_reftable_offset
,
3090 allocated_reftable_size
* sizeof(uint64_t),
3091 QCOW2_DISCARD_NEVER
);
3094 new_reftable_offset
= qcow2_alloc_clusters(bs
, new_reftable_size
*
3096 if (new_reftable_offset
< 0) {
3097 error_setg_errno(errp
, -new_reftable_offset
,
3098 "Failed to allocate the new reftable");
3099 ret
= new_reftable_offset
;
3102 allocated_reftable_size
= new_reftable_size
;
3104 } while (new_allocation
);
3106 /* Second, write the new refblocks */
3107 ret
= walk_over_reftable(bs
, &new_reftable
, &new_reftable_index
,
3108 &new_reftable_size
, new_refblock
,
3109 new_refblock_size
, new_refcount_bits
,
3110 &flush_refblock
, &new_allocation
, new_set_refcount
,
3111 status_cb
, cb_opaque
, walk_index
, walk_index
+ 1,
3116 assert(!new_allocation
);
3119 /* Write the new reftable */
3120 ret
= qcow2_pre_write_overlap_check(bs
, 0, new_reftable_offset
,
3121 new_reftable_size
* sizeof(uint64_t));
3123 error_setg_errno(errp
, -ret
, "Overlap check failed");
3127 for (i
= 0; i
< new_reftable_size
; i
++) {
3128 cpu_to_be64s(&new_reftable
[i
]);
3131 ret
= bdrv_pwrite(bs
->file
, new_reftable_offset
, new_reftable
,
3132 new_reftable_size
* sizeof(uint64_t));
3134 for (i
= 0; i
< new_reftable_size
; i
++) {
3135 be64_to_cpus(&new_reftable
[i
]);
3139 error_setg_errno(errp
, -ret
, "Failed to write the new reftable");
3144 /* Empty the refcount cache */
3145 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
3147 error_setg_errno(errp
, -ret
, "Failed to flush the refblock cache");
3151 /* Update the image header to point to the new reftable; this only updates
3152 * the fields which are relevant to qcow2_update_header(); other fields
3153 * such as s->refcount_table or s->refcount_bits stay stale for now
3154 * (because we have to restore everything if qcow2_update_header() fails) */
3155 old_refcount_order
= s
->refcount_order
;
3156 old_reftable_size
= s
->refcount_table_size
;
3157 old_reftable_offset
= s
->refcount_table_offset
;
3159 s
->refcount_order
= refcount_order
;
3160 s
->refcount_table_size
= new_reftable_size
;
3161 s
->refcount_table_offset
= new_reftable_offset
;
3163 ret
= qcow2_update_header(bs
);
3165 s
->refcount_order
= old_refcount_order
;
3166 s
->refcount_table_size
= old_reftable_size
;
3167 s
->refcount_table_offset
= old_reftable_offset
;
3168 error_setg_errno(errp
, -ret
, "Failed to update the qcow2 header");
3172 /* Now update the rest of the in-memory information */
3173 old_reftable
= s
->refcount_table
;
3174 s
->refcount_table
= new_reftable
;
3175 update_max_refcount_table_index(s
);
3177 s
->refcount_bits
= 1 << refcount_order
;
3178 s
->refcount_max
= UINT64_C(1) << (s
->refcount_bits
- 1);
3179 s
->refcount_max
+= s
->refcount_max
- 1;
3181 s
->refcount_block_bits
= s
->cluster_bits
- (refcount_order
- 3);
3182 s
->refcount_block_size
= 1 << s
->refcount_block_bits
;
3184 s
->get_refcount
= new_get_refcount
;
3185 s
->set_refcount
= new_set_refcount
;
3187 /* For cleaning up all old refblocks and the old reftable below the "done"
3189 new_reftable
= old_reftable
;
3190 new_reftable_size
= old_reftable_size
;
3191 new_reftable_offset
= old_reftable_offset
;
3195 /* On success, new_reftable actually points to the old reftable (and
3196 * new_reftable_size is the old reftable's size); but that is just
3198 for (i
= 0; i
< new_reftable_size
; i
++) {
3199 uint64_t offset
= new_reftable
[i
] & REFT_OFFSET_MASK
;
3201 qcow2_free_clusters(bs
, offset
, s
->cluster_size
,
3202 QCOW2_DISCARD_OTHER
);
3205 g_free(new_reftable
);
3207 if (new_reftable_offset
> 0) {
3208 qcow2_free_clusters(bs
, new_reftable_offset
,
3209 new_reftable_size
* sizeof(uint64_t),
3210 QCOW2_DISCARD_OTHER
);
3214 qemu_vfree(new_refblock
);
3218 static int64_t get_refblock_offset(BlockDriverState
*bs
, uint64_t offset
)
3220 BDRVQcow2State
*s
= bs
->opaque
;
3221 uint32_t index
= offset_to_reftable_index(s
, offset
);
3222 int64_t covering_refblock_offset
= 0;
3224 if (index
< s
->refcount_table_size
) {
3225 covering_refblock_offset
= s
->refcount_table
[index
] & REFT_OFFSET_MASK
;
3227 if (!covering_refblock_offset
) {
3228 qcow2_signal_corruption(bs
, true, -1, -1, "Refblock at %#" PRIx64
" is "
3229 "not covered by the refcount structures",
3234 return covering_refblock_offset
;
3237 static int qcow2_discard_refcount_block(BlockDriverState
*bs
,
3238 uint64_t discard_block_offs
)
3240 BDRVQcow2State
*s
= bs
->opaque
;
3241 int64_t refblock_offs
;
3242 uint64_t cluster_index
= discard_block_offs
>> s
->cluster_bits
;
3243 uint32_t block_index
= cluster_index
& (s
->refcount_block_size
- 1);
3247 refblock_offs
= get_refblock_offset(bs
, discard_block_offs
);
3248 if (refblock_offs
< 0) {
3249 return refblock_offs
;
3252 assert(discard_block_offs
!= 0);
3254 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refblock_offs
,
3260 if (s
->get_refcount(refblock
, block_index
) != 1) {
3261 qcow2_signal_corruption(bs
, true, -1, -1, "Invalid refcount:"
3262 " refblock offset %#" PRIx64
3263 ", reftable index %u"
3264 ", block offset %#" PRIx64
3265 ", refcount %#" PRIx64
,
3267 offset_to_reftable_index(s
, discard_block_offs
),
3269 s
->get_refcount(refblock
, block_index
));
3270 qcow2_cache_put(s
->refcount_block_cache
, &refblock
);
3273 s
->set_refcount(refblock
, block_index
, 0);
3275 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
, refblock
);
3277 qcow2_cache_put(s
->refcount_block_cache
, &refblock
);
3279 if (cluster_index
< s
->free_cluster_index
) {
3280 s
->free_cluster_index
= cluster_index
;
3283 refblock
= qcow2_cache_is_table_offset(s
->refcount_block_cache
,
3284 discard_block_offs
);
3286 /* discard refblock from the cache if refblock is cached */
3287 qcow2_cache_discard(s
->refcount_block_cache
, refblock
);
3289 update_refcount_discard(bs
, discard_block_offs
, s
->cluster_size
);
3294 int qcow2_shrink_reftable(BlockDriverState
*bs
)
3296 BDRVQcow2State
*s
= bs
->opaque
;
3297 uint64_t *reftable_tmp
=
3298 g_malloc(s
->refcount_table_size
* sizeof(uint64_t));
3301 for (i
= 0; i
< s
->refcount_table_size
; i
++) {
3302 int64_t refblock_offs
= s
->refcount_table
[i
] & REFT_OFFSET_MASK
;
3306 if (refblock_offs
== 0) {
3307 reftable_tmp
[i
] = 0;
3310 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refblock_offs
,
3316 /* the refblock has own reference */
3317 if (i
== offset_to_reftable_index(s
, refblock_offs
)) {
3318 uint64_t block_index
= (refblock_offs
>> s
->cluster_bits
) &
3319 (s
->refcount_block_size
- 1);
3320 uint64_t refcount
= s
->get_refcount(refblock
, block_index
);
3322 s
->set_refcount(refblock
, block_index
, 0);
3324 unused_block
= buffer_is_zero(refblock
, s
->cluster_size
);
3326 s
->set_refcount(refblock
, block_index
, refcount
);
3328 unused_block
= buffer_is_zero(refblock
, s
->cluster_size
);
3330 qcow2_cache_put(s
->refcount_block_cache
, &refblock
);
3332 reftable_tmp
[i
] = unused_block
? 0 : cpu_to_be64(s
->refcount_table
[i
]);
3335 ret
= bdrv_pwrite_sync(bs
->file
, s
->refcount_table_offset
, reftable_tmp
,
3336 s
->refcount_table_size
* sizeof(uint64_t));
3338 * If the write in the reftable failed the image may contain a partially
3339 * overwritten reftable. In this case it would be better to clear the
3340 * reftable in memory to avoid possible image corruption.
3342 for (i
= 0; i
< s
->refcount_table_size
; i
++) {
3343 if (s
->refcount_table
[i
] && !reftable_tmp
[i
]) {
3345 ret
= qcow2_discard_refcount_block(bs
, s
->refcount_table
[i
] &
3348 s
->refcount_table
[i
] = 0;
3352 if (!s
->cache_discards
) {
3353 qcow2_process_discards(bs
, ret
);
3357 g_free(reftable_tmp
);
3361 int64_t qcow2_get_last_cluster(BlockDriverState
*bs
, int64_t size
)
3363 BDRVQcow2State
*s
= bs
->opaque
;
3366 for (i
= size_to_clusters(s
, size
) - 1; i
>= 0; i
--) {
3368 int ret
= qcow2_get_refcount(bs
, i
, &refcount
);
3370 fprintf(stderr
, "Can't get refcount for cluster %" PRId64
": %s\n",
3378 qcow2_signal_corruption(bs
, true, -1, -1,
3379 "There are no references in the refcount table.");