Skip to content

Commit bb6a159

Browse files
committed
btrfs-progs: defrag: new option --nocomp to do no-compression defrag
Add new option --nocomp to set flag which will tell kernel to defragment file extents without compression and decompress existing extents if needed. The defrag setting will override any current compression settings like mount options or file properties. The option is separate from '-c' so it's more obvious it's mutually exclusive. Signed-off-by: David Sterba <[email protected]>
1 parent ce5ac84 commit bb6a159

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

Documentation/btrfs-filesystem.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ defragment [options] <file>|<dir> [<file>|<dir>...]
124124
1..9, *lzo* does not have any levels, *zstd* the standard levels 1..15 and also the
125125
realtime -1..-15.
126126

127+
--nocomp
128+
Do not compress while defragmenting, uncompress extents if needed.
127129
-r
128130
defragment files recursively in given directories, does not descend to
129131
subvolumes or mount points

cmds/filesystem.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,7 @@ static const char * const cmd_filesystem_defrag_usage[] = {
948948
OPTLINE("-r", "defragment files recursively"),
949949
OPTLINE("-c[zlib,lzo,zstd]", "compress the file while defragmenting, optional parameter (no space in between)"),
950950
OPTLINE("-L|--level level", "use given compression level if enabled (zlib: 1..9, zstd: -15..15, and 0 selects the default level)"),
951+
OPTLINE("--nocomp", "don't compress while defragmenting (uncompress if needed)"),
951952
OPTLINE("-f", "flush data to disk immediately after defragmenting"),
952953
OPTLINE("-s start", "defragment only from byte onward"),
953954
OPTLINE("-l len", "defragment only up to len bytes"),
@@ -1053,6 +1054,7 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,
10531054
int ret = 0;
10541055
int compress_type = BTRFS_COMPRESS_NONE;
10551056
int compress_level = 0;
1057+
bool opt_nocomp = false;
10561058

10571059
/*
10581060
* Kernel 4.19+ supports defragmention of files open read-only,
@@ -1085,10 +1087,11 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,
10851087
defrag_global_errors = 0;
10861088
optind = 0;
10871089
while(1) {
1088-
enum { GETOPT_VAL_STEP = GETOPT_VAL_FIRST };
1090+
enum { GETOPT_VAL_STEP = GETOPT_VAL_FIRST, GETOPT_VAL_NOCOMP };
10891091
static const struct option long_options[] = {
10901092
{ "level", required_argument, NULL, 'L' },
10911093
{ "step", required_argument, NULL, GETOPT_VAL_STEP },
1094+
{ "nocomp", no_argument, NULL, GETOPT_VAL_NOCOMP },
10921095
{ NULL, 0, NULL, 0 }
10931096
};
10941097
int c;
@@ -1099,6 +1102,11 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,
10991102

11001103
switch(c) {
11011104
case 'c':
1105+
if (opt_nocomp) {
1106+
error("cannot use compression with --nocomp");
1107+
return 1;
1108+
}
1109+
11021110
compress_type = BTRFS_COMPRESS_ZLIB;
11031111
if (optarg)
11041112
compress_type = parse_compress_type_arg(optarg);
@@ -1142,6 +1150,13 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,
11421150
case 'r':
11431151
recursive = true;
11441152
break;
1153+
case GETOPT_VAL_NOCOMP:
1154+
if (compress_level != BTRFS_COMPRESS_NONE) {
1155+
error("cannot use --nocomp with compression set");
1156+
return 1;
1157+
}
1158+
opt_nocomp = true;
1159+
break;
11451160
case GETOPT_VAL_STEP:
11461161
defrag_global_step = arg_strtou64_with_suffix(optarg);
11471162
if (defrag_global_step < SZ_256K) {
@@ -1171,6 +1186,8 @@ static int cmd_filesystem_defrag(const struct cmd_struct *cmd,
11711186
} else
11721187
defrag_global_range.compress_type = compress_type;
11731188
}
1189+
if (opt_nocomp)
1190+
defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_NOCOMPRESS;
11741191
if (flush)
11751192
defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
11761193

kernel-shared/uapi/btrfs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,8 +646,10 @@ _static_assert(sizeof(struct btrfs_ioctl_clone_range_args) == 32);
646646
#define BTRFS_DEFRAG_RANGE_COMPRESS 1
647647
#define BTRFS_DEFRAG_RANGE_START_IO 2
648648
#define BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL 4
649+
#define BTRFS_DEFRAG_RANGE_NOCOMPRESS 8
649650
#define BTRFS_DEFRAG_RANGE_FLAGS_SUPP (BTRFS_DEFRAG_RANGE_COMPRESS | \
650651
BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL | \
652+
BTRFS_DEFRAG_RANGE_NOCOMPRESS | \
651653
BTRFS_DEFRAG_RANGE_START_IO)
652654

653655
struct btrfs_ioctl_defrag_range_args {

libbtrfsutil/btrfs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,10 @@ struct btrfs_ioctl_clone_range_args {
609609
#define BTRFS_DEFRAG_RANGE_COMPRESS 1
610610
#define BTRFS_DEFRAG_RANGE_START_IO 2
611611
#define BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL 4
612+
#define BTRFS_DEFRAG_RANGE_NOCOMPRESS 8
612613
#define BTRFS_DEFRAG_RANGE_FLAGS_SUPP (BTRFS_DEFRAG_RANGE_COMPRESS | \
613614
BTRFS_DEFRAG_RANGE_COMPRESS_LEVEL | \
615+
BTRFS_DEFRAG_RANGE_NOCOMPRESS | \
614616
BTRFS_DEFRAG_RANGE_START_IO)
615617

616618
struct btrfs_ioctl_defrag_range_args {

0 commit comments

Comments
 (0)