OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
From: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
To: andrey.zhadchenko@virtuozzo.com
Cc: devel@openvz.org
Subject: [Devel] [PATCH hci-8.1 v8 3/6] dm-qcow2: extend the file for a new refblock in allocate_cluster()
Date: Mon, 26 Oct 2026 15:42:20 +0100	[thread overview]
Message-ID: <20261026144223.2787145-4-alexander.ivanov@virtuozzo.com> (raw)
In-Reply-To: <20261026144223.2787145-1-alexander.ivanov@virtuozzo.com>

place_r2() links a new refblock into R1, but moves neither file_size
nor file_preallocated_area_start. The refblock stays at or above the
watermark if truncate_prealloc_safe() in allocate_cluster() fails
after place_r2(), or if a parallel mark_cluster_unused() lowers
free_cluster_search_pos before the second pass of
find_unused_cluster(). Truncation of preallocations on suspend then
cuts the file under the refblock: R1 on disk points past EOF, and
the next session gets -EIO on allocations in the range it covers.

Make find_unused_cluster() report a position not covered by R2
instead of placing the refblock there itself. allocate_cluster()
prepares that cluster the same way as a data cluster, with the punch,
extend and watermark update moved into prepare_unused_cluster(), and
calls place_r2() before searching again.

https://virtuozzo.atlassian.net/browse/VSTOR-148171
Feature: dm-qcow2: block device over QCOW2 files driver

Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
 drivers/md/dm-qcow2-map.c | 88 ++++++++++++++++++++++-----------------
 1 file changed, 50 insertions(+), 38 deletions(-)

diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
index 78ee7cf5e881..5952b5feb8bf 100644
--- a/drivers/md/dm-qcow2-map.c
+++ b/drivers/md/dm-qcow2-map.c
@@ -2085,21 +2085,12 @@ static int parse_metadata(struct qcow2 *qcow2, struct qio **qio,
  * and connects it to R1 table entry.
  */
 static int place_r2(struct qcow2 *qcow2, struct qcow2_map_item *r1,
-		    struct qcow2_map_item *r2, loff_t r2_pos, struct qio **qio)
+		    struct qcow2_map_item *r2, loff_t r2_pos)
 {
 	struct dm_target *ti = qcow2->tgt->ti;
 	u64 page_id = r2_pos >> PAGE_SHIFT;
 	int ret;
 
-	if (delay_if_writeback(qcow2, r1->md, r1->index_in_page, qio, true))
-		return 0;
-
-	ret = qcow2_punch_hole(qcow2->file, r2_pos, qcow2->clu_size);
-	if (ret) {
-		QC_ERR(ti, "punch hole: %d", ret);
-		return ret;
-	}
-
 	ret = qcow2_alloc_and_insert_md_page(qcow2, page_id, &r2->md);
 	if (ret < 0) {
 		QC_ERR(ti, "Can't alloc: ret=%d, page_id=%llu", ret, page_id);
@@ -2115,7 +2106,7 @@ static int place_r2(struct qcow2 *qcow2, struct qcow2_map_item *r1,
 	spin_unlock_irq(&qcow2->md_pages_lock);
 
 	mark_cluster_used(qcow2, r2->md, r2->index_in_page);
-	return 1;
+	return 0;
 }
 
 static s32 find_unused_block_entry(struct qcow2 *qcow2, struct md_page *md,
@@ -2137,11 +2128,13 @@ static s32 find_unused_block_entry(struct qcow2 *qcow2, struct md_page *md,
 
 static loff_t find_unused_cluster(struct qcow2 *qcow2, struct qio **qio,
 				  struct qcow2_map_item *r1,
-				  struct qcow2_map_item *r2)
+				  struct qcow2_map_item *r2, bool *needs_r2)
 {
 	u32 clu_size = qcow2->clu_size;
 	s32 index, ret;
 	loff_t pos;
+
+	*needs_r2 = false;
 again:
 	pos = READ_ONCE(qcow2->free_cluster_search_pos);
 	if (pos >= qcow2->reftable_max_file_size)
@@ -2153,15 +2146,15 @@ static loff_t find_unused_cluster(struct qcow2 *qcow2, struct qio **qio,
 			return ret;
 		/*
 		 * Since pos is not covered by R2, the whole cluster
-		 * must be unused. Use it to store R2 cluster.
+		 * must be unused. The caller stores R2 cluster there.
 		 * Both indexes must be 0 here, because of we allocate
 		 * clusters from small to big.
 		 */
 		WARN_ON_ONCE(r2->index_in_page != 0 || r2->index != 0);
-		ret = place_r2(qcow2, r1, r2, pos, qio);
-		if (ret <= 0)
-			return ret;
-		goto again;
+		if (delay_if_writeback(qcow2, r1->md, r1->index_in_page, qio, true))
+			return 0;
+		*needs_r2 = true;
+		return pos;
 	}
 
 	spin_lock_irq(&qcow2->md_pages_lock);
@@ -2432,6 +2425,37 @@ static int relocate_refcount_table(struct qcow2 *qcow2, struct qio **qio)
 	return -EIO;
 }
 
+/*
+ * Zero the unused cluster at @pos and make the file size and
+ * file_preallocated_area_start cover it.
+ */
+static int prepare_unused_cluster(struct qcow2 *qcow2, loff_t pos)
+{
+	loff_t off, end = pos + qcow2->clu_size;
+	loff_t old_size = qcow2->file_size;
+	int ret;
+
+	if (pos < qcow2->file_preallocated_area_start) {
+		/* Clu at @pos may contain dirty data */
+		off = min_t(loff_t, old_size, end);
+		ret = qcow2_punch_hole(qcow2->file, pos, off - pos);
+		if (ret) {
+			QC_ERR(qcow2->tgt->ti, "punch hole: %d", ret);
+			return ret;
+		}
+	}
+
+	if (end > old_size) {
+		ret = truncate_prealloc_safe(qcow2, end, __func__);
+		if (ret)
+			return ret;
+	}
+
+	if (end > qcow2->file_preallocated_area_start)
+		qcow2->file_preallocated_area_start = end;
+	return 0;
+}
+
 /*
  * This function is aimed to be called only from main work.
  * In case of wish to use it from more places, it's needed
@@ -2441,13 +2465,12 @@ static int relocate_refcount_table(struct qcow2 *qcow2, struct qio **qio)
 static loff_t allocate_cluster(struct qcow2 *qcow2, struct qio *qio,
 		      struct md_page **r2_md, u32 *r2_index_in_page)
 {
-	u32 clu_size = qcow2->clu_size;
-	struct file *file = qcow2->file;
-	loff_t pos, off, end, old_size;
 	struct qcow2_map_item r1, r2;
+	bool needs_r2;
+	loff_t pos;
 	int ret;
 again:
-	pos = find_unused_cluster(qcow2, &qio, &r1, &r2);
+	pos = find_unused_cluster(qcow2, &qio, &r1, &r2, &needs_r2);
 	if (unlikely(pos == -ENOENT)) {
 		ret = relocate_refcount_table(qcow2, &qio);
 		if (ret <= 0)
@@ -2457,28 +2480,17 @@ static loff_t allocate_cluster(struct qcow2 *qcow2, struct qio *qio,
 	if (pos <= 0)
 		return pos;
 
-	end = pos + clu_size;
-	old_size = qcow2->file_size;
-
-	if (pos < qcow2->file_preallocated_area_start) {
-		/* Clu at @pos may contain dirty data */
-		off = min_t(loff_t, old_size, end);
-		ret = qcow2_punch_hole(file, pos, off - pos);
-		if (ret) {
-			QC_ERR(qcow2->tgt->ti, "punch hole: %d", ret);
-			return ret;
-		}
-	}
+	ret = prepare_unused_cluster(qcow2, pos);
+	if (ret)
+		return ret;
 
-	if (end > old_size) {
-		ret = truncate_prealloc_safe(qcow2, end, __func__);
+	if (needs_r2) {
+		ret = place_r2(qcow2, &r1, &r2, pos);
 		if (ret)
 			return ret;
+		goto again;
 	}
 
-	if (end > qcow2->file_preallocated_area_start)
-		qcow2->file_preallocated_area_start = end;
-
 	mark_cluster_used(qcow2, r2.md, r2.index_in_page);
 	if (r2_md)
 		*r2_md = r2.md;
-- 
2.53.0

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

  parent reply	other threads:[~2026-10-26 14:43 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-26 14:42 [Devel] [PATCH hci-8.1 v8 0/6] dm-qcow2: truncate unused tail of image on suspend Alexander Ivanov
2026-10-26 14:42 ` [Devel] [PATCH hci-8.1 v8 1/6] dm-qcow2: add qcow2_md_page_read_sync() helper Alexander Ivanov
2026-10-26 14:42 ` [Devel] [PATCH hci-8.1 v8 2/6] dm-qcow2: drop duplicate prefix from QC_ERR/QC_INFO messages Alexander Ivanov
2026-10-26 14:42 ` Alexander Ivanov [this message]
2026-10-26 14:42 ` [Devel] [PATCH hci-8.1 v8 4/6] dm-qcow2: complete wbd before ending the data qio Alexander Ivanov
2026-10-26 14:42 ` [Devel] [PATCH hci-8.1 v8 5/6] dm-qcow2: keep the dirty bit of an image opened dirty Alexander Ivanov
2026-10-28 11:59   ` Andrey Zhadchenko
2026-10-26 14:42 ` [Devel] [PATCH hci-8.1 v8 6/6] dm-qcow2: truncate unused tail of image on suspend Alexander Ivanov
2026-10-28 12:41   ` Andrey Zhadchenko
2026-10-28 15:08     ` Alexander Ivanov

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=20261026144223.2787145-4-alexander.ivanov@virtuozzo.com \
    --to=alexander.ivanov@virtuozzo.com \
    --cc=andrey.zhadchenko@virtuozzo.com \
    --cc=devel@openvz.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox