]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: log the space counters when data writeback hits ENOSPC
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 25 Oct 2026 08:48:50 +0000 (04:48 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:16:30 +0000 (21:16 -0500)
A data writeback allocation that fails with ENOSPC under stripe_alloc
drops the buffered data in its range.  The reservation margin is meant to
make that impossible, so the warning is the only evidence that admission
and the allocator disagreed, and today it names only the allocation size.
A drop seen once in a long test cannot be explained afterwards.

Log what admission decided on in the same line: the whole-stripe supply
(claimable, the part dedicated to relocation, unmanaged free space), what
is held against it (may_use, the outstanding extents' margin), the open,
trapped and stranded bytes, reserved and total, and the margin unit.  The
"despite reservation margin" wording is kept, so existing log scanners
still match it.

On CONFIG_BTRFS_DEBUG builds, also log one line per block group with its
full stripe length, claimable bytes, largest known claimable run, open
remainder and trapped and stranded bytes.  Unlike btrfs_dump_space_info()
this walks no free space entries.  It takes groups_sem for read and each
group's lock, as the ENOSPC_DEBUG dump a few lines above already does at
this call site, so it adds no lock ordering.

The line and the walk share one rate limit, so a burst of failures cannot
flood the log with group lines.

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/extent-tree.c
fs/btrfs/space-info.c
fs/btrfs/space-info.h

index cd945076a6d7934b694f72744f2ccab8fca3980b..2fd40907cac6224864e16b40a5ea3d201a46c1f3 100644 (file)
@@ -5191,12 +5191,36 @@ again:
                 * The pessimistic reservation margin should make a data
                 * writeback allocation failure impossible: admission held
                 * back a full stripe per delalloc extent.  If it happens
-                * anyway the pages are dropped, so say so loudly.
+                * anyway the pages are dropped, so say so loudly, with the
+                * counters admission decided on (and, on debug builds, every
+                * group's view of its stripes): a drop seen once with nothing
+                * else logged cannot be explained afterwards.
                 */
-               if (is_data && delalloc && btrfs_test_opt(fs_info, STRIPE_ALLOC))
-                       btrfs_warn_rl(fs_info,
-       "stripe_alloc: data writeback allocation of %llu bytes returned ENOSPC despite reservation margin; buffered data in this range will be dropped",
-                                     num_bytes);
+               if (is_data && delalloc && btrfs_test_opt(fs_info, STRIPE_ALLOC)) {
+                       static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
+                                                     DEFAULT_RATELIMIT_BURST);
+                       struct btrfs_space_info *sinfo;
+
+                       sinfo = btrfs_find_space_info(fs_info, flags);
+                       if (sinfo && __ratelimit(&rs)) {
+                               btrfs_warn(fs_info,
+       "stripe_alloc: data writeback allocation of %llu bytes returned ENOSPC despite reservation margin; buffered data in this range will be dropped: claimable %llu claimable_reloc %llu open %llu trapped %llu stranded %llu may_use %llu margin %llu unmanaged_free %lld reserved %llu total %llu unit %llu",
+                                          num_bytes,
+                                          sinfo->bytes_stripe_claimable,
+                                          sinfo->bytes_stripe_claimable_reloc,
+                                          sinfo->bytes_stripe_open,
+                                          sinfo->bytes_stripe_unusable,
+                                          sinfo->bytes_stripe_stranded,
+                                          sinfo->bytes_may_use,
+                                          sinfo->bytes_stripe_margin,
+                                          (s64)atomic64_read(&sinfo->bytes_unmanaged_free),
+                                          sinfo->bytes_reserved,
+                                          sinfo->total_bytes,
+                                          READ_ONCE(fs_info->stripe_margin_unit));
+                               if (IS_ENABLED(CONFIG_BTRFS_DEBUG))
+                                       btrfs_dump_stripe_groups(sinfo);
+                       }
+               }
        }
 
        return ret;
index f5e83f636b56e60bdf9629f844a98ada20208a09..2d9010624bbc00647ca0b2df83124a0650f89886 100644 (file)
@@ -1036,6 +1036,45 @@ again:
        btrfs_info(fs_info, "%llu bytes available across all block groups", total_avail);
 }
 
+/*
+ * One line per block group of what stripe_alloc's allocator sees: the
+ * stripe width, the whole-stripe supply and the largest run a claim may
+ * find, the open remainder and the trapped and stranded bytes.  Cheap
+ * enough for a failed writeback allocation, unlike btrfs_dump_space_info()
+ * with its free space entries.
+ */
+void btrfs_dump_stripe_groups(struct btrfs_space_info *info)
+{
+       struct btrfs_fs_info *fs_info = info->fs_info;
+       const u64 reloc_bg = READ_ONCE(fs_info->data_reloc_bg);
+       struct btrfs_block_group *cache;
+       int index;
+
+       down_read(&info->groups_sem);
+       for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
+               list_for_each_entry(cache, &info->block_groups[index], list) {
+                       spin_lock(&cache->lock);
+                       btrfs_info(fs_info,
+"stripe group %llu len %llu fsl %lu used %llu reserved %llu pinned %llu claimable %llu max_run %lld open_rem %llu trapped %llu stranded %llu cached %d%s%s%s",
+                                  cache->start, cache->length,
+                                  cache->full_stripe_len, cache->used,
+                                  cache->reserved, cache->pinned,
+                                  READ_ONCE(cache->stripe_claimable),
+                                  (s64)READ_ONCE(cache->max_claimable_run),
+                                  READ_ONCE(cache->stripe_open_remainder),
+                                  READ_ONCE(cache->stripe_unusable_ready) ?
+                                          READ_ONCE(cache->stripe_unusable) : 0,
+                                  READ_ONCE(cache->stripe_stranded),
+                                  cache->cached,
+                                  cache->ro ? " ro" : "",
+                                  cache->start == reloc_bg ? " reloc" : "",
+                                  btrfs_is_stripe_alloc_bg(cache) ? "" : " unmanaged");
+                       spin_unlock(&cache->lock);
+               }
+       }
+       up_read(&info->groups_sem);
+}
+
 static inline u64 calc_reclaim_items_nr(const struct btrfs_fs_info *fs_info,
                                        u64 to_reclaim)
 {
index 3a8bda447183026190de625646ea8e34de6197d8..5e13008c688f597dbf16db0edba37e5fdc38d5a7 100644 (file)
@@ -393,6 +393,7 @@ struct btrfs_space_info *btrfs_find_space_info(const struct btrfs_fs_info *info,
 void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
 void btrfs_dump_space_info(struct btrfs_space_info *info, u64 bytes,
                           bool dump_block_groups);
+void btrfs_dump_stripe_groups(struct btrfs_space_info *info);
 int btrfs_reserve_metadata_bytes(struct btrfs_space_info *space_info,
                                 u64 orig_bytes,
                                 enum btrfs_reserve_flush_enum flush);