]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: give each log commit its own metadata stripe run
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 5 Oct 2026 03:16:03 +0000 (23:16 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:16:31 +0000 (21:16 -0500)
A metadata stripe run is keyed by transaction id alone, so every fsync log
commit inside one transaction appends at the same frontier and into the same
full stripe.  A log commit's tree blocks are durable the instant fsync
returns, so a later writer sharing that stripe -- the next log commit, or the
transaction's ordinary metadata at commit -- has its sub-stripe write
recompute parity over metadata the user was already promised.  That is the
raid56 write hole, on data a user was told was safe.  Padding cannot cover it
either: the earlier blocks are live in the run's liveness map, so
rmw_try_pad_full() must decline.

Give the log tree its own class.  A BTRFS_STRIPE_RUN_META_LOG run is keyed by
the owning subvolume's root id and that subvolume's log_transid, and is
private like the per-inode runs: never in the shared band slots, found by
walking for its key, so nothing else can join it.  Each log commit therefore
gets its own whole stripes and the sharing cannot arise, rather than being
detected and worked around after the fact.

The space cost is temporary by construction, which is the point of keying on
log_transid rather than merely separating log blocks from other metadata.  The
log tree is freed at transaction commit, so a stripe holding only one log
commit's blocks is wholly free once that happens and the commit-time rescan
credits the whole stripe back.  Measured on a 4-device raid5 filesystem with
raid5 metadata and 16K nodes under six fsync writers, trapped space falls from
a 20.2 MiB loaded peak to 1.25 MiB once the load stops and three commits pass,
which is below the 23.7 MiB the plain allocator settles at.  Metadata
total_bytes settles at 2.15x the plain allocator's, and since allocated
metadata chunks are not available to data, that multiple is the real cost on a
small or full filesystem.  Filling to ENOSPC leaves 384 KiB of df-avail, the
same figure the plain allocator gives, so nothing is over-committed.

Both directions of the sharing go to zero, and so does the disk-side witness:
an audit counting uncovered tree blocks whose stripe holds log-tree headers
reads 0 under load, against 223847 without the change.  Padding declines fall
from 36732 uncovered to 1.

Retirement handles both metadata classes through
btrfs_stripe_run_class_is_meta(), since a log run must not be closed and
waited on in the commit-time drain that settles the data classes.  Drain is by
bytenr through end_bbio_meta_write() and clean_log_buffer(), so it is
class-agnostic and covers the log tree's private free path.  A sysfs knob,
stripe_log_run, turns the class off so the change can be measured against
itself on one boot.

log_transid starts over with every transaction's log tree, so a run also
carries its transaction id, and a run from an earlier transaction is
closed rather than joined even when its log_transid matches: that
transaction's commit retires the run and waits for its blocks, and
would wait out its timeout for a block only the later transaction
writes.  Commit-heavy load (relocation of metadata groups during an fsync
storm) hit that within minutes.

Metadata runs drain only when their tree blocks are written, in a commit,
so making a raid56 metadata group read-only (relocation, scrub) commits
the current transaction first when the group has metadata runs, instead
of waiting up to commit_interval for the next one while relocation holds
reclaim_bgs_lock.

The drain waits for metadata runs, at commit and when a group goes
read-only, stay unbounded: a run that never drains has a tree block freed
or allocated without a report, an accounting bug that should show as a
hung commit where it happens rather than as later symptoms.  After 60
seconds they warn once and dump the group's runs (every run class) and
keep waiting.  Only a filesystem error ends them.

Assisted-by: Claude:claude-opus-5
Assisted-by: Claude:claude-opus-5-5
fs/btrfs/block-group.c
fs/btrfs/block-group.h
fs/btrfs/disk-io.c
fs/btrfs/extent-tree.c
fs/btrfs/extent-tree.h
fs/btrfs/fs.h
fs/btrfs/sysfs.c

index 8bf9914d98bce94d0fcc96a53399f4ecebb3b3fd..4333b3067a44f59b2f686bf98d932106a8dd1818 100644 (file)
@@ -533,22 +533,21 @@ struct btrfs_open_stripe_run {
         */
        unsigned long *written;
        u32 live_nbits;                 /* sectors @live covers; caps growth */
+       /* META_LOG: the owning subvolume's log_transid this run belongs to. */
+       u64 log_transid;
 };
 
-static void btrfs_dump_open_meta_runs(struct btrfs_block_group *bg)
+static void btrfs_dump_open_stripe_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;
+       list_for_each_entry(run, &bg->open_stripe_runs, list)
                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);
