]> git.hungrycats.org Git - linux/commitdiff
btrfs: raid56: pad sub-stripe writes to full stripes in open runs
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 19 Sep 2026 17:17:38 +0000 (13:17 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:37:44 +0000 (18:37 -0500)
A sub-stripe write does a full RMW: read every untouched data sector of
the stripe, recompute parity, write.  Under stripe-exclusive allocation
the read phase is usually pointless: a stripe covered by a live stripe
run has never been written at or past the run's allocation frontier, so
the sectors being read contain nothing.

When every data sector the rbio does not cover lies at or past the
frontier, zero-fill those sectors instead of reading them and write
them out with the stripe -- the zeros must reach the disk, or the
parity (computed over them) would not match what scrub reads back.  The
partial write becomes one full-stripe write: no read phase, one parity
pass.  A frontier that grows during the attempt is safe: the newer
allocation's write serializes behind this rbio's stripe lock and lands
over the zeros.  If any uncovered sector is below the frontier (already
allocated to someone else), fall back to the normal RMW.

Together with kicking parked rbios before the fast fsync's writeback
wait, this removes both stalls the raid56 layer added to fsync under
stripe_alloc: the park deadline and the RMW read round trip.

The pad sets the rbio's data bitmap under bio_list_lock: until
RBIO_RMW_LOCKED_BIT is set another writer can merge into the rbio and OR
its bits in under that lock, and a lost pad bit would leave rows out of
the write while the cached stripe marks their zero-filled pages up to date.

[zygo: 7.3 port: 7.3 has no per-chunk stripe length (map->stripe_len,
bioc->stripe_len, the two-argument btrfs_stripe_nr_to_offset()):
BTRFS_STRIPE_LEN is used instead; the change itself is unchanged]

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

index 88075d1902877c4b04d577551ef1aa10bd986e3b..7532a2d99f9850e825dab47fc9c8277da4e656eb 100644 (file)
@@ -909,6 +909,49 @@ bool btrfs_stripe_run_range_usable(struct btrfs_block_group *bg,
        return true;
 }
 
+/*
+ * For the raid56 pad-to-full optimization: a sub-stripe write into a
+ * stripe covered by a live stripe run may zero-fill (instead of read) any
+ * sector at or past the run's allocation frontier, because such sectors
+ * have never been written in the run's lifetime and, once the run closes,
+ * its stripe's tail cannot be reallocated while the stripe holds live
+ * data.  Returns true and sets *pad_from to the frontier when the stripe
+ * at [stripe_start, stripe_start + stripe_len) has a paddable tail.  The
+ * frontier can grow right after this returns; that is safe, because the
+ * later allocation's write serializes behind the caller's stripe lock
+ * and overwrites the zeros.
+ */
+bool btrfs_stripe_run_pad_start(struct btrfs_fs_info *fs_info,
+                               u64 stripe_start, u64 stripe_len,
+                               u64 *pad_from)
+{
+       struct btrfs_block_group *bg;
+       struct btrfs_open_stripe_run *run;
+       unsigned long flags;
+       bool ret = false;
+
+       if (list_empty_careful(&fs_info->open_stripe_bgs))
+               return false;
+       bg = btrfs_lookup_block_group(fs_info, stripe_start);
+       if (!bg)
+               return false;
+
+       spin_lock_irqsave(&bg->stripe_run_lock, flags);
+       list_for_each_entry(run, &bg->open_stripe_runs, list) {
+               if (stripe_start < run->start || stripe_start >= run->end)
+                       continue;
+               if (run->offset > stripe_start &&
+                   run->offset < stripe_start + stripe_len) {
+                       *pad_from = run->offset;
+                       ret = true;
+               }
+               break;
+       }
+       spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+       btrfs_put_block_group(bg);
+       return ret;
+}
+
 /*
  * Can stripe-exclusive allocation be enabled on this filesystem?  Called for
  * the "stripe_alloc" filesystem property, on a mounted (or mounting, options
index 8fef8b474fa86116b96a1a63fc6c5affd1cc4bf4..b2871b6f7443b7cf63c83fd6b3b52ca6b219a4c2 100644 (file)
@@ -422,6 +422,9 @@ void btrfs_wake_open_stripe_waiters(struct btrfs_fs_info *fs_info);
 bool btrfs_stripe_in_open_run(struct btrfs_fs_info *fs_info, u64 logical);
 bool btrfs_stripe_run_range_usable(struct btrfs_block_group *bg,
                                   u64 run_start, u64 *run_len);
+bool btrfs_stripe_run_pad_start(struct btrfs_fs_info *fs_info,
+                               u64 stripe_start, u64 stripe_len,
+                               u64 *pad_from);
 int btrfs_stripe_alloc_check_support(struct btrfs_fs_info *fs_info);
 int btrfs_enable_stripe_alloc(struct btrfs_fs_info *fs_info);
 void btrfs_disable_stripe_alloc(struct btrfs_fs_info *fs_info);
index 8f3c1e6d30cfe6f12d52f219e6e0846290540f65..bdb858859c22c5fe8c026de6051b8aa5fcf1720b 100644 (file)
  */
 #define RBIO_PARKED_BIT                4
 
+/*
+ * Set when a partial write rbio's uncovered data sectors were zero-filled
+ * instead of read, because the covering stripe run says they hold nothing
+ * (see rmw_try_pad_full()).  The zeroes are written with the stripe so
+ * parity matches the disk: the partial write becomes a full-stripe write
+ * with no read phase.
+ */
+#define RBIO_PADDED_BIT                5
+
 /*
  * Set when an unplug callback finished merging this rbio's submission
  * batch: the submitter has no more bios coming.  A sync rbio past this
@@ -1754,7 +1763,12 @@ static int rmw_assemble_write_bios(struct btrfs_raid_bio *rbio,
                        continue;
 
                if (stripe < rbio->nr_data) {
-                       paddrs = sector_paddrs_in_rbio(rbio, stripe, sectornr, 1);
+                       /*
+                        * A padded rbio writes its zero-filled stripe sectors
+                        * too, so parity matches the disk (see RBIO_PADDED_BIT).
+                        */
+                       paddrs = sector_paddrs_in_rbio(rbio, stripe, sectornr,
+                                       !test_bit(RBIO_PADDED_BIT, &rbio->flags));
                        if (paddrs == NULL)
                                continue;
                } else {
@@ -2697,6 +2711,79 @@ static void submit_write_bios(struct btrfs_raid_bio *rbio,
        }
 }
 
