Skip to contentSkip to conversation

f2fs: add some sysfs nodes for segment

PATCH In review Guowei Du 3 participants 3 messages Jul 6, 2022 – Jul 12, 2022 F2FS file system

lore
Share & tools
lore

Guowei Du

author
#0 lore raw

PATCH 1/1 f2fs: add some sysfs nodes for segment

From: duguowei <[email protected]>

There are two ways to show meta segment information, one is by dump.f2fs, another is by sysfs node. But, sometimes dump needs root privilege,So adding a few sysfs nodes.

The generic permission of node is 0444(S_IRUGO).

$ cd /sys/fs/f2fs/DEVICE/...
$ ls -l
...
-r--r--r-- 1 root root 4096 7月   5 23:21 reserved_segments
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count_ckpt
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count_main
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count_nat
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count_sit
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count_ssa
...
$ sudo dump.f2fs -d 1 DEVICE
...
Super block
segment_count                           [0x      26 : 38]
segment_count_ckpt                      [0x       2 : 2]
segment_count_sit                       [0x       2 : 2]
segment_count_nat                       [0x       2 : 2]
segment_count_ssa                       [0x       1 : 1]
segment_count_main                      [0x      1f : 31]
...
Checkpoint
rsvd_segment_count                      [0x       e : 14]
...
---
 fs/f2fs/sysfs.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
fs/f2fs/sysfs.c +63 -0
index 4c50aedd5144..05350bba2c83 100644
@@ -697,6 +697,55 @@ static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
 	return sprintf(buf, "unsupported\n");
 }
 
+static ssize_t segment_count_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count));
+}
+
+static ssize_t segment_count_ckpt_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count_ckpt));
+}
+
+static ssize_t segment_count_sit_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count_sit));
+}
+
+static ssize_t segment_count_nat_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count_nat));
+}
+
+static ssize_t segment_count_ssa_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count_ssa));
+}
+
+static ssize_t segment_count_main_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count_main));
+}
+
+static ssize_t reserved_segments_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(reserved_segments(sbi)));
+}
+
 #define F2FS_SB_FEATURE_RO_ATTR(_name, _feat)			\
 static struct f2fs_attr f2fs_attr_sb_##_name = {		\
 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
@@ -801,6 +850,13 @@ F2FS_GENERAL_RO_ATTR(moved_blocks_background);
 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
 F2FS_GENERAL_RO_ATTR(avg_vblocks);
 #endif
+F2FS_GENERAL_RO_ATTR(segment_count);
+F2FS_GENERAL_RO_ATTR(segment_count_ckpt);
+F2FS_GENERAL_RO_ATTR(segment_count_sit);
+F2FS_GENERAL_RO_ATTR(segment_count_nat);
+F2FS_GENERAL_RO_ATTR(segment_count_ssa);
+F2FS_GENERAL_RO_ATTR(segment_count_main);
+F2FS_GENERAL_RO_ATTR(reserved_segments);
 
 #ifdef CONFIG_FS_ENCRYPTION
 F2FS_FEATURE_RO_ATTR(encryption);
@@ -934,6 +990,13 @@ static struct attribute *f2fs_attrs[] = {
 	ATTR_LIST(gc_reclaimed_segments),
 	ATTR_LIST(max_fragment_chunk),
 	ATTR_LIST(max_fragment_hole),
+	ATTR_LIST(segment_count),
+	ATTR_LIST(segment_count_ckpt),
+	ATTR_LIST(segment_count_sit),
+	ATTR_LIST(segment_count_nat),
+	ATTR_LIST(segment_count_ssa),
+	ATTR_LIST(segment_count_main),
+	ATTR_LIST(reserved_segments),
 	NULL,
 };
 ATTRIBUTE_GROUPS(f2fs);
Signature · 1 line
2.36.1
maintainer
#1 lore raw
Quoted 86 lines · Guowei Du

From: duguowei <[email protected]>

There are two ways to show meta segment information, one is by dump.f2fs, another is by sysfs node. But, sometimes dump needs root privilege,So adding a few sysfs nodes.

