Skip to contentSkip to conversation

btrfs-progs: print-tree: cleanup for regular bitmap based flags print

PATCH New Qu Wenruo 1 participant 4 messages Oct 4, 2024 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs-progs: print-tree: cleanup for regular bitmap based flags print

The first 2 are small cleanups for __print_readable_flag().

The last one introduces an sprint version, sprint_readable_flag(), allowing the same bitmap handling of print_readable_flag() to be output into a string buffer.

And use that sprint_readable_flag() to handle inode flags, inspired by a recent report that Synology's out-of-tree btrfs can not be handled by upstream kernel (unsupported inode flag).

This allows print-tree to handle the unknown flags of inode flags.

Unfortunately I didn't find any other location can benefit the sprint_readable_flag() yet.

It's either bg flags which needs special handling for SINGLE profile, or not bitmap in the first place (e.g. compress flags).

Qu Wenruo (3):
btrfs-progs: print-tree: use ARRAY_SIZE() to replace open-coded ones
btrfs-progs: print-tree: cleanup __print_readable_flag()
btrfs-progs: print-tree: use readable_flag_entry for inode flags

 kernel-shared/print-tree.c | 128 +++++++++++++++++++++----------------
 1 file changed, 72 insertions(+), 56 deletions(-)

--
2.46.2

author
#1 lore raw

PATCH 1/3 btrfs-progs: print-tree: use ARRAY_SIZE() to replace open-coded ones

For compat_ro_flags_num and incompat_flags_num, just use ARRAY_SIZE().

---
 kernel-shared/print-tree.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
kernel-shared/print-tree.c +2 -4
index ab85432e2807..14f7dcdf0ee9 100644
@@ -1889,8 +1889,7 @@ static struct readable_flag_entry compat_ro_flags_array[] = {
 	DEF_COMPAT_RO_FLAG_ENTRY(FREE_SPACE_TREE_VALID),
 	DEF_COMPAT_RO_FLAG_ENTRY(BLOCK_GROUP_TREE),
 };
