u64 reserved;
u64 old_total;
int ret = 0;
+ bool compact = false;
/*
* A stripe_alloc group's live extents can only move into
*/
if (btrfs_is_stripe_alloc_bg(bg)) {
u64 elsewhere = READ_ONCE(space_info->bytes_stripe_claimable);
+ u64 need = READ_ONCE(bg->used);
+ /* Compaction moves at most half of it. */
+ if (btrfs_stripe_bg_wants_compaction(bg))
+ need /= 2;
elsewhere -= min(elsewhere, READ_ONCE(bg->stripe_claimable));
- if (elsewhere < READ_ONCE(bg->used)) {
+ if (elsewhere < need) {
struct btrfs_trans_handle *trans;
trans = btrfs_join_transaction(fs_info->tree_root);
* the first place.
*/
if (!should_reclaim_block_group(bg, bg->length)) {
- spin_unlock(&bg->lock);
- spin_unlock(&space_info->lock);
- up_write(&space_info->groups_sem);
- return 0;
+ /*
+ * Too full to relocate whole. A stripe_alloc
+ * group whose free space is trapped in partially used stripes
+ * can still be compacted: only the extents in those stripes
+ * move.
+ */
+ compact = btrfs_stripe_bg_wants_compaction(bg);
+ if (!compact) {
+ spin_unlock(&bg->lock);
+ spin_unlock(&space_info->lock);
+ up_write(&space_info->groups_sem);
+ return 0;
+ }
}
spin_unlock(&bg->lock);
"reclaim: relocating block group %llu flags 0x%llx length %llu used %llu reserved %llu",
bg->start, bg->flags,
bg->length, used, reserved);
- ret = btrfs_relocate_chunk(fs_info, bg->start, false);
+ if (compact) {
+ bool compacted;
+
+ ret = btrfs_compact_chunk(fs_info, bg->start, &compacted);
+ /*
+ * Compacted, the group stays: drop our read-only hold. It
+ * gave back trapped space, not a chunk; what moved is counted
+ * in stripe_run_stats.
+ */
+ if (!ret && compacted) {
+ btrfs_dec_block_group_ro(bg);
+ used = 0;
+ reserved = 0;
+ }
+ } else {
+ ret = btrfs_relocate_chunk(fs_info, bg->start, false);
+ }
if (btrfs_is_zoned(fs_info) && ret == -EAGAIN) {
btrfs_dec_block_group_ro(bg);
btrfs_debug(fs_info, "deferring reclaim of chunk %llu", bg->start);
INIT_LIST_HEAD(&fs_info->caching_block_groups);
INIT_LIST_HEAD(&fs_info->open_stripe_bgs);
fs_info->stripe_park_timeout_ms = BTRFS_RBIO_PARK_TIMEOUT_MS;
+ fs_info->stripe_compact = 10;
fs_info->stripe_park_sync_timeout_ms = BTRFS_RBIO_PARK_SYNC_TIMEOUT_MS;
spin_lock_init(&fs_info->open_stripe_lock);
spin_lock_init(&fs_info->delalloc_root_lock);
}
/*
- * Total trapped free bytes (free space in partially filled stripes) over the
- * whole block group -- the authoritative definition of stripe_unusable,
- * recomputed at cache-in and after each commit that changed the group's free
- * space. Every free byte is accumulated into a per-stripe array (so a stripe
- * whose total free is neither zero nor a whole stripe is partially filled, and
- * its free bytes are trapped). @freep is a zeroed array of stripe_unusable_
- * nstripes() u32 entries the caller allocates before taking the tree lock (it
- * can be large); this runs with ctl->tree_lock held.
+ * Accumulate every free byte of @bg into its stripe's slot of @freep (a zeroed
+ * array of stripe_unusable_nstripes() entries). Returns the bytes counted.
+ * Caller holds ctl->tree_lock.
*/
-static u64 stripe_unusable_scan(struct btrfs_block_group *bg, u32 *freep,
- u64 nstripes)
+static u64 stripe_free_walk(struct btrfs_block_group *bg, u32 *freep)
{
struct btrfs_free_space_ctl *ctl = bg->free_space_ctl;
const u32 unit = bg->fs_info->sectorsize;
const u64 fsl = bg->full_stripe_len;
const u64 bg_start = bg->start;
u64 counted = 0;
- u64 trapped = 0;
struct rb_node *n;
- u64 i;
lockdep_assert_held(&ctl->tree_lock);
e->offset + e->bytes, &counted);
}
}
+ return counted;
+}
+
+/*
+ * The authoritative stripe_unusable of the block group: free bytes in
+ * partially filled stripes, plus whole free stripes stranded behind live
+ * runs. Recomputed at cache-in and after each commit that changed the
+ * group's free space. @freep is a zeroed array of
+ * stripe_unusable_nstripes() entries, allocated by the caller before taking
+ * ctl->tree_lock, which is held here.
+ */
+static u64 stripe_unusable_scan(struct btrfs_block_group *bg, u32 *freep,
+ u64 nstripes)
+{
+ struct btrfs_free_space_ctl *ctl = bg->free_space_ctl;
+ const u64 fsl = bg->full_stripe_len;
+ u64 counted;
+ u64 trapped = 0;
+ u64 i;
+
+ counted = stripe_free_walk(bg, freep);
/* Every free byte was distributed to exactly one stripe (debug check). */
ASSERT(counted == ctl->free_space,
return trapped;
}
+/*
+ * For compaction: set in @map (@nstripes bits, zeroed by the caller) every
+ * stripe of @bg that holds free space but not a whole stripe of it. Returns
+ * how many, or a negative errno.
+ */
+s64 btrfs_stripe_partial_map(struct btrfs_block_group *bg, unsigned long *map,
+ u64 nstripes)
+{
+ struct btrfs_free_space_ctl *ctl = bg->free_space_ctl;
+ const u64 fsl = bg->full_stripe_len;
+ s64 found = 0;
+ u32 *freep;
+ u64 i;
+
+ if (nstripes != stripe_unusable_nstripes(bg))
+ return -EINVAL;
+ freep = kvcalloc(nstripes, sizeof(*freep), GFP_NOFS);
+ if (!freep)
+ return -ENOMEM;
+ spin_lock(&ctl->tree_lock);
+ stripe_free_walk(bg, freep);
+ spin_unlock(&ctl->tree_lock);
+ for (i = 0; i < nstripes; i++) {
+ if (freep[i] && freep[i] < fsl) {
+ set_bit(i, map);
+ found++;
+ }
+ }
+ kvfree(freep);
+ return found;
+}
+
/*
* The legacy allocator parks a group's free space in an allocation cluster
* (for a metadata group, its whole initial extent), where the group's own
/*
- * Does this stripe_alloc group want the reclaim worker? Like
- * zoned's zone_unusable reclaim: once enough of the group's FREE space is
- * trapped in partially used stripes (zoned compares with the capacity, but a
- * stripe group also holds claimable whole stripes), relocation packs the live
- * extents into whole stripes elsewhere and frees the group. Only groups
- * whose live data is below the same threshold are cheap enough to move, and
- * only when that data fits in the claimable supply of the OTHER groups:
- * relocation needs whole stripes too, and a group queued while there is
- * nowhere to move it starves writers with read-only transitions and fails
- * each time at the fill edge. The worker re-checks with
- * should_reclaim_block_group(). Reads the counters without locks; the
- * commit-time rescan calls it with fresh values, the cleaner's periodic
- * re-evaluation with slightly stale ones, and either error is caught by
- * the worker.
+ * Does this stripe_alloc group want the reclaim worker? As
+ * with zoned's zone_unusable, reclaim once enough of the group's free space
+ * is trapped. A group whose live data is below the threshold is relocated
+ * whole; a fuller one is compacted (btrfs_compact_chunk()), moving only the
+ * extents in its partial stripes. Either way, only when the data fits in
+ * the other groups' claimable supply: relocation needs whole stripes too,
+ * and queueing a group with nowhere to move it only costs writers read-only
+ * transitions. Counters are read unlocked; the worker re-checks.
*/
-bool btrfs_stripe_bg_wants_reclaim(struct btrfs_block_group *bg)
+enum stripe_reclaim_kind {
+ STRIPE_RECLAIM_NONE,
+ STRIPE_RECLAIM_RELOCATE,
+ STRIPE_RECLAIM_COMPACT,
+};
+
+static enum stripe_reclaim_kind stripe_bg_reclaim_kind(struct btrfs_block_group *bg,
+ bool check_room)
{
struct btrfs_space_info *sinfo = bg->space_info;
const int thresh = READ_ONCE(sinfo->bg_reclaim_threshold);
* relocating the group would only force that.
*/
const u64 trapped = unusable - min(unusable, READ_ONCE(bg->stripe_stranded));
+ enum stripe_reclaim_kind kind = STRIPE_RECLAIM_RELOCATE;
+ u64 need = used;
u64 elsewhere;
u64 promised;
if (thresh <= 0 || bg->ro || !READ_ONCE(bg->stripe_unusable_ready))
- return false;
- if (trapped < mult_perc(bg->length - min(used, bg->length), thresh))
- return false;
- if (used >= mult_perc(bg->length, thresh))
- return false;
+ return STRIPE_RECLAIM_NONE;
+ if (used >= mult_perc(bg->length, thresh)) {
+ /*
+ * Too full to move whole: compact once trapped space reaches
+ * stripe_compact percent of the group's length. (A share of
+ * free space, as below, would stay small here: a mostly live
+ * group's little free space is mostly whole stripes.)
+ */
+ const u32 pct = READ_ONCE(bg->fs_info->stripe_compact);
+
+ if (!pct || trapped < mult_perc(bg->length, pct))
+ return STRIPE_RECLAIM_NONE;
+ kind = STRIPE_RECLAIM_COMPACT;
+ need = used / 2;
+ } else if (trapped < mult_perc(bg->length - min(used, bg->length),
+ thresh)) {
+ return STRIPE_RECLAIM_NONE;
+ }
+ if (!check_room)
+ return kind;
elsewhere = READ_ONCE(sinfo->bytes_stripe_claimable);
promised = READ_ONCE(sinfo->bytes_may_use) +
READ_ONCE(sinfo->bytes_stripe_margin);
elsewhere -= min(elsewhere, READ_ONCE(bg->stripe_claimable));
elsewhere -= min(elsewhere, promised);
- return elsewhere >= used;
+ return elsewhere >= need ? kind : STRIPE_RECLAIM_NONE;
+}
+
+bool btrfs_stripe_bg_wants_reclaim(struct btrfs_block_group *bg)
+{
+ return stripe_bg_reclaim_kind(bg, true) != STRIPE_RECLAIM_NONE;
+}
+
+/*
+ * For the reclaim worker, about a group it took: compact it rather than
+ * relocate it whole. Room is not re-checked; the worker makes room first,
+ * and relocation fails cleanly without it.
+ */
+bool btrfs_stripe_bg_wants_compaction(struct btrfs_block_group *bg)
+{
+ return stripe_bg_reclaim_kind(bg, false) == STRIPE_RECLAIM_COMPACT;
}
/*
void btrfs_stripe_unmanaged_sync(struct btrfs_block_group *block_group);
void btrfs_block_group_rescan_stripe_unusable(struct btrfs_block_group *block_group);
bool btrfs_stripe_bg_wants_reclaim(struct btrfs_block_group *bg);
+bool btrfs_stripe_bg_wants_compaction(struct btrfs_block_group *bg);
+s64 btrfs_stripe_partial_map(struct btrfs_block_group *bg, unsigned long *map,
+ u64 nstripes);
void btrfs_dump_free_space(struct btrfs_block_group *block_group,
u64 bytes);
int btrfs_find_space_cluster(struct btrfs_block_group *block_group,
/* Relocation runs retained past their drain, and released. */
atomic64_t reloc_run_retained;
atomic64_t reloc_run_released;
+ /* compaction passes that kept their group */
+ atomic64_t compact_runs;
+ /* compaction plans over the cap, relocated whole instead */
+ atomic64_t compact_fallbacks;
+ /* stripes evacuated by compaction (closure included) */
+ atomic64_t compact_stripes;
+ /* live bytes compaction moved */
+ atomic64_t compact_bytes;
} stripe_run_stats;
/* BTRFS_STRIPE_RMW_* masks; see btrfs_stripe_allow_rmw(). */
u32 stripe_rmw_prop;
+ /*
+ * Reclaim compacts a mostly live stripe_alloc group once
+ * this percent of its length is trapped; 0 = never (sysfs
+ * stripe_compact, default 10). See btrfs_compact_chunk().
+ */
+ u32 stripe_compact;
/*
* Park deadlines, ms (sysfs); 0 disables parking for that class. See
* rbio_try_park().
bool found_file_extent;
refcount_t refs;
+
+ /*
+ * Compaction: the stripes whose extents are relocated (compact_plan());
+ * NULL relocates the whole group.
+ */
+ unsigned long *compact_map;
+ u64 compact_fsl;
};
+/*
+ * Stripe compaction. A stripe_alloc group whose free space is
+ * trapped in partially used stripes, but whose live data mostly fills whole
+ * ones, does not need all of that data moved to get the space back: only the
+ * extents in the partial stripes, and -- since moving an extent frees its
+ * bytes in every stripe it spans -- the extents sharing a stripe with those,
+ * transitively. compact_plan() selects that closure, find_next_extent()
+ * skips every extent outside it, and the group is kept and made writable
+ * again instead of removed.
+ */
+static bool compact_selected(const struct reloc_control *rc, u64 start, u64 len)
+{
+ const struct btrfs_block_group *bg = rc->block_group;
+ u64 first, last;
+
+ if (!rc->compact_map)
+ return true;
+ if (start < bg->start || !len)
+ return false;
+ first = div64_u64(start - bg->start, rc->compact_fsl);
+ last = div64_u64(min(start + len, bg->start + bg->length) - 1 - bg->start,
+ rc->compact_fsl);
+ return find_next_bit(rc->compact_map, last + 1, first) <= last;
+}
+
static struct reloc_control *get_reloc_control(struct btrfs_fs_info *fs_info)
{
struct reloc_control *rc;
if (rc->block_group)
btrfs_put_block_group(rc->block_group);
+ bitmap_free(rc->compact_map);
kfree(rc);
}
}
num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
if (bytenr == 0)
continue;
+ /*
+ * Only the extents that moved have a new location: under
+ * compaction that is the selected ones, not every extent in
+ * the group.
+ */
if (!in_range(bytenr, rc->block_group->start,
- rc->block_group->length))
+ rc->block_group->length) ||
+ !compact_selected(rc, bytenr, num_bytes))
continue;
/*
else
rc->search_start = key.objectid +
fs_info->nodesize;
+ if (!compact_selected(rc, key.objectid,
+ rc->search_start - key.objectid)) {
+ path->slots[0]++;
+ goto next;
+ }
memcpy(extent_key, &key, sizeof(key));
return 0;
}
rc->extents_found, stage_to_string(finishes_stage));
}
- WARN_ON(rc->block_group->pinned > 0);
- WARN_ON(rc->block_group->reserved > 0);
- WARN_ON(rc->block_group->used > 0);
+ /* A compacted group keeps the extents it was not asked to move. */
+ if (!rc->compact_map) {
+ WARN_ON(rc->block_group->pinned > 0);
+ WARN_ON(rc->block_group->reserved > 0);
+ WARN_ON(rc->block_group->used > 0);
+ }
return 0;
}
+static u32 compact_root(u32 *parent, u32 x)
+{
+ while (parent[x] != x) {
+ parent[x] = parent[parent[x]];
+ x = parent[x];
+ }
+ return x;
+}
+
+/*
+ * Select the stripes to evacuate: the partially used ones, and every stripe
+ * joined to one of them through an extent spanning both. A union-find over
+ * the group's stripes, in one pass over its extent items in the commit root,
+ * each union carrying the live bytes of its extents. Sets rc->compact_map;
+ * returns the live bytes the selection moves in @moved and the number of
+ * stripes selected in @nsel.
+ */
+static int compact_plan(struct reloc_control *rc, u64 *moved, u64 *nsel)
+{
+ struct btrfs_block_group *bg = rc->block_group;
+ const u64 fsl = bg->full_stripe_len;
+ const u64 end = bg->start + bg->length;
+ BTRFS_PATH_AUTO_FREE(path);
+ struct btrfs_key key;
+ unsigned long *map = NULL;
+ unsigned long *sel = NULL;
+ u32 *parent = NULL;
+ u64 *bytes = NULL;
+ u64 nstripes;
+ s64 npartial;
+ u64 i;
+ int ret;
+
+ *moved = 0;
+ *nsel = 0;
+ if (!fsl)
+ return -EINVAL;
+ nstripes = div64_u64(bg->length + fsl - 1, fsl);
+ if (nstripes > U32_MAX)
+ return -EINVAL;
+
+ path = btrfs_alloc_path();
+ map = bitmap_zalloc(nstripes, GFP_NOFS);
+ sel = bitmap_zalloc(nstripes, GFP_NOFS);
+ parent = kvmalloc_array(nstripes, sizeof(*parent), GFP_NOFS);
+ bytes = kvcalloc(nstripes, sizeof(*bytes), GFP_NOFS);
+ if (!path || !map || !sel || !parent || !bytes) {
+ ret = -ENOMEM;
+ goto out;
+ }
+ npartial = btrfs_stripe_partial_map(bg, map, nstripes);
+ if (npartial < 0) {
+ ret = npartial;
+ goto out;
+ }
+ for (i = 0; i < nstripes; i++)
+ parent[i] = i;
+
+ path->search_commit_root = true;
+ path->skip_locking = true;
+ path->need_commit_sem = true;
+ key.objectid = bg->start;
+ key.type = 0;
+ key.offset = 0;
+ ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
+ if (ret < 0)
+ goto out;
+ while (1) {
+ struct extent_buffer *leaf = path->nodes[0];
+ u64 len;
+ u32 a, b, r;
+
+ if (path->slots[0] >= btrfs_header_nritems(leaf)) {
+ ret = btrfs_next_leaf(rc->extent_root, path);
+ if (ret < 0)
+ goto out;
+ if (ret > 0)
+ break;
+ cond_resched();
+ continue;
+ }
+ btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
+ path->slots[0]++;
+ if (key.objectid >= end)
+ break;
+ if (key.type == BTRFS_EXTENT_ITEM_KEY)
+ len = key.offset;
+ else
+ continue;
+ if (key.objectid < bg->start || !len)
+ continue;
+ a = div64_u64(key.objectid - bg->start, fsl);
+ b = div64_u64(min(key.objectid + len, end) - 1 - bg->start, fsl);
+ r = compact_root(parent, a);
+ for (u32 s = a + 1; s <= b; s++) {
+ u32 t = compact_root(parent, s);
+
+ if (t != r) {
+ parent[t] = r;
+ bytes[r] += bytes[t];
+ }
+ }
+ bytes[r] += len;
+ }
+ ret = 0;
+
+ /* Select every union holding a partial stripe... */
+ for_each_set_bit(i, map, nstripes) {
+ u32 r = compact_root(parent, i);
+
+ if (!__test_and_set_bit(r, sel))
+ *moved += bytes[r];
+ }
+ /* ...and evacuate all of its stripes. */
+ for (i = 0; i < nstripes; i++) {
+ if (test_bit(compact_root(parent, i), sel)) {
+ __set_bit(i, map);
+ (*nsel)++;
+ }
+ }
+ rc->compact_map = map;
+ rc->compact_fsl = fsl;
+ map = NULL;
+out:
+ bitmap_free(map);
+ bitmap_free(sel);
+ kvfree(parent);
+ kvfree(bytes);
+ return ret;
+}
+
+/*
+ * Plan the compaction of a read-only group. Returns 1 to compact
+ * (rc->compact_map set), 0 to relocate the whole group instead -- the
+ * closure would move more than half its live bytes, most of the cost of
+ * moving all of them, which also gets the group's raw space back -- or a
+ * negative errno.
+ */
+static int compact_prepare(struct reloc_control *rc)
+{
+ struct btrfs_block_group *bg = rc->block_group;
+ struct btrfs_fs_info *fs_info = bg->fs_info;
+ struct btrfs_trans_handle *trans;
+ u64 moved, nsel, used;
+ int ret;
+
+ /*
+ * Settle first: what the group freed stays pinned until a commit, and
+ * the plan reads the free space cache and the commit root, which then
+ * agree.
+ */
+ trans = btrfs_join_transaction(rc->extent_root);
+ if (IS_ERR(trans))
+ return PTR_ERR(trans);
+ ret = btrfs_commit_transaction(trans);
+ if (ret)
+ return ret;
+ ret = btrfs_cache_block_group(bg, true);
+ if (ret)
+ return ret;
+ ret = compact_plan(rc, &moved, &nsel);
+ if (ret)
+ return ret;
+
+ spin_lock(&bg->lock);
+ used = bg->used;
+ spin_unlock(&bg->lock);
+ if (moved > used / 2) {
+ bitmap_free(rc->compact_map);
+ rc->compact_map = NULL;
+ atomic64_inc(&fs_info->stripe_run_stats.compact_fallbacks);
+ return 0;
+ }
+ atomic64_inc(&fs_info->stripe_run_stats.compact_runs);
+ atomic64_add(nsel, &fs_info->stripe_run_stats.compact_stripes);
+ atomic64_add(moved, &fs_info->stripe_run_stats.compact_bytes);
+ btrfs_info(fs_info,
+ "compacting block group %llu: %llu stripes, %llu of %llu live bytes",
+ bg->start, nsel, moved, used);
+ return 1;
+}
+
/*
* function to relocate all extents in a block group.
+ *
+ * @compact: NULL relocates the whole group. Pointing at true, the group is
+ * compacted if that moves at most half its live bytes and relocated whole
+ * otherwise; on return it says which. A compacted group is left writable.
*/
int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
- bool verbose)
+ bool verbose, bool *compact)
{
struct btrfs_block_group *bg;
struct btrfs_root *extent_root = btrfs_extent_root(fs_info, group_start);
struct btrfs_path *path = NULL;
int ret;
bool bg_is_ro = false;
+ bool compacting = false;
if (unlikely(!extent_root)) {
btrfs_err(fs_info,
ret = btrfs_zone_finish(rc->block_group);
WARN_ON(ret && ret != -EAGAIN);
+ /* Compaction is for the relocation that moves extents, not remapping. */
+ if (compact && *compact && should_relocate_using_remap_tree(bg))
+ *compact = false;
+ if (compact && *compact) {
+ ret = compact_prepare(rc);
+ if (ret < 0)
+ goto out;
+ compacting = ret;
+ *compact = compacting;
+ ret = 0;
+ }
+
if (should_relocate_using_remap_tree(bg)) {
if (bg->remap_bytes != 0) {
ret = move_existing_remaps(fs_info, bg, path);
ret = do_nonremap_reloc(fs_info, verbose, rc);
}
+ if (!ret && compacting) {
+ struct btrfs_trans_handle *trans;
+
+ /*
+ * The group stays. Commit, so what moved out of it is unpinned
+ * and its stripes are whole again before it is writable.
+ */
+ trans = btrfs_join_transaction(rc->extent_root);
+ if (IS_ERR(trans))
+ ret = PTR_ERR(trans);
+ else
+ ret = btrfs_commit_transaction(trans);
+ btrfs_dec_block_group_ro(rc->block_group);
+ bg_is_ro = false;
+ }
+
out:
if (ret && bg_is_ro)
btrfs_dec_block_group_ro(rc->block_group);
}
int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
- bool verbose);
+ bool verbose, bool *compact);
int btrfs_init_reloc_root(struct btrfs_trans_handle *trans, struct btrfs_root *root);
int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
struct btrfs_root *root);
"claim_refused_live_run %lld\n"
"strand_scan_bytes %lld\n"
"strand_rescan_marks %lld\n"
- "nocow_run_dead_closed %lld\n",
+ "nocow_run_dead_closed %lld\n"
+ "compact_runs %lld\n"
+ "compact_fallbacks %lld\n"
+ "compact_stripes %lld\n"
+ "compact_bytes %lld\n",
atomic64_read(&fs_info->stripe_run_stats.nocow_run_opened),
atomic64_read(&fs_info->stripe_run_stats.claim_refused_live_run),
atomic64_read(&fs_info->stripe_run_stats.strand_scan_bytes),
atomic64_read(&fs_info->stripe_run_stats.strand_rescan_marks),
- atomic64_read(&fs_info->stripe_run_stats.nocow_run_dead_closed));
+ atomic64_read(&fs_info->stripe_run_stats.nocow_run_dead_closed),
+ atomic64_read(&fs_info->stripe_run_stats.compact_runs),
+ atomic64_read(&fs_info->stripe_run_stats.compact_fallbacks),
+ atomic64_read(&fs_info->stripe_run_stats.compact_stripes),
+ atomic64_read(&fs_info->stripe_run_stats.compact_bytes));
}
BTRFS_ATTR(, stripe_run_stats, btrfs_stripe_run_stats_show);
BTRFS_PARK_TIMEOUT_ATTR(stripe_park_sync_timeout_ms, stripe_park_sync_timeout_ms);
BTRFS_PARK_TIMEOUT_ATTR(stripe_park_congestion, stripe_park_congestion);
+/*
+ * Reclaim compacts a mostly live stripe group once this percent of its length
+ * is trapped (btrfs_compact_chunk()); 0 turns compaction off.
+ */
+static ssize_t btrfs_stripe_compact_show(struct kobject *kobj,
+ struct kobj_attribute *a, char *buf)
+{
+ struct btrfs_fs_info *fs_info = to_fs_info(kobj);
+
+ return sysfs_emit(buf, "%u\n", READ_ONCE(fs_info->stripe_compact));
+}
+
+static ssize_t btrfs_stripe_compact_store(struct kobject *kobj,
+ struct kobj_attribute *a,
+ const char *buf, size_t len)
+{
+ struct btrfs_fs_info *fs_info = to_fs_info(kobj);
+ unsigned int val;
+ int ret;
+
+ ret = kstrtouint(buf, 10, &val);
+ if (ret)
+ return ret;
+ if (val > 100)
+ return -EINVAL;
+ WRITE_ONCE(fs_info->stripe_compact, val);
+ return len;
+}
+BTRFS_ATTR_RW(, stripe_compact, btrfs_stripe_compact_show,
+ btrfs_stripe_compact_store);
+
static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
BTRFS_ATTR_PTR(, stripe_park_timeout_ms),
BTRFS_ATTR_PTR(, stripe_park_sync_timeout_ms),
BTRFS_ATTR_PTR(, stripe_park_congestion),
+ BTRFS_ATTR_PTR(, stripe_compact),
BTRFS_ATTR_PTR(, temp_fsid),
NULL,
};
return ret;
}
-int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset, bool verbose)
+static int relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset,
+ bool verbose, bool *compact)
{
struct btrfs_block_group *block_group;
int ret;
/* step one, relocate all the extents inside this chunk */
btrfs_scrub_pause(fs_info);
- ret = btrfs_relocate_block_group(fs_info, chunk_offset, verbose);
+ ret = btrfs_relocate_block_group(fs_info, chunk_offset, verbose, compact);
btrfs_scrub_continue(fs_info);
if (ret) {
/*
return ret;
}
+ /* Compacted: the group keeps the rest of its live extents. */
+ if (compact && *compact)
+ return 0;
+
block_group = btrfs_lookup_block_group(fs_info, chunk_offset);
if (!block_group)
return -ENOENT;
return ret;
}
+int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset, bool verbose)
+{
+ return relocate_chunk(fs_info, chunk_offset, verbose, NULL);
+}
+
+/*
+ * Compact a stripe_alloc group: relocate only the extents in
+ * its partially used stripes (and those sharing a stripe with them) and keep
+ * it. When that would move more than half its live bytes the group is
+ * relocated and removed as by btrfs_relocate_chunk() instead; @compacted
+ * says which happened.
+ */
+int btrfs_compact_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset,
+ bool *compacted)
+{
+ *compacted = true;
+ return relocate_chunk(fs_info, chunk_offset, false, compacted);
+}
+
static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
{
struct btrfs_root *chunk_root = fs_info->chunk_root;
int btrfs_pause_balance(struct btrfs_fs_info *fs_info);
int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset,
bool verbose);
+int btrfs_compact_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset,
+ bool *compacted);
int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);
bool btrfs_chunk_writeable(struct btrfs_fs_info *fs_info, u64 chunk_offset);
void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index);