]> git.hungrycats.org Git - linux/commitdiff
btrfs: keep the old resume filters for balance items without a vrange 7.3/topics/balance-resume
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 13 Oct 2026 18:24:01 +0000 (14:24 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 9 Nov 2026 03:24:59 +0000 (22:24 -0500)
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.

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/volumes.c

index c8ed0bccb035c3b0d3683216bf18a652ca13722f..35d4c3e468f0a214d489010e889ba6b9f8d0ca3b 100644 (file)
@@ -4076,6 +4076,41 @@ out:
        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)) {
@@ -5380,7 +5415,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));