Skip to contentSkip to conversation

Add subcommand that dumps file extents

RFC PATCH New Sidong Yang 1 participant 3 messages Aug 22, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v5 cover Add subcommand that dumps file extents

Hi, I'm want to print which device has the file_extent. but I don't know how. Maybe it could be found by find block group in extent tree and find devid in chunk tree again. I don't know it's good way to find it. is there any good way to find devid?

This patch series add an subcommand in inspect-internal. It dumps file extents of the file that user provided. For avoiding duplicated code, the patch that export functions is in this series.

Sidong Yang (2):
btrfs-progs: export util functions about file extent items
btrfs-progs: cmds: Add subcommand that dumps file extents

---
v2:
- Prints type and compression
- Use the terms from file_extents_item like disk_bytenr not like "physical"
v3:
- export util functions for removing duplication
- change the way to loop with search ioctl
v4:
- export COMPRESS_STR_LEN for using function safely
v5:
- Supports json format
- Print hole type by checking disk_bytenr
---

 Makefile                         |   2 +-
 cmds/commands.h                  |   2 +-
 cmds/inspect-dump-file-extents.c | 161 +++++++++++++++++++++++++++++++
 cmds/inspect.c                   |   1 +
 kernel-shared/print-tree.c       |  18 ++--
 kernel-shared/print-tree.h       |   5 +
 6 files changed, 178 insertions(+), 11 deletions(-)
 create mode 100644 cmds/inspect-dump-file-extents.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH v5 1/2 btrfs-progs: export util functions about file extent items

This patch export two functions that convert enum about file extents to string. It can be used in other code like inspect-internal command. And this patch also make compress_type_to_str() function more safe by using strncpy() than strcpy().

---
 kernel-shared/print-tree.c | 18 +++++++++---------
 kernel-shared/print-tree.h |  5 +++++
 2 files changed, 14 insertions(+), 9 deletions(-)
kernel-shared/print-tree.c +9 -9
index e5d4b453..ce1c0ed3 100644
@@ -338,27 +338,27 @@ static void print_uuids(struct extent_buffer *eb)
 	printf("fs uuid %s\nchunk uuid %s\n", fs_uuid, chunk_uuid);
 }
 
-static void compress_type_to_str(u8 compress_type, char *ret)
+void btrfs_compress_type_to_str(u8 compress_type, char *ret)
 {
 	switch (compress_type) {
 	case BTRFS_COMPRESS_NONE:
-		strcpy(ret, "none");
+		strncpy(ret, "none", BTRFS_COMPRESS_STR_LEN);
 		break;
 	case BTRFS_COMPRESS_ZLIB:
-		strcpy(ret, "zlib");
+		strncpy(ret, "zlib", BTRFS_COMPRESS_STR_LEN);
 		break;
 	case BTRFS_COMPRESS_LZO:
-		strcpy(ret, "lzo");
+		strncpy(ret, "lzo", BTRFS_COMPRESS_STR_LEN);
 		break;
 	case BTRFS_COMPRESS_ZSTD:
-		strcpy(ret, "zstd");
+		strncpy(ret, "zstd", BTRFS_COMPRESS_STR_LEN);
 		break;
 	default:
-		sprintf(ret, "UNKNOWN.%d", compress_type);
+		snprintf(ret, BTRFS_COMPRESS_STR_LEN, "UNKNOWN.%d", compress_type);
 	}
 }
 
-static const char* file_extent_type_to_str(u8 type)
+const char* btrfs_file_extent_type_to_str(u8 type)
 {
 	switch (type) {
 	case BTRFS_FILE_EXTENT_INLINE: return "inline";
@@ -376,12 +376,12 @@ static void print_file_extent_item(struct extent_buffer *eb,
 	unsigned char extent_type = btrfs_file_extent_type(eb, fi);
 	char compress_str[16];
 
-	compress_type_to_str(btrfs_file_extent_compression(eb, fi),
+	btrfs_compress_type_to_str(btrfs_file_extent_compression(eb, fi),
 			     compress_str);
 
 	printf("\t\tgeneration %llu type %hhu (%s)\n",
 			btrfs_file_extent_generation(eb, fi),
-			extent_type, file_extent_type_to_str(extent_type));
+			extent_type, btrfs_file_extent_type_to_str(extent_type));
 
 	if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
 		printf("\t\tinline extent data size %u ram_bytes %llu compression %hhu (%s)\n",
kernel-shared/print-tree.h +5 -0
index 80fb6ef7..3b96e89d 100644
@@ -33,6 +33,8 @@ enum {
 	BTRFS_PRINT_TREE_DEFAULT = BTRFS_PRINT_TREE_BFS,
 };
 
+#define BTRFS_COMPRESS_STR_LEN 12
+
 void btrfs_print_tree(struct extent_buffer *eb, unsigned int mode);
 void btrfs_print_leaf(struct extent_buffer *eb, unsigned int mode);
 
@@ -43,4 +45,7 @@ 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_compress_type_to_str(u8 compress_type, char *ret);
+const char* btrfs_file_extent_type_to_str(u8 type);
+
 #endif
Signature · 1 line
2.25.1
#2 lore raw

PATCH v5 2/2 btrfs-progs: cmds: Add subcommand that dumps file extents

In reply to Sidong Yang

This patch adds an subcommand in inspect-internal. It dumps file extents of the file that user provided. It helps to show the internal information about file extents comprise the file. It supports json format by using formatted printing. An example is below.

{

  "__header": {
    "version": "1"
  },
  "file-extents": [
    {
      "start": "0",
      "disk_bytenr": "21651456",
      "disk_num_bytes": "4096",
      "offset": "0",
      "len": "65536",
      "compression": "zstd",
      "type": "regular"
    },
    {
      "start": "65536",
      "len": "131072",
      "compression": "none",
      "type": "hole"
    }

]
}

---
 Makefile                         |   2 +-
 cmds/commands.h                  |   2 +-
 cmds/inspect-dump-file-extents.c | 161 +++++++++++++++++++++++++++++++
 cmds/inspect.c                   |   1 +
 4 files changed, 164 insertions(+), 2 deletions(-)
 create mode 100644 cmds/inspect-dump-file-extents.c
Makefile +1 -1
index a1cc457b..911e16de 100644
@@ -156,7 +156,7 @@ cmds_objects = cmds/subvolume.o cmds/filesystem.o cmds/device.o cmds/scrub.o \
 	       cmds/restore.o cmds/rescue.o cmds/rescue-chunk-recover.o \
 	       cmds/rescue-super-recover.o \
 	       cmds/property.o cmds/filesystem-usage.o cmds/inspect-dump-tree.o \
-	       cmds/inspect-dump-super.o cmds/inspect-tree-stats.o cmds/filesystem-du.o \
+	       cmds/inspect-dump-super.o cmds/inspect-tree-stats.o cmds/inspect-dump-file-extents.o cmds/filesystem-du.o \
 	       mkfs/common.o check/mode-common.o check/mode-lowmem.o
 libbtrfs_objects = common/send-stream.o common/send-utils.o kernel-lib/rbtree.o btrfs-list.o \
 		   kernel-lib/radix-tree.o common/extent-cache.o kernel-shared/extent_io.o \
cmds/commands.h +1 -1
index 8fa85d6c..55de248e 100644
@@ -154,5 +154,5 @@ DECLARE_COMMAND(select_super);
 DECLARE_COMMAND(dump_super);
 DECLARE_COMMAND(debug_tree);
 DECLARE_COMMAND(rescue);
-
+DECLARE_COMMAND(inspect_dump_file_extents);
 #endif
cmds/inspect-dump-file-extents.c new file +161 -0

161 lines not on this page · raw message

cmds/inspect.c +1 -0
index 2ef5f4b6..dfb0e27b 100644
@@ -696,6 +696,7 @@ static const struct cmd_group inspect_cmd_group = {
 		&cmd_struct_inspect_dump_tree,
 		&cmd_struct_inspect_dump_super,
 		&cmd_struct_inspect_tree_stats,
+		&cmd_struct_inspect_dump_file_extents,
 		NULL
 	}
 };
Signature · 1 line
2.25.1