Linux Documentation
 help / color / mirror / Atom feed
* [PATCH 0/2] dm thin: complete writes only after the metadata commit
@ 2026-11-25  4:33 Peng Yu
  2026-11-25  4:33 ` [PATCH 1/2] dm thin metadata: report uncommitted state and refuse commits after a failure Peng Yu
  2026-11-25  4:33 ` [PATCH 2/2] dm thin: add wait_for_commit pool feature Peng Yu
  0 siblings, 2 replies; 3+ messages in thread
From: Peng Yu @ 2026-11-25  4:33 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
thin pool behaves exactly as before.

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.

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   |

Peng Yu (2):
  dm thin metadata: report uncommitted state and refuse commits after a
    failure
  dm thin: add wait_for_commit pool feature

 .../device-mapper/thin-provisioning.rst       |  25 +++-
 drivers/md/dm-thin-metadata.c                 |  61 +++++++--
 drivers/md/dm-thin-metadata.h                 |  14 +++
 drivers/md/dm-thin.c                          | 116 +++++++++++++++---
 4 files changed, 190 insertions(+), 26 deletions(-)


base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
-- 
2.43.0


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

* [PATCH 1/2] dm thin metadata: report uncommitted state and refuse commits after a failure
  2026-11-25  4:33 [PATCH 0/2] dm thin: complete writes only after the metadata commit Peng Yu
@ 2026-11-25  4:33 ` Peng Yu
  2026-11-25  4:33 ` [PATCH 2/2] dm thin: add wait_for_commit pool feature Peng Yu
  1 sibling, 0 replies; 3+ messages in thread
From: Peng Yu @ 2026-11-25  4:33 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Mikulas Patocka,
	Benjamin Marzinski
  Cc: Joe Thornber, dm-devel, linux-doc, linux-kernel, Peng Yu

Add two flags for a thin pool:
* commit_failed: Set to true if a commit failed. It is a fact that may
  be caused by IO errors.
* refuse_commit_after_failure: Set to true if we enable the
  wait_for_commit option. It is selected by a user.

If both of them are true, the user should repair the pool.

Add a new field:
* changed: A new field of struct dm_thin_lookup_result, set by each
  lookup that finds a mapping. False means the thin device's metadata
  in memory was the same as on disk at the time of the lookup; true
  means they may not be the same.

Add some helper functions:
* dm_thin_uncommitted: Return true if the thin device might have
  metadata changes that are not on disk yet. When we get a commit
  failure, td->changed might be cleared, but the metadata changes are
  not on disk due to the commit failure. So when commit_failed is true,
  the function will always return true.
* dm_pool_metadata_set_refuse_commit_after_failure: Set the
  refuse_commit_after_failure flag.

Signed-off-by: Peng Yu <yupeng0921@gmail.com>
Assisted-by: Claude:claude-opus-5-5 [Claude Code]
Assisted-by: Claude:claude-fable-5-1 [Claude Code]
---
 drivers/md/dm-thin-metadata.c | 61 +++++++++++++++++++++++++++++++----
 drivers/md/dm-thin-metadata.h | 14 ++++++++
 2 files changed, 68 insertions(+), 7 deletions(-)

