Skip to contentSkip to conversation

btrfs: subvol qgroup lifetime invariants

PATCH In review Boris Burkov 4 participants 12 messages Jan 20, 2024 – Jan 26, 2024 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: subvol qgroup lifetime invariants

Subvol qgroups (id 0/SUBVOLID) are special. They get created and reaped automatically for subvols, and can never have any children by virtue of being at level 0.

Manually managing them doesn't provide much value but does create the possibility for weird states and races. To that end, ban deleting subvol qgroups that still have usage and creating subvol qgroups at all.

Testing Note: this patch series breaks btrfs/303 as that test is hunting a race to do with creating a subvol qgroup which now explicitly fails.

Boris Burkov (2):
btrfs: forbid creating subvol qgroups
btrfs: forbid deleting live subvol qgroup

 fs/btrfs/ioctl.c  |  5 +++++
 fs/btrfs/qgroup.c | 15 +++++++++++++++
 2 files changed, 20 insertions(+)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 btrfs: forbid creating subvol qgroups

This leads to various races and it isn't helpful, because you can't specify a subvol id when creating a subvol, so you can't be sure it will be the right one. Any requirements on the automatic subvol can be gratified by using a higher level qgroup and the inheritance parameters of subvol creation.

---
 fs/btrfs/ioctl.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/btrfs/ioctl.c +5 -0
index 58e0c59bc4cd..3d476decde52 100644
@@ -3815,6 +3815,11 @@ static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
 		goto out;
 	}
 
+	if (sa->create && is_fstree(sa->qgroupid)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
 	trans = btrfs_join_transaction(root);
 	if (IS_ERR(trans)) {
 		ret = PTR_ERR(trans);
Signature · 1 line
2.43.0
#9 lore raw

PATCH 2/2 btrfs: forbid deleting live subvol qgroup

In reply to Boris Burkov

If a subvolume still exists, forbid deleting its qgroup. This behavior generally leads to incorrect behavior in squotas and doesn't have a legitimate purpose.

---
 fs/btrfs/qgroup.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
fs/btrfs/qgroup.c +15 -0
index 63b426cc7798..672896b2b7a0 100644
@@ -29,6 +29,7 @@
 #include "extent-tree.h"
 #include "root-tree.h"
 #include "tree-checker.h"
+#include "super.h"
 
 enum btrfs_qgroup_mode btrfs_qgroup_mode(struct btrfs_fs_info *fs_info)
 {
@@ -1736,6 +1737,15 @@ int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
 	return ret;
 }
 
+static bool qgroup_has_usage(struct btrfs_qgroup *qgroup)
+{
+	return (qgroup->rfer > 0 || qgroup->rfer_cmpr > 0 ||
+		qgroup->excl > 0 || qgroup->excl_cmpr > 0 ||
+		qgroup->rsv.values[BTRFS_QGROUP_RSV_DATA] > 0 ||
+		qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PREALLOC] > 0 ||
+		qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PERTRANS] > 0);
+}
+
 int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
 {
 	struct btrfs_fs_info *fs_info = trans->fs_info;
@@ -1755,6 +1765,11 @@ int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
 		goto out;
 	}
 
+	if (is_fstree(qgroupid) && qgroup_has_usage(qgroup)) {
+		ret = -EBUSY;
+		goto out;
+	}
+
 	/* Check if there are no children of this qgroup */
 	if (!list_empty(&qgroup->members)) {
 		ret = -EBUSY;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] btrfs: forbid creating subvol qgroups

In reply to Boris Burkov
Quoted 7 lines · Boris Burkov

This leads to various races and it isn't helpful, because you can't specify a subvol id when creating a subvol, so you can't be sure it will be the right one. Any requirements on the automatic subvol can be gratified by using a higher level qgroup and the inheritance parameters of subvol creation.

Thanks,
Qu

Quoted 20 lines
---
  fs/btrfs/ioctl.c | 5 +++++
  1 file changed, 5 insertions(+)
fs/btrfs/ioctl.c +5 -0
index 58e0c59bc4cd..3d476decde52 100644
@@ -3815,6 +3815,11 @@ static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
  		goto out;
  	}
 
