]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_meta: cover raid56 system chunks
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 20 Sep 2026 05:33:09 +0000 (01:33 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:38:13 +0000 (18:38 -0500)
The chunk tree lives in system chunks, which carry no METADATA bit, so the
whole-stripe rule that stripe_meta applies to raid56 tree blocks never
applied to it: chunk tree blocks were placed by the clustered allocator,
sharing stripes with chunk tree blocks committed earlier, and every chunk
tree COW was a read-modify-write of such a stripe.  The RMW audit measured
it (meta_rmw_old over system chunks during chunk churn), and the mount
warned about it.  A torn parity rewrite there is the worst write hole the
filesystem can take: with a member then lost, the committed chunk tree
block that shared the stripe reconstructs wrong, the checksum rejects it,
and nothing maps -- the degraded mount fails, and there is no second copy.

Include system block groups in btrfs_is_stripe_meta_bg().  Everything
downstream already keys on that predicate: the whole-stripe allocation in
do_allocation_stripe(), the trapped-space accounting, the covered/uncovered
decision in the RMW report and the audit.  The four walks that select
groups by space_info flags (the enable-time sweep, the unusable-space scan,
the reclaim tick, the commit-time metadata run retirement) gain SYSTEM
alongside DATA and METADATA, so system groups are armed, scanned, reclaimed
when trapped, and have their runs closed after the commit's tree writes
like metadata groups.

reserve_chunk_space() sizes its request in tree blocks, but a chunk tree
COW that needs a fresh run takes a whole stripe for its first block; the
trapped bytes and the open run's remainder are already counted as used, so
what is left is whole free stripes, and the check now demands one stripe
of headroom beyond the request -- deciding when to allocate the next
system chunk, without reserving the headroom -- or the chunk item for that
very allocation could find no stripe to land in.

The "does not cover raid56 system chunks" warning has nothing left to
warn about and goes.

[zygo: 7.3 port: 7.3 predates upstream cd5e2d2f2ea2 (allocate additional
SYSTEM space earlier), so reserve_chunk_space() takes the headroom as on
6.18: left < bytes + headroom decides both the ENOSPC_DEBUG report and
the allocation]

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

index d5774fdfc3219ddf0716a9a8d26d597b88e752c2..535bacbe21116def7bc0d098ee68d799a28d9c9f 100644 (file)
@@ -2376,7 +2376,8 @@ static void stripe_alloc_sweep_groups(struct btrfs_fs_info *fs_info, bool arm)
                int raid;
 
                if (!(sinfo->flags & (BTRFS_BLOCK_GROUP_DATA |
-                                     BTRFS_BLOCK_GROUP_METADATA)))
+                                     BTRFS_BLOCK_GROUP_METADATA |
+                                     BTRFS_BLOCK_GROUP_SYSTEM)))
                        continue;
                /*
                 * Metadata: the scan that maintains the stripe_meta margin
@@ -2421,33 +2422,6 @@ static void stripe_alloc_sweep_groups(struct btrfs_fs_info *fs_info, bool arm)
        }
 }
 
-/*
- * stripe_alloc covers raid56 *data* block groups only.  A filesystem whose
- * metadata is also raid56 keeps the write hole there.  The data guarantee is
- * easy to read as a filesystem-wide one, so say once, when the policy turns
- * on, what is not covered.  That is btrfs_enable_stripe_alloc(): at mount the
- * property applies after the block groups are read, so a report from
- * btrfs_read_block_groups() would find the flag still clear.  (Mixed block
- * groups cannot get here: the property refuses them.)
- */
-static void warn_stripe_alloc_uncovered(struct btrfs_fs_info *info)
-{
-       if (!btrfs_test_opt(info, STRIPE_ALLOC))
-               return;
-
-       /*
-        * raid56 metadata is covered now that the policy follows the chunk
-        * profile.  System chunks are not: btrfs_is_stripe_meta_bg() tests
-        * the METADATA flag, and a system chunk does not carry it, so a
-        * raid56 system chunk keeps the legacy read-modify-write and its
-        * write hole.  They are small and rewritten rarely, but say so
-        * rather than let the earlier "covered" message imply otherwise.
-        */
-       if (info->avail_system_alloc_bits & BTRFS_BLOCK_GROUP_RAID56_MASK)
-               btrfs_warn(info,
-"stripe_alloc does not cover raid56 system chunks: the write hole remains for them, consider converting 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.
@@ -2501,7 +2475,6 @@ int btrfs_enable_stripe_alloc(struct btrfs_fs_info *fs_info)
        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);
        /*
         * Arm stripe_unusable accounting for raid56 data groups whose caches
         * loaded before the policy was enabled: block groups are read before
@@ -3168,7 +3141,8 @@ void btrfs_retire_meta_stripes(struct btrfs_fs_info *fs_info, u64 transid)
                bg = list_first_entry(&retire_list, struct btrfs_block_group,
                                      open_stripe_retire_list);
                list_del_init(&bg->open_stripe_retire_list);
-               if (!(bg->flags & BTRFS_BLOCK_GROUP_METADATA))
+               if (!(bg->flags & (BTRFS_BLOCK_GROUP_METADATA |
+                                  BTRFS_BLOCK_GROUP_SYSTEM)))
                        continue;
 
                while (again) {
@@ -3385,7 +3359,8 @@ void btrfs_scan_stripe_unusable(struct btrfs_fs_info *fs_info, bool force)
                 * both carry trapped free space.  Mixed groups are refused.
                 */
                if (!(sinfo->flags & (BTRFS_BLOCK_GROUP_DATA |
-                                     BTRFS_BLOCK_GROUP_METADATA)))
+                                     BTRFS_BLOCK_GROUP_METADATA |
+                                     BTRFS_BLOCK_GROUP_SYSTEM)))
                        continue;
                down_read(&sinfo->groups_sem);
                for (raid = 0; raid < BTRFS_NR_RAID_TYPES; raid++) {
@@ -5657,7 +5632,8 @@ static void btrfs_stripe_reclaim_tick(struct btrfs_fs_info *fs_info)
                int raid;
 
                if (!(sinfo->flags & (BTRFS_BLOCK_GROUP_DATA |
-                                     BTRFS_BLOCK_GROUP_METADATA)))
+                                     BTRFS_BLOCK_GROUP_METADATA |
+                                     BTRFS_BLOCK_GROUP_SYSTEM)))
                        continue;
                if (READ_ONCE(sinfo->bg_reclaim_threshold) <= 0)
                        continue;
