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"
33 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, uint64_t size
);
34 static int QEMU_WARN_UNUSED_RESULT
update_refcount(BlockDriverState
*bs
,
35 int64_t offset
, int64_t length
, uint64_t addend
,
36 bool decrease
, enum qcow2_discard_type type
);
38 static uint64_t get_refcount_ro0(const void *refcount_array
, uint64_t index
);
39 static uint64_t get_refcount_ro1(const void *refcount_array
, uint64_t index
);
40 static uint64_t get_refcount_ro2(const void *refcount_array
, uint64_t index
);
41 static uint64_t get_refcount_ro3(const void *refcount_array
, uint64_t index
);
42 static uint64_t get_refcount_ro4(const void *refcount_array
, uint64_t index
);
43 static uint64_t get_refcount_ro5(const void *refcount_array
, uint64_t index
);
44 static uint64_t get_refcount_ro6(const void *refcount_array
, uint64_t index
);
46 static void set_refcount_ro0(void *refcount_array
, uint64_t index
,
48 static void set_refcount_ro1(void *refcount_array
, uint64_t index
,
50 static void set_refcount_ro2(void *refcount_array
, uint64_t index
,
52 static void set_refcount_ro3(void *refcount_array
, uint64_t index
,
54 static void set_refcount_ro4(void *refcount_array
, uint64_t index
,
56 static void set_refcount_ro5(void *refcount_array
, uint64_t index
,
58 static void set_refcount_ro6(void *refcount_array
, uint64_t index
,
62 static Qcow2GetRefcountFunc
*const get_refcount_funcs
[] = {
72 static Qcow2SetRefcountFunc
*const set_refcount_funcs
[] = {
83 /*********************************************************/
84 /* refcount handling */
86 static void update_max_refcount_table_index(BDRVQcow2State
*s
)
88 unsigned i
= s
->refcount_table_size
- 1;
89 while (i
> 0 && (s
->refcount_table
[i
] & REFT_OFFSET_MASK
) == 0) {
92 /* Set s->max_refcount_table_index to the index of the last used entry */
93 s
->max_refcount_table_index
= i
;
96 int qcow2_refcount_init(BlockDriverState
*bs
)
98 BDRVQcow2State
*s
= bs
->opaque
;
99 unsigned int refcount_table_size2
, i
;
102 assert(s
->refcount_order
>= 0 && s
->refcount_order
<= 6);
104 s
->get_refcount
= get_refcount_funcs
[s
->refcount_order
];
105 s
->set_refcount
= set_refcount_funcs
[s
->refcount_order
];
107 assert(s
->refcount_table_size
<= INT_MAX
/ sizeof(uint64_t));
108 refcount_table_size2
= s
->refcount_table_size
* sizeof(uint64_t);
109 s
->refcount_table
= g_try_malloc(refcount_table_size2
);
111 if (s
->refcount_table_size
> 0) {
112 if (s
->refcount_table
== NULL
) {
116 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_LOAD
);
117 ret
= bdrv_pread(bs
->file
, s
->refcount_table_offset
,
118 s
->refcount_table
, refcount_table_size2
);
122 for(i
= 0; i
< s
->refcount_table_size
; i
++)
123 be64_to_cpus(&s
->refcount_table
[i
]);
124 update_max_refcount_table_index(s
);
131 void qcow2_refcount_close(BlockDriverState
*bs
)
133 BDRVQcow2State
*s
= bs
->opaque
;
134 g_free(s
->refcount_table
);
138 static uint64_t get_refcount_ro0(const void *refcount_array
, uint64_t index
)
140 return (((const uint8_t *)refcount_array
)[index
/ 8] >> (index
% 8)) & 0x1;
143 static void set_refcount_ro0(void *refcount_array
, uint64_t index
,
146 assert(!(value
>> 1));
147 ((uint8_t *)refcount_array
)[index
/ 8] &= ~(0x1 << (index
% 8));
148 ((uint8_t *)refcount_array
)[index
/ 8] |= value
<< (index
% 8);
151 static uint64_t get_refcount_ro1(const void *refcount_array
, uint64_t index
)
153 return (((const uint8_t *)refcount_array
)[index
/ 4] >> (2 * (index
% 4)))
157 static void set_refcount_ro1(void *refcount_array
, uint64_t index
,
160 assert(!(value
>> 2));
161 ((uint8_t *)refcount_array
)[index
/ 4] &= ~(0x3 << (2 * (index
% 4)));
162 ((uint8_t *)refcount_array
)[index
/ 4] |= value
<< (2 * (index
% 4));
165 static uint64_t get_refcount_ro2(const void *refcount_array
, uint64_t index
)
167 return (((const uint8_t *)refcount_array
)[index
/ 2] >> (4 * (index
% 2)))
171 static void set_refcount_ro2(void *refcount_array
, uint64_t index
,
174 assert(!(value
>> 4));
175 ((uint8_t *)refcount_array
)[index
/ 2] &= ~(0xf << (4 * (index
% 2)));
176 ((uint8_t *)refcount_array
)[index
/ 2] |= value
<< (4 * (index
% 2));
179 static uint64_t get_refcount_ro3(const void *refcount_array
, uint64_t index
)
181 return ((const uint8_t *)refcount_array
)[index
];
184 static void set_refcount_ro3(void *refcount_array
, uint64_t index
,
187 assert(!(value
>> 8));
188 ((uint8_t *)refcount_array
)[index
] = value
;
191 static uint64_t get_refcount_ro4(const void *refcount_array
, uint64_t index
)
193 return be16_to_cpu(((const uint16_t *)refcount_array
)[index
]);
196 static void set_refcount_ro4(void *refcount_array
, uint64_t index
,
199 assert(!(value
>> 16));
200 ((uint16_t *)refcount_array
)[index
] = cpu_to_be16(value
);
203 static uint64_t get_refcount_ro5(const void *refcount_array
, uint64_t index
)
205 return be32_to_cpu(((const uint32_t *)refcount_array
)[index
]);
208 static void set_refcount_ro5(void *refcount_array
, uint64_t index
,
211 assert(!(value
>> 32));
212 ((uint32_t *)refcount_array
)[index
] = cpu_to_be32(value
);
215 static uint64_t get_refcount_ro6(const void *refcount_array
, uint64_t index
)
217 return be64_to_cpu(((const uint64_t *)refcount_array
)[index
]);
220 static void set_refcount_ro6(void *refcount_array
, uint64_t index
,
223 ((uint64_t *)refcount_array
)[index
] = cpu_to_be64(value
);
227 static int load_refcount_block(BlockDriverState
*bs
,
228 int64_t refcount_block_offset
,
229 void **refcount_block
)
231 BDRVQcow2State
*s
= bs
->opaque
;
233 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_LOAD
);
234 return qcow2_cache_get(bs
, s
->refcount_block_cache
, refcount_block_offset
,
239 * Retrieves the refcount of the cluster given by its index and stores it in
240 * *refcount. Returns 0 on success and -errno on failure.
242 int qcow2_get_refcount(BlockDriverState
*bs
, int64_t cluster_index
,
245 BDRVQcow2State
*s
= bs
->opaque
;
246 uint64_t refcount_table_index
, block_index
;
247 int64_t refcount_block_offset
;
249 void *refcount_block
;
251 refcount_table_index
= cluster_index
>> s
->refcount_block_bits
;
252 if (refcount_table_index
>= s
->refcount_table_size
) {
256 refcount_block_offset
=
257 s
->refcount_table
[refcount_table_index
] & REFT_OFFSET_MASK
;
258 if (!refcount_block_offset
) {
263 if (offset_into_cluster(s
, refcount_block_offset
)) {
264 qcow2_signal_corruption(bs
, true, -1, -1, "Refblock offset %#" PRIx64
265 " unaligned (reftable index: %#" PRIx64
")",
266 refcount_block_offset
, refcount_table_index
);
270 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refcount_block_offset
,
276 block_index
= cluster_index
& (s
->refcount_block_size
- 1);
277 *refcount
= s
->get_refcount(refcount_block
, block_index
);
279 qcow2_cache_put(bs
, s
->refcount_block_cache
, &refcount_block
);
285 * Rounds the refcount table size up to avoid growing the table for each single
286 * refcount block that is allocated.
288 static unsigned int next_refcount_table_size(BDRVQcow2State
*s
,
289 unsigned int min_size
)
291 unsigned int min_clusters
= (min_size
>> (s
->cluster_bits
- 3)) + 1;
292 unsigned int refcount_table_clusters
=
293 MAX(1, s
->refcount_table_size
>> (s
->cluster_bits
- 3));
295 while (min_clusters
> refcount_table_clusters
) {
296 refcount_table_clusters
= (refcount_table_clusters
* 3 + 1) / 2;
299 return refcount_table_clusters
<< (s
->cluster_bits
- 3);
303 /* Checks if two offsets are described by the same refcount block */
304 static int in_same_refcount_block(BDRVQcow2State
*s
, uint64_t offset_a
,
307 uint64_t block_a
= offset_a
>> (s
->cluster_bits
+ s
->refcount_block_bits
);
308 uint64_t block_b
= offset_b
>> (s
->cluster_bits
+ s
->refcount_block_bits
);
310 return (block_a
== block_b
);
314 * Loads a refcount block. If it doesn't exist yet, it is allocated first
315 * (including growing the refcount table if needed).
317 * Returns 0 on success or -errno in error case
319 static int alloc_refcount_block(BlockDriverState
*bs
,
320 int64_t cluster_index
, void **refcount_block
)
322 BDRVQcow2State
*s
= bs
->opaque
;
323 unsigned int refcount_table_index
;
326 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC
);
328 /* Find the refcount block for the given cluster */
329 refcount_table_index
= cluster_index
>> s
->refcount_block_bits
;
331 if (refcount_table_index
< s
->refcount_table_size
) {
333 uint64_t refcount_block_offset
=
334 s
->refcount_table
[refcount_table_index
] & REFT_OFFSET_MASK
;
336 /* If it's already there, we're done */
337 if (refcount_block_offset
) {
338 if (offset_into_cluster(s
, refcount_block_offset
)) {
339 qcow2_signal_corruption(bs
, true, -1, -1, "Refblock offset %#"
340 PRIx64
" unaligned (reftable index: "
341 "%#x)", refcount_block_offset
,
342 refcount_table_index
);
346 return load_refcount_block(bs
, refcount_block_offset
,
352 * If we came here, we need to allocate something. Something is at least
353 * a cluster for the new refcount block. It may also include a new refcount
354 * table if the old refcount table is too small.
356 * Note that allocating clusters here needs some special care:
358 * - We can't use the normal qcow2_alloc_clusters(), it would try to
359 * increase the refcount and very likely we would end up with an endless
360 * recursion. Instead we must place the refcount blocks in a way that
361 * they can describe them themselves.
363 * - We need to consider that at this point we are inside update_refcounts
364 * and potentially doing an initial refcount increase. This means that
365 * some clusters have already been allocated by the caller, but their
366 * refcount isn't accurate yet. If we allocate clusters for metadata, we
367 * need to return -EAGAIN to signal the caller that it needs to restart
368 * the search for free clusters.
370 * - alloc_clusters_noref and qcow2_free_clusters may load a different
371 * refcount block into the cache
374 *refcount_block
= NULL
;
376 /* We write to the refcount table, so we might depend on L2 tables */
377 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
382 /* Allocate the refcount block itself and mark it as used */
383 int64_t new_block
= alloc_clusters_noref(bs
, s
->cluster_size
);
389 fprintf(stderr
, "qcow2: Allocate refcount block %d for %" PRIx64
391 refcount_table_index
, cluster_index
<< s
->cluster_bits
, new_block
);
394 if (in_same_refcount_block(s
, new_block
, cluster_index
<< s
->cluster_bits
)) {
395 /* Zero the new refcount block before updating it */
396 ret
= qcow2_cache_get_empty(bs
, s
->refcount_block_cache
, new_block
,
402 memset(*refcount_block
, 0, s
->cluster_size
);
404 /* The block describes itself, need to update the cache */
405 int block_index
= (new_block
>> s
->cluster_bits
) &
406 (s
->refcount_block_size
- 1);
407 s
->set_refcount(*refcount_block
, block_index
, 1);
409 /* Described somewhere else. This can recurse at most twice before we
410 * arrive at a block that describes itself. */
411 ret
= update_refcount(bs
, new_block
, s
->cluster_size
, 1, false,
412 QCOW2_DISCARD_NEVER
);
417 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
422 /* Initialize the new refcount block only after updating its refcount,
423 * update_refcount uses the refcount cache itself */
424 ret
= qcow2_cache_get_empty(bs
, s
->refcount_block_cache
, new_block
,
430 memset(*refcount_block
, 0, s
->cluster_size
);
433 /* Now the new refcount block needs to be written to disk */
434 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE
);
435 qcow2_cache_entry_mark_dirty(bs
, s
->refcount_block_cache
, *refcount_block
);
436 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
441 /* If the refcount table is big enough, just hook the block up there */
442 if (refcount_table_index
< s
->refcount_table_size
) {
443 uint64_t data64
= cpu_to_be64(new_block
);
444 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_HOOKUP
);
445 ret
= bdrv_pwrite_sync(bs
->file
,
446 s
->refcount_table_offset
+ refcount_table_index
* sizeof(uint64_t),
447 &data64
, sizeof(data64
));
452 s
->refcount_table
[refcount_table_index
] = new_block
;
453 /* If there's a hole in s->refcount_table then it can happen
454 * that refcount_table_index < s->max_refcount_table_index */
455 s
->max_refcount_table_index
=
456 MAX(s
->max_refcount_table_index
, refcount_table_index
);
458 /* The new refcount block may be where the caller intended to put its
459 * data, so let it restart the search. */
463 qcow2_cache_put(bs
, s
->refcount_block_cache
, refcount_block
);
466 * If we come here, we need to grow the refcount table. Again, a new
467 * refcount table needs some space and we can't simply allocate to avoid
470 * Therefore let's grab new refcount blocks at the end of the image, which
471 * will describe themselves and the new refcount table. This way we can
472 * reference them only in the new table and do the switch to the new
473 * refcount table at once without producing an inconsistent state in
476 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_GROW
);
478 /* Calculate the number of refcount blocks needed so far; this will be the
479 * basis for calculating the index of the first cluster used for the
480 * self-describing refcount structures which we are about to create.
482 * Because we reached this point, there cannot be any refcount entries for
483 * cluster_index or higher indices yet. However, because new_block has been
484 * allocated to describe that cluster (and it will assume this role later
485 * on), we cannot use that index; also, new_block may actually have a higher
486 * cluster index than cluster_index, so it needs to be taken into account
487 * here (and 1 needs to be added to its value because that cluster is used).
489 uint64_t blocks_used
= DIV_ROUND_UP(MAX(cluster_index
+ 1,
490 (new_block
>> s
->cluster_bits
) + 1),
491 s
->refcount_block_size
);
493 if (blocks_used
> QCOW_MAX_REFTABLE_SIZE
/ sizeof(uint64_t)) {
497 /* And now we need at least one block more for the new metadata */
498 uint64_t table_size
= next_refcount_table_size(s
, blocks_used
+ 1);
499 uint64_t last_table_size
;
500 uint64_t blocks_clusters
;
502 uint64_t table_clusters
=
503 size_to_clusters(s
, table_size
* sizeof(uint64_t));
504 blocks_clusters
= 1 +
505 DIV_ROUND_UP(table_clusters
, s
->refcount_block_size
);
506 uint64_t meta_clusters
= table_clusters
+ blocks_clusters
;
508 last_table_size
= table_size
;
509 table_size
= next_refcount_table_size(s
, blocks_used
+
510 DIV_ROUND_UP(meta_clusters
, s
->refcount_block_size
));
512 } while (last_table_size
!= table_size
);
515 fprintf(stderr
, "qcow2: Grow refcount table %" PRId32
" => %" PRId64
"\n",
516 s
->refcount_table_size
, table_size
);
519 /* Create the new refcount table and blocks */
520 uint64_t meta_offset
= (blocks_used
* s
->refcount_block_size
) *
522 uint64_t table_offset
= meta_offset
+ blocks_clusters
* s
->cluster_size
;
523 uint64_t *new_table
= g_try_new0(uint64_t, table_size
);
524 void *new_blocks
= g_try_malloc0(blocks_clusters
* s
->cluster_size
);
526 assert(table_size
> 0 && blocks_clusters
> 0);
527 if (new_table
== NULL
|| new_blocks
== NULL
) {
532 /* Fill the new refcount table */
533 memcpy(new_table
, s
->refcount_table
,
534 s
->refcount_table_size
* sizeof(uint64_t));
535 new_table
[refcount_table_index
] = new_block
;
538 for (i
= 0; i
< blocks_clusters
; i
++) {
539 new_table
[blocks_used
+ i
] = meta_offset
+ (i
* s
->cluster_size
);
542 /* Fill the refcount blocks */
543 uint64_t table_clusters
= size_to_clusters(s
, table_size
* sizeof(uint64_t));
545 for (i
= 0; i
< table_clusters
+ blocks_clusters
; i
++) {
546 s
->set_refcount(new_blocks
, block
++, 1);
549 /* Write refcount blocks to disk */
550 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS
);
551 ret
= bdrv_pwrite_sync(bs
->file
, meta_offset
, new_blocks
,
552 blocks_clusters
* s
->cluster_size
);
559 /* Write refcount table to disk */
560 for(i
= 0; i
< table_size
; i
++) {
561 cpu_to_be64s(&new_table
[i
]);
564 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE
);
565 ret
= bdrv_pwrite_sync(bs
->file
, table_offset
, new_table
,
566 table_size
* sizeof(uint64_t));
571 for(i
= 0; i
< table_size
; i
++) {
572 be64_to_cpus(&new_table
[i
]);
575 /* Hook up the new refcount table in the qcow2 header */
580 data
.d64
= cpu_to_be64(table_offset
);
581 data
.d32
= cpu_to_be32(table_clusters
);
582 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE
);
583 ret
= bdrv_pwrite_sync(bs
->file
,
584 offsetof(QCowHeader
, refcount_table_offset
),
585 &data
, sizeof(data
));
590 /* And switch it in memory */
591 uint64_t old_table_offset
= s
->refcount_table_offset
;
592 uint64_t old_table_size
= s
->refcount_table_size
;
594 g_free(s
->refcount_table
);
595 s
->refcount_table
= new_table
;
596 s
->refcount_table_size
= table_size
;
597 s
->refcount_table_offset
= table_offset
;
598 update_max_refcount_table_index(s
);
600 /* Free old table. */
601 qcow2_free_clusters(bs
, old_table_offset
, old_table_size
* sizeof(uint64_t),
602 QCOW2_DISCARD_OTHER
);
604 ret
= load_refcount_block(bs
, new_block
, refcount_block
);
609 /* If we were trying to do the initial refcount update for some cluster
610 * allocation, we might have used the same clusters to store newly
611 * allocated metadata. Make the caller search some new space. */
618 if (*refcount_block
!= NULL
) {
619 qcow2_cache_put(bs
, s
->refcount_block_cache
, refcount_block
);
624 void qcow2_process_discards(BlockDriverState
*bs
, int ret
)
626 BDRVQcow2State
*s
= bs
->opaque
;
627 Qcow2DiscardRegion
*d
, *next
;
629 QTAILQ_FOREACH_SAFE(d
, &s
->discards
, next
, next
) {
630 QTAILQ_REMOVE(&s
->discards
, d
, next
);
632 /* Discard is optional, ignore the return value */
634 bdrv_pdiscard(bs
->file
->bs
, d
->offset
, d
->bytes
);
641 static void update_refcount_discard(BlockDriverState
*bs
,
642 uint64_t offset
, uint64_t length
)
644 BDRVQcow2State
*s
= bs
->opaque
;
645 Qcow2DiscardRegion
*d
, *p
, *next
;
647 QTAILQ_FOREACH(d
, &s
->discards
, next
) {
648 uint64_t new_start
= MIN(offset
, d
->offset
);
649 uint64_t new_end
= MAX(offset
+ length
, d
->offset
+ d
->bytes
);
651 if (new_end
- new_start
<= length
+ d
->bytes
) {
652 /* There can't be any overlap, areas ending up here have no
653 * references any more and therefore shouldn't get freed another
655 assert(d
->bytes
+ length
== new_end
- new_start
);
656 d
->offset
= new_start
;
657 d
->bytes
= new_end
- new_start
;
662 d
= g_malloc(sizeof(*d
));
663 *d
= (Qcow2DiscardRegion
) {
668 QTAILQ_INSERT_TAIL(&s
->discards
, d
, next
);
671 /* Merge discard requests if they are adjacent now */
672 QTAILQ_FOREACH_SAFE(p
, &s
->discards
, next
, next
) {
674 || p
->offset
> d
->offset
+ d
->bytes
675 || d
->offset
> p
->offset
+ p
->bytes
)
680 /* Still no overlap possible */
681 assert(p
->offset
== d
->offset
+ d
->bytes
682 || d
->offset
== p
->offset
+ p
->bytes
);
684 QTAILQ_REMOVE(&s
->discards
, p
, next
);
685 d
->offset
= MIN(d
->offset
, p
->offset
);
686 d
->bytes
+= p
->bytes
;
691 /* XXX: cache several refcount block clusters ? */
692 /* @addend is the absolute value of the addend; if @decrease is set, @addend
693 * will be subtracted from the current refcount, otherwise it will be added */
694 static int QEMU_WARN_UNUSED_RESULT
update_refcount(BlockDriverState
*bs
,
699 enum qcow2_discard_type type
)
701 BDRVQcow2State
*s
= bs
->opaque
;
702 int64_t start
, last
, cluster_offset
;
703 void *refcount_block
= NULL
;
704 int64_t old_table_index
= -1;
708 fprintf(stderr
, "update_refcount: offset=%" PRId64
" size=%" PRId64
709 " addend=%s%" PRIu64
"\n", offset
, length
, decrease
? "-" : "",
714 } else if (length
== 0) {
719 qcow2_cache_set_dependency(bs
, s
->refcount_block_cache
,
723 start
= start_of_cluster(s
, offset
);
724 last
= start_of_cluster(s
, offset
+ length
- 1);
725 for(cluster_offset
= start
; cluster_offset
<= last
;
726 cluster_offset
+= s
->cluster_size
)
730 int64_t cluster_index
= cluster_offset
>> s
->cluster_bits
;
731 int64_t table_index
= cluster_index
>> s
->refcount_block_bits
;
733 /* Load the refcount block and allocate it if needed */
734 if (table_index
!= old_table_index
) {
735 if (refcount_block
) {
736 qcow2_cache_put(bs
, s
->refcount_block_cache
, &refcount_block
);
738 ret
= alloc_refcount_block(bs
, cluster_index
, &refcount_block
);
743 old_table_index
= table_index
;
745 qcow2_cache_entry_mark_dirty(bs
, s
->refcount_block_cache
,
748 /* we can update the count and save it */
749 block_index
= cluster_index
& (s
->refcount_block_size
- 1);
751 refcount
= s
->get_refcount(refcount_block
, block_index
);
752 if (decrease
? (refcount
- addend
> refcount
)
753 : (refcount
+ addend
< refcount
||
754 refcount
+ addend
> s
->refcount_max
))
764 if (refcount
== 0 && cluster_index
< s
->free_cluster_index
) {
765 s
->free_cluster_index
= cluster_index
;
767 s
->set_refcount(refcount_block
, block_index
, refcount
);
769 if (refcount
== 0 && s
->discard_passthrough
[type
]) {
770 update_refcount_discard(bs
, cluster_offset
, s
->cluster_size
);
776 if (!s
->cache_discards
) {
777 qcow2_process_discards(bs
, ret
);
780 /* Write last changed block to disk */
781 if (refcount_block
) {
782 qcow2_cache_put(bs
, s
->refcount_block_cache
, &refcount_block
);
786 * Try do undo any updates if an error is returned (This may succeed in
787 * some cases like ENOSPC for allocating a new refcount block)
791 dummy
= update_refcount(bs
, offset
, cluster_offset
- offset
, addend
,
792 !decrease
, QCOW2_DISCARD_NEVER
);
800 * Increases or decreases the refcount of a given cluster.
802 * @addend is the absolute value of the addend; if @decrease is set, @addend
803 * will be subtracted from the current refcount, otherwise it will be added.
805 * On success 0 is returned; on failure -errno is returned.
807 int qcow2_update_cluster_refcount(BlockDriverState
*bs
,
808 int64_t cluster_index
,
809 uint64_t addend
, bool decrease
,
810 enum qcow2_discard_type type
)
812 BDRVQcow2State
*s
= bs
->opaque
;
815 ret
= update_refcount(bs
, cluster_index
<< s
->cluster_bits
, 1, addend
,
826 /*********************************************************/
827 /* cluster allocation functions */
831 /* return < 0 if error */
832 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, uint64_t size
)
834 BDRVQcow2State
*s
= bs
->opaque
;
835 uint64_t i
, nb_clusters
, refcount
;
838 /* We can't allocate clusters if they may still be queued for discard. */
839 if (s
->cache_discards
) {
840 qcow2_process_discards(bs
, 0);
843 nb_clusters
= size_to_clusters(s
, size
);
845 for(i
= 0; i
< nb_clusters
; i
++) {
846 uint64_t next_cluster_index
= s
->free_cluster_index
++;
847 ret
= qcow2_get_refcount(bs
, next_cluster_index
, &refcount
);
851 } else if (refcount
!= 0) {
856 /* Make sure that all offsets in the "allocated" range are representable
858 if (s
->free_cluster_index
> 0 &&
859 s
->free_cluster_index
- 1 > (INT64_MAX
>> s
->cluster_bits
))
865 fprintf(stderr
, "alloc_clusters: size=%" PRId64
" -> %" PRId64
"\n",
867 (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
);
869 return (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
;
872 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, uint64_t size
)
877 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC
);
879 offset
= alloc_clusters_noref(bs
, size
);
884 ret
= update_refcount(bs
, offset
, size
, 1, false, QCOW2_DISCARD_NEVER
);
885 } while (ret
== -EAGAIN
);
894 int64_t qcow2_alloc_clusters_at(BlockDriverState
*bs
, uint64_t offset
,
897 BDRVQcow2State
*s
= bs
->opaque
;
898 uint64_t cluster_index
, refcount
;
902 assert(nb_clusters
>= 0);
903 if (nb_clusters
== 0) {
908 /* Check how many clusters there are free */
909 cluster_index
= offset
>> s
->cluster_bits
;
910 for(i
= 0; i
< nb_clusters
; i
++) {
911 ret
= qcow2_get_refcount(bs
, cluster_index
++, &refcount
);
914 } else if (refcount
!= 0) {
919 /* And then allocate them */
920 ret
= update_refcount(bs
, offset
, i
<< s
->cluster_bits
, 1, false,
921 QCOW2_DISCARD_NEVER
);
922 } while (ret
== -EAGAIN
);
931 /* only used to allocate compressed sectors. We try to allocate
932 contiguous sectors. size must be <= cluster_size */
933 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
)
935 BDRVQcow2State
*s
= bs
->opaque
;
937 size_t free_in_cluster
;
940 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC_BYTES
);
941 assert(size
> 0 && size
<= s
->cluster_size
);
942 assert(!s
->free_byte_offset
|| offset_into_cluster(s
, s
->free_byte_offset
));
944 offset
= s
->free_byte_offset
;
948 ret
= qcow2_get_refcount(bs
, offset
>> s
->cluster_bits
, &refcount
);
953 if (refcount
== s
->refcount_max
) {
958 free_in_cluster
= s
->cluster_size
- offset_into_cluster(s
, offset
);
960 if (!offset
|| free_in_cluster
< size
) {
961 int64_t new_cluster
= alloc_clusters_noref(bs
, s
->cluster_size
);
962 if (new_cluster
< 0) {
966 if (!offset
|| ROUND_UP(offset
, s
->cluster_size
) != new_cluster
) {
967 offset
= new_cluster
;
968 free_in_cluster
= s
->cluster_size
;
970 free_in_cluster
+= s
->cluster_size
;
975 ret
= update_refcount(bs
, offset
, size
, 1, false, QCOW2_DISCARD_NEVER
);
979 } while (ret
== -EAGAIN
);
984 /* The cluster refcount was incremented; refcount blocks must be flushed
985 * before the caller's L2 table updates. */
986 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
, s
->refcount_block_cache
);
988 s
->free_byte_offset
= offset
+ size
;
989 if (!offset_into_cluster(s
, s
->free_byte_offset
)) {
990 s
->free_byte_offset
= 0;
996 void qcow2_free_clusters(BlockDriverState
*bs
,
997 int64_t offset
, int64_t size
,
998 enum qcow2_discard_type type
)
1002 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_FREE
);
1003 ret
= update_refcount(bs
, offset
, size
, 1, true, type
);
1005 fprintf(stderr
, "qcow2_free_clusters failed: %s\n", strerror(-ret
));
1006 /* TODO Remember the clusters to free them later and avoid leaking */
1011 * Free a cluster using its L2 entry (handles clusters of all types, e.g.
1012 * normal cluster, compressed cluster, etc.)
1014 void qcow2_free_any_clusters(BlockDriverState
*bs
, uint64_t l2_entry
,
1015 int nb_clusters
, enum qcow2_discard_type type
)
1017 BDRVQcow2State
*s
= bs
->opaque
;
1019 switch (qcow2_get_cluster_type(l2_entry
)) {
1020 case QCOW2_CLUSTER_COMPRESSED
:
1023 nb_csectors
= ((l2_entry
>> s
->csize_shift
) &
1025 qcow2_free_clusters(bs
,
1026 (l2_entry
& s
->cluster_offset_mask
) & ~511,
1027 nb_csectors
* 512, type
);
1030 case QCOW2_CLUSTER_NORMAL
:
1031 case QCOW2_CLUSTER_ZERO
:
1032 if (l2_entry
& L2E_OFFSET_MASK
) {
1033 if (offset_into_cluster(s
, l2_entry
& L2E_OFFSET_MASK
)) {
1034 qcow2_signal_corruption(bs
, false, -1, -1,
1035 "Cannot free unaligned cluster %#llx",
1036 l2_entry
& L2E_OFFSET_MASK
);
1038 qcow2_free_clusters(bs
, l2_entry
& L2E_OFFSET_MASK
,
1039 nb_clusters
<< s
->cluster_bits
, type
);
1043 case QCOW2_CLUSTER_UNALLOCATED
:
1052 /*********************************************************/
1053 /* snapshots and image creation */
1057 /* update the refcounts of snapshots and the copied flag */
1058 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
1059 int64_t l1_table_offset
, int l1_size
, int addend
)
1061 BDRVQcow2State
*s
= bs
->opaque
;
1062 uint64_t *l1_table
, *l2_table
, l2_offset
, entry
, l1_size2
, refcount
;
1063 bool l1_allocated
= false;
1064 int64_t old_entry
, old_l2_offset
;
1065 int i
, j
, l1_modified
= 0, nb_csectors
;
1068 assert(addend
>= -1 && addend
<= 1);
1072 l1_size2
= l1_size
* sizeof(uint64_t);
1074 s
->cache_discards
= true;
1076 /* WARNING: qcow2_snapshot_goto relies on this function not using the
1077 * l1_table_offset when it is the current s->l1_table_offset! Be careful
1078 * when changing this! */
1079 if (l1_table_offset
!= s
->l1_table_offset
) {
1080 l1_table
= g_try_malloc0(align_offset(l1_size2
, 512));
1081 if (l1_size2
&& l1_table
== NULL
) {
1085 l1_allocated
= true;
1087 ret
= bdrv_pread(bs
->file
, l1_table_offset
, l1_table
, l1_size2
);
1092 for (i
= 0; i
< l1_size
; i
++) {
1093 be64_to_cpus(&l1_table
[i
]);
1096 assert(l1_size
== s
->l1_size
);
1097 l1_table
= s
->l1_table
;
1098 l1_allocated
= false;
1101 for (i
= 0; i
< l1_size
; i
++) {
1102 l2_offset
= l1_table
[i
];
1104 old_l2_offset
= l2_offset
;
1105 l2_offset
&= L1E_OFFSET_MASK
;
1107 if (offset_into_cluster(s
, l2_offset
)) {
1108 qcow2_signal_corruption(bs
, true, -1, -1, "L2 table offset %#"
1109 PRIx64
" unaligned (L1 index: %#x)",
1115 ret
= qcow2_cache_get(bs
, s
->l2_table_cache
, l2_offset
,
1116 (void**) &l2_table
);
1121 for (j
= 0; j
< s
->l2_size
; j
++) {
1122 uint64_t cluster_index
;
1125 entry
= be64_to_cpu(l2_table
[j
]);
1127 entry
&= ~QCOW_OFLAG_COPIED
;
1128 offset
= entry
& L2E_OFFSET_MASK
;
1130 switch (qcow2_get_cluster_type(entry
)) {
1131 case QCOW2_CLUSTER_COMPRESSED
:
1132 nb_csectors
= ((entry
>> s
->csize_shift
) &
1135 ret
= update_refcount(bs
,
1136 (entry
& s
->cluster_offset_mask
) & ~511,
1137 nb_csectors
* 512, abs(addend
), addend
< 0,
1138 QCOW2_DISCARD_SNAPSHOT
);
1143 /* compressed clusters are never modified */
1147 case QCOW2_CLUSTER_NORMAL
:
1148 case QCOW2_CLUSTER_ZERO
:
1149 if (offset_into_cluster(s
, offset
)) {
1150 qcow2_signal_corruption(bs
, true, -1, -1, "Data "
1151 "cluster offset %#" PRIx64
1152 " unaligned (L2 offset: %#"
1153 PRIx64
", L2 index: %#x)",
1154 offset
, l2_offset
, j
);
1159 cluster_index
= offset
>> s
->cluster_bits
;
1160 if (!cluster_index
) {
1166 ret
= qcow2_update_cluster_refcount(bs
,
1167 cluster_index
, abs(addend
), addend
< 0,
1168 QCOW2_DISCARD_SNAPSHOT
);
1174 ret
= qcow2_get_refcount(bs
, cluster_index
, &refcount
);
1180 case QCOW2_CLUSTER_UNALLOCATED
:
1188 if (refcount
== 1) {
1189 entry
|= QCOW_OFLAG_COPIED
;
1191 if (entry
!= old_entry
) {
1193 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
,
1194 s
->refcount_block_cache
);
1196 l2_table
[j
] = cpu_to_be64(entry
);
1197 qcow2_cache_entry_mark_dirty(bs
, s
->l2_table_cache
,
1202 qcow2_cache_put(bs
, s
->l2_table_cache
, (void **) &l2_table
);
1205 ret
= qcow2_update_cluster_refcount(bs
, l2_offset
>>
1207 abs(addend
), addend
< 0,
1208 QCOW2_DISCARD_SNAPSHOT
);
1213 ret
= qcow2_get_refcount(bs
, l2_offset
>> s
->cluster_bits
,
1217 } else if (refcount
== 1) {
1218 l2_offset
|= QCOW_OFLAG_COPIED
;
1220 if (l2_offset
!= old_l2_offset
) {
1221 l1_table
[i
] = l2_offset
;
1227 ret
= bdrv_flush(bs
);
1230 qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
1233 s
->cache_discards
= false;
1234 qcow2_process_discards(bs
, ret
);
1236 /* Update L1 only if it isn't deleted anyway (addend = -1) */
1237 if (ret
== 0 && addend
>= 0 && l1_modified
) {
1238 for (i
= 0; i
< l1_size
; i
++) {
1239 cpu_to_be64s(&l1_table
[i
]);
1242 ret
= bdrv_pwrite_sync(bs
->file
, l1_table_offset
,
1243 l1_table
, l1_size2
);
1245 for (i
= 0; i
< l1_size
; i
++) {
1246 be64_to_cpus(&l1_table
[i
]);
1257 /*********************************************************/
1258 /* refcount checking functions */
1261 static uint64_t refcount_array_byte_size(BDRVQcow2State
*s
, uint64_t entries
)
1263 /* This assertion holds because there is no way we can address more than
1264 * 2^(64 - 9) clusters at once (with cluster size 512 = 2^9, and because
1265 * offsets have to be representable in bytes); due to every cluster
1266 * corresponding to one refcount entry, we are well below that limit */
1267 assert(entries
< (UINT64_C(1) << (64 - 9)));
1269 /* Thanks to the assertion this will not overflow, because
1270 * s->refcount_order < 7.
1271 * (note: x << s->refcount_order == x * s->refcount_bits) */
1272 return DIV_ROUND_UP(entries
<< s
->refcount_order
, 8);
1276 * Reallocates *array so that it can hold new_size entries. *size must contain
1277 * the current number of entries in *array. If the reallocation fails, *array
1278 * and *size will not be modified and -errno will be returned. If the
1279 * reallocation is successful, *array will be set to the new buffer, *size
1280 * will be set to new_size and 0 will be returned. The size of the reallocated
1281 * refcount array buffer will be aligned to a cluster boundary, and the newly
1282 * allocated area will be zeroed.
1284 static int realloc_refcount_array(BDRVQcow2State
*s
, void **array
,
1285 int64_t *size
, int64_t new_size
)
1287 int64_t old_byte_size
, new_byte_size
;
1290 /* Round to clusters so the array can be directly written to disk */
1291 old_byte_size
= size_to_clusters(s
, refcount_array_byte_size(s
, *size
))
1293 new_byte_size
= size_to_clusters(s
, refcount_array_byte_size(s
, new_size
))
1296 if (new_byte_size
== old_byte_size
) {
1301 assert(new_byte_size
> 0);
1303 if (new_byte_size
> SIZE_MAX
) {
1307 new_ptr
= g_try_realloc(*array
, new_byte_size
);
1312 if (new_byte_size
> old_byte_size
) {
1313 memset((char *)new_ptr
+ old_byte_size
, 0,
1314 new_byte_size
- old_byte_size
);
1324 * Increases the refcount for a range of clusters in a given refcount table.
1325 * This is used to construct a temporary refcount table out of L1 and L2 tables
1326 * which can be compared to the refcount table saved in the image.
1328 * Modifies the number of errors in res.
1330 static int inc_refcounts(BlockDriverState
*bs
,
1331 BdrvCheckResult
*res
,
1332 void **refcount_table
,
1333 int64_t *refcount_table_size
,
1334 int64_t offset
, int64_t size
)
1336 BDRVQcow2State
*s
= bs
->opaque
;
1337 uint64_t start
, last
, cluster_offset
, k
, refcount
;
1344 start
= start_of_cluster(s
, offset
);
1345 last
= start_of_cluster(s
, offset
+ size
- 1);
1346 for(cluster_offset
= start
; cluster_offset
<= last
;
1347 cluster_offset
+= s
->cluster_size
) {
1348 k
= cluster_offset
>> s
->cluster_bits
;
1349 if (k
>= *refcount_table_size
) {
1350 ret
= realloc_refcount_array(s
, refcount_table
,
1351 refcount_table_size
, k
+ 1);
1353 res
->check_errors
++;
1358 refcount
= s
->get_refcount(*refcount_table
, k
);
1359 if (refcount
== s
->refcount_max
) {
1360 fprintf(stderr
, "ERROR: overflow cluster offset=0x%" PRIx64
1361 "\n", cluster_offset
);
1362 fprintf(stderr
, "Use qemu-img amend to increase the refcount entry "
1363 "width or qemu-img convert to create a clean copy if the "
1364 "image cannot be opened for writing\n");
1368 s
->set_refcount(*refcount_table
, k
, refcount
+ 1);
1374 /* Flags for check_refcounts_l1() and check_refcounts_l2() */
1376 CHECK_FRAG_INFO
= 0x2, /* update BlockFragInfo counters */
1380 * Increases the refcount in the given refcount table for the all clusters
1381 * referenced in the L2 table. While doing so, performs some checks on L2
1384 * Returns the number of errors found by the checks or -errno if an internal
1387 static int check_refcounts_l2(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1388 void **refcount_table
,
1389 int64_t *refcount_table_size
, int64_t l2_offset
,
1392 BDRVQcow2State
*s
= bs
->opaque
;
1393 uint64_t *l2_table
, l2_entry
;
1394 uint64_t next_contiguous_offset
= 0;
1395 int i
, l2_size
, nb_csectors
, ret
;
1397 /* Read L2 table from disk */
1398 l2_size
= s
->l2_size
* sizeof(uint64_t);
1399 l2_table
= g_malloc(l2_size
);
1401 ret
= bdrv_pread(bs
->file
, l2_offset
, l2_table
, l2_size
);
1403 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l2\n");
1404 res
->check_errors
++;
1408 /* Do the actual checks */
1409 for(i
= 0; i
< s
->l2_size
; i
++) {
1410 l2_entry
= be64_to_cpu(l2_table
[i
]);
1412 switch (qcow2_get_cluster_type(l2_entry
)) {
1413 case QCOW2_CLUSTER_COMPRESSED
:
1414 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
1415 if (l2_entry
& QCOW_OFLAG_COPIED
) {
1416 fprintf(stderr
, "ERROR: cluster %" PRId64
": "
1417 "copied flag must never be set for compressed "
1418 "clusters\n", l2_entry
>> s
->cluster_bits
);
1419 l2_entry
&= ~QCOW_OFLAG_COPIED
;
1423 /* Mark cluster as used */
1424 nb_csectors
= ((l2_entry
>> s
->csize_shift
) &
1426 l2_entry
&= s
->cluster_offset_mask
;
1427 ret
= inc_refcounts(bs
, res
, refcount_table
, refcount_table_size
,
1428 l2_entry
& ~511, nb_csectors
* 512);
1433 if (flags
& CHECK_FRAG_INFO
) {
1434 res
->bfi
.allocated_clusters
++;
1435 res
->bfi
.compressed_clusters
++;
1437 /* Compressed clusters are fragmented by nature. Since they
1438 * take up sub-sector space but we only have sector granularity
1439 * I/O we need to re-read the same sectors even for adjacent
1440 * compressed clusters.
1442 res
->bfi
.fragmented_clusters
++;
1446 case QCOW2_CLUSTER_ZERO
:
1447 if ((l2_entry
& L2E_OFFSET_MASK
) == 0) {
1452 case QCOW2_CLUSTER_NORMAL
:
1454 uint64_t offset
= l2_entry
& L2E_OFFSET_MASK
;
1456 if (flags
& CHECK_FRAG_INFO
) {
1457 res
->bfi
.allocated_clusters
++;
1458 if (next_contiguous_offset
&&
1459 offset
!= next_contiguous_offset
) {
1460 res
->bfi
.fragmented_clusters
++;
1462 next_contiguous_offset
= offset
+ s
->cluster_size
;
1465 /* Mark cluster as used */
1466 ret
= inc_refcounts(bs
, res
, refcount_table
, refcount_table_size
,
1467 offset
, s
->cluster_size
);
1472 /* Correct offsets are cluster aligned */
1473 if (offset_into_cluster(s
, offset
)) {
1474 fprintf(stderr
, "ERROR offset=%" PRIx64
": Cluster is not "
1475 "properly aligned; L2 entry corrupted.\n", offset
);
1481 case QCOW2_CLUSTER_UNALLOCATED
:
1498 * Increases the refcount for the L1 table, its L2 tables and all referenced
1499 * clusters in the given refcount table. While doing so, performs some checks
1500 * on L1 and L2 entries.
1502 * Returns the number of errors found by the checks or -errno if an internal
1505 static int check_refcounts_l1(BlockDriverState
*bs
,
1506 BdrvCheckResult
*res
,
1507 void **refcount_table
,
1508 int64_t *refcount_table_size
,
1509 int64_t l1_table_offset
, int l1_size
,
1512 BDRVQcow2State
*s
= bs
->opaque
;
1513 uint64_t *l1_table
= NULL
, l2_offset
, l1_size2
;
1516 l1_size2
= l1_size
* sizeof(uint64_t);
1518 /* Mark L1 table as used */
1519 ret
= inc_refcounts(bs
, res
, refcount_table
, refcount_table_size
,
1520 l1_table_offset
, l1_size2
);
1525 /* Read L1 table entries from disk */
1527 l1_table
= g_try_malloc(l1_size2
);
1528 if (l1_table
== NULL
) {
1530 res
->check_errors
++;
1533 ret
= bdrv_pread(bs
->file
, l1_table_offset
, l1_table
, l1_size2
);
1535 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
1536 res
->check_errors
++;
1539 for(i
= 0;i
< l1_size
; i
++)
1540 be64_to_cpus(&l1_table
[i
]);
1543 /* Do the actual checks */
1544 for(i
= 0; i
< l1_size
; i
++) {
1545 l2_offset
= l1_table
[i
];
1547 /* Mark L2 table as used */
1548 l2_offset
&= L1E_OFFSET_MASK
;
1549 ret
= inc_refcounts(bs
, res
, refcount_table
, refcount_table_size
,
1550 l2_offset
, s
->cluster_size
);
1555 /* L2 tables are cluster aligned */
1556 if (offset_into_cluster(s
, l2_offset
)) {
1557 fprintf(stderr
, "ERROR l2_offset=%" PRIx64
": Table is not "
1558 "cluster aligned; L1 entry corrupted\n", l2_offset
);
1562 /* Process and check L2 entries */
1563 ret
= check_refcounts_l2(bs
, res
, refcount_table
,
1564 refcount_table_size
, l2_offset
, flags
);
1579 * Checks the OFLAG_COPIED flag for all L1 and L2 entries.
1581 * This function does not print an error message nor does it increment
1582 * check_errors if qcow2_get_refcount fails (this is because such an error will
1583 * have been already detected and sufficiently signaled by the calling function
1584 * (qcow2_check_refcounts) by the time this function is called).
1586 static int check_oflag_copied(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1589 BDRVQcow2State
*s
= bs
->opaque
;
1590 uint64_t *l2_table
= qemu_blockalign(bs
, s
->cluster_size
);
1595 for (i
= 0; i
< s
->l1_size
; i
++) {
1596 uint64_t l1_entry
= s
->l1_table
[i
];
1597 uint64_t l2_offset
= l1_entry
& L1E_OFFSET_MASK
;
1598 bool l2_dirty
= false;
1604 ret
= qcow2_get_refcount(bs
, l2_offset
>> s
->cluster_bits
,
1607 /* don't print message nor increment check_errors */
1610 if ((refcount
== 1) != ((l1_entry
& QCOW_OFLAG_COPIED
) != 0)) {
1611 fprintf(stderr
, "%s OFLAG_COPIED L2 cluster: l1_index=%d "
1612 "l1_entry=%" PRIx64
" refcount=%" PRIu64
"\n",
1613 fix
& BDRV_FIX_ERRORS
? "Repairing" :
1615 i
, l1_entry
, refcount
);
1616 if (fix
& BDRV_FIX_ERRORS
) {
1617 s
->l1_table
[i
] = refcount
== 1
1618 ? l1_entry
| QCOW_OFLAG_COPIED
1619 : l1_entry
& ~QCOW_OFLAG_COPIED
;
1620 ret
= qcow2_write_l1_entry(bs
, i
);
1622 res
->check_errors
++;
1625 res
->corruptions_fixed
++;
1631 ret
= bdrv_pread(bs
->file
, l2_offset
, l2_table
,
1632 s
->l2_size
* sizeof(uint64_t));
1634 fprintf(stderr
, "ERROR: Could not read L2 table: %s\n",
1636 res
->check_errors
++;
1640 for (j
= 0; j
< s
->l2_size
; j
++) {
1641 uint64_t l2_entry
= be64_to_cpu(l2_table
[j
]);
1642 uint64_t data_offset
= l2_entry
& L2E_OFFSET_MASK
;
1643 QCow2ClusterType cluster_type
= qcow2_get_cluster_type(l2_entry
);
1645 if ((cluster_type
== QCOW2_CLUSTER_NORMAL
) ||
1646 ((cluster_type
== QCOW2_CLUSTER_ZERO
) && (data_offset
!= 0))) {
1647 ret
= qcow2_get_refcount(bs
,
1648 data_offset
>> s
->cluster_bits
,
1651 /* don't print message nor increment check_errors */
1654 if ((refcount
== 1) != ((l2_entry
& QCOW_OFLAG_COPIED
) != 0)) {
1655 fprintf(stderr
, "%s OFLAG_COPIED data cluster: "
1656 "l2_entry=%" PRIx64
" refcount=%" PRIu64
"\n",
1657 fix
& BDRV_FIX_ERRORS
? "Repairing" :
1659 l2_entry
, refcount
);
1660 if (fix
& BDRV_FIX_ERRORS
) {
1661 l2_table
[j
] = cpu_to_be64(refcount
== 1
1662 ? l2_entry
| QCOW_OFLAG_COPIED
1663 : l2_entry
& ~QCOW_OFLAG_COPIED
);
1665 res
->corruptions_fixed
++;
1674 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L2
,
1675 l2_offset
, s
->cluster_size
);
1677 fprintf(stderr
, "ERROR: Could not write L2 table; metadata "
1678 "overlap check failed: %s\n", strerror(-ret
));
1679 res
->check_errors
++;
1683 ret
= bdrv_pwrite(bs
->file
, l2_offset
, l2_table
,
1686 fprintf(stderr
, "ERROR: Could not write L2 table: %s\n",
1688 res
->check_errors
++;
1697 qemu_vfree(l2_table
);
1702 * Checks consistency of refblocks and accounts for each refblock in
1705 static int check_refblocks(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1706 BdrvCheckMode fix
, bool *rebuild
,
1707 void **refcount_table
, int64_t *nb_clusters
)
1709 BDRVQcow2State
*s
= bs
->opaque
;
1713 for(i
= 0; i
< s
->refcount_table_size
; i
++) {
1714 uint64_t offset
, cluster
;
1715 offset
= s
->refcount_table
[i
];
1716 cluster
= offset
>> s
->cluster_bits
;
1718 /* Refcount blocks are cluster aligned */
1719 if (offset_into_cluster(s
, offset
)) {
1720 fprintf(stderr
, "ERROR refcount block %" PRId64
" is not "
1721 "cluster aligned; refcount table entry corrupted\n", i
);
1727 if (cluster
>= *nb_clusters
) {
1728 fprintf(stderr
, "%s refcount block %" PRId64
" is outside image\n",
1729 fix
& BDRV_FIX_ERRORS
? "Repairing" : "ERROR", i
);
1731 if (fix
& BDRV_FIX_ERRORS
) {
1732 int64_t new_nb_clusters
;
1733 Error
*local_err
= NULL
;
1735 if (offset
> INT64_MAX
- s
->cluster_size
) {
1740 ret
= bdrv_truncate(bs
->file
, offset
+ s
->cluster_size
,
1743 error_report_err(local_err
);
1746 size
= bdrv_getlength(bs
->file
->bs
);
1752 new_nb_clusters
= size_to_clusters(s
, size
);
1753 assert(new_nb_clusters
>= *nb_clusters
);
1755 ret
= realloc_refcount_array(s
, refcount_table
,
1756 nb_clusters
, new_nb_clusters
);
1758 res
->check_errors
++;
1762 if (cluster
>= *nb_clusters
) {
1767 res
->corruptions_fixed
++;
1768 ret
= inc_refcounts(bs
, res
, refcount_table
, nb_clusters
,
1769 offset
, s
->cluster_size
);
1773 /* No need to check whether the refcount is now greater than 1:
1774 * This area was just allocated and zeroed, so it can only be
1775 * exactly 1 after inc_refcounts() */
1781 fprintf(stderr
, "ERROR could not resize image: %s\n",
1790 ret
= inc_refcounts(bs
, res
, refcount_table
, nb_clusters
,
1791 offset
, s
->cluster_size
);
1795 if (s
->get_refcount(*refcount_table
, cluster
) != 1) {
1796 fprintf(stderr
, "ERROR refcount block %" PRId64
1797 " refcount=%" PRIu64
"\n", i
,
1798 s
->get_refcount(*refcount_table
, cluster
));
1809 * Calculates an in-memory refcount table.
1811 static int calculate_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1812 BdrvCheckMode fix
, bool *rebuild
,
1813 void **refcount_table
, int64_t *nb_clusters
)
1815 BDRVQcow2State
*s
= bs
->opaque
;
1820 if (!*refcount_table
) {
1821 int64_t old_size
= 0;
1822 ret
= realloc_refcount_array(s
, refcount_table
,
1823 &old_size
, *nb_clusters
);
1825 res
->check_errors
++;
1831 ret
= inc_refcounts(bs
, res
, refcount_table
, nb_clusters
,
1832 0, s
->cluster_size
);
1837 /* current L1 table */
1838 ret
= check_refcounts_l1(bs
, res
, refcount_table
, nb_clusters
,
1839 s
->l1_table_offset
, s
->l1_size
, CHECK_FRAG_INFO
);
1845 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
1846 sn
= s
->snapshots
+ i
;
1847 ret
= check_refcounts_l1(bs
, res
, refcount_table
, nb_clusters
,
1848 sn
->l1_table_offset
, sn
->l1_size
, 0);
1853 ret
= inc_refcounts(bs
, res
, refcount_table
, nb_clusters
,
1854 s
->snapshots_offset
, s
->snapshots_size
);
1860 ret
= inc_refcounts(bs
, res
, refcount_table
, nb_clusters
,
1861 s
->refcount_table_offset
,
1862 s
->refcount_table_size
* sizeof(uint64_t));
1867 return check_refblocks(bs
, res
, fix
, rebuild
, refcount_table
, nb_clusters
);
1871 * Compares the actual reference count for each cluster in the image against the
1872 * refcount as reported by the refcount structures on-disk.
1874 static void compare_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1875 BdrvCheckMode fix
, bool *rebuild
,
1876 int64_t *highest_cluster
,
1877 void *refcount_table
, int64_t nb_clusters
)
1879 BDRVQcow2State
*s
= bs
->opaque
;
1881 uint64_t refcount1
, refcount2
;
1884 for (i
= 0, *highest_cluster
= 0; i
< nb_clusters
; i
++) {
1885 ret
= qcow2_get_refcount(bs
, i
, &refcount1
);
1887 fprintf(stderr
, "Can't get refcount for cluster %" PRId64
": %s\n",
1889 res
->check_errors
++;
1893 refcount2
= s
->get_refcount(refcount_table
, i
);
1895 if (refcount1
> 0 || refcount2
> 0) {
1896 *highest_cluster
= i
;
1899 if (refcount1
!= refcount2
) {
1900 /* Check if we're allowed to fix the mismatch */
1901 int *num_fixed
= NULL
;
1902 if (refcount1
== 0) {
1904 } else if (refcount1
> refcount2
&& (fix
& BDRV_FIX_LEAKS
)) {
1905 num_fixed
= &res
->leaks_fixed
;
1906 } else if (refcount1
< refcount2
&& (fix
& BDRV_FIX_ERRORS
)) {
1907 num_fixed
= &res
->corruptions_fixed
;
1910 fprintf(stderr
, "%s cluster %" PRId64
" refcount=%" PRIu64
1911 " reference=%" PRIu64
"\n",
1912 num_fixed
!= NULL
? "Repairing" :
1913 refcount1
< refcount2
? "ERROR" :
1915 i
, refcount1
, refcount2
);
1918 ret
= update_refcount(bs
, i
<< s
->cluster_bits
, 1,
1919 refcount_diff(refcount1
, refcount2
),
1920 refcount1
> refcount2
,
1921 QCOW2_DISCARD_ALWAYS
);
1928 /* And if we couldn't, print an error */
1929 if (refcount1
< refcount2
) {
1939 * Allocates clusters using an in-memory refcount table (IMRT) in contrast to
1940 * the on-disk refcount structures.
1942 * On input, *first_free_cluster tells where to start looking, and need not
1943 * actually be a free cluster; the returned offset will not be before that
1944 * cluster. On output, *first_free_cluster points to the first gap found, even
1945 * if that gap was too small to be used as the returned offset.
1947 * Note that *first_free_cluster is a cluster index whereas the return value is
1950 static int64_t alloc_clusters_imrt(BlockDriverState
*bs
,
1952 void **refcount_table
,
1953 int64_t *imrt_nb_clusters
,
1954 int64_t *first_free_cluster
)
1956 BDRVQcow2State
*s
= bs
->opaque
;
1957 int64_t cluster
= *first_free_cluster
, i
;
1958 bool first_gap
= true;
1959 int contiguous_free_clusters
;
1962 /* Starting at *first_free_cluster, find a range of at least cluster_count
1963 * continuously free clusters */
1964 for (contiguous_free_clusters
= 0;
1965 cluster
< *imrt_nb_clusters
&&
1966 contiguous_free_clusters
< cluster_count
;
1969 if (!s
->get_refcount(*refcount_table
, cluster
)) {
1970 contiguous_free_clusters
++;
1972 /* If this is the first free cluster found, update
1973 * *first_free_cluster accordingly */
1974 *first_free_cluster
= cluster
;
1977 } else if (contiguous_free_clusters
) {
1978 contiguous_free_clusters
= 0;
1982 /* If contiguous_free_clusters is greater than zero, it contains the number
1983 * of continuously free clusters until the current cluster; the first free
1984 * cluster in the current "gap" is therefore
1985 * cluster - contiguous_free_clusters */
1987 /* If no such range could be found, grow the in-memory refcount table
1988 * accordingly to append free clusters at the end of the image */
1989 if (contiguous_free_clusters
< cluster_count
) {
1990 /* contiguous_free_clusters clusters are already empty at the image end;
1991 * we need cluster_count clusters; therefore, we have to allocate
1992 * cluster_count - contiguous_free_clusters new clusters at the end of
1993 * the image (which is the current value of cluster; note that cluster
1994 * may exceed old_imrt_nb_clusters if *first_free_cluster pointed beyond
1996 ret
= realloc_refcount_array(s
, refcount_table
, imrt_nb_clusters
,
1997 cluster
+ cluster_count
1998 - contiguous_free_clusters
);
2004 /* Go back to the first free cluster */
2005 cluster
-= contiguous_free_clusters
;
2006 for (i
= 0; i
< cluster_count
; i
++) {
2007 s
->set_refcount(*refcount_table
, cluster
+ i
, 1);
2010 return cluster
<< s
->cluster_bits
;
2014 * Creates a new refcount structure based solely on the in-memory information
2015 * given through *refcount_table. All necessary allocations will be reflected
2018 * On success, the old refcount structure is leaked (it will be covered by the
2019 * new refcount structure).
2021 static int rebuild_refcount_structure(BlockDriverState
*bs
,
2022 BdrvCheckResult
*res
,
2023 void **refcount_table
,
2024 int64_t *nb_clusters
)
2026 BDRVQcow2State
*s
= bs
->opaque
;
2027 int64_t first_free_cluster
= 0, reftable_offset
= -1, cluster
= 0;
2028 int64_t refblock_offset
, refblock_start
, refblock_index
;
2029 uint32_t reftable_size
= 0;
2030 uint64_t *on_disk_reftable
= NULL
;
2031 void *on_disk_refblock
;
2034 uint64_t reftable_offset
;
2035 uint32_t reftable_clusters
;
2036 } QEMU_PACKED reftable_offset_and_clusters
;
2038 qcow2_cache_empty(bs
, s
->refcount_block_cache
);
2041 for (; cluster
< *nb_clusters
; cluster
++) {
2042 if (!s
->get_refcount(*refcount_table
, cluster
)) {
2046 refblock_index
= cluster
>> s
->refcount_block_bits
;
2047 refblock_start
= refblock_index
<< s
->refcount_block_bits
;
2049 /* Don't allocate a cluster in a refblock already written to disk */
2050 if (first_free_cluster
< refblock_start
) {
2051 first_free_cluster
= refblock_start
;
2053 refblock_offset
= alloc_clusters_imrt(bs
, 1, refcount_table
,
2054 nb_clusters
, &first_free_cluster
);
2055 if (refblock_offset
< 0) {
2056 fprintf(stderr
, "ERROR allocating refblock: %s\n",
2057 strerror(-refblock_offset
));
2058 res
->check_errors
++;
2059 ret
= refblock_offset
;
2063 if (reftable_size
<= refblock_index
) {
2064 uint32_t old_reftable_size
= reftable_size
;
2065 uint64_t *new_on_disk_reftable
;
2067 reftable_size
= ROUND_UP((refblock_index
+ 1) * sizeof(uint64_t),
2068 s
->cluster_size
) / sizeof(uint64_t);
2069 new_on_disk_reftable
= g_try_realloc(on_disk_reftable
,
2072 if (!new_on_disk_reftable
) {
2073 res
->check_errors
++;
2077 on_disk_reftable
= new_on_disk_reftable
;
2079 memset(on_disk_reftable
+ old_reftable_size
, 0,
2080 (reftable_size
- old_reftable_size
) * sizeof(uint64_t));
2082 /* The offset we have for the reftable is now no longer valid;
2083 * this will leak that range, but we can easily fix that by running
2084 * a leak-fixing check after this rebuild operation */
2085 reftable_offset
= -1;
2087 on_disk_reftable
[refblock_index
] = refblock_offset
;
2089 /* If this is apparently the last refblock (for now), try to squeeze the
2091 if (refblock_index
== (*nb_clusters
- 1) >> s
->refcount_block_bits
&&
2092 reftable_offset
< 0)
2094 uint64_t reftable_clusters
= size_to_clusters(s
, reftable_size
*
2096 reftable_offset
= alloc_clusters_imrt(bs
, reftable_clusters
,
2097 refcount_table
, nb_clusters
,
2098 &first_free_cluster
);
2099 if (reftable_offset
< 0) {
2100 fprintf(stderr
, "ERROR allocating reftable: %s\n",
2101 strerror(-reftable_offset
));
2102 res
->check_errors
++;
2103 ret
= reftable_offset
;
2108 ret
= qcow2_pre_write_overlap_check(bs
, 0, refblock_offset
,
2111 fprintf(stderr
, "ERROR writing refblock: %s\n", strerror(-ret
));
2115 /* The size of *refcount_table is always cluster-aligned, therefore the
2116 * write operation will not overflow */
2117 on_disk_refblock
= (void *)((char *) *refcount_table
+
2118 refblock_index
* s
->cluster_size
);
2120 ret
= bdrv_write(bs
->file
, refblock_offset
/ BDRV_SECTOR_SIZE
,
2121 on_disk_refblock
, s
->cluster_sectors
);
2123 fprintf(stderr
, "ERROR writing refblock: %s\n", strerror(-ret
));
2127 /* Go to the end of this refblock */
2128 cluster
= refblock_start
+ s
->refcount_block_size
- 1;
2131 if (reftable_offset
< 0) {
2132 uint64_t post_refblock_start
, reftable_clusters
;
2134 post_refblock_start
= ROUND_UP(*nb_clusters
, s
->refcount_block_size
);
2135 reftable_clusters
= size_to_clusters(s
,
2136 reftable_size
* sizeof(uint64_t));
2137 /* Not pretty but simple */
2138 if (first_free_cluster
< post_refblock_start
) {
2139 first_free_cluster
= post_refblock_start
;
2141 reftable_offset
= alloc_clusters_imrt(bs
, reftable_clusters
,
2142 refcount_table
, nb_clusters
,
2143 &first_free_cluster
);
2144 if (reftable_offset
< 0) {
2145 fprintf(stderr
, "ERROR allocating reftable: %s\n",
2146 strerror(-reftable_offset
));
2147 res
->check_errors
++;
2148 ret
= reftable_offset
;
2152 goto write_refblocks
;
2155 assert(on_disk_reftable
);
2157 for (refblock_index
= 0; refblock_index
< reftable_size
; refblock_index
++) {
2158 cpu_to_be64s(&on_disk_reftable
[refblock_index
]);
2161 ret
= qcow2_pre_write_overlap_check(bs
, 0, reftable_offset
,
2162 reftable_size
* sizeof(uint64_t));
2164 fprintf(stderr
, "ERROR writing reftable: %s\n", strerror(-ret
));
2168 assert(reftable_size
< INT_MAX
/ sizeof(uint64_t));
2169 ret
= bdrv_pwrite(bs
->file
, reftable_offset
, on_disk_reftable
,
2170 reftable_size
* sizeof(uint64_t));
2172 fprintf(stderr
, "ERROR writing reftable: %s\n", strerror(-ret
));
2176 /* Enter new reftable into the image header */
2177 reftable_offset_and_clusters
.reftable_offset
= cpu_to_be64(reftable_offset
);
2178 reftable_offset_and_clusters
.reftable_clusters
=
2179 cpu_to_be32(size_to_clusters(s
, reftable_size
* sizeof(uint64_t)));
2180 ret
= bdrv_pwrite_sync(bs
->file
,
2181 offsetof(QCowHeader
, refcount_table_offset
),
2182 &reftable_offset_and_clusters
,
2183 sizeof(reftable_offset_and_clusters
));
2185 fprintf(stderr
, "ERROR setting reftable: %s\n", strerror(-ret
));
2189 for (refblock_index
= 0; refblock_index
< reftable_size
; refblock_index
++) {
2190 be64_to_cpus(&on_disk_reftable
[refblock_index
]);
2192 s
->refcount_table
= on_disk_reftable
;
2193 s
->refcount_table_offset
= reftable_offset
;
2194 s
->refcount_table_size
= reftable_size
;
2195 update_max_refcount_table_index(s
);
2200 g_free(on_disk_reftable
);
2205 * Checks an image for refcount consistency.
2207 * Returns 0 if no errors are found, the number of errors in case the image is
2208 * detected as corrupted, and -errno when an internal error occurred.
2210 int qcow2_check_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
2213 BDRVQcow2State
*s
= bs
->opaque
;
2214 BdrvCheckResult pre_compare_res
;
2215 int64_t size
, highest_cluster
, nb_clusters
;
2216 void *refcount_table
= NULL
;
2217 bool rebuild
= false;
2220 size
= bdrv_getlength(bs
->file
->bs
);
2222 res
->check_errors
++;
2226 nb_clusters
= size_to_clusters(s
, size
);
2227 if (nb_clusters
> INT_MAX
) {
2228 res
->check_errors
++;
2232 res
->bfi
.total_clusters
=
2233 size_to_clusters(s
, bs
->total_sectors
* BDRV_SECTOR_SIZE
);
2235 ret
= calculate_refcounts(bs
, res
, fix
, &rebuild
, &refcount_table
,
2241 /* In case we don't need to rebuild the refcount structure (but want to fix
2242 * something), this function is immediately called again, in which case the
2243 * result should be ignored */
2244 pre_compare_res
= *res
;
2245 compare_refcounts(bs
, res
, 0, &rebuild
, &highest_cluster
, refcount_table
,
2248 if (rebuild
&& (fix
& BDRV_FIX_ERRORS
)) {
2249 BdrvCheckResult old_res
= *res
;
2250 int fresh_leaks
= 0;
2252 fprintf(stderr
, "Rebuilding refcount structure\n");
2253 ret
= rebuild_refcount_structure(bs
, res
, &refcount_table
,
2259 res
->corruptions
= 0;
2262 /* Because the old reftable has been exchanged for a new one the
2263 * references have to be recalculated */
2265 memset(refcount_table
, 0, refcount_array_byte_size(s
, nb_clusters
));
2266 ret
= calculate_refcounts(bs
, res
, 0, &rebuild
, &refcount_table
,
2272 if (fix
& BDRV_FIX_LEAKS
) {
2273 /* The old refcount structures are now leaked, fix it; the result
2274 * can be ignored, aside from leaks which were introduced by
2275 * rebuild_refcount_structure() that could not be fixed */
2276 BdrvCheckResult saved_res
= *res
;
2277 *res
= (BdrvCheckResult
){ 0 };
2279 compare_refcounts(bs
, res
, BDRV_FIX_LEAKS
, &rebuild
,
2280 &highest_cluster
, refcount_table
, nb_clusters
);
2282 fprintf(stderr
, "ERROR rebuilt refcount structure is still "
2286 /* Any leaks accounted for here were introduced by
2287 * rebuild_refcount_structure() because that function has created a
2288 * new refcount structure from scratch */
2289 fresh_leaks
= res
->leaks
;
2293 if (res
->corruptions
< old_res
.corruptions
) {
2294 res
->corruptions_fixed
+= old_res
.corruptions
- res
->corruptions
;
2296 if (res
->leaks
< old_res
.leaks
) {
2297 res
->leaks_fixed
+= old_res
.leaks
- res
->leaks
;
2299 res
->leaks
+= fresh_leaks
;
2302 fprintf(stderr
, "ERROR need to rebuild refcount structures\n");
2303 res
->check_errors
++;
2308 if (res
->leaks
|| res
->corruptions
) {
2309 *res
= pre_compare_res
;
2310 compare_refcounts(bs
, res
, fix
, &rebuild
, &highest_cluster
,
2311 refcount_table
, nb_clusters
);
2315 /* check OFLAG_COPIED */
2316 ret
= check_oflag_copied(bs
, res
, fix
);
2321 res
->image_end_offset
= (highest_cluster
+ 1) * s
->cluster_size
;
2325 g_free(refcount_table
);
2330 #define overlaps_with(ofs, sz) \
2331 ranges_overlap(offset, size, ofs, sz)
2334 * Checks if the given offset into the image file is actually free to use by
2335 * looking for overlaps with important metadata sections (L1/L2 tables etc.),
2336 * i.e. a sanity check without relying on the refcount tables.
2338 * The ign parameter specifies what checks not to perform (being a bitmask of
2339 * QCow2MetadataOverlap values), i.e., what sections to ignore.
2342 * - 0 if writing to this offset will not affect the mentioned metadata
2343 * - a positive QCow2MetadataOverlap value indicating one overlapping section
2344 * - a negative value (-errno) indicating an error while performing a check,
2345 * e.g. when bdrv_read failed on QCOW2_OL_INACTIVE_L2
2347 int qcow2_check_metadata_overlap(BlockDriverState
*bs
, int ign
, int64_t offset
,
2350 BDRVQcow2State
*s
= bs
->opaque
;
2351 int chk
= s
->overlap_check
& ~ign
;
2358 if (chk
& QCOW2_OL_MAIN_HEADER
) {
2359 if (offset
< s
->cluster_size
) {
2360 return QCOW2_OL_MAIN_HEADER
;
2364 /* align range to test to cluster boundaries */
2365 size
= align_offset(offset_into_cluster(s
, offset
) + size
, s
->cluster_size
);
2366 offset
= start_of_cluster(s
, offset
);
2368 if ((chk
& QCOW2_OL_ACTIVE_L1
) && s
->l1_size
) {
2369 if (overlaps_with(s
->l1_table_offset
, s
->l1_size
* sizeof(uint64_t))) {
2370 return QCOW2_OL_ACTIVE_L1
;
2374 if ((chk
& QCOW2_OL_REFCOUNT_TABLE
) && s
->refcount_table_size
) {
2375 if (overlaps_with(s
->refcount_table_offset
,
2376 s
->refcount_table_size
* sizeof(uint64_t))) {
2377 return QCOW2_OL_REFCOUNT_TABLE
;
2381 if ((chk
& QCOW2_OL_SNAPSHOT_TABLE
) && s
->snapshots_size
) {
2382 if (overlaps_with(s
->snapshots_offset
, s
->snapshots_size
)) {
2383 return QCOW2_OL_SNAPSHOT_TABLE
;
2387 if ((chk
& QCOW2_OL_INACTIVE_L1
) && s
->snapshots
) {
2388 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
2389 if (s
->snapshots
[i
].l1_size
&&
2390 overlaps_with(s
->snapshots
[i
].l1_table_offset
,
2391 s
->snapshots
[i
].l1_size
* sizeof(uint64_t))) {
2392 return QCOW2_OL_INACTIVE_L1
;
2397 if ((chk
& QCOW2_OL_ACTIVE_L2
) && s
->l1_table
) {
2398 for (i
= 0; i
< s
->l1_size
; i
++) {
2399 if ((s
->l1_table
[i
] & L1E_OFFSET_MASK
) &&
2400 overlaps_with(s
->l1_table
[i
] & L1E_OFFSET_MASK
,
2402 return QCOW2_OL_ACTIVE_L2
;
2407 if ((chk
& QCOW2_OL_REFCOUNT_BLOCK
) && s
->refcount_table
) {
2408 unsigned last_entry
= s
->max_refcount_table_index
;
2409 assert(last_entry
< s
->refcount_table_size
);
2410 assert(last_entry
+ 1 == s
->refcount_table_size
||
2411 (s
->refcount_table
[last_entry
+ 1] & REFT_OFFSET_MASK
) == 0);
2412 for (i
= 0; i
<= last_entry
; i
++) {
2413 if ((s
->refcount_table
[i
] & REFT_OFFSET_MASK
) &&
2414 overlaps_with(s
->refcount_table
[i
] & REFT_OFFSET_MASK
,
2416 return QCOW2_OL_REFCOUNT_BLOCK
;
2421 if ((chk
& QCOW2_OL_INACTIVE_L2
) && s
->snapshots
) {
2422 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
2423 uint64_t l1_ofs
= s
->snapshots
[i
].l1_table_offset
;
2424 uint32_t l1_sz
= s
->snapshots
[i
].l1_size
;
2425 uint64_t l1_sz2
= l1_sz
* sizeof(uint64_t);
2426 uint64_t *l1
= g_try_malloc(l1_sz2
);
2429 if (l1_sz2
&& l1
== NULL
) {
2433 ret
= bdrv_pread(bs
->file
, l1_ofs
, l1
, l1_sz2
);
2439 for (j
= 0; j
< l1_sz
; j
++) {
2440 uint64_t l2_ofs
= be64_to_cpu(l1
[j
]) & L1E_OFFSET_MASK
;
2441 if (l2_ofs
&& overlaps_with(l2_ofs
, s
->cluster_size
)) {
2443 return QCOW2_OL_INACTIVE_L2
;
2454 static const char *metadata_ol_names
[] = {
2455 [QCOW2_OL_MAIN_HEADER_BITNR
] = "qcow2_header",
2456 [QCOW2_OL_ACTIVE_L1_BITNR
] = "active L1 table",
2457 [QCOW2_OL_ACTIVE_L2_BITNR
] = "active L2 table",
2458 [QCOW2_OL_REFCOUNT_TABLE_BITNR
] = "refcount table",
2459 [QCOW2_OL_REFCOUNT_BLOCK_BITNR
] = "refcount block",
2460 [QCOW2_OL_SNAPSHOT_TABLE_BITNR
] = "snapshot table",
2461 [QCOW2_OL_INACTIVE_L1_BITNR
] = "inactive L1 table",
2462 [QCOW2_OL_INACTIVE_L2_BITNR
] = "inactive L2 table",
2466 * First performs a check for metadata overlaps (through
2467 * qcow2_check_metadata_overlap); if that fails with a negative value (error
2468 * while performing a check), that value is returned. If an impending overlap
2469 * is detected, the BDS will be made unusable, the qcow2 file marked corrupt
2470 * and -EIO returned.
2472 * Returns 0 if there were neither overlaps nor errors while checking for
2473 * overlaps; or a negative value (-errno) on error.
2475 int qcow2_pre_write_overlap_check(BlockDriverState
*bs
, int ign
, int64_t offset
,
2478 int ret
= qcow2_check_metadata_overlap(bs
, ign
, offset
, size
);
2482 } else if (ret
> 0) {
2483 int metadata_ol_bitnr
= ctz32(ret
);
2484 assert(metadata_ol_bitnr
< QCOW2_OL_MAX_BITNR
);
2486 qcow2_signal_corruption(bs
, true, offset
, size
, "Preventing invalid "
2487 "write on metadata (overlaps with %s)",
2488 metadata_ol_names
[metadata_ol_bitnr
]);
2495 /* A pointer to a function of this type is given to walk_over_reftable(). That
2496 * function will create refblocks and pass them to a RefblockFinishOp once they
2497 * are completed (@refblock). @refblock_empty is set if the refblock is
2500 * Along with the refblock, a corresponding reftable entry is passed, in the
2501 * reftable @reftable (which may be reallocated) at @reftable_index.
2503 * @allocated should be set to true if a new cluster has been allocated.
2505 typedef int (RefblockFinishOp
)(BlockDriverState
*bs
, uint64_t **reftable
,
2506 uint64_t reftable_index
, uint64_t *reftable_size
,
2507 void *refblock
, bool refblock_empty
,
2508 bool *allocated
, Error
**errp
);
2511 * This "operation" for walk_over_reftable() allocates the refblock on disk (if
2512 * it is not empty) and inserts its offset into the new reftable. The size of
2513 * this new reftable is increased as required.
2515 static int alloc_refblock(BlockDriverState
*bs
, uint64_t **reftable
,
2516 uint64_t reftable_index
, uint64_t *reftable_size
,
2517 void *refblock
, bool refblock_empty
, bool *allocated
,
2520 BDRVQcow2State
*s
= bs
->opaque
;
2523 if (!refblock_empty
&& reftable_index
>= *reftable_size
) {
2524 uint64_t *new_reftable
;
2525 uint64_t new_reftable_size
;
2527 new_reftable_size
= ROUND_UP(reftable_index
+ 1,
2528 s
->cluster_size
/ sizeof(uint64_t));
2529 if (new_reftable_size
> QCOW_MAX_REFTABLE_SIZE
/ sizeof(uint64_t)) {
2531 "This operation would make the refcount table grow "
2532 "beyond the maximum size supported by QEMU, aborting");
2536 new_reftable
= g_try_realloc(*reftable
, new_reftable_size
*
2538 if (!new_reftable
) {
2539 error_setg(errp
, "Failed to increase reftable buffer size");
2543 memset(new_reftable
+ *reftable_size
, 0,
2544 (new_reftable_size
- *reftable_size
) * sizeof(uint64_t));
2546 *reftable
= new_reftable
;
2547 *reftable_size
= new_reftable_size
;
2550 if (!refblock_empty
&& !(*reftable
)[reftable_index
]) {
2551 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
2553 error_setg_errno(errp
, -offset
, "Failed to allocate refblock");
2556 (*reftable
)[reftable_index
] = offset
;
2564 * This "operation" for walk_over_reftable() writes the refblock to disk at the
2565 * offset specified by the new reftable's entry. It does not modify the new
2566 * reftable or change any refcounts.
2568 static int flush_refblock(BlockDriverState
*bs
, uint64_t **reftable
,
2569 uint64_t reftable_index
, uint64_t *reftable_size
,
2570 void *refblock
, bool refblock_empty
, bool *allocated
,
2573 BDRVQcow2State
*s
= bs
->opaque
;
2577 if (reftable_index
< *reftable_size
&& (*reftable
)[reftable_index
]) {
2578 offset
= (*reftable
)[reftable_index
];
2580 ret
= qcow2_pre_write_overlap_check(bs
, 0, offset
, s
->cluster_size
);
2582 error_setg_errno(errp
, -ret
, "Overlap check failed");
2586 ret
= bdrv_pwrite(bs
->file
, offset
, refblock
, s
->cluster_size
);
2588 error_setg_errno(errp
, -ret
, "Failed to write refblock");
2592 assert(refblock_empty
);
2599 * This function walks over the existing reftable and every referenced refblock;
2600 * if @new_set_refcount is non-NULL, it is called for every refcount entry to
2601 * create an equal new entry in the passed @new_refblock. Once that
2602 * @new_refblock is completely filled, @operation will be called.
2604 * @status_cb and @cb_opaque are used for the amend operation's status callback.
2605 * @index is the index of the walk_over_reftable() calls and @total is the total
2606 * number of walk_over_reftable() calls per amend operation. Both are used for
2607 * calculating the parameters for the status callback.
2609 * @allocated is set to true if a new cluster has been allocated.
2611 static int walk_over_reftable(BlockDriverState
*bs
, uint64_t **new_reftable
,
2612 uint64_t *new_reftable_index
,
2613 uint64_t *new_reftable_size
,
2614 void *new_refblock
, int new_refblock_size
,
2615 int new_refcount_bits
,
2616 RefblockFinishOp
*operation
, bool *allocated
,
2617 Qcow2SetRefcountFunc
*new_set_refcount
,
2618 BlockDriverAmendStatusCB
*status_cb
,
2619 void *cb_opaque
, int index
, int total
,
2622 BDRVQcow2State
*s
= bs
->opaque
;
2623 uint64_t reftable_index
;
2624 bool new_refblock_empty
= true;
2626 int new_refblock_index
= 0;
2629 for (reftable_index
= 0; reftable_index
< s
->refcount_table_size
;
2632 uint64_t refblock_offset
= s
->refcount_table
[reftable_index
]
2635 status_cb(bs
, (uint64_t)index
* s
->refcount_table_size
+ reftable_index
,
2636 (uint64_t)total
* s
->refcount_table_size
, cb_opaque
);
2638 if (refblock_offset
) {
2641 if (offset_into_cluster(s
, refblock_offset
)) {
2642 qcow2_signal_corruption(bs
, true, -1, -1, "Refblock offset %#"
2643 PRIx64
" unaligned (reftable index: %#"
2644 PRIx64
")", refblock_offset
,
2647 "Image is corrupt (unaligned refblock offset)");
2651 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refblock_offset
,
2654 error_setg_errno(errp
, -ret
, "Failed to retrieve refblock");
2658 for (refblock_index
= 0; refblock_index
< s
->refcount_block_size
;
2663 if (new_refblock_index
>= new_refblock_size
) {
2664 /* new_refblock is now complete */
2665 ret
= operation(bs
, new_reftable
, *new_reftable_index
,
2666 new_reftable_size
, new_refblock
,
2667 new_refblock_empty
, allocated
, errp
);
2669 qcow2_cache_put(bs
, s
->refcount_block_cache
, &refblock
);
2673 (*new_reftable_index
)++;
2674 new_refblock_index
= 0;
2675 new_refblock_empty
= true;
2678 refcount
= s
->get_refcount(refblock
, refblock_index
);
2679 if (new_refcount_bits
< 64 && refcount
>> new_refcount_bits
) {
2682 qcow2_cache_put(bs
, s
->refcount_block_cache
, &refblock
);
2684 offset
= ((reftable_index
<< s
->refcount_block_bits
)
2685 + refblock_index
) << s
->cluster_bits
;
2687 error_setg(errp
, "Cannot decrease refcount entry width to "
2688 "%i bits: Cluster at offset %#" PRIx64
" has a "
2689 "refcount of %" PRIu64
, new_refcount_bits
,
2694 if (new_set_refcount
) {
2695 new_set_refcount(new_refblock
, new_refblock_index
++,
2698 new_refblock_index
++;
2700 new_refblock_empty
= new_refblock_empty
&& refcount
== 0;
2703 qcow2_cache_put(bs
, s
->refcount_block_cache
, &refblock
);
2705 /* No refblock means every refcount is 0 */
2706 for (refblock_index
= 0; refblock_index
< s
->refcount_block_size
;
2709 if (new_refblock_index
>= new_refblock_size
) {
2710 /* new_refblock is now complete */
2711 ret
= operation(bs
, new_reftable
, *new_reftable_index
,
2712 new_reftable_size
, new_refblock
,
2713 new_refblock_empty
, allocated
, errp
);
2718 (*new_reftable_index
)++;
2719 new_refblock_index
= 0;
2720 new_refblock_empty
= true;
2723 if (new_set_refcount
) {
2724 new_set_refcount(new_refblock
, new_refblock_index
++, 0);
2726 new_refblock_index
++;
2732 if (new_refblock_index
> 0) {
2733 /* Complete the potentially existing partially filled final refblock */
2734 if (new_set_refcount
) {
2735 for (; new_refblock_index
< new_refblock_size
;
2736 new_refblock_index
++)
2738 new_set_refcount(new_refblock
, new_refblock_index
, 0);
2742 ret
= operation(bs
, new_reftable
, *new_reftable_index
,
2743 new_reftable_size
, new_refblock
, new_refblock_empty
,
2749 (*new_reftable_index
)++;
2752 status_cb(bs
, (uint64_t)(index
+ 1) * s
->refcount_table_size
,
2753 (uint64_t)total
* s
->refcount_table_size
, cb_opaque
);
2758 int qcow2_change_refcount_order(BlockDriverState
*bs
, int refcount_order
,
2759 BlockDriverAmendStatusCB
*status_cb
,
2760 void *cb_opaque
, Error
**errp
)
2762 BDRVQcow2State
*s
= bs
->opaque
;
2763 Qcow2GetRefcountFunc
*new_get_refcount
;
2764 Qcow2SetRefcountFunc
*new_set_refcount
;
2765 void *new_refblock
= qemu_blockalign(bs
->file
->bs
, s
->cluster_size
);
2766 uint64_t *new_reftable
= NULL
, new_reftable_size
= 0;
2767 uint64_t *old_reftable
, old_reftable_size
, old_reftable_offset
;
2768 uint64_t new_reftable_index
= 0;
2770 int64_t new_reftable_offset
= 0, allocated_reftable_size
= 0;
2771 int new_refblock_size
, new_refcount_bits
= 1 << refcount_order
;
2772 int old_refcount_order
;
2775 bool new_allocation
;
2777 assert(s
->qcow_version
>= 3);
2778 assert(refcount_order
>= 0 && refcount_order
<= 6);
2780 /* see qcow2_open() */
2781 new_refblock_size
= 1 << (s
->cluster_bits
- (refcount_order
- 3));
2783 new_get_refcount
= get_refcount_funcs
[refcount_order
];
2784 new_set_refcount
= set_refcount_funcs
[refcount_order
];
2790 new_allocation
= false;
2792 /* At least we have to do this walk and the one which writes the
2793 * refblocks; also, at least we have to do this loop here at least
2794 * twice (normally), first to do the allocations, and second to
2795 * determine that everything is correctly allocated, this then makes
2796 * three walks in total */
2797 total_walks
= MAX(walk_index
+ 2, 3);
2799 /* First, allocate the structures so they are present in the refcount
2801 ret
= walk_over_reftable(bs
, &new_reftable
, &new_reftable_index
,
2802 &new_reftable_size
, NULL
, new_refblock_size
,
2803 new_refcount_bits
, &alloc_refblock
,
2804 &new_allocation
, NULL
, status_cb
, cb_opaque
,
2805 walk_index
++, total_walks
, errp
);
2810 new_reftable_index
= 0;
2812 if (new_allocation
) {
2813 if (new_reftable_offset
) {
2814 qcow2_free_clusters(bs
, new_reftable_offset
,
2815 allocated_reftable_size
* sizeof(uint64_t),
2816 QCOW2_DISCARD_NEVER
);
2819 new_reftable_offset
= qcow2_alloc_clusters(bs
, new_reftable_size
*
2821 if (new_reftable_offset
< 0) {
2822 error_setg_errno(errp
, -new_reftable_offset
,
2823 "Failed to allocate the new reftable");
2824 ret
= new_reftable_offset
;
2827 allocated_reftable_size
= new_reftable_size
;
2829 } while (new_allocation
);
2831 /* Second, write the new refblocks */
2832 ret
= walk_over_reftable(bs
, &new_reftable
, &new_reftable_index
,
2833 &new_reftable_size
, new_refblock
,
2834 new_refblock_size
, new_refcount_bits
,
2835 &flush_refblock
, &new_allocation
, new_set_refcount
,
2836 status_cb
, cb_opaque
, walk_index
, walk_index
+ 1,
2841 assert(!new_allocation
);
2844 /* Write the new reftable */
2845 ret
= qcow2_pre_write_overlap_check(bs
, 0, new_reftable_offset
,
2846 new_reftable_size
* sizeof(uint64_t));
2848 error_setg_errno(errp
, -ret
, "Overlap check failed");
2852 for (i
= 0; i
< new_reftable_size
; i
++) {
2853 cpu_to_be64s(&new_reftable
[i
]);
2856 ret
= bdrv_pwrite(bs
->file
, new_reftable_offset
, new_reftable
,
2857 new_reftable_size
* sizeof(uint64_t));
2859 for (i
= 0; i
< new_reftable_size
; i
++) {
2860 be64_to_cpus(&new_reftable
[i
]);
2864 error_setg_errno(errp
, -ret
, "Failed to write the new reftable");
2869 /* Empty the refcount cache */
2870 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
2872 error_setg_errno(errp
, -ret
, "Failed to flush the refblock cache");
2876 /* Update the image header to point to the new reftable; this only updates
2877 * the fields which are relevant to qcow2_update_header(); other fields
2878 * such as s->refcount_table or s->refcount_bits stay stale for now
2879 * (because we have to restore everything if qcow2_update_header() fails) */
2880 old_refcount_order
= s
->refcount_order
;
2881 old_reftable_size
= s
->refcount_table_size
;
2882 old_reftable_offset
= s
->refcount_table_offset
;
2884 s
->refcount_order
= refcount_order
;
2885 s
->refcount_table_size
= new_reftable_size
;
2886 s
->refcount_table_offset
= new_reftable_offset
;
2888 ret
= qcow2_update_header(bs
);
2890 s
->refcount_order
= old_refcount_order
;
2891 s
->refcount_table_size
= old_reftable_size
;
2892 s
->refcount_table_offset
= old_reftable_offset
;
2893 error_setg_errno(errp
, -ret
, "Failed to update the qcow2 header");
2897 /* Now update the rest of the in-memory information */
2898 old_reftable
= s
->refcount_table
;
2899 s
->refcount_table
= new_reftable
;
2900 update_max_refcount_table_index(s
);
2902 s
->refcount_bits
= 1 << refcount_order
;
2903 s
->refcount_max
= UINT64_C(1) << (s
->refcount_bits
- 1);
2904 s
->refcount_max
+= s
->refcount_max
- 1;
2906 s
->refcount_block_bits
= s
->cluster_bits
- (refcount_order
- 3);
2907 s
->refcount_block_size
= 1 << s
->refcount_block_bits
;
2909 s
->get_refcount
= new_get_refcount
;
2910 s
->set_refcount
= new_set_refcount
;
2912 /* For cleaning up all old refblocks and the old reftable below the "done"
2914 new_reftable
= old_reftable
;
2915 new_reftable_size
= old_reftable_size
;
2916 new_reftable_offset
= old_reftable_offset
;
2920 /* On success, new_reftable actually points to the old reftable (and
2921 * new_reftable_size is the old reftable's size); but that is just
2923 for (i
= 0; i
< new_reftable_size
; i
++) {
2924 uint64_t offset
= new_reftable
[i
] & REFT_OFFSET_MASK
;
2926 qcow2_free_clusters(bs
, offset
, s
->cluster_size
,
2927 QCOW2_DISCARD_OTHER
);
2930 g_free(new_reftable
);
2932 if (new_reftable_offset
> 0) {
2933 qcow2_free_clusters(bs
, new_reftable_offset
,
2934 new_reftable_size
* sizeof(uint64_t),
2935 QCOW2_DISCARD_OTHER
);
2939 qemu_vfree(new_refblock
);