Skip to contentSkip to conversation

btrfs: qgroup fixes and a cleanup

PATCH In review [email protected] 3 participants 8 messages Jul 1, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: qgroup fixes and a cleanup

From: Filipe Manana <[email protected]>

A couple fixes and a cleanup, details in the change logs.

Filipe Manana (3):
btrfs: qgroup: set quota enabled bit if quota disable fails flushing reservations
btrfs: qgroup: fix qgroup create ioctl returning success after quotas disabled
btrfs: qgroup: use btrfs_qgroup_enabled() in ioctls

 fs/btrfs/ioctl.c       | 24 ++++--------------------
 fs/btrfs/qgroup.c      | 10 +++++-----
 fs/btrfs/transaction.c |  6 ++++--
 3 files changed, 13 insertions(+), 27 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH 1/3 btrfs: qgroup: set quota enabled bit if quota disable fails flushing reservations

From: Filipe Manana <[email protected]>

Before waiting for the rescan worker to finish and flushing reservations, we clear the BTRFS_FS_QUOTA_ENABLED flag from fs_info. If we fail flushing reservations we leave with the flag not set which is not correct since quotas are still enabled - we must set back the flag on error paths, such as when we fail to start a transaction, except for error paths that abort a transaction. The reservation flushing happens very early before we do any operation that actually disables quotas and before we start a transaction, so set back BTRFS_FS_QUOTA_ENABLED if it fails.

Fixes af0e2aab3b70 ("btrfs: qgroup: flush reservations during quota disable")
---
 fs/btrfs/qgroup.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/btrfs/qgroup.c +5 -2
index 42d3cfb84318..eb1bb57dee7d 100644
@@ -1334,11 +1334,14 @@ int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
 
 	/*
 	 * We have nothing held here and no trans handle, just return the error
-	 * if there is one.
+	 * if there is one and set back the quota enabled bit since we didn't
+	 * actually disable quotas.
 	 */
 	ret = flush_reservations(fs_info);
