Skip to contentSkip to conversation

bcachefs: compression workspaces should be indexed by opt, not type

PATCH New Kent Overstreet 1 participant 3 messages Nov 18, 2024

lore
Share & tools
lore
#0 lore raw

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.

---
 fs/bcachefs/bcachefs.h |  2 +-
 fs/bcachefs/compress.c | 19 +++++++++++--------
 2 files changed, 12 insertions(+), 9 deletions(-)
fs/bcachefs/bcachefs.h +1 -1
index c59a58b93a92..60ad547c52a8 100644
@@ -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
index 1410365a8891..4f541a195c84 100644
@@ -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
#1 lore raw

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

---
 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
index 60ad547c52a8..7a947d43d504 100644
@@ -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
index 18c995d41203..3affdafc2c04 100644
@@ -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
#2 lore raw

PATCH 3/3 bcachefs: Don't BUG_ON() when superblock feature wasn't set for compressed data

In reply to Kent Overstreet

We don't allocate the mempools for compression/decompression unless we need them - but that means there's an inconsistency to check for.

---
 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
index 2813e4556f0d..f99ff1819597 100644
@@ -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
index 3affdafc2c04..2dda7f962e5b 100644
@@ -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
index 0ba58d74c21f..6772faf385a5 100644
@@ -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
index 6b29339ea725..ea69099e681d 100644
@@ -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
index f2b38493356d..d5b18ff1645c 100644
@@ -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