Skip to contentSkip to conversation

btrfs: qgroup: minor cleanups

PATCH In review Qu Wenruo 2 participants 4 messages Aug 25 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs: qgroup: minor cleanups

Remove one unused variable from btrfs_read_qgroup_config(), then use atomic operations for qgroup_flags.

Check the changelog for each patch for details.

Qu Wenruo (2):
btrfs: remove the unused variable @flags from
btrfs_read_qgroup_config()
btrfs: qgroup: use atomic operations for btrfs_fs_info::qgroup_flags

 fs/btrfs/fs.h                   |   2 +-
 fs/btrfs/ioctl.c                |   2 +-
 fs/btrfs/qgroup.c               | 110 +++++++++++++++++---------------
 fs/btrfs/qgroup.h               |   4 +-
 fs/btrfs/sysfs.c                |   9 +--
 include/uapi/linux/btrfs_tree.h |  26 +++++---
 6 files changed, 84 insertions(+), 69 deletions(-)
Signature · 1 line
2.55.0
author
#1 lore raw

PATCH 1/2 btrfs: remove the unused variable @flags from btrfs_read_qgroup_config()

Since commit e562a8bdf652 ("btrfs: introduce BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN"), that @flags variable is no longer utilized.

Just remove it.

---
 fs/btrfs/qgroup.c | 2 --
 1 file changed, 2 deletions(-)
fs/btrfs/qgroup.c +0 -2
index f68b696b4bf7..f3685cbb8f2e 100644
@@ -426,7 +426,6 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
 	struct extent_buffer *l;
 	int slot;
 	int ret = 0;
-	u64 flags = 0;
 	u64 rescan_progress = 0;
 
 	if (!fs_info->quota_root)
@@ -609,7 +608,6 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
 	}
 out:
 	btrfs_free_path(path);
-	fs_info->qgroup_flags |= flags;
 	if (ret >= 0) {
 		if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)
 			set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
Signature · 1 line
2.55.0
author
#2 lore raw

PATCH 2/2 btrfs: qgroup: use atomic operations for btrfs_fs_info::qgroup_flags

In reply to Qu Wenruo

Currently we define btrfs_fs_info::qgroup_flags as u64, to match the on-disk qgroup status item's flag.

But for now we have only 4 bits utilized for that flag, and since it's u64 we have no way to properly use the existing atomic bit operations (requires an unsigned long pointer).

This results in a lot of non-atomic operations inside qgroup code. Some maybe fine as other locks are involved, but still it's not a good practice.

Remove those non-atomic operations by:

- Re-define btrfs_fs_info::qgroup_flags as unsigned long
- Define BTRFS_QGROUP_STATUS_BIT_* and BTRFS_QGROUP_RUNTIME_BIT_* Instead of the old value define the bit number.

- Use set_bit()/clear_bit()/test_bit() to replace open-coded bit operations

- Add one extra check at qgroup status item read time To make sure the on-disk flag is still inside ULONG_MAX. Otherwise reject the status item and disable qgroup.

- Get rid of unnecessary spinlock when checking a single bit

---
 fs/btrfs/fs.h                   |   2 +-
 fs/btrfs/ioctl.c                |   2 +-
 fs/btrfs/qgroup.c               | 108 +++++++++++++++++---------------
 fs/btrfs/qgroup.h               |   4 +-
 fs/btrfs/sysfs.c                |   9 +--
 include/uapi/linux/btrfs_tree.h |  26 +++++---
 6 files changed, 84 insertions(+), 67 deletions(-)
