* Re: [PATCH 2/2] btrfs: scrub: report read-only and failed write correctly
2026-11-28 22:46 ` [PATCH 2/2] btrfs: scrub: report read-only and failed write correctly Qu Wenruo
@ 2026-12-03 7:08 ` Zygo Blaxell
0 siblings, 0 replies; 4+ messages in thread
From: Zygo Blaxell @ 2026-12-03 7:08 UTC (permalink / raw)
To: Qu Wenruo; +Cc: linux-btrfs, Wesley Atwell
On Sun, Nov 29, 2026 at 09:16:07AM +1030, Qu Wenruo wrote:
> As long as there is a good copy, scrub will report a corrupted block as
> repaired. But this can be wrong in the following cases:
>
> - Read-only scrub
> The corruption is repairable but no write is submitted, so the
> corruption is still there.
>
> - Write failure
> Write is submitted but failed, so the corruption is still there.
>
> Fix the mismatch and strictly follow the UABI by:
>
> - Add is_sector_corrected() helper to report the repairbility
> This also changes the error message, now we put the repairbility
> report at the end.
>
> This will affect scripts parsing the kernel dmesg for scrub errors,
> but those messages can be rate limited in the first place, so it's not
> a reliable tool for monitoring.
>
> Please note that, for zoned btrfs the corrupted block will be reported
> as "fixed up", as long as there is a good copy, no matter if the
> relocation succeeded or not.
> This is the existing behavior, this patch is not making it worse or
> better.
>
> - Treat read-only and failed write as uncorrectable errors
> The UABI only mentioned write failure. So read-only scrub behavior is
> undefined.
It's defined in include/uapi/linux/btrfs.h as:
__u64 uncorrectable_errors; /* # of errors where either no intact
* copy was found or the writeback
* failed */
__u64 corrected_errors; /* # of errors corrected */
uncorrectable_errors clearly excludes errors where any intact copy
is found. "Writeback failed" can't occur on a read-only scrub since
there is no writeback attempted.
An argument could be made that corrected_errors should always be exactly
zero on a read-only scrub according to its definition, but that would
give read-only scrub nowhere to report correctable errors at all.
That's much less useful than the current behavior.
Instead, I'd prefer to define corrected_errors as the complement of
uncorrectable_errors, which already says what it excludes: an error is
corrected if an intact copy was found and no writeback of it failed.
On a read-write scrub that means it was repaired. On a read-only
scrub, where no writeback is attempted, it means it could have been.
That keeps the read-only behavior from 6.4, and it agrees with the half
of this patch that counts a failed repair write as uncorrectable.
> Here I treat read-only the same as write failure.
As a user, that's really unexpected. With this change, if I have a
device with correctable errors, a read-only scrub will tell me they are
all uncorrectable, which doesn't reflect reality at all.
If my device management workflow has used read-only scrub since 6.4,
then after this change I might abandon a degraded but still healthy
filesystem, believing its data to be permanently lost, because I didn't
know about the change in uncorrectable_errors meaning.
To me, "uncorrectable" means "correction is not possible," not "correction
was possible, but we have chosen not to do it." The kernel has behaved
this way since the 6.4 scrub rewrite. e02ee89baa66 ("btrfs: scrub:
switch scrub_simple_mirror() to scrub_stripe infrastructure") started
counting corrected and uncorrectable errors in read-only scrub, and
1f2030ff6e49 ("btrfs: scrub: respect the read-only flag during repair")
kept reporting repairable sectors as corrected on a read-only scrub,
noting the compromise. That behavior has been much more useful in
practice.
I expect that a read-only scrub would report the same number of
errors that could or could not have been corrected as if I had done a
read-write scrub (assuming the device would accept all repair writes,
which a read-only scrub has no business trying to predict).
Knowing that I had requested a read-only scrub, I would know that the
counts are advisory, and are a prediction of what is possible, instead
of a report of work actually done.
> The current limit is that btrfs_scrub_progress has no way to tell if a
> block can be repairable but not repaired.
>
> In the long run we should expand btrfs_scrub_progress, but that will be
> a UABI change, and require a dedicated series.
If the UAPI is redesigned, then we can get these counters separated,
and we can have unambiguous counts of attempted and successful reads,
and possible and completed repair writes. Four fields, no mode-specific
meaning.
Until then, the historical meanings of the legacy counters should not
change too drastically, especially by removing already useful behavior.
A UAPI redesign would also be a chance to fix the units of
data_bytes_scrubbed and tree_bytes_scrubbed. They count only sectors
that hold extents on the device being scrubbed, without RAID56 parity,
while the only per-device size userspace can get is the space allocated
to chunks, including their free space and parity. Userspace can't
calculate a correct percentage done for a running scrub. A counter of
bytes read from the device, or a total to scrub reported by the kernel,
would fix that.
> Reported-by: Wesley Atwell <atwellwea@gmail.com>
> Link: https://lore.kernel.org/linux-btrfs/20261128-btrfs-scrub-write-error-revalidated-v1-1-be9f21eede17@gmail.com/
> Signed-off-by: Qu Wenruo <wqu@suse.com>
> ---
> fs/btrfs/scrub.c | 60 +++++++++++++++++++++++++++++++++++-------------
> 1 file changed, 44 insertions(+), 16 deletions(-)
>
> diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
> index 9ce22c069a96..5c633d7c8e1e 100644
> --- a/fs/btrfs/scrub.c
> +++ b/fs/btrfs/scrub.c
> @@ -1014,6 +1014,23 @@ static void scrub_stripe_submit_repair_read(struct scrub_stripe *stripe,
> scrub_stripe_put_io(stripe);
> }
>
> +static bool is_sector_corrected(const struct scrub_ctx *sctx,
> + const struct scrub_stripe *stripe,
> + unsigned int sector_nr,
> + const char **desc_ret)
> +{
> + if (sctx->readonly) {
> + *desc_ret = "repairable but read-only scrub";
> + return false;
> + }
> + if (test_bit(sector_nr, &stripe->write_error_bitmap)) {
> + *desc_ret = "repairable but write failed";
> + return false;
> + }
> + *desc_ret = "fixed up";
> + return true;
> +}
> +
> static void scrub_stripe_report_errors(struct scrub_ctx *sctx,
> struct scrub_stripe *stripe,
> const struct scrub_error_records *errors)
> @@ -1023,7 +1040,7 @@ static void scrub_stripe_report_errors(struct scrub_ctx *sctx,
> struct btrfs_fs_info *fs_info = sctx->fs_info;
> struct btrfs_device *dev = NULL;
> const unsigned long extent_bitmap = scrub_bitmap_read_has_extent(stripe);
> - const unsigned long error_bitmap = scrub_bitmap_read_error(stripe);
> + unsigned long error_bitmap = scrub_bitmap_read_error(stripe);
> u64 physical = 0;
> int nr_data_sectors = 0;
> int nr_meta_sectors = 0;
> @@ -1068,7 +1085,7 @@ static void scrub_stripe_report_errors(struct scrub_ctx *sctx,
> ((u64)sector_nr << fs_info->sectorsize_bits);
> const u64 sector_physical = physical +
> ((u64)sector_nr << fs_info->sectorsize_bits);
> - bool repaired = false;
> + bool has_good_copy = false;
>
> if (scrub_bitmap_test_bit_is_metadata(stripe, sector_nr)) {
> nr_meta_sectors++;
> @@ -1079,30 +1096,41 @@ static void scrub_stripe_report_errors(struct scrub_ctx *sctx,
> }
>
> if (test_bit(sector_nr, &errors->init_error_bitmap) &&
> - !test_bit(sector_nr, &error_bitmap)) {
> - nr_repaired_sectors++;
> - repaired = true;
> - }
> + !test_bit(sector_nr, &error_bitmap))
> + has_good_copy = true;
>
> /* Good sector from the beginning, nothing need to be done. */
> if (!test_bit(sector_nr, &errors->init_error_bitmap))
> continue;
>
> /*
> - * Report error for the corrupted sectors. If repaired, just
> - * output the message of repaired message.
> + * Report error for the corrupted sectors. If repairable, just
> + * output the message.
> */
> - if (repaired) {
> - if (dev) {
> + if (has_good_copy) {
> + const char *desc;
> + const bool corrected = is_sector_corrected(sctx, stripe,
> + sector_nr, &desc);
> +
> + if (dev)
> btrfs_err_rl(fs_info,
> - "scrub: fixed up error at logical %llu on dev %s physical %llu",
Do we _need_ to change this? I have regression tests and monitoring
scripts that look for this message, and I suspect I'm not the only one.
Rate limiting doesn't matter, as a single instance of this message starts
a triage response.
Please consider appending to the end of the message to indicate failed
writes or read-only scrub.
> + "scrub: error at logical %llu on dev %s physical %llu, %s",
> sector_logical, btrfs_dev_name(dev),
> - sector_physical);
> - } else {
> + sector_physical, desc);
> + else
> btrfs_err_rl(fs_info,
> - "scrub: fixed up error at logical %llu on mirror %u",
> - sector_logical, stripe->mirror_num);
> - }
> + "scrub: error at logical %llu on mirror %u, %s",
> + sector_logical, stripe->mirror_num,
> + desc);
> +
> + /*
> + * If not repaired, the block should be reported as
> + * uncorrectable.
> + */
> + if (!corrected)
> + set_bit(sector_nr, &error_bitmap);
> + else
> + nr_repaired_sectors++;
> continue;
> }
>
> --
> 2.56.1
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread