Skip to content

Commit

Permalink
btrfs: change set_level() to bound the level passed in
Browse files Browse the repository at this point in the history
Currently, the only user of set_level() is zlib which sets an internal
workspace parameter. As level is now plumbed into get_workspace(), this
can be handled there rather than separately.

This repurposes set_level() to bound the level passed in so it can be
used when setting the mounts compression level and as well as verifying
the level before getting a workspace. The other benefit is this divides
the meaning of compress(0) and get_workspace(0). The former means we
want to use the default compression level of the compression type. The
latter means we can use any workspace available.

Signed-off-by: Dennis Zhou <dennis@kernel.org>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
  • Loading branch information
Dennis Zhou authored and David Sterba committed Feb 25, 2019
1 parent 7bf4994 commit d0ab62c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 21 deletions.
25 changes: 17 additions & 8 deletions fs/btrfs/compression.c
Original file line number Diff line number Diff line change
Expand Up @@ -1009,8 +1009,6 @@ int btrfs_compress_pages(unsigned int type_level, struct address_space *mapping,
int ret;

workspace = get_workspace(type, level);

btrfs_compress_op[type]->set_level(workspace, level);
ret = btrfs_compress_op[type]->compress_pages(workspace, mapping,
start, pages,
out_pages,
Expand Down Expand Up @@ -1563,14 +1561,25 @@ int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end)
return ret;
}

unsigned int btrfs_compress_str2level(const char *str)
/*
* Convert the compression suffix (eg. after "zlib" starting with ":") to
* level, unrecognized string will set the default level
*/
unsigned int btrfs_compress_str2level(unsigned int type, const char *str)
{
if (strncmp(str, "zlib", 4) != 0)
unsigned int level = 0;
int ret;

if (!type)
return 0;

/* Accepted form: zlib:1 up to zlib:9 and nothing left after the number */
if (str[4] == ':' && '1' <= str[5] && str[5] <= '9' && str[6] == 0)
return str[5] - '0';
if (str[0] == ':') {
ret = kstrtouint(str + 1, 10, &level);
if (ret)
level = 0;
}

level = btrfs_compress_op[type]->set_level(level);

return BTRFS_ZLIB_DEFAULT_LEVEL;
return level;
}
9 changes: 7 additions & 2 deletions fs/btrfs/compression.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ 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,
int mirror_num, unsigned long bio_flags);

unsigned btrfs_compress_str2level(const char *str);
unsigned int btrfs_compress_str2level(unsigned int type, const char *str);

enum btrfs_compression_type {
BTRFS_COMPRESS_NONE = 0,
Expand Down Expand Up @@ -156,7 +156,12 @@ struct btrfs_compress_op {
unsigned long start_byte,
size_t srclen, size_t destlen);

void (*set_level)(struct list_head *ws, unsigned int type);
/*
* This bounds the level set by the user to be within range of a
* particular compression type. It returns the level that will be used
* if the level is out of bounds or the default if 0 is passed in.
*/
unsigned int (*set_level)(unsigned int level);
};

/* The heuristic workspaces are managed via the 0th workspace manager */
Expand Down
3 changes: 2 additions & 1 deletion fs/btrfs/lzo.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,9 @@ static int lzo_decompress(struct list_head *ws, unsigned char *data_in,
return ret;
}

static void lzo_set_level(struct list_head *ws, unsigned int type)
static unsigned int lzo_set_level(unsigned int level)
{
return 0;
}

const struct btrfs_compress_op btrfs_lzo_compress = {
Expand Down
4 changes: 3 additions & 1 deletion fs/btrfs/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,9 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
if (token != Opt_compress &&
token != Opt_compress_force)
info->compress_level =
btrfs_compress_str2level(args[0].from);
btrfs_compress_str2level(
BTRFS_COMPRESS_ZLIB,
args[0].from + 4);
btrfs_set_opt(info->mount_opt, COMPRESS);
btrfs_clear_opt(info->mount_opt, NODATACOW);
btrfs_clear_opt(info->mount_opt, NODATASUM);
Expand Down
18 changes: 10 additions & 8 deletions fs/btrfs/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ static void zlib_cleanup_workspace_manager(void)

static struct list_head *zlib_get_workspace(unsigned int level)
{
return btrfs_get_workspace(&wsm, level);
struct list_head *ws = btrfs_get_workspace(&wsm, level);
struct workspace *workspace = list_entry(ws, struct workspace, list);

workspace->level = level;

return ws;
}

static void zlib_put_workspace(struct list_head *ws)
Expand Down Expand Up @@ -413,15 +418,12 @@ static int zlib_decompress(struct list_head *ws, unsigned char *data_in,
return ret;
}

static void zlib_set_level(struct list_head *ws, unsigned int type)
static unsigned int zlib_set_level(unsigned int level)
{
struct workspace *workspace = list_entry(ws, struct workspace, list);
unsigned int level = btrfs_compress_level(type);

if (level > 9)
level = 9;
if (!level)
return BTRFS_ZLIB_DEFAULT_LEVEL;

workspace->level = level > 0 ? level : 3;
return min_t(unsigned int, level, 9);
}

const struct btrfs_compress_op btrfs_zlib_compress = {
Expand Down
3 changes: 2 additions & 1 deletion fs/btrfs/zstd.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,9 @@ static int zstd_decompress(struct list_head *ws, unsigned char *data_in,
return ret;
}

static void zstd_set_level(struct list_head *ws, unsigned int type)
static unsigned int zstd_set_level(unsigned int level)
{
return ZSTD_BTRFS_DEFAULT_LEVEL;
}

const struct btrfs_compress_op btrfs_zstd_compress = {
Expand Down

0 comments on commit d0ab62c

Please sign in to comment.