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 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 if (bdrv_pwrite(s
->hd
, s
->refcount_block_cache_offset
,
46 s
->refcount_block_cache
, size
) != size
)
54 /*********************************************************/
55 /* refcount handling */
57 int qcow2_refcount_init(BlockDriverState
*bs
)
59 BDRVQcowState
*s
= bs
->opaque
;
60 int ret
, refcount_table_size2
, i
;
62 s
->refcount_block_cache
= qemu_malloc(s
->cluster_size
);
63 refcount_table_size2
= s
->refcount_table_size
* sizeof(uint64_t);
64 s
->refcount_table
= qemu_malloc(refcount_table_size2
);
65 if (s
->refcount_table_size
> 0) {
66 ret
= bdrv_pread(s
->hd
, s
->refcount_table_offset
,
67 s
->refcount_table
, refcount_table_size2
);
68 if (ret
!= refcount_table_size2
)
70 for(i
= 0; i
< s
->refcount_table_size
; i
++)
71 be64_to_cpus(&s
->refcount_table
[i
]);
78 void qcow2_refcount_close(BlockDriverState
*bs
)
80 BDRVQcowState
*s
= bs
->opaque
;
81 qemu_free(s
->refcount_block_cache
);
82 qemu_free(s
->refcount_table
);
86 static int load_refcount_block(BlockDriverState
*bs
,
87 int64_t refcount_block_offset
)
89 BDRVQcowState
*s
= bs
->opaque
;
92 if (cache_refcount_updates
) {
93 write_refcount_block(s
);
96 ret
= bdrv_pread(s
->hd
, refcount_block_offset
, s
->refcount_block_cache
,
98 if (ret
!= s
->cluster_size
)
100 s
->refcount_block_cache_offset
= refcount_block_offset
;
104 static int get_refcount(BlockDriverState
*bs
, int64_t cluster_index
)
106 BDRVQcowState
*s
= bs
->opaque
;
107 int refcount_table_index
, block_index
;
108 int64_t refcount_block_offset
;
110 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
111 if (refcount_table_index
>= s
->refcount_table_size
)
113 refcount_block_offset
= s
->refcount_table
[refcount_table_index
];
114 if (!refcount_block_offset
)
116 if (refcount_block_offset
!= s
->refcount_block_cache_offset
) {
117 /* better than nothing: return allocated if read error */
118 if (load_refcount_block(bs
, refcount_block_offset
) < 0)
121 block_index
= cluster_index
&
122 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
123 return be16_to_cpu(s
->refcount_block_cache
[block_index
]);
127 * Rounds the refcount table size up to avoid growing the table for each single
128 * refcount block that is allocated.
130 static unsigned int next_refcount_table_size(BDRVQcowState
*s
,
131 unsigned int min_size
)
133 unsigned int min_clusters
= (min_size
>> (s
->cluster_bits
- 3)) + 1;
134 unsigned int refcount_table_clusters
=
135 MAX(1, s
->refcount_table_size
>> (s
->cluster_bits
- 3));
137 while (min_clusters
> refcount_table_clusters
) {
138 refcount_table_clusters
= (refcount_table_clusters
* 3 + 1) / 2;
141 return refcount_table_clusters
<< (s
->cluster_bits
- 3);
144 static int grow_refcount_table(BlockDriverState
*bs
, int min_size
)
146 BDRVQcowState
*s
= bs
->opaque
;
147 int new_table_size
, new_table_size2
, refcount_table_clusters
, i
, ret
;
149 int64_t table_offset
;
152 int64_t old_table_offset
;
154 if (min_size
<= s
->refcount_table_size
)
156 /* compute new table size */
157 new_table_size
= next_refcount_table_size(s
, min_size
);
159 printf("grow_refcount_table from %d to %d\n",
160 s
->refcount_table_size
,
163 new_table_size2
= new_table_size
* sizeof(uint64_t);
164 new_table
= qemu_mallocz(new_table_size2
);
165 memcpy(new_table
, s
->refcount_table
,
166 s
->refcount_table_size
* sizeof(uint64_t));
167 for(i
= 0; i
< s
->refcount_table_size
; i
++)
168 cpu_to_be64s(&new_table
[i
]);
169 /* Note: we cannot update the refcount now to avoid recursion */
170 table_offset
= alloc_clusters_noref(bs
, new_table_size2
);
171 ret
= bdrv_pwrite(s
->hd
, table_offset
, new_table
, new_table_size2
);
172 if (ret
!= new_table_size2
)
174 for(i
= 0; i
< s
->refcount_table_size
; i
++)
175 be64_to_cpus(&new_table
[i
]);
177 cpu_to_be64w((uint64_t*)data
, table_offset
);
178 cpu_to_be32w((uint32_t*)(data
+ 8), refcount_table_clusters
);
179 ret
= bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, refcount_table_offset
),
181 if (ret
!= sizeof(data
)) {
185 qemu_free(s
->refcount_table
);
186 old_table_offset
= s
->refcount_table_offset
;
187 old_table_size
= s
->refcount_table_size
;
188 s
->refcount_table
= new_table
;
189 s
->refcount_table_size
= new_table_size
;
190 s
->refcount_table_offset
= table_offset
;
192 update_refcount(bs
, table_offset
, new_table_size2
, 1);
193 qcow2_free_clusters(bs
, old_table_offset
, old_table_size
* sizeof(uint64_t));
196 qemu_free(new_table
);
197 return ret
< 0 ? ret
: -EIO
;
201 static int64_t alloc_refcount_block(BlockDriverState
*bs
, int64_t cluster_index
)
203 BDRVQcowState
*s
= bs
->opaque
;
204 int64_t offset
, refcount_block_offset
;
205 unsigned int refcount_table_index
;
208 int cache
= cache_refcount_updates
;
210 /* Find L1 index and grow refcount table if needed */
211 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
212 if (refcount_table_index
>= s
->refcount_table_size
) {
213 ret
= grow_refcount_table(bs
, refcount_table_index
+ 1);
218 /* Load or allocate the refcount block */
219 refcount_block_offset
= s
->refcount_table
[refcount_table_index
];
220 if (!refcount_block_offset
) {
221 if (cache_refcount_updates
) {
222 write_refcount_block(s
);
223 cache_refcount_updates
= 0;
225 /* create a new refcount block */
226 /* Note: we cannot update the refcount now to avoid recursion */
227 offset
= alloc_clusters_noref(bs
, s
->cluster_size
);
228 memset(s
->refcount_block_cache
, 0, s
->cluster_size
);
229 ret
= bdrv_pwrite(s
->hd
, offset
, s
->refcount_block_cache
, s
->cluster_size
);
230 if (ret
!= s
->cluster_size
)
232 s
->refcount_table
[refcount_table_index
] = offset
;
233 data64
= cpu_to_be64(offset
);
234 ret
= bdrv_pwrite(s
->hd
, s
->refcount_table_offset
+
235 refcount_table_index
* sizeof(uint64_t),
236 &data64
, sizeof(data64
));
237 if (ret
!= sizeof(data64
))
240 refcount_block_offset
= offset
;
241 s
->refcount_block_cache_offset
= offset
;
242 update_refcount(bs
, offset
, s
->cluster_size
, 1);
243 cache_refcount_updates
= cache
;
245 if (refcount_block_offset
!= s
->refcount_block_cache_offset
) {
246 if (load_refcount_block(bs
, refcount_block_offset
) < 0)
251 return refcount_block_offset
;
254 #define REFCOUNTS_PER_SECTOR (512 >> REFCOUNT_SHIFT)
255 static int write_refcount_block_entries(BDRVQcowState
*s
,
256 int64_t refcount_block_offset
, int first_index
, int last_index
)
260 if (cache_refcount_updates
) {
264 first_index
&= ~(REFCOUNTS_PER_SECTOR
- 1);
265 last_index
= (last_index
+ REFCOUNTS_PER_SECTOR
)
266 & ~(REFCOUNTS_PER_SECTOR
- 1);
268 size
= (last_index
- first_index
) << REFCOUNT_SHIFT
;
269 if (bdrv_pwrite(s
->hd
,
270 refcount_block_offset
+ (first_index
<< REFCOUNT_SHIFT
),
271 &s
->refcount_block_cache
[first_index
], size
) != size
)
279 /* XXX: cache several refcount block clusters ? */
280 static int QEMU_WARN_UNUSED_RESULT
update_refcount(BlockDriverState
*bs
,
281 int64_t offset
, int64_t length
, int addend
)
283 BDRVQcowState
*s
= bs
->opaque
;
284 int64_t start
, last
, cluster_offset
;
285 int64_t refcount_block_offset
= 0;
286 int64_t table_index
= -1, old_table_index
;
287 int first_index
= -1, last_index
= -1;
291 printf("update_refcount: offset=%" PRId64
" size=%" PRId64
" addend=%d\n",
292 offset
, length
, addend
);
296 } else if (length
== 0) {
300 start
= offset
& ~(s
->cluster_size
- 1);
301 last
= (offset
+ length
- 1) & ~(s
->cluster_size
- 1);
302 for(cluster_offset
= start
; cluster_offset
<= last
;
303 cluster_offset
+= s
->cluster_size
)
305 int block_index
, refcount
;
306 int64_t cluster_index
= cluster_offset
>> s
->cluster_bits
;
309 /* Only write refcount block to disk when we are done with it */
310 old_table_index
= table_index
;
311 table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
312 if ((old_table_index
>= 0) && (table_index
!= old_table_index
)) {
314 if (write_refcount_block_entries(s
, refcount_block_offset
,
315 first_index
, last_index
) < 0)
324 /* Load the refcount block and allocate it if needed */
325 new_block
= alloc_refcount_block(bs
, cluster_index
);
330 refcount_block_offset
= new_block
;
332 /* we can update the count and save it */
333 block_index
= cluster_index
&
334 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
335 if (first_index
== -1 || block_index
< first_index
) {
336 first_index
= block_index
;
338 if (block_index
> last_index
) {
339 last_index
= block_index
;
342 refcount
= be16_to_cpu(s
->refcount_block_cache
[block_index
]);
344 if (refcount
< 0 || refcount
> 0xffff) {
348 if (refcount
== 0 && cluster_index
< s
->free_cluster_index
) {
349 s
->free_cluster_index
= cluster_index
;
351 s
->refcount_block_cache
[block_index
] = cpu_to_be16(refcount
);
357 /* Write last changed block to disk */
358 if (refcount_block_offset
!= 0) {
359 if (write_refcount_block_entries(s
, refcount_block_offset
,
360 first_index
, last_index
) < 0)
362 return ret
< 0 ? ret
: -EIO
;
367 * Try do undo any updates if an error is returned (This may succeed in
368 * some cases like ENOSPC for allocating a new refcount block)
372 dummy
= update_refcount(bs
, offset
, cluster_offset
- offset
, -addend
);
378 /* addend must be 1 or -1 */
379 static int update_cluster_refcount(BlockDriverState
*bs
,
380 int64_t cluster_index
,
383 BDRVQcowState
*s
= bs
->opaque
;
386 ret
= update_refcount(bs
, cluster_index
<< s
->cluster_bits
, 1, addend
);
391 return get_refcount(bs
, cluster_index
);
396 /*********************************************************/
397 /* cluster allocation functions */
401 /* return < 0 if error */
402 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, int64_t size
)
404 BDRVQcowState
*s
= bs
->opaque
;
407 nb_clusters
= size_to_clusters(s
, size
);
409 for(i
= 0; i
< nb_clusters
; i
++) {
410 int64_t i
= s
->free_cluster_index
++;
411 if (get_refcount(bs
, i
) != 0)
415 printf("alloc_clusters: size=%" PRId64
" -> %" PRId64
"\n",
417 (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
);
419 return (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
;
422 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, int64_t size
)
427 offset
= alloc_clusters_noref(bs
, size
);
428 ret
= update_refcount(bs
, offset
, size
, 1);
435 /* only used to allocate compressed sectors. We try to allocate
436 contiguous sectors. size must be <= cluster_size */
437 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
)
439 BDRVQcowState
*s
= bs
->opaque
;
440 int64_t offset
, cluster_offset
;
443 assert(size
> 0 && size
<= s
->cluster_size
);
444 if (s
->free_byte_offset
== 0) {
445 s
->free_byte_offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
446 if (s
->free_byte_offset
< 0) {
447 return s
->free_byte_offset
;
451 free_in_cluster
= s
->cluster_size
-
452 (s
->free_byte_offset
& (s
->cluster_size
- 1));
453 if (size
<= free_in_cluster
) {
454 /* enough space in current cluster */
455 offset
= s
->free_byte_offset
;
456 s
->free_byte_offset
+= size
;
457 free_in_cluster
-= size
;
458 if (free_in_cluster
== 0)
459 s
->free_byte_offset
= 0;
460 if ((offset
& (s
->cluster_size
- 1)) != 0)
461 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
463 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
467 cluster_offset
= s
->free_byte_offset
& ~(s
->cluster_size
- 1);
468 if ((cluster_offset
+ s
->cluster_size
) == offset
) {
469 /* we are lucky: contiguous data */
470 offset
= s
->free_byte_offset
;
471 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
472 s
->free_byte_offset
+= size
;
474 s
->free_byte_offset
= offset
;
481 void qcow2_free_clusters(BlockDriverState
*bs
,
482 int64_t offset
, int64_t size
)
486 ret
= update_refcount(bs
, offset
, size
, -1);
488 fprintf(stderr
, "qcow2_free_clusters failed: %s\n", strerror(-ret
));
496 * free clusters according to its type: compressed or not
500 void qcow2_free_any_clusters(BlockDriverState
*bs
,
501 uint64_t cluster_offset
, int nb_clusters
)
503 BDRVQcowState
*s
= bs
->opaque
;
505 /* free the cluster */
507 if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
509 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) &
511 qcow2_free_clusters(bs
,
512 (cluster_offset
& s
->cluster_offset_mask
) & ~511,
517 qcow2_free_clusters(bs
, cluster_offset
, nb_clusters
<< s
->cluster_bits
);
524 /*********************************************************/
525 /* snapshots and image creation */
529 void qcow2_create_refcount_update(QCowCreateState
*s
, int64_t offset
,
533 int64_t start
, last
, cluster_offset
;
536 start
= offset
& ~(s
->cluster_size
- 1);
537 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
538 for(cluster_offset
= start
; cluster_offset
<= last
;
539 cluster_offset
+= s
->cluster_size
) {
540 p
= &s
->refcount_block
[cluster_offset
>> s
->cluster_bits
];
541 refcount
= be16_to_cpu(*p
);
543 *p
= cpu_to_be16(refcount
);
547 /* update the refcounts of snapshots and the copied flag */
548 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
549 int64_t l1_table_offset
, int l1_size
, int addend
)
551 BDRVQcowState
*s
= bs
->opaque
;
552 uint64_t *l1_table
, *l2_table
, l2_offset
, offset
, l1_size2
, l1_allocated
;
553 int64_t old_offset
, old_l2_offset
;
554 int l2_size
, i
, j
, l1_modified
, l2_modified
, nb_csectors
, refcount
;
556 qcow2_l2_cache_reset(bs
);
557 cache_refcount_updates
= 1;
561 l1_size2
= l1_size
* sizeof(uint64_t);
562 if (l1_table_offset
!= s
->l1_table_offset
) {
564 l1_table
= qemu_mallocz(align_offset(l1_size2
, 512));
569 if (bdrv_pread(s
->hd
, l1_table_offset
,
570 l1_table
, l1_size2
) != l1_size2
)
572 for(i
= 0;i
< l1_size
; i
++)
573 be64_to_cpus(&l1_table
[i
]);
575 assert(l1_size
== s
->l1_size
);
576 l1_table
= s
->l1_table
;
580 l2_size
= s
->l2_size
* sizeof(uint64_t);
581 l2_table
= qemu_malloc(l2_size
);
583 for(i
= 0; i
< l1_size
; i
++) {
584 l2_offset
= l1_table
[i
];
586 old_l2_offset
= l2_offset
;
587 l2_offset
&= ~QCOW_OFLAG_COPIED
;
589 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, l2_size
) != l2_size
)
591 for(j
= 0; j
< s
->l2_size
; j
++) {
592 offset
= be64_to_cpu(l2_table
[j
]);
595 offset
&= ~QCOW_OFLAG_COPIED
;
596 if (offset
& QCOW_OFLAG_COMPRESSED
) {
597 nb_csectors
= ((offset
>> s
->csize_shift
) &
601 ret
= update_refcount(bs
,
602 (offset
& s
->cluster_offset_mask
) & ~511,
603 nb_csectors
* 512, addend
);
608 /* compressed clusters are never modified */
612 refcount
= update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, addend
);
614 refcount
= get_refcount(bs
, offset
>> s
->cluster_bits
);
619 offset
|= QCOW_OFLAG_COPIED
;
621 if (offset
!= old_offset
) {
622 l2_table
[j
] = cpu_to_be64(offset
);
628 if (bdrv_pwrite(s
->hd
,
629 l2_offset
, l2_table
, l2_size
) != l2_size
)
634 refcount
= update_cluster_refcount(bs
, l2_offset
>> s
->cluster_bits
, addend
);
636 refcount
= get_refcount(bs
, l2_offset
>> s
->cluster_bits
);
639 l2_offset
|= QCOW_OFLAG_COPIED
;
641 if (l2_offset
!= old_l2_offset
) {
642 l1_table
[i
] = l2_offset
;
648 for(i
= 0; i
< l1_size
; i
++)
649 cpu_to_be64s(&l1_table
[i
]);
650 if (bdrv_pwrite(s
->hd
, l1_table_offset
, l1_table
,
651 l1_size2
) != l1_size2
)
653 for(i
= 0; i
< l1_size
; i
++)
654 be64_to_cpus(&l1_table
[i
]);
659 cache_refcount_updates
= 0;
660 write_refcount_block(s
);
666 cache_refcount_updates
= 0;
667 write_refcount_block(s
);
674 /*********************************************************/
675 /* refcount checking functions */
680 * Increases the refcount for a range of clusters in a given refcount table.
681 * This is used to construct a temporary refcount table out of L1 and L2 tables
682 * which can be compared the the refcount table saved in the image.
684 * Returns the number of errors in the image that were found
686 static int inc_refcounts(BlockDriverState
*bs
,
687 uint16_t *refcount_table
,
688 int refcount_table_size
,
689 int64_t offset
, int64_t size
)
691 BDRVQcowState
*s
= bs
->opaque
;
692 int64_t start
, last
, cluster_offset
;
699 start
= offset
& ~(s
->cluster_size
- 1);
700 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
701 for(cluster_offset
= start
; cluster_offset
<= last
;
702 cluster_offset
+= s
->cluster_size
) {
703 k
= cluster_offset
>> s
->cluster_bits
;
704 if (k
< 0 || k
>= refcount_table_size
) {
705 fprintf(stderr
, "ERROR: invalid cluster offset=0x%" PRIx64
"\n",
709 if (++refcount_table
[k
] == 0) {
710 fprintf(stderr
, "ERROR: overflow cluster offset=0x%" PRIx64
711 "\n", cluster_offset
);
721 * Increases the refcount in the given refcount table for the all clusters
722 * referenced in the L2 table. While doing so, performs some checks on L2
725 * Returns the number of errors found by the checks or -errno if an internal
728 static int check_refcounts_l2(BlockDriverState
*bs
,
729 uint16_t *refcount_table
, int refcount_table_size
, int64_t l2_offset
,
732 BDRVQcowState
*s
= bs
->opaque
;
733 uint64_t *l2_table
, offset
;
734 int i
, l2_size
, nb_csectors
, refcount
;
737 /* Read L2 table from disk */
738 l2_size
= s
->l2_size
* sizeof(uint64_t);
739 l2_table
= qemu_malloc(l2_size
);
741 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, l2_size
) != l2_size
)
744 /* Do the actual checks */
745 for(i
= 0; i
< s
->l2_size
; i
++) {
746 offset
= be64_to_cpu(l2_table
[i
]);
748 if (offset
& QCOW_OFLAG_COMPRESSED
) {
749 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
750 if (offset
& QCOW_OFLAG_COPIED
) {
751 fprintf(stderr
, "ERROR: cluster %" PRId64
": "
752 "copied flag must never be set for compressed "
753 "clusters\n", offset
>> s
->cluster_bits
);
754 offset
&= ~QCOW_OFLAG_COPIED
;
758 /* Mark cluster as used */
759 nb_csectors
= ((offset
>> s
->csize_shift
) &
761 offset
&= s
->cluster_offset_mask
;
762 errors
+= inc_refcounts(bs
, refcount_table
,
764 offset
& ~511, nb_csectors
* 512);
766 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
768 uint64_t entry
= offset
;
769 offset
&= ~QCOW_OFLAG_COPIED
;
770 refcount
= get_refcount(bs
, offset
>> s
->cluster_bits
);
771 if ((refcount
== 1) != ((entry
& QCOW_OFLAG_COPIED
) != 0)) {
772 fprintf(stderr
, "ERROR OFLAG_COPIED: offset=%"
773 PRIx64
" refcount=%d\n", entry
, refcount
);
778 /* Mark cluster as used */
779 offset
&= ~QCOW_OFLAG_COPIED
;
780 errors
+= inc_refcounts(bs
, refcount_table
,
782 offset
, s
->cluster_size
);
784 /* Correct offsets are cluster aligned */
785 if (offset
& (s
->cluster_size
- 1)) {
786 fprintf(stderr
, "ERROR offset=%" PRIx64
": Cluster is not "
787 "properly aligned; L2 entry corrupted.\n", offset
);
798 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
804 * Increases the refcount for the L1 table, its L2 tables and all referenced
805 * clusters in the given refcount table. While doing so, performs some checks
806 * on L1 and L2 entries.
808 * Returns the number of errors found by the checks or -errno if an internal
811 static int check_refcounts_l1(BlockDriverState
*bs
,
812 uint16_t *refcount_table
,
813 int refcount_table_size
,
814 int64_t l1_table_offset
, int l1_size
,
817 BDRVQcowState
*s
= bs
->opaque
;
818 uint64_t *l1_table
, l2_offset
, l1_size2
;
819 int i
, refcount
, ret
;
822 l1_size2
= l1_size
* sizeof(uint64_t);
824 /* Mark L1 table as used */
825 errors
+= inc_refcounts(bs
, refcount_table
, refcount_table_size
,
826 l1_table_offset
, l1_size2
);
828 /* Read L1 table entries from disk */
832 l1_table
= qemu_malloc(l1_size2
);
833 if (bdrv_pread(s
->hd
, l1_table_offset
,
834 l1_table
, l1_size2
) != l1_size2
)
836 for(i
= 0;i
< l1_size
; i
++)
837 be64_to_cpus(&l1_table
[i
]);
840 /* Do the actual checks */
841 for(i
= 0; i
< l1_size
; i
++) {
842 l2_offset
= l1_table
[i
];
844 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
846 refcount
= get_refcount(bs
, (l2_offset
& ~QCOW_OFLAG_COPIED
)
848 if ((refcount
== 1) != ((l2_offset
& QCOW_OFLAG_COPIED
) != 0)) {
849 fprintf(stderr
, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
850 " refcount=%d\n", l2_offset
, refcount
);
855 /* Mark L2 table as used */
856 l2_offset
&= ~QCOW_OFLAG_COPIED
;
857 errors
+= inc_refcounts(bs
, refcount_table
,
862 /* L2 tables are cluster aligned */
863 if (l2_offset
& (s
->cluster_size
- 1)) {
864 fprintf(stderr
, "ERROR l2_offset=%" PRIx64
": Table is not "
865 "cluster aligned; L1 entry corrupted\n", l2_offset
);
869 /* Process and check L2 entries */
870 ret
= check_refcounts_l2(bs
, refcount_table
, refcount_table_size
,
871 l2_offset
, check_copied
);
882 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
888 * Checks an image for refcount consistency.
890 * Returns 0 if no errors are found, the number of errors in case the image is
891 * detected as corrupted, and -errno when an internal error occured.
893 int qcow2_check_refcounts(BlockDriverState
*bs
)
895 BDRVQcowState
*s
= bs
->opaque
;
897 int nb_clusters
, refcount1
, refcount2
, i
;
899 uint16_t *refcount_table
;
902 size
= bdrv_getlength(s
->hd
);
903 nb_clusters
= size_to_clusters(s
, size
);
904 refcount_table
= qemu_mallocz(nb_clusters
* sizeof(uint16_t));
907 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
910 /* current L1 table */
911 ret
= check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
912 s
->l1_table_offset
, s
->l1_size
, 1);
919 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
920 sn
= s
->snapshots
+ i
;
921 check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
922 sn
->l1_table_offset
, sn
->l1_size
, 0);
924 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
925 s
->snapshots_offset
, s
->snapshots_size
);
928 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
929 s
->refcount_table_offset
,
930 s
->refcount_table_size
* sizeof(uint64_t));
931 for(i
= 0; i
< s
->refcount_table_size
; i
++) {
933 offset
= s
->refcount_table
[i
];
935 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
936 offset
, s
->cluster_size
);
940 /* compare ref counts */
941 for(i
= 0; i
< nb_clusters
; i
++) {
942 refcount1
= get_refcount(bs
, i
);
943 refcount2
= refcount_table
[i
];
944 if (refcount1
!= refcount2
) {
945 fprintf(stderr
, "ERROR cluster %d refcount=%d reference=%d\n",
946 i
, refcount1
, refcount2
);
951 qemu_free(refcount_table
);