[PATCH 1/2] dm thin metadata: report uncommitted state and refuse commits after a failure

From: Peng Yu

Date: Tue Nov 24 2026 - 23:33:53 EST


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@xxxxxxxxx>
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