]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_meta: extend stripe reclaim to metadata block groups
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Thu, 22 Oct 2026 20:47:05 +0000 (16:47 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:38:11 +0000 (18:38 -0500)
The stripe reclaim of the reclaim subtopic handles stripe_alloc data block
groups only.  stripe_meta allocates tree blocks in whole stripes too, so
its block groups trap free space in partially used stripes the same way,
and that space comes back only by moving the live tree blocks out of those
stripes.  Metadata needs it more than data: a metadata reservation that
finds no claimable stripe aborts the transaction instead of failing one
write, so trapped metadata space that is never reclaimed ends in a forced
read-only filesystem.

Extend each reclaim mechanism to stripe_meta block groups:

 - btrfs_reclaim_block_group(): allocate a chunk to relocate into when the
   other groups' claimable stripes cannot take the group's live bytes, and
   relocate the group even while it has reserved or pinned bytes.  Under
   COW, a metadata group always has tree blocks reserved or pinned, so the
   generic bail-out would defer it forever.

 - The cleaner's periodic re-evaluation scans the metadata space_info as
   well as the data one.

 - Compaction counts metadata extent items: a tree block is nodesize long
   and never spans a stripe, so for a metadata group the closure is the
   partial stripes themselves.

 - stripe_reclaim_now accepts stripe_meta block groups, for one group or
   for all of them.

The comments that name the groups reclaim handles now name both.

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/block-group.c
fs/btrfs/free-space-cache.c
fs/btrfs/fs.h
fs/btrfs/relocation.c
fs/btrfs/sysfs.c
fs/btrfs/volumes.c

index c717fd109aa5652aca6b343eaeb5391531c29318..d8273bfcd6a46bf19fb23ba6b3f185c4bfeefc7e 100644 (file)
@@ -5168,14 +5168,14 @@ static int btrfs_reclaim_block_group(struct btrfs_block_group *bg, int *reclaime
        bool compacted = false;
 
        /*
-        * A stripe_alloc group's live extents can only move into
+        * A stripe_alloc / stripe_meta group's live extents can only move into
         * whole free stripes.  When the rest of the space_info has too few
         * claimable bytes for them, allocate a chunk to relocate into, once per
         * attempt.  Done here rather than in the reservation path so that a
         * device shrink, which relocates without spare room by design, still
         * fails cleanly.
         */
-       if (btrfs_is_stripe_alloc_bg(bg)) {
+       if (btrfs_is_stripe_alloc_bg(bg) || btrfs_is_stripe_meta_bg(bg)) {
                u64 elsewhere = READ_ONCE(space_info->bytes_stripe_claimable);
                u64 need = READ_ONCE(bg->used);
 
@@ -5208,14 +5208,15 @@ static int btrfs_reclaim_block_group(struct btrfs_block_group *bg, int *reclaime
        bg->stripe_reclaim_forced = STRIPE_RECLAIM_NONE;
        if (bg->ro ||
            ((bg->reserved || bg->pinned) &&
-            !btrfs_is_stripe_alloc_bg(bg))) {
+            !btrfs_is_stripe_alloc_bg(bg) &&
+            !btrfs_is_stripe_meta_bg(bg))) {
                /*
                 * We want to bail if we made new allocations or have
                 * outstanding allocations in this block group.  We do
                 * the ro check in case balance is currently acting on
                 * this block group.
                 *
-                * Not for a stripe_alloc group: under
+                * Not for a stripe_alloc or stripe_meta group: under
                 * COW churn it always has blocks reserved or pinned,
                 * so the bail-out would defer it forever, while the
                 * trapped free space it was queued for only grows.
@@ -5262,7 +5263,7 @@ static int btrfs_reclaim_block_group(struct btrfs_block_group *bg, int *reclaime
                compact = (forced == STRIPE_RECLAIM_COMPACT);
        } else if (!should_reclaim_block_group(bg, bg->length)) {
                /*
-                * Too full to relocate whole.  A stripe_alloc
+                * Too full to relocate whole.  A stripe_alloc / stripe_meta
                 * group whose free space is trapped in partially used stripes
                 * can still be compacted: only the extents in those stripes
                 * move.
@@ -5554,7 +5555,7 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
 }
 
 /*
- * Periodic re-evaluation of stripe_alloc groups from the
+ * Periodic re-evaluation of stripe_alloc / stripe_meta groups from the
  * cleaner.  The commit-time rescan only sees groups whose free space changed
  * in that transaction, and only when there is a transaction: a mark that
  * did not land (the group was on another list) or a group the worker dropped
@@ -5571,7 +5572,8 @@ static void btrfs_stripe_reclaim_tick(struct btrfs_fs_info *fs_info)
        list_for_each_entry(sinfo, &fs_info->space_info, list) {
                int raid;
 
-               if (!(sinfo->flags & BTRFS_BLOCK_GROUP_DATA))
+               if (!(sinfo->flags & (BTRFS_BLOCK_GROUP_DATA |
+                                     BTRFS_BLOCK_GROUP_METADATA)))
                        continue;
                if (READ_ONCE(sinfo->bg_reclaim_threshold) <= 0)
                        continue;
@@ -5671,7 +5673,7 @@ static bool stripe_reclaim_now_queue(struct btrfs_block_group *bg,
 }
 
 /*
- * /sys/fs/btrfs/<uuid>/stripe_reclaim_now: reclaim stripe_alloc
+ * /sys/fs/btrfs/<uuid>/stripe_reclaim_now: reclaim stripe_alloc / stripe_meta
  * raid56 block groups now, e.g. from an idle-time job, rather than waiting
  * for the commit-time rescan and its five-minute hysteresis.  The threshold
  * is the space_info's bg_reclaim_threshold, or BTRFS_DEFAULT_RECLAIM_THRESH
@@ -5684,7 +5686,7 @@ static bool stripe_reclaim_now_queue(struct btrfs_block_group *bg,
  * Otherwise @start names one group (its logical start): it is reclaimed
  * whatever the trapped minimums say, compacted if it is above the threshold
  * and has trapped stripes, relocated otherwise.  -ENOENT: no group starts
- * there; -EINVAL: not a stripe_alloc raid56 group (relocate
+ * there; -EINVAL: not a stripe_alloc / stripe_meta raid56 group (relocate
  * any other group with balance's vrange filter); -EBUSY: the group is
  * read-only (balance or scrub hold it); -EAGAIN: it is already on a list
  * (queued, unused or new); -ENOSPC: the other groups' claimable stripes plus
@@ -5722,7 +5724,8 @@ int btrfs_stripe_reclaim_now(struct btrfs_fs_info *fs_info, bool all, u64 start)
                        return -ENOENT;
                if (bg->start != start) {
                        ret = -ENOENT;
-               } else if (!btrfs_is_stripe_alloc_bg(bg)) {
+               } else if (!btrfs_is_stripe_alloc_bg(bg) &&
+                          !btrfs_is_stripe_meta_bg(bg)) {
                        ret = -EINVAL;
                } else if (READ_ONCE(bg->ro)) {
                        ret = -EBUSY;
@@ -5757,7 +5760,8 @@ int btrfs_stripe_reclaim_now(struct btrfs_fs_info *fs_info, bool all, u64 start)
                              BTRFS_BLOCK_GROUP_RAID56_MASK))
                                continue;
                        list_for_each_entry(bg, &sinfo->block_groups[raid], list) {
-                               if (!btrfs_is_stripe_alloc_bg(bg))
+                               if (!btrfs_is_stripe_alloc_bg(bg) &&
+                                   !btrfs_is_stripe_meta_bg(bg))
                                        continue;
                                kind = btrfs_stripe_bg_reclaim_now_kind(bg, thresh,
                                                false, stripe_reclaim_chunk_room(bg));
index ff47e70b26bdad913c7e308d435e5120c19f1fd3..719c515dd58030d026bc3131139ea51d22d431fb 100644 (file)
@@ -3680,7 +3680,7 @@ void btrfs_block_group_disarm_stripe_unusable(struct btrfs_block_group *bg)
 
 
 /*
- * Does this stripe_alloc group want the reclaim worker?  As
+ * Does this stripe_alloc / stripe_meta group want the reclaim worker?  As
  * with zoned's zone_unusable, reclaim once enough of the group's free space
  * is trapped.  A group whose live data is below the threshold is relocated
  * whole; a fuller one is compacted (btrfs_compact_chunk()), moving only the
index ec863798633b22278160070b6edd495ea205a697..d078298d34b912183e12e72fe0def75bb90ed9af 100644 (file)
@@ -992,7 +992,7 @@ struct btrfs_fs_info {
        /* BTRFS_STRIPE_RMW_* masks; see btrfs_stripe_allow_rmw(). */
        u32 stripe_rmw_prop;
        /*
-        * Reclaim compacts a mostly live stripe_alloc group once
+        * Reclaim compacts a mostly live stripe_alloc / stripe_meta group once
         * this percent of its length is trapped; 0 = never (sysfs
         * stripe_compact, default 10).  See btrfs_compact_chunk().
         */
index eeb6a25d82fae28b709e1c51962b81718dfec665..369e74b7dd2060c267983d08f230e47cd799a07f 100644 (file)
@@ -190,14 +190,15 @@ struct reloc_control {
 };
 
 /*
- * Stripe compaction.  A stripe_alloc group whose free space is
+ * Stripe compaction.  A stripe_alloc / stripe_meta group whose free space is
  * trapped in partially used stripes, but whose live data mostly fills whole
  * ones, does not need all of that data moved to get the space back: only the
  * extents in the partial stripes, and -- since moving an extent frees its
  * bytes in every stripe it spans -- the extents sharing a stripe with those,
  * transitively.  compact_plan() selects that closure, find_next_extent()
  * skips every extent outside it, and the group is kept and made writable
- * again instead of removed.
+ * again instead of removed.  A tree block never spans a stripe, so for a
+ * metadata group the closure is the partial stripes themselves.
  */
 static bool compact_selected(const struct reloc_control *rc, u64 start, u64 len)
 {
@@ -5501,6 +5502,7 @@ static u32 compact_root(u32 *parent, u32 x)
 static int compact_plan(struct reloc_control *rc, u64 *moved, u64 *nsel)
 {
        struct btrfs_block_group *bg = rc->block_group;
+       struct btrfs_fs_info *fs_info = bg->fs_info;
        const u64 fsl = bg->full_stripe_len;
        const u64 end = bg->start + bg->length;
        BTRFS_PATH_AUTO_FREE(path);
@@ -5568,6 +5570,8 @@ static int compact_plan(struct reloc_control *rc, u64 *moved, u64 *nsel)
                        break;
                if (key.type == BTRFS_EXTENT_ITEM_KEY)
                        len = key.offset;
+               else if (key.type == BTRFS_METADATA_ITEM_KEY)
+                       len = fs_info->nodesize;
                else
                        continue;
                if (key.objectid < bg->start || !len)
index b2f8af411a02a5e396cd4ac64893704cff07ebaa..cf7fca35a44742f28e023f3d6b4e08fb9d5b822b 100644 (file)
@@ -1484,7 +1484,7 @@ BTRFS_ATTR_RW(, stripe_compact, btrfs_stripe_compact_show,
              btrfs_stripe_compact_store);
 
 /*
- * Reclaim stripe_alloc raid56 block groups now: "all" (or "1")
+ * Reclaim stripe_alloc / stripe_meta raid56 block groups now: "all" (or "1")
  * queues every group the reclaim predicate picks, a block group's logical
  * start forces that group.  See btrfs_stripe_reclaim_now().
  */
index 6b833229632be19ce25156b237a8b366081f7776..1198c181eb9262e9f2c62c44ed818a1297cb7331 100644 (file)
@@ -3764,7 +3764,7 @@ int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset, bool v
 }
 
 /*
- * Compact a stripe_alloc group: relocate only the extents in
+ * Compact a stripe_alloc / stripe_meta group: relocate only the extents in
  * its partially used stripes (and those sharing a stripe with them) and keep
  * it.  When that would move more than half its live bytes the group is
  * relocated and removed as by btrfs_relocate_chunk() instead; @compacted