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;
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);
}
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;
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;
* 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",