+	if (sa->create && is_fstree(sa->qgroupid)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
  	trans = btrfs_join_transaction(root);
  	if (IS_ERR(trans)) {
  		ret = PTR_ERR(trans);
Reviewed-by on v1 2/2
#10 lore raw

Re: [PATCH 2/2] btrfs: forbid deleting live subvol qgroup

In reply to Boris Burkov
Quoted 5 lines · Boris Burkov

If a subvolume still exists, forbid deleting its qgroup. This behavior generally leads to incorrect behavior in squotas and doesn't have a legitimate purpose.

Thanks,
Qu

Quoted 44 lines
---
  fs/btrfs/qgroup.c | 15 +++++++++++++++
  1 file changed, 15 insertions(+)
fs/btrfs/qgroup.c +15 -0
index 63b426cc7798..672896b2b7a0 100644
@@ -29,6 +29,7 @@
  #include "extent-tree.h"
  #include "root-tree.h"
  #include "tree-checker.h"
+#include "super.h"
 
  enum btrfs_qgroup_mode btrfs_qgroup_mode(struct btrfs_fs_info *fs_info)
  {
@@ -1736,6 +1737,15 @@ int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
  	return ret;
  }
 
+static bool qgroup_has_usage(struct btrfs_qgroup *qgroup)
+{
+	return (qgroup->rfer > 0 || qgroup->rfer_cmpr > 0 ||
+		qgroup->excl > 0 || qgroup->excl_cmpr > 0 ||
+		qgroup->rsv.values[BTRFS_QGROUP_RSV_DATA] > 0 ||
+		qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PREALLOC] > 0 ||
+		qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PERTRANS] > 0);
+}
+
  int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
  {
  	struct btrfs_fs_info *fs_info = trans->fs_info;
@@ -1755,6 +1765,11 @@ int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
  		goto out;
  	}
 
+	if (is_fstree(qgroupid) && qgroup_has_usage(qgroup)) {
+		ret = -EBUSY;
+		goto out;
+	}
+
  	/* Check if there are no children of this qgroup */
  	if (!list_empty(&qgroup->members)) {
  		ret = -EBUSY;
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] btrfs: forbid creating subvol qgroups

In reply to Boris Burkov
Quoted 7 lines · Boris Burkov

This leads to various races and it isn't helpful, because you can't specify a subvol id when creating a subvol, so you can't be sure it will be the right one. Any requirements on the automatic subvol can be gratified by using a higher level qgroup and the inheritance parameters of subvol creation.

#4 lore raw

Re: [PATCH 1/2] btrfs: forbid creating subvol qgroups

In reply to Boris Burkov
Quoted 7 lines · Boris Burkov

This leads to various races and it isn't helpful, because you can't specify a subvol id when creating a subvol, so you can't be sure it will be the right one. Any requirements on the automatic subvol can be gratified by using a higher level qgroup and the inheritance parameters of subvol creation.

Hold up, does this mean that qgroups can't be used *at all* on Fedora, where we use subvolumes for both the root and user home directory hierarchies?

Signature · 1 line
真実はいつも一つ!/ Always, there's only one truth!
#5 lore raw

Re: [PATCH 1/2] btrfs: forbid creating subvol qgroups

Quoted 11 lines · Neal Gompa
Quoted 6 lines · Boris Burkov

This leads to various races and it isn't helpful, because you can't specify a subvol id when creating a subvol, so you can't be sure it will be the right one. Any requirements on the automatic subvol can be gratified by using a higher level qgroup and the inheritance parameters of subvol creation.

Hold up, does this mean that qgroups can't be used *at all* on Fedora, where we use subvolumes for both the root and user home directory hierarchies?

How do you imply that from the patch? This is about preventing creating the subvolume qgroups, i.e. with the level 0 and referred to as 0/1234 where 1234 is a subvolume id. Such qgroups are supposed to be created only at the time the subvolume is created.

#6 lore raw

Re: [PATCH 1/2] btrfs: forbid creating subvol qgroups

Quoted 10 lines · David Sterba
Quoted 8 lines · Neal Gompa
Quoted 6 lines · Boris Burkov

This leads to various races and it isn't helpful, because you can't specify a subvol id when creating a subvol, so you can't be sure it will be the right one. Any requirements on the automatic subvol can be gratified by using a higher level qgroup and the inheritance parameters of subvol creation.

Quoted 9 lines
Quoted 3 lines

Hold up, does this mean that qgroups can't be used *at all* on Fedora, where we use subvolumes for both the root and user home directory hierarchies?

How do you imply that from the patch? This is about preventing creating the subvolume qgroups, i.e. with the level 0 and referred to as 0/1234 where 1234 is a subvolume id. Such qgroups are supposed to be created only at the time the subvolume is created.

Because I don't really understand what the description of this patch implies. It is not clear to me what is actually changing here, that's why I'm asking.

Signature · 1 line
真実はいつも一つ!/ Always, there's only one truth!
#7 lore raw

Re: [PATCH 1/2] btrfs: forbid creating subvol qgroups

Quoted 21 lines · Neal Gompa
Quoted 10 lines · David Sterba
Quoted 8 lines · Neal Gompa
Quoted 6 lines · Boris Burkov

This leads to various races and it isn't helpful, because you can't specify a subvol id when creating a subvol, so you can't be sure it will be the right one. Any requirements on the automatic subvol can be gratified by using a higher level qgroup and the inheritance parameters of subvol creation.

Quoted 9 lines
Quoted 3 lines

Hold up, does this mean that qgroups can't be used *at all* on Fedora, where we use subvolumes for both the root and user home directory hierarchies?

How do you imply that from the patch? This is about preventing creating the subvolume qgroups, i.e. with the level 0 and referred to as 0/1234 where 1234 is a subvolume id. Such qgroups are supposed to be created only at the time the subvolume is created.

Quoted 3 lines

Because I don't really understand what the description of this patch implies. It is not clear to me what is actually changing here, that's why I'm asking.

Sorry for the unclear patch description!

If Fedora is creating/snapshotting subvolumes but not explicitly creating subvolume qgroups (qg id of the form 0/X), that is fully supported and there should be no issue.

More details, just to be extra thorough to make up for the bad initial description:

In the qgroup hierarchy, level 0 is special, these are the so called "subvolume qgroups". The qgroup with id 0/X is the qgroup for the subvolume with id X. When that subvolume accumulates usage, it will always be reported into that qgroup (in a hard-coded fashion in fs/btrfs/qgroup.c).

This patch prevents users from explicitly creating subvolume qgroups with BTRFS_IOC_QGROUP_CREATE.

This does not affect the existing behavior that creating a subvolume also creates the corresponding subvolume qgroup.

examples using the btrfs cli to illustrate:

# create a subvol qgroup explicitly; EINVAL
btrfs qgroup create 0/1234 <mnt>
# create a higher level qgroup explicitly; OK
btrfs qgroup create 1/1234 <mnt>
# create sv with id X and qgroup with id 0/X
btrfs subvol create <mnt>/<sv>

The basic motivation for both of the patches in this series is that qgroup lifetime is more complicated in simple quotas and these are relatively unobtrusive changes that make it (a lot) easier to manage without affecting any known useful classic qgroups use case.

Thanks,
Boris

Quoted 4 lines
Signature · 2 lines
-- 
真実はいつも一つ!/ Always, there's only one truth!
#8 lore raw

Re: [PATCH 1/2] btrfs: forbid creating subvol qgroups

Quoted 56 lines · Boris Burkov
Quoted 21 lines · Neal Gompa
Quoted 10 lines · David Sterba
Quoted 8 lines · Neal Gompa
Quoted 6 lines · Boris Burkov

This leads to various races and it isn't helpful, because you can't specify a subvol id when creating a subvol, so you can't be sure it will be the right one. Any requirements on the automatic subvol can be gratified by using a higher level qgroup and the inheritance parameters of subvol creation.

Quoted 9 lines
Quoted 3 lines

Hold up, does this mean that qgroups can't be used *at all* on Fedora, where we use subvolumes for both the root and user home directory hierarchies?

How do you imply that from the patch? This is about preventing creating the subvolume qgroups, i.e. with the level 0 and referred to as 0/1234 where 1234 is a subvolume id. Such qgroups are supposed to be created only at the time the subvolume is created.

Quoted 3 lines

Because I don't really understand what the description of this patch implies. It is not clear to me what is actually changing here, that's why I'm asking.

Sorry for the unclear patch description!

If Fedora is creating/snapshotting subvolumes but not explicitly creating subvolume qgroups (qg id of the form 0/X), that is fully supported and there should be no issue.

More details, just to be extra thorough to make up for the bad initial description:

In the qgroup hierarchy, level 0 is special, these are the so called "subvolume qgroups". The qgroup with id 0/X is the qgroup for the subvolume with id X. When that subvolume accumulates usage, it will always be reported into that qgroup (in a hard-coded fashion in fs/btrfs/qgroup.c).

This patch prevents users from explicitly creating subvolume qgroups with BTRFS_IOC_QGROUP_CREATE.

This does not affect the existing behavior that creating a subvolume also creates the corresponding subvolume qgroup.

examples using the btrfs cli to illustrate:

# create a subvol qgroup explicitly; EINVAL
btrfs qgroup create 0/1234 <mnt>
# create a higher level qgroup explicitly; OK
btrfs qgroup create 1/1234 <mnt>
# create sv with id X and qgroup with id 0/X
btrfs subvol create <mnt>/<sv>
Quoted 5 lines

The basic motivation for both of the patches in this series is that qgroup lifetime is more complicated in simple quotas and these are relatively unobtrusive changes that make it (a lot) easier to manage without affecting any known useful classic qgroups use case.

Okay, that makes sense! Can you put some of this into the initial patch description then, assuming David hasn't already checked it in?

Signature · 1 line
真実はいつも一つ!/ Always, there's only one truth!