]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: count whole stripes stranded behind live runs as trapped
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 19 Sep 2026 17:18:19 +0000 (13:18 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:37:51 +0000 (18:37 -0500)
The data admission gate admits a reservation while the whole-stripe
supply, bytes_stripe_claimable, covers it, and the per-commit scan
measures that supply as the block group's wholly free full stripes.
But a wholly free stripe inside a live stripe run's range cannot be
claimed: btrfs_stripe_run_range_usable() rejects any claim overlapping
a run object, because a second run over the same range would make the
range-to-run lookups ambiguous.  The run was claimed over the stripe,
the extents allocated there were freed again, and the run has not been
freed yet.  For a COW run that window closes at the next commit; a
nodatacow inode's private run deliberately survives commits and lives
until the inode is evicted.

On a raid56 filesystem filled to ENOSPC by fsstress (thousands of
fallocated files, so thousands of cached inodes each holding a private
NOCOW run) a live walk of the free space cache showed the accounting
exact -- the wholly free stripes summed to bytes_stripe_claimable to the
byte -- and 99% of them inside the ranges of open NOCOW runs with no
inflight IO: 22.8 MiB of 22.8 MiB behind 387 runs in one group, 73.4 MiB
of 73.4 MiB behind 976 runs in another.  Every claim failed in O(1) on
the "no claimable run" verdict, every write() admitted against the 99 MiB
of phantom supply failed at writeback, and the data was dropped ("data
writeback allocation ... returned ENOSPC despite reservation margin"),
about 100,000 delalloc ranges per run of the degraded acceptance suite
where the stock allocator drops none.

Have the scan take such stripes out of the claimable supply: count them
in stripe_unusable, which btrfs_space_info_used() already charges so the
gate cannot admit against them, and record them separately as
stripe_stranded so that btrfs_stripe_bg_wants_reclaim() does not ask
relocation to recover space that comes back by itself.  When a run is
freed, mark its group for rescan so the next commit credits the stripes
back.  The space_info total is exposed as bytes_stripe_stranded in sysfs
and in the space info dump.

This makes the gate honest -- write() gets the ENOSPC instead of
writeback dropping the data -- but does not return the stranded space
while the runs live; that is the run lifecycle's business.

The space_info totals are kept equal to the sum of the armed groups:
claim, arm, disarm and rescan update a group's value and the total
together under sinfo->lock, and the commit scan sums the groups under
sinfo->lock while it holds groups_sem.  Reading the groups unlocked and
assigning the totals later lost or doubled updates made in between, and a
group made read-only could have its whole claimable supply counted again
until the next commit, admitting writes that writeback could not place.
The scan does not restore the stripe_meta margin and reserve when
stripe_alloc was disabled after it started.

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

index 88f5da29160967377898bd7de72785b76e099f92..7d3bf73ec00553a76364bfce7f4d9821199e4ebb 100644 (file)
@@ -596,6 +596,14 @@ static void free_open_stripe_run(struct btrfs_block_group *bg,
        list_del(&run->list);
        bitmap_free(run->live);
        kfree(run);
+       /*
+        * Wholly free stripes inside this run's range were counted as trapped
+        * by the last stripe_unusable scan (btrfs_stripe_run_stranded_stripes());
+        * with the run gone they are claimable again, so have the next commit
+        * rescan the group and credit them back.
+        */
+       if (READ_ONCE(bg->stripe_unusable_ready))
+               set_bit(BLOCK_GROUP_FLAG_STRIPE_UNUSABLE_DIRTY, &bg->runtime_flags);
        wake_up_var(&bg->open_stripe_runs);
 }
 
@@ -1058,6 +1066,42 @@ bool btrfs_stripe_run_range_usable(struct btrfs_block_group *bg,
        return true;
 }
 
+/*
+ * For the stripe_unusable scan: whole free stripes inside a live run's
+ * range cannot be claimed while the run exists
+ * (btrfs_stripe_run_range_usable()), so they must not count as claimable
+ * or the admission gate would admit writes that cannot be placed.  Clear
+ * them from @freep and return their total; free_open_stripe_run() marks the
+ * group for rescan so they are credited back when the run goes.  Caller
+ * holds ctl->tree_lock; stripe_run_lock nests inside it.
+ */
+u64 btrfs_stripe_run_stranded_stripes(struct btrfs_block_group *bg, u32 *freep,
+                                     u64 nstripes)
+{
+       const u64 fsl = bg->full_stripe_len;
+       struct btrfs_open_stripe_run *run;
+       unsigned long flags;
+       u64 stranded = 0;
+
+       spin_lock_irqsave(&bg->stripe_run_lock, flags);
+       list_for_each_entry(run, &bg->open_stripe_runs, list) {
+               u64 idx, last;
+
+               if (run->end <= run->start)
+                       continue;
+               idx = div64_u64(run->start - bg->start, fsl);
+               last = div64_u64(run->end - 1 - bg->start, fsl);
+               for (; idx <= last && idx < nstripes; idx++) {
+                       if (freep[idx] != fsl)
+                               continue;
+                       freep[idx] = 0;
+                       stranded += fsl;
+               }
+       }
+       spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+       return stranded;
+}
+
 /*
  * Is the full stripe at @stripe inside a live run's range, i.e. stranded by
  * the rule of btrfs_stripe_run_stranded_stripes()?  Caller holds
@@ -1752,6 +1796,7 @@ void btrfs_scan_stripe_unusable(struct btrfs_fs_info *fs_info, bool force)
                u64 total = 0;
                u64 total_claimable = 0;
                u64 total_claimable_reloc = 0;
+               u64 total_stranded = 0;
                int raid;
 
                if (!(sinfo->flags & BTRFS_BLOCK_GROUP_DATA))
@@ -1776,29 +1821,44 @@ void btrfs_scan_stripe_unusable(struct btrfs_fs_info *fs_info, bool force)
                                 */
                                if (force || dirty)
                                        btrfs_block_group_rescan_stripe_unusable(bg);
-                               /*
-                                * Sum the authoritative per-group values into the
-                                * space_info total so it cannot drift from repeated
-                                * arm/disarm/rescan deltas.  Disarmed (read-only)
-                                * groups are excluded; their free space is already
-                                * accounted as read-only.
-                                */
-                               if (READ_ONCE(bg->stripe_unusable_ready)) {
-                                       total += READ_ONCE(bg->stripe_unusable);
-                                       total_claimable +=
-                                               READ_ONCE(bg->stripe_claimable);
+                       }
+               }
+
+               /*
+                * Sum the authoritative per-group values into the space_info
+                * total so it cannot drift from repeated arm/disarm/rescan
+                * deltas.  Disarmed (read-only) groups are excluded; their
+                * free space is already accounted as read-only.  Every update
+                * of a group's values also updates the total under
+                * sinfo->lock, so summing under it gives totals that match
+                * the groups: claims, arms and disarms that race with the
+                * scan are neither lost nor counted twice.
+                */
+               spin_lock(&sinfo->lock);
+               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, &sinfo->block_groups[raid],
+                                           list) {
+                               if (bg->stripe_unusable_ready) {
+                                       total += bg->stripe_unusable;
+                                       total_stranded += READ_ONCE(bg->stripe_stranded);
+                                       total_claimable += bg->stripe_claimable;
                                        if (bg->start == READ_ONCE(fs_info->data_reloc_bg))
                                                total_claimable_reloc +=
-                                                       READ_ONCE(bg->stripe_claimable);
+                                                       bg->stripe_claimable;
                                }
                        }
                }
                up_read(&sinfo->groups_sem);
 
