Skip to content

Commit 140b568

Browse files
maharmstoneadam900710
authored andcommitted
btrfs-progs: mkfs: add --compress option
Add an option --compress to mkfs.btrfs, to allow creating files using zlib when using --rootdir. Signed-off-by: Mark Harmstone <maharmstone@fb.com>
1 parent ae87b25 commit 140b568

File tree

11 files changed

+337
-31
lines changed

11 files changed

+337
-31
lines changed

Documentation/mkfs.btrfs.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ OPTIONS
212212
213213
$ mkfs.btrfs -O list-all
214214
215+
--compress <algo>[:<level>]
216+
Try to compress files when using *--rootdir*. Supported values for *algo* are
217+
*no* (the default) and *zlib*. The optional value *level* is a
218+
compression level, from 1 to 9 for ZLIB.
219+
220+
As with the kernel, :command:`mkfs.btrfs` won't write compressed extents when
221+
they would be larger than the uncompressed versions, and will mark a file as
222+
`nocompress` if its beginning is found to be incompressible.
223+
215224
-f|--force
216225
Forcibly overwrite the block devices when an existing filesystem is detected.
217226
By default, :command:`mkfs.btrfs` will utilize *libblkid* to check for any known

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,11 +740,11 @@ btrfsck.static: btrfs.static
740740

741741
mkfs.btrfs: $(mkfs_objects) $(objects) libbtrfsutil.a
742742
@echo " [LD] $@"
743-
$(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS)
743+
$(Q)$(CC) -o $@ $^ $(LDFLAGS) $(LIBS) $(LIBS_COMP)
744744

745745
mkfs.btrfs.static: $(static_mkfs_objects) $(static_objects) $(static_libbtrfs_objects)
746746
@echo " [LD] $@"
747-
$(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS)
747+
$(Q)$(CC) -o $@ $^ $(STATIC_LDFLAGS) $(STATIC_LIBS) $(STATIC_LIBS_COMP)
748748

749749
btrfstune: $(tune_objects) $(objects) libbtrfsutil.a
750750
@echo " [LD] $@"

common/extent-tree-utils.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "kernel-shared/uapi/btrfs_tree.h"
2121
#include "kernel-shared/ctree.h"
2222
#include "kernel-shared/disk-io.h"
23+
#include "kernel-shared/free-space-tree.h"
2324
#include "kernel-shared/transaction.h"
2425
#include "common/extent-tree-utils.h"
2526

common/extent-tree-utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "kerncompat.h"
2121
#include "kernel-lib/bitops.h"
22+
#include "kernel-shared/compression.h"
2223

2324
struct btrfs_inode_item;
2425
struct btrfs_path;

convert/source-ext2.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,9 @@ static int ext2_create_file_extents(struct btrfs_trans_handle *trans,
457457
if (num_bytes > inode_size)
458458
num_bytes = inode_size;
459459
ret = btrfs_insert_inline_extent(trans, root, objectid,
460-
0, buffer, num_bytes);
460+
0, buffer, num_bytes,
461+
BTRFS_COMPRESS_NONE,
462+
num_bytes);
461463
if (ret)
462464
goto fail;
463465
nbytes = btrfs_stack_inode_nbytes(btrfs_inode) + num_bytes;
@@ -506,7 +508,8 @@ static int ext2_create_symlink(struct btrfs_trans_handle *trans,
506508
pathname = (char *)&(ext2_inode->i_block[0]);
507509
BUG_ON(pathname[inode_size] != 0);
508510
ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
509-
pathname, inode_size);
511+
pathname, inode_size,
512+
BTRFS_COMPRESS_NONE, inode_size);
510513
btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size);
511514
return ret;
512515
}

convert/source-reiserfs.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,8 @@ static int reiserfs_convert_tail(struct btrfs_trans_handle *trans,
384384
length, offset, convert_flags);
385385

386386
ret = btrfs_insert_inline_extent(trans, root, objectid,
387-
offset, body, length);
387+
offset, body, length,
388+
BTRFS_COMPRESS_NONE, length);
388389
if (ret)
389390
return ret;
390391

@@ -544,7 +545,8 @@ static int reiserfs_copy_symlink(struct btrfs_trans_handle *trans,
544545
goto fail;
545546
}
546547
ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
547-
symlink, len);
548+
symlink, len, BTRFS_COMPRESS_NONE,
549+
len);
548550
btrfs_set_stack_inode_nbytes(btrfs_inode, len);
549551
fail:
550552
pathrelse(&path);

kernel-shared/file-item.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
6363

6464
int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans,
6565
struct btrfs_root *root, u64 objectid,
66-
u64 offset, const char *buffer, size_t size)
66+
u64 offset, const char *buffer, size_t size,
67+
enum btrfs_compression_type comp,
68+
u64 ram_bytes)
6769
{
6870
struct btrfs_fs_info *fs_info = trans->fs_info;
6971
struct btrfs_key key;
@@ -99,8 +101,8 @@ int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans,
99101
struct btrfs_file_extent_item);
100102
btrfs_set_file_extent_generation(leaf, ei, trans->transid);
101103
btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
102-
btrfs_set_file_extent_ram_bytes(leaf, ei, size);
103-
btrfs_set_file_extent_compression(leaf, ei, 0);
104+
btrfs_set_file_extent_ram_bytes(leaf, ei, ram_bytes);
105+
btrfs_set_file_extent_compression(leaf, ei, comp);
104106
btrfs_set_file_extent_encryption(leaf, ei, 0);
105107
btrfs_set_file_extent_other_encoding(leaf, ei, 0);
106108

