]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_meta: allocate tree blocks by whole stripes
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 19 Sep 2026 17:17:58 +0000 (13:17 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:16:31 +0000 (21:16 -0500)
Metadata gets the placement rule data already has: tree blocks are handed
out from runs of fully free stripes, and a stripe is never revisited once
the transaction that filled it has written it.  No separate mount option: the
chunk profile already decides it.  raid56 metadata has the same write hole
as raid56 data, so it gets the same protection at the same time, and
metadata on any other profile has no write hole to close -- the policy's
RAID56_MASK test simply does not match it, so nothing changes there.

It does cost a stranded stripe tail per commit, and a metadata allocation
that fails aborts the transaction rather than failing one write.  Those
are the price of covering raid56 metadata at all rather than a reason to
make it opt-in separately: a filesystem unwilling to pay them does not
want raid56 metadata, and converting it to raid1c3/raid1c4 removes both
the cost and the write hole.

Only system chunks are left uncovered: btrfs_is_stripe_meta_bg() tests the
METADATA flag and a system chunk does not carry it, so a raid56 system
chunk keeps the legacy read-modify-write.  The mount-time warning is
narrowed to say exactly that instead of claiming metadata is uncovered,
which it no longer is.

The machinery is the data machinery, including the per-sector liveness
map the previous patch gave it.  Metadata needed three things of its own:

 - A completion report.  A run drains when every byte allocated from it
   has been accounted for, and metadata had no equivalent of ordered
   extent completion, so its runs could never drain -- the deadlock that
   mixed block groups hit.  end_bbio_meta_write() now reports, and
   btrfs_open_stripe_write_abandoned() covers a tree block freed before
   it was ever written (COWed twice in one transaction, say).

 - A two-phase closure.  btrfs_retire_open_stripes() runs before the
   commit writes tree blocks, so it skips metadata runs; they are closed
   and drained afterwards by btrfs_retire_meta_stripes(), once every block
   is on disk.  Closing earlier would strand blocks the commit has not
   allocated yet, draining earlier would wait for writes it has not
   issued.  Metadata runs are also freed there rather than when their
   counter reaches zero: a stripe's columns can be written by separate
   rbios, and freeing on the first completion leaves the second with no
   run to pad or merge against.

 - Whole stripes per writeback pass.  The opportunistic writeback passes
   do not know about stripes and will happily write three tree blocks now
   and five later; btrfs_defer_stripe_meta_write() leaves the blocks
   dirty for the commit (or a WB_SYNC_ALL waiter) instead, the same shape
   as the zoned write-pointer deferral.

Two free paths bypass the usual completion reporting and must report the
block abandoned themselves: btrfs_free_tree_block() can send a
same-transaction unwritten block through the delayed-ref machinery when
check_ref_cleanup() finds its ref head already processed, and
clean_log_buffer() discards unwritten log tree blocks via
btrfs_pin_reserved_extent() without ever visiting btrfs_free_tree_block().
Either miss leaves the block counted as inflight forever: the run never
drains, the block group reference never drops, and unmount asserts.  Both
were found by the dm-log-writes replay matrix, whose crash-recovery mounts
exercise both paths routinely.

Trapped free space is maintained for stripe_meta groups as for data: the
disarm, rescan and commit-time scan accept them, the commit scan sums the
METADATA space_info, and inc_block_group_ro() applies the trapped-bytes
subtraction to metadata groups too.  btrfs_update_global_block_rsv()
compares the global reserve against the capacity minus the trapped bytes,
so a metadata chunk is allocated while unallocated space remains, and a
stripe_meta tree block allocation that fails with ENOSPC (which aborts the
transaction) prints the stripe counters.

btrfs_retire_meta_stripes() syncs the open-run remainder into the
space_info after it closes the runs, as the data retirement does.
report_uncovered_rmw() keeps its "not covered by stripe_alloc" message for
what it was written for, system chunks and mixed groups, and no longer
says it of a covered stripe_meta group.  btrfs_free_tree_block() checks
its block group lookup.

A mount whose log replay fails after allocating tree blocks, and the
unmount of an aborted filesystem, never commit again, so the commit-time
retirement never releases their open runs and block group references:
both release every open run before the block groups are freed, reporting
any run that still counts bytes in flight.

Assisted-by: Claude:claude-fable-5
Assisted-by: Claude:claude-fable-5-1
Assisted-by: Claude:claude-opus-5-5
12 files changed:
fs/btrfs/block-group.c
fs/btrfs/block-group.h
fs/btrfs/block-rsv.c
fs/btrfs/disk-io.c
fs/btrfs/extent-tree.c
fs/btrfs/extent_io.c
fs/btrfs/free-space-cache.c
fs/btrfs/raid56.c
fs/btrfs/sysfs.c
fs/btrfs/tests/free-space-tests.c
fs/btrfs/transaction.c
fs/btrfs/tree-log.c

index 27b7fb36361fa304c28321a6d8997e854547556d..d29803e43bd561b916fe2804921e4ae2d65a1dea 100644 (file)
@@ -500,6 +500,7 @@ struct btrfs_open_stripe_run {
        u64 offset;                     /* next unallocated byte */
        u64 inflight_bytes;             /* reserved bytes with data IO pending */
        u64 open_seq;                   /* fs_info->stripe_retire_seq at open */
+       u64 transid;                    /* fs_info->generation at open */
        /*
         * Private runs are found by (root, owner): root is the subvolume's
         * root id, owner the inode number (LOG, NOCOW), a shared LOG key
@@ -534,6 +535,60 @@ struct btrfs_open_stripe_run {
        u32 live_nbits;                 /* sectors @live covers; caps growth */
 };
 
+static void btrfs_dump_open_meta_runs(struct btrfs_block_group *bg)
+{
+       struct btrfs_open_stripe_run *run;
+       unsigned long flags;
+
+       spin_lock_irqsave(&bg->stripe_run_lock, flags);
+       list_for_each_entry(run, &bg->open_stripe_runs, list) {
+               if (run->class != BTRFS_STRIPE_RUN_META)
+                       continue;
+               btrfs_warn(bg->fs_info,
+"  run %llu..%llu frontier %llu open %d inflight %llu transid %llu",
+                          run->start, run->end, run->offset, run->open,
+                          run->inflight_bytes, run->transid);
+       }
+       spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+}
+
+static bool bg_has_undrained_meta_runs(struct btrfs_block_group *bg,
+                                      u64 transid)
+{
+       struct btrfs_open_stripe_run *run;
+       unsigned long flags;
+       bool ret = false;
+
+       spin_lock_irqsave(&bg->stripe_run_lock, flags);
+       list_for_each_entry(run, &bg->open_stripe_runs, list) {
+               if (run->class != BTRFS_STRIPE_RUN_META)
+                       continue;
+               /*
+                * An open run here belongs to the running transaction: the
+                * retirer closed every run it is responsible for before its
+                * wait, so this one was opened while it waited, and drains
+                * when that transaction's own commit writes its tree blocks.
+                * Waiting on it would be waiting for writes this commit never
+                * issued.
+                */
+               if (run->open)
+                       continue;
+               /*
+                * Likewise a run the running transaction opened and a band
+                * collision or eviction closed early: its blocks are written,
+                * and its stripes settled, by that transaction's own commit.
+                */
+               if (run->transid > transid)
+                       continue;
+               if (run->inflight_bytes) {
+                       ret = true;
+                       break;
+               }
+       }
+       spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+       return ret;
+}
+
 static bool open_stripe_run_drained(const struct btrfs_open_stripe_run *run)
 {
        return !run->open && run->inflight_bytes == 0;
@@ -656,7 +711,27 @@ static bool open_stripe_run_dead(const struct btrfs_open_stripe_run *run)
        return bitmap_empty(run->live, run->live_nbits);
 }
 
-/* Free a drained run.  Caller holds bg->stripe_run_lock. */
+/*
+ * Whether a run is freed as soon as it drains.  Two kinds must outlive the
+ * drain, because later writes to their stripes still need a covering run to
+ * park against or pad from:
+ *
+ * - Metadata runs: writeback may submit a stripe's tree blocks as separate
+ *   rbios, so the run drains after the first one.  They are freed by
+ *   btrfs_retire_meta_stripes() once every tree block is on disk.
+ *
+ * - Relocation runs: relocation preallocates its destination extents (no
+ *   IO, so the run drains at once) and writes them only after the commit
+ *   that ends relocate_block_group().  They are retained until relocation
+ *   releases them (btrfs_release_stripe_reloc_runs()).  Meanwhile their
+ *   stripes hold only relocation's own copies, and the closed run keeps its
+ *   range unclaimable.
+ */
+static bool run_freed_on_drain(const struct btrfs_open_stripe_run *run)
+{
+       return run->class != BTRFS_STRIPE_RUN_META && !run->retained;
+}
+
 static void free_open_stripe_run(struct btrfs_block_group *bg,
                                 struct btrfs_open_stripe_run *run)
 {
@@ -678,6 +753,23 @@ static void free_open_stripe_run(struct btrfs_block_group *bg,
        wake_up_var(&bg->open_stripe_runs);
 }
 
+/*
+ * A run has drained.  Free it if nothing needs it any more; otherwise
+ * (metadata or retained relocation runs) wake the waiters directly, since a
+ * report can arrive after btrfs_retire_meta_stripes() started waiting, e.g.
+ * when an unwritten tree block is freed.
+ */
+static void open_stripe_run_drain_reported(struct btrfs_block_group *bg,
+                                          struct btrfs_open_stripe_run *run)
+{
+       if (!open_stripe_run_drained(run))
+               return;
+       if (run_freed_on_drain(run))
+               free_open_stripe_run(bg, run);
+       else
+               wake_up_var(&bg->open_stripe_runs);
+}
+
 /*
  * Close a run: no further allocation may join it, and its unallocated tail
  * must be returned to the free space cache by the caller (after dropping the
@@ -786,6 +878,10 @@ int btrfs_alloc_from_open_stripe(struct btrfs_block_group *bg, u64 num_bytes,
                                 u64 *ret_offset, u64 *available)
 {
        struct btrfs_fs_info *fs_info = bg->fs_info;
+       const u64 transid = btrfs_get_fs_generation(fs_info);
+       u64 evict_start[BTRFS_STRIPE_RUN_NR_BANDS];
+       u64 evict_len[BTRFS_STRIPE_RUN_NR_BANDS];
+       int nr_evict = 0;
        const u64 fsl = bg->full_stripe_len;
        struct btrfs_open_stripe_run *new_run;
        struct btrfs_open_stripe_run *run;
@@ -813,6 +909,24 @@ int btrfs_alloc_from_open_stripe(struct btrfs_block_group *bg, u64 num_bytes,
 
                if (!r)
                        continue;
+               /*
+                * Metadata runs are closed after the next transaction is
+                * already running (btrfs_retire_meta_stripes()), so a slot can
+                * still hold the previous transaction's run.  Joining it would
+                * put two transactions' tree blocks in the same stripes, and
+                * the later one's writeback would read-modify-write parity
+                * over the earlier one's committed blocks: the write hole.
+                * Close it; its tail returns to the free space cache below.
+                */
+               if (class == BTRFS_STRIPE_RUN_META && r->transid != transid) {
+                       bg->open_stripe[class][band] = NULL;
+                       evict_len[nr_evict] =
+                               close_open_stripe_run(bg, r,
+                                                     &evict_start[nr_evict]);
+                       if (evict_len[nr_evict])
+                               nr_evict++;
+                       continue;
+               }
                rem = r->end - r->offset;
                if (rem > *available)
                        *available = rem;
@@ -834,10 +948,16 @@ int btrfs_alloc_from_open_stripe(struct btrfs_block_group *bg, u64 num_bytes,
                spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
                if (tail_len)
                        btrfs_add_free_space(bg, tail_start, tail_len);
+               for (int i = 0; i < nr_evict; i++)
+                       btrfs_add_free_space(bg, evict_start[i], evict_len[i]);
                stripe_open_remainder_sync(bg);
                return 0;
        }
        spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+       for (int i = 0; i < nr_evict; i++)
+               btrfs_add_free_space(bg, evict_start[i], evict_len[i]);
+       if (nr_evict)
+               stripe_open_remainder_sync(bg);
 
        /* Zeroed, so the error paths can free ->live and ->written. */
        new_run = kzalloc(sizeof(*new_run), GFP_NOFS);
@@ -909,6 +1029,9 @@ int btrfs_alloc_from_open_stripe(struct btrfs_block_group *bg, u64 num_bytes,
 
                if (!r || r->end != start)
                        continue;
+               /* Never grow another transaction's run (see the scan above). */
+               if (class == BTRFS_STRIPE_RUN_META && r->transid != transid)
+                       continue;
                /*
                 * Only grow as far as the run's liveness map reaches; it
                 * cannot be reallocated here (see alloc_run_live_map()).
@@ -951,6 +1074,7 @@ int btrfs_alloc_from_open_stripe(struct btrfs_block_group *bg, u64 num_bytes,
        if (new_run->retained)
                atomic64_inc(&fs_info->stripe_run_stats.reloc_run_retained);
        new_run->owner = 0;
+       new_run->transid = transid;
        new_run->start = start;
        new_run->end = start + len;
        new_run->offset = start + num_bytes;
@@ -2274,15 +2398,20 @@ static void stripe_alloc_sweep_groups(struct btrfs_fs_info *fs_info, bool arm)
  */
 static void warn_stripe_alloc_uncovered(struct btrfs_fs_info *info)
 {
-       const u64 meta_bits = info->avail_metadata_alloc_bits |
-                             info->avail_system_alloc_bits;
-
        if (!btrfs_test_opt(info, STRIPE_ALLOC))
                return;
 
-       if (meta_bits & BTRFS_BLOCK_GROUP_RAID56_MASK)
+       /*
+        * raid56 metadata is covered now that the policy follows the chunk
+        * profile.  System chunks are not: btrfs_is_stripe_meta_bg() tests
+        * the METADATA flag, and a system chunk does not carry it, so a
+        * raid56 system chunk keeps the legacy read-modify-write and its
+        * write hole.  They are small and rewritten rarely, but say so
+        * rather than let the earlier "covered" message imply otherwise.
+        */
+       if (info->avail_system_alloc_bits & BTRFS_BLOCK_GROUP_RAID56_MASK)
                btrfs_warn(info,
-"stripe_alloc does not cover raid56 metadata: the write hole remains for metadata, consider converting it to raid1c3/raid1c4");
+"stripe_alloc does not cover raid56 system chunks: the write hole remains for them, consider converting to raid1c3/raid1c4");
 }
 
 /*
@@ -2593,8 +2722,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) && !run->retained)
-                               free_open_stripe_run(bg, run);
+                       open_stripe_run_drain_reported(bg, run);
                        break;
                }
        }
@@ -2619,8 +2747,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) && !run->retained)
-                               free_open_stripe_run(bg, run);
+                       open_stripe_run_drain_reported(bg, run);
                        break;
                }
        }
@@ -2727,8 +2854,7 @@ u32 btrfs_open_stripe_write_done_run(struct btrfs_open_stripe_run *run,
        }
        /* Under the lock: the run and the group may be gone after it. */
        wake_up_var(&bg->open_stripe_runs);
-       if (open_stripe_run_drained(run) && !run->retained)
-               free_open_stripe_run(bg, run);
+       open_stripe_run_drain_reported(bg, run);
        spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
        return unwritten;
 }
@@ -2770,6 +2896,13 @@ restart:
                                continue;
                        atomic64_inc(&fs_info->stripe_run_stats.nocow_run_dead_closed);
                }
+               /*
+                * Metadata runs are still being filled at this point in the
+                * commit and their tree blocks are not written until later;
+                * btrfs_retire_meta_stripes() closes them once they are.
+                */
+               if (run->class == BTRFS_STRIPE_RUN_META && seq != U64_MAX)
+                       continue;
                for (class = 0; class < BTRFS_STRIPE_RUN_NR_CLASSES; class++)
                        for (band = 0; band < BTRFS_STRIPE_RUN_NR_BANDS; band++)
                                if (bg->open_stripe[class][band] == run)
@@ -2815,6 +2948,9 @@ static bool bg_open_stripes_settled(struct btrfs_block_group *bg, u64 seq)
                if (run->retained && open_stripe_run_drained(run) &&
                    seq != U64_MAX)
                        continue;
+               /* Metadata drains after tree writeback, not here. */
+               if (run->class == BTRFS_STRIPE_RUN_META && seq != U64_MAX)
+                       continue;
                if (run->open_seq < seq) {
                        ret = false;
                        break;
@@ -2912,16 +3048,204 @@ void btrfs_wake_open_stripe_waiters(struct btrfs_fs_info *fs_info)
 }
 
 /*
- * Commit-time retirement: close every stripe run opened before this call
- * and wait for all data IO into the stripes of those runs to complete.
- * Must run while the committing transaction still accepts joins (before
- * TRANS_STATE_COMMIT_DOING): tasks holding unreported run bytes can block
- * on a join (e.g. a chunk allocation mid-delalloc), and this drain waits
- * for their IO.  Records the bumped sequence in @trans; ordered extents
- * from runs with open_seq >= it defer their file extents to the next
- * transaction, closing the fast-completion window this placement opens.
- * Calls are serialized by the transaction commit; @trans may be NULL for
- * the final cleanup call.
+ * Teardown: free every remaining stripe run and drop each group's
+ * membership reference.  Needed when no final commit retired them: a mount
+ * that fails after log replay allocated tree blocks, or an unmount after a
+ * transaction abort.  IO is quiesced by now, so a run still counting
+ * inflight bytes is reported and freed anyway.
+ *
+ * Ordered extents point at their runs (ordered->stripe_run), so this relies
+ * on none being left: at unmount every inode, and with it every ordered
+ * extent, is gone before close_ctree() runs, delayed iputs are drained, and
+ * on both paths the workers that complete ordered extents are stopped first.
+ */
+void btrfs_free_open_stripe_runs(struct btrfs_fs_info *fs_info)
+{
+       LIST_HEAD(bgs);
+       struct btrfs_block_group *bg, *tmp_bg;
+       unsigned long flags;
+
+       spin_lock(&fs_info->open_stripe_lock);
+       list_splice_init(&fs_info->open_stripe_bgs, &bgs);
+       spin_unlock(&fs_info->open_stripe_lock);
+
+       list_for_each_entry_safe(bg, tmp_bg, &bgs, open_stripe_bg_list) {
+               struct btrfs_open_stripe_run *run, *tmp;
+
+               list_del_init(&bg->open_stripe_bg_list);
+               spin_lock_irqsave(&bg->stripe_run_lock, flags);
+               list_for_each_entry_safe(run, tmp, &bg->open_stripe_runs, list) {
+                       if (run->inflight_bytes)
+                               btrfs_warn(fs_info,
+"stripe run %llu-%llu in block group %llu released at teardown with %llu bytes still in flight",
+                                          run->start, run->end, bg->start,
+                                          run->inflight_bytes);
+                       list_del(&run->list);
+                       bitmap_free(run->live);
+                       kfree(run);
+               }
+               memset(bg->open_stripe, 0, sizeof(bg->open_stripe));
+               bg->stripe_open_remainder = 0;
+               spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+               stripe_open_remainder_sync(bg);
+               btrfs_put_block_group(bg);
+       }
+}
+
+/*
+ * Close and drain the metadata stripe runs once every tree block this
+ * transaction allocated has reached the disk.
+ *
+ * btrfs_retire_open_stripes() runs earlier in the commit and skips these
+ * runs, so they stay open for the commit's own tree block allocations.
+ * Closing earlier would strand blocks the commit has yet to allocate;
+ * draining earlier would wait for writes the commit has yet to issue.
+ */
+void btrfs_retire_meta_stripes(struct btrfs_fs_info *fs_info, u64 transid)
+{
+       struct btrfs_block_group *bg;
+       LIST_HEAD(retire_list);
+       unsigned long flags;
+
+       if (!btrfs_test_opt(fs_info, STRIPE_ALLOC))
+               return;
+
+       spin_lock(&fs_info->open_stripe_lock);
+       list_for_each_entry(bg, &fs_info->open_stripe_bgs, open_stripe_bg_list)
+               list_add_tail(&bg->open_stripe_retire_list, &retire_list);
+       spin_unlock(&fs_info->open_stripe_lock);
+
+       while (!list_empty(&retire_list)) {
+               struct btrfs_open_stripe_run *run;
+               bool again = true;
+
+               bg = list_first_entry(&retire_list, struct btrfs_block_group,
+                                     open_stripe_retire_list);
+               list_del_init(&bg->open_stripe_retire_list);
+               if (!(bg->flags & BTRFS_BLOCK_GROUP_METADATA))
+                       continue;
+
+               while (again) {
+                       u64 tail_start;
+                       u64 tail_len = 0;
+
+                       again = false;
+                       spin_lock_irqsave(&bg->stripe_run_lock, flags);
+                       list_for_each_entry(run, &bg->open_stripe_runs, list) {
+                               if (run->class != BTRFS_STRIPE_RUN_META ||
+                                   !run->open)
+                                       continue;
+                               /*
+                                * The running transaction's runs are not ours
+                                * to close: their tree blocks are written, and
+                                * their stripes settled, by that transaction's
+                                * own commit.
+                                */
+                               if (run->transid > transid)
+                                       continue;
+                               for (int band = 0;
+                                    band < BTRFS_STRIPE_RUN_NR_BANDS; band++)
+                                       if (bg->open_stripe[BTRFS_STRIPE_RUN_META][band] == run)
+                                               bg->open_stripe[BTRFS_STRIPE_RUN_META][band] = NULL;
+                               tail_len = close_open_stripe_run(bg, run, &tail_start);
+                               again = true;
+                               break;
+                       }
+                       spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+                       if (tail_len)
+                               btrfs_add_free_space(bg, tail_start, tail_len);
+               }
+               /*
+                * The closes above shrank the group's open-run remainder;
+                * fold the delta into the space_info now.  While the policy
+                * is on the next allocation from this group would carry it,
+                * but after a runtime disable nothing allocates here again
+                * and the stale remainder holds metadata back for good.
+                */
+               stripe_open_remainder_sync(bg);
+
+               /*
+                * Every tree block is on disk, so end_bbio_meta_write() has
+                * reported all of them and this normally does not wait.  The
+                * timeout guards against a path that frees a tree block
+                * without reporting it to its run: warn and keep the run,
+                * which costs space, rather than hang the commit.
+                */
+               if (!wait_var_event_timeout(&bg->open_stripe_runs,
+                                           !bg_has_undrained_meta_runs(bg, transid),
+                                           60 * HZ)) {
+                       btrfs_warn(fs_info,
+"metadata stripe runs in block group %llu did not drain; a tree block was freed without reporting to its run",
+                                  bg->start);
+                       btrfs_dump_open_meta_runs(bg);
+               }
+
+               /* Drained and no longer needed for padding or merging. */
+               spin_lock_irqsave(&bg->stripe_run_lock, flags);
+               while (true) {
+                       struct btrfs_open_stripe_run *next = NULL;
+
+                       list_for_each_entry(run, &bg->open_stripe_runs, list) {
+                               if (run->class == BTRFS_STRIPE_RUN_META &&
+                                   open_stripe_run_drained(run)) {
+                                       next = run;
+                                       break;
+                               }
+                       }
+                       if (!next)
+                               break;
+                       free_open_stripe_run(bg, next);
+               }
+               spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+       }
+}
+
+/*
+ * Should this tree block be left dirty for the transaction commit to write?
+ *
+ * A stripe of tree blocks goes out as one full-stripe write only if all of
+ * its blocks are written in the same pass.  Opportunistic writeback (memory
+ * reclaim, the periodic timer) does not know about stripes and would split
+ * it into read-modify-writes, so it is declined here, as zoned mode does in
+ * btrfs_check_meta_write_pointer(), and a commit is requested instead.
+ * WB_SYNC_ALL is never deferred, so a caller that needs the pages written
+ * can always get them written.
+ */
+bool btrfs_defer_stripe_meta_write(struct btrfs_fs_info *fs_info,
+                                  struct extent_buffer *eb,
+                                  const struct writeback_control *wbc)
+{
+       struct btrfs_block_group *bg;
+       bool defer;
+
+       if (!btrfs_test_opt(fs_info, STRIPE_ALLOC))
+               return false;
+       if (wbc->sync_mode == WB_SYNC_ALL)
+               return false;
+
+       bg = btrfs_lookup_block_group(fs_info, eb->start);
+       if (!bg)
+               return false;
+       defer = btrfs_is_stripe_meta_bg(bg);
+       btrfs_put_block_group(bg);
+
+       if (defer) {
+               set_bit(BTRFS_FS_NEED_TRANS_COMMIT, &fs_info->flags);
+               wake_up_process(fs_info->transaction_kthread);
+       }
+       return defer;
+}
+
+/*
+ * Commit-time retirement: close every data stripe run opened before this
+ * call and wait for its IO to complete.  Metadata runs are retired later by
+ * btrfs_retire_meta_stripes(), and persistent NOCOW runs survive.  Must run
+ * while the committing transaction still accepts joins (before
+ * TRANS_STATE_COMMIT_DOING): tasks holding unreported run bytes can block on
+ * a join, and this drain waits for their IO.  Records the bumped sequence in
+ * @trans; ordered extents from runs with open_seq >= it defer their file
+ * extents to the next transaction.  Calls are serialized by the transaction
+ * commit; @trans is NULL for the final cleanup call.
  */
 void btrfs_retire_open_stripes(struct btrfs_fs_info *fs_info,
                               struct btrfs_transaction *trans)