fs/btrfs/fs.h +1 -1
index 10e15a319b93..3eba8438593c 100644
@@ -811,7 +811,7 @@ struct btrfs_fs_info {
 	struct btrfs_discard_ctl discard_ctl;
 
 	/* Is qgroup tracking in a consistent state? */
-	u64 qgroup_flags;
+	unsigned long qgroup_flags;
 
 	/* Holds configuration and tracking. Protected by qgroup_lock. */
 	struct rb_root qgroup_tree;
fs/btrfs/ioctl.c +1 -1
index 21c4e755f9c5..6ffd4b9f384d 100644
@@ -3881,7 +3881,7 @@ static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
-	if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
+	if (test_bit(BTRFS_QGROUP_STATUS_BIT_RESCAN, &fs_info->qgroup_flags)) {
 		qsa.flags = 1;
 		qsa.progress = fs_info->qgroup_rescan_progress.objectid;
 	}
fs/btrfs/qgroup.c +59 -49

268 lines not on this page · raw message

fs/btrfs/qgroup.h +2 -2
index 80dd2dacd56d..b3aaad5e617d 100644
@@ -121,8 +121,8 @@ struct btrfs_qgroup_swapped_blocks;
  * To minimize the chance of collision with new persisted status flags, these
  * count backwards from the MSB.
  */
-#define BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN		(1ULL << 63)
-#define BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING		(1ULL << 62)
+#define BTRFS_QGROUP_RUNTIME_BIT_CANCEL_RESCAN		(BITS_PER_LONG - 1)
+#define BTRFS_QGROUP_RUNTIME_BIT_NO_ACCOUNTING		(BITS_PER_LONG - 2)
 
 #define BTRFS_QGROUP_DROP_SUBTREE_THRES_DEFAULT		(3)
 
fs/btrfs/sysfs.c +3 -6
index 39cb01ee441a..bec53da5678a 100644
@@ -2359,9 +2359,7 @@ static ssize_t qgroup_enabled_show(struct kobject *qgroups_kobj,
 	struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
 	bool enabled;
 
-	spin_lock(&fs_info->qgroup_lock);
-	enabled = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON;
-	spin_unlock(&fs_info->qgroup_lock);
+	enabled = test_bit(BTRFS_QGROUP_STATUS_BIT_ON, &fs_info->qgroup_flags);
 
 	return sysfs_emit(buf, "%d\n", enabled);
 }
@@ -2401,9 +2399,8 @@ static ssize_t qgroup_inconsistent_show(struct kobject *qgroups_kobj,
 	struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
 	bool inconsistent;
 
-	spin_lock(&fs_info->qgroup_lock);
-	inconsistent = (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT);
-	spin_unlock(&fs_info->qgroup_lock);
+	inconsistent = test_bit(BTRFS_QGROUP_STATUS_BIT_INCONSISTENT,
+				&fs_info->qgroup_flags);
 
 	return sysfs_emit(buf, "%d\n", inconsistent);
 }
include/uapi/linux/btrfs_tree.h +18 -8
index cc3b9f7dccaf..fa4984c80c9d 100644
@@ -1255,13 +1255,19 @@ static inline __u16 btrfs_qgroup_level(__u64 qgroupid)
 }
 
 /*
- * is subvolume quota turned on?
+ * The following BTRFS_QGROUP_STATUS_BIT_* are for
+ * btrfs_qgroup_status_item::flags.
+ *
+ * Is subvolume quota turned on?
  */
-#define BTRFS_QGROUP_STATUS_FLAG_ON		(1ULL << 0)
-/*
- * RESCAN is set during the initialization phase
- */
-#define BTRFS_QGROUP_STATUS_FLAG_RESCAN		(1ULL << 1)
+#define BTRFS_QGROUP_STATUS_BIT_ON		(0)
+#define BTRFS_QGROUP_STATUS_FLAG_ON		\
+	(1ULL << BTRFS_QGROUP_STATUS_BIT_ON)
+
+/* RESCAN is set during the initialization phase */
+#define BTRFS_QGROUP_STATUS_BIT_RESCAN		(1)
+#define BTRFS_QGROUP_STATUS_FLAG_RESCAN		\
+	(1ULL << BTRFS_QGROUP_STATUS_BIT_RESCAN)
 /*
  * Some qgroup entries are known to be out of date,
  * either because the configuration has changed in a way that
@@ -1269,14 +1275,18 @@ static inline __u16 btrfs_qgroup_level(__u64 qgroupid)
  * with a non-qgroup-aware version.
  * Turning qouta off and on again makes it inconsistent, too.
  */
-#define BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT	(1ULL << 2)
+#define BTRFS_QGROUP_STATUS_BIT_INCONSISTENT	(2)
+#define BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT	\
+	(1ULL << BTRFS_QGROUP_STATUS_BIT_INCONSISTENT)
 
 /*
  * Whether or not this filesystem is using simple quotas.  Not exactly the
  * incompat bit, because we support using simple quotas, disabling it, then
  * going back to full qgroup quotas.
  */
-#define BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE	(1ULL << 3)
+#define BTRFS_QGROUP_STATUS_BIT_SIMPLE_MODE	(3)
+#define BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE	\
+	(1ULL << BTRFS_QGROUP_STATUS_BIT_SIMPLE_MODE)
 
 #define BTRFS_QGROUP_STATUS_FLAGS_MASK	(BTRFS_QGROUP_STATUS_FLAG_ON |		\
 					 BTRFS_QGROUP_STATUS_FLAG_RESCAN |	\
Signature · 1 line
2.55.0