]> git.hungrycats.org Git - linux/commitdiff
btrfs: give the read repair bio an end_io for submit failures
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Thu, 22 Oct 2026 06:16:13 +0000 (02:16 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 06:07:55 +0000 (01:07 -0500)
repair_one_sector() initializes the repair bbio with a NULL end_io.  A
repair bio normally completes through btrfs_check_read_bio(), which hands
it to btrfs_end_repair_bio() and never calls end_io.  But
btrfs_submit_chunk() can fail a bio before it reaches a device: when
btrfs_map_block() fails, when btrfs_split_bio() fails, or when
btrfs_lookup_bio_sums() fails for a data read.  It then ends the bio with
btrfs_bio_end_io(), which calls bbio->end_io.  For a repair bio that is a
jump to address 0 in a btrfs-endio worker.  The original read never
completes, and an unmount hangs on it later:

  BUG: kernel NULL pointer dereference, address: 0000000000000000
  Workqueue: btrfs-endio simple_end_io_work
  RIP: 0010:0x0
  Call Trace:
   btrfs_bio_end_io+0x1b7/0x420
   btrfs_submit_bbio+0xc1e/0x1fa0
   btrfs_check_read_bio+0x80e/0x1760
   simple_end_io_work+0xc8/0x190

The repair bio's checksum lookup can fail where the original read's
lookup succeeded, because the two search different trees.  A buffered
read of an extent from a past transaction searches the commit root
(csum_search_commit_root).  The repair bbio leaves that flag clear and
searches the running transaction's csum tree.  When that transaction
aborts, the cleanup frees its dirty tree blocks.  A later lookup reads
such a block from disk, where it was never written, and fails with -EIO:

  BTRFS error (device dm-6 state EA): bad tree block start, mirror 2 want 31490048 have 14842989077554535085
  BTRFS error (device dm-6 state EA): bad tree block start, mirror 1 want 31490048 have 8810637562337132983

This was first seen right after a relocation forced the filesystem
read-only: six btrfs-endio workers oopsed within 1.5 seconds.  It
reproduces with a raid1 filesystem:
- Commit a file, write more data so that the csum leaf holding the file's
  tail is COWed in the running transaction, and corrupt the tail on both
  mirrors.
- Abort the transaction by failing a log commit's superblock write, and
  drop the page cache.
- A buffered read of the tail now oopses.

Give the repair bbio an end_io for this case.  A failed submission does
not depend on the mirror: the mapping and the checksum lookup fail the
same way for every copy.  So give up on the sector the way an exhausted
repair does: mark the original bio failed, drop the repair's count on
the failed_bio, and free the repair bio.  The read then fails with -EIO.

Fixes: 7609afac6775 ("btrfs: handle checksum validation and repair at the storage layer")
Cc: stable@vger.kernel.org # 6.6+
Assisted-by: Claude:claude-opus-5-5
fs/btrfs/bio.c

index f8d4c2d550073adad5dce769db954367e26f30b5..212a46af0a2db2ab55a853082f3cf690e0bc912e 100644 (file)
@@ -228,6 +228,26 @@ done:
        bio_put(&repair_bbio->bio);
 }
 
+/*
+ * A repair bio completes through btrfs_check_read_bio() and
+ * btrfs_end_repair_bio(), never through its end_io.  This end_io only runs
+ * when btrfs_submit_bbio() fails the bio before it reaches a device, for
+ * example when the block mapping or the checksum lookup fails.  Neither
+ * depends on the mirror, so give up on the sector instead of trying the
+ * next copy.
+ */
+static void btrfs_repair_bio_submit_failed(struct btrfs_bio *repair_bbio)
+{
+       struct btrfs_failed_bio *fbio = repair_bbio->private;
+
+       btrfs_debug(repair_bbio->inode->root->fs_info,
+                   "repair read submit failed: %d",
+                   blk_status_to_errno(repair_bbio->bio.bi_status));
+       fbio->bbio->bio.bi_status = BLK_STS_IOERR;
+       btrfs_repair_done(fbio);
+       bio_put(&repair_bbio->bio);
+}
+
 /*
  * Try to kick off a repair read to the next available mirror for a bad sector.
  *
@@ -290,7 +310,7 @@ static struct btrfs_failed_bio *repair_one_sector(struct btrfs_bio *failed_bbio,
 
        repair_bbio = btrfs_bio(repair_bio);
        btrfs_bio_init(repair_bbio, failed_bbio->inode, failed_bbio->file_offset + bio_offset,
-                      NULL, fbio);
+                      btrfs_repair_bio_submit_failed, fbio);
 
        mirror = next_repair_mirror(fbio, failed_bbio->mirror_num);
        btrfs_debug(fs_info, "submitting repair read to mirror %d", mirror);