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
* 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)
* 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);
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;
* 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,
* 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 {
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);
* 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);
}
/*
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)
}
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,
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;
}
}
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,
*/
info->max_extent_size = 0;
info->bytes += bytes;
- ctl->free_space += bytes;
+ free_space_stat_mod(ctl, bytes);
relink_bitmap_entry(ctl, info);
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
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.
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;
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);
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)
{
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) {
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);
}
/*
* 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) {
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;
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)
{
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,