]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: keep relocation's stripe runs until relocation ends
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Thu, 1 Oct 2026 04:57:50 +0000 (00:57 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:37:52 +0000 (18:37 -0500)
Relocation preallocates each destination extent and writes the data into
it later, after the commit that ends relocate_block_group().  Under
stripe_alloc the preallocation lands in a BTRFS_STRIPE_RUN_RELOC run,
which drains at once (a preallocation has no IO to report) and is freed
when that commit closes it.  By the time the relocated data is written,
no run covers its stripes: a write that does not cover a whole stripe
can neither park to merge with its neighbour nor pad, and goes down as a
read-modify-write.  Relocated extents rarely line up with stripes, so
that is roughly one RMW per relocated extent: an audited data balance on
a 4-device raid5 made 2247 of them, every one NO_RUN, every one written
by the data reloc inode into its own preallocation, and none over
another owner's committed data (the stripes hold only relocation's own
orphaned copies) -- harmless, but every one a stripe read.

Keep RELOC runs after they close and drain, as metadata runs already are
for the same reason, until relocation releases them from
btrfs_release_data_reloc_bg(), which it calls on its way out (after the
pointer update, before the next relocation can allocate).  A retained
run that has nothing in flight is not waited for by the commit's drain,
and any forced quiesce of its block group (read-only, removal, unmount)
releases it first.  While retained, its range stays unclaimable and its
stripes are relocation's alone, so the writes into them park and merge
or pad like any other run's.

reloc_run_retained / reloc_run_released in stripe_run_stats count the
runs kept and released.

The open stripe self-test frees the runs it leaves retained, as unmount
does.

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/block-group.c
fs/btrfs/fs.h

