Skip to content

Commit a6e0984

Browse files
Vladimir Sementsov-OgievskiyXanClic
authored andcommitted
qcow2: introduce qcow2_parse_compressed_l2_entry() helper
Add helper to parse compressed l2_entry and use it everywhere instead of open-coding. Note, that in most places we move to precise coffset/csize instead of sector-aligned. Still it should work good enough for updating refcounts. Signed-off-by: Vladimir Sementsov-Ogievskiy <[email protected]> Reviewed-by: Eric Blake <[email protected]> Reviewed-by: Hanna Reitz <[email protected]> Message-Id: <[email protected]> Signed-off-by: Hanna Reitz <[email protected]>
1 parent 9a3978a commit a6e0984

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

block/qcow2-cluster.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2480,3 +2480,18 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs,
24802480
g_free(l1_table);
24812481
return ret;
24822482
}
2483+
2484+
void qcow2_parse_compressed_l2_entry(BlockDriverState *bs, uint64_t l2_entry,
2485+
uint64_t *coffset, int *csize)
2486+
{
2487+
BDRVQcow2State *s = bs->opaque;
2488+
int nb_csectors;
2489+
2490+
assert(qcow2_get_cluster_type(bs, l2_entry) == QCOW2_CLUSTER_COMPRESSED);
2491+
2492+
*coffset = l2_entry & s->cluster_offset_mask;
2493+
2494+
nb_csectors = ((l2_entry >> s->csize_shift) & s->csize_mask) + 1;
2495+
*csize = nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE -
2496+
(*coffset & (QCOW2_COMPRESSED_SECTOR_SIZE - 1));
2497+
}

block/qcow2-refcount.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,11 +1177,11 @@ void qcow2_free_any_cluster(BlockDriverState *bs, uint64_t l2_entry,
11771177
switch (ctype) {
11781178
case QCOW2_CLUSTER_COMPRESSED:
11791179
{
1180-
int64_t offset = (l2_entry & s->cluster_offset_mask)
1181-
& QCOW2_COMPRESSED_SECTOR_MASK;
1182-
int size = QCOW2_COMPRESSED_SECTOR_SIZE *
1183-
(((l2_entry >> s->csize_shift) & s->csize_mask) + 1);
1184-
qcow2_free_clusters(bs, offset, size, type);
1180+
uint64_t coffset;
1181+
int csize;
1182+
1183+
qcow2_parse_compressed_l2_entry(bs, l2_entry, &coffset, &csize);
1184+
qcow2_free_clusters(bs, coffset, csize, type);
11851185
}
11861186
break;
11871187
case QCOW2_CLUSTER_NORMAL:
@@ -1247,7 +1247,7 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
12471247
bool l1_allocated = false;
12481248
int64_t old_entry, old_l2_offset;
12491249
unsigned slice, slice_size2, n_slices;
1250-
int i, j, l1_modified = 0, nb_csectors;
1250+
int i, j, l1_modified = 0;
12511251
int ret;
12521252

12531253
assert(addend >= -1 && addend <= 1);
@@ -1318,14 +1318,14 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
13181318

13191319
switch (qcow2_get_cluster_type(bs, entry)) {
13201320
case QCOW2_CLUSTER_COMPRESSED:
1321-
nb_csectors = ((entry >> s->csize_shift) &
1322-
s->csize_mask) + 1;
13231321
if (addend != 0) {
1324-
uint64_t coffset = (entry & s->cluster_offset_mask)
1325-
& QCOW2_COMPRESSED_SECTOR_MASK;
1322+
uint64_t coffset;
1323+
int csize;
1324+
1325+
qcow2_parse_compressed_l2_entry(bs, entry,
1326+
&coffset, &csize);
13261327
ret = update_refcount(
1327-
bs, coffset,
1328-
nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE,
1328+
bs, coffset, csize,
13291329
abs(addend), addend < 0,
13301330
QCOW2_DISCARD_SNAPSHOT);
13311331
if (ret < 0) {
@@ -1603,7 +1603,7 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
16031603
BDRVQcow2State *s = bs->opaque;
16041604
uint64_t l2_entry;
16051605
uint64_t next_contiguous_offset = 0;
1606-
int i, nb_csectors, ret;
1606+
int i, ret;
16071607
size_t l2_size_bytes = s->l2_size * l2_entry_size(s);
16081608
g_autofree uint64_t *l2_table = g_malloc(l2_size_bytes);
16091609

@@ -1617,6 +1617,8 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
16171617

16181618
/* Do the actual checks */
16191619
for (i = 0; i < s->l2_size; i++) {
1620+
uint64_t coffset;
1621+
int csize;
16201622
l2_entry = get_l2_entry(s, l2_table, i);
16211623

16221624
switch (qcow2_get_cluster_type(bs, l2_entry)) {
@@ -1638,13 +1640,9 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
16381640
}
16391641

16401642
/* Mark cluster as used */
1641-
nb_csectors = ((l2_entry >> s->csize_shift) &
1642-
s->csize_mask) + 1;
1643-
l2_entry &= s->cluster_offset_mask;
1643+
qcow2_parse_compressed_l2_entry(bs, l2_entry, &coffset, &csize);
16441644
ret = qcow2_inc_refcounts_imrt(
1645-
bs, res, refcount_table, refcount_table_size,
1646-
l2_entry & QCOW2_COMPRESSED_SECTOR_MASK,
1647-
nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE);
1645+
bs, res, refcount_table, refcount_table_size, coffset, csize);
16481646
if (ret < 0) {
16491647
return ret;
16501648
}

block/qcow2.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4700,17 +4700,12 @@ qcow2_co_preadv_compressed(BlockDriverState *bs,
47004700
size_t qiov_offset)
47014701
{
47024702
BDRVQcow2State *s = bs->opaque;
4703-
int ret = 0, csize, nb_csectors;
4703+
int ret = 0, csize;
47044704
uint64_t coffset;
47054705
uint8_t *buf, *out_buf;
47064706
int offset_in_cluster = offset_into_cluster(s, offset);
47074707

4708-
assert(qcow2_get_cluster_type(bs, l2_entry) == QCOW2_CLUSTER_COMPRESSED);
4709-
4710-
coffset = l2_entry & s->cluster_offset_mask;
4711-
nb_csectors = ((l2_entry >> s->csize_shift) & s->csize_mask) + 1;
4712-
csize = nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE -
4713-
(coffset & ~QCOW2_COMPRESSED_SECTOR_MASK);
4708+
qcow2_parse_compressed_l2_entry(bs, l2_entry, &coffset, &csize);
47144709

47154710
buf = g_try_malloc(csize);
47164711
if (!buf) {

block/qcow2.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@
110110

111111
/* Defined in the qcow2 spec (compressed cluster descriptor) */
112112
#define QCOW2_COMPRESSED_SECTOR_SIZE 512U
113-
#define QCOW2_COMPRESSED_SECTOR_MASK (~(QCOW2_COMPRESSED_SECTOR_SIZE - 1ULL))
114113

115114
/* Must be at least 2 to cover COW */
116115
#define MIN_L2_CACHE_SIZE 2 /* cache entries */
@@ -913,6 +912,8 @@ int qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
913912
uint64_t offset,
914913
int compressed_size,
915914
uint64_t *host_offset);
915+
void qcow2_parse_compressed_l2_entry(BlockDriverState *bs, uint64_t l2_entry,
916+
uint64_t *coffset, int *csize);
916917

917918
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
918919
void qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m);

0 commit comments

Comments
 (0)