diff --git a/drivers/md/dm-thin-metadata.c b/drivers/md/dm-thin-metadata.c
index e60e1326376a..7b56c970a524 100644
--- a/drivers/md/dm-thin-metadata.c
+++ b/drivers/md/dm-thin-metadata.c
@@ -221,6 +221,9 @@ struct dm_pool_metadata {
 	 */
 	bool in_service:1;
 
+	bool commit_failed;
+	bool refuse_commit_after_failure;
+
 	/*
 	 * Reading the space map roots can fail, so we read it into these
 	 * buffers before the superblock is locked and updated.
@@ -972,6 +975,8 @@ struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
 	INIT_LIST_HEAD(&pmd->thin_devices);
 	pmd->fail_io = false;
 	pmd->in_service = false;
+	pmd->commit_failed = false;
+	pmd->refuse_commit_after_failure = false;
 	pmd->bdev = bdev;
 	pmd->data_block_size = data_block_size;
 	pmd->pre_commit_fn = NULL;
@@ -1020,10 +1025,15 @@ int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
 
 	pmd_write_lock_in_core(pmd);
 	if (!pmd->fail_io && !dm_bm_is_read_only(pmd->bm)) {
-		r = __commit_transaction(pmd);
-		if (r < 0)
-			DMWARN("%s: __commit_transaction() failed, error = %d",
-			       __func__, r);
+		if (pmd->refuse_commit_after_failure && pmd->commit_failed) {
+			DMERR_LIMIT("%s: refusing to commit after a failed commit",
+				    __func__);
+		} else {
+			r = __commit_transaction(pmd);
+			if (r < 0)
+				DMWARN("%s: __commit_transaction() failed, error = %d",
+				       __func__, r);
+		}
 	}
 	pmd_write_unlock(pmd);
 	__destroy_persistent_data_objects(pmd, true);
@@ -1351,6 +1361,7 @@ static int __reserve_metadata_snap(struct dm_pool_metadata *pmd)
 	 */
 	r = __commit_transaction(pmd);
 	if (r < 0) {
+		pmd->commit_failed = true;
 		DMWARN("%s: __commit_transaction() failed, error = %d",
 		       __func__, r);
 		return r;
@@ -1397,8 +1408,15 @@ int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd)
 	int r = -EINVAL;
 
 	pmd_write_lock(pmd);
-	if (!pmd->fail_io)
-		r = __reserve_metadata_snap(pmd);
+	if (!pmd->fail_io) {
+		if (pmd->refuse_commit_after_failure && pmd->commit_failed) {
+			DMERR_LIMIT("%s: refusing to commit after a failed commit",
+				    __func__);
+			r = -EIO;
+		} else {
+			r = __reserve_metadata_snap(pmd);
+		}
+	}
 	pmd_write_unlock(pmd);
 
 	return r;
@@ -1516,6 +1534,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 || td->pmd->commit_failed;
 }
 
 static int __find_block(struct dm_thin_device *td, dm_block_t block,
@@ -1796,6 +1815,17 @@ bool dm_thin_changed_this_transaction(struct dm_thin_device *td)
 	return r;
 }
 
+bool dm_thin_uncommitted(struct dm_thin_device *td)
+{
+	bool r;
+
+	down_read(&td->pmd->root_lock);
+	r = td->changed || td->pmd->commit_failed;
+	up_read(&td->pmd->root_lock);
+
+	return r;
+}
+
 bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd)
 {
 	bool r = false;
@@ -1836,6 +1866,14 @@ int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
 	return r;
 }
 
+void dm_pool_metadata_set_refuse_commit_after_failure(struct dm_pool_metadata *pmd,
+						      bool refuse_commit_after_failure)
+{
+	pmd_write_lock_in_core(pmd);
+	pmd->refuse_commit_after_failure = refuse_commit_after_failure;
+	pmd_write_unlock(pmd);
+}
+
 int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
 {
 	int r = -EINVAL;
@@ -1848,9 +1886,18 @@ int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
 	if (pmd->fail_io)
 		goto out;
 
+	if (pmd->refuse_commit_after_failure && pmd->commit_failed) {
+		DMERR_LIMIT("%s: refusing to commit after a failed commit",
+			    __func__);
+		r = -EIO;
+		goto out;
+	}
+
 	r = __commit_transaction(pmd);
-	if (r < 0)
+	if (r < 0) {
+		pmd->commit_failed = true;
 		goto out;
+	}
 
 	/*
 	 * Open the next transaction.
diff --git a/drivers/md/dm-thin-metadata.h b/drivers/md/dm-thin-metadata.h
index 2f64f48b5f19..d7b149471e0b 100644
--- a/drivers/md/dm-thin-metadata.h
+++ b/drivers/md/dm-thin-metadata.h
@@ -81,9 +81,16 @@ int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
 /*
  * Commits _all_ metadata changes: device creation, deletion, mapping
  * updates.
+ *
+ * Returns -EIO without committing once a commit has failed, if the pool
+ * asked to refuse commits after a failure (see
+ * dm_pool_metadata_set_refuse_commit_after_failure()).
  */
 int dm_pool_commit_metadata(struct dm_pool_metadata *pmd);
 
+void dm_pool_metadata_set_refuse_commit_after_failure(struct dm_pool_metadata *pmd,
+						      bool refuse_commit_after_failure);
+
 /*
  * Discards all uncommitted changes.  Rereads the superblock, rolling back
  * to the last good transaction.  Thin devices remain open.
@@ -112,6 +119,10 @@ int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
  * operations only.  A small performance hit is incurred by providing this
  * copy of the metadata to userland due to extra copy-on-write operations
  * on the metadata nodes.  Release this as soon as you finish with it.
+ *
+ * The reservation commits first, so it returns -EIO once a commit has
+ * failed, if the pool asked to refuse commits after a failure (see
+ * dm_pool_metadata_set_refuse_commit_after_failure()).
  */
 int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd);
 int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd);
