Skip to contentSkip to conversation

btrfs-progs: Add 2 new subcommands to inspect-internal

PATCH In review Divya Indi 7 participants 21 messages Oct 18, 2016 – Jun 9, 2017 Btrfs file system

lore
Share & tools
lore

Divya Indi

author
#0 lore raw

These patches aim to add 2 new subcommands that:
-> provide information about block groups
-> help to decide whether balance can reduce the no. of data block groups and if
it can, provides the block group object id for "-dvrange"

[PATCH 1/3] btrfs-progs: Generic functions to retrieve chunks and their bg info [PATCH 2/3] btrfs-progs: Add a command to show bg info
[PATCH 3/3] btrfs-progs: Add command to check if balance op is req

Now, you can run:
-------------------------------------------------------------------------------

$ btrfs inspect-internal balance_check /mnt/btrfs
               Start                 Len                Used
            12582912             8.00MiB           128.00KiB
           136708096           208.00MiB            74.00MiB

Total data bgs: 2
Total free space: 141.88MiB
For min used bg 12582912 used = 128.00KiB free = 7.88MiB run 'btrfs balance start -dvrange=12582912..12582913 <mountpoint>'

$ btrfs balance start -dvrange=12582912..12582913 /mnt/btrfs
Done, had to relocate 1 out of 4 chunks
$ btrfs inspect-internal balance_check /mnt/btrfs
               Start                 Len                Used
           136708096           208.00MiB            74.12MiB
Data block groups in fs = 1, no need to do balance.
$ btrfs inspect-internal bg_analysis /mnt/btrfs
                Type               Start                 Len                Used
              SYSTEM            20971520             8.00MiB            16.00KiB
            METADATA            29360128           102.38MiB           192.00KiB
                DATA           136708096           208.00MiB            74.12MiB

-------------------------------------------------------------------------------

Thanks to the suggestion by Hans van Kranenburg, and the python-btrfs module, an efficient alternative to retrieving block groups could be used:
Now, it first looks at the chunk tree, and for every chunk listed it looks up an exact match in the extent tree.

author
#1 lore raw

PATCH 1/3 btrfs-progs: Generic functions to retrieve chunks and their bg info

An efficient alternative to retrieving block groups:
get_chunks(): Walk the chunk tree to retrieve the chunks. get_bg_info(): For each retrieved chunk, lookup an exact match of block group in the extent tree.

---
 cmds-inspect.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 66 insertions(+), 0 deletions(-)
cmds-inspect.c +66 -0
index 4b7cea0..f435ea9 100644
@@ -81,6 +81,72 @@ out:
 	return !!ret;
 }
 
+static void bg_flags_to_str(u64 flags, char *ret)
+{
+	int empty = 1;
+
+	if (flags & BTRFS_BLOCK_GROUP_DATA) {
+		empty = 0;
+		strcpy(ret, "DATA");
+	}
+	if (flags & BTRFS_BLOCK_GROUP_METADATA) {
+		if (!empty)
+			strcat(ret, "|");
+		strcat(ret, "METADATA");
+	}
+	if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
+		if (!empty)
+			strcat(ret, "|");
+		strcat(ret, "SYSTEM");
+	}
+}
+
+/* Walking through the chunk tree to retrieve chunks. */
+
+static int get_chunks(int fd, struct btrfs_ioctl_search_args *chunk_args)
+{
+	struct btrfs_ioctl_search_key *sk;
+	int ret;
+	int e;
+
+	sk = &chunk_args->key;
+
+	sk->tree_id = BTRFS_CHUNK_TREE_OBJECTID;
+	sk->min_objectid = sk->max_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
+	sk->max_type = sk->min_type = BTRFS_CHUNK_ITEM_KEY;
+	sk->nr_items = 4096;
+
+	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, chunk_args);
+	e = errno;
+	if (ret < 0) {
+		fprintf(stderr, "ret %d error '%s'\n", ret,
+				strerror(e));
+	}
+	return ret;
+}
+
+/* Given the objectid, find the block group item in the extent tree */
+static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args,
+		       u64 objectid, unsigned long length)
+{
+	struct btrfs_ioctl_search_key *bg_sk;
+	int ret;
+	int e;
+
+	bg_sk = &bg_args->key;
+
+	bg_sk->min_objectid = bg_sk->max_objectid = objectid;
+	bg_sk->nr_items = 1;
+	bg_sk->min_offset = bg_sk->max_offset = length;
+
+	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, bg_args);
+	e = errno;
+	if (ret < 0) {
+		fprintf(stderr, "ret %d error '%s'\n", ret,
+				strerror(e));
+	}
+	return ret;
+}
 static const char * const cmd_inspect_inode_resolve_usage[] = {
 	"btrfs inspect-internal inode-resolve [-v] <inode> <path>",
 	"Get file system paths for the given inode",
Signature · 1 line
1.7.1
author
#2 lore raw

PATCH 2/3 btrfs-progs: Add a command to show bg info

Add a new subcommand to btrfs inspect-internal

btrfs inspect-internal bg_analysis <path> Gives information about all the block groups.

---
 cmds-inspect.c |  114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 114 insertions(+), 0 deletions(-)
cmds-inspect.c +114 -0
index f435ea9..0e2f15a 100644
@@ -147,6 +147,118 @@ static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args,
 	}
 	return ret;
 }
+
+static const char * const cmd_inspect_bg_analysis_usage[] = {
+	"btrfs inspect-internal bg_analysis <path> ",
+	"Get information about all block groups",
+	"",
+	"",
+	NULL
+};
+
+static int cmd_inspect_bg_analysis(int argc, char **argv)
+{
+	struct btrfs_ioctl_search_args args;
+	struct btrfs_ioctl_search_args bg_args;
+	struct btrfs_ioctl_search_header *header;
+	struct btrfs_ioctl_search_header *bg_header;
+	struct btrfs_ioctl_search_key *sk;
+	struct btrfs_ioctl_search_key *bg_sk;
+	struct btrfs_block_group_item *bg;
+	struct btrfs_chunk *chunk;
+	unsigned long off = 0;
+	unsigned long bg_off = 0;
+	DIR *dirstream = NULL;
+	int fd;
+	int i;
+	int ret = 0;
+	u64 used;
+	u64 flags;
+	char bg_type[32] = {0};
+
+	if (check_argc_exact(argc, 2))
+		usage(cmd_inspect_bg_analysis_usage);
+
+	fd = btrfs_open_dir(argv[optind], &dirstream, 1);
+	if (fd < 0)
+		return 1;
+
+	memset(&args, 0, sizeof(args));
+	sk = &args.key;
+	sk->min_offset = sk->min_transid = 0;
+	sk->max_offset = sk->max_transid = (u64)-1;
+	printf("%20s%20s%20s%20s\n", "Type", "Start", "Len", "Used");
+	while (1) {
+
+		/* Walk through the chunk tree and retrieve all the chunks */
+		ret = get_chunks(fd, &args);
+		if (ret < 0)
+			goto out;
+
+		/*
+		 * it should not happen.
+		 */
+		if (sk->nr_items == 0)
+			break;
+
+		off = 0;
+		memset(&bg_args, 0, sizeof(bg_args));
+		bg_sk = &bg_args.key;
+
+		bg_sk->tree_id = BTRFS_EXTENT_TREE_OBJECTID;
+		bg_sk->min_type = BTRFS_BLOCK_GROUP_ITEM_KEY;
+		bg_sk->max_type = BTRFS_BLOCK_GROUP_ITEM_KEY;
+		bg_sk->min_transid =  0;
+		bg_sk->max_transid = (u64)-1;
+
+		for (i = 0; i < sk->nr_items; i++) {
+			header = (struct btrfs_ioctl_search_header *)(args.buf
+								      + off);
+			off += sizeof(*header);
+			if (header->type == BTRFS_CHUNK_ITEM_KEY) {
+				chunk = (struct btrfs_chunk *)
+					(args.buf + off);
+
+				/* For every chunk lookup an exact match(bg) in
+				 * the extent tree and read its used values */
+				ret = get_bg_info(fd, &bg_args, header->offset,
+						  chunk->length);
+				if (ret < 0)
+					goto out;
+
+				/*
+				 * it should not happen.
+				 */
+				if (bg_sk->nr_items == 0)
+					continue;
+
+				bg_off = 0;
+				bg_header = (struct btrfs_ioctl_search_header *)
+					    (bg_args.buf + bg_off);
+				bg_off += sizeof(*bg_header);
+				bg = (struct btrfs_block_group_item *)
+				     (bg_args.buf + bg_off);
+
+				used = btrfs_block_group_used(bg);
+				memset(&bg_type, 0, 32);
+				flags = btrfs_block_group_flags(bg);
+				bg_flags_to_str(flags, bg_type);
+				printf("%20s%20llu%20s%20s\n",
+					bg_type,
+					bg_header->objectid,
+					pretty_size(bg_header->offset),
+					pretty_size(used));
+			}
+			off += header->len;
+			sk->min_offset = header->offset + header->len;
+		}
+		sk->nr_items = 4096;
+	}
+out:
+	close_file_or_dir(fd, dirstream);
+	return ret;
+}
+
 static const char * const cmd_inspect_inode_resolve_usage[] = {
 	"btrfs inspect-internal inode-resolve [-v] <inode> <path>",
 	"Get file system paths for the given inode",
@@ -702,6 +814,8 @@ const struct cmd_group inspect_cmd_group = {
 			0 },
 		{ "min-dev-size", cmd_inspect_min_dev_size,
 			cmd_inspect_min_dev_size_usage, NULL, 0 },
+		{ "bg_analysis", cmd_inspect_bg_analysis,
+			cmd_inspect_bg_analysis_usage, NULL, 0 },
 		{ "dump-tree", cmd_inspect_dump_tree,
 				cmd_inspect_dump_tree_usage, NULL, 0 },
 		{ "dump-super", cmd_inspect_dump_super,
Signature · 1 line
1.7.1
author
#3 lore raw

PATCH 3/3 btrfs-progs: Add command to check if balance op is req

Add new subcommand to btrfs inspect-internal

btrfs inspect-internal balance_check <path>
Checks whether 'btrfs balance' can help creating more space (Only considers data block groups).

---
 cmds-inspect.c |  147 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 147 insertions(+), 0 deletions(-)
cmds-inspect.c +147 -0
index 0e2f15a..5baaa49 100644
@@ -267,6 +267,151 @@ static const char * const cmd_inspect_inode_resolve_usage[] = {
 	NULL
 };
 
+static const char * const cmd_inspect_balance_check_usage[] = {
+	"btrfs inspect-internal balance_check <path>",
+	"To check whether 'btrfs balance' can help creating more space",
+	"",
+	"",
+	NULL
+};
+
+static int cmd_inspect_balance_check(int argc, char **argv)
+{
+	struct btrfs_ioctl_search_args args;
+	struct btrfs_ioctl_search_args bg_args;
+	struct btrfs_ioctl_search_key *sk;
+	struct btrfs_ioctl_search_key *bg_sk;
+	struct btrfs_ioctl_search_header *header;
+	struct btrfs_ioctl_search_header *bg_header;
+	struct btrfs_block_group_item *bg;
+	struct btrfs_chunk *chunk;
+	unsigned long off = 0;
+	unsigned long bg_off = 0;
+	DIR *dirstream = NULL;
+	int fd;
+	int i;
+	u64 total_free = 0;
+	u64 min_used = (u64)-1;
+	u64 free_of_min_used = 0;
+	u64 bg_of_min_used = 0;
+	u64 flags;
+	u64 used;
+	int ret = 0;
+	int nr_data_bgs = 0;
+
+	if (check_argc_exact(argc, 2))
+		usage(cmd_inspect_balance_check_usage);
+
+	fd = btrfs_open_dir(argv[optind], &dirstream, 1);
+	if (fd < 0)
+		return 1;
+
+	memset(&args, 0, sizeof(args));
+	sk = &args.key;
+	sk->min_offset = sk->min_transid = 0;
+	sk->max_offset = sk->max_transid = (u64)-1;
+
+	printf("%20s%20s%20s\n", "Start", "Len", "Used");
+	while (1) {
+		ret = get_chunks(fd, &args);
+		if (ret < 0)
+			goto out;
+
+		/*
+		 * it should not happen.
+		 */
+		if (sk->nr_items == 0)
+			break;
+
+		off = 0;
+		memset(&bg_args, 0, sizeof(bg_args));
+		bg_sk = &bg_args.key;
+
+		/* For every chunk, look up 1 exact match for block group in
+		 * the extent tree. */
+		bg_sk->tree_id = BTRFS_EXTENT_TREE_OBJECTID;
+		bg_sk->min_type = BTRFS_BLOCK_GROUP_ITEM_KEY;
+		bg_sk->max_type = BTRFS_BLOCK_GROUP_ITEM_KEY;
+		bg_sk->min_transid =  0;
+		bg_sk->max_transid = (u64)-1;
+
+		for (i = 0; i < sk->nr_items; i++) {
+			header = (struct btrfs_ioctl_search_header *)(args.buf
+								      + off);
+			off += sizeof(*header);
+			if (header->type == BTRFS_CHUNK_ITEM_KEY) {
+				chunk = (struct btrfs_chunk *)
+					(args.buf + off);
+				ret = get_bg_info(fd, &bg_args, header->offset,
+						  chunk->length);
+				if (ret < 0)
+					goto out;
+
+				/*
+				 * it should not happen.
+				 */
+				if (bg_sk->nr_items == 0)
+					continue;
+
+				bg_off = 0;
+				bg_header = (struct btrfs_ioctl_search_header *)
+					    (bg_args.buf + bg_off);
+				bg_off += sizeof(*bg_header);
+				bg = (struct btrfs_block_group_item *)
+				     (bg_args.buf + bg_off);
+
+				flags = btrfs_block_group_flags(bg);
+				if (flags & BTRFS_BLOCK_GROUP_DATA) {
+					used = btrfs_block_group_used(bg);
+					nr_data_bgs++;
+					printf("%20llu%20s%20s\n",
+						bg_header->objectid,
+						pretty_size(bg_header->offset),
+						pretty_size(used));
+					total_free += bg_header->offset - used;
+					if (min_used >= used) {
+						min_used = used;
+						free_of_min_used =
+							bg_header->offset - used;
+						bg_of_min_used =
+							bg_header->objectid;
+					}
+				}
+			}
+
+			off += header->len;
+			sk->min_offset = header->offset + header->len;
+		}
+		sk->nr_items = 4096;
+
+	}
+
+	if (nr_data_bgs <= 1) {
+		printf("Data block groups in fs = %d, no need to do balance.\n",
+				nr_data_bgs);
+		ret = 1;
+		goto out;
+	}
+
+	printf("Total data bgs: %d\nTotal free space: %s\n"
+	       "For min used bg %llu used = %s free = %s\n",
+		nr_data_bgs, pretty_size(total_free), bg_of_min_used,
+		pretty_size(min_used), pretty_size(free_of_min_used));
+
+	if (total_free - free_of_min_used > min_used) {
+		printf("run 'btrfs balance start -dvrange=%llu..%llu <mountpoint>'\n",
+				bg_of_min_used, bg_of_min_used + 1);
+		ret = 0;
+	} else {
+		printf("Please don't balance data block groups, no free space\n");
+		ret = 1;
+	}
+
+out:
+	close_file_or_dir(fd, dirstream);
+	return ret;
+}
+
 static int cmd_inspect_inode_resolve(int argc, char **argv)
 {
 	int fd;
@@ -816,6 +961,8 @@ const struct cmd_group inspect_cmd_group = {
 			cmd_inspect_min_dev_size_usage, NULL, 0 },
 		{ "bg_analysis", cmd_inspect_bg_analysis,
 			cmd_inspect_bg_analysis_usage, NULL, 0 },
+		{ "balance_check", cmd_inspect_balance_check,
+			cmd_inspect_balance_check_usage, NULL, 0 },
 		{ "dump-tree", cmd_inspect_dump_tree,
 				cmd_inspect_dump_tree_usage, NULL, 0 },
 		{ "dump-super", cmd_inspect_dump_super,
Signature · 1 line
1.7.1
#4 lore raw

Re: [PATCH 3/3] btrfs-progs: Add command to check if balance op is req

Quoted 5 lines · Divya Indi

Add new subcommand to btrfs inspect-internal

btrfs inspect-internal balance_check <path>
Checks whether 'btrfs balance' can help creating more space (Only considers data block groups).

I didn't think it's good to add a new subcommand just for that.

Why not output such relocation sugguestion for you previous bg-analyze subcommand? (It's better to make it a parameter to trigger such output)

Thanks,
Qu

Quoted 174 lines
---
 cmds-inspect.c |  147 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 147 insertions(+), 0 deletions(-)
cmds-inspect.c +147 -0
index 0e2f15a..5baaa49 100644
@@ -267,6 +267,151 @@ static const char * const cmd_inspect_inode_resolve_usage[] = {
 	NULL
 };
 
+static const char * const cmd_inspect_balance_check_usage[] = {
+	"btrfs inspect-internal balance_check <path>",
+	"To check whether 'btrfs balance' can help creating more space",
+	"",
+	"",
+	NULL
+};
+
+static int cmd_inspect_balance_check(int argc, char **argv)
+{
+	struct btrfs_ioctl_search_args args;
+	struct btrfs_ioctl_search_args bg_args;
+	struct btrfs_ioctl_search_key *sk;
+	struct btrfs_ioctl_search_key *bg_sk;
+	struct btrfs_ioctl_search_header *header;
+	struct btrfs_ioctl_search_header *bg_header;
+	struct btrfs_block_group_item *bg;
+	struct btrfs_chunk *chunk;
+	unsigned long off = 0;
+	unsigned long bg_off = 0;
+	DIR *dirstream = NULL;
+	int fd;
+	int i;
+	u64 total_free = 0;
+	u64 min_used = (u64)-1;
+	u64 free_of_min_used = 0;
+	u64 bg_of_min_used = 0;
+	u64 flags;
+	u64 used;
+	int ret = 0;
+	int nr_data_bgs = 0;
+
+	if (check_argc_exact(argc, 2))
+		usage(cmd_inspect_balance_check_usage);
+
+	fd = btrfs_open_dir(argv[optind], &dirstream, 1);
+	if (fd < 0)
+		return 1;
+
+	memset(&args, 0, sizeof(args));
+	sk = &args.key;
+	sk->min_offset = sk->min_transid = 0;
+	sk->max_offset = sk->max_transid = (u64)-1;
+
+	printf("%20s%20s%20s\n", "Start", "Len", "Used");
+	while (1) {
+		ret = get_chunks(fd, &args);
+		if (ret < 0)
+			goto out;
+
+		/*
+		 * it should not happen.
+		 */
+		if (sk->nr_items == 0)
+			break;
+
+		off = 0;
+		memset(&bg_args, 0, sizeof(bg_args));
+		bg_sk = &bg_args.key;
+
+		/* For every chunk, look up 1 exact match for block group in
+		 * the extent tree. */
+		bg_sk->tree_id = BTRFS_EXTENT_TREE_OBJECTID;
+		bg_sk->min_type = BTRFS_BLOCK_GROUP_ITEM_KEY;
+		bg_sk->max_type = BTRFS_BLOCK_GROUP_ITEM_KEY;
+		bg_sk->min_transid =  0;
+		bg_sk->max_transid = (u64)-1;
+
+		for (i = 0; i < sk->nr_items; i++) {
+			header = (struct btrfs_ioctl_search_header *)(args.buf
+								      + off);
+			off += sizeof(*header);
+			if (header->type == BTRFS_CHUNK_ITEM_KEY) {
+				chunk = (struct btrfs_chunk *)
+					(args.buf + off);
+				ret = get_bg_info(fd, &bg_args, header->offset,
+						  chunk->length);
+				if (ret < 0)
+					goto out;
+
+				/*
+				 * it should not happen.
+				 */
+				if (bg_sk->nr_items == 0)
+					continue;
+
+				bg_off = 0;
+				bg_header = (struct btrfs_ioctl_search_header *)
+					    (bg_args.buf + bg_off);
+				bg_off += sizeof(*bg_header);
+				bg = (struct btrfs_block_group_item *)
+				     (bg_args.buf + bg_off);
+
+				flags = btrfs_block_group_flags(bg);
+				if (flags & BTRFS_BLOCK_GROUP_DATA) {
+					used = btrfs_block_group_used(bg);
+					nr_data_bgs++;
+					printf("%20llu%20s%20s\n",
+						bg_header->objectid,
+						pretty_size(bg_header->offset),
+						pretty_size(used));
+					total_free += bg_header->offset - used;
+					if (min_used >= used) {
+						min_used = used;
+						free_of_min_used =
+							bg_header->offset - used;
+						bg_of_min_used =
+							bg_header->objectid;
+					}
+				}
+			}
+
+			off += header->len;
+			sk->min_offset = header->offset + header->len;
+		}
+		sk->nr_items = 4096;
+
+	}
+
+	if (nr_data_bgs <= 1) {
+		printf("Data block groups in fs = %d, no need to do balance.\n",
+				nr_data_bgs);
+		ret = 1;
+		goto out;
+	}
+
+	printf("Total data bgs: %d\nTotal free space: %s\n"
+	       "For min used bg %llu used = %s free = %s\n",
+		nr_data_bgs, pretty_size(total_free), bg_of_min_used,
+		pretty_size(min_used), pretty_size(free_of_min_used));
+
+	if (total_free - free_of_min_used > min_used) {
+		printf("run 'btrfs balance start -dvrange=%llu..%llu <mountpoint>'\n",
+				bg_of_min_used, bg_of_min_used + 1);
+		ret = 0;
+	} else {
+		printf("Please don't balance data block groups, no free space\n");
+		ret = 1;
+	}
+
+out:
+	close_file_or_dir(fd, dirstream);
+	return ret;
+}
+
 static int cmd_inspect_inode_resolve(int argc, char **argv)
 {
 	int fd;
@@ -816,6 +961,8 @@ const struct cmd_group inspect_cmd_group = {
 			cmd_inspect_min_dev_size_usage, NULL, 0 },
 		{ "bg_analysis", cmd_inspect_bg_analysis,
 			cmd_inspect_bg_analysis_usage, NULL, 0 },
+		{ "balance_check", cmd_inspect_balance_check,
+			cmd_inspect_balance_check_usage, NULL, 0 },
 		{ "dump-tree", cmd_inspect_dump_tree,
 				cmd_inspect_dump_tree_usage, NULL, 0 },
 		{ "dump-super", cmd_inspect_dump_super,
author
#5 lore raw

Re: [PATCH 3/3] btrfs-progs: Add command to check if balance op is req

Quoted 9 lines · Qu Wenruo
Quoted 5 lines · Divya Indi

Add new subcommand to btrfs inspect-internal

btrfs inspect-internal balance_check <path>
Checks whether 'btrfs balance' can help creating more space (Only considers data block groups).

Quoted 7 lines

I didn't think it's good to add a new subcommand just for that.

Why not output such relocation sugguestion for you previous bg-analyze subcommand? (It's better to make it a parameter to trigger such output)

Thanks,
Qu

Or maybe as an option to btrfs balance start?
Eg: btrfs balance start --check-only <path>

Quoted 181 lines
Quoted 179 lines
---
 cmds-inspect.c |  147 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 147 insertions(+), 0 deletions(-)
cmds-inspect.c +137 -0
index 0e2f15a..5baaa49 100644
@@ -267,6 +267,151 @@ static const char * const 
 cmd_inspect_inode_resolve_usage[] = {
     NULL
 };
 
+static const char * const cmd_inspect_balance_check_usage[] = {
+    "btrfs inspect-internal balance_check <path>",
+    "To check whether 'btrfs balance' can help creating more space",
+    "",
+    "",
+    NULL
+};
+
+static int cmd_inspect_balance_check(int argc, char **argv)
+{
+    struct btrfs_ioctl_search_args args;
+    struct btrfs_ioctl_search_args bg_args;
+    struct btrfs_ioctl_search_key *sk;
+    struct btrfs_ioctl_search_key *bg_sk;
+    struct btrfs_ioctl_search_header *header;
+    struct btrfs_ioctl_search_header *bg_header;
+    struct btrfs_block_group_item *bg;
+    struct btrfs_chunk *chunk;
+    unsigned long off = 0;
+    unsigned long bg_off = 0;
+    DIR *dirstream = NULL;
+    int fd;
+    int i;
+    u64 total_free = 0;
+    u64 min_used = (u64)-1;
+    u64 free_of_min_used = 0;
+    u64 bg_of_min_used = 0;
+    u64 flags;
+    u64 used;
+    int ret = 0;
+    int nr_data_bgs = 0;
+
+    if (check_argc_exact(argc, 2))
+        usage(cmd_inspect_balance_check_usage);
+
+    fd = btrfs_open_dir(argv[optind], &dirstream, 1);
+    if (fd < 0)
+        return 1;
+
+    memset(&args, 0, sizeof(args));
+    sk = &args.key;
+    sk->min_offset = sk->min_transid = 0;
+    sk->max_offset = sk->max_transid = (u64)-1;
+
+    printf("%20s%20s%20s\n", "Start", "Len", "Used");
+    while (1) {
+        ret = get_chunks(fd, &args);
+        if (ret < 0)
+            goto out;
+
+        /*
+         * it should not happen.
+         */
+        if (sk->nr_items == 0)
+            break;
+
+        off = 0;
+        memset(&bg_args, 0, sizeof(bg_args));
+        bg_sk = &bg_args.key;
+
+        /* For every chunk, look up 1 exact match for block group in
+         * the extent tree. */
+        bg_sk->tree_id = BTRFS_EXTENT_TREE_OBJECTID;
+        bg_sk->min_type = BTRFS_BLOCK_GROUP_ITEM_KEY;
+        bg_sk->max_type = BTRFS_BLOCK_GROUP_ITEM_KEY;
+        bg_sk->min_transid =  0;
+        bg_sk->max_transid = (u64)-1;
+
+        for (i = 0; i < sk->nr_items; i++) {
+            header = (struct btrfs_ioctl_search_header *)(args.buf
+                                      + off);
+            off += sizeof(*header);
+            if (header->type == BTRFS_CHUNK_ITEM_KEY) {
+                chunk = (struct btrfs_chunk *)
+                    (args.buf + off);
+                ret = get_bg_info(fd, &bg_args, header->offset,
+                          chunk->length);
+                if (ret < 0)
+                    goto out;
+
+                /*
+                 * it should not happen.
+                 */
+                if (bg_sk->nr_items == 0)
+                    continue;
+
+                bg_off = 0;
+                bg_header = (struct btrfs_ioctl_search_header *)
+                        (bg_args.buf + bg_off);
+                bg_off += sizeof(*bg_header);
+                bg = (struct btrfs_block_group_item *)
+                     (bg_args.buf + bg_off);
+
+                flags = btrfs_block_group_flags(bg);
+                if (flags & BTRFS_BLOCK_GROUP_DATA) {
+                    used = btrfs_block_group_used(bg);
+                    nr_data_bgs++;
+                    printf("%20llu%20s%20s\n",
+                        bg_header->objectid,
+                        pretty_size(bg_header->offset),
+                        pretty_size(used));
+                    total_free += bg_header->offset - used;
+                    if (min_used >= used) {
+                        min_used = used;
+                        free_of_min_used =
+                            bg_header->offset - used;
+                        bg_of_min_used =
+                            bg_header->objectid;
+                    }
+                }
+            }
+
+            off += header->len;
+            sk->min_offset = header->offset + header->len;
+        }
+        sk->nr_items = 4096;
+
+    }
+
+    if (nr_data_bgs <= 1) {
+        printf("Data block groups in fs = %d, no need to do 
 balance.\n",
+                nr_data_bgs);
+        ret = 1;
+        goto out;
+    }
+
+    printf("Total data bgs: %d\nTotal free space: %s\n"
+           "For min used bg %llu used = %s free = %s\n",
+        nr_data_bgs, pretty_size(total_free), bg_of_min_used,
+        pretty_size(min_used), pretty_size(free_of_min_used));
+
+    if (total_free - free_of_min_used > min_used) {
+        printf("run 'btrfs balance start -dvrange=%llu..%llu 
 <mountpoint>'\n",
+                bg_of_min_used, bg_of_min_used + 1);
+        ret = 0;
+    } else {
+        printf("Please don't balance data block groups, no free 

space\n");

cmds-inspect.c +10 -0
+        ret = 1;
+    }
+
+out:
+    close_file_or_dir(fd, dirstream);
+    return ret;
+}
+
 static int cmd_inspect_inode_resolve(int argc, char **argv)
 {
     int fd;
@@ -816,6 +961,8 @@ const struct cmd_group inspect_cmd_group = {
             cmd_inspect_min_dev_size_usage, NULL, 0 },
         { "bg_analysis", cmd_inspect_bg_analysis,
             cmd_inspect_bg_analysis_usage, NULL, 0 },
+        { "balance_check", cmd_inspect_balance_check,
+            cmd_inspect_balance_check_usage, NULL, 0 },
         { "dump-tree", cmd_inspect_dump_tree,
                 cmd_inspect_dump_tree_usage, NULL, 0 },
         { "dump-super", cmd_inspect_dump_super,
#6 lore raw

Re: [PATCH 3/3] btrfs-progs: Add command to check if balance op is req

Quoted 8 lines · Divya Indi
Quoted 7 lines · Qu Wenruo
Quoted 5 lines · Divya Indi

Add new subcommand to btrfs inspect-internal

btrfs inspect-internal balance_check <path>
Checks whether 'btrfs balance' can help creating more space (Only considers data block groups).

Quoted 7 lines
Quoted 5 lines

I didn't think it's good to add a new subcommand just for that.

Why not output such relocation sugguestion for you previous bg-analyze subcommand?
(It's better to make it a parameter to trigger such output)

Or maybe as an option to btrfs balance start?
Eg: btrfs balance start --check-only <path>

I tend to agree with this approach. The usecase, with some random sample balance options:

 $ btrfs balance start --analyze -dusage=10 -musage=5 /path

returns if the command would be able to make any progress according to the given filters and then

 $ btrfs balance start -dusage=10 -musage=5 /path

would actually do that. This is inherently racy, as the analysis will happen in userspace and the filesystem block group layout could change in the meantime.

This also implies that the filters are implemented in the userspace, duplicating the kernel code. This is not neccesarily bad, as this would work even on older kernels (with new progs), compared to a new analysis mode of balance implemented in kernel.

#7 lore raw

Re: [PATCH 3/3] btrfs-progs: Add command to check if balance op is req

Quoted 4 lines · David Sterba

I tend to agree with this approach. The usecase, with some random sample balance options:

 $ btrfs balance start --analyze -dusage=10 -musage=5 /path

Wouldn't a "balance analyze" command be better than "balance start --analyze"? I would have guessed the latter started the balance but printed some analysis as well (before or, probably more usefully, afterwards).

There might, of course, be some point in a (future)

$ btrfs balance start --if-needed -dusage=10 -musage=5 /path

command.

#8 lore raw

Re: [PATCH 3/3] btrfs-progs: Add command to check if balance op is req

Quoted 10 lines · Graham Cobb
Quoted 4 lines · David Sterba

I tend to agree with this approach. The usecase, with some random sample balance options:

 $ btrfs balance start --analyze -dusage=10 -musage=5 /path

Wouldn't a "balance analyze" command be better than "balance start --analyze"? I would have guessed the latter started the balance but printed some analysis as well (before or, probably more usefully, afterwards).

Right, separate command seems better.

#9 lore raw

Re: [PATCH 3/3] btrfs-progs: Add command to check if balance op is req

Quoted 11 lines · David Sterba
Quoted 9 lines · Graham Cobb
Quoted 4 lines · David Sterba

I tend to agree with this approach. The usecase, with some random sample balance options:

  $ btrfs balance start --analyze -dusage=10 -musage=5 /path

Wouldn't a "balance analyze" command be better than "balance start --analyze"? I would have guessed the latter started the balance but printed some analysis as well (before or, probably more usefully, afterwards).

Right, separate command seems better.

What about btrfs inspect-internal bg_analysis (new name: show-block-groups)? It can still be a subcommand for inspect-internal? So, wel have 2 new sub commands:
1) btrfs balance analyze
2) btrfs inspect-internal show-block-groups

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw

Re: [PATCH 3/3] btrfs-progs: Add command to check if balance op is req

In reply to David Sterba
Quoted 14 lines · David Sterba
Quoted 13 lines · Divya Indi
Quoted 12 lines · Qu Wenruo
Quoted 5 lines · Divya Indi

Add new subcommand to btrfs inspect-internal

btrfs inspect-internal balance_check <path>
Checks whether 'btrfs balance' can help creating more space (Only considers data block groups).

I didn't think it's good to add a new subcommand just for that.

Why not output such relocation sugguestion for you previous bg-analyze subcommand?
(It's better to make it a parameter to trigger such output)

Quoted 7 lines
Quoted 2 lines

Or maybe as an option to btrfs balance start?
Eg: btrfs balance start --check-only <path>

I tend to agree with this approach. The usecase, with some random sample balance options:

 $ btrfs balance start --analyze -dusage=10 -musage=5 /path

This use case is better than my original idea.
It is really useful then.

+1 for this approach.

Thanks,
Qu

Quoted 14 lines

returns if the command would be able to make any progress according to the given filters and then

 $ btrfs balance start -dusage=10 -musage=5 /path

would actually do that. This is inherently racy, as the analysis will happen in userspace and the filesystem block group layout could change in the meantime.

This also implies that the filters are implemented in the userspace, duplicating the kernel code. This is not neccesarily bad, as this would work even on older kernels (with new progs), compared to a new analysis mode of balance implemented in kernel.

#11 lore raw

Re: [PATCH 2/3] btrfs-progs: Add a command to show bg info

In reply to Divya Indi
Quoted 17 lines · Divya Indi

Add a new subcommand to btrfs inspect-internal

btrfs inspect-internal bg_analysis <path> Gives information about all the block groups.

---
 cmds-inspect.c |  114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 114 insertions(+), 0 deletions(-)
cmds-inspect.c +0 -0
index f435ea9..0e2f15a 100644

cmds-inspect.c static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args, · line 153

Quoted 7 lines
cmds-inspect.c +4 -0
@@ -147,6 +147,118 @@ static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args,
 	}
 	return ret;
 }
+
+static const char * const cmd_inspect_bg_analysis_usage[] = {
+	"btrfs inspect-internal bg_analysis <path> ",
+	"Get information about all block groups",

What about get bg info for given range, and not all?

I think it's just several new parameters, as your design is capable to handle that.

Quoted 87 lines
cmds-inspect.c +87 -0
+	"",
+	"",
+	NULL
+};
+
+static int cmd_inspect_bg_analysis(int argc, char **argv)
+{
+	struct btrfs_ioctl_search_args args;
+	struct btrfs_ioctl_search_args bg_args;
+	struct btrfs_ioctl_search_header *header;
+	struct btrfs_ioctl_search_header *bg_header;
+	struct btrfs_ioctl_search_key *sk;
+	struct btrfs_ioctl_search_key *bg_sk;
+	struct btrfs_block_group_item *bg;
+	struct btrfs_chunk *chunk;
+	unsigned long off = 0;
+	unsigned long bg_off = 0;
+	DIR *dirstream = NULL;
+	int fd;
+	int i;
+	int ret = 0;
+	u64 used;
+	u64 flags;
+	char bg_type[32] = {0};
+
+	if (check_argc_exact(argc, 2))
+		usage(cmd_inspect_bg_analysis_usage);
+
+	fd = btrfs_open_dir(argv[optind], &dirstream, 1);
+	if (fd < 0)
+		return 1;
+
+	memset(&args, 0, sizeof(args));
+	sk = &args.key;
+	sk->min_offset = sk->min_transid = 0;
+	sk->max_offset = sk->max_transid = (u64)-1;
+	printf("%20s%20s%20s%20s\n", "Type", "Start", "Len", "Used");
+	while (1) {
+
+		/* Walk through the chunk tree and retrieve all the chunks */
+		ret = get_chunks(fd, &args);
+		if (ret < 0)
+			goto out;
+
+		/*
+		 * it should not happen.
+		 */
+		if (sk->nr_items == 0)
+			break;
+
+		off = 0;
+		memset(&bg_args, 0, sizeof(bg_args));
+		bg_sk = &bg_args.key;
+
+		bg_sk->tree_id = BTRFS_EXTENT_TREE_OBJECTID;
+		bg_sk->min_type = BTRFS_BLOCK_GROUP_ITEM_KEY;
+		bg_sk->max_type = BTRFS_BLOCK_GROUP_ITEM_KEY;
+		bg_sk->min_transid =  0;
+		bg_sk->max_transid = (u64)-1;
+
+		for (i = 0; i < sk->nr_items; i++) {
+			header = (struct btrfs_ioctl_search_header *)(args.buf
+								      + off);
+			off += sizeof(*header);
+			if (header->type == BTRFS_CHUNK_ITEM_KEY) {
+				chunk = (struct btrfs_chunk *)
+					(args.buf + off);
+
+				/* For every chunk lookup an exact match(bg) in
+				 * the extent tree and read its used values */
+				ret = get_bg_info(fd, &bg_args, header->offset,
+						  chunk->length);
+				if (ret < 0)
+					goto out;
+
+				/*
+				 * it should not happen.
+				 */
+				if (bg_sk->nr_items == 0)
+					continue;
+
+				bg_off = 0;
+				bg_header = (struct btrfs_ioctl_search_header *)
+					    (bg_args.buf + bg_off);
+				bg_off += sizeof(*bg_header);
+				bg = (struct btrfs_block_group_item *)
+				     (bg_args.buf + bg_off);

cmds-inspect.c static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args, · line 250

Quoted 10 lines
cmds-inspect.c +10 -0
+
+				used = btrfs_block_group_used(bg);
+				memset(&bg_type, 0, 32);
+				flags = btrfs_block_group_flags(bg);
+				bg_flags_to_str(flags, bg_type);
+				printf("%20s%20llu%20s%20s\n",
+					bg_type,
+					bg_header->objectid,
+					pretty_size(bg_header->offset),
+					pretty_size(used));

BTW, it would be quite handy if we can specify units.

Quoted 15 lines
cmds-inspect.c +11 -0
+			}
+			off += header->len;
+			sk->min_offset = header->offset + header->len;
+		}
+		sk->nr_items = 4096;
+	}
+out:
+	close_file_or_dir(fd, dirstream);
+	return ret;
+}
+
 static const char * const cmd_inspect_inode_resolve_usage[] = {
 	"btrfs inspect-internal inode-resolve [-v] <inode> <path>",
 	"Get file system paths for the given inode",

cmds-inspect.c const struct cmd_group inspect_cmd_group = { · line 818

Quoted 5 lines
cmds-inspect.c +2 -0
@@ -702,6 +814,8 @@ const struct cmd_group inspect_cmd_group = {
 			0 },
 		{ "min-dev-size", cmd_inspect_min_dev_size,
 			cmd_inspect_min_dev_size_usage, NULL, 0 },
+		{ "bg_analysis", cmd_inspect_bg_analysis,
+			cmd_inspect_bg_analysis_usage, NULL, 0 },

Just naming preference, IMHO show-block-groups or dump-block-groups seems better for me.

Other part seems good.
Feel free to add my reviewed by tag.

Thanks,
Qu

Quoted 4 lines
cmds-inspect.c +0 -0
 		{ "dump-tree", cmd_inspect_dump_tree,
 				cmd_inspect_dump_tree_usage, NULL, 0 },
 		{ "dump-super", cmd_inspect_dump_super,
#12 lore raw

Re: [PATCH 2/3] btrfs-progs: Add a command to show bg info

Quoted 4 lines · On Tue, 18 Oct 2016 09:39:32 +0800 Qu Wenruo <[email protected]> wrote:
Quoted 4 lines

static const char * const cmd_inspect_inode_resolve_usage[] = {

cmds-inspect.c +0 -0
 	"btrfs inspect-internal inode-resolve [-v] <inode> <path>",
 	"Get file system paths for the given inode",
@@ -702,6 +814,8 @@ const struct cmd_group inspect_cmd_group = {
Quoted 8 lines

cmds-inspect.c const struct cmd_group inspect_cmd_group = { · line 818

Quoted 5 lines
cmds-inspect.c +2 -0
 			0 },
 		{ "min-dev-size", cmd_inspect_min_dev_size,
 			cmd_inspect_min_dev_size_usage, NULL, 0 },
+		{ "bg_analysis", cmd_inspect_bg_analysis,
+			cmd_inspect_bg_analysis_usage, NULL, 0 },

Just naming preference, IMHO show-block-groups or dump-block-groups seems better for me.

And in any case please don't mix separation by "-" and "_" in the same command string. In btrfs tool the convention is to separate words in subcommand names using "-".

Signature · 2 lines
With respect,
Roman
author
#13 lore raw

Re: [PATCH 2/3] btrfs-progs: Add a command to show bg info

Quoted 14 lines · Roman Mamedov
Quoted 11 lines · On Tue, 18 Oct 2016 09:39:32 +0800 Qu Wenruo <[email protected]> wrote:

cmds-inspect.c const struct cmd_group inspect_cmd_group = { · line 818

Quoted 9 lines

static const char * const cmd_inspect_inode_resolve_usage[] = {
"btrfs inspect-internal inode-resolve [-v] <inode> <path>", "Get file system paths for the given inode",

cmds-inspect.c +2 -0
@@ -702,6 +814,8 @@ const struct cmd_group inspect_cmd_group = {
  			0 },
  		{ "min-dev-size", cmd_inspect_min_dev_size,
  			cmd_inspect_min_dev_size_usage, NULL, 0 },
+		{ "bg_analysis", cmd_inspect_bg_analysis,
+			cmd_inspect_bg_analysis_usage, NULL, 0 },

Just naming preference, IMHO show-block-groups or dump-block-groups seems better for me.

Quoted 4 lines

And in any case please don't mix separation by "-" and "_" in the same command string. In btrfs tool the convention is to separate words in subcommand names using "-".

Noted, thanks! Will update the patch to correct this.

#14 lore raw

Re: [PATCH 2/3] btrfs-progs: Add a command to show bg info

In reply to Divya Indi
Quoted 4 lines · Divya Indi

Add a new subcommand to btrfs inspect-internal

btrfs inspect-internal bg_analysis <path>
Gives information about all the block groups.

The sample output from the cover letter should also go here (or just here).

Below are some comments, but overall, I think the block group dumping should be more advanced than just what this patch does. It's a good start, but given the rich structure of the blockgroups and chunks, I'd rather see the basics done right from the beginning.

The blockgroups and chunks can be viewed in a logical or physical way. I've sent a patch some time agou, that dumps the physical structure, but got reminded that the balance analysis is more interesting on the logical level.

Ideally I'd like to keep both ways to show the information. The physical way is easier, just iterate the device extents and print start, lenght etc. The logical is more tricky as it's a tree structure, when one logical chunk could comprised of several physical chunks. And this must reflect all current raid profiles, where we're mixing mirorring and striping, and chunks of special kind (parity).

Now, I'm not asking you to implement all of that, but I want to make sure that code that touches the area of interest does not block further extensions. I understand that you want to add the ability to optimize the balance.

Quoted 58 lines
---
 cmds-inspect.c |  114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 114 insertions(+), 0 deletions(-)
cmds-inspect.c +43 -0
index f435ea9..0e2f15a 100644
@@ -147,6 +147,118 @@ static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args,
 	}
 	return ret;
 }
+
+static const char * const cmd_inspect_bg_analysis_usage[] = {
+	"btrfs inspect-internal bg_analysis <path> ",
+	"Get information about all block groups",
+	"",
+	"",
+	NULL
+};
+
+static int cmd_inspect_bg_analysis(int argc, char **argv)
+{
+	struct btrfs_ioctl_search_args args;
+	struct btrfs_ioctl_search_args bg_args;
+	struct btrfs_ioctl_search_header *header;
+	struct btrfs_ioctl_search_header *bg_header;
+	struct btrfs_ioctl_search_key *sk;
+	struct btrfs_ioctl_search_key *bg_sk;
+	struct btrfs_block_group_item *bg;
+	struct btrfs_chunk *chunk;
+	unsigned long off = 0;
+	unsigned long bg_off = 0;
+	DIR *dirstream = NULL;
+	int fd;
+	int i;
+	int ret = 0;
+	u64 used;
+	u64 flags;
+	char bg_type[32] = {0};
+
+	if (check_argc_exact(argc, 2))
+		usage(cmd_inspect_bg_analysis_usage);
+
+	fd = btrfs_open_dir(argv[optind], &dirstream, 1);
+	if (fd < 0)
+		return 1;
+
+	memset(&args, 0, sizeof(args));
+	sk = &args.key;
+	sk->min_offset = sk->min_transid = 0;
+	sk->max_offset = sk->max_transid = (u64)-1;
+	printf("%20s%20s%20s%20s\n", "Type", "Start", "Len", "Used");
+	while (1) {
+

cmds-inspect.c static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args, · line 202

Quoted 10 lines
cmds-inspect.c +10 -0
+		/* Walk through the chunk tree and retrieve all the chunks */
+		ret = get_chunks(fd, &args);
+		if (ret < 0)
+			goto out;
+
+		/*
+		 * it should not happen.
+		 */
+		if (sk->nr_items == 0)
+			break;

So is this an error condition? If yes, then it should be handled.

Quoted 11 lines
cmds-inspect.c +11 -0
+
+		off = 0;
+		memset(&bg_args, 0, sizeof(bg_args));
+		bg_sk = &bg_args.key;
+
+		bg_sk->tree_id = BTRFS_EXTENT_TREE_OBJECTID;
+		bg_sk->min_type = BTRFS_BLOCK_GROUP_ITEM_KEY;
+		bg_sk->max_type = BTRFS_BLOCK_GROUP_ITEM_KEY;
+		bg_sk->min_transid =  0;
+		bg_sk->max_transid = (u64)-1;
+

cmds-inspect.c static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args, · line 223

Quoted 10 lines
cmds-inspect.c +10 -0
+		for (i = 0; i < sk->nr_items; i++) {
+			header = (struct btrfs_ioctl_search_header *)(args.buf
+								      + off);
+			off += sizeof(*header);
+			if (header->type == BTRFS_CHUNK_ITEM_KEY) {
+				chunk = (struct btrfs_chunk *)
+					(args.buf + off);
+
+				/* For every chunk lookup an exact match(bg) in
+				 * the extent tree and read its used values */

Comment formatting

Quoted 10 lines
cmds-inspect.c +10 -0
+				ret = get_bg_info(fd, &bg_args, header->offset,
+						  chunk->length);
+				if (ret < 0)
+					goto out;
+
+				/*
+				 * it should not happen.
+				 */
+				if (bg_sk->nr_items == 0)
+					continue;

cmds-inspect.c static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args, · line 243

Quoted 10 lines
cmds-inspect.c +10 -0
+
+				bg_off = 0;
+				bg_header = (struct btrfs_ioctl_search_header *)
+					    (bg_args.buf + bg_off);
+				bg_off += sizeof(*bg_header);
+				bg = (struct btrfs_block_group_item *)
+				     (bg_args.buf + bg_off);
+
+				used = btrfs_block_group_used(bg);
+				memset(&bg_type, 0, 32);

This should be sizeof(bg_type), no?

cmds-inspect.c static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args, · line 255

Quoted 12 lines
cmds-inspect.c +12 -0
+				flags = btrfs_block_group_flags(bg);
+				bg_flags_to_str(flags, bg_type);
+				printf("%20s%20llu%20s%20s\n",
+					bg_type,
+					bg_header->objectid,
+					pretty_size(bg_header->offset),
+					pretty_size(used));
+			}
+			off += header->len;
+			sk->min_offset = header->offset + header->len;
+		}
+		sk->nr_items = 4096;

Better put that right before the search ioctls.

Quoted 25 lines
cmds-inspect.c +8 -0
+	}
+out:
+	close_file_or_dir(fd, dirstream);
+	return ret;
+}
+
 static const char * const cmd_inspect_inode_resolve_usage[] = {
 	"btrfs inspect-internal inode-resolve [-v] <inode> <path>",
 	"Get file system paths for the given inode",
@@ -702,6 +814,8 @@ const struct cmd_group inspect_cmd_group = {
 			0 },
 		{ "min-dev-size", cmd_inspect_min_dev_size,
 			cmd_inspect_min_dev_size_usage, NULL, 0 },
+		{ "bg_analysis", cmd_inspect_bg_analysis,
+			cmd_inspect_bg_analysis_usage, NULL, 0 },
 		{ "dump-tree", cmd_inspect_dump_tree,
 				cmd_inspect_dump_tree_usage, NULL, 0 },
 		{ "dump-super", cmd_inspect_dump_super,
Signature · 6 lines
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#15 lore raw

Re: [PATCH 2/3] btrfs-progs: Add a command to show bg info

Quoted 18 lines · David Sterba
Quoted 4 lines · Divya Indi

Add a new subcommand to btrfs inspect-internal

btrfs inspect-internal bg_analysis <path>
Gives information about all the block groups.

The sample output from the cover letter should also go here (or just here).

Below are some comments, but overall, I think the block group dumping should be more advanced than just what this patch does. It's a good start, but given the rich structure of the blockgroups and chunks, I'd rather see the basics done right from the beginning.

The blockgroups and chunks can be viewed in a logical or physical way. I've sent a patch some time agou, that dumps the physical structure, but got reminded that the balance analysis is more interesting on the logical level.

Quoted 6 lines

Ideally I'd like to keep both ways to show the information. The physical way is easier, just iterate the device extents and print start, lenght etc. The logical is more tricky as it's a tree structure, when one logical chunk could comprised of several physical chunks. And this must reflect all current raid profiles, where we're mixing mirorring and striping, and chunks of special kind (parity).

Since most of this information is available through the chunk, we can try to implement these as part of v2.

Quoted 164 lines

Now, I'm not asking you to implement all of that, but I want to make sure that code that touches the area of interest does not block further extensions. I understand that you want to add the ability to optimize the balance.

Quoted 58 lines
---
  cmds-inspect.c |  114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 files changed, 114 insertions(+), 0 deletions(-)
cmds-inspect.c +43 -0
index f435ea9..0e2f15a 100644
@@ -147,6 +147,118 @@ static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args,
  	}
  	return ret;
  }
+
+static const char * const cmd_inspect_bg_analysis_usage[] = {
+	"btrfs inspect-internal bg_analysis <path> ",
+	"Get information about all block groups",
+	"",
+	"",
+	NULL
+};
+
+static int cmd_inspect_bg_analysis(int argc, char **argv)
+{
+	struct btrfs_ioctl_search_args args;
+	struct btrfs_ioctl_search_args bg_args;
+	struct btrfs_ioctl_search_header *header;
+	struct btrfs_ioctl_search_header *bg_header;
+	struct btrfs_ioctl_search_key *sk;
+	struct btrfs_ioctl_search_key *bg_sk;
+	struct btrfs_block_group_item *bg;
+	struct btrfs_chunk *chunk;
+	unsigned long off = 0;
+	unsigned long bg_off = 0;
+	DIR *dirstream = NULL;
+	int fd;
+	int i;
+	int ret = 0;
+	u64 used;
+	u64 flags;
+	char bg_type[32] = {0};
+
+	if (check_argc_exact(argc, 2))
+		usage(cmd_inspect_bg_analysis_usage);
+
+	fd = btrfs_open_dir(argv[optind], &dirstream, 1);
+	if (fd < 0)
+		return 1;
+
+	memset(&args, 0, sizeof(args));
+	sk = &args.key;
+	sk->min_offset = sk->min_transid = 0;
+	sk->max_offset = sk->max_transid = (u64)-1;
+	printf("%20s%20s%20s%20s\n", "Type", "Start", "Len", "Used");
+	while (1) {
+

cmds-inspect.c static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args, · line 202

Quoted 10 lines
cmds-inspect.c +10 -0
+		/* Walk through the chunk tree and retrieve all the chunks */
+		ret = get_chunks(fd, &args);
+		if (ret < 0)
+			goto out;
+
+		/*
+		 * it should not happen.
+		 */
+		if (sk->nr_items == 0)
+			break;

So is this an error condition? If yes, then it should be handled.

Quoted 11 lines
cmds-inspect.c +11 -0
+
+		off = 0;
+		memset(&bg_args, 0, sizeof(bg_args));
+		bg_sk = &bg_args.key;
+
+		bg_sk->tree_id = BTRFS_EXTENT_TREE_OBJECTID;
+		bg_sk->min_type = BTRFS_BLOCK_GROUP_ITEM_KEY;
+		bg_sk->max_type = BTRFS_BLOCK_GROUP_ITEM_KEY;
+		bg_sk->min_transid =  0;
+		bg_sk->max_transid = (u64)-1;
+

cmds-inspect.c static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args, · line 223

Quoted 10 lines
cmds-inspect.c +10 -0
+		for (i = 0; i < sk->nr_items; i++) {
+			header = (struct btrfs_ioctl_search_header *)(args.buf
+								      + off);
+			off += sizeof(*header);
+			if (header->type == BTRFS_CHUNK_ITEM_KEY) {
+				chunk = (struct btrfs_chunk *)
+					(args.buf + off);
+
+				/* For every chunk lookup an exact match(bg) in
+				 * the extent tree and read its used values */

Comment formatting

Quoted 10 lines
cmds-inspect.c +10 -0
+				ret = get_bg_info(fd, &bg_args, header->offset,
+						  chunk->length);
+				if (ret < 0)
+					goto out;
+
+				/*
+				 * it should not happen.
+				 */
+				if (bg_sk->nr_items == 0)
+					continue;

cmds-inspect.c static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args, · line 243

Quoted 10 lines
cmds-inspect.c +10 -0
+
+				bg_off = 0;
+				bg_header = (struct btrfs_ioctl_search_header *)
+					    (bg_args.buf + bg_off);
+				bg_off += sizeof(*bg_header);
+				bg = (struct btrfs_block_group_item *)
+				     (bg_args.buf + bg_off);
+
+				used = btrfs_block_group_used(bg);
+				memset(&bg_type, 0, 32);

This should be sizeof(bg_type), no?

cmds-inspect.c static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args, · line 255

Quoted 12 lines
cmds-inspect.c +12 -0
+				flags = btrfs_block_group_flags(bg);
+				bg_flags_to_str(flags, bg_type);
+				printf("%20s%20llu%20s%20s\n",
+					bg_type,
+					bg_header->objectid,
+					pretty_size(bg_header->offset),
+					pretty_size(used));
+			}
+			off += header->len;
+			sk->min_offset = header->offset + header->len;
+		}
+		sk->nr_items = 4096;

Better put that right before the search ioctls.

Quoted 25 lines
cmds-inspect.c +8 -0
+	}
+out:
+	close_file_or_dir(fd, dirstream);
+	return ret;
+}
+
  static const char * const cmd_inspect_inode_resolve_usage[] = {
  	"btrfs inspect-internal inode-resolve [-v] <inode> <path>",
  	"Get file system paths for the given inode",
@@ -702,6 +814,8 @@ const struct cmd_group inspect_cmd_group = {
  			0 },
  		{ "min-dev-size", cmd_inspect_min_dev_size,
  			cmd_inspect_min_dev_size_usage, NULL, 0 },
+		{ "bg_analysis", cmd_inspect_bg_analysis,
+			cmd_inspect_bg_analysis_usage, NULL, 0 },
  		{ "dump-tree", cmd_inspect_dump_tree,
  				cmd_inspect_dump_tree_usage, NULL, 0 },
  		{ "dump-super", cmd_inspect_dump_super,
Signature · 6 lines
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#16 lore raw

Re: [PATCH 1/3] btrfs-progs: Generic functions to retrieve chunks and their bg info

In reply to Divya Indi
Quoted 29 lines · Divya Indi

An efficient alternative to retrieving block groups:
get_chunks(): Walk the chunk tree to retrieve the chunks. get_bg_info(): For each retrieved chunk, lookup an exact match of block group in the extent tree.

---
 cmds-inspect.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 66 insertions(+), 0 deletions(-)
cmds-inspect.c +9 -0
index 4b7cea0..f435ea9 100644
@@ -81,6 +81,72 @@ out:
 	return !!ret;
 }
 
+static void bg_flags_to_str(u64 flags, char *ret)
+{
+	int empty = 1;
+
+	if (flags & BTRFS_BLOCK_GROUP_DATA) {
+		empty = 0;
+		strcpy(ret, "DATA");
+	}
+	if (flags & BTRFS_BLOCK_GROUP_METADATA) {

cmds-inspect.c out: · line 102

Quoted 10 lines
cmds-inspect.c +10 -0
+		if (!empty)
+			strcat(ret, "|");
+		strcat(ret, "METADATA");
+	}
+	if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
+		if (!empty)
+			strcat(ret, "|");
+		strcat(ret, "SYSTEM");
+	}
+}

Check print-tree.c, it has the same function.
Just export it.

And it's stronger than your version, which can also output profiles.

Quoted 11 lines
cmds-inspect.c +11 -0
+
+/* Walking through the chunk tree to retrieve chunks. */
+
+static int get_chunks(int fd, struct btrfs_ioctl_search_args *chunk_args)
+{
+	struct btrfs_ioctl_search_key *sk;
+	int ret;
+	int e;
+
+	sk = &chunk_args->key;
+

cmds-inspect.c out: · line 123

Quoted 10 lines
cmds-inspect.c +10 -0
+	sk->tree_id = BTRFS_CHUNK_TREE_OBJECTID;
+	sk->min_objectid = sk->max_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
+	sk->max_type = sk->min_type = BTRFS_CHUNK_ITEM_KEY;
+	sk->nr_items = 4096;
+
+	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, chunk_args);
+	e = errno;
+	if (ret < 0) {
+		fprintf(stderr, "ret %d error '%s'\n", ret,
+				strerror(e));

Use error() function.

Quoted 13 lines
cmds-inspect.c +13 -0
+	}
+	return ret;
+}
+
+/* Given the objectid, find the block group item in the extent tree */
+static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args,
+		       u64 objectid, unsigned long length)
+{
+	struct btrfs_ioctl_search_key *bg_sk;
+	int ret;
+	int e;
+
+	bg_sk = &bg_args->key;

cmds-inspect.c out: · line 146

Quoted 10 lines
cmds-inspect.c +10 -0
+
+	bg_sk->min_objectid = bg_sk->max_objectid = objectid;
+	bg_sk->nr_items = 1;
+	bg_sk->min_offset = bg_sk->max_offset = length;
+
+	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, bg_args);
+	e = errno;
+	if (ret < 0) {
+		fprintf(stderr, "ret %d error '%s'\n", ret,
+				strerror(e));

Same problem here.

Quoted 7 lines
cmds-inspect.c +3 -0
+	}
+	return ret;
+}
 static const char * const cmd_inspect_inode_resolve_usage[] = {
 	"btrfs inspect-internal inode-resolve [-v] <inode> <path>",
 	"Get file system paths for the given inode",
#17 lore raw

Re: [PATCH 1/3] btrfs-progs: Generic functions to retrieve chunks and their bg info

In reply to Divya Indi
Quoted 31 lines · Divya Indi

An efficient alternative to retrieving block groups:
get_chunks(): Walk the chunk tree to retrieve the chunks. get_bg_info(): For each retrieved chunk, lookup an exact match of block group in the extent tree.

---
 cmds-inspect.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 66 insertions(+), 0 deletions(-)
cmds-inspect.c +11 -0
index 4b7cea0..f435ea9 100644
@@ -81,6 +81,72 @@ out:
 	return !!ret;
 }
 
+static void bg_flags_to_str(u64 flags, char *ret)
+{
+	int empty = 1;
+
+	if (flags & BTRFS_BLOCK_GROUP_DATA) {
+		empty = 0;
+		strcpy(ret, "DATA");
+	}
+	if (flags & BTRFS_BLOCK_GROUP_METADATA) {
+		if (!empty)
+			strcat(ret, "|");

cmds-inspect.c out: · line 104

Quoted 10 lines
cmds-inspect.c +10 -0
+		strcat(ret, "METADATA");
+	}
+	if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
+		if (!empty)
+			strcat(ret, "|");
+		strcat(ret, "SYSTEM");
+	}
+}
+
+/* Walking through the chunk tree to retrieve chunks. */

No empty newline.

cmds-inspect.c out: · line 116

Quoted 12 lines
cmds-inspect.c +12 -0
+
+static int get_chunks(int fd, struct btrfs_ioctl_search_args *chunk_args)
+{
+	struct btrfs_ioctl_search_key *sk;
+	int ret;
+	int e;
+
+	sk = &chunk_args->key;
+
+	sk->tree_id = BTRFS_CHUNK_TREE_OBJECTID;
+	sk->min_objectid = sk->max_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
+	sk->max_type = sk->min_type = BTRFS_CHUNK_ITEM_KEY;

Please don't do multiple asignments in one statement.

cmds-inspect.c out: · line 120

Quoted 4 lines
cmds-inspect.c +4 -0
+	sk->nr_items = 4096;
+
+	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, chunk_args);
+	e = errno;

This is useless asignment, I've removed it from the code, please don't reintrduce it.

Quoted 12 lines
cmds-inspect.c +10 -0
+	if (ret < 0) {
+		fprintf(stderr, "ret %d error '%s'\n", ret,
+				strerror(e));
+	}
+	return ret;
+}
 
 
+
+/* Given the objectid, find the block group item in the extent tree */
+static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args,
+		       u64 objectid, unsigned long length)

cmds-inspect.c out: · line 140

Quoted 10 lines
cmds-inspect.c +10 -0
+{
+	struct btrfs_ioctl_search_key *bg_sk;
+	int ret;
+	int e;
+
+	bg_sk = &bg_args->key;
+
+	bg_sk->min_objectid = bg_sk->max_objectid = objectid;
+	bg_sk->nr_items = 1;
+	bg_sk->min_offset = bg_sk->max_offset = length;

Same here.

cmds-inspect.c out: · line 146

Quoted 6 lines
cmds-inspect.c +6 -0
+
+	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, bg_args);
+	e = errno;
+	if (ret < 0) {
+		fprintf(stderr, "ret %d error '%s'\n", ret,
+				strerror(e));

Please take a look how the error messages are constructed when the tree search ioctl fails, there are enough examples in the code.

cmds-inspect.c out: · line 152

Quoted 6 lines
cmds-inspect.c +3 -0
+	}
+	return ret;
+}
 static const char * const cmd_inspect_inode_resolve_usage[] = {
cmds-inspect.c +0 -0
 	"btrfs inspect-internal inode-resolve [-v] <inode> <path>",
 	"Get file system paths for the given inode",

Actually, I'm not sure if such functions should exist at all, as they only hide the search ioctl but don't do any validation of the returned keys and data.

#18 lore raw

Re: [PATCH 1/3] btrfs-progs: Generic functions to retrieve chunks and their bg info

Quoted 97 lines · David Sterba
Quoted 31 lines · Divya Indi

An efficient alternative to retrieving block groups:
get_chunks(): Walk the chunk tree to retrieve the chunks. get_bg_info(): For each retrieved chunk, lookup an exact match of block group in the extent tree.

---
  cmds-inspect.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 files changed, 66 insertions(+), 0 deletions(-)
cmds-inspect.c +11 -0
index 4b7cea0..f435ea9 100644
@@ -81,6 +81,72 @@ out:
  	return !!ret;
  }
 
+static void bg_flags_to_str(u64 flags, char *ret)
+{
+	int empty = 1;
+
+	if (flags & BTRFS_BLOCK_GROUP_DATA) {
+		empty = 0;
+		strcpy(ret, "DATA");
+	}
+	if (flags & BTRFS_BLOCK_GROUP_METADATA) {
+		if (!empty)
+			strcat(ret, "|");

cmds-inspect.c out: · line 104

Quoted 10 lines
cmds-inspect.c +10 -0
+		strcat(ret, "METADATA");
+	}
+	if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
+		if (!empty)
+			strcat(ret, "|");
+		strcat(ret, "SYSTEM");
+	}
+}
+
+/* Walking through the chunk tree to retrieve chunks. */

No empty newline.

cmds-inspect.c out: · line 116

Quoted 12 lines
cmds-inspect.c +12 -0
+
+static int get_chunks(int fd, struct btrfs_ioctl_search_args *chunk_args)
+{
+	struct btrfs_ioctl_search_key *sk;
+	int ret;
+	int e;
+
+	sk = &chunk_args->key;
+
+	sk->tree_id = BTRFS_CHUNK_TREE_OBJECTID;
+	sk->min_objectid = sk->max_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
+	sk->max_type = sk->min_type = BTRFS_CHUNK_ITEM_KEY;

Please don't do multiple asignments in one statement.

cmds-inspect.c out: · line 120

Quoted 4 lines
cmds-inspect.c +4 -0
+	sk->nr_items = 4096;
+
+	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, chunk_args);
+	e = errno;

This is useless asignment, I've removed it from the code, please don't reintrduce it.

Quoted 11 lines
cmds-inspect.c +10 -0
+	if (ret < 0) {
+		fprintf(stderr, "ret %d error '%s'\n", ret,
+				strerror(e));
+	}
+	return ret;
+}
 
+
+/* Given the objectid, find the block group item in the extent tree */
+static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args,
+		       u64 objectid, unsigned long length)

cmds-inspect.c out: · line 140

Quoted 10 lines
cmds-inspect.c +10 -0
+{
+	struct btrfs_ioctl_search_key *bg_sk;
+	int ret;
+	int e;
+
+	bg_sk = &bg_args->key;
+
+	bg_sk->min_objectid = bg_sk->max_objectid = objectid;
+	bg_sk->nr_items = 1;
+	bg_sk->min_offset = bg_sk->max_offset = length;

Same here.

cmds-inspect.c out: · line 146

Quoted 6 lines
cmds-inspect.c +6 -0
+
+	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, bg_args);
+	e = errno;
+	if (ret < 0) {
+		fprintf(stderr, "ret %d error '%s'\n", ret,
+				strerror(e));

Please take a look how the error messages are constructed when the tree search ioctl fails, there are enough examples in the code.

Quoted 9 lines
Quoted 6 lines
cmds-inspect.c +3 -0
+	}
+	return ret;
+}
  static const char * const cmd_inspect_inode_resolve_usage[] = {
  	"btrfs inspect-internal inode-resolve [-v] <inode> <path>",

"Get file system paths for the given inode",

Actually, I'm not sure if such functions should exist at all, as they only hide the search ioctl but don't do any validation of the returned keys and data.

The intent was to avoid the same assignments and calls in both the sub commands, but I see your point.
Noted- for v2.

#19 lore raw

Re: [PATCH 1/3] btrfs-progs: Generic functions to retrieve chunks and their bg info

In reply to David Sterba

Hi,

any news about these commands ?

BR
G.Baroncelli

Quoted 118 lines · David Sterba
Quoted 31 lines · Divya Indi

An efficient alternative to retrieving block groups:
get_chunks(): Walk the chunk tree to retrieve the chunks. get_bg_info(): For each retrieved chunk, lookup an exact match of block group in the extent tree.

---
 cmds-inspect.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 66 insertions(+), 0 deletions(-)
cmds-inspect.c +11 -0
index 4b7cea0..f435ea9 100644
@@ -81,6 +81,72 @@ out:
 	return !!ret;
 }
 
+static void bg_flags_to_str(u64 flags, char *ret)
+{
+	int empty = 1;
+
+	if (flags & BTRFS_BLOCK_GROUP_DATA) {
+		empty = 0;
+		strcpy(ret, "DATA");
+	}
+	if (flags & BTRFS_BLOCK_GROUP_METADATA) {
+		if (!empty)
+			strcat(ret, "|");

cmds-inspect.c out: · line 104

Quoted 10 lines
cmds-inspect.c +10 -0
+		strcat(ret, "METADATA");
+	}
+	if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
+		if (!empty)
+			strcat(ret, "|");
+		strcat(ret, "SYSTEM");
+	}
+}
+
+/* Walking through the chunk tree to retrieve chunks. */

No empty newline.

cmds-inspect.c out: · line 116

Quoted 12 lines
cmds-inspect.c +12 -0
+
+static int get_chunks(int fd, struct btrfs_ioctl_search_args *chunk_args)
+{
+	struct btrfs_ioctl_search_key *sk;
+	int ret;
+	int e;
+
+	sk = &chunk_args->key;
+
+	sk->tree_id = BTRFS_CHUNK_TREE_OBJECTID;
+	sk->min_objectid = sk->max_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
+	sk->max_type = sk->min_type = BTRFS_CHUNK_ITEM_KEY;

Please don't do multiple asignments in one statement.

cmds-inspect.c out: · line 120

Quoted 4 lines
cmds-inspect.c +4 -0
+	sk->nr_items = 4096;
+
+	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, chunk_args);
+	e = errno;

This is useless asignment, I've removed it from the code, please don't reintrduce it.

Quoted 12 lines
cmds-inspect.c +10 -0
+	if (ret < 0) {
+		fprintf(stderr, "ret %d error '%s'\n", ret,
+				strerror(e));
+	}
+	return ret;
+}
 
 
+
+/* Given the objectid, find the block group item in the extent tree */
+static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args,
+		       u64 objectid, unsigned long length)