The generic permission of node is 0444(S_IRUGO).

$ cd /sys/fs/f2fs/DEVICE/...
$ ls -l
...
(quoted diff) +0 -7
-r--r--r-- 1 root root 4096 7月   5 23:21 reserved_segments
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count_ckpt
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count_main
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count_nat
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count_sit
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count_ssa
...
$ sudo dump.f2fs -d 1 DEVICE
...
Super block
segment_count                           [0x      26 : 38]
segment_count_ckpt                      [0x       2 : 2]
segment_count_sit                       [0x       2 : 2]
segment_count_nat                       [0x       2 : 2]
segment_count_ssa                       [0x       1 : 1]
segment_count_main                      [0x      1f : 31]
...
Checkpoint
rsvd_segment_count                      [0x       e : 14]
...
---
 fs/f2fs/sysfs.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
fs/f2fs/sysfs.c +38 -0
index 4c50aedd5144..05350bba2c83 100644
@@ -697,6 +697,55 @@ static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
 	return sprintf(buf, "unsupported\n");
 }
 
+static ssize_t segment_count_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count));
+}
+
+static ssize_t segment_count_ckpt_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count_ckpt));
+}
+
+static ssize_t segment_count_sit_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count_sit));
+}
+
+static ssize_t segment_count_nat_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count_nat));
+}
+
+static ssize_t segment_count_ssa_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count_ssa));
+}
+
+static ssize_t segment_count_main_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{

fs/f2fs/sysfs.c static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a, · line 747

Quoted 10 lines
fs/f2fs/sysfs.c +10 -0
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count_main));
+}
+
+static ssize_t reserved_segments_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(reserved_segments(sbi)));
+}

Can we create a macro to do the above functions?

Quoted 34 lines
fs/f2fs/sysfs.c +15 -0
+
 #define F2FS_SB_FEATURE_RO_ATTR(_name, _feat)			\
 static struct f2fs_attr f2fs_attr_sb_##_name = {		\
 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
@@ -801,6 +850,13 @@ F2FS_GENERAL_RO_ATTR(moved_blocks_background);
 F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
 F2FS_GENERAL_RO_ATTR(avg_vblocks);
 #endif
+F2FS_GENERAL_RO_ATTR(segment_count);
+F2FS_GENERAL_RO_ATTR(segment_count_ckpt);
+F2FS_GENERAL_RO_ATTR(segment_count_sit);
+F2FS_GENERAL_RO_ATTR(segment_count_nat);
+F2FS_GENERAL_RO_ATTR(segment_count_ssa);
+F2FS_GENERAL_RO_ATTR(segment_count_main);
+F2FS_GENERAL_RO_ATTR(reserved_segments);
 
 #ifdef CONFIG_FS_ENCRYPTION
 F2FS_FEATURE_RO_ATTR(encryption);
@@ -934,6 +990,13 @@ static struct attribute *f2fs_attrs[] = {
 	ATTR_LIST(gc_reclaimed_segments),
 	ATTR_LIST(max_fragment_chunk),
 	ATTR_LIST(max_fragment_hole),
+	ATTR_LIST(segment_count),
+	ATTR_LIST(segment_count_ckpt),
+	ATTR_LIST(segment_count_sit),
+	ATTR_LIST(segment_count_nat),
+	ATTR_LIST(segment_count_ssa),
+	ATTR_LIST(segment_count_main),
+	ATTR_LIST(reserved_segments),
 	NULL,
 };
 ATTRIBUTE_GROUPS(f2fs);
Signature · 1 line
2.36.1
maintainer
#2 lore raw
Quoted 7 lines · Jaegeuk Kim
Quoted 6 lines · Guowei Du

From: duguowei <[email protected]>

There are two ways to show meta segment information, one is by dump.f2fs, another is by sysfs node. But, sometimes dump needs root privilege,So adding a few sysfs nodes.

If this is used for debug, how about checking /sys/kernel/debug/f2fs/status?

