]> git.hungrycats.org Git - linux/commitdiff
btrfs: do not write back a read repair after a transaction abort
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Thu, 22 Oct 2026 18:31:48 +0000 (14:31 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 06:07:56 +0000 (01:07 -0500)
btrfs_repair_bbio_failure() writes a repaired data sector or tree block
back to the bad copy unless the filesystem is read-only.  SB_RDONLY is
not the only state that must stop that write:

- A transaction abort sets BTRFS_FS_STATE_TRANS_ABORTED, reports the
  abort (with a stack dump for most errors), and then
  __btrfs_handle_fs_error() sets fs_info->fs_error and only after that
  SB_RDONLY.  A repair that runs in between still writes after the
  abort.
- An emergency shutdown (btrfs_force_shutdown()) sets fs_error but
  deliberately leaves SB_RDONLY clear.  A read that still reaches the
  filesystem after the shutdown, such as a page fault on a mapping made
  before it or a tree block read, repairs a bad copy and writes it
  back.
- An error during mount, before SB_BORN, sets fs_error without
  SB_RDONLY.

Nothing should be written after an abort or a shutdown.  Add
btrfs_can_submit_repair_write(), which also checks the error and the
abort bit, and use it in btrfs_repair_bbio_failure().  That covers data
read repair and tree block repair (btrfs_repair_eb_io_failure()).  The
read still gets the good copy's data.

read-repair-submit-fail-test.sh RR_ARM=shutdown (raid56-wh-harness)
maps a raid1 file whose tail is corrupt on one mirror, shuts the
filesystem down, drops the tail from the page cache and touches it
through the mapping.  Without this patch the read repair writes the
tail back to the corrupt mirror after the shutdown; with it the read
returns the file's data, nothing is written to either device and the
corrupt copy stays as it was.

Fixes: 8ceaad6cd6e7 ("btrfs: do not ASSERT() when the fs flips RO inside btrfs_repair_io_failure()")
Fixes: 9b2839451dfc ("btrfs: introduce a new shutdown state")
Assisted-by: Claude:claude-opus-5-5
fs/btrfs/bio.c
fs/btrfs/fs.h

index 7521e37e516659b2faa9c46d107c5c95e9a813fe..9a67b4b2a9b386401eaa0017d88bc7d354311c87 100644 (file)
@@ -985,7 +985,7 @@ int btrfs_repair_bbio_failure(struct btrfs_bio *bbio, const struct bvec_iter *or
         * The fs either mounted RO or hit critical errors, no need
         * to continue repairing.
         */
-       if (unlikely(sb_rdonly(fs_info->sb)))
+       if (unlikely(!btrfs_can_submit_repair_write(fs_info)))
                return 0;
 
        if (btrfs_repair_one_zone(fs_info, logical))
index 10e15a319b93de61f5eeaf775bd22876c0a2d8ec..c008dba40181ec9c2cd958f727a4da6647d82ebc 100644 (file)
@@ -1216,6 +1216,19 @@ static inline void btrfs_wake_unfinished_drop(struct btrfs_fs_info *fs_info)
        (unlikely(test_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR,            \
                           &(fs_info)->fs_state)))
 
+/*
+ * Return true if a repair write (read repair, scrub) may be submitted: never on
+ * a read-only filesystem, nor after a transaction abort or another
+ * filesystem error.  A transaction abort sets BTRFS_FS_STATE_TRANS_ABORTED,
+ * then the error, and only then SB_RDONLY, and an emergency shutdown sets the
+ * error without SB_RDONLY, so checking SB_RDONLY alone is not enough.
+ */
+static inline bool btrfs_can_submit_repair_write(const struct btrfs_fs_info *fs_info)
+{
+       return !sb_rdonly(fs_info->sb) && !BTRFS_FS_ERROR(fs_info) &&
+              !test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state);
+}
+
 static inline bool btrfs_is_shutdown(const struct btrfs_fs_info *fs_info)
 {
        return unlikely(test_bit(BTRFS_FS_STATE_EMERGENCY_SHUTDOWN, &fs_info->fs_state));