]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_meta: hold back whole stripes so tree blocks always land and trapped...
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 19 Sep 2026 17:18:12 +0000 (13:18 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:16:31 +0000 (21:16 -0500)
Two failures at the metadata fill edge under stripe_meta, both seen
with the tiny-file fill test on raid5 metadata.

The first is a transaction abort.  A tree block is reserved in bytes
and admitted while claimable whole stripes cover the outstanding
reservations, but it is placed in whole stripes: the open run it would
have joined is closed at the transaction boundary, so every group
claims at least one fresh full stripe per transaction.  The last few
megabytes of claimable supply go to those claims, made for reservations
admitted earlier, and the next admitted tree block finds no stripe at
all ("stripe_meta: tree block allocation of 16384 bytes returned
ENOSPC: claimable 0 open 1622016 trapped 1614266368"), which aborts the
transaction.  Pre-pay two transactions' worth of claims -- twice the
number of writable groups times the widest full stripe -- in the
space_info's stripe margin, recomputed at each commit scan.  Best-fit
run selection opens a fresh run only when a group's runs are full, so
this is a few megabytes, not a fraction of the space_info.

The second is a filesystem that quietly runs out at half capacity.
COW frees trap stripes rather than free them: a stripe is claimable
again only once every tree block in it is dead, which random deaths in
a sixteen-block stripe essentially never achieve, so the METADATA
space_info settles around half used, half trapped and no claimable
stripe left (3.04 GiB metadata, 49% used, fs not writable, deletes
included).  Only relocation packs live tree blocks back into whole
stripes, and it needs the moved group's live bytes claimable elsewhere
before it starts -- by the time the trigger fires there is nothing
left.  Hold back a reserve of whole stripes -- the largest writable
group's length, capped at a quarter of the space_info, nothing with a
single group -- from every metadata reservation except those made by
the relocation task, identified by a task pointer that reloc_ctl
already implies.  Reservations admitted against the reserve reach the
same tickets, so the flag travels with the ticket.  The reserve is an
accounting hold-back, not a partition: a committing transaction still
allocates from those stripes when it must, and the next scan restates
the reserve from what remains.  Both counters are cleared when
stripe_alloc is turned off, since the scan that maintains them stops
with it.

The reserve is reported through sysfs as bytes_stripe_reserve and in
the space_info dump, whose stripe_claimable and stripe_margin values
were printed under each other's label.

The reserve belongs to the whole of btrfs_relocate_block_group(), not
only the stretches where reloc_ctl is set: relocation reserves metadata
before its first stage (create_reloc_inode() starts a transaction for
six items), and that reservation, made as an ordinary user, fails with
-ENOSPC exactly when relocation is needed -- once the claimable supply
has fallen below the reserve.  fs_info->reloc_owner records the task
for the whole run (reloc_chunk_start() serializes it) and counts as
relocation here.

Who may draw on the reserve:

- Relocation, for the whole of btrfs_relocate_block_group(), which
  reloc_chunk_start() serializes: the reservation create_reloc_inode()
  makes before the first stage must not stay behind the reserve, or every
  metadata relocation fails exactly when the supply is below it (511 of
  536 failed relocations of a raid6/raid6 octopus run ended so).  Making a
  group read-only, relocation's first step, draws on it too.

- The transaction machinery's own reservations, NO_FLUSH, FLUSH_LIMIT and
  EMERGENCY: the reserve holds back user operations so relocation and the
  commit after it have room, and must not hold back the commit (an
  extent tree COW freeing deleted leaves aborted the transaction with
  163 MiB of whole stripes unused).  User reservations (FLUSH_ALL,
  ALL_STEAL, EVICT, DATA) still stop at it.

- The global reserve steal is refused once the claimable supply is down
  to the reserve plus the claim margin: an inline unlink frees no stripe
  and each transaction of unlinks takes fresh ones, so stolen
  reservations end in an abort.  The unlink fails with ENOSPC instead.
  The orphan cleanup at mount and ro->rw remount (the task is recorded in
  btrfs_start_pre_rw_mount()) steals regardless: a failed eviction there
  fails the read-write mount, and a filesystem that cannot be mounted
  read-write can never run the relocation the reserve is for.

The reserve is a multiple of the metadata full stripe (stripe_round_up(),
not round_up(): the widths are not powers of two).

Raid56 metadata groups go through a runtime enable or disable too: the
arm and disarm sweep covers METADATA space_infos, the commit-time
metadata retirement runs whether or not the policy is on (so a disable
does not leave the running transaction's metadata runs and their group
references until unmount), and the enable drain commits the running
transaction before arming the write-hole check, since tree blocks the
legacy allocator placed in it are written by that commit into partly used
stripes.  That one commit's exposure is inherent.

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

index cd6fe47d6b0f4d8d80b48087ea78289e0c05b6de..73c7b5575c39d5ec5cba36911500144e825f0a8b 100644 (file)
@@ -607,6 +607,11 @@ static u64 stripe_round_down(u64 val, u64 fsl)
        return div64_u64(val, fsl) * fsl;
 }
 
+static u64 stripe_round_up(u64 val, u64 fsl)
+{
+       return stripe_round_down(val + fsl - 1, fsl);
+}
+
 static bool stripe_is_aligned(u64 val, u64 fsl)
 {
        return stripe_round_down(val, fsl) == val;
@@ -2349,8 +2354,9 @@ int btrfs_stripe_alloc_check_support(struct btrfs_fs_info *fs_info)
 }
 
 /*
- * Arm or disarm stripe_unusable accounting across all raid56 data block
- * groups whose caches are loaded.  Same walk as the commit-time rescan.
+ * Arm or disarm stripe_unusable accounting across all raid56 data and
+ * metadata block groups whose caches are loaded.  Same walk as the
+ * commit-time rescan.
  */
 static void stripe_alloc_sweep_groups(struct btrfs_fs_info *fs_info, bool arm)
 {
@@ -2359,8 +2365,26 @@ static void stripe_alloc_sweep_groups(struct btrfs_fs_info *fs_info, bool arm)
        list_for_each_entry(sinfo, &fs_info->space_info, list) {
                int raid;
 
-               if (!(sinfo->flags & BTRFS_BLOCK_GROUP_DATA))
+               if (!(sinfo->flags & (BTRFS_BLOCK_GROUP_DATA |
+                                     BTRFS_BLOCK_GROUP_METADATA)))
                        continue;
+               /*
+                * Metadata: the scan that maintains the stripe_meta margin
+                * and reserve stops with the option, so clear them here or
+                * they hold metadata back forever.  The groups themselves
+                * are armed and disarmed below like data groups: raid56
+                * metadata places tree blocks in whole stripes from the
+                * moment the option is on, so its trapped and claimable
+                * bytes count from that moment and stop counting when it
+                * goes off.
+                */
+               if (!arm && (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)) {
+                       spin_lock(&sinfo->lock);
+                       sinfo->bytes_stripe_margin = 0;
+                       sinfo->bytes_stripe_reserve = 0;
+                       btrfs_try_granting_tickets(sinfo);
+                       spin_unlock(&sinfo->lock);
+               }
                /* The writeback quarantine is the gate's; it goes with it. */
                if (!arm && (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)) {
                        spin_lock(&sinfo->lock);
@@ -2427,6 +2451,13 @@ void btrfs_stripe_alloc_enable_work(struct work_struct *work)
 
        btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
        btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
+       /*
+        * Tree blocks the legacy allocator placed in the transaction running at
+        * the flip are written into partly used stripes by that transaction's
+        * commit.  Wait for it before arming the check, which would otherwise
+        * report those writes as violations.
+        */
+       btrfs_commit_current_transaction(fs_info->tree_root);
        clear_bit(BTRFS_FS_STRIPE_ALLOC_ENABLING, &fs_info->flags);
        btrfs_info(fs_info,
           "stripe_alloc: writes placed before the policy was enabled have landed");
@@ -3108,9 +3139,13 @@ void btrfs_retire_meta_stripes(struct btrfs_fs_info *fs_info, u64 transid)
        LIST_HEAD(retire_list);
        unsigned long flags;
 
-       if (!btrfs_test_opt(fs_info, STRIPE_ALLOC))
-               return;
-
+       /*
+        * No option test: a runtime disable leaves the running transaction's
+        * metadata runs open, and the commit that follows still has to close
+        * and drain them or their block groups never drop their references.
+        * The list is empty when nothing is open, so this costs nothing on a
+        * filesystem that never had the policy.
+        */
        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);
@@ -3329,6 +3364,9 @@ void btrfs_scan_stripe_unusable(struct btrfs_fs_info *fs_info, bool force)
                u64 total_claimable = 0;
                u64 total_claimable_reloc = 0;
                u64 total_stranded = 0;
+               u64 largest = 0;
+               u64 unit = 0;
+               u32 nr_rw = 0;
                int raid;
 
                /*
@@ -3388,7 +3426,10 @@ void btrfs_scan_stripe_unusable(struct btrfs_fs_info *fs_info, bool force)
                                        if (bg->start == READ_ONCE(fs_info->data_reloc_bg))
                                                total_claimable_reloc +=
                                                        bg->stripe_claimable;
+                                       nr_rw++;
+                                       largest = max(largest, bg->length);
                                }
+                               unit = max(unit, bg->full_stripe_len);
                        }
                }
                up_read(&sinfo->groups_sem);
@@ -3397,6 +3438,42 @@ void btrfs_scan_stripe_unusable(struct btrfs_fs_info *fs_info, bool force)
                sinfo->bytes_stripe_claimable = total_claimable;
                sinfo->bytes_stripe_claimable_reloc = total_claimable_reloc;
                sinfo->bytes_stripe_stranded = total_stranded;
+               /*
+                * Not if the option went off since the check above: the
+                * disable path has cleared the margin and reserve, and no
+                * later scan would clear them again.
+                */
+               if (!(sinfo->flags & BTRFS_BLOCK_GROUP_DATA) &&
+                   btrfs_test_opt(fs_info, STRIPE_ALLOC)) {
+                       u64 reserve = 0;
+
+                       /*
+                        * stripe_meta.  A tree block is reserved in bytes but
+                        * placed in whole stripes: the run it would join is
+                        * closed at the transaction boundary, so each group
+                        * claims at least one fresh stripe per transaction.
+                        * Pre-pay two transactions' worth of such claims, or
+                        * the last claimable stripes go to claims made for
+                        * reservations admitted earlier and an admitted tree
+                        * block finds none, which aborts the transaction.
+                        */
+                       sinfo->bytes_stripe_margin = 2ULL * nr_rw * unit;
+                       /*
+                        * COW frees trap stripes (a stripe is claimable only
+                        * once every block in it is dead), and only relocation
+                        * packs live blocks back into whole stripes, which
+                        * needs room elsewhere for the moved group.  With more
+                        * than one group, hold back one group's worth, capped
+                        * at a quarter of the space_info, from all but
+                        * relocation.  Accounting only: a committing
+                        * transaction may still allocate here.
+                        */
+                       if (nr_rw > 1 && unit)
+                               reserve = stripe_round_up(min(largest,
+                                                             sinfo->total_bytes / 4),
+                                                         unit);
+                       sinfo->bytes_stripe_reserve = reserve;
+               }
                /*
                 * The rescan usually lowers the counter (mid-transaction
                 * incremental adds overcount conservatively); admission
@@ -4629,8 +4706,15 @@ static int inc_block_group_ro(struct btrfs_block_group *cache, bool force)
 
                if (READ_ONCE(cache->stripe_unusable_ready))
                        trapped = min(READ_ONCE(cache->stripe_unusable), num_bytes);
+               /*
+                * The stripe_meta reserve is the room a group's live blocks
+                * move into when it is relocated, and going read-only is the
+                * first step of exactly that: count the reserve as available
+                * here, or the guard refuses every relocation at the edge by
+                * the width of the reserve.
+                */
                if (btrfs_can_overcommit(sinfo, num_bytes - trapped,
-                                        BTRFS_RESERVE_NO_FLUSH))
+                                        BTRFS_RESERVE_NO_FLUSH, true))
                        ret = 0;
        }
 
index a54c687aee50aae2922f32666401b79636235478..593f94f9c42de6bebaf83e69ea17be57ca1cbe2b 100644 (file)
@@ -3148,17 +3148,22 @@ int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info)
        if (ret)
                return ret;
 
+       WRITE_ONCE(fs_info->mount_cleanup_task, current);
        ret = btrfs_cleanup_fs_roots(fs_info);
-       if (ret)
+       if (ret) {
+               WRITE_ONCE(fs_info->mount_cleanup_task, NULL);
                return ret;
+       }
 
        down_read(&fs_info->cleanup_work_sem);
        if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) ||
            (ret = btrfs_orphan_cleanup(fs_info->tree_root))) {
                up_read(&fs_info->cleanup_work_sem);
+               WRITE_ONCE(fs_info->mount_cleanup_task, NULL);
                return ret;
        }
        up_read(&fs_info->cleanup_work_sem);
+       WRITE_ONCE(fs_info->mount_cleanup_task, NULL);
 
        mutex_lock(&fs_info->cleaner_mutex);
        ret = btrfs_recover_relocation(fs_info);
index 1117b14e23d53c78dea1eb6cd9e4d22cc9f2405f..2106eec0978127e0c2ad7d6d66b96678a68978ac 100644 (file)
@@ -2452,7 +2452,7 @@ static bool stripe_reclaim_meta_room(struct btrfs_fs_info *fs_info)
                return true;
        spin_lock(&meta->lock);
        ok = btrfs_space_info_used(meta, true) + bytes <= meta->total_bytes ||
-            btrfs_can_overcommit(meta, bytes, BTRFS_RESERVE_FLUSH_ALL);
+            btrfs_can_overcommit(meta, bytes, BTRFS_RESERVE_FLUSH_ALL, true);
        spin_unlock(&meta->lock);
        return ok;
 }
index 686494b85d3e4a10ca0d7a989b0e94efa0de722f..a21750342d1e547c9384b96bcba6acd515b9c046 100644 (file)
@@ -789,6 +789,30 @@ struct btrfs_fs_info {
        struct btrfs_space_info *data_sinfo;
 
        struct reloc_control *reloc_ctl;
+       /*
+        * The task running relocation while reloc_ctl is set.  Under
+        * stripe_meta it alone may reserve metadata out of the whole stripes
+        * held back in bytes_stripe_reserve, so that a trapped metadata
+        * group can always be relocated (see btrfs_can_overcommit()).
+        */
+       struct task_struct *reloc_task;
+       /*
+        * The task inside btrfs_relocate_block_group(), for its whole run
+        * (relocation is serialized by reloc_chunk_start()).  reloc_task only
+        * covers the stretches where reloc_ctl is set; the reservations made
+        * before and between them -- creating the data reloc inode first of
+        * all -- are relocation's too, and may draw on the same reserve.
+        */
+       struct task_struct *reloc_owner;
+       /*
+        * The task running the mount-time (and ro->rw remount) orphan
+        * cleanup in btrfs_start_pre_rw_mount().  Its evictions may steal
+        * from the global reserve even below the stripe_meta reserve: a
+        * mount that cannot finish orphan cleanup fails, and a filesystem
+        * that cannot be mounted read-write can never run the relocation
+        * the reserve is held back for.
+        */
+       struct task_struct *mount_cleanup_task;
 
        /* data_alloc_cluster is only used in ssd_spread mode */
        struct btrfs_free_cluster data_alloc_cluster;
index 89a915b46e72727fcb85854b16b96dd3bfaed893..d9f218e128f6c2104c2dc7c06bd371b546a3f54e 100644 (file)
@@ -3598,6 +3598,7 @@ static void set_reloc_control(struct reloc_control *rc)
        spin_lock(&fs_info->reloc_ctl_lock);
        fs_info->reloc_ctl = rc;
        spin_unlock(&fs_info->reloc_ctl_lock);
+       WRITE_ONCE(fs_info->reloc_task, current);
        mutex_unlock(&fs_info->reloc_mutex);
 }
 
@@ -3609,6 +3610,7 @@ static void unset_reloc_control(struct reloc_control *rc)
        spin_lock(&fs_info->reloc_ctl_lock);
        fs_info->reloc_ctl = NULL;
        spin_unlock(&fs_info->reloc_ctl_lock);
+       WRITE_ONCE(fs_info->reloc_task, NULL);
        mutex_unlock(&fs_info->reloc_mutex);
 }
 
@@ -5736,6 +5738,7 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
        ret = reloc_chunk_start(fs_info);
        if (ret < 0)
                goto out_put_rc;
+       WRITE_ONCE(fs_info->reloc_owner, current);
 
        ret = btrfs_inc_block_group_ro(rc->block_group, true);
        if (ret)
@@ -5837,6 +5840,7 @@ out:
        if (!btrfs_is_zoned(fs_info))
                btrfs_release_data_reloc_bg(fs_info);
        btrfs_free_path(path);
+       WRITE_ONCE(fs_info->reloc_owner, NULL);
        reloc_chunk_end(fs_info);
 out_put_rc:
        put_reloc_control(rc);
index 2d9010624bbc00647ca0b2df83124a0650f89886..563a8832732bbc96adb32160150a15a1e347792a 100644 (file)
@@ -187,6 +187,10 @@ struct reserve_ticket {
        u64 bytes;
        int error;
        bool steal;
+       /* Made by the relocation task: may draw on bytes_stripe_reserve. */
+       bool reloc;
+       /* Made by the mount-time orphan cleanup: may steal regardless. */
+       bool mount_cleanup;
        /*
         * A preallocation: one whole stripe of collateral is charged into
         * bytes_stripe_margin in the same critical section that grants the
@@ -684,7 +688,7 @@ static inline bool can_overcommit(const struct btrfs_space_info *space_info,
 }
 
 bool btrfs_can_overcommit(const struct btrfs_space_info *space_info, u64 bytes,
-                         enum btrfs_reserve_flush_enum flush)
+                         enum btrfs_reserve_flush_enum flush, bool reloc)
 {
        u64 used;
 
@@ -693,6 +697,13 @@ bool btrfs_can_overcommit(const struct btrfs_space_info *space_info, u64 bytes,
                return false;
 
        used = btrfs_space_info_used(space_info, true);
+       /*
+        * The stripe_meta reserve exists for relocation: it is the room in
+        * which the live tree blocks of a trapped group are packed into whole
+        * stripes.  Everyone else sees it as used.
+        */
+       if (reloc)
+               used -= min(used, space_info->bytes_stripe_reserve);
 
        return check_can_overcommit(space_info, used, bytes, flush);
 }
@@ -924,13 +935,26 @@ again:
        while (!list_empty(head)) {
                struct reserve_ticket *ticket;
                u64 used_after;
+               u64 tused;
 
                ticket = list_first_entry(head, struct reserve_ticket, list);
                used_after = used + ticket->bytes;
+               /*
+                * Relocation's tickets may draw on the stripe_meta reserve: for
+                * this ticket only, the tickets after it still see it as used.
+                */
+               tused = used;
+               if (ticket->reloc)
+                       tused -= min(tused, space_info->bytes_stripe_reserve);
 
-               /* Check and see if our ticket can be satisfied now. */
-               if ((used_after <= space_info->total_bytes ||
-                    can_overcommit(space_info, used, ticket->bytes, flush)) &&
+               /*
+                * Check and see if our ticket can be satisfied now.  The
+                * gate's relocation rule is for FLUSH_DATA_RELOC, which never
+                * waits on a ticket (can_ticket()); ticket->reloc is the
+                * stripe_meta reserve's eligibility, a different question.
+                */
+               if ((tused + ticket->bytes <= space_info->total_bytes ||
+                    can_overcommit(space_info, tused, ticket->bytes, flush)) &&
                    stripe_claimable_admit(space_info, ticket->bytes,
                                           ticket->stripe_margin, false)) {
                        btrfs_space_info_update_bytes_may_use(space_info, ticket->bytes);
@@ -983,13 +1007,13 @@ static void __btrfs_dump_space_info(const struct btrfs_space_info *info)
                   (s64)(info->total_bytes - btrfs_space_info_used(info, true)),
                   info->full ? "" : "not ");
        btrfs_info(fs_info,
-"space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu zone_unusable=%llu stripe_unusable=%llu stripe_open=%llu stripe_margin=%llu stripe_claimable=%llu",
+"space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu zone_unusable=%llu stripe_unusable=%llu stripe_open=%llu stripe_claimable=%llu stripe_margin=%llu stripe_reserve=%llu",
                info->total_bytes, info->bytes_used, info->bytes_pinned,
                info->bytes_reserved, info->bytes_may_use,
                info->bytes_readonly, info->bytes_zone_unusable,
                info->bytes_stripe_unusable, info->bytes_stripe_open,
                info->bytes_stripe_claimable,
-               info->bytes_stripe_margin);
+               info->bytes_stripe_margin, info->bytes_stripe_reserve);
 }
 
 void btrfs_dump_space_info(struct btrfs_space_info *info, u64 bytes,
@@ -1482,6 +1506,22 @@ static bool steal_from_global_rsv(struct btrfs_space_info *space_info,
        if (global_rsv->space_info != space_info)
                return false;
 
+       /*
+        * stripe_meta: the global reserve is refilled at every commit, but its
+        * bytes are only as real as the whole stripes behind them.  Once the
+        * claimable supply is down to the relocation reserve, a stolen
+        * reservation lets a transaction consume fresh stripes through COW
+        * while its frees come back trapped, until a tree block finds no stripe
+        * and the commit aborts.  Refuse the steal: -ENOSPC is recoverable and
+        * an abort is not.  Mount-time orphan cleanup is exempt: it is bounded,
+        * and failing it fails the mount, leaving no way to relocate.
+        */
+       if (space_info->bytes_stripe_reserve && !ticket->mount_cleanup &&
+           space_info->bytes_stripe_claimable <
+           space_info->bytes_stripe_reserve + space_info->bytes_stripe_margin +
+           ticket->bytes)
+               return false;
+
        spin_lock(&global_rsv->lock);
        min_bytes = mult_perc(global_rsv->size, 10);
        if (global_rsv->reserved < min_bytes + ticket->bytes) {
@@ -2176,6 +2216,20 @@ static int __reserve_bytes(struct btrfs_space_info *space_info, u64 orig_bytes,
        u64 used;
        int ret = -ENOSPC;
        bool pending_tickets;
+       /*
+        * Who may draw on the stripe_meta reserve (bytes_stripe_reserve):
+        * the relocation task, which is what the reserve is for, and the
+        * transaction machinery's own bounded reservations -- NO_FLUSH from
+        * btrfs_use_block_rsv() when a handle's reserve runs dry mid-COW,
+        * FLUSH_LIMIT for delayed refs and delayed inodes, EMERGENCY -- which
+        * a committing transaction cannot fail without aborting.  User
+        * operations (FLUSH_ALL, ALL_STEAL, EVICT, DATA) stay behind it.
+        */
+       const bool reloc = (READ_ONCE(fs_info->reloc_task) == current) ||
+                          (READ_ONCE(fs_info->reloc_owner) == current) ||
+                          flush == BTRFS_RESERVE_NO_FLUSH ||
+                          flush == BTRFS_RESERVE_FLUSH_LIMIT ||
+                          flush == BTRFS_RESERVE_FLUSH_EMERGENCY;
 
        ASSERT(orig_bytes, "orig_bytes=%llu", orig_bytes);
        /*
@@ -2215,6 +2269,8 @@ static int __reserve_bytes(struct btrfs_space_info *space_info, u64 orig_bytes,
         * Carry on if we have enough space (short-circuit) OR call
         * can_overcommit() to ensure we can overcommit to continue.
         */
+       if (reloc)
+               used -= min(used, space_info->bytes_stripe_reserve);
        if (!pending_tickets &&
            ((used + orig_bytes <= space_info->total_bytes) ||
             can_overcommit(space_info, used, orig_bytes, flush)) &&
@@ -2253,6 +2309,9 @@ static int __reserve_bytes(struct btrfs_space_info *space_info, u64 orig_bytes,
                init_waitqueue_head(&ticket.wait);
                spin_lock_init(&ticket.lock);
                ticket.steal = can_steal(flush);
+               ticket.reloc = reloc;
+               ticket.mount_cleanup =
+                       READ_ONCE(fs_info->mount_cleanup_task) == current;
                ticket.hold_stripe = held != NULL;
                ticket.held = 0;
                ticket.stripe_margin = stripe_margin;
index 5e13008c688f597dbf16db0edba37e5fdc38d5a7..a38a36362e9bc0514da8c0bc276fa1473762fed0 100644 (file)
@@ -192,16 +192,21 @@ struct btrfs_space_info {
                                           reservations must not count on it
                                           either.  Synced from the block groups'
                                           stripe_open_remainder. */
-       u64 bytes_stripe_margin;        /* stripe_alloc pessimistic reservation
-                                          margin: one full stripe width per
-                                          outstanding delalloc extent.  Pre-pays
-                                          the worst-case sub-stripe tail each
-                                          future extent can strand when its run
-                                          closes, so commits between write() and
-                                          writeback can never trap space out
-                                          from under an admitted reservation.
-                                          Charged and released in lockstep with
-                                          the inodes' outstanding_extents. */
+       u64 bytes_stripe_margin;        /* data: one full stripe per
+                                          outstanding delalloc extent or
+                                          preallocation in flight, pre-paying
+                                          the sub-stripe tail a commit can trap
+                                          before writeback (see
+                                          btrfs_stripe_margin_mod()).
+                                          stripe_meta metadata: two
+                                          transactions' worth of stripe claims,
+                                          set at each commit scan. */
+       u64 bytes_stripe_reserve;       /* stripe_meta: whole stripes held back
+                                          for relocation (and the transaction
+                                          machinery's own reservations), so a
+                                          trapped metadata group can always be
+                                          moved.  Counted as used; recomputed
+                                          at each commit scan. */
        u64 bytes_stripe_quarantine;    /* data: what buffered writeback that
                                           failed with ENOSPC released, held
                                           out of the admission supply until
@@ -370,6 +375,7 @@ static inline u64 btrfs_space_info_used(const struct btrfs_space_info *s_info,
                s_info->bytes_zone_unusable +
                s_info->bytes_stripe_unusable + s_info->bytes_stripe_open +
                s_info->bytes_stripe_margin +
+               s_info->bytes_stripe_reserve +
                (may_use_included ? s_info->bytes_may_use : 0);
 }
 
@@ -399,7 +405,7 @@ int btrfs_reserve_metadata_bytes(struct btrfs_space_info *space_info,
                                 enum btrfs_reserve_flush_enum flush);
 void btrfs_try_granting_tickets(struct btrfs_space_info *space_info);
 bool btrfs_can_overcommit(const struct btrfs_space_info *space_info, u64 bytes,
-                         enum btrfs_reserve_flush_enum flush);
+                         enum btrfs_reserve_flush_enum flush, bool reloc);
 
 static inline void btrfs_space_info_free_bytes_may_use(
                                struct btrfs_space_info *space_info,
index 60bf7da1790b4842b088381bc612b6bf1cb98603..48b10f03e809219858fdd54d1de3806439521237 100644 (file)
@@ -883,6 +883,7 @@ SPACE_INFO_ATTR(bytes_stripe_stranded);
 SPACE_INFO_ATTR(bytes_stripe_claimable);
 SPACE_INFO_ATTR(bytes_stripe_open);
 SPACE_INFO_ATTR(bytes_stripe_margin);
+SPACE_INFO_ATTR(bytes_stripe_reserve);
 SPACE_INFO_ATTR(bytes_stripe_quarantine);
 SPACE_INFO_ATTR(disk_used);
 SPACE_INFO_ATTR(disk_total);
@@ -1018,6 +1019,7 @@ static struct attribute *space_info_attrs[] = {
        BTRFS_ATTR_PTR(space_info, bytes_stripe_claimable),
        BTRFS_ATTR_PTR(space_info, bytes_stripe_open),
        BTRFS_ATTR_PTR(space_info, bytes_stripe_margin),
+       BTRFS_ATTR_PTR(space_info, bytes_stripe_reserve),
        BTRFS_ATTR_PTR(space_info, bytes_stripe_quarantine),
        BTRFS_ATTR_PTR(space_info, disk_used),
        BTRFS_ATTR_PTR(space_info, disk_total),