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"
31 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, int64_t size
);
32 static int QEMU_WARN_UNUSED_RESULT
update_refcount(BlockDriverState
*bs
,
33 int64_t offset
, int64_t length
,
34 int addend
, enum qcow2_discard_type type
);
37 /*********************************************************/
38 /* refcount handling */
40 int qcow2_refcount_init(BlockDriverState
*bs
)
42 BDRVQcowState
*s
= bs
->opaque
;
43 int ret
, refcount_table_size2
, i
;
45 refcount_table_size2
= s
->refcount_table_size
* sizeof(uint64_t);
46 s
->refcount_table
= g_malloc(refcount_table_size2
);
47 if (s
->refcount_table_size
> 0) {
48 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_LOAD
);
49 ret
= bdrv_pread(bs
->file
, s
->refcount_table_offset
,
50 s
->refcount_table
, refcount_table_size2
);
51 if (ret
!= refcount_table_size2
)
53 for(i
= 0; i
< s
->refcount_table_size
; i
++)
54 be64_to_cpus(&s
->refcount_table
[i
]);
61 void qcow2_refcount_close(BlockDriverState
*bs
)
63 BDRVQcowState
*s
= bs
->opaque
;
64 g_free(s
->refcount_table
);
68 static int load_refcount_block(BlockDriverState
*bs
,
69 int64_t refcount_block_offset
,
70 void **refcount_block
)
72 BDRVQcowState
*s
= bs
->opaque
;
75 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_LOAD
);
76 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refcount_block_offset
,
83 * Returns the refcount of the cluster given by its index. Any non-negative
84 * return value is the refcount of the cluster, negative values are -errno
85 * and indicate an error.
87 static int get_refcount(BlockDriverState
*bs
, int64_t cluster_index
)
89 BDRVQcowState
*s
= bs
->opaque
;
90 int refcount_table_index
, block_index
;
91 int64_t refcount_block_offset
;
93 uint16_t *refcount_block
;
96 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
97 if (refcount_table_index
>= s
->refcount_table_size
)
99 refcount_block_offset
= s
->refcount_table
[refcount_table_index
];
100 if (!refcount_block_offset
)
103 ret
= qcow2_cache_get(bs
, s
->refcount_block_cache
, refcount_block_offset
,
104 (void**) &refcount_block
);
109 block_index
= cluster_index
&
110 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
111 refcount
= be16_to_cpu(refcount_block
[block_index
]);
113 ret
= qcow2_cache_put(bs
, s
->refcount_block_cache
,
114 (void**) &refcount_block
);
123 * Rounds the refcount table size up to avoid growing the table for each single
124 * refcount block that is allocated.
126 static unsigned int next_refcount_table_size(BDRVQcowState
*s
,
127 unsigned int min_size
)
129 unsigned int min_clusters
= (min_size
>> (s
->cluster_bits
- 3)) + 1;
130 unsigned int refcount_table_clusters
=
131 MAX(1, s
->refcount_table_size
>> (s
->cluster_bits
- 3));
133 while (min_clusters
> refcount_table_clusters
) {
134 refcount_table_clusters
= (refcount_table_clusters
* 3 + 1) / 2;
137 return refcount_table_clusters
<< (s
->cluster_bits
- 3);
141 /* Checks if two offsets are described by the same refcount block */
142 static int in_same_refcount_block(BDRVQcowState
*s
, uint64_t offset_a
,
145 uint64_t block_a
= offset_a
>> (2 * s
->cluster_bits
- REFCOUNT_SHIFT
);
146 uint64_t block_b
= offset_b
>> (2 * s
->cluster_bits
- REFCOUNT_SHIFT
);
148 return (block_a
== block_b
);
152 * Loads a refcount block. If it doesn't exist yet, it is allocated first
153 * (including growing the refcount table if needed).
155 * Returns 0 on success or -errno in error case
157 static int alloc_refcount_block(BlockDriverState
*bs
,
158 int64_t cluster_index
, uint16_t **refcount_block
)
160 BDRVQcowState
*s
= bs
->opaque
;
161 unsigned int refcount_table_index
;
164 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC
);
166 /* Find the refcount block for the given cluster */
167 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
169 if (refcount_table_index
< s
->refcount_table_size
) {
171 uint64_t refcount_block_offset
=
172 s
->refcount_table
[refcount_table_index
] & REFT_OFFSET_MASK
;
174 /* If it's already there, we're done */
175 if (refcount_block_offset
) {
176 return load_refcount_block(bs
, refcount_block_offset
,
177 (void**) refcount_block
);
182 * If we came here, we need to allocate something. Something is at least
183 * a cluster for the new refcount block. It may also include a new refcount
184 * table if the old refcount table is too small.
186 * Note that allocating clusters here needs some special care:
188 * - We can't use the normal qcow2_alloc_clusters(), it would try to
189 * increase the refcount and very likely we would end up with an endless
190 * recursion. Instead we must place the refcount blocks in a way that
191 * they can describe them themselves.
193 * - We need to consider that at this point we are inside update_refcounts
194 * and doing the initial refcount increase. This means that some clusters
195 * have already been allocated by the caller, but their refcount isn't
196 * accurate yet. free_cluster_index tells us where this allocation ends
197 * as long as we don't overwrite it by freeing clusters.
199 * - alloc_clusters_noref and qcow2_free_clusters may load a different
200 * refcount block into the cache
203 *refcount_block
= NULL
;
205 /* We write to the refcount table, so we might depend on L2 tables */
206 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
211 /* Allocate the refcount block itself and mark it as used */
212 int64_t new_block
= alloc_clusters_noref(bs
, s
->cluster_size
);
218 fprintf(stderr
, "qcow2: Allocate refcount block %d for %" PRIx64
220 refcount_table_index
, cluster_index
<< s
->cluster_bits
, new_block
);
223 if (in_same_refcount_block(s
, new_block
, cluster_index
<< s
->cluster_bits
)) {
224 /* Zero the new refcount block before updating it */
225 ret
= qcow2_cache_get_empty(bs
, s
->refcount_block_cache
, new_block
,
226 (void**) refcount_block
);
231 memset(*refcount_block
, 0, s
->cluster_size
);
233 /* The block describes itself, need to update the cache */
234 int block_index
= (new_block
>> s
->cluster_bits
) &
235 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
236 (*refcount_block
)[block_index
] = cpu_to_be16(1);
238 /* Described somewhere else. This can recurse at most twice before we
239 * arrive at a block that describes itself. */
240 ret
= update_refcount(bs
, new_block
, s
->cluster_size
, 1,
241 QCOW2_DISCARD_NEVER
);
246 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
251 /* Initialize the new refcount block only after updating its refcount,
252 * update_refcount uses the refcount cache itself */
253 ret
= qcow2_cache_get_empty(bs
, s
->refcount_block_cache
, new_block
,
254 (void**) refcount_block
);
259 memset(*refcount_block
, 0, s
->cluster_size
);
262 /* Now the new refcount block needs to be written to disk */
263 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE
);
264 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
, *refcount_block
);
265 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
270 /* If the refcount table is big enough, just hook the block up there */
271 if (refcount_table_index
< s
->refcount_table_size
) {
272 uint64_t data64
= cpu_to_be64(new_block
);
273 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_HOOKUP
);
274 ret
= bdrv_pwrite_sync(bs
->file
,
275 s
->refcount_table_offset
+ refcount_table_index
* sizeof(uint64_t),
276 &data64
, sizeof(data64
));
281 s
->refcount_table
[refcount_table_index
] = new_block
;
285 ret
= qcow2_cache_put(bs
, s
->refcount_block_cache
, (void**) refcount_block
);
291 * If we come here, we need to grow the refcount table. Again, a new
292 * refcount table needs some space and we can't simply allocate to avoid
295 * Therefore let's grab new refcount blocks at the end of the image, which
296 * will describe themselves and the new refcount table. This way we can
297 * reference them only in the new table and do the switch to the new
298 * refcount table at once without producing an inconsistent state in
301 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_GROW
);
303 /* Calculate the number of refcount blocks needed so far */
304 uint64_t refcount_block_clusters
= 1 << (s
->cluster_bits
- REFCOUNT_SHIFT
);
305 uint64_t blocks_used
= (s
->free_cluster_index
+
306 refcount_block_clusters
- 1) / refcount_block_clusters
;
308 /* And now we need at least one block more for the new metadata */
309 uint64_t table_size
= next_refcount_table_size(s
, blocks_used
+ 1);
310 uint64_t last_table_size
;
311 uint64_t blocks_clusters
;
313 uint64_t table_clusters
=
314 size_to_clusters(s
, table_size
* sizeof(uint64_t));
315 blocks_clusters
= 1 +
316 ((table_clusters
+ refcount_block_clusters
- 1)
317 / refcount_block_clusters
);
318 uint64_t meta_clusters
= table_clusters
+ blocks_clusters
;
320 last_table_size
= table_size
;
321 table_size
= next_refcount_table_size(s
, blocks_used
+
322 ((meta_clusters
+ refcount_block_clusters
- 1)
323 / refcount_block_clusters
));
325 } while (last_table_size
!= table_size
);
328 fprintf(stderr
, "qcow2: Grow refcount table %" PRId32
" => %" PRId64
"\n",
329 s
->refcount_table_size
, table_size
);
332 /* Create the new refcount table and blocks */
333 uint64_t meta_offset
= (blocks_used
* refcount_block_clusters
) *
335 uint64_t table_offset
= meta_offset
+ blocks_clusters
* s
->cluster_size
;
336 uint16_t *new_blocks
= g_malloc0(blocks_clusters
* s
->cluster_size
);
337 uint64_t *new_table
= g_malloc0(table_size
* sizeof(uint64_t));
339 assert(meta_offset
>= (s
->free_cluster_index
* s
->cluster_size
));
341 /* Fill the new refcount table */
342 memcpy(new_table
, s
->refcount_table
,
343 s
->refcount_table_size
* sizeof(uint64_t));
344 new_table
[refcount_table_index
] = new_block
;
347 for (i
= 0; i
< blocks_clusters
; i
++) {
348 new_table
[blocks_used
+ i
] = meta_offset
+ (i
* s
->cluster_size
);
351 /* Fill the refcount blocks */
352 uint64_t table_clusters
= size_to_clusters(s
, table_size
* sizeof(uint64_t));
354 for (i
= 0; i
< table_clusters
+ blocks_clusters
; i
++) {
355 new_blocks
[block
++] = cpu_to_be16(1);
358 /* Write refcount blocks to disk */
359 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS
);
360 ret
= bdrv_pwrite_sync(bs
->file
, meta_offset
, new_blocks
,
361 blocks_clusters
* s
->cluster_size
);
367 /* Write refcount table to disk */
368 for(i
= 0; i
< table_size
; i
++) {
369 cpu_to_be64s(&new_table
[i
]);
372 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE
);
373 ret
= bdrv_pwrite_sync(bs
->file
, table_offset
, new_table
,
374 table_size
* sizeof(uint64_t));
379 for(i
= 0; i
< table_size
; i
++) {
380 be64_to_cpus(&new_table
[i
]);
383 /* Hook up the new refcount table in the qcow2 header */
385 cpu_to_be64w((uint64_t*)data
, table_offset
);
386 cpu_to_be32w((uint32_t*)(data
+ 8), table_clusters
);
387 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE
);
388 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, refcount_table_offset
),
394 /* And switch it in memory */
395 uint64_t old_table_offset
= s
->refcount_table_offset
;
396 uint64_t old_table_size
= s
->refcount_table_size
;
398 g_free(s
->refcount_table
);
399 s
->refcount_table
= new_table
;
400 s
->refcount_table_size
= table_size
;
401 s
->refcount_table_offset
= table_offset
;
403 /* Free old table. Remember, we must not change free_cluster_index */
404 uint64_t old_free_cluster_index
= s
->free_cluster_index
;
405 qcow2_free_clusters(bs
, old_table_offset
, old_table_size
* sizeof(uint64_t),
406 QCOW2_DISCARD_OTHER
);
407 s
->free_cluster_index
= old_free_cluster_index
;
409 ret
= load_refcount_block(bs
, new_block
, (void**) refcount_block
);
419 if (*refcount_block
!= NULL
) {
420 qcow2_cache_put(bs
, s
->refcount_block_cache
, (void**) refcount_block
);
425 void qcow2_process_discards(BlockDriverState
*bs
, int ret
)
427 BDRVQcowState
*s
= bs
->opaque
;
428 Qcow2DiscardRegion
*d
, *next
;
430 QTAILQ_FOREACH_SAFE(d
, &s
->discards
, next
, next
) {
431 QTAILQ_REMOVE(&s
->discards
, d
, next
);
433 /* Discard is optional, ignore the return value */
435 bdrv_discard(bs
->file
,
436 d
->offset
>> BDRV_SECTOR_BITS
,
437 d
->bytes
>> BDRV_SECTOR_BITS
);
444 static void update_refcount_discard(BlockDriverState
*bs
,
445 uint64_t offset
, uint64_t length
)
447 BDRVQcowState
*s
= bs
->opaque
;
448 Qcow2DiscardRegion
*d
, *p
, *next
;
450 QTAILQ_FOREACH(d
, &s
->discards
, next
) {
451 uint64_t new_start
= MIN(offset
, d
->offset
);
452 uint64_t new_end
= MAX(offset
+ length
, d
->offset
+ d
->bytes
);
454 if (new_end
- new_start
<= length
+ d
->bytes
) {
455 /* There can't be any overlap, areas ending up here have no
456 * references any more and therefore shouldn't get freed another
458 assert(d
->bytes
+ length
== new_end
- new_start
);
459 d
->offset
= new_start
;
460 d
->bytes
= new_end
- new_start
;
465 d
= g_malloc(sizeof(*d
));
466 *d
= (Qcow2DiscardRegion
) {
471 QTAILQ_INSERT_TAIL(&s
->discards
, d
, next
);
474 /* Merge discard requests if they are adjacent now */
475 QTAILQ_FOREACH_SAFE(p
, &s
->discards
, next
, next
) {
477 || p
->offset
> d
->offset
+ d
->bytes
478 || d
->offset
> p
->offset
+ p
->bytes
)
483 /* Still no overlap possible */
484 assert(p
->offset
== d
->offset
+ d
->bytes
485 || d
->offset
== p
->offset
+ p
->bytes
);
487 QTAILQ_REMOVE(&s
->discards
, p
, next
);
488 d
->offset
= MIN(d
->offset
, p
->offset
);
489 d
->bytes
+= p
->bytes
;
493 /* XXX: cache several refcount block clusters ? */
494 static int QEMU_WARN_UNUSED_RESULT
update_refcount(BlockDriverState
*bs
,
495 int64_t offset
, int64_t length
, int addend
, enum qcow2_discard_type type
)
497 BDRVQcowState
*s
= bs
->opaque
;
498 int64_t start
, last
, cluster_offset
;
499 uint16_t *refcount_block
= NULL
;
500 int64_t old_table_index
= -1;
504 fprintf(stderr
, "update_refcount: offset=%" PRId64
" size=%" PRId64
" addend=%d\n",
505 offset
, length
, addend
);
509 } else if (length
== 0) {
514 qcow2_cache_set_dependency(bs
, s
->refcount_block_cache
,
518 start
= offset
& ~(s
->cluster_size
- 1);
519 last
= (offset
+ length
- 1) & ~(s
->cluster_size
- 1);
520 for(cluster_offset
= start
; cluster_offset
<= last
;
521 cluster_offset
+= s
->cluster_size
)
523 int block_index
, refcount
;
524 int64_t cluster_index
= cluster_offset
>> s
->cluster_bits
;
525 int64_t table_index
=
526 cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
528 /* Load the refcount block and allocate it if needed */
529 if (table_index
!= old_table_index
) {
530 if (refcount_block
) {
531 ret
= qcow2_cache_put(bs
, s
->refcount_block_cache
,
532 (void**) &refcount_block
);
538 ret
= alloc_refcount_block(bs
, cluster_index
, &refcount_block
);
543 old_table_index
= table_index
;
545 qcow2_cache_entry_mark_dirty(s
->refcount_block_cache
, refcount_block
);
547 /* we can update the count and save it */
548 block_index
= cluster_index
&
549 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
551 refcount
= be16_to_cpu(refcount_block
[block_index
]);
553 if (refcount
< 0 || refcount
> 0xffff) {
557 if (refcount
== 0 && cluster_index
< s
->free_cluster_index
) {
558 s
->free_cluster_index
= cluster_index
;
560 refcount_block
[block_index
] = cpu_to_be16(refcount
);
562 if (refcount
== 0 && s
->discard_passthrough
[type
]) {
563 update_refcount_discard(bs
, cluster_offset
, s
->cluster_size
);
569 if (!s
->cache_discards
) {
570 qcow2_process_discards(bs
, ret
);
573 /* Write last changed block to disk */
574 if (refcount_block
) {
576 wret
= qcow2_cache_put(bs
, s
->refcount_block_cache
,
577 (void**) &refcount_block
);
579 return ret
< 0 ? ret
: wret
;
584 * Try do undo any updates if an error is returned (This may succeed in
585 * some cases like ENOSPC for allocating a new refcount block)
589 dummy
= update_refcount(bs
, offset
, cluster_offset
- offset
, -addend
,
590 QCOW2_DISCARD_NEVER
);
598 * Increases or decreases the refcount of a given cluster by one.
599 * addend must be 1 or -1.
601 * If the return value is non-negative, it is the new refcount of the cluster.
602 * If it is negative, it is -errno and indicates an error.
604 static int update_cluster_refcount(BlockDriverState
*bs
,
605 int64_t cluster_index
,
607 enum qcow2_discard_type type
)
609 BDRVQcowState
*s
= bs
->opaque
;
612 ret
= update_refcount(bs
, cluster_index
<< s
->cluster_bits
, 1, addend
,
618 return get_refcount(bs
, cluster_index
);
623 /*********************************************************/
624 /* cluster allocation functions */
628 /* return < 0 if error */
629 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, int64_t size
)
631 BDRVQcowState
*s
= bs
->opaque
;
632 int i
, nb_clusters
, refcount
;
634 nb_clusters
= size_to_clusters(s
, size
);
636 for(i
= 0; i
< nb_clusters
; i
++) {
637 int64_t next_cluster_index
= s
->free_cluster_index
++;
638 refcount
= get_refcount(bs
, next_cluster_index
);
642 } else if (refcount
!= 0) {
647 fprintf(stderr
, "alloc_clusters: size=%" PRId64
" -> %" PRId64
"\n",
649 (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
);
651 return (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
;
654 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, int64_t size
)
659 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC
);
660 offset
= alloc_clusters_noref(bs
, size
);
665 ret
= update_refcount(bs
, offset
, size
, 1, QCOW2_DISCARD_NEVER
);
673 int qcow2_alloc_clusters_at(BlockDriverState
*bs
, uint64_t offset
,
676 BDRVQcowState
*s
= bs
->opaque
;
677 uint64_t cluster_index
;
678 uint64_t old_free_cluster_index
;
679 int i
, refcount
, ret
;
681 /* Check how many clusters there are free */
682 cluster_index
= offset
>> s
->cluster_bits
;
683 for(i
= 0; i
< nb_clusters
; i
++) {
684 refcount
= get_refcount(bs
, cluster_index
++);
688 } else if (refcount
!= 0) {
693 /* And then allocate them */
694 old_free_cluster_index
= s
->free_cluster_index
;
695 s
->free_cluster_index
= cluster_index
+ i
;
697 ret
= update_refcount(bs
, offset
, i
<< s
->cluster_bits
, 1,
698 QCOW2_DISCARD_NEVER
);
703 s
->free_cluster_index
= old_free_cluster_index
;
708 /* only used to allocate compressed sectors. We try to allocate
709 contiguous sectors. size must be <= cluster_size */
710 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
)
712 BDRVQcowState
*s
= bs
->opaque
;
713 int64_t offset
, cluster_offset
;
716 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC_BYTES
);
717 assert(size
> 0 && size
<= s
->cluster_size
);
718 if (s
->free_byte_offset
== 0) {
719 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
723 s
->free_byte_offset
= offset
;
726 free_in_cluster
= s
->cluster_size
-
727 (s
->free_byte_offset
& (s
->cluster_size
- 1));
728 if (size
<= free_in_cluster
) {
729 /* enough space in current cluster */
730 offset
= s
->free_byte_offset
;
731 s
->free_byte_offset
+= size
;
732 free_in_cluster
-= size
;
733 if (free_in_cluster
== 0)
734 s
->free_byte_offset
= 0;
735 if ((offset
& (s
->cluster_size
- 1)) != 0)
736 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1,
737 QCOW2_DISCARD_NEVER
);
739 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
743 cluster_offset
= s
->free_byte_offset
& ~(s
->cluster_size
- 1);
744 if ((cluster_offset
+ s
->cluster_size
) == offset
) {
745 /* we are lucky: contiguous data */
746 offset
= s
->free_byte_offset
;
747 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1,
748 QCOW2_DISCARD_NEVER
);
749 s
->free_byte_offset
+= size
;
751 s
->free_byte_offset
= offset
;
756 /* The cluster refcount was incremented, either by qcow2_alloc_clusters()
757 * or explicitly by update_cluster_refcount(). Refcount blocks must be
758 * flushed before the caller's L2 table updates.
760 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
, s
->refcount_block_cache
);
764 void qcow2_free_clusters(BlockDriverState
*bs
,
765 int64_t offset
, int64_t size
,
766 enum qcow2_discard_type type
)
770 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_FREE
);
771 ret
= update_refcount(bs
, offset
, size
, -1, type
);
773 fprintf(stderr
, "qcow2_free_clusters failed: %s\n", strerror(-ret
));
774 /* TODO Remember the clusters to free them later and avoid leaking */
779 * Free a cluster using its L2 entry (handles clusters of all types, e.g.
780 * normal cluster, compressed cluster, etc.)
782 void qcow2_free_any_clusters(BlockDriverState
*bs
, uint64_t l2_entry
,
783 int nb_clusters
, enum qcow2_discard_type type
)
785 BDRVQcowState
*s
= bs
->opaque
;
787 switch (qcow2_get_cluster_type(l2_entry
)) {
788 case QCOW2_CLUSTER_COMPRESSED
:
791 nb_csectors
= ((l2_entry
>> s
->csize_shift
) &
793 qcow2_free_clusters(bs
,
794 (l2_entry
& s
->cluster_offset_mask
) & ~511,
795 nb_csectors
* 512, type
);
798 case QCOW2_CLUSTER_NORMAL
:
799 qcow2_free_clusters(bs
, l2_entry
& L2E_OFFSET_MASK
,
800 nb_clusters
<< s
->cluster_bits
, type
);
802 case QCOW2_CLUSTER_UNALLOCATED
:
803 case QCOW2_CLUSTER_ZERO
:
812 /*********************************************************/
813 /* snapshots and image creation */
817 /* update the refcounts of snapshots and the copied flag */
818 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
819 int64_t l1_table_offset
, int l1_size
, int addend
)
821 BDRVQcowState
*s
= bs
->opaque
;
822 uint64_t *l1_table
, *l2_table
, l2_offset
, offset
, l1_size2
, l1_allocated
;
823 int64_t old_offset
, old_l2_offset
;
824 int i
, j
, l1_modified
= 0, nb_csectors
, refcount
;
829 l1_size2
= l1_size
* sizeof(uint64_t);
831 s
->cache_discards
= true;
833 /* WARNING: qcow2_snapshot_goto relies on this function not using the
834 * l1_table_offset when it is the current s->l1_table_offset! Be careful
835 * when changing this! */
836 if (l1_table_offset
!= s
->l1_table_offset
) {
837 l1_table
= g_malloc0(align_offset(l1_size2
, 512));
840 ret
= bdrv_pread(bs
->file
, l1_table_offset
, l1_table
, l1_size2
);
845 for(i
= 0;i
< l1_size
; i
++)
846 be64_to_cpus(&l1_table
[i
]);
848 assert(l1_size
== s
->l1_size
);
849 l1_table
= s
->l1_table
;
853 for(i
= 0; i
< l1_size
; i
++) {
854 l2_offset
= l1_table
[i
];
856 old_l2_offset
= l2_offset
;
857 l2_offset
&= L1E_OFFSET_MASK
;
859 ret
= qcow2_cache_get(bs
, s
->l2_table_cache
, l2_offset
,
865 for(j
= 0; j
< s
->l2_size
; j
++) {
866 uint64_t cluster_index
;
868 offset
= be64_to_cpu(l2_table
[j
]);
870 offset
&= ~QCOW_OFLAG_COPIED
;
872 switch (qcow2_get_cluster_type(offset
)) {
873 case QCOW2_CLUSTER_COMPRESSED
:
874 nb_csectors
= ((offset
>> s
->csize_shift
) &
878 ret
= update_refcount(bs
,
879 (offset
& s
->cluster_offset_mask
) & ~511,
880 nb_csectors
* 512, addend
,
881 QCOW2_DISCARD_SNAPSHOT
);
886 /* compressed clusters are never modified */
890 case QCOW2_CLUSTER_NORMAL
:
891 case QCOW2_CLUSTER_ZERO
:
892 cluster_index
= (offset
& L2E_OFFSET_MASK
) >> s
->cluster_bits
;
893 if (!cluster_index
) {
899 refcount
= update_cluster_refcount(bs
, cluster_index
, addend
,
900 QCOW2_DISCARD_SNAPSHOT
);
902 refcount
= get_refcount(bs
, cluster_index
);
911 case QCOW2_CLUSTER_UNALLOCATED
:
920 offset
|= QCOW_OFLAG_COPIED
;
922 if (offset
!= old_offset
) {
924 qcow2_cache_set_dependency(bs
, s
->l2_table_cache
,
925 s
->refcount_block_cache
);
927 l2_table
[j
] = cpu_to_be64(offset
);
928 qcow2_cache_entry_mark_dirty(s
->l2_table_cache
, l2_table
);
932 ret
= qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
939 refcount
= update_cluster_refcount(bs
, l2_offset
>> s
->cluster_bits
, addend
,
940 QCOW2_DISCARD_SNAPSHOT
);
942 refcount
= get_refcount(bs
, l2_offset
>> s
->cluster_bits
);
947 } else if (refcount
== 1) {
948 l2_offset
|= QCOW_OFLAG_COPIED
;
950 if (l2_offset
!= old_l2_offset
) {
951 l1_table
[i
] = l2_offset
;
957 ret
= bdrv_flush(bs
);
960 qcow2_cache_put(bs
, s
->l2_table_cache
, (void**) &l2_table
);
963 s
->cache_discards
= false;
964 qcow2_process_discards(bs
, ret
);
966 /* Update L1 only if it isn't deleted anyway (addend = -1) */
967 if (ret
== 0 && addend
>= 0 && l1_modified
) {
968 for (i
= 0; i
< l1_size
; i
++) {
969 cpu_to_be64s(&l1_table
[i
]);
972 ret
= bdrv_pwrite_sync(bs
->file
, l1_table_offset
, l1_table
, l1_size2
);
974 for (i
= 0; i
< l1_size
; i
++) {
975 be64_to_cpus(&l1_table
[i
]);
986 /*********************************************************/
987 /* refcount checking functions */
992 * Increases the refcount for a range of clusters in a given refcount table.
993 * This is used to construct a temporary refcount table out of L1 and L2 tables
994 * which can be compared the the refcount table saved in the image.
996 * Modifies the number of errors in res.
998 static void inc_refcounts(BlockDriverState
*bs
,
999 BdrvCheckResult
*res
,
1000 uint16_t *refcount_table
,
1001 int refcount_table_size
,
1002 int64_t offset
, int64_t size
)
1004 BDRVQcowState
*s
= bs
->opaque
;
1005 int64_t start
, last
, cluster_offset
;
1011 start
= offset
& ~(s
->cluster_size
- 1);
1012 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
1013 for(cluster_offset
= start
; cluster_offset
<= last
;
1014 cluster_offset
+= s
->cluster_size
) {
1015 k
= cluster_offset
>> s
->cluster_bits
;
1017 fprintf(stderr
, "ERROR: invalid cluster offset=0x%" PRIx64
"\n",
1020 } else if (k
>= refcount_table_size
) {
1021 fprintf(stderr
, "Warning: cluster offset=0x%" PRIx64
" is after "
1022 "the end of the image file, can't properly check refcounts.\n",
1024 res
->check_errors
++;
1026 if (++refcount_table
[k
] == 0) {
1027 fprintf(stderr
, "ERROR: overflow cluster offset=0x%" PRIx64
1028 "\n", cluster_offset
);
1035 /* Flags for check_refcounts_l1() and check_refcounts_l2() */
1037 CHECK_OFLAG_COPIED
= 0x1, /* check QCOW_OFLAG_COPIED matches refcount */
1038 CHECK_FRAG_INFO
= 0x2, /* update BlockFragInfo counters */
1042 * Increases the refcount in the given refcount table for the all clusters
1043 * referenced in the L2 table. While doing so, performs some checks on L2
1046 * Returns the number of errors found by the checks or -errno if an internal
1049 static int check_refcounts_l2(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1050 uint16_t *refcount_table
, int refcount_table_size
, int64_t l2_offset
,
1053 BDRVQcowState
*s
= bs
->opaque
;
1054 uint64_t *l2_table
, l2_entry
;
1055 uint64_t next_contiguous_offset
= 0;
1056 int i
, l2_size
, nb_csectors
, refcount
;
1058 /* Read L2 table from disk */
1059 l2_size
= s
->l2_size
* sizeof(uint64_t);
1060 l2_table
= g_malloc(l2_size
);
1062 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
, l2_size
) != l2_size
)
1065 /* Do the actual checks */
1066 for(i
= 0; i
< s
->l2_size
; i
++) {
1067 l2_entry
= be64_to_cpu(l2_table
[i
]);
1069 switch (qcow2_get_cluster_type(l2_entry
)) {
1070 case QCOW2_CLUSTER_COMPRESSED
:
1071 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
1072 if (l2_entry
& QCOW_OFLAG_COPIED
) {
1073 fprintf(stderr
, "ERROR: cluster %" PRId64
": "
1074 "copied flag must never be set for compressed "
1075 "clusters\n", l2_entry
>> s
->cluster_bits
);
1076 l2_entry
&= ~QCOW_OFLAG_COPIED
;
1080 /* Mark cluster as used */
1081 nb_csectors
= ((l2_entry
>> s
->csize_shift
) &
1083 l2_entry
&= s
->cluster_offset_mask
;
1084 inc_refcounts(bs
, res
, refcount_table
, refcount_table_size
,
1085 l2_entry
& ~511, nb_csectors
* 512);
1087 if (flags
& CHECK_FRAG_INFO
) {
1088 res
->bfi
.allocated_clusters
++;
1089 res
->bfi
.compressed_clusters
++;
1091 /* Compressed clusters are fragmented by nature. Since they
1092 * take up sub-sector space but we only have sector granularity
1093 * I/O we need to re-read the same sectors even for adjacent
1094 * compressed clusters.
1096 res
->bfi
.fragmented_clusters
++;
1100 case QCOW2_CLUSTER_ZERO
:
1101 if ((l2_entry
& L2E_OFFSET_MASK
) == 0) {
1106 case QCOW2_CLUSTER_NORMAL
:
1108 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
1109 uint64_t offset
= l2_entry
& L2E_OFFSET_MASK
;
1111 if (flags
& CHECK_OFLAG_COPIED
) {
1112 refcount
= get_refcount(bs
, offset
>> s
->cluster_bits
);
1114 fprintf(stderr
, "Can't get refcount for offset %"
1115 PRIx64
": %s\n", l2_entry
, strerror(-refcount
));
1118 if ((refcount
== 1) != ((l2_entry
& QCOW_OFLAG_COPIED
) != 0)) {
1119 fprintf(stderr
, "ERROR OFLAG_COPIED: offset=%"
1120 PRIx64
" refcount=%d\n", l2_entry
, refcount
);
1125 if (flags
& CHECK_FRAG_INFO
) {
1126 res
->bfi
.allocated_clusters
++;
1127 if (next_contiguous_offset
&&
1128 offset
!= next_contiguous_offset
) {
1129 res
->bfi
.fragmented_clusters
++;
1131 next_contiguous_offset
= offset
+ s
->cluster_size
;
1134 /* Mark cluster as used */
1135 inc_refcounts(bs
, res
, refcount_table
,refcount_table_size
,
1136 offset
, s
->cluster_size
);
1138 /* Correct offsets are cluster aligned */
1139 if (offset
& (s
->cluster_size
- 1)) {
1140 fprintf(stderr
, "ERROR offset=%" PRIx64
": Cluster is not "
1141 "properly aligned; L2 entry corrupted.\n", offset
);
1147 case QCOW2_CLUSTER_UNALLOCATED
:
1159 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l2\n");
1165 * Increases the refcount for the L1 table, its L2 tables and all referenced
1166 * clusters in the given refcount table. While doing so, performs some checks
1167 * on L1 and L2 entries.
1169 * Returns the number of errors found by the checks or -errno if an internal
1172 static int check_refcounts_l1(BlockDriverState
*bs
,
1173 BdrvCheckResult
*res
,
1174 uint16_t *refcount_table
,
1175 int refcount_table_size
,
1176 int64_t l1_table_offset
, int l1_size
,
1179 BDRVQcowState
*s
= bs
->opaque
;
1180 uint64_t *l1_table
, l2_offset
, l1_size2
;
1181 int i
, refcount
, ret
;
1183 l1_size2
= l1_size
* sizeof(uint64_t);
1185 /* Mark L1 table as used */
1186 inc_refcounts(bs
, res
, refcount_table
, refcount_table_size
,
1187 l1_table_offset
, l1_size2
);
1189 /* Read L1 table entries from disk */
1190 if (l1_size2
== 0) {
1193 l1_table
= g_malloc(l1_size2
);
1194 if (bdrv_pread(bs
->file
, l1_table_offset
,
1195 l1_table
, l1_size2
) != l1_size2
)
1197 for(i
= 0;i
< l1_size
; i
++)
1198 be64_to_cpus(&l1_table
[i
]);
1201 /* Do the actual checks */
1202 for(i
= 0; i
< l1_size
; i
++) {
1203 l2_offset
= l1_table
[i
];
1205 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
1206 if (flags
& CHECK_OFLAG_COPIED
) {
1207 refcount
= get_refcount(bs
, (l2_offset
& ~QCOW_OFLAG_COPIED
)
1208 >> s
->cluster_bits
);
1210 fprintf(stderr
, "Can't get refcount for l2_offset %"
1211 PRIx64
": %s\n", l2_offset
, strerror(-refcount
));
1214 if ((refcount
== 1) != ((l2_offset
& QCOW_OFLAG_COPIED
) != 0)) {
1215 fprintf(stderr
, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
1216 " refcount=%d\n", l2_offset
, refcount
);
1221 /* Mark L2 table as used */
1222 l2_offset
&= L1E_OFFSET_MASK
;
1223 inc_refcounts(bs
, res
, refcount_table
, refcount_table_size
,
1224 l2_offset
, s
->cluster_size
);
1226 /* L2 tables are cluster aligned */
1227 if (l2_offset
& (s
->cluster_size
- 1)) {
1228 fprintf(stderr
, "ERROR l2_offset=%" PRIx64
": Table is not "
1229 "cluster aligned; L1 entry corrupted\n", l2_offset
);
1233 /* Process and check L2 entries */
1234 ret
= check_refcounts_l2(bs
, res
, refcount_table
,
1235 refcount_table_size
, l2_offset
, flags
);
1245 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
1246 res
->check_errors
++;
1252 * Checks an image for refcount consistency.
1254 * Returns 0 if no errors are found, the number of errors in case the image is
1255 * detected as corrupted, and -errno when an internal error occurred.
1257 int qcow2_check_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
1260 BDRVQcowState
*s
= bs
->opaque
;
1261 int64_t size
, i
, highest_cluster
;
1262 int nb_clusters
, refcount1
, refcount2
;
1264 uint16_t *refcount_table
;
1267 size
= bdrv_getlength(bs
->file
);
1268 nb_clusters
= size_to_clusters(s
, size
);
1269 refcount_table
= g_malloc0(nb_clusters
* sizeof(uint16_t));
1271 res
->bfi
.total_clusters
=
1272 size_to_clusters(s
, bs
->total_sectors
* BDRV_SECTOR_SIZE
);
1275 inc_refcounts(bs
, res
, refcount_table
, nb_clusters
,
1276 0, s
->cluster_size
);
1278 /* current L1 table */
1279 ret
= check_refcounts_l1(bs
, res
, refcount_table
, nb_clusters
,
1280 s
->l1_table_offset
, s
->l1_size
,
1281 CHECK_OFLAG_COPIED
| CHECK_FRAG_INFO
);
1287 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1288 sn
= s
->snapshots
+ i
;
1289 ret
= check_refcounts_l1(bs
, res
, refcount_table
, nb_clusters
,
1290 sn
->l1_table_offset
, sn
->l1_size
, 0);
1295 inc_refcounts(bs
, res
, refcount_table
, nb_clusters
,
1296 s
->snapshots_offset
, s
->snapshots_size
);
1299 inc_refcounts(bs
, res
, refcount_table
, nb_clusters
,
1300 s
->refcount_table_offset
,
1301 s
->refcount_table_size
* sizeof(uint64_t));
1303 for(i
= 0; i
< s
->refcount_table_size
; i
++) {
1304 uint64_t offset
, cluster
;
1305 offset
= s
->refcount_table
[i
];
1306 cluster
= offset
>> s
->cluster_bits
;
1308 /* Refcount blocks are cluster aligned */
1309 if (offset
& (s
->cluster_size
- 1)) {
1310 fprintf(stderr
, "ERROR refcount block %" PRId64
" is not "
1311 "cluster aligned; refcount table entry corrupted\n", i
);
1316 if (cluster
>= nb_clusters
) {
1317 fprintf(stderr
, "ERROR refcount block %" PRId64
1318 " is outside image\n", i
);
1324 inc_refcounts(bs
, res
, refcount_table
, nb_clusters
,
1325 offset
, s
->cluster_size
);
1326 if (refcount_table
[cluster
] != 1) {
1327 fprintf(stderr
, "ERROR refcount block %" PRId64
1329 i
, refcount_table
[cluster
]);
1335 /* compare ref counts */
1336 for (i
= 0, highest_cluster
= 0; i
< nb_clusters
; i
++) {
1337 refcount1
= get_refcount(bs
, i
);
1338 if (refcount1
< 0) {
1339 fprintf(stderr
, "Can't get refcount for cluster %" PRId64
": %s\n",
1340 i
, strerror(-refcount1
));
1341 res
->check_errors
++;
1345 refcount2
= refcount_table
[i
];
1347 if (refcount1
> 0 || refcount2
> 0) {
1348 highest_cluster
= i
;
1351 if (refcount1
!= refcount2
) {
1353 /* Check if we're allowed to fix the mismatch */
1354 int *num_fixed
= NULL
;
1355 if (refcount1
> refcount2
&& (fix
& BDRV_FIX_LEAKS
)) {
1356 num_fixed
= &res
->leaks_fixed
;
1357 } else if (refcount1
< refcount2
&& (fix
& BDRV_FIX_ERRORS
)) {
1358 num_fixed
= &res
->corruptions_fixed
;
1361 fprintf(stderr
, "%s cluster %" PRId64
" refcount=%d reference=%d\n",
1362 num_fixed
!= NULL
? "Repairing" :
1363 refcount1
< refcount2
? "ERROR" :
1365 i
, refcount1
, refcount2
);
1368 ret
= update_refcount(bs
, i
<< s
->cluster_bits
, 1,
1369 refcount2
- refcount1
,
1370 QCOW2_DISCARD_ALWAYS
);
1377 /* And if we couldn't, print an error */
1378 if (refcount1
< refcount2
) {
1386 res
->image_end_offset
= (highest_cluster
+ 1) * s
->cluster_size
;
1390 g_free(refcount_table
);
1395 #define overlaps_with(ofs, sz) \
1396 ranges_overlap(offset, size, ofs, sz)
1399 * Checks if the given offset into the image file is actually free to use by
1400 * looking for overlaps with important metadata sections (L1/L2 tables etc.),
1401 * i.e. a sanity check without relying on the refcount tables.
1403 * The chk parameter specifies exactly what checks to perform (being a bitmask
1404 * of QCow2MetadataOverlap values).
1407 * - 0 if writing to this offset will not affect the mentioned metadata
1408 * - a positive QCow2MetadataOverlap value indicating one overlapping section
1409 * - a negative value (-errno) indicating an error while performing a check,
1410 * e.g. when bdrv_read failed on QCOW2_OL_INACTIVE_L2
1412 int qcow2_check_metadata_overlap(BlockDriverState
*bs
, int chk
, int64_t offset
,
1415 BDRVQcowState
*s
= bs
->opaque
;
1422 if (chk
& QCOW2_OL_MAIN_HEADER
) {
1423 if (offset
< s
->cluster_size
) {
1424 return QCOW2_OL_MAIN_HEADER
;
1428 /* align range to test to cluster boundaries */
1429 size
= align_offset(offset_into_cluster(s
, offset
) + size
, s
->cluster_size
);
1430 offset
= start_of_cluster(s
, offset
);
1432 if ((chk
& QCOW2_OL_ACTIVE_L1
) && s
->l1_size
) {
1433 if (overlaps_with(s
->l1_table_offset
, s
->l1_size
* sizeof(uint64_t))) {
1434 return QCOW2_OL_ACTIVE_L1
;
1438 if ((chk
& QCOW2_OL_REFCOUNT_TABLE
) && s
->refcount_table_size
) {
1439 if (overlaps_with(s
->refcount_table_offset
,
1440 s
->refcount_table_size
* sizeof(uint64_t))) {
1441 return QCOW2_OL_REFCOUNT_TABLE
;
1445 if ((chk
& QCOW2_OL_SNAPSHOT_TABLE
) && s
->snapshots_size
) {
1446 if (overlaps_with(s
->snapshots_offset
, s
->snapshots_size
)) {
1447 return QCOW2_OL_SNAPSHOT_TABLE
;
1451 if ((chk
& QCOW2_OL_INACTIVE_L1
) && s
->snapshots
) {
1452 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
1453 if (s
->snapshots
[i
].l1_size
&&
1454 overlaps_with(s
->snapshots
[i
].l1_table_offset
,
1455 s
->snapshots
[i
].l1_size
* sizeof(uint64_t))) {
1456 return QCOW2_OL_INACTIVE_L1
;
1461 if ((chk
& QCOW2_OL_ACTIVE_L2
) && s
->l1_table
) {
1462 for (i
= 0; i
< s
->l1_size
; i
++) {
1463 if ((s
->l1_table
[i
] & L1E_OFFSET_MASK
) &&
1464 overlaps_with(s
->l1_table
[i
] & L1E_OFFSET_MASK
,
1466 return QCOW2_OL_ACTIVE_L2
;
1471 if ((chk
& QCOW2_OL_REFCOUNT_BLOCK
) && s
->refcount_table
) {
1472 for (i
= 0; i
< s
->refcount_table_size
; i
++) {
1473 if ((s
->refcount_table
[i
] & REFT_OFFSET_MASK
) &&
1474 overlaps_with(s
->refcount_table
[i
] & REFT_OFFSET_MASK
,
1476 return QCOW2_OL_REFCOUNT_BLOCK
;
1481 if ((chk
& QCOW2_OL_INACTIVE_L2
) && s
->snapshots
) {
1482 for (i
= 0; i
< s
->nb_snapshots
; i
++) {
1483 uint64_t l1_ofs
= s
->snapshots
[i
].l1_table_offset
;
1484 uint32_t l1_sz
= s
->snapshots
[i
].l1_size
;
1485 uint64_t *l1
= g_malloc(l1_sz
* sizeof(uint64_t));
1488 ret
= bdrv_read(bs
->file
, l1_ofs
/ BDRV_SECTOR_SIZE
, (uint8_t *)l1
,
1489 l1_sz
* sizeof(uint64_t) / BDRV_SECTOR_SIZE
);
1496 for (j
= 0; j
< l1_sz
; j
++) {
1497 if ((l1
[j
] & L1E_OFFSET_MASK
) &&
1498 overlaps_with(l1
[j
] & L1E_OFFSET_MASK
, s
->cluster_size
)) {
1500 return QCOW2_OL_INACTIVE_L2
;
1511 static const char *metadata_ol_names
[] = {
1512 [QCOW2_OL_MAIN_HEADER_BITNR
] = "qcow2_header",
1513 [QCOW2_OL_ACTIVE_L1_BITNR
] = "active L1 table",
1514 [QCOW2_OL_ACTIVE_L2_BITNR
] = "active L2 table",
1515 [QCOW2_OL_REFCOUNT_TABLE_BITNR
] = "refcount table",
1516 [QCOW2_OL_REFCOUNT_BLOCK_BITNR
] = "refcount block",
1517 [QCOW2_OL_SNAPSHOT_TABLE_BITNR
] = "snapshot table",
1518 [QCOW2_OL_INACTIVE_L1_BITNR
] = "inactive L1 table",
1519 [QCOW2_OL_INACTIVE_L2_BITNR
] = "inactive L2 table",
1523 * First performs a check for metadata overlaps (through
1524 * qcow2_check_metadata_overlap); if that fails with a negative value (error
1525 * while performing a check), that value is returned. If an impending overlap
1526 * is detected, the BDS will be made unusable, the qcow2 file marked corrupt
1527 * and -EIO returned.
1529 * Returns 0 if there were neither overlaps nor errors while checking for
1530 * overlaps; or a negative value (-errno) on error.
1532 int qcow2_pre_write_overlap_check(BlockDriverState
*bs
, int chk
, int64_t offset
,
1535 int ret
= qcow2_check_metadata_overlap(bs
, chk
, offset
, size
);
1539 } else if (ret
> 0) {
1540 int metadata_ol_bitnr
= ffs(ret
) - 1;
1544 assert(metadata_ol_bitnr
< QCOW2_OL_MAX_BITNR
);
1546 fprintf(stderr
, "qcow2: Preventing invalid write on metadata (overlaps "
1547 "with %s); image marked as corrupt.\n",
1548 metadata_ol_names
[metadata_ol_bitnr
]);
1549 message
= g_strdup_printf("Prevented %s overwrite",
1550 metadata_ol_names
[metadata_ol_bitnr
]);
1551 data
= qobject_from_jsonf("{ 'device': %s, 'msg': %s, 'offset': %"
1552 PRId64
", 'size': %" PRId64
" }", bs
->device_name
, message
,
1554 monitor_protocol_event(QEVENT_BLOCK_IMAGE_CORRUPTED
, data
);
1556 qobject_decref(data
);
1558 qcow2_mark_corrupt(bs
);
1559 bs
->drv
= NULL
; /* make BDS unusable */