Skip to contentSkip to conversation

btrfs-progs: boilerplate code for printing devices.

RFC PATCH New Anand Jain 1 participant 3 messages Jul 3, 2023 Btrfs file system

lore
Share & tools
lore

Anand Jain

author
#0 lore raw

cover btrfs-progs: boilerplate code for printing devices.

This is a set of boilerplate code to print the whole list of devices wherever in the code, for example:

	#include "kernel-shared/print-tree.h"

	btrfs_print_devlist(NULL);

This code has been helping me to debug and fix certain bugs for some time.

The code is a port from its kernel version and has been roughly optimized but not cleaned. However, I am sending it out to see if it can be integrated. Otherwise, I am fine with maintaining it externally and keeping it updated when needed. Thx.

Anand Jain (2):
btrfs-progs: print device list volumes-c api
btrfs-progs: print device list

 kernel-shared/print-tree.c | 71 ++++++++++++++++++++++++++++++++++++++
 kernel-shared/print-tree.h |  1 +
 kernel-shared/volumes.c    |  5 +++
 kernel-shared/volumes.h    |  2 +-
 4 files changed, 78 insertions(+), 1 deletion(-)
Signature · 1 line
2.31.1
author
#1 lore raw

RFC PATCH 1/2 btrfs-progs: print device list volumes-c api

We need fs_uuid to print the device list. Export it.

---
 kernel-shared/volumes.c | 5 +++++
 kernel-shared/volumes.h | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)
kernel-shared/volumes.c +5 -0
index 92282524867d..c6c71fd29215 100644
@@ -2939,3 +2939,8 @@ int btrfs_fix_device_and_super_size(struct btrfs_fs_info *fs_info)
 	}
 	return ret;
 }
+
+struct list_head * btrfs_get_fs_uuids(void)
+{
+	return &fs_uuids;
+}
kernel-shared/volumes.h +1 -1
index ab5ac40269bb..2022ce37cf09 100644
@@ -312,5 +312,5 @@ int btrfs_bg_type_to_nparity(u64 flags);
 int btrfs_bg_type_to_sub_stripes(u64 flags);
 u64 btrfs_bg_flags_for_device_num(int number);
 bool btrfs_bg_type_is_stripey(u64 flags);
-
+struct list_head * btrfs_get_fs_uuids(void);
 #endif
Signature · 1 line
2.31.1
author
#2 lore raw

RFC PATCH 2/2 btrfs-progs: print device list

In reply to Anand Jain

Provides the boilerplate code to print the device list.

---
 kernel-shared/print-tree.c | 71 ++++++++++++++++++++++++++++++++++++++
 kernel-shared/print-tree.h |  1 +
 2 files changed, 72 insertions(+)
kernel-shared/print-tree.c +71 -0
index 0f7f7b72f96a..b3699e70b979 100644
@@ -18,6 +18,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/types.h>
 #include <uuid/uuid.h>
 #include <ctype.h>
 #include "kerncompat.h"
@@ -2108,3 +2109,73 @@ void btrfs_print_superblock(struct btrfs_super_block *sb, int full)
 		print_backup_roots(sb);
 	}
 }
+
+#define BPSL    256
+
+#define BTRFS_SEQ_PRINT(plist, arg) \
+	do { \
+		snprintf(str, BPSL, plist, arg); \
+		printf("%s", str); \
+	} while (0)
+
+static void print_a_device(struct btrfs_device *device)
+{
+	char str[BPSL];
+	char uuidstr[BTRFS_UUID_UNPARSED_SIZE];
+
+	uuid_unparse(device->uuid, uuidstr);
+	BTRFS_SEQ_PRINT("\t[[UUID: %s]]\n", uuidstr);
+	BTRFS_SEQ_PRINT("\t\tdev_addr:\t%p\n", device);
+	BTRFS_SEQ_PRINT("\t\tdevice:\t\t%s\n", device->name);
+	BTRFS_SEQ_PRINT("\t\tdevid:\t\t%llu\n", device->devid);
+	BTRFS_SEQ_PRINT("\t\tgeneration:\t%llu\n", device->generation);
+	BTRFS_SEQ_PRINT("\t\ttotal_bytes:\t%llu\n", device->total_bytes);
+	BTRFS_SEQ_PRINT("\t\tbytes_used:\t%llu\n", device->bytes_used);
+	BTRFS_SEQ_PRINT("\t\ttype:\t\t%llu\n", device->type);
+	BTRFS_SEQ_PRINT("\t\tio_align:\t%u\n", device->io_align);
+	BTRFS_SEQ_PRINT("\t\tio_width:\t%u\n", device->io_width);
+	BTRFS_SEQ_PRINT("\t\tsector_size:\t%u\n", device->sector_size);
+}
+
+static void print_a_fs_device(struct btrfs_fs_devices *fs_devices)
+{
+	char str[BPSL];
+	char uuidstr[BTRFS_UUID_UNPARSED_SIZE];
+	struct btrfs_device *device = NULL;
+	size_t sz = sizeof(*fs_devices);
+
+	uuid_unparse(fs_devices->fsid, uuidstr);
+	BTRFS_SEQ_PRINT("[fsid: %s]\n", uuidstr);
+
+	BTRFS_SEQ_PRINT("\tsize:\t\t\t%ld\n", sz);
+
+	uuid_unparse(fs_devices->metadata_uuid, uuidstr);
+	BTRFS_SEQ_PRINT("\tmetadata_uuid:\t\t%s\n", uuidstr);
+
+	BTRFS_SEQ_PRINT("\tfs_devs_addr:\t\t%p\n", fs_devices);
+	BTRFS_SEQ_PRINT("\ttotal_rw_bytes:\t\t%llu\n", fs_devices->total_rw_bytes);
+
+	list_for_each_entry(device, &fs_devices->devices, dev_list) {
+		print_a_device(device);
+	}
+}
+
+
+void btrfs_print_devlist(struct btrfs_fs_devices *the_fs_devices)
+{
+	struct list_head *fs_uuids = btrfs_get_fs_uuids();
+	struct list_head *cur_uuid;
+
+	list_for_each(cur_uuid, fs_uuids) {
+		struct btrfs_fs_devices *fs_devices;
+
+		fs_devices  = list_entry(cur_uuid, struct btrfs_fs_devices, list);
+		if (the_fs_devices) {
+			if (the_fs_devices == fs_devices)
+				print_a_fs_device(fs_devices);
+		} else {
+			print_a_fs_device(fs_devices);
+		}
+		printf("\n");
+	}
+}
kernel-shared/print-tree.h +1 -0
index 80fb6ef75ff5..826e9281ef6f 100644
@@ -42,5 +42,6 @@ void print_extent_item(struct extent_buffer *eb, int slot, int metadata);
 void print_objectid(FILE *stream, u64 objectid, u8 type);
 void print_key_type(FILE *stream, u64 objectid, u8 type);
 void btrfs_print_superblock(struct btrfs_super_block *sb, int full);
+void btrfs_print_devlist(struct btrfs_fs_devices *the_fs_devices);
 
 #endif
Signature · 1 line
2.31.1