]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: do not count a writeback ENOSPC drop's give-back as supply
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 25 Oct 2026 10:27:49 +0000 (06:27 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:37:54 +0000 (18:37 -0500)
When buffered writeback fails with ENOSPC under stripe_alloc,
cow_file_range() drops the range: the folios end writeback unwritten, the
range's may_use and its margin are released, and the extents it had
already allocated are freed, so their stripes come back as claimable at
the next commit's rescan.  The data was acknowledged by write(2), so it
is lost; that is the bug the earlier patches remove at its causes.  This
patch is about what happens next.

The write admission gate ("btrfs: stripe_alloc: gate data admission on
claimable whole-stripe supply", earlier in this series) sees the
give-back as supply.  The writer blocked on the FLUSH_DATA ticket whose
flush caused the drop is admitted when the same reclaim pass commits
(COMMIT_TRANS is one of the data flush states) and the rescan returns
the dropped extents' stripes.  The supply was not enough for the range
that was just dropped, so the next range fails the same way.  With split
pieces stranding stripes (as they did before "btrfs: stripe_alloc:
pessimistic data reservation margin", earlier in this series, trimmed
them to the group's stripes), one writer of 64 MiB got every write(2)
acknowledged and lost 62.8 MiB in five identical drop cycles of 12.6
MiB.  Stock btrfs releases the same way after a cow_file_range() ENOSPC;
it just does not expect one, because its data reservation is exact.

Keep a quarantine, bytes_stripe_quarantine, that the gate adds to every
non-relocation data admission:

- On the drop, add the range length plus one full-stripe unit per extent
  the range had allocated: each extent claimed at most its length rounded
  up to its group's full stripe, so this bounds what the drop gives back.
  The whole stripes covering the range plus one unit would not: in the
  case above, the range was 13172736 bytes and its first 10747904 were
  allocated as 448K pieces that each claimed 640K in 320K groups, so the
  drop gives back about 15.3 MiB of stripes, where need(L) + unit is
  13.6 MiB.  Capped at the space info's size.  Relocation's own drops do not count:
  its data is never acknowledged to a writer.
- Release it only by claimable stripes that come back for real: whole
  stripes completed by extents unpinned at a commit (deletes, overwrites,
  relocation, compaction), measured with the rescan's own rule, and the
  claimable of a new group (a new chunk, or a group whose free space only
  now finished loading).  Not a group back from read-only: a failed
  relocation at the fill edge returns stripes the supply had before, and
  reclaim's read-only cycling would drain the quarantine.  The rescan
  cannot be used either: it recomputes the total and cannot tell the
  dropped extents' stripes from real frees.
- Relocation is not held back by it, since relocation is what frees
  stripes.  It is cleared when stripe_alloc is turned off at runtime, and
  it is not persistent, so an unmount clears it.

The ticket that waited on the dropping flush now runs out of flush states
with no new supply and fails with ENOSPC, and so does every later write
until something is freed.  The trade-off: ENOSPC in place of loss of
acknowledged data, and stripes that were only temporarily held at the
drop (run tails that a commit returns) stay quarantined until real frees
or a new group arrive.

The counter is in sysfs (bytes_stripe_quarantine), in the space info
dump, and in the data writeback ENOSPC line; each drop logs the new total.

[zygo: 7.3 port: 7.3's btrfs_add_bg_to_space_info() still takes fs_info,
context adjusted; the change itself is unchanged]

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

index f27250895ec76ade05dadc2754b82348fd90b840..0e14c4717b471b4c28c65c8530831c6283386990 100644 (file)
@@ -1389,6 +1389,13 @@ static void stripe_alloc_sweep_groups(struct btrfs_fs_info *fs_info, bool arm)
 
                if (!(sinfo->flags & BTRFS_BLOCK_GROUP_DATA))
                        continue;
+               /* The writeback quarantine is the gate's; it goes with it. */
+               if (!arm && (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)) {
+                       spin_lock(&sinfo->lock);
+                       sinfo->bytes_stripe_quarantine = 0;
+                       btrfs_try_granting_tickets(sinfo);
+                       spin_unlock(&sinfo->lock);
+               }
                down_read(&sinfo->groups_sem);
                for (raid = 0; raid < BTRFS_NR_RAID_TYPES; raid++) {
                        struct btrfs_block_group *bg;
@@ -2663,8 +2670,10 @@ done:
         * accounting.  Nothing until this point counted against
         * stripe_unusable, so the scan captures the full loaded state.
         */
-       if (!ret)
+       if (!ret) {
                btrfs_block_group_init_stripe_unusable(block_group);
+               btrfs_stripe_quarantine_new_group(block_group);
+       }
 
 #ifdef CONFIG_BTRFS_DEBUG
        if (btrfs_should_fragment_free_space(block_group)) {
@@ -5037,6 +5046,7 @@ struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *tran
 
        /* A freshly created stripe_alloc group starts fully free (0 trapped). */
        btrfs_block_group_init_stripe_unusable(cache);
+       btrfs_stripe_quarantine_new_group(cache);
 
        set_avail_alloc_bits(fs_info, type);
        return cache;
index 89ef13b713f280f6eb9433170d9a13e67fbe21f2..658b7d45d7fee243d3113a6b087cc970013269e2 100644 (file)
@@ -2843,6 +2843,7 @@ static int unpin_extent_range(struct btrfs_fs_info *fs_info,
        u64 empty_cluster = 0;
 
        while (start <= end) {
+               u64 quarantine_back;
                u64 len;
                bool readonly;
 
@@ -2867,6 +2868,16 @@ static int unpin_extent_range(struct btrfs_fs_info *fs_info,
 
                if (return_free_space)
                        btrfs_add_free_space(cache, start, len);
+               /*
+                * Whole stripes this free completes are real supply: they pay
+                * back writeback quarantine (stripe_claimable_admit()).
+                */
+               quarantine_back = 0;
+               if (return_free_space &&
+                   READ_ONCE(cache->space_info->bytes_stripe_quarantine) &&
+                   !READ_ONCE(cache->ro))
+                       quarantine_back = btrfs_stripe_claimable_in_range(cache,
+                                                               start, len);
 
                start += len;
                total_unpinned += len;
@@ -2902,6 +2913,8 @@ static int unpin_extent_range(struct btrfs_fs_info *fs_info,
                } else if (return_free_space) {
                        btrfs_return_free_space(space_info, len);
                }
+               if (quarantine_back)
+                       btrfs_stripe_quarantine_free(space_info, quarantine_back);
                spin_unlock(&space_info->lock);
        }
 
index 481f62011baa4f4dd25ade63667b3b745f1f1a1f..225d161277201939d089134b0c2e5d9087ce5d60 100644 (file)
@@ -2909,6 +2909,25 @@ static u64 stripe_claimable_with_range(struct btrfs_block_group *bg,
        return whole;
 }
 
+/*
+ * The claimable whole stripes that the free range [bytenr, bytenr + size) of
+ * a stripe_alloc data group belongs to, for the writeback quarantine.
+ */
+u64 btrfs_stripe_claimable_in_range(struct btrfs_block_group *bg, u64 bytenr,
+                                   u64 size)
+{
+       struct btrfs_free_space_ctl *ctl = bg->free_space_ctl;
+       u64 whole = 0;
+
+       if (!btrfs_is_stripe_alloc_bg(bg))
+               return 0;
+       spin_lock(&ctl->tree_lock);
+       if (bg->stripe_unusable_ready)
+               whole = stripe_claimable_with_range(bg, bytenr, size);
+       spin_unlock(&ctl->tree_lock);
+       return whole;
+}
+
 /*
  * A trim takes a free range out of the tree for the length of its discard
  * (trim_no_bitmap(), trim_bitmaps()) and do_trimming() adds it back.  A
index 92db8205cc4350deffb35c7126f7c898f6a7d2b4..b37d324d28e7a921c095b3e2ee11143518c9cd14 100644 (file)
@@ -141,6 +141,8 @@ u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,
 int btrfs_claim_free_stripe_run(struct btrfs_block_group *block_group,
                                u64 want_bytes, u64 *start, u64 *len);
 void btrfs_block_group_init_stripe_unusable(struct btrfs_block_group *block_group);
+u64 btrfs_stripe_claimable_in_range(struct btrfs_block_group *bg, u64 bytenr,
+                                   u64 size);
 void btrfs_block_group_disarm_stripe_unusable(struct btrfs_block_group *block_group);
 void btrfs_stripe_unmanaged_sync(struct btrfs_block_group *block_group);
 void btrfs_block_group_rescan_stripe_unusable(struct btrfs_block_group *block_group);
index 66f0d5f5aa403c3f1772b8937fd418465151d00a..d8085ab0384eaf79a5d10058c455738c15dd104e 100644 (file)
@@ -1357,6 +1357,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
        u32 min_alloc_size;
        u32 blocksize = fs_info->sectorsize;
        u32 cur_alloc_size = 0;
+       u64 nr_allocated = 0;
        struct btrfs_key ins;
        unsigned clear_bits;
        unsigned long page_ops;
@@ -1442,6 +1443,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
                }
                if (ret < 0)
                        goto out_unlock;
+               nr_allocated++;
 
                /* We should not allocate an extent larger than requested.*/
                ASSERT(cur_alloc_size <= num_bytes);
@@ -1509,6 +1511,15 @@ out_unlock:
                btrfs_qgroup_free_data(inode, NULL, start + cur_alloc_size,
                                       end - start - cur_alloc_size + 1, NULL);
        }
+       /*
+        * The range is dropped: under stripe_alloc, hold what it gives back
+        * out of write admission.  Not for relocation, whose data is never
+        * acknowledged to a writer and whose shortfall must not hold back
+        * user writes.
+        */
+       if (ret == -ENOSPC && !btrfs_is_data_reloc_root(root))
+               btrfs_stripe_quarantine_add(fs_info, end + 1 - orig_start,
+                                           nr_allocated);
        btrfs_err(fs_info,
 "%s failed, root=%llu inode=%llu start=%llu len=%llu cur_offset=%llu cur_alloc_size=%u: %pe",
                  __func__, btrfs_root_id(inode->root),
index b5320d2a9a589d5ca63186c24c629fb151739a6f..b6bbbf0237ec391b912d58cdb73596cb81406256 100644 (file)
@@ -728,6 +728,69 @@ static void remove_ticket(struct btrfs_space_info *space_info,
        spin_unlock(&ticket->lock);
 }
 
+/*
+ * Buffered writeback of @len bytes of delalloc failed with ENOSPC under
+ * stripe_alloc after @nr_extents of its extents were allocated, and the
+ * range is being dropped (cow_file_range()).  Quarantine what the drop gives
+ * back: the range's may_use and margin now, and the whole stripes its
+ * allocated extents claimed at the next commit's rescan.  Each extent claims
+ * at most its length rounded up to its group's full stripe, so @len plus one
+ * unit per extent bounds both.  The quarantine never exceeds the space
+ * info's size.
+ */
+void btrfs_stripe_quarantine_add(struct btrfs_fs_info *fs_info, u64 len,
+                                u64 nr_extents)
+{
+       struct btrfs_space_info *sinfo = fs_info->data_sinfo;
+       const u64 unit = READ_ONCE(fs_info->stripe_margin_unit);
+       u64 bytes;
+
+       if (!sinfo || !unit || !btrfs_test_opt(fs_info, STRIPE_ALLOC))
+               return;
+       bytes = len + nr_extents * unit;
+       spin_lock(&sinfo->lock);
+       sinfo->bytes_stripe_quarantine = min(sinfo->bytes_stripe_quarantine + bytes,
+                                            sinfo->total_bytes);
+       bytes = sinfo->bytes_stripe_quarantine;
+       spin_unlock(&sinfo->lock);
+       btrfs_info_rl(fs_info,
+"stripe_alloc: buffered writeback of %llu bytes failed with ENOSPC; %llu bytes held out of write admission until space is freed",
+                     len, bytes);
+}
+
+/*
+ * @claimable bytes of whole stripes came back for real: extents freed at a
+ * commit (unpin_extent_range()), or a new group (see below).  Release that
+ * much quarantine.  Caller holds space_info->lock.
+ */
+void btrfs_stripe_quarantine_free(struct btrfs_space_info *space_info,
+                                 u64 claimable)
+{
+       lockdep_assert_held(&space_info->lock);
+       space_info->bytes_stripe_quarantine -=
+               min(claimable, space_info->bytes_stripe_quarantine);
+}
+
+/*
+ * A data group just armed for stripe_alloc that the supply did not have
+ * when the quarantined writeback failed: a new chunk, or a group whose free
+ * space only now finished loading.  Not a group back from read-only: a
+ * failed relocation at the fill edge returns stripes the supply had before,
+ * and its read-only/read-write cycling would drain the quarantine.
+ */
+void btrfs_stripe_quarantine_new_group(struct btrfs_block_group *bg)
+{
+       struct btrfs_space_info *sinfo = bg->space_info;
+
+       if (!sinfo || !btrfs_is_stripe_alloc_bg(bg) ||
+           !READ_ONCE(sinfo->bytes_stripe_quarantine))
+               return;
+       spin_lock(&sinfo->lock);
+       if (bg->stripe_unusable_ready)
+               btrfs_stripe_quarantine_free(sinfo, bg->stripe_claimable);
+       spin_unlock(&sinfo->lock);
+}
+
 /*
  * The most whole stripes that @bytes of data can claim when writeback
  * allocates them: in a group of full stripe f, an allocation claims
@@ -830,6 +893,19 @@ static bool stripe_claimable_admit(struct btrfs_space_info *space_info, u64 byte
                 * there.
                 */
                bytes += space_info->bytes_stripe_margin;
+               /*
+                * When buffered writeback fails with ENOSPC, its range is
+                * dropped and gives back its may_use, its margin and, at the
+                * next commit's rescan, the stripes its extents claimed.  The
+                * supply that admitted it was not enough, so that give-back
+                * is not supply: counted as supply, it admits the next writes
+                * (the ticket waiting on that very flush first), and their
+                * writeback drops them the same way.  Hold it out until real
+                * frees or new groups bring claimable stripes back.
+                * Relocation is what frees them, so it is not held back.
+                */
+               if (!reloc)
+                       bytes += space_info->bytes_stripe_quarantine;
                return space_info->bytes_may_use + bytes <= supply;
        }
 }
index 760104a6752b79f109bb33926f274baf69cba7c5..d141e258094d11fb5d13b255f96e24a8c751e5c0 100644 (file)
@@ -206,6 +206,12 @@ struct btrfs_space_info {
                                           from under an admitted reservation.
                                           Charged and released in lockstep with
                                           the inodes' outstanding_extents. */
+       u64 bytes_stripe_quarantine;    /* data: what buffered writeback that
+                                          failed with ENOSPC released, held
+                                          out of the admission supply until
+                                          real frees or new groups give
+                                          claimable stripes back (see
+                                          btrfs_stripe_quarantine_add()) */
 
        u64 max_extent_size;    /* This will hold the maximum extent size of
                                   the space info if we had an ENOSPC in the
@@ -378,6 +384,11 @@ void btrfs_stripe_margin_mod(struct btrfs_inode *inode, int mod);
 int btrfs_reserve_data_bytes_held(struct btrfs_space_info *space_info, u64 bytes,
                                  enum btrfs_reserve_flush_enum flush, u64 *held);
 void btrfs_stripe_prealloc_release(struct btrfs_fs_info *fs_info, u64 held);
+void btrfs_stripe_quarantine_add(struct btrfs_fs_info *fs_info, u64 len,
+                                u64 nr_extents);
+void btrfs_stripe_quarantine_free(struct btrfs_space_info *space_info,
+                                 u64 claimable);
+void btrfs_stripe_quarantine_new_group(struct btrfs_block_group *bg);
 void btrfs_add_bg_to_space_info(struct btrfs_fs_info *info,
                                struct btrfs_block_group *block_group);
 void btrfs_update_space_info_chunk_size(struct btrfs_space_info *space_info,
index f77bdecee174626974597bb13bfa52e8f78566ef..2097a0047151a7303945e1a196b3663b6457dc2f 100644 (file)
@@ -935,6 +935,7 @@ SPACE_INFO_ATTR(bytes_stripe_stranded);
 SPACE_INFO_ATTR(bytes_stripe_claimable);
 SPACE_INFO_ATTR(bytes_stripe_open);
 SPACE_INFO_ATTR(bytes_stripe_margin);
+SPACE_INFO_ATTR(bytes_stripe_quarantine);
 SPACE_INFO_ATTR(disk_used);
 SPACE_INFO_ATTR(disk_total);
 SPACE_INFO_ATTR(reclaim_count);
@@ -1069,6 +1070,7 @@ static struct attribute *space_info_attrs[] = {
        BTRFS_ATTR_PTR(space_info, bytes_stripe_claimable),
        BTRFS_ATTR_PTR(space_info, bytes_stripe_open),
        BTRFS_ATTR_PTR(space_info, bytes_stripe_margin),
+       BTRFS_ATTR_PTR(space_info, bytes_stripe_quarantine),
        BTRFS_ATTR_PTR(space_info, disk_used),
        BTRFS_ATTR_PTR(space_info, disk_total),
        BTRFS_ATTR_PTR(space_info, bg_reclaim_threshold),