index 3fb3478d9f792aef35bff4302d7e637b71dfe3cf..8f3049fa8855e6fc2f42164b2187b44e050d5eb7 100644 (file)
@@ -495,6 +495,12 @@ struct btrfs_open_stripe_run {
        u64 owner;                      /* LOG class: btrfs_ino of the owner */
        enum btrfs_stripe_run_class class;
        bool open;                      /* accepting allocations */
+       /*
+        * Relocation's run: kept after it closes and drains, until relocation
+        * releases it (btrfs_release_stripe_reloc_runs()).  See
+        * run_freed_on_drain().
+        */
+       bool retained;
        /*
         * One bit per sector of the run, set while the sector holds an
         * allocation someone still intends to write.  Padding uses it to find
@@ -627,7 +633,7 @@ static u64 close_open_stripe_run(struct btrfs_block_group *bg,
        tail_len = run->end - run->offset;
        run->end = run->offset;
        bg->stripe_open_remainder -= tail_len;
-       if (open_stripe_run_drained(run))
+       if (open_stripe_run_drained(run) && !run->retained)
                free_open_stripe_run(bg, run);
        return tail_len;
 }
@@ -875,6 +881,9 @@ int btrfs_alloc_from_open_stripe(struct btrfs_block_group *bg, u64 num_bytes,
        }
        new_run->bg = bg;
        new_run->class = class;
+       new_run->retained = class == BTRFS_STRIPE_RUN_RELOC;
+       if (new_run->retained)
+               atomic64_inc(&fs_info->stripe_run_stats.reloc_run_retained);
        new_run->owner = 0;
        new_run->start = start;
        new_run->end = start + len;
@@ -1485,7 +1494,7 @@ void btrfs_open_stripe_write_abandoned(struct btrfs_block_group *bg, u64 start,
                        ASSERT(run->inflight_bytes >= num_bytes);
                        run_live_set(run, start, num_bytes, false);
                        run->inflight_bytes -= num_bytes;
-                       if (open_stripe_run_drained(run))
+                       if (open_stripe_run_drained(run) && !run->retained)
                                free_open_stripe_run(bg, run);
                        break;
                }
@@ -1511,7 +1520,7 @@ void btrfs_open_stripe_write_done(struct btrfs_block_group *bg, u64 start,
                        ASSERT(start + num_bytes <= run->offset);
                        ASSERT(run->inflight_bytes >= num_bytes);
                        run->inflight_bytes -= num_bytes;
-                       if (open_stripe_run_drained(run))
+                       if (open_stripe_run_drained(run) && !run->retained)
                                free_open_stripe_run(bg, run);
                        break;
                }
@@ -1587,7 +1596,7 @@ void btrfs_open_stripe_write_done_run(struct btrfs_open_stripe_run *run,
        spin_lock_irqsave(&bg->stripe_run_lock, flags);
        ASSERT(run->inflight_bytes >= num_bytes);
        run->inflight_bytes -= num_bytes;
-       if (open_stripe_run_drained(run))
+       if (open_stripe_run_drained(run) && !run->retained)
                free_open_stripe_run(bg, run);
        spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
 }
@@ -1652,6 +1661,10 @@ static bool bg_open_stripes_settled(struct btrfs_block_group *bg, u64 seq)
 
        spin_lock_irqsave(&bg->stripe_run_lock, flags);
        list_for_each_entry(run, &bg->open_stripe_runs, list) {
+               /* Nor are retained relocation runs, once nothing is in flight. */
+               if (run->retained && open_stripe_run_drained(run) &&
+                   seq != U64_MAX)
+                       continue;
                if (run->open_seq < seq) {
                        ret = false;
                        break;
@@ -1661,6 +1674,42 @@ static bool bg_open_stripes_settled(struct btrfs_block_group *bg, u64 seq)
        return ret;
 }
 
+/*
+ * Stop retaining @bg's relocation runs: free the ones already drained, and
+ * let any still open or in flight be freed when they drain, like any other.
+ */
+static void release_retained_stripe_runs(struct btrfs_block_group *bg)
+{
+       struct btrfs_open_stripe_run *run, *tmp;
+       unsigned long flags;
+
+       spin_lock_irqsave(&bg->stripe_run_lock, flags);
+       list_for_each_entry_safe(run, tmp, &bg->open_stripe_runs, list) {
+               if (!run->retained)
+                       continue;
+               run->retained = false;
+               atomic64_inc(&bg->fs_info->stripe_run_stats.reloc_run_released);
+               if (open_stripe_run_drained(run))
+                       free_open_stripe_run(bg, run);
+       }
+       spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+}
+
+/*
+ * Relocation is done with its destination stripes: release its retained
+ * runs in every block group (the dedication of btrfs_release_data_reloc_bg()
+ * can have moved between groups during the relocation).
+ */
+static void btrfs_release_stripe_reloc_runs(struct btrfs_fs_info *fs_info)
+{
+       struct btrfs_block_group *bg;
+
+       spin_lock(&fs_info->open_stripe_lock);
+       list_for_each_entry(bg, &fs_info->open_stripe_bgs, open_stripe_bg_list)
+               release_retained_stripe_runs(bg);
+       spin_unlock(&fs_info->open_stripe_lock);
+}
+
 /*
  * Close and drain all of a block group's stripe runs.  The caller must
  * prevent new allocations first (e.g. the block group is read-only),
@@ -1675,6 +1724,8 @@ void btrfs_retire_block_group_stripes(struct btrfs_block_group *bg)
 {
        struct btrfs_fs_info *fs_info = bg->fs_info;
 
+       /* Nothing may outlive this, relocation's retained runs included. */
+       release_retained_stripe_runs(bg);
        close_block_group_stripe_runs(bg, U64_MAX);
        /* Parked partial-stripe rbios hold the very bios the wait drains. */
        btrfs_flush_parked_rbios(fs_info, bg->start, bg->length, false);
@@ -1938,10 +1989,10 @@ void btrfs_release_data_reloc_bg(struct btrfs_fs_info *fs_info)
        spin_unlock(&fs_info->relocation_bg_lock);
 
        if (!bytenr)
-               return;
+               goto release;
        bg = btrfs_lookup_block_group(fs_info, bytenr);
        if (!bg)
-               return;
+               goto release;
        if (bg->space_info) {
                spin_lock(&bg->space_info->lock);
                bg->space_info->bytes_stripe_claimable_reloc = 0;
@@ -1949,6 +2000,8 @@ void btrfs_release_data_reloc_bg(struct btrfs_fs_info *fs_info)
        }
        btrfs_close_bg_open_stripes(bg);
        btrfs_put_block_group(bg);
+release:
+       btrfs_release_stripe_reloc_runs(fs_info);
 }
 
 struct btrfs_caching_control *btrfs_get_caching_control(
index ab892c3bcd33874d5bf9a903d00dc5d47f8da65d..0488b460edc0cf0bbb91c1b5d85e083c159366a0 100644 (file)
@@ -866,6 +866,9 @@ struct btrfs_fs_info {
                atomic64_t strand_scan_bytes;
                /* rescans requested by a freed run to credit its stripes back */
                atomic64_t strand_rescan_marks;
+               /* Relocation runs retained past their drain, and released. */
+               atomic64_t reloc_run_retained;
+               atomic64_t reloc_run_released;
        } stripe_run_stats;
        /*
         * Park deadlines, ms (sysfs); 0 disables parking for that class.  See