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
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;
* 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)) {
/* 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;
u64 empty_cluster = 0;
while (start <= end) {
+ u64 quarantine_back;
u64 len;
bool readonly;
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;
} 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);
}
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
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);
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;
}
if (ret < 0)
goto out_unlock;
+ nr_allocated++;
/* We should not allocate an extent larger than requested.*/
ASSERT(cur_alloc_size <= num_bytes);
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),
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
* 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;
}
}
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
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,
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);
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),