-static const int compat_ro_flags_num = sizeof(compat_ro_flags_array) /
-				       sizeof(struct readable_flag_entry);
+static const int compat_ro_flags_num = ARRAY_SIZE(compat_ro_flags_array);
 
 #define DEF_INCOMPAT_FLAG_ENTRY(bit_name)		\
 	{BTRFS_FEATURE_INCOMPAT_##bit_name, #bit_name}
@@ -1913,8 +1912,7 @@ static struct readable_flag_entry incompat_flags_array[] = {
 	DEF_INCOMPAT_FLAG_ENTRY(RAID_STRIPE_TREE),
 	DEF_INCOMPAT_FLAG_ENTRY(SIMPLE_QUOTA),
 };
-static const int incompat_flags_num = sizeof(incompat_flags_array) /
-				      sizeof(struct readable_flag_entry);
+static const int incompat_flags_num = ARRAY_SIZE(incompat_flags_array);
 
 #define DEF_HEADER_FLAG_ENTRY(bit_name)			\
 	{BTRFS_HEADER_FLAG_##bit_name, #bit_name}
Signature · 1 line
2.46.2
author
#2 lore raw

PATCH 2/3 btrfs-progs: print-tree: cleanup __print_readable_flag()

In reply to Qu Wenruo

This includes:

- Remove the "__" prefix
Now the "__" is no longer recommended, and there is no function taking the "print_readable_flag" in the first place.

- Move the supported flags calculation into print_readable_flag() Since all callers are doing the same work before calling the function.

---
 kernel-shared/print-tree.c | 33 ++++++++++++---------------------
 1 file changed, 12 insertions(+), 21 deletions(-)
kernel-shared/print-tree.c +12 -21
index 14f7dcdf0ee9..bbd625d9b1eb 100644
@@ -1933,13 +1933,17 @@ static struct readable_flag_entry super_flags_array[] = {
 };
 static const int super_flags_num = ARRAY_SIZE(super_flags_array);
 
-static void __print_readable_flag(u64 flag, struct readable_flag_entry *array,
-				  int array_size, u64 supported_flags)
+static void print_readable_flag(u64 flag, struct readable_flag_entry *array,
+				int array_size)
 {
 	int i;
 	int first = 1;
+	u64 supported_flags = 0;
 	struct readable_flag_entry *entry;
 
+	for (i = 0; i < array_size; i++)
+		supported_flags |= array[i].bit;
+
 	if (!flag)
 		return;
 
@@ -1966,33 +1970,20 @@ static void __print_readable_flag(u64 flag, struct readable_flag_entry *array,
 
 static void print_readable_compat_ro_flag(u64 flag)
 {
-	u64 print_flags = 0;
-
-	for (int i = 0; i < compat_ro_flags_num; i++)
-		print_flags |= compat_ro_flags_array[i].bit;
-	return __print_readable_flag(flag, compat_ro_flags_array,
-				     compat_ro_flags_num,
-				     print_flags);
+	return print_readable_flag(flag, compat_ro_flags_array,
+				   compat_ro_flags_num);
 }
 
 static void print_readable_incompat_flag(u64 flag)
 {
-	u64 print_flags = 0;
-
-	for (int i = 0; i < incompat_flags_num; i++)
-		print_flags |= incompat_flags_array[i].bit;
-	return __print_readable_flag(flag, incompat_flags_array,
-				     incompat_flags_num, print_flags);
+	return print_readable_flag(flag, incompat_flags_array,
+				   incompat_flags_num);
 }
 
 static void print_readable_super_flag(u64 flag)
 {
-	u64 print_flags = 0;
-
-	for (int i = 0; i < super_flags_num; i++)
-		print_flags |= super_flags_array[i].bit;
-	return __print_readable_flag(flag, super_flags_array,
-				     super_flags_num, print_flags);
+	return print_readable_flag(flag, super_flags_array,
+				   super_flags_num);
 }
 
 static void print_sys_chunk_array(struct btrfs_super_block *sb)
Signature · 1 line
2.46.2
author
#3 lore raw

PATCH 3/3 btrfs-progs: print-tree: use readable_flag_entry for inode flags

In reply to Qu Wenruo

The current print-tree can not handle unsupported inode flags, e.g. created by Synology's out-of-tree btrfs implementation.

The existing one just checks all the supported flags, and if no flag hits, it will output "none" no matter if there is any unsupported one.

Fix this by implementing sprint_readable_flag(), and use the same handling of print_readable_flag().
Although for inode flag, adds one extra handling to output "none" if no flag hit at all.

---
 kernel-shared/print-tree.c | 89 +++++++++++++++++++++++++-------------
 1 file changed, 58 insertions(+), 31 deletions(-)
kernel-shared/print-tree.c +58 -31
index bbd625d9b1eb..bd2117e637c2 100644
@@ -183,9 +183,43 @@ static void print_inode_ref_item(struct extent_buffer *eb, u32 size,
 	}
 }
 
+struct readable_flag_entry {
+	u64 bit;
+	char *output;
+};
+
 /* The minimal length for the string buffer of block group/chunk flags */
 #define BG_FLAG_STRING_LEN	64
 
+static void sprint_readable_flag(char *restrict dest, u64 flag,
+				 struct readable_flag_entry *array,
+				 int array_size)
+{
+	int i;
+	u64 supported_flags = 0;
+	int cur = 0;
+
+	dest[0] = '\0';
+	for (i = 0; i < array_size; i++)
+		supported_flags |= array[i].bit;
+
+	for (i = 0; i < array_size; i++) {
+		struct readable_flag_entry *entry = array + i;
+
+		if ((flag & supported_flags) && (flag & entry->bit)) {
+			if (dest[0])
+				cur += sprintf(dest + cur, "|");
+			cur += sprintf(dest + cur, "%s", entry->output);
+		}
+	}
+	flag &= ~supported_flags;
+	if (flag) {
+		if (dest[0])
+			cur += sprintf(dest + cur, "|");
+		cur += sprintf(dest + cur, "UNKNOWN: 0x%llx", flag);
+	}
+}
+
 static void bg_flags_to_str(u64 flags, char *ret)
 {
 	int empty = 1;
@@ -932,37 +966,35 @@ static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
 	}
 }
 
-/* Btrfs inode flag stringification helper */
-#define STRCAT_ONE_INODE_FLAG(flags, name, empty, dst) ({			\
-	if (flags & BTRFS_INODE_##name) {				\
-		if (!empty)						\
-			strcat(dst, "|");				\
-		strcat(dst, #name);					\
-		empty = 0;						\
-	}								\
-})
+#define DEF_INODE_FLAG_ENTRY(name)			\
+	{ BTRFS_INODE_##name, #name }
+
+static struct readable_flag_entry inode_flags_array[] = {
+	DEF_INODE_FLAG_ENTRY(NODATASUM),
+	DEF_INODE_FLAG_ENTRY(NODATACOW),
+	DEF_INODE_FLAG_ENTRY(READONLY),
+	DEF_INODE_FLAG_ENTRY(NOCOMPRESS),
+	DEF_INODE_FLAG_ENTRY(PREALLOC),
+	DEF_INODE_FLAG_ENTRY(SYNC),
+	DEF_INODE_FLAG_ENTRY(IMMUTABLE),
+	DEF_INODE_FLAG_ENTRY(APPEND),
+	DEF_INODE_FLAG_ENTRY(NODUMP),
+	DEF_INODE_FLAG_ENTRY(NOATIME),
+	DEF_INODE_FLAG_ENTRY(DIRSYNC),
+	DEF_INODE_FLAG_ENTRY(COMPRESS),
+	DEF_INODE_FLAG_ENTRY(ROOT_ITEM_INIT),
+};
+static const int inode_flags_num = ARRAY_SIZE(inode_flags_array);
 
 /*
- * Caller should ensure sizeof(*ret) >= 102: all characters plus '|' of
- * BTRFS_INODE_* flags
+ * Caller should ensure sizeof(*ret) >= 129: all characters plus '|' of
+ * BTRFS_INODE_* flags + "UNKNOWN: 0xffffffffffffffff"
  */
 static void inode_flags_to_str(u64 flags, char *ret)
 {
-	int empty = 1;
-
-	STRCAT_ONE_INODE_FLAG(flags, NODATASUM, empty, ret);
-	STRCAT_ONE_INODE_FLAG(flags, NODATACOW, empty, ret);
-	STRCAT_ONE_INODE_FLAG(flags, READONLY, empty, ret);
-	STRCAT_ONE_INODE_FLAG(flags, NOCOMPRESS, empty, ret);
-	STRCAT_ONE_INODE_FLAG(flags, PREALLOC, empty, ret);
-	STRCAT_ONE_INODE_FLAG(flags, SYNC, empty, ret);
-	STRCAT_ONE_INODE_FLAG(flags, IMMUTABLE, empty, ret);
-	STRCAT_ONE_INODE_FLAG(flags, APPEND, empty, ret);
-	STRCAT_ONE_INODE_FLAG(flags, NODUMP, empty, ret);
-	STRCAT_ONE_INODE_FLAG(flags, NOATIME, empty, ret);
-	STRCAT_ONE_INODE_FLAG(flags, DIRSYNC, empty, ret);
-	STRCAT_ONE_INODE_FLAG(flags, COMPRESS, empty, ret);
-	if (empty)
+	sprint_readable_flag(ret, flags, inode_flags_array, inode_flags_num);
+	/* No flag hit at all, set the output to "none"*/
+	if (!ret[0])
 		strcat(ret, "none");
 }
 
@@ -1876,11 +1908,6 @@ static int check_csum_sblock(void *sb, int csum_size, u16 csum_type)
 	return !memcmp(sb, result, csum_size);
 }
 
-struct readable_flag_entry {
-	u64 bit;
-	char *output;
-};
-
 #define DEF_COMPAT_RO_FLAG_ENTRY(bit_name)		\
 	{BTRFS_FEATURE_COMPAT_RO_##bit_name, #bit_name}
 
Signature · 1 line
2.46.2