]> git.hungrycats.org Git - linux/commitdiff
btrfs: add a write-hole invariant checker to the raid56 write path
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 19 Sep 2026 17:17:27 +0000 (13:17 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:16:30 +0000 (21:16 -0500)
With stripe-exclusive allocation, a raid56 data stripe may only be
written while an open or draining stripe run covers it: after its run
retires and drains at a transaction commit, nothing may ever write to
it again, and a write outside any run means an allocation bypassed the
policy.  Both cases are the write hole about to happen.

Check the invariant (under CONFIG_BTRFS_DEBUG) for every raid56 write
operation, full-stripe and sub-stripe alike, at rmw_rbio() time.  This
turns every upstream violation -- a missed allocation path, a
retirement ordering bug, an accounting leak -- into a deterministic
WARN at the moment of the offending write, instead of silent damage
that needs a crash plus a device failure plus a scrub to observe.  The
bios gathered in an rbio have not reported their IO done yet, so their
runs cannot drain under the check: no false positives from completion
races.

Block groups that ever hosted relocation-class runs are skipped
(sticky, debug-only flag): relocation legitimately overwrites its
preallocated extents in place after their runs drain, and the write
path cannot tell those writes from violations.

A runtime enable of the policy (the btrfs.stripe_alloc property set on a
live filesystem) flips it while the legacy allocator may still have writes
in flight, in partly used stripes that no run covers, and the check would
warn on the first of them.  So a runtime enable holds the check off until
a worker has started delalloc on all roots and waited for every ordered
extent (the property is applied inside a transaction, where that cannot
wait); the same wait writes any delalloc tail a writeback racing the
enable left unsubmitted.  A mount-time enable has nothing in flight.
close_ctree() flushes the worker.

Assisted-by: Claude:claude-fable-5
Assisted-by: Claude:claude-fable-5-1
Assisted-by: Claude:claude-opus-5-5
fs/btrfs/block-group.c
fs/btrfs/block-group.h
fs/btrfs/disk-io.c
fs/btrfs/extent-tree.c
fs/btrfs/fs.h
fs/btrfs/raid56.c

index ce88e22f780aaa00333cd9030e01c3a2fe712884..09aa4404f7058b0030db106a8ca519a90307fd68 100644 (file)
@@ -5,6 +5,8 @@
 #include "misc.h"
 #include "ctree.h"
 #include "block-group.h"
+#include "ordered-data.h"
+#include "btrfs_inode.h"
 #include "space-info.h"
 #include "disk-io.h"
 #include "free-space-cache.h"
@@ -2268,8 +2270,33 @@ static void warn_stripe_alloc_uncovered(struct btrfs_fs_info *info)
 "stripe_alloc does not cover raid56 metadata: the write hole remains for metadata, consider converting it to raid1c3/raid1c4");
 }
 
