Re: [PATCH v5 14/18] btrfs: simple quota auto hierarchy for nested subvols

Josef Bacik
2023-08-21T18:10:23Z

On Thu, Jul 27, 2023 at 03:13:01PM -0700, Boris Burkov wrote:
> Consider the following sequence:
> - enable quotas
> - create subvol S id 256 at dir outer/
> - create a qgroup 1/100
> - add 0/256 (S's auto qgroup) to 1/100
> - create subvol T id 257 at dir outer/inner/
>
> With full qgroups, there is no relationship between 0/257 and either of
> 0/256 or 1/100. There is an inherit feature that the creator of inner/
> can use to specify it ought to be in 1/100.
>
> Simple quotas are targeted at container isolation, where such automatic
> inheritance for not necessarily trusted/controlled nested subvol
> creation would be quite helpful. Therefore, add a new default behavior
> for simple quotas: when you create a nested subvol, automatically
> inherit as parents any parents of the qgroup of the subvol the new inode
> is going in.
>
> In our example, 257/0 would also be under 1/100, allowing easy control
> of a total quota over an arbitrary hierarchy of subvolumes.
>
> I think this _might_ be a generally useful behavior, so it could be
> interesting to put it behind a new inheritance flag that simple quotas
> always use while traditional quotas let the user specify, but this is a
> minimally intrusive change to start.
>
> Signed-off-by: Boris Burkov <boris@bur.io>
> ---
> fs/btrfs/ioctl.c | 2 +-
> fs/btrfs/qgroup.c | 44 +++++++++++++++++++++++++++++++++++++++---
> fs/btrfs/qgroup.h | 6 +++---
> fs/btrfs/transaction.c | 13 +++++++++----
> 4 files changed, 54 insertions(+), 11 deletions(-)
>
> diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
> index 9b61bc62e439..c9b069077fd0 100644
> --- a/fs/btrfs/ioctl.c
> +++ b/fs/btrfs/ioctl.c
> @@ -652,7 +652,7 @@ static noinline int create_subvol(struct mnt_idmap *idmap,
> /* Tree log can't currently deal with an inode which is a new root. */
> btrfs_set_log_full_commit(trans);
>
> - ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
> + ret = btrfs_qgroup_inherit(trans, 0, objectid, root->root_key.objectid, inherit);
> if (ret)
> goto out;
>
> diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
> index dedc532669f4..58e9ed0deedd 100644
> --- a/fs/btrfs/qgroup.c
> +++ b/fs/btrfs/qgroup.c
> @@ -1550,8 +1550,7 @@ static int quick_update_accounting(struct btrfs_fs_info *fs_info,
> return ret;
> }
>
> -int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
> - u64 dst)
> +int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, u64 dst)
> {
> struct btrfs_fs_info *fs_info = trans->fs_info;
> struct btrfs_qgroup *parent;
> @@ -2991,6 +2990,40 @@ int btrfs_run_qgroups(struct btrfs_trans_handle *trans)
> return ret;
> }
>
> +static int qgroup_auto_inherit(struct btrfs_fs_info *fs_info,
> + u64 inode_rootid,
> + struct btrfs_qgroup_inherit **inherit)
> +{
> + int i = 0;
> + u64 num_qgroups = 0;
> + struct btrfs_qgroup *inode_qg;
> + struct btrfs_qgroup_list *qg_list;
> +
> + if (*inherit)
> + return -EEXIST;
> +
> + inode_qg = find_qgroup_rb(fs_info, inode_rootid);
> + if (!inode_qg)
> + return -ENOENT;
> +
> + num_qgroups = list_count_nodes(&inode_qg->groups);
> +
> + if (!num_qgroups)
> + return 0;
> +
> + *inherit = kzalloc(sizeof(**inherit) + num_qgroups * sizeof(u64), GFP_NOFS);
> + if (!*inherit)
> + return -ENOMEM;
> + (*inherit)->num_qgroups = num_qgroups;
> +
> + list_for_each_entry(qg_list, &inode_qg->groups, next_group) {
> + u64 qg_id = qg_list->group->qgroupid;
> + *((u64 *)((*inherit)+1) + i) = qg_id;
> + }
> +
> + return 0;
> +}
> +
> /*
> * Copy the accounting information between qgroups. This is necessary
> * when a snapshot or a subvolume is created. Throwing an error will
> @@ -2998,7 +3031,8 @@ int btrfs_run_qgroups(struct btrfs_trans_handle *trans)
> * when a readonly fs is a reasonable outcome.
> */
> int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
> - u64 objectid, struct btrfs_qgroup_inherit *inherit)
> + u64 objectid, u64 inode_rootid,
> + struct btrfs_qgroup_inherit *inherit)
> {
> int ret = 0;
> int i;
> @@ -3040,6 +3074,9 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
> goto out;
> }
>
> + if (!inherit && btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
> + qgroup_auto_inherit(fs_info, inode_rootid, &inherit);
> +
> if (inherit) {
> i_qgroups = (u64 *)(inherit + 1);
> nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
> @@ -3066,6 +3103,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
> if (ret)
> goto out;
>
> +

Extraneous whitespace change. Once fixed you can add

Reviewed-by: Josef Bacik <josef@toxicpanda.com>

Thanks,

Josef