[Devel] [PATCH hci-8.2] dm-qcow2: truncate the unused tail of an image after backward merge
Alexander Ivanov
alexander.ivanov at virtuozzo.com
Thu Nov 19 19:34:02 MSK 2026
On 11/19/26 15:31, Denis V. Lunev wrote:
> From: Denis V. Lunev <den at openvz.org>
>
> qcow2_postsuspend() shrinks the top image to the end of its last used
> cluster, which drops freed clusters and the preallocated area. A
> backward merge detaches the merged image in
> qcow2_merge_backward_complete() and never shrinks it: the clusters the
> merge freed and up to the whole preallocation stay at the end of the
> file, and qemu-img check reports them as space leaked at the end of the
> image.
>
> Factor the truncation out of qcow2_postsuspend() into
> qcow2_truncate_unused_tail(tgt, qcow2) and call it for a writable merged
> image before its features are cleared. The device is live there, but
> the image is already unlinked from the stack under seek_rwsem, its
> qios are drained by the ref switch and its md pages written back, so
> qcow2_calc_used_end() sees a stable image. A failure is only logged,
> as the image no longer belongs to the target.
>
> The used end comes from "dm-qcow2: truncate unused tail of image on
> suspend"; the preallocated tail has been left behind since the driver
> was added.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-156099
>
> Fixes: 222b6f1291c1 ("dm-qcow2: Introduce driver to create block devices over QCOW2 files")
> Feature: dm-qcow2: block device over QCOW2 files driver
> Signed-off-by: Denis V. Lunev <den at openvz.org>
> ---
> drivers/md/dm-qcow2-cmd.c | 1 +
> drivers/md/dm-qcow2-map.c | 5 +++--
> drivers/md/dm-qcow2-target.c | 20 +++++++++-----------
> drivers/md/dm-qcow2.h | 1 +
> 4 files changed, 14 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/md/dm-qcow2-cmd.c b/drivers/md/dm-qcow2-cmd.c
> index 3bccce090f01..d94036b94aae 100644
> --- a/drivers/md/dm-qcow2-cmd.c
> +++ b/drivers/md/dm-qcow2-cmd.c
> @@ -391,6 +391,7 @@ static int qcow2_merge_backward_complete(struct qcow2_target *tgt)
> qcow2->lower = NULL;
>
> if (qcow2_file_is_writable(qcow2)) {
> + qcow2_truncate_unused_tail(tgt, qcow2);
> ret = qcow2_set_image_file_features(qcow2, false);
> if (ret < 0)
> QC_ERR(tgt->ti, "Can't unuse merged img (%d)", ret);
> diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
> index 32373114580e..fc070c452144 100644
> --- a/drivers/md/dm-qcow2-map.c
> +++ b/drivers/md/dm-qcow2-map.c
> @@ -2282,8 +2282,9 @@ int qcow2_truncate_safe(struct file *file, loff_t new_len)
> }
>
> /*
> - * Return the end of the last cluster with non-zero refcount. Must be
> - * called on a suspended device after md writeback.
> + * Return the end of the last cluster with non-zero refcount. No qio may
> + * reach @qcow2 and its md must be written back: the device is suspended,
> + * or @qcow2 is unlinked from the stack and drained.
> */
> loff_t qcow2_calc_used_end(struct qcow2 *qcow2)
> {
> diff --git a/drivers/md/dm-qcow2-target.c b/drivers/md/dm-qcow2-target.c
> index 4cd5f2c0ed36..cf5645423cf4 100644
> --- a/drivers/md/dm-qcow2-target.c
> +++ b/drivers/md/dm-qcow2-target.c
> @@ -1072,16 +1072,12 @@ static void qcow2_dtr(struct dm_target *ti)
> qcow2_tgt_destroy(tgt);
> }
>
> -static void qcow2_truncate_unused_tail(struct dm_target *ti)
> +int qcow2_truncate_unused_tail(struct qcow2_target *tgt, struct qcow2 *qcow2)
> {
> - struct qcow2_target *tgt = to_qcow2_target(ti);
> - struct qcow2 *qcow2 = top_qcow2_protected(ti);
> + struct dm_target *ti = tgt->ti;
> loff_t used_end, end = qcow2->file_preallocated_area_start;
> int ret;
>
> - if (!(dm_table_get_mode(ti->table) & FMODE_WRITE))
> - return;
> -
> if (tgt->md_writeback_error) {
> QC_INFO(ti, "Skip shrinking to used end after md writeback error");
> } else if (kernel_sets_dirty_bit && qcow2->opened_dirty) {
> @@ -1095,17 +1091,17 @@ static void qcow2_truncate_unused_tail(struct dm_target *ti)
> }
>
> if (end == qcow2->file_size)
> - return;
> + return 0;
>
> ret = qcow2_truncate_safe(qcow2->file, end);
> if (ret) {
> QC_ERR(ti, "Can't truncate unused tail");
nit: The message now comes from two places, postsuspend for the top image
and merge_backward_complete for the detached one, and the log cannot
tell them apart. The error code is lost as well, since the merge path
ignores the return value. Could we print it here:
QC_ERR(ti, "Can't truncate unused tail: %pe", ERR_PTR(ret));
and add a message with context in qcow2_merge_backward_complete(), like
ret = qcow2_truncate_unused_tail(tgt, qcow2);
if (ret < 0)
QC_ERR(tgt->ti, "Can't unuse merged img (%d)", ret);
?
> - tgt->truncate_error = true;
> - return;
> + return ret;
> }
>
> qcow2->file_preallocated_area_start = end;
> qcow2->file_size = end;
> + return 0;
> }
>
> static void qcow2_io_hints(struct dm_target *ti, struct queue_limits *limits)
> @@ -1177,15 +1173,17 @@ static void qcow2_presuspend_undo(struct dm_target *ti)
> }
> static void qcow2_postsuspend(struct dm_target *ti)
> {
> + struct qcow2_target *tgt = to_qcow2_target(ti);
> struct qcow2 *qcow2 = top_qcow2_protected(ti);
> unsigned int noio_flag;
> int ret;
>
> noio_flag = memalloc_noio_save();
> - qcow2_flush_deferred_activity_all(to_qcow2_target(ti));
> - qcow2_truncate_unused_tail(ti);
> + qcow2_flush_deferred_activity_all(tgt);
>
> if (dm_table_get_mode(ti->table) & FMODE_WRITE) {
> + if (qcow2_truncate_unused_tail(tgt, qcow2))
> + tgt->truncate_error = true;
> ret = qcow2_set_image_file_features(qcow2, false);
> if (ret)
> QC_ERR(ti, "Can't set features");
> diff --git a/drivers/md/dm-qcow2.h b/drivers/md/dm-qcow2.h
> index 3f1d5b5eef26..4acddbe7c95f 100644
> --- a/drivers/md/dm-qcow2.h
> +++ b/drivers/md/dm-qcow2.h
> @@ -428,6 +428,7 @@ void qcow2_ref_dec(struct qcow2_target *tgt, u8 ref_index);
> int qcow2_inflight_ref_switch(struct qcow2_target *tgt);
> void qcow2_flush_deferred_activity(struct qcow2_target *tgt, struct qcow2 *qcow2);
> int qcow2_truncate_safe(struct file *file, loff_t new_len);
> +int qcow2_truncate_unused_tail(struct qcow2_target *tgt, struct qcow2 *qcow2);
> loff_t qcow2_calc_used_end(struct qcow2 *qcow2);
>
> void qcow2_merge_backward_work(struct work_struct *work);
More information about the Devel
mailing list