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(BDRVQcowState
*s
)
39 size_t size
= s
->cluster_size
;
41 if (s
->refcount_block_cache_offset
== 0) {
45 BLKDBG_EVENT(s
->hd
, BLKDBG_REFBLOCK_UPDATE
);
46 if (bdrv_pwrite(s
->hd
, s
->refcount_block_cache_offset
,
47 s
->refcount_block_cache
, size
) != size
)
55 /*********************************************************/
56 /* refcount handling */
58 int qcow2_refcount_init(BlockDriverState
*bs
)
60 BDRVQcowState
*s
= bs
->opaque
;
61 int ret
, refcount_table_size2
, i
;
63 s
->refcount_block_cache
= qemu_malloc(s
->cluster_size
);
64 refcount_table_size2
= s
->refcount_table_size
* sizeof(uint64_t);
65 s
->refcount_table
= qemu_malloc(refcount_table_size2
);
66 if (s
->refcount_table_size
> 0) {
67 BLKDBG_EVENT(s
->hd
, BLKDBG_REFTABLE_LOAD
);
68 ret
= bdrv_pread(s
->hd
, s
->refcount_table_offset
,
69 s
->refcount_table
, refcount_table_size2
);
70 if (ret
!= refcount_table_size2
)
72 for(i
= 0; i
< s
->refcount_table_size
; i
++)
73 be64_to_cpus(&s
->refcount_table
[i
]);
80 void qcow2_refcount_close(BlockDriverState
*bs
)
82 BDRVQcowState
*s
= bs
->opaque
;
83 qemu_free(s
->refcount_block_cache
);
84 qemu_free(s
->refcount_table
);
88 static int load_refcount_block(BlockDriverState
*bs
,
89 int64_t refcount_block_offset
)
91 BDRVQcowState
*s
= bs
->opaque
;
94 if (cache_refcount_updates
) {
95 write_refcount_block(s
);
98 BLKDBG_EVENT(s
->hd
, BLKDBG_REFBLOCK_LOAD
);
99 ret
= bdrv_pread(s
->hd
, refcount_block_offset
, s
->refcount_block_cache
,
101 if (ret
!= s
->cluster_size
)
103 s
->refcount_block_cache_offset
= refcount_block_offset
;
107 static int get_refcount(BlockDriverState
*bs
, int64_t cluster_index
)
109 BDRVQcowState
*s
= bs
->opaque
;
110 int refcount_table_index
, block_index
;
111 int64_t refcount_block_offset
;
113 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
114 if (refcount_table_index
>= s
->refcount_table_size
)
116 refcount_block_offset
= s
->refcount_table
[refcount_table_index
];
117 if (!refcount_block_offset
)
119 if (refcount_block_offset
!= s
->refcount_block_cache_offset
) {
120 /* better than nothing: return allocated if read error */
121 if (load_refcount_block(bs
, refcount_block_offset
) < 0)
124 block_index
= cluster_index
&
125 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
126 return be16_to_cpu(s
->refcount_block_cache
[block_index
]);
130 * Rounds the refcount table size up to avoid growing the table for each single
131 * refcount block that is allocated.
133 static unsigned int next_refcount_table_size(BDRVQcowState
*s
,
134 unsigned int min_size
)
136 unsigned int min_clusters
= (min_size
>> (s
->cluster_bits
- 3)) + 1;
137 unsigned int refcount_table_clusters
=
138 MAX(1, s
->refcount_table_size
>> (s
->cluster_bits
- 3));
140 while (min_clusters
> refcount_table_clusters
) {
141 refcount_table_clusters
= (refcount_table_clusters
* 3 + 1) / 2;
144 return refcount_table_clusters
<< (s
->cluster_bits
- 3);
148 /* Checks if two offsets are described by the same refcount block */
149 static int in_same_refcount_block(BDRVQcowState
*s
, uint64_t offset_a
,
152 uint64_t block_a
= offset_a
>> (2 * s
->cluster_bits
- REFCOUNT_SHIFT
);
153 uint64_t block_b
= offset_b
>> (2 * s
->cluster_bits
- REFCOUNT_SHIFT
);
155 return (block_a
== block_b
);
159 * Loads a refcount block. If it doesn't exist yet, it is allocated first
160 * (including growing the refcount table if needed).
162 * Returns the offset of the refcount block on success or -errno in error case
164 static int64_t alloc_refcount_block(BlockDriverState
*bs
, int64_t cluster_index
)
166 BDRVQcowState
*s
= bs
->opaque
;
167 unsigned int refcount_table_index
;
170 BLKDBG_EVENT(s
->hd
, BLKDBG_REFBLOCK_ALLOC
);
172 /* Find the refcount block for the given cluster */
173 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
175 if (refcount_table_index
< s
->refcount_table_size
) {
177 uint64_t refcount_block_offset
=
178 s
->refcount_table
[refcount_table_index
];
180 /* If it's already there, we're done */
181 if (refcount_block_offset
) {
182 if (refcount_block_offset
!= s
->refcount_block_cache_offset
) {
183 ret
= load_refcount_block(bs
, refcount_block_offset
);
188 return refcount_block_offset
;
193 * If we came here, we need to allocate something. Something is at least
194 * a cluster for the new refcount block. It may also include a new refcount
195 * table if the old refcount table is too small.
197 * Note that allocating clusters here needs some special care:
199 * - We can't use the normal qcow2_alloc_clusters(), it would try to
200 * increase the refcount and very likely we would end up with an endless
201 * recursion. Instead we must place the refcount blocks in a way that
202 * they can describe them themselves.
204 * - We need to consider that at this point we are inside update_refcounts
205 * and doing the initial refcount increase. This means that some clusters
206 * have already been allocated by the caller, but their refcount isn't
207 * accurate yet. free_cluster_index tells us where this allocation ends
208 * as long as we don't overwrite it by freeing clusters.
210 * - alloc_clusters_noref and qcow2_free_clusters may load a different
211 * refcount block into the cache
214 if (cache_refcount_updates
) {
215 ret
= write_refcount_block(s
);
221 /* Allocate the refcount block itself and mark it as used */
222 uint64_t new_block
= alloc_clusters_noref(bs
, s
->cluster_size
);
223 memset(s
->refcount_block_cache
, 0, s
->cluster_size
);
224 s
->refcount_block_cache_offset
= new_block
;
227 fprintf(stderr
, "qcow2: Allocate refcount block %d for %" PRIx64
229 refcount_table_index
, cluster_index
<< s
->cluster_bits
, new_block
);
232 if (in_same_refcount_block(s
, new_block
, cluster_index
<< s
->cluster_bits
)) {
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 s
->refcount_block_cache
[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);
246 /* Now the new refcount block needs to be written to disk */
247 BLKDBG_EVENT(s
->hd
, BLKDBG_REFBLOCK_ALLOC_WRITE
);
248 ret
= bdrv_pwrite(s
->hd
, new_block
, s
->refcount_block_cache
,
254 /* If the refcount table is big enough, just hook the block up there */
255 if (refcount_table_index
< s
->refcount_table_size
) {
256 uint64_t data64
= cpu_to_be64(new_block
);
257 BLKDBG_EVENT(s
->hd
, BLKDBG_REFBLOCK_ALLOC_HOOKUP
);
258 ret
= bdrv_pwrite(s
->hd
,
259 s
->refcount_table_offset
+ refcount_table_index
* sizeof(uint64_t),
260 &data64
, sizeof(data64
));
265 s
->refcount_table
[refcount_table_index
] = new_block
;
270 * If we come here, we need to grow the refcount table. Again, a new
271 * refcount table needs some space and we can't simply allocate to avoid
274 * Therefore let's grab new refcount blocks at the end of the image, which
275 * will describe themselves and the new refcount table. This way we can
276 * reference them only in the new table and do the switch to the new
277 * refcount table at once without producing an inconsistent state in
280 BLKDBG_EVENT(s
->hd
, BLKDBG_REFTABLE_GROW
);
282 /* Calculate the number of refcount blocks needed so far */
283 uint64_t refcount_block_clusters
= 1 << (s
->cluster_bits
- REFCOUNT_SHIFT
);
284 uint64_t blocks_used
= (s
->free_cluster_index
+
285 refcount_block_clusters
- 1) / refcount_block_clusters
;
287 /* And now we need at least one block more for the new metadata */
288 uint64_t table_size
= next_refcount_table_size(s
, blocks_used
+ 1);
289 uint64_t last_table_size
;
290 uint64_t blocks_clusters
;
292 uint64_t table_clusters
= size_to_clusters(s
, table_size
);
293 blocks_clusters
= 1 +
294 ((table_clusters
+ refcount_block_clusters
- 1)
295 / refcount_block_clusters
);
296 uint64_t meta_clusters
= table_clusters
+ blocks_clusters
;
298 last_table_size
= table_size
;
299 table_size
= next_refcount_table_size(s
, blocks_used
+
300 ((meta_clusters
+ refcount_block_clusters
- 1)
301 / refcount_block_clusters
));
303 } while (last_table_size
!= table_size
);
306 fprintf(stderr
, "qcow2: Grow refcount table %" PRId32
" => %" PRId64
"\n",
307 s
->refcount_table_size
, table_size
);
310 /* Create the new refcount table and blocks */
311 uint64_t meta_offset
= (blocks_used
* refcount_block_clusters
) *
313 uint64_t table_offset
= meta_offset
+ blocks_clusters
* s
->cluster_size
;
314 uint16_t *new_blocks
= qemu_mallocz(blocks_clusters
* s
->cluster_size
);
315 uint64_t *new_table
= qemu_mallocz(table_size
* sizeof(uint64_t));
317 assert(meta_offset
>= (s
->free_cluster_index
* s
->cluster_size
));
319 /* Fill the new refcount table */
320 memcpy(new_table
, s
->refcount_table
,
321 s
->refcount_table_size
* sizeof(uint64_t));
322 new_table
[refcount_table_index
] = new_block
;
325 for (i
= 0; i
< blocks_clusters
; i
++) {
326 new_table
[blocks_used
+ i
] = meta_offset
+ (i
* s
->cluster_size
);
329 /* Fill the refcount blocks */
330 uint64_t table_clusters
= size_to_clusters(s
, table_size
* sizeof(uint64_t));
332 for (i
= 0; i
< table_clusters
+ blocks_clusters
; i
++) {
333 new_blocks
[block
++] = cpu_to_be16(1);
336 /* Write refcount blocks to disk */
337 BLKDBG_EVENT(s
->hd
, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS
);
338 ret
= bdrv_pwrite(s
->hd
, meta_offset
, new_blocks
,
339 blocks_clusters
* s
->cluster_size
);
340 qemu_free(new_blocks
);
345 /* Write refcount table to disk */
346 for(i
= 0; i
< table_size
; i
++) {
347 cpu_to_be64s(&new_table
[i
]);
350 BLKDBG_EVENT(s
->hd
, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE
);
351 ret
= bdrv_pwrite(s
->hd
, table_offset
, new_table
,
352 table_size
* sizeof(uint64_t));
357 for(i
= 0; i
< table_size
; i
++) {
358 cpu_to_be64s(&new_table
[i
]);
361 /* Hook up the new refcount table in the qcow2 header */
363 cpu_to_be64w((uint64_t*)data
, table_offset
);
364 cpu_to_be32w((uint32_t*)(data
+ 8), table_clusters
);
365 BLKDBG_EVENT(s
->hd
, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE
);
366 ret
= bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, refcount_table_offset
),
372 /* And switch it in memory */
373 uint64_t old_table_offset
= s
->refcount_table_offset
;
374 uint64_t old_table_size
= s
->refcount_table_size
;
376 qemu_free(s
->refcount_table
);
377 s
->refcount_table
= new_table
;
378 s
->refcount_table_size
= table_size
;
379 s
->refcount_table_offset
= table_offset
;
381 /* Free old table. Remember, we must not change free_cluster_index */
382 uint64_t old_free_cluster_index
= s
->free_cluster_index
;
383 qcow2_free_clusters(bs
, old_table_offset
, old_table_size
* sizeof(uint64_t));
384 s
->free_cluster_index
= old_free_cluster_index
;
386 ret
= load_refcount_block(bs
, new_block
);
394 qemu_free(new_table
);
396 s
->refcount_block_cache_offset
= 0;
400 #define REFCOUNTS_PER_SECTOR (512 >> REFCOUNT_SHIFT)
401 static int write_refcount_block_entries(BDRVQcowState
*s
,
402 int64_t refcount_block_offset
, int first_index
, int last_index
)
406 if (cache_refcount_updates
) {
410 first_index
&= ~(REFCOUNTS_PER_SECTOR
- 1);
411 last_index
= (last_index
+ REFCOUNTS_PER_SECTOR
)
412 & ~(REFCOUNTS_PER_SECTOR
- 1);
414 size
= (last_index
- first_index
) << REFCOUNT_SHIFT
;
415 BLKDBG_EVENT(s
->hd
, BLKDBG_REFBLOCK_UPDATE_PART
);
416 if (bdrv_pwrite(s
->hd
,
417 refcount_block_offset
+ (first_index
<< REFCOUNT_SHIFT
),
418 &s
->refcount_block_cache
[first_index
], size
) != size
)
426 /* XXX: cache several refcount block clusters ? */
427 static int QEMU_WARN_UNUSED_RESULT
update_refcount(BlockDriverState
*bs
,
428 int64_t offset
, int64_t length
, int addend
)
430 BDRVQcowState
*s
= bs
->opaque
;
431 int64_t start
, last
, cluster_offset
;
432 int64_t refcount_block_offset
= 0;
433 int64_t table_index
= -1, old_table_index
;
434 int first_index
= -1, last_index
= -1;
438 printf("update_refcount: offset=%" PRId64
" size=%" PRId64
" addend=%d\n",
439 offset
, length
, addend
);
443 } else if (length
== 0) {
447 start
= offset
& ~(s
->cluster_size
- 1);
448 last
= (offset
+ length
- 1) & ~(s
->cluster_size
- 1);
449 for(cluster_offset
= start
; cluster_offset
<= last
;
450 cluster_offset
+= s
->cluster_size
)
452 int block_index
, refcount
;
453 int64_t cluster_index
= cluster_offset
>> s
->cluster_bits
;
456 /* Only write refcount block to disk when we are done with it */
457 old_table_index
= table_index
;
458 table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
459 if ((old_table_index
>= 0) && (table_index
!= old_table_index
)) {
461 if (write_refcount_block_entries(s
, refcount_block_offset
,
462 first_index
, last_index
) < 0)
471 /* Load the refcount block and allocate it if needed */
472 new_block
= alloc_refcount_block(bs
, cluster_index
);
477 refcount_block_offset
= new_block
;
479 /* we can update the count and save it */
480 block_index
= cluster_index
&
481 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
482 if (first_index
== -1 || block_index
< first_index
) {
483 first_index
= block_index
;
485 if (block_index
> last_index
) {
486 last_index
= block_index
;
489 refcount
= be16_to_cpu(s
->refcount_block_cache
[block_index
]);
491 if (refcount
< 0 || refcount
> 0xffff) {
495 if (refcount
== 0 && cluster_index
< s
->free_cluster_index
) {
496 s
->free_cluster_index
= cluster_index
;
498 s
->refcount_block_cache
[block_index
] = cpu_to_be16(refcount
);
504 /* Write last changed block to disk */
505 if (refcount_block_offset
!= 0) {
506 if (write_refcount_block_entries(s
, refcount_block_offset
,
507 first_index
, last_index
) < 0)
509 return ret
< 0 ? ret
: -EIO
;
514 * Try do undo any updates if an error is returned (This may succeed in
515 * some cases like ENOSPC for allocating a new refcount block)
519 dummy
= update_refcount(bs
, offset
, cluster_offset
- offset
, -addend
);
525 /* addend must be 1 or -1 */
526 static int update_cluster_refcount(BlockDriverState
*bs
,
527 int64_t cluster_index
,
530 BDRVQcowState
*s
= bs
->opaque
;
533 ret
= update_refcount(bs
, cluster_index
<< s
->cluster_bits
, 1, addend
);
538 return get_refcount(bs
, cluster_index
);
543 /*********************************************************/
544 /* cluster allocation functions */
548 /* return < 0 if error */
549 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, int64_t size
)
551 BDRVQcowState
*s
= bs
->opaque
;
554 nb_clusters
= size_to_clusters(s
, size
);
556 for(i
= 0; i
< nb_clusters
; i
++) {
557 int64_t i
= s
->free_cluster_index
++;
558 if (get_refcount(bs
, i
) != 0)
562 printf("alloc_clusters: size=%" PRId64
" -> %" PRId64
"\n",
564 (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
);
566 return (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
;
569 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, int64_t size
)
571 BDRVQcowState
*s
= bs
->opaque
;
575 BLKDBG_EVENT(s
->hd
, BLKDBG_CLUSTER_ALLOC
);
576 offset
= alloc_clusters_noref(bs
, size
);
577 ret
= update_refcount(bs
, offset
, size
, 1);
584 /* only used to allocate compressed sectors. We try to allocate
585 contiguous sectors. size must be <= cluster_size */
586 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
)
588 BDRVQcowState
*s
= bs
->opaque
;
589 int64_t offset
, cluster_offset
;
592 BLKDBG_EVENT(s
->hd
, BLKDBG_CLUSTER_ALLOC_BYTES
);
593 assert(size
> 0 && size
<= s
->cluster_size
);
594 if (s
->free_byte_offset
== 0) {
595 s
->free_byte_offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
596 if (s
->free_byte_offset
< 0) {
597 return s
->free_byte_offset
;
601 free_in_cluster
= s
->cluster_size
-
602 (s
->free_byte_offset
& (s
->cluster_size
- 1));
603 if (size
<= free_in_cluster
) {
604 /* enough space in current cluster */
605 offset
= s
->free_byte_offset
;
606 s
->free_byte_offset
+= size
;
607 free_in_cluster
-= size
;
608 if (free_in_cluster
== 0)
609 s
->free_byte_offset
= 0;
610 if ((offset
& (s
->cluster_size
- 1)) != 0)
611 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
613 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
617 cluster_offset
= s
->free_byte_offset
& ~(s
->cluster_size
- 1);
618 if ((cluster_offset
+ s
->cluster_size
) == offset
) {
619 /* we are lucky: contiguous data */
620 offset
= s
->free_byte_offset
;
621 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
622 s
->free_byte_offset
+= size
;
624 s
->free_byte_offset
= offset
;
631 void qcow2_free_clusters(BlockDriverState
*bs
,
632 int64_t offset
, int64_t size
)
634 BDRVQcowState
*s
= bs
->opaque
;
637 BLKDBG_EVENT(s
->hd
, BLKDBG_CLUSTER_FREE
);
638 ret
= update_refcount(bs
, offset
, size
, -1);
640 fprintf(stderr
, "qcow2_free_clusters failed: %s\n", strerror(-ret
));
648 * free clusters according to its type: compressed or not
652 void qcow2_free_any_clusters(BlockDriverState
*bs
,
653 uint64_t cluster_offset
, int nb_clusters
)
655 BDRVQcowState
*s
= bs
->opaque
;
657 /* free the cluster */
659 if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
661 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) &
663 qcow2_free_clusters(bs
,
664 (cluster_offset
& s
->cluster_offset_mask
) & ~511,
669 qcow2_free_clusters(bs
, cluster_offset
, nb_clusters
<< s
->cluster_bits
);
676 /*********************************************************/
677 /* snapshots and image creation */
681 void qcow2_create_refcount_update(QCowCreateState
*s
, int64_t offset
,
685 int64_t start
, last
, cluster_offset
;
688 start
= offset
& ~(s
->cluster_size
- 1);
689 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
690 for(cluster_offset
= start
; cluster_offset
<= last
;
691 cluster_offset
+= s
->cluster_size
) {
692 p
= &s
->refcount_block
[cluster_offset
>> s
->cluster_bits
];
693 refcount
= be16_to_cpu(*p
);
695 *p
= cpu_to_be16(refcount
);
699 /* update the refcounts of snapshots and the copied flag */
700 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
701 int64_t l1_table_offset
, int l1_size
, int addend
)
703 BDRVQcowState
*s
= bs
->opaque
;
704 uint64_t *l1_table
, *l2_table
, l2_offset
, offset
, l1_size2
, l1_allocated
;
705 int64_t old_offset
, old_l2_offset
;
706 int l2_size
, i
, j
, l1_modified
, l2_modified
, nb_csectors
, refcount
;
708 qcow2_l2_cache_reset(bs
);
709 cache_refcount_updates
= 1;
713 l1_size2
= l1_size
* sizeof(uint64_t);
714 if (l1_table_offset
!= s
->l1_table_offset
) {
716 l1_table
= qemu_mallocz(align_offset(l1_size2
, 512));
721 if (bdrv_pread(s
->hd
, l1_table_offset
,
722 l1_table
, l1_size2
) != l1_size2
)
724 for(i
= 0;i
< l1_size
; i
++)
725 be64_to_cpus(&l1_table
[i
]);
727 assert(l1_size
== s
->l1_size
);
728 l1_table
= s
->l1_table
;
732 l2_size
= s
->l2_size
* sizeof(uint64_t);
733 l2_table
= qemu_malloc(l2_size
);
735 for(i
= 0; i
< l1_size
; i
++) {
736 l2_offset
= l1_table
[i
];
738 old_l2_offset
= l2_offset
;
739 l2_offset
&= ~QCOW_OFLAG_COPIED
;
741 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, l2_size
) != l2_size
)
743 for(j
= 0; j
< s
->l2_size
; j
++) {
744 offset
= be64_to_cpu(l2_table
[j
]);
747 offset
&= ~QCOW_OFLAG_COPIED
;
748 if (offset
& QCOW_OFLAG_COMPRESSED
) {
749 nb_csectors
= ((offset
>> s
->csize_shift
) &
753 ret
= update_refcount(bs
,
754 (offset
& s
->cluster_offset_mask
) & ~511,
755 nb_csectors
* 512, addend
);
760 /* compressed clusters are never modified */
764 refcount
= update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, addend
);
766 refcount
= get_refcount(bs
, offset
>> s
->cluster_bits
);
771 offset
|= QCOW_OFLAG_COPIED
;
773 if (offset
!= old_offset
) {
774 l2_table
[j
] = cpu_to_be64(offset
);
780 if (bdrv_pwrite(s
->hd
,
781 l2_offset
, l2_table
, l2_size
) != l2_size
)
786 refcount
= update_cluster_refcount(bs
, l2_offset
>> s
->cluster_bits
, addend
);
788 refcount
= get_refcount(bs
, l2_offset
>> s
->cluster_bits
);
791 l2_offset
|= QCOW_OFLAG_COPIED
;
793 if (l2_offset
!= old_l2_offset
) {
794 l1_table
[i
] = l2_offset
;
800 for(i
= 0; i
< l1_size
; i
++)
801 cpu_to_be64s(&l1_table
[i
]);
802 if (bdrv_pwrite(s
->hd
, l1_table_offset
, l1_table
,
803 l1_size2
) != l1_size2
)
805 for(i
= 0; i
< l1_size
; i
++)
806 be64_to_cpus(&l1_table
[i
]);
811 cache_refcount_updates
= 0;
812 write_refcount_block(s
);
818 cache_refcount_updates
= 0;
819 write_refcount_block(s
);
826 /*********************************************************/
827 /* refcount checking functions */
832 * Increases the refcount for a range of clusters in a given refcount table.
833 * This is used to construct a temporary refcount table out of L1 and L2 tables
834 * which can be compared the the refcount table saved in the image.
836 * Returns the number of errors in the image that were found
838 static int inc_refcounts(BlockDriverState
*bs
,
839 uint16_t *refcount_table
,
840 int refcount_table_size
,
841 int64_t offset
, int64_t size
)
843 BDRVQcowState
*s
= bs
->opaque
;
844 int64_t start
, last
, cluster_offset
;
851 start
= offset
& ~(s
->cluster_size
- 1);
852 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
853 for(cluster_offset
= start
; cluster_offset
<= last
;
854 cluster_offset
+= s
->cluster_size
) {
855 k
= cluster_offset
>> s
->cluster_bits
;
856 if (k
< 0 || k
>= refcount_table_size
) {
857 fprintf(stderr
, "ERROR: invalid cluster offset=0x%" PRIx64
"\n",
861 if (++refcount_table
[k
] == 0) {
862 fprintf(stderr
, "ERROR: overflow cluster offset=0x%" PRIx64
863 "\n", cluster_offset
);
873 * Increases the refcount in the given refcount table for the all clusters
874 * referenced in the L2 table. While doing so, performs some checks on L2
877 * Returns the number of errors found by the checks or -errno if an internal
880 static int check_refcounts_l2(BlockDriverState
*bs
,
881 uint16_t *refcount_table
, int refcount_table_size
, int64_t l2_offset
,
884 BDRVQcowState
*s
= bs
->opaque
;
885 uint64_t *l2_table
, offset
;
886 int i
, l2_size
, nb_csectors
, refcount
;
889 /* Read L2 table from disk */
890 l2_size
= s
->l2_size
* sizeof(uint64_t);
891 l2_table
= qemu_malloc(l2_size
);
893 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, l2_size
) != l2_size
)
896 /* Do the actual checks */
897 for(i
= 0; i
< s
->l2_size
; i
++) {
898 offset
= be64_to_cpu(l2_table
[i
]);
900 if (offset
& QCOW_OFLAG_COMPRESSED
) {
901 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
902 if (offset
& QCOW_OFLAG_COPIED
) {
903 fprintf(stderr
, "ERROR: cluster %" PRId64
": "
904 "copied flag must never be set for compressed "
905 "clusters\n", offset
>> s
->cluster_bits
);
906 offset
&= ~QCOW_OFLAG_COPIED
;
910 /* Mark cluster as used */
911 nb_csectors
= ((offset
>> s
->csize_shift
) &
913 offset
&= s
->cluster_offset_mask
;
914 errors
+= inc_refcounts(bs
, refcount_table
,
916 offset
& ~511, nb_csectors
* 512);
918 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
920 uint64_t entry
= offset
;
921 offset
&= ~QCOW_OFLAG_COPIED
;
922 refcount
= get_refcount(bs
, offset
>> s
->cluster_bits
);
923 if ((refcount
== 1) != ((entry
& QCOW_OFLAG_COPIED
) != 0)) {
924 fprintf(stderr
, "ERROR OFLAG_COPIED: offset=%"
925 PRIx64
" refcount=%d\n", entry
, refcount
);
930 /* Mark cluster as used */
931 offset
&= ~QCOW_OFLAG_COPIED
;
932 errors
+= inc_refcounts(bs
, refcount_table
,
934 offset
, s
->cluster_size
);
936 /* Correct offsets are cluster aligned */
937 if (offset
& (s
->cluster_size
- 1)) {
938 fprintf(stderr
, "ERROR offset=%" PRIx64
": Cluster is not "
939 "properly aligned; L2 entry corrupted.\n", offset
);
950 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
956 * Increases the refcount for the L1 table, its L2 tables and all referenced
957 * clusters in the given refcount table. While doing so, performs some checks
958 * on L1 and L2 entries.
960 * Returns the number of errors found by the checks or -errno if an internal
963 static int check_refcounts_l1(BlockDriverState
*bs
,
964 uint16_t *refcount_table
,
965 int refcount_table_size
,
966 int64_t l1_table_offset
, int l1_size
,
969 BDRVQcowState
*s
= bs
->opaque
;
970 uint64_t *l1_table
, l2_offset
, l1_size2
;
971 int i
, refcount
, ret
;
974 l1_size2
= l1_size
* sizeof(uint64_t);
976 /* Mark L1 table as used */
977 errors
+= inc_refcounts(bs
, refcount_table
, refcount_table_size
,
978 l1_table_offset
, l1_size2
);
980 /* Read L1 table entries from disk */
984 l1_table
= qemu_malloc(l1_size2
);
985 if (bdrv_pread(s
->hd
, l1_table_offset
,
986 l1_table
, l1_size2
) != l1_size2
)
988 for(i
= 0;i
< l1_size
; i
++)
989 be64_to_cpus(&l1_table
[i
]);
992 /* Do the actual checks */
993 for(i
= 0; i
< l1_size
; i
++) {
994 l2_offset
= l1_table
[i
];
996 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
998 refcount
= get_refcount(bs
, (l2_offset
& ~QCOW_OFLAG_COPIED
)
1000 if ((refcount
== 1) != ((l2_offset
& QCOW_OFLAG_COPIED
) != 0)) {
1001 fprintf(stderr
, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
1002 " refcount=%d\n", l2_offset
, refcount
);
1007 /* Mark L2 table as used */
1008 l2_offset
&= ~QCOW_OFLAG_COPIED
;
1009 errors
+= inc_refcounts(bs
, refcount_table
,
1010 refcount_table_size
,
1014 /* L2 tables are cluster aligned */
1015 if (l2_offset
& (s
->cluster_size
- 1)) {
1016 fprintf(stderr
, "ERROR l2_offset=%" PRIx64
": Table is not "
1017 "cluster aligned; L1 entry corrupted\n", l2_offset
);
1021 /* Process and check L2 entries */
1022 ret
= check_refcounts_l2(bs
, refcount_table
, refcount_table_size
,
1023 l2_offset
, check_copied
);
1030 qemu_free(l1_table
);
1034 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
1035 qemu_free(l1_table
);
1040 * Checks an image for refcount consistency.
1042 * Returns 0 if no errors are found, the number of errors in case the image is
1043 * detected as corrupted, and -errno when an internal error occured.
1045 int qcow2_check_refcounts(BlockDriverState
*bs
)
1047 BDRVQcowState
*s
= bs
->opaque
;
1049 int nb_clusters
, refcount1
, refcount2
, i
;
1051 uint16_t *refcount_table
;
1052 int ret
, errors
= 0;
1054 size
= bdrv_getlength(s
->hd
);
1055 nb_clusters
= size_to_clusters(s
, size
);
1056 refcount_table
= qemu_mallocz(nb_clusters
* sizeof(uint16_t));
1059 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
1060 0, s
->cluster_size
);
1062 /* current L1 table */
1063 ret
= check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
1064 s
->l1_table_offset
, s
->l1_size
, 1);
1071 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
1072 sn
= s
->snapshots
+ i
;
1073 check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
1074 sn
->l1_table_offset
, sn
->l1_size
, 0);
1076 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
1077 s
->snapshots_offset
, s
->snapshots_size
);
1080 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
1081 s
->refcount_table_offset
,
1082 s
->refcount_table_size
* sizeof(uint64_t));
1083 for(i
= 0; i
< s
->refcount_table_size
; i
++) {
1085 offset
= s
->refcount_table
[i
];
1087 /* Refcount blocks are cluster aligned */
1088 if (offset
& (s
->cluster_size
- 1)) {
1089 fprintf(stderr
, "ERROR refcount block %d is not "
1090 "cluster aligned; refcount table entry corrupted\n", i
);
1095 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
1096 offset
, s
->cluster_size
);
1097 if (refcount_table
[offset
/ s
->cluster_size
] != 1) {
1098 fprintf(stderr
, "ERROR refcount block %d refcount=%d\n",
1099 i
, refcount_table
[offset
/ s
->cluster_size
]);
1104 /* compare ref counts */
1105 for(i
= 0; i
< nb_clusters
; i
++) {
1106 refcount1
= get_refcount(bs
, i
);
1107 refcount2
= refcount_table
[i
];
1108 if (refcount1
!= refcount2
) {
1109 fprintf(stderr
, "ERROR cluster %d refcount=%d reference=%d\n",
1110 i
, refcount1
, refcount2
);
1115 qemu_free(refcount_table
);