-       }
+"  run %llu..%llu class %d frontier %llu open %d inflight %llu transid %llu",
+                          run->start, run->end, run->class, run->offset,
+                          run->open, run->inflight_bytes, run->transid);
        spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
 }
 
@@ -561,7 +560,7 @@ static bool bg_has_undrained_meta_runs(struct btrfs_block_group *bg,
 
        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)
+               if (!btrfs_stripe_run_class_is_meta(run->class))
                        continue;
                /*
                 * An open run here belongs to the running transaction: the
@@ -734,7 +733,7 @@ static bool open_stripe_run_dead(const struct btrfs_open_stripe_run *run)
  */
 static bool run_freed_on_drain(const struct btrfs_open_stripe_run *run)
 {
-       return run->class != BTRFS_STRIPE_RUN_META && !run->retained;
+       return !btrfs_stripe_run_class_is_meta(run->class) && !run->retained;
 }
 
 static void free_open_stripe_run(struct btrfs_block_group *bg,
@@ -1115,6 +1114,150 @@ out:
        return ret;
 }
 
+/*
+ * Allocate a log tree block from the private run of this log commit.
+ *
+ * The run is keyed by @owner (the subvolume's root id) and @log_transid, so
+ * each log commit gets whole stripes of its own: its blocks are durable once
+ * fsync returns, and no later writer may share a stripe with them (see
+ * BTRFS_STRIPE_RUN_META_LOG).  An open run of the same owner from an earlier
+ * log commit is closed, as a stale-transid metadata run is in
+ * btrfs_alloc_from_open_stripe().  Like the other private runs, it is never
+ * placed in a band slot.
+ *
+ * Returns 0 and sets *ret_offset, -ENOSPC if no fully-free stripe run in this
+ * block group can satisfy the allocation, or -ENOMEM.
+ */
+int btrfs_alloc_from_log_stripe_run(struct btrfs_block_group *bg, u64 num_bytes,
+                                   u64 owner, u64 log_transid,
+                                   u64 *ret_offset, u64 *available)
+{
+       struct btrfs_fs_info *fs_info = bg->fs_info;
+       const u64 fsl = bg->full_stripe_len;
+       const enum btrfs_stripe_run_class class = BTRFS_STRIPE_RUN_META_LOG;
+       struct btrfs_open_stripe_run *new_run;
+       struct btrfs_open_stripe_run *run;
+       unsigned long flags;
+       u64 tail_start = 0;
+       u64 tail_len = 0;
+       u64 start;
+       u64 len;
+       int ret;
+
+       ASSERT(num_bytes);
+       *available = 0;
+
+       spin_lock_irqsave(&bg->stripe_run_lock, flags);
+       list_for_each_entry(run, &bg->open_stripe_runs, list) {
+               if (run->class != class || run->owner != owner || !run->open)
+                       continue;
+               /*
+                * A previous log commit of the same subvolume.  Its blocks are
+                * durable, so this one must not continue into them; close it
+                * and claim fresh stripes below.  log_transid starts over with
+                * every transaction's log tree, so a run from an earlier
+                * transaction can carry the same log_transid: the transid
+                * tells them apart.  Joining it would hand that transaction's
+                * commit, which retires the run and waits for its blocks, a
+                * block only this transaction will write.
+                */
+               if (run->log_transid != log_transid ||
+                   run->transid != btrfs_get_fs_generation(fs_info)) {
+                       tail_len = close_open_stripe_run(bg, run, &tail_start);
+                       break;
+               }
+               if (num_bytes <= run->end - run->offset) {
+                       *ret_offset = run->offset;
+                       run_live_set(run, run->offset, num_bytes, true);
+                       run->offset += num_bytes;
+                       run->inflight_bytes += num_bytes;
+                       bg->stripe_open_remainder -= num_bytes;
+                       if (run->offset == run->end)
+                               run->open = false; /* full: closed, no tail */
+                       spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+                       stripe_open_remainder_sync(bg);
+                       return 0;
+               }
+               /* Too small for this block: replace it. */
+               tail_len = close_open_stripe_run(bg, run, &tail_start);
+               break;
+       }
+       spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+       if (tail_len) {
+               btrfs_add_free_space(bg, tail_start, tail_len);
+               stripe_open_remainder_sync(bg);
+       }
+
+       /* Zeroed: the error paths below free ->live and must find NULL. */
+       new_run = kzalloc(sizeof(*new_run), GFP_NOFS);
+       if (!new_run)
+               return -ENOMEM;
+
+       ret = btrfs_claim_free_stripe_run(bg,
+                       div64_u64(num_bytes + fsl - 1, fsl) * fsl,
+                       &start, &len);
+       if (ret)
+               goto out;
+       if (len < num_bytes) {
+               btrfs_add_free_space(bg, start, len);
+               *available = max(*available, len);
+               ret = -ENOSPC;
+               goto out;
+       }
+
+       /* Sleeps, so it has to happen before the locks below. */
+       new_run->live = alloc_run_live_map(bg, len, &new_run->live_nbits);
+       new_run->written = new_run->live_nbits ?
+                          bitmap_zalloc(new_run->live_nbits, GFP_NOFS) : NULL;
+
+       spin_lock(&fs_info->open_stripe_lock);
+       if (list_empty(&bg->open_stripe_bg_list)) {
+               btrfs_get_block_group(bg);
+               list_add_tail(&bg->open_stripe_bg_list,
+                             &fs_info->open_stripe_bgs);
+       }
+       spin_lock_irqsave(&bg->stripe_run_lock, flags);
+       if (bg->ro) {
+               spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+               spin_unlock(&fs_info->open_stripe_lock);
+               btrfs_add_free_space(bg, start, len);
+               *available = 0;
+               ret = -ENOSPC;
+               goto out;
+       }
+       new_run->bg = bg;
+       new_run->class = class;
+       new_run->persistent = false;
+       new_run->root = owner;
+       new_run->owner = owner;
+       new_run->log_transid = log_transid;
+       new_run->transid = btrfs_get_fs_generation(fs_info);
+       new_run->start = start;
+       new_run->end = start + len;
+       new_run->offset = start + num_bytes;
+       new_run->inflight_bytes = num_bytes;
+       new_run->open_seq = fs_info->stripe_retire_seq;
+       new_run->open = (new_run->offset != new_run->end);
+       run_live_set(new_run, start, num_bytes, true);
+       list_add_tail(&new_run->list, &bg->open_stripe_runs);
+       bg->stripe_open_remainder += new_run->end - new_run->offset;
+       spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+       spin_unlock(&fs_info->open_stripe_lock);
+
+       atomic64_inc(&fs_info->stripe_run_stats.log_run_opened);
+       stripe_open_remainder_sync(bg);
+       *ret_offset = start;
+       new_run = NULL;
+       ret = 0;
+out:
+       if (new_run) {
+               bitmap_free(new_run->live);
+               bitmap_free(new_run->written);
+       }
+       kfree(new_run);
+       return ret;
+}
+
 /*
  * The owner a private run is found by: the inode, or with
  * stripe_log_data_shared a LOG-class run shared by every inode of the
@@ -2905,7 +3048,7 @@ restart:
                 * 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)
+               if (btrfs_stripe_run_class_is_meta(run->class) && seq != U64_MAX)
                        continue;
                for (class = 0; class < BTRFS_STRIPE_RUN_NR_CLASSES; class++)
                        for (band = 0; band < BTRFS_STRIPE_RUN_NR_BANDS; band++)
@@ -2953,7 +3096,7 @@ static bool bg_open_stripes_settled(struct btrfs_block_group *bg, u64 seq)
                    seq != U64_MAX)
                        continue;
                /* Metadata drains after tree writeback, not here. */
