mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] dm thin: complete writes only after the metadata commit
@ 2026-12-01  7:25 Peng Yu
  2026-12-01  7:25 ` [PATCH v2 1/2] dm thin metadata: keep the changed flags until the commit succeeds Peng Yu
  2026-12-01  7:25 ` [PATCH v2 2/2] dm thin: add wait_for_commit pool feature Peng Yu
  0 siblings, 2 replies; 3+ messages in thread
From: Peng Yu @ 2026-12-01  7:25 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka, Benjamin Marzinski
  Cc: Joe Thornber, dm-devel, linux-doc, linux-kernel, Peng Yu

This series adds a new option to the dm thin pool: wait_for_commit. When
wait_for_commit is enabled, a write or a discard will return after the
metadata is committed. By default, wait_for_commit is disabled, and the
option changes nothing for the thin pool.

Patch 1 is a bug fix that does not depend on the option. After a failed
metadata commit, a FLUSH or FUA write could succeed although the thin
device had lost changes. Sashiko's review of v1 pointed to it. Patch 2
relies on it for what happens after a failed commit.

Why we need wait_for_commit:

Assume there is a server A, and the underlying device of its thin pool
is an NVMe-oF device. Server A creates a thin device and exports the
thin device to a remote host via NVMe-oF too. When server A fails, a
control plane will rebuild the pool and thin device on a standby
server B, and server B will export the thin device to the same remote
host. When the remote host issues a FLUSH, server A fails, so the
metadata are not committed to the pool. The thin pool fails over to
server B, and then the NVMe-oF multipath resends the FLUSH to B.
B receives the command and finds nothing to flush. In this case, some
uncommitted data are lost, but the remote host can't get any error.

As the dm thin pool doc says: "the thin-provisioning target behaves like
a physical disk that has a volatile write cache". If the underlying disk
of a thin pool is a remote disk and we want to fail over the thin pool
from server A to server B, the "volatile write cache" is only on
server A, and server B doesn't know about it, so we will get a
consistency issue. The wait_for_commit option could fix this issue with
a performance penalty.

Below is a performance test on my laptop. We got the results in a VM on
loop devices. The numbers are the ones of v1. I did not measure them
again, because v2 does not change the write paths that they exercise.

1 MiB sequential first writes (no zero fill)
|                      | 1 job, depth 1           | 8 jobs, depth 8          |
|                      | MiB/s | mean ms | p99 ms | MiB/s | mean ms | p99 ms |
| no wait_for_commit   | 1307  | 0.65    | 1.59   | 1596  | 39.7    | 276    |
| with wait_for_commit | 131   | 7.57    | 17.7   | 776   | 78.2    | 148    |

4 KiB random writes, mostly to new blocks (allocation and zero fill)
|                      | 1 job, depth 1, 2000 writes | 8 jobs, depth 2, 10000 writes |
|                      | s       | IOPS   | p99 ms   | s       | IOPS    | p99 ms    |
| no wait_for_commit   | 1.16    | 1723   | 0.97     | 4.96    | 2066    | 15.3      |
| with wait_for_commit | 10.5    | 195    | 11.6     | 15.1    | 673     | 49.5      |

4 KiB random overwrites of mapped blocks, 4 jobs, depth 16
|                      | IOPS   | mean ms | p99 ms |
| no wait_for_commit   | 202095 | 0.31    | 1.12   |
| with wait_for_commit | 203293 | 0.31    | 1.13   |

Changes since v1:
* New patch 1: keep the changed flags of the thin devices until the
  commit has succeeded. It replaces v1's patch 1, whose commit_failed
  flag worked around the same problem for plain writes only.
* Patch 2: a plain write is now tested exactly like a FUA write, a
  discard keeps its cell until the commit, and the worker is woken after
  the waiting discards are completed. The notes of patch 2 have the
  reasons for the findings of Sashiko's review that did not lead to a
  change.
* Rebased on the for-next branch of linux-dm.git.

v1: https://lore.kernel.org/all/20261125043325.138351-1-yupeng0921@gmail.com/
Sashiko's review of v1 patch 1: https://lore.kernel.org/all/sashiko-outbox-232598@kernel.org/
Sashiko's review of v1 patch 2: https://lore.kernel.org/all/sashiko-outbox-232600@kernel.org/

Peng Yu (2):
  dm thin metadata: keep the changed flags until the commit succeeds
  dm thin: add wait_for_commit pool feature

 .../device-mapper/thin-provisioning.rst       |  18 ++-
 drivers/md/dm-thin-metadata.c                 |  32 +++-
 drivers/md/dm-thin-metadata.h                 |   6 +
 drivers/md/dm-thin.c                          | 153 +++++++++++++++---
 4 files changed, 182 insertions(+), 27 deletions(-)


base-commit: 5feb56b402e9827d599b4076da9ad71c374b9ef5
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 1/2] dm thin metadata: keep the changed flags until the commit succeeds
  2026-12-01  7:25 [PATCH v2 0/2] dm thin: complete writes only after the metadata commit Peng Yu
@ 2026-12-01  7:25 ` Peng Yu
  2026-12-01  7:25 ` [PATCH v2 2/2] dm thin: add wait_for_commit pool feature Peng Yu
  1 sibling, 0 replies; 3+ messages in thread
From: Peng Yu @ 2026-12-01  7:25 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka, Benjamin Marzinski
  Cc: Joe Thornber, dm-devel, linux-doc, linux-kernel, Peng Yu,
	Sashiko, stable

The code clears td->changed before a commit succeeds. So we may get the
issue below:
When a commit fails, dm-thin aborts the transaction, and
__set_abort_with_changes_flags() copies td->changed to
td->aborted_with_changes. td->changed is already false, so the abort
records that no thin device lost changes. A later FLUSH or FUA write
then succeeds: bio_triggers_commit() sees no changes, and issue() and
complete_overwrite_bio() do not get to the dm_thin_aborted_changes()
check. The changes of the aborted transaction are lost and nothing
reports an error.

Clear td->changed, and free the closed devices, only after
dm_tm_commit() has succeeded.

Reproducer. The metadata device fails writes but not reads, so the pool
becomes read-only and does not go to the fail mode:

  dmsetup create meta --table "0 16384 linear $META 0"
  dd if=/dev/zero of=/dev/mapper/meta bs=4k count=1 oflag=direct
  dmsetup create pool --table \
    "0 2097152 thin-pool /dev/mapper/meta $DATA 128 0"
  dmsetup message pool 0 "create_thin 0"
  dmsetup create thin --table "0 2097152 thin /dev/mapper/pool 0"
  # map block 0 and commit
  dd if=/dev/zero of=/dev/mapper/thin bs=4k count=1 oflag=direct \
    conv=fsync
  # from now on writes to the metadata device fail
  dmsetup suspend meta
  dmsetup load meta --table "0 16384 flakey $META 0 0 1 1 error_writes"
  dmsetup resume meta
  # provision block 8; the commit one second later fails
  dd if=/dev/urandom of=/dev/mapper/thin bs=4k seek=128 count=1 \
    oflag=direct
  sleep 3
  dmsetup status pool   # ro, needs_check
  # FUA write to block 0
  dd if=/dev/zero of=/dev/mapper/thin bs=4k count=1 oflag=direct,dsync

Without this patch the last dd succeeds although block 8 was lost. With
this patch it fails with an I/O error.

Fixes: da105ed5fd7e ("dm thin metadata: introduce dm_pool_abort_metadata")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-232598@kernel.org/
Cc: stable@vger.kernel.org
Signed-off-by: Peng Yu <yupeng0921@gmail.com>
Assisted-by: Claude:claude-fable-5-2 [Claude Code]
---
New in v2. Sashiko's review of v1 patch 1 asked if the clearing of
td->changed could wait until dm_tm_commit() has succeeded. v1 had a
commit_failed flag instead, which covered only the plain writes of the
new feature. This patch fixes the cause for the FLUSH and FUA writes of
every pool, and patch 2 no longer needs the flag.

Tested with the reproducer above. A second thin device of the same pool
that had no uncommitted changes still completes FLUSH and FUA writes
after the failed commit.

Sashiko's review of v1 patch 1: https://lore.kernel.org/all/sashiko-outbox-232598@kernel.org/
v1: https://lore.kernel.org/all/20261125043325.138351-1-yupeng0921@gmail.com/

 drivers/md/dm-thin-metadata.c | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/drivers/md/dm-thin-metadata.c b/drivers/md/dm-thin-metadata.c
index 8fca13dcb..ecea21dd1 100644
--- a/drivers/md/dm-thin-metadata.c
+++ b/drivers/md/dm-thin-metadata.c
@@ -852,11 +852,11 @@ static int __begin_transaction(struct dm_pool_metadata *pmd)
 static int __write_changed_details(struct dm_pool_metadata *pmd)
 {
 	int r;
-	struct dm_thin_device *td, *tmp;
+	struct dm_thin_device *td;
 	struct disk_device_details details;
 	uint64_t key;
 
-	list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
+	list_for_each_entry(td, &pmd->thin_devices, list) {
 		if (!td->changed)
 			continue;
 
@@ -872,6 +872,23 @@ static int __write_changed_details(struct dm_pool_metadata *pmd)
 				    &key, &details, &pmd->details_root);
 		if (r)
 			return r;
+	}
+
+	return 0;
+}
+
+/*
+ * The commit has succeeded, so the details of the changed devices are on
+ * disk.  The changed flags must stay set until now: if the commit fails,
+ * dm_pool_abort_metadata() uses them to tell which devices lost changes.
+ */
+static void __clear_changed_details(struct dm_pool_metadata *pmd)
+{
+	struct dm_thin_device *td, *tmp;
+
+	list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
+		if (!td->changed)
+			continue;
 
 		if (td->open_count)
 			td->changed = false;
@@ -880,8 +897,6 @@ static int __write_changed_details(struct dm_pool_metadata *pmd)
 			kfree(td);
 		}
 	}
-
-	return 0;
 }
 
 static int __commit_transaction(struct dm_pool_metadata *pmd)
@@ -937,7 +952,13 @@ static int __commit_transaction(struct dm_pool_metadata *pmd)
 
 	copy_sm_roots(pmd, disk_super);
 
-	return dm_tm_commit(pmd->tm, sblock);
+	r = dm_tm_commit(pmd->tm, sblock);
+	if (r < 0)
+		return r;
+
+	__clear_changed_details(pmd);
+
+	return 0;
 }
 
 static void __set_metadata_reserve(struct dm_pool_metadata *pmd)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 2/2] dm thin: add wait_for_commit pool feature
  2026-12-01  7:25 [PATCH v2 0/2] dm thin: complete writes only after the metadata commit Peng Yu
  2026-12-01  7:25 ` [PATCH v2 1/2] dm thin metadata: keep the changed flags until the commit succeeds Peng Yu
@ 2026-12-01  7:25 ` Peng Yu
  1 sibling, 0 replies; 3+ messages in thread
From: Peng Yu @ 2026-12-01  7:25 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka, Benjamin Marzinski
  Cc: Joe Thornber, dm-devel, linux-doc, linux-kernel, Peng Yu

Add the wait_for_commit option to the thin pool. When it is set, a write
or a discard will be completed after the metadata changes are committed.
We perform the actions below to make sure of it:
* In the worker, a plain write is treated like a FUA write for the
  commit.
* Add a new `changed` field to the struct dm_thin_lookup_result. If
  the `changed` is true, the write will be handled by the worker.
* A write that covers a whole new block still sends its data at once.
  Its completion waits for the commit, similar to a FUA write.
* A discard will wait for the removal of its mapping. A discard may
  remove multiple mappings, and the discard will wait until all these
  mapping removals are committed. The discard keeps its cell until
  then, so that no other IO to the range sees the discard before the
  commit.

After a failed commit a plain write fails where a FUA write fails. This
relies on the previous patch.

