]> 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:32:00 +0000 (14:32 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:04:06 +0000 (21:04 -0500)
btrfs_repair_io_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 error during mount, before SB_BORN, sets fs_error without
  SB_RDONLY.

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

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

index ca75d40a35d0e75097330b9658ad8bb0a515ca27..9fd848b323f2ad65fd445559f2c15f6e8d0310b3 100644 (file)
@@ -929,7 +929,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 8687cf6952d00a6a6c96fea87c2db465d30fd00f..b03b983b7e35e7e3af2c696d1a7303f4680a09ed 100644 (file)
@@ -1143,6 +1143,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, 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);
+}
+
 /*
  * We use folio flag owner_2 to indicate there is an ordered extent with
  * unfinished IO.