]> git.hungrycats.org Git - linux/commitdiff
btrfs: do not write queued remap chunks as processed to the balance item
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Wed, 11 Nov 2026 05:32:39 +0000 (00:32 -0500)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Wed, 11 Nov 2026 05:32:39 +0000 (00:32 -0500)
The balance item gets the remaining limits after each processed chunk
(count_limited_chunk()).  METADATA_REMAP chunks are not processed in the
chunk walk: __btrfs_balance() queues them and balances them after the
walk, but should_balance_chunk() has already counted them down from the
metadata limit when it selected them.  Any limited chunk processed after
that writes the item with the queued chunks counted as done.  If the
balance stops before the end of the walk, by a pause, an unmount or a
crash, the queue is gone, and the balance resumed from the item has too
little left of its metadata limit to balance the chunks it never
balanced: with -mlimit=1..1 and the remap chunk selected as the one
metadata chunk, the resumed balance balances no metadata chunk.

Write the item with the queued chunks added back to the metadata limit
(limit, or limit_max of a range, as should_balance_chunk() counted them).
The in-memory limit is not changed: the walk needs it to stop selecting
chunks once the limit is used up.

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

index f9e299b382ed017000101dea0ffbc027284dda61..2fd20d58d43e0180a4aa20d442aa807e3a6e4b4a 100644 (file)
@@ -3970,15 +3970,26 @@ static void btrfs_cpu_balance_args_to_disk(struct btrfs_disk_balance_args *disk,
        disk->stripes_max = cpu_to_le32(cpu->stripes_max);
 }
 
+/*
+ * @queued_meta: metadata chunks the limit filter has counted that are queued
+ * but not processed yet.  Their count is written back to the metadata limit.
+ */
 static void set_balance_item(struct extent_buffer *leaf,
                             struct btrfs_balance_item *item,
-                            const struct btrfs_balance_control *bctl)
+                            const struct btrfs_balance_control *bctl,
+                            u32 queued_meta)
 {
        struct btrfs_disk_balance_args disk_bargs;
+       struct btrfs_balance_args meta = bctl->meta;
+
+       if (meta.flags & BTRFS_BALANCE_ARGS_LIMIT)
+               meta.limit += queued_meta;
+       else if (meta.flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
+               meta.limit_max += queued_meta;
 
        btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
        btrfs_set_balance_data(leaf, item, &disk_bargs);
-       btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
+       btrfs_cpu_balance_args_to_disk(&disk_bargs, &meta);
        btrfs_set_balance_meta(leaf, item, &disk_bargs);
        btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
        btrfs_set_balance_sys(leaf, item, &disk_bargs);
@@ -4019,7 +4030,7 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info,
        item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
 
        memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
-       set_balance_item(leaf, item, bctl);
+       set_balance_item(leaf, item, bctl, 0);
 out:
        btrfs_free_path(path);
        if (ret == 0)
@@ -4031,12 +4042,13 @@ out:
 }
 
 /*
- * Write the arguments of a balance to its existing item.  With @commit false
- * the update joins the running transaction and is committed with it.
+ * Write the arguments of a balance to its existing item, see
+ * set_balance_item() for @queued_meta.  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,
-                              bool commit)
+                              u32 queued_meta, bool commit)
 {
        struct btrfs_root *root = fs_info->tree_root;
        struct btrfs_trans_handle *trans;
@@ -4066,7 +4078,7 @@ static int update_balance_item(struct btrfs_fs_info *fs_info,
 
        set_balance_item(path->nodes[0],
                         btrfs_item_ptr(path->nodes[0], path->slots[0],
-                                       struct btrfs_balance_item), bctl);
+                                       struct btrfs_balance_item), bctl, queued_meta);
        btrfs_release_path(path);
        if (!commit)
                return btrfs_end_transaction(trans);
@@ -4500,8 +4512,14 @@ static int should_balance_chunk(struct extent_buffer *leaf, struct btrfs_chunk *
  * 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.
+ *
+ * METADATA_REMAP chunks are counted when they are selected too, but they are
+ * queued and balanced only after the chunk walk.  @queued_meta of them are
+ * waiting: the item gets them back on the metadata limit, so a balance that
+ * stops before it gets to them still processes them when it is resumed.
  */
-static int count_limited_chunk(struct btrfs_fs_info *fs_info, u64 chunk_type)
+static int count_limited_chunk(struct btrfs_fs_info *fs_info, u64 chunk_type,
+                              u32 queued_meta)
 {
        struct btrfs_balance_control *bctl = fs_info->balance_ctl;
        struct btrfs_balance_args *bargs;
@@ -4527,7 +4545,7 @@ static int count_limited_chunk(struct btrfs_fs_info *fs_info, u64 chunk_type)
         * 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);
+       return update_balance_item(fs_info, bctl, queued_meta, false);
 }
 
 struct remap_chunk_info {
@@ -4849,7 +4867,7 @@ again:
                        spin_unlock(&fs_info->balance_lock);
                }
 
-               ret = count_limited_chunk(fs_info, chunk_type);
+               ret = count_limited_chunk(fs_info, chunk_type, num_remap_chunks);
                if (ret)
                        goto error;
 loop:
@@ -5244,7 +5262,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, true);
+                       ret = update_balance_item(fs_info, bctl, 0, true);
                        if (ret) {
                                btrfs_err(fs_info,
                        "balance: failed to write the bounded balance item: %d",