[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-block] [PULL 02/24] qcow2: Define and use QCOW2_COMPRESSED_SECTOR_
From: |
Kevin Wolf |
Subject: |
[Qemu-block] [PULL 02/24] qcow2: Define and use QCOW2_COMPRESSED_SECTOR_SIZE |
Date: |
Mon, 20 May 2019 18:14:31 +0200 |
From: Alberto Garcia <address@hidden>
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 <address@hidden>
Signed-off-by: Kevin Wolf <address@hidden>
---
block/qcow2.h | 4 ++++
block/qcow2-cluster.c | 5 +++--
block/qcow2-refcount.c | 25 ++++++++++++++-----------
block/qcow2.c | 3 ++-
4 files changed, 23 insertions(+), 14 deletions(-)
diff --git a/block/qcow2.h b/block/qcow2.h
index e62508d1ce..8d92ef1fee 100644
--- a/block/qcow2.h
+++ b/block/qcow2.h
@@ -74,6 +74,10 @@
#define MIN_CLUSTER_BITS 9
#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 */
#define MIN_L2_CACHE_SIZE 2 /* cache entries */
diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index 974a4e8656..b36f4aa84a 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -796,8 +796,9 @@ int qcow2_alloc_compressed_cluster_offset(BlockDriverState
*bs,
return cluster_offset;
}
- nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
- (cluster_offset >> 9);
+ nb_csectors =
+ (cluster_offset + compressed_size - 1) / QCOW2_COMPRESSED_SECTOR_SIZE -
+ (cluster_offset / QCOW2_COMPRESSED_SECTOR_SIZE);
cluster_offset |= QCOW_OFLAG_COMPRESSED |
((uint64_t)nb_csectors << s->csize_shift);
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
index 7481903396..0b09d6838b 100644
--- a/block/qcow2-refcount.c
+++ b/block/qcow2-refcount.c
@@ -1172,12 +1172,11 @@ void qcow2_free_any_clusters(BlockDriverState *bs,
uint64_t l2_entry,
switch (ctype) {
case QCOW2_CLUSTER_COMPRESSED:
{
- int nb_csectors;
- nb_csectors = ((l2_entry >> s->csize_shift) &
- s->csize_mask) + 1;
- qcow2_free_clusters(bs,
- (l2_entry & s->cluster_offset_mask) & ~511,
- nb_csectors * 512, type);
+ int64_t offset = (l2_entry & s->cluster_offset_mask)
+ & QCOW2_COMPRESSED_SECTOR_MASK;
+ int size = QCOW2_COMPRESSED_SECTOR_SIZE *
+ (((l2_entry >> s->csize_shift) & s->csize_mask) + 1);
+ qcow2_free_clusters(bs, offset, size, type);
}
break;
case QCOW2_CLUSTER_NORMAL:
@@ -1317,9 +1316,12 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
nb_csectors = ((entry >> s->csize_shift) &
s->csize_mask) + 1;
if (addend != 0) {
+ uint64_t coffset = (entry & s->cluster_offset_mask)
+ & QCOW2_COMPRESSED_SECTOR_MASK;
ret = update_refcount(
- bs, (entry & s->cluster_offset_mask) & ~511,
- nb_csectors * 512, abs(addend), addend < 0,
+ bs, coffset,
+ nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE,
+ abs(addend), addend < 0,
QCOW2_DISCARD_SNAPSHOT);
if (ret < 0) {
goto fail;
@@ -1635,9 +1637,10 @@ static int check_refcounts_l2(BlockDriverState *bs,
BdrvCheckResult *res,
nb_csectors = ((l2_entry >> s->csize_shift) &
s->csize_mask) + 1;
l2_entry &= s->cluster_offset_mask;
- ret = qcow2_inc_refcounts_imrt(bs, res,
- refcount_table, refcount_table_size,
- l2_entry & ~511, nb_csectors * 512);
+ ret = qcow2_inc_refcounts_imrt(
+ bs, res, refcount_table, refcount_table_size,
+ l2_entry & QCOW2_COMPRESSED_SECTOR_MASK,
+ nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE);
if (ret < 0) {
goto fail;
}
diff --git a/block/qcow2.c b/block/qcow2.c
index 8e024007db..d39882785d 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -4187,7 +4187,8 @@ qcow2_co_preadv_compressed(BlockDriverState *bs,
coffset = file_cluster_offset & s->cluster_offset_mask;
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);
if (!buf) {
--
2.20.1
- [Qemu-block] [PULL 00/24] Block layer patches, Kevin Wolf, 2019/05/20
- [Qemu-block] [PULL 06/24] nvme: fix copy direction in DMA reads going to CMB, Kevin Wolf, 2019/05/20
- [Qemu-block] [PULL 04/24] qmp: forbid qmp_cont in RUN_STATE_FINISH_MIGRATE, Kevin Wolf, 2019/05/20
- [Qemu-block] [PULL 03/24] block: Use BDRV_REQUEST_MAX_BYTES instead of BDRV_REQUEST_MAX_SECTORS, Kevin Wolf, 2019/05/20
- [Qemu-block] [PULL 01/24] block/file-posix: Truncate in xfs_write_zeroes(), Kevin Wolf, 2019/05/20
- [Qemu-block] [PULL 02/24] qcow2: Define and use QCOW2_COMPRESSED_SECTOR_SIZE,
Kevin Wolf <=
- [Qemu-block] [PULL 09/24] block: Move recursion to bdrv_set_aio_context(), Kevin Wolf, 2019/05/20
- [Qemu-block] [PULL 12/24] block: Implement .(can_)set_aio_ctx for BlockBackend, Kevin Wolf, 2019/05/20
- [Qemu-block] [PULL 07/24] block: Add bdrv_try_set_aio_context(), Kevin Wolf, 2019/05/20
- [Qemu-block] [PULL 05/24] iotest: fix 169: do not run qmp_cont in RUN_STATE_FINISH_MIGRATE, Kevin Wolf, 2019/05/20
- [Qemu-block] [PULL 10/24] block: Propagate AioContext change to parents, Kevin Wolf, 2019/05/20
- [Qemu-block] [PULL 11/24] test-block-iothread: Test AioContext propagation through the tree, Kevin Wolf, 2019/05/20
- [Qemu-block] [PULL 16/24] test-block-iothread: Test AioContext propagation for block jobs, Kevin Wolf, 2019/05/20
- [Qemu-block] [PULL 13/24] block: Add blk_set_allow_aio_context_change(), Kevin Wolf, 2019/05/20
- [Qemu-block] [PULL 08/24] block: Make bdrv_attach/detach_aio_context() static, Kevin Wolf, 2019/05/20
- [Qemu-block] [PULL 15/24] blockjob: Remove AioContext notifiers, Kevin Wolf, 2019/05/20