]> git.hungrycats.org Git - linux/commitdiff
raid56-scrub-replace: read-only scrub fix, scrub error device names, parity-scrub...
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Wed, 21 Oct 2026 11:10:20 +0000 (07:10 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Wed, 21 Oct 2026 11:10:20 +0000 (07:10 -0400)
Promoted on both lanes 2026-10-21.  The read-only fix goes before the
raid6 rebuild, so the former patches 9-10 are now 10-11.  misc-next
raid56-scrub-replace 9970d7e44abf, testing 318dec80d29b, zb64
9c343098c7ea.  Cover describes patches 9-13.

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

18 files changed:
README.md
fixups/post-merge.patch
topics/raid56-scrub-replace/0000-cover-letter.patch
topics/raid56-scrub-replace/0001-btrfs-scrub-fix-lost-error-return-value-when-scrubbi.patch
topics/raid56-scrub-replace/0002-btrfs-scrub-check-raid56-parity-of-the-rows-without-.patch
topics/raid56-scrub-replace/0003-btrfs-scrub-copy-raid56-parity-of-unrepairable-rows-.patch
topics/raid56-scrub-replace/0004-btrfs-scrub-copy-unrepaired-raid56-data-sectors-to-t.patch
topics/raid56-scrub-replace/0005-btrfs-scrub-copy-every-sector-of-raid56-data-stripes.patch
topics/raid56-scrub-replace/0006-btrfs-scrub-only-avoid-the-replace-source-device-for.patch
topics/raid56-scrub-replace/0007-btrfs-scrub-read-raid56-sectors-without-an-extent-fr.patch
topics/raid56-scrub-replace/0008-btrfs-don-t-abort-raid56-data-scrub-on-first-uncorre.patch
topics/raid56-scrub-replace/0009-btrfs-scrub-do-not-write-raid56-parity-in-a-read-onl.patch [new file with mode: 0644]
topics/raid56-scrub-replace/0009-btrfs-scrub-rebuild-a-raid6-data-stripe-that-both-P-.patch [deleted file]
topics/raid56-scrub-replace/0010-btrfs-scrub-count-raid56-parity-mismatches-as-corrup.patch [deleted file]
topics/raid56-scrub-replace/0010-btrfs-scrub-rebuild-a-raid6-data-stripe-that-both-P-.patch [new file with mode: 0644]
topics/raid56-scrub-replace/0011-btrfs-scrub-count-raid56-parity-mismatches-as-corrup.patch [new file with mode: 0644]
topics/raid56-scrub-replace/0012-btrfs-scrub-name-the-device-a-raid56-data-sector-was.patch [new file with mode: 0644]
topics/raid56-scrub-replace/0013-btrfs-scrub-count-the-raid56-data-repairs-made-by-a-.patch [new file with mode: 0644]

index 8309fce7b9e5b26ee3d437b31f9c5040755ece56..e7167f4845b5cc07786ef4c4e982e784461eebac 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` `12c700ee1440` (octopus of all 18 topics) |
-| lane head | `zygo-misc-next-zb64` `aa5e2b4c73c0`; its own commits on top of `testing` (kernel configs, `covers/`) are not exported |
+| integration branch | `misc-next/testing` `318dec80d29b` (octopus of all 18 topics) |
+| lane head | `zygo-misc-next-zb64` `9c343098c7ea`; 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
@@ -68,7 +68,7 @@ the integration recipe below uses.
 | `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` | 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`) |
+| `raid56-scrub-replace` | 13 | `9970d7e44abf` | Filipe Manana, Zygo | new | patch 1 is Filipe's, already in btrfs/for-next (`ec0795c045c3`) |
 | `stripe-alloc` | 114 | `bcadbfc8ac02` | 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 |
 | `dedupe-mtime` | 1 | `288d851bbbc2` | Ivan Kozik | new, 0/1 (drafted 2026-10-18) | Ivan's patch |
@@ -203,5 +203,5 @@ The fixup adds 640 lines and removes 16, in 10 files.
 - `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` `12c700ee1440`.
+  directories reflect the lane at `misc-next/testing` `318dec80d29b`.
   Commit the result with the misc-next SHAs that the script prints.
index f35a1fde4b5c65073cb1069e053fc55d232d70cd..222527fe6644bb3c42a8b0e29f331814b59d38e1 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 b6fae5d9295ab887faab60bdf7b20f1bc96f5cd3..65a01492ab1ca8664e91ce1890575896e1bd0089 100644
+index bcf8dbc7fdf8dc3455b08855149f351046850b1c..c10f75587c2310196a13faf797c868d294cc276d 100644
 --- a/fs/btrfs/raid56.c
 +++ b/fs/btrfs/raid56.c
 @@ -991,13 +991,15 @@ static void cache_rbio(struct btrfs_raid_bio *rbio)
@@ -477,10 +477,10 @@ index b6fae5d9295ab887faab60bdf7b20f1bc96f5cd3..65a01492ab1ca8664e91ce1890575896
   * 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
+index d71c5c5f4b2ed8f930cb1c2991f98fbafba35261..cfcd9bce4652f861774763844076ef755cc70475 100644
 --- a/fs/btrfs/raid56.h
 +++ b/fs/btrfs/raid56.h
-@@ -331,5 +331,11 @@ void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info);
+@@ -334,5 +334,11 @@ void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info);
  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);
index 27f35a743926d58050bd697b0d692ca6a092e2f2..247baa885a06d3553e087e3c82e5408e566e2b53 100644 (file)
@@ -1,7 +1,7 @@
-From 2a30d9423f4e3ba05ab8cf4058ccfc94c7dccb47 Mon Sep 17 00:00:00 2001
+From 9970d7e44abf0e5a7c67c93228908af5a0303f86 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/10] btrfs: raid56 scrub and device replace with unrepairable data
+Date: Wed, 21 Oct 2026 07:09:39 -0400
+Subject: [PATCH 00/13] btrfs: raid56 scrub and device replace with unrepairable data
 To: linux-btrfs@vger.kernel.org
 
 RAID5/6 scrub and device replace handle uncorrectable data differently
@@ -58,7 +58,14 @@ uncounted and correctable ones unrepaired.  It comes last so there is
 no point in the series where replace carries on past a bad row without
 patches 2-3 writing its parity.
 
-Patch 9 lets raid56 parity scrub repair a raid6 data strip that has
+Patch 9 makes a read-only scrub (-r) of raid56 parity write nothing.
+finish_parity_scrub() never checked sctx->readonly, so a read-only
+scrub rewrote P/Q that disagreed with the data, on a read-only mount
+too.  The mismatches are still verified and counted.  It comes before
+patch 10 so that no point in the series rebuilds data in a read-only
+scrub.  Fixes: 5a6ac9eacb49.
+
+Patch 10 lets raid56 parity scrub repair a raid6 data strip that has
 no csum.  When the scrubbed P and Q both disagree with the data in a
 way that locates a single data strip (dQ = g^z dP for every differing
 byte), scrub rebuilds that strip from P and Q and writes it back
@@ -68,14 +75,29 @@ within tolerance.  Only the scrub of P rebuilds, so the parallel scrub
 of Q does not repeat it.  RAID5 has no such redundancy and keeps
 today's behaviour.  Fixes: 5a6ac9eacb49.
 
-Patch 10 counts parity mismatches found by raid56 parity scrub as
-corruption errors: in the device stats of the data member patch 9
+Patch 11 counts parity mismatches found by raid56 parity scrub as
+corruption errors: in the device stats of the data member patch 10
 rebuilt, otherwise of the scrubbed parity member, and in the scrub's
 corrected count.  They were rewritten silently before.  On RAID5, and
-on a RAID6 row patch 9 cannot locate, a mismatch may equally be
+on a RAID6 row patch 10 cannot locate, a mismatch may equally be
 undetectable corruption in a data strip without csums; it is charged to
 the parity member because there is no better information.
 
+Patch 12 names the right device in raid56 scrub error messages.  The
+"fixed up error / checksum error ... on dev X" lines mapped the sector
+with a non-READ op, which on raid56 returns the full-stripe write map,
+so they named the device of the full stripe's first data strip: damage
+on one device was reported on most of them.  Fixes: 0096580713ff.
+
+Patch 13 counts the data repairs a raid56 parity scrub makes.  The
+parity scrub of the P/Q holder reads and repairs the full stripe's data
+strips without reporting them, so a damaged sector it reached before
+the data device's own scrub was repaired and counted nowhere: a raid6
+scrub of a failing member counted 23% of its damaged sectors, and none
+when that member was scrubbed last.  Repairs are now counted once, by
+the parity scrub only when the data device is not being scrubbed at the
+same time, and not at all in a read-only scrub.  Fixes: 1009254bf22a.
+
 Testing: a raid5 filesystem filled with block-location-identifying
 data in partially filled stripes, two devices corrupted in chosen rows,
 every block classified (readable, content, EIO) before and after
@@ -90,7 +112,7 @@ Filipe Manana (1):
   btrfs: scrub: fix lost error return value when scrubbing raid56 parity
     stripe
 
-Zygo Blaxell (9):
+Zygo Blaxell (12):
   btrfs: scrub: check raid56 parity of the rows without unrepaired data
   btrfs: scrub: copy raid56 parity of unrepairable rows to the replace
     target
@@ -102,14 +124,17 @@ Zygo Blaxell (9):
   btrfs: scrub: read raid56 sectors without an extent from the replace
     source
   btrfs: don't abort raid56 data scrub on first uncorrectable sector
+  btrfs: scrub: do not write raid56 parity in a read-only scrub
   btrfs: scrub: rebuild a raid6 data stripe that both P and Q disagree
     with
   btrfs: scrub: count raid56 parity mismatches as corruption errors
+  btrfs: scrub: name the device a raid56 data sector was read from
+  btrfs: scrub: count the raid56 data repairs made by a parity scrub
 
- fs/btrfs/raid56.c | 229 +++++++++++++++++++++++++++++++++---
- fs/btrfs/raid56.h |   6 +-
- fs/btrfs/scrub.c  | 293 ++++++++++++++++++++++++++++++++++++++++++++--
- 3 files changed, 497 insertions(+), 31 deletions(-)
+ fs/btrfs/raid56.c | 234 +++++++++++++++++++++++--
+ fs/btrfs/raid56.h |   9 +-
+ fs/btrfs/scrub.c  | 433 +++++++++++++++++++++++++++++++++++++++++-----
+ 3 files changed, 617 insertions(+), 59 deletions(-)
 
 
 base-commit: 0c48d26d46bdd6422bb91ba7f1c519a2993116dc
index 7cc9c72b90ab7f371583c7acf337d63ee3e119ca..9ae42781cc05026eb75bab873b94b397f5c54604 100644 (file)
@@ -1,7 +1,7 @@
 From 84fa71315be6366bf0d836536c7c7f50560aa1f5 Mon Sep 17 00:00:00 2001
 From: Filipe Manana <fdmanana@suse.com>
 Date: Thu, 15 Oct 2026 18:45:45 +0100
