btrfs-progs: fi defrag: allow passing compression levels
Subsystems (2)
PATCH 1/1 btrfs-progs: fi defrag: allow passing compression levels
zlib and zstd compression methods support using compression levels. Enable defrag to pass them to kernel.
--- Documentation/ch-compression.rst | 10 +++--- cmds/filesystem.c | 55 ++++++++++++++++++++++++++++++-- kernel-shared/uapi/btrfs.h | 10 +++++- libbtrfs/ioctl.h | 1 + libbtrfsutil/btrfs.h | 12 +++++-- 5 files changed, 77 insertions(+), 11 deletions(-)
Documentation/ch-compression.rst +5 -5
@@ -25,8 +25,8 @@ LZO * good backward compatibility ZSTD * compression comparable to ZLIB with higher compression/decompression speeds and different ratio - * levels: 1 to 15, mapped directly (higher levels are not available) - * since 4.14, levels since 5.1 + * levels: -15 to 15, mapped directly, default is 3 + * since 4.14, levels 1 to 15 since 5.1, -15 to -1 since 6.15 The differences depend on the actual data set and cannot be expressed by a single number or recommendation. Higher levels consume more CPU time and may
@@ -78,7 +78,7 @@ Compression levels The level support of ZLIB has been added in v4.14, LZO does not support levels (the kernel implementation provides only one), ZSTD level support has been added -in v5.1. +in v5.1 and negative levels since v6.15. There are 9 levels of ZLIB supported (1 to 9), mapping 1:1 from the mount option to the algorithm defined level. The default is level 3, which provides the
@@ -86,8 +86,8 @@ reasonably good compression ratio and is still reasonably fast. The difference in compression gain of levels 7, 8 and 9 is comparable but the higher levels take longer. -The ZSTD support includes levels 1 to 15, a subset of full range of what ZSTD -provides. Levels 1-3 are real-time, 4-8 slower with improved compression and +The ZSTD support includes levels -15 to 15, a subset of full range of what ZSTD +provides. Levels -15-3 are real-time, 4-8 slower with improved compression and 9-15 try even harder though the resulting size may not be significantly improved. Level 0 always maps to the default. The compression level does not affect
cmds/filesystem.c +22 -3
@@ -962,6 +962,7 @@ static const char * const cmd_filesystem_defrag_usage[] = {"" ,OPTLINE ("-r" ,"defragment files recursively" ),OPTLINE ("-c[zlib,lzo,zstd]" ,"compress the file while defragmenting, optional parameter (no space in between)" ), +OPTLINE ("-L|--level level" ,"use given compression level if enabled (zlib supports levels 1-9, zstd -15-15, and 0 selects the default level)" ),OPTLINE ("-f" ,"flush data to disk immediately after defragmenting" ),OPTLINE ("-s start" ,"defragment only from byte onward" ), OPTLINE("-l len", "defragment only up to len bytes"),
@@ -1066,6 +1067,7 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,bool recursive = false;int ret =0 ;int compress_type =BTRFS_COMPRESS_NONE ; +int compress_level =0 ;/* * Kernel 4.19+ supports defragmention of files open read-only,
@@ -1095,18 +1097,18 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,if (bconf.verbose !=BTRFS_BCONF_UNSET ) bconf.verbose++; - defrag_global_errors =0 ; defrag_global_errors =0 ; optind =0 ;while (1 ) {enum {GETOPT_VAL_STEP =GETOPT_VAL_FIRST };static const struct option long_options[] = { + {"level" , required_argument,NULL ,'L' }, {"step" , required_argument,NULL ,GETOPT_VAL_STEP }, {NULL ,0 ,NULL ,0 } };int c; - c =getopt_long (argc, argv,"vrc::fs:l:t:" , long_options,NULL ); + c =getopt_long (argc, argv,"vrc::L:fs:l:t:" , long_options,NULL );if (c <0 )break ;
@@ -1116,6 +1118,18 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,if (optarg) compress_type =parse_compress_type_arg (optarg);break ; +case 'L' : +/* +* Do not enforce any limits here, kernel will do itself +* based on what's supported by the running version. +* Just clip to the s8 type of the API. +*/ + compress_level =atoi (optarg); +if (compress_level <-128 ) + compress_level =-128 ; +else if (compress_level >127 ) + compress_level =127 ; +break ;case 'f' : flush = true;break ;
@@ -1165,7 +1179,12 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd, defrag_global_range.extent_thresh = (u32 )thresh;if (compress_type) { defrag_global_range.flags |=BTRFS_DEFRAG_RANGE_COMPRESS ; - defrag_global_range.compress_type = compress_type; +if (compress_level) { + defrag_global_range.flags |=BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL ; + defrag_global_range.compress.type = compress_type; + defrag_global_range.compress.level= compress_level; + }else + defrag_global_range.compress_type = compress_type; }if (flush) defrag_global_range.flags |=BTRFS_DEFRAG_RANGE_START_IO ;
kernel-shared/uapi/btrfs.h +9 -1
@@ -645,7 +645,9 @@ _static_assert(sizeof(struct btrfs_ioctl_clone_range_args) == 32);*/ #define BTRFS_DEFRAG_RANGE_COMPRESS 1#define BTRFS_DEFRAG_RANGE_START_IO 2 +#define BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL 4#define BTRFS_DEFRAG_RANGE_FLAGS_SUPP (BTRFS_DEFRAG_RANGE_COMPRESS | \ + BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL | \ BTRFS_DEFRAG_RANGE_START_IO)struct btrfs_ioctl_defrag_range_args {
@@ -673,7 +675,13 @@ struct btrfs_ioctl_defrag_range_args {* for this defrag operation. If unspecified, zlib will * be used */ -__u32 compress_type; +union { +__u32 compress_type; +struct { +__u8 type; +__s8 level; + } compress; + };/* spare for later */ __u32 unused[4 ];
libbtrfs/ioctl.h +1 -0
@@ -398,6 +398,7 @@ struct btrfs_ioctl_clone_range_args {/* flags for the defrag range ioctl */ #define BTRFS_DEFRAG_RANGE_COMPRESS 1#define BTRFS_DEFRAG_RANGE_START_IO 2 +#define BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL 4#define BTRFS_SAME_DATA_DIFFERS 1/* For extent-same ioctl */
libbtrfsutil/btrfs.h +9 -1
@@ -608,7 +608,9 @@ struct btrfs_ioctl_clone_range_args {*/ #define BTRFS_DEFRAG_RANGE_COMPRESS 1#define BTRFS_DEFRAG_RANGE_START_IO 2 +#define BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL 4#define BTRFS_DEFRAG_RANGE_FLAGS_SUPP (BTRFS_DEFRAG_RANGE_COMPRESS | \ + BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL | \ BTRFS_DEFRAG_RANGE_START_IO)struct btrfs_ioctl_defrag_range_args {
@@ -636,7 +638,13 @@ struct btrfs_ioctl_defrag_range_args {* for this defrag operation. If unspecified, zlib will * be used */ -__u32 compress_type; +union { +__u32 compress_type; +struct { +__u8 type; +__s8 level; + } compress; + };/* spare for later */ __u32 unused[4 ];
Signature · 1 line
2.43.0
Quoted 17 lines · Daniel Vacek
zlib and zstd compression methods support using compression levels. Enable defrag to pass them to kernel.
Signed-off-by Daniel Vacek <[email protected]>--- Documentation/ch-compression.rst | 10 +++--- cmds/filesystem.c | 55 ++++++++++++++++++++++++++++++-- kernel-shared/uapi/btrfs.h | 10 +++++- libbtrfs/ioctl.h | 1 + libbtrfsutil/btrfs.h | 12 +++++-- 5 files changed, 77 insertions(+), 11 deletions(-)Documentation/ch-compression.rst +0
-0
Documentation/ch-compression.rst LZO · line 29
Quoted 7 lines
Documentation/ch-compression.rst +2
-2@@ -25,8 +25,8 @@ LZO * good backward compatibility ZSTD * compression comparable to ZLIB with higher compression/decompression speeds and different ratio - * levels: 1 to 15, mapped directly (higher levels are not available) - * since 4.14, levels since 5.1 + * levels: -15 to 15, mapped directly, default is 3 + * since 4.14, levels 1 to 15 since 5.1, -15 to -1 since 6.15
I believe the doc updates can be a separate patch since it's not related to the new defrag ioctl flag.
Quoted 84 lines
Documentation/ch-compression.rst +3
-3The differences depend on the actual data set and cannot be expressed by a single number or recommendation. Higher levels consume more CPU time and may@@ -78,7 +78,7 @@ Compression levels The level support of ZLIB has been added in v4.14, LZO does not support levels (the kernel implementation provides only one), ZSTD level support has been added -in v5.1. +in v5.1 and negative levels since v6.15. There are 9 levels of ZLIB supported (1 to 9), mapping 1:1 from the mount option to the algorithm defined level. The default is level 3, which provides the@@ -86,8 +86,8 @@ reasonably good compression ratio and is still reasonably fast. The difference in compression gain of levels 7, 8 and 9 is comparable but the higher levels take longer. -The ZSTD support includes levels 1 to 15, a subset of full range of what ZSTD -provides. Levels 1-3 are real-time, 4-8 slower with improved compression and +The ZSTD support includes levels -15 to 15, a subset of full range of what ZSTD +provides. Levels -15-3 are real-time, 4-8 slower with improved compression and 9-15 try even harder though the resulting size may not be significantly improved. Level 0 always maps to the default. The compression level does not affectcmds/filesystem.c +16
-2@@ -962,6 +962,7 @@ static const char * const cmd_filesystem_defrag_usage[] = {"" ,OPTLINE ("-r" ,"defragment files recursively" ),OPTLINE ("-c[zlib,lzo,zstd]" ,"compress the file while defragmenting, optional parameter (no space in between)" ), +OPTLINE ("-L|--level level" ,"use given compression level if enabled (zlib supports levels 1-9, zstd -15-15, and 0 selects the default level)" ),OPTLINE ("-f" ,"flush data to disk immediately after defragmenting" ),OPTLINE ("-s start" ,"defragment only from byte onward" ), OPTLINE("-l len", "defragment only up to len bytes"),@@ -1066,6 +1067,7 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,bool recursive = false;int ret =0 ;int compress_type =BTRFS_COMPRESS_NONE ; +int compress_level =0 ;/* * Kernel 4.19+ supports defragmention of files open read-only, @@ -1095,18 +1097,18 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,if (bconf.verbose !=BTRFS_BCONF_UNSET ) bconf.verbose++; - defrag_global_errors =0 ; defrag_global_errors =0 ; optind =0 ;while (1 ) {enum {GETOPT_VAL_STEP =GETOPT_VAL_FIRST };static const struct option long_options[] = { + {"level" , required_argument,NULL ,'L' }, {"step" , required_argument,NULL ,GETOPT_VAL_STEP }, {NULL ,0 ,NULL ,0 } };int c; - c =getopt_long (argc, argv,"vrc::fs:l:t:" , long_options,NULL ); + c =getopt_long (argc, argv,"vrc::L:fs:l:t:" , long_options,NULL );if (c <0 )break ;@@ -1116,6 +1118,18 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,if (optarg) compress_type =parse_compress_type_arg (optarg);break ; +case 'L' : +/* +* Do not enforce any limits here, kernel will do itself +* based on what's supported by the running version. +* Just clip to the s8 type of the API. +*/ + compress_level =atoi (optarg); +if (compress_level <-128 ) + compress_level =-128 ; +else if (compress_level >127 ) + compress_level =127 ; +break ;case 'f' : flush = true;break ;
cmds/filesystem.c static int cmd_filesystem_defrag(const struct cmd_struct *cmd, · line 1183
Quoted 6 lines
cmds/filesystem.c +2
-1@@ -1165,7 +1179,12 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd, defrag_global_range.extent_thresh = (u32 )thresh;if (compress_type) { defrag_global_range.flags |=BTRFS_DEFRAG_RANGE_COMPRESS ; - defrag_global_range.compress_type = compress_type; +if (compress_level) { + defrag_global_range.flags |=BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL ;
Since it's a newer flag, the ioctl() may fail with -EOPNOTSUPP on older kernels, in that case it's better to fall back to flags without the new COMPRESS_LEVEL one, and try it again. (With some warning showing that the kernel doesn't support the new flag).
Thanks,
Qu
Quoted 77 lines
cmds/filesystem.c +4
-0+ defrag_global_range.compress.type = compress_type; + defrag_global_range.compress.level= compress_level; + }else + defrag_global_range.compress_type = compress_type; }if (flush) defrag_global_range.flags |=BTRFS_DEFRAG_RANGE_START_IO ;kernel-shared/uapi/btrfs.h +9
-1@@ -645,7 +645,9 @@ _static_assert(sizeof(struct btrfs_ioctl_clone_range_args) == 32);*/ #define BTRFS_DEFRAG_RANGE_COMPRESS 1#define BTRFS_DEFRAG_RANGE_START_IO 2 +#define BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL 4#define BTRFS_DEFRAG_RANGE_FLAGS_SUPP (BTRFS_DEFRAG_RANGE_COMPRESS | \ + BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL | \ BTRFS_DEFRAG_RANGE_START_IO)struct btrfs_ioctl_defrag_range_args {@@ -673,7 +675,13 @@ struct btrfs_ioctl_defrag_range_args {* for this defrag operation. If unspecified, zlib will * be used */ -__u32 compress_type; +union { +__u32 compress_type; +struct { +__u8 type; +__s8 level; + } compress; + };/* spare for later */ __u32 unused[4 ];libbtrfs/ioctl.h +1
-0@@ -398,6 +398,7 @@ struct btrfs_ioctl_clone_range_args {/* flags for the defrag range ioctl */ #define BTRFS_DEFRAG_RANGE_COMPRESS 1#define BTRFS_DEFRAG_RANGE_START_IO 2 +#define BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL 4#define BTRFS_SAME_DATA_DIFFERS 1/* For extent-same ioctl */ libbtrfsutil/btrfs.h +9
-1@@ -608,7 +608,9 @@ struct btrfs_ioctl_clone_range_args {*/ #define BTRFS_DEFRAG_RANGE_COMPRESS 1#define BTRFS_DEFRAG_RANGE_START_IO 2 +#define BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL 4#define BTRFS_DEFRAG_RANGE_FLAGS_SUPP (BTRFS_DEFRAG_RANGE_COMPRESS | \ + BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL | \ BTRFS_DEFRAG_RANGE_START_IO)struct btrfs_ioctl_defrag_range_args {@@ -636,7 +638,13 @@ struct btrfs_ioctl_defrag_range_args {* for this defrag operation. If unspecified, zlib will * be used */ -__u32 compress_type; +union { +__u32 compress_type; +struct { +__u8 type; +__s8 level; + } compress; + };/* spare for later */ __u32 unused[4 ];
Quoted 16 lines · Qu Wenruo
Quoted 12 lines · Daniel Vacek
zlib and zstd compression methods support using compression levels. Enable defrag to pass them to kernel.
Signed-off-by Daniel Vacek <[email protected]>--- Documentation/ch-compression.rst | 10 +++--- cmds/filesystem.c | 55 ++++++++++++++++++++++++++++++-- kernel-shared/uapi/btrfs.h | 10 +++++- libbtrfs/ioctl.h | 1 + libbtrfsutil/btrfs.h | 12 +++++-- 5 files changed, 77 insertions(+), 11 deletions(-)
Quoted 15 lines
Documentation/ch-compression.rst LZO · line 29
Quoted 12 lines
Documentation/ch-compression.rst +2
-2@@ -25,8 +25,8 @@ LZO * good backward compatibility ZSTD * compression comparable to ZLIB with higher compression/decompression speeds and different ratio - * levels: 1 to 15, mapped directly (higher levels are not available) - * since 4.14, levels since 5.1 + * levels: -15 to 15, mapped directly, default is 3 + * since 4.14, levels 1 to 15 since 5.1, -15 to -1 since 6.15I believe the doc updates can be a separate patch since it's not related to the new defrag ioctl flag.
Good point. Thanks.
Quoted 87 lines
Quoted 87 lines
Documentation/ch-compression.rst +3
-3The differences depend on the actual data set and cannot be expressed by a single number or recommendation. Higher levels consume more CPU time and may@@ -78,7 +78,7 @@ Compression levels The level support of ZLIB has been added in v4.14, LZO does not support levels (the kernel implementation provides only one), ZSTD level support has been added -in v5.1. +in v5.1 and negative levels since v6.15. There are 9 levels of ZLIB supported (1 to 9), mapping 1:1 from the mount option to the algorithm defined level. The default is level 3, which provides the@@ -86,8 +86,8 @@ reasonably good compression ratio and is still reasonably fast. The difference in compression gain of levels 7, 8 and 9 is comparable but the higher levels take longer. -The ZSTD support includes levels 1 to 15, a subset of full range of what ZSTD -provides. Levels 1-3 are real-time, 4-8 slower with improved compression and +The ZSTD support includes levels -15 to 15, a subset of full range of what ZSTD +provides. Levels -15-3 are real-time, 4-8 slower with improved compression and 9-15 try even harder though the resulting size may not be significantly improved. Level 0 always maps to the default. The compression level does not affectcmds/filesystem.c +16
-2@@ -962,6 +962,7 @@ static const char * const cmd_filesystem_defrag_usage[] = {"" ,OPTLINE ("-r" ,"defragment files recursively" ),OPTLINE ("-c[zlib,lzo,zstd]" ,"compress the file while defragmenting, optional parameter (no space in between)" ), +OPTLINE ("-L|--level level" ,"use given compression level if enabled (zlib supports levels 1-9, zstd -15-15, and 0 selects the default level)" ),OPTLINE ("-f" ,"flush data to disk immediately after defragmenting" ),OPTLINE ("-s start" ,"defragment only from byte onward" ), OPTLINE("-l len", "defragment only up to len bytes"),@@ -1066,6 +1067,7 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,bool recursive = false;int ret =0 ;int compress_type =BTRFS_COMPRESS_NONE ; +int compress_level =0 ;/* * Kernel 4.19+ supports defragmention of files open read-only, @@ -1095,18 +1097,18 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,if (bconf.verbose !=BTRFS_BCONF_UNSET ) bconf.verbose++; - defrag_global_errors =0 ; defrag_global_errors =0 ; optind =0 ;while (1 ) {enum {GETOPT_VAL_STEP =GETOPT_VAL_FIRST };static const struct option long_options[] = { + {"level" , required_argument,NULL ,'L' }, {"step" , required_argument,NULL ,GETOPT_VAL_STEP }, {NULL ,0 ,NULL ,0 } };int c; - c =getopt_long (argc, argv,"vrc::fs:l:t:" , long_options,NULL ); + c =getopt_long (argc, argv,"vrc::L:fs:l:t:" , long_options,NULL );if (c <0 )break ;@@ -1116,6 +1118,18 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,if (optarg) compress_type =parse_compress_type_arg (optarg);break ; +case 'L' : +/* +* Do not enforce any limits here, kernel will do itself +* based on what's supported by the running version. +* Just clip to the s8 type of the API. +*/ + compress_level =atoi (optarg); +if (compress_level <-128 ) + compress_level =-128 ; +else if (compress_level >127 ) + compress_level =127 ; +break ;case 'f' : flush = true;break ;@@ -1165,7 +1179,12 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd, defrag_global_range.extent_thresh = (u32 )thresh;if (compress_type) { defrag_global_range.flags |=BTRFS_DEFRAG_RANGE_COMPRESS ;
Quoted 8 lines
cmds/filesystem.c static int cmd_filesystem_defrag(const struct cmd_struct *cmd, · line 1183
Quoted 3 lines
cmds/filesystem.c +2
-1- defrag_global_range.compress_type = compress_type; +if (compress_level) { + defrag_global_range.flags |=BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL ;Since it's a newer flag, the
ioctl()may fail with-EOPNOTSUPPon older kernels, in that case it's better to fall back to flags without the newCOMPRESS_LEVELone, and try it again. (With some warning showing that the kernel doesn't support the new flag).
Well, we don't know if the user really wants to proceed without the specified level. This way the defrag fails and the user can rerun without that option if he's fine using the default level.
Ie. Do we really want to do something different than what was asked on the command line? Let me know and I'll change that but this looks rigorous to me.
Quoted 81 lines
Thanks,
QuQuoted 77 lines
cmds/filesystem.c +4
-0+ defrag_global_range.compress.type = compress_type; + defrag_global_range.compress.level= compress_level; + }else + defrag_global_range.compress_type = compress_type; }if (flush) defrag_global_range.flags |=BTRFS_DEFRAG_RANGE_START_IO ;kernel-shared/uapi/btrfs.h +9
-1@@ -645,7 +645,9 @@ _static_assert(sizeof(struct btrfs_ioctl_clone_range_args) == 32);*/ #define BTRFS_DEFRAG_RANGE_COMPRESS 1#define BTRFS_DEFRAG_RANGE_START_IO 2 +#define BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL 4#define BTRFS_DEFRAG_RANGE_FLAGS_SUPP (BTRFS_DEFRAG_RANGE_COMPRESS | \ + BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL | \ BTRFS_DEFRAG_RANGE_START_IO)struct btrfs_ioctl_defrag_range_args {@@ -673,7 +675,13 @@ struct btrfs_ioctl_defrag_range_args {* for this defrag operation. If unspecified, zlib will * be used */ -__u32 compress_type; +union { +__u32 compress_type; +struct { +__u8 type; +__s8 level; + } compress; + };/* spare for later */ __u32 unused[4 ];libbtrfs/ioctl.h +1
-0@@ -398,6 +398,7 @@ struct btrfs_ioctl_clone_range_args {/* flags for the defrag range ioctl */ #define BTRFS_DEFRAG_RANGE_COMPRESS 1#define BTRFS_DEFRAG_RANGE_START_IO 2 +#define BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL 4#define BTRFS_SAME_DATA_DIFFERS 1/* For extent-same ioctl */ libbtrfsutil/btrfs.h +9
-1@@ -608,7 +608,9 @@ struct btrfs_ioctl_clone_range_args {*/ #define BTRFS_DEFRAG_RANGE_COMPRESS 1#define BTRFS_DEFRAG_RANGE_START_IO 2 +#define BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL 4#define BTRFS_DEFRAG_RANGE_FLAGS_SUPP (BTRFS_DEFRAG_RANGE_COMPRESS | \ + BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL | \ BTRFS_DEFRAG_RANGE_START_IO)struct btrfs_ioctl_defrag_range_args {@@ -636,7 +638,13 @@ struct btrfs_ioctl_defrag_range_args {* for this defrag operation. If unspecified, zlib will * be used */ -__u32 compress_type; +union { +__u32 compress_type; +struct { +__u8 type; +__s8 level; + } compress; + };/* spare for later */ __u32 unused[4 ];
Qu Wenruo
Reviewed-byQuoted 1 line · Daniel Vacek
On Tue, 4 Mar 2025 at 22:40, Qu Wenruo <[email protected]> wrote:
[...]
Quoted 10 lines
Quoted 9 lines
cmds/filesystem.c static int cmd_filesystem_defrag(const struct cmd_struct *cmd, · line 1183
Quoted 4 lines
defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;cmds/filesystem.c +2
-1- defrag_global_range.compress_type = compress_type; +if (compress_level) { + defrag_global_range.flags |=BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL ;Since it's a newer flag, the
ioctl()may fail with-EOPNOTSUPPon older kernels, in that case it's better to fall back to flags without the newCOMPRESS_LEVELone, and try it again. (With some warning showing that the kernel doesn't support the new flag).
Quoted 7 lines
Well, we don't know if the user really wants to proceed without the specified level. This way the defrag fails and the user can rerun without that option if he's fine using the default level.
Ie. Do we really want to do something different than what was asked on the command line? Let me know and I'll change that but this looks rigorous to me.
Right, since the level can only be enabled by explicitly specifying the --level option, we should not fallback.
With the doc update split out:
Thanks,
Qu
Quoted 83 lines
Quoted 82 lines
Thanks,
QuQuoted 77 lines
cmds/filesystem.c +4
-0+ defrag_global_range.compress.type = compress_type; + defrag_global_range.compress.level= compress_level; + }else + defrag_global_range.compress_type = compress_type; }if (flush) defrag_global_range.flags |=BTRFS_DEFRAG_RANGE_START_IO ;kernel-shared/uapi/btrfs.h +9
-1@@ -645,7 +645,9 @@ _static_assert(sizeof(struct btrfs_ioctl_clone_range_args) == 32);*/ #define BTRFS_DEFRAG_RANGE_COMPRESS 1#define BTRFS_DEFRAG_RANGE_START_IO 2 +#define BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL 4#define BTRFS_DEFRAG_RANGE_FLAGS_SUPP (BTRFS_DEFRAG_RANGE_COMPRESS | \ + BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL | \ BTRFS_DEFRAG_RANGE_START_IO)struct btrfs_ioctl_defrag_range_args {@@ -673,7 +675,13 @@ struct btrfs_ioctl_defrag_range_args {* for this defrag operation. If unspecified, zlib will * be used */ -__u32 compress_type; +union { +__u32 compress_type; +struct { +__u8 type; +__s8 level; + } compress; + };/* spare for later */ __u32 unused[4 ];libbtrfs/ioctl.h +1
-0@@ -398,6 +398,7 @@ struct btrfs_ioctl_clone_range_args {/* flags for the defrag range ioctl */ #define BTRFS_DEFRAG_RANGE_COMPRESS 1#define BTRFS_DEFRAG_RANGE_START_IO 2 +#define BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL 4#define BTRFS_SAME_DATA_DIFFERS 1/* For extent-same ioctl */ libbtrfsutil/btrfs.h +9
-1@@ -608,7 +608,9 @@ struct btrfs_ioctl_clone_range_args {*/ #define BTRFS_DEFRAG_RANGE_COMPRESS 1#define BTRFS_DEFRAG_RANGE_START_IO 2 +#define BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL 4#define BTRFS_DEFRAG_RANGE_FLAGS_SUPP (BTRFS_DEFRAG_RANGE_COMPRESS | \ + BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL | \ BTRFS_DEFRAG_RANGE_START_IO)struct btrfs_ioctl_defrag_range_args {@@ -636,7 +638,13 @@ struct btrfs_ioctl_defrag_range_args {* for this defrag operation. If unspecified, zlib will * be used */ -__u32 compress_type; +union { +__u32 compress_type; +struct { +__u8 type; +__s8 level; + } compress; + };/* spare for later */ __u32 unused[4 ];
Quoted 15 lines · Qu Wenruo
Quoted 12 lines · Daniel Vacek
zlib and zstd compression methods support using compression levels. Enable defrag to pass them to kernel.
Signed-off-by Daniel Vacek <[email protected]>--- Documentation/ch-compression.rst | 10 +++--- cmds/filesystem.c | 55 ++++++++++++++++++++++++++++++-- kernel-shared/uapi/btrfs.h | 10 +++++- libbtrfs/ioctl.h | 1 + libbtrfsutil/btrfs.h | 12 +++++-- 5 files changed, 77 insertions(+), 11 deletions(-)
Quoted 15 lines
Documentation/ch-compression.rst LZO · line 29
Quoted 12 lines
Documentation/ch-compression.rst +2
-2@@ -25,8 +25,8 @@ LZO * good backward compatibility ZSTD * compression comparable to ZLIB with higher compression/decompression speeds and different ratio - * levels: 1 to 15, mapped directly (higher levels are not available) - * since 4.14, levels since 5.1 + * levels: -15 to 15, mapped directly, default is 3 + * since 4.14, levels 1 to 15 since 5.1, -15 to -1 since 6.15I believe the doc updates can be a separate patch since it's not related to the new defrag ioctl flag.
In progs I don't mind either way, included in the functional change or separate.
Quoted 33 lines · David Sterba
Quoted 15 lines · Qu Wenruo
Quoted 12 lines · Daniel Vacek
zlib and zstd compression methods support using compression levels. Enable defrag to pass them to kernel.
Signed-off-by Daniel Vacek <[email protected]>--- Documentation/ch-compression.rst | 10 +++--- cmds/filesystem.c | 55 ++++++++++++++++++++++++++++++-- kernel-shared/uapi/btrfs.h | 10 +++++- libbtrfs/ioctl.h | 1 + libbtrfsutil/btrfs.h | 12 +++++-- 5 files changed, 77 insertions(+), 11 deletions(-)Quoted 15 lines
Documentation/ch-compression.rst LZO · line 29
Quoted 12 lines
Documentation/ch-compression.rst +2
-2@@ -25,8 +25,8 @@ LZO * good backward compatibility ZSTD * compression comparable to ZLIB with higher compression/decompression speeds and different ratio - * levels: 1 to 15, mapped directly (higher levels are not available) - * since 4.14, levels since 5.1 + * levels: -15 to 15, mapped directly, default is 3 + * since 4.14, levels 1 to 15 since 5.1, -15 to -1 since 6.15I believe the doc updates can be a separate patch since it's not related to the new defrag ioctl flag.
Quoted 2 lines
In progs I don't mind either way, included in the functional change or separate.
Next time I'll split them. But if you're happy with it as it is, there's no need for v2 I guess.
Quoted 4 lines · Daniel Vacek
zlib and zstd compression methods support using compression levels. Enable defrag to pass them to kernel.
Signed-off-by Daniel Vacek <[email protected]>
Added to devel, with some adjustments, thanks. We'll also need a test, that can fail if the kernel does not support the levels. The github CI is 6.8 so we won't be able to test it there completely.
cmds/filesystem.c static int cmd_filesystem_defrag(const struct cmd_struct *cmd, · line 1125
Quoted 5 lines
cmds/filesystem.c +5
-0+case 'L' : +/* +* Do not enforce any limits here, kernel will do itself +* based on what's supported by the running version. +* Just clip to the s8 type of the API.
I'm not sure this is a good practice, we know what are the level ranges so the validation should be done here as well even if kernel will check them as well. I'll keep it like that for now but may change it eventually.
Quoted 11 lines
cmds/filesystem.c +7
-0+*/ + compress_level =atoi (optarg); +if (compress_level <-128 ) + compress_level =-128 ; +else if (compress_level >127 ) + compress_level =127 ; +break ;
libbtrfs/ioctl.h struct btrfs_ioctl_clone_range_args { · line 401
Quoted 4 lines
libbtrfs/ioctl.h +1
-0@@ -398,6 +398,7 @@ struct btrfs_ioctl_clone_range_args {/* flags for the defrag range ioctl */ #define BTRFS_DEFRAG_RANGE_COMPRESS 1#define BTRFS_DEFRAG_RANGE_START_IO 2 +#define BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL 4
I've removed this, libbtrfs is frozen, deprecated and should not be used, so there are no feature/API updates.
Quoted 2 lines
libbtrfs/ioctl.h +0
-0#define BTRFS_SAME_DATA_DIFFERS 1/* For extent-same ioctl */