qcow2: put discards in discard queue when discard-no-unref is enabled

When discard-no-unref is enabled, discards are not queued like it
should.
This was broken since discard-no-unref was added.

Add a helper function qcow2_discard_cluster which handles some common
checks and calls the queue_discards function if needed to add the
discard request to the queue.

Signed-off-by: Jean-Louis Dupond <jean-louis@dupond.be>
Message-ID: <20250513132628.1055549-3-jean-louis@dupond.be>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Jean-Louis Dupond
2025-05-13 15:26:28 +02:00
committed by Kevin Wolf
parent 31242df6ca
commit 524d5ba8c0
3 changed files with 27 additions and 10 deletions

View File

@@ -1978,12 +1978,10 @@ discard_in_l2_slice(BlockDriverState *bs, uint64_t offset, uint64_t nb_clusters,
if (!keep_reference) {
/* Then decrease the refcount */
qcow2_free_any_cluster(bs, old_l2_entry, type);
} else if (s->discard_passthrough[type] &&
(cluster_type == QCOW2_CLUSTER_NORMAL ||
cluster_type == QCOW2_CLUSTER_ZERO_ALLOC)) {
} else {
/* If we keep the reference, pass on the discard still */
bdrv_pdiscard(s->data_file, old_l2_entry & L2E_OFFSET_MASK,
s->cluster_size);
qcow2_discard_cluster(bs, old_l2_entry & L2E_OFFSET_MASK,
s->cluster_size, cluster_type, type);
}
}
@@ -2092,12 +2090,10 @@ zero_in_l2_slice(BlockDriverState *bs, uint64_t offset,
if (!keep_reference) {
/* Then decrease the refcount */
qcow2_free_any_cluster(bs, old_l2_entry, QCOW2_DISCARD_REQUEST);
} else if (s->discard_passthrough[QCOW2_DISCARD_REQUEST] &&
(type == QCOW2_CLUSTER_NORMAL ||
type == QCOW2_CLUSTER_ZERO_ALLOC)) {
} else {
/* If we keep the reference, pass on the discard still */
bdrv_pdiscard(s->data_file, old_l2_entry & L2E_OFFSET_MASK,
s->cluster_size);
qcow2_discard_cluster(bs, old_l2_entry & L2E_OFFSET_MASK,
s->cluster_size, type, QCOW2_DISCARD_REQUEST);
}
}
}

View File

@@ -1205,6 +1205,23 @@ void qcow2_free_any_cluster(BlockDriverState *bs, uint64_t l2_entry,
}
}
void qcow2_discard_cluster(BlockDriverState *bs, uint64_t offset,
uint64_t length, QCow2ClusterType ctype,
enum qcow2_discard_type dtype)
{
BDRVQcow2State *s = bs->opaque;
if (s->discard_passthrough[dtype] &&
(ctype == QCOW2_CLUSTER_NORMAL ||
ctype == QCOW2_CLUSTER_ZERO_ALLOC)) {
if (has_data_file(bs)) {
bdrv_pdiscard(s->data_file, offset, length);
} else {
queue_discard(bs, offset, length);
}
}
}
int qcow2_write_caches(BlockDriverState *bs)
{
BDRVQcow2State *s = bs->opaque;

View File

@@ -880,6 +880,10 @@ void GRAPH_RDLOCK qcow2_free_clusters(BlockDriverState *bs,
void GRAPH_RDLOCK
qcow2_free_any_cluster(BlockDriverState *bs, uint64_t l2_entry,
enum qcow2_discard_type type);
void GRAPH_RDLOCK
qcow2_discard_cluster(BlockDriverState *bs, uint64_t offset,
uint64_t length, QCow2ClusterType ctype,
enum qcow2_discard_type dtype);
int GRAPH_RDLOCK
qcow2_update_snapshot_refcount(BlockDriverState *bs, int64_t l1_table_offset,