We change the version of the thin-pool and thin targets to 1.25.0.

Signed-off-by: Peng Yu <yupeng0921@gmail.com>
Assisted-by: Claude:claude-opus-5-5 [Claude Code]
Assisted-by: Claude:claude-fable-5-2 [Claude Code]
---
Findings of Sashiko's review of v1 patch 2 that did not lead to a
change:
* "__issue() defers plain writes after releasing virtual prison cell",
  and the reordering half of "intermediate commit in alloc_data_block()
  reorders or fails writes". A write that waits on deferred_flush_bios
  has not completed. Until it completes, a read may return the old data,
  and a second write to the same block has no defined order against it.
  Mainline holds FUA writes on the same list after their cell is
  released.
* The failing half of the same finding. If the commit in
  alloc_data_block() succeeds and the pool then runs out of metadata
  space, the commit at the end of the pass fails, and so does the
  waiting IO although its changes are committed. Mainline does the same
  to the FLUSH and FUA bios on these lists. The error is on the safe
  side.
* "process_prepared_discard_passdown_pt2() skips commit after passdown".
  The feature promises that the removal of the mapping is on disk when
  the discard completes. Here it is: the removal was committed between
  pt1 and pt2. What is not committed is the decrement in the data space
  map. If the node crashes before the next commit, the blocks stay
  allocated without a mapping until the pool is repaired. That window
  exists without the feature too, and holding the discard for one more
  commit would not close it.
* "process_discard_cell_passdown() completes unmapped discards early".
  The example is an earlier discard of the same range in the same
  transaction. That discard holds the virtual cell of the range until
  its bio ends, which is after the commit, so the later discard is not
  processed before the commit. A discard that finds nothing to remove
  for another reason changes nothing.

Sashiko's review of v1 patch 2: https://lore.kernel.org/all/sashiko-outbox-232600@kernel.org/
v1: https://lore.kernel.org/all/20261125043325.138351-1-yupeng0921@gmail.com/

 .../device-mapper/thin-provisioning.rst       |  18 ++-
 drivers/md/dm-thin-metadata.c                 |   1 +
 drivers/md/dm-thin-metadata.h                 |   6 +
 drivers/md/dm-thin.c                          | 153 +++++++++++++++---
 4 files changed, 156 insertions(+), 22 deletions(-)

diff --git a/Documentation/admin-guide/device-mapper/thin-provisioning.rst b/Documentation/admin-guide/device-mapper/thin-provisioning.rst
index b2fa49a56..f46acb0b0 100644
--- a/Documentation/admin-guide/device-mapper/thin-provisioning.rst
+++ b/Documentation/admin-guide/device-mapper/thin-provisioning.rst
@@ -138,6 +138,12 @@ means the thin-provisioning target behaves like a physical disk that has
 a volatile write cache.  If power is lost you may lose some recent
 writes.  The metadata should always be consistent in spite of any crash.
 
+When the wait_for_commit feature is enabled, if the thin device of a
+write or a discard operation has uncommitted metadata changes, the
+operation will be completed only after the next commit, so the mapping
+of every completed write is on disk.  It makes every write and discard
+operation wait for the metadata commit like the FLUSH and FUA writes.
+
 If data space is exhausted the pool will either error or queue IO
 according to the configuration (see: error_if_no_space).  If metadata
 space is exhausted or a metadata operation fails: the pool will error IO
@@ -291,6 +297,13 @@ i) Constructor
       error_if_no_space:
 	Error IOs, instead of queueing, if no space.
 
+      wait_for_commit:
+	Complete a write or a discard only after the uncommitted
+	metadata changes of its thin device are committed.  A thin
+	device without uncommitted changes is not affected.  Data
+	durability still needs FLUSH or FUA, as for any device with a
+	write cache.  See "Updating on-disk metadata" above.
+
     Data block size must be between 64KiB (128 sectors) and 1GiB
     (2097152 sectors) inclusive.
 