+/*
+ * Try to avoid the RMW read phase of a sub-stripe write by zero-filling
+ * the uncovered data sectors.  Legal only when every uncovered sector
+ * lies at or past its stripe run's allocation frontier -- never written
+ * in the run's lifetime, and unreachable by other allocations while this
+ * stripe holds live data (see btrfs_stripe_run_pad_start()).  A frontier
+ * that grows under us is safe: the newer allocation's write serializes
+ * behind this rbio's stripe lock and lands over the zeros.
+ */
+static bool rmw_try_pad_full(struct btrfs_raid_bio *rbio)
+{
+       struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
+       const u64 stripe_start = rbio->bioc->full_stripe_logical;
+       const u32 sectorsize = fs_info->sectorsize;
+       const u64 stripe_len = (u64)rbio->nr_data * BTRFS_STRIPE_LEN;
+       u64 pad_from;
+       int i;
+
+       if (!btrfs_test_opt(fs_info, STRIPE_ALLOC))
+               return false;
+       if (!btrfs_stripe_run_pad_start(fs_info, stripe_start, stripe_len,
+                                       &pad_from))
+               return false;
+
+       /* Populate bio_paddrs so covered sectors are visible below. */
+       index_rbio_pages(rbio);
+
+       /* Every data sector this rbio does not cover must be paddable. */
+       for (i = 0; i < rbio->nr_data * rbio->stripe_nsectors; i++) {
+               const int stripe_nr = i / rbio->stripe_nsectors;
+               const int sectornr = i % rbio->stripe_nsectors;
+               u64 logical = stripe_start + stripe_nr * BTRFS_STRIPE_LEN +
+                             (u64)sectornr * sectorsize;
+
+               if (rbio->bio_paddrs[i * rbio->sector_nsteps] != INVALID_PADDR)
+                       continue;
+               if (logical < pad_from)
+                       return false;
+       }
+
+       if (alloc_rbio_data_pages(rbio) < 0)
+               return false;
+
+       for (i = 0; i < rbio->nr_data * rbio->stripe_nsectors; i++) {
+               const int stripe_nr = i / rbio->stripe_nsectors;
+               const int sectornr = i % rbio->stripe_nsectors;
+               int step;
+
+               if (rbio->bio_paddrs[i * rbio->sector_nsteps] != INVALID_PADDR)
+                       continue;
+               for (step = 0; step < rbio->sector_nsteps; step++) {
+                       phys_addr_t paddr = rbio_stripe_paddr(rbio, stripe_nr,
+                                                             sectornr, step);
+                       void *kaddr = kmap_local_paddr(paddr);
+
+                       memset(kaddr, 0, min_t(u32, sectorsize, PAGE_SIZE));
+                       kunmap_local(kaddr);
+               }
+               set_bit(i, rbio->stripe_uptodate_bitmap);
+       }
+
+       set_bit(RBIO_PADDED_BIT, &rbio->flags);
+       /*
+        * The whole stripe goes out: every vertical position is written.
+        * Under bio_list_lock: until RBIO_RMW_LOCKED_BIT is set, merge_rbio()
+        * can still OR another rbio's bits into this bitmap.
+        */
+       spin_lock(&rbio->bio_list_lock);
+       bitmap_set(&rbio->dbitmap, 0, rbio->stripe_nsectors);
+       spin_unlock(&rbio->bio_list_lock);
+       return true;
+}
+
 /*
  * To determine if we need to read any sector from the disk.
  * Should only be utilized in RMW path, to skip cached rbio.
@@ -2737,8 +2824,15 @@ static void rmw_rbio(struct btrfs_raid_bio *rbio)
        /*
         * Either full stripe write, or we have every data sector already
         * cached, can go to write path immediately.
+        *
+        * Try padding before trusting the cache: a cached rbio can hold pages
+        * from the stripe's previous life, before its extents were freed and
+        * the stripe re-claimed, and parity computed from them matches the
+        * disk only if every uncovered column still holds that data.  Padding
+        * writes every uncovered column, so the stripe needs nothing on disk.
         */
-       if (!rbio_is_full(rbio) && need_read_stripe_sectors(rbio)) {
+       if (!rbio_is_full(rbio) && !rmw_try_pad_full(rbio) &&
+           need_read_stripe_sectors(rbio)) {
                /*
                 * Now we're doing sub-stripe write, also need all data stripes
                 * to do the full RMW.