-               spin_lock(&sinfo->lock);
                sinfo->bytes_stripe_unusable = total;
                sinfo->bytes_stripe_claimable = total_claimable;
                sinfo->bytes_stripe_claimable_reloc = total_claimable_reloc;
+               sinfo->bytes_stripe_stranded = total_stranded;
                /*
                 * The rescan usually lowers the counter (mid-transaction
                 * incremental adds overcount conservatively); admission
index 4f91a65dc97731f4e7497fefbe8629ae171c28ad..da1ff62805b297ace336c2e441b1f1d309030dcc 100644 (file)
@@ -278,6 +278,16 @@ struct btrfs_block_group {
         * free_space_ctl->tree_lock and space_info->lock.
         */
        u64 stripe_unusable;
+       /*
+        * The part of stripe_unusable that is wholly free stripes lying inside
+        * live stripe-run ranges (stripe_alloc): the run was claimed over them,
+        * the extents allocated there were freed again, and the run object
+        * still exists, so btrfs_stripe_run_range_usable() refuses to claim
+        * them.  Trapped for admission, but they come back by themselves when
+        * the run is freed, so reclaim leaves them out.  Set by the same scan,
+        * under the same lock.
+        */
+       u64 stripe_stranded;
        /*
         * Directly measured claimable supply: bytes of fully free, aligned
         * whole stripes in the free space cache -- exactly what
@@ -451,6 +461,8 @@ void btrfs_close_bg_open_stripes(struct btrfs_block_group *bg);
 void btrfs_retire_block_group_stripes(struct btrfs_block_group *bg);
 void btrfs_wake_open_stripe_waiters(struct btrfs_fs_info *fs_info);
 bool btrfs_stripe_in_open_run(struct btrfs_fs_info *fs_info, u64 logical);
+u64 btrfs_stripe_run_stranded_stripes(struct btrfs_block_group *bg, u32 *freep,
+                                     u64 nstripes);
 bool btrfs_stripe_run_strands(struct btrfs_block_group *bg, u64 stripe);
 bool btrfs_stripe_run_range_usable(struct btrfs_block_group *bg,
                                   u64 run_start, u64 *run_len);
index 0388f450cac57a8b80ddb8e31ae300c830f7fba8..7f0a206f65dd9795ff9bb8341c494b348605f930 100644 (file)
@@ -2786,6 +2786,10 @@ static void stripe_unusable_mark_dirty(struct btrfs_block_group *bg);
  * free space is never credited here (see add_free_space()), save a trim's
  * return (stripe_trim_claimable()); the commit rescan restores it.  Caller
  * holds ctl->tree_lock.
+ *
+ * The group's value and the space_info total change in one sinfo->lock
+ * section, as everywhere else, so that btrfs_scan_stripe_unusable(), which
+ * sums the groups under that lock, never sees one without the other.
  */
 static void stripe_claimable_mod(struct btrfs_block_group *bg, s64 delta)
 {
@@ -2793,14 +2797,16 @@ static void stripe_claimable_mod(struct btrfs_block_group *bg, s64 delta)
 
        if (!READ_ONCE(bg->stripe_unusable_ready) || !delta)
                return;
+       if (!sinfo) {
+               if (delta < 0 && bg->stripe_claimable < (u64)-delta)
+                       delta = -(s64)bg->stripe_claimable;
+               bg->stripe_claimable += delta;
+               return;
+       }
+       spin_lock(&sinfo->lock);
        if (delta < 0 && bg->stripe_claimable < (u64)-delta)
                delta = -(s64)bg->stripe_claimable;
-       if (!delta)
-               return;
        bg->stripe_claimable += delta;
-       if (!sinfo)
-               return;
-       spin_lock(&sinfo->lock);
        if (delta < 0 && sinfo->bytes_stripe_claimable < (u64)-delta)
                sinfo->bytes_stripe_claimable = 0;
        else
@@ -3447,6 +3453,13 @@ static u64 stripe_unusable_scan(struct btrfs_block_group *bg, u32 *freep,
               "stripe_unusable_scan counted=%llu != free_space=%llu",
               counted, ctl->free_space);
 
+       /*
+        * Whole stripes stranded behind live stripe runs are free but not
+        * claimable until their run is freed: trapped, like a partial stripe's
+        * free bytes, so the admission gate does not admit writes against them.
+        */
+       bg->stripe_stranded = btrfs_stripe_run_stranded_stripes(bg, freep, nstripes);
+       trapped = bg->stripe_stranded;
        for (i = 0; i < nstripes; i++)
                if (freep[i] && freep[i] < fsl)
                        trapped += freep[i];
@@ -3508,22 +3521,19 @@ void btrfs_block_group_init_stripe_unusable(struct btrfs_block_group *bg)
                return;
        }
        trapped = stripe_unusable_scan(bg, freep, nstripes);