-	if (ret)
+	if (ret) {
+		set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
 		return ret;
+	}
 
 	/*
 	 * 1 For the root item
Signature · 1 line
2.47.2
#3 lore raw

PATCH 2/3 btrfs: qgroup: fix qgroup create ioctl returning success after quotas disabled

In reply to [email protected]

From: Filipe Manana <[email protected]>

When quotas are disabled qgroup ioctls are supposed to return -ENOTCONN, but the qgroup create ioctl stopped doing that when it races with a quota disable operation, returning 0 instead. This change of behaviour happened in commit 6ed05643ddb1 ("btrfs: create qgroup earlier in snapshot creation").

The issue happens as follows:

1) Task A enters btrfs_ioctl_qgroup_create(), qgroups are enabled and so qgroup_enabled() returns true since fs_info->quota_root is not NULL;

2) Task B enters btrfs_ioctl_quota_ctl() -> btrfs_quota_disable() and disables qgroups, so now fs_info->quota_root is NULL;

3) Task A enters btrfs_create_qgroup() and calls btrfs_qgroup_mode(), which returns BTRFS_QGROUP_MODE_DISABLED since quotas are disabled, and then btrfs_create_qgroup() returns 0 to the caller, which makes the ioctl return 0 instead of -ENOTCONN.

The check for fs_info->quota_root and returning -ENOTCONN if it's NULL is made only after the call btrfs_qgroup_mode().

Fix this by moving the check for disabled quotas with btrfs_qgroup_mode() into transaction.c:create_pending_snapshot(), so that we don't abort the transaction if btrfs_create_qgroup() returns -ENOTCONN and quotas are disabled.

Fixes 6ed05643ddb1 ("btrfs: create qgroup earlier in snapshot creation")
---
 fs/btrfs/qgroup.c      | 3 ---
 fs/btrfs/transaction.c | 6 ++++--
 2 files changed, 4 insertions(+), 5 deletions(-)
fs/btrfs/qgroup.c +0 -3
index eb1bb57dee7d..ae9bc7c71a34 100644
@@ -1662,9 +1662,6 @@ int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
 	struct btrfs_qgroup *prealloc = NULL;
 	int ret = 0;
 
-	if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED)
-		return 0;
-
 	mutex_lock(&fs_info->qgroup_ioctl_lock);
 	if (!fs_info->quota_root) {
 		ret = -ENOTCONN;
fs/btrfs/transaction.c +4 -2
index 2e07c90be5cd..c5c0d9cf1a80 100644
@@ -1735,8 +1735,10 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
 
 	ret = btrfs_create_qgroup(trans, objectid);
 	if (ret && ret != -EEXIST) {
-		btrfs_abort_transaction(trans, ret);
-		goto fail;
+		if (ret != -ENOTCONN || btrfs_qgroup_enabled(fs_info)) {
+			btrfs_abort_transaction(trans, ret);
+			goto fail;
+		}
 	}
 
 	/*
Signature · 1 line
2.47.2
#5 lore raw

PATCH 3/3 btrfs: qgroup: use btrfs_qgroup_enabled() in ioctls

In reply to [email protected]

From: Filipe Manana <[email protected]>

We have a publicly exported btrfs_qgroup_enabled() and an ioctl.c private qgroup_enabled() helper. Both of these test if qgroups are enabled, the first check if the flag BTRFS_FS_QUOTA_ENABLED is set in fs_info->flags while the second checks if fs_info->quota_root is not NULL while holding the mutex fs_info->qgroup_ioctl_lock.

We can get away with the private ioctl.c:qgroup_enabled(), as all entry points into the qgroup code check if fs_info->quota_root is NULL or not while holding the mutex fs_info->qgroup_ioctl_lock, and returning the error -ENOTCONN in case it's NULL.

---
 fs/btrfs/ioctl.c | 24 ++++--------------------
 1 file changed, 4 insertions(+), 20 deletions(-)
fs/btrfs/ioctl.c +4 -20
index 3621ed2eacd1..3c4619375bc9 100644
@@ -3714,22 +3714,6 @@ static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
 	return ret;
 }
 
-/*
- * Quick check for ioctl handlers if quotas are enabled. Proper locking must be
- * done before any operations.
- */
-static bool qgroup_enabled(struct btrfs_fs_info *fs_info)
-{
-	bool ret = true;
-
-	mutex_lock(&fs_info->qgroup_ioctl_lock);
-	if (!fs_info->quota_root)
-		ret = false;
-	mutex_unlock(&fs_info->qgroup_ioctl_lock);
-
-	return ret;
-}
-
 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
 {
 	struct inode *inode = file_inode(file);
@@ -3744,7 +3728,7 @@ static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
-	if (!qgroup_enabled(root->fs_info))
+	if (!btrfs_qgroup_enabled(fs_info))
 		return -ENOTCONN;
 
 	ret = mnt_want_write_file(file);
@@ -3814,7 +3798,7 @@ static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
-	if (!qgroup_enabled(root->fs_info))
+	if (!btrfs_qgroup_enabled(root->fs_info))
 		return -ENOTCONN;
 
 	ret = mnt_want_write_file(file);
@@ -3873,7 +3857,7 @@ static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
-	if (!qgroup_enabled(root->fs_info))
+	if (!btrfs_qgroup_enabled(root->fs_info))
 		return -ENOTCONN;
 
 	ret = mnt_want_write_file(file);
@@ -3921,7 +3905,7 @@ static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
-	if (!qgroup_enabled(fs_info))
+	if (!btrfs_qgroup_enabled(fs_info))
 		return -ENOTCONN;
 
 	ret = mnt_want_write_file(file);
Signature · 1 line
2.47.2
Reviewed-by on the v1 series
#7 lore raw
In reply to [email protected]
Quoted 8 lines · On Tue, Jul 01, 2025 at 04:42:19PM +0100, [email protected] wrote:

From: Filipe Manana <[email protected]>

A couple fixes and a cleanup, details in the change logs.

Filipe Manana (3):
btrfs: qgroup: set quota enabled bit if quota disable fails flushing reservations
btrfs: qgroup: fix qgroup create ioctl returning success after quotas disabled
btrfs: qgroup: use btrfs_qgroup_enabled() in ioctls

Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] btrfs: qgroup: set quota enabled bit if quota disable fails flushing reservations

