Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] dm thin metadata: inconsistent td->changed state after
  __commit_transaction()
--

commit 0a571384422ac18472855b719999a78add089dae
Author: Peng Yu <[email protected]>

dm thin metadata: report uncommitted state and refuse commits after a failure

This commit tracks metadata commit failures via a pool-wide commit_failed
flag, exposes whether a thin device or lookup result has uncommitted
metadata changes, and optionally refuses further commits after a failure.

> diff --git a/drivers/md/dm-thin-metadata.c b/drivers/md/dm-thin-metadata.c
> index e60e1326376a0..7b56c970a524d 100644
> --- a/drivers/md/dm-thin-metadata.c
> +++ b/drivers/md/dm-thin-metadata.c

[ ... ]

> @@ -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;
> +     }

[Severity: Medium]
Does setting pmd->commit_failed after __commit_transaction() fails leave
td->changed and td->aborted_with_changes inconsistent with the actual
on-disk state?

Inside __commit_transaction(), __write_changed_details() clears
td->changed = false (and frees closed thin devices) before dm_sm_commit(),
dm_tm_pre_commit(), save_sm_roots(), superblock_lock(), and dm_tm_commit()
run:

drivers/md/dm-thin-metadata.c:__write_changed_details() {
        ...
                if (td->open_count)
                        td->changed = false;
                else {
                        list_del(&td->list);
                        kfree(td);
                }
        ...
}

When a commit fails in any of those later steps, td->changed is already
false, while dm_thin_changed_this_transaction(),
dm_pool_changed_this_transaction(), and __set_abort_with_changes_flags()
still read td->changed directly:

drivers/md/dm-thin-metadata.c:__set_abort_with_changes_flags() {
        ...
        list_for_each_entry(td, &pmd->thin_devices, list)
                td->aborted_with_changes = td->changed;
}

When commit() in drivers/md/dm-thin.c fails and calls
metadata_operation_failed() -> dm_pool_abort_metadata(),
td->aborted_with_changes is set to false.  Subsequent REQ_PREFLUSH and
REQ_FUA bios then see both dm_thin_changed_this_transaction() and
dm_thin_aborted_changes() as false in bio_triggers_commit(), and since
wait_for_commit_write() in commit "dm thin: add wait_for_commit pool
feature" skips op_is_flush(bio->bi_opf), those flush and FUA bios silently
succeed instead of failing with an I/O error.

In addition, when pmd->refuse_commit_after_failure is false,
pmd->commit_failed is never cleared if a later commit or abort succeeds,
leaving result->changed and dm_thin_uncommitted() permanently true across
the pool.  Could __write_changed_details() defer clearing td->changed (and
freeing closed td entries) until dm_tm_commit() succeeds?

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to