@@ -302,7 +315,7 @@ ii) Status
       <transaction id> <used metadata blocks>/<total metadata blocks>
       <used data blocks>/<total data blocks> <held metadata root>
       ro|rw|out_of_data_space [no_]discard_passdown [error|queue]_if_no_space
-      needs_check|- metadata_low_watermark
+      needs_check|- metadata_low_watermark [wait_for_commit]
 
     transaction id:
 	A 64-bit number used by userspace to help synchronise with metadata
@@ -354,6 +367,9 @@ ii) Status
 	value internally but userspace needs to know this value to
 	determine if an event was caused by crossing this threshold.
 
+    wait_for_commit:
+	Present when the wait_for_commit feature is on.  Absent otherwise.
+
 iii) Messages
 
     create_thin <dev id>
diff --git a/drivers/md/dm-thin-metadata.c b/drivers/md/dm-thin-metadata.c
index ecea21dd1..f72b2809e 100644
--- a/drivers/md/dm-thin-metadata.c
+++ b/drivers/md/dm-thin-metadata.c
@@ -1538,6 +1538,7 @@ static void unpack_lookup_result(struct dm_thin_device *td, __le64 value,
 	unpack_block_time(block_time, &exception_block, &exception_time);
 	result->block = exception_block;
 	result->shared = __snapshotted_since(td, exception_time);
+	result->changed = td->changed;
 }
 
 static int __find_block(struct dm_thin_device *td, dm_block_t block,
diff --git a/drivers/md/dm-thin-metadata.h b/drivers/md/dm-thin-metadata.h
index 2f64f48b5..717822c13 100644
--- a/drivers/md/dm-thin-metadata.h
+++ b/drivers/md/dm-thin-metadata.h
@@ -136,6 +136,12 @@ dm_thin_id dm_thin_dev_id(struct dm_thin_device *td);
 struct dm_thin_lookup_result {
 	dm_block_t block;
 	bool shared:1;
+
+	/*
+	 * The device had uncommitted changes at the time of the lookup, so
+	 * the mapping may not be on disk yet.
+	 */
+	bool changed:1;
 };
 
 /*
diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c
index e695fc356..6485c1950 100644
--- a/drivers/md/dm-thin.c
+++ b/drivers/md/dm-thin.c
@@ -220,6 +220,7 @@ struct pool_features {
 	bool discard_enabled:1;
 	bool discard_passdown:1;
 	bool error_if_no_space:1;
+	bool wait_for_commit:1;
 };
 
 struct thin_c;
@@ -262,6 +263,7 @@ struct pool {
 	spinlock_t lock;
 	struct bio_list deferred_flush_bios;
 	struct bio_list deferred_flush_completions;
+	struct list_head deferred_discard_completions;
 	struct list_head prepared_mappings;
 	struct list_head prepared_discards;
 	struct list_head prepared_discards_pt2;
@@ -741,9 +743,19 @@ static void remap_to_origin(struct thin_c *tc, struct bio *bio)
 	bio_set_dev(bio, tc->origin_dev->bdev);
 }
 
-static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
+static bool wait_for_commit_write(struct thin_c *tc, struct bio *bio)
 {
-	return op_is_flush(bio->bi_opf) &&
+	return tc->pool->pf.wait_for_commit && bio_op(bio) == REQ_OP_WRITE &&
+		!op_is_flush(bio->bi_opf);
+}
+
+/*
+ * @hold says that the bio is a plain write that the wait_for_commit
+ * feature holds for the commit like a FUA write.
+ */
+static int bio_triggers_commit(struct thin_c *tc, struct bio *bio, bool hold)
+{
+	return (hold || op_is_flush(bio->bi_opf)) &&
 		dm_thin_changed_this_transaction(tc->td);
 }
 
@@ -758,11 +770,12 @@ static void inc_all_io_entry(struct pool *pool, struct bio *bio)
 	h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
 }
 