In reply to [email protected]
Quoted 11 lines · 在 2025/7/2 01:12, [email protected] 写道:

From: Filipe Manana <[email protected]>

Before waiting for the rescan worker to finish and flushing reservations, we clear the BTRFS_FS_QUOTA_ENABLED flag from fs_info. If we fail flushing reservations we leave with the flag not set which is not correct since quotas are still enabled - we must set back the flag on error paths, such as when we fail to start a transaction, except for error paths that abort a transaction. The reservation flushing happens very early before we do any operation that actually disables quotas and before we start a transaction, so set back BTRFS_FS_QUOTA_ENABLED if it fails.

Quoted 2 lines
Fixes af0e2aab3b70 ("btrfs: qgroup: flush reservations during quota disable")

I initially thought it will be a little racy when setting/clearing the ENABLED flag without qgroup_ioctl_mutex, but since we're already holding subvol_sem() for both enable and disable, it should be fine.

Thanks,
Qu

Quoted 25 lines
---
  fs/btrfs/qgroup.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
fs/btrfs/qgroup.c +5 -2
index 42d3cfb84318..eb1bb57dee7d 100644
@@ -1334,11 +1334,14 @@ int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
 
  	/*
  	 * We have nothing held here and no trans handle, just return the error
-	 * if there is one.
+	 * if there is one and set back the quota enabled bit since we didn't
+	 * actually disable quotas.
  	 */
  	ret = flush_reservations(fs_info);
-	if (ret)
+	if (ret) {
+		set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  		return ret;
+	}
 
  	/*
  	 * 1 For the root item
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] btrfs: qgroup: fix qgroup create ioctl returning success after quotas disabled

In reply to [email protected]
Quoted 24 lines · 在 2025/7/2 01:12, [email protected] 写道:

From: Filipe Manana <[email protected]>

When quotas are disabled qgroup ioctls are supposed to return -ENOTCONN, but the qgroup create ioctl stopped doing that when it races with a quota disable operation, returning 0 instead. This change of behaviour happened in commit 6ed05643ddb1 ("btrfs: create qgroup earlier in snapshot creation").

The issue happens as follows:

1) Task A enters btrfs_ioctl_qgroup_create(), qgroups are enabled and so

    qgroup_enabled() returns true since fs_info->quota_root is not NULL;

2) Task B enters btrfs_ioctl_quota_ctl() -> btrfs_quota_disable() and

    disables qgroups, so now fs_info->quota_root is NULL;

3) Task A enters btrfs_create_qgroup() and calls btrfs_qgroup_mode(), which returns BTRFS_QGROUP_MODE_DISABLED since quotas are disabled, and then btrfs_create_qgroup() returns 0 to the caller, which makes the ioctl return 0 instead of -ENOTCONN.

The check for fs_info->quota_root and returning -ENOTCONN if it's NULL is made only after the call btrfs_qgroup_mode().

Quoted 7 lines

Fix this by moving the check for disabled quotas with btrfs_qgroup_mode() into transaction.c:create_pending_snapshot(), so that we don't abort the transaction if btrfs_create_qgroup() returns -ENOTCONN and quotas are disabled.

Fixes 6ed05643ddb1 ("btrfs: create qgroup earlier in snapshot creation")

Since create_pending_snapshot() is called during btrfs_commit_transaction(), we're safe from a btrfs_quota_enable() call as it requires a trans handle.

So there will not be a race between create_pending_snapshot() and btrfs_quota_enable().

Thanks,
Qu

Quoted 36 lines
---
  fs/btrfs/qgroup.c      | 3 ---
  fs/btrfs/transaction.c | 6 ++++--
  2 files changed, 4 insertions(+), 5 deletions(-)
fs/btrfs/qgroup.c +0 -3
index eb1bb57dee7d..ae9bc7c71a34 100644
@@ -1662,9 +1662,6 @@ int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
  	struct btrfs_qgroup *prealloc = NULL;
  	int ret = 0;
 
-	if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED)
-		return 0;
-
  	mutex_lock(&fs_info->qgroup_ioctl_lock);
  	if (!fs_info->quota_root) {
  		ret = -ENOTCONN;
fs/btrfs/transaction.c +4 -2
index 2e07c90be5cd..c5c0d9cf1a80 100644
@@ -1735,8 +1735,10 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
 
  	ret = btrfs_create_qgroup(trans, objectid);
  	if (ret && ret != -EEXIST) {
-		btrfs_abort_transaction(trans, ret);
-		goto fail;
+		if (ret != -ENOTCONN || btrfs_qgroup_enabled(fs_info)) {
+			btrfs_abort_transaction(trans, ret);
+			goto fail;
+		}
  	}
 
  	/*
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] btrfs: qgroup: use btrfs_qgroup_enabled() in ioctls

In reply to [email protected]
Quoted 8 lines · 在 2025/7/2 01:12, [email protected] 写道:

From: Filipe Manana <[email protected]>

We have a publicly exported btrfs_qgroup_enabled() and an ioctl.c private qgroup_enabled() helper. Both of these test if qgroups are enabled, the first check if the flag BTRFS_FS_QUOTA_ENABLED is set in fs_info->flags while the second checks if fs_info->quota_root is not NULL while holding the mutex fs_info->qgroup_ioctl_lock.

Quoted 6 lines

We can get away with the private ioctl.c:qgroup_enabled(), as all entry points into the qgroup code check if fs_info->quota_root is NULL or not while holding the mutex fs_info->qgroup_ioctl_lock, and returning the error -ENOTCONN in case it's NULL.

Thanks,
Qu

Quoted 67 lines
---
  fs/btrfs/ioctl.c | 24 ++++--------------------
  1 file changed, 4 insertions(+), 20 deletions(-)
fs/btrfs/ioctl.c +4 -20
index 3621ed2eacd1..3c4619375bc9 100644
@@ -3714,22 +3714,6 @@ static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
  	return ret;
  }
 
-/*
- * Quick check for ioctl handlers if quotas are enabled. Proper locking must be
- * done before any operations.
- */
-static bool qgroup_enabled(struct btrfs_fs_info *fs_info)
-{
-	bool ret = true;
-
-	mutex_lock(&fs_info->qgroup_ioctl_lock);
-	if (!fs_info->quota_root)
-		ret = false;
-	mutex_unlock(&fs_info->qgroup_ioctl_lock);
-
-	return ret;
-}
-
  static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
  {
  	struct inode *inode = file_inode(file);
@@ -3744,7 +3728,7 @@ static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
  	if (!capable(CAP_SYS_ADMIN))
  		return -EPERM;
 
-	if (!qgroup_enabled(root->fs_info))
+	if (!btrfs_qgroup_enabled(fs_info))
  		return -ENOTCONN;
 
  	ret = mnt_want_write_file(file);
@@ -3814,7 +3798,7 @@ static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
  	if (!capable(CAP_SYS_ADMIN))
  		return -EPERM;
 
-	if (!qgroup_enabled(root->fs_info))
+	if (!btrfs_qgroup_enabled(root->fs_info))
  		return -ENOTCONN;
 
  	ret = mnt_want_write_file(file);
@@ -3873,7 +3857,7 @@ static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
  	if (!capable(CAP_SYS_ADMIN))
  		return -EPERM;
 
-	if (!qgroup_enabled(root->fs_info))
+	if (!btrfs_qgroup_enabled(root->fs_info))
  		return -ENOTCONN;
 
  	ret = mnt_want_write_file(file);
@@ -3921,7 +3905,7 @@ static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
  	if (!capable(CAP_SYS_ADMIN))
  		return -EPERM;
 
-	if (!qgroup_enabled(fs_info))
+	if (!btrfs_qgroup_enabled(fs_info))
  		return -ENOTCONN;
 
  	ret = mnt_want_write_file(file);