@@ -8250,6 +8226,7 @@ static void reserve_chunk_space(struct btrfs_trans_handle *trans,
 {
        struct btrfs_fs_info *fs_info = trans->fs_info;
        struct btrfs_space_info *info;
+       u64 headroom = 0;
        u64 left;
        int ret = 0;
 
@@ -8264,13 +8241,41 @@ static void reserve_chunk_space(struct btrfs_trans_handle *trans,
        left = info->total_bytes - btrfs_space_info_used(info, true);
        spin_unlock(&info->lock);
 
-       if (left < bytes && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
-               btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
-                          left, bytes, type);
+       /*
+        * Under stripe_alloc a raid56 system chunk places its tree blocks by
+        * whole stripes (btrfs_is_stripe_meta_bg()), and the bytes trapped in
+        * partial stripes and the open run's remainder are already counted
+        * as used above.  What is left is whole free stripes, but the request
+        * is sized in tree blocks: a chunk tree COW that needs a fresh run
+        * takes a whole stripe for its first block, so demand one stripe of
+        * headroom beyond the request, or the chunk item for the very system
+        * chunk this would allocate finds no stripe to land in.  The headroom
+        * decides when to allocate; it is not reserved.
+        */
+       if (btrfs_test_opt(fs_info, STRIPE_ALLOC)) {
+               int raid;
+
+               down_read(&info->groups_sem);
+               for (raid = 0; raid < BTRFS_NR_RAID_TYPES; raid++) {
+                       struct btrfs_block_group *bg;
+
+                       if (!(btrfs_raid_array[raid].bg_flag &
+                             BTRFS_BLOCK_GROUP_RAID56_MASK))
+                               continue;
+                       list_for_each_entry(bg, &info->block_groups[raid], list)
+                               headroom = max_t(u64, headroom,
+                                                bg->full_stripe_len);
+               }
+               up_read(&info->groups_sem);
+       }
+
+       if (left < bytes + headroom && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
+               btrfs_info(fs_info, "left=%llu, need=%llu, headroom=%llu, flags=%llu",
+                          left, bytes, headroom, type);
                btrfs_dump_space_info(info, 0, false);
        }
 
-       if (left < bytes) {
+       if (left < bytes + headroom) {
                u64 flags = btrfs_system_alloc_profile(fs_info);
                struct btrfs_block_group *bg;
                struct btrfs_space_info *space_info;
index 5ec42c57150ddaefb35154f803c8338cc13611d0..da2027d79cfa5ce0d6758026c19c12bfedba16b0 100644 (file)
@@ -4246,23 +4246,20 @@ bool btrfs_is_stripe_alloc_bg(const struct btrfs_block_group *bg)
 /*
  * Should this block group allocate tree blocks by whole stripes?
  *
- * The same rule data follows: only fully free stripes are claimed, and the
- * partial stripe left at the end of a commit is padded out rather than
- * revisited, so no tree block write ever lands in a stripe holding earlier
- * committed data.  Not separately gated: the chunk profile already decides
- * it.  raid56 metadata has the same write hole as raid56 data and gets the
- * same protection at the same time; metadata on any other profile has no
- * write hole to close, and the RAID56_MASK test below means the policy
- * simply does not apply to it.  The costs metadata brings -- a stranded
- * stripe tail per commit, and an allocation failure that aborts the
- * transaction rather than failing one write -- are the price of covering
- * raid56 metadata at all, and a filesystem that does not want them wants
- * raid1c3/raid1c4 metadata instead, which is the better answer anyway.
+ * The same rule as data: only fully free stripes are claimed, and the
+ * partial stripe left at the end of a commit is padded rather than
+ * revisited, so no tree block lands in a stripe holding committed blocks.
+ * It applies to every raid56 metadata and system group while stripe_alloc
+ * is on.  The costs are a stranded stripe tail per commit, and an
+ * allocation failure aborts the transaction instead of failing one write.
+ * System chunks carry no METADATA bit but hold the chunk tree, so they are
+ * named explicitly.
  */
 bool btrfs_is_stripe_meta_bg(const struct btrfs_block_group *bg)
 {
        return btrfs_test_opt(bg->fs_info, STRIPE_ALLOC) &&
-              (bg->flags & BTRFS_BLOCK_GROUP_METADATA) &&
+              (bg->flags & (BTRFS_BLOCK_GROUP_METADATA |
+                            BTRFS_BLOCK_GROUP_SYSTEM)) &&
               !(bg->flags & BTRFS_BLOCK_GROUP_DATA) &&
               (bg->flags & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
               !test_bit(BLOCK_GROUP_FLAG_RAID56_MIRRORED, &bg->runtime_flags);
index ffc9498dfb35b2cd2df4b55aa2d7359783b94d8b..5db459538b73f2991bb6c7a042bc2ee9b3738718 100644 (file)
@@ -3654,12 +3654,11 @@ static void report_uncovered_rmw(struct btrfs_raid_bio *rbio)
 
        atomic64_inc(&fs_info->stripe_park_stats.meta_rmw);
        /*
-        * A raid56 metadata group under stripe_meta is covered: its
-        * read-modify-writes are partial writes inside one transaction's
-        * run, counted by the audit as meta_rmw_cur, and the audit warns
-        * by itself should one ever touch a committed block.  Only system
-        * chunks and mixed groups are the uncovered case this message is
-        * about.
+        * Every raid56 metadata and system group is a stripe_meta group while
+        * stripe_alloc is on, and stripe_alloc refuses to enable on mixed
+        * groups (btrfs_stripe_alloc_check_support()), so this warning is a
+        * defensive check: it fires only if a tree block group that should be
+        * covered is not.
         */
        bg = btrfs_lookup_block_group(fs_info, rbio->bioc->full_stripe_logical);
        if (bg) {