-Subject: [PATCH 01/10] btrfs: scrub: fix lost error return value when
+Subject: [PATCH 01/13] btrfs: scrub: fix lost error return value when
  scrubbing raid56 parity stripe
 To: linux-btrfs@vger.kernel.org
 
index ce648b38ca3dc0356a1c5f452a319a474dd47631..f07e50dd883aa16bb005397abbd1dc09411031c8 100644 (file)
@@ -1,7 +1,7 @@
 From fdce5181605d8fc3d78a4852e5a04d310c35a521 Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Fri, 16 Oct 2026 02:32:16 -0400
-Subject: [PATCH 02/10] btrfs: scrub: check raid56 parity of the rows without
+Subject: [PATCH 02/13] btrfs: scrub: check raid56 parity of the rows without
  unrepaired data
 To: linux-btrfs@vger.kernel.org
 
index ca75206629cb56d47f5ea254ae3cd70a790ed255..5f91ce3262ddefe3d44af27a4234de36cdccfe64 100644 (file)
@@ -1,7 +1,7 @@
 From e47bef121de9dabf61d7044fb5adf5b0aabad031 Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Fri, 16 Oct 2026 02:32:16 -0400
-Subject: [PATCH 03/10] btrfs: scrub: copy raid56 parity of unrepairable rows
+Subject: [PATCH 03/13] btrfs: scrub: copy raid56 parity of unrepairable rows
  to the replace target
 To: linux-btrfs@vger.kernel.org
 
index 75592762d6fdea5eb3b99db6c3cd1f7e9291e193..a6002cec60728e7198e784cb6a224c8c658433d4 100644 (file)
@@ -1,7 +1,7 @@
 From a2f3c61ffd00b724c66a9a3e7b28af23bcaee0e4 Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Fri, 16 Oct 2026 02:32:17 -0400
-Subject: [PATCH 04/10] btrfs: scrub: copy unrepaired raid56 data sectors to
+Subject: [PATCH 04/13] btrfs: scrub: copy unrepaired raid56 data sectors to
  the replace target
 To: linux-btrfs@vger.kernel.org
 
index 996f3213f8ef1d6e06425417aa1a398aa1672b59..92e897c3a6b106bc6f5f28450c87a04dc9080c0a 100644 (file)
@@ -1,7 +1,7 @@
 From 8a2a9c58e862444eb6dedaf3198c3241f525e031 Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Fri, 16 Oct 2026 20:33:38 -0400
-Subject: [PATCH 05/10] btrfs: scrub: copy every sector of raid56 data stripes
+Subject: [PATCH 05/13] btrfs: scrub: copy every sector of raid56 data stripes
  to the replace target
 To: linux-btrfs@vger.kernel.org
 
index 1f45b11878e921ff395648c6053a127e7a176c2c..bbed28ca2c335a7f0b07252771ec9b797c3953ba 100644 (file)
@@ -1,7 +1,7 @@
 From 979b9e694a567841b7acdb09358c7e056f0325ac Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Fri, 16 Oct 2026 20:33:38 -0400
-Subject: [PATCH 06/10] btrfs: scrub: only avoid the replace source device for
+Subject: [PATCH 06/13] btrfs: scrub: only avoid the replace source device for
  its own stripes
 To: linux-btrfs@vger.kernel.org
 
index 3d7e29e531eca88bba2997e50168a98f8163af42..f3c856335aee9d58087c7482d9099f8aa92929b8 100644 (file)
@@ -1,7 +1,7 @@
 From 31a9785c46bf8c3c7c66b1c0dc40b42511ce5923 Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Fri, 16 Oct 2026 21:06:45 -0400
-Subject: [PATCH 07/10] btrfs: scrub: read raid56 sectors without an extent
+Subject: [PATCH 07/13] btrfs: scrub: read raid56 sectors without an extent
  from the replace source
 To: linux-btrfs@vger.kernel.org
 
index ba0eb9bd73c5406d4d902cc5fd7cff393c88fc8c..7745778f9af0d06dab70cd46c675ce861e977337 100644 (file)
@@ -1,7 +1,7 @@
 From 336c48ef59723ac8e1d01abf1e9076a369d685ef Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
 Date: Wed, 3 Jul 2024 21:00:18 -0400
-Subject: [PATCH 08/10] btrfs: don't abort raid56 data scrub on first
+Subject: [PATCH 08/13] btrfs: don't abort raid56 data scrub on first
  uncorrectable sector
 To: linux-btrfs@vger.kernel.org
 
diff --git a/topics/raid56-scrub-replace/0009-btrfs-scrub-do-not-write-raid56-parity-in-a-read-onl.patch b/topics/raid56-scrub-replace/0009-btrfs-scrub-do-not-write-raid56-parity-in-a-read-onl.patch
new file mode 100644 (file)
index 0000000..59500b0
--- /dev/null
@@ -0,0 +1,105 @@
+From d67280810713477185cf1dae7e72d5fe91300be1 Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Wed, 21 Oct 2026 03:59:11 -0400
+Subject: [PATCH 09/13] btrfs: scrub: do not write raid56 parity in a read-only
+ scrub
+To: linux-btrfs@vger.kernel.org
+
+A read-only scrub (btrfs scrub start -r) must not write to the devices.
+The data stripes honour that: their repairs are only written back when
+the scrub is not read-only.  The raid56 parity scrub never looks at it:
+finish_parity_scrub() rewrites every P or Q sector that does not match
+the data, whatever the scrub's mode.  So a read-only scrub of a raid56
+filesystem with stale or damaged parity rewrites it.
+
+Tell the parity scrub rbio whether the scrub is read-only, and in that
+case stop after the check, before any write is built.
+
+Test (raid56-wh-harness scrub-blame-test.sh SB_RO=1: read-only mount,
+scrub -r): with 8 damaged data sectors (every order) or 8 damaged parity
+sectors on one raid5/raid6 member, every member image is byte-identical
+after the scrub, and the damage is still counted.  Before: with damaged
+parity, the scrub rewrote it, and a member image changed.
+
+Fixes: 5a6ac9eacb49 ("Btrfs, raid56: support parity scrub on raid56")
+Assisted-by: Claude:claude-opus-5-5
+---
+ fs/btrfs/raid56.c | 8 +++++++-
+ fs/btrfs/raid56.h | 6 +++++-
+ fs/btrfs/scrub.c  | 3 ++-
+ 3 files changed, 14 insertions(+), 3 deletions(-)
+
+diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
+index 5ec31299f1c30..637be11a1ab4a 100644
+--- a/fs/btrfs/raid56.c
++++ b/fs/btrfs/raid56.c
+@@ -2557,7 +2557,8 @@ static void rmw_rbio_work_locked(struct work_struct *work)
+ struct btrfs_raid_bio *raid56_parity_alloc_scrub_rbio(struct bio *bio,
+                               struct btrfs_io_context *bioc,
+                               struct btrfs_device *scrub_dev,
+-                              unsigned long *dbitmap, int stripe_nsectors)
++                              unsigned long *dbitmap, int stripe_nsectors,
++                              bool readonly)
+ {
+       struct btrfs_fs_info *fs_info = bioc->fs_info;
+       struct btrfs_raid_bio *rbio;
+@@ -2588,6 +2589,7 @@ struct btrfs_raid_bio *raid56_parity_alloc_scrub_rbio(struct bio *bio,
+       ASSERT_RBIO_STRIPE(i < rbio->real_stripes, rbio, i);
+ 
+       bitmap_copy(&rbio->dbitmap, dbitmap, stripe_nsectors);
++      rbio->scrub_readonly = readonly;
+       return rbio;
+ }
+ 
+@@ -2770,6 +2772,10 @@ static int finish_parity_scrub(struct btrfs_raid_bio *rbio)
+               q_paddr = INVALID_PADDR;
+       }
+ 
++      /* A read-only scrub only checks: submit no write. */
++      if (rbio->scrub_readonly)
++              goto submit_write;
++
+       /*
+        * time to start writing.  Make bios for everything from the
+        * higher layers (the bio_list in our rbio) and our p/q.  Ignore
+diff --git a/fs/btrfs/raid56.h b/fs/btrfs/raid56.h
+index 8542648199f1a..1f68218598072 100644
+--- a/fs/btrfs/raid56.h
++++ b/fs/btrfs/raid56.h
+@@ -170,6 +170,9 @@ struct btrfs_raid_bio {
+       /* Stripe number that we're scrubbing  */
+       u8 scrubp;
+ 
++      /* Parity scrub of a read-only scrub: check, but write nothing. */
++      bool scrub_readonly;
++
+       /*
+        * Size of all the bios in the bio_list.  This helps us decide if the
+        * rbio maps to a full stripe or not.
+@@ -279,7 +282,8 @@ void raid56_parity_write(struct bio *bio, struct btrfs_io_context *bioc);
+ struct btrfs_raid_bio *raid56_parity_alloc_scrub_rbio(struct bio *bio,
+                               struct btrfs_io_context *bioc,
+                               struct btrfs_device *scrub_dev,
+-                              unsigned long *dbitmap, int stripe_nsectors);
++                              unsigned long *dbitmap, int stripe_nsectors,
++                              bool readonly);
+ void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio);
+ 
+ void raid56_parity_cache_data_folios(struct btrfs_raid_bio *rbio,
+diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
+index 02c015d9f09e4..690c8c9d76b5d 100644
+--- a/fs/btrfs/scrub.c
++++ b/fs/btrfs/scrub.c
+@@ -2294,7 +2294,8 @@ static int scrub_raid56_cached_parity(struct scrub_ctx *sctx,
+       /* For RAID56 write there must be an @bioc allocated. */
+       ASSERT(bioc);
+       rbio = raid56_parity_alloc_scrub_rbio(&bio, bioc, scrub_dev, extent_bitmap,
+-                              BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits);
++                              BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits,
++                              sctx->readonly);
+       btrfs_put_bioc(bioc);
+       if (!rbio) {
+               ret = -ENOMEM;
+-- 
+2.47.3
+
diff --git a/topics/raid56-scrub-replace/0009-btrfs-scrub-rebuild-a-raid6-data-stripe-that-both-P-.patch b/topics/raid56-scrub-replace/0009-btrfs-scrub-rebuild-a-raid6-data-stripe-that-both-P-.patch
deleted file mode 100644 (file)
index f539bb7..0000000
+++ /dev/null
@@ -1,327 +0,0 @@
-From 48d1eb9d60442cb3aafe1bbec2c2691aa7aaa12c Mon Sep 17 00:00:00 2001
-From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
-Date: Tue, 20 Oct 2026 13:43:04 -0400
-Subject: [PATCH 09/10] btrfs: scrub: rebuild a raid6 data stripe that both P
- and Q disagree with
-To: linux-btrfs@vger.kernel.org
-
-Scrub checks raid56 parity against the data stripes as they read.  Data
-without checksums (nodatasum and nodatacow files) cannot be verified, so
-it is taken as it reads, and when the parity being scrubbed differs,
-finish_parity_scrub() rewrites it from that data.
-
-That is wrong when the data is what is stale.  A write that fails on one
-device but succeeds within the profile's tolerance is acked and leaves
-that device's strip with its old contents, while P and Q were written
-from the new data.  With the device missing, the new data still rebuilds
-from parity.  Scrub then rewrites P and Q from the stale strip, and from
-then on the acked data is gone: with that device missing it rebuilds to
-the old contents.
-
-On RAID6, P and Q together locate one bad stripe without a checksum: if
-data stripe z is off by e, P is off by e and Q by g^z * e, in every
-byte.  When both differ from the data in that pattern, rebuild data
-stripe z and write it back instead of rewriting the parity.  Two parity
-strips that are stale independently do not fit that pattern by chance
-over a whole sector.  Any other difference (P or Q alone, or no single
-z) keeps the parity rewrite.  The P and Q devices are scrubbed in
-parallel, each from its own read of the data, so only the scrub of P
-rebuilds; the scrub of Q leaves such a row alone (Q is right).  A torn
-write that reached one data stripe but neither P nor Q fits the pattern
-too; the rebuild returns that stripe to its old contents, as valid for
-an unacked write as the new ones.  Rows with a read error were rebuilt
-by recover_scrub_rbio() from the parity that is not being scrubbed, and
-are left alone.
-
-RAID5 has a single parity, so it cannot tell a stale data strip from
-stale parity, and is unchanged.
-
-Test (raid56-wh-harness nodatasum-scrub-repair-test.sh): nodatasum files
-on a 5-device raid6, each file written with one member failing writes
-during its writeback (each member in turn; within tolerance, so acked),
-then scrub, then every file read with no member, each member and each
-pair missing.  Before: scrub kept the 6 stale data strips and rewrote P
-and Q from them; 64 of 80 reads were wrong afterwards, 20 of them correct
-before the scrub.  After: the stale strips are rebuilt, P and Q match the
-data, and 80 of 80 reads are correct, with and without stripe_alloc.  Two
-stale strips per row (two members failing) cannot be located and still
-get their parity rewritten, as before.
-
-Fixes: 5a6ac9eacb49 ("Btrfs, raid56: support parity scrub on raid56")
-Assisted-by: Claude:claude-opus-5-5
----
- fs/btrfs/raid56.c | 195 +++++++++++++++++++++++++++++++++++++++++-----
- 1 file changed, 177 insertions(+), 18 deletions(-)
-
-diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
-index 5ec31299f1c30..e25646c4b5cff 100644
---- a/fs/btrfs/raid56.c
-+++ b/fs/btrfs/raid56.c
-@@ -9,6 +9,7 @@
- #include <linux/slab.h>
- #include <linux/blkdev.h>
- #include <linux/raid/pq.h>
-+#include <linux/raid/pq_tables.h>
- #include <linux/hash.h>
- #include <linux/list_sort.h>
- #include <linux/raid/xor.h>
-@@ -2634,16 +2635,13 @@ static int alloc_rbio_essential_pages(struct btrfs_raid_bio *rbio)
-       return 0;
- }
- 
--/* Return true if the content of the step matches the calculated one. */
--static bool verify_one_parity_step(struct btrfs_raid_bio *rbio,
--                                 void *pointers[], unsigned int sector_nr,
--                                 unsigned int step_nr)
-+/* Calculate P (and Q) of one step into the temporary pages in @pointers. */
-+static void gen_one_parity_step(struct btrfs_raid_bio *rbio, void *pointers[],
-+                              unsigned int sector_nr, unsigned int step_nr)
- {
-       const unsigned int nr_data = rbio->nr_data;
-       const bool has_qstripe = (rbio->real_stripes - rbio->nr_data == 2);
-       const u32 step = min(rbio->bioc->fs_info->sectorsize, PAGE_SIZE);
--      void *parity;
--      bool ret = false;
- 
-       ASSERT(step_nr < rbio->sector_nsteps);
- 
-@@ -2663,36 +2661,159 @@ static bool verify_one_parity_step(struct btrfs_raid_bio *rbio,
-               xor_gen(pointers[nr_data], pointers + 1, nr_data - 1, step);
-       }
- 
-+      for (int stripe = nr_data - 1; stripe >= 0; stripe--)
-+              kunmap_local(pointers[stripe]);
-+}
-+
-+/*
-+ * Return true if the content of the step matches the calculated one.  If not
-+ * and @repair is set, replace it with the calculated one.
-+ */
-+static bool verify_one_parity_step(struct btrfs_raid_bio *rbio,
-+                                 void *pointers[], unsigned int sector_nr,
-+                                 unsigned int step_nr, bool repair)
-+{
-+      const u32 step = min(rbio->bioc->fs_info->sectorsize, PAGE_SIZE);
-+      void *parity;
-+      bool ret = false;
-+
-+      gen_one_parity_step(rbio, pointers, sector_nr, step_nr);
-+
-       /* Check scrubbing parity and repair it. */
-       parity = kmap_local_paddr(rbio_stripe_paddr(rbio, rbio->scrubp, sector_nr, step_nr));
--      if (memcmp(parity, pointers[rbio->scrubp], step) != 0)
--              memcpy(parity, pointers[rbio->scrubp], step);
--      else
-+      if (memcmp(parity, pointers[rbio->scrubp], step) != 0) {
-+              if (repair)
-+                      memcpy(parity, pointers[rbio->scrubp], step);
-+      } else {
-               ret = true;
-+      }
-       kunmap_local(parity);
--
--      for (int stripe = nr_data - 1; stripe >= 0; stripe--)
--              kunmap_local(pointers[stripe]);
-       return ret;
- }
- 
-+/*
-+ * RAID6: find the one data stripe that explains why P and Q of @sector_nr
-+ * both differ from the ones calculated from the data.
-+ *
-+ * If data stripe z is off by e, P is off by e and Q by g^z * e, in every
-+ * byte.  Return z if every differing byte of the sector shows that pattern
-+ * with the same z, or -1 if it does not (P or Q alone is off, or more than
-+ * one stripe is).
-+ */
-+static int raid6_locate_bad_data(struct btrfs_raid_bio *rbio, void *pointers[],
-+                               unsigned int sector_nr)
-+{
-+      const int nr_data = rbio->nr_data;
-+      const u32 step = min(rbio->bioc->fs_info->sectorsize, PAGE_SIZE);
-+      const u8 *p_calc = pointers[nr_data];
-+      const u8 *q_calc = pointers[rbio->real_stripes - 1];
-+      int bad = -1;
-+
-+      for (int step_nr = 0; step_nr < rbio->sector_nsteps; step_nr++) {
-+              const u8 *p;
-+              const u8 *q;
-+
-+              gen_one_parity_step(rbio, pointers, sector_nr, step_nr);
-+              p = kmap_local_paddr(rbio_pstripe_paddr(rbio, sector_nr, step_nr));
-+              q = kmap_local_paddr(rbio_qstripe_paddr(rbio, sector_nr, step_nr));
-+              for (u32 i = 0; i < step; i++) {
-+                      const u8 dp = p[i] ^ p_calc[i];
-+                      const u8 dq = q[i] ^ q_calc[i];
-+                      int z;
-+
-+                      if (!dp && !dq)
-+                              continue;
-+                      if (!dp || !dq) {
-+                              bad = -2;
-+                              break;
-+                      }
-+                      z = (raid6_gflog[dq] + 255 - raid6_gflog[dp]) % 255;
-+                      if (z >= nr_data || (bad >= 0 && z != bad)) {
-+                              bad = -2;
-+                              break;
-+                      }
-+                      bad = z;
-+              }
-+              kunmap_local(q);
-+              kunmap_local(p);
-+              if (bad == -2)
-+                      break;
-+      }
-+      return bad < 0 ? -1 : bad;
-+}
-+
-+/* Rebuild data stripe @stripe of @sector_nr: it is off by P ^ calculated P. */
-+static void raid6_rebuild_data_sector(struct btrfs_raid_bio *rbio, void *pointers[],
-+                                    unsigned int sector_nr, int stripe)
-+{
-+      const u32 step = min(rbio->bioc->fs_info->sectorsize, PAGE_SIZE);
-+
-+      for (int step_nr = 0; step_nr < rbio->sector_nsteps; step_nr++) {
-+              void *srcs[2];
-+              void *data;
-+
-+              gen_one_parity_step(rbio, pointers, sector_nr, step_nr);
-+              data = kmap_local_paddr(rbio_stripe_paddr(rbio, stripe, sector_nr, step_nr));
-+              srcs[0] = kmap_local_paddr(rbio_pstripe_paddr(rbio, sector_nr, step_nr));
-+              srcs[1] = pointers[rbio->nr_data];
-+              xor_gen(data, srcs, 2, step);
-+              kunmap_local(srcs[0]);
-+              kunmap_local(data);
-+      }
-+}
-+
- /*
-  * The @pointers array should have the P/Q parity already mapped.
-+ *
-+ * Return the data stripe that was rebuilt instead of rewriting the parity,
-+ * or -1.
-  */
--static void verify_one_parity_sector(struct btrfs_raid_bio *rbio,
--                                   void *pointers[], unsigned int sector_nr)
-+static int verify_one_parity_sector(struct btrfs_raid_bio *rbio,
-+                                  void *pointers[], unsigned int sector_nr,
-+                                  bool can_locate)
- {
-+      const bool has_qstripe = (rbio->real_stripes - rbio->nr_data == 2);
-       bool found_error = false;
- 
-       for (int step_nr = 0; step_nr < rbio->sector_nsteps; step_nr++) {
-               bool match;
- 
--              match = verify_one_parity_step(rbio, pointers, sector_nr, step_nr);
-+              match = verify_one_parity_step(rbio, pointers, sector_nr, step_nr, false);
-               if (!match)
-                       found_error = true;
-       }
--      if (!found_error)
-+      if (!found_error) {
-               bitmap_clear(&rbio->dbitmap, sector_nr, 1);
-+              return -1;
-+      }
-+
-+      /*
-+       * Data without a checksum is taken as it was read, so parity
-+       * calculated from it is not necessarily right: one data stripe may be
-+       * stale, e.g. after a write that failed on its device but succeeded on
-+       * the others.  On RAID6, P and Q together tell which one, so rebuild
-+       * that data stripe instead of rewriting the parity from it.
-+       */
-+      if (has_qstripe && can_locate) {
-+              const int bad = raid6_locate_bad_data(rbio, pointers, sector_nr);
-+
-+              if (bad >= 0) {
-+                      /*
-+                       * P and Q are scrubbed in parallel, each with its own
-+                       * read of the data: leave the rebuild to the scrub of P,
-+                       * so it is done and counted once.  Q is right.
-+                       */
-+                      bitmap_clear(&rbio->dbitmap, sector_nr, 1);
-+                      if (rbio->scrubp != rbio->nr_data)
-+                              return -1;
-+                      raid6_rebuild_data_sector(rbio, pointers, sector_nr, bad);
-+                      return bad;
-+              }
-+      }
-+
-+      for (int step_nr = 0; step_nr < rbio->sector_nsteps; step_nr++)
-+              verify_one_parity_step(rbio, pointers, sector_nr, step_nr, true);
-+      return -1;
- }
- 
- static int finish_parity_scrub(struct btrfs_raid_bio *rbio)
-@@ -2708,6 +2829,9 @@ static int finish_parity_scrub(struct btrfs_raid_bio *rbio)
-       phys_addr_t q_paddr = INVALID_PADDR;
-       struct bio_list bio_list;
-       bool is_replace = false;
-+      unsigned long read_errors = 0;
-+      unsigned long rebuilt = 0;
-+      u8 rebuilt_stripe[BITS_PER_LONG];
-       int ret;
- 
-       bio_list_init(&bio_list);
-@@ -2755,12 +2879,27 @@ static int finish_parity_scrub(struct btrfs_raid_bio *rbio)
-               pointers[rbio->real_stripes - 1] = kmap_local_paddr(q_paddr);
-       }
- 
-+      /*
-+       * Rows with a read error were rebuilt by recover_scrub_rbio(): do not
-+       * look for a bad data stripe there.
-+       */
-+      for_each_set_bit(sectornr, &rbio->dbitmap, rbio->stripe_nsectors)
-+              if (get_rbio_vertical_errors(rbio, sectornr, NULL, NULL))
-+                      __set_bit(sectornr, &read_errors);
-+
-       bitmap_clear(rbio->error_bitmap, 0, rbio->nr_sectors);
- 
-       /* Map the parity stripe just once */
- 
--      for_each_set_bit(sectornr, &rbio->dbitmap, rbio->stripe_nsectors)
--              verify_one_parity_sector(rbio, pointers, sectornr);
-+      for_each_set_bit(sectornr, &rbio->dbitmap, rbio->stripe_nsectors) {
-+              const int stripe = verify_one_parity_sector(rbio, pointers, sectornr,
-+                                              !test_bit(sectornr, &read_errors));
-+
-+              if (stripe >= 0) {
-+                      __set_bit(sectornr, &rebuilt);
-+                      rebuilt_stripe[sectornr] = stripe;
-+              }
-+      }
- 
-       kunmap_local(pointers[nr_data]);
-       __free_page(phys_to_page(p_paddr));
-@@ -2770,6 +2909,26 @@ static int finish_parity_scrub(struct btrfs_raid_bio *rbio)
-               q_paddr = INVALID_PADDR;
-       }
- 
-+      /* Write the data sectors rebuilt from P and Q. */
-+      for_each_set_bit(sectornr, &rebuilt, rbio->stripe_nsectors) {
-+              const int stripe = rebuilt_stripe[sectornr];
-+              const struct btrfs_io_stripe *io_stripe = &bioc->stripes[stripe];
-+
-+              btrfs_warn_rl(bioc->fs_info,
-+"scrub: raid6 data at logical %llu on devid %llu physical %llu did not match P and Q, rebuilt it",
-+                            bioc->full_stripe_logical +
-+                            btrfs_stripe_nr_to_offset(stripe) +
-+                            ((u64)sectornr << bioc->fs_info->sectorsize_bits),
-+                            io_stripe->dev->devid,
-+                            io_stripe->physical +
-+                            ((u64)sectornr << bioc->fs_info->sectorsize_bits));
-+              ret = rbio_add_io_paddrs(rbio, &bio_list,
-+                                       rbio_stripe_paddrs(rbio, stripe, sectornr),
-+                                       stripe, sectornr, REQ_OP_WRITE);
-+              if (ret)
-+                      goto cleanup;
-+      }
-+
-       /*
-        * time to start writing.  Make bios for everything from the
-        * higher layers (the bio_list in our rbio) and our p/q.  Ignore
--- 
-2.47.3
-
diff --git a/topics/raid56-scrub-replace/0010-btrfs-scrub-count-raid56-parity-mismatches-as-corrup.patch b/topics/raid56-scrub-replace/0010-btrfs-scrub-count-raid56-parity-mismatches-as-corrup.patch
deleted file mode 100644 (file)
index 635b68d..0000000
+++ /dev/null
@@ -1,174 +0,0 @@
-From 2a30d9423f4e3ba05ab8cf4058ccfc94c7dccb47 Mon Sep 17 00:00:00 2001
-From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
-Date: Tue, 20 Oct 2026 16:15:58 -0400
-Subject: [PATCH 10/10] btrfs: scrub: count raid56 parity mismatches as
- corruption errors
-To: linux-btrfs@vger.kernel.org
-
-When scrub finds raid56 parity that does not match the data, it rewrites
-the parity (or, on RAID6, rebuilds the data stripe that P and Q locate),
-but counts nothing: the device stats do not change, and the scrub
-progress, and so `btrfs scrub status`, says "no errors found".  A
-corrupted parity strip, a stale data strip, or a write hole is repaired
-or papered over without a trace.
-
-Count every sector that did not match as a corruption error
-(BTRFS_DEV_STAT_CORRUPTION_ERRS) of the device that held the bad copy,
-and add it to the scrub's corrected_errors:
-
-- a data stripe that P and Q located and that was rebuilt: its device;
-- otherwise the parity being scrubbed: its device.
-
-The second case is not always right.  Without a checksum on the data and
-without a second parity to locate the error (RAID5, or a RAID6 row that
-the decode cannot place), a mismatch may as well be a corrupt or stale
-data sector that nothing can detect; scrub rewrites the parity from that
-data, and the parity device is charged.  Rows whose stripes had a read
-error are not counted again: the read error was.
-
-The device is the one in the scrub rbio's io context at the stripe that
-held the bad copy.  During dev-replace the scrubbed parity is the
-source's (the target is only written), so the source is charged.
-
-Test (raid56-wh-harness raid56-devstats-test.sh, arm P): 4 sectors of a
-P strip (on raid6 also 4 of a Q strip, other rows) overwritten, then
-scrub.  Before: no counter moved and scrub said "no errors found".
-After: corruption_errs +4 on exactly the corrupted member(s), Corrected
-4 (raid6 8), and a second scrub finds nothing.
-nodatasum-scrub-repair-test.sh, raid6 with one stale member per row:
-every stale sector is charged once, to the member that held it.
-
-Assisted-by: Claude:claude-opus-5-5
----
- fs/btrfs/raid56.c | 34 +++++++++++++++++++++++++++++++++-
- fs/btrfs/raid56.h |  6 +++++-
- fs/btrfs/scrub.c  |  9 ++++++++-
- 3 files changed, 46 insertions(+), 3 deletions(-)
-
-diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
-index e25646c4b5cff..f02dfb791fd84 100644
---- a/fs/btrfs/raid56.c
-+++ b/fs/btrfs/raid56.c
-@@ -2558,7 +2558,8 @@ static void rmw_rbio_work_locked(struct work_struct *work)
- struct btrfs_raid_bio *raid56_parity_alloc_scrub_rbio(struct bio *bio,
-                               struct btrfs_io_context *bioc,
-                               struct btrfs_device *scrub_dev,
--                              unsigned long *dbitmap, int stripe_nsectors)
-+                              unsigned long *dbitmap, int stripe_nsectors,
-+                              u32 *corrected)
- {
-       struct btrfs_fs_info *fs_info = bioc->fs_info;
-       struct btrfs_raid_bio *rbio;
-@@ -2589,6 +2590,7 @@ struct btrfs_raid_bio *raid56_parity_alloc_scrub_rbio(struct bio *bio,
-       ASSERT_RBIO_STRIPE(i < rbio->real_stripes, rbio, i);
- 
-       bitmap_copy(&rbio->dbitmap, dbitmap, stripe_nsectors);
-+      rbio->scrub_corrected = corrected;
-       return rbio;
- }
- 
-@@ -2816,6 +2818,35 @@ static int verify_one_parity_sector(struct btrfs_raid_bio *rbio,
-       return -1;
- }
- 
-+/*
-+ * Count each sector that did not match as a corruption error of the device
-+ * that held it: the data stripe rebuilt from P and Q, or else the scrubbed
-+ * parity, which is rewritten from the data.  Without a checksum and without
-+ * a decode (RAID5, or a RAID6 row the decode cannot place), the mismatch may
-+ * as well be corrupt data that nothing can find; the parity is charged.
-+ * Rows with a read error are not counted here: the read error was.
-+ */
-+static void count_parity_scrub_errors(struct btrfs_raid_bio *rbio,
-+                                    unsigned long read_errors,
-+                                    unsigned long rebuilt,
-+                                    const u8 *rebuilt_stripe)
-+{
-+      struct btrfs_io_context *bioc = rbio->bioc;
-+      unsigned long parity_bad;
-+      unsigned int sectornr;
-+
-+      bitmap_andnot(&parity_bad, &rbio->dbitmap, &read_errors, rbio->stripe_nsectors);
-+      for_each_set_bit(sectornr, &rebuilt, rbio->stripe_nsectors)
-+              btrfs_dev_stat_inc_and_print(bioc->stripes[rebuilt_stripe[sectornr]].dev,
-+                                           BTRFS_DEV_STAT_CORRUPTION_ERRS);
-+      for_each_set_bit(sectornr, &parity_bad, rbio->stripe_nsectors)
-+              btrfs_dev_stat_inc_and_print(bioc->stripes[rbio->scrubp].dev,
-+                                           BTRFS_DEV_STAT_CORRUPTION_ERRS);
-+      if (rbio->scrub_corrected)
-+              *rbio->scrub_corrected += bitmap_weight(&rebuilt, rbio->stripe_nsectors) +
-+                                        bitmap_weight(&parity_bad, rbio->stripe_nsectors);
-+}
-+
- static int finish_parity_scrub(struct btrfs_raid_bio *rbio)
- {
-       struct btrfs_io_context *bioc = rbio->bioc;
-@@ -2900,6 +2931,7 @@ static int finish_parity_scrub(struct btrfs_raid_bio *rbio)
-                       rebuilt_stripe[sectornr] = stripe;
-               }
-       }
-+      count_parity_scrub_errors(rbio, read_errors, rebuilt, rebuilt_stripe);
- 
-       kunmap_local(pointers[nr_data]);
-       __free_page(phys_to_page(p_paddr));
-diff --git a/fs/btrfs/raid56.h b/fs/btrfs/raid56.h
-index 8542648199f1a..8fb697be9f78b 100644
---- a/fs/btrfs/raid56.h
-+++ b/fs/btrfs/raid56.h
-@@ -170,6 +170,9 @@ struct btrfs_raid_bio {
-       /* Stripe number that we're scrubbing  */
-       u8 scrubp;
- 
-+      /* Parity scrub: where to add the number of mismatching sectors. */
-+      u32 *scrub_corrected;
-+
-       /*
-        * Size of all the bios in the bio_list.  This helps us decide if the
-        * rbio maps to a full stripe or not.
-@@ -279,7 +282,8 @@ void raid56_parity_write(struct bio *bio, struct btrfs_io_context *bioc);
- struct btrfs_raid_bio *raid56_parity_alloc_scrub_rbio(struct bio *bio,
-                               struct btrfs_io_context *bioc,
-                               struct btrfs_device *scrub_dev,
--                              unsigned long *dbitmap, int stripe_nsectors);
-+                              unsigned long *dbitmap, int stripe_nsectors,
-+                              u32 *corrected);
- void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio);
- 
- void raid56_parity_cache_data_folios(struct btrfs_raid_bio *rbio,
-diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
-index 02c015d9f09e4..c827535b88ef8 100644
---- a/fs/btrfs/scrub.c
-+++ b/fs/btrfs/scrub.c
-@@ -2279,6 +2279,7 @@ static int scrub_raid56_cached_parity(struct scrub_ctx *sctx,
-       struct bio bio;
-       const int data_stripes = nr_data_stripes(map);
-       u64 length = btrfs_stripe_nr_to_offset(data_stripes);
-+      u32 corrected = 0;
-       int ret;
- 
-       bio_init(&bio, NULL, NULL, 0, REQ_OP_READ);
-@@ -2294,7 +2295,8 @@ static int scrub_raid56_cached_parity(struct scrub_ctx *sctx,
-       /* For RAID56 write there must be an @bioc allocated. */
-       ASSERT(bioc);
-       rbio = raid56_parity_alloc_scrub_rbio(&bio, bioc, scrub_dev, extent_bitmap,
--                              BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits);
-+                              BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits,
-+                              &corrected);
-       btrfs_put_bioc(bioc);
-       if (!rbio) {
-               ret = -ENOMEM;
-@@ -2310,6 +2312,11 @@ static int scrub_raid56_cached_parity(struct scrub_ctx *sctx,
-       raid56_parity_submit_scrub_rbio(rbio);
-       wait_for_completion_io(&io_done);
-       ret = blk_status_to_errno(bio.bi_status);
-+      if (corrected) {
-+              spin_lock(&sctx->stat_lock);
-+              sctx->stat.corrected_errors += corrected;
-+              spin_unlock(&sctx->stat_lock);
-+      }
- out:
-       btrfs_bio_counter_dec(fs_info);
-       bio_uninit(&bio);
--- 
-2.47.3
-
diff --git a/topics/raid56-scrub-replace/0010-btrfs-scrub-rebuild-a-raid6-data-stripe-that-both-P-.patch b/topics/raid56-scrub-replace/0010-btrfs-scrub-rebuild-a-raid6-data-stripe-that-both-P-.patch
new file mode 100644 (file)
index 0000000..ca47be9
--- /dev/null
@@ -0,0 +1,328 @@
+From 1395225e695d542703268c68a433194058a322de Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Tue, 20 Oct 2026 13:43:04 -0400
+Subject: [PATCH 10/13] btrfs: scrub: rebuild a raid6 data stripe that both P
+ and Q disagree with
+To: linux-btrfs@vger.kernel.org
+
+Scrub checks raid56 parity against the data stripes as they read.  Data
+without checksums (nodatasum and nodatacow files) cannot be verified, so
+it is taken as it reads, and when the parity being scrubbed differs,
+finish_parity_scrub() rewrites it from that data.
+
+That is wrong when the data is what is stale.  A write that fails on one
+device but succeeds within the profile's tolerance is acked and leaves
+that device's strip with its old contents, while P and Q were written
+from the new data.  With the device missing, the new data still rebuilds
+from parity.  Scrub then rewrites P and Q from the stale strip, and from
+then on the acked data is gone: with that device missing it rebuilds to
+the old contents.
+
+On RAID6, P and Q together locate one bad stripe without a checksum: if
+data stripe z is off by e, P is off by e and Q by g^z * e, in every
+byte.  When both differ from the data in that pattern, rebuild data
+stripe z and write it back instead of rewriting the parity.  Two parity
+strips that are stale independently do not fit that pattern by chance
+over a whole sector.  Any other difference (P or Q alone, or no single
+z) keeps the parity rewrite.  The P and Q devices are scrubbed in
+parallel, each from its own read of the data, so only the scrub of P
+rebuilds; the scrub of Q leaves such a row alone (Q is right).  A torn
+write that reached one data stripe but neither P nor Q fits the pattern
+too; the rebuild returns that stripe to its old contents, as valid for
+an unacked write as the new ones.  Rows with a read error were rebuilt
+by recover_scrub_rbio() from the parity that is not being scrubbed, and
+are left alone.  In a read-only scrub nothing is written (previous
+patch).
+
+RAID5 has a single parity, so it cannot tell a stale data strip from
+stale parity, and is unchanged.
+
+Test (raid56-wh-harness nodatasum-scrub-repair-test.sh): nodatasum files
+on a 5-device raid6, each file written with one member failing writes
+during its writeback (each member in turn; within tolerance, so acked),
+then scrub, then every file read with no member, each member and each
+pair missing.  Before: scrub kept the 6 stale data strips and rewrote P
+and Q from them; 64 of 80 reads were wrong afterwards, 20 of them correct
+before the scrub.  After: the stale strips are rebuilt, P and Q match the
+data, and 80 of 80 reads are correct, with and without stripe_alloc.  Two
+stale strips per row (two members failing) cannot be located and still
+get their parity rewritten, as before.
+
+Fixes: 5a6ac9eacb49 ("Btrfs, raid56: support parity scrub on raid56")
+Assisted-by: Claude:claude-opus-5-5
+---
+ fs/btrfs/raid56.c | 195 +++++++++++++++++++++++++++++++++++++++++-----
+ 1 file changed, 177 insertions(+), 18 deletions(-)
+
+diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
+index 637be11a1ab4a..b3366434f0d90 100644
+--- a/fs/btrfs/raid56.c
++++ b/fs/btrfs/raid56.c
+@@ -9,6 +9,7 @@
+ #include <linux/slab.h>
+ #include <linux/blkdev.h>
+ #include <linux/raid/pq.h>
++#include <linux/raid/pq_tables.h>
+ #include <linux/hash.h>
+ #include <linux/list_sort.h>
+ #include <linux/raid/xor.h>
+@@ -2636,16 +2637,13 @@ static int alloc_rbio_essential_pages(struct btrfs_raid_bio *rbio)
+       return 0;
+ }
+ 
+-/* Return true if the content of the step matches the calculated one. */
+-static bool verify_one_parity_step(struct btrfs_raid_bio *rbio,
+-                                 void *pointers[], unsigned int sector_nr,
+-                                 unsigned int step_nr)
++/* Calculate P (and Q) of one step into the temporary pages in @pointers. */
++static void gen_one_parity_step(struct btrfs_raid_bio *rbio, void *pointers[],
++                              unsigned int sector_nr, unsigned int step_nr)
+ {
+       const unsigned int nr_data = rbio->nr_data;
+       const bool has_qstripe = (rbio->real_stripes - rbio->nr_data == 2);
+       const u32 step = min(rbio->bioc->fs_info->sectorsize, PAGE_SIZE);
+-      void *parity;
+-      bool ret = false;
+ 
+       ASSERT(step_nr < rbio->sector_nsteps);
+ 
+@@ -2665,36 +2663,159 @@ static bool verify_one_parity_step(struct btrfs_raid_bio *rbio,
+               xor_gen(pointers[nr_data], pointers + 1, nr_data - 1, step);
+       }
+ 
++      for (int stripe = nr_data - 1; stripe >= 0; stripe--)
++              kunmap_local(pointers[stripe]);
++}
++
++/*
++ * Return true if the content of the step matches the calculated one.  If not
++ * and @repair is set, replace it with the calculated one.
++ */
++static bool verify_one_parity_step(struct btrfs_raid_bio *rbio,
++                                 void *pointers[], unsigned int sector_nr,
++                                 unsigned int step_nr, bool repair)
++{
++      const u32 step = min(rbio->bioc->fs_info->sectorsize, PAGE_SIZE);
++      void *parity;
++      bool ret = false;
++
++      gen_one_parity_step(rbio, pointers, sector_nr, step_nr);
++
+       /* Check scrubbing parity and repair it. */
+       parity = kmap_local_paddr(rbio_stripe_paddr(rbio, rbio->scrubp, sector_nr, step_nr));
+-      if (memcmp(parity, pointers[rbio->scrubp], step) != 0)
+-              memcpy(parity, pointers[rbio->scrubp], step);
+-      else
++      if (memcmp(parity, pointers[rbio->scrubp], step) != 0) {
++              if (repair)
++                      memcpy(parity, pointers[rbio->scrubp], step);
++      } else {
+               ret = true;
++      }
+       kunmap_local(parity);
+-
+-      for (int stripe = nr_data - 1; stripe >= 0; stripe--)
+-              kunmap_local(pointers[stripe]);
+       return ret;
+ }
+ 
++/*
++ * RAID6: find the one data stripe that explains why P and Q of @sector_nr
++ * both differ from the ones calculated from the data.
++ *
++ * If data stripe z is off by e, P is off by e and Q by g^z * e, in every
++ * byte.  Return z if every differing byte of the sector shows that pattern
++ * with the same z, or -1 if it does not (P or Q alone is off, or more than
++ * one stripe is).
++ */
++static int raid6_locate_bad_data(struct btrfs_raid_bio *rbio, void *pointers[],
++                               unsigned int sector_nr)
++{
++      const int nr_data = rbio->nr_data;
++      const u32 step = min(rbio->bioc->fs_info->sectorsize, PAGE_SIZE);
++      const u8 *p_calc = pointers[nr_data];
++      const u8 *q_calc = pointers[rbio->real_stripes - 1];
++      int bad = -1;
++
++      for (int step_nr = 0; step_nr < rbio->sector_nsteps; step_nr++) {
++              const u8 *p;
++              const u8 *q;
++
++              gen_one_parity_step(rbio, pointers, sector_nr, step_nr);
++              p = kmap_local_paddr(rbio_pstripe_paddr(rbio, sector_nr, step_nr));
++              q = kmap_local_paddr(rbio_qstripe_paddr(rbio, sector_nr, step_nr));
++              for (u32 i = 0; i < step; i++) {
++                      const u8 dp = p[i] ^ p_calc[i];
++                      const u8 dq = q[i] ^ q_calc[i];
++                      int z;
++
++                      if (!dp && !dq)
++                              continue;
++                      if (!dp || !dq) {
++                              bad = -2;
++                              break;
++                      }
++                      z = (raid6_gflog[dq] + 255 - raid6_gflog[dp]) % 255;
++                      if (z >= nr_data || (bad >= 0 && z != bad)) {
++                              bad = -2;
++                              break;
++                      }
++                      bad = z;
++              }
++              kunmap_local(q);
++              kunmap_local(p);
++              if (bad == -2)
++                      break;
++      }
++      return bad < 0 ? -1 : bad;
++}
++
++/* Rebuild data stripe @stripe of @sector_nr: it is off by P ^ calculated P. */
++static void raid6_rebuild_data_sector(struct btrfs_raid_bio *rbio, void *pointers[],
++                                    unsigned int sector_nr, int stripe)
++{
++      const u32 step = min(rbio->bioc->fs_info->sectorsize, PAGE_SIZE);
++
++      for (int step_nr = 0; step_nr < rbio->sector_nsteps; step_nr++) {
++              void *srcs[2];
++              void *data;
++
++              gen_one_parity_step(rbio, pointers, sector_nr, step_nr);
++              data = kmap_local_paddr(rbio_stripe_paddr(rbio, stripe, sector_nr, step_nr));
++              srcs[0] = kmap_local_paddr(rbio_pstripe_paddr(rbio, sector_nr, step_nr));
++              srcs[1] = pointers[rbio->nr_data];
++              xor_gen(data, srcs, 2, step);
++              kunmap_local(srcs[0]);
++              kunmap_local(data);
++      }
++}
++
+ /*
+  * The @pointers array should have the P/Q parity already mapped.
++ *
++ * Return the data stripe that was rebuilt instead of rewriting the parity,
++ * or -1.
+  */
+-static void verify_one_parity_sector(struct btrfs_raid_bio *rbio,
+-                                   void *pointers[], unsigned int sector_nr)
++static int verify_one_parity_sector(struct btrfs_raid_bio *rbio,
++                                  void *pointers[], unsigned int sector_nr,
++                                  bool can_locate)
+ {
++      const bool has_qstripe = (rbio->real_stripes - rbio->nr_data == 2);
+       bool found_error = false;
+ 
+       for (int step_nr = 0; step_nr < rbio->sector_nsteps; step_nr++) {
+               bool match;
+ 
+-              match = verify_one_parity_step(rbio, pointers, sector_nr, step_nr);
++              match = verify_one_parity_step(rbio, pointers, sector_nr, step_nr, false);
+               if (!match)
+                       found_error = true;
+       }
+-      if (!found_error)
++      if (!found_error) {
+               bitmap_clear(&rbio->dbitmap, sector_nr, 1);
++              return -1;
++      }
++
++      /*
++       * Data without a checksum is taken as it was read, so parity
++       * calculated from it is not necessarily right: one data stripe may be
++       * stale, e.g. after a write that failed on its device but succeeded on
++       * the others.  On RAID6, P and Q together tell which one, so rebuild
++       * that data stripe instead of rewriting the parity from it.
++       */
++      if (has_qstripe && can_locate) {
++              const int bad = raid6_locate_bad_data(rbio, pointers, sector_nr);
++
++              if (bad >= 0) {
++                      /*
++                       * P and Q are scrubbed in parallel, each with its own
++                       * read of the data: leave the rebuild to the scrub of P,
++                       * so it is done and counted once.  Q is right.
++                       */
++                      bitmap_clear(&rbio->dbitmap, sector_nr, 1);
++                      if (rbio->scrubp != rbio->nr_data)
++                              return -1;
++                      raid6_rebuild_data_sector(rbio, pointers, sector_nr, bad);
++                      return bad;
++              }
++      }
++
++      for (int step_nr = 0; step_nr < rbio->sector_nsteps; step_nr++)
++              verify_one_parity_step(rbio, pointers, sector_nr, step_nr, true);
++      return -1;
+ }
+ 
+ static int finish_parity_scrub(struct btrfs_raid_bio *rbio)
+@@ -2710,6 +2831,9 @@ static int finish_parity_scrub(struct btrfs_raid_bio *rbio)
+       phys_addr_t q_paddr = INVALID_PADDR;
+       struct bio_list bio_list;
+       bool is_replace = false;
++      unsigned long read_errors = 0;
++      unsigned long rebuilt = 0;
++      u8 rebuilt_stripe[BITS_PER_LONG];
+       int ret;
+ 
+       bio_list_init(&bio_list);
+@@ -2757,12 +2881,27 @@ static int finish_parity_scrub(struct btrfs_raid_bio *rbio)
+               pointers[rbio->real_stripes - 1] = kmap_local_paddr(q_paddr);
+       }
+ 
++      /*
++       * Rows with a read error were rebuilt by recover_scrub_rbio(): do not
++       * look for a bad data stripe there.
++       */
++      for_each_set_bit(sectornr, &rbio->dbitmap, rbio->stripe_nsectors)
++              if (get_rbio_vertical_errors(rbio, sectornr, NULL, NULL))
++                      __set_bit(sectornr, &read_errors);
++
+       bitmap_clear(rbio->error_bitmap, 0, rbio->nr_sectors);
+ 
+       /* Map the parity stripe just once */
+ 
+-      for_each_set_bit(sectornr, &rbio->dbitmap, rbio->stripe_nsectors)
+-              verify_one_parity_sector(rbio, pointers, sectornr);
++      for_each_set_bit(sectornr, &rbio->dbitmap, rbio->stripe_nsectors) {
++              const int stripe = verify_one_parity_sector(rbio, pointers, sectornr,
++                                              !test_bit(sectornr, &read_errors));
++
++              if (stripe >= 0) {
++                      __set_bit(sectornr, &rebuilt);
++                      rebuilt_stripe[sectornr] = stripe;
++              }
++      }
+ 
+       kunmap_local(pointers[nr_data]);
+       __free_page(phys_to_page(p_paddr));
+@@ -2776,6 +2915,26 @@ static int finish_parity_scrub(struct btrfs_raid_bio *rbio)
+       if (rbio->scrub_readonly)
+               goto submit_write;
+ 
++      /* Write the data sectors rebuilt from P and Q. */
++      for_each_set_bit(sectornr, &rebuilt, rbio->stripe_nsectors) {
++              const int stripe = rebuilt_stripe[sectornr];
++              const struct btrfs_io_stripe *io_stripe = &bioc->stripes[stripe];
++
++              btrfs_warn_rl(bioc->fs_info,
++"scrub: raid6 data at logical %llu on devid %llu physical %llu did not match P and Q, rebuilt it",
++                            bioc->full_stripe_logical +
++                            btrfs_stripe_nr_to_offset(stripe) +
++                            ((u64)sectornr << bioc->fs_info->sectorsize_bits),
++                            io_stripe->dev->devid,
++                            io_stripe->physical +
++                            ((u64)sectornr << bioc->fs_info->sectorsize_bits));
++              ret = rbio_add_io_paddrs(rbio, &bio_list,
++                                       rbio_stripe_paddrs(rbio, stripe, sectornr),
++                                       stripe, sectornr, REQ_OP_WRITE);
++              if (ret)
++                      goto cleanup;
++      }
++
+       /*
+        * time to start writing.  Make bios for everything from the
+        * higher layers (the bio_list in our rbio) and our p/q.  Ignore
+-- 
+2.47.3
+
diff --git a/topics/raid56-scrub-replace/0011-btrfs-scrub-count-raid56-parity-mismatches-as-corrup.patch b/topics/raid56-scrub-replace/0011-btrfs-scrub-count-raid56-parity-mismatches-as-corrup.patch
new file mode 100644 (file)
index 0000000..9bb9490
--- /dev/null
@@ -0,0 +1,171 @@
+From f6b508f255f92a45cbceff07ac7b080e98c49f7d Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Tue, 20 Oct 2026 16:15:58 -0400
+Subject: [PATCH 11/13] btrfs: scrub: count raid56 parity mismatches as
+ corruption errors
+To: linux-btrfs@vger.kernel.org
+
+When scrub finds raid56 parity that does not match the data, it rewrites
+the parity (or, on RAID6, rebuilds the data stripe that P and Q locate),
+but counts nothing: the device stats do not change, and the scrub
+progress, and so `btrfs scrub status`, says "no errors found".  A
+corrupted parity strip, a stale data strip, or a write hole is repaired
+or papered over without a trace.
+
+Count every sector that did not match as a corruption error
+(BTRFS_DEV_STAT_CORRUPTION_ERRS) of the device that held the bad copy,
+and add it to the scrub's corrected_errors:
+
+- a data stripe that P and Q located and that was rebuilt: its device;
+- otherwise the parity being scrubbed: its device.
+
+The second case is not always right.  Without a checksum on the data and
+without a second parity to locate the error (RAID5, or a RAID6 row that
+the decode cannot place), a mismatch may as well be a corrupt or stale
+data sector that nothing can detect; scrub rewrites the parity from that
+data, and the parity device is charged.  Rows whose stripes had a read
+error are not counted again: the read error was.
+
+The device is the one in the scrub rbio's io context at the stripe that
+held the bad copy.  During dev-replace the scrubbed parity is the
+source's (the target is only written), so the source is charged.
+
+Test (raid56-wh-harness raid56-devstats-test.sh, arm P): 4 sectors of a
+P strip (on raid6 also 4 of a Q strip, other rows) overwritten, then
+scrub.  Before: no counter moved and scrub said "no errors found".
+After: corruption_errs +4 on exactly the corrupted member(s), Corrected
+4 (raid6 8), and a second scrub finds nothing.
+nodatasum-scrub-repair-test.sh, raid6 with one stale member per row:
+every stale sector is charged once, to the member that held it.
+
+Assisted-by: Claude:claude-opus-5-5
+---
+ fs/btrfs/raid56.c | 33 ++++++++++++++++++++++++++++++++-
+ fs/btrfs/raid56.h |  5 ++++-
+ fs/btrfs/scrub.c  |  8 +++++++-
+ 3 files changed, 43 insertions(+), 3 deletions(-)
+
+diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
+index b3366434f0d90..4d5f61481226e 100644
+--- a/fs/btrfs/raid56.c
++++ b/fs/btrfs/raid56.c
+@@ -2559,7 +2559,7 @@ struct btrfs_raid_bio *raid56_parity_alloc_scrub_rbio(struct bio *bio,
+                               struct btrfs_io_context *bioc,
+                               struct btrfs_device *scrub_dev,
+                               unsigned long *dbitmap, int stripe_nsectors,
+-                              bool readonly)
++                              bool readonly, u32 *corrected)
+ {
+       struct btrfs_fs_info *fs_info = bioc->fs_info;
+       struct btrfs_raid_bio *rbio;
+@@ -2591,6 +2591,7 @@ struct btrfs_raid_bio *raid56_parity_alloc_scrub_rbio(struct bio *bio,
+ 
+       bitmap_copy(&rbio->dbitmap, dbitmap, stripe_nsectors);
+       rbio->scrub_readonly = readonly;
++      rbio->scrub_corrected = corrected;
+       return rbio;
+ }
+ 
+@@ -2818,6 +2819,35 @@ static int verify_one_parity_sector(struct btrfs_raid_bio *rbio,
+       return -1;
+ }
+ 
++/*
++ * Count each sector that did not match as a corruption error of the device
++ * that held it: the data stripe rebuilt from P and Q, or else the scrubbed
++ * parity, which is rewritten from the data.  Without a checksum and without
++ * a decode (RAID5, or a RAID6 row the decode cannot place), the mismatch may
++ * as well be corrupt data that nothing can find; the parity is charged.
++ * Rows with a read error are not counted here: the read error was.
++ */
++static void count_parity_scrub_errors(struct btrfs_raid_bio *rbio,
++                                    unsigned long read_errors,
++                                    unsigned long rebuilt,
++                                    const u8 *rebuilt_stripe)
++{
++      struct btrfs_io_context *bioc = rbio->bioc;
++      unsigned long parity_bad;
++      unsigned int sectornr;
++
++      bitmap_andnot(&parity_bad, &rbio->dbitmap, &read_errors, rbio->stripe_nsectors);
++      for_each_set_bit(sectornr, &rebuilt, rbio->stripe_nsectors)
++              btrfs_dev_stat_inc_and_print(bioc->stripes[rebuilt_stripe[sectornr]].dev,
++                                           BTRFS_DEV_STAT_CORRUPTION_ERRS);
++      for_each_set_bit(sectornr, &parity_bad, rbio->stripe_nsectors)
++              btrfs_dev_stat_inc_and_print(bioc->stripes[rbio->scrubp].dev,
++                                           BTRFS_DEV_STAT_CORRUPTION_ERRS);
++      if (rbio->scrub_corrected)
++              *rbio->scrub_corrected += bitmap_weight(&rebuilt, rbio->stripe_nsectors) +
++                                        bitmap_weight(&parity_bad, rbio->stripe_nsectors);
++}
++
+ static int finish_parity_scrub(struct btrfs_raid_bio *rbio)
+ {
+       struct btrfs_io_context *bioc = rbio->bioc;
+@@ -2902,6 +2932,7 @@ static int finish_parity_scrub(struct btrfs_raid_bio *rbio)
+                       rebuilt_stripe[sectornr] = stripe;
+               }
+       }
++      count_parity_scrub_errors(rbio, read_errors, rebuilt, rebuilt_stripe);
+ 
+       kunmap_local(pointers[nr_data]);
+       __free_page(phys_to_page(p_paddr));
+diff --git a/fs/btrfs/raid56.h b/fs/btrfs/raid56.h
+index 1f68218598072..dc125624fbf14 100644
+--- a/fs/btrfs/raid56.h
++++ b/fs/btrfs/raid56.h
+@@ -170,6 +170,9 @@ struct btrfs_raid_bio {
+       /* Stripe number that we're scrubbing  */
+       u8 scrubp;
+ 
++      /* Parity scrub: where to add the number of mismatching sectors. */
++      u32 *scrub_corrected;
++
+       /* Parity scrub of a read-only scrub: check, but write nothing. */
+       bool scrub_readonly;
+ 
+@@ -283,7 +286,7 @@ struct btrfs_raid_bio *raid56_parity_alloc_scrub_rbio(struct bio *bio,
+                               struct btrfs_io_context *bioc,
+                               struct btrfs_device *scrub_dev,
+                               unsigned long *dbitmap, int stripe_nsectors,
+-                              bool readonly);
++                              bool readonly, u32 *corrected);
+ void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio);
+ 
+ void raid56_parity_cache_data_folios(struct btrfs_raid_bio *rbio,
+diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
+index 690c8c9d76b5d..869e284aff461 100644
+--- a/fs/btrfs/scrub.c
++++ b/fs/btrfs/scrub.c
+@@ -2279,6 +2279,7 @@ static int scrub_raid56_cached_parity(struct scrub_ctx *sctx,
+       struct bio bio;
+       const int data_stripes = nr_data_stripes(map);
+       u64 length = btrfs_stripe_nr_to_offset(data_stripes);
++      u32 corrected = 0;
+       int ret;
+ 
+       bio_init(&bio, NULL, NULL, 0, REQ_OP_READ);
+@@ -2295,7 +2296,7 @@ static int scrub_raid56_cached_parity(struct scrub_ctx *sctx,
+       ASSERT(bioc);
+       rbio = raid56_parity_alloc_scrub_rbio(&bio, bioc, scrub_dev, extent_bitmap,
+                               BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits,
+-                              sctx->readonly);
++                              sctx->readonly, &corrected);
+       btrfs_put_bioc(bioc);
+       if (!rbio) {
+               ret = -ENOMEM;
+@@ -2311,6 +2312,11 @@ static int scrub_raid56_cached_parity(struct scrub_ctx *sctx,
+       raid56_parity_submit_scrub_rbio(rbio);
+       wait_for_completion_io(&io_done);
+       ret = blk_status_to_errno(bio.bi_status);
++      if (corrected) {
++              spin_lock(&sctx->stat_lock);
++              sctx->stat.corrected_errors += corrected;
++              spin_unlock(&sctx->stat_lock);
++      }
+ out:
+       btrfs_bio_counter_dec(fs_info);
+       bio_uninit(&bio);
+-- 
+2.47.3
+
diff --git a/topics/raid56-scrub-replace/0012-btrfs-scrub-name-the-device-a-raid56-data-sector-was.patch b/topics/raid56-scrub-replace/0012-btrfs-scrub-name-the-device-a-raid56-data-sector-was.patch
new file mode 100644 (file)
index 0000000..86c46e2
--- /dev/null
@@ -0,0 +1,82 @@
+From 302044132402f123134b510a46f366a1a1544135 Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Tue, 20 Oct 2026 22:15:15 -0400
+Subject: [PATCH 12/13] btrfs: scrub: name the device a raid56 data sector was
+ read from
+To: linux-btrfs@vger.kernel.org
+
+scrub_stripe_report_errors() finds the device and physical address for
+its messages by mapping the stripe's logical address with
+BTRFS_MAP_GET_READ_MIRRORS and taking stripes[mirror_num - 1].  On RAID56
+that mapping is the full stripe's write mapping, and stripes[0] is the
+device of the full stripe's first data stripe, not the device the sector
+was read from.  So "checksum error at logical ... on dev ...", "fixed up
+error ... on dev ..." and "unable to fixup (regular) error ... on dev ..."
+name a device that rotates over the members with the full stripes, and a
+physical address on it that holds something else.  The device stats were
+always charged to the right device (stripe->dev).
+
+The scrub stripe already records where it was read from: use
+stripe->dev and stripe->physical.  For the mirrored and striped profiles
+that is the same device and address as before.
+
+Test (raid56-wh-harness scrub-blame-test.sh): with damaged data sectors
+on one raid5/raid6 member, every per-sector line names that member, in
+every scrub order.  Before: 1 to 8 of up to 10 lines named another
+member, always the predicted stripes[0].
+
+Fixes: 0096580713ff ("btrfs: scrub: introduce error reporting functionality for scrub_stripe")
+Assisted-by: Claude:claude-opus-5-5
+---
+ fs/btrfs/scrub.c | 31 +++++++------------------------
+ 1 file changed, 7 insertions(+), 24 deletions(-)
+
+diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
+index 869e284aff461..d704fd0d64435 100644
+--- a/fs/btrfs/scrub.c
++++ b/fs/btrfs/scrub.c
+@@ -996,34 +996,17 @@ static void scrub_stripe_report_errors(struct scrub_ctx *sctx,
+               return;
+ 
+       /*
+-       * Init needed infos for error reporting.
+-       *
+-       * Although our scrub_stripe infrastructure is mostly based on btrfs_submit_bio()
+-       * thus no need for dev/physical, error reporting still needs dev and physical.
++       * The sectors were read from @stripe->dev at @stripe->physical.  Mapping
++       * @stripe->logical back with BTRFS_MAP_GET_READ_MIRRORS does not give that
++       * on RAID56: it returns the full stripe's write mapping, whose
++       * stripes[mirror_num - 1] is the device of the full stripe's first data
++       * stripe.
+        */
+       if (!bitmap_empty(&errors->init_error_bitmap, stripe->nr_sectors)) {
+-              u64 mapped_len = fs_info->sectorsize;
+-              struct btrfs_io_context *bioc = NULL;
+-              int stripe_index = stripe->mirror_num - 1;
+-              int ret;
+-
+-              /* For scrub, our mirror_num should always start at 1. */
+-              ASSERT(stripe->mirror_num >= 1, "stripe->mirror_num=%d", stripe->mirror_num);
+-              ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
+-                                    stripe->logical, &mapped_len, &bioc,
+-                                    NULL, NULL);
+-              /*
+-               * If we failed, dev will be NULL, and later detailed reports
+-               * will just be skipped.
+-               */
+-              if (ret < 0)
+-                      goto skip;
+-              physical = bioc->stripes[stripe_index].physical;
+-              dev = bioc->stripes[stripe_index].dev;
+-              btrfs_put_bioc(bioc);
++              dev = stripe->dev;
++              physical = stripe->physical;
+       }
+ 
+-skip:
+       for_each_set_bit(sector_nr, &extent_bitmap, stripe->nr_sectors) {
+               const u64 sector_logical = stripe->logical +
+                                          ((u64)sector_nr << fs_info->sectorsize_bits);
+-- 
+2.47.3
+
diff --git a/topics/raid56-scrub-replace/0013-btrfs-scrub-count-the-raid56-data-repairs-made-by-a-.patch b/topics/raid56-scrub-replace/0013-btrfs-scrub-count-the-raid56-data-repairs-made-by-a-.patch
new file mode 100644 (file)
index 0000000..a4e0af7
--- /dev/null
@@ -0,0 +1,228 @@
+From 9970d7e44abf0e5a7c67c93228908af5a0303f86 Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Tue, 20 Oct 2026 22:15:16 -0400
+Subject: [PATCH 13/13] btrfs: scrub: count the raid56 data repairs made by a
+ parity scrub
+To: linux-btrfs@vger.kernel.org
+
+The scrub of a RAID56 P/Q device reads every data stripe of each full
+stripe it checks, verifies and repairs them, and writes the repairs back,
+but counts nothing (SCRUB_STRIPE_FLAG_NO_REPORT): neither the scrub
+progress nor the device stats.  When the P/Q device's scrub reaches a
+full stripe before the data device's own scrub does, the data device's
+scrub finds the sector already repaired, and the error is counted
+nowhere.  With all devices scrubbed at once that is a large share of the
+errors (one failing raid5 member: 25831 of 42371 damaged data sectors
+counted; raid6: 7894 of 34146), and when the damaged device is scrubbed
+last, none of them.
+
+Count the sectors that the parity scrub repaired and wrote back, as the
+data device's scrub would have: read, checksum and verify errors in the
+device stats of the data stripe's own device, and in the parity scrub's
+progress with corrected_errors.  To count each bad sector once:
+
+- errors left unrepaired are not counted: they stay on disk for the data
+  device's own scrub;
+- a read-only scrub counts nothing here: nothing is written back, and the
+  data device's own scrub will see the same errors;
+- when the data device is being scrubbed at the same time (its
+  scrub_ctx is set), that scrub repairs and counts its errors itself, so
+  the parity scrub neither writes back nor counts them.  It still uses
+  the repaired data for its parity check.  Crediting the data device's
+  scrub from here instead would count a sector twice whenever both scrubs
+  read it before either wrote the repair back, which is common when all
+  devices are scrubbed at once, since they move through the full stripes
+  together.
+
+A missing data device is not counted against.
+
+Test (raid56-wh-harness scrub-blame-test.sh): 8 damaged data sectors (or
+whole damaged strips: 288 raid5, 224 raid6) on one member, all devices
+scrubbed at once, the damaged one first, or last: every damaged sector
+counted once in every order, the device stats charged to that member
+only; read-only scrubs too.  Before: 7 of 8 (raid6 2 of 8) at once, 0
+with the damaged member last.  failing-member-scrub-test.sh, one member
+failing under load: raid5 36128 of 36128, raid6 30231 of 30231 damaged
+sectors counted; before, 26519 of 40556 and 11049 of 30723.
+
+Fixes: 1009254bf22a ("btrfs: scrub: use scrub_stripe to implement RAID56 P/Q scrub")
+Assisted-by: Claude:claude-opus-5-5
+---
+ fs/btrfs/scrub.c | 109 +++++++++++++++++++++++++++++++++++++++++++++--
+ 1 file changed, 105 insertions(+), 4 deletions(-)
+
+diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
+index d704fd0d64435..5cf7830c63a4b 100644
+--- a/fs/btrfs/scrub.c
++++ b/fs/btrfs/scrub.c
+@@ -85,10 +85,17 @@ enum scrub_stripe_flags {
+ 
+       /*
+        * Set for data stripes if it's triggered from P/Q stripe.
+-       * During such scrub, we should not report errors in data stripes, nor
+-       * update the accounting.
++       * During such scrub, only the errors repaired and written back are
++       * reported and counted, see scrub_report_parity_repairs().
+        */
+       SCRUB_STRIPE_FLAG_NO_REPORT,
++
++      /*
++       * Set for such a data stripe whose own device is being scrubbed at the
++       * same time: that scrub repairs and counts its errors, so they are not
++       * written back or counted here.
++       */
++      SCRUB_STRIPE_FLAG_OWN_SCRUB,
+ };
+ 
+ /*
+@@ -326,6 +333,12 @@ struct scrub_error_records {
+       unsigned int nr_csum_errors;
+       unsigned int nr_meta_errors;
+       unsigned int nr_meta_gen_errors;
++
++      /* The initial errors per type, for scrub_report_parity_repairs(). */
++      unsigned long init_io_error_bitmap;
++      unsigned long init_csum_error_bitmap;
++      unsigned long init_meta_error_bitmap;
++      unsigned long init_meta_gen_error_bitmap;
+ };
+ 
+ static void release_scrub_stripe(struct scrub_stripe *stripe)
+@@ -975,6 +988,73 @@ static void scrub_stripe_submit_repair_read(struct scrub_stripe *stripe,
+       }
+ }
+ 
++/*
++ * A RAID56 P/Q scrub reads, verifies and repairs the data stripes of each full
++ * stripe it checks, and writes the repairs back, so the data device's own
++ * scrub, if it gets there later, finds nothing.  Count here what was repaired
++ * and written back, as that scrub would have: in the device stats of the data
++ * stripe's device and in the progress of this scrub.
++ *
++ * Nothing else is counted here.  Errors left unrepaired, and everything in a
++ * read-only scrub, stay on disk for the data device's own scrub to find; and a
++ * data device that is being scrubbed at the same time repairs and counts its
++ * own errors (SCRUB_STRIPE_FLAG_OWN_SCRUB: nothing is written back here).  So
++ * each bad sector is counted once.
++ */
++static void scrub_report_parity_repairs(struct scrub_ctx *sctx,
++                                      struct scrub_stripe *stripe,
++                                      const struct scrub_error_records *errors)
++{
++      struct btrfs_fs_info *fs_info = sctx->fs_info;
++      const unsigned long error_bitmap = scrub_bitmap_read_error(stripe);
++      const unsigned int nr_sectors = stripe->nr_sectors;
++      unsigned long repaired;
++      unsigned int nr_io_errors;
++      unsigned int nr_csum_errors;
++      unsigned int nr_meta_errors;
++      unsigned int nr_meta_gen_errors;
++      int sector_nr;
++
++      if (sctx->readonly || test_bit(SCRUB_STRIPE_FLAG_OWN_SCRUB, &stripe->state) ||
++          !stripe->dev->bdev)
++              return;
++      bitmap_andnot(&repaired, &errors->init_error_bitmap, &error_bitmap, nr_sectors);
++      if (bitmap_empty(&repaired, nr_sectors))
++              return;
++
++      nr_io_errors = bitmap_weight_and(&repaired, &errors->init_io_error_bitmap,
++                                       nr_sectors);
++      nr_csum_errors = bitmap_weight_and(&repaired, &errors->init_csum_error_bitmap,
++                                         nr_sectors);
++      nr_meta_errors = bitmap_weight_and(&repaired, &errors->init_meta_error_bitmap,
++                                         nr_sectors);
++      nr_meta_gen_errors = bitmap_weight_and(&repaired,
++                                             &errors->init_meta_gen_error_bitmap,
++                                             nr_sectors);
++
++      for_each_set_bit(sector_nr, &repaired, nr_sectors)
++              btrfs_err_rl(fs_info,
++              "scrub: fixed up error at logical %llu on dev %s physical %llu",
++                           stripe->logical + ((u64)sector_nr << fs_info->sectorsize_bits),
++                           btrfs_dev_name(stripe->dev),
++                           stripe->physical + ((u64)sector_nr << fs_info->sectorsize_bits));
++
++      for (int i = 0; i < nr_io_errors; i++)
++              btrfs_dev_stat_inc_and_print(stripe->dev, BTRFS_DEV_STAT_READ_ERRS);
++      for (int i = 0; i < nr_csum_errors; i++)
++              btrfs_dev_stat_inc_and_print(stripe->dev, BTRFS_DEV_STAT_CORRUPTION_ERRS);
++      for (int i = 0; i < nr_meta_gen_errors;
++           i += (fs_info->nodesize >> fs_info->sectorsize_bits))
++              btrfs_dev_stat_inc_and_print(stripe->dev, BTRFS_DEV_STAT_GENERATION_ERRS);
++
++      spin_lock(&sctx->stat_lock);
++      sctx->stat.read_errors += nr_io_errors;
++      sctx->stat.csum_errors += nr_csum_errors;
++      sctx->stat.verify_errors += nr_meta_errors + nr_meta_gen_errors;
++      sctx->stat.corrected_errors += bitmap_weight(&repaired, nr_sectors);
++      spin_unlock(&sctx->stat_lock);
++}
++
+ static void scrub_stripe_report_errors(struct scrub_ctx *sctx,
+                                      struct scrub_stripe *stripe,
+                                      const struct scrub_error_records *errors)
+@@ -992,8 +1072,10 @@ static void scrub_stripe_report_errors(struct scrub_ctx *sctx,
+       int nr_repaired_sectors = 0;
+       int sector_nr;
+ 
+-      if (test_bit(SCRUB_STRIPE_FLAG_NO_REPORT, &stripe->state))
++      if (test_bit(SCRUB_STRIPE_FLAG_NO_REPORT, &stripe->state)) {
++              scrub_report_parity_repairs(sctx, stripe, errors);
+               return;
++      }
+ 
+       /*
+        * The sectors were read from @stripe->dev at @stripe->physical.  Mapping
+@@ -1145,6 +1227,10 @@ static void scrub_stripe_read_repair_worker(struct work_struct *work)
+       errors.nr_csum_errors = scrub_bitmap_weight_csum_error(stripe);
+       errors.nr_meta_errors = scrub_bitmap_weight_meta_error(stripe);
+       errors.nr_meta_gen_errors = scrub_bitmap_weight_meta_gen_error(stripe);
++      errors.init_io_error_bitmap = scrub_bitmap_read_io_error(stripe);
++      errors.init_csum_error_bitmap = scrub_bitmap_read_csum_error(stripe);
++      errors.init_meta_error_bitmap = scrub_bitmap_read_meta_error(stripe);
++      errors.init_meta_gen_error_bitmap = scrub_bitmap_read_meta_gen_error(stripe);
+ 
+       if (bitmap_empty(&errors.init_error_bitmap, stripe->nr_sectors))
+               goto out;
+@@ -1199,7 +1285,8 @@ static void scrub_stripe_read_repair_worker(struct work_struct *work)
+        */
+       bitmap_andnot(&repaired, &errors.init_error_bitmap, &error,
+                     stripe->nr_sectors);
+-      if (!sctx->readonly && !bitmap_empty(&repaired, stripe->nr_sectors)) {
++      if (!sctx->readonly && !bitmap_empty(&repaired, stripe->nr_sectors) &&
++          !test_bit(SCRUB_STRIPE_FLAG_OWN_SCRUB, &stripe->state)) {
+               if (btrfs_is_zoned(fs_info)) {
+                       btrfs_repair_one_zone(fs_info, sctx->stripes[0].bg->start);
+               } else {
+@@ -2364,6 +2451,18 @@ static void scrub_raid56_copy_parity_rows(struct scrub_ctx *sctx,
+       kfree(buf);
+ }
+ 
++/* Is @dev being scrubbed right now, by a scrub other than @sctx? */
++static bool scrubbed_by_other(struct scrub_ctx *sctx, struct btrfs_device *dev)
++{
++      struct btrfs_fs_info *fs_info = sctx->fs_info;
++      bool ret;
++
++      mutex_lock(&fs_info->scrub_lock);
++      ret = dev->scrub_ctx && dev->scrub_ctx != sctx;
++      mutex_unlock(&fs_info->scrub_lock);
++      return ret;
++}
++
+ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
+                                     struct btrfs_device *scrub_dev,
+                                     struct btrfs_block_group *bg,
+@@ -2421,6 +2520,8 @@ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
+ 
+               scrub_reset_stripe(stripe);
+               set_bit(SCRUB_STRIPE_FLAG_NO_REPORT, &stripe->state);
++              if (scrubbed_by_other(sctx, map->stripes[stripe_index].dev))
++                      set_bit(SCRUB_STRIPE_FLAG_OWN_SCRUB, &stripe->state);
+               ret = scrub_find_fill_first_stripe(bg, &extent_path, &csum_path,
+                               map->stripes[stripe_index].dev, physical, 1,
+                               full_stripe_start + btrfs_stripe_nr_to_offset(i),
+-- 
+2.47.3
+