cmds-inspect.c out: · line 140

Quoted 10 lines
cmds-inspect.c +10 -0
+{
+	struct btrfs_ioctl_search_key *bg_sk;
+	int ret;
+	int e;
+
+	bg_sk = &bg_args->key;
+
+	bg_sk->min_objectid = bg_sk->max_objectid = objectid;
+	bg_sk->nr_items = 1;
+	bg_sk->min_offset = bg_sk->max_offset = length;

Same here.

cmds-inspect.c out: · line 146

Quoted 6 lines
cmds-inspect.c +6 -0
+
+	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, bg_args);
+	e = errno;
+	if (ret < 0) {
+		fprintf(stderr, "ret %d error '%s'\n", ret,
+				strerror(e));

Please take a look how the error messages are constructed when the tree search ioctl fails, there are enough examples in the code.

cmds-inspect.c out: · line 152

Quoted 6 lines
cmds-inspect.c +3 -0
+	}
+	return ret;
+}
 static const char * const cmd_inspect_inode_resolve_usage[] = {
cmds-inspect.c +0 -0
 	"btrfs inspect-internal inode-resolve [-v] <inode> <path>",
 	"Get file system paths for the given inode",

Actually, I'm not sure if such functions should exist at all, as they only hide the search ioctl but don't do any validation of the returned keys and data.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
gpg @keyserver.linux.it: Goffredo Baroncelli <kreijackATinwind.it>
Key fingerprint BBF5 1610 0B64 DAC6 5F7D  17B2 0EDA 9B37 8B82 E0B5
#20 lore raw

Re: [PATCH 1/3] btrfs-progs: Generic functions to retrieve chunks and their bg info

Hi,

Working on a v2 of this patch based on the comments received.

Thanks,

Divya

Quoted 120 lines · Goffredo Baroncelli

Hi,

any news about these commands ?

BR
G.Baroncelli

Quoted 111 lines · David Sterba
Quoted 31 lines · Divya Indi

An efficient alternative to retrieving block groups:
get_chunks(): Walk the chunk tree to retrieve the chunks. get_bg_info(): For each retrieved chunk, lookup an exact match of block group in the extent tree.

---
  cmds-inspect.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 files changed, 66 insertions(+), 0 deletions(-)
cmds-inspect.c +11 -0
index 4b7cea0..f435ea9 100644
@@ -81,6 +81,72 @@ out:
  	return !!ret;
  }
 
+static void bg_flags_to_str(u64 flags, char *ret)
+{
+	int empty = 1;
+
+	if (flags & BTRFS_BLOCK_GROUP_DATA) {
+		empty = 0;
+		strcpy(ret, "DATA");
+	}
+	if (flags & BTRFS_BLOCK_GROUP_METADATA) {
+		if (!empty)
+			strcat(ret, "|");

cmds-inspect.c out: · line 104

Quoted 10 lines
cmds-inspect.c +10 -0
+		strcat(ret, "METADATA");
+	}
+	if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
+		if (!empty)
+			strcat(ret, "|");
+		strcat(ret, "SYSTEM");
+	}
+}
+
+/* Walking through the chunk tree to retrieve chunks. */

No empty newline.

cmds-inspect.c out: · line 116

Quoted 12 lines
cmds-inspect.c +12 -0
+
+static int get_chunks(int fd, struct btrfs_ioctl_search_args *chunk_args)
+{
+	struct btrfs_ioctl_search_key *sk;
+	int ret;
+	int e;
+
+	sk = &chunk_args->key;
+
+	sk->tree_id = BTRFS_CHUNK_TREE_OBJECTID;
+	sk->min_objectid = sk->max_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
+	sk->max_type = sk->min_type = BTRFS_CHUNK_ITEM_KEY;

Please don't do multiple asignments in one statement.

cmds-inspect.c out: · line 120

Quoted 4 lines
cmds-inspect.c +4 -0
+	sk->nr_items = 4096;
+
+	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, chunk_args);
+	e = errno;

