]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: pad a stripe's allocated sectors whose write is still to come
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Fri, 2 Oct 2026 06:41:44 +0000 (02:41 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:37:53 +0000 (18:37 -0500)
A stripe run's first partial write into a stripe cannot pad today: the
stripe's other sectors are allocated (live) to writes that are still in
flight, so the pad oracle calls them live and the write goes down as a
read-modify-write of the whole stripe.  With the stripe pin keeping the
stripe cached for every later write, this first write is the only data RMW
left in a stripe run (traced on the strip-fault fixture: every remaining
data RMW read was a first write), and the read it does is of sectors
nobody has written: it can only fetch garbage into the parity, and on a
device that returns wrong data it makes that parity disagree with what is
on the disk.

Use the run's written map: a live sector whose write has not completed
holds nothing anyone has written, so it is as paddable as a dead one.
Zeroing it is safe because its write is ordered after this one -- the
padding write holds the stripe lock, every raid56 write to the stripe
takes it, and rbio_orig_end_io() records a write's sectors in the written
map before it unlocks the stripe -- and nothing reads the sector before
its own write lands, since its extent is not committed until then.  That
write replaces the zeros; it is a sub-stripe write into a stripe written
before, which the stripe pin serves from the cache.

A live sector that HAS been written still refuses the pad, as before:
that is now the precise meaning of the UNCOVERED decline.  Nodatacow runs
keep no written map (their extents are rewritten in place for as long as
the inode lives) and are excluded.  padded_unwritten in stripe_park_stats
counts the pads that zeroed such sectors.

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/block-group.c
fs/btrfs/block-group.h
fs/btrfs/fs.h
fs/btrfs/raid56.c
fs/btrfs/sysfs.c

index d85b1ec0e0533ffb119948148f25658f5e0bdc11..bd9209572479ad9016c832278024cd6b0515acf2 100644 (file)
@@ -1223,28 +1223,28 @@ u64 btrfs_stripe_run_alloc_ceiling(struct btrfs_fs_info *fs_info,
 /*
  * Which sectors of this stripe may be padded?
  *
- * Fills @dead with one bit per sector of the stripe, set where the sector
- * holds nothing anyone intends to write: space the run has not handed out
- * yet, and space it handed out but got back before the write was issued.
- * A caller may zero-fill exactly those and write the whole stripe.
+ * Fills @dead with one bit per sector, set where the sector holds nothing
+ * anyone has written: never handed out, handed out and abandoned, or
+ * handed out with the write not yet completed (per the run's written map).
+ * The caller may zero-fill exactly those and write the whole stripe.
+ * @unwritten, when non-NULL, gets the subset that is allocated but
+ * unwritten.
  *
- * Returns false if no run covers the stripe, in which case nothing may be
- * padded: without a run there is no claim of exclusivity, and an uncovered
- * sector may hold committed data.
+ * Zeroing an allocated sector whose write is still to come is safe: the
+ * caller holds the stripe lock, which every raid56 write to the stripe
+ * takes, so that write lands after the caller's; completed writes are in
+ * the written map before the stripe is unlocked (rbio_orig_end_io()); and
+ * nothing reads the sector before its own write lands, since its extent is
+ * not committed until then.  A NOCOW run's written map is not consulted:
+ * its live sectors are rewritten in place, so they may already hold data.
  *
- * This replaces asking where the run's frontier is.  The frontier answers
- * "what has never been allocated", which misses allocations that were
- * abandoned below it -- a tree block COWed twice in one transaction, a
- * reservation released, an allocation the finder discarded.  Those leave
- * holes that are equally safe to pad, and skipping them is the difference
- * between padding most partial stripes and padding all of them.
- *
- * @info, when non-NULL, reports the covering run's state at the moment of
- * the decision, for the pad-decline trace: a later extent-tree dump cannot
- * reconstruct where the frontier was when the write arrived.
+ * Returns false if no run covers the stripe; nothing may be padded then,
+ * since an uncovered sector may hold committed data.  @info, when non-NULL,
+ * records the covering run's state for the pad-decline trace.
  */
 bool btrfs_stripe_run_pad_mask(struct btrfs_fs_info *fs_info, u64 stripe_start,
                               u64 stripe_len, unsigned long *dead,
+                              unsigned long *unwritten,
                               struct btrfs_stripe_pad_info *info)
 {
        const u32 bits = fs_info->sectorsize_bits;
@@ -1306,9 +1306,18 @@ bool btrfs_stripe_run_pad_mask(struct btrfs_fs_info *fs_info, u64 stripe_start,
                if (first + nsectors > run->live_nbits)
                        break;
                bitmap_zero(dead, nsectors);
-               for (u32 i = 0; i < nsectors; i++)
-                       if (!test_bit(first + i, run->live))
+               if (unwritten)
+                       bitmap_zero(unwritten, nsectors);
+               for (u32 i = 0; i < nsectors; i++) {
+                       if (!test_bit(first + i, run->live)) {
+                               set_bit(i, dead);
+                       } else if (run->written &&
+                                  !test_bit(first + i, run->written)) {
                                set_bit(i, dead);
+                               if (unwritten)
+                                       set_bit(i, unwritten);
+                       }
+               }
                if (info)
                        info->reason = BTRFS_STRIPE_PAD_OK;
                ret = true;
index 1ac9a8e6667e75918b1ec9b35b6c6280f3f6b689..5aa929a0d56fb12a2c8da1e1bf5faf565f951c6a 100644 (file)
@@ -492,6 +492,7 @@ u64 btrfs_stripe_run_alloc_ceiling(struct btrfs_fs_info *fs_info,
                                   u64 stripe_start, u64 stripe_len);
 bool btrfs_stripe_run_pad_mask(struct btrfs_fs_info *fs_info, u64 stripe_start,
                               u64 stripe_len, unsigned long *dead,
+                              unsigned long *unwritten,
                               struct btrfs_stripe_pad_info *info);
 int btrfs_stripe_alloc_check_support(struct btrfs_fs_info *fs_info);
 int btrfs_enable_stripe_alloc(struct btrfs_fs_info *fs_info);
index 665881ef873a8f877bf9af3587ebdfcdde6d5414..e32819ade071a0f0d188207b474274a1f72ded19 100644 (file)
@@ -833,6 +833,8 @@ struct btrfs_fs_info {
                atomic64_t stuck;
                atomic64_t rmw_reads;
                atomic64_t padded;
+               /* Pads that zeroed allocated sectors whose write was to come. */
+               atomic64_t padded_unwritten;
                /*
                 * Data read-modify-writes in a covered block group, parked or
                 * not; zero with allow_rmw empty.  rmw_reads counts only the
index 662b4017cc62968edb53753d8725baf025e3fd8e..5f56d1e19e749fdaa4665687ad7b5b4ac6de10f8 100644 (file)
@@ -2959,7 +2959,9 @@ static bool rmw_try_pad_full(struct btrfs_raid_bio *rbio)
        const u32 nsectors = rbio->nr_data * rbio->stripe_nsectors;
        struct btrfs_stripe_pad_info info;
        unsigned long *dead;
+       unsigned long *unwritten;
        u32 covered = 0;
+       u32 nr_unwritten = 0;
        bool ok = false;
        int i;
 
@@ -2967,10 +2969,14 @@ static bool rmw_try_pad_full(struct btrfs_raid_bio *rbio)
                return false;
 
        dead = bitmap_zalloc(nsectors, GFP_NOFS);
-       if (!dead)
+       unwritten = bitmap_zalloc(nsectors, GFP_NOFS);
+       if (!dead || !unwritten) {
+               bitmap_free(dead);
+               bitmap_free(unwritten);
                return false;
+       }
        if (!btrfs_stripe_run_pad_mask(fs_info, stripe_start, stripe_len, dead,
-                                      &info)) {
+                                      unwritten, &info)) {
                trace_btrfs_stripe_pad_decline(fs_info, stripe_start,
                                               stripe_len, 0, 0, nsectors,
                                               info.run_start, info.run_end,
@@ -2988,11 +2994,10 @@ static bool rmw_try_pad_full(struct btrfs_raid_bio *rbio)
                        covered++;
 
        /*
-        * Every data sector this rbio does not cover must be dead.  A live
-        * uncovered sector belongs to another writer's allocation and this
-        * write does not carry its data, so padding would destroy it: that
-        * is a distinct refusal from the oracle's, and it is the one that
-        * says another writer's allocation shares this stripe.
+        * Every uncovered sector must be dead per the run.  A live uncovered
+        * sector that was already written holds data this write does not
+        * carry, so refuse (UNCOVERED); the stripe pin (rbio_needs_pin())
+        * normally supplies such sectors from the cache, so nothing is read.
         */
        for (i = 0; i < nsectors; i++) {
                if (rbio->bio_paddrs[i * rbio->sector_nsteps] != INVALID_PADDR)
@@ -3010,10 +3015,13 @@ static bool rmw_try_pad_full(struct btrfs_raid_bio *rbio)
                                                       info.reason);
                        goto out;
                }
+               if (test_bit(i, unwritten))
+                       nr_unwritten++;
        }
        ok = true;
 out:
        bitmap_free(dead);
+       bitmap_free(unwritten);
        if (!ok)
                return false;
 
@@ -3048,6 +3056,8 @@ out:
        bitmap_set(&rbio->dbitmap, 0, rbio->stripe_nsectors);
        spin_unlock(&rbio->bio_list_lock);
        atomic64_inc(&fs_info->stripe_park_stats.padded);
+       if (nr_unwritten)
+               atomic64_inc(&fs_info->stripe_park_stats.padded_unwritten);
        return true;
 }
 
index 4d9d728ec956d592114342f16c8714ab234ccd91..102abae2fe1595abfe570a960b80ee221c0749dd 100644 (file)
@@ -1245,6 +1245,7 @@ static ssize_t btrfs_stripe_park_stats_show(struct kobject *kobj,
                "expired %lld\n"
                "stuck %lld\n"
                "padded %lld\n"
+               "padded_unwritten %lld\n"
                "rmw_reads %lld\n",
                atomic64_read(&fs_info->stripe_park_stats.parked),
                atomic64_read(&fs_info->stripe_park_stats.merged_bytes),
@@ -1254,6 +1255,7 @@ static ssize_t btrfs_stripe_park_stats_show(struct kobject *kobj,
                atomic64_read(&fs_info->stripe_park_stats.expired),
                atomic64_read(&fs_info->stripe_park_stats.stuck),
                atomic64_read(&fs_info->stripe_park_stats.padded),
+               atomic64_read(&fs_info->stripe_park_stats.padded_unwritten),
                atomic64_read(&fs_info->stripe_park_stats.rmw_reads));
 }
 BTRFS_ATTR(, stripe_park_stats, btrfs_stripe_park_stats_show);