Skip to content

Commit bbc3a46

Browse files
author
Kent Overstreet
committed
bcachefs: Fix zstd compress workspace size
zstd apparently lies about the size of the compression workspace it requires; if we double it compression succeeds. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent 3f3ae12 commit bbc3a46

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

fs/bcachefs/bcachefs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ struct bch_fs {
931931
mempool_t compression_bounce[2];
932932
mempool_t compress_workspace[BCH_COMPRESSION_TYPE_NR];
933933
mempool_t decompress_workspace;
934-
ZSTD_parameters zstd_params;
934+
size_t zstd_workspace_size;
935935

936936
struct crypto_shash *sha256;
937937
struct crypto_sync_skcipher *chacha20;

fs/bcachefs/compress.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,7 @@ static int attempt_compress(struct bch_fs *c,
354354
*/
355355
unsigned level = min((compression.level * 3) / 2, zstd_max_clevel());
356356
ZSTD_parameters params = zstd_get_params(level, c->opts.encoded_extent_max);
357-
ZSTD_CCtx *ctx = zstd_init_cctx(workspace,
358-
zstd_cctx_workspace_bound(&params.cParams));
357+
ZSTD_CCtx *ctx = zstd_init_cctx(workspace, c->zstd_workspace_size);
359358

360359
/*
361360
* ZSTD requires that when we decompress we pass in the exact
@@ -371,7 +370,7 @@ static int attempt_compress(struct bch_fs *c,
371370
size_t len = zstd_compress_cctx(ctx,
372371
dst + 4, dst_len - 4 - 7,
373372
src, src_len,
374-
&c->zstd_params);
373+
&params);
375374
if (zstd_is_error(len))
376375
return 0;
377376

@@ -572,6 +571,13 @@ static int __bch2_fs_compress_init(struct bch_fs *c, u64 features)
572571
size_t decompress_workspace_size = 0;
573572
ZSTD_parameters params = zstd_get_params(zstd_max_clevel(),
574573
c->opts.encoded_extent_max);
574+
575+
/*
576+
* ZSTD is lying: if we allocate the size of the workspace it says it
577+
* requires, it returns memory allocation errors
578+
*/
579+
c->zstd_workspace_size = zstd_cctx_workspace_bound(&params.cParams);
580+
575581
struct {
576582
unsigned feature;
577583
enum bch_compression_type type;
@@ -585,13 +591,11 @@ static int __bch2_fs_compress_init(struct bch_fs *c, u64 features)
585591
zlib_deflate_workspacesize(MAX_WBITS, DEF_MEM_LEVEL),
586592
zlib_inflate_workspacesize(), },
587593
{ BCH_FEATURE_zstd, BCH_COMPRESSION_TYPE_zstd,
588-
zstd_cctx_workspace_bound(&params.cParams),
594+
c->zstd_workspace_size,
589595
zstd_dctx_workspace_bound() },
590596
}, *i;
591597
bool have_compressed = false;
592598

593-
c->zstd_params = params;
594-
595599
for (i = compression_types;
596600
i < compression_types + ARRAY_SIZE(compression_types);
597601
i++)

0 commit comments

Comments
 (0)