This is useless asignment, I've removed it from the code, please don't reintrduce it.

Quoted 11 lines
cmds-inspect.c +10 -0
+	if (ret < 0) {
+		fprintf(stderr, "ret %d error '%s'\n", ret,
+				strerror(e));
+	}
+	return ret;
+}
 
+
+/* Given the objectid, find the block group item in the extent tree */
+static int get_bg_info(int fd, struct btrfs_ioctl_search_args *bg_args,
+		       u64 objectid, unsigned long length)

cmds-inspect.c out: · line 140

Quoted 10 lines
cmds-inspect.c +10 -0
+{
+	struct btrfs_ioctl_search_key *bg_sk;
+	int ret;
+	int e;
+
+	bg_sk = &bg_args->key;
+
+	bg_sk->min_objectid = bg_sk->max_objectid = objectid;
+	bg_sk->nr_items = 1;
+	bg_sk->min_offset = bg_sk->max_offset = length;

Same here.

cmds-inspect.c out: · line 146

Quoted 6 lines
cmds-inspect.c +6 -0
+
+	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, bg_args);
+	e = errno;
+	if (ret < 0) {
+		fprintf(stderr, "ret %d error '%s'\n", ret,
+				strerror(e));

Please take a look how the error messages are constructed when the tree search ioctl fails, there are enough examples in the code.

Quoted 6 lines
cmds-inspect.c +3 -0
+	}
+	return ret;
+}
  static const char * const cmd_inspect_inode_resolve_usage[] = {
  	"btrfs inspect-internal inode-resolve [-v] <inode> <path>",

"Get file system paths for the given inode",

Actually, I'm not sure if such functions should exist at all, as they only hide the search ioctl but don't do any validation of the returned keys and data.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html