@@ -3006,7 +3330,13 @@ void btrfs_scan_stripe_unusable(struct btrfs_fs_info *fs_info, bool force)
                u64 total_stranded = 0;
                int raid;
 
-               if (!(sinfo->flags & BTRFS_BLOCK_GROUP_DATA))
+               /*
+                * Data groups under stripe_alloc and metadata groups under
+                * stripe_meta both place extents in whole stripes only, so
+                * both carry trapped free space.  Mixed groups are refused.
+                */
+               if (!(sinfo->flags & (BTRFS_BLOCK_GROUP_DATA |
+                                     BTRFS_BLOCK_GROUP_METADATA)))
                        continue;
                down_read(&sinfo->groups_sem);
                for (raid = 0; raid < BTRFS_NR_RAID_TYPES; raid++) {
@@ -3126,7 +3456,14 @@ void btrfs_stripe_check_write(struct btrfs_fs_info *fs_info,
        bg = btrfs_lookup_block_group(fs_info, full_stripe_start);
        if (!bg)
                return;
-       if (!btrfs_is_stripe_alloc_bg(bg) ||
+       /*
+        * Metadata is checked on the same terms once stripe_meta places it:
+        * its runs stay open across the commit's tree building but are closed
+        * and drained before the commit ends, so a tree block write outside
+        * every live run means a stripe was revisited after the transaction
+        * that filled it finished -- the same violation as for data.
+        */
+       if ((!btrfs_is_stripe_alloc_bg(bg) && !btrfs_is_stripe_meta_bg(bg)) ||
            test_bit(BLOCK_GROUP_FLAG_STRIPE_RELOC_USED, &bg->runtime_flags) ||
            test_bit(BLOCK_GROUP_FLAG_STRIPE_NOCOW_USED, &bg->runtime_flags))
                goto out;
@@ -4283,8 +4620,16 @@ static int inc_block_group_ro(struct btrfs_block_group *cache, bool force)
                 * btrfs_can_overcommit check here, and we need to pass in
                 * BTRFS_RESERVE_NO_FLUSH to give ourselves the most amount of
                 * leeway to allow us to mark this block group as read only.
+                *
+                * As for data above: a stripe_meta group's trapped bytes are
+                * already in the space_info total, do not re-provide them.
                 */
-               if (btrfs_can_overcommit(sinfo, num_bytes, BTRFS_RESERVE_NO_FLUSH))
+               u64 trapped = 0;
+
+               if (READ_ONCE(cache->stripe_unusable_ready))
+                       trapped = min(READ_ONCE(cache->stripe_unusable), num_bytes);
+               if (btrfs_can_overcommit(sinfo, num_bytes - trapped,
+                                        BTRFS_RESERVE_NO_FLUSH))
                        ret = 0;
        }
 
index 211dd5feaa27ecb86135a8b1825cf8fdf639fcaa..b1cbc08ce058d3a42204b39898c57366367a17fe 100644 (file)
@@ -47,6 +47,14 @@ enum btrfs_stripe_run_class {
         * blast radius to the owning file alone.
         */
        BTRFS_STRIPE_RUN_NOCOW,
+       /*
+        * Tree blocks.  Kept out of the commit-time drain that settles the
+        * classes above, because metadata writeback happens later in the same
+        * commit than that drain runs: these are sealed against further
+        * allocation and drained separately once the tree blocks are on disk
+        * (see btrfs_retire_meta_stripes()).
+        */
+       BTRFS_STRIPE_RUN_META,
        BTRFS_STRIPE_RUN_NR_CLASSES,
 };
 
@@ -509,9 +517,14 @@ struct btrfs_open_stripe_run *btrfs_get_open_stripe_run(
                struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
                u64 *open_seq);
 void btrfs_close_bg_open_stripes(struct btrfs_block_group *bg);
+void btrfs_retire_meta_stripes(struct btrfs_fs_info *fs_info, u64 transid);
+void btrfs_free_open_stripe_runs(struct btrfs_fs_info *fs_info);
 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);
+bool btrfs_defer_stripe_meta_write(struct btrfs_fs_info *fs_info,
+                                  struct extent_buffer *eb,
+                                  const struct writeback_control *wbc);
 void btrfs_stripe_run_mark_written(struct btrfs_fs_info *fs_info, u64 start,
                                   u64 len);
 bool btrfs_stripe_run_incomplete(struct btrfs_fs_info *fs_info,
@@ -585,6 +598,7 @@ void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg);
 void btrfs_release_data_reloc_bg(struct btrfs_fs_info *fs_info);
 bool btrfs_stripe_alloc_forces_cow(struct btrfs_fs_info *fs_info, u64 bytenr);
 bool btrfs_is_stripe_alloc_bg(const struct btrfs_block_group *bg);
+bool btrfs_is_stripe_meta_bg(const struct btrfs_block_group *bg);
 void btrfs_scan_stripe_unusable(struct btrfs_fs_info *fs_info, bool force);
 void btrfs_stripe_alloc_enable_work(struct work_struct *work);
 #ifdef CONFIG_BTRFS_DEBUG
index fd604631b9ca8ef6b5608fd2f2ebda9516a29c65..d78ae83bd00d04592d50608a978a133526261bad 100644 (file)
@@ -400,7 +400,14 @@ void btrfs_update_global_block_rsv(struct btrfs_fs_info *fs_info)
 
        block_rsv->full = (block_rsv->reserved == block_rsv->size);
 
-       if (block_rsv->size >= sinfo->total_bytes)
+       /*
+        * The global reserve must be backed by space a tree block can
+        * actually land in.  Under stripe_meta that excludes the bytes
+        * trapped in partially used stripes, so compare against the usable
+        * capacity, not the raw total, and force a chunk allocation as soon
+        * as the reserve would not fit in it.
+        */
+       if (block_rsv->size >= sinfo->total_bytes - sinfo->bytes_stripe_unusable)
                sinfo->force_alloc = CHUNK_ALLOC_FORCE;
        spin_unlock(&block_rsv->lock);
        spin_unlock(&sinfo->lock);
index e5c3ebca75dd658c6f614ea239d136116d4c6dc7..a54c687aee50aae2922f32666401b79636235478 100644 (file)
@@ -3853,6 +3853,7 @@ fail_tree_roots:
 fail_sb_buffer:
        btrfs_stop_parked_rbios(fs_info);
        btrfs_stop_all_workers(fs_info);
+       btrfs_free_open_stripe_runs(fs_info);
        btrfs_free_block_groups(fs_info);
 fail_alloc:
        btrfs_mapping_tree_free(fs_info);
@@ -4621,6 +4622,7 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info)
         * accounting appear to be wrong because there's pending reserved bytes,
         * so make sure we do the block group cleanup afterwards.
         */