-               if (run->class == BTRFS_STRIPE_RUN_META && seq != U64_MAX)
+               if (btrfs_stripe_run_class_is_meta(run->class) && seq != U64_MAX)
                        continue;
                if (run->open_seq < seq) {
                        ret = false;
@@ -3000,6 +3143,24 @@ static void btrfs_release_stripe_reloc_runs(struct btrfs_fs_info *fs_info)
        spin_unlock(&fs_info->open_stripe_lock);
 }
 
+/* Does @bg have metadata stripe runs? */
+static bool bg_has_meta_runs(struct btrfs_block_group *bg)
+{
+       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 (btrfs_stripe_run_class_is_meta(run->class)) {
+                       ret = true;
+                       break;
+               }
+       }
+       spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+       return ret;
+}
+
 /*
  * 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),
@@ -3013,15 +3174,37 @@ static void btrfs_release_stripe_reloc_runs(struct btrfs_fs_info *fs_info)
 void btrfs_retire_block_group_stripes(struct btrfs_block_group *bg)
 {
        struct btrfs_fs_info *fs_info = bg->fs_info;
+       bool warned = false;
 
        /* 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);
-       wait_var_event(&bg->open_stripe_runs,
-                      bg_open_stripes_settled(bg, U64_MAX) ||
-                      BTRFS_FS_ERROR(fs_info));
+       /*
+        * Metadata runs drain when their tree blocks are written and are
+        * freed by btrfs_retire_meta_stripes(), both in a commit.  Commit now
+        * instead of waiting for the next one; no caller holds a transaction
+        * handle.
+        */
+       if (bg_has_meta_runs(bg))
+               btrfs_commit_current_transaction(fs_info->tree_root);
+       /*
+        * No timeout: a run that does not drain has an allocation that was
+        * never reported written or abandoned, an accounting bug to fix, not
+        * to wait out.  Say which runs once, and keep waiting.
+        */
+       while (!wait_var_event_timeout(&bg->open_stripe_runs,
+                                      bg_open_stripes_settled(bg, U64_MAX) ||
+                                      BTRFS_FS_ERROR(fs_info), 60 * HZ)) {
+               if (warned)
+                       continue;
+               btrfs_warn(fs_info,
+"block group %llu read-only transition waiting for stripe runs that do not drain",
+                          bg->start);
+               btrfs_dump_open_stripe_runs(bg);
+               warned = true;
+       }
        if (BTRFS_FS_ERROR(fs_info) && !bg_open_stripes_settled(bg, U64_MAX))
                btrfs_warn(fs_info,
 "block group %llu read-only with stripe runs still in flight after a filesystem error; not waiting for them",
@@ -3127,6 +3310,7 @@ void btrfs_retire_meta_stripes(struct btrfs_fs_info *fs_info, u64 transid)
        while (!list_empty(&retire_list)) {
                struct btrfs_open_stripe_run *run;
                bool again = true;
+               bool warned = false;
 
                bg = list_first_entry(&retire_list, struct btrfs_block_group,
                                      open_stripe_retire_list);
@@ -3142,7 +3326,7 @@ void btrfs_retire_meta_stripes(struct btrfs_fs_info *fs_info, u64 transid)
                        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 ||
+                               if (!btrfs_stripe_run_class_is_meta(run->class) ||
                                    !run->open)
                                        continue;
                                /*
@@ -3176,18 +3360,22 @@ void btrfs_retire_meta_stripes(struct btrfs_fs_info *fs_info, u64 transid)
 
                /*
                 * 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.
+                * reported all of them and this normally does not wait.  A run
+                * that does not drain has a tree block that was freed without
+                * reporting to its run, an accounting bug: say which runs
+                * once, and keep waiting.  Only a filesystem error ends the
+                * wait early.
                 */
-               if (!wait_var_event_timeout(&bg->open_stripe_runs,
-                                           !bg_has_undrained_meta_runs(bg, transid),
-                                           60 * HZ)) {
+               while (!wait_var_event_timeout(&bg->open_stripe_runs,
+                               !bg_has_undrained_meta_runs(bg, transid) ||
+                               BTRFS_FS_ERROR(fs_info), 60 * HZ)) {
+                       if (warned)
+                               continue;
                        btrfs_warn(fs_info,
-"metadata stripe runs in block group %llu did not drain; a tree block was freed without reporting to its run",
+"metadata stripe runs in block group %llu do not drain; a tree block was freed without reporting to its run",
                                   bg->start);
-                       btrfs_dump_open_meta_runs(bg);
+                       btrfs_dump_open_stripe_runs(bg);
+                       warned = true;
                }
 
                /* Drained and no longer needed for padding or merging. */
@@ -3196,7 +3384,7 @@ void btrfs_retire_meta_stripes(struct btrfs_fs_info *fs_info, u64 transid)
                        struct btrfs_open_stripe_run *next = NULL;
 
                        list_for_each_entry(run, &bg->open_stripe_runs, list) {
-                               if (run->class == BTRFS_STRIPE_RUN_META &&
+                               if (btrfs_stripe_run_class_is_meta(run->class) &&
                                    open_stripe_run_drained(run)) {
                                        next = run;
                                        break;
index b1cbc08ce058d3a42204b39898c57366367a17fe..605e040421e45609cd3b4eb80dd77d09075e7df8 100644 (file)
@@ -55,6 +55,15 @@ enum btrfs_stripe_run_class {
         * (see btrfs_retire_meta_stripes()).
         */
        BTRFS_STRIPE_RUN_META,
+       /*
+        * Tree blocks of one log commit: a private run keyed by the subvolume's
+        * root id and log_transid, never placed in a band slot.  A log commit's
+        * blocks are durable once fsync returns, so no later writer (the next
+        * log commit, or the transaction's own metadata) may share their
+        * stripes.  It also keeps the space cost temporary: the log tree is
+        * freed at transaction commit, which frees its stripes whole.
+        */
+       BTRFS_STRIPE_RUN_META_LOG,
        BTRFS_STRIPE_RUN_NR_CLASSES,
 };
 
@@ -496,6 +505,20 @@ void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg);
 int btrfs_alloc_from_open_stripe(struct btrfs_block_group *bg, u64 num_bytes,
                                 enum btrfs_stripe_run_class class,
                                 u64 *ret_offset, u64 *available);
+int btrfs_alloc_from_log_stripe_run(struct btrfs_block_group *bg, u64 num_bytes,
+                                   u64 owner, u64 log_transid,
+                                   u64 *ret_offset, u64 *available);
+/*
+ * Both metadata classes: tree blocks, retired after tree writeback rather
+ * than in the commit-time drain that settles the data classes.  Every site
+ * that special-cases BTRFS_STRIPE_RUN_META for that reason must use this, or
+ * a log run would be closed and waited on in the wrong place.
+ */
+static inline bool btrfs_stripe_run_class_is_meta(enum btrfs_stripe_run_class c)
+{
+       return c == BTRFS_STRIPE_RUN_META || c == BTRFS_STRIPE_RUN_META_LOG;
+}
+
 int btrfs_alloc_from_inode_stripe_run(struct btrfs_block_group *bg,
                                      struct btrfs_inode *inode,
                                      enum btrfs_stripe_run_class class,
index 593f94f9c42de6bebaf83e69ea17be57ca1cbe2b..8eb61d7c088454d2dddc0c0a1f422db2838ec677 100644 (file)
@@ -2806,6 +2806,11 @@ 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);
        INIT_WORK(&fs_info->stripe_alloc_enable_work, btrfs_stripe_alloc_enable_work);
+       /*
+        * Closing the log-tree fsync write hole is on by default; sysfs can
+        * turn it off so the change can be measured against itself on one boot.
+        */
+       fs_info->stripe_log_run = true;
        fs_info->stripe_log_data_shared = true;
        fs_info->stripe_park_timeout_ms = BTRFS_RBIO_PARK_TIMEOUT_MS;
        fs_info->stripe_compact = 10;
index 39fac849accd03a8b3d8c80835c0e160186451f4..1ad3d605e45434c4b7d609df8f5da2476dda632a 100644 (file)
@@ -4320,7 +4320,20 @@ static int do_allocation_stripe(struct btrfs_block_group *block_group,
         * through the normal metadata allocator.
         */
        if (btrfs_is_stripe_meta_bg(block_group)) {
-               ret = btrfs_alloc_from_open_stripe(block_group,
+               /*
+                * Log tree blocks take a private run of their own, keyed by
+                * the log commit, so that no later writer can share a stripe
+                * with blocks an fsync has already made durable.  Everything
+                * else shares the transaction's metadata runs.
+                */
+               if (ffe_ctl->for_treelog && fs_info->stripe_log_run)
+                       ret = btrfs_alloc_from_log_stripe_run(block_group,
+                                                  ffe_ctl->num_bytes,
+                                                  ffe_ctl->log_owner,
+                                                  ffe_ctl->log_transid,
+                                                  &offset, &available);
+               else
+                       ret = btrfs_alloc_from_open_stripe(block_group,
                                                   ffe_ctl->num_bytes,
                                                   BTRFS_STRIPE_RUN_META,
                                                   &offset, &available);
@@ -5161,6 +5174,13 @@ int btrfs_reserve_extent(struct btrfs_root *root, struct btrfs_inode *inode,
        u64 flags;
        int ret;
        bool for_treelog = (btrfs_root_id(root) == BTRFS_TREE_LOG_OBJECTID);
+       /*
+        * A log root's key offset is the subvolume it logs, and log_transid
+        * names the commit; together they key the private stripe run that
+        * keeps one log commit's blocks off every later writer's stripes.
+        */
+       u64 log_owner = for_treelog ? root->root_key.offset : 0;
+       u64 log_transid = for_treelog ? root->log_transid : 0;
        bool for_data_reloc = (btrfs_is_data_reloc_root(root) && is_data);
        struct btrfs_inode *steer_inode = NULL;
        enum btrfs_stripe_run_class steer_class = BTRFS_STRIPE_RUN_COW;
@@ -5212,6 +5232,8 @@ again:
        ffe_ctl.delalloc = delalloc;
        ffe_ctl.hint_byte = hint_byte;
        ffe_ctl.for_treelog = for_treelog;
+       ffe_ctl.log_owner = log_owner;
+       ffe_ctl.log_transid = log_transid;
        ffe_ctl.for_data_reloc = for_data_reloc;
        ffe_ctl.steer_inode = steer_inode;
        ffe_ctl.steer_class = steer_class;
index f650cc79b094dc40cfae9fc5923a1c35a74cf676..751babb11dedb125bf6b48441cd5de287517a6f8 100644 (file)
@@ -45,6 +45,13 @@ struct find_free_extent_ctl {
 
        /* Allocation is called for tree-log */
        bool for_treelog;
+       /*
+        * Log tree allocations only: the owning subvolume's root id and its
+        * log_transid, the key of the private stripe run this log commit's
+        * tree blocks belong to.  See BTRFS_STRIPE_RUN_META_LOG.
+        */
+       u64 log_owner;
+       u64 log_transid;
 
        /* Allocation is called for data relocation */
        bool for_data_reloc;
index a21750342d1e547c9384b96bcba6acd515b9c046..9e639f1788cb15f1fc73a5483584d0c0f1ce0403 100644 (file)
@@ -959,6 +959,13 @@ struct btrfs_fs_info {
                atomic64_t audit_err;
        } stripe_rmw_audit_stats;
 #endif
+       /*
+        * Give each log commit its own private metadata stripe run
+        * (BTRFS_STRIPE_RUN_META_LOG), so that no later writer shares a stripe
+        * with tree blocks an fsync has already made durable.  On by default
+        * (sysfs stripe_log_run).
+        */
+       bool stripe_log_run;
        /*
         * LOG-class data runs.  Set (the default): runs are shared per
         * (subvolume, log_transid); a log commit seals the stripe holding the
@@ -992,6 +999,8 @@ struct btrfs_fs_info {
                atomic64_t compact_stripes;
                /* live bytes compaction moved */
                atomic64_t compact_bytes;
+               /* private per-log-commit metadata runs opened */
+               atomic64_t log_run_opened;
                /* log commits that waited to settle a logged extent, and how long */
                atomic64_t log_settle_waits;
                atomic64_t log_settle_wait_ns;
index 48b10f03e809219858fdd54d1de3806439521237..6ea904c9805e352c2ebe0496aa8614afef412fb4 100644 (file)
@@ -1190,7 +1190,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", "meta",
+               "cow", "reloc", "log", "nocow", "meta", "meta_log",
        };
        int live_by_class[BTRFS_STRIPE_RUN_NR_CLASSES];
        int open_runs[BTRFS_STRIPE_RUN_NR_CLASSES];
@@ -1267,7 +1267,8 @@ static ssize_t btrfs_stripe_run_stats_show(struct kobject *kobj,
                "compact_runs %lld\n"
                "compact_fallbacks %lld\n"
                "compact_stripes %lld\n"
-               "compact_bytes %lld\n",
+               "compact_bytes %lld\n"
+               "log_run_opened %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),
@@ -1278,7 +1279,8 @@ static ssize_t btrfs_stripe_run_stats_show(struct kobject *kobj,
                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));
+               atomic64_read(&fs_info->stripe_run_stats.compact_bytes),
+               atomic64_read(&fs_info->stripe_run_stats.log_run_opened));
 }
 BTRFS_ATTR(, stripe_run_stats, btrfs_stripe_run_stats_show);
 
@@ -1314,6 +1316,36 @@ BTRFS_ATTR_RW(, _name, btrfs_##_name##_show, btrfs_##_name##_store)
 
 BTRFS_PARK_TIMEOUT_ATTR(stripe_park_timeout_ms, stripe_park_timeout_ms);
 
+/*
+ * Whether each log commit gets its own private metadata stripe run.  0 puts
+ * log tree blocks back in the transaction's shared metadata runs, which
+ * reopens the log-tree fsync write hole.
+ */
+static ssize_t btrfs_stripe_log_run_show(struct kobject *kobj,
+                                        struct kobj_attribute *a, char *buf)
+{
+       struct btrfs_fs_info *fs_info = to_fs_info(kobj);
+
+       return sysfs_emit(buf, "%d\n", fs_info->stripe_log_run ? 1 : 0);
+}
+
+static ssize_t btrfs_stripe_log_run_store(struct kobject *kobj,
+                                         struct kobj_attribute *a,
+                                         const char *buf, size_t len)
+{
+       struct btrfs_fs_info *fs_info = to_fs_info(kobj);
+       bool val;
+       int ret;
+
+       ret = kstrtobool(buf, &val);
+       if (ret)
+               return ret;
+       fs_info->stripe_log_run = val;
+       return len;
+}
+BTRFS_ATTR_RW(, stripe_log_run, btrfs_stripe_log_run_show,
+             btrfs_stripe_log_run_store);
+
 static ssize_t btrfs_stripe_log_data_shared_show(struct kobject *kobj,
                                                 struct kobj_attribute *a, char *buf)
 {
@@ -1771,6 +1803,7 @@ static const struct attribute *btrfs_attrs[] = {
        BTRFS_ATTR_PTR(, stripe_park_stats),
        BTRFS_ATTR_PTR(, stripe_run_stats),
        BTRFS_ATTR_PTR(, stripe_park_timeout_ms),
+       BTRFS_ATTR_PTR(, stripe_log_run),
        BTRFS_ATTR_PTR(, stripe_log_data_shared),
        BTRFS_ATTR_PTR(, stripe_park_sync_timeout_ms),
        BTRFS_ATTR_PTR(, stripe_park_congestion),