-       bg->stripe_unusable = trapped;
        /* Every free byte is claimable or trapped; the scan split them. */
-       bg->stripe_claimable = ctl->free_space - trapped;
-       claimable = bg->stripe_claimable;
+       claimable = ctl->free_space - trapped;
+       /* Group and total together; see stripe_claimable_mod(). */
+       spin_lock(&bg->space_info->lock);
+       bg->stripe_unusable = trapped;
+       bg->stripe_claimable = claimable;
        bg->stripe_unusable_ready = true;
+       btrfs_space_info_update_bytes_stripe_unusable(bg->space_info, trapped);
+       btrfs_space_info_update_bytes_stripe_claimable(bg->space_info,
+                                                      claimable);
+       spin_unlock(&bg->space_info->lock);
        spin_unlock(&ctl->tree_lock);
        kvfree(freep);
-
-       if (trapped || claimable) {
-               spin_lock(&bg->space_info->lock);
-               btrfs_space_info_update_bytes_stripe_unusable(bg->space_info,
-                                                             trapped);
-               btrfs_space_info_update_bytes_stripe_claimable(bg->space_info,
-                                                              claimable);
-               spin_unlock(&bg->space_info->lock);
-       }
 }
 
 /*
@@ -3548,22 +3558,20 @@ void btrfs_block_group_disarm_stripe_unusable(struct btrfs_block_group *bg)
                spin_unlock(&ctl->tree_lock);
                return;
        }
+       /* Group and total together; see stripe_claimable_mod(). */
+       spin_lock(&bg->space_info->lock);
        trapped = bg->stripe_unusable;
        claimable = bg->stripe_claimable;
        bg->stripe_claimable = 0;
        bg->stripe_unusable_ready = false;
+       btrfs_space_info_update_bytes_stripe_unusable(bg->space_info,
+                                                     -(s64)trapped);
+       btrfs_space_info_update_bytes_stripe_claimable(bg->space_info,
+                                                      -(s64)claimable);
+       if (bg->start == READ_ONCE(bg->fs_info->data_reloc_bg))
+               bg->space_info->bytes_stripe_claimable_reloc = 0;
+       spin_unlock(&bg->space_info->lock);
        spin_unlock(&ctl->tree_lock);
-
-       if (trapped || claimable) {
-               spin_lock(&bg->space_info->lock);
-               btrfs_space_info_update_bytes_stripe_unusable(bg->space_info,
-                                                             -(s64)trapped);
-               btrfs_space_info_update_bytes_stripe_claimable(bg->space_info,
-                                                              -(s64)claimable);
-               if (bg->start == READ_ONCE(bg->fs_info->data_reloc_bg))
-                       bg->space_info->bytes_stripe_claimable_reloc = 0;
-               spin_unlock(&bg->space_info->lock);
-       }
 }
 
 
@@ -3591,10 +3599,9 @@ void btrfs_block_group_rescan_stripe_unusable(struct btrfs_block_group *bg)
        stripe_return_clusters(bg);
        spin_lock(&ctl->tree_lock);
        if (bg->stripe_unusable_ready) {        /* skip if disarmed (read-only) */
-               u64 scanned;
+               const u64 trapped = stripe_unusable_scan(bg, freep, nstripes);
+               const u64 scanned = ctl->free_space - trapped;
 
-               bg->stripe_unusable = stripe_unusable_scan(bg, freep, nstripes);
-               scanned = ctl->free_space - bg->stripe_unusable;
                /*
                 * The incremental rules may only under-count claimable
                 * supply; more than the scan finds means a missed debit,
@@ -3605,7 +3612,14 @@ void btrfs_block_group_rescan_stripe_unusable(struct btrfs_block_group *bg)
                WARN_RATELIMIT(bg->stripe_claimable > scanned,
                          "bg %llu incremental stripe_claimable %llu above scanned %llu",
                          bg->start, bg->stripe_claimable, scanned);
+               /*
+                * Under sinfo->lock so btrfs_scan_stripe_unusable() sums
+                * both new values or both old ones.
+                */
+               spin_lock(&bg->space_info->lock);
+               bg->stripe_unusable = trapped;
                bg->stripe_claimable = scanned;
+               spin_unlock(&bg->space_info->lock);
        }
        spin_unlock(&ctl->tree_lock);
        kvfree(freep);
index 5766ffaac56d9b5ff5c1de67aa64f7d546f7f399..82eeb090785b7edaec6fc9819d3262ca49d858f7 100644 (file)
@@ -168,6 +168,11 @@ struct btrfs_space_info {
                                           stripes stranded behind live runs.
                                           Counted as used, and subtracted in
                                           statfs. */
+       u64 bytes_stripe_stranded;      /* the part of bytes_stripe_unusable that
+                                          is whole stripes stranded behind live
+                                          stripe runs (see the block group's
+                                          stripe_stranded); back to claimable when
+                                          the runs are freed */
        u64 bytes_stripe_claimable_reloc;       /* the part of bytes_stripe_claimable
                                           in the group dedicated to data
                                           relocation (fs_info->data_reloc_bg):
index 9dfc4e92e48bf5970ec9c5b6bc7f56f22b9b273f..690dd862f5650fe19b01198e76d10de30a57533e 100644 (file)
@@ -930,6 +930,7 @@ SPACE_INFO_ATTR(bytes_may_use);
 SPACE_INFO_ATTR(bytes_readonly);
 SPACE_INFO_ATTR(bytes_zone_unusable);
 SPACE_INFO_ATTR(bytes_stripe_unusable);
+SPACE_INFO_ATTR(bytes_stripe_stranded);
 SPACE_INFO_ATTR(bytes_stripe_claimable);
 SPACE_INFO_ATTR(bytes_stripe_open);
 SPACE_INFO_ATTR(bytes_stripe_margin);
@@ -1063,6 +1064,7 @@ static struct attribute *space_info_attrs[] = {
        BTRFS_ATTR_PTR(space_info, bytes_readonly),
        BTRFS_ATTR_PTR(space_info, bytes_zone_unusable),
        BTRFS_ATTR_PTR(space_info, bytes_stripe_unusable),
+       BTRFS_ATTR_PTR(space_info, bytes_stripe_stranded),
        BTRFS_ATTR_PTR(space_info, bytes_stripe_claimable),
        BTRFS_ATTR_PTR(space_info, bytes_stripe_open),
        BTRFS_ATTR_PTR(space_info, bytes_stripe_margin),