+       btrfs_free_open_stripe_runs(fs_info);
        btrfs_free_block_groups(fs_info);
 
        iput(fs_info->btree_inode);
index cf54a9d35f1d14b8f0dd13448bc3b2d870bd833c..25a339856b07e69f12e70622972d49eb044c575a 100644 (file)
@@ -3667,6 +3667,23 @@ int btrfs_free_tree_block(struct btrfs_trans_handle *trans,
        if (btrfs_header_generation(buf) != trans->transid)
                return 0;
 
+       /*
+        * A tree block this transaction allocated and never wrote gets no
+        * writeback completion to report it to its stripe run, so report it
+        * here, before the free paths diverge: if its delayed ref head already
+        * ran, the free goes through __btrfs_free_extent() and the pin
+        * machinery, which know nothing of stripe runs.
+        */
+       if (!btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
+               bg = btrfs_lookup_block_group(fs_info, buf->start);
+               if (bg) {
+                       if (btrfs_is_stripe_meta_bg(bg))
+                               btrfs_open_stripe_write_abandoned(bg, buf->start,
+                                                                 buf->len);
+                       btrfs_put_block_group(bg);
+               }
+       }
+
        if (root_id != BTRFS_TREE_LOG_OBJECTID) {
                ret = check_ref_cleanup(trans, buf->start);
                if (!ret)
@@ -4222,6 +4239,30 @@ bool btrfs_is_stripe_alloc_bg(const struct btrfs_block_group *bg)
               !test_bit(BLOCK_GROUP_FLAG_RAID56_MIRRORED, &bg->runtime_flags);
 }
 
+/*
+ * Should this block group allocate tree blocks by whole stripes?
+ *
+ * The same rule data follows: only fully free stripes are claimed, and the
+ * partial stripe left at the end of a commit is padded out rather than
+ * revisited, so no tree block write ever lands in a stripe holding earlier
+ * committed data.  Not separately gated: the chunk profile already decides
+ * it.  raid56 metadata has the same write hole as raid56 data and gets the
+ * same protection at the same time; metadata on any other profile has no
+ * write hole to close, and the RAID56_MASK test below means the policy
+ * simply does not apply to it.  The costs metadata brings -- a stranded
+ * stripe tail per commit, and an allocation failure that aborts the
+ * transaction rather than failing one write -- are the price of covering
+ * raid56 metadata at all, and a filesystem that does not want them wants
+ * raid1c3/raid1c4 metadata instead, which is the better answer anyway.
+ */
+bool btrfs_is_stripe_meta_bg(const struct btrfs_block_group *bg)
+{
+       return btrfs_test_opt(bg->fs_info, STRIPE_ALLOC) &&
+              (bg->flags & BTRFS_BLOCK_GROUP_METADATA) &&
+              !(bg->flags & BTRFS_BLOCK_GROUP_DATA) &&
+              (bg->flags & BTRFS_BLOCK_GROUP_RAID56_MASK);
+}
+
 /*
  * The length to ask this group's stripe allocator for.  A piece of a split
  * allocation (ffe_ctl->split_bytes) is trimmed to whole stripes of this
@@ -4273,6 +4314,30 @@ static int do_allocation_stripe(struct btrfs_block_group *block_group,
        bool skip = false;
        int ret;
 
+       /*
+        * Tree blocks take the same path as data -- whole free stripes, a
+        * frontier that padding can complete -- in their own class, and none
+        * of the steering below applies to them: there is no inode to steer
+        * by, metadata is always COW, and relocation moves tree blocks
+        * through the normal metadata allocator.
+        */
+       if (btrfs_is_stripe_meta_bg(block_group)) {
+               ret = btrfs_alloc_from_open_stripe(block_group,
+                                                  ffe_ctl->num_bytes,
+                                                  BTRFS_STRIPE_RUN_META,
+                                                  &offset, &available);
+               if (ret == -ENOMEM)
+                       return ret;
+               if (ret) {
+                       if (available > ffe_ctl->max_extent_size)
+                               ffe_ctl->max_extent_size = available;
+                       return 1;
+               }
+               ffe_ctl->found_offset = offset;
+               ffe_ctl->search_start = offset;
+               return 0;
+       }
+
        /*
         * Soft dedication of one block group to relocation, mirroring the
         * zoned data_reloc_bg rule: it keeps relocation's prealloc-then-
@@ -4381,7 +4446,8 @@ static int do_allocation_stripe(struct btrfs_block_group *block_group,
 static void undo_stripe_allocation(struct btrfs_block_group *block_group,
                                   struct find_free_extent_ctl *ffe_ctl)
 {
-       if (btrfs_is_stripe_alloc_bg(block_group))
+       if (btrfs_is_stripe_alloc_bg(block_group) ||
+           btrfs_is_stripe_meta_bg(block_group))
                btrfs_open_stripe_write_abandoned(block_group,
                                                  ffe_ctl->found_offset,
                                                  ffe_ctl->num_bytes);
@@ -4393,7 +4459,8 @@ static int do_allocation(struct btrfs_block_group *block_group,
 {
        switch (ffe_ctl->policy) {
        case BTRFS_EXTENT_ALLOC_CLUSTERED:
-               if (btrfs_is_stripe_alloc_bg(block_group))
+               if (btrfs_is_stripe_alloc_bg(block_group) ||
+                   btrfs_is_stripe_meta_bg(block_group))
                        return do_allocation_stripe(block_group, ffe_ctl,
                                                    bg_ret);
                return do_allocation_clustered(block_group, ffe_ctl, bg_ret);
@@ -5222,6 +5289,28 @@ again:
                                        btrfs_dump_stripe_groups(sinfo);
                        }
                }
+               /*
+                * Tree block reservations are admitted against the
+                * whole-stripe supply, so a stripe_meta tree block should
+                * always find a whole stripe or an open run.  If not, the
+                * transaction aborts: log the counters.
+                */
+               if (!is_data && btrfs_test_opt(fs_info, STRIPE_ALLOC) &&
+                   (flags & BTRFS_BLOCK_GROUP_RAID56_MASK)) {
+                       struct btrfs_space_info *sinfo;
+
+                       sinfo = btrfs_find_space_info(fs_info, flags);
+                       if (sinfo)
+                               btrfs_warn_rl(fs_info,
+       "stripe_meta: tree block allocation of %llu bytes returned ENOSPC: claimable %llu open %llu trapped %llu may_use %llu reserved %llu total %llu",
+                                             num_bytes,
+                                             sinfo->bytes_stripe_claimable,
+                                             sinfo->bytes_stripe_open,
+                                             sinfo->bytes_stripe_unusable,
+                                             sinfo->bytes_may_use,
+                                             sinfo->bytes_reserved,
+                                             sinfo->total_bytes);
+               }
        }
 
        return ret;
