All Virtuozzo development lists (kernel + QEMU)
 help / color / mirror / Atom feed
From: "Denis V. Lunev" <den@openvz.org>
To: alexander.ivanov@virtuozzo.com, vasileios.almpanis@virtuozzo.com
Cc: devel@openvz.org
Subject: [Devel] [PATCH hci-8.2] dm-qcow2: truncate the unused tail of an image after backward merge
Date: Thu, 19 Nov 2026 15:31:03 +0100	[thread overview]
Message-ID: <20261119143103.999720-1-den@openvz.org> (raw)

From: Denis V. Lunev <den@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@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");
-		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);
-- 
2.55.0

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

             reply	other threads:[~2026-11-19 14:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 14:31 Denis V. Lunev [this message]
2026-11-19 14:39 ` no-reply
2026-11-19 16:34 ` Alexander Ivanov
2026-11-19 17:57 ` Konstantin Khorenko
2026-11-19 17:58 ` Konstantin Khorenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119143103.999720-1-den@openvz.org \
    --to=den@openvz.org \
    --cc=alexander.ivanov@virtuozzo.com \
    --cc=devel@openvz.org \
    --cc=vasileios.almpanis@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.