]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: back off stripe reclaim after repeated ENOSPC
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Thu, 22 Oct 2026 08:02:52 +0000 (04:02 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:16:29 +0000 (21:16 -0500)
The reclaim worker backs off a space_info after repeated -ENOSPC failures.
Under stripe_alloc two more paths queue trapped groups, the commit-time
rescan and the cleaner tick, every five minutes each; have them honour the
backoff too, and have the reclaim predicate require room for relocation's
up-front metadata reservation as well.  A stripe_reclaim_now request is
still tried during a backoff.

It showed in two ways:

 - The octopus P1 pass (raid5 6 x 3 GiB, misc-next, bhive 2026-10-20):
   with the data space fully allocated and ~2 GiB trapped, reclaim held the
   exclusive operation for over an hour of fsstress and long after it,
   "error relocating chunk ...: -28" repeating (28 attempts, 13 errors in
   one loop), so a resize could not start and the replace failed.  The
   worker sat in a metadata reservation (btrfs_block_rsv_refill under
   relocate_block_group): metadata had no room either.

 - reclaim-futile-test.sh (raid5 6 x 1 GiB at the edge, fsstress churn):
   226 reclaim errors in 600 s.  Of the -28 returns, relocation's data
   reservation lost the claimable stripes the room check had counted to
   the concurrent writers.

The room check (claimable stripes in the other groups) is an estimate
taken when the group is queued, and it did not cover metadata: it now
also requires room for relocation's up-front metadata reservation (512
tree blocks, twice RELOCATION_RESERVED_NODES), which the octopus failure
needed.

Tested: reclaim-futile-test.sh measured 226 reclaim errors in 600 s on the
lane without the backoff (the fill-edge shape that loops; it does not loop
on every run).  With it no run logged more than 5 errors per 600 s; in one,
five relocations failed with -ENOSPC and reclaim backed off once
(reclaim_backoffs +1), then resumed.  stripe-reclaim-test and
stripe-reclaim-now-test pass.

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

index 7050b82514cb6e8e48e53f930056bec9583e1998..2af51e4883cb6f80fda8b2fe5655dc442023c274 100644 (file)
@@ -4531,7 +4531,7 @@ static int btrfs_reclaim_block_group(struct btrfs_block_group *bg, int *reclaime
 
 /*
  * Reclaim gives up on a space_info for a while after this many reclaims in a
- * row failed with -ENOSPC.  Relocating a group needs room for
+ * row failed with -ENOSPC.  Relocating or compacting a group needs room for
  * its live data (and metadata reservations for moving it); at the fill edge
  * there is none, every attempt fails after doing its work, and retrying each
  * failed group on every worker run held the balance exclusive operation for
@@ -4655,9 +4655,10 @@ void btrfs_reclaim_block_groups(struct btrfs_fs_info *fs_info, unsigned int limi
                /*
                 * Backing off: drop the group rather than requeue it.  The
                 * rescans and usage updates queue it again once reclaim may
-                * succeed.
+                * succeed.  A stripe_reclaim_now request is tried regardless.
                 */
-               if (btrfs_reclaim_backoff_active(space_info)) {
+               if (!READ_ONCE(bg->stripe_reclaim_forced) &&
+                   btrfs_reclaim_backoff_active(space_info)) {
                        ret = 0;
                } else {
                        int before = reclaimed;
@@ -4745,6 +4746,8 @@ static void btrfs_stripe_reclaim_tick(struct btrfs_fs_info *fs_info)
                                        continue;
                                if (!btrfs_stripe_bg_wants_reclaim(bg))
                                        continue;
+                               if (btrfs_reclaim_backoff_active(sinfo))
+                                       break;
                                spin_lock(&bg->lock);
                                if (btrfs_mark_bg_to_reclaim(bg))
                                        bg->stripe_reclaim_jiffies = jiffies ?: 1;
index 1078f2019743f3d73cbea4b3fbd4d4cedbc25e92..eaed680e362dcdea45e211582987ac8629401a6e 100644 (file)
@@ -2430,6 +2430,33 @@ void btrfs_block_group_disarm_stripe_unusable(struct btrfs_block_group *bg)
  * and queueing a group with nowhere to move it only costs writers read-only
  * transitions.  Counters are read unlocked; the worker re-checks.
  */
+/*
+ * Relocation reserves metadata before it moves anything: 256 tree blocks
+ * up front (RELOCATION_RESERVED_NODES), doubled on its first shortfall.  When
+ * the metadata space_info can neither hold nor overcommit that, relocating
+ * or compacting a data group fails with -ENOSPC after the group was made
+ * read-only, while the worker held the exclusive operation (octopus P1 at the
+ * fill edge, bhive 2026-10-20: the data side had room, metadata had none).
+ */
+#define STRIPE_RECLAIM_META_NODES      512
+
+static bool stripe_reclaim_meta_room(struct btrfs_fs_info *fs_info)
+{
+       struct btrfs_space_info *meta;
+       const u64 bytes = (u64)fs_info->nodesize * STRIPE_RECLAIM_META_NODES;
+
+       bool ok;
+
+       meta = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
+       if (!meta)
+               return true;
+       spin_lock(&meta->lock);
+       ok = btrfs_space_info_used(meta, true) + bytes <= meta->total_bytes ||
+            btrfs_can_overcommit(meta, bytes, BTRFS_RESERVE_FLUSH_ALL);
+       spin_unlock(&meta->lock);
+       return ok;
+}
+
 static enum stripe_reclaim_kind __stripe_bg_reclaim_kind(struct btrfs_block_group *bg,
                                                         int thresh, bool force,
                                                         bool check_room,
@@ -2486,7 +2513,9 @@ static enum stripe_reclaim_kind __stripe_bg_reclaim_kind(struct btrfs_block_grou
                   READ_ONCE(sinfo->bytes_stripe_margin);
        elsewhere -= min(elsewhere, READ_ONCE(bg->stripe_claimable));
        elsewhere -= min(elsewhere, promised);
-       return elsewhere + extra_room >= need ? kind : STRIPE_RECLAIM_NONE;
+       if (elsewhere + extra_room < need)
+               return STRIPE_RECLAIM_NONE;
+       return stripe_reclaim_meta_room(bg->fs_info) ? kind : STRIPE_RECLAIM_NONE;
 }
 
 static enum stripe_reclaim_kind stripe_bg_reclaim_kind(struct btrfs_block_group *bg,
@@ -2586,6 +2615,9 @@ void btrfs_block_group_rescan_stripe_unusable(struct btrfs_block_group *bg)
        }
        spin_unlock(&ctl->tree_lock);
        kvfree(freep);
+       /* Reclaim backing off after ENOSPC: leave the group, the stamp too. */
+       if (reclaim && btrfs_reclaim_backoff_active(bg->space_info))
+               reclaim = false;
        if (reclaim) {
                /*
                 * Stamp the hysteresis only if the mark lands: a group still on