-static void issue(struct thin_c *tc, struct bio *bio)
+static void __issue(struct thin_c *tc, struct bio *bio, bool may_hold)
 {
 	struct pool *pool = tc->pool;
+	bool hold = may_hold && wait_for_commit_write(tc, bio);
 
-	if (!bio_triggers_commit(tc, bio)) {
+	if (!bio_triggers_commit(tc, bio, hold)) {
 		dm_submit_bio_remap(bio, NULL);
 		return;
 	}
@@ -786,6 +799,11 @@ static void issue(struct thin_c *tc, struct bio *bio)
 	spin_unlock_irq(&pool->lock);
 }
 
+static void issue(struct thin_c *tc, struct bio *bio)
+{
+	__issue(tc, bio, true);
+}
+
 static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
 {
 	remap_to_origin(tc, bio);
@@ -969,12 +987,14 @@ static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
 static void complete_overwrite_bio(struct thin_c *tc, struct bio *bio)
 {
 	struct pool *pool = tc->pool;
+	bool hold = wait_for_commit_write(tc, bio);
 
 	/*
 	 * If the bio has the REQ_FUA flag set we must commit the metadata
-	 * before signaling its completion.
+	 * before signaling its completion.  With the wait_for_commit feature
+	 * a plain write waits the same way.
 	 */
-	if (!bio_triggers_commit(tc, bio)) {
+	if (!bio_triggers_commit(tc, bio, hold)) {
 		bio_endio(bio);
 		return;
 	}
@@ -1065,6 +1085,40 @@ static void process_prepared_discard_success(struct dm_thin_new_mapping *m)
 	free_discard_mapping(m);
 }
 
+/*
+ * The metadata work of the discard is done.  With the wait_for_commit
+ * feature the discard is completed, and its cell released, only after the
+ * commit: until then no other IO to the range may see the discard.
+ */
+static void complete_discard_mapping(struct dm_thin_new_mapping *m)
+{
+	struct thin_c *tc = m->tc;
+	struct pool *pool = tc->pool;
+
+	if (!pool->pf.wait_for_commit ||
+	    !dm_thin_changed_this_transaction(tc->td)) {
+		process_prepared_discard_success(m);
+		return;
+	}
+
+	/*
+	 * As for a FUA write: fail the discard if earlier changes to the
+	 * metadata could not be committed.
+	 */
+	if (dm_thin_aborted_changes(tc->td)) {
+		process_prepared_discard_fail(m);
+		return;
+	}
+
+	/*
+	 * process_deferred_bios() commits once for all the IO that waits
+	 * and then completes the discard.
+	 */
+	spin_lock_irq(&pool->lock);
+	list_add_tail(&m->list, &pool->deferred_discard_completions);
+	spin_unlock_irq(&pool->lock);
+}
+
 static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m)
 {
 	int r;
@@ -1073,12 +1127,11 @@ static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m)
 	r = dm_thin_remove_range(tc->td, m->cell->key.block_begin, m->cell->key.block_end);
 	if (r) {
 		metadata_operation_failed(tc->pool, "dm_thin_remove_range", r);
-		bio_io_error(m->bio);
-	} else
-		bio_endio(m->bio);
+		process_prepared_discard_fail(m);
+		return;
+	}
 
-	cell_defer_no_holder(tc, m->cell);
-	mempool_free(m, &tc->pool->mapping_pool);
+	complete_discard_mapping(m);
 }
 
 /*----------------------------------------------------------------*/
@@ -1214,12 +1267,11 @@ static void process_prepared_discard_passdown_pt2(struct dm_thin_new_mapping *m)
 				   m->data_block + (m->virt_end - m->virt_begin));
 	if (r) {
 		metadata_operation_failed(pool, "dm_pool_dec_data_range", r);
-		bio_io_error(m->bio);
-	} else
-		bio_endio(m->bio);
+		process_prepared_discard_fail(m);
+		return;
+	}
 
-	cell_defer_no_holder(tc, m->cell);
-	mempool_free(m, &pool->mapping_pool);
+	complete_discard_mapping(m);
 }
 
 static void process_prepared(struct pool *pool, struct list_head *head,
@@ -1307,7 +1359,14 @@ static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
 	m->bio = bio;
 	save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
 	inc_all_io_entry(pool, bio);
-	remap_and_issue(tc, bio, data_begin);
+	remap(tc, bio, data_begin);
+
+	/*
+	 * The data of a plain write goes out at once, also with the
+	 * wait_for_commit feature: complete_overwrite_bio() holds the
+	 * completion for the commit.
+	 */
+	__issue(tc, bio, false);
 }
 
 /*
@@ -2370,10 +2429,30 @@ static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
 	return NULL;
 }
 
+/*
+ * The metadata work of the discard is done.  This function does nothing
+ * if the wait_for_commit feature is not enabled.  If the wait_for_commit
+ * feature is enabled, the discard completion will be called after committing.
+ */
+static void complete_deferred_discards(struct pool *pool, struct list_head *head,
+				       process_mapping_fn fn)
+{
+	struct dm_thin_new_mapping *m, *tmp;
+
+	if (list_empty(head))
+		return;
+
+	list_for_each_entry_safe(m, tmp, head, list)
+		fn(m);
+
+	wake_worker(pool);
+}
+
 static void process_deferred_bios(struct pool *pool)
 {
 	struct bio *bio;
 	struct bio_list bios, bio_completions;
+	struct list_head discard_completions;
 	struct thin_c *tc;
 
 	tc = get_first_thin(pool);
@@ -2389,6 +2468,7 @@ static void process_deferred_bios(struct pool *pool)
 	 */
 	bio_list_init(&bios);
 	bio_list_init(&bio_completions);
+	INIT_LIST_HEAD(&discard_completions);
 
 	spin_lock_irq(&pool->lock);
 	bio_list_merge(&bios, &pool->deferred_flush_bios);
@@ -2396,9 +2476,12 @@ static void process_deferred_bios(struct pool *pool)
 
 	bio_list_merge(&bio_completions, &pool->deferred_flush_completions);
 	bio_list_init(&pool->deferred_flush_completions);
+
+	list_splice_init(&pool->deferred_discard_completions, &discard_completions);
 	spin_unlock_irq(&pool->lock);
 
 	if (bio_list_empty(&bios) && bio_list_empty(&bio_completions) &&
+	    list_empty(&discard_completions) &&
 	    !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
 		return;
 
@@ -2407,6 +2490,9 @@ static void process_deferred_bios(struct pool *pool)
 
 		while ((bio = bio_list_pop(&bios)))
 			bio_io_error(bio);
+
+		complete_deferred_discards(pool, &discard_completions,
+					   process_prepared_discard_fail);
 		return;
 	}
 	pool->last_commit_jiffies = jiffies;
@@ -2414,6 +2500,9 @@ static void process_deferred_bios(struct pool *pool)
 	while ((bio = bio_list_pop(&bio_completions)))
 		bio_endio(bio);
 
+	complete_deferred_discards(pool, &discard_completions,
+				   process_prepared_discard_success);
+
 	while ((bio = bio_list_pop(&bios))) {
 		/*
 		 * The data device was flushed as part of metadata commit,
@@ -2795,6 +2884,16 @@ static int thin_bio_map(struct dm_target *ti, struct bio *bio)
 			return DM_MAPIO_SUBMITTED;
 		}
 
+		if (result.changed && wait_for_commit_write(tc, bio)) {
+			/*
+			 * The device has uncommitted changes, so the write
+			 * waits for the commit like a FUA write.  The worker
+			 * will work on it.
+			 */
+			thin_defer_cell(tc, virt_cell);
+			return DM_MAPIO_SUBMITTED;
+		}
+
 		build_data_key(tc->td, result.block, &key);
 		if (bio_detain(tc->pool, &key, bio, &data_cell)) {
 			cell_defer_no_holder(tc, virt_cell);
@@ -2922,6 +3021,7 @@ static void pool_features_init(struct pool_features *pf)
 	pf->discard_enabled = true;
 	pf->discard_passdown = true;
 	pf->error_if_no_space = false;
+	pf->wait_for_commit = false;
 }
 
 static void __pool_destroy(struct pool *pool)
@@ -3016,6 +3116,7 @@ static struct pool *pool_create(struct mapped_device *pool_md,
 	spin_lock_init(&pool->lock);
 	bio_list_init(&pool->deferred_flush_bios);
 	bio_list_init(&pool->deferred_flush_completions);
+	INIT_LIST_HEAD(&pool->deferred_discard_completions);
 	INIT_LIST_HEAD(&pool->prepared_mappings);
 	INIT_LIST_HEAD(&pool->prepared_discards);
 	INIT_LIST_HEAD(&pool->prepared_discards_pt2);
@@ -3165,7 +3266,7 @@ static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
 	const char *arg_name;
 
 	static const struct dm_arg _args[] = {
-		{0, 5, "Invalid number of pool feature arguments"},
+		{0, 6, "Invalid number of pool feature arguments"},
 	};
 
 	/*
@@ -3197,6 +3298,9 @@ static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
 		else if (!strcasecmp(arg_name, "error_if_no_space"))
 			pf->error_if_no_space = true;
 
+		else if (!strcasecmp(arg_name, "wait_for_commit"))
+			pf->wait_for_commit = true;
+
 		else {
 			ti->error = "Unrecognised pool feature requested";
 			r = -EINVAL;
@@ -3900,7 +4004,7 @@ static void emit_flags(struct pool_features *pf, char *result,
 {
 	unsigned int count = !pf->zero_new_blocks + !pf->discard_enabled +
 		!pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
-		pf->error_if_no_space;
+		pf->error_if_no_space + pf->wait_for_commit;
 	DMEMIT("%u ", count);
 
 	if (!pf->zero_new_blocks)
@@ -3917,6 +4021,9 @@ static void emit_flags(struct pool_features *pf, char *result,
 
 	if (pf->error_if_no_space)
 		DMEMIT("error_if_no_space ");
+
+	if (pf->wait_for_commit)
+		DMEMIT("wait_for_commit ");
 }
 
 /*
@@ -3924,6 +4031,7 @@ static void emit_flags(struct pool_features *pf, char *result,
  *    <transaction id> <used metadata sectors>/<total metadata sectors>
  *    <used data sectors>/<total data sectors> <held metadata root>
  *    <pool mode> <discard config> <no space config> <needs_check>
+ *    <metadata low watermark> [wait_for_commit]
  */
 static void pool_status(struct dm_target *ti, status_type_t type,
 			unsigned int status_flags, char *result, unsigned int maxlen)
@@ -4034,6 +4142,9 @@ static void pool_status(struct dm_target *ti, status_type_t type,
 
 		DMEMIT("%llu ", (unsigned long long)calc_metadata_threshold(pt));
 
+		if (pool->pf.wait_for_commit)
+			DMEMIT("wait_for_commit ");
+
 		break;
 
 	case STATUSTYPE_TABLE:
@@ -4126,7 +4237,7 @@ static struct target_type pool_target = {
 	.name = "thin-pool",
 	.features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
 		    DM_TARGET_IMMUTABLE | DM_TARGET_PASSES_CRYPTO,
-	.version = {1, 24, 0},
+	.version = {1, 25, 0},
 	.module = THIS_MODULE,
 	.ctr = pool_ctr,
 	.dtr = pool_dtr,
@@ -4504,7 +4615,7 @@ static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
 static struct target_type thin_target = {
 	.name = "thin",
 	.features = DM_TARGET_PASSES_CRYPTO,
-	.version = {1, 24, 0},
+	.version = {1, 25, 0},
 	.module	= THIS_MODULE,
 	.ctr = thin_ctr,
 	.dtr = thin_dtr,
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-01  7:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01  7:25 [PATCH v2 0/2] dm thin: complete writes only after the metadata commit Peng Yu
2026-12-01  7:25 ` [PATCH v2 1/2] dm thin metadata: keep the changed flags until the commit succeeds Peng Yu
2026-12-01  7:25 ` [PATCH v2 2/2] dm thin: add wait_for_commit pool feature Peng Yu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®