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
]);
126 static int grow_refcount_table(BlockDriverState
*bs
, int min_size
)
128 BDRVQcowState
*s
= bs
->opaque
;
129 int new_table_size
, new_table_size2
, refcount_table_clusters
, i
, ret
;
131 int64_t table_offset
;
134 int64_t old_table_offset
;
136 if (min_size
<= s
->refcount_table_size
)
138 /* compute new table size */
139 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
141 if (refcount_table_clusters
== 0) {
142 refcount_table_clusters
= 1;
144 refcount_table_clusters
= (refcount_table_clusters
* 3 + 1) / 2;
146 new_table_size
= refcount_table_clusters
<< (s
->cluster_bits
- 3);
147 if (min_size
<= new_table_size
)
151 printf("grow_refcount_table from %d to %d\n",
152 s
->refcount_table_size
,
155 new_table_size2
= new_table_size
* sizeof(uint64_t);
156 new_table
= qemu_mallocz(new_table_size2
);
157 memcpy(new_table
, s
->refcount_table
,
158 s
->refcount_table_size
* sizeof(uint64_t));
159 for(i
= 0; i
< s
->refcount_table_size
; i
++)
160 cpu_to_be64s(&new_table
[i
]);
161 /* Note: we cannot update the refcount now to avoid recursion */
162 table_offset
= alloc_clusters_noref(bs
, new_table_size2
);
163 ret
= bdrv_pwrite(s
->hd
, table_offset
, new_table
, new_table_size2
);
164 if (ret
!= new_table_size2
)
166 for(i
= 0; i
< s
->refcount_table_size
; i
++)
167 be64_to_cpus(&new_table
[i
]);
169 cpu_to_be64w((uint64_t*)data
, table_offset
);
170 cpu_to_be32w((uint32_t*)(data
+ 8), refcount_table_clusters
);
171 if (bdrv_pwrite(s
->hd
, offsetof(QCowHeader
, refcount_table_offset
),
172 data
, sizeof(data
)) != sizeof(data
))
174 qemu_free(s
->refcount_table
);
175 old_table_offset
= s
->refcount_table_offset
;
176 old_table_size
= s
->refcount_table_size
;
177 s
->refcount_table
= new_table
;
178 s
->refcount_table_size
= new_table_size
;
179 s
->refcount_table_offset
= table_offset
;
181 update_refcount(bs
, table_offset
, new_table_size2
, 1);
182 qcow2_free_clusters(bs
, old_table_offset
, old_table_size
* sizeof(uint64_t));
185 qcow2_free_clusters(bs
, table_offset
, new_table_size2
);
186 qemu_free(new_table
);
191 static int64_t alloc_refcount_block(BlockDriverState
*bs
, int64_t cluster_index
)
193 BDRVQcowState
*s
= bs
->opaque
;
194 int64_t offset
, refcount_block_offset
;
195 unsigned int refcount_table_index
;
198 int cache
= cache_refcount_updates
;
200 /* Find L1 index and grow refcount table if needed */
201 refcount_table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
202 if (refcount_table_index
>= s
->refcount_table_size
) {
203 ret
= grow_refcount_table(bs
, refcount_table_index
+ 1);
208 /* Load or allocate the refcount block */
209 refcount_block_offset
= s
->refcount_table
[refcount_table_index
];
210 if (!refcount_block_offset
) {
211 if (cache_refcount_updates
) {
212 write_refcount_block(s
);
213 cache_refcount_updates
= 0;
215 /* create a new refcount block */
216 /* Note: we cannot update the refcount now to avoid recursion */
217 offset
= alloc_clusters_noref(bs
, s
->cluster_size
);
218 memset(s
->refcount_block_cache
, 0, s
->cluster_size
);
219 ret
= bdrv_pwrite(s
->hd
, offset
, s
->refcount_block_cache
, s
->cluster_size
);
220 if (ret
!= s
->cluster_size
)
222 s
->refcount_table
[refcount_table_index
] = offset
;
223 data64
= cpu_to_be64(offset
);
224 ret
= bdrv_pwrite(s
->hd
, s
->refcount_table_offset
+
225 refcount_table_index
* sizeof(uint64_t),
226 &data64
, sizeof(data64
));
227 if (ret
!= sizeof(data64
))
230 refcount_block_offset
= offset
;
231 s
->refcount_block_cache_offset
= offset
;
232 update_refcount(bs
, offset
, s
->cluster_size
, 1);
233 cache_refcount_updates
= cache
;
235 if (refcount_block_offset
!= s
->refcount_block_cache_offset
) {
236 if (load_refcount_block(bs
, refcount_block_offset
) < 0)
241 return refcount_block_offset
;
244 #define REFCOUNTS_PER_SECTOR (512 >> REFCOUNT_SHIFT)
245 static int write_refcount_block_entries(BDRVQcowState
*s
,
246 int64_t refcount_block_offset
, int first_index
, int last_index
)
250 if (cache_refcount_updates
) {
254 first_index
&= ~(REFCOUNTS_PER_SECTOR
- 1);
255 last_index
= (last_index
+ REFCOUNTS_PER_SECTOR
)
256 & ~(REFCOUNTS_PER_SECTOR
- 1);
258 size
= (last_index
- first_index
) << REFCOUNT_SHIFT
;
259 if (bdrv_pwrite(s
->hd
,
260 refcount_block_offset
+ (first_index
<< REFCOUNT_SHIFT
),
261 &s
->refcount_block_cache
[first_index
], size
) != size
)
269 /* XXX: cache several refcount block clusters ? */
270 static int update_refcount(BlockDriverState
*bs
,
271 int64_t offset
, int64_t length
,
274 BDRVQcowState
*s
= bs
->opaque
;
275 int64_t start
, last
, cluster_offset
;
276 int64_t refcount_block_offset
= 0;
277 int64_t table_index
= -1, old_table_index
;
278 int first_index
= -1, last_index
= -1;
281 printf("update_refcount: offset=%" PRId64
" size=%" PRId64
" addend=%d\n",
282 offset
, length
, addend
);
286 start
= offset
& ~(s
->cluster_size
- 1);
287 last
= (offset
+ length
- 1) & ~(s
->cluster_size
- 1);
288 for(cluster_offset
= start
; cluster_offset
<= last
;
289 cluster_offset
+= s
->cluster_size
)
291 int block_index
, refcount
;
292 int64_t cluster_index
= cluster_offset
>> s
->cluster_bits
;
294 /* Only write refcount block to disk when we are done with it */
295 old_table_index
= table_index
;
296 table_index
= cluster_index
>> (s
->cluster_bits
- REFCOUNT_SHIFT
);
297 if ((old_table_index
>= 0) && (table_index
!= old_table_index
)) {
299 if (write_refcount_block_entries(s
, refcount_block_offset
,
300 first_index
, last_index
) < 0)
309 /* Load the refcount block and allocate it if needed */
310 refcount_block_offset
= alloc_refcount_block(bs
, cluster_index
);
311 if (refcount_block_offset
< 0) {
312 return refcount_block_offset
;
315 /* we can update the count and save it */
316 block_index
= cluster_index
&
317 ((1 << (s
->cluster_bits
- REFCOUNT_SHIFT
)) - 1);
318 if (first_index
== -1 || block_index
< first_index
) {
319 first_index
= block_index
;
321 if (block_index
> last_index
) {
322 last_index
= block_index
;
325 refcount
= be16_to_cpu(s
->refcount_block_cache
[block_index
]);
327 if (refcount
< 0 || refcount
> 0xffff)
329 if (refcount
== 0 && cluster_index
< s
->free_cluster_index
) {
330 s
->free_cluster_index
= cluster_index
;
332 s
->refcount_block_cache
[block_index
] = cpu_to_be16(refcount
);
335 /* Write last changed block to disk */
336 if (refcount_block_offset
!= 0) {
337 if (write_refcount_block_entries(s
, refcount_block_offset
,
338 first_index
, last_index
) < 0)
347 /* addend must be 1 or -1 */
348 static int update_cluster_refcount(BlockDriverState
*bs
,
349 int64_t cluster_index
,
352 BDRVQcowState
*s
= bs
->opaque
;
355 ret
= update_refcount(bs
, cluster_index
<< s
->cluster_bits
, 1, addend
);
360 return get_refcount(bs
, cluster_index
);
365 /*********************************************************/
366 /* cluster allocation functions */
370 /* return < 0 if error */
371 static int64_t alloc_clusters_noref(BlockDriverState
*bs
, int64_t size
)
373 BDRVQcowState
*s
= bs
->opaque
;
376 nb_clusters
= size_to_clusters(s
, size
);
378 for(i
= 0; i
< nb_clusters
; i
++) {
379 int64_t i
= s
->free_cluster_index
++;
380 if (get_refcount(bs
, i
) != 0)
384 printf("alloc_clusters: size=%" PRId64
" -> %" PRId64
"\n",
386 (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
);
388 return (s
->free_cluster_index
- nb_clusters
) << s
->cluster_bits
;
391 int64_t qcow2_alloc_clusters(BlockDriverState
*bs
, int64_t size
)
395 offset
= alloc_clusters_noref(bs
, size
);
396 update_refcount(bs
, offset
, size
, 1);
400 /* only used to allocate compressed sectors. We try to allocate
401 contiguous sectors. size must be <= cluster_size */
402 int64_t qcow2_alloc_bytes(BlockDriverState
*bs
, int size
)
404 BDRVQcowState
*s
= bs
->opaque
;
405 int64_t offset
, cluster_offset
;
408 assert(size
> 0 && size
<= s
->cluster_size
);
409 if (s
->free_byte_offset
== 0) {
410 s
->free_byte_offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
413 free_in_cluster
= s
->cluster_size
-
414 (s
->free_byte_offset
& (s
->cluster_size
- 1));
415 if (size
<= free_in_cluster
) {
416 /* enough space in current cluster */
417 offset
= s
->free_byte_offset
;
418 s
->free_byte_offset
+= size
;
419 free_in_cluster
-= size
;
420 if (free_in_cluster
== 0)
421 s
->free_byte_offset
= 0;
422 if ((offset
& (s
->cluster_size
- 1)) != 0)
423 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
425 offset
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
426 cluster_offset
= s
->free_byte_offset
& ~(s
->cluster_size
- 1);
427 if ((cluster_offset
+ s
->cluster_size
) == offset
) {
428 /* we are lucky: contiguous data */
429 offset
= s
->free_byte_offset
;
430 update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, 1);
431 s
->free_byte_offset
+= size
;
433 s
->free_byte_offset
= offset
;
440 void qcow2_free_clusters(BlockDriverState
*bs
,
441 int64_t offset
, int64_t size
)
443 update_refcount(bs
, offset
, size
, -1);
449 * free clusters according to its type: compressed or not
453 void qcow2_free_any_clusters(BlockDriverState
*bs
,
454 uint64_t cluster_offset
, int nb_clusters
)
456 BDRVQcowState
*s
= bs
->opaque
;
458 /* free the cluster */
460 if (cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
462 nb_csectors
= ((cluster_offset
>> s
->csize_shift
) &
464 qcow2_free_clusters(bs
,
465 (cluster_offset
& s
->cluster_offset_mask
) & ~511,
470 qcow2_free_clusters(bs
, cluster_offset
, nb_clusters
<< s
->cluster_bits
);
477 /*********************************************************/
478 /* snapshots and image creation */
482 void qcow2_create_refcount_update(QCowCreateState
*s
, int64_t offset
,
486 int64_t start
, last
, cluster_offset
;
489 start
= offset
& ~(s
->cluster_size
- 1);
490 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
491 for(cluster_offset
= start
; cluster_offset
<= last
;
492 cluster_offset
+= s
->cluster_size
) {
493 p
= &s
->refcount_block
[cluster_offset
>> s
->cluster_bits
];
494 refcount
= be16_to_cpu(*p
);
496 *p
= cpu_to_be16(refcount
);
500 /* update the refcounts of snapshots and the copied flag */
501 int qcow2_update_snapshot_refcount(BlockDriverState
*bs
,
502 int64_t l1_table_offset
, int l1_size
, int addend
)
504 BDRVQcowState
*s
= bs
->opaque
;
505 uint64_t *l1_table
, *l2_table
, l2_offset
, offset
, l1_size2
, l1_allocated
;
506 int64_t old_offset
, old_l2_offset
;
507 int l2_size
, i
, j
, l1_modified
, l2_modified
, nb_csectors
, refcount
;
509 qcow2_l2_cache_reset(bs
);
510 cache_refcount_updates
= 1;
514 l1_size2
= l1_size
* sizeof(uint64_t);
516 if (l1_table_offset
!= s
->l1_table_offset
) {
517 l1_table
= qemu_mallocz(align_offset(l1_size2
, 512));
519 if (bdrv_pread(s
->hd
, l1_table_offset
,
520 l1_table
, l1_size2
) != l1_size2
)
522 for(i
= 0;i
< l1_size
; i
++)
523 be64_to_cpus(&l1_table
[i
]);
525 assert(l1_size
== s
->l1_size
);
526 l1_table
= s
->l1_table
;
530 l2_size
= s
->l2_size
* sizeof(uint64_t);
531 l2_table
= qemu_malloc(l2_size
);
533 for(i
= 0; i
< l1_size
; i
++) {
534 l2_offset
= l1_table
[i
];
536 old_l2_offset
= l2_offset
;
537 l2_offset
&= ~QCOW_OFLAG_COPIED
;
539 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, l2_size
) != l2_size
)
541 for(j
= 0; j
< s
->l2_size
; j
++) {
542 offset
= be64_to_cpu(l2_table
[j
]);
545 offset
&= ~QCOW_OFLAG_COPIED
;
546 if (offset
& QCOW_OFLAG_COMPRESSED
) {
547 nb_csectors
= ((offset
>> s
->csize_shift
) &
550 update_refcount(bs
, (offset
& s
->cluster_offset_mask
) & ~511,
551 nb_csectors
* 512, addend
);
552 /* compressed clusters are never modified */
556 refcount
= update_cluster_refcount(bs
, offset
>> s
->cluster_bits
, addend
);
558 refcount
= get_refcount(bs
, offset
>> s
->cluster_bits
);
563 offset
|= QCOW_OFLAG_COPIED
;
565 if (offset
!= old_offset
) {
566 l2_table
[j
] = cpu_to_be64(offset
);
572 if (bdrv_pwrite(s
->hd
,
573 l2_offset
, l2_table
, l2_size
) != l2_size
)
578 refcount
= update_cluster_refcount(bs
, l2_offset
>> s
->cluster_bits
, addend
);
580 refcount
= get_refcount(bs
, l2_offset
>> s
->cluster_bits
);
583 l2_offset
|= QCOW_OFLAG_COPIED
;
585 if (l2_offset
!= old_l2_offset
) {
586 l1_table
[i
] = l2_offset
;
592 for(i
= 0; i
< l1_size
; i
++)
593 cpu_to_be64s(&l1_table
[i
]);
594 if (bdrv_pwrite(s
->hd
, l1_table_offset
, l1_table
,
595 l1_size2
) != l1_size2
)
597 for(i
= 0; i
< l1_size
; i
++)
598 be64_to_cpus(&l1_table
[i
]);
603 cache_refcount_updates
= 0;
604 write_refcount_block(s
);
610 cache_refcount_updates
= 0;
611 write_refcount_block(s
);
618 /*********************************************************/
619 /* refcount checking functions */
624 * Increases the refcount for a range of clusters in a given refcount table.
625 * This is used to construct a temporary refcount table out of L1 and L2 tables
626 * which can be compared the the refcount table saved in the image.
628 * Returns the number of errors in the image that were found
630 static int inc_refcounts(BlockDriverState
*bs
,
631 uint16_t *refcount_table
,
632 int refcount_table_size
,
633 int64_t offset
, int64_t size
)
635 BDRVQcowState
*s
= bs
->opaque
;
636 int64_t start
, last
, cluster_offset
;
643 start
= offset
& ~(s
->cluster_size
- 1);
644 last
= (offset
+ size
- 1) & ~(s
->cluster_size
- 1);
645 for(cluster_offset
= start
; cluster_offset
<= last
;
646 cluster_offset
+= s
->cluster_size
) {
647 k
= cluster_offset
>> s
->cluster_bits
;
648 if (k
< 0 || k
>= refcount_table_size
) {
649 fprintf(stderr
, "ERROR: invalid cluster offset=0x%" PRIx64
"\n",
653 if (++refcount_table
[k
] == 0) {
654 fprintf(stderr
, "ERROR: overflow cluster offset=0x%" PRIx64
655 "\n", cluster_offset
);
665 * Increases the refcount in the given refcount table for the all clusters
666 * referenced in the L2 table. While doing so, performs some checks on L2
669 * Returns the number of errors found by the checks or -errno if an internal
672 static int check_refcounts_l2(BlockDriverState
*bs
,
673 uint16_t *refcount_table
, int refcount_table_size
, int64_t l2_offset
,
676 BDRVQcowState
*s
= bs
->opaque
;
677 uint64_t *l2_table
, offset
;
678 int i
, l2_size
, nb_csectors
, refcount
;
681 /* Read L2 table from disk */
682 l2_size
= s
->l2_size
* sizeof(uint64_t);
683 l2_table
= qemu_malloc(l2_size
);
685 if (bdrv_pread(s
->hd
, l2_offset
, l2_table
, l2_size
) != l2_size
)
688 /* Do the actual checks */
689 for(i
= 0; i
< s
->l2_size
; i
++) {
690 offset
= be64_to_cpu(l2_table
[i
]);
692 if (offset
& QCOW_OFLAG_COMPRESSED
) {
693 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
694 if (offset
& QCOW_OFLAG_COPIED
) {
695 fprintf(stderr
, "ERROR: cluster %" PRId64
": "
696 "copied flag must never be set for compressed "
697 "clusters\n", offset
>> s
->cluster_bits
);
698 offset
&= ~QCOW_OFLAG_COPIED
;
702 /* Mark cluster as used */
703 nb_csectors
= ((offset
>> s
->csize_shift
) &
705 offset
&= s
->cluster_offset_mask
;
706 errors
+= inc_refcounts(bs
, refcount_table
,
708 offset
& ~511, nb_csectors
* 512);
710 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
712 uint64_t entry
= offset
;
713 offset
&= ~QCOW_OFLAG_COPIED
;
714 refcount
= get_refcount(bs
, offset
>> s
->cluster_bits
);
715 if ((refcount
== 1) != ((entry
& QCOW_OFLAG_COPIED
) != 0)) {
716 fprintf(stderr
, "ERROR OFLAG_COPIED: offset=%"
717 PRIx64
" refcount=%d\n", entry
, refcount
);
722 /* Mark cluster as used */
723 offset
&= ~QCOW_OFLAG_COPIED
;
724 errors
+= inc_refcounts(bs
, refcount_table
,
726 offset
, s
->cluster_size
);
728 /* Correct offsets are cluster aligned */
729 if (offset
& (s
->cluster_size
- 1)) {
730 fprintf(stderr
, "ERROR offset=%" PRIx64
": Cluster is not "
731 "properly aligned; L2 entry corrupted.\n", offset
);
742 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
748 * Increases the refcount for the L1 table, its L2 tables and all referenced
749 * clusters in the given refcount table. While doing so, performs some checks
750 * on L1 and L2 entries.
752 * Returns the number of errors found by the checks or -errno if an internal
755 static int check_refcounts_l1(BlockDriverState
*bs
,
756 uint16_t *refcount_table
,
757 int refcount_table_size
,
758 int64_t l1_table_offset
, int l1_size
,
761 BDRVQcowState
*s
= bs
->opaque
;
762 uint64_t *l1_table
, l2_offset
, l1_size2
;
763 int i
, refcount
, ret
;
766 l1_size2
= l1_size
* sizeof(uint64_t);
768 /* Mark L1 table as used */
769 errors
+= inc_refcounts(bs
, refcount_table
, refcount_table_size
,
770 l1_table_offset
, l1_size2
);
772 /* Read L1 table entries from disk */
773 l1_table
= qemu_malloc(l1_size2
);
774 if (bdrv_pread(s
->hd
, l1_table_offset
,
775 l1_table
, l1_size2
) != l1_size2
)
777 for(i
= 0;i
< l1_size
; i
++)
778 be64_to_cpus(&l1_table
[i
]);
780 /* Do the actual checks */
781 for(i
= 0; i
< l1_size
; i
++) {
782 l2_offset
= l1_table
[i
];
784 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
786 refcount
= get_refcount(bs
, (l2_offset
& ~QCOW_OFLAG_COPIED
)
788 if ((refcount
== 1) != ((l2_offset
& QCOW_OFLAG_COPIED
) != 0)) {
789 fprintf(stderr
, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
790 " refcount=%d\n", l2_offset
, refcount
);
795 /* Mark L2 table as used */
796 l2_offset
&= ~QCOW_OFLAG_COPIED
;
797 errors
+= inc_refcounts(bs
, refcount_table
,
802 /* L2 tables are cluster aligned */
803 if (l2_offset
& (s
->cluster_size
- 1)) {
804 fprintf(stderr
, "ERROR l2_offset=%" PRIx64
": Table is not "
805 "cluster aligned; L1 entry corrupted\n", l2_offset
);
809 /* Process and check L2 entries */
810 ret
= check_refcounts_l2(bs
, refcount_table
, refcount_table_size
,
811 l2_offset
, check_copied
);
822 fprintf(stderr
, "ERROR: I/O error in check_refcounts_l1\n");
828 * Checks an image for refcount consistency.
830 * Returns 0 if no errors are found, the number of errors in case the image is
831 * detected as corrupted, and -errno when an internal error occured.
833 int qcow2_check_refcounts(BlockDriverState
*bs
)
835 BDRVQcowState
*s
= bs
->opaque
;
837 int nb_clusters
, refcount1
, refcount2
, i
;
839 uint16_t *refcount_table
;
842 size
= bdrv_getlength(s
->hd
);
843 nb_clusters
= size_to_clusters(s
, size
);
844 refcount_table
= qemu_mallocz(nb_clusters
* sizeof(uint16_t));
847 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
850 /* current L1 table */
851 ret
= check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
852 s
->l1_table_offset
, s
->l1_size
, 1);
859 for(i
= 0; i
< s
->nb_snapshots
; i
++) {
860 sn
= s
->snapshots
+ i
;
861 check_refcounts_l1(bs
, refcount_table
, nb_clusters
,
862 sn
->l1_table_offset
, sn
->l1_size
, 0);
864 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
865 s
->snapshots_offset
, s
->snapshots_size
);
868 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
869 s
->refcount_table_offset
,
870 s
->refcount_table_size
* sizeof(uint64_t));
871 for(i
= 0; i
< s
->refcount_table_size
; i
++) {
873 offset
= s
->refcount_table
[i
];
875 errors
+= inc_refcounts(bs
, refcount_table
, nb_clusters
,
876 offset
, s
->cluster_size
);
880 /* compare ref counts */
881 for(i
= 0; i
< nb_clusters
; i
++) {
882 refcount1
= get_refcount(bs
, i
);
883 refcount2
= refcount_table
[i
];
884 if (refcount1
!= refcount2
) {
885 fprintf(stderr
, "ERROR cluster %d refcount=%d reference=%d\n",
886 i
, refcount1
, refcount2
);
891 qemu_free(refcount_table
);