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"
29 #include "qapi/qmp/types.h"
30 #include "qapi-event.h"
32 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, uint64_t size
);
33 static int QEMU_WARN_UNUSED_RESULT
update_refcount(BlockDriverState
*bs
,
34 int64_t offset
, int64_t length
,
35 int addend
, enum qcow2_discard_type type
);
38 /*********************************************************/
39 /* refcount handling */
41 int qcow2_refcount_init(BlockDriverState
*bs
)
43 BDRVQcowState
*s
= bs
->opaque
;
44 unsigned int refcount_table_size2
, i
;
47 assert(s
->refcount_table_size
<= INT_MAX
/ sizeof(uint64_t));
48 refcount_table_size2
= s
->refcount_table_size
* sizeof(uint64_t);
49 s
->refcount_table
= g_malloc(refcount_table_size2
);
50 if (s
->refcount_table_size
> 0) {
51 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_LOAD
);
52 ret
= bdrv_pread(bs
->file
, s
->refcount_table_offset
,
53 s
->refcount_table
, refcount_table_size2
);
54 if (ret
!= refcount_table_size2
)
56 for(i
= 0; i
< s
->refcount_table_size
; i
++)
57 be64_to_cpus(&s
->refcount_table
[i
]);
64 void qcow2_refcount_close(BlockDriverState
*bs
)
66 BDRVQcowState
*s
= bs
->opaque
;
67 g_free(s
->refcount_table
);
71 static int load_refcount_block(BlockDriverState
*bs
,
72 int64_t refcount_block_offset
,
73 void **refcount_block
)
75 BDRVQcowState
*s
= bs
->opaque
;
78 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_LOAD
);
79 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refcount_block_offset
,
86 * Returns the refcount of the cluster given by its index. Any non-negative
87 * return value is the refcount of the cluster, negative values are -errno
88 * and indicate an error.
90 static int get_refcount(BlockDriverState
*bs
, int64_t cluster_index
)
92 BDRVQcowState
*s
= bs
->opaque
;
93 uint64_t refcount_table_index
, block_index
;
94 int64_t refcount_block_offset
;
96 uint16_t *refcount_block
;
99 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
100 if (refcount_table_index
>= s
->refcount_table_size
)
102 refcount_block_offset
=
103 s
->refcount_table
[refcount_table_index
] & REFT_OFFSET_MASK
;
104 if (!refcount_block_offset
)
107 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refcount_block_offset
,
108 (void**) &refcount_block
);
113 block_index
= cluster_index
&
114 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
115 refcount
= be16_to_cpu(refcount_block
[block_index
]);
117 ret
= qcow2_cache_put(bs
, s
->refcount_block_cache
,
118 (void**) &refcount_block
);
127 * Rounds the refcount table size up to avoid growing the table for each single
128 * refcount block that is allocated.
130 static unsigned int next_refcount_table_size(BDRVQcowState
*s
,
131 unsigned int min_size
)
133 unsigned int min_clusters
= (min_size
>> (s
->cluster_bits
- 3)) + 1;
134 unsigned int refcount_table_clusters
=
135 MAX(1, s
->refcount_table_size
>> (s
->cluster_bits
- 3));
137 while (min_clusters
> refcount_table_clusters
) {
138 refcount_table_clusters
= (refcount_table_clusters
* 3 + 1) / 2;
141 return refcount_table_clusters
<< (s
->cluster_bits
- 3);
145 /* Checks if two offsets are described by the same refcount block */
146 static int in_same_refcount_block(BDRVQcowState
*s
, uint64_t offset_a
,
149 uint64_t block_a
= offset_a
>> (2 * s
->cluster_bits
- REFCOUNT_SHIFT
);
150 uint64_t block_b
= offset_b
>> (2 * s
->cluster_bits
- REFCOUNT_SHIFT
);
152 return (block_a
== block_b
);
156 * Loads a refcount block. If it doesn't exist yet, it is allocated first
157 * (including growing the refcount table if needed).
159 * Returns 0 on success or -errno in error case
161 static int alloc_refcount_block(BlockDriverState
*bs
,
162 int64_t cluster_index
, uint16_t **refcount_block
)
164 BDRVQcowState
*s
= bs
->opaque
;
165 unsigned int refcount_table_index
;
168 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC
);
170 /* Find the refcount block for the given cluster */
171 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
173 if (refcount_table_index
< s
->refcount_table_size
) {
175 uint64_t refcount_block_offset
=
176 s
->refcount_table
[refcount_table_index
] & REFT_OFFSET_MASK
;
178 /* If it's already there, we're done */
179 if (refcount_block_offset
) {
180 return load_refcount_block(bs
, refcount_block_offset
,
181 (void**) refcount_block
);
186 * If we came here, we need to allocate something. Something is at least
187 * a cluster for the new refcount block. It may also include a new refcount
188 * table if the old refcount table is too small.
190 * Note that allocating clusters here needs some special care:
192 * - We can't use the normal qcow2_alloc_clusters(), it would try to
193 * increase the refcount and very likely we would end up with an endless
194 * recursion. Instead we must place the refcount blocks in a way that
195 * they can describe them themselves.
197 * - We need to consider that at this point we are inside update_refcounts
198 * and potentially doing an initial refcount increase. This means that
199 * some clusters have already been allocated by the caller, but their
200 * refcount isn't accurate yet. If we allocate clusters for metadata, we
201 * need to return -EAGAIN to signal the caller that it needs to restart
202 * the search for free clusters.
204 * - alloc_clusters_noref and qcow2_free_clusters may load a different
205 * refcount block into the cache
208 *refcount_block
= NULL
;
210 /* We write to the refcount table, so we might depend on L2 tables */
211 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
216 /* Allocate the refcount block itself and mark it as used */
217 int64_t new_block
= alloc_clusters_noref(bs
, s
->cluster_size
);
223 fprintf(stderr
, "qcow2: Allocate refcount block %d for %" PRIx64
225 refcount_table_index
, cluster_index
<< s
->cluster_bits
, new_block
);
228 if (in_same_refcount_block(s
, new_block
, cluster_index
<< s
->cluster_bits
)) {
229 /* Zero the new refcount block before updating it */
230 ret
= qcow2_cache_get_empty(bs
, s
->refcount_block_cache
, new_block
,
231 (void**) refcount_block
);
236 memset(*refcount_block
, 0, s
->cluster_size
);
238 /* The block describes itself, need to update the cache */
239 int block_index
= (new_block
>> s
->cluster_bits
) &
240 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
241 (*refcount_block
)[block_index
] = cpu_to_be16(1);
243 /* Described somewhere else. This can recurse at most twice before we
244 * arrive at a block that describes itself. */
245 ret
= update_refcount(bs
, new_block
, s
->cluster_size
, 1,
246 QCOW2_DISCARD_NEVER
);
251 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
256 /* Initialize the new refcount block only after updating its refcount,
257 * update_refcount uses the refcount cache itself */
258 ret
= qcow2_cache_get_empty(bs
, s
->refcount_block_cache
, new_block
,
259 (void**) refcount_block
);
264 memset(*refcount_block
, 0, s
->cluster_size
);
267 /* Now the new refcount block needs to be written to disk */
268 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE
);
269 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
, *refcount_block
);
270 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
275 /* If the refcount table is big enough, just hook the block up there */
276 if (refcount_table_index
< s
->refcount_table_size
) {
277 uint64_t data64
= cpu_to_be64(new_block
);
278 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_HOOKUP
);
279 ret
= bdrv_pwrite_sync(bs
->file
,
280 s
->refcount_table_offset
+ refcount_table_index
* sizeof(uint64_t),
281 &data64
, sizeof(data64
));
286 s
->refcount_table
[refcount_table_index
] = new_block
;
288 /* The new refcount block may be where the caller intended to put its
289 * data, so let it restart the search. */
293 ret
= qcow2_cache_put(bs
, s
->refcount_block_cache
, (void**) refcount_block
);
299 * If we come here, we need to grow the refcount table. Again, a new
300 * refcount table needs some space and we can't simply allocate to avoid
303 * Therefore let's grab new refcount blocks at the end of the image, which
304 * will describe themselves and the new refcount table. This way we can
305 * reference them only in the new table and do the switch to the new
306 * refcount table at once without producing an inconsistent state in
309 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_GROW
);
311 /* Calculate the number of refcount blocks needed so far */
312 uint64_t refcount_block_clusters
= 1 << (s
->cluster_bits
- REFCOUNT_SHIFT
);
313 uint64_t blocks_used
= DIV_ROUND_UP(cluster_index
, refcount_block_clusters
);
315 if (blocks_used
> QCOW_MAX_REFTABLE_SIZE
/ sizeof(uint64_t)) {
319 /* And now we need at least one block more for the new metadata */
320 uint64_t table_size
= next_refcount_table_size(s
, blocks_used
+ 1);
321 uint64_t last_table_size
;
322 uint64_t blocks_clusters
;
324 uint64_t table_clusters
=
325 size_to_clusters(s
, table_size
* sizeof(uint64_t));
326 blocks_clusters
= 1 +
327 ((table_clusters
+ refcount_block_clusters
- 1)
328 / refcount_block_clusters
);
329 uint64_t meta_clusters
= table_clusters
+ blocks_clusters
;
331 last_table_size
= table_size
;
332 table_size
= next_refcount_table_size(s
, blocks_used
+
333 ((meta_clusters
+ refcount_block_clusters
- 1)
334 / refcount_block_clusters
));
336 } while (last_table_size
!= table_size
);
339 fprintf(stderr
, "qcow2: Grow refcount table %" PRId32
" => %" PRId64
"\n",
340 s
->refcount_table_size
, table_size
);
343 /* Create the new refcount table and blocks */
344 uint64_t meta_offset
= (blocks_used
* refcount_block_clusters
) *
346 uint64_t table_offset
= meta_offset
+ blocks_clusters
* s
->cluster_size
;
347 uint16_t *new_blocks
= g_malloc0(blocks_clusters
* s
->cluster_size
);
348 uint64_t *new_table
= g_malloc0(table_size
* sizeof(uint64_t));
350 /* Fill the new refcount table */
351 memcpy(new_table
, s
->refcount_table
,
352 s
->refcount_table_size
* sizeof(uint64_t));
353 new_table
[refcount_table_index
] = new_block
;
356 for (i
= 0; i
< blocks_clusters
; i
++) {
357 new_table
[blocks_used
+ i
] = meta_offset
+ (i
* s
->cluster_size
);
360 /* Fill the refcount blocks */
361 uint64_t table_clusters
= size_to_clusters(s
, table_size
* sizeof(uint64_t));
363 for (i
= 0; i
< table_clusters
+ blocks_clusters
; i
++) {
364 new_blocks
[block
++] = cpu_to_be16(1);
367 /* Write refcount blocks to disk */
368 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS
);
369 ret
= bdrv_pwrite_sync(bs
->file
, meta_offset
, new_blocks
,
370 blocks_clusters
* s
->cluster_size
);
376 /* Write refcount table to disk */
377 for(i
= 0; i
< table_size
; i
++) {
378 cpu_to_be64s(&new_table
[i
]);
381 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE
);
382 ret
= bdrv_pwrite_sync(bs
->file
, table_offset
, new_table
,
383 table_size
* sizeof(uint64_t));
388 for(i
= 0; i
< table_size
; i
++) {
389 be64_to_cpus(&new_table
[i
]);
392 /* Hook up the new refcount table in the qcow2 header */
394 cpu_to_be64w((uint64_t*)data
, table_offset
);
395 cpu_to_be32w((uint32_t*)(data
+ 8), table_clusters
);
396 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE
);
397 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, refcount_table_offset
),
403 /* And switch it in memory */
404 uint64_t old_table_offset
= s
->refcount_table_offset
;
405 uint64_t old_table_size
= s
->refcount_table_size
;
407 g_free(s
->refcount_table
);
408 s
->refcount_table
= new_table
;
409 s
->refcount_table_size
= table_size
;
410 s
->refcount_table_offset
= table_offset
;
412 /* Free old table. */
413 qcow2_free_clusters(bs
, old_table_offset
, old_table_size
* sizeof(uint64_t),
414 QCOW2_DISCARD_OTHER
);
416 ret
= load_refcount_block(bs
, new_block
, (void**) refcount_block
);
421 /* If we were trying to do the initial refcount update for some cluster
422 * allocation, we might have used the same clusters to store newly
423 * allocated metadata. Make the caller search some new space. */
429 if (*refcount_block
!= NULL
) {
430 qcow2_cache_put(bs
, s
->refcount_block_cache
, (void**) refcount_block
);
435 void qcow2_process_discards(BlockDriverState
*bs
, int ret
)
437 BDRVQcowState
*s
= bs
->opaque
;
438 Qcow2DiscardRegion
*d
, *next
;
440 QTAILQ_FOREACH_SAFE(d
, &s
->discards
, next
, next
) {
441 QTAILQ_REMOVE(&s
->discards
, d
, next
);
443 /* Discard is optional, ignore the return value */
445 bdrv_discard(bs
->file
,
446 d
->offset
>> BDRV_SECTOR_BITS
,
447 d
->bytes
>> BDRV_SECTOR_BITS
);
454 static void update_refcount_discard(BlockDriverState
*bs
,
455 uint64_t offset
, uint64_t length
)
457 BDRVQcowState
*s
= bs
->opaque
;
458 Qcow2DiscardRegion
*d
, *p
, *next
;
460 QTAILQ_FOREACH(d
, &s
->discards
, next
) {
461 uint64_t new_start
= MIN(offset
, d
->offset
);
462 uint64_t new_end
= MAX(offset
+ length
, d
->offset
+ d
->bytes
);
464 if (new_end
- new_start
<= length
+ d
->bytes
) {
465 /* There can't be any overlap, areas ending up here have no
466 * references any more and therefore shouldn't get freed another
468 assert(d
->bytes
+ length
== new_end
- new_start
);
469 d
->offset
= new_start
;
470 d
->bytes
= new_end
- new_start
;
475 d
= g_malloc(sizeof(*d
));
476 *d
= (Qcow2DiscardRegion
) {
481 QTAILQ_INSERT_TAIL(&s
->discards
, d
, next
);
484 /* Merge discard requests if they are adjacent now */
485 QTAILQ_FOREACH_SAFE(p
, &s
->discards
, next
, next
) {
487 || p
->offset
> d
->offset
+ d
->bytes
488 || d
->offset
> p
->offset
+ p
->bytes
)
493 /* Still no overlap possible */
494 assert(p
->offset
== d
->offset
+ d
->bytes
495 || d
->offset
== p
->offset
+ p
->bytes
);
497 QTAILQ_REMOVE(&s
->discards
, p
, next
);
498 d
->offset
= MIN(d
->offset
, p
->offset
);
499 d
->bytes
+= p
->bytes
;
503 /* XXX: cache several refcount block clusters ? */
504 static int QEMU_WARN_UNUSED_RESULT
update_refcount(BlockDriverState
*bs
,
505 int64_t offset
, int64_t length
, int addend
, enum qcow2_discard_type type
)
507 BDRVQcowState
*s
= bs
->opaque
;
508 int64_t start
, last
, cluster_offset
;
509 uint16_t *refcount_block
= NULL
;
510 int64_t old_table_index
= -1;
514 fprintf(stderr
, "update_refcount: offset=%" PRId64
" size=%" PRId64
" addend=%d\n",
515 offset
, length
, addend
);
519 } else if (length
== 0) {
524 qcow2_cache_set_dependency(bs
, s
->refcount_block_cache
,
528 start
= start_of_cluster(s
, offset
);
529 last
= start_of_cluster(s
, offset
+ length
- 1);
530 for(cluster_offset
= start
; cluster_offset
<= last
;
531 cluster_offset
+= s
->cluster_size
)
533 int block_index
, refcount
;
534 int64_t cluster_index
= cluster_offset
>> s
->cluster_bits
;
535 int64_t table_index
=
536 cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
538 /* Load the refcount block and allocate it if needed */
539 if (table_index
!= old_table_index
) {
540 if (refcount_block
) {
541 ret
= qcow2_cache_put(bs
, s
->refcount_block_cache
,
542 (void**) &refcount_block
);
548 ret
= alloc_refcount_block(bs
, cluster_index
, &refcount_block
);
553 old_table_index
= table_index
;
555 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
, refcount_block
);
557 /* we can update the count and save it */
558 block_index
= cluster_index
&
559 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
561 refcount
= be16_to_cpu(refcount_block
[block_index
]);
563 if (refcount
< 0 || refcount
> 0xffff) {
567 if (refcount
== 0 && cluster_index
< s
->free_cluster_index
) {
568 s
->free_cluster_index
= cluster_index
;
570 refcount_block
[block_index
] = cpu_to_be16(refcount
);
572 if (refcount
== 0 && s
->discard_passthrough
[type
]) {
573 update_refcount_discard(bs
, cluster_offset
, s
->cluster_size
);
579 if (!s
->cache_discards
) {
580 qcow2_process_discards(bs
, ret
);
583 /* Write last changed block to disk */
584 if (refcount_block
) {
586 wret
= qcow2_cache_put(bs
, s
->refcount_block_cache
,
587 (void**) &refcount_block
);
589 return ret
< 0 ? ret
: wret
;
594 * Try do undo any updates if an error is returned (This may succeed in
595 * some cases like ENOSPC for allocating a new refcount block)
599 dummy
= update_refcount(bs
, offset
, cluster_offset
- offset
, -addend
,
600 QCOW2_DISCARD_NEVER
);
608 * Increases or decreases the refcount of a given cluster by one.
609 * addend must be 1 or -1.
611 * If the return value is non-negative, it is the new refcount of the cluster.
612 * If it is negative, it is -errno and indicates an error.
614 int qcow2_update_cluster_refcount(BlockDriverState
*bs
,
615 int64_t cluster_index
,
617 enum qcow2_discard_type type
)
619 BDRVQcowState
*s
= bs
->opaque
;
622 ret
= update_refcount(bs
, cluster_index
<< s
->cluster_bits
, 1, addend
,
628 return get_refcount(bs
, cluster_index
);
633 /*********************************************************/
634 /* cluster allocation functions */
638 /* return < 0 if error */
639 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, uint64_t size
)
641 BDRVQcowState
*s
= bs
->opaque
;
642 uint64_t i
, nb_clusters
;
645 nb_clusters
= size_to_clusters(s
, size
);
647 for(i
= 0; i
< nb_clusters
; i
++) {
648 uint64_t next_cluster_index
= s
->free_cluster_index
++;
649 refcount
= get_refcount(bs
, next_cluster_index
);
653 } else if (refcount
!= 0) {
658 /* Make sure that all offsets in the "allocated" range are representable
660 if (s
->free_cluster_index
> 0 &&
661 s
->free_cluster_index
- 1 > (INT64_MAX
>> s
->cluster_bits
))
667 fprintf(stderr
, "alloc_clusters: size=%" PRId64
" -> %" PRId64
"\n",
669 (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
);
671 return (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
;
674 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, uint64_t size
)
679 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC
);
681 offset
= alloc_clusters_noref(bs
, size
);
686 ret
= update_refcount(bs
, offset
, size
, 1, QCOW2_DISCARD_NEVER
);
687 } while (ret
== -EAGAIN
);
696 int qcow2_alloc_clusters_at(BlockDriverState
*bs
, uint64_t offset
,
699 BDRVQcowState
*s
= bs
->opaque
;
700 uint64_t cluster_index
;
704 assert(nb_clusters
>= 0);
705 if (nb_clusters
== 0) {
710 /* Check how many clusters there are free */
711 cluster_index
= offset
>> s
->cluster_bits
;
712 for(i
= 0; i
< nb_clusters
; i
++) {
713 refcount
= get_refcount(bs
, cluster_index
++);
717 } else if (refcount
!= 0) {
722 /* And then allocate them */
723 ret
= update_refcount(bs
, offset
, i
<< s
->cluster_bits
, 1,
724 QCOW2_DISCARD_NEVER
);
725 } while (ret
== -EAGAIN
);
734 /* only used to allocate compressed sectors. We try to allocate
735 contiguous sectors. size must be <= cluster_size */
736 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
)
738 BDRVQcowState
*s
= bs
->opaque
;
739 int64_t offset
, cluster_offset
;
742 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC_BYTES
);
743 assert(size
> 0 && size
<= s
->cluster_size
);
744 if (s
->free_byte_offset
== 0) {
745 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
749 s
->free_byte_offset
= offset
;
752 free_in_cluster
= s
->cluster_size
-
753 offset_into_cluster(s
, s
->free_byte_offset
);
754 if (size
<= free_in_cluster
) {
755 /* enough space in current cluster */
756 offset
= s
->free_byte_offset
;
757 s
->free_byte_offset
+= size
;
758 free_in_cluster
-= size
;
759 if (free_in_cluster
== 0)
760 s
->free_byte_offset
= 0;
761 if (offset_into_cluster(s
, offset
) != 0)
762 qcow2_update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1,
763 QCOW2_DISCARD_NEVER
);
765 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
769 cluster_offset
= start_of_cluster(s
, s
->free_byte_offset
);
770 if ((cluster_offset
+ s
->cluster_size
) == offset
) {
771 /* we are lucky: contiguous data */
772 offset
= s
->free_byte_offset
;
773 qcow2_update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1,
774 QCOW2_DISCARD_NEVER
);
775 s
->free_byte_offset
+= size
;
777 s
->free_byte_offset
= offset
;
782 /* The cluster refcount was incremented, either by qcow2_alloc_clusters()
783 * or explicitly by qcow2_update_cluster_refcount(). Refcount blocks must
784 * be flushed before the caller's L2 table updates.
786 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
, s
->refcount_block_cache
);
790 void qcow2_free_clusters(BlockDriverState
*bs
,
791 int64_t offset
, int64_t size
,
792 enum qcow2_discard_type type
)
796 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_FREE
);
797 ret
= update_refcount(bs
, offset
, size
, -1, type
);
799 fprintf(stderr
, "qcow2_free_clusters failed: %s\n", strerror(-ret
));
800 /* TODO Remember the clusters to free them later and avoid leaking */
805 * Free a cluster using its L2 entry (handles clusters of all types, e.g.
806 * normal cluster, compressed cluster, etc.)
808 void qcow2_free_any_clusters(BlockDriverState
*bs
, uint64_t l2_entry
,
809 int nb_clusters
, enum qcow2_discard_type type
)
811 BDRVQcowState
*s
= bs
->opaque
;
813 switch (qcow2_get_cluster_type(l2_entry
)) {
814 case QCOW2_CLUSTER_COMPRESSED
:
817 nb_csectors
= ((l2_entry
>> s
->csize_shift
) &
819 qcow2_free_clusters(bs
,
820 (l2_entry
& s
->cluster_offset_mask
) & ~511,
821 nb_csectors
* 512, type
);
824 case QCOW2_CLUSTER_NORMAL
:
825 case QCOW2_CLUSTER_ZERO
:
826 if (l2_entry
& L2E_OFFSET_MASK
) {
827 qcow2_free_clusters(bs
, l2_entry
& L2E_OFFSET_MASK
,
828 nb_clusters
<< s
->cluster_bits
, type
);
831 case QCOW2_CLUSTER_UNALLOCATED
:
840 /*********************************************************/
841 /* snapshots and image creation */
845 /* update the refcounts of snapshots and the copied flag */
846 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
847 int64_t l1_table_offset
, int l1_size
, int addend
)
849 BDRVQcowState
*s
= bs
->opaque
;
850 uint64_t *l1_table
, *l2_table
, l2_offset
, offset
, l1_size2
, l1_allocated
;
851 int64_t old_offset
, old_l2_offset
;
852 int i
, j
, l1_modified
= 0, nb_csectors
, refcount
;
857 l1_size2
= l1_size
* sizeof(uint64_t);
859 s
->cache_discards
= true;
861 /* WARNING: qcow2_snapshot_goto relies on this function not using the
862 * l1_table_offset when it is the current s->l1_table_offset! Be careful
863 * when changing this! */
864 if (l1_table_offset
!= s
->l1_table_offset
) {
865 l1_table
= g_malloc0(align_offset(l1_size2
, 512));
868 ret
= bdrv_pread(bs
->file
, l1_table_offset
, l1_table
, l1_size2
);
873 for(i
= 0;i
< l1_size
; i
++)
874 be64_to_cpus(&l1_table
[i
]);
876 assert(l1_size
== s
->l1_size
);
877 l1_table
= s
->l1_table
;
881 for(i
= 0; i
< l1_size
; i
++) {
882 l2_offset
= l1_table
[i
];
884 old_l2_offset
= l2_offset
;
885 l2_offset
&= L1E_OFFSET_MASK
;
887 ret
= qcow2_cache_get(bs
, s
->l2_table_cache
, l2_offset
,
893 for(j
= 0; j
< s
->l2_size
; j
++) {
894 uint64_t cluster_index
;
896 offset
= be64_to_cpu(l2_table
[j
]);
898 offset
&= ~QCOW_OFLAG_COPIED
;
900 switch (qcow2_get_cluster_type(offset
)) {
901 case QCOW2_CLUSTER_COMPRESSED
:
902 nb_csectors
= ((offset
>> s
->csize_shift
) &
905 ret
= update_refcount(bs
,
906 (offset
& s
->cluster_offset_mask
) & ~511,
907 nb_csectors
* 512, addend
,
908 QCOW2_DISCARD_SNAPSHOT
);
913 /* compressed clusters are never modified */
917 case QCOW2_CLUSTER_NORMAL
:
918 case QCOW2_CLUSTER_ZERO
:
919 cluster_index
= (offset
& L2E_OFFSET_MASK
) >> s
->cluster_bits
;
920 if (!cluster_index
) {
926 refcount
= qcow2_update_cluster_refcount(bs
,
927 cluster_index
, addend
,
928 QCOW2_DISCARD_SNAPSHOT
);
930 refcount
= get_refcount(bs
, cluster_index
);
939 case QCOW2_CLUSTER_UNALLOCATED
:
948 offset
|= QCOW_OFLAG_COPIED
;
950 if (offset
!= old_offset
) {
952 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
,
953 s
->refcount_block_cache
);
955 l2_table
[j
] = cpu_to_be64(offset
);
956 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_table
);
960 ret
= qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
967 refcount
= qcow2_update_cluster_refcount(bs
, l2_offset
>>
968 s
->cluster_bits
, addend
, QCOW2_DISCARD_SNAPSHOT
);
970 refcount
= get_refcount(bs
, l2_offset
>> s
->cluster_bits
);
975 } else if (refcount
== 1) {
976 l2_offset
|= QCOW_OFLAG_COPIED
;
978 if (l2_offset
!= old_l2_offset
) {
979 l1_table
[i
] = l2_offset
;
985 ret
= bdrv_flush(bs
);
988 qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
991 s
->cache_discards
= false;
992 qcow2_process_discards(bs
, ret
);
994 /* Update L1 only if it isn't deleted anyway (addend = -1) */
995 if (ret
== 0 && addend
>= 0 && l1_modified
) {
996 for (i
= 0; i
< l1_size
; i
++) {
997 cpu_to_be64s(&l1_table
[i
]);
1000 ret
= bdrv_pwrite_sync(bs
->file
, l1_table_offset
, l1_table
, l1_size2
);
1002 for (i
= 0; i
< l1_size
; i
++) {
1003 be64_to_cpus(&l1_table
[i
]);
1014 /*********************************************************/
1015 /* refcount checking functions */
1020 * Increases the refcount for a range of clusters in a given refcount table.
1021 * This is used to construct a temporary refcount table out of L1 and L2 tables
1022 * which can be compared the the refcount table saved in the image.
1024 * Modifies the number of errors in res.
1026 static void inc_refcounts(BlockDriverState
*bs
,
1027 BdrvCheckResult
*res
,
1028 uint16_t *refcount_table
,
1029 int refcount_table_size
,
1030 int64_t offset
, int64_t size
)
1032 BDRVQcowState
*s
= bs
->opaque
;
1033 uint64_t start
, last
, cluster_offset
, k
;
1038 start
= start_of_cluster(s
, offset
);
1039 last
= start_of_cluster(s
, offset
+ size
- 1);
1040 for(cluster_offset
= start
; cluster_offset
<= last
;
1041 cluster_offset
+= s
->cluster_size
) {
1042 k
= cluster_offset
>> s
->cluster_bits
;
1043 if (k
>= refcount_table_size
) {
1044 fprintf(stderr
, "Warning: cluster offset=0x%" PRIx64
" is after "
1045 "the end of the image file, can't properly check refcounts.\n",
1047 res
->check_errors
++;
1049 if (++refcount_table
[k
] == 0) {
1050 fprintf(stderr
, "ERROR: overflow cluster offset=0x%" PRIx64
1051 "\n", cluster_offset
);
1058 /* Flags for check_refcounts_l1() and check_refcounts_l2() */
1060 CHECK_FRAG_INFO
= 0x2, /* update BlockFragInfo counters */
1064 * Increases the refcount in the given refcount table for the all clusters
1065 * referenced in the L2 table. While doing so, performs some checks on L2
1068 * Returns the number of errors found by the checks or -errno if an internal
1071 static int check_refcounts_l2(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1072 uint16_t *refcount_table
, int refcount_table_size
, int64_t l2_offset
,
1075 BDRVQcowState
*s
= bs
->opaque
;
1076 uint64_t *l2_table
, l2_entry
;
1077 uint64_t next_contiguous_offset
= 0;
1078 int i
, l2_size
, nb_csectors
;
1080 /* Read L2 table from disk */
1081 l2_size
= s
->l2_size
* sizeof(uint64_t);
1082 l2_table
= g_malloc(l2_size
);
1084 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
, l2_size
) != l2_size
)
1087 /* Do the actual checks */
1088 for(i
= 0; i
< s
->l2_size
; i
++) {
1089 l2_entry
= be64_to_cpu(l2_table
[i
]);
1091 switch (qcow2_get_cluster_type(l2_entry
)) {
1092 case QCOW2_CLUSTER_COMPRESSED
:
1093 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
1094 if (l2_entry
& QCOW_OFLAG_COPIED
) {
1095 fprintf(stderr
, "ERROR: cluster %" PRId64
": "
1096 "copied flag must never be set for compressed "
1097 "clusters\n", l2_entry
>> s
->cluster_bits
);
1098 l2_entry
&= ~QCOW_OFLAG_COPIED
;
1102 /* Mark cluster as used */
1103 nb_csectors
= ((l2_entry
>> s
->csize_shift
) &
1105 l2_entry
&= s
->cluster_offset_mask
;
1106 inc_refcounts(bs
, res
, refcount_table
, refcount_table_size
,
1107 l2_entry
& ~511, nb_csectors
* 512);
1109 if (flags
& CHECK_FRAG_INFO
) {
1110 res
->bfi
.allocated_clusters
++;
1111 res
->bfi
.compressed_clusters
++;
1113 /* Compressed clusters are fragmented by nature. Since they
1114 * take up sub-sector space but we only have sector granularity
1115 * I/O we need to re-read the same sectors even for adjacent
1116 * compressed clusters.
1118 res
->bfi
.fragmented_clusters
++;
1122 case QCOW2_CLUSTER_ZERO
:
1123 if ((l2_entry
& L2E_OFFSET_MASK
) == 0) {
1128 case QCOW2_CLUSTER_NORMAL
:
1130 uint64_t offset
= l2_entry
& L2E_OFFSET_MASK
;
1132 if (flags
& CHECK_FRAG_INFO
) {
1133 res
->bfi
.allocated_clusters
++;
1134 if (next_contiguous_offset
&&
1135 offset
!= next_contiguous_offset
) {
1136 res
->bfi
.fragmented_clusters
++;
1138 next_contiguous_offset
= offset
+ s
->cluster_size
;
1141 /* Mark cluster as used */
1142 inc_refcounts(bs
, res
, refcount_table
,refcount_table_size
,
1143 offset
, s
->cluster_size
);
1145 /* Correct offsets are cluster aligned */
1146 if (offset_into_cluster(s
, offset
)) {
1147 fprintf(stderr
, "ERROR offset=%" PRIx64
": Cluster is not "
1148 "properly aligned; L2 entry corrupted.\n", offset
);
1154 case QCOW2_CLUSTER_UNALLOCATED
:
1166 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l2\n");
1172 * Increases the refcount for the L1 table, its L2 tables and all referenced
1173 * clusters in the given refcount table. While doing so, performs some checks
1174 * on L1 and L2 entries.
1176 * Returns the number of errors found by the checks or -errno if an internal
1179 static int check_refcounts_l1(BlockDriverState
*bs
,
1180 BdrvCheckResult
*res
,
1181 uint16_t *refcount_table
,
1182 int refcount_table_size
,
1183 int64_t l1_table_offset
, int l1_size
,
1186 BDRVQcowState
*s
= bs
->opaque
;
1187 uint64_t *l1_table
, l2_offset
, l1_size2
;
1190 l1_size2
= l1_size
* sizeof(uint64_t);
1192 /* Mark L1 table as used */
1193 inc_refcounts(bs
, res
, refcount_table
, refcount_table_size
,
1194 l1_table_offset
, l1_size2
);
1196 /* Read L1 table entries from disk */
1197 if (l1_size2
== 0) {
1200 l1_table
= g_malloc(l1_size2
);
1201 if (bdrv_pread(bs
->file
, l1_table_offset
,
1202 l1_table
, l1_size2
) != l1_size2
)
1204 for(i
= 0;i
< l1_size
; i
++)
1205 be64_to_cpus(&l1_table
[i
]);
1208 /* Do the actual checks */
1209 for(i
= 0; i
< l1_size
; i
++) {
1210 l2_offset
= l1_table
[i
];
1212 /* Mark L2 table as used */
1213 l2_offset
&= L1E_OFFSET_MASK
;
1214 inc_refcounts(bs
, res
, refcount_table
, refcount_table_size
,
1215 l2_offset
, s
->cluster_size
);
1217 /* L2 tables are cluster aligned */
1218 if (offset_into_cluster(s
, l2_offset
)) {
1219 fprintf(stderr
, "ERROR l2_offset=%" PRIx64
": Table is not "
1220 "cluster aligned; L1 entry corrupted\n", l2_offset
);
1224 /* Process and check L2 entries */
1225 ret
= check_refcounts_l2(bs
, res
, refcount_table
,
1226 refcount_table_size
, l2_offset
, flags
);
1236 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
1237 res
->check_errors
++;
1243 * Checks the OFLAG_COPIED flag for all L1 and L2 entries.
1245 * This function does not print an error message nor does it increment
1246 * check_errors if get_refcount fails (this is because such an error will have
1247 * been already detected and sufficiently signaled by the calling function
1248 * (qcow2_check_refcounts) by the time this function is called).
1250 static int check_oflag_copied(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1253 BDRVQcowState
*s
= bs
->opaque
;
1254 uint64_t *l2_table
= qemu_blockalign(bs
, s
->cluster_size
);
1259 for (i
= 0; i
< s
->l1_size
; i
++) {
1260 uint64_t l1_entry
= s
->l1_table
[i
];
1261 uint64_t l2_offset
= l1_entry
& L1E_OFFSET_MASK
;
1262 bool l2_dirty
= false;
1268 refcount
= get_refcount(bs
, l2_offset
>> s
->cluster_bits
);
1270 /* don't print message nor increment check_errors */
1273 if ((refcount
== 1) != ((l1_entry
& QCOW_OFLAG_COPIED
) != 0)) {
1274 fprintf(stderr
, "%s OFLAG_COPIED L2 cluster: l1_index=%d "
1275 "l1_entry=%" PRIx64
" refcount=%d\n",
1276 fix
& BDRV_FIX_ERRORS
? "Repairing" :
1278 i
, l1_entry
, refcount
);
1279 if (fix
& BDRV_FIX_ERRORS
) {
1280 s
->l1_table
[i
] = refcount
== 1
1281 ? l1_entry
| QCOW_OFLAG_COPIED
1282 : l1_entry
& ~QCOW_OFLAG_COPIED
;
1283 ret
= qcow2_write_l1_entry(bs
, i
);
1285 res
->check_errors
++;
1288 res
->corruptions_fixed
++;
1294 ret
= bdrv_pread(bs
->file
, l2_offset
, l2_table
,
1295 s
->l2_size
* sizeof(uint64_t));
1297 fprintf(stderr
, "ERROR: Could not read L2 table: %s\n",
1299 res
->check_errors
++;
1303 for (j
= 0; j
< s
->l2_size
; j
++) {
1304 uint64_t l2_entry
= be64_to_cpu(l2_table
[j
]);
1305 uint64_t data_offset
= l2_entry
& L2E_OFFSET_MASK
;
1306 int cluster_type
= qcow2_get_cluster_type(l2_entry
);
1308 if ((cluster_type
== QCOW2_CLUSTER_NORMAL
) ||
1309 ((cluster_type
== QCOW2_CLUSTER_ZERO
) && (data_offset
!= 0))) {
1310 refcount
= get_refcount(bs
, data_offset
>> s
->cluster_bits
);
1312 /* don't print message nor increment check_errors */
1315 if ((refcount
== 1) != ((l2_entry
& QCOW_OFLAG_COPIED
) != 0)) {
1316 fprintf(stderr
, "%s OFLAG_COPIED data cluster: "
1317 "l2_entry=%" PRIx64
" refcount=%d\n",
1318 fix
& BDRV_FIX_ERRORS
? "Repairing" :
1320 l2_entry
, refcount
);
1321 if (fix
& BDRV_FIX_ERRORS
) {
1322 l2_table
[j
] = cpu_to_be64(refcount
== 1
1323 ? l2_entry
| QCOW_OFLAG_COPIED
1324 : l2_entry
& ~QCOW_OFLAG_COPIED
);
1326 res
->corruptions_fixed
++;
1335 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_ACTIVE_L2
,
1336 l2_offset
, s
->cluster_size
);
1338 fprintf(stderr
, "ERROR: Could not write L2 table; metadata "
1339 "overlap check failed: %s\n", strerror(-ret
));
1340 res
->check_errors
++;
1344 ret
= bdrv_pwrite(bs
->file
, l2_offset
, l2_table
, s
->cluster_size
);
1346 fprintf(stderr
, "ERROR: Could not write L2 table: %s\n",
1348 res
->check_errors
++;
1357 qemu_vfree(l2_table
);
1362 * Writes one sector of the refcount table to the disk
1364 #define RT_ENTRIES_PER_SECTOR (512 / sizeof(uint64_t))
1365 static int write_reftable_entry(BlockDriverState
*bs
, int rt_index
)
1367 BDRVQcowState
*s
= bs
->opaque
;
1368 uint64_t buf
[RT_ENTRIES_PER_SECTOR
];
1372 rt_start_index
= rt_index
& ~(RT_ENTRIES_PER_SECTOR
- 1);
1373 for (i
= 0; i
< RT_ENTRIES_PER_SECTOR
; i
++) {
1374 buf
[i
] = cpu_to_be64(s
->refcount_table
[rt_start_index
+ i
]);
1377 ret
= qcow2_pre_write_overlap_check(bs
, QCOW2_OL_REFCOUNT_TABLE
,
1378 s
->refcount_table_offset
+ rt_start_index
* sizeof(uint64_t),
1384 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_UPDATE
);
1385 ret
= bdrv_pwrite_sync(bs
->file
, s
->refcount_table_offset
+
1386 rt_start_index
* sizeof(uint64_t), buf
, sizeof(buf
));
1395 * Allocates a new cluster for the given refcount block (represented by its
1396 * offset in the image file) and copies the current content there. This function
1397 * does _not_ decrement the reference count for the currently occupied cluster.
1399 * This function prints an informative message to stderr on error (and returns
1400 * -errno); on success, the offset of the newly allocated cluster is returned.
1402 static int64_t realloc_refcount_block(BlockDriverState
*bs
, int reftable_index
,
1405 BDRVQcowState
*s
= bs
->opaque
;
1406 int64_t new_offset
= 0;
1407 void *refcount_block
= NULL
;
1410 /* allocate new refcount block */
1411 new_offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
1412 if (new_offset
< 0) {
1413 fprintf(stderr
, "Could not allocate new cluster: %s\n",
1414 strerror(-new_offset
));
1419 /* fetch current refcount block content */
1420 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, offset
, &refcount_block
);
1422 fprintf(stderr
, "Could not fetch refcount block: %s\n", strerror(-ret
));
1423 goto fail_free_cluster
;
1426 /* new block has not yet been entered into refcount table, therefore it is
1427 * no refcount block yet (regarding this check) */
1428 ret
= qcow2_pre_write_overlap_check(bs
, 0, new_offset
, s
->cluster_size
);
1430 fprintf(stderr
, "Could not write refcount block; metadata overlap "
1431 "check failed: %s\n", strerror(-ret
));
1432 /* the image will be marked corrupt, so don't even attempt on freeing
1437 /* write to new block */
1438 ret
= bdrv_write(bs
->file
, new_offset
/ BDRV_SECTOR_SIZE
, refcount_block
,
1439 s
->cluster_sectors
);
1441 fprintf(stderr
, "Could not write refcount block: %s\n", strerror(-ret
));
1442 goto fail_free_cluster
;
1445 /* update refcount table */
1446 assert(!offset_into_cluster(s
, new_offset
));
1447 s
->refcount_table
[reftable_index
] = new_offset
;
1448 ret
= write_reftable_entry(bs
, reftable_index
);
1450 fprintf(stderr
, "Could not update refcount table: %s\n",
1452 goto fail_free_cluster
;
1458 qcow2_free_clusters(bs
, new_offset
, s
->cluster_size
, QCOW2_DISCARD_OTHER
);
1461 if (refcount_block
) {
1462 /* This should never fail, as it would only do so if the given refcount
1463 * block cannot be found in the cache. As this is impossible as long as
1464 * there are no bugs, assert the success. */
1465 int tmp
= qcow2_cache_put(bs
, s
->refcount_block_cache
, &refcount_block
);
1477 * Checks an image for refcount consistency.
1479 * Returns 0 if no errors are found, the number of errors in case the image is
1480 * detected as corrupted, and -errno when an internal error occurred.
1482 int qcow2_check_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1485 BDRVQcowState
*s
= bs
->opaque
;
1486 int64_t size
, i
, highest_cluster
, nb_clusters
;
1487 int refcount1
, refcount2
;
1489 uint16_t *refcount_table
;
1492 size
= bdrv_getlength(bs
->file
);
1494 res
->check_errors
++;
1498 nb_clusters
= size_to_clusters(s
, size
);
1499 if (nb_clusters
> INT_MAX
) {
1500 res
->check_errors
++;
1504 refcount_table
= g_malloc0(nb_clusters
* sizeof(uint16_t));
1506 res
->bfi
.total_clusters
=
1507 size_to_clusters(s
, bs
->total_sectors
* BDRV_SECTOR_SIZE
);
1510 inc_refcounts(bs
, res
, refcount_table
, nb_clusters
,
1511 0, s
->cluster_size
);
1513 /* current L1 table */
1514 ret
= check_refcounts_l1(bs
, res
, refcount_table
, nb_clusters
,
1515 s
->l1_table_offset
, s
->l1_size
, CHECK_FRAG_INFO
);
1521 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1522 sn
= s
->snapshots
+ i
;
1523 ret
= check_refcounts_l1(bs
, res
, refcount_table
, nb_clusters
,
1524 sn
->l1_table_offset
, sn
->l1_size
, 0);
1529 inc_refcounts(bs
, res
, refcount_table
, nb_clusters
,
1530 s
->snapshots_offset
, s
->snapshots_size
);
1533 inc_refcounts(bs
, res
, refcount_table
, nb_clusters
,
1534 s
->refcount_table_offset
,
1535 s
->refcount_table_size
* sizeof(uint64_t));
1537 for(i
= 0; i
< s
->refcount_table_size
; i
++) {
1538 uint64_t offset
, cluster
;
1539 offset
= s
->refcount_table
[i
];
1540 cluster
= offset
>> s
->cluster_bits
;
1542 /* Refcount blocks are cluster aligned */
1543 if (offset_into_cluster(s
, offset
)) {
1544 fprintf(stderr
, "ERROR refcount block %" PRId64
" is not "
1545 "cluster aligned; refcount table entry corrupted\n", i
);
1550 if (cluster
>= nb_clusters
) {
1551 fprintf(stderr
, "ERROR refcount block %" PRId64
1552 " is outside image\n", i
);
1558 inc_refcounts(bs
, res
, refcount_table
, nb_clusters
,
1559 offset
, s
->cluster_size
);
1560 if (refcount_table
[cluster
] != 1) {
1561 fprintf(stderr
, "%s refcount block %" PRId64
1563 fix
& BDRV_FIX_ERRORS
? "Repairing" :
1565 i
, refcount_table
[cluster
]);
1567 if (fix
& BDRV_FIX_ERRORS
) {
1570 new_offset
= realloc_refcount_block(bs
, i
, offset
);
1571 if (new_offset
< 0) {
1576 /* update refcounts */
1577 if ((new_offset
>> s
->cluster_bits
) >= nb_clusters
) {
1578 /* increase refcount_table size if necessary */
1579 int old_nb_clusters
= nb_clusters
;
1580 nb_clusters
= (new_offset
>> s
->cluster_bits
) + 1;
1581 refcount_table
= g_realloc(refcount_table
,
1582 nb_clusters
* sizeof(uint16_t));
1583 memset(&refcount_table
[old_nb_clusters
], 0, (nb_clusters
1584 - old_nb_clusters
) * sizeof(uint16_t));
1586 refcount_table
[cluster
]--;
1587 inc_refcounts(bs
, res
, refcount_table
, nb_clusters
,
1588 new_offset
, s
->cluster_size
);
1590 res
->corruptions_fixed
++;
1598 /* compare ref counts */
1599 for (i
= 0, highest_cluster
= 0; i
< nb_clusters
; i
++) {
1600 refcount1
= get_refcount(bs
, i
);
1601 if (refcount1
< 0) {
1602 fprintf(stderr
, "Can't get refcount for cluster %" PRId64
": %s\n",
1603 i
, strerror(-refcount1
));
1604 res
->check_errors
++;
1608 refcount2
= refcount_table
[i
];
1610 if (refcount1
> 0 || refcount2
> 0) {
1611 highest_cluster
= i
;
1614 if (refcount1
!= refcount2
) {
1616 /* Check if we're allowed to fix the mismatch */
1617 int *num_fixed
= NULL
;
1618 if (refcount1
> refcount2
&& (fix
& BDRV_FIX_LEAKS
)) {
1619 num_fixed
= &res
->leaks_fixed
;
1620 } else if (refcount1
< refcount2
&& (fix
& BDRV_FIX_ERRORS
)) {
1621 num_fixed
= &res
->corruptions_fixed
;
1624 fprintf(stderr
, "%s cluster %" PRId64
" refcount=%d reference=%d\n",
1625 num_fixed
!= NULL
? "Repairing" :
1626 refcount1
< refcount2
? "ERROR" :
1628 i
, refcount1
, refcount2
);
1631 ret
= update_refcount(bs
, i
<< s
->cluster_bits
, 1,
1632 refcount2
- refcount1
,
1633 QCOW2_DISCARD_ALWAYS
);
1640 /* And if we couldn't, print an error */
1641 if (refcount1
< refcount2
) {
1649 /* check OFLAG_COPIED */
1650 ret
= check_oflag_copied(bs
, res
, fix
);
1655 res
->image_end_offset
= (highest_cluster
+ 1) * s
->cluster_size
;
1659 g_free(refcount_table
);
1664 #define overlaps_with(ofs, sz) \
1665 ranges_overlap(offset, size, ofs, sz)
1668 * Checks if the given offset into the image file is actually free to use by
1669 * looking for overlaps with important metadata sections (L1/L2 tables etc.),
1670 * i.e. a sanity check without relying on the refcount tables.
1672 * The ign parameter specifies what checks not to perform (being a bitmask of
1673 * QCow2MetadataOverlap values), i.e., what sections to ignore.
1676 * - 0 if writing to this offset will not affect the mentioned metadata
1677 * - a positive QCow2MetadataOverlap value indicating one overlapping section
1678 * - a negative value (-errno) indicating an error while performing a check,
1679 * e.g. when bdrv_read failed on QCOW2_OL_INACTIVE_L2
1681 int qcow2_check_metadata_overlap(BlockDriverState
*bs
, int ign
, int64_t offset
,
1684 BDRVQcowState
*s
= bs
->opaque
;
1685 int chk
= s
->overlap_check
& ~ign
;
1692 if (chk
& QCOW2_OL_MAIN_HEADER
) {
1693 if (offset
< s
->cluster_size
) {
1694 return QCOW2_OL_MAIN_HEADER
;
1698 /* align range to test to cluster boundaries */
1699 size
= align_offset(offset_into_cluster(s
, offset
) + size
, s
->cluster_size
);
1700 offset
= start_of_cluster(s
, offset
);
1702 if ((chk
& QCOW2_OL_ACTIVE_L1
) && s
->l1_size
) {
1703 if (overlaps_with(s
->l1_table_offset
, s
->l1_size
* sizeof(uint64_t))) {
1704 return QCOW2_OL_ACTIVE_L1
;
1708 if ((chk
& QCOW2_OL_REFCOUNT_TABLE
) && s
->refcount_table_size
) {
1709 if (overlaps_with(s
->refcount_table_offset
,
1710 s
->refcount_table_size
* sizeof(uint64_t))) {
1711 return QCOW2_OL_REFCOUNT_TABLE
;
1715 if ((chk
& QCOW2_OL_SNAPSHOT_TABLE
) && s
->snapshots_size
) {
1716 if (overlaps_with(s
->snapshots_offset
, s
->snapshots_size
)) {
1717 return QCOW2_OL_SNAPSHOT_TABLE
;
1721 if ((chk
& QCOW2_OL_INACTIVE_L1
) && s
->snapshots
) {
1722 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
1723 if (s
->snapshots
[i
].l1_size
&&
1724 overlaps_with(s
->snapshots
[i
].l1_table_offset
,
1725 s
->snapshots
[i
].l1_size
* sizeof(uint64_t))) {
1726 return QCOW2_OL_INACTIVE_L1
;
1731 if ((chk
& QCOW2_OL_ACTIVE_L2
) && s
->l1_table
) {
1732 for (i
= 0; i
< s
->l1_size
; i
++) {
1733 if ((s
->l1_table
[i
] & L1E_OFFSET_MASK
) &&
1734 overlaps_with(s
->l1_table
[i
] & L1E_OFFSET_MASK
,
1736 return QCOW2_OL_ACTIVE_L2
;
1741 if ((chk
& QCOW2_OL_REFCOUNT_BLOCK
) && s
->refcount_table
) {
1742 for (i
= 0; i
< s
->refcount_table_size
; i
++) {
1743 if ((s
->refcount_table
[i
] & REFT_OFFSET_MASK
) &&
1744 overlaps_with(s
->refcount_table
[i
] & REFT_OFFSET_MASK
,
1746 return QCOW2_OL_REFCOUNT_BLOCK
;
1751 if ((chk
& QCOW2_OL_INACTIVE_L2
) && s
->snapshots
) {
1752 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
1753 uint64_t l1_ofs
= s
->snapshots
[i
].l1_table_offset
;
1754 uint32_t l1_sz
= s
->snapshots
[i
].l1_size
;
1755 uint64_t l1_sz2
= l1_sz
* sizeof(uint64_t);
1756 uint64_t *l1
= g_malloc(l1_sz2
);
1759 ret
= bdrv_pread(bs
->file
, l1_ofs
, l1
, l1_sz2
);
1765 for (j
= 0; j
< l1_sz
; j
++) {
1766 uint64_t l2_ofs
= be64_to_cpu(l1
[j
]) & L1E_OFFSET_MASK
;
1767 if (l2_ofs
&& overlaps_with(l2_ofs
, s
->cluster_size
)) {
1769 return QCOW2_OL_INACTIVE_L2
;
1780 static const char *metadata_ol_names
[] = {
1781 [QCOW2_OL_MAIN_HEADER_BITNR
] = "qcow2_header",
1782 [QCOW2_OL_ACTIVE_L1_BITNR
] = "active L1 table",
1783 [QCOW2_OL_ACTIVE_L2_BITNR
] = "active L2 table",
1784 [QCOW2_OL_REFCOUNT_TABLE_BITNR
] = "refcount table",
1785 [QCOW2_OL_REFCOUNT_BLOCK_BITNR
] = "refcount block",
1786 [QCOW2_OL_SNAPSHOT_TABLE_BITNR
] = "snapshot table",
1787 [QCOW2_OL_INACTIVE_L1_BITNR
] = "inactive L1 table",
1788 [QCOW2_OL_INACTIVE_L2_BITNR
] = "inactive L2 table",
1792 * First performs a check for metadata overlaps (through
1793 * qcow2_check_metadata_overlap); if that fails with a negative value (error
1794 * while performing a check), that value is returned. If an impending overlap
1795 * is detected, the BDS will be made unusable, the qcow2 file marked corrupt
1796 * and -EIO returned.
1798 * Returns 0 if there were neither overlaps nor errors while checking for
1799 * overlaps; or a negative value (-errno) on error.
1801 int qcow2_pre_write_overlap_check(BlockDriverState
*bs
, int ign
, int64_t offset
,
1804 int ret
= qcow2_check_metadata_overlap(bs
, ign
, offset
, size
);
1808 } else if (ret
> 0) {
1809 int metadata_ol_bitnr
= ffs(ret
) - 1;
1812 assert(metadata_ol_bitnr
< QCOW2_OL_MAX_BITNR
);
1814 fprintf(stderr
, "qcow2: Preventing invalid write on metadata (overlaps "
1815 "with %s); image marked as corrupt.\n",
1816 metadata_ol_names
[metadata_ol_bitnr
]);
1817 message
= g_strdup_printf("Prevented %s overwrite",
1818 metadata_ol_names
[metadata_ol_bitnr
]);
1819 qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs
),
1828 qcow2_mark_corrupt(bs
);
1829 bs
->drv
= NULL
; /* make BDS unusable */