]> 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>
Mon, 2 Nov 2026 02:16:31 +0000 (21:16 -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 45a09c6b6fdfdc869f530419e83c6b49b85b8281..cd6fe47d6b0f4d8d80b48087ea78289e0c05b6de 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 362e31f8be3d57702e7e37ffbb797ff0764a4b01..1117b14e23d53c78dea1eb6cd9e4d22cc9f2405f 100644 (file)
@@ -2421,7 +2421,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 7186c62e8120cd17b160147a908f7ad2e368b360..686494b85d3e4a10ca0d7a989b0e94efa0de722f 100644 (file)
@@ -990,7 +990,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 79e65dfd9d14b23a57bc91f41c3021ff136c9798..89a915b46e72727fcb85854b16b96dd3bfaed893 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)
 {
@@ -5498,6 +5499,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);
@@ -5565,6 +5567,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 1812ba343ca146d83dff48b4b92996865e9dc4d1..60bf7da1790b4842b088381bc612b6bf1cb98603 100644 (file)
@@ -1371,7 +1371,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 ea98cef92dc3d179cc8565d0a21f2d2796452d74..6c2a9e391dae7a6bc023e1108fbfd5c4e7feabc7 100644 (file)
@@ -3814,7 +3814,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