]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: compact mostly live groups instead of skipping them
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 29 Sep 2026 09:29:38 +0000 (05:29 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:37:59 +0000 (18:37 -0500)
Reclaim only takes a stripe_alloc group whose live data is below
bg_reclaim_threshold: moving everything out of a fuller group costs nearly
a group's worth of writes and needs that much claimable space elsewhere.
But trapped free space is not confined to emptyish groups.  Under steady
COW churn a group can stay mostly live while every commit leaves
partially used stripes behind.  Metadata under stripe_meta showed it
first: on a raid6 metadata fill the trapped share of the metadata free
space reached 88% with no group ever falling below the threshold:
reclaim never ran, and the space never came back.

A group like that does not need all of its data moved.  The trapped space
is in its partially used stripes; moving only the extents in those
stripes makes them whole again.  An extent spanning several stripes frees
its bytes in all of them when it moves, so the stripes it shares with
others must move too, transitively: the selection is the closure of the
partial stripes under "shares an extent with".

Compaction runs from the reclaim worker, for a group whose live data does
not fall below the threshold, once its trapped space reaches
stripe_compact percent of the whole group.  (The test whole relocation
uses, trapped share of the FREE space, never admits a mostly live group:
its little free space is mostly whole stripes.)

 - The group goes read-only as for relocation.  After a commit, so the
   free space cache and the extent tree's commit root agree, the partial
   stripes are read from the free space cache and the closure is found
   with a union-find over the group's stripes in one pass over its extent
   items, each union carrying the live bytes of its extents.

 - If the closure would move more than half the group's live bytes, the
   group is relocated whole and removed instead: that costs about the
   same and also gives back the group's raw space.

 - Otherwise relocation runs with find_next_extent() skipping every
   extent outside the selection, through both data stages, and the group
   is committed (so what moved out is unpinned) and made writable again
   instead of removed.  replace_file_extents() rewrites only the pointers
   to selected extents: the others did not move and have no new location
   (a leaf referencing both kinds is common).

Balance, device shrink and removal still relocate whole groups
(btrfs_relocate_chunk() is unchanged; btrfs_compact_chunk() is the new
entry point).  sysfs stripe_compact is the trapped percent of a group's
length that makes it a candidate (default 10; 0 turns compaction off),
and stripe_run_stats counts compaction passes, fallbacks to whole
relocation, stripes evacuated and bytes moved.

The destination is ordinary relocation's: the moved extents need whole
claimable stripes in other groups, and a commit during the pass can leave
a partial stripe at the tail of a relocation run there.  The admission
test for compaction therefore asks for half the group's live bytes
claimable elsewhere, the most a compaction can move.

The stripe_meta block group part of this change moved to
"btrfs: stripe_meta: extend stripe reclaim to metadata block groups".

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/block-group.c
fs/btrfs/disk-io.c
fs/btrfs/free-space-cache.c
fs/btrfs/free-space-cache.h
fs/btrfs/fs.h
fs/btrfs/relocation.c
fs/btrfs/relocation.h
fs/btrfs/sysfs.c
fs/btrfs/volumes.c
fs/btrfs/volumes.h

index 692c51a10cf542e1bdc195781da201fcdd7b903e..9f20424a2d5aaad33ef4adfff9a907d55d20ff6c 100644 (file)
@@ -4317,6 +4317,7 @@ static int btrfs_reclaim_block_group(struct btrfs_block_group *bg, int *reclaime
        u64 reserved;
        u64 old_total;
        int ret = 0;
+       bool compact = false;
 
        /*
         * A stripe_alloc group's live extents can only move into
@@ -4328,9 +4329,13 @@ static int btrfs_reclaim_block_group(struct btrfs_block_group *bg, int *reclaime
         */
        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);
@@ -4401,10 +4406,19 @@ static int btrfs_reclaim_block_group(struct btrfs_block_group *bg, int *reclaime
         * 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);
@@ -4456,7 +4470,23 @@ static int btrfs_reclaim_block_group(struct btrfs_block_group *bg, int *reclaime
 "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);
index d461066c858d697b309e29dbb126acbdb32ba4fd..5d438c05d196ce18706c36877d03d9643c78548f 100644 (file)
@@ -2793,6 +2793,7 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
        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);
index b9e88f239c8ff11ccbcddb08c753000ff78e1853..465b77fab1cfb76f0589ec99a85c1fbd0081523c 100644 (file)
@@ -3432,26 +3432,18 @@ static u64 stripe_unusable_nstripes(const struct btrfs_block_group *bg)
 }
 
 /*
- * 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);
 
@@ -3480,6 +3472,27 @@ static u64 stripe_unusable_scan(struct btrfs_block_group *bg, u32 *freep,
                                    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,
@@ -3499,6 +3512,38 @@ static u64 stripe_unusable_scan(struct btrfs_block_group *bg, u32 *freep,
        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
@@ -3635,22 +3680,23 @@ void btrfs_block_group_disarm_stripe_unusable(struct btrfs_block_group *bg)
 
 
 /*
- * 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);
@@ -3662,21 +3708,53 @@ bool btrfs_stripe_bg_wants_reclaim(struct btrfs_block_group *bg)
         * 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;
 }
 
 /*
index 70475caf38cd81760cce49ce25951d920a6ba811..8e57fdf61e49eb3df4477eab9938d13a51a8b459 100644 (file)
@@ -149,6 +149,9 @@ void btrfs_block_group_disarm_stripe_unusable(struct btrfs_block_group *block_gr
 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,
index d419b85914953d09391b9b7fdd1d3c813d35740a..bb26294f3d4c0048ed1889b89795e40fa8653491 100644 (file)
@@ -908,9 +908,23 @@ struct btrfs_fs_info {
                /* 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().
index 178dc18d760adb5acf630b23e99376853e1a1d6b..eeb6a25d82fae28b709e1c51962b81718dfec665 100644 (file)
@@ -180,8 +180,40 @@ struct reloc_control {
        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;
@@ -226,6 +258,7 @@ static void put_reloc_control(struct reloc_control *rc)
                if (rc->block_group)
                        btrfs_put_block_group(rc->block_group);
 
+               bitmap_free(rc->compact_map);
                kfree(rc);
        }
 }
@@ -974,8 +1007,14 @@ int replace_file_extents(struct btrfs_trans_handle *trans,
                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;
 
                /*
@@ -3538,6 +3577,11 @@ next:
                        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;
                }
@@ -5427,18 +5471,207 @@ static int do_nonremap_reloc(struct btrfs_fs_info *fs_info, bool verbose,
                                   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);
@@ -5447,6 +5680,7 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 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,
@@ -5543,6 +5777,18 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
        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);
@@ -5562,6 +5808,22 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
                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);
index bb7a86e7dbe3a6a7c1d1080c5d3838c5d58f8f96..e9f95976aae4b79bbfa6b7d44d73c2b859fb0c6c 100644 (file)
@@ -24,7 +24,7 @@ static inline bool should_relocate_using_remap_tree(const struct btrfs_block_gro
 }
 
 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);
index 4855f10c4c9997f6e8f4eb703b4a96696e3776ac..02d5ba71e1e71d483243100102ae5d381d157505 100644 (file)
@@ -1293,12 +1293,20 @@ static ssize_t btrfs_stripe_run_stats_show(struct kobject *kobj,
                "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);
 
@@ -1336,6 +1344,37 @@ BTRFS_PARK_TIMEOUT_ATTR(stripe_park_timeout_ms, stripe_park_timeout_ms);
 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,
@@ -1712,6 +1751,7 @@ static const struct attribute *btrfs_attrs[] = {
        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,
 };
index 0b10487182e5caca53255aaafbefe6eca1e9d2b2..6b833229632be19ce25156b237a8b366081f7776 100644 (file)
@@ -3699,7 +3699,8 @@ static int btrfs_relocate_chunk_finish(struct btrfs_fs_info *fs_info,
        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;
@@ -3726,7 +3727,7 @@ int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset, bool v
 
        /* 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) {
                /*
@@ -3738,6 +3739,10 @@ int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset, bool v
                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;
@@ -3753,6 +3758,25 @@ int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset, bool v
        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;
index e2557ac93a3d732bef58481fd2469f2e32aa39f5..3c79696155f37213cac1abf080da0147e296c2a9 100644 (file)
@@ -786,6 +786,8 @@ int btrfs_recover_balance(struct btrfs_fs_info *fs_info);
 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);