bcachefs: compression workspaces should be indexed by opt, not type
PATCH 1/3 bcachefs: compression workspaces should be indexed by opt, not type
type includes lz4 and lz4_old, which do not get different compression workspaces, and incompressible, a fake type - BCH_COMPRESSION_OPTS() is the correct enum to use.
Signed-off-by Kent Overstreet <[email protected]>
--- fs/bcachefs/bcachefs.h | 2 +- fs/bcachefs/compress.c | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-)
fs/bcachefs/bcachefs.h +1 -1
@@ -982,7 +982,7 @@ struct bch_fs {struct rhashtable promote_table;mempool_t compression_bounce[2 ]; -mempool_t compress_workspace[BCH_COMPRESSION_TYPE_NR ]; +mempool_t compress_workspace[BCH_COMPRESSION_OPT_NR ];mempool_t decompress_workspace;size_t zstd_workspace_size;
fs/bcachefs/compress.c +11 -8
@@ -394,8 +394,11 @@ static unsigned __bio_compress(struct bch_fs *c,unsigned pad;int ret =0 ; -BUG_ON (compression_type >=BCH_COMPRESSION_TYPE_NR ); -BUG_ON (!mempool_initialized (&c->compress_workspace[compression_type])); +/* bch2_compression_decode catches unknown compression types: */ +BUG_ON (compression.type >=BCH_COMPRESSION_OPT_NR ); + +mempool_t *workspace_pool = &c->compress_workspace[compression.type]; +BUG_ON (!mempool_initialized (workspace_pool));/* If it's only one block, don't bother trying to compress: */ if (src->bi_iter.bi_size <= c->opts.block_size)
@@ -404,7 +407,7 @@ static unsigned __bio_compress(struct bch_fs *c, dst_data =bio_map_or_bounce (c, dst,WRITE ); src_data =bio_map_or_bounce (c, src,READ ); - workspace =mempool_alloc (&c->compress_workspace[compression_type],GFP_NOFS ); + workspace =mempool_alloc (workspace_pool,GFP_NOFS ); *src_len = src->bi_iter.bi_size; *dst_len = dst->bi_iter.bi_size;
@@ -447,7 +450,7 @@ static unsigned __bio_compress(struct bch_fs *c, *src_len =round_down (*src_len,block_bytes (c)); } -mempool_free (workspace, &c->compress_workspace[compression_type]); +mempool_free (workspace, workspace_pool);if (ret) goto err;
@@ -576,17 +579,17 @@ static int __bch2_fs_compress_init(struct bch_fs *c, u64 features)struct {unsigned feature; -enum bch_compression_type type; +enum bch_compression_opts type;size_t compress_workspace;size_t decompress_workspace; } compression_types[] = { - { BCH_FEATURE_lz4, BCH_COMPRESSION_TYPE_lz4, + { BCH_FEATURE_lz4, BCH_COMPRESSION_OPT_lz4,max_t (size_t,LZ4_MEM_COMPRESS ,LZ4HC_MEM_COMPRESS ),0 }, - { BCH_FEATURE_gzip, BCH_COMPRESSION_TYPE_gzip, + { BCH_FEATURE_gzip, BCH_COMPRESSION_OPT_gzip,zlib_deflate_workspacesize (MAX_WBITS ,DEF_MEM_LEVEL ),zlib_inflate_workspacesize (), }, - { BCH_FEATURE_zstd, BCH_COMPRESSION_TYPE_zstd, + { BCH_FEATURE_zstd, BCH_COMPRESSION_OPT_zstd, c->zstd_workspace_size,zstd_dctx_workspace_bound () }, }, *i;
Signature · 1 line
2.45.2
PATCH 2/3 bcachefs: Don't use a shared decompress workspace mempool
gzip and zstd require different decompress workspace sizes, and if we start with one and then start using the other at runtime we may not get the correct size
Signed-off-by Kent Overstreet <[email protected]>
--- fs/bcachefs/bcachefs.h | 1 - fs/bcachefs/compress.c | 52 +++++++++++++++++++++++++----------------- fs/bcachefs/errcode.h | 1 - 3 files changed, 31 insertions(+), 23 deletions(-)
fs/bcachefs/bcachefs.h +0 -1
@@ -983,7 +983,6 @@ struct bch_fs {mempool_t compression_bounce[2 ];mempool_t compress_workspace[BCH_COMPRESSION_OPT_NR ]; -mempool_t decompress_workspace;size_t zstd_workspace_size;struct crypto_shash *sha256;
fs/bcachefs/compress.c +31 -21
121 lines not on this page · raw message
fs/bcachefs/errcode.h +0 -1
@@ -54,7 +54,6 @@ x(ENOMEM, ENOMEM_compression_bounce_read_init) \ x(ENOMEM, ENOMEM_compression_bounce_write_init) \ x(ENOMEM, ENOMEM_compression_workspace_init) \ - x(ENOMEM, ENOMEM_decompression_workspace_init) \ x(ENOMEM, ENOMEM_bucket_gens) \ x(ENOMEM, ENOMEM_buckets_nouse) \x (ENOMEM , ENOMEM_usage_init) \
Signature · 1 line
2.45.2
PATCH 3/3 bcachefs: Don't BUG_ON() when superblock feature wasn't set for compressed data
In reply to Kent OverstreetWe don't allocate the mempools for compression/decompression unless we need them - but that means there's an inconsistency to check for.
Reported-by [email protected]
Signed-off-by Kent Overstreet <[email protected]>
--- fs/bcachefs/compress.c | 29 +++++++++++++++++++++++++++-- fs/bcachefs/errcode.h | 1 + fs/bcachefs/opts.c | 2 +- fs/bcachefs/opts.h | 1 + fs/bcachefs/sb-errors_format.h | 4 +++- 5 files changed, 33 insertions(+), 4 deletions(-)
fs/bcachefs/compress.c +27 -2
@@ -2,7 +2,9 @@#include "bcachefs.h" #include "checksum.h" #include "compress.h" +#include "error.h" #include "extents.h" +#include "opts.h" #include "super-io.h" #include <linux/lz4.h>
@@ -178,7 +180,16 @@ static int __bio_uncompress(struct bch_fs *c, struct bio *src,enum bch_compression_opts opt =bch2_compression_type_to_opt (crc.compression_type);mempool_t *workspace_pool = &c->compress_workspace[opt]; -BUG_ON (!mempool_initialized (workspace_pool)); +if (unlikely (!mempool_initialized (workspace_pool))) { +if (fsck_err (c, compression_type_not_marked_in_sb, +"compression type %s set but not marked in superblock" , + __bch2_compression_types[crc.compression_type])) + ret =bch2_check_set_has_compressed_data (c, opt); +else + ret = -BCH_ERR_compression_workspace_not_initialized; +if (ret) + goto out; + } src_data =bio_map_or_bounce (c, src,READ );
@@ -234,6 +245,7 @@ static int __bio_uncompress(struct bch_fs *c, struct bio *src,BUG (); } ret =0 ; +fsck_err: out:bio_unmap_or_unbounce (c, src_data);return ret;
@@ -420,7 +432,17 @@ static unsigned __bio_compress(struct bch_fs *c,BUG_ON (compression.type >=BCH_COMPRESSION_OPT_NR );mempool_t *workspace_pool = &c->compress_workspace[compression.type]; -BUG_ON (!mempool_initialized (workspace_pool)); +if (unlikely (!mempool_initialized (workspace_pool))) { +if (fsck_err (c, compression_opt_not_marked_in_sb, +"compression opt %s set but not marked in superblock" , + bch2_compression_opts[compression.type])) { + ret =bch2_check_set_has_compressed_data (c, compression.type); +if (ret)/* memory allocation failure, don't compress */ +return 0 ; + }else { +return 0 ; + } + }/* If it's only one block, don't bother trying to compress: */ if (src->bi_iter.bi_size <= c->opts.block_size)
@@ -502,6 +524,9 @@ static unsigned __bio_compress(struct bch_fs *c, err: ret = BCH_COMPRESSION_TYPE_incompressible; goto out; +fsck_err: + ret =0 ; + goto out; } unsigned bch2_bio_compress(struct bch_fs *c,
fs/bcachefs/errcode.h +1 -0
@@ -54,6 +54,7 @@ x(ENOMEM, ENOMEM_compression_bounce_read_init) \ x(ENOMEM, ENOMEM_compression_bounce_write_init) \ x(ENOMEM, ENOMEM_compression_workspace_init) \ + x(EIO, compression_workspace_not_initialized) \ x(ENOMEM, ENOMEM_bucket_gens) \ x(ENOMEM, ENOMEM_buckets_nouse) \x (ENOMEM , ENOMEM_usage_init) \
fs/bcachefs/opts.c +1 -1
@@ -54,7 +54,7 @@ const char * const __bch2_csum_opts[] = {NULL }; -static const char *const __bch2_compression_types[] = { +const char *const __bch2_compression_types[] = {BCH_COMPRESSION_TYPES () NULL };
fs/bcachefs/opts.h +1 -0
@@ -17,6 +17,7 @@ extern const char * const bch2_sb_features[];extern const char *const bch2_sb_compat[];extern const char *const __bch2_btree_ids[];extern const char *const __bch2_csum_opts[]; +extern const char *const __bch2_compression_types[];extern const char *const bch2_compression_opts[];extern const char *const __bch2_str_hash_types[];extern const char *const bch2_str_hash_opts[];
fs/bcachefs/sb-errors_format.h +3 -1
@@ -305,7 +305,9 @@ enum bch_fsck_flags { x(accounting_key_replicas_devs_unsorted, 280, FSCK_AUTOFIX) \ x(accounting_key_version_0, 282, FSCK_AUTOFIX) \ x(logged_op_but_clean, 283, FSCK_AUTOFIX) \ - x(MAX, 295, 0) + x(compression_opt_not_marked_in_sb, 295, FSCK_AUTOFIX) \ + x(compression_type_not_marked_in_sb, 296, FSCK_AUTOFIX) \ + x(MAX, 297, 0)enum bch_sb_error_id {#define x(t, n, ...) BCH_FSCK_ERR_##t = n,
Signature · 1 line
2.45.2