list_del(&run->list);
bitmap_free(run->live);
kfree(run);
+ /*
+ * Wholly free stripes inside this run's range were counted as trapped
+ * by the last stripe_unusable scan (btrfs_stripe_run_stranded_stripes());
+ * with the run gone they are claimable again, so have the next commit
+ * rescan the group and credit them back.
+ */
+ if (READ_ONCE(bg->stripe_unusable_ready))
+ set_bit(BLOCK_GROUP_FLAG_STRIPE_UNUSABLE_DIRTY, &bg->runtime_flags);
wake_up_var(&bg->open_stripe_runs);
}
return true;
}
+/*
+ * For the stripe_unusable scan: whole free stripes inside a live run's
+ * range cannot be claimed while the run exists
+ * (btrfs_stripe_run_range_usable()), so they must not count as claimable
+ * or the admission gate would admit writes that cannot be placed. Clear
+ * them from @freep and return their total; free_open_stripe_run() marks the
+ * group for rescan so they are credited back when the run goes. Caller
+ * holds ctl->tree_lock; stripe_run_lock nests inside it.
+ */
+u64 btrfs_stripe_run_stranded_stripes(struct btrfs_block_group *bg, u32 *freep,
+ u64 nstripes)
+{
+ const u64 fsl = bg->full_stripe_len;
+ struct btrfs_open_stripe_run *run;
+ unsigned long flags;
+ u64 stranded = 0;
+
+ spin_lock_irqsave(&bg->stripe_run_lock, flags);
+ list_for_each_entry(run, &bg->open_stripe_runs, list) {
+ u64 idx, last;
+
+ if (run->end <= run->start)
+ continue;
+ idx = div64_u64(run->start - bg->start, fsl);
+ last = div64_u64(run->end - 1 - bg->start, fsl);
+ for (; idx <= last && idx < nstripes; idx++) {
+ if (freep[idx] != fsl)
+ continue;
+ freep[idx] = 0;
+ stranded += fsl;
+ }
+ }
+ spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+ return stranded;
+}
+
/*
* Is the full stripe at @stripe inside a live run's range, i.e. stranded by
* the rule of btrfs_stripe_run_stranded_stripes()? Caller holds
u64 total = 0;
u64 total_claimable = 0;
u64 total_claimable_reloc = 0;
+ u64 total_stranded = 0;
int raid;
if (!(sinfo->flags & BTRFS_BLOCK_GROUP_DATA))
*/
if (force || dirty)
btrfs_block_group_rescan_stripe_unusable(bg);
- /*
- * Sum the authoritative per-group values into the
- * space_info total so it cannot drift from repeated
- * arm/disarm/rescan deltas. Disarmed (read-only)
- * groups are excluded; their free space is already
- * accounted as read-only.
- */
- if (READ_ONCE(bg->stripe_unusable_ready)) {
- total += READ_ONCE(bg->stripe_unusable);
- total_claimable +=
- READ_ONCE(bg->stripe_claimable);
+ }
+ }
+
+ /*
+ * Sum the authoritative per-group values into the space_info
+ * total so it cannot drift from repeated arm/disarm/rescan
+ * deltas. Disarmed (read-only) groups are excluded; their
+ * free space is already accounted as read-only. Every update
+ * of a group's values also updates the total under
+ * sinfo->lock, so summing under it gives totals that match
+ * the groups: claims, arms and disarms that race with the
+ * scan are neither lost nor counted twice.
+ */
+ spin_lock(&sinfo->lock);
+ for (raid = 0; raid < BTRFS_NR_RAID_TYPES; raid++) {
+ struct btrfs_block_group *bg;
+
+ if (!(btrfs_raid_array[raid].bg_flag &
+ BTRFS_BLOCK_GROUP_RAID56_MASK))
+ continue;
+ list_for_each_entry(bg, &sinfo->block_groups[raid],
+ list) {
+ if (bg->stripe_unusable_ready) {
+ total += bg->stripe_unusable;
+ total_stranded += READ_ONCE(bg->stripe_stranded);
+ total_claimable += bg->stripe_claimable;
if (bg->start == READ_ONCE(fs_info->data_reloc_bg))
total_claimable_reloc +=
- READ_ONCE(bg->stripe_claimable);
+ bg->stripe_claimable;
}
}
}
up_read(&sinfo->groups_sem);
- spin_lock(&sinfo->lock);
sinfo->bytes_stripe_unusable = total;
sinfo->bytes_stripe_claimable = total_claimable;
sinfo->bytes_stripe_claimable_reloc = total_claimable_reloc;
+ sinfo->bytes_stripe_stranded = total_stranded;
/*
* The rescan usually lowers the counter (mid-transaction
* incremental adds overcount conservatively); admission
* free_space_ctl->tree_lock and space_info->lock.
*/
u64 stripe_unusable;
+ /*
+ * The part of stripe_unusable that is wholly free stripes lying inside
+ * live stripe-run ranges (stripe_alloc): the run was claimed over them,
+ * the extents allocated there were freed again, and the run object
+ * still exists, so btrfs_stripe_run_range_usable() refuses to claim
+ * them. Trapped for admission, but they come back by themselves when
+ * the run is freed, so reclaim leaves them out. Set by the same scan,
+ * under the same lock.
+ */
+ u64 stripe_stranded;
/*
* Directly measured claimable supply: bytes of fully free, aligned
* whole stripes in the free space cache -- exactly what
void btrfs_retire_block_group_stripes(struct btrfs_block_group *bg);
void btrfs_wake_open_stripe_waiters(struct btrfs_fs_info *fs_info);
bool btrfs_stripe_in_open_run(struct btrfs_fs_info *fs_info, u64 logical);
+u64 btrfs_stripe_run_stranded_stripes(struct btrfs_block_group *bg, u32 *freep,
+ u64 nstripes);
bool btrfs_stripe_run_strands(struct btrfs_block_group *bg, u64 stripe);
bool btrfs_stripe_run_range_usable(struct btrfs_block_group *bg,
u64 run_start, u64 *run_len);
* free space is never credited here (see add_free_space()), save a trim's
* return (stripe_trim_claimable()); the commit rescan restores it. Caller
* holds ctl->tree_lock.
+ *
+ * The group's value and the space_info total change in one sinfo->lock
+ * section, as everywhere else, so that btrfs_scan_stripe_unusable(), which
+ * sums the groups under that lock, never sees one without the other.
*/
static void stripe_claimable_mod(struct btrfs_block_group *bg, s64 delta)
{
if (!READ_ONCE(bg->stripe_unusable_ready) || !delta)
return;
+ if (!sinfo) {
+ if (delta < 0 && bg->stripe_claimable < (u64)-delta)
+ delta = -(s64)bg->stripe_claimable;
+ bg->stripe_claimable += delta;
+ return;
+ }
+ spin_lock(&sinfo->lock);
if (delta < 0 && bg->stripe_claimable < (u64)-delta)
delta = -(s64)bg->stripe_claimable;
- if (!delta)
- return;
bg->stripe_claimable += delta;
- if (!sinfo)
- return;
- spin_lock(&sinfo->lock);
if (delta < 0 && sinfo->bytes_stripe_claimable < (u64)-delta)
sinfo->bytes_stripe_claimable = 0;
else
"stripe_unusable_scan counted=%llu != free_space=%llu",
counted, ctl->free_space);
+ /*
+ * Whole stripes stranded behind live stripe runs are free but not
+ * claimable until their run is freed: trapped, like a partial stripe's
+ * free bytes, so the admission gate does not admit writes against them.
+ */
+ bg->stripe_stranded = btrfs_stripe_run_stranded_stripes(bg, freep, nstripes);
+ trapped = bg->stripe_stranded;
for (i = 0; i < nstripes; i++)
if (freep[i] && freep[i] < fsl)
trapped += freep[i];
return;
}
trapped = stripe_unusable_scan(bg, freep, nstripes);
- bg->stripe_unusable = trapped;
/* Every free byte is claimable or trapped; the scan split them. */
- bg->stripe_claimable = ctl->free_space - trapped;
- claimable = bg->stripe_claimable;
+ claimable = ctl->free_space - trapped;
+ /* Group and total together; see stripe_claimable_mod(). */
+ spin_lock(&bg->space_info->lock);
+ bg->stripe_unusable = trapped;
+ bg->stripe_claimable = claimable;
bg->stripe_unusable_ready = true;
+ btrfs_space_info_update_bytes_stripe_unusable(bg->space_info, trapped);
+ btrfs_space_info_update_bytes_stripe_claimable(bg->space_info,
+ claimable);
+ spin_unlock(&bg->space_info->lock);
spin_unlock(&ctl->tree_lock);
kvfree(freep);
-
- if (trapped || claimable) {
- spin_lock(&bg->space_info->lock);
- btrfs_space_info_update_bytes_stripe_unusable(bg->space_info,
- trapped);
- btrfs_space_info_update_bytes_stripe_claimable(bg->space_info,
- claimable);
- spin_unlock(&bg->space_info->lock);
- }
}
/*
spin_unlock(&ctl->tree_lock);
return;
}
+ /* Group and total together; see stripe_claimable_mod(). */
+ spin_lock(&bg->space_info->lock);
trapped = bg->stripe_unusable;
claimable = bg->stripe_claimable;
bg->stripe_claimable = 0;
bg->stripe_unusable_ready = false;
+ btrfs_space_info_update_bytes_stripe_unusable(bg->space_info,
+ -(s64)trapped);
+ btrfs_space_info_update_bytes_stripe_claimable(bg->space_info,
+ -(s64)claimable);
+ if (bg->start == READ_ONCE(bg->fs_info->data_reloc_bg))
+ bg->space_info->bytes_stripe_claimable_reloc = 0;
+ spin_unlock(&bg->space_info->lock);
spin_unlock(&ctl->tree_lock);
-
- if (trapped || claimable) {
- spin_lock(&bg->space_info->lock);
- btrfs_space_info_update_bytes_stripe_unusable(bg->space_info,
- -(s64)trapped);
- btrfs_space_info_update_bytes_stripe_claimable(bg->space_info,
- -(s64)claimable);
- if (bg->start == READ_ONCE(bg->fs_info->data_reloc_bg))
- bg->space_info->bytes_stripe_claimable_reloc = 0;
- spin_unlock(&bg->space_info->lock);
- }
}
stripe_return_clusters(bg);
spin_lock(&ctl->tree_lock);
if (bg->stripe_unusable_ready) { /* skip if disarmed (read-only) */
- u64 scanned;
+ const u64 trapped = stripe_unusable_scan(bg, freep, nstripes);
+ const u64 scanned = ctl->free_space - trapped;
- bg->stripe_unusable = stripe_unusable_scan(bg, freep, nstripes);
- scanned = ctl->free_space - bg->stripe_unusable;
/*
* The incremental rules may only under-count claimable
* supply; more than the scan finds means a missed debit,
WARN_RATELIMIT(bg->stripe_claimable > scanned,
"bg %llu incremental stripe_claimable %llu above scanned %llu",
bg->start, bg->stripe_claimable, scanned);
+ /*
+ * Under sinfo->lock so btrfs_scan_stripe_unusable() sums
+ * both new values or both old ones.
+ */
+ spin_lock(&bg->space_info->lock);
+ bg->stripe_unusable = trapped;
bg->stripe_claimable = scanned;
+ spin_unlock(&bg->space_info->lock);
}
spin_unlock(&ctl->tree_lock);
kvfree(freep);
stripes stranded behind live runs.
Counted as used, and subtracted in
statfs. */
+ u64 bytes_stripe_stranded; /* the part of bytes_stripe_unusable that
+ is whole stripes stranded behind live
+ stripe runs (see the block group's
+ stripe_stranded); back to claimable when
+ the runs are freed */
u64 bytes_stripe_claimable_reloc; /* the part of bytes_stripe_claimable
in the group dedicated to data
relocation (fs_info->data_reloc_bg):
SPACE_INFO_ATTR(bytes_readonly);
SPACE_INFO_ATTR(bytes_zone_unusable);
SPACE_INFO_ATTR(bytes_stripe_unusable);
+SPACE_INFO_ATTR(bytes_stripe_stranded);
SPACE_INFO_ATTR(bytes_stripe_claimable);
SPACE_INFO_ATTR(bytes_stripe_open);
SPACE_INFO_ATTR(bytes_stripe_margin);
BTRFS_ATTR_PTR(space_info, bytes_readonly),
BTRFS_ATTR_PTR(space_info, bytes_zone_unusable),
BTRFS_ATTR_PTR(space_info, bytes_stripe_unusable),
+ BTRFS_ATTR_PTR(space_info, bytes_stripe_stranded),
BTRFS_ATTR_PTR(space_info, bytes_stripe_claimable),
BTRFS_ATTR_PTR(space_info, bytes_stripe_open),
BTRFS_ATTR_PTR(space_info, bytes_stripe_margin),