]> git.hungrycats.org Git - linux/commitdiff
btrfs: apply the balance limit_min filter only to a limit range
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 10 Nov 2026 04:34:16 +0000 (23:34 -0500)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 10 Nov 2026 04:34:16 +0000 (23:34 -0500)
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")
Assisted-by: Claude:claude-opus-5-5
fs/btrfs/volumes.c

index 200b4e4a07994a6be460d1d7e8b9394c6d7966d6..a1a3023554a494547865547e8d9951b7110fa225 100644 (file)
@@ -4089,6 +4089,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 bool should_balance_chunk(struct extent_buffer *leaf, struct btrfs_chunk *chunk,
                                 u64 chunk_offset)
 {
@@ -4302,16 +4318,13 @@ again:
                        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;
                }