btrfs: Introduce helper functions for compression.
PATCH 2/5 btrfs: Introduce helper functions for compression.
1. Introduce the compression type and compression level combination function. Use the lowest 4 bits of unsigned int to record the compression type and 5 - 8 bits to record the compression level.
2. Introduce functions to parse compression description strings into filesystem flags and vice versa.
Signed-off-by Li Zhang <[email protected]>
--- fs/btrfs/compression.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/compression.h | 14 ++++++++++++++ 2 files changed, 65 insertions(+)
fs/btrfs/compression.c +51 -0
@@ -49,6 +49,38 @@ const char* btrfs_compress_type2str(enum btrfs_compression_type type)return NULL ; } +/* +* convert str to combination of compression type and level +* ret: combination of compression type and compress level +* return type and level separately if caller specify type and level pointer +*/ +unsigned int btrfs_compress_str2type_level (const char *str,unsigned int *type,unsigned int *level){ +int i; +unsigned int result_type, result_level; +size_t len =strlen (str); + result_type =BTRFS_COMPRESS_NONE ; + result_level =0 ; +for (i =1 ; i <ARRAY_SIZE (btrfs_compress_types); i ++){ +size_t comp_len =strlen (btrfs_compress_types[i]); +if (len < comp_len){ +continue ; + } +if (!strncmp (btrfs_compress_types[i], str, comp_len)){ + result_type =BTRFS_COMPRESS_NONE + i; + result_level =btrfs_compress_str2level (result_type, str + comp_len); +break ; + } + } +if (type !=NULL ){ + *type = result_type; + } +if (level !=NULL ){ + *level = result_level; + } +return btrfs_compress_combine_type_level (result_type, result_level); +} + +bool btrfs_compress_is_valid_type (const char *str,size_t len) {int i;
@@ -1888,3 +1920,22 @@ unsigned int btrfs_compress_str2level(unsigned int type, const char *str)return level; } + + +/* +* convert combination of compression type and level to meaningful string +* the caller should specify buffer and buffer size +*/ +char *btrfs_compress_type_level2str (unsigned type,unsigned level,char *out,size_t out_len){ +switch (type) { +case BTRFS_COMPRESS_ZLIB : +case BTRFS_COMPRESS_LZO : +case BTRFS_COMPRESS_ZSTD : +case BTRFS_COMPRESS_NONE : +snprintf (out, out_len,"%s:%d" , btrfs_compress_types[type],btrfs_compress_set_level (type, level)); +return out; +default : +break ; + } +return NULL ; +}
fs/btrfs/compression.h +14 -0
@@ -76,6 +76,16 @@ static inline unsigned int btrfs_compress_level(unsigned int type_level)return ((type_level &0xF0 ) >>4 ); } +/* +* helper function combine compression type and level +* combine compression type and level in one byte +* the lower 4bit represent compression type +* the upper 4bit represent compression level +*/ +static inline unsigned int btrfs_compress_combine_type_level (unsigned int type,unsigned level){ +return (level <<4 ) | type; +} +void __init btrfs_init_compress (void );void __cold btrfs_exit_compress (void );
@@ -101,6 +111,10 @@ blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,unsigned int btrfs_compress_str2level (unsigned int type,const char *str); +unsigned int btrfs_compress_str2type_level (const char *str,unsigned int * type,unsigned int *level); + +char *btrfs_compress_type_level2str (unsigned type,unsigned level,char *out,size_t out_len); +enum btrfs_compression_type {BTRFS_COMPRESS_NONE =0 ,BTRFS_COMPRESS_ZLIB =1 ,
Signature · 1 line
1.8.3.1