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-common.h"
26 #include "block/block_int.h"
27 #include "block/qcow2.h"
28 #include "qemu/range.h"
30 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, uint64_t size
);
31 static int QEMU_WARN_UNUSED_RESULT
update_refcount(BlockDriverState
*bs
,
32 int64_t offset
, int64_t length
,
33 int addend
, enum qcow2_discard_type type
);
36 /*********************************************************/
37 /* refcount handling */
39 int qcow2_refcount_init(BlockDriverState
*bs
)
41 BDRVQcowState
*s
= bs
->opaque
;
42 unsigned int refcount_table_size2
, i
;
45 assert(s
->refcount_table_size
<= INT_MAX
/ sizeof(uint64_t));
46 refcount_table_size2
= s
->refcount_table_size
* sizeof(uint64_t);
47 s
->refcount_table
= g_try_malloc(refcount_table_size2
);
49 if (s
->refcount_table_size
> 0) {
50 if (s
->refcount_table
== NULL
) {
54 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_LOAD
);
55 ret
= bdrv_pread(bs
->file
, s
->refcount_table_offset
,
56 s
->refcount_table
, refcount_table_size2
);
60 for(i
= 0; i
< s
->refcount_table_size
; i
++)
61 be64_to_cpus(&s
->refcount_table
[i
]);
68 void qcow2_refcount_close(BlockDriverState
*bs
)
70 BDRVQcowState
*s
= bs
->opaque
;
71 g_free(s
->refcount_table
);
75 static int load_refcount_block(BlockDriverState
*bs
,
76 int64_t refcount_block_offset
,
77 void **refcount_block
)
79 BDRVQcowState
*s
= bs
->opaque
;
82 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_LOAD
);
83 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refcount_block_offset
,
90 * Returns the refcount of the cluster given by its index. Any non-negative
91 * return value is the refcount of the cluster, negative values are -errno
92 * and indicate an error.
94 static int get_refcount(BlockDriverState
*bs
, int64_t cluster_index
)
96 BDRVQcowState
*s
= bs
->opaque
;
97 uint64_t refcount_table_index
, block_index
;
98 int64_t refcount_block_offset
;
100 uint16_t *refcount_block
;
103 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
104 if (refcount_table_index
>= s
->refcount_table_size
)
106 refcount_block_offset
=
107 s
->refcount_table
[refcount_table_index
] & REFT_OFFSET_MASK
;
108 if (!refcount_block_offset
)
111 if (offset_into_cluster(s
, refcount_block_offset
)) {
112 qcow2_signal_corruption(bs
, true, -1, -1, "Refblock offset %#" PRIx64
113 " unaligned (reftable index: %#" PRIx64
")",
114 refcount_block_offset
, refcount_table_index
);
118 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refcount_block_offset
,
119 (void**) &refcount_block
);
124 block_index
= cluster_index
&
125 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
126 refcount
= be16_to_cpu(refcount_block
[block_index
]);
128 ret
= qcow2_cache_put(bs
, s
->refcount_block_cache
,
129 (void**) &refcount_block
);
138 * Rounds the refcount table size up to avoid growing the table for each single
139 * refcount block that is allocated.
141 static unsigned int next_refcount_table_size(BDRVQcowState
*s
,
142 unsigned int min_size
)
144 unsigned int min_clusters
= (min_size
>> (s
->cluster_bits
- 3)) + 1;
145 unsigned int refcount_table_clusters
=
146 MAX(1, s
->refcount_table_size
>> (s
->cluster_bits
- 3));
148 while (min_clusters
> refcount_table_clusters
) {
149 refcount_table_clusters
= (refcount_table_clusters
* 3 + 1) / 2;
152 return refcount_table_clusters
<< (s
->cluster_bits
- 3);
156 /* Checks if two offsets are described by the same refcount block */
157 static int in_same_refcount_block(BDRVQcowState
*s
, uint64_t offset_a
,
160 uint64_t block_a
= offset_a
>> (2 * s
->cluster_bits
- REFCOUNT_SHIFT
);
161 uint64_t block_b
= offset_b
>> (2 * s
->cluster_bits
- REFCOUNT_SHIFT
);
163 return (block_a
== block_b
);
167 * Loads a refcount block. If it doesn't exist yet, it is allocated first
168 * (including growing the refcount table if needed).
170 * Returns 0 on success or -errno in error case
172 static int alloc_refcount_block(BlockDriverState
*bs
,
173 int64_t cluster_index
, uint16_t **refcount_block
)
175 BDRVQcowState
*s
= bs
->opaque
;
176 unsigned int refcount_table_index
;
179 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC
);
181 /* Find the refcount block for the given cluster */
182 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
184 if (refcount_table_index
< s
->refcount_table_size
) {
186 uint64_t refcount_block_offset
=
187 s
->refcount_table
[refcount_table_index
] & REFT_OFFSET_MASK
;
189 /* If it's already there, we're done */
190 if (refcount_block_offset
) {
191 if (offset_into_cluster(s
, refcount_block_offset
)) {
192 qcow2_signal_corruption(bs
, true, -1, -1, "Refblock offset %#"
193 PRIx64
" unaligned (reftable index: "
194 "%#x)", refcount_block_offset
,
195 refcount_table_index
);
199 return load_refcount_block(bs
, refcount_block_offset
,
200 (void**) refcount_block
);
205 * If we came here, we need to allocate something. Something is at least
206 * a cluster for the new refcount block. It may also include a new refcount
207 * table if the old refcount table is too small.
209 * Note that allocating clusters here needs some special care:
211 * - We can't use the normal qcow2_alloc_clusters(), it would try to
212 * increase the refcount and very likely we would end up with an endless
213 * recursion. Instead we must place the refcount blocks in a way that
214 * they can describe them themselves.
216 * - We need to consider that at this point we are inside update_refcounts
217 * and potentially doing an initial refcount increase. This means that
218 * some clusters have already been allocated by the caller, but their
219 * refcount isn't accurate yet. If we allocate clusters for metadata, we
220 * need to return -EAGAIN to signal the caller that it needs to restart
221 * the search for free clusters.
223 * - alloc_clusters_noref and qcow2_free_clusters may load a different
224 * refcount block into the cache
227 *refcount_block
= NULL
;
229 /* We write to the refcount table, so we might depend on L2 tables */
230 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
235 /* Allocate the refcount block itself and mark it as used */
236 int64_t new_block
= alloc_clusters_noref(bs
, s
->cluster_size
);
242 fprintf(stderr
, "qcow2: Allocate refcount block %d for %" PRIx64
244 refcount_table_index
, cluster_index
<< s
->cluster_bits
, new_block
);
247 if (in_same_refcount_block(s
, new_block
, cluster_index
<< s
->cluster_bits
)) {
248 /* Zero the new refcount block before updating it */
249 ret
= qcow2_cache_get_empty(bs
, s
->refcount_block_cache
, new_block
,
250 (void**) refcount_block
);
255 memset(*refcount_block
, 0, s
->cluster_size
);
257 /* The block describes itself, need to update the cache */
258 int block_index
= (new_block
>> s
->cluster_bits
) &
259 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
260 (*refcount_block
)[block_index
] = cpu_to_be16(1);
262 /* Described somewhere else. This can recurse at most twice before we
263 * arrive at a block that describes itself. */
264 ret
= update_refcount(bs
, new_block
, s
->cluster_size
, 1,
265 QCOW2_DISCARD_NEVER
);
270 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
275 /* Initialize the new refcount block only after updating its refcount,
276 * update_refcount uses the refcount cache itself */
277 ret
= qcow2_cache_get_empty(bs
, s
->refcount_block_cache
, new_block
,
278 (void**) refcount_block
);
283 memset(*refcount_block
, 0, s
->cluster_size
);
286 /* Now the new refcount block needs to be written to disk */
287 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE
);
288 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
, *refcount_block
);
289 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
294 /* If the refcount table is big enough, just hook the block up there */
295 if (refcount_table_index
< s
->refcount_table_size
) {
296 uint64_t data64
= cpu_to_be64(new_block
);
297 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_HOOKUP
);
298 ret
= bdrv_pwrite_sync(bs
->file
,
299 s
->refcount_table_offset
+ refcount_table_index
* sizeof(uint64_t),
300 &data64
, sizeof(data64
));
305 s
->refcount_table
[refcount_table_index
] = new_block
;
307 /* The new refcount block may be where the caller intended to put its
308 * data, so let it restart the search. */
312 ret
= qcow2_cache_put(bs
, s
->refcount_block_cache
, (void**) refcount_block
);
318 * If we come here, we need to grow the refcount table. Again, a new
319 * refcount table needs some space and we can't simply allocate to avoid
322 * Therefore let's grab new refcount blocks at the end of the image, which
323 * will describe themselves and the new refcount table. This way we can
324 * reference them only in the new table and do the switch to the new
325 * refcount table at once without producing an inconsistent state in
328 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_GROW
);
330 /* Calculate the number of refcount blocks needed so far */
331 uint64_t refcount_block_clusters
= 1 << (s
->cluster_bits
- REFCOUNT_SHIFT
);
332 uint64_t blocks_used
= DIV_ROUND_UP(cluster_index
, refcount_block_clusters
);
334 if (blocks_used
> QCOW_MAX_REFTABLE_SIZE
/ sizeof(uint64_t)) {
338 /* And now we need at least one block more for the new metadata */
339 uint64_t table_size
= next_refcount_table_size(s
, blocks_used
+ 1);
340 uint64_t last_table_size
;
341 uint64_t blocks_clusters
;
343 uint64_t table_clusters
=
344 size_to_clusters(s
, table_size
* sizeof(uint64_t));
345 blocks_clusters
= 1 +
346 ((table_clusters
+ refcount_block_clusters
- 1)
347 / refcount_block_clusters
);
348 uint64_t meta_clusters
= table_clusters
+ blocks_clusters
;
350 last_table_size
= table_size
;
351 table_size
= next_refcount_table_size(s
, blocks_used
+
352 ((meta_clusters
+ refcount_block_clusters
- 1)
353 / refcount_block_clusters
));
355 } while (last_table_size
!= table_size
);
358 fprintf(stderr
, "qcow2: Grow refcount table %" PRId32
" => %" PRId64
"\n",
359 s
->refcount_table_size
, table_size
);
362 /* Create the new refcount table and blocks */
363 uint64_t meta_offset
= (blocks_used
* refcount_block_clusters
) *
365 uint64_t table_offset
= meta_offset
+ blocks_clusters
* s
->cluster_size
;
366 uint64_t *new_table
= g_try_new0(uint64_t, table_size
);
367 uint16_t *new_blocks
= g_try_malloc0(blocks_clusters
* s
->cluster_size
);
369 assert(table_size
> 0 && blocks_clusters
> 0);
370 if (new_table
== NULL
|| new_blocks
== NULL
) {
375 /* Fill the new refcount table */
376 memcpy(new_table
, s
->refcount_table
,
377 s
->refcount_table_size
* sizeof(uint64_t));
378 new_table
[refcount_table_index
] = new_block
;
381 for (i
= 0; i
< blocks_clusters
; i
++) {
382 new_table
[blocks_used
+ i
] = meta_offset
+ (i
* s
->cluster_size
);
385 /* Fill the refcount blocks */
386 uint64_t table_clusters
= size_to_clusters(s
, table_size
* sizeof(uint64_t));
388 for (i
= 0; i
< table_clusters
+ blocks_clusters
; i
++) {
389 new_blocks
[block
++] = cpu_to_be16(1);
392 /* Write refcount blocks to disk */
393 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS
);
394 ret
= bdrv_pwrite_sync(bs
->file
, meta_offset
, new_blocks
,
395 blocks_clusters
* s
->cluster_size
);
402 /* Write refcount table to disk */
403 for(i
= 0; i
< table_size
; i
++) {
404 cpu_to_be64s(&new_table
[i
]);
407 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE
);
408 ret
= bdrv_pwrite_sync(bs
->file
, table_offset
, new_table
,
409 table_size
* sizeof(uint64_t));
414 for(i
= 0; i
< table_size
; i
++) {
415 be64_to_cpus(&new_table
[i
]);
418 /* Hook up the new refcount table in the qcow2 header */
420 cpu_to_be64w((uint64_t*)data
, table_offset
);
421 cpu_to_be32w((uint32_t*)(data
+ 8), table_clusters
);
422 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE
);
423 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, refcount_table_offset
),
429 /* And switch it in memory */
430 uint64_t old_table_offset
= s
->refcount_table_offset
;
431 uint64_t old_table_size
= s
->refcount_table_size
;
433 g_free(s
->refcount_table
);
434 s
->refcount_table
= new_table
;
435 s
->refcount_table_size
= table_size
;
436 s
->refcount_table_offset
= table_offset
;
438 /* Free old table. */
439 qcow2_free_clusters(bs
, old_table_offset
, old_table_size
* sizeof(uint64_t),
440 QCOW2_DISCARD_OTHER
);
442 ret
= load_refcount_block(bs
, new_block
, (void**) refcount_block
);
447 /* If we were trying to do the initial refcount update for some cluster
448 * allocation, we might have used the same clusters to store newly
449 * allocated metadata. Make the caller search some new space. */
456 if (*refcount_block
!= NULL
) {
457 qcow2_cache_put(bs
, s
->refcount_block_cache
, (void**) refcount_block
);
462 void qcow2_process_discards(BlockDriverState
*bs
, int ret
)
464 BDRVQcowState
*s
= bs
->opaque
;
465 Qcow2DiscardRegion
*d
, *next
;
467 QTAILQ_FOREACH_SAFE(d
, &s
->discards
, next
, next
) {
468 QTAILQ_REMOVE(&s
->discards
, d
, next
);
470 /* Discard is optional, ignore the return value */
472 bdrv_discard(bs
->file
,
473 d
->offset
>> BDRV_SECTOR_BITS
,
474 d
->bytes
>> BDRV_SECTOR_BITS
);
481 static void update_refcount_discard(BlockDriverState
*bs
,
482 uint64_t offset
, uint64_t length
)
484 BDRVQcowState
*s
= bs
->opaque
;
485 Qcow2DiscardRegion
*d
, *p
, *next
;
487 QTAILQ_FOREACH(d
, &s
->discards
, next
) {
488 uint64_t new_start
= MIN(offset
, d
->offset
);
489 uint64_t new_end
= MAX(offset
+ length
, d
->offset
+ d
->bytes
);
491 if (new_end
- new_start
<= length
+ d
->bytes
) {
492 /* There can't be any overlap, areas ending up here have no
493 * references any more and therefore shouldn't get freed another
495 assert(d
->bytes
+ length
== new_end
- new_start
);
496 d
->offset
= new_start
;
497 d
->bytes
= new_end
- new_start
;
502 d
= g_malloc(sizeof(*d
));
503 *d
= (Qcow2DiscardRegion
) {
508 QTAILQ_INSERT_TAIL(&s
->discards
, d
, next
);
511 /* Merge discard requests if they are adjacent now */
512 QTAILQ_FOREACH_SAFE(p
, &s
->discards
, next
, next
) {
514 || p
->offset
> d
->offset
+ d
->bytes
515 || d
->offset
> p
->offset
+ p
->bytes
)
520 /* Still no overlap possible */
521 assert(p
->offset
== d
->offset
+ d
->bytes
522 || d
->offset
== p
->offset
+ p
->bytes
);
524 QTAILQ_REMOVE(&s
->discards
, p
, next
);
525 d
->offset
= MIN(d
->offset
, p
->offset
);
526 d
->bytes
+= p
->bytes
;
531 /* XXX: cache several refcount block clusters ? */
532 static int QEMU_WARN_UNUSED_RESULT
update_refcount(BlockDriverState
*bs
,
533 int64_t offset
, int64_t length
, int addend
, enum qcow2_discard_type type
)
535 BDRVQcowState
*s
= bs
->opaque
;
536 int64_t start
, last
, cluster_offset
;
537 uint16_t *refcount_block
= NULL
;
538 int64_t old_table_index
= -1;
542 fprintf(stderr
, "update_refcount: offset=%" PRId64
" size=%" PRId64
" addend=%d\n",
543 offset
, length
, addend
);
547 } else if (length
== 0) {
552 qcow2_cache_set_dependency(bs
, s
->refcount_block_cache
,
556 start
= start_of_cluster(s
, offset
);
557 last
= start_of_cluster(s
, offset
+ length
- 1);
558 for(cluster_offset
= start
; cluster_offset
<= last
;
559 cluster_offset
+= s
->cluster_size
)
561 int block_index
, refcount
;
562 int64_t cluster_index
= cluster_offset
>> s
->cluster_bits
;
563 int64_t table_index
=
564 cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
566 /* Load the refcount block and allocate it if needed */
567 if (table_index
!= old_table_index
) {
568 if (refcount_block
) {
569 ret
= qcow2_cache_put(bs
, s
->refcount_block_cache
,
570 (void**) &refcount_block
);
576 ret
= alloc_refcount_block(bs
, cluster_index
, &refcount_block
);
581 old_table_index
= table_index
;
583 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
, refcount_block
);
585 /* we can update the count and save it */
586 block_index
= cluster_index
&
587 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
589 refcount
= be16_to_cpu(refcount_block
[block_index
]);
591 if (refcount
< 0 || refcount
> 0xffff) {
595 if (refcount
== 0 && cluster_index
< s
->free_cluster_index
) {
596 s
->free_cluster_index
= cluster_index
;
598 refcount_block
[block_index
] = cpu_to_be16(refcount
);
600 if (refcount
== 0 && s
->discard_passthrough
[type
]) {
601 update_refcount_discard(bs
, cluster_offset
, s
->cluster_size
);
607 if (!s
->cache_discards
) {
608 qcow2_process_discards(bs
, ret
);
611 /* Write last changed block to disk */
612 if (refcount_block
) {
614 wret
= qcow2_cache_put(bs
, s
->refcount_block_cache
,
615 (void**) &refcount_block
);
617 return ret
< 0 ? ret
: wret
;
622 * Try do undo any updates if an error is returned (This may succeed in
623 * some cases like ENOSPC for allocating a new refcount block)
627 dummy
= update_refcount(bs
, offset
, cluster_offset
- offset
, -addend
,
628 QCOW2_DISCARD_NEVER
);
636 * Increases or decreases the refcount of a given cluster by one.
637 * addend must be 1 or -1.
639 * If the return value is non-negative, it is the new refcount of the cluster.
640 * If it is negative, it is -errno and indicates an error.
642 int qcow2_update_cluster_refcount(BlockDriverState
*bs
,
643 int64_t cluster_index
,
645 enum qcow2_discard_type type
)
647 BDRVQcowState
*s
= bs
->opaque
;
650 ret
= update_refcount(bs
, cluster_index
<< s
->cluster_bits
, 1, addend
,
656 return get_refcount(bs
, cluster_index
);
661 /*********************************************************/
662 /* cluster allocation functions */
666 /* return < 0 if error */
667 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, uint64_t size
)
669 BDRVQcowState
*s
= bs
->opaque
;
670 uint64_t i
, nb_clusters
;
673 nb_clusters
= size_to_clusters(s
, size
);
675 for(i
= 0; i
< nb_clusters
; i
++) {
676 uint64_t next_cluster_index
= s
->free_cluster_index
++;
677 refcount
= get_refcount(bs
, next_cluster_index
);
681 } else if (refcount
!= 0) {
686 /* Make sure that all offsets in the "allocated" range are representable
688 if (s
->free_cluster_index
> 0 &&
689 s
->free_cluster_index
- 1 > (INT64_MAX
>> s
->cluster_bits
))
695 fprintf(stderr
, "alloc_clusters: size=%" PRId64
" -> %" PRId64
"\n",
697 (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
);
699 return (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
;
702 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, uint64_t size
)
707 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC
);
709 offset
= alloc_clusters_noref(bs
, size
);
714 ret
= update_refcount(bs
, offset
, size
, 1, QCOW2_DISCARD_NEVER
);
715 } while (ret
== -EAGAIN
);
724 int qcow2_alloc_clusters_at(BlockDriverState
*bs
, uint64_t offset
,
727 BDRVQcowState
*s
= bs
->opaque
;
728 uint64_t cluster_index
;
732 assert(nb_clusters
>= 0);
733 if (nb_clusters
== 0) {
738 /* Check how many clusters there are free */
739 cluster_index
= offset
>> s
->cluster_bits
;
740 for(i
= 0; i
< nb_clusters
; i
++) {
741 refcount
= get_refcount(bs
, cluster_index
++);
745 } else if (refcount
!= 0) {
750 /* And then allocate them */
751 ret
= update_refcount(bs
, offset
, i
<< s
->cluster_bits
, 1,
752 QCOW2_DISCARD_NEVER
);
753 } while (ret
== -EAGAIN
);
762 /* only used to allocate compressed sectors. We try to allocate
763 contiguous sectors. size must be <= cluster_size */
764 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
)
766 BDRVQcowState
*s
= bs
->opaque
;
767 int64_t offset
, cluster_offset
;
770 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC_BYTES
);
771 assert(size
> 0 && size
<= s
->cluster_size
);
772 if (s
->free_byte_offset
== 0) {
773 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
777 s
->free_byte_offset
= offset
;
780 free_in_cluster
= s
->cluster_size
-
781 offset_into_cluster(s
, s
->free_byte_offset
);
782 if (size
<= free_in_cluster
) {
783 /* enough space in current cluster */
784 offset
= s
->free_byte_offset
;
785 s
->free_byte_offset
+= size
;
786 free_in_cluster
-= size
;
787 if (free_in_cluster
== 0)
788 s
->free_byte_offset
= 0;
789 if (offset_into_cluster(s
, offset
) != 0)
790 qcow2_update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1,
791 QCOW2_DISCARD_NEVER
);
793 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
797 cluster_offset
= start_of_cluster(s
, s
->free_byte_offset
);
798 if ((cluster_offset
+ s
->cluster_size
) == offset
) {
799 /* we are lucky: contiguous data */
800 offset
= s
->free_byte_offset
;
801 qcow2_update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1,
802 QCOW2_DISCARD_NEVER
);
803 s
->free_byte_offset
+= size
;
805 s
->free_byte_offset
= offset
;
810 /* The cluster refcount was incremented, either by qcow2_alloc_clusters()
811 * or explicitly by qcow2_update_cluster_refcount(). Refcount blocks must
812 * be flushed before the caller's L2 table updates.
814 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
, s
->refcount_block_cache
);
818 void qcow2_free_clusters(BlockDriverState
*bs
,
819 int64_t offset
, int64_t size
,
820 enum qcow2_discard_type type
)
824 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_FREE
);
825 ret
= update_refcount(bs
, offset
, size
, -1, type
);
827 fprintf(stderr
, "qcow2_free_clusters failed: %s\n", strerror(-ret
));
828 /* TODO Remember the clusters to free them later and avoid leaking */
833 * Free a cluster using its L2 entry (handles clusters of all types, e.g.
834 * normal cluster, compressed cluster, etc.)
836 void qcow2_free_any_clusters(BlockDriverState
*bs
, uint64_t l2_entry
,
837 int nb_clusters
, enum qcow2_discard_type type
)
839 BDRVQcowState
*s
= bs
->opaque
;
841 switch (qcow2_get_cluster_type(l2_entry
)) {
842 case QCOW2_CLUSTER_COMPRESSED
:
845 nb_csectors
= ((l2_entry
>> s
->csize_shift
) &
847 qcow2_free_clusters(bs
,
848 (l2_entry
& s
->cluster_offset_mask
) & ~511,
849 nb_csectors
* 512, type
);
852 case QCOW2_CLUSTER_NORMAL
:
853 case QCOW2_CLUSTER_ZERO
:
854 if (l2_entry
& L2E_OFFSET_MASK
) {
855 if (offset_into_cluster(s
, l2_entry
& L2E_OFFSET_MASK
)) {
856 qcow2_signal_corruption(bs
, false, -1, -1,
857 "Cannot free unaligned cluster %#llx",
858 l2_entry
& L2E_OFFSET_MASK
);
860 qcow2_free_clusters(bs
, l2_entry
& L2E_OFFSET_MASK
,
861 nb_clusters
<< s
->cluster_bits
, type
);
865 case QCOW2_CLUSTER_UNALLOCATED
:
874 /*********************************************************/
875 /* snapshots and image creation */
879 /* update the refcounts of snapshots and the copied flag */
880 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
881 int64_t l1_table_offset
, int l1_size
, int addend
)
883 BDRVQcowState
*s
= bs
->opaque
;
884 uint64_t *l1_table
, *l2_table
, l2_offset
, offset
, l1_size2
;
885 bool l1_allocated
= false;
886 int64_t old_offset
, old_l2_offset
;
887 int i
, j
, l1_modified
= 0, nb_csectors
, refcount
;
892 l1_size2
= l1_size
* sizeof(uint64_t);
894 s
->cache_discards
= true;
896 /* WARNING: qcow2_snapshot_goto relies on this function not using the
897 * l1_table_offset when it is the current s->l1_table_offset! Be careful
898 * when changing this! */
899 if (l1_table_offset
!= s
->l1_table_offset
) {
900 l1_table
= g_try_malloc0(align_offset(l1_size2
, 512));
901 if (l1_size2
&& l1_table
== NULL
) {
907 ret
= bdrv_pread(bs
->file
, l1_table_offset
, l1_table
, l1_size2
);
912 for(i
= 0;i
< l1_size
; i
++)
913 be64_to_cpus(&l1_table
[i
]);
915 assert(l1_size
== s
->l1_size
);
916 l1_table
= s
->l1_table
;
917 l1_allocated
= false;
920 for(i
= 0; i
< l1_size
; i
++) {
921 l2_offset
= l1_table
[i
];
923 old_l2_offset
= l2_offset
;
924 l2_offset
&= L1E_OFFSET_MASK
;
926 if (offset_into_cluster(s
, l2_offset
)) {
927 qcow2_signal_corruption(bs
, true, -1, -1, "L2 table offset %#"
928 PRIx64
" unaligned (L1 index: %#x)",
934 ret
= qcow2_cache_get(bs
, s
->l2_table_cache
, l2_offset
,
940 for(j
= 0; j
< s
->l2_size
; j
++) {
941 uint64_t cluster_index
;
943 offset
= be64_to_cpu(l2_table
[j
]);
945 offset
&= ~QCOW_OFLAG_COPIED
;
947 switch (qcow2_get_cluster_type(offset
)) {
948 case QCOW2_CLUSTER_COMPRESSED
:
949 nb_csectors
= ((offset
>> s
->csize_shift
) &
952 ret
= update_refcount(bs
,
953 (offset
& s
->cluster_offset_mask
) & ~511,
954 nb_csectors
* 512, addend
,
955 QCOW2_DISCARD_SNAPSHOT
);
960 /* compressed clusters are never modified */
964 case QCOW2_CLUSTER_NORMAL
:
965 case QCOW2_CLUSTER_ZERO
:
966 if (offset_into_cluster(s
, offset
& L2E_OFFSET_MASK
)) {
967 qcow2_signal_corruption(bs
, true, -1, -1, "Data "
968 "cluster offset %#llx "
969 "unaligned (L2 offset: %#"
970 PRIx64
", L2 index: %#x)",
971 offset
& L2E_OFFSET_MASK
,
977 cluster_index
= (offset
& L2E_OFFSET_MASK
) >> s
->cluster_bits
;
978 if (!cluster_index
) {
984 refcount
= qcow2_update_cluster_refcount(bs
,
985 cluster_index
, addend
,
986 QCOW2_DISCARD_SNAPSHOT
);
988 refcount
= get_refcount(bs
, cluster_index
);
997 case QCOW2_CLUSTER_UNALLOCATED
:
1005 if (refcount
== 1) {
1006 offset
|= QCOW_OFLAG_COPIED
;
1008 if (offset
!= old_offset
) {
1010 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
,
1011 s
->refcount_block_cache
);
1013 l2_table
[j
] = cpu_to_be64(offset
);
1014 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_table
);
1018 ret
= qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
1025 refcount
= qcow2_update_cluster_refcount(bs
, l2_offset
>>
1026 s
->cluster_bits
, addend
, QCOW2_DISCARD_SNAPSHOT
);
1028 refcount
= get_refcount(bs
, l2_offset
>> s
->cluster_bits
);
1033 } else if (refcount
== 1) {
1034 l2_offset
|= QCOW_OFLAG_COPIED
;
1036 if (l2_offset
!= old_l2_offset
) {
1037 l1_table
[i
] = l2_offset
;
1043 ret
= bdrv_flush(bs
);
1046 qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
1049 s
->cache_discards
= false;
1050 qcow2_process_discards(bs
, ret
);
1052 /* Update L1 only if it isn't deleted anyway (addend = -1) */
1053 if (ret
== 0 && addend
>= 0 && l1_modified
) {
1054 for (i
= 0; i
< l1_size
; i
++) {
1055 cpu_to_be64s(&l1_table
[i
]);
1058 ret
= bdrv_pwrite_sync(bs
->file
, l1_table_offset
, l1_table
, l1_size2
);
1060 for (i
= 0; i
< l1_size
; i
++) {
1061 be64_to_cpus(&l1_table
[i
]);
1072 /*********************************************************/
1073 /* refcount checking functions */
1078 * Increases the refcount for a range of clusters in a given refcount table.
1079 * This is used to construct a temporary refcount table out of L1 and L2 tables
1080 * which can be compared the the refcount table saved in the image.
1082 * Modifies the number of errors in res.
1084 static void inc_refcounts(BlockDriverState
*bs
,
1085 BdrvCheckResult
*res
,
1086 uint16_t *refcount_table
,
1087 int refcount_table_size
,
1088 int64_t offset
, int64_t size
)
1090 BDRVQcowState
*s
= bs
->opaque
;
1091 uint64_t start
, last
, cluster_offset
, k
;
1096 start
= start_of_cluster(s
, offset
);
1097 last
= start_of_cluster(s
, offset
+ size
- 1);
1098 for(cluster_offset
= start
; cluster_offset
<= last
;
1099 cluster_offset
+= s
->cluster_size
) {
1100 k
= cluster_offset
>> s
->cluster_bits
;
1101 if (k
>= refcount_table_size
) {
1102 fprintf(stderr
, "Warning: cluster offset=0x%" PRIx64
" is after "
1103 "the end of the image file, can't properly check refcounts.\n",
1105 res
->check_errors
++;
1107 if (++refcount_table
[k
] == 0) {
1108 fprintf(stderr
, "ERROR: overflow cluster offset=0x%" PRIx64
1109 "\n", cluster_offset
);
1116 /* Flags for check_refcounts_l1() and check_refcounts_l2() */
1118 CHECK_FRAG_INFO
= 0x2, /* update BlockFragInfo counters */
1122 * Increases the refcount in the given refcount table for the all clusters
1123 * referenced in the L2 table. While doing so, performs some checks on L2
1126 * Returns the number of errors found by the checks or -errno if an internal
1129 static int check_refcounts_l2(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1130 uint16_t *refcount_table
, int refcount_table_size
, int64_t l2_offset
,
1133 BDRVQcowState
*s
= bs
->opaque
;
1134 uint64_t *l2_table
, l2_entry
;
1135 uint64_t next_contiguous_offset
= 0;
1136 int i
, l2_size
, nb_csectors
;
1138 /* Read L2 table from disk */
1139 l2_size
= s
->l2_size
* sizeof(uint64_t);
1140 l2_table
= g_malloc(l2_size
);
1142 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
, l2_size
) != l2_size
)
1145 /* Do the actual checks */
1146 for(i
= 0; i
< s
->l2_size
; i
++) {
1147 l2_entry
= be64_to_cpu(l2_table
[i
]);
1149 switch (qcow2_get_cluster_type(l2_entry
)) {
1150 case QCOW2_CLUSTER_COMPRESSED
:
1151 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
1152 if (l2_entry
& QCOW_OFLAG_COPIED
) {
1153 fprintf(stderr
, "ERROR: cluster %" PRId64
": "
1154 "copied flag must never be set for compressed "
1155 "clusters\n", l2_entry
>> s
->cluster_bits
);
1156 l2_entry
&= ~QCOW_OFLAG_COPIED
;
1160 /* Mark cluster as used */
1161 nb_csectors
= ((l2_entry
>> s
->csize_shift
) &
1163 l2_entry
&= s
->cluster_offset_mask
;
1164 inc_refcounts(bs
, res
, refcount_table
, refcount_table_size
,
1165 l2_entry
& ~511, nb_csectors
* 512);
1167 if (flags
& CHECK_FRAG_INFO
) {
1168 res
->bfi
.allocated_clusters
++;
1169 res
->bfi
.compressed_clusters
++;
1171 /* Compressed clusters are fragmented by nature. Since they
1172 * take up sub-sector space but we only have sector granularity
1173 * I/O we need to re-read the same sectors even for adjacent
1174 * compressed clusters.
1176 res
->bfi
.fragmented_clusters
++;
1180 case QCOW2_CLUSTER_ZERO
:
1181 if ((l2_entry
& L2E_OFFSET_MASK
) == 0) {
1186 case QCOW2_CLUSTER_NORMAL
:
1188 uint64_t offset
= l2_entry
& L2E_OFFSET_MASK
;
1190 if (flags
& CHECK_FRAG_INFO
) {
1191 res
->bfi
.allocated_clusters
++;
1192 if (next_contiguous_offset
&&
1193 offset
!= next_contiguous_offset
) {
1194 res
->bfi
.fragmented_clusters
++;
1196 next_contiguous_offset
= offset
+ s
->cluster_size
;
1199 /* Mark cluster as used */
1200 inc_refcounts(bs
, res
, refcount_table
,refcount_table_size
,
1201 offset
, s
->cluster_size
);
1203 /* Correct offsets are cluster aligned */
1204 if (offset_into_cluster(s
, offset
)) {
1205 fprintf(stderr
, "ERROR offset=%" PRIx64
": Cluster is not "
1206 "properly aligned; L2 entry corrupted.\n", offset
);
1212 case QCOW2_CLUSTER_UNALLOCATED
:
1224 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l2\n");
1230 * Increases the refcount for the L1 table, its L2 tables and all referenced
1231 * clusters in the given refcount table. While doing so, performs some checks
1232 * on L1 and L2 entries.
1234 * Returns the number of errors found by the checks or -errno if an internal
1237 static int check_refcounts_l1(BlockDriverState
*bs
,
1238 BdrvCheckResult
*res
,
1239 uint16_t *refcount_table
,
1240 int refcount_table_size
,
1241 int64_t l1_table_offset
, int l1_size
,
1244 BDRVQcowState
*s
= bs
->opaque
;
1245 uint64_t *l1_table
, l2_offset
, l1_size2
;
1248 l1_size2
= l1_size
* sizeof(uint64_t);
1250 /* Mark L1 table as used */
1251 inc_refcounts(bs
, res
, refcount_table
, refcount_table_size
,
1252 l1_table_offset
, l1_size2
);
1254 /* Read L1 table entries from disk */
1255 if (l1_size2
== 0) {
1258 l1_table
= g_try_malloc(l1_size2
);
1259 if (l1_table
== NULL
) {
1263 if (bdrv_pread(bs
->file
, l1_table_offset
,
1264 l1_table
, l1_size2
) != l1_size2
)
1266 for(i
= 0;i
< l1_size
; i
++)
1267 be64_to_cpus(&l1_table
[i
]);
1270 /* Do the actual checks */
1271 for(i
= 0; i
< l1_size
; i
++) {
1272 l2_offset
= l1_table
[i
];
1274 /* Mark L2 table as used */
1275 l2_offset
&= L1E_OFFSET_MASK
;
1276 inc_refcounts(bs
, res
, refcount_table
, refcount_table_size
,
1277 l2_offset
, s
->cluster_size
);
1279 /* L2 tables are cluster aligned */
1280 if (offset_into_cluster(s
, l2_offset
)) {
1281 fprintf(stderr
, "ERROR l2_offset=%" PRIx64
": Table is not "
1282 "cluster aligned; L1 entry corrupted\n", l2_offset
);
1286 /* Process and check L2 entries */
1287 ret
= check_refcounts_l2(bs
, res
, refcount_table
,
1288 refcount_table_size
, l2_offset
, flags
);
1298 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
1299 res
->check_errors
++;
1305 * Checks the OFLAG_COPIED flag for all L1 and L2 entries.
1307 * This function does not print an error message nor does it increment
1308 * check_errors if get_refcount fails (this is because such an error will have
1309 * been already detected and sufficiently signaled by the calling function
1310 * (qcow2_check_refcounts) by the time this function is called).
1312 static int check_oflag_copied(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1315 BDRVQcowState
*s
= bs
->opaque
;
1316 uint64_t *l2_table
= qemu_blockalign(bs
, s
->cluster_size
);
1321 for (i
= 0; i
< s
->l1_size
; i
++) {
1322 uint64_t l1_entry
= s
->l1_table
[i
];
1323 uint64_t l2_offset
= l1_entry
& L1E_OFFSET_MASK
;
1324 bool l2_dirty
= false;
1330 refcount
= get_refcount(bs
, l2_offset
>> s
->cluster_bits
);
1332 /* don't print message nor increment check_errors */
1335 if ((refcount
== 1) != ((l1_entry
& QCOW_OFLAG_COPIED
) != 0)) {
1336 fprintf(stderr
, "%s OFLAG_COPIED L2 cluster: l1_index=%d "
1337 "l1_entry=%" PRIx64
" refcount=%d\n",
1338 fix
& BDRV_FIX_ERRORS
? "Repairing" :
1340 i
, l1_entry
, refcount
);
1341 if (fix
& BDRV_FIX_ERRORS
) {
1342 s
->l1_table
[i
] = refcount
== 1
1343 ? l1_entry
| QCOW_OFLAG_COPIED
1344 : l1_entry
& ~QCOW_OFLAG_COPIED
;
1345 ret
= qcow2_write_l1_entry(bs
, i
);
1347 res
->check_errors
++;
1350 res
->corruptions_fixed
++;
1356 ret
= bdrv_pread(bs
->file
, l2_offset
, l2_table
,
1357 s
->l2_size
* sizeof(uint64_t));
1359 fprintf(stderr
, "ERROR: Could not read L2 table: %s\n",
1361 res
->check_errors
++;
1365 for (j
= 0; j
< s
->l2_size
; j
++) {
1366 uint64_t l2_entry
= be64_to_cpu(l2_table
[j
]);
1367 uint64_t data_offset
= l2_entry
& L2E_OFFSET_MASK
;
1368 int cluster_type
= qcow2_get_cluster_type(l2_entry
);
1370 if ((cluster_type
== QCOW2_CLUSTER_NORMAL
) ||
1371 ((cluster_type
== QCOW2_CLUSTER_ZERO
) && (data_offset
!= 0))) {
1372 refcount
= get_refcount(bs
, data_offset
>> s
->cluster_bits
);
1374 /* don't print message nor increment check_errors */
1377 if ((refcount
== 1) != ((l2_entry
& QCOW_OFLAG_COPIED
) != 0)) {
1378 fprintf(stderr
, "%s OFLAG_COPIED data cluster: "
1379 "l2_entry=%" PRIx64
" refcount=%d\n",
1380 fix
& BDRV_FIX_ERRORS
? "Repairing" :
1382 l2_entry
, refcount
);
1383 if (fix
& BDRV_FIX_ERRORS
) {
1384 l2_table
[j
] = cpu_to_be64(refcount
== 1
1385 ? l2_entry
| QCOW_OFLAG_COPIED
1386 : l2_entry
& ~QCOW_OFLAG_COPIED
);
1388 res
->corruptions_fixed
++;
1397 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L2
,
1398 l2_offset
, s
->cluster_size
);
1400 fprintf(stderr
, "ERROR: Could not write L2 table; metadata "
1401 "overlap check failed: %s\n", strerror(-ret
));
1402 res
->check_errors
++;
1406 ret
= bdrv_pwrite(bs
->file
, l2_offset
, l2_table
, s
->cluster_size
);
1408 fprintf(stderr
, "ERROR: Could not write L2 table: %s\n",
1410 res
->check_errors
++;
1419 qemu_vfree(l2_table
);
1424 * Writes one sector of the refcount table to the disk
1426 #define RT_ENTRIES_PER_SECTOR (512 / sizeof(uint64_t))
1427 static int write_reftable_entry(BlockDriverState
*bs
, int rt_index
)
1429 BDRVQcowState
*s
= bs
->opaque
;
1430 uint64_t buf
[RT_ENTRIES_PER_SECTOR
];
1434 rt_start_index
= rt_index
& ~(RT_ENTRIES_PER_SECTOR
- 1);
1435 for (i
= 0; i
< RT_ENTRIES_PER_SECTOR
; i
++) {
1436 buf
[i
] = cpu_to_be64(s
->refcount_table
[rt_start_index
+ i
]);
1439 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_REFCOUNT_TABLE
,
1440 s
->refcount_table_offset
+ rt_start_index
* sizeof(uint64_t),
1446 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_UPDATE
);
1447 ret
= bdrv_pwrite_sync(bs
->file
, s
->refcount_table_offset
+
1448 rt_start_index
* sizeof(uint64_t), buf
, sizeof(buf
));
1457 * Allocates a new cluster for the given refcount block (represented by its
1458 * offset in the image file) and copies the current content there. This function
1459 * does _not_ decrement the reference count for the currently occupied cluster.
1461 * This function prints an informative message to stderr on error (and returns
1462 * -errno); on success, the offset of the newly allocated cluster is returned.
1464 static int64_t realloc_refcount_block(BlockDriverState
*bs
, int reftable_index
,
1467 BDRVQcowState
*s
= bs
->opaque
;
1468 int64_t new_offset
= 0;
1469 void *refcount_block
= NULL
;
1472 /* allocate new refcount block */
1473 new_offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
1474 if (new_offset
< 0) {
1475 fprintf(stderr
, "Could not allocate new cluster: %s\n",
1476 strerror(-new_offset
));
1481 /* fetch current refcount block content */
1482 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, offset
, &refcount_block
);
1484 fprintf(stderr
, "Could not fetch refcount block: %s\n", strerror(-ret
));
1485 goto fail_free_cluster
;
1488 /* new block has not yet been entered into refcount table, therefore it is
1489 * no refcount block yet (regarding this check) */
1490 ret
= qcow2_pre_write_overlap_check(bs
, 0, new_offset
, s
->cluster_size
);
1492 fprintf(stderr
, "Could not write refcount block; metadata overlap "
1493 "check failed: %s\n", strerror(-ret
));
1494 /* the image will be marked corrupt, so don't even attempt on freeing
1499 /* write to new block */
1500 ret
= bdrv_write(bs
->file
, new_offset
/ BDRV_SECTOR_SIZE
, refcount_block
,
1501 s
->cluster_sectors
);
1503 fprintf(stderr
, "Could not write refcount block: %s\n", strerror(-ret
));
1504 goto fail_free_cluster
;
1507 /* update refcount table */
1508 assert(!offset_into_cluster(s
, new_offset
));
1509 s
->refcount_table
[reftable_index
] = new_offset
;
1510 ret
= write_reftable_entry(bs
, reftable_index
);
1512 fprintf(stderr
, "Could not update refcount table: %s\n",
1514 goto fail_free_cluster
;
1520 qcow2_free_clusters(bs
, new_offset
, s
->cluster_size
, QCOW2_DISCARD_OTHER
);
1523 if (refcount_block
) {
1524 /* This should never fail, as it would only do so if the given refcount
1525 * block cannot be found in the cache. As this is impossible as long as
1526 * there are no bugs, assert the success. */
1527 int tmp
= qcow2_cache_put(bs
, s
->refcount_block_cache
, &refcount_block
);
1539 * Checks consistency of refblocks and accounts for each refblock in
1542 static int check_refblocks(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1543 BdrvCheckMode fix
, uint16_t **refcount_table
,
1544 int64_t *nb_clusters
)
1546 BDRVQcowState
*s
= bs
->opaque
;
1549 for(i
= 0; i
< s
->refcount_table_size
; i
++) {
1550 uint64_t offset
, cluster
;
1551 offset
= s
->refcount_table
[i
];
1552 cluster
= offset
>> s
->cluster_bits
;
1554 /* Refcount blocks are cluster aligned */
1555 if (offset_into_cluster(s
, offset
)) {
1556 fprintf(stderr
, "ERROR refcount block %" PRId64
" is not "
1557 "cluster aligned; refcount table entry corrupted\n", i
);
1562 if (cluster
>= *nb_clusters
) {
1563 fprintf(stderr
, "ERROR refcount block %" PRId64
1564 " is outside image\n", i
);
1570 inc_refcounts(bs
, res
, *refcount_table
, *nb_clusters
,
1571 offset
, s
->cluster_size
);
1572 if ((*refcount_table
)[cluster
] != 1) {
1573 fprintf(stderr
, "%s refcount block %" PRId64
1575 fix
& BDRV_FIX_ERRORS
? "Repairing" :
1577 i
, (*refcount_table
)[cluster
]);
1579 if (fix
& BDRV_FIX_ERRORS
) {
1582 new_offset
= realloc_refcount_block(bs
, i
, offset
);
1583 if (new_offset
< 0) {
1588 /* update refcounts */
1589 if ((new_offset
>> s
->cluster_bits
) >= *nb_clusters
) {
1590 /* increase refcount_table size if necessary */
1591 int old_nb_clusters
= *nb_clusters
;
1592 *nb_clusters
= (new_offset
>> s
->cluster_bits
) + 1;
1593 *refcount_table
= g_renew(uint16_t, *refcount_table
,
1595 memset(&(*refcount_table
)[old_nb_clusters
], 0,
1596 (*nb_clusters
- old_nb_clusters
) *
1597 sizeof(**refcount_table
));
1599 (*refcount_table
)[cluster
]--;
1600 inc_refcounts(bs
, res
, *refcount_table
, *nb_clusters
,
1601 new_offset
, s
->cluster_size
);
1603 res
->corruptions_fixed
++;
1615 * Calculates an in-memory refcount table.
1617 static int calculate_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1618 BdrvCheckMode fix
, uint16_t **refcount_table
,
1619 int64_t *nb_clusters
)
1621 BDRVQcowState
*s
= bs
->opaque
;
1626 *refcount_table
= g_try_new0(uint16_t, *nb_clusters
);
1627 if (*nb_clusters
&& *refcount_table
== NULL
) {
1628 res
->check_errors
++;
1633 inc_refcounts(bs
, res
, *refcount_table
, *nb_clusters
,
1634 0, s
->cluster_size
);
1636 /* current L1 table */
1637 ret
= check_refcounts_l1(bs
, res
, *refcount_table
, *nb_clusters
,
1638 s
->l1_table_offset
, s
->l1_size
, CHECK_FRAG_INFO
);
1644 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
1645 sn
= s
->snapshots
+ i
;
1646 ret
= check_refcounts_l1(bs
, res
, *refcount_table
, *nb_clusters
,
1647 sn
->l1_table_offset
, sn
->l1_size
, 0);
1652 inc_refcounts(bs
, res
, *refcount_table
, *nb_clusters
,
1653 s
->snapshots_offset
, s
->snapshots_size
);
1656 inc_refcounts(bs
, res
, *refcount_table
, *nb_clusters
,
1657 s
->refcount_table_offset
,
1658 s
->refcount_table_size
* sizeof(uint64_t));
1660 return check_refblocks(bs
, res
, fix
, refcount_table
, nb_clusters
);
1664 * Compares the actual reference count for each cluster in the image against the
1665 * refcount as reported by the refcount structures on-disk.
1667 static void compare_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1668 BdrvCheckMode fix
, int64_t *highest_cluster
,
1669 uint16_t *refcount_table
, int64_t nb_clusters
)
1671 BDRVQcowState
*s
= bs
->opaque
;
1673 int refcount1
, refcount2
, ret
;
1675 for (i
= 0, *highest_cluster
= 0; i
< nb_clusters
; i
++) {
1676 refcount1
= get_refcount(bs
, i
);
1677 if (refcount1
< 0) {
1678 fprintf(stderr
, "Can't get refcount for cluster %" PRId64
": %s\n",
1679 i
, strerror(-refcount1
));
1680 res
->check_errors
++;
1684 refcount2
= refcount_table
[i
];
1686 if (refcount1
> 0 || refcount2
> 0) {
1687 *highest_cluster
= i
;
1690 if (refcount1
!= refcount2
) {
1691 /* Check if we're allowed to fix the mismatch */
1692 int *num_fixed
= NULL
;
1693 if (refcount1
> refcount2
&& (fix
& BDRV_FIX_LEAKS
)) {
1694 num_fixed
= &res
->leaks_fixed
;
1695 } else if (refcount1
< refcount2
&& (fix
& BDRV_FIX_ERRORS
)) {
1696 num_fixed
= &res
->corruptions_fixed
;
1699 fprintf(stderr
, "%s cluster %" PRId64
" refcount=%d reference=%d\n",
1700 num_fixed
!= NULL
? "Repairing" :
1701 refcount1
< refcount2
? "ERROR" :
1703 i
, refcount1
, refcount2
);
1706 ret
= update_refcount(bs
, i
<< s
->cluster_bits
, 1,
1707 refcount2
- refcount1
,
1708 QCOW2_DISCARD_ALWAYS
);
1715 /* And if we couldn't, print an error */
1716 if (refcount1
< refcount2
) {
1726 * Checks an image for refcount consistency.
1728 * Returns 0 if no errors are found, the number of errors in case the image is
1729 * detected as corrupted, and -errno when an internal error occurred.
1731 int qcow2_check_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1734 BDRVQcowState
*s
= bs
->opaque
;
1735 int64_t size
, highest_cluster
, nb_clusters
;
1736 uint16_t *refcount_table
;
1739 size
= bdrv_getlength(bs
->file
);
1741 res
->check_errors
++;
1745 nb_clusters
= size_to_clusters(s
, size
);
1746 if (nb_clusters
> INT_MAX
) {
1747 res
->check_errors
++;
1751 res
->bfi
.total_clusters
=
1752 size_to_clusters(s
, bs
->total_sectors
* BDRV_SECTOR_SIZE
);
1754 ret
= calculate_refcounts(bs
, res
, fix
, &refcount_table
, &nb_clusters
);
1759 compare_refcounts(bs
, res
, fix
, &highest_cluster
, refcount_table
,
1762 /* check OFLAG_COPIED */
1763 ret
= check_oflag_copied(bs
, res
, fix
);
1768 res
->image_end_offset
= (highest_cluster
+ 1) * s
->cluster_size
;
1772 g_free(refcount_table
);
1777 #define overlaps_with(ofs, sz) \
1778 ranges_overlap(offset, size, ofs, sz)
1781 * Checks if the given offset into the image file is actually free to use by
1782 * looking for overlaps with important metadata sections (L1/L2 tables etc.),
1783 * i.e. a sanity check without relying on the refcount tables.
1785 * The ign parameter specifies what checks not to perform (being a bitmask of
1786 * QCow2MetadataOverlap values), i.e., what sections to ignore.
1789 * - 0 if writing to this offset will not affect the mentioned metadata
1790 * - a positive QCow2MetadataOverlap value indicating one overlapping section
1791 * - a negative value (-errno) indicating an error while performing a check,
1792 * e.g. when bdrv_read failed on QCOW2_OL_INACTIVE_L2
1794 int qcow2_check_metadata_overlap(BlockDriverState
*bs
, int ign
, int64_t offset
,
1797 BDRVQcowState
*s
= bs
->opaque
;
1798 int chk
= s
->overlap_check
& ~ign
;
1805 if (chk
& QCOW2_OL_MAIN_HEADER
) {
1806 if (offset
< s
->cluster_size
) {
1807 return QCOW2_OL_MAIN_HEADER
;
1811 /* align range to test to cluster boundaries */
1812 size
= align_offset(offset_into_cluster(s
, offset
) + size
, s
->cluster_size
);
1813 offset
= start_of_cluster(s
, offset
);
1815 if ((chk
& QCOW2_OL_ACTIVE_L1
) && s
->l1_size
) {
1816 if (overlaps_with(s
->l1_table_offset
, s
->l1_size
* sizeof(uint64_t))) {
1817 return QCOW2_OL_ACTIVE_L1
;
1821 if ((chk
& QCOW2_OL_REFCOUNT_TABLE
) && s
->refcount_table_size
) {
1822 if (overlaps_with(s
->refcount_table_offset
,
1823 s
->refcount_table_size
* sizeof(uint64_t))) {
1824 return QCOW2_OL_REFCOUNT_TABLE
;
1828 if ((chk
& QCOW2_OL_SNAPSHOT_TABLE
) && s
->snapshots_size
) {
1829 if (overlaps_with(s
->snapshots_offset
, s
->snapshots_size
)) {
1830 return QCOW2_OL_SNAPSHOT_TABLE
;
1834 if ((chk
& QCOW2_OL_INACTIVE_L1
) && s
->snapshots
) {
1835 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
1836 if (s
->snapshots
[i
].l1_size
&&
1837 overlaps_with(s
->snapshots
[i
].l1_table_offset
,
1838 s
->snapshots
[i
].l1_size
* sizeof(uint64_t))) {
1839 return QCOW2_OL_INACTIVE_L1
;
1844 if ((chk
& QCOW2_OL_ACTIVE_L2
) && s
->l1_table
) {
1845 for (i
= 0; i
< s
->l1_size
; i
++) {
1846 if ((s
->l1_table
[i
] & L1E_OFFSET_MASK
) &&
1847 overlaps_with(s
->l1_table
[i
] & L1E_OFFSET_MASK
,
1849 return QCOW2_OL_ACTIVE_L2
;
1854 if ((chk
& QCOW2_OL_REFCOUNT_BLOCK
) && s
->refcount_table
) {
1855 for (i
= 0; i
< s
->refcount_table_size
; i
++) {
1856 if ((s
->refcount_table
[i
] & REFT_OFFSET_MASK
) &&
1857 overlaps_with(s
->refcount_table
[i
] & REFT_OFFSET_MASK
,
1859 return QCOW2_OL_REFCOUNT_BLOCK
;
1864 if ((chk
& QCOW2_OL_INACTIVE_L2
) && s
->snapshots
) {
1865 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
1866 uint64_t l1_ofs
= s
->snapshots
[i
].l1_table_offset
;
1867 uint32_t l1_sz
= s
->snapshots
[i
].l1_size
;
1868 uint64_t l1_sz2
= l1_sz
* sizeof(uint64_t);
1869 uint64_t *l1
= g_try_malloc(l1_sz2
);
1872 if (l1_sz2
&& l1
== NULL
) {
1876 ret
= bdrv_pread(bs
->file
, l1_ofs
, l1
, l1_sz2
);
1882 for (j
= 0; j
< l1_sz
; j
++) {
1883 uint64_t l2_ofs
= be64_to_cpu(l1
[j
]) & L1E_OFFSET_MASK
;
1884 if (l2_ofs
&& overlaps_with(l2_ofs
, s
->cluster_size
)) {
1886 return QCOW2_OL_INACTIVE_L2
;
1897 static const char *metadata_ol_names
[] = {
1898 [QCOW2_OL_MAIN_HEADER_BITNR
] = "qcow2_header",
1899 [QCOW2_OL_ACTIVE_L1_BITNR
] = "active L1 table",
1900 [QCOW2_OL_ACTIVE_L2_BITNR
] = "active L2 table",
1901 [QCOW2_OL_REFCOUNT_TABLE_BITNR
] = "refcount table",
1902 [QCOW2_OL_REFCOUNT_BLOCK_BITNR
] = "refcount block",
1903 [QCOW2_OL_SNAPSHOT_TABLE_BITNR
] = "snapshot table",
1904 [QCOW2_OL_INACTIVE_L1_BITNR
] = "inactive L1 table",
1905 [QCOW2_OL_INACTIVE_L2_BITNR
] = "inactive L2 table",
1909 * First performs a check for metadata overlaps (through
1910 * qcow2_check_metadata_overlap); if that fails with a negative value (error
1911 * while performing a check), that value is returned. If an impending overlap
1912 * is detected, the BDS will be made unusable, the qcow2 file marked corrupt
1913 * and -EIO returned.
1915 * Returns 0 if there were neither overlaps nor errors while checking for
1916 * overlaps; or a negative value (-errno) on error.
1918 int qcow2_pre_write_overlap_check(BlockDriverState
*bs
, int ign
, int64_t offset
,
1921 int ret
= qcow2_check_metadata_overlap(bs
, ign
, offset
, size
);
1925 } else if (ret
> 0) {
1926 int metadata_ol_bitnr
= ffs(ret
) - 1;
1927 assert(metadata_ol_bitnr
< QCOW2_OL_MAX_BITNR
);
1929 qcow2_signal_corruption(bs
, true, offset
, size
, "Preventing invalid "
1930 "write on metadata (overlaps with %s)",
1931 metadata_ol_names
[metadata_ol_bitnr
]);