Commit f51d2b59 authored by David Sterba's avatar David Sterba

btrfs: allow to set compression level for zlib

Preliminary support for setting compression level for zlib, the
following works:

$ mount -o compess=zlib                 # default
$ mount -o compess=zlib0                # same
$ mount -o compess=zlib9                # level 9, slower sync, less data
$ mount -o compess=zlib1                # level 1, faster sync, more data
$ mount -o remount,compress=zlib3	# level set by remount

The compress-force works the same as compress'.  The level is visible in
the same format in /proc/mounts. Level set via file property does not
work yet.

Required patch: "btrfs: prepare for extensions in compression options"
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent d4417e22
...@@ -884,6 +884,11 @@ static void free_workspaces(void) ...@@ -884,6 +884,11 @@ static void free_workspaces(void)
* Given an address space and start and length, compress the bytes into @pages * Given an address space and start and length, compress the bytes into @pages
* that are allocated on demand. * that are allocated on demand.
* *
* @type_level is encoded algorithm and level, where level 0 means whatever
* default the algorithm chooses and is opaque here;
* - compression algo are 0-3
* - the level are bits 4-7
*
* @out_pages is an in/out parameter, holds maximum number of pages to allocate * @out_pages is an in/out parameter, holds maximum number of pages to allocate
* and returns number of actually allocated pages * and returns number of actually allocated pages
* *
...@@ -898,7 +903,7 @@ static void free_workspaces(void) ...@@ -898,7 +903,7 @@ static void free_workspaces(void)
* @max_out tells us the max number of bytes that we're allowed to * @max_out tells us the max number of bytes that we're allowed to
* stuff into pages * stuff into pages
*/ */
int btrfs_compress_pages(int type, struct address_space *mapping, int btrfs_compress_pages(unsigned int type_level, struct address_space *mapping,
u64 start, struct page **pages, u64 start, struct page **pages,
unsigned long *out_pages, unsigned long *out_pages,
unsigned long *total_in, unsigned long *total_in,
...@@ -906,9 +911,11 @@ int btrfs_compress_pages(int type, struct address_space *mapping, ...@@ -906,9 +911,11 @@ int btrfs_compress_pages(int type, struct address_space *mapping,
{ {
struct list_head *workspace; struct list_head *workspace;
int ret; int ret;
int type = type_level & 0xF;
workspace = find_workspace(type); workspace = find_workspace(type);
btrfs_compress_op[type - 1]->set_level(workspace, type_level);
ret = btrfs_compress_op[type-1]->compress_pages(workspace, mapping, ret = btrfs_compress_op[type-1]->compress_pages(workspace, mapping,
start, pages, start, pages,
out_pages, out_pages,
...@@ -1098,3 +1105,14 @@ int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end) ...@@ -1098,3 +1105,14 @@ int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end)
return ret; return ret;
} }
unsigned int btrfs_compress_str2level(const char *str)
{
if (strncmp(str, "zlib", 4) != 0)
return 0;
if ('1' <= str[4] && str[4] <= '9' )
return str[4] - '0';
return 0;
}
...@@ -76,7 +76,7 @@ struct compressed_bio { ...@@ -76,7 +76,7 @@ struct compressed_bio {
void btrfs_init_compress(void); void btrfs_init_compress(void);
void btrfs_exit_compress(void); void btrfs_exit_compress(void);
int btrfs_compress_pages(int type, struct address_space *mapping, int btrfs_compress_pages(unsigned int type_level, struct address_space *mapping,
u64 start, struct page **pages, u64 start, struct page **pages,
unsigned long *out_pages, unsigned long *out_pages,
unsigned long *total_in, unsigned long *total_in,
...@@ -95,6 +95,8 @@ blk_status_t btrfs_submit_compressed_write(struct inode *inode, u64 start, ...@@ -95,6 +95,8 @@ blk_status_t btrfs_submit_compressed_write(struct inode *inode, u64 start,
blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio, blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
int mirror_num, unsigned long bio_flags); int mirror_num, unsigned long bio_flags);
unsigned btrfs_compress_str2level(const char *str);
enum btrfs_compression_type { enum btrfs_compression_type {
BTRFS_COMPRESS_NONE = 0, BTRFS_COMPRESS_NONE = 0,
BTRFS_COMPRESS_ZLIB = 1, BTRFS_COMPRESS_ZLIB = 1,
...@@ -124,6 +126,8 @@ struct btrfs_compress_op { ...@@ -124,6 +126,8 @@ struct btrfs_compress_op {
struct page *dest_page, struct page *dest_page,
unsigned long start_byte, unsigned long start_byte,
size_t srclen, size_t destlen); size_t srclen, size_t destlen);
void (*set_level)(struct list_head *ws, unsigned int type);
}; };
extern const struct btrfs_compress_op btrfs_zlib_compress; extern const struct btrfs_compress_op btrfs_zlib_compress;
......
...@@ -790,6 +790,7 @@ struct btrfs_fs_info { ...@@ -790,6 +790,7 @@ struct btrfs_fs_info {
*/ */
unsigned long pending_changes; unsigned long pending_changes;
unsigned long compress_type:4; unsigned long compress_type:4;
unsigned int compress_level;
int commit_interval; int commit_interval;
/* /*
* It is a suggestive number, the read side is safe even it gets a * It is a suggestive number, the read side is safe even it gets a
......
...@@ -539,7 +539,10 @@ static noinline void compress_file_range(struct inode *inode, ...@@ -539,7 +539,10 @@ static noinline void compress_file_range(struct inode *inode,
*/ */
extent_range_clear_dirty_for_io(inode, start, end); extent_range_clear_dirty_for_io(inode, start, end);
redirty = 1; redirty = 1;
ret = btrfs_compress_pages(compress_type,
/* Compression level is applied here and only here */
ret = btrfs_compress_pages(
compress_type | (fs_info->compress_level << 4),
inode->i_mapping, start, inode->i_mapping, start,
pages, pages,
&nr_pages, &nr_pages,
......
...@@ -430,10 +430,15 @@ static int lzo_decompress(struct list_head *ws, unsigned char *data_in, ...@@ -430,10 +430,15 @@ static int lzo_decompress(struct list_head *ws, unsigned char *data_in,
return ret; return ret;
} }
static void lzo_set_level(struct list_head *ws, unsigned int type)
{
}
const struct btrfs_compress_op btrfs_lzo_compress = { const struct btrfs_compress_op btrfs_lzo_compress = {
.alloc_workspace = lzo_alloc_workspace, .alloc_workspace = lzo_alloc_workspace,
.free_workspace = lzo_free_workspace, .free_workspace = lzo_free_workspace,
.compress_pages = lzo_compress_pages, .compress_pages = lzo_compress_pages,
.decompress_bio = lzo_decompress_bio, .decompress_bio = lzo_decompress_bio,
.decompress = lzo_decompress, .decompress = lzo_decompress,
.set_level = lzo_set_level,
}; };
...@@ -508,6 +508,8 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options, ...@@ -508,6 +508,8 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
strncmp(args[0].from, "zlib", 4) == 0) { strncmp(args[0].from, "zlib", 4) == 0) {
compress_type = "zlib"; compress_type = "zlib";
info->compress_type = BTRFS_COMPRESS_ZLIB; info->compress_type = BTRFS_COMPRESS_ZLIB;
info->compress_level =
btrfs_compress_str2level(args[0].from);
btrfs_set_opt(info->mount_opt, COMPRESS); btrfs_set_opt(info->mount_opt, COMPRESS);
btrfs_clear_opt(info->mount_opt, NODATACOW); btrfs_clear_opt(info->mount_opt, NODATACOW);
btrfs_clear_opt(info->mount_opt, NODATASUM); btrfs_clear_opt(info->mount_opt, NODATASUM);
...@@ -555,9 +557,9 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options, ...@@ -555,9 +557,9 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
compress_force != saved_compress_force)) || compress_force != saved_compress_force)) ||
(!btrfs_test_opt(info, COMPRESS) && (!btrfs_test_opt(info, COMPRESS) &&
no_compress == 1)) { no_compress == 1)) {
btrfs_info(info, "%s %s compression", btrfs_info(info, "%s %s compression, level %d",
(compress_force) ? "force" : "use", (compress_force) ? "force" : "use",
compress_type); compress_type, info->compress_level);
} }
compress_force = false; compress_force = false;
break; break;
...@@ -1258,6 +1260,8 @@ static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) ...@@ -1258,6 +1260,8 @@ static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
seq_printf(seq, ",compress-force=%s", compress_type); seq_printf(seq, ",compress-force=%s", compress_type);
else else
seq_printf(seq, ",compress=%s", compress_type); seq_printf(seq, ",compress=%s", compress_type);
if (info->compress_level)
seq_printf(seq, "%d", info->compress_level);
} }
if (btrfs_test_opt(info, NOSSD)) if (btrfs_test_opt(info, NOSSD))
seq_puts(seq, ",nossd"); seq_puts(seq, ",nossd");
......
...@@ -37,6 +37,7 @@ struct workspace { ...@@ -37,6 +37,7 @@ struct workspace {
z_stream strm; z_stream strm;
char *buf; char *buf;
struct list_head list; struct list_head list;
int level;
}; };
static void zlib_free_workspace(struct list_head *ws) static void zlib_free_workspace(struct list_head *ws)
...@@ -96,7 +97,7 @@ static int zlib_compress_pages(struct list_head *ws, ...@@ -96,7 +97,7 @@ static int zlib_compress_pages(struct list_head *ws,
*total_out = 0; *total_out = 0;
*total_in = 0; *total_in = 0;
if (Z_OK != zlib_deflateInit(&workspace->strm, 3)) { if (Z_OK != zlib_deflateInit(&workspace->strm, workspace->level)) {
pr_warn("BTRFS: deflateInit failed\n"); pr_warn("BTRFS: deflateInit failed\n");
ret = -EIO; ret = -EIO;
goto out; goto out;
...@@ -402,10 +403,22 @@ static int zlib_decompress(struct list_head *ws, unsigned char *data_in, ...@@ -402,10 +403,22 @@ static int zlib_decompress(struct list_head *ws, unsigned char *data_in,
return ret; return ret;
} }
static void zlib_set_level(struct list_head *ws, unsigned int type)
{
struct workspace *workspace = list_entry(ws, struct workspace, list);
unsigned level = (type & 0xF0) >> 4;
if (level > 9)
level = 9;
workspace->level = level > 0 ? level : 3;
}
const struct btrfs_compress_op btrfs_zlib_compress = { const struct btrfs_compress_op btrfs_zlib_compress = {
.alloc_workspace = zlib_alloc_workspace, .alloc_workspace = zlib_alloc_workspace,
.free_workspace = zlib_free_workspace, .free_workspace = zlib_free_workspace,
.compress_pages = zlib_compress_pages, .compress_pages = zlib_compress_pages,
.decompress_bio = zlib_decompress_bio, .decompress_bio = zlib_decompress_bio,
.decompress = zlib_decompress, .decompress = zlib_decompress,
.set_level = zlib_set_level,
}; };
...@@ -423,10 +423,15 @@ static int zstd_decompress(struct list_head *ws, unsigned char *data_in, ...@@ -423,10 +423,15 @@ static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
return ret; return ret;
} }
static void zstd_set_level(struct list_head *ws, unsigned int type)
{
}
const struct btrfs_compress_op btrfs_zstd_compress = { const struct btrfs_compress_op btrfs_zstd_compress = {
.alloc_workspace = zstd_alloc_workspace, .alloc_workspace = zstd_alloc_workspace,
.free_workspace = zstd_free_workspace, .free_workspace = zstd_free_workspace,
.compress_pages = zstd_compress_pages, .compress_pages = zstd_compress_pages,
.decompress_bio = zstd_decompress_bio, .decompress_bio = zstd_decompress_bio,
.decompress = zstd_decompress, .decompress = zstd_decompress,
.set_level = zstd_set_level,
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment