Skip to content

Commit 9631c78

Browse files
Vladimir Sementsov-OgievskiyXanClic
authored andcommitted
qcow2-refcount: check_refcounts_l2(): check l2_bitmap
Check subcluster bitmap of the l2 entry for different types of clusters: - for compressed it must be zero - for allocated check consistency of two parts of the bitmap - for unallocated all subclusters should be unallocated (or zero-plain) Signed-off-by: Vladimir Sementsov-Ogievskiy <[email protected]> Tested-by: Kirill Tkhai <[email protected]> Message-Id: <[email protected]> Reviewed-by: Eric Blake <[email protected]> Reviewed-by: Hanna Reitz <[email protected]> Signed-off-by: Hanna Reitz <[email protected]>
1 parent 5c3216c commit 9631c78

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

block/qcow2-refcount.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,7 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
16611661
int flags, BdrvCheckMode fix, bool active)
16621662
{
16631663
BDRVQcow2State *s = bs->opaque;
1664-
uint64_t l2_entry;
1664+
uint64_t l2_entry, l2_bitmap;
16651665
uint64_t next_contiguous_offset = 0;
16661666
int i, ret;
16671667
size_t l2_size_bytes = s->l2_size * l2_entry_size(s);
@@ -1681,6 +1681,7 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
16811681
uint64_t coffset;
16821682
int csize;
16831683
l2_entry = get_l2_entry(s, l2_table, i);
1684+
l2_bitmap = get_l2_bitmap(s, l2_table, i);
16841685

16851686
switch (qcow2_get_cluster_type(bs, l2_entry)) {
16861687
case QCOW2_CLUSTER_COMPRESSED:
@@ -1700,6 +1701,14 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
17001701
break;
17011702
}
17021703

1704+
if (l2_bitmap) {
1705+
fprintf(stderr, "ERROR compressed cluster %d with non-zero "
1706+
"subcluster allocation bitmap, entry=0x%" PRIx64 "\n",
1707+
i, l2_entry);
1708+
res->corruptions++;
1709+
break;
1710+
}
1711+
17031712
/* Mark cluster as used */
17041713
qcow2_parse_compressed_l2_entry(bs, l2_entry, &coffset, &csize);
17051714
ret = qcow2_inc_refcounts_imrt(
@@ -1727,13 +1736,19 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
17271736
{
17281737
uint64_t offset = l2_entry & L2E_OFFSET_MASK;
17291738

1739+
if ((l2_bitmap >> 32) & l2_bitmap) {
1740+
res->corruptions++;
1741+
fprintf(stderr, "ERROR offset=%" PRIx64 ": Allocated "
1742+
"cluster has corrupted subcluster allocation bitmap\n",
1743+
offset);
1744+
}
1745+
17301746
/* Correct offsets are cluster aligned */
17311747
if (offset_into_cluster(s, offset)) {
17321748
bool contains_data;
17331749
res->corruptions++;
17341750

17351751
if (has_subclusters(s)) {
1736-
uint64_t l2_bitmap = get_l2_bitmap(s, l2_table, i);
17371752
contains_data = (l2_bitmap & QCOW_L2_BITMAP_ALL_ALLOC);
17381753
} else {
17391754
contains_data = !(l2_entry & QCOW_OFLAG_ZERO);
@@ -1799,7 +1814,16 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
17991814
}
18001815

18011816
case QCOW2_CLUSTER_ZERO_PLAIN:
1817+
/* Impossible when image has subclusters */
1818+
assert(!l2_bitmap);
1819+
break;
1820+
18021821
case QCOW2_CLUSTER_UNALLOCATED:
1822+
if (l2_bitmap & QCOW_L2_BITMAP_ALL_ALLOC) {
1823+
res->corruptions++;
1824+
fprintf(stderr, "ERROR: Unallocated "
1825+
"cluster has non-zero subcluster allocation map\n");
1826+
}
18031827
break;
18041828

18051829
default:

0 commit comments

Comments
 (0)