]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: skip RAID5/6 groups that are mapped as RAID1/RAID1C3
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 10 Oct 2026 04:19:18 +0000 (00:19 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:37:54 +0000 (18:37 -0500)
Since commit 49ba3a3c0d67 ("btrfs: disguise single-data-RAID56 as
RAID1/RAID1C3"), a RAID5 chunk with one data stripe (two devices) or a
RAID6 chunk with one (three devices) keeps its RAID5/6 type on disk, in
the chunk map's on_disk_type and in the block group's flags, but is
mapped as RAID1/RAID1C3.  Such chunks appear when only two or three
devices have unallocated space, e.g. on devices of unequal size.

stripe_alloc and stripe_meta decide by the block group's flags, so they
kept opening stripe runs in these groups.  Their IO goes down the
mirrored path and never reaches raid56, so no write ever marked the
runs' sectors written: every ordered extent there ended with all its
sectors unwritten, and the groups paid stripe_alloc's per-allocation run
bookkeeping for a write hole they do not have.  (Nothing was trapped there:
their full stripe is one sector, since btrfs_full_stripe_len() reads the
chunk map's type.)

Record at block group creation whether the chunk map's type differs from
its on_disk_type in this way (BLOCK_GROUP_FLAG_RAID56_MIRRORED), and
leave such groups to the normal allocator.

Their flags still match the RAID5/6 allocation profile, so writes land in
them, and with two of five raid6 devices missing every new data chunk is
one: the admission supply must include them, or every degraded write fails
with ENOSPC.  Each space_info counts the free space of its writable
single-data-stripe RAID5/6 data groups, following ctl->free_space, which
every free space change goes through; btrfs_stripe_unmanaged_sync() runs
when a group joins its space_info and after each read-only transition, and
the read-only guard treats a counted group's free space as the supply it
takes away.  Groups of other profiles are not counted: a RAID5/6 write
cannot use them.  The full-stripe widths the admission charge is taken
over leave these groups out, as their full stripe is one sector.

[zygo: 7.3 port: 7.3's btrfs_add_bg_to_space_info() still takes fs_info;
7.3 still calls a chunk's device extent size stripe_size
(btrfs_calc_stripe_length()); context adjusted, the change itself is
unchanged]

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

index bd9209572479ad9016c832278024cd6b0515acf2..f27250895ec76ade05dadc2754b82348fd90b840 100644 (file)
@@ -3233,17 +3233,26 @@ static int inc_block_group_ro(struct btrfs_block_group *cache, bool force)
                 * the bytes already admitted would no longer be placeable:
                 * otherwise their writebacks fail and the data is dropped.
                 */
-               if (!ret && READ_ONCE(cache->stripe_unusable_ready)) {
+               if (!ret && (READ_ONCE(cache->stripe_unusable_ready) ||
+                            test_bit(BLOCK_GROUP_FLAG_UNMANAGED_COUNTED,
+                                     &cache->runtime_flags)) &&
+                   btrfs_test_opt(cache->fs_info, STRIPE_ALLOC) &&
+                   READ_ONCE(cache->fs_info->stripe_margin_unit)) {
                        /*
                         * stripe_claimable_mod() updates the group's value
                         * under the free-space tree lock and only then the
                         * aggregate under sinfo->lock, so a reader here can see
                         * the group ahead of the total: saturate, or the
                         * difference wraps and waves the transition through.
+                        * A group stripe_alloc does not manage supplies its
+                        * free space (bytes_unmanaged_free) instead.
                         */
-                       const u64 supply = sinfo->bytes_stripe_claimable;
+                       const u64 supply = sinfo->bytes_stripe_claimable +
+                               atomic64_read(&sinfo->bytes_unmanaged_free);
                        const u64 mine = min(supply,
-                                            READ_ONCE(cache->stripe_claimable));
+                               READ_ONCE(cache->stripe_unusable_ready) ?
+                               READ_ONCE(cache->stripe_claimable) :
+                               READ_ONCE(cache->free_space_ctl->free_space));
 
                        if (sinfo->bytes_may_use + sinfo->bytes_stripe_margin >
                            supply - mine)
@@ -3280,8 +3289,10 @@ out:
         * contribution so it is not subtracted twice.  Idempotent, so repeated
         * ro increments are harmless.
         */
-       if (!ret)
+       if (!ret) {
                btrfs_block_group_disarm_stripe_unusable(cache);
+               btrfs_stripe_unmanaged_sync(cache);
+       }
        if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) {
                btrfs_info(cache->fs_info,
                        "unable to make block group %llu ro", cache->start);
@@ -4309,6 +4320,8 @@ static struct btrfs_block_group *btrfs_create_block_group(
 
        cache->fs_info = fs_info;
        cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
+       if (btrfs_chunk_raid56_mirrored(fs_info, start))
+               set_bit(BLOCK_GROUP_FLAG_RAID56_MIRRORED, &cache->runtime_flags);
        cache->max_claimable_run = (u64)-1;
 
        cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED;
@@ -5196,8 +5209,10 @@ void btrfs_dec_block_group_ro(struct btrfs_block_group *cache)
         * the current free space (it may have changed while read-only).  The
         * scan re-adds this group's trapped free space to the space_info total.
         */
-       if (became_rw)
+       if (became_rw) {
                btrfs_block_group_init_stripe_unusable(cache);
+               btrfs_stripe_unmanaged_sync(cache);
+       }
 }
 
 static int update_block_group_item(struct btrfs_trans_handle *trans,
index 5aa929a0d56fb12a2c8da1e1bf5faf565f951c6a..2d6bff8c008314e0a32ebeb67ce191bb2bbf2eb1 100644 (file)
@@ -130,6 +130,18 @@ enum btrfs_block_group_flags {
         * counter must be rescanned at commit (stripe_alloc only).
         */
        BLOCK_GROUP_FLAG_STRIPE_UNUSABLE_DIRTY,
+       /*
+        * A RAID5/6 group with a single data stripe, which is mapped as
+        * RAID1/RAID1C3 (its chunk map's type differs from on_disk_type).  Its
+        * IO never reaches raid56, so stripe_alloc and stripe_meta skip it.
+        */
+       BLOCK_GROUP_FLAG_RAID56_MIRRORED,
+       /*
+        * This group's free space is in its space_info's
+        * bytes_unmanaged_free (see btrfs_stripe_unmanaged_sync()).  Set and
+        * cleared under the free-space ctl's tree_lock.
+        */
+       BLOCK_GROUP_FLAG_UNMANAGED_COUNTED,
 };
 
 enum btrfs_caching_type {
@@ -396,6 +408,21 @@ static inline u64 btrfs_block_group_end(const struct btrfs_block_group *block_gr
        return (block_group->start + block_group->length);
 }
 
+/*
+ * A RAID5/6 data group stripe_alloc does not manage because it has a single
+ * data stripe and is mapped as RAID1/RAID1C3.  Its flags still match the
+ * RAID5/6 allocation profile, so writes stripe_alloc admits can land there,
+ * and the admission has to count its free space.  Groups of other profiles
+ * are left out: a write allocated for a RAID5/6 profile cannot use them.
+ */
+static inline bool btrfs_stripe_bg_unmanaged(const struct btrfs_block_group *bg)
+{
+       return (bg->flags & BTRFS_BLOCK_GROUP_DATA) &&
+              !(bg->flags & BTRFS_BLOCK_GROUP_METADATA) &&
+              (bg->flags & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
+              test_bit(BLOCK_GROUP_FLAG_RAID56_MIRRORED, &bg->runtime_flags);
+}
+
 static inline bool btrfs_is_block_group_used(const struct btrfs_block_group *bg)
 {
        lockdep_assert_held(&bg->lock);
index 6f7f0929c133a91566c0ec05fce3ea6b6b40d8e6..89ef13b713f280f6eb9433170d9a13e67fbe21f2 100644 (file)
@@ -4201,14 +4201,16 @@ out:
  * closes the raid56 write hole; see "Open stripe runs" in block-group.c.
  * Metadata is covered by btrfs_is_stripe_meta_bg().  Mixed groups are
  * excluded: stripe_alloc refuses to enable on them
- * (btrfs_stripe_alloc_check_support()).
+ * (btrfs_stripe_alloc_check_support()).  So are groups with a single data
+ * stripe: they are mapped as RAID1/RAID1C3 and have no write hole.
  */
 bool btrfs_is_stripe_alloc_bg(const struct btrfs_block_group *bg)
 {
        return btrfs_test_opt(bg->fs_info, STRIPE_ALLOC) &&
               (bg->flags & BTRFS_BLOCK_GROUP_DATA) &&
               !(bg->flags & BTRFS_BLOCK_GROUP_METADATA) &&
-              (bg->flags & BTRFS_BLOCK_GROUP_RAID56_MASK);
+              (bg->flags & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
+              !test_bit(BLOCK_GROUP_FLAG_RAID56_MIRRORED, &bg->runtime_flags);
 }
 
 /*
index 7f0a206f65dd9795ff9bb8341c494b348605f930..481f62011baa4f4dd25ade63667b3b745f1f1a1f 100644 (file)
@@ -1807,6 +1807,19 @@ tree_search_offset(struct btrfs_free_space_ctl *ctl,
        return entry;
 }
 
+/*
+ * Change the group's free-space total, and with it the space_info's
+ * bytes_unmanaged_free while the group is counted there.  Under tree_lock.
+ */
+static void free_space_stat_mod(struct btrfs_free_space_ctl *ctl, s64 delta)
+{
+       struct btrfs_block_group *bg = ctl->block_group;
+
+       ctl->free_space += delta;
+       if (bg && test_bit(BLOCK_GROUP_FLAG_UNMANAGED_COUNTED, &bg->runtime_flags))
+               atomic64_add(delta, &bg->space_info->bytes_unmanaged_free);
+}
+
 static inline void unlink_free_space(struct btrfs_free_space_ctl *ctl,
                                     struct btrfs_free_space *info,
                                     bool update_stat)
@@ -1823,7 +1836,7 @@ static inline void unlink_free_space(struct btrfs_free_space_ctl *ctl,
        }
 
        if (update_stat)
-               ctl->free_space -= info->bytes;
+               free_space_stat_mod(ctl, -(s64)info->bytes);
 }
 
 static int link_free_space(struct btrfs_free_space_ctl *ctl,
@@ -1845,7 +1858,7 @@ static int link_free_space(struct btrfs_free_space_ctl *ctl,
                ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes;
        }
 
-       ctl->free_space += info->bytes;
+       free_space_stat_mod(ctl, info->bytes);
        ctl->free_extents++;
        return ret;
 }
@@ -1902,7 +1915,7 @@ static inline void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
        }
 
        if (update_stat)
-               ctl->free_space -= bytes;
+               free_space_stat_mod(ctl, -(s64)bytes);
 }
 
 static void btrfs_bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
@@ -1926,7 +1939,7 @@ static void btrfs_bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
         */
        info->max_extent_size = 0;
        info->bytes += bytes;
-       ctl->free_space += bytes;
+       free_space_stat_mod(ctl, bytes);
 
        relink_bitmap_entry(ctl, info);
 
@@ -2749,7 +2762,7 @@ static int __btrfs_add_free_space_zoned(struct btrfs_block_group *block_group,
        to_unusable = size - to_free;
 
        spin_lock(&ctl->tree_lock);
-       ctl->free_space += to_free;
+       free_space_stat_mod(ctl, to_free);
        spin_unlock(&ctl->tree_lock);
        /*
         * If the block group is read-only, we should account freed space into
@@ -3491,6 +3504,32 @@ static void stripe_return_clusters(struct btrfs_block_group *bg)
        spin_unlock(&cluster->refill_lock);
 }
 
+/*
+ * Count a writable single-data-stripe RAID5/6 data group, which stripe_alloc
+ * does not manage, in its space_info's bytes_unmanaged_free, or stop counting
+ * it once it is read-only.  Called when the group joins its space_info and
+ * after every read-only transition; it re-reads ->ro, so of two racing
+ * transitions the later call settles the state.
+ */
+void btrfs_stripe_unmanaged_sync(struct btrfs_block_group *bg)
+{
+       struct btrfs_free_space_ctl *ctl = bg->free_space_ctl;
+       bool want;
+
+       if (!btrfs_stripe_bg_unmanaged(bg) || !bg->space_info)
+               return;
+
+       spin_lock(&ctl->tree_lock);
+       want = !READ_ONCE(bg->ro);
+       if (want && !test_and_set_bit(BLOCK_GROUP_FLAG_UNMANAGED_COUNTED,
+                                     &bg->runtime_flags))
+               atomic64_add(ctl->free_space, &bg->space_info->bytes_unmanaged_free);
+       else if (!want && test_and_clear_bit(BLOCK_GROUP_FLAG_UNMANAGED_COUNTED,
+                                            &bg->runtime_flags))
+               atomic64_sub(ctl->free_space, &bg->space_info->bytes_unmanaged_free);
+       spin_unlock(&ctl->tree_lock);
+}
+
 /*
  * Compute a stripe_alloc block group's initial trapped free space once its
  * free space cache is loaded, and enable incremental accounting from here on.
@@ -4247,7 +4286,7 @@ out:
        if (!btrfs_free_space_trimmed(entry))
                atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
 
-       ctl->free_space -= bytes;
+       free_space_stat_mod(ctl, -(s64)bytes);
        if (!entry->bitmap && !btrfs_free_space_trimmed(entry))
                ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes;
 
index 441973bbd99a93c10bfbb88c64d1e3231f1db601..92db8205cc4350deffb35c7126f7c898f6a7d2b4 100644 (file)
@@ -142,6 +142,7 @@ 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);
 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);
 void btrfs_dump_free_space(struct btrfs_block_group *block_group,
                           u64 bytes);
index e23934e298b27ed5d4403c227eb98094596afd6b..b5320d2a9a589d5ca63186c24c629fb151739a6f 100644 (file)
@@ -392,6 +392,25 @@ int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
        return ret;
 }
 
+/*
+ * Remember a raid56 data full-stripe width for stripe_whole_need().  The
+ * entries are published before the count, so a lockless reader never sees
+ * an unset entry.
+ */
+static void stripe_width_record(struct btrfs_fs_info *fs_info, u64 width)
+{
+       const int nr = fs_info->nr_stripe_widths;
+
+       if (nr > BTRFS_STRIPE_WIDTHS_MAX)
+               return;
+       for (int i = 0; i < nr; i++)
+               if (fs_info->stripe_widths[i] == width)
+                       return;
+       if (nr < BTRFS_STRIPE_WIDTHS_MAX)
+               WRITE_ONCE(fs_info->stripe_widths[nr], width);
+       smp_store_release(&fs_info->nr_stripe_widths, nr + 1);
+}
+
 void btrfs_add_bg_to_space_info(struct btrfs_fs_info *info,
                                struct btrfs_block_group *block_group)
 {
@@ -413,6 +432,17 @@ void btrfs_add_bg_to_space_info(struct btrfs_fs_info *info,
                           block_group->full_stripe_len);
 
        spin_lock(&space_info->lock);
+       /*
+        * A RAID5/6 group with one data stripe is mapped as RAID1/RAID1C3 and
+        * its full_stripe_len is a sector: not a stripe width (and not
+        * managed, see btrfs_stripe_bg_unmanaged()).
+        */
+       if ((block_group->flags & BTRFS_BLOCK_GROUP_DATA) &&
+           (block_group->flags & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
+           !test_bit(BLOCK_GROUP_FLAG_RAID56_MIRRORED,
+                     &block_group->runtime_flags))
+               stripe_width_record(block_group->fs_info,
+                                   block_group->full_stripe_len);
 
        if (!(block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED) ||
            block_group->identity_remap_count != 0) {
@@ -435,6 +465,8 @@ void btrfs_add_bg_to_space_info(struct btrfs_fs_info *info,
        down_write(&space_info->groups_sem);
        list_add_tail(&block_group->list, &space_info->block_groups[index]);
        up_write(&space_info->groups_sem);
+
+       btrfs_stripe_unmanaged_sync(block_group);
 }
 
 /*
@@ -754,7 +786,14 @@ static bool stripe_claimable_admit(struct btrfs_space_info *space_info, u64 byte
         * runs can lose at the next commit.
         */
        {
-               u64 supply = space_info->bytes_stripe_claimable;
+               /*
+                * Single-data-stripe RAID5/6 groups, which stripe_alloc does
+                * not manage, take these writes too: count their free space,
+                * or a degraded filesystem, where every new chunk is one,
+                * refuses every write.
+                */
+               u64 supply = space_info->bytes_stripe_claimable +
+                            atomic64_read(&space_info->bytes_unmanaged_free);
                const u64 dedicated = space_info->bytes_stripe_claimable_reloc;
 
                if (reloc) {
index 82eeb090785b7edaec6fc9819d3262ca49d858f7..760104a6752b79f109bb33926f274baf69cba7c5 100644 (file)
@@ -182,6 +182,12 @@ struct btrfs_space_info {
                                           measured claimable whole-stripe
                                           bytes; see stripe_claimable in
                                           struct btrfs_block_group */
+       atomic64_t bytes_unmanaged_free;        /* free bytes in writable
+                                          single-data-stripe RAID5/6 data
+                                          groups, which stripe_alloc does not
+                                          manage (btrfs_stripe_bg_unmanaged());
+                                          write admission counts them with the
+                                          claimable stripes */
        u64 bytes_stripe_open;          /* sum of open stripe runs' unallocated
                                           remainders.  Claimed out of the free
                                           space cache but not yet allocated;
index 85ea9c5d4536c8a6357977b543bc1409ad77cfcf..0b10487182e5caca53255aaafbefe6eca1e9d2b2 100644 (file)
@@ -6547,6 +6547,27 @@ unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
        return len;
 }
 
+/*
+ * Is the chunk at @logical a RAID5/6 chunk with a single data stripe, mapped
+ * as RAID1/RAID1C3 (see set_real_chunk_type())?
+ */
+bool btrfs_chunk_raid56_mirrored(struct btrfs_fs_info *fs_info, u64 logical)
+{
+       struct btrfs_chunk_map *map;
+       bool ret = false;
+
+       if (!btrfs_fs_incompat(fs_info, RAID56))
+               return false;
+
+       map = btrfs_get_chunk_map(fs_info, logical, fs_info->sectorsize);
+       if (!WARN_ON(IS_ERR(map))) {
+               ret = (map->on_disk_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
+                     !(map->type & BTRFS_BLOCK_GROUP_RAID56_MASK);
+               btrfs_free_chunk_map(map);
+       }
+       return ret;
+}
+
 #ifdef CONFIG_BTRFS_EXPERIMENTAL
 static int btrfs_read_preferred(struct btrfs_chunk_map *map, int first, int num_stripes)
 {
index 0415d74cad9ba9fd9d5c2a9a4d0bb6c2015ecc2d..e2557ac93a3d732bef58481fd2469f2e32aa39f5 100644 (file)
@@ -801,6 +801,7 @@ void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev,
                                      bool allow_freeze);
 unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
                                    u64 logical);
+bool btrfs_chunk_raid56_mirrored(struct btrfs_fs_info *fs_info, u64 logical);
 u64 btrfs_calc_stripe_length(const struct btrfs_chunk_map *map);
 int btrfs_nr_parity_stripes(u64 type);
 int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans,