]> git.hungrycats.org Git - linux/commitdiff
balance-resume: re-export after the lr1109 misc-next promotion
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 10 Nov 2026 08:09:48 +0000 (03:09 -0500)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 10 Nov 2026 08:09:48 +0000 (03:09 -0500)
misc-next testing 3f4e1b6e5913, zb64 d4cc0ed0de64 (base 83cd81353093;
kdave and btrfs fetched first, no public commit with the base's tree,
so no base-commit line).

- balance-resume is now 4 patches: "btrfs: apply the balance limit_min
  filter only to a limit range" (Fixes: 12907fc79818) first, then the
  vrange bound (2), the limit write-back (3) and the legacy filters (4),
  replayed unchanged.  The cover's headers, shortlog and diffstat are
  regenerated; README's series table and "Before sending" note follow
  the renumbering.
- fixups/post-merge.patch: one hunk offset in volumes.c (+13 lines
  above it), same content.
- The other covers changed only in their Date: headers (reverted).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
README.md
fixups/post-merge.patch
topics/balance-resume/0000-cover-letter.patch
topics/balance-resume/0001-btrfs-apply-the-balance-limit_min-filter-only-to-a-l.patch [new file with mode: 0644]
topics/balance-resume/0001-btrfs-replace-the-balance-resume-heuristics-with-a-v.patch [deleted file]
topics/balance-resume/0002-btrfs-replace-the-balance-resume-heuristics-with-a-v.patch [new file with mode: 0644]
topics/balance-resume/0002-btrfs-write-the-remaining-balance-limits-back-to-the.patch [deleted file]
topics/balance-resume/0003-btrfs-keep-the-old-resume-filters-for-balance-items-.patch [deleted file]
topics/balance-resume/0003-btrfs-write-the-remaining-balance-limits-back-to-the.patch [new file with mode: 0644]
topics/balance-resume/0004-btrfs-keep-the-old-resume-filters-for-balance-items-.patch [new file with mode: 0644]

index 8cdbb8a96da2a42def78e227aa89df6d211f21ed..de16b3c8b98d132b34ca71a764b43d899a9d3f1b 100644 (file)
--- a/README.md
+++ b/README.md
@@ -112,7 +112,7 @@ series applies on top of; the others apply to the base.
 | directory | patches | on | authors | notes |
 |---|---|---|---|---|
 | `logical-ino-costs` | 3 | | Zygo, Kai Krakow | patch 3 is Kai's, already posted by him |
-| `balance-resume` | 3 | | Zygo | patch 3 optional, offered for rejection |
+| `balance-resume` | 4 | | Zygo | patch 1 fixes a pre-existing limit filter bug (independent); patch 4 optional, offered for rejection |
 | `remap-balance-leak` | 1 | | Zygo | was balance-resume patch 1 until 2026-11-08; independent of it |
 | `compress-fixes` | 5 | | Zygo | |
 | `deadlock-fixes` | 3 | | Zygo | the relock fix is folded into patch 2 (2026-10-30) |
@@ -253,7 +253,7 @@ code: it is what `-X ours` drops from conflicting hunks, put back.
 
 - Nothing that is not for upstream is in `topics/`; check the "dropped"
   list that `scripts/export.sh` prints.
-- `balance-resume` patch 3 and `dev-stats` are offered for rejection, as
+- `balance-resume` patch 4 and `dev-stats` are offered for rejection, as
   their cover letters say.
 - `resize-missing` needs Zygo's Signed-off-by.
 - Re-export with `scripts/export.sh` after any lane change, and commit
index d744a3399393fc17105b65cf1d1b66da3a9247af..3881fb257b95b866e11ba6ba36f0e08c47b2b2b6 100644 (file)
@@ -403,10 +403,10 @@ index 1644b1edfe15a6f98308e7377a40e9ebd520274d..90b62d7d59ffe1546cf1e27a5dd5cc87
   * Information about one device.
   *
 diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
-index 6f481b00224e56dedd5b219117aad90bca24353f..68c890eedcefa9321408eab5542d06bd27188e89 100644
+index a7a37ffe79764c9265eec2ec9af18dcf1a0d45e3..3f44fa89ad53aaeb2d6bc8354047133886c64ff9 100644
 --- a/fs/btrfs/volumes.c
 +++ b/fs/btrfs/volumes.c
-@@ -7113,7 +7113,293 @@ bool btrfs_chunk_raid56_mirrored(struct btrfs_fs_info *fs_info, u64 logical)
+@@ -7126,7 +7126,293 @@ bool btrfs_chunk_raid56_mirrored(struct btrfs_fs_info *fs_info, u64 logical)
        return ret;
  }
  
index 95b121b168e9ccaad97c5ce13c810b9cd08552a4..79d2cf314fab697d976bf36c3da7fee1d6243fcb 100644 (file)
@@ -1,6 +1,6 @@
-From 968195f996534fe154954da3246b2b2a3920a653 Mon Sep 17 00:00:00 2001
+From 9732a4fa540f1db0c048915a83d5606dd5615a0e Mon Sep 17 00:00:00 2001
 From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
-Date: Mon, 9 Nov 2026 08:41:05 -0500
+Date: Tue, 10 Nov 2026 03:08:20 -0500
 Subject: [PATCH 0/4] btrfs: balance resume selects what the original request selected
 To: linux-btrfs@vger.kernel.org
 
@@ -198,15 +198,16 @@ Testing (KASAN kernels):
    resume of a 1 TiB item allocates its chunks from 1 TiB up.  A balance
    started on the patched kernel must get the vrange and no usage filter.
 
-Zygo Blaxell (3):
+Zygo Blaxell (4):
+  btrfs: apply the balance limit_min filter only to a limit range
   btrfs: replace the balance resume heuristics with a vrange bound
   btrfs: write the remaining balance limits back to the balance item
   btrfs: keep the old resume filters for balance items without a vrange
 
  fs/btrfs/fs.h      |   6 +
- fs/btrfs/volumes.c | 265 +++++++++++++++++++++++++++++++++++++--------
+ fs/btrfs/volumes.c | 296 ++++++++++++++++++++++++++++++++++++---------
  fs/btrfs/volumes.h |   6 +
- 3 files changed, 231 insertions(+), 46 deletions(-)
+ 3 files changed, 253 insertions(+), 55 deletions(-)
 
 
 -- 