index 3283835ccb5e8b7e44816e71c1a5c55e014b7c44..04c1d65f9acb3974dc61f23d1660e2b565ff70e1 100644 (file)
@@ -2402,6 +2402,16 @@ static void end_bbio_meta_write(struct btrfs_bio *bbio)
        if (bbio->bio.bi_status != BLK_STS_OK)
                set_btree_ioerr(eb);
 
+       /*
+        * Tell the stripe run this tree block is written.  Data reports the
+        * same thing from ordered extent completion; metadata has no ordered
+        * extent, so without this the run's inflight bytes would never come
+        * back down and nothing waiting on it could ever proceed.  Reported
+        * on error too: the bytes will not be written again, and a run that
+        * cannot drain is worse than one that drains after a failed write.
+        */
+       btrfs_open_stripe_write_done_bytenr(eb->fs_info, eb->start, eb->len);
+
        /*
         * Clear writeback on the buffer's own folios. The bio may carry the
         * shared zero page instead (EXTENT_BUFFER_ZONED_ZEROOUT), so iterate
@@ -2663,6 +2673,14 @@ retry:
                while ((eb = eb_batch_next(&batch)) != NULL) {
                        ctx.eb = eb;
 
+                       /*
+                        * Leave it dirty for the commit, so its stripe goes
+                        * out in one piece instead of once per writeback
+                        * pass.
+                        */
+                       if (btrfs_defer_stripe_meta_write(fs_info, eb, wbc))
+                               continue;
+
                        ret = write_meta_extent_buffer(&ctx, wbc);
                        if (ret == -EBUSY) {
                                ret = 0;
index eaed680e362dcdea45e211582987ac8629401a6e..362e31f8be3d57702e7e37ffbb797ff0764a4b01 100644 (file)
@@ -1687,7 +1687,7 @@ static void stripe_trim_claimable(struct btrfs_block_group *bg, u64 bytenr,
 {
        s64 delta;
 
-       if (!btrfs_is_stripe_alloc_bg(bg))
+       if (!btrfs_is_stripe_alloc_bg(bg) && !btrfs_is_stripe_meta_bg(bg))
                return;
        if (!READ_ONCE(bg->stripe_unusable_ready) || !size)
                return;
@@ -2348,7 +2348,7 @@ void btrfs_block_group_init_stripe_unusable(struct btrfs_block_group *bg)
        u64 trapped, claimable, nstripes;
        u32 *freep;
 
-       if (!btrfs_is_stripe_alloc_bg(bg))
+       if (!btrfs_is_stripe_alloc_bg(bg) && !btrfs_is_stripe_meta_bg(bg))
                return;
        if (READ_ONCE(bg->stripe_unusable_ready))
                return;
@@ -2395,7 +2395,7 @@ void btrfs_block_group_disarm_stripe_unusable(struct btrfs_block_group *bg)
        u64 trapped;
        u64 claimable;
 
-       if (!btrfs_is_stripe_alloc_bg(bg))
+       if (!btrfs_is_stripe_alloc_bg(bg) && !btrfs_is_stripe_meta_bg(bg))
                return;
 
        spin_lock(&ctl->tree_lock);
@@ -2568,7 +2568,7 @@ void btrfs_block_group_rescan_stripe_unusable(struct btrfs_block_group *bg)
        u64 nstripes;
        u32 *freep;
 
-       if (!btrfs_is_stripe_alloc_bg(bg))
+       if (!btrfs_is_stripe_alloc_bg(bg) && !btrfs_is_stripe_meta_bg(bg))
                return;
 
        nstripes = stripe_unusable_nstripes(bg);
index d472dd7288a206c364b694582f37643f01f9bd59..3688c225d44523b83d29d8b7fe84c72eb960be0c 100644 (file)
@@ -3582,6 +3582,8 @@ static bool need_read_stripe_sectors(struct btrfs_raid_bio *rbio)
 static void report_uncovered_rmw(struct btrfs_raid_bio *rbio)
 {
        struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
+       struct btrfs_block_group *bg;
+       bool covered = false;
 
        if (!btrfs_test_opt(fs_info, STRIPE_ALLOC))
                return;
@@ -3591,6 +3593,21 @@ static void report_uncovered_rmw(struct btrfs_raid_bio *rbio)
                return;
 
        atomic64_inc(&fs_info->stripe_park_stats.meta_rmw);
+       /*
+        * A raid56 metadata group under stripe_meta is covered: its
+        * read-modify-writes are partial writes inside one transaction's
+        * run, counted by the audit as meta_rmw_cur, and the audit warns
+        * by itself should one ever touch a committed block.  Only system
+        * chunks and mixed groups are the uncovered case this message is
+        * about.
+        */
+       bg = btrfs_lookup_block_group(fs_info, rbio->bioc->full_stripe_logical);
+       if (bg) {
+               covered = btrfs_is_stripe_meta_bg(bg);
+               btrfs_put_block_group(bg);
+       }
+       if (covered)
+               return;
        btrfs_warn_rl(fs_info,
 "read-modify-write of full stripe %llu: this block group is not covered by stripe_alloc, the raid56 write hole applies to it",
                      rbio->bioc->full_stripe_logical);
index 609e3e4462a1c4e1e83ecb156570716fe4ba771a..1812ba343ca146d83dff48b4b92996865e9dc4d1 100644 (file)
@@ -1188,7 +1188,7 @@ static ssize_t btrfs_stripe_park_stats_show(struct kobject *kobj,
        struct btrfs_fs_info *fs_info = to_fs_info(kobj);
        /* Indexed by enum btrfs_stripe_run_class. */
        static const char * const class_names[] = {
-               "cow", "reloc", "log", "nocow",
+               "cow", "reloc", "log", "nocow", "meta",
        };
        int live_by_class[BTRFS_STRIPE_RUN_NR_CLASSES];
        int open_runs[BTRFS_STRIPE_RUN_NR_CLASSES];
index 5da244e148321a88cc7d0038ee90f637f699e517..10fd9d6b344b3eb03cd1aa4fdd907fc26ebee27c 100644 (file)
@@ -1478,6 +1478,13 @@ static int test_open_stripe(struct btrfs_block_group *cache, u32 sectorsize)
        }
        ret = 0;
 out:
+       /*
+        * The relocation run is retained after retirement, as it is until
+        * relocation ends, and keeps the group on fs_info->open_stripe_bgs.
+        * Nothing here ends a relocation, so free what is left as unmount
+        * does, or the run and the group's reference leak.
+        */
+       btrfs_free_open_stripe_runs(fs_info);
        cache->full_stripe_len = orig_fsl;
        return ret;
 }
index c97e37951c2af7cd70a3564b1bd7d757b9e5ec22..b217566a6c9ba922a11ca5dca59a5a00bf2a50b5 100644 (file)
@@ -23,6 +23,7 @@
 #include "dev-replace.h"
 #include "qgroup.h"
 #include "block-group.h"
+#include "free-space-cache.h"
 #include "raid56.h"
 #include "space-info.h"
 #include "fs.h"
@@ -1346,6 +1347,13 @@ static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans)
        blk_start_plug(&plug);
        ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY);
        blk_finish_plug(&plug);
+       /*
+        * Everything this commit is going to submit has been submitted, so any
+        * rbio still parked waiting for a sibling is waiting for one that will
+        * never come.  Kick them down now rather than let the wait below sit
+        * out their park deadline.
+        */
+       btrfs_flush_parked_rbios(fs_info, 0, U64_MAX, true);
        ret2 = btrfs_wait_extents(fs_info, dirty_pages);
 
        if (ret)
@@ -2659,6 +2667,16 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
                goto scrub_continue;
        }
 
