should_balance_chunk() already treats a METADATA_REMAP chunk as a
metadata chunk: the metadata type flag selects it, the metadata filters
apply to it, and it is charged against the metadata limit or limit_max
when it is selected, so the remap chunks queued by __btrfs_balance()
stay within the MAX of -mlimit=MIN..MAX. The counting pass of
__btrfs_balance() also counts it in count_meta.
The limit_min check in the second pass is the only place that tests the
raw METADATA bit, which a METADATA_REMAP chunk does not have. With
-mlimit=MIN..MAX and fewer than MIN matching metadata chunks, the
balance skips every METADATA chunk, as asked, but still relocates the
METADATA_REMAP chunk.
Test the METADATA_REMAP bit in that check as well, so that remap chunks
count as metadata in both passes and the limit bounds the total number
of block groups relocated.
Fixes: 81e5a4551c32 ("btrfs: allow balancing remap tree")
Assisted-by: Claude:claude-opus-5-5
/* Apply the limit_min filter of a limit range. */
if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
chunk_limit_min_filter(&bctl->data, count_data)) ||
- ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
+ ((chunk_type & (BTRFS_BLOCK_GROUP_METADATA |
+ BTRFS_BLOCK_GROUP_METADATA_REMAP)) &&
chunk_limit_min_filter(&bctl->meta, count_meta)) ||
((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
chunk_limit_min_filter(&bctl->sys, count_sys))) {