Skip to contentSkip to conversation

bcachefs: BCH_IOCTL_QUERY_COUNTERS

PATCH In review Kent Overstreet 2 participants 3 messages Jan 29, 2025 – Feb 5, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bcachefs: BCH_IOCTL_QUERY_COUNTERS

Add an ioctl for querying counters, the same ones provided in /sys/fs/bcachefs/<uuid>/counters/, but more suitable for a 'bcachefs top' command.

---
 fs/bcachefs/bcachefs_ioctl.h | 10 ++++++++++
 fs/bcachefs/chardev.c        | 32 ++++++++++++++++++++++++++++++++
 fs/bcachefs/sb-counters.c    |  2 +-
 fs/bcachefs/sb-counters.h    |  1 +
 4 files changed, 44 insertions(+), 1 deletion(-)
fs/bcachefs/bcachefs_ioctl.h +10 -0
index 3c23bdf788ce..f1b746fac007 100644
@@ -87,6 +87,7 @@ struct bch_ioctl_incremental {
 #define BCH_IOCTL_FSCK_OFFLINE	_IOW(0xbc,	19,  struct bch_ioctl_fsck_offline)
 #define BCH_IOCTL_FSCK_ONLINE	_IOW(0xbc,	20,  struct bch_ioctl_fsck_online)
 #define BCH_IOCTL_QUERY_ACCOUNTING _IOW(0xbc,	21,  struct bch_ioctl_query_accounting)
+#define BCH_IOCTL_QUERY_COUNTERS _IOW(0xbc,	21,  struct bch_ioctl_query_counters)
 
 /* ioctl below act on a particular file, not the filesystem as a whole: */
 
@@ -443,4 +444,13 @@ struct bch_ioctl_query_accounting {
 	struct bkey_i_accounting accounting[];
 };
 
+#define BCH_IOCTL_QUERY_COUNTERS_MOUNT	(1 << 0)
+
+struct bch_ioctl_query_counters {
+	__u16			nr;
+	__u16			flags;
+	__u32			pad;
+	__u64			d[];
+};
+
 #endif /* _BCACHEFS_IOCTL_H */
fs/bcachefs/chardev.c +32 -0
index 46e9e32105a9..c2d9efe8db53 100644
@@ -448,6 +448,36 @@ static long bch2_ioctl_query_accounting(struct bch_fs *c,
 	return ret;
 }
 
+static long bch2_ioctl_query_counters(struct bch_fs *c,
+			struct bch_ioctl_query_counters __user *user_arg)
+{
+	struct bch_ioctl_query_counters arg;
+	int ret = copy_from_user_errcode(&arg, user_arg, sizeof(arg));
+	if (ret)
+		return ret;
+
+	if ((arg.flags & !BCH_IOCTL_QUERY_COUNTERS_MOUNT) ||
+	    arg.pad)
+		return -EINVAL;
+
+	arg.nr = min(arg.nr, BCH_COUNTER_NR);
+	ret = put_user(arg.nr, &user_arg->nr);
+	if (ret)
+		return ret;
+
+	for (unsigned i = 0; i < arg.nr; i++) {
+		u64 v = !(arg.flags & BCH_IOCTL_QUERY_COUNTERS_MOUNT)
+			? percpu_u64_get(&c->counters[i])
+			: c->counters_on_mount[i];
+
+		ret = put_user(v, &user_arg->d[i]);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 /* obsolete, didn't allow for new data types: */
 static long bch2_ioctl_dev_usage(struct bch_fs *c,
 				 struct bch_ioctl_dev_usage __user *user_arg)
@@ -710,6 +740,8 @@ long bch2_fs_ioctl(struct bch_fs *c, unsigned cmd, void __user *arg)
 		BCH_IOCTL(fsck_online, struct bch_ioctl_fsck_online);
 	case BCH_IOCTL_QUERY_ACCOUNTING:
 		return bch2_ioctl_query_accounting(c, arg);
+	case BCH_IOCTL_QUERY_COUNTERS:
+		return bch2_ioctl_query_counters(c, arg);
 	default:
 		return -ENOTTY;
 	}
fs/bcachefs/sb-counters.c +1 -1
index 6992e7469112..67f4e7a5aa5a 100644
@@ -5,7 +5,7 @@
 
 /* BCH_SB_FIELD_counters */
 
-static const char * const bch2_counter_names[] = {
+const char * const bch2_counter_names[] = {
 #define x(t, n, ...) (#t),
 	BCH_PERSISTENT_COUNTERS()
 #undef x
fs/bcachefs/sb-counters.h +1 -0
index 81f8aec9fcb1..eef7211edf42 100644
@@ -11,6 +11,7 @@ int bch2_sb_counters_from_cpu(struct bch_fs *);
 void bch2_fs_counters_exit(struct bch_fs *);
 int bch2_fs_counters_init(struct bch_fs *);
 
+extern const char * const bch2_counter_names[];
 extern const struct bch_sb_field_ops bch_sb_field_ops_counters;
 
 #endif // _BCACHEFS_SB_COUNTERS_H
Signature · 1 line
2.45.2
#1 lore raw
Quoted 50 lines · Kent Overstreet

Add an ioctl for querying counters, the same ones provided in /sys/fs/bcachefs/<uuid>/counters/, but more suitable for a 'bcachefs top' command.

---
  fs/bcachefs/bcachefs_ioctl.h | 10 ++++++++++
  fs/bcachefs/chardev.c        | 32 ++++++++++++++++++++++++++++++++
  fs/bcachefs/sb-counters.c    |  2 +-
  fs/bcachefs/sb-counters.h    |  1 +
  4 files changed, 44 insertions(+), 1 deletion(-)
fs/bcachefs/bcachefs_ioctl.h +10 -0
index 3c23bdf788ce..f1b746fac007 100644
@@ -87,6 +87,7 @@ struct bch_ioctl_incremental {
  #define BCH_IOCTL_FSCK_OFFLINE	_IOW(0xbc,	19,  struct bch_ioctl_fsck_offline)
  #define BCH_IOCTL_FSCK_ONLINE	_IOW(0xbc,	20,  struct bch_ioctl_fsck_online)
  #define BCH_IOCTL_QUERY_ACCOUNTING _IOW(0xbc,	21,  struct bch_ioctl_query_accounting)
+#define BCH_IOCTL_QUERY_COUNTERS _IOW(0xbc,	21,  struct bch_ioctl_query_counters)
 
  /* ioctl below act on a particular file, not the filesystem as a whole: */
 
@@ -443,4 +444,13 @@ struct bch_ioctl_query_accounting {
  	struct bkey_i_accounting accounting[];
  };
 
+#define BCH_IOCTL_QUERY_COUNTERS_MOUNT	(1 << 0)
+
+struct bch_ioctl_query_counters {
+	__u16			nr;
+	__u16			flags;
+	__u32			pad;
+	__u64			d[];
+};
+
  #endif /* _BCACHEFS_IOCTL_H */
fs/bcachefs/chardev.c +4 -0
index 46e9e32105a9..c2d9efe8db53 100644
@@ -448,6 +448,36 @@ static long bch2_ioctl_query_accounting(struct bch_fs *c,
  	return ret;
  }
 
+static long bch2_ioctl_query_counters(struct bch_fs *c,
+			struct bch_ioctl_query_counters __user *user_arg)
+{
+	struct bch_ioctl_query_counters arg;

fs/bcachefs/chardev.c static long bch2_ioctl_query_accounting(struct bch_fs *c, · line 464

Quoted 10 lines
fs/bcachefs/chardev.c +10 -0
+	int ret = copy_from_user_errcode(&arg, user_arg, sizeof(arg));
+	if (ret)
+		return ret;
+
+	if ((arg.flags & !BCH_IOCTL_QUERY_COUNTERS_MOUNT) ||
+	    arg.pad)
+		return -EINVAL;
+
+	arg.nr = min(arg.nr, BCH_COUNTER_NR);
+	ret = put_user(arg.nr, &user_arg->nr);

This will change the input parameter @nr (records the size of filling space). Instead, how about introducing the extra parameter @max to keep the input array size?

Thanks,
Hongbo

Quoted 53 lines
fs/bcachefs/chardev.c +18 -0
+	if (ret)
+		return ret;
+
+	for (unsigned i = 0; i < arg.nr; i++) {
+		u64 v = !(arg.flags & BCH_IOCTL_QUERY_COUNTERS_MOUNT)
+			? percpu_u64_get(&c->counters[i])
+			: c->counters_on_mount[i];
+
+		ret = put_user(v, &user_arg->d[i]);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
  /* obsolete, didn't allow for new data types: */
  static long bch2_ioctl_dev_usage(struct bch_fs *c,
  				 struct bch_ioctl_dev_usage __user *user_arg)
@@ -710,6 +740,8 @@ long bch2_fs_ioctl(struct bch_fs *c, unsigned cmd, void __user *arg)
  		BCH_IOCTL(fsck_online, struct bch_ioctl_fsck_online);
  	case BCH_IOCTL_QUERY_ACCOUNTING:
  		return bch2_ioctl_query_accounting(c, arg);
+	case BCH_IOCTL_QUERY_COUNTERS:
+		return bch2_ioctl_query_counters(c, arg);
  	default:
  		return -ENOTTY;
  	}
fs/bcachefs/sb-counters.c +1 -1
index 6992e7469112..67f4e7a5aa5a 100644
@@ -5,7 +5,7 @@
 
  /* BCH_SB_FIELD_counters */
 
-static const char * const bch2_counter_names[] = {
+const char * const bch2_counter_names[] = {
  #define x(t, n, ...) (#t),
  	BCH_PERSISTENT_COUNTERS()
  #undef x
fs/bcachefs/sb-counters.h +1 -0
index 81f8aec9fcb1..eef7211edf42 100644
@@ -11,6 +11,7 @@ int bch2_sb_counters_from_cpu(struct bch_fs *);
  void bch2_fs_counters_exit(struct bch_fs *);
  int bch2_fs_counters_init(struct bch_fs *);
 
+extern const char * const bch2_counter_names[];
  extern const struct bch_sb_field_ops bch_sb_field_ops_counters;
 
  #endif // _BCACHEFS_SB_COUNTERS_H
#2 lore raw
Quoted 54 lines · Hongbo Li
Quoted 51 lines · Kent Overstreet

Add an ioctl for querying counters, the same ones provided in /sys/fs/bcachefs/<uuid>/counters/, but more suitable for a 'bcachefs top' command.

---
  fs/bcachefs/bcachefs_ioctl.h | 10 ++++++++++
  fs/bcachefs/chardev.c        | 32 ++++++++++++++++++++++++++++++++
  fs/bcachefs/sb-counters.c    |  2 +-
  fs/bcachefs/sb-counters.h    |  1 +
  4 files changed, 44 insertions(+), 1 deletion(-)
fs/bcachefs/bcachefs_ioctl.h +10 -0
index 3c23bdf788ce..f1b746fac007 100644
@@ -87,6 +87,7 @@ struct bch_ioctl_incremental {
  #define BCH_IOCTL_FSCK_OFFLINE	_IOW(0xbc,	19,  struct bch_ioctl_fsck_offline)
  #define BCH_IOCTL_FSCK_ONLINE	_IOW(0xbc,	20,  struct bch_ioctl_fsck_online)
  #define BCH_IOCTL_QUERY_ACCOUNTING _IOW(0xbc,	21,  struct bch_ioctl_query_accounting)
+#define BCH_IOCTL_QUERY_COUNTERS _IOW(0xbc,	21,  struct bch_ioctl_query_counters)
  /* ioctl below act on a particular file, not the filesystem as a whole: */
@@ -443,4 +444,13 @@ struct bch_ioctl_query_accounting {
  	struct bkey_i_accounting accounting[];
  };
+#define BCH_IOCTL_QUERY_COUNTERS_MOUNT	(1 << 0)
+
+struct bch_ioctl_query_counters {
+	__u16			nr;
+	__u16			flags;
+	__u32			pad;
+	__u64			d[];
+};
+
  #endif /* _BCACHEFS_IOCTL_H */
fs/bcachefs/chardev.c +9 -0
index 46e9e32105a9..c2d9efe8db53 100644
@@ -448,6 +448,36 @@ static long bch2_ioctl_query_accounting(struct bch_fs *c,
  	return ret;
  }
+static long bch2_ioctl_query_counters(struct bch_fs *c,
+			struct bch_ioctl_query_counters __user *user_arg)
+{
+	struct bch_ioctl_query_counters arg;
+	int ret = copy_from_user_errcode(&arg, user_arg, sizeof(arg));
+	if (ret)
+		return ret;
+
+	if ((arg.flags & !BCH_IOCTL_QUERY_COUNTERS_MOUNT) ||
Quoted 8 lines

fs/bcachefs/chardev.c static long bch2_ioctl_query_accounting(struct bch_fs *c, · line 464

Quoted 5 lines
fs/bcachefs/chardev.c +5 -0
+	    arg.pad)
+		return -EINVAL;
+
+	arg.nr = min(arg.nr, BCH_COUNTER_NR);
+	ret = put_user(arg.nr, &user_arg->nr);

This will change the input parameter @nr (records the size of filling space). Instead, how about introducing the extra parameter @max to keep the input array size?

Well, this is the pattern I've been doing so far through the other bcachefs ioctls that return arrays, so I'm inclined to stick with that.

Although your idea is a good one, that would be more standard - we'd be returning something more like just a darray.