+/*
+ * Drain the writes the legacy allocator placed before stripe_alloc was
+ * enabled on a live filesystem, then arm the write-hole invariant check.
+ * Runs in a worker: the property that enables the policy is applied inside
+ * a transaction, where flushing delalloc cannot wait.
+ */
+void btrfs_stripe_alloc_enable_work(struct work_struct *work)
+{
+       struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
+                                                    stripe_alloc_enable_work);
+
+       btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
+       btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
+       clear_bit(BTRFS_FS_STRIPE_ALLOC_ENABLING, &fs_info->flags);
+       btrfs_info(fs_info,
+          "stripe_alloc: writes placed before the policy was enabled have landed");
+}
+
+/*
+ * Turn on stripe-exclusive allocation, from the "stripe_alloc" filesystem
+ * property on the top-level subvolume's root directory.  Runs during mount
+ * when the root directory inode loads its properties -- before any user IO
+ * -- or at runtime from btrfs_set_prop().
+ */
 int btrfs_enable_stripe_alloc(struct btrfs_fs_info *fs_info)
 {
+       bool live = test_bit(BTRFS_FS_OPEN, &fs_info->flags);
        int ret;
 
        if (btrfs_test_opt(fs_info, STRIPE_ALLOC))
@@ -2277,6 +2304,17 @@ int btrfs_enable_stripe_alloc(struct btrfs_fs_info *fs_info)
        ret = btrfs_stripe_alloc_check_support(fs_info);
        if (ret)
                return ret;
+       /*
+        * On a live filesystem the legacy allocator may still have writes in
+        * flight.  Their extents sit in partly used stripes that no stripe run
+        * covers, so the write-hole invariant check would report them although
+        * they were placed before the policy existed.  Keep the check off until
+        * every ordered extent that could have been allocated before the flip
+        * has landed; extents allocated after it have stripe runs.  A mount-time
+        * enable has nothing in flight and skips the drain.
+        */
+       if (live)
+               set_bit(BTRFS_FS_STRIPE_ALLOC_ENABLING, &fs_info->flags);
        btrfs_set_opt(fs_info->mount_opt, STRIPE_ALLOC);
        warn_stripe_alloc_uncovered(fs_info);
        /*
@@ -2289,6 +2327,8 @@ int btrfs_enable_stripe_alloc(struct btrfs_fs_info *fs_info)
        stripe_alloc_sweep_groups(fs_info, true);
        btrfs_info(fs_info,
                   "using stripe-exclusive allocation for raid56 data");
+       if (live)
+               queue_work(system_unbound_wq, &fs_info->stripe_alloc_enable_work);
        return 0;
 }
 
@@ -2976,6 +3016,56 @@ bool btrfs_stripe_alloc_forces_cow(struct btrfs_fs_info *fs_info, u64 bytenr)
        return ret;
 }
 
+#ifdef CONFIG_BTRFS_DEBUG
+/*
+ * Write-hole invariant checker, called for every raid56 write (full-stripe
+ * and sub-stripe alike).  Under stripe_alloc and stripe_meta a stripe may
+ * only be written while an open or draining run covers it; a write outside
+ * every run means a retired stripe is being rewritten or an allocation
+ * bypassed the policy.
+ *
+ * Groups that hosted RELOC or NOCOW runs are skipped: those extents are
+ * legitimately overwritten in place after their runs drain.
+ */
+void btrfs_stripe_check_write(struct btrfs_fs_info *fs_info,
+                             u64 full_stripe_start, bool sub_stripe)
+{
+       struct btrfs_open_stripe_run *run;
+       struct btrfs_block_group *bg;
+       unsigned long flags;
+       bool live = false;
+       u64 fsl;
+
+       if (!btrfs_test_opt(fs_info, STRIPE_ALLOC))
+               return;
+       /* the legacy allocator's writes are still landing after a runtime enable */
+       if (test_bit(BTRFS_FS_STRIPE_ALLOC_ENABLING, &fs_info->flags))
+               return;
+       bg = btrfs_lookup_block_group(fs_info, full_stripe_start);
+       if (!bg)
+               return;
+       if (!btrfs_is_stripe_alloc_bg(bg) ||
+           test_bit(BLOCK_GROUP_FLAG_STRIPE_RELOC_USED, &bg->runtime_flags))
+               goto out;
+       fsl = bg->full_stripe_len;
+       spin_lock_irqsave(&bg->stripe_run_lock, flags);
+       list_for_each_entry(run, &bg->open_stripe_runs, list) {
+               if (full_stripe_start < run->end &&
+                   full_stripe_start + fsl > run->start) {
+                       live = true;
+                       break;
+               }
+       }
+       spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+       WARN_ONCE(!live,
+"btrfs: %s write to stripe %llu (block group %llu) outside any live stripe run: write hole window violated",
+                 sub_stripe ? "sub-stripe" : "full-stripe",
+                 full_stripe_start, bg->start);
+out:
+       btrfs_put_block_group(bg);
+}
+#endif
+
 /*
  * Drop the dedication of a block group to data relocation.  Shared by the
  * zoned allocator and the stripe allocation policy; both dedicate one
index d6b2c12c7230c09b6348221abb922ea97e7d4156..7f4227e488fbd8dd294454c55be96cdb42198937 100644 (file)
@@ -127,6 +127,13 @@ enum btrfs_block_group_flags {
        BLOCK_GROUP_FLAG_NEW,
        BLOCK_GROUP_FLAG_FULLY_REMAPPED,
        BLOCK_GROUP_FLAG_STRIPE_REMOVAL_PENDING,
+       /*
+        * The group has hosted relocation-class stripe runs.  Relocation
+        * overwrites its preallocated extents in place after their runs
+        * drain, so the write-hole debug check cannot tell those writes
+        * from violations and skips such groups (sticky, debug only).
+        */
+       BLOCK_GROUP_FLAG_STRIPE_RELOC_USED,
        /*
         * Free space changed this transaction, so the group's stripe_unusable
         * counter must be rescanned at commit (stripe_alloc only).
@@ -556,6 +563,15 @@ void btrfs_release_data_reloc_bg(struct btrfs_fs_info *fs_info);
 bool btrfs_stripe_alloc_forces_cow(struct btrfs_fs_info *fs_info, u64 bytenr);
 bool btrfs_is_stripe_alloc_bg(const struct btrfs_block_group *bg);
 void btrfs_scan_stripe_unusable(struct btrfs_fs_info *fs_info, bool force);
+void btrfs_stripe_alloc_enable_work(struct work_struct *work);
+#ifdef CONFIG_BTRFS_DEBUG
+void btrfs_stripe_check_write(struct btrfs_fs_info *fs_info,
+                             u64 full_stripe_start, bool sub_stripe);
+#else
+static inline void btrfs_stripe_check_write(struct btrfs_fs_info *fs_info,
+                                           u64 full_stripe_start,
+                                           bool sub_stripe) { }
+#endif
 struct btrfs_block_group *btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info,
                                                  u64 bytenr);
 void btrfs_dec_nocow_writers(struct btrfs_block_group *bg);
index 870e8dd6b6eedc3ccaedd0e8a186a2cb81ce4458..e5c3ebca75dd658c6f614ea239d136116d4c6dc7 100644 (file)
@@ -2805,6 +2805,7 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
        INIT_LIST_HEAD(&fs_info->delalloc_roots);
        INIT_LIST_HEAD(&fs_info->caching_block_groups);
        INIT_LIST_HEAD(&fs_info->open_stripe_bgs);
+       INIT_WORK(&fs_info->stripe_alloc_enable_work, btrfs_stripe_alloc_enable_work);
        fs_info->stripe_log_data_shared = true;
        fs_info->stripe_park_timeout_ms = BTRFS_RBIO_PARK_TIMEOUT_MS;
        fs_info->stripe_compact = 10;
@@ -4500,6 +4501,7 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info)
         */
        btrfs_run_delayed_iputs(fs_info);
 
+       flush_work(&fs_info->stripe_alloc_enable_work);
        cancel_work_sync(&fs_info->async_reclaim_work);
        cancel_work_sync(&fs_info->async_data_reclaim_work);
        cancel_work_sync(&fs_info->preempt_reclaim_work);
index 5a464351b72dece643163b876bea7493f47db442..cd945076a6d7934b694f72744f2ccab8fca3980b 100644 (file)
@@ -4358,6 +4358,9 @@ static int do_allocation_stripe(struct btrfs_block_group *block_group,
                        btrfs_clear_data_reloc_bg(block_group);
                return 1;
        }
+       if (class == BTRFS_STRIPE_RUN_RELOC)
+               set_bit(BLOCK_GROUP_FLAG_STRIPE_RELOC_USED,
+                       &block_group->runtime_flags);
        stripe_split_taken(ffe_ctl, num_bytes);
        ffe_ctl->found_offset = offset;
        ffe_ctl->search_start = offset;
index 54b568b419bf03ef2657f01fb43f4effe9970a99..c09060496cb205486dcff5c723d4747063ab9b9a 100644 (file)
@@ -234,6 +234,13 @@ enum {
         */
        BTRFS_FS_UNALIGNED_TREE_BLOCK,
 
+       /*
+        * stripe_alloc was enabled on a live filesystem and the legacy
+        * allocator's writes have not all landed yet: the write-hole
+        * invariant check stays off until they have.
+        */
+       BTRFS_FS_STRIPE_ALLOC_ENABLING,
+
 #if BITS_PER_LONG == 32
        /* Indicate if we have error/warn message printed on 32bit systems */
        BTRFS_FS_32BIT_ERROR,
@@ -1023,6 +1030,8 @@ struct btrfs_fs_info {
 
        struct semaphore uuid_tree_rescan_sem;
 
+       /* Drains legacy writes after stripe_alloc is enabled at runtime. */
+       struct work_struct stripe_alloc_enable_work;
        /* Used to reclaim the metadata space in the background. */
        struct work_struct async_reclaim_work;
        struct work_struct async_data_reclaim_work;
index f458014e857928d2ff1fc2fed0805b37f99d7123..e1886e245d2f267ba5d03cb78bc9fc9c09375520 100644 (file)
@@ -3189,6 +3189,16 @@ static void rmw_rbio(struct btrfs_raid_bio *rbio)
        int sectornr;
        int ret = 0;
 
+       /*
+        * Write-hole invariant check: every write must land in a stripe
+        * covered by a live stripe run when stripe-exclusive allocation is
+        * enabled.  The bios gathered in this rbio have not reported their
+        * IO done yet, so their runs cannot drain under us.
+        */
+       btrfs_stripe_check_write(rbio->bioc->fs_info,
+                                rbio->bioc->full_stripe_logical,
+                                !rbio_is_full(rbio));
+
        /*
         * Allocate the pages for parity first, as P/Q pages will always be
         * needed for both full-stripe and sub-stripe writes.