}
read_unlock(&fs_info->mapping_tree_lock);
- return ret;
+ return max(ret, READ_ONCE(fs_info->balance_chunk_floor));
}
static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
disk->stripes_max = cpu_to_le32(cpu->stripes_max);
}
+static void set_balance_item(struct extent_buffer *leaf,
+ struct btrfs_balance_item *item,
+ const struct btrfs_balance_control *bctl)
+{
+ struct btrfs_disk_balance_args disk_bargs;
+
+ 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_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);
+ btrfs_set_balance_flags(leaf, item, bctl->flags & ~BTRFS_BALANCE_RESUME);
+}
+
static int insert_balance_item(struct btrfs_fs_info *fs_info,
struct btrfs_balance_control *bctl)
{
struct btrfs_root *root = fs_info->tree_root;
struct btrfs_trans_handle *trans;
struct btrfs_balance_item *item;
- struct btrfs_disk_balance_args disk_bargs;
struct btrfs_path *path;
struct extent_buffer *leaf;
struct btrfs_key key;
item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
-
- 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_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);
- btrfs_set_balance_flags(leaf, item, bctl->flags);
+ set_balance_item(leaf, item, bctl);
out:
btrfs_free_path(path);
err = btrfs_commit_transaction(trans);
return ret;
}
+/* Write the arguments of a resumed balance to its existing item. */
+static int update_balance_item(struct btrfs_fs_info *fs_info,
+ const struct btrfs_balance_control *bctl)
+{
+ struct btrfs_root *root = fs_info->tree_root;
+ struct btrfs_trans_handle *trans;
+ BTRFS_PATH_AUTO_FREE(path);
+ struct btrfs_key key;
+ int ret;
+
+ path = btrfs_alloc_path();
+ if (!path)
+ return -ENOMEM;
+
+ trans = btrfs_start_transaction(root, 0);
+ if (IS_ERR(trans))
+ return PTR_ERR(trans);
+
+ key.objectid = BTRFS_BALANCE_OBJECTID;
+ key.type = BTRFS_TEMPORARY_ITEM_KEY;
+ key.offset = 0;
+
+ ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
+ if (ret > 0)
+ ret = -ENOENT;
+ if (ret < 0) {
+ btrfs_end_transaction(trans);
+ return ret;
+ }
+
+ set_balance_item(path->nodes[0],
+ btrfs_item_ptr(path->nodes[0], path->slots[0],
+ struct btrfs_balance_item), bctl);
+ btrfs_release_path(path);
+ return btrfs_commit_transaction(trans);
+}
+
static int del_balance_item(struct btrfs_fs_info *fs_info)
{
struct btrfs_root *root = fs_info->tree_root;
return ret;
}
+static bool limit_balance_vrange(struct btrfs_balance_args *bargs, u64 end)
+{
+ if (!(bargs->flags & BTRFS_BALANCE_ARGS_VRANGE)) {
+ bargs->flags |= BTRFS_BALANCE_ARGS_VRANGE;
+ bargs->vstart = 0;
+ bargs->vend = end;
+ return true;
+ }
+ if (bargs->vend <= end)
+ return false;
+ bargs->vend = end;
+ return true;
+}
+
/*
- * This is a heuristic used to reduce the number of chunks balanced on
- * resume after balance was interrupted.
+ * Limit the request to the chunks that exist now. The bound is written with
+ * the balance item and never moves, so there is no progress to persist. New
+ * chunks are allocated after the last chunk, but once relocation has removed
+ * the highest chunks the last chunk is lower than the bound, so while the
+ * balance item exists find_next_chunk() also observes balance_chunk_floor:
+ * every chunk allocated in that time, relocation targets included, stays
+ * outside the range, on this run and after a resume.
+ *
+ * Return true if any of the arguments changed.
*/
-static void update_balance_args(struct btrfs_balance_control *bctl)
+static bool limit_balance_to_existing_chunks(struct btrfs_fs_info *fs_info,
+ struct btrfs_balance_control *bctl)
{
- /*
- * Turn on soft mode for chunk types that were being converted.
- */
- if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
- bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
- if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
- bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
- if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
- bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
+ const u64 end = find_next_chunk(fs_info);
+ bool changed = false;
- /*
- * Turn on usage filter if is not already used. The idea is
- * that chunks that we have already balanced should be
- * reasonably full. Don't do it for chunks that are being
- * converted - that will keep us from relocating unconverted
- * (albeit full) chunks.
- */
- if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
- !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
- !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
- bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
- bctl->data.usage = 90;
- }
- if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
- !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
- !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
- bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
- bctl->sys.usage = 90;
- }
- if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
- !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
- !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
- bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
- bctl->meta.usage = 90;
- }
+ if (bctl->flags & BTRFS_BALANCE_DATA)
+ changed |= limit_balance_vrange(&bctl->data, end);
+ if (bctl->flags & BTRFS_BALANCE_METADATA)
+ changed |= limit_balance_vrange(&bctl->meta, end);
+ if (bctl->flags & BTRFS_BALANCE_SYSTEM)
+ changed |= limit_balance_vrange(&bctl->sys, end);
+ return changed;
+}
+
+/* The highest vrange end of the selected chunk types, 0 if none has one. */
+static u64 balance_chunk_floor(const struct btrfs_balance_control *bctl)
+{
+ u64 floor = 0;
+
+ if ((bctl->flags & BTRFS_BALANCE_DATA) &&
+ (bctl->data.flags & BTRFS_BALANCE_ARGS_VRANGE))
+ floor = max(floor, bctl->data.vend);
+ if ((bctl->flags & BTRFS_BALANCE_METADATA) &&
+ (bctl->meta.flags & BTRFS_BALANCE_ARGS_VRANGE))
+ floor = max(floor, bctl->meta.vend);
+ if ((bctl->flags & BTRFS_BALANCE_SYSTEM) &&
+ (bctl->sys.flags & BTRFS_BALANCE_ARGS_VRANGE))
+ floor = max(floor, bctl->sys.vend);
+ return floor;
}
/*
kfree(bctl);
ret = del_balance_item(fs_info);
+ WRITE_ONCE(fs_info->balance_chunk_floor, 0);
if (ret)
btrfs_handle_fs_error(fs_info, ret, NULL);
}
btrfs_bg_type_to_raid_name(data_target));
}
+ if (!(bctl->flags & BTRFS_BALANCE_RESUME))
+ limit_balance_to_existing_chunks(fs_info, bctl);
+
ret = insert_balance_item(fs_info, bctl);
if (ret && ret != -EEXIST)
goto out;
spin_lock(&fs_info->balance_lock);
fs_info->balance_ctl = bctl;
spin_unlock(&fs_info->balance_lock);
+ WRITE_ONCE(fs_info->balance_chunk_floor,
+ balance_chunk_floor(bctl));
} else {
BUG_ON(ret != -EEXIST);
- spin_lock(&fs_info->balance_lock);
- update_balance_args(bctl);
- spin_unlock(&fs_info->balance_lock);
+ if (bctl->update_item) {
+ /*
+ * Nothing has been relocated yet, so the item on disk,
+ * bounded again at the next mount, is still right. As
+ * for the other errors here, the balance ends and its
+ * item is deleted.
+ */
+ ret = update_balance_item(fs_info, bctl);
+ if (ret) {
+ btrfs_err(fs_info,
+ "balance: failed to write the bounded balance item: %d",
+ ret);
+ goto out;
+ }
+ bctl->update_item = false;
+ }
}
ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
btrfs_balance_sys(leaf, item, &disk_bargs);
btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
+ /*
+ * Bound the request as a new balance would be: an item written by an
+ * older kernel has no vrange, and one a user gave to such a kernel can
+ * end anywhere. The floor is still 0, so find_next_chunk() returns the
+ * end of the last chunk, and lowering a vend above it drops no block
+ * group from the range. The bound is written back to the item when
+ * the balance resumes, so a resume that is interrupted again keeps it.
+ */
+ bctl->update_item = limit_balance_to_existing_chunks(fs_info, bctl);
+ WRITE_ONCE(fs_info->balance_chunk_floor, balance_chunk_floor(bctl));
+
/*
* This should never happen, as the paused balance state is recovered
* during mount without any chance of other exclusive ops to collide.