qcow2: Define and use QCOW2_COMPRESSED_SECTOR_SIZE

When an L2 table entry points to a compressed cluster the space used
by the data is specified in 512-byte sectors. This size is independent
from BDRV_SECTOR_SIZE and is specific to the qcow2 file format.

The QCOW2_COMPRESSED_SECTOR_SIZE constant defined in this patch makes
this explicit.

Signed-off-by: Alberto Garcia <berto@igalia.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Alberto Garcia 2019-05-10 19:22:54 +03:00 committed by Kevin Wolf
parent 50ba5b2d99
commit b6c246942b
4 changed files with 23 additions and 14 deletions

View file

@ -796,8 +796,9 @@ int qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
return cluster_offset; return cluster_offset;
} }
nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) - nb_csectors =
(cluster_offset >> 9); (cluster_offset + compressed_size - 1) / QCOW2_COMPRESSED_SECTOR_SIZE -
(cluster_offset / QCOW2_COMPRESSED_SECTOR_SIZE);
cluster_offset |= QCOW_OFLAG_COMPRESSED | cluster_offset |= QCOW_OFLAG_COMPRESSED |
((uint64_t)nb_csectors << s->csize_shift); ((uint64_t)nb_csectors << s->csize_shift);

View file

@ -1172,12 +1172,11 @@ void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
switch (ctype) { switch (ctype) {
case QCOW2_CLUSTER_COMPRESSED: case QCOW2_CLUSTER_COMPRESSED:
{ {
int nb_csectors; int64_t offset = (l2_entry & s->cluster_offset_mask)
nb_csectors = ((l2_entry >> s->csize_shift) & & QCOW2_COMPRESSED_SECTOR_MASK;
s->csize_mask) + 1; int size = QCOW2_COMPRESSED_SECTOR_SIZE *
qcow2_free_clusters(bs, (((l2_entry >> s->csize_shift) & s->csize_mask) + 1);
(l2_entry & s->cluster_offset_mask) & ~511, qcow2_free_clusters(bs, offset, size, type);
nb_csectors * 512, type);
} }
break; break;
case QCOW2_CLUSTER_NORMAL: case QCOW2_CLUSTER_NORMAL:
@ -1317,9 +1316,12 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
nb_csectors = ((entry >> s->csize_shift) & nb_csectors = ((entry >> s->csize_shift) &
s->csize_mask) + 1; s->csize_mask) + 1;
if (addend != 0) { if (addend != 0) {
uint64_t coffset = (entry & s->cluster_offset_mask)
& QCOW2_COMPRESSED_SECTOR_MASK;
ret = update_refcount( ret = update_refcount(
bs, (entry & s->cluster_offset_mask) & ~511, bs, coffset,
nb_csectors * 512, abs(addend), addend < 0, nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE,
abs(addend), addend < 0,
QCOW2_DISCARD_SNAPSHOT); QCOW2_DISCARD_SNAPSHOT);
if (ret < 0) { if (ret < 0) {
goto fail; goto fail;
@ -1635,9 +1637,10 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
nb_csectors = ((l2_entry >> s->csize_shift) & nb_csectors = ((l2_entry >> s->csize_shift) &
s->csize_mask) + 1; s->csize_mask) + 1;
l2_entry &= s->cluster_offset_mask; l2_entry &= s->cluster_offset_mask;
ret = qcow2_inc_refcounts_imrt(bs, res, ret = qcow2_inc_refcounts_imrt(
refcount_table, refcount_table_size, bs, res, refcount_table, refcount_table_size,
l2_entry & ~511, nb_csectors * 512); l2_entry & QCOW2_COMPRESSED_SECTOR_MASK,
nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE);
if (ret < 0) { if (ret < 0) {
goto fail; goto fail;
} }

View file

@ -4187,7 +4187,8 @@ qcow2_co_preadv_compressed(BlockDriverState *bs,
coffset = file_cluster_offset & s->cluster_offset_mask; coffset = file_cluster_offset & s->cluster_offset_mask;
nb_csectors = ((file_cluster_offset >> s->csize_shift) & s->csize_mask) + 1; nb_csectors = ((file_cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
csize = nb_csectors * 512 - (coffset & 511); csize = nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE -
(coffset & ~QCOW2_COMPRESSED_SECTOR_MASK);
buf = g_try_malloc(csize); buf = g_try_malloc(csize);
if (!buf) { if (!buf) {

View file

@ -74,6 +74,10 @@
#define MIN_CLUSTER_BITS 9 #define MIN_CLUSTER_BITS 9
#define MAX_CLUSTER_BITS 21 #define MAX_CLUSTER_BITS 21
/* Defined in the qcow2 spec (compressed cluster descriptor) */
#define QCOW2_COMPRESSED_SECTOR_SIZE 512U
#define QCOW2_COMPRESSED_SECTOR_MASK (~(QCOW2_COMPRESSED_SECTOR_SIZE - 1))
/* Must be at least 2 to cover COW */ /* Must be at least 2 to cover COW */
#define MIN_L2_CACHE_SIZE 2 /* cache entries */ #define MIN_L2_CACHE_SIZE 2 /* cache entries */