Thanks,

Quoted 127 lines
Quoted 80 lines

The generic permission of node is 0444(S_IRUGO).

$ cd /sys/fs/f2fs/DEVICE/...
$ ls -l
...
(quoted diff) +0 -7
-r--r--r-- 1 root root 4096 7月   5 23:21 reserved_segments
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count_ckpt
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count_main
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count_nat
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count_sit
-r--r--r-- 1 root root 4096 7月   5 23:21 segment_count_ssa
...
$ sudo dump.f2fs -d 1 DEVICE
...
Super block
segment_count                           [0x      26 : 38]
segment_count_ckpt                      [0x       2 : 2]
segment_count_sit                       [0x       2 : 2]
segment_count_nat                       [0x       2 : 2]
segment_count_ssa                       [0x       1 : 1]
segment_count_main                      [0x      1f : 31]
...
Checkpoint
rsvd_segment_count                      [0x       e : 14]
...
---
  fs/f2fs/sysfs.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 63 insertions(+)
fs/f2fs/sysfs.c +38 -0
index 4c50aedd5144..05350bba2c83 100644
@@ -697,6 +697,55 @@ static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
  	return sprintf(buf, "unsupported\n");
  }
 
+static ssize_t segment_count_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count));
+}
+
+static ssize_t segment_count_ckpt_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count_ckpt));
+}
+
+static ssize_t segment_count_sit_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count_sit));
+}
+
+static ssize_t segment_count_nat_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count_nat));
+}
+
+static ssize_t segment_count_ssa_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count_ssa));
+}
+
+static ssize_t segment_count_main_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{

fs/f2fs/sysfs.c static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a, · line 747

Quoted 10 lines
fs/f2fs/sysfs.c +10 -0
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(F2FS_RAW_SUPER(sbi)->segment_count_main));
+}
+
+static ssize_t reserved_segments_show(struct f2fs_attr *a,
+		struct f2fs_sb_info *sbi, char *buf)
+{
+	return sprintf(buf, "%llu\n",
+			(unsigned long long)(reserved_segments(sbi)));
+}

Can we create a macro to do the above functions?

Quoted 34 lines
fs/f2fs/sysfs.c +15 -0
+
  #define F2FS_SB_FEATURE_RO_ATTR(_name, _feat)			\
  static struct f2fs_attr f2fs_attr_sb_##_name = {		\
  	.attr = {.name = __stringify(_name), .mode = 0444 },	\
@@ -801,6 +850,13 @@ F2FS_GENERAL_RO_ATTR(moved_blocks_background);
  F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
  F2FS_GENERAL_RO_ATTR(avg_vblocks);
  #endif
+F2FS_GENERAL_RO_ATTR(segment_count);
+F2FS_GENERAL_RO_ATTR(segment_count_ckpt);
+F2FS_GENERAL_RO_ATTR(segment_count_sit);
+F2FS_GENERAL_RO_ATTR(segment_count_nat);
+F2FS_GENERAL_RO_ATTR(segment_count_ssa);
+F2FS_GENERAL_RO_ATTR(segment_count_main);
+F2FS_GENERAL_RO_ATTR(reserved_segments);
 
  #ifdef CONFIG_FS_ENCRYPTION
  F2FS_FEATURE_RO_ATTR(encryption);
@@ -934,6 +990,13 @@ static struct attribute *f2fs_attrs[] = {
  	ATTR_LIST(gc_reclaimed_segments),
  	ATTR_LIST(max_fragment_chunk),
  	ATTR_LIST(max_fragment_hole),
+	ATTR_LIST(segment_count),
+	ATTR_LIST(segment_count_ckpt),
+	ATTR_LIST(segment_count_sit),
+	ATTR_LIST(segment_count_nat),
+	ATTR_LIST(segment_count_ssa),
+	ATTR_LIST(segment_count_main),
+	ATTR_LIST(reserved_segments),
  	NULL,
  };
  ATTRIBUTE_GROUPS(f2fs);
Signature · 1 line
2.36.1