+       /*
+        * Every tree block this transaction allocated is now on disk, so its
+        * metadata stripe runs can be closed and drained.  This is the point
+        * the earlier btrfs_retire_open_stripes() deliberately left them
+        * alone for: closing before here would strand blocks the commit had
+        * not allocated yet, draining before here would wait on writes it had
+        * not issued.
+        */
+       btrfs_retire_meta_stripes(fs_info, cur_trans->transid);
+
        ret = write_all_supers(trans);
        /*
         * the super is written, we can safely allow the tree-loggers
index ee00af739267044ced6de72be0f8fd68d34a6dcc..9045a1b5e2ca82154b439eb4e77d695bf6fa6894 100644 (file)
@@ -2938,6 +2938,28 @@ static int clean_log_buffer(struct btrfs_trans_handle *trans,
        wait_on_extent_buffer_writeback(eb);
        btrfs_tree_unlock(eb);
 
+       /*
+        * A log tree block discarded before it was ever written has no
+        * writeback completion coming: nothing else will report it to its
+        * stripe run, and an unreported allocation keeps the run's inflight
+        * count from ever draining.  Report it abandoned here.  Written
+        * blocks were already reported by their write endio (the writeback
+        * wait above means no submission is still in flight), and log blocks
+        * from a previous mount walked during log replay carry the WRITTEN
+        * flag on disk, so both are excluded.
+        */
+       if (!btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN)) {
+               struct btrfs_block_group *stripe_bg;
+
+               stripe_bg = btrfs_lookup_block_group(fs_info, eb->start);
+               if (stripe_bg) {
+                       if (btrfs_is_stripe_meta_bg(stripe_bg))
+                               btrfs_open_stripe_write_abandoned(stripe_bg,
+                                               eb->start, eb->len);
+                       btrfs_put_block_group(stripe_bg);
+               }
+       }
+
        if (trans) {
                int ret;