]> git.hungrycats.org Git - linux/commitdiff
raid56-fixes: export patch 11 (RMW writes back repaired sectors); knob is now patch 12
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Wed, 21 Oct 2026 02:28:51 +0000 (22:28 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Wed, 21 Oct 2026 02:28:51 +0000 (22:28 -0400)
Promoted on both lanes 2026-10-21: misc-next raid56-fixes f7e693d10be2,
testing bbc86ab46e84, zb64 3e1390a9de03.  The integration merge with
stripe-alloc conflicts where both insert helpers before rmw_rbio(); the
post-merge fixup carries the resolution (README item 1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B6wxsJn1aY7PwnhgXdz6Ec

16 files changed:
README.md
fixups/post-merge.patch
topics/raid56-fixes/0000-cover-letter.patch
topics/raid56-fixes/0001-btrfs-log-a-message-when-dropping-an-extent-due-to-I.patch
topics/raid56-fixes/0002-btrfs-raid56-fix-use-after-free-of-rbio-in-bio-end_i.patch
topics/raid56-fixes/0003-btrfs-propagate-a-split-bio-s-error-when-the-failing.patch
topics/raid56-fixes/0004-btrfs-raid56-drop-cached-rbios-when-their-stripes-ex.patch
topics/raid56-fixes/0005-btrfs-raid56-verify-reconstructed-tree-blocks.patch
topics/raid56-fixes/0006-btrfs-raid56-keep-a-reference-on-a-stolen-cached-rbi.patch
topics/raid56-fixes/0007-btrfs-raid56-do-not-unhash-a-cached-rbio-that-has-rb.patch
topics/raid56-fixes/0008-btrfs-raid56-do-not-let-a-failed-write-leave-its-pag.patch
topics/raid56-fixes/0009-btrfs-raid56-rebuild-from-Q-when-a-sector-rebuilt-fr.patch
topics/raid56-fixes/0010-btrfs-raid56-count-I-O-errors-in-the-device-stats.patch
topics/raid56-fixes/0011-DO-NOT-UPSTREAM-btrfs-raid56-debug-knob-for-the-rbio.patch [deleted file]
topics/raid56-fixes/0011-btrfs-raid56-write-back-the-sectors-a-sub-stripe-wri.patch [new file with mode: 0644]
topics/raid56-fixes/0012-DO-NOT-UPSTREAM-btrfs-raid56-debug-knob-for-the-rbio.patch [new file with mode: 0644]

index d14727cd68e3b6304e54b9b7dc387748a0145e1d..34eab74ba4786538dba7c2efe3423dcb0a1ec976 100644 (file)
--- a/README.md
+++ b/README.md
@@ -8,8 +8,8 @@ upstream at all (see the table).
 | | |
 |---|---|
 | base of every series | `0c48d26d46bd` ("btrfs: stop reading free space inodes from the commit root"), the misc-next lane base (`misc-next/testing^1`) |
-| integration branch | `misc-next/testing` `cd56f55e64cf` (octopus of all 17 topics) |
-| lane head | `zygo-misc-next-zb64` `923450976193`; its own commits on top of `testing` (kernel configs, `covers/`) are not exported |
+| integration branch | `misc-next/testing` `bbc86ab46e84` (octopus of all 17 topics) |
+| lane head | `zygo-misc-next-zb64` `3e1390a9de03`; its own commits on top of `testing` (kernel configs, `covers/`) are not exported |
 
 The base is not a published commit: the lane rebuilds it from
 btrfs/for-next plus David Sterba's queue.  It lives in the
@@ -67,7 +67,7 @@ the integration recipe below uses.
 | `delalloc-fixes` | 1 | `7c23eb7ad5c9` | Zygo | from misc-next `covers/`, rewritten 2026-10-19 for 0/1 | patch 1 (delalloc leak) dropped 2026-10-19: fixed upstream by 095be159f3eb; 6.18 keeps it |
 | `dev-extent-defrag` | 1 | `40c2fffa8348` | Zygo | new, 0/1 (drafted 2026-10-18) | |
 | `prealloc-size` | 1 | `93d225865ab2` | Zygo | new, 0/1 (drafted 2026-10-18) | |
-| `raid56-fixes` | 11 | `8112c9e577e6` | Zygo | new; replaces `covers/raid56-rbio-fixes.txt` (2 of these patches) | **patch 11 is DO-NOT-UPSTREAM** (rbio cache size debug knob) |
+| `raid56-fixes` | 12 | `f7e693d10be2` | Zygo | new; replaces `covers/raid56-rbio-fixes.txt` (2 of these patches) | **patch 12 is DO-NOT-UPSTREAM** (rbio cache size debug knob) |
 | `raid56-scrub-replace` | 10 | `2a30d9423f4e` | Filipe Manana, Zygo | new | patch 1 is Filipe's, already in btrfs/for-next (`ec0795c045c3`) |
 | `stripe-alloc` | 112 | `52074eb4d12e` | Zygo | from misc-next `covers/`, **stale**: written for 65 patches, marked DO NOT SEND AS IS | RFC |
 | `low-space` | 2 | `57034df182f0` | Zygo | from misc-next `covers/`, current | reserve off by default |
@@ -145,7 +145,7 @@ conflicts by hand using the description below, and compare with
 
 ### What the fixup contains
 
-The fixup adds 455 lines and removes 18, in 10 files.
+The fixup adds 640 lines and removes 16, in 10 files.
 
 1. **stripe-alloc merged after raid56-fixes** conflicts in `raid56.c`,
    `raid56.h`, `block-group.c` and `sysfs.c`.
@@ -159,6 +159,10 @@ The fixup adds 455 lines and removes 18, in 10 files.
        run has not finished").
      - raid56-fixes' prune loop becomes stripe-alloc's
        `rbio_cache_prune(table, rbio, limit)`.
+   - `rmw_rbio()` in `raid56.c`: raid56-fixes' write-back helpers
+     (patch 11) and stripe-alloc's RMW audit helpers are both inserted
+     before `rmw_rbio()`; keep both, write-back's hunks inside
+     `rmw_rbio()` unchanged.
    - `sysfs.c`: the debug attribute list keeps stripe-alloc's
      `stripe_rmw_audit` next to raid56-fixes' `rbio_cache_size`.
    - The `block-group.c` conflict needs nothing beyond what `-X ours`
@@ -193,10 +197,10 @@ The fixup adds 455 lines and removes 18, in 10 files.
 
 ## Before sending
 
-- Drop `raid56-fixes` patch 11 and the whole `dm-stripfault` series.
+- Drop `raid56-fixes` patch 12 and the whole `dm-stripfault` series.
 - `stripe-alloc`'s cover letter needs a rewrite.
 - `balance-resume` patch 3 is offered for rejection, as its cover letter
   says.
 - Re-export with `scripts/export.sh` after any lane change.  The
-  directories reflect the lane at `misc-next/testing` `cd56f55e64cf`.
+  directories reflect the lane at `misc-next/testing` `bbc86ab46e84`.
   Commit the result with the misc-next SHAs that the script prints.
index ea82f733680dc56fa32999f24ca1d634899e8bed..21ad63e47540d7dad38a23e67c8d6a8230568ebe 100644 (file)
@@ -175,7 +175,7 @@ index cd3073cc60fe361602e0a154f1e7085dd76a4cc2..cfc0d9a2169a4448eb13ce64128db7ad
        ret = find_free_extent(root, ins, &ffe_ctl);
        if (!ret && !is_data) {
 diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
-index 0e1cd49ac44074a353adb6702e1b2bef5e514b21..3d5a6baae67e629a2d5a0e7846390f95bbefde0a 100644
+index b6fae5d9295ab887faab60bdf7b20f1bc96f5cd3..65a01492ab1ca8664e91ce1890575896e1bd0089 100644
 --- a/fs/btrfs/raid56.c
 +++ b/fs/btrfs/raid56.c
 @@ -991,13 +991,15 @@ static void cache_rbio(struct btrfs_raid_bio *rbio)
@@ -217,6 +217,265 @@ index 0e1cd49ac44074a353adb6702e1b2bef5e514b21..3d5a6baae67e629a2d5a0e7846390f95
  
        spin_unlock(&table->cache_lock);
  }
+@@ -4032,6 +4024,258 @@ static bool need_read_stripe_sectors(struct btrfs_raid_bio *rbio)
+       return false;
+ }
+ 
++/*
++ * Read the header of the tree block at data-stripe sector @i, if there is
++ * one, from whichever copy of the sector this rbio is holding.
++ *
++ * @from_bio picks the source: the caller's pages (what this write is putting
++ * there) or the stripe pages (what an RMW read found on the disk).  Returns
++ * false when the sector holds no tree block that belongs at this address --
++ * free space, or the remains of one from a previous life of the stripe.
++ */
++static bool rmw_peek_eb_generation(struct btrfs_raid_bio *rbio, u32 i,
++                                 bool from_bio, u64 *generation)
++{
++      struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
++      const int stripe_nr = i / rbio->stripe_nsectors;
++      const int sectornr = i % rbio->stripe_nsectors;
++      const u64 logical = rbio->bioc->full_stripe_logical +
++                          (u64)stripe_nr * BTRFS_STRIPE_LEN +
++                          (u64)sectornr * fs_info->sectorsize;
++      const phys_addr_t *paddrs = from_bio ? rbio->bio_paddrs
++                                           : rbio->stripe_paddrs;
++      struct btrfs_header *hdr;
++      phys_addr_t paddr;
++      u64 found_bytenr;
++      bool bad_fsid;
++      void *kaddr;
++
++      paddr = paddrs[i * rbio->sector_nsteps];
++      if (paddr == INVALID_PADDR)
++              return false;
++      if (!from_bio && !test_bit(i, rbio->stripe_uptodate_bitmap))
++              return false;
++
++      kaddr = kmap_local_paddr(paddr);
++      hdr = kaddr;
++      found_bytenr = btrfs_stack_header_bytenr(hdr);
++      *generation = btrfs_stack_header_generation(hdr);
++      bad_fsid = memcmp(hdr->fsid, fs_info->fs_devices->metadata_uuid,
++                        BTRFS_FSID_SIZE) != 0;
++      kunmap_local(kaddr);
++
++      return found_bytenr == logical && !bad_fsid;
++}
++
++/*
++ * Is @bytenr a tree block the committed extent tree still references?  Read
++ * the commit root without locking, as the caching thread does, so this is
++ * safe from the raid56 worker with no transaction to join.
++ *
++ * Returns 1 if it is (or the lookup failed), 0 if not, and -EAGAIN if the
++ * answer needs a tree block that is not in memory.  The caller holds the
++ * full stripe lock, so it must not read from disk: a read of a block in
++ * this stripe that needs reconstruction would wait for that lock.
++ */
++static int rmw_block_referenced(struct btrfs_fs_info *fs_info, u64 bytenr,
++                              u64 header_gen, u64 *refs, u64 *extent_gen)
++{
++      struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bytenr);
++      struct btrfs_path *path;
++      struct btrfs_key key;
++      int live = 1;
++      int ret;
++
++      *refs = 0;
++      *extent_gen = 0;
++      path = btrfs_alloc_path();
++      if (!path)
++              return 1;
++      path->search_commit_root = 1;
++      path->skip_locking = 1;
++      path->nowait = 1;
++      key.objectid = bytenr;
++      key.type = BTRFS_METADATA_ITEM_KEY;
++      key.offset = (u64)-1;
++      if (!down_read_trylock(&fs_info->commit_root_sem)) {
++              btrfs_free_path(path);
++              return -EAGAIN;
++      }
++      ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
++      if (ret > 0)
++              ret = btrfs_previous_extent_item(extent_root, path, bytenr);
++      if (ret == 0) {
++              btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
++              if (key.objectid == bytenr &&
++                  (key.type == BTRFS_METADATA_ITEM_KEY ||
++                   key.type == BTRFS_EXTENT_ITEM_KEY)) {
++                      struct btrfs_extent_item *ei;
++
++                      ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
++                                          struct btrfs_extent_item);
++                      *refs = btrfs_extent_refs(path->nodes[0], ei);
++                      *extent_gen = btrfs_extent_generation(path->nodes[0], ei);
++                      /*
++                       * An extent item at this bytenr from a LATER
++                       * transaction than the header on disk belongs to a
++                       * new block allocated here whose write has not landed
++                       * yet; the header is the dead previous occupant.
++                       * Only an item of the header's own generation makes
++                       * the block on disk live.
++                       */
++                      live = (*refs != 0 && *extent_gen == header_gen);
++              } else {
++                      live = 0;
++              }
++      } else if (ret > 0) {
++              live = 0;
++      } else if (ret == -EAGAIN) {
++              live = -EAGAIN;
++      }
++      up_read(&fs_info->commit_root_sem);
++      btrfs_free_path(path);
++      return live;
++}
++
++/*
++ * Decide whether an uncovered metadata read-modify-write is a write hole or
++ * only a cost, from the tree blocks it read: their parity is about to be
++ * rewritten while their own sectors stay put.
++ *
++ *  - Same generation as the blocks this write carries (meta_rmw_cur): the
++ *    block belongs to the running transaction, which a crash discards --
++ *    except a log tree block, which is durable once its fsync returns.
++ *    The audit does not tell log blocks apart, so it cannot see a write
++ *    hole over fsynced log blocks; keeping them out of shared stripes is
++ *    stripe_log_run's job.
++ *  - Older, and still referenced by the committed extent tree: a write hole
++ *    over committed metadata (meta_rmw_old).
++ *
++ * Comparing against this rbio's own blocks, not fs_info's committed
++ * generation, keeps a concurrent commit from skewing the verdict.
++ */
++static void audit_uncovered_rmw(struct btrfs_raid_bio *rbio)
++{
++      struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
++      const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits;
++      const u32 nsectors = rbio->nr_data * rbio->stripe_nsectors;
++      u64 write_gen = 0;
++
++      /*
++       * Tree blocks only: metadata and system groups.  A mixed group's
++       * sectors need not hold tree blocks.
++       */
++      if (rbio->bioc->map_type & BTRFS_BLOCK_GROUP_DATA)
++              return;
++      if (!(rbio->bioc->map_type & (BTRFS_BLOCK_GROUP_METADATA |
++                                    BTRFS_BLOCK_GROUP_SYSTEM)))
++              return;
++
++      /*
++       * Both loops read bio_paddrs to tell what this write covers, and the
++       * paths that reach here do not all populate it (a cached rbio that
++       * failed to pad never indexes).  Idempotent, so just do it.
++       */
++      index_rbio_pages(rbio);
++
++      /* The generation this write is putting into the stripe. */
++      for (u32 i = 0; i < nsectors; i += sectors_per_tree) {
++              u64 gen;
++
++              if (rmw_peek_eb_generation(rbio, i, true, &gen))
++                      write_gen = max(write_gen, gen);
++      }
++      if (!write_gen)
++              return;
++
++      for (u32 i = 0; i < nsectors; i += sectors_per_tree) {
++              u64 gen;
++
++              /* Covered by this write: it is what we are putting there. */
++              if (rbio->bio_paddrs[i * rbio->sector_nsteps] != INVALID_PADDR)
++                      continue;
++              if (!rmw_peek_eb_generation(rbio, i, false, &gen)) {
++                      atomic64_inc(&fs_info->stripe_park_stats.meta_rmw_free);
++                      continue;
++              }
++              if (gen >= write_gen) {
++                      atomic64_inc(&fs_info->stripe_park_stats.meta_rmw_cur);
++                      continue;
++              }
++              /*
++               * An older header is a write hole only if the committed extent
++               * tree still references the block: a reclaimed stripe keeps the
++               * headers of dead blocks, and tearing those harms nothing a
++               * reader can reach.  The commit root is what a crash would see.
++               */
++              {
++                      const int stripe_nr = i / rbio->stripe_nsectors;
++                      const int sectornr = i % rbio->stripe_nsectors;
++                      const u64 logical = rbio->bioc->full_stripe_logical +
++                              (u64)stripe_nr * BTRFS_STRIPE_LEN +
++                              (u64)sectornr * fs_info->sectorsize;
++                      u64 refs;
++                      u64 egen;
++                      int ret;
++
++                      ret = rmw_block_referenced(fs_info, logical, gen, &refs,
++                                                 &egen);
++                      if (ret == -EAGAIN) {
++                              atomic64_inc(&fs_info->stripe_park_stats.meta_rmw_unknown);
++                              continue;
++                      }
++                      if (!ret) {
++                              atomic64_inc(&fs_info->stripe_park_stats.meta_rmw_ghost);
++                              continue;
++                      }
++                      atomic64_inc(&fs_info->stripe_park_stats.meta_rmw_old);
++                      btrfs_warn_rl(fs_info,
++"read-modify-write of stripe %llu rewrites parity over committed tree block %llu at generation %llu (refs %llu, extent generation %llu) while writing generation %llu: write hole",
++                                    rbio->bioc->full_stripe_logical, logical,
++                                    gen, refs, egen, write_gen);
++              }
++      }
++}
++
++/*
++ * Count and audit a metadata or system read-modify-write
++ * (audit_uncovered_rmw()), and warn if it lands in a raid56 group that
++ * stripe_meta does not cover.  Silent when stripe_alloc is off.
++ */
++static void report_uncovered_rmw(struct btrfs_raid_bio *rbio)
++{
++      struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
++      struct btrfs_block_group *bg;
++      bool covered = false;
++
++      if (!btrfs_test_opt(fs_info, STRIPE_ALLOC))
++              return;
++      /* Data groups are btrfs_is_stripe_alloc_bg()'s. */
++      if ((rbio->bioc->map_type & BTRFS_BLOCK_GROUP_DATA) &&
++          !(rbio->bioc->map_type & BTRFS_BLOCK_GROUP_METADATA))
++              return;
++
++      atomic64_inc(&fs_info->stripe_park_stats.meta_rmw);
++      audit_uncovered_rmw(rbio);
++      /*
++       * Every raid56 metadata and system group is a stripe_meta group while
++       * stripe_alloc is on, and stripe_alloc refuses to enable on mixed
++       * groups (btrfs_stripe_alloc_check_support()), so this warning is a
++       * defensive check: it fires only if a tree block group that should be
++       * covered is not.
++       */
++      bg = btrfs_lookup_block_group(fs_info, rbio->bioc->full_stripe_logical);
++      if (bg) {
++              covered = btrfs_is_stripe_meta_bg(bg);
++              btrfs_put_block_group(bg);
++      }
++      if (covered)
++              return;
++      btrfs_warn_rl(fs_info,
++"read-modify-write of full stripe %llu: this block group is not covered by stripe_alloc, the raid56 write hole applies to it",
++                    rbio->bioc->full_stripe_logical);
++}
++
+ /*
+  * Return true if the rebuild of data sector @sector_nr of @stripe_nr can be
+  * trusted enough to write it back.  It can when the sector has a csum: the
 diff --git a/fs/btrfs/raid56.h b/fs/btrfs/raid56.h
 index b860e4e7bb03e5f4289230e07b0e6475cae16c92..4ce06b34b7b37eaaf59dc555805b76f6dc3e67e5 100644
 --- a/fs/btrfs/raid56.h
index d1960253252f25f6c18580705c4f54ea18cbae5d..428d66b66e6b56b5578f05046ce40cd972f7ea43 100644 (file)
@@ -1,7 +1,7 @@
-From 8112c9e577e6199e90529a850d86ad3c01e6e40e Mon Sep 17 00:00:00 2001
+From f7e693d10be20b7f83d5ba52569425bc68c36807 Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
-Date: Tue, 20 Oct 2026 22:14:03 -0400
-Subject: [PATCH 00/11] btrfs: raid56: rbio lifetime and cache fixes, error reporting
+Date: Tue, 20 Oct 2026 22:28:28 -0400
+Subject: [PATCH 00/12] btrfs: raid56: rbio lifetime and cache fixes, error reporting
 To: linux-btrfs@vger.kernel.org
 
 Fixes found running raid56 filesystems to ENOSPC under fsstress with a
@@ -82,11 +82,21 @@ write_io_errs or read_io_errs: a failing member looked healthy in
 `btrfs device stats`.  The device is found by the bio's bdev, so writes
 to a replace target are charged to the target.  Fixes: 53b381b3abeb.
 
-Patch 11 is a test fixture and NOT for submission: a CONFIG_BTRFS_DEBUG
+Patch 11 makes a sub-stripe write write back the sectors its RMW read
+repaired.  The read phase verifies data sectors against their csums and
+rebuilds bad ones (and, with patch 9, a stale P or Q), but only the new
+data and parity were written, so the repaired strip stayed stale on
+disk until read repair or scrub found it.  Csummed data sectors, P/Q
+with an error bit, and nodatasum sectors rebuilt from P+Q together with
+a verified csummed sector of the same row are written back; a missing
+device never is, and a failed write-back does not count against the
+write's tolerance.  Fixes: 7a3150723061.
+
+Patch 12 is a test fixture and NOT for submission: a CONFIG_BTRFS_DEBUG
 sysfs knob for the rbio cache size, used by the dm-stripfault tests to
 reach the RMW read path.  Drop it before sending.
 
-Zygo Blaxell (11):
+Zygo Blaxell (12):
   btrfs: log a message when dropping an extent due to IO error
   btrfs: raid56: fix use-after-free of rbio in bio end_io wakeups
   btrfs: propagate a split bio's error when the failing split completes
@@ -102,6 +112,7 @@ Zygo Blaxell (11):
   btrfs: raid56: rebuild from Q when a sector rebuilt from P fails its
     csum
   btrfs: raid56: count I/O errors in the device stats
+  btrfs: raid56: write back the sectors a sub-stripe write repaired
   DO-NOT-UPSTREAM: btrfs: raid56: debug knob for the rbio stripe cache
     size
 
@@ -110,10 +121,10 @@ Zygo Blaxell (11):
  fs/btrfs/extent-tree.c |  10 +
  fs/btrfs/fs.h          |   4 +
  fs/btrfs/inode.c       |  16 ++
- fs/btrfs/raid56.c      | 481 +++++++++++++++++++++++++++++++++++++----
+ fs/btrfs/raid56.c      | 633 ++++++++++++++++++++++++++++++++++++++---
  fs/btrfs/raid56.h      |  14 +-
- fs/btrfs/sysfs.c       |  28 +++
- 8 files changed, 525 insertions(+), 43 deletions(-)
+ fs/btrfs/sysfs.c       |  28 ++
+ 8 files changed, 677 insertions(+), 43 deletions(-)
 
 
 base-commit: 0c48d26d46bdd6422bb91ba7f1c519a2993116dc
index 0e9daf6f1b8e78fb20a057449ee0bf238b8701d9..70ebbcf4aad2fde7e3fc58a64485519d133a4a70 100644 (file)
@@ -1,7 +1,7 @@
 From 06db5c045ee4d05f55678163dc984793966b09af Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Fri, 5 Jul 2024 23:08:46 -0400
-Subject: [PATCH 01/11] btrfs: log a message when dropping an extent due to IO
+Subject: [PATCH 01/12] btrfs: log a message when dropping an extent due to IO
  error
 To: linux-btrfs@vger.kernel.org
 
index 650c34843ac992a29a92d1bafe96c9ce736714b2..f5cbcb9572779aaf54e5d70a92188d9012f6bf1f 100644 (file)
@@ -1,7 +1,7 @@
 From dec088fcc6e6b83401e437af76e1beac9f403bd1 Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Sat, 25 Jul 2026 16:06:34 -0400
-Subject: [PATCH 02/11] btrfs: raid56: fix use-after-free of rbio in bio end_io
+Subject: [PATCH 02/12] btrfs: raid56: fix use-after-free of rbio in bio end_io
  wakeups
 To: linux-btrfs@vger.kernel.org
 
index 4f1b1131637a4e066460c849edf917940761178f..f21f4de00cd882fdbb44b7817c927662cd3ad4b5 100644 (file)
@@ -1,7 +1,7 @@
 From c4ca7d88372eb775e03b9fb0ae3e080d171256c1 Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Sat, 1 Aug 2026 01:31:24 -0400
-Subject: [PATCH 03/11] btrfs: propagate a split bio's error when the failing
+Subject: [PATCH 03/12] btrfs: propagate a split bio's error when the failing
  split completes last
 To: linux-btrfs@vger.kernel.org
 
index aa8988d0834abee6929fe05d997d86cb19d8c955..f15d946bd6116e53e4c95c87d243a0b51d64a6fe 100644 (file)
@@ -1,7 +1,7 @@
 From c05f4a996dc3d9f233e27e191e3bc8d1f92492bb Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Mon, 3 Aug 2026 03:24:38 -0400
-Subject: [PATCH 04/11] btrfs: raid56: drop cached rbios when their stripes'
+Subject: [PATCH 04/12] btrfs: raid56: drop cached rbios when their stripes'
  extents are freed
 To: linux-btrfs@vger.kernel.org
 
index 7983ec31c41f4821753b4fb2641cad222b2ebef3..49a6166c09e34110de0918a2544d74326ce06180 100644 (file)
@@ -1,7 +1,7 @@
 From 0fe89be282ee3d71b69555adab49dd43729aad45 Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Wed, 5 Aug 2026 02:59:50 -0400
-Subject: [PATCH 05/11] btrfs: raid56: verify reconstructed tree blocks
+Subject: [PATCH 05/12] btrfs: raid56: verify reconstructed tree blocks
 To: linux-btrfs@vger.kernel.org
 
 Data sectors recovered from parity are checked against the csum tree
index 1889df02b3fed40ba2e5668e28662ab412560e01..a09a6be2df94d2f47eda2db1d0934f18cf7d6279 100644 (file)
@@ -1,7 +1,7 @@
 From b2c793a1845e5928abc500c6719aebf14e543c05 Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Fri, 4 Sep 2026 22:14:19 -0400
-Subject: [PATCH 06/11] btrfs: raid56: keep a reference on a stolen cached rbio
+Subject: [PATCH 06/12] btrfs: raid56: keep a reference on a stolen cached rbio
  until it is dropped
 To: linux-btrfs@vger.kernel.org
 
index 7bef7c9c6ea499fe683ee6022b1d25aa0e2fe164..4cdae225aad8a3b8a53729ea41cc423566590d78 100644 (file)
@@ -1,7 +1,7 @@
 From 3a80dae0c04b17fa72ef154d058a763334da4438 Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Sat, 12 Sep 2026 07:17:54 -0400
-Subject: [PATCH 07/11] btrfs: raid56: do not unhash a cached rbio that has
+Subject: [PATCH 07/12] btrfs: raid56: do not unhash a cached rbio that has
  rbios plugged on it
 To: linux-btrfs@vger.kernel.org
 
index 2fe4ca061192fa967606463da0bc784508b264b7..ac61edb88d167375e529868e4a72af2c0f905c78 100644 (file)
@@ -1,7 +1,7 @@
 From 773140d8d8973b91099623dedb025073a34b0974 Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Tue, 20 Oct 2026 01:19:22 -0400
-Subject: [PATCH 08/11] btrfs: raid56: do not let a failed write leave its
+Subject: [PATCH 08/12] btrfs: raid56: do not let a failed write leave its
  pages for the next write
 To: linux-btrfs@vger.kernel.org
 
index 61b4ff9bbdfd97c9b1e518e9b18f1d619bee4375..192091f7433f48f4ab93127019bcc7beaad18f14 100644 (file)
@@ -1,7 +1,7 @@
 From d507eefdb5832348bc728b963f3d7c8898ffb0a4 Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Tue, 20 Oct 2026 13:04:12 -0400
-Subject: [PATCH 09/11] btrfs: raid56: rebuild from Q when a sector rebuilt
+Subject: [PATCH 09/12] btrfs: raid56: rebuild from Q when a sector rebuilt
  from P fails its csum
 To: linux-btrfs@vger.kernel.org
 
index 7d71a453584145391d4a22595761f5467e049a8d..8131a08a0f500af955f9a420524d57d956adf1f7 100644 (file)
@@ -1,7 +1,7 @@
 From 6dfdbfdffe7d7f18f1112740a6e3a9baa06dbb93 Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Tue, 20 Oct 2026 13:43:10 -0400
-Subject: [PATCH 10/11] btrfs: raid56: count I/O errors in the device stats
+Subject: [PATCH 10/12] btrfs: raid56: count I/O errors in the device stats
 To: linux-btrfs@vger.kernel.org
 
 raid56.c submits the reads and writes of a raid56 stripe itself, with its
diff --git a/topics/raid56-fixes/0011-DO-NOT-UPSTREAM-btrfs-raid56-debug-knob-for-the-rbio.patch b/topics/raid56-fixes/0011-DO-NOT-UPSTREAM-btrfs-raid56-debug-knob-for-the-rbio.patch
deleted file mode 100644 (file)
index 4681b20..0000000
+++ /dev/null
@@ -1,194 +0,0 @@
-From 8112c9e577e6199e90529a850d86ad3c01e6e40e Mon Sep 17 00:00:00 2001
-From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
-Date: Thu, 1 Oct 2026 03:54:55 -0400
-Subject: [PATCH 11/11] DO-NOT-UPSTREAM: btrfs: raid56: debug knob for the rbio
- stripe cache size
-To: linux-btrfs@vger.kernel.org
-
-Test fixture, not for submission.  A read-modify-write whose stripe is
-still in the rbio stripe cache takes the other strips from memory instead
-of reading them, so a test that injects faults into reads (dm-stripfault)
-cannot reach the RMW read path for recently written stripes.
-
-With CONFIG_BTRFS_DEBUG, /sys/fs/btrfs/<uuid>/debug/rbio_cache_size
-replaces the RBIO_CACHE_SIZE limit (default unchanged, 1024); 0 disables
-the cache.  Lowering it empties the cache, and the prune in cache_rbio()
-now loops so a lowered limit holds.
-
-Assisted-by: Claude:claude-opus-5-5
----
- fs/btrfs/fs.h     |  4 ++++
- fs/btrfs/raid56.c | 41 ++++++++++++++++++++++++++++++++++++++---
- fs/btrfs/raid56.h |  3 +++
- fs/btrfs/sysfs.c  | 28 ++++++++++++++++++++++++++++
- 4 files changed, 73 insertions(+), 3 deletions(-)
-
-diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
-index 2dd167c32d612..326a54601289c 100644
---- a/fs/btrfs/fs.h
-+++ b/fs/btrfs/fs.h
-@@ -635,6 +635,10 @@ struct btrfs_fs_info {
-        * trying to mod the same stripe at the same time.
-        */
-       struct btrfs_stripe_hash_table *stripe_hash_table;
-+#ifdef CONFIG_BTRFS_DEBUG
-+      /* Size limit of the rbio stripe cache (debug/rbio_cache_size). */
-+      unsigned int rbio_cache_size;
-+#endif
- 
-       /*
-        * This protects the ordered operations list only while we are
-diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
-index 232cc91584ea2..f2df584b2e415 100644
---- a/fs/btrfs/raid56.c
-+++ b/fs/btrfs/raid56.c
-@@ -198,6 +198,9 @@ int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info)
-       struct btrfs_stripe_hash *h;
-       unsigned int num_entries = 1U << BTRFS_STRIPE_HASH_TABLE_BITS;
- 
-+#ifdef CONFIG_BTRFS_DEBUG
-+      WRITE_ONCE(info->rbio_cache_size, RBIO_CACHE_SIZE);
-+#endif
-       if (info->stripe_hash_table)
-               return 0;
- 
-@@ -601,6 +604,23 @@ static void btrfs_clear_rbio_cache(struct btrfs_fs_info *info)
-       spin_unlock(&table->cache_lock);
- }
- 
-+#ifdef CONFIG_BTRFS_DEBUG
-+/*
-+ * Test knob (debug/rbio_cache_size): with a small or zero limit, a
-+ * read-modify-write cannot take its stripe from the cache and must read it,
-+ * which is what a fault-injecting test needs to see.  Lowering it empties the
-+ * cache, so the new limit holds from the next rbio on.
-+ */
-+void btrfs_set_rbio_cache_size(struct btrfs_fs_info *info, unsigned int size)
-+{
-+      unsigned int old = READ_ONCE(info->rbio_cache_size);
-+
-+      WRITE_ONCE(info->rbio_cache_size, size);
-+      if (size < old && info->stripe_hash_table)
-+              btrfs_clear_rbio_cache(info);
-+}
-+#endif
-+
- /*
-  * remove all cached entries and free the hash table
-  * used by unmount
-@@ -625,12 +645,25 @@ void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info)
-  * If the size of the rbio cache is too big, we
-  * prune an item.
-  */
-+/* How many rbios the stripe cache keeps (0: none). */
-+static unsigned int rbio_cache_limit(const struct btrfs_fs_info *fs_info)
-+{
-+#ifdef CONFIG_BTRFS_DEBUG
-+      return READ_ONCE(fs_info->rbio_cache_size);
-+#else
-+      return RBIO_CACHE_SIZE;
-+#endif
-+}
-+
- static void cache_rbio(struct btrfs_raid_bio *rbio)
- {
-       struct btrfs_stripe_hash_table *table;
-+      const unsigned int limit = rbio_cache_limit(rbio->bioc->fs_info);
- 
-       if (!test_bit(RBIO_CACHE_READY_BIT, &rbio->flags))
-               return;
-+      if (!limit)
-+              return;
- 
-       table = rbio->bioc->fs_info->stripe_hash_table;
- 
-@@ -650,15 +683,17 @@ static void cache_rbio(struct btrfs_raid_bio *rbio)
- 
-       spin_unlock(&rbio->bio_list_lock);
- 
--      if (table->cache_size > RBIO_CACHE_SIZE) {
-+      /* A loop, not one eviction: the limit can have been lowered. */
-+      while (table->cache_size > limit) {
-               struct btrfs_raid_bio *found;
- 
-               found = list_last_entry(&table->stripe_cache,
-                                       struct btrfs_raid_bio,
-                                       stripe_cache);
- 
--              if (found != rbio)
--                      __remove_rbio_from_cache(found);
-+              if (found == rbio)
-+                      break;
-+              __remove_rbio_from_cache(found);
-       }
- 
-       spin_unlock(&table->cache_lock);
-diff --git a/fs/btrfs/raid56.h b/fs/btrfs/raid56.h
-index c7c4b0db61f4c..8d28e2a77c62b 100644
---- a/fs/btrfs/raid56.h
-+++ b/fs/btrfs/raid56.h
-@@ -295,6 +295,9 @@ void raid56_parity_cache_data_folios(struct btrfs_raid_bio *rbio,
- 
- int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info);
- void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info);
-+#ifdef CONFIG_BTRFS_DEBUG
-+void btrfs_set_rbio_cache_size(struct btrfs_fs_info *info, unsigned int size);
-+#endif
- void btrfs_raid56_uncache_range(struct btrfs_fs_info *info, u64 start, u64 len);
- 
- #endif
-diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
-index 3ac1fa608d3b5..78688c36f8b02 100644
---- a/fs/btrfs/sysfs.c
-+++ b/fs/btrfs/sysfs.c
-@@ -25,6 +25,7 @@
- #include "misc.h"
- #include "fs.h"
- #include "accessors.h"
-+#include "raid56.h"
- 
- /*
-  * Structure name                       Path
-@@ -520,12 +521,39 @@ static const struct attribute *discard_attrs[] = {
- 
- #ifdef CONFIG_BTRFS_DEBUG
- 
-+/* The raid56 stripe cache limit (raid56.c), 0 to disable the cache. */
-+static ssize_t btrfs_rbio_cache_size_show(struct kobject *kobj,
-+                                        struct kobj_attribute *a, char *buf)
-+{
-+      struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
-+
-+      return sysfs_emit(buf, "%u\n", READ_ONCE(fs_info->rbio_cache_size));
-+}
-+
-+static ssize_t btrfs_rbio_cache_size_store(struct kobject *kobj,
-+                                         struct kobj_attribute *a,
-+                                         const char *buf, size_t len)
-+{
-+      struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
-+      unsigned int val;
-+      int ret;
-+
-+      ret = kstrtouint(buf, 10, &val);
-+      if (ret)
-+              return ret;
-+      btrfs_set_rbio_cache_size(fs_info, val);
-+      return len;
-+}
-+BTRFS_ATTR_RW(debug_mount, rbio_cache_size, btrfs_rbio_cache_size_show,
-+            btrfs_rbio_cache_size_store);
-+
- /*
-  * Per-filesystem runtime debugging exported via sysfs.
-  *
-  * Path: /sys/fs/btrfs/UUID/debug/
-  */
- static const struct attribute *btrfs_debug_mount_attrs[] = {
-+      BTRFS_ATTR_PTR(debug_mount, rbio_cache_size),
-       NULL,
- };
- 
--- 
-2.47.3
-
diff --git a/topics/raid56-fixes/0011-btrfs-raid56-write-back-the-sectors-a-sub-stripe-wri.patch b/topics/raid56-fixes/0011-btrfs-raid56-write-back-the-sectors-a-sub-stripe-wri.patch
new file mode 100644 (file)
index 0000000..ce8b499
--- /dev/null
@@ -0,0 +1,264 @@
+From 95d142c556b58d28b69c8c9aa579470eddce6d19 Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Tue, 20 Oct 2026 15:52:51 -0400
+Subject: [PATCH 11/12] btrfs: raid56: write back the sectors a sub-stripe
+ write repaired
+To: linux-btrfs@vger.kernel.org
+
+A sub-stripe write reads the rest of its full stripe and verifies the data
+sectors that have csums.  It rebuilds the sectors that failed their csum or
+their read, and the sectors of a missing device, and computes the new P and
+Q from the result.  On RAID6 it also finds a stale P, or a stale data
+sector without a csum, by rebuilding again with one more stripe treated as
+failed.  The write phase then writes only the new data, and P and Q of the
+vertical stripes the new data covers.
+
+The repaired sectors stay stale on disk until a read repair or a scrub
+finds them, even though the new parity was computed from their rebuilt
+contents.  Until then the full stripe has one healthy member fewer than it
+seems, and one more lost device loses data that was recoverable.
+
+Write the repaired sectors back in the same write phase:
+
+- a data sector whose rebuild is verified.  It has a csum, or it has none
+  but was rebuilt from P and Q together with a data sector of the same
+  vertical stripe that has one.  The RAID6 retry finds a stale data sector
+  without a csum that way.  A lone rebuild of a sector without a csum comes
+  from parity that nothing checked, and the copy on disk may be the right
+  one, so it is left alone;
+- P or Q that failed its read or was found stale, in the vertical stripes
+  the write does not cover (the others get new P and Q anyway);
+- nothing on a missing device.  The replace target gets the same sectors as
+  its source, as with the other writes.
+
+The write-backs go out with the rest of the write.  A write-back that fails
+leaves its sector as stale as it was before the write, so it does not count
+against the write's tolerance, and the write completes as it did before.
+
+The rbio repairs before it writes anything, so a write-back never retries a
+write that the same rbio just failed.  A write that failed on a member
+within the tolerance leaves its pages in the stripe cache, and the next
+sub-stripe write into that full stripe takes them without reading, so it
+has nothing to repair either.  A write-back can still go to a sector whose
+write by an earlier rbio failed, when that rbio's pages were not cached (a
+full stripe write) or were evicted: then it fails again, or heals the
+sector.  A btrfs_debug line counts the write-backs and the failed ones.
+
+rmw-writeback-test.sh (raid56-wh-harness) puts strips of one full stripe
+back to stale contents, writes into the rest of the stripe, once with
+fsync and once with a plain write(), and reads the members back offline
+before anything else touches them.  The stale strips are, on RAID5, a data
+strip with a csum, alone or also failing its reads; on RAID6, a data strip
+with P, with Q, with a second data strip with a csum, or with a strip of a
+nodatasum file, with the write covering all rows or only some of them.
+Without this patch every repaired strip, and P outside the written rows,
+is still stale after the write in all 18 runs; with it none is, and P and
+Q match the data in every row.  With the stale member failing writes, the
+write-back fails, the write still completes and its data reads back,
+also on RAID5 when the write to P fails as well (counting the failed
+write-back against the tolerance fails that write with -EIO).  A
+nodatasum strip rebuilt from P alone after a read error is not written
+back.
+
+Fixes: 7a3150723061 ("btrfs: raid56: do data csum verification during RMW cycle")
+Assisted-by: Claude:claude-opus-5-5
+---
+ fs/btrfs/raid56.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++
+ 1 file changed, 152 insertions(+)
+
+diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
+index 232cc91584ea2..5732c2d8ea359 100644
+--- a/fs/btrfs/raid56.c
++++ b/fs/btrfs/raid56.c
+@@ -2813,10 +2813,153 @@ static bool need_read_stripe_sectors(struct btrfs_raid_bio *rbio)
+       return false;
+ }
+ 
++/*
++ * Return true if the rebuild of data sector @sector_nr of @stripe_nr can be
++ * trusted enough to write it back.  It can when the sector has a csum: the
++ * rebuild was verified.  Without a csum, it can only when another data sector
++ * of the same vertical stripe also failed and has a csum: the two were
++ * rebuilt together from P and Q (on RAID6 that is also how a stale sector
++ * without a csum is found, see recover_vertical_retry()), and the csum that
++ * verified one verifies the other.  A lone rebuild without a csum came from
++ * parity that nothing checked, and the copy on the disk may still be right.
++ */
++static bool rmw_repair_verified(struct btrfs_raid_bio *rbio, int stripe_nr,
++                              int sector_nr)
++{
++      if (!rbio->csum_bitmap)
++              return false;
++
++      for (int i = 0; i < rbio->nr_data; i++) {
++              const int index = rbio_sector_index(rbio, i, sector_nr);
++
++              if (!test_bit(index, rbio->csum_bitmap))
++                      continue;
++              if (i == stripe_nr || test_bit(index, rbio->error_bitmap))
++                      return true;
++      }
++      return false;
++}
++
++/*
++ * After the read phase of a sub-stripe write, return the sectors it repaired
++ * that the write phase should also write back, or NULL if there are none.
++ *
++ * recover_sectors() leaves the error bit set on every sector it rebuilt: a
++ * data sector that failed its csum or its read, every sector of a missing
++ * device, and the extra stripe that recover_vertical_retry() found stale.
++ * The rebuilt sectors are in the stripe pages and the new P and Q are
++ * generated from them, so without a write-back the disk copies stay stale
++ * until read repair or scrub finds them.
++ *
++ * Nothing is written to a missing device.  A data sector is written back only
++ * if its rebuild is verified (rmw_repair_verified()).  P and Q are generated
++ * from the data either way, so a failed or stale one is always written back.
++ *
++ * This rbio has not written anything yet, so none of these is a retry of a
++ * write that just failed here.
++ */
++static unsigned long *rmw_repaired_sectors(struct btrfs_raid_bio *rbio)
++{
++      unsigned long *repaired = NULL;
++      int total_sector_nr;
++
++      for_each_set_bit(total_sector_nr, rbio->error_bitmap, rbio->nr_sectors) {
++              const int stripe = total_sector_nr / rbio->stripe_nsectors;
++              const int sectornr = total_sector_nr % rbio->stripe_nsectors;
++
++              if (!rbio->bioc->stripes[stripe].dev->bdev)
++                      continue;
++              if (stripe < rbio->nr_data &&
++                  !rmw_repair_verified(rbio, stripe, sectornr))
++                      continue;
++
++              if (!repaired) {
++                      /* Without memory the sectors just stay stale on disk. */
++                      repaired = bitmap_zalloc(rbio->nr_sectors, GFP_NOFS);
++                      if (!repaired)
++                              return NULL;
++              }
++              set_bit(total_sector_nr, repaired);
++      }
++      return repaired;
++}
++
++/*
++ * Add write bios for the repaired sectors that the write phase does not write
++ * anyway: data sectors no bio of this rbio covers, and P/Q outside the
++ * vertical stripes being written.  The others are dropped from @repaired.
++ * The replace target gets the same sectors as its source.
++ */
++static int rmw_assemble_repair_bios(struct btrfs_raid_bio *rbio,
++                                  struct bio_list *bio_list,
++                                  unsigned long *repaired)
++{
++      int total_sector_nr;
++      int ret;
++
++      for_each_set_bit(total_sector_nr, repaired, rbio->nr_sectors) {
++              const int stripe = total_sector_nr / rbio->stripe_nsectors;
++              const int sectornr = total_sector_nr % rbio->stripe_nsectors;
++              phys_addr_t *paddrs;
++              bool written;
++
++              if (stripe < rbio->nr_data)
++                      written = sector_paddrs_in_rbio(rbio, stripe, sectornr, 1);
++              else
++                      written = test_bit(sectornr, &rbio->dbitmap);
++              if (written) {
++                      clear_bit(total_sector_nr, repaired);
++                      continue;
++              }
++
++              paddrs = rbio_stripe_paddrs(rbio, stripe, sectornr);
++              ret = rbio_add_io_paddrs(rbio, bio_list, paddrs, stripe,
++                                       sectornr, REQ_OP_WRITE);
++              if (ret)
++                      goto error;
++
++              if (likely(!rbio->bioc->replace_nr_stripes) ||
++                  stripe != rbio->bioc->replace_stripe_src)
++                      continue;
++              ret = rbio_add_io_paddrs(rbio, bio_list, paddrs,
++                                       rbio->real_stripes, sectornr,
++                                       REQ_OP_WRITE);
++              if (ret)
++                      goto error;
++      }
++      return 0;
++error:
++      bio_list_put(bio_list);
++      return -EIO;
++}
++
++/*
++ * A write-back that fails leaves the sector as stale as it was before this
++ * write, so it does not count against the tolerance of the write itself.
++ */
++static void rmw_finish_repair_bios(struct btrfs_raid_bio *rbio,
++                                 unsigned long *repaired)
++{
++      int written = 0;
++      int failed = 0;
++      int total_sector_nr;
++
++      for_each_set_bit(total_sector_nr, repaired, rbio->nr_sectors) {
++              written++;
++              if (test_and_clear_bit(total_sector_nr, rbio->error_bitmap))
++                      failed++;
++      }
++      if (written)
++              btrfs_debug_rl(rbio->bioc->fs_info,
++      "sub-stripe write for full stripe %llu wrote back %d repaired sectors, %d failed",
++                             rbio->bioc->full_stripe_logical, written, failed);
++}
++
+ static void rmw_rbio(struct btrfs_raid_bio *rbio)
+ {
+       struct bio_list bio_list;
+       int sectornr;
++      unsigned long *repaired = NULL;
+       int ret = 0;
+ 
+       /*
+@@ -2845,6 +2988,7 @@ static void rmw_rbio(struct btrfs_raid_bio *rbio)
+               ret = rmw_read_wait_recover(rbio);
+               if (ret < 0)
+                       goto out;
++              repaired = rmw_repaired_sectors(rbio);
+       }
+ 
+       /*
+@@ -2878,11 +3022,18 @@ static void rmw_rbio(struct btrfs_raid_bio *rbio)
+       ret = rmw_assemble_write_bios(rbio, &bio_list);
+       if (ret < 0)
+               goto out;
++      if (repaired) {
++              ret = rmw_assemble_repair_bios(rbio, &bio_list, repaired);
++              if (ret < 0)
++                      goto out;
++      }
+ 
+       /* We should have at least one bio assembled. */
+       ASSERT(bio_list_size(&bio_list));
+       submit_write_bios(rbio, &bio_list);
+       rbio_wait_io(rbio);
++      if (repaired)
++              rmw_finish_repair_bios(rbio, repaired);
+ 
+       /* We may have more errors than our tolerance during the read. */
+       for (sectornr = 0; sectornr < rbio->stripe_nsectors; sectornr++) {
+@@ -2895,6 +3046,7 @@ static void rmw_rbio(struct btrfs_raid_bio *rbio)
+               }
+       }
+ out:
++      bitmap_free(repaired);
+       rbio_orig_end_io(rbio, errno_to_blk_status(ret));
+ }
+ 
+-- 
+2.47.3
+
diff --git a/topics/raid56-fixes/0012-DO-NOT-UPSTREAM-btrfs-raid56-debug-knob-for-the-rbio.patch b/topics/raid56-fixes/0012-DO-NOT-UPSTREAM-btrfs-raid56-debug-knob-for-the-rbio.patch
new file mode 100644 (file)
index 0000000..200b232
--- /dev/null
@@ -0,0 +1,194 @@
+From f7e693d10be20b7f83d5ba52569425bc68c36807 Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Thu, 1 Oct 2026 03:54:55 -0400
+Subject: [PATCH 12/12] DO-NOT-UPSTREAM: btrfs: raid56: debug knob for the rbio
+ stripe cache size
+To: linux-btrfs@vger.kernel.org
+
+Test fixture, not for submission.  A read-modify-write whose stripe is
+still in the rbio stripe cache takes the other strips from memory instead
+of reading them, so a test that injects faults into reads (dm-stripfault)
+cannot reach the RMW read path for recently written stripes.
+
+With CONFIG_BTRFS_DEBUG, /sys/fs/btrfs/<uuid>/debug/rbio_cache_size
+replaces the RBIO_CACHE_SIZE limit (default unchanged, 1024); 0 disables
+the cache.  Lowering it empties the cache, and the prune in cache_rbio()
+now loops so a lowered limit holds.
+
+Assisted-by: Claude:claude-opus-5-5
+---
+ fs/btrfs/fs.h     |  4 ++++
+ fs/btrfs/raid56.c | 41 ++++++++++++++++++++++++++++++++++++++---
+ fs/btrfs/raid56.h |  3 +++
+ fs/btrfs/sysfs.c  | 28 ++++++++++++++++++++++++++++
+ 4 files changed, 73 insertions(+), 3 deletions(-)
+
+diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
+index 2dd167c32d612..326a54601289c 100644
+--- a/fs/btrfs/fs.h
++++ b/fs/btrfs/fs.h
+@@ -635,6 +635,10 @@ struct btrfs_fs_info {
+        * trying to mod the same stripe at the same time.
+        */
+       struct btrfs_stripe_hash_table *stripe_hash_table;
++#ifdef CONFIG_BTRFS_DEBUG
++      /* Size limit of the rbio stripe cache (debug/rbio_cache_size). */
++      unsigned int rbio_cache_size;
++#endif
+ 
+       /*
+        * This protects the ordered operations list only while we are
+diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
+index 5732c2d8ea359..c44e447b7c4a5 100644
+--- a/fs/btrfs/raid56.c
++++ b/fs/btrfs/raid56.c
+@@ -198,6 +198,9 @@ int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info)
+       struct btrfs_stripe_hash *h;
+       unsigned int num_entries = 1U << BTRFS_STRIPE_HASH_TABLE_BITS;
+ 
++#ifdef CONFIG_BTRFS_DEBUG
++      WRITE_ONCE(info->rbio_cache_size, RBIO_CACHE_SIZE);
++#endif
+       if (info->stripe_hash_table)
+               return 0;
+ 
+@@ -601,6 +604,23 @@ static void btrfs_clear_rbio_cache(struct btrfs_fs_info *info)
+       spin_unlock(&table->cache_lock);
+ }
+ 
++#ifdef CONFIG_BTRFS_DEBUG
++/*
++ * Test knob (debug/rbio_cache_size): with a small or zero limit, a
++ * read-modify-write cannot take its stripe from the cache and must read it,
++ * which is what a fault-injecting test needs to see.  Lowering it empties the
++ * cache, so the new limit holds from the next rbio on.
++ */
++void btrfs_set_rbio_cache_size(struct btrfs_fs_info *info, unsigned int size)
++{
++      unsigned int old = READ_ONCE(info->rbio_cache_size);
++
++      WRITE_ONCE(info->rbio_cache_size, size);
++      if (size < old && info->stripe_hash_table)
++              btrfs_clear_rbio_cache(info);
++}
++#endif
++
+ /*
+  * remove all cached entries and free the hash table
+  * used by unmount
+@@ -625,12 +645,25 @@ void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info)
+  * If the size of the rbio cache is too big, we
+  * prune an item.
+  */
++/* How many rbios the stripe cache keeps (0: none). */
++static unsigned int rbio_cache_limit(const struct btrfs_fs_info *fs_info)
++{
++#ifdef CONFIG_BTRFS_DEBUG
++      return READ_ONCE(fs_info->rbio_cache_size);
++#else
++      return RBIO_CACHE_SIZE;
++#endif
++}
++
+ static void cache_rbio(struct btrfs_raid_bio *rbio)
+ {
+       struct btrfs_stripe_hash_table *table;
++      const unsigned int limit = rbio_cache_limit(rbio->bioc->fs_info);
+ 
+       if (!test_bit(RBIO_CACHE_READY_BIT, &rbio->flags))
+               return;
++      if (!limit)
++              return;
+ 
+       table = rbio->bioc->fs_info->stripe_hash_table;
+ 
+@@ -650,15 +683,17 @@ static void cache_rbio(struct btrfs_raid_bio *rbio)
+ 
+       spin_unlock(&rbio->bio_list_lock);
+ 
+-      if (table->cache_size > RBIO_CACHE_SIZE) {
++      /* A loop, not one eviction: the limit can have been lowered. */
++      while (table->cache_size > limit) {
+               struct btrfs_raid_bio *found;
+ 
+               found = list_last_entry(&table->stripe_cache,
+                                       struct btrfs_raid_bio,
+                                       stripe_cache);
+ 
+-              if (found != rbio)
+-                      __remove_rbio_from_cache(found);
++              if (found == rbio)
++                      break;
++              __remove_rbio_from_cache(found);
+       }
+ 
+       spin_unlock(&table->cache_lock);
+diff --git a/fs/btrfs/raid56.h b/fs/btrfs/raid56.h
+index c7c4b0db61f4c..8d28e2a77c62b 100644
+--- a/fs/btrfs/raid56.h
++++ b/fs/btrfs/raid56.h
+@@ -295,6 +295,9 @@ void raid56_parity_cache_data_folios(struct btrfs_raid_bio *rbio,
+ 
+ int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info);
+ void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info);
++#ifdef CONFIG_BTRFS_DEBUG
++void btrfs_set_rbio_cache_size(struct btrfs_fs_info *info, unsigned int size);
++#endif
+ void btrfs_raid56_uncache_range(struct btrfs_fs_info *info, u64 start, u64 len);
+ 
+ #endif
+diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
+index 3ac1fa608d3b5..78688c36f8b02 100644
+--- a/fs/btrfs/sysfs.c
++++ b/fs/btrfs/sysfs.c
+@@ -25,6 +25,7 @@
+ #include "misc.h"
+ #include "fs.h"
+ #include "accessors.h"
++#include "raid56.h"
+ 
+ /*
+  * Structure name                       Path
+@@ -520,12 +521,39 @@ static const struct attribute *discard_attrs[] = {
+ 
+ #ifdef CONFIG_BTRFS_DEBUG
+ 
++/* The raid56 stripe cache limit (raid56.c), 0 to disable the cache. */
++static ssize_t btrfs_rbio_cache_size_show(struct kobject *kobj,
++                                        struct kobj_attribute *a, char *buf)
++{
++      struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
++
++      return sysfs_emit(buf, "%u\n", READ_ONCE(fs_info->rbio_cache_size));
++}
++
++static ssize_t btrfs_rbio_cache_size_store(struct kobject *kobj,
++                                         struct kobj_attribute *a,
++                                         const char *buf, size_t len)
++{
++      struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
++      unsigned int val;
++      int ret;
++
++      ret = kstrtouint(buf, 10, &val);
++      if (ret)
++              return ret;
++      btrfs_set_rbio_cache_size(fs_info, val);
++      return len;
++}
++BTRFS_ATTR_RW(debug_mount, rbio_cache_size, btrfs_rbio_cache_size_show,
++            btrfs_rbio_cache_size_store);
++
+ /*
+  * Per-filesystem runtime debugging exported via sysfs.
+  *
+  * Path: /sys/fs/btrfs/UUID/debug/
+  */
+ static const struct attribute *btrfs_debug_mount_attrs[] = {
++      BTRFS_ATTR_PTR(debug_mount, rbio_cache_size),
+       NULL,
+ };
+ 
+-- 
+2.47.3
+