btrfs-progs: export util functions about file extent items
Subsystems (2)
PATCH v3 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().
---
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
--- kernel-shared/print-tree.c | 18 ++++++++++-------- kernel-shared/print-tree.h | 3 +++ 2 files changed, 13 insertions(+), 8 deletions(-)
kernel-shared/print-tree.c +10 -8
@@ -27,6 +27,8 @@#include "kernel-shared/print-tree.h" #include "common/utils.h" +#define COMPRESS_STR_LEN 5 +static void print_dir_item_type (struct extent_buffer *eb,struct btrfs_dir_item *di) {
@@ -338,27 +340,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" ,COMPRESS_STR_LEN );break ;case BTRFS_COMPRESS_ZLIB : -strcpy (ret,"zlib" ); +strncpy (ret,"zlib" ,COMPRESS_STR_LEN );break ;case BTRFS_COMPRESS_LZO : -strcpy (ret,"lzo" ); +strncpy (ret,"lzo" ,COMPRESS_STR_LEN );break ;case BTRFS_COMPRESS_ZSTD : -strcpy (ret,"zstd" ); +strncpy (ret,"zstd" ,COMPRESS_STR_LEN );break ;default :sprintf (ret,"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 +378,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 +3 -0
@@ -43,4 +43,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
PATCH v3 2/2 btrfs-progs: cmds: Add subcommand that dumps file extents
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.
---
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
--- Makefile | 2 +- cmds/commands.h | 2 +- cmds/inspect-dump-file-extents.c | 134 +++++++++++++++++++++++++++++++ cmds/inspect.c | 1 + 4 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 cmds/inspect-dump-file-extents.c
Makefile +1 -1
@@ -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
@@ -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 +134 -0
134 lines not on this page · raw message
cmds/inspect.c +1 -0
@@ -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
Quoted 24 lines · Sidong Yang
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 usingstrncpy()thanstrcpy().Signed-off-by Sidong Yang <[email protected]>---
v2:
- Prints type and compression
- Use the terms fromfile_extents_itemlikedisk_bytenrnot like "physical"
v3:
- export util functions for removing duplication
- change the way to loop with search ioctl--- kernel-shared/print-tree.c | 18 ++++++++++-------- kernel-shared/print-tree.h | 3 +++ 2 files changed, 13 insertions(+), 8 deletions(-)kernel-shared/print-tree.c +0
-0
kernel-shared/print-tree.c line 30
Quoted 4 lines
kernel-shared/print-tree.c +1
-0@@ -27,6 +27,8 @@#include "kernel-shared/print-tree.h" #include "common/utils.h" +#define COMPRESS_STR_LEN 5
It's already too small to contain all the strings.
We have a default branch:
default:
sprintf(ret, "UNKNOWN.%d", compress_type);
In that case, our minimal value should be strlen("UNKNOWN.255") + 1, which is 12 bytes.
Your old rounded up 16 bytes is in fact perfect in this case.
Despite that, this patch looks good to me.
Thanks,
Qu
Quoted 66 lines
kernel-shared/print-tree.c +9
-8+static void print_dir_item_type (struct extent_buffer *eb,struct btrfs_dir_item *di) {@@ -338,27 +340,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" ,COMPRESS_STR_LEN );break ;case BTRFS_COMPRESS_ZLIB : -strcpy (ret,"zlib" ); +strncpy (ret,"zlib" ,COMPRESS_STR_LEN );break ;case BTRFS_COMPRESS_LZO : -strcpy (ret,"lzo" ); +strncpy (ret,"lzo" ,COMPRESS_STR_LEN );break ;case BTRFS_COMPRESS_ZSTD : -strcpy (ret,"zstd" ); +strncpy (ret,"zstd" ,COMPRESS_STR_LEN );break ;default :sprintf (ret,"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 +378,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 +3
-0@@ -43,4 +43,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
Quoted 38 lines · Qu Wenruo
Quoted 24 lines · Sidong Yang
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 usingstrncpy()thanstrcpy().Signed-off-by Sidong Yang <[email protected]>---
v2:
- Prints type and compression
- Use the terms fromfile_extents_itemlikedisk_bytenrnot like "physical"
v3:
- export util functions for removing duplication
- change the way to loop with search ioctl--- kernel-shared/print-tree.c | 18 ++++++++++-------- kernel-shared/print-tree.h | 3 +++ 2 files changed, 13 insertions(+), 8 deletions(-)kernel-shared/print-tree.c +0
-0kernel-shared/print-tree.c line 29
Quoted 3 lines
kernel-shared/print-tree.c +1
-0@@ -27,6 +27,8 @@#include "kernel-shared/print-tree.h" #include "common/utils.h" +#define COMPRESS_STR_LEN 5It's already too small to contain all the strings.
We have a default branch:
kernel-shared/print-tree.c +0
-0default :sprintf (ret,"UNKNOWN.%d" , compress_type);
Quoted 6 lines
In that case, our minimal value should be
strlen("UNKNOWN.255")+ 1, which is 12 bytes.Your old rounded up 16 bytes is in fact perfect in this case.
Despite that, this patch looks good to me.
Thanks for review.
I've been thought about this. should COMPRESS_STR_LEN be exported? Developer that use this function needs to know max length of string. Or we can choose the simple implementation that just return const char* like btrfs_file_extent_type_to_str(). But it will be hard to print unknown compress_type.
Thanks,
Sidong
Quoted 64 lines
Thanks,
QuQuoted 60 lines
kernel-shared/print-tree.c +9
-8+static void print_dir_item_type (struct extent_buffer *eb,struct btrfs_dir_item *di) {@@ -338,27 +340,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" ,COMPRESS_STR_LEN );break ;case BTRFS_COMPRESS_ZLIB : -strcpy (ret,"zlib" ); +strncpy (ret,"zlib" ,COMPRESS_STR_LEN );break ;case BTRFS_COMPRESS_LZO : -strcpy (ret,"lzo" ); +strncpy (ret,"lzo" ,COMPRESS_STR_LEN );break ;case BTRFS_COMPRESS_ZSTD : -strcpy (ret,"zstd" ); +strncpy (ret,"zstd" ,COMPRESS_STR_LEN );break ;default :sprintf (ret,"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 +378,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 +3
-0@@ -43,4 +43,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
Quoted 42 lines · Sidong Yang
Quoted 41 lines · Qu Wenruo
Quoted 24 lines · Sidong Yang
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 usingstrncpy()thanstrcpy().Signed-off-by Sidong Yang <[email protected]>---
v2:
- Prints type and compression
- Use the terms fromfile_extents_itemlikedisk_bytenrnot like "physical"
v3:
- export util functions for removing duplication
- change the way to loop with search ioctl--- kernel-shared/print-tree.c | 18 ++++++++++-------- kernel-shared/print-tree.h | 3 +++ 2 files changed, 13 insertions(+), 8 deletions(-)kernel-shared/print-tree.c +0
-0kernel-shared/print-tree.c line 29
Quoted 3 lines
kernel-shared/print-tree.c +1
-0@@ -27,6 +27,8 @@#include "kernel-shared/print-tree.h" #include "common/utils.h" +#define COMPRESS_STR_LEN 5It's already too small to contain all the strings.
We have a default branch:
default: sprintf(ret, "UNKNOWN.%d", compress_type);In that case, our minimal value should be
strlen("UNKNOWN.255")+ 1, which is 12 bytes.
Quoted 7 lines
Quoted 3 lines
Your old rounded up 16 bytes is in fact perfect in this case.
Despite that, this patch looks good to me.
Thanks for review.
I've been thought about this. should
COMPRESS_STR_LENbe exported?
I guess yes.
Thanks,
Qu
Quoted 71 lines
Developer that use this function needs to know max length of string. Or we can choose the simple implementation that just return const char* like
btrfs_file_extent_type_to_str(). But it will be hard to print unknowncompress_type.Thanks,
SidongQuoted 64 lines
Thanks,
QuQuoted 60 lines
kernel-shared/print-tree.c +9
-8+static void print_dir_item_type (struct extent_buffer *eb,struct btrfs_dir_item *di) {@@ -338,27 +340,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" ,COMPRESS_STR_LEN );break ;case BTRFS_COMPRESS_ZLIB : -strcpy (ret,"zlib" ); +strncpy (ret,"zlib" ,COMPRESS_STR_LEN );break ;case BTRFS_COMPRESS_LZO : -strcpy (ret,"lzo" ); +strncpy (ret,"lzo" ,COMPRESS_STR_LEN );break ;case BTRFS_COMPRESS_ZSTD : -strcpy (ret,"zstd" ); +strncpy (ret,"zstd" ,COMPRESS_STR_LEN );break ;default :sprintf (ret,"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 +378,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 +3
-0@@ -43,4 +43,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
Quoted 47 lines · Qu Wenruo
Quoted 44 lines · Sidong Yang
Quoted 43 lines · Qu Wenruo
Quoted 24 lines · Sidong Yang
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 usingstrncpy()thanstrcpy().Signed-off-by Sidong Yang <[email protected]>---
v2:
- Prints type and compression
- Use the terms fromfile_extents_itemlikedisk_bytenrnot like "physical"
v3:
- export util functions for removing duplication
- change the way to loop with search ioctl--- kernel-shared/print-tree.c | 18 ++++++++++-------- kernel-shared/print-tree.h | 3 +++ 2 files changed, 13 insertions(+), 8 deletions(-)kernel-shared/print-tree.c +0
-0kernel-shared/print-tree.c line 29
Quoted 3 lines
kernel-shared/print-tree.c +1
-0@@ -27,6 +27,8 @@#include "kernel-shared/print-tree.h" #include "common/utils.h" +#define COMPRESS_STR_LEN 5It's already too small to contain all the strings.
We have a default branch:
default: sprintf(ret, "UNKNOWN.%d", compress_type);In that case, our minimal value should be
strlen("UNKNOWN.255")+ 1, which is 12 bytes.Your old rounded up 16 bytes is in fact perfect in this case.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Despite that, this patch looks good to me.
Thanks for review.
I've been thought about this. should
COMPRESS_STR_LENbe exported?I guess yes.
Thanks, I'll apply this.
Thanks,
Sidong
Quoted 75 lines
Thanks,
QuQuoted 71 lines
Developer that use this function needs to know max length of string. Or we can choose the simple implementation that just return const char* like
btrfs_file_extent_type_to_str(). But it will be hard to print unknowncompress_type.Thanks,
SidongQuoted 64 lines
Thanks,
QuQuoted 60 lines
kernel-shared/print-tree.c +9
-8+static void print_dir_item_type (struct extent_buffer *eb,struct btrfs_dir_item *di) {@@ -338,27 +340,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" ,COMPRESS_STR_LEN );break ;case BTRFS_COMPRESS_ZLIB : -strcpy (ret,"zlib" ); +strncpy (ret,"zlib" ,COMPRESS_STR_LEN );break ;case BTRFS_COMPRESS_LZO : -strcpy (ret,"lzo" ); +strncpy (ret,"lzo" ,COMPRESS_STR_LEN );break ;case BTRFS_COMPRESS_ZSTD : -strcpy (ret,"zstd" ); +strncpy (ret,"zstd" ,COMPRESS_STR_LEN );break ;default :sprintf (ret,"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 +378,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 +3
-0@@ -43,4 +43,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