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.

Signed-off-by: Divya Indi <divya.i...@oracle.com>
Reviewed-by: Ashish Samant <ashish.sam...@oracle.com>
Reviewed-by: Liu Bo <bo.li....@oracle.com>
---
 cmds-inspect.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 66 insertions(+), 0 deletions(-)

diff --git a/cmds-inspect.c b/cmds-inspect.c
index 4b7cea0..f435ea9 100644
--- a/cmds-inspect.c
+++ b/cmds-inspect.c
@@ -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",
-- 
1.7.1

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

Reply via email to