From 5cec55a2741762713980cd58a1e8ae724bf96b1a Mon Sep 17 00:00:00 2001 From: Zygo Blaxell Date: Tue, 13 Oct 2026 14:24:01 -0400 Subject: [PATCH] btrfs: keep the old resume filters for balance items without a vrange 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 | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index c8ed0bccb035c..35d4c3e468f0a 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -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)); -- 2.53.0