diff --git a/topics/balance-resume/0001-btrfs-apply-the-balance-limit_min-filter-only-to-a-l.patch b/topics/balance-resume/0001-btrfs-apply-the-balance-limit_min-filter-only-to-a-l.patch
new file mode 100644 (file)
index 0000000..56a937c
--- /dev/null
@@ -0,0 +1,87 @@
+From 53738adfdeb5ceba662ef852896dd6b4ea81d4ee Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Mon, 9 Nov 2026 23:34:11 -0500
+Subject: [PATCH 1/4] btrfs: apply the balance limit_min filter only to a limit
+ range
+To: linux-btrfs@vger.kernel.org
+
+The balance limit filter takes a single count (BTRFS_BALANCE_ARGS_LIMIT)
+or a min..max range (BTRFS_BALANCE_ARGS_LIMIT_RANGE), and limit shares
+its bytes with limit_min and limit_max in struct btrfs_balance_args.
+__btrfs_balance() applies the limit_min check to every chunk, on the
+assumption that limit_min is 0 unless a range was given.  With a single
+limit it is not: on little-endian machines limit_min is the low 32 bits
+of limit.
+
+The check runs after should_balance_chunk() has decremented limit for
+the chunk.  With limit=L and M chunks passing the other filters, L > M,
+the counting pass counts M, and the i-th of those chunks (from 1) sees
+limit_min = L - i, so it is skipped while M < L - i.  The first L - M - 1
+chunks are skipped, and each of them still uses up one count of the
+limit.  The balance relocates 2M + 1 - L chunks instead of M, and none
+when L >= 2M + 1.  For example, limit=9 with 7 matching data chunks
+relocates 6.  A limit no larger than the number of matching chunks is
+not affected.
+
+Apply the check only to a limit range: LIMIT_RANGE set and LIMIT not
+set.  should_balance_chunk() applies the single limit when both flags
+are set, so the minimum of the range does not apply then either.
+
+Fixes: 12907fc79818 ("btrfs: extend balance filter limit to take minimum and maximum")
+---
+ fs/btrfs/volumes.c | 31 ++++++++++++++++++++++---------
+ 1 file changed, 22 insertions(+), 9 deletions(-)
+
+diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
+index 3d4ef6725de0e..8cf1645f899a6 100644
+--- a/fs/btrfs/volumes.c
++++ b/fs/btrfs/volumes.c
+@@ -4306,6 +4306,22 @@ static bool chunk_soft_convert_filter(u64 chunk_type, struct btrfs_balance_args
+       return false;
+ }
+ 
++/*
++ * The minimum of a limit range: skip the chunk if the counting pass found
++ * fewer chunks than limit_min.  A single limit has no minimum, and its value
++ * shares its bytes with limit_min and limit_max.  When both flags are set,
++ * should_balance_chunk() applies the single limit, so the minimum does not
++ * apply then either.
++ */
++static bool chunk_limit_min_filter(const struct btrfs_balance_args *bargs, u32 count)
++{
++      if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT) ||
++          !(bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE))
++              return false;
++
++      return count < bargs->limit_min;
++}
++
+ static int should_balance_chunk(struct extent_buffer *leaf, struct btrfs_chunk *chunk,
+                               u64 chunk_offset)
+ {
+@@ -4661,16 +4677,13 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
+                       goto loop;
+               }
+ 
+-              /*
+-               * Apply limit_min filter, no need to check if the LIMITS
+-               * filter is used, limit_min is 0 by default
+-               */
++              /* Apply the limit_min filter of a limit range. */
+               if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
+-                                      count_data < bctl->data.limit_min)
+-                              || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
+-                                      count_meta < bctl->meta.limit_min)
+-                              || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
+-                                      count_sys < bctl->sys.limit_min)) {
++                   chunk_limit_min_filter(&bctl->data, count_data)) ||
++                  ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
++                   chunk_limit_min_filter(&bctl->meta, count_meta)) ||
++                  ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
++                   chunk_limit_min_filter(&bctl->sys, count_sys))) {
+                       mutex_unlock(&fs_info->reclaim_bgs_lock);
+                       goto loop;
+               }
+-- 
+2.47.3
+
diff --git a/topics/balance-resume/0001-btrfs-replace-the-balance-resume-heuristics-with-a-v.patch b/topics/balance-resume/0001-btrfs-replace-the-balance-resume-heuristics-with-a-v.patch
deleted file mode 100644 (file)
index f3298ec..0000000
+++ /dev/null
@@ -1,392 +0,0 @@
-From 26550289a0ae301b3e2e933d1b8a11fe70150786 Mon Sep 17 00:00:00 2001
-From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
-Date: Wed, 16 Sep 2026 02:29:10 -0400
-Subject: [PATCH 1/3] btrfs: replace the balance resume heuristics with a
- vrange bound
-To: linux-btrfs@vger.kernel.org
-
-When a balance is resumed after a pause or an unmount,
-update_balance_args() adds a usage=90 filter to every chunk type that is
-not being converted.  That changes the set of block groups the original
-request selected.  It skips full block groups the interrupted balance had
-not reached yet, so the resumed balance can finish without having done
-what was asked, and it selects again any block group the interrupted
-balance created that is less than 90% full, relocating the same data a
-second time.  For the chunk types that are being converted it adds the
-soft filter instead, so the resume does not convert again the block
-groups the interrupted balance converted, but it also skips the ones
-that already had the target profile when the balance started, which the
-original request selected.
-
-Replace it with a bound fixed when the balance starts.  Before the
-balance item is written, limit the vrange filter of each chunk type to
-the addresses below the next chunk to be allocated: add a 0..end range
-when the request had no vrange filter, or lower the end of the one it
-had.  Every block group that existed when the balance started stays
-selected until the balance relocates it.
-
-New chunks are allocated after the last chunk, but relocation can remove
-the highest chunks without allocating any (their data fits in free space
-lower down), and the next chunk would then reuse their addresses, below
-the bound.  A resumed balance would select it again, although the
-interrupted one, walking downward, never would have.  So while a balance
-item exists, find_next_chunk() never returns an address below the bound:
-an in-memory floor, set when the balance starts and when the balance
-item is recovered at mount, and cleared when the item is deleted.  Every
-chunk allocated in that time, relocation targets included, stays outside
-the range, on the first run and after a resume.  The gap the floor can
-leave in the logical address space is harmless.
-
-A resume now uses the arguments exactly as they were stored: the bound
-already keeps out every block group the interrupted balance created,
-converted ones included, so neither the usage filter nor the soft filter
-is needed, and update_balance_args() goes away.
-
-The bound does not move as the balance progresses.  Block groups the
-balance has relocated are gone, so on resume the walk only revisits the
-chunks it left in place above the point where it stopped (filtered out,
-ENOSPC, active swapfile) and checks them against the filters again.
-Nothing is skipped.
-
-Because the bound is written with the balance item and never changes,
-there is no progress to persist: a resume after an unmount or a crash
-needs no extra writes.
-
-A balance item recovered at mount is limited in the same way before the
-floor is set from it: a vrange 0..end is added to each chunk type that
-has none, and a vrange that ends above the next chunk is lowered to it.
-That covers items written by older kernels, which have no vrange or a
-vrange the user gave, which can end anywhere (a 1 TiB end would put
-every new chunk at 1 TiB and above).  A bound written by this kernel
-changes only when relocation removed the highest chunks and allocated
-none above it; no chunk exists between the next chunk and the old end,
-so lowering it drops nothing.  When the recovered arguments change,
-they are written back to the balance item once, when the balance
-resumes, so a resume that is interrupted again resumes with the same
-bound and not with a new one that includes the chunks the first resume
-allocated.  If that write fails, the balance is not resumed: as for the
-other errors at that point, it ends with the error logged, and its item
-is deleted.  Nothing has been relocated yet, so if the item survives (the
-delete fails too, or the system crashes first), the next mount bounds it
-again to the same chunks.
-
-A balance item written by an older kernel therefore resumes with the
-bound and without the usage or soft filter: the resumed balance restarts
-from the beginning, relocating again every block group the request
-selects, including the ones the interrupted run created or converted,
-but it skips none.
-
-The bound is visible: `btrfs balance status -v`, the
-BTRFS_IOC_BALANCE_PROGRESS ioctl and the "balance: start" and "balance:
-resume" log lines show a vrange the user did not give.
-
----
- fs/btrfs/fs.h      |   6 ++
- fs/btrfs/volumes.c | 188 +++++++++++++++++++++++++++++++++------------
- fs/btrfs/volumes.h |   6 ++
- 3 files changed, 150 insertions(+), 50 deletions(-)
-
-diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
-index 2dd167c32d612..a42aa4de2bcc6 100644
---- a/fs/btrfs/fs.h
-+++ b/fs/btrfs/fs.h
-@@ -785,6 +785,12 @@ struct btrfs_fs_info {
-       atomic_t balance_cancel_req;
-       struct btrfs_balance_control *balance_ctl;
-       wait_queue_head_t balance_wait_q;
-+      /*
-+       * 0, or the end of the balance's vrange bound: while a balance item
-+       * exists (running or paused) find_next_chunk() allocates at or above
-+       * it, so the balance never selects a chunk it allocated.
-+       */
-+      u64 balance_chunk_floor;
- 
-       /* Cancellation requests for chunk relocation */
-       atomic_t reloc_cancel_req;
-diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
-index 3d4ef6725de0e..74f7e96ceb9e0 100644
---- a/fs/btrfs/volumes.c
-+++ b/fs/btrfs/volumes.c
-@@ -2035,7 +2035,7 @@ static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
-       }
-       read_unlock(&fs_info->mapping_tree_lock);
- 
--      return ret;
-+      return max(ret, READ_ONCE(fs_info->balance_chunk_floor));
- }
- 
- static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
-@@ -3970,13 +3970,27 @@ static void btrfs_cpu_balance_args_to_disk(struct btrfs_disk_balance_args *disk,
-       disk->stripes_max = cpu_to_le32(cpu->stripes_max);
- }
- 
-+static void set_balance_item(struct extent_buffer *leaf,
-+                           struct btrfs_balance_item *item,
-+                           const struct btrfs_balance_control *bctl)
-+{
-+      struct btrfs_disk_balance_args disk_bargs;
-+
-+      btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
-+      btrfs_set_balance_data(leaf, item, &disk_bargs);
-+      btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
-+      btrfs_set_balance_meta(leaf, item, &disk_bargs);
-+      btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
-+      btrfs_set_balance_sys(leaf, item, &disk_bargs);
-+      btrfs_set_balance_flags(leaf, item, bctl->flags & ~BTRFS_BALANCE_RESUME);
-+}
-+
- static int insert_balance_item(struct btrfs_fs_info *fs_info,
-                              struct btrfs_balance_control *bctl)
- {
-       struct btrfs_root *root = fs_info->tree_root;
-       struct btrfs_trans_handle *trans;
-       struct btrfs_balance_item *item;
--      struct btrfs_disk_balance_args disk_bargs;
-       struct btrfs_path *path;
-       struct extent_buffer *leaf;
-       struct btrfs_key key;
-@@ -4005,14 +4019,7 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info,
-       item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
- 
-       memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
--
--      btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
--      btrfs_set_balance_data(leaf, item, &disk_bargs);
--      btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
--      btrfs_set_balance_meta(leaf, item, &disk_bargs);
--      btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
--      btrfs_set_balance_sys(leaf, item, &disk_bargs);
--      btrfs_set_balance_flags(leaf, item, bctl->flags);
-+      set_balance_item(leaf, item, bctl);
- out:
-       btrfs_free_path(path);
-       if (ret == 0)
-@@ -4023,6 +4030,43 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info,
-       return ret;
- }
- 
-+/* Write the arguments of a resumed balance to its existing item. */
-+static int update_balance_item(struct btrfs_fs_info *fs_info,
-+                             const struct btrfs_balance_control *bctl)
-+{
-+      struct btrfs_root *root = fs_info->tree_root;
-+      struct btrfs_trans_handle *trans;
-+      BTRFS_PATH_AUTO_FREE(path);
-+      struct btrfs_key key;
-+      int ret;
-+
-+      path = btrfs_alloc_path();
-+      if (!path)
-+              return -ENOMEM;
-+
-+      trans = btrfs_start_transaction(root, 0);
-+      if (IS_ERR(trans))
-+              return PTR_ERR(trans);
-+
-+      key.objectid = BTRFS_BALANCE_OBJECTID;
-+      key.type = BTRFS_TEMPORARY_ITEM_KEY;
-+      key.offset = 0;
-+
-+      ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
-+      if (ret > 0)
-+              ret = -ENOENT;
-+      if (ret < 0) {
-+              btrfs_end_transaction(trans);
-+              return ret;
-+      }
-+
-+      set_balance_item(path->nodes[0],
-+                       btrfs_item_ptr(path->nodes[0], path->slots[0],
-+                                      struct btrfs_balance_item), bctl);
-+      btrfs_release_path(path);
-+      return btrfs_commit_transaction(trans);
-+}
-+
- static int del_balance_item(struct btrfs_fs_info *fs_info)
- {
-       struct btrfs_root *root = fs_info->tree_root;
-@@ -4064,47 +4108,61 @@ static int del_balance_item(struct btrfs_fs_info *fs_info)
-       return ret;
- }
- 
-+static bool limit_balance_vrange(struct btrfs_balance_args *bargs, u64 end)
-+{
-+      if (!(bargs->flags & BTRFS_BALANCE_ARGS_VRANGE)) {
-+              bargs->flags |= BTRFS_BALANCE_ARGS_VRANGE;
-+              bargs->vstart = 0;
-+              bargs->vend = end;
-+              return true;
-+      }
-+      if (bargs->vend <= end)
-+              return false;
-+      bargs->vend = end;
-+      return true;
-+}
-+
- /*
-- * This is a heuristic used to reduce the number of chunks balanced on
-- * resume after balance was interrupted.
-+ * Limit the request to the chunks that exist now.  The bound is written with
-+ * the balance item and never moves, so there is no progress to persist.  New
-+ * chunks are allocated after the last chunk, but once relocation has removed
-+ * the highest chunks the last chunk is lower than the bound, so while the
-+ * balance item exists find_next_chunk() also observes balance_chunk_floor:
-+ * every chunk allocated in that time, relocation targets included, stays
-+ * outside the range, on this run and after a resume.
-+ *
-+ * Return true if any of the arguments changed.
-  */
--static void update_balance_args(struct btrfs_balance_control *bctl)
-+static bool limit_balance_to_existing_chunks(struct btrfs_fs_info *fs_info,
-+                                           struct btrfs_balance_control *bctl)
- {
--      /*
--       * Turn on soft mode for chunk types that were being converted.
--       */
--      if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
--              bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
--      if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
--              bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
--      if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
--              bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
-+      const u64 end = find_next_chunk(fs_info);
-+      bool changed = false;
- 
--      /*
--       * Turn on usage filter if is not already used.  The idea is
--       * that chunks that we have already balanced should be
--       * reasonably full.  Don't do it for chunks that are being
--       * converted - that will keep us from relocating unconverted
--       * (albeit full) chunks.
--       */
--      if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
--          !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
--          !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
--              bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
--              bctl->data.usage = 90;
--      }
--      if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
--          !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
--          !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
--              bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
--              bctl->sys.usage = 90;
--      }
--      if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
--          !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
--          !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
--              bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
--              bctl->meta.usage = 90;
--      }
-+      if (bctl->flags & BTRFS_BALANCE_DATA)
-+              changed |= limit_balance_vrange(&bctl->data, end);
-+      if (bctl->flags & BTRFS_BALANCE_METADATA)
-+              changed |= limit_balance_vrange(&bctl->meta, end);
-+      if (bctl->flags & BTRFS_BALANCE_SYSTEM)
-+              changed |= limit_balance_vrange(&bctl->sys, end);
-+      return changed;
-+}
-+
-+/* The highest vrange end of the selected chunk types, 0 if none has one. */
-+static u64 balance_chunk_floor(const struct btrfs_balance_control *bctl)
-+{
-+      u64 floor = 0;
-+
-+      if ((bctl->flags & BTRFS_BALANCE_DATA) &&
-+          (bctl->data.flags & BTRFS_BALANCE_ARGS_VRANGE))
-+              floor = max(floor, bctl->data.vend);
-+      if ((bctl->flags & BTRFS_BALANCE_METADATA) &&
-+          (bctl->meta.flags & BTRFS_BALANCE_ARGS_VRANGE))
-+              floor = max(floor, bctl->meta.vend);
-+      if ((bctl->flags & BTRFS_BALANCE_SYSTEM) &&
-+          (bctl->sys.flags & BTRFS_BALANCE_ARGS_VRANGE))
-+              floor = max(floor, bctl->sys.vend);
-+      return floor;
- }
- 
- /*
-@@ -4123,6 +4181,7 @@ static void reset_balance_state(struct btrfs_fs_info *fs_info)
- 
-       kfree(bctl);
-       ret = del_balance_item(fs_info);
-+      WRITE_ONCE(fs_info->balance_chunk_floor, 0);
-       if (ret)
-               btrfs_handle_fs_error(fs_info, ret, NULL);
- }
-@@ -5080,6 +5139,9 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
-                               btrfs_bg_type_to_raid_name(data_target));
-       }
- 
-+      if (!(bctl->flags & BTRFS_BALANCE_RESUME))
-+              limit_balance_to_existing_chunks(fs_info, bctl);
-+
-       ret = insert_balance_item(fs_info, bctl);
-       if (ret && ret != -EEXIST)
-               goto out;
-@@ -5090,11 +5152,26 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
-               spin_lock(&fs_info->balance_lock);
-               fs_info->balance_ctl = bctl;
-               spin_unlock(&fs_info->balance_lock);
-+              WRITE_ONCE(fs_info->balance_chunk_floor,
-+                         balance_chunk_floor(bctl));
-       } else {
-               BUG_ON(ret != -EEXIST);
--              spin_lock(&fs_info->balance_lock);
--              update_balance_args(bctl);
--              spin_unlock(&fs_info->balance_lock);
-+              if (bctl->update_item) {
-+                      /*
-+                       * Nothing has been relocated yet, so the item on disk,
-+                       * bounded again at the next mount, is still right.  As
-+                       * for the other errors here, the balance ends and its
-+                       * item is deleted.
-+                       */
-+                      ret = update_balance_item(fs_info, bctl);
-+                      if (ret) {
-+                              btrfs_err(fs_info,
-+                      "balance: failed to write the bounded balance item: %d",
-+                                        ret);
-+                              goto out;
-+                      }
-+                      bctl->update_item = false;
-+              }
-       }
- 
-       ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
-@@ -5247,6 +5324,17 @@ int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
-       btrfs_balance_sys(leaf, item, &disk_bargs);
-       btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
- 
-+      /*
-+       * Bound the request as a new balance would be: an item written by an
-+       * older kernel has no vrange, and one a user gave to such a kernel can
-+       * end anywhere.  The floor is still 0, so find_next_chunk() returns the
-+       * end of the last chunk, and lowering a vend above it drops no block
-+       * group from the range.  The bound is written back to the item when
-+       * the balance resumes, so a resume that is interrupted again keeps it.
-+       */
-+      bctl->update_item = limit_balance_to_existing_chunks(fs_info, bctl);
-+      WRITE_ONCE(fs_info->balance_chunk_floor, balance_chunk_floor(bctl));
-+
-       /*
-        * This should never happen, as the paused balance state is recovered
-        * during mount without any chance of other exclusive ops to collide.
-diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
-index 891bc2432870a..e7112257014e8 100644
---- a/fs/btrfs/volumes.h
-+++ b/fs/btrfs/volumes.h
-@@ -665,6 +665,12 @@ struct btrfs_balance_control {
- 
-       u64 flags;
- 
-+      /*
-+       * The arguments recovered at mount were bounded and differ from the
-+       * balance item: write them back when the balance resumes.
-+       */
-+      bool update_item;
-+
-       struct btrfs_balance_progress stat;
- };
- 
--- 
-2.47.3
-
diff --git a/topics/balance-resume/0002-btrfs-replace-the-balance-resume-heuristics-with-a-v.patch b/topics/balance-resume/0002-btrfs-replace-the-balance-resume-heuristics-with-a-v.patch
new file mode 100644 (file)
index 0000000..463712b
--- /dev/null
@@ -0,0 +1,392 @@
+From cd2e90d19d386642d877eb4170bb8f9d97bd8639 Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Wed, 16 Sep 2026 02:29:10 -0400
+Subject: [PATCH 2/4] btrfs: replace the balance resume heuristics with a
+ vrange bound
+To: linux-btrfs@vger.kernel.org
+
+When a balance is resumed after a pause or an unmount,
+update_balance_args() adds a usage=90 filter to every chunk type that is
+not being converted.  That changes the set of block groups the original
+request selected.  It skips full block groups the interrupted balance had
+not reached yet, so the resumed balance can finish without having done
+what was asked, and it selects again any block group the interrupted
+balance created that is less than 90% full, relocating the same data a
+second time.  For the chunk types that are being converted it adds the
+soft filter instead, so the resume does not convert again the block
+groups the interrupted balance converted, but it also skips the ones
+that already had the target profile when the balance started, which the
+original request selected.
+
+Replace it with a bound fixed when the balance starts.  Before the
+balance item is written, limit the vrange filter of each chunk type to
+the addresses below the next chunk to be allocated: add a 0..end range
+when the request had no vrange filter, or lower the end of the one it
+had.  Every block group that existed when the balance started stays
+selected until the balance relocates it.
+
+New chunks are allocated after the last chunk, but relocation can remove
+the highest chunks without allocating any (their data fits in free space
+lower down), and the next chunk would then reuse their addresses, below
+the bound.  A resumed balance would select it again, although the
+interrupted one, walking downward, never would have.  So while a balance
+item exists, find_next_chunk() never returns an address below the bound:
+an in-memory floor, set when the balance starts and when the balance
+item is recovered at mount, and cleared when the item is deleted.  Every
+chunk allocated in that time, relocation targets included, stays outside
+the range, on the first run and after a resume.  The gap the floor can
+leave in the logical address space is harmless.
+
+A resume now uses the arguments exactly as they were stored: the bound
+already keeps out every block group the interrupted balance created,
+converted ones included, so neither the usage filter nor the soft filter
+is needed, and update_balance_args() goes away.
+
+The bound does not move as the balance progresses.  Block groups the
+balance has relocated are gone, so on resume the walk only revisits the
+chunks it left in place above the point where it stopped (filtered out,
+ENOSPC, active swapfile) and checks them against the filters again.
+Nothing is skipped.
+
+Because the bound is written with the balance item and never changes,
+there is no progress to persist: a resume after an unmount or a crash
+needs no extra writes.
+
+A balance item recovered at mount is limited in the same way before the
+floor is set from it: a vrange 0..end is added to each chunk type that
+has none, and a vrange that ends above the next chunk is lowered to it.
+That covers items written by older kernels, which have no vrange or a
+vrange the user gave, which can end anywhere (a 1 TiB end would put
+every new chunk at 1 TiB and above).  A bound written by this kernel
+changes only when relocation removed the highest chunks and allocated
+none above it; no chunk exists between the next chunk and the old end,
+so lowering it drops nothing.  When the recovered arguments change,
+they are written back to the balance item once, when the balance
+resumes, so a resume that is interrupted again resumes with the same
+bound and not with a new one that includes the chunks the first resume
+allocated.  If that write fails, the balance is not resumed: as for the
+other errors at that point, it ends with the error logged, and its item
+is deleted.  Nothing has been relocated yet, so if the item survives (the
+delete fails too, or the system crashes first), the next mount bounds it
+again to the same chunks.
+
+A balance item written by an older kernel therefore resumes with the
+bound and without the usage or soft filter: the resumed balance restarts
+from the beginning, relocating again every block group the request
+selects, including the ones the interrupted run created or converted,
+but it skips none.
+
+The bound is visible: `btrfs balance status -v`, the
+BTRFS_IOC_BALANCE_PROGRESS ioctl and the "balance: start" and "balance:
+resume" log lines show a vrange the user did not give.
+
+---
+ fs/btrfs/fs.h      |   6 ++
+ fs/btrfs/volumes.c | 188 +++++++++++++++++++++++++++++++++------------
+ fs/btrfs/volumes.h |   6 ++
+ 3 files changed, 150 insertions(+), 50 deletions(-)
+
+diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
+index 2dd167c32d612..a42aa4de2bcc6 100644
+--- a/fs/btrfs/fs.h
++++ b/fs/btrfs/fs.h
+@@ -785,6 +785,12 @@ struct btrfs_fs_info {
+       atomic_t balance_cancel_req;
+       struct btrfs_balance_control *balance_ctl;
+       wait_queue_head_t balance_wait_q;
++      /*
++       * 0, or the end of the balance's vrange bound: while a balance item
++       * exists (running or paused) find_next_chunk() allocates at or above
++       * it, so the balance never selects a chunk it allocated.
++       */
++      u64 balance_chunk_floor;
+ 
+       /* Cancellation requests for chunk relocation */
+       atomic_t reloc_cancel_req;
+diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
+index 8cf1645f899a6..ca364971c3828 100644
+--- a/fs/btrfs/volumes.c
++++ b/fs/btrfs/volumes.c
+@@ -2035,7 +2035,7 @@ static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
+       }
+       read_unlock(&fs_info->mapping_tree_lock);
+ 
+-      return ret;
++      return max(ret, READ_ONCE(fs_info->balance_chunk_floor));
+ }
+ 
+ static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
+@@ -3970,13 +3970,27 @@ static void btrfs_cpu_balance_args_to_disk(struct btrfs_disk_balance_args *disk,
+       disk->stripes_max = cpu_to_le32(cpu->stripes_max);
+ }
+ 
++static void set_balance_item(struct extent_buffer *leaf,
++                           struct btrfs_balance_item *item,
++                           const struct btrfs_balance_control *bctl)
++{
++      struct btrfs_disk_balance_args disk_bargs;
++
++      btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
++      btrfs_set_balance_data(leaf, item, &disk_bargs);
++      btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
++      btrfs_set_balance_meta(leaf, item, &disk_bargs);
++      btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
++      btrfs_set_balance_sys(leaf, item, &disk_bargs);
++      btrfs_set_balance_flags(leaf, item, bctl->flags & ~BTRFS_BALANCE_RESUME);
++}
++
+ static int insert_balance_item(struct btrfs_fs_info *fs_info,
+                              struct btrfs_balance_control *bctl)
+ {
+       struct btrfs_root *root = fs_info->tree_root;
+       struct btrfs_trans_handle *trans;
+       struct btrfs_balance_item *item;
+-      struct btrfs_disk_balance_args disk_bargs;
+       struct btrfs_path *path;
+       struct extent_buffer *leaf;
+       struct btrfs_key key;
+@@ -4005,14 +4019,7 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info,
+       item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
+ 
+       memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
+-
+-      btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
+-      btrfs_set_balance_data(leaf, item, &disk_bargs);
+-      btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
+-      btrfs_set_balance_meta(leaf, item, &disk_bargs);
+-      btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
+-      btrfs_set_balance_sys(leaf, item, &disk_bargs);
+-      btrfs_set_balance_flags(leaf, item, bctl->flags);
++      set_balance_item(leaf, item, bctl);
+ out:
+       btrfs_free_path(path);
+       if (ret == 0)
+@@ -4023,6 +4030,43 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info,
+       return ret;
+ }
+ 
++/* Write the arguments of a resumed balance to its existing item. */
++static int update_balance_item(struct btrfs_fs_info *fs_info,
++                             const struct btrfs_balance_control *bctl)
++{
++      struct btrfs_root *root = fs_info->tree_root;
++      struct btrfs_trans_handle *trans;
++      BTRFS_PATH_AUTO_FREE(path);
++      struct btrfs_key key;
++      int ret;
++
++      path = btrfs_alloc_path();
++      if (!path)
++              return -ENOMEM;
++
++      trans = btrfs_start_transaction(root, 0);
++      if (IS_ERR(trans))
++              return PTR_ERR(trans);
++
++      key.objectid = BTRFS_BALANCE_OBJECTID;
++      key.type = BTRFS_TEMPORARY_ITEM_KEY;
++      key.offset = 0;
++
++      ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
++      if (ret > 0)
++              ret = -ENOENT;
++      if (ret < 0) {
++              btrfs_end_transaction(trans);
++              return ret;
++      }
++
++      set_balance_item(path->nodes[0],
++                       btrfs_item_ptr(path->nodes[0], path->slots[0],
++                                      struct btrfs_balance_item), bctl);
++      btrfs_release_path(path);
++      return btrfs_commit_transaction(trans);
++}
++
+ static int del_balance_item(struct btrfs_fs_info *fs_info)
+ {
+       struct btrfs_root *root = fs_info->tree_root;
+@@ -4064,47 +4108,61 @@ static int del_balance_item(struct btrfs_fs_info *fs_info)
+       return ret;
+ }
+ 
++static bool limit_balance_vrange(struct btrfs_balance_args *bargs, u64 end)
++{
++      if (!(bargs->flags & BTRFS_BALANCE_ARGS_VRANGE)) {
++              bargs->flags |= BTRFS_BALANCE_ARGS_VRANGE;
++              bargs->vstart = 0;
++              bargs->vend = end;
++              return true;
++      }
++      if (bargs->vend <= end)
++              return false;
++      bargs->vend = end;
++      return true;
++}
++
+ /*
+- * This is a heuristic used to reduce the number of chunks balanced on
+- * resume after balance was interrupted.
++ * Limit the request to the chunks that exist now.  The bound is written with
++ * the balance item and never moves, so there is no progress to persist.  New
++ * chunks are allocated after the last chunk, but once relocation has removed
++ * the highest chunks the last chunk is lower than the bound, so while the
++ * balance item exists find_next_chunk() also observes balance_chunk_floor:
++ * every chunk allocated in that time, relocation targets included, stays
++ * outside the range, on this run and after a resume.
++ *
++ * Return true if any of the arguments changed.
+  */
+-static void update_balance_args(struct btrfs_balance_control *bctl)
++static bool limit_balance_to_existing_chunks(struct btrfs_fs_info *fs_info,
++                                           struct btrfs_balance_control *bctl)
+ {
+-      /*
+-       * Turn on soft mode for chunk types that were being converted.
+-       */
+-      if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
+-              bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
+-      if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
+-              bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
+-      if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
+-              bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
++      const u64 end = find_next_chunk(fs_info);
++      bool changed = false;
+ 
+-      /*
+-       * Turn on usage filter if is not already used.  The idea is
+-       * that chunks that we have already balanced should be
+-       * reasonably full.  Don't do it for chunks that are being
+-       * converted - that will keep us from relocating unconverted
+-       * (albeit full) chunks.
+-       */
+-      if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
+-          !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
+-          !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
+-              bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
+-              bctl->data.usage = 90;
+-      }
+-      if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
+-          !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
+-          !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
+-              bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
+-              bctl->sys.usage = 90;
+-      }
+-      if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
+-          !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
+-          !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
+-              bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
+-              bctl->meta.usage = 90;
+-      }
++      if (bctl->flags & BTRFS_BALANCE_DATA)
++              changed |= limit_balance_vrange(&bctl->data, end);
++      if (bctl->flags & BTRFS_BALANCE_METADATA)
++              changed |= limit_balance_vrange(&bctl->meta, end);
++      if (bctl->flags & BTRFS_BALANCE_SYSTEM)
++              changed |= limit_balance_vrange(&bctl->sys, end);
++      return changed;
++}
++
++/* The highest vrange end of the selected chunk types, 0 if none has one. */
++static u64 balance_chunk_floor(const struct btrfs_balance_control *bctl)
++{
++      u64 floor = 0;
++
++      if ((bctl->flags & BTRFS_BALANCE_DATA) &&
++          (bctl->data.flags & BTRFS_BALANCE_ARGS_VRANGE))
++              floor = max(floor, bctl->data.vend);
++      if ((bctl->flags & BTRFS_BALANCE_METADATA) &&
++          (bctl->meta.flags & BTRFS_BALANCE_ARGS_VRANGE))
++              floor = max(floor, bctl->meta.vend);
++      if ((bctl->flags & BTRFS_BALANCE_SYSTEM) &&
++          (bctl->sys.flags & BTRFS_BALANCE_ARGS_VRANGE))
++              floor = max(floor, bctl->sys.vend);
++      return floor;
+ }
+ 
+ /*
+@@ -4123,6 +4181,7 @@ static void reset_balance_state(struct btrfs_fs_info *fs_info)
+ 
+       kfree(bctl);
+       ret = del_balance_item(fs_info);
++      WRITE_ONCE(fs_info->balance_chunk_floor, 0);
+       if (ret)
+               btrfs_handle_fs_error(fs_info, ret, NULL);
+ }
+@@ -5093,6 +5152,9 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
+                               btrfs_bg_type_to_raid_name(data_target));
+       }
+ 
++      if (!(bctl->flags & BTRFS_BALANCE_RESUME))
++              limit_balance_to_existing_chunks(fs_info, bctl);
++
+       ret = insert_balance_item(fs_info, bctl);
+       if (ret && ret != -EEXIST)
+               goto out;
+@@ -5103,11 +5165,26 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
+               spin_lock(&fs_info->balance_lock);
+               fs_info->balance_ctl = bctl;
+               spin_unlock(&fs_info->balance_lock);
++              WRITE_ONCE(fs_info->balance_chunk_floor,
++                         balance_chunk_floor(bctl));
+       } else {
+               BUG_ON(ret != -EEXIST);
+-              spin_lock(&fs_info->balance_lock);
+-              update_balance_args(bctl);
+-              spin_unlock(&fs_info->balance_lock);
++              if (bctl->update_item) {
++                      /*
++                       * Nothing has been relocated yet, so the item on disk,
++                       * bounded again at the next mount, is still right.  As
++                       * for the other errors here, the balance ends and its
++                       * item is deleted.
++                       */
++                      ret = update_balance_item(fs_info, bctl);
++                      if (ret) {
++                              btrfs_err(fs_info,
++                      "balance: failed to write the bounded balance item: %d",
++                                        ret);
++                              goto out;
++                      }
++                      bctl->update_item = false;
++              }
+       }
+ 
+       ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
+@@ -5260,6 +5337,17 @@ int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
+       btrfs_balance_sys(leaf, item, &disk_bargs);
+       btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
+ 
++      /*
++       * Bound the request as a new balance would be: an item written by an
++       * older kernel has no vrange, and one a user gave to such a kernel can
++       * end anywhere.  The floor is still 0, so find_next_chunk() returns the
++       * end of the last chunk, and lowering a vend above it drops no block
++       * group from the range.  The bound is written back to the item when
++       * the balance resumes, so a resume that is interrupted again keeps it.
++       */
++      bctl->update_item = limit_balance_to_existing_chunks(fs_info, bctl);
++      WRITE_ONCE(fs_info->balance_chunk_floor, balance_chunk_floor(bctl));
++
+       /*
+        * This should never happen, as the paused balance state is recovered
+        * during mount without any chance of other exclusive ops to collide.
+diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
+index 891bc2432870a..e7112257014e8 100644
+--- a/fs/btrfs/volumes.h
++++ b/fs/btrfs/volumes.h
+@@ -665,6 +665,12 @@ struct btrfs_balance_control {
+ 
+       u64 flags;
+ 
++      /*
++       * The arguments recovered at mount were bounded and differ from the
++       * balance item: write them back when the balance resumes.
++       */
++      bool update_item;
++
+       struct btrfs_balance_progress stat;
+ };
+ 
+-- 
+2.47.3
+
diff --git a/topics/balance-resume/0002-btrfs-write-the-remaining-balance-limits-back-to-the.patch b/topics/balance-resume/0002-btrfs-write-the-remaining-balance-limits-back-to-the.patch
deleted file mode 100644 (file)
index 1df0714..0000000
+++ /dev/null
@@ -1,158 +0,0 @@
-From b7304f376da97b2bfd6b0b9646226e9926eba81f Mon Sep 17 00:00:00 2001
-From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
-Date: Sun, 8 Nov 2026 22:24:56 -0500
-Subject: [PATCH 2/3] btrfs: write the remaining balance limits back to the
- balance item
-To: linux-btrfs@vger.kernel.org
-
-The limit filters are counted down in memory: should_balance_chunk()
-decrements limit, or limit_max of a limit_min..limit_max range, for every
-chunk that passes the other filters, and __btrfs_balance() restores them
-after its counting pass.  A balance resumed after a pause continues with
-what is left.  The balance item is never updated, so a balance resumed
-after an unmount or a crash starts again from the limits that were given:
-every resume can relocate up to limit more chunks.  The minimum shifts
-too: the counting pass of the resume sees only the chunks that are left,
-which can be fewer than limit_min, and the resume then relocates nothing.
-
-Count a chunk against both ends of the range, and write the remaining
-limits to the balance item, once the chunk has been processed.  limit and
-limit_max are still decremented when the chunk is selected, as before,
-and limit_min is now decremented (down to 0) when it has been processed,
-after the limit_min check has let it through, so limit_min means the
-remaining minimum, on disk and in memory.  A resume after an unmount or a
-crash then continues from the remaining limits, like a resume after a
-pause, and its limit_min check compares the chunks that are left with the
-minimum that is left.  The values written are those of the real pass:
-the counting pass returns to the loop before any chunk is processed, and
-its decrements are undone before the real pass starts.
-
-A chunk counts as processed when its relocation has returned for that
-chunk: relocated, or left in place by ENOSPC or an active swapfile.  This
-is what the in-memory limit has always counted, so a resume after an
-unmount or a crash now behaves exactly like a resume after a pause.  A
-pause cannot stop a chunk halfway: the pause request is checked between
-chunks, so the chunk being relocated is processed and written first.  A
-cancel or a fatal signal can stop a relocation, but it also ends the
-balance and deletes the item.  If the item cannot be written, the balance
-stops with the error, like any other error in the loop, and its item is
-deleted; if the item survives that (an aborted transaction), it holds
-the last committed limits, within the bound below.
-
-The update takes its own handle on the running transaction once the
-relocation has returned, and ends it without committing.  The
-transaction that removed the relocated chunk has ended by then, and is
-usually still the running one, so both changes usually commit together,
-and the update can never commit before the removal.  A crash in between
-loses the count of that one chunk, and the resumed balance can relocate
-one chunk more than requested, never one fewer.  Making the update part
-of the removal's transaction would mean passing the balance into
-btrfs_relocate_chunk(), which device shrink and removal also use.
-
-Only chunk types with a limit filter write the item, once per processed
-chunk; a balance without limits makes no more writes than before.  The
-remaining limits are visible: the BTRFS_IOC_BALANCE_PROGRESS ioctl
-already returned them during the balance and now returns the same values
-after a remount, and the "balance: resume" log line shows them.
-
----
- fs/btrfs/volumes.c | 54 +++++++++++++++++++++++++++++++++++++++++++---
- 1 file changed, 51 insertions(+), 3 deletions(-)
-
-diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
-index 74f7e96ceb9e0..7878a8b926266 100644
---- a/fs/btrfs/volumes.c
-+++ b/fs/btrfs/volumes.c
-@@ -4030,9 +4030,13 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info,
-       return ret;
- }
- 
--/* Write the arguments of a resumed balance to its existing item. */
-+/*
-+ * Write the arguments of a balance to its existing item.  With @commit false
-+ * the update joins the running transaction and is committed with it.
-+ */
- static int update_balance_item(struct btrfs_fs_info *fs_info,
--                             const struct btrfs_balance_control *bctl)
-+                             const struct btrfs_balance_control *bctl,
-+                             bool commit)
- {
-       struct btrfs_root *root = fs_info->tree_root;
-       struct btrfs_trans_handle *trans;
-@@ -4064,6 +4068,8 @@ static int update_balance_item(struct btrfs_fs_info *fs_info,
-                        btrfs_item_ptr(path->nodes[0], path->slots[0],
-                                       struct btrfs_balance_item), bctl);
-       btrfs_release_path(path);
-+      if (!commit)
-+              return btrfs_end_transaction(trans);
-       return btrfs_commit_transaction(trans);
- }
- 
-@@ -4470,6 +4476,44 @@ static int should_balance_chunk(struct extent_buffer *leaf, struct btrfs_chunk *
-       return true;
- }
- 
-+/*
-+ * The limit filters count a chunk when should_balance_chunk() selects it, so
-+ * it counts whether its relocation succeeds or leaves it in place (ENOSPC, an
-+ * active swapfile).  Once the chunk has been processed, count it against
-+ * limit_min as well, and write the remaining limits to the balance item, so a
-+ * balance resumed after an unmount or a crash processes only what is left of
-+ * the request, as one resumed after a pause does.  Chunk types without a limit
-+ * filter write nothing.
-+ */
-+static int count_limited_chunk(struct btrfs_fs_info *fs_info, u64 chunk_type)
-+{
-+      struct btrfs_balance_control *bctl = fs_info->balance_ctl;
-+      struct btrfs_balance_args *bargs;
-+
-+      if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
-+              bargs = &bctl->data;
-+      else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
-+              bargs = &bctl->sys;
-+      else
-+              bargs = &bctl->meta;
-+
-+      if (!(bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
-+              if (!(bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE))
-+                      return 0;
-+              if (bargs->limit_min > 0)
-+                      bargs->limit_min--;
-+      }
-+
-+      /*
-+       * A relocated chunk was removed in a transaction that has ended but
-+       * may not have committed.  The update usually joins that transaction
-+       * and never lands in an earlier one, so a crash can lose the count of
-+       * the last relocated chunk, and the resumed balance can then relocate
-+       * one chunk more than requested, but never one fewer.
-+       */
-+      return update_balance_item(fs_info, bctl, false);
-+}
-+
- struct remap_chunk_info {
-       struct list_head list;
-       u64 offset;
-@@ -4790,6 +4834,10 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
-                       bctl->stat.completed++;
-                       spin_unlock(&fs_info->balance_lock);
-               }
-+
-+              ret = count_limited_chunk(fs_info, chunk_type);
-+              if (ret)
-+                      goto error;
- loop:
-               if (found_key.offset == 0)
-                       break;
-@@ -5163,7 +5211,7 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
-                        * for the other errors here, the balance ends and its
-                        * item is deleted.
-                        */
--                      ret = update_balance_item(fs_info, bctl);
-+                      ret = update_balance_item(fs_info, bctl, true);
-                       if (ret) {
-                               btrfs_err(fs_info,
-                       "balance: failed to write the bounded balance item: %d",
--- 
-2.47.3
-
diff --git a/topics/balance-resume/0003-btrfs-keep-the-old-resume-filters-for-balance-items-.patch b/topics/balance-resume/0003-btrfs-keep-the-old-resume-filters-for-balance-items-.patch
deleted file mode 100644 (file)
index 803da86..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-From 968195f996534fe154954da3246b2b2a3920a653 Mon Sep 17 00:00:00 2001
-From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
-Date: Tue, 13 Oct 2026 14:24:01 -0400
-Subject: [PATCH 3/3] btrfs: keep the old resume filters for balance items
- without a vrange
-To: linux-btrfs@vger.kernel.org
-
-This patch is optional.  It is offered so that reviewers can take it or
-drop it; the author's preference is to drop it.  The previous patches do
-not depend on it.
-
-A balance item written by a kernel without the vrange bound has no vrange
-filter.  With the previous patch alone, a balance that was paused or
-interrupted on such a kernel and is resumed on a new one restarts from
-the beginning: every block group the original request selects is
-relocated again, including the ones the interrupted run created.
-
-This patch restores the old resume adjustments for exactly those items.
-When a chunk type has no vrange filter in the stored item, the resume
-changes its arguments as before: it adds the soft filter if the type is
-being converted, and usage=90 if it has no usage, usage range or convert
-filter.  This is decided when the item is recovered at mount, before
-the previous patch adds the bound, and is written back with the bound.
-A chunk type with a vrange, which is every balance started by a kernel
-with the previous patch and any balance the user gave a vrange, relies
-on the vrange and is resumed unchanged.
-
-The trade-off: the legacy resume neither completes the original request
-(block groups more than 90% full are skipped, and so are the ones being
-converted that already had the target profile) nor avoids much work on a
-filesystem that is more than 90% full, and keeping it adds a layer of
-surprising behavior and code for a case that only arises once, when a
-balance spans the kernel upgrade.  Without it, such a balance repeats
-work but skips nothing.
-
----
- fs/btrfs/volumes.c | 37 +++++++++++++++++++++++++++++++++++++
- 1 file changed, 37 insertions(+)
-
-diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
-index 7878a8b926266..4f8b780992e22 100644
---- a/fs/btrfs/volumes.c
-+++ b/fs/btrfs/volumes.c
-@@ -4114,6 +4114,41 @@ static int del_balance_item(struct btrfs_fs_info *fs_info)
-       return ret;
- }
- 
-+/*
-+ * A balance item written by a kernel without the vrange bound has no vrange
-+ * filter.  Resume it as those kernels did.  A chunk type with a vrange relies
-+ * on it and is resumed unchanged.
-+ */
-+static void update_legacy_balance_args(struct btrfs_balance_args *bargs)
-+{
-+      if (bargs->flags & BTRFS_BALANCE_ARGS_VRANGE)
-+              return;
-+
-+      /* Turn on soft mode for chunk types that were being converted. */
-+      if (bargs->flags & BTRFS_BALANCE_ARGS_CONVERT) {
-+              bargs->flags |= BTRFS_BALANCE_ARGS_SOFT;
-+              return;
-+      }
-+
-+      /*
-+       * Turn on the usage filter if it is not already used.  The idea is
-+       * that chunks that we have already balanced should be reasonably full.
-+       */
-+      if (!(bargs->flags & (BTRFS_BALANCE_ARGS_USAGE |
-+                            BTRFS_BALANCE_ARGS_USAGE_RANGE))) {
-+              bargs->flags |= BTRFS_BALANCE_ARGS_USAGE;
-+              bargs->usage = 90;
-+      }
-+}
-+
-+/* Update the arguments of a balance item recovered at mount. */
-+static void update_balance_args(struct btrfs_balance_control *bctl)
-+{
-+      update_legacy_balance_args(&bctl->data);
-+      update_legacy_balance_args(&bctl->sys);
-+      update_legacy_balance_args(&bctl->meta);
-+}
-+
- static bool limit_balance_vrange(struct btrfs_balance_args *bargs, u64 end)
- {
-       if (!(bargs->flags & BTRFS_BALANCE_ARGS_VRANGE)) {
-@@ -5379,7 +5414,9 @@ int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
-        * end of the last chunk, and lowering a vend above it drops no block
-        * group from the range.  The bound is written back to the item when
-        * the balance resumes, so a resume that is interrupted again keeps it.
-+       * Adjust the chunk types that have no vrange on disk first.
-        */
-+      update_balance_args(bctl);
-       bctl->update_item = limit_balance_to_existing_chunks(fs_info, bctl);
-       WRITE_ONCE(fs_info->balance_chunk_floor, balance_chunk_floor(bctl));
- 
--- 
-2.47.3
-
diff --git a/topics/balance-resume/0003-btrfs-write-the-remaining-balance-limits-back-to-the.patch b/topics/balance-resume/0003-btrfs-write-the-remaining-balance-limits-back-to-the.patch
new file mode 100644 (file)
index 0000000..dcc9b7f
--- /dev/null
@@ -0,0 +1,158 @@
+From 40ddcea6883e9fdd6232327fd6c69e1a15f4d638 Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Sun, 8 Nov 2026 22:24:56 -0500
+Subject: [PATCH 3/4] btrfs: write the remaining balance limits back to the
+ balance item
+To: linux-btrfs@vger.kernel.org
+
+The limit filters are counted down in memory: should_balance_chunk()
+decrements limit, or limit_max of a limit_min..limit_max range, for every
+chunk that passes the other filters, and __btrfs_balance() restores them
+after its counting pass.  A balance resumed after a pause continues with
+what is left.  The balance item is never updated, so a balance resumed
+after an unmount or a crash starts again from the limits that were given:
+every resume can relocate up to limit more chunks.  The minimum shifts
+too: the counting pass of the resume sees only the chunks that are left,
+which can be fewer than limit_min, and the resume then relocates nothing.
+
+Count a chunk against both ends of the range, and write the remaining
+limits to the balance item, once the chunk has been processed.  limit and
+limit_max are still decremented when the chunk is selected, as before,
+and limit_min is now decremented (down to 0) when it has been processed,
+after the limit_min check has let it through, so limit_min means the
+remaining minimum, on disk and in memory.  A resume after an unmount or a
+crash then continues from the remaining limits, like a resume after a
+pause, and its limit_min check compares the chunks that are left with the
+minimum that is left.  The values written are those of the real pass:
+the counting pass returns to the loop before any chunk is processed, and
+its decrements are undone before the real pass starts.
+
+A chunk counts as processed when its relocation has returned for that
+chunk: relocated, or left in place by ENOSPC or an active swapfile.  This
+is what the in-memory limit has always counted, so a resume after an
+unmount or a crash now behaves exactly like a resume after a pause.  A
+pause cannot stop a chunk halfway: the pause request is checked between
+chunks, so the chunk being relocated is processed and written first.  A
+cancel or a fatal signal can stop a relocation, but it also ends the
+balance and deletes the item.  If the item cannot be written, the balance
+stops with the error, like any other error in the loop, and its item is
+deleted; if the item survives that (an aborted transaction), it holds
+the last committed limits, within the bound below.
+
+The update takes its own handle on the running transaction once the
+relocation has returned, and ends it without committing.  The
+transaction that removed the relocated chunk has ended by then, and is
+usually still the running one, so both changes usually commit together,
+and the update can never commit before the removal.  A crash in between
+loses the count of that one chunk, and the resumed balance can relocate
+one chunk more than requested, never one fewer.  Making the update part
+of the removal's transaction would mean passing the balance into
+btrfs_relocate_chunk(), which device shrink and removal also use.
+
+Only chunk types with a limit filter write the item, once per processed
+chunk; a balance without limits makes no more writes than before.  The
+remaining limits are visible: the BTRFS_IOC_BALANCE_PROGRESS ioctl
+already returned them during the balance and now returns the same values
+after a remount, and the "balance: resume" log line shows them.
+
+---
+ fs/btrfs/volumes.c | 54 +++++++++++++++++++++++++++++++++++++++++++---
+ 1 file changed, 51 insertions(+), 3 deletions(-)
+
+diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
+index ca364971c3828..0f6ae59dc4b70 100644
+--- a/fs/btrfs/volumes.c
++++ b/fs/btrfs/volumes.c
+@@ -4030,9 +4030,13 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info,
+       return ret;
+ }
+ 
+-/* Write the arguments of a resumed balance to its existing item. */
++/*
++ * Write the arguments of a balance to its existing item.  With @commit false
++ * the update joins the running transaction and is committed with it.
++ */
+ static int update_balance_item(struct btrfs_fs_info *fs_info,
+-                             const struct btrfs_balance_control *bctl)
++                             const struct btrfs_balance_control *bctl,
++                             bool commit)
+ {
+       struct btrfs_root *root = fs_info->tree_root;
+       struct btrfs_trans_handle *trans;
+@@ -4064,6 +4068,8 @@ static int update_balance_item(struct btrfs_fs_info *fs_info,
+                        btrfs_item_ptr(path->nodes[0], path->slots[0],
+                                       struct btrfs_balance_item), bctl);
+       btrfs_release_path(path);
++      if (!commit)
++              return btrfs_end_transaction(trans);
+       return btrfs_commit_transaction(trans);
+ }
+ 
+@@ -4486,6 +4492,44 @@ static int should_balance_chunk(struct extent_buffer *leaf, struct btrfs_chunk *
+       return true;
+ }
+ 
++/*
++ * The limit filters count a chunk when should_balance_chunk() selects it, so
++ * it counts whether its relocation succeeds or leaves it in place (ENOSPC, an
++ * active swapfile).  Once the chunk has been processed, count it against
++ * limit_min as well, and write the remaining limits to the balance item, so a
++ * balance resumed after an unmount or a crash processes only what is left of
++ * the request, as one resumed after a pause does.  Chunk types without a limit
++ * filter write nothing.
++ */
++static int count_limited_chunk(struct btrfs_fs_info *fs_info, u64 chunk_type)
++{
++      struct btrfs_balance_control *bctl = fs_info->balance_ctl;
++      struct btrfs_balance_args *bargs;
++
++      if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
++              bargs = &bctl->data;
++      else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
++              bargs = &bctl->sys;
++      else
++              bargs = &bctl->meta;
++
++      if (!(bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
++              if (!(bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE))
++                      return 0;
++              if (bargs->limit_min > 0)
++                      bargs->limit_min--;
++      }
++
++      /*
++       * A relocated chunk was removed in a transaction that has ended but
++       * may not have committed.  The update usually joins that transaction
++       * and never lands in an earlier one, so a crash can lose the count of
++       * the last relocated chunk, and the resumed balance can then relocate
++       * one chunk more than requested, but never one fewer.
++       */
++      return update_balance_item(fs_info, bctl, false);
++}
++
+ struct remap_chunk_info {
+       struct list_head list;
+       u64 offset;
+@@ -4803,6 +4847,10 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
+                       bctl->stat.completed++;
+                       spin_unlock(&fs_info->balance_lock);
+               }
++
++              ret = count_limited_chunk(fs_info, chunk_type);
++              if (ret)
++                      goto error;
+ loop:
+               if (found_key.offset == 0)
+                       break;
+@@ -5176,7 +5224,7 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
+                        * for the other errors here, the balance ends and its
+                        * item is deleted.
+                        */
+-                      ret = update_balance_item(fs_info, bctl);
++                      ret = update_balance_item(fs_info, bctl, true);
+                       if (ret) {
+                               btrfs_err(fs_info,
+                       "balance: failed to write the bounded balance item: %d",
+-- 
+2.47.3
+
diff --git a/topics/balance-resume/0004-btrfs-keep-the-old-resume-filters-for-balance-items-.patch b/topics/balance-resume/0004-btrfs-keep-the-old-resume-filters-for-balance-items-.patch
new file mode 100644 (file)
index 0000000..1ccb588
--- /dev/null
@@ -0,0 +1,98 @@
+From 9732a4fa540f1db0c048915a83d5606dd5615a0e Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Tue, 13 Oct 2026 14:24:01 -0400
+Subject: [PATCH 4/4] btrfs: keep the old resume filters for balance items
+ without a vrange
+To: linux-btrfs@vger.kernel.org
+
+This patch is optional.  It is offered so that reviewers can take it or
+drop it; the author's preference is to drop it.  The previous patches do
+not depend on it.
+
+A balance item written by a kernel without the vrange bound has no vrange
+filter.  With the previous patch alone, a balance that was paused or
+interrupted on such a kernel and is resumed on a new one restarts from
+the beginning: every block group the original request selects is
+relocated again, including the ones the interrupted run created.
+
+This patch restores the old resume adjustments for exactly those items.
+When a chunk type has no vrange filter in the stored item, the resume
+changes its arguments as before: it adds the soft filter if the type is
+being converted, and usage=90 if it has no usage, usage range or convert
+filter.  This is decided when the item is recovered at mount, before
+the previous patch adds the bound, and is written back with the bound.
+A chunk type with a vrange, which is every balance started by a kernel
+with the previous patch and any balance the user gave a vrange, relies
+on the vrange and is resumed unchanged.
+
+The trade-off: the legacy resume neither completes the original request
+(block groups more than 90% full are skipped, and so are the ones being
+converted that already had the target profile) nor avoids much work on a
+filesystem that is more than 90% full, and keeping it adds a layer of
+surprising behavior and code for a case that only arises once, when a
+balance spans the kernel upgrade.  Without it, such a balance repeats
+work but skips nothing.
+
+---
+ fs/btrfs/volumes.c | 37 +++++++++++++++++++++++++++++++++++++
+ 1 file changed, 37 insertions(+)
+
+diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
+index 0f6ae59dc4b70..eacc70f2ac90a 100644
+--- a/fs/btrfs/volumes.c
++++ b/fs/btrfs/volumes.c
+@@ -4114,6 +4114,41 @@ static int del_balance_item(struct btrfs_fs_info *fs_info)
+       return ret;
+ }
+ 
++/*
++ * A balance item written by a kernel without the vrange bound has no vrange
++ * filter.  Resume it as those kernels did.  A chunk type with a vrange relies
++ * on it and is resumed unchanged.
++ */
++static void update_legacy_balance_args(struct btrfs_balance_args *bargs)
++{
++      if (bargs->flags & BTRFS_BALANCE_ARGS_VRANGE)
++              return;
++
++      /* Turn on soft mode for chunk types that were being converted. */
++      if (bargs->flags & BTRFS_BALANCE_ARGS_CONVERT) {
++              bargs->flags |= BTRFS_BALANCE_ARGS_SOFT;
++              return;
++      }
++
++      /*
++       * Turn on the usage filter if it is not already used.  The idea is
++       * that chunks that we have already balanced should be reasonably full.
++       */
++      if (!(bargs->flags & (BTRFS_BALANCE_ARGS_USAGE |
++                            BTRFS_BALANCE_ARGS_USAGE_RANGE))) {
++              bargs->flags |= BTRFS_BALANCE_ARGS_USAGE;
++              bargs->usage = 90;
++      }
++}
++
++/* Update the arguments of a balance item recovered at mount. */
++static void update_balance_args(struct btrfs_balance_control *bctl)
++{
++      update_legacy_balance_args(&bctl->data);
++      update_legacy_balance_args(&bctl->sys);
++      update_legacy_balance_args(&bctl->meta);
++}
++
+ static bool limit_balance_vrange(struct btrfs_balance_args *bargs, u64 end)
+ {
+       if (!(bargs->flags & BTRFS_BALANCE_ARGS_VRANGE)) {
+@@ -5392,7 +5427,9 @@ int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
+        * end of the last chunk, and lowering a vend above it drops no block
+        * group from the range.  The bound is written back to the item when
+        * the balance resumes, so a resume that is interrupted again keeps it.
++       * Adjust the chunk types that have no vrange on disk first.
+        */
++      update_balance_args(bctl);
+       bctl->update_item = limit_balance_to_existing_chunks(fs_info, bctl);
+       WRITE_ONCE(fs_info->balance_chunk_floor, balance_chunk_floor(bctl));
+ 
+-- 
+2.47.3
+