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_int.h"
27 #include "block/qcow2.h"
29 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, int64_t size
);
30 static int QEMU_WARN_UNUSED_RESULT
update_refcount(BlockDriverState
*bs
,
31 int64_t offset
, int64_t length
,
35 static int cache_refcount_updates
= 0;
37 static int write_refcount_block(BlockDriverState
*bs
)
39 BDRVQcowState
*s
= bs
->opaque
;
40 size_t size
= s
->cluster_size
;
42 if (s
->refcount_block_cache_offset
== 0) {
46 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_UPDATE
);
47 if (bdrv_pwrite(bs
->file
, s
->refcount_block_cache_offset
,
48 s
->refcount_block_cache
, size
) != size
)
56 /*********************************************************/
57 /* refcount handling */
59 int qcow2_refcount_init(BlockDriverState
*bs
)
61 BDRVQcowState
*s
= bs
->opaque
;
62 int ret
, refcount_table_size2
, i
;
64 s
->refcount_block_cache
= qemu_malloc(s
->cluster_size
);
65 refcount_table_size2
= s
->refcount_table_size
* sizeof(uint64_t);
66 s
->refcount_table
= qemu_malloc(refcount_table_size2
);
67 if (s
->refcount_table_size
> 0) {
68 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_LOAD
);
69 ret
= bdrv_pread(bs
->file
, s
->refcount_table_offset
,
70 s
->refcount_table
, refcount_table_size2
);
71 if (ret
!= refcount_table_size2
)
73 for(i
= 0; i
< s
->refcount_table_size
; i
++)
74 be64_to_cpus(&s
->refcount_table
[i
]);
81 void qcow2_refcount_close(BlockDriverState
*bs
)
83 BDRVQcowState
*s
= bs
->opaque
;
84 qemu_free(s
->refcount_block_cache
);
85 qemu_free(s
->refcount_table
);
89 static int load_refcount_block(BlockDriverState
*bs
,
90 int64_t refcount_block_offset
)
92 BDRVQcowState
*s
= bs
->opaque
;
95 if (cache_refcount_updates
) {
96 write_refcount_block(bs
);
99 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_LOAD
);
100 ret
= bdrv_pread(bs
->file
, refcount_block_offset
, s
->refcount_block_cache
,
102 if (ret
!= s
->cluster_size
)
104 s
->refcount_block_cache_offset
= refcount_block_offset
;
108 static int get_refcount(BlockDriverState
*bs
, int64_t cluster_index
)
110 BDRVQcowState
*s
= bs
->opaque
;
111 int refcount_table_index
, block_index
;
112 int64_t refcount_block_offset
;
114 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
115 if (refcount_table_index
>= s
->refcount_table_size
)
117 refcount_block_offset
= s
->refcount_table
[refcount_table_index
];
118 if (!refcount_block_offset
)
120 if (refcount_block_offset
!= s
->refcount_block_cache_offset
) {
121 /* better than nothing: return allocated if read error */
122 if (load_refcount_block(bs
, refcount_block_offset
) < 0)
125 block_index
= cluster_index
&
126 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
127 return be16_to_cpu(s
->refcount_block_cache
[block_index
]);
131 * Rounds the refcount table size up to avoid growing the table for each single
132 * refcount block that is allocated.
134 static unsigned int next_refcount_table_size(BDRVQcowState
*s
,
135 unsigned int min_size
)
137 unsigned int min_clusters
= (min_size
>> (s
->cluster_bits
- 3)) + 1;
138 unsigned int refcount_table_clusters
=
139 MAX(1, s
->refcount_table_size
>> (s
->cluster_bits
- 3));
141 while (min_clusters
> refcount_table_clusters
) {
142 refcount_table_clusters
= (refcount_table_clusters
* 3 + 1) / 2;
145 return refcount_table_clusters
<< (s
->cluster_bits
- 3);
149 /* Checks if two offsets are described by the same refcount block */
150 static int in_same_refcount_block(BDRVQcowState
*s
, uint64_t offset_a
,
153 uint64_t block_a
= offset_a
>> (2 * s
->cluster_bits
- REFCOUNT_SHIFT
);
154 uint64_t block_b
= offset_b
>> (2 * s
->cluster_bits
- REFCOUNT_SHIFT
);
156 return (block_a
== block_b
);
160 * Loads a refcount block. If it doesn't exist yet, it is allocated first
161 * (including growing the refcount table if needed).
163 * Returns the offset of the refcount block on success or -errno in error case
165 static int64_t alloc_refcount_block(BlockDriverState
*bs
, int64_t cluster_index
)
167 BDRVQcowState
*s
= bs
->opaque
;
168 unsigned int refcount_table_index
;
171 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC
);
173 /* Find the refcount block for the given cluster */
174 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
176 if (refcount_table_index
< s
->refcount_table_size
) {
178 uint64_t refcount_block_offset
=
179 s
->refcount_table
[refcount_table_index
];
181 /* If it's already there, we're done */
182 if (refcount_block_offset
) {
183 if (refcount_block_offset
!= s
->refcount_block_cache_offset
) {
184 ret
= load_refcount_block(bs
, refcount_block_offset
);
189 return refcount_block_offset
;
194 * If we came here, we need to allocate something. Something is at least
195 * a cluster for the new refcount block. It may also include a new refcount
196 * table if the old refcount table is too small.
198 * Note that allocating clusters here needs some special care:
200 * - We can't use the normal qcow2_alloc_clusters(), it would try to
201 * increase the refcount and very likely we would end up with an endless
202 * recursion. Instead we must place the refcount blocks in a way that
203 * they can describe them themselves.
205 * - We need to consider that at this point we are inside update_refcounts
206 * and doing the initial refcount increase. This means that some clusters
207 * have already been allocated by the caller, but their refcount isn't
208 * accurate yet. free_cluster_index tells us where this allocation ends
209 * as long as we don't overwrite it by freeing clusters.
211 * - alloc_clusters_noref and qcow2_free_clusters may load a different
212 * refcount block into the cache
215 if (cache_refcount_updates
) {
216 ret
= write_refcount_block(bs
);
222 /* Allocate the refcount block itself and mark it as used */
223 uint64_t new_block
= alloc_clusters_noref(bs
, s
->cluster_size
);
226 fprintf(stderr
, "qcow2: Allocate refcount block %d for %" PRIx64
228 refcount_table_index
, cluster_index
<< s
->cluster_bits
, new_block
);
231 if (in_same_refcount_block(s
, new_block
, cluster_index
<< s
->cluster_bits
)) {
232 /* Zero the new refcount block before updating it */
233 memset(s
->refcount_block_cache
, 0, s
->cluster_size
);
234 s
->refcount_block_cache_offset
= new_block
;
236 /* The block describes itself, need to update the cache */
237 int block_index
= (new_block
>> s
->cluster_bits
) &
238 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
239 s
->refcount_block_cache
[block_index
] = cpu_to_be16(1);
241 /* Described somewhere else. This can recurse at most twice before we
242 * arrive at a block that describes itself. */
243 ret
= update_refcount(bs
, new_block
, s
->cluster_size
, 1);
248 /* Initialize the new refcount block only after updating its refcount,
249 * update_refcount uses the refcount cache itself */
250 memset(s
->refcount_block_cache
, 0, s
->cluster_size
);
251 s
->refcount_block_cache_offset
= new_block
;
254 /* Now the new refcount block needs to be written to disk */
255 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE
);
256 ret
= bdrv_pwrite(bs
->file
, new_block
, s
->refcount_block_cache
,
262 /* If the refcount table is big enough, just hook the block up there */
263 if (refcount_table_index
< s
->refcount_table_size
) {
264 uint64_t data64
= cpu_to_be64(new_block
);
265 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_HOOKUP
);
266 ret
= bdrv_pwrite(bs
->file
,
267 s
->refcount_table_offset
+ refcount_table_index
* sizeof(uint64_t),
268 &data64
, sizeof(data64
));
273 s
->refcount_table
[refcount_table_index
] = new_block
;
278 * If we come here, we need to grow the refcount table. Again, a new
279 * refcount table needs some space and we can't simply allocate to avoid
282 * Therefore let's grab new refcount blocks at the end of the image, which
283 * will describe themselves and the new refcount table. This way we can
284 * reference them only in the new table and do the switch to the new
285 * refcount table at once without producing an inconsistent state in
288 BLKDBG_EVENT(bs
->file
, BLKDBG_REFTABLE_GROW
);
290 /* Calculate the number of refcount blocks needed so far */
291 uint64_t refcount_block_clusters
= 1 << (s
->cluster_bits
- REFCOUNT_SHIFT
);
292 uint64_t blocks_used
= (s
->free_cluster_index
+
293 refcount_block_clusters
- 1) / refcount_block_clusters
;
295 /* And now we need at least one block more for the new metadata */
296 uint64_t table_size
= next_refcount_table_size(s
, blocks_used
+ 1);
297 uint64_t last_table_size
;
298 uint64_t blocks_clusters
;
300 uint64_t table_clusters
= size_to_clusters(s
, table_size
);
301 blocks_clusters
= 1 +
302 ((table_clusters
+ refcount_block_clusters
- 1)
303 / refcount_block_clusters
);
304 uint64_t meta_clusters
= table_clusters
+ blocks_clusters
;
306 last_table_size
= table_size
;
307 table_size
= next_refcount_table_size(s
, blocks_used
+
308 ((meta_clusters
+ refcount_block_clusters
- 1)
309 / refcount_block_clusters
));
311 } while (last_table_size
!= table_size
);
314 fprintf(stderr
, "qcow2: Grow refcount table %" PRId32
" => %" PRId64
"\n",
315 s
->refcount_table_size
, table_size
);
318 /* Create the new refcount table and blocks */
319 uint64_t meta_offset
= (blocks_used
* refcount_block_clusters
) *
321 uint64_t table_offset
= meta_offset
+ blocks_clusters
* s
->cluster_size
;
322 uint16_t *new_blocks
= qemu_mallocz(blocks_clusters
* s
->cluster_size
);
323 uint64_t *new_table
= qemu_mallocz(table_size
* sizeof(uint64_t));
325 assert(meta_offset
>= (s
->free_cluster_index
* s
->cluster_size
));
327 /* Fill the new refcount table */
328 memcpy(new_table
, s
->refcount_table
,
329 s
->refcount_table_size
* sizeof(uint64_t));
330 new_table
[refcount_table_index
] = new_block
;
333 for (i
= 0; i
< blocks_clusters
; i
++) {
334 new_table
[blocks_used
+ i
] = meta_offset
+ (i
* s
->cluster_size
);
337 /* Fill the refcount blocks */
338 uint64_t table_clusters
= size_to_clusters(s
, table_size
* sizeof(uint64_t));
340 for (i
= 0; i
< table_clusters
+ blocks_clusters
; i
++) {
341 new_blocks
[block
++] = cpu_to_be16(1);
344 /* Write refcount blocks to disk */
345 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS
);
346 ret
= bdrv_pwrite(bs
->file
, meta_offset
, new_blocks
,
347 blocks_clusters
* s
->cluster_size
);
348 qemu_free(new_blocks
);
353 /* Write refcount table to disk */
354 for(i
= 0; i
< table_size
; i
++) {
355 cpu_to_be64s(&new_table
[i
]);
358 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE
);
359 ret
= bdrv_pwrite(bs
->file
, table_offset
, new_table
,
360 table_size
* sizeof(uint64_t));
365 for(i
= 0; i
< table_size
; i
++) {
366 cpu_to_be64s(&new_table
[i
]);
369 /* Hook up the new refcount table in the qcow2 header */
371 cpu_to_be64w((uint64_t*)data
, table_offset
);
372 cpu_to_be32w((uint32_t*)(data
+ 8), table_clusters
);
373 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE
);
374 ret
= bdrv_pwrite(bs
->file
, offsetof(QCowHeader
, refcount_table_offset
),
380 /* And switch it in memory */
381 uint64_t old_table_offset
= s
->refcount_table_offset
;
382 uint64_t old_table_size
= s
->refcount_table_size
;
384 qemu_free(s
->refcount_table
);
385 s
->refcount_table
= new_table
;
386 s
->refcount_table_size
= table_size
;
387 s
->refcount_table_offset
= table_offset
;
389 /* Free old table. Remember, we must not change free_cluster_index */
390 uint64_t old_free_cluster_index
= s
->free_cluster_index
;
391 qcow2_free_clusters(bs
, old_table_offset
, old_table_size
* sizeof(uint64_t));
392 s
->free_cluster_index
= old_free_cluster_index
;
394 ret
= load_refcount_block(bs
, new_block
);
402 qemu_free(new_table
);
404 s
->refcount_block_cache_offset
= 0;
408 #define REFCOUNTS_PER_SECTOR (512 >> REFCOUNT_SHIFT)
409 static int write_refcount_block_entries(BlockDriverState
*bs
,
410 int64_t refcount_block_offset
, int first_index
, int last_index
)
412 BDRVQcowState
*s
= bs
->opaque
;
416 if (cache_refcount_updates
) {
420 if (first_index
< 0) {
424 first_index
&= ~(REFCOUNTS_PER_SECTOR
- 1);
425 last_index
= (last_index
+ REFCOUNTS_PER_SECTOR
)
426 & ~(REFCOUNTS_PER_SECTOR
- 1);
428 size
= (last_index
- first_index
) << REFCOUNT_SHIFT
;
430 BLKDBG_EVENT(bs
->file
, BLKDBG_REFBLOCK_UPDATE_PART
);
431 ret
= bdrv_pwrite(bs
->file
,
432 refcount_block_offset
+ (first_index
<< REFCOUNT_SHIFT
),
433 &s
->refcount_block_cache
[first_index
], size
);
441 /* XXX: cache several refcount block clusters ? */
442 static int QEMU_WARN_UNUSED_RESULT
update_refcount(BlockDriverState
*bs
,
443 int64_t offset
, int64_t length
, int addend
)
445 BDRVQcowState
*s
= bs
->opaque
;
446 int64_t start
, last
, cluster_offset
;
447 int64_t refcount_block_offset
= 0;
448 int64_t table_index
= -1, old_table_index
;
449 int first_index
= -1, last_index
= -1;
453 printf("update_refcount: offset=%" PRId64
" size=%" PRId64
" addend=%d\n",
454 offset
, length
, addend
);
458 } else if (length
== 0) {
462 start
= offset
& ~(s
->cluster_size
- 1);
463 last
= (offset
+ length
- 1) & ~(s
->cluster_size
- 1);
464 for(cluster_offset
= start
; cluster_offset
<= last
;
465 cluster_offset
+= s
->cluster_size
)
467 int block_index
, refcount
;
468 int64_t cluster_index
= cluster_offset
>> s
->cluster_bits
;
471 /* Only write refcount block to disk when we are done with it */
472 old_table_index
= table_index
;
473 table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
474 if ((old_table_index
>= 0) && (table_index
!= old_table_index
)) {
476 ret
= write_refcount_block_entries(bs
, refcount_block_offset
,
477 first_index
, last_index
);
486 /* Load the refcount block and allocate it if needed */
487 new_block
= alloc_refcount_block(bs
, cluster_index
);
492 refcount_block_offset
= new_block
;
494 /* we can update the count and save it */
495 block_index
= cluster_index
&
496 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
497 if (first_index
== -1 || block_index
< first_index
) {
498 first_index
= block_index
;
500 if (block_index
> last_index
) {
501 last_index
= block_index
;
504 refcount
= be16_to_cpu(s
->refcount_block_cache
[block_index
]);
506 if (refcount
< 0 || refcount
> 0xffff) {
510 if (refcount
== 0 && cluster_index
< s
->free_cluster_index
) {
511 s
->free_cluster_index
= cluster_index
;
513 s
->refcount_block_cache
[block_index
] = cpu_to_be16(refcount
);
519 /* Write last changed block to disk */
520 if (refcount_block_offset
!= 0) {
522 wret
= write_refcount_block_entries(bs
, refcount_block_offset
,
523 first_index
, last_index
);
525 return ret
< 0 ? ret
: wret
;
530 * Try do undo any updates if an error is returned (This may succeed in
531 * some cases like ENOSPC for allocating a new refcount block)
535 dummy
= update_refcount(bs
, offset
, cluster_offset
- offset
, -addend
);
541 /* addend must be 1 or -1 */
542 static int update_cluster_refcount(BlockDriverState
*bs
,
543 int64_t cluster_index
,
546 BDRVQcowState
*s
= bs
->opaque
;
549 ret
= update_refcount(bs
, cluster_index
<< s
->cluster_bits
, 1, addend
);
554 return get_refcount(bs
, cluster_index
);
559 /*********************************************************/
560 /* cluster allocation functions */
564 /* return < 0 if error */
565 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, int64_t size
)
567 BDRVQcowState
*s
= bs
->opaque
;
570 nb_clusters
= size_to_clusters(s
, size
);
572 for(i
= 0; i
< nb_clusters
; i
++) {
573 int64_t next_cluster_index
= s
->free_cluster_index
++;
574 if (get_refcount(bs
, next_cluster_index
) != 0)
578 printf("alloc_clusters: size=%" PRId64
" -> %" PRId64
"\n",
580 (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
);
582 return (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
;
585 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, int64_t size
)
590 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC
);
591 offset
= alloc_clusters_noref(bs
, size
);
592 ret
= update_refcount(bs
, offset
, size
, 1);
599 /* only used to allocate compressed sectors. We try to allocate
600 contiguous sectors. size must be <= cluster_size */
601 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
)
603 BDRVQcowState
*s
= bs
->opaque
;
604 int64_t offset
, cluster_offset
;
607 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_ALLOC_BYTES
);
608 assert(size
> 0 && size
<= s
->cluster_size
);
609 if (s
->free_byte_offset
== 0) {
610 s
->free_byte_offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
611 if (s
->free_byte_offset
< 0) {
612 return s
->free_byte_offset
;
616 free_in_cluster
= s
->cluster_size
-
617 (s
->free_byte_offset
& (s
->cluster_size
- 1));
618 if (size
<= free_in_cluster
) {
619 /* enough space in current cluster */
620 offset
= s
->free_byte_offset
;
621 s
->free_byte_offset
+= size
;
622 free_in_cluster
-= size
;
623 if (free_in_cluster
== 0)
624 s
->free_byte_offset
= 0;
625 if ((offset
& (s
->cluster_size
- 1)) != 0)
626 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
628 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
632 cluster_offset
= s
->free_byte_offset
& ~(s
->cluster_size
- 1);
633 if ((cluster_offset
+ s
->cluster_size
) == offset
) {
634 /* we are lucky: contiguous data */
635 offset
= s
->free_byte_offset
;
636 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
637 s
->free_byte_offset
+= size
;
639 s
->free_byte_offset
= offset
;
646 void qcow2_free_clusters(BlockDriverState
*bs
,
647 int64_t offset
, int64_t size
)
651 BLKDBG_EVENT(bs
->file
, BLKDBG_CLUSTER_FREE
);
652 ret
= update_refcount(bs
, offset
, size
, -1);
654 fprintf(stderr
, "qcow2_free_clusters failed: %s\n", strerror(-ret
));
655 /* TODO Remember the clusters to free them later and avoid leaking */
662 * free clusters according to its type: compressed or not
666 void qcow2_free_any_clusters(BlockDriverState
*bs
,
667 uint64_t cluster_offset
, int nb_clusters
)
669 BDRVQcowState
*s
= bs
->opaque
;
671 /* free the cluster */
673 if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
675 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) &
677 qcow2_free_clusters(bs
,
678 (cluster_offset
& s
->cluster_offset_mask
) & ~511,
683 qcow2_free_clusters(bs
, cluster_offset
, nb_clusters
<< s
->cluster_bits
);
690 /*********************************************************/
691 /* snapshots and image creation */
695 void qcow2_create_refcount_update(QCowCreateState
*s
, int64_t offset
,
699 int64_t start
, last
, cluster_offset
;
702 start
= offset
& ~(s
->cluster_size
- 1);
703 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
704 for(cluster_offset
= start
; cluster_offset
<= last
;
705 cluster_offset
+= s
->cluster_size
) {
706 p
= &s
->refcount_block
[cluster_offset
>> s
->cluster_bits
];
707 refcount
= be16_to_cpu(*p
);
709 *p
= cpu_to_be16(refcount
);
713 /* update the refcounts of snapshots and the copied flag */
714 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
715 int64_t l1_table_offset
, int l1_size
, int addend
)
717 BDRVQcowState
*s
= bs
->opaque
;
718 uint64_t *l1_table
, *l2_table
, l2_offset
, offset
, l1_size2
, l1_allocated
;
719 int64_t old_offset
, old_l2_offset
;
720 int l2_size
, i
, j
, l1_modified
, l2_modified
, nb_csectors
, refcount
;
722 qcow2_l2_cache_reset(bs
);
723 cache_refcount_updates
= 1;
727 l1_size2
= l1_size
* sizeof(uint64_t);
728 if (l1_table_offset
!= s
->l1_table_offset
) {
730 l1_table
= qemu_mallocz(align_offset(l1_size2
, 512));
735 if (bdrv_pread(bs
->file
, l1_table_offset
,
736 l1_table
, l1_size2
) != l1_size2
)
738 for(i
= 0;i
< l1_size
; i
++)
739 be64_to_cpus(&l1_table
[i
]);
741 assert(l1_size
== s
->l1_size
);
742 l1_table
= s
->l1_table
;
746 l2_size
= s
->l2_size
* sizeof(uint64_t);
747 l2_table
= qemu_malloc(l2_size
);
749 for(i
= 0; i
< l1_size
; i
++) {
750 l2_offset
= l1_table
[i
];
752 old_l2_offset
= l2_offset
;
753 l2_offset
&= ~QCOW_OFLAG_COPIED
;
755 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
, l2_size
) != l2_size
)
757 for(j
= 0; j
< s
->l2_size
; j
++) {
758 offset
= be64_to_cpu(l2_table
[j
]);
761 offset
&= ~QCOW_OFLAG_COPIED
;
762 if (offset
& QCOW_OFLAG_COMPRESSED
) {
763 nb_csectors
= ((offset
>> s
->csize_shift
) &
767 ret
= update_refcount(bs
,
768 (offset
& s
->cluster_offset_mask
) & ~511,
769 nb_csectors
* 512, addend
);
774 /* compressed clusters are never modified */
778 refcount
= update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, addend
);
780 refcount
= get_refcount(bs
, offset
>> s
->cluster_bits
);
785 offset
|= QCOW_OFLAG_COPIED
;
787 if (offset
!= old_offset
) {
788 l2_table
[j
] = cpu_to_be64(offset
);
794 if (bdrv_pwrite(bs
->file
,
795 l2_offset
, l2_table
, l2_size
) != l2_size
)
800 refcount
= update_cluster_refcount(bs
, l2_offset
>> s
->cluster_bits
, addend
);
802 refcount
= get_refcount(bs
, l2_offset
>> s
->cluster_bits
);
805 l2_offset
|= QCOW_OFLAG_COPIED
;
807 if (l2_offset
!= old_l2_offset
) {
808 l1_table
[i
] = l2_offset
;
814 for(i
= 0; i
< l1_size
; i
++)
815 cpu_to_be64s(&l1_table
[i
]);
816 if (bdrv_pwrite(bs
->file
, l1_table_offset
, l1_table
,
817 l1_size2
) != l1_size2
)
819 for(i
= 0; i
< l1_size
; i
++)
820 be64_to_cpus(&l1_table
[i
]);
825 cache_refcount_updates
= 0;
826 write_refcount_block(bs
);
832 cache_refcount_updates
= 0;
833 write_refcount_block(bs
);
840 /*********************************************************/
841 /* refcount checking functions */
846 * Increases the refcount for a range of clusters in a given refcount table.
847 * This is used to construct a temporary refcount table out of L1 and L2 tables
848 * which can be compared the the refcount table saved in the image.
850 * Returns the number of errors in the image that were found
852 static int inc_refcounts(BlockDriverState
*bs
,
853 uint16_t *refcount_table
,
854 int refcount_table_size
,
855 int64_t offset
, int64_t size
)
857 BDRVQcowState
*s
= bs
->opaque
;
858 int64_t start
, last
, cluster_offset
;
865 start
= offset
& ~(s
->cluster_size
- 1);
866 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
867 for(cluster_offset
= start
; cluster_offset
<= last
;
868 cluster_offset
+= s
->cluster_size
) {
869 k
= cluster_offset
>> s
->cluster_bits
;
870 if (k
< 0 || k
>= refcount_table_size
) {
871 fprintf(stderr
, "ERROR: invalid cluster offset=0x%" PRIx64
"\n",
875 if (++refcount_table
[k
] == 0) {
876 fprintf(stderr
, "ERROR: overflow cluster offset=0x%" PRIx64
877 "\n", cluster_offset
);
887 * Increases the refcount in the given refcount table for the all clusters
888 * referenced in the L2 table. While doing so, performs some checks on L2
891 * Returns the number of errors found by the checks or -errno if an internal
894 static int check_refcounts_l2(BlockDriverState
*bs
,
895 uint16_t *refcount_table
, int refcount_table_size
, int64_t l2_offset
,
898 BDRVQcowState
*s
= bs
->opaque
;
899 uint64_t *l2_table
, offset
;
900 int i
, l2_size
, nb_csectors
, refcount
;
903 /* Read L2 table from disk */
904 l2_size
= s
->l2_size
* sizeof(uint64_t);
905 l2_table
= qemu_malloc(l2_size
);
907 if (bdrv_pread(bs
->file
, l2_offset
, l2_table
, l2_size
) != l2_size
)
910 /* Do the actual checks */
911 for(i
= 0; i
< s
->l2_size
; i
++) {
912 offset
= be64_to_cpu(l2_table
[i
]);
914 if (offset
& QCOW_OFLAG_COMPRESSED
) {
915 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
916 if (offset
& QCOW_OFLAG_COPIED
) {
917 fprintf(stderr
, "ERROR: cluster %" PRId64
": "
918 "copied flag must never be set for compressed "
919 "clusters\n", offset
>> s
->cluster_bits
);
920 offset
&= ~QCOW_OFLAG_COPIED
;
924 /* Mark cluster as used */
925 nb_csectors
= ((offset
>> s
->csize_shift
) &
927 offset
&= s
->cluster_offset_mask
;
928 errors
+= inc_refcounts(bs
, refcount_table
,
930 offset
& ~511, nb_csectors
* 512);
932 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
934 uint64_t entry
= offset
;
935 offset
&= ~QCOW_OFLAG_COPIED
;
936 refcount
= get_refcount(bs
, offset
>> s
->cluster_bits
);
937 if ((refcount
== 1) != ((entry
& QCOW_OFLAG_COPIED
) != 0)) {
938 fprintf(stderr
, "ERROR OFLAG_COPIED: offset=%"
939 PRIx64
" refcount=%d\n", entry
, refcount
);
944 /* Mark cluster as used */
945 offset
&= ~QCOW_OFLAG_COPIED
;
946 errors
+= inc_refcounts(bs
, refcount_table
,
948 offset
, s
->cluster_size
);
950 /* Correct offsets are cluster aligned */
951 if (offset
& (s
->cluster_size
- 1)) {
952 fprintf(stderr
, "ERROR offset=%" PRIx64
": Cluster is not "
953 "properly aligned; L2 entry corrupted.\n", offset
);
964 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
970 * Increases the refcount for the L1 table, its L2 tables and all referenced
971 * clusters in the given refcount table. While doing so, performs some checks
972 * on L1 and L2 entries.
974 * Returns the number of errors found by the checks or -errno if an internal
977 static int check_refcounts_l1(BlockDriverState
*bs
,
978 uint16_t *refcount_table
,
979 int refcount_table_size
,
980 int64_t l1_table_offset
, int l1_size
,
983 BDRVQcowState
*s
= bs
->opaque
;
984 uint64_t *l1_table
, l2_offset
, l1_size2
;
985 int i
, refcount
, ret
;
988 l1_size2
= l1_size
* sizeof(uint64_t);
990 /* Mark L1 table as used */
991 errors
+= inc_refcounts(bs
, refcount_table
, refcount_table_size
,
992 l1_table_offset
, l1_size2
);
994 /* Read L1 table entries from disk */
998 l1_table
= qemu_malloc(l1_size2
);
999 if (bdrv_pread(bs
->file
, l1_table_offset
,
1000 l1_table
, l1_size2
) != l1_size2
)
1002 for(i
= 0;i
< l1_size
; i
++)
1003 be64_to_cpus(&l1_table
[i
]);
1006 /* Do the actual checks */
1007 for(i
= 0; i
< l1_size
; i
++) {
1008 l2_offset
= l1_table
[i
];
1010 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
1012 refcount
= get_refcount(bs
, (l2_offset
& ~QCOW_OFLAG_COPIED
)
1013 >> s
->cluster_bits
);
1014 if ((refcount
== 1) != ((l2_offset
& QCOW_OFLAG_COPIED
) != 0)) {
1015 fprintf(stderr
, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
1016 " refcount=%d\n", l2_offset
, refcount
);
1021 /* Mark L2 table as used */
1022 l2_offset
&= ~QCOW_OFLAG_COPIED
;
1023 errors
+= inc_refcounts(bs
, refcount_table
,
1024 refcount_table_size
,
1028 /* L2 tables are cluster aligned */
1029 if (l2_offset
& (s
->cluster_size
- 1)) {
1030 fprintf(stderr
, "ERROR l2_offset=%" PRIx64
": Table is not "
1031 "cluster aligned; L1 entry corrupted\n", l2_offset
);
1035 /* Process and check L2 entries */
1036 ret
= check_refcounts_l2(bs
, refcount_table
, refcount_table_size
,
1037 l2_offset
, check_copied
);
1044 qemu_free(l1_table
);
1048 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
1049 qemu_free(l1_table
);
1054 * Checks an image for refcount consistency.
1056 * Returns 0 if no errors are found, the number of errors in case the image is
1057 * detected as corrupted, and -errno when an internal error occured.
1059 int qcow2_check_refcounts(BlockDriverState
*bs
)
1061 BDRVQcowState
*s
= bs
->opaque
;
1063 int nb_clusters
, refcount1
, refcount2
, i
;
1065 uint16_t *refcount_table
;
1066 int ret
, errors
= 0;
1068 size
= bdrv_getlength(bs
->file
);
1069 nb_clusters
= size_to_clusters(s
, size
);
1070 refcount_table
= qemu_mallocz(nb_clusters
* sizeof(uint16_t));
1073 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
1074 0, s
->cluster_size
);
1076 /* current L1 table */
1077 ret
= check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
1078 s
->l1_table_offset
, s
->l1_size
, 1);
1085 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1086 sn
= s
->snapshots
+ i
;
1087 check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
1088 sn
->l1_table_offset
, sn
->l1_size
, 0);
1090 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
1091 s
->snapshots_offset
, s
->snapshots_size
);
1094 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
1095 s
->refcount_table_offset
,
1096 s
->refcount_table_size
* sizeof(uint64_t));
1097 for(i
= 0; i
< s
->refcount_table_size
; i
++) {
1099 offset
= s
->refcount_table
[i
];
1101 /* Refcount blocks are cluster aligned */
1102 if (offset
& (s
->cluster_size
- 1)) {
1103 fprintf(stderr
, "ERROR refcount block %d is not "
1104 "cluster aligned; refcount table entry corrupted\n", i
);
1109 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
1110 offset
, s
->cluster_size
);
1111 if (refcount_table
[offset
/ s
->cluster_size
] != 1) {
1112 fprintf(stderr
, "ERROR refcount block %d refcount=%d\n",
1113 i
, refcount_table
[offset
/ s
->cluster_size
]);
1118 /* compare ref counts */
1119 for(i
= 0; i
< nb_clusters
; i
++) {
1120 refcount1
= get_refcount(bs
, i
);
1121 refcount2
= refcount_table
[i
];
1122 if (refcount1
!= refcount2
) {
1123 fprintf(stderr
, "ERROR cluster %d refcount=%d reference=%d\n",
1124 i
, refcount1
, refcount2
);
1129 qemu_free(refcount_table
);