]> git.hungrycats.org Git - linux/commitdiff
btrfs: write the remaining balance limits back to the balance item
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 9 Nov 2026 03:25:01 +0000 (22:25 -0500)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 9 Nov 2026 03:25:01 +0000 (22:25 -0500)
The limit filters are counted down in memory: should_balance_chunk()
decrements limit, or limit_max of a limit_min..limit_max range, for every
chunk that passes the other filters, and __btrfs_balance() restores them
after its counting pass.  A balance resumed after a pause continues with
what is left.  The balance item is never updated, so a balance resumed
after an unmount or a crash starts again from the limits that were given:
every resume can relocate up to limit more chunks.  The minimum shifts
too: the counting pass of the resume sees only the chunks that are left,
which can be fewer than limit_min, and the resume then relocates nothing.

Count a chunk against both ends of the range, and write the remaining
limits to the balance item, once the chunk has been processed.  limit and
limit_max are still decremented when the chunk is selected, as before,
and limit_min is now decremented (down to 0) when it has been processed,
after the limit_min check has let it through, so limit_min means the
remaining minimum, on disk and in memory.  A resume after an unmount or a
crash then continues from the remaining limits, like a resume after a
pause, and its limit_min check compares the chunks that are left with the
minimum that is left.  The values written are those of the real pass:
the counting pass returns to the loop before any chunk is processed, and
its decrements are undone before the real pass starts.

A chunk counts as processed when its relocation has returned for that
chunk: relocated, or left in place by ENOSPC or an active swapfile.  This
is what the in-memory limit has always counted, so a resume after an
unmount or a crash now behaves exactly like a resume after a pause.  A
pause cannot stop a chunk halfway: the pause request is checked between
chunks, so the chunk being relocated is processed and written first.  A
cancel or a fatal signal can stop a relocation, but it also ends the
balance and deletes the item.  If the item cannot be written, the balance
stops with the error, like any other error in the loop, and its item is
deleted; if the item survives that (an aborted transaction), it holds
the last committed limits, within the bound below.

The update takes its own handle on the running transaction once the
relocation has returned, and ends it without committing.  The
transaction that removed the relocated chunk has ended by then, and is
usually still the running one, so both changes usually commit together,
and the update can never commit before the removal.  A crash in between
loses the count of that one chunk, and the resumed balance can relocate
one chunk more than requested, never one fewer.  Making the update part
of the removal's transaction would mean passing the balance into
btrfs_relocate_chunk(), which device shrink and removal also use.

Only chunk types with a limit filter write the item, once per processed
chunk; a balance without limits makes no more writes than before.  The
remaining limits are visible: the BTRFS_IOC_BALANCE_PROGRESS ioctl
already returned them during the balance and now returns the same values
after a remount, and the "balance: resume" log line shows them.

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

index 24f50b1ac146e3bc276d701057b252201226a92d..23ebee5b917fd4e1b4ef6bc84126e1c9335a204a 100644 (file)
@@ -3825,9 +3825,13 @@ out:
        return ret;
 }
 
-/* Write the arguments of a resumed balance to its existing item. */
+/*
+ * Write the arguments of a balance to its existing item.  With @commit false
+ * the update joins the running transaction and is committed with it.
+ */
 static int update_balance_item(struct btrfs_fs_info *fs_info,
-                              const struct btrfs_balance_control *bctl)
+                              const struct btrfs_balance_control *bctl,
+                              bool commit)
 {
        struct btrfs_root *root = fs_info->tree_root;
        struct btrfs_trans_handle *trans;
@@ -3859,6 +3863,8 @@ static int update_balance_item(struct btrfs_fs_info *fs_info,
                         btrfs_item_ptr(path->nodes[0], path->slots[0],
                                        struct btrfs_balance_item), bctl);
        btrfs_release_path(path);
+       if (!commit)
+               return btrfs_end_transaction(trans);
        return btrfs_commit_transaction(trans);
 }
 
@@ -4253,6 +4259,44 @@ static bool should_balance_chunk(struct extent_buffer *leaf, struct btrfs_chunk
        return true;
 }
 
+/*
+ * The limit filters count a chunk when should_balance_chunk() selects it, so
+ * it counts whether its relocation succeeds or leaves it in place (ENOSPC, an
+ * active swapfile).  Once the chunk has been processed, count it against
+ * limit_min as well, and write the remaining limits to the balance item, so a
+ * balance resumed after an unmount or a crash processes only what is left of
+ * the request, as one resumed after a pause does.  Chunk types without a limit
+ * filter write nothing.
+ */
+static int count_limited_chunk(struct btrfs_fs_info *fs_info, u64 chunk_type)
+{
+       struct btrfs_balance_control *bctl = fs_info->balance_ctl;
+       struct btrfs_balance_args *bargs;
+
+       if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
+               bargs = &bctl->data;
+       else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
+               bargs = &bctl->sys;
+       else
+               bargs = &bctl->meta;
+
+       if (!(bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
+               if (!(bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE))
+                       return 0;
+               if (bargs->limit_min > 0)
+                       bargs->limit_min--;
+       }
+
+       /*
+        * A relocated chunk was removed in a transaction that has ended but
+        * may not have committed.  The update usually joins that transaction
+        * and never lands in an earlier one, so a crash can lose the count of
+        * the last relocated chunk, and the resumed balance can then relocate
+        * one chunk more than requested, but never one fewer.
+        */
+       return update_balance_item(fs_info, bctl, false);
+}
+
 static int __btrfs_balance(struct btrfs_fs_info *fs_info)
 {
        struct btrfs_balance_control *bctl = fs_info->balance_ctl;
@@ -4421,6 +4465,10 @@ again:
                        bctl->stat.completed++;
                        spin_unlock(&fs_info->balance_lock);
                }
+
+               ret = count_limited_chunk(fs_info, chunk_type);
+               if (ret)
+                       goto error;
 loop:
                if (found_key.offset == 0)
                        break;
@@ -4781,7 +4829,7 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
                         * for the other errors here, the balance ends and its
                         * item is deleted.
                         */
-                       ret = update_balance_item(fs_info, bctl);
+                       ret = update_balance_item(fs_info, bctl, true);
                        if (ret) {
                                btrfs_err(fs_info,
                        "balance: failed to write the bounded balance item: %d",