@@ -136,6 +147,7 @@ dm_thin_id dm_thin_dev_id(struct dm_thin_device *td);
 struct dm_thin_lookup_result {
 	dm_block_t block;
 	bool shared:1;
+	bool changed:1;
 };
 
 /*
@@ -175,6 +187,8 @@ int dm_thin_remove_range(struct dm_thin_device *td,
  */
 bool dm_thin_changed_this_transaction(struct dm_thin_device *td);
 
+bool dm_thin_uncommitted(struct dm_thin_device *td);
+
 bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd);
 
 bool dm_thin_aborted_changes(struct dm_thin_device *td);
-- 
2.43.0


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

* [PATCH 2/2] dm thin: add wait_for_commit pool feature
  2026-11-25  4:33 [PATCH 0/2] dm thin: complete writes only after the metadata commit Peng Yu
  2026-11-25  4:33 ` [PATCH 1/2] dm thin metadata: report uncommitted state and refuse commits after a failure Peng Yu
@ 2026-11-25  4:33 ` Peng Yu
  1 sibling, 0 replies; 3+ messages in thread
From: Peng Yu @ 2026-11-25  4:33 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.
* On the direct path of thin_bio_map(), a write to a mapped block whose
  device has uncommitted changes is handed to 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.

With wait_for_commit set, every plain write fails after a failed commit.

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-1 [Claude Code]
---
 .../device-mapper/thin-provisioning.rst       |  25 +++-
 drivers/md/dm-thin.c                          | 116 +++++++++++++++---
 2 files changed, 122 insertions(+), 19 deletions(-)

diff --git a/Documentation/admin-guide/device-mapper/thin-provisioning.rst b/Documentation/admin-guide/device-mapper/thin-provisioning.rst
index b2fa49a5608a..8c5fdb1475af 100644
--- a/Documentation/admin-guide/device-mapper/thin-provisioning.rst
+++ b/Documentation/admin-guide/device-mapper/thin-provisioning.rst
@@ -138,6 +138,19 @@ 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.
 
+With the wait_for_commit feature a write or a discard whose thin device
+has uncommitted metadata changes is completed only after the next
+commit, so the mapping of every completed write is on disk.  Commits
+stay batched: the pool commits once for all the IO that is waiting, and
+IO to blocks whose mapping is already committed is not affected.  The
+feature is for a pool whose devices another node takes over after a
+crash: that node starts from the last commit, and a flush it receives
+cannot cover what the crashed node completed.  It costs a commit per
+batch of allocating writes, and every device of the pool waits for the
+metadata lock while a commit runs.  After a failed commit a pool with
+this feature refuses every later commit and fails its writes until it is
+repaired, because a later commit could not be trusted.
+
 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 +304,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 metadata changes
+	it depends on are committed.  Writes to blocks whose mapping is
+	already committed are 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 +322,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 +374,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.c b/drivers/md/dm-thin.c
index 358ed77ffb2b..93ce83674c63 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;
@@ -732,10 +734,18 @@ 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) &&
-		dm_thin_changed_this_transaction(tc->td);
+	return tc->pool->pf.wait_for_commit && bio_op(bio) == REQ_OP_WRITE &&
+		!op_is_flush(bio->bi_opf);
+}
+
+static int bio_triggers_commit(struct thin_c *tc, struct bio *bio, bool hold)
+{
+	if (op_is_flush(bio->bi_opf))
+		return dm_thin_changed_this_transaction(tc->td);
+
+	return hold && dm_thin_uncommitted(tc->td);
 }
 
 static void inc_all_io_entry(struct pool *pool, struct bio *bio)
@@ -749,11 +759,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;
 	}
@@ -777,6 +788,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);
@@ -960,12 +976,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;
 	}
@@ -1056,6 +1074,28 @@ static void process_prepared_discard_success(struct dm_thin_new_mapping *m)
 	free_discard_mapping(m);
 }
 
