]> git.hungrycats.org Git - linux/commitdiff
btrfs: give back the limit of queued remap chunks when balance bails out
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Wed, 11 Nov 2026 05:32:31 +0000 (00:32 -0500)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Wed, 11 Nov 2026 05:32:31 +0000 (00:32 -0500)
__btrfs_balance() does not relocate METADATA_REMAP chunks in its chunk
walk: it queues them and balances them all after the walk, in
balance_remap_chunks().  should_balance_chunk() selects them as metadata
chunks, and a metadata limit filter counts them down when it selects
them, like any other chunk.

A pause request ends the walk at the error label, which drops the queue:
the queued chunks are not balanced, but the metadata limit still counts
them.  The balance control stays in memory for the resume, which then
sees the chunks again and finds the limit already used up for them.  For
example, with -mlimit=1 and the remap chunk selected as the one metadata
chunk, a balance paused while it relocates the data chunks below the
remap chunk relocates no metadata chunk at all after the resume.

Add the dropped chunks back to the metadata limit (limit, or limit_max of
a range, whichever should_balance_chunk() counted down) at the error
label.  For the other ways out through the label (cancel, errors) the
balance ends and the count does not matter.

Fixes: 81e5a4551c32 ("btrfs: allow balancing remap tree")
Assisted-by: Claude:claude-opus-5-5
fs/btrfs/volumes.c

index a0d7f4faf23dbe6aa7a40a830967a59acece69ca..79df7e08b43028abd1a3822443ff23a561d36db7 100644 (file)
@@ -4769,7 +4769,18 @@ loop:
                }
        }
 error:
-       /* Entries left over when the loop bailed out before balancing them. */
+       /*
+        * Entries left over when the loop bailed out before balancing them.
+        * should_balance_chunk() took each of them from the metadata limit
+        * when it selected them.  Give that back, so a balance resumed after
+        * a pause still processes as many chunks as it was asked to.
+        */
+       if (!list_empty(&remap_chunks)) {
+               if (bctl->meta.flags & BTRFS_BALANCE_ARGS_LIMIT)
+                       bctl->meta.limit += num_remap_chunks;
+               else if (bctl->meta.flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
+                       bctl->meta.limit_max += num_remap_chunks;
+       }
        while (!list_empty(&remap_chunks)) {
                rci = list_first_entry(&remap_chunks, struct remap_chunk_info,
                                       list);