Skip to content

Commit 5343cd9

Browse files
boryaskdave
authored andcommitted
btrfs: qgroup: simple quota auto hierarchy for nested subvolumes
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 <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent cecbb53 commit 5343cd9

File tree

4 files changed

+67
-12
lines changed

4 files changed

+67
-12
lines changed

fs/btrfs/ioctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ static noinline int create_subvol(struct mnt_idmap *idmap,
652652
/* Tree log can't currently deal with an inode which is a new root. */
653653
btrfs_set_log_full_commit(trans);
654654

655-
ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
655+
ret = btrfs_qgroup_inherit(trans, 0, objectid, root->root_key.objectid, inherit);
656656
if (ret)
657657
goto out;
658658

fs/btrfs/qgroup.c

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,8 +1549,7 @@ static int quick_update_accounting(struct btrfs_fs_info *fs_info,
15491549
return ret;
15501550
}
15511551

1552-
int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1553-
u64 dst)
1552+
int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, u64 dst)
15541553
{
15551554
struct btrfs_fs_info *fs_info = trans->fs_info;
15561555
struct btrfs_qgroup *parent;
@@ -3018,14 +3017,56 @@ int btrfs_run_qgroups(struct btrfs_trans_handle *trans)
30183017
return ret;
30193018
}
30203019

3020+
static int qgroup_auto_inherit(struct btrfs_fs_info *fs_info,
3021+
u64 inode_rootid,
3022+
struct btrfs_qgroup_inherit **inherit)
3023+
{
3024+
int i = 0;
3025+
u64 num_qgroups = 0;
3026+
struct btrfs_qgroup *inode_qg;
3027+
struct btrfs_qgroup_list *qg_list;
3028+
struct btrfs_qgroup_inherit *res;
3029+
size_t struct_sz;
3030+
u64 *qgids;
3031+
3032+
if (*inherit)
3033+
return -EEXIST;
3034+
3035+
inode_qg = find_qgroup_rb(fs_info, inode_rootid);
3036+
if (!inode_qg)
3037+
return -ENOENT;
3038+
3039+
num_qgroups = list_count_nodes(&inode_qg->groups);
3040+
3041+
if (!num_qgroups)
3042+
return 0;
3043+
3044+
struct_sz = struct_size(res, qgroups, num_qgroups);
3045+
if (struct_sz == SIZE_MAX)
3046+
return -ERANGE;
3047+
3048+
res = kzalloc(struct_sz, GFP_NOFS);
3049+
if (!res)
3050+
return -ENOMEM;
3051+
res->num_qgroups = num_qgroups;
3052+
qgids = res->qgroups;
3053+
3054+
list_for_each_entry(qg_list, &inode_qg->groups, next_group)
3055+
qgids[i] = qg_list->group->qgroupid;
3056+
3057+
*inherit = res;
3058+
return 0;
3059+
}
3060+
30213061
/*
30223062
* Copy the accounting information between qgroups. This is necessary
30233063
* when a snapshot or a subvolume is created. Throwing an error will
30243064
* cause a transaction abort so we take extra care here to only error
30253065
* when a readonly fs is a reasonable outcome.
30263066
*/
30273067
int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
3028-
u64 objectid, struct btrfs_qgroup_inherit *inherit)
3068+
u64 objectid, u64 inode_rootid,
3069+
struct btrfs_qgroup_inherit *inherit)
30293070
{
30303071
int ret = 0;
30313072
int i;
@@ -3037,6 +3078,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
30373078
struct btrfs_qgroup *dstgroup;
30383079
struct btrfs_qgroup *prealloc;
30393080
struct btrfs_qgroup_list **qlist_prealloc = NULL;
3081+
bool free_inherit = false;
30403082
bool need_rescan = false;
30413083
u32 level_size = 0;
30423084
u64 nums;
@@ -3073,6 +3115,13 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
30733115
goto out;
30743116
}
30753117

3118+
if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE && !inherit) {
3119+
ret = qgroup_auto_inherit(fs_info, inode_rootid, &inherit);
3120+
if (ret)
3121+
goto out;
3122+
free_inherit = true;
3123+
}
3124+
30763125
if (inherit) {
30773126
i_qgroups = (u64 *)(inherit + 1);
30783127
nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
@@ -3256,6 +3305,8 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
32563305
kfree(qlist_prealloc[i]);
32573306
kfree(qlist_prealloc);
32583307
}
3308+
if (free_inherit)
3309+
kfree(inherit);
32593310
kfree(prealloc);
32603311
return ret;
32613312
}

fs/btrfs/qgroup.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,7 @@ int btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info);
312312
void btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info);
313313
int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
314314
bool interruptible);
315-
int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
316-
u64 dst);
315+
int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, u64 dst);
317316
int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
318317
u64 dst);
319318
int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid);
@@ -343,7 +342,8 @@ int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr,
343342
int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans);
344343
int btrfs_run_qgroups(struct btrfs_trans_handle *trans);
345344
int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
346-
u64 objectid, struct btrfs_qgroup_inherit *inherit);
345+
u64 objectid, u64 inode_rootid,
346+
struct btrfs_qgroup_inherit *inherit);
347347
void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
348348
u64 ref_root, u64 num_bytes,
349349
enum btrfs_qgroup_rsv_type type);

fs/btrfs/transaction.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,8 +1620,8 @@ static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
16201620
int ret;
16211621

16221622
/*
1623-
* Save some performance in the case that full qgroups are not enabled.
1624-
* If this check races with the ioctl, rescan will kick in anyway.
1623+
* Save some performance in the case that qgroups are not enabled. If
1624+
* this check races with the ioctl, rescan will kick in anyway.
16251625
*/
16261626
if (!btrfs_qgroup_full_accounting(fs_info))
16271627
return 0;
@@ -1662,7 +1662,7 @@ static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
16621662

16631663
/* Now qgroup are all updated, we can inherit it to new qgroups */
16641664
ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid,
1665-
inherit);
1665+
parent->root_key.objectid, inherit);
16661666
if (ret < 0)
16671667
goto out;
16681668

@@ -1929,8 +1929,12 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
19291929
* To co-operate with that hack, we do hack again.
19301930
* Or snapshot will be greatly slowed down by a subtree qgroup rescan
19311931
*/
1932-
ret = qgroup_account_snapshot(trans, root, parent_root,
1933-
pending->inherit, objectid);
1932+
if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL)
1933+
ret = qgroup_account_snapshot(trans, root, parent_root,
1934+
pending->inherit, objectid);
1935+
else if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
1936+
ret = btrfs_qgroup_inherit(trans, root->root_key.objectid, objectid,
1937+
parent_root->root_key.objectid, pending->inherit);
19341938
if (ret < 0)
19351939
goto fail;
19361940

0 commit comments

Comments
 (0)