+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_uncommitted(tc->td)) {
+		bio_endio(m->bio);
+		mempool_free(m, &pool->mapping_pool);
+		return;
+	}
+
+	if (dm_thin_aborted_changes(tc->td)) {
+		bio_io_error(m->bio);
+		mempool_free(m, &pool->mapping_pool);
+		return;
+	}
+
+	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;
@@ -1065,11 +1105,13 @@ static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m)
 	if (r) {
 		metadata_operation_failed(tc->pool, "dm_thin_remove_range", r);
 		bio_io_error(m->bio);
-	} else
-		bio_endio(m->bio);
+		cell_defer_no_holder(tc, m->cell);
+		mempool_free(m, &tc->pool->mapping_pool);
+		return;
+	}
 
 	cell_defer_no_holder(tc, m->cell);
-	mempool_free(m, &tc->pool->mapping_pool);
+	complete_discard_mapping(m);
 }
 
 /*----------------------------------------------------------------*/
@@ -1206,11 +1248,13 @@ static void process_prepared_discard_passdown_pt2(struct dm_thin_new_mapping *m)
 	if (r) {
 		metadata_operation_failed(pool, "dm_pool_dec_data_range", r);
 		bio_io_error(m->bio);
-	} else
-		bio_endio(m->bio);
+		cell_defer_no_holder(tc, m->cell);
+		mempool_free(m, &pool->mapping_pool);
+		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,
@@ -1298,7 +1342,8 @@ 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);
+	__issue(tc, bio, false);
 }
 
 /*
@@ -2359,6 +2404,8 @@ static void process_deferred_bios(struct pool *pool)
 {
 	struct bio *bio;
 	struct bio_list bios, bio_completions;
+	struct list_head discard_completions;
+	struct dm_thin_new_mapping *m, *tmp;
 	struct thin_c *tc;
 
 	tc = get_first_thin(pool);
@@ -2374,6 +2421,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);
@@ -2381,9 +2429,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;
 
@@ -2392,6 +2443,11 @@ static void process_deferred_bios(struct pool *pool)
 
 		while ((bio = bio_list_pop(&bios)))
 			bio_io_error(bio);
+
+		list_for_each_entry_safe(m, tmp, &discard_completions, list) {
+			bio_io_error(m->bio);
+			mempool_free(m, &pool->mapping_pool);
+		}
 		return;
 	}
 	pool->last_commit_jiffies = jiffies;
@@ -2399,6 +2455,11 @@ static void process_deferred_bios(struct pool *pool)
 	while ((bio = bio_list_pop(&bio_completions)))
 		bio_endio(bio);
 
+	list_for_each_entry_safe(m, tmp, &discard_completions, list) {
+		bio_endio(m->bio);
+		mempool_free(m, &pool->mapping_pool);
+	}
+
 	while ((bio = bio_list_pop(&bios))) {
 		/*
 		 * The data device was flushed as part of metadata commit,
@@ -2778,6 +2839,11 @@ 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)) {
+			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);
@@ -2878,6 +2944,8 @@ static int bind_control_target(struct pool *pool, struct dm_target *ti)
 	pool->ti = ti;
 	pool->pf = pt->adjusted_pf;
 	pool->low_water_blocks = pt->low_water_blocks;
+	dm_pool_metadata_set_refuse_commit_after_failure(pool->pmd,
+							 pool->pf.wait_for_commit);
 
 	set_pool_mode(pool, new_mode);
 
@@ -2903,6 +2971,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)
@@ -2997,6 +3066,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);
@@ -3146,7 +3216,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, 4, "Invalid number of pool feature arguments"},
+		{0, 5, "Invalid number of pool feature arguments"},
 	};
 
 	/*
@@ -3178,6 +3248,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;
@@ -3881,7 +3954,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)
@@ -3898,6 +3971,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 ");
 }
 
 /*
@@ -3905,6 +3981,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)
@@ -4015,6 +4092,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:
@@ -4107,7 +4187,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,
@@ -4490,7 +4570,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 related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-25  4:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  4:33 [PATCH 0/2] dm thin: complete writes only after the metadata commit Peng Yu
2026-11-25  4:33 ` [PATCH 1/2] dm thin metadata: report uncommitted state and refuse commits after a failure Peng Yu
2026-11-25  4:33 ` [PATCH 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