kernel-shared/file-item.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "kernel-shared/ctree.h"
1111
#include "kernel-shared/uapi/btrfs_tree.h"
1212
#include "kernel-shared/accessors.h"
13+
#include "kernel-shared/compression.h"
1314

1415
struct bio;
1516
struct inode;
@@ -91,7 +92,8 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans, u64 logical,
9192
u64 csum_objectid, u32 csum_type, const char *data);
9293
int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans,
9394
struct btrfs_root *root, u64 objectid,
94-
u64 offset, const char *buffer, size_t size);
95+
u64 offset, const char *buffer, size_t size,
96+
enum btrfs_compression_type comp, u64 ram_bytes);
9597
/*
9698
* For symlink we allow up to PATH_MAX - 1 (PATH_MAX includes the terminating NUL,
9799
* but fs doesn't store that terminating NUL).

mkfs/main.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ static const char * const mkfs_usage[] = {
443443
OPTLINE("-u|--subvol TYPE:SUBDIR", "create SUBDIR as subvolume rather than normal directory, can be specified multiple times"),
444444
OPTLINE("--shrink", "(with --rootdir) shrink the filled filesystem to minimal size"),
445445
OPTLINE("-K|--nodiscard", "do not perform whole device TRIM"),
446+
OPTLINE("--compress ALGO[:LEVEL]", "compression algorithm and level to use; ALGO can be no (default), zlib"),
446447
OPTLINE("-f|--force", "force overwrite of existing filesystem"),
447448
"General:",
448449
OPTLINE("-q|--quiet", "no messages except errors"),
@@ -1058,6 +1059,8 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
10581059
char *source_dir = NULL;
10591060
struct rootdir_subvol *rds;
10601061
bool has_default_subvol = false;
1062+
enum btrfs_compression_type compression = BTRFS_COMPRESS_NONE;
1063+
u64 compression_level = 0;
10611064
LIST_HEAD(subvols);
10621065

10631066
cpu_detect_flags();
@@ -1072,6 +1075,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
10721075
GETOPT_VAL_CHECKSUM,
10731076
GETOPT_VAL_GLOBAL_ROOTS,
10741077
GETOPT_VAL_DEVICE_UUID,
1078+
GETOPT_VAL_COMPRESS,
10751079
};
10761080
static const struct option long_options[] = {
10771081
{ "byte-count", required_argument, NULL, 'b' },
@@ -1099,6 +1103,8 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
10991103
{ "quiet", 0, NULL, 'q' },
11001104
{ "verbose", 0, NULL, 'v' },
11011105
{ "shrink", no_argument, NULL, GETOPT_VAL_SHRINK },
1106+
{ "compress", required_argument, NULL,
1107+
GETOPT_VAL_COMPRESS },
11021108
#if EXPERIMENTAL
11031109
{ "param", required_argument, NULL, GETOPT_VAL_PARAM },
11041110
{ "num-global-roots", required_argument, NULL, GETOPT_VAL_GLOBAL_ROOTS },
@@ -1272,6 +1278,38 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
12721278
case 'q':
12731279
bconf_be_quiet();
12741280
break;
1281+
case GETOPT_VAL_COMPRESS: {
1282+
char *colon;
1283+
size_t type_size;
1284+
1285+
if (!strcmp(optarg, "no")) {
1286+
compression = BTRFS_COMPRESS_NONE;
1287+
break;
1288+
}
1289+
1290+
colon = strstr(optarg, ":");
1291+
1292+
if (colon)
1293+
type_size = colon - optarg;
1294+
else
1295+
type_size = strlen(optarg);
1296+
1297+
if (!strncmp(optarg, "zlib", type_size)) {
1298+
compression = BTRFS_COMPRESS_ZLIB;
1299+
} else {
1300+
error("unrecognized compression type %s",
1301+
optarg);
1302+
ret = 1;
1303+
goto error;
1304+
}
1305+
1306+
if (colon)
1307+
compression_level = arg_strtou64(colon + 1);
1308+
else
1309+
compression_level = 0;
1310+
1311+
break;
1312+
}
12751313
case GETOPT_VAL_DEVICE_UUID:
12761314
strncpy_null(dev_uuid, optarg, BTRFS_UUID_UNPARSED_SIZE);
12771315
break;
@@ -1953,7 +1991,8 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
19531991
}
19541992

19551993
ret = btrfs_mkfs_fill_dir(trans, source_dir, root,
1956-
&subvols);
1994+
&subvols, compression,
1995+
compression_level);
19571996
if (ret) {
19581997
error("error while filling filesystem: %d", ret);
19591998
btrfs_abort_transaction(trans, ret);

0 commit comments

Comments
 (0)