[PATCH v2 1/4] erofs-utils: lib: refactor erofs compressors init

Guo Xuenan guoxuenan at huawei.com
Sun Jul 23 01:04:31 AEST 2023


using struct erofs_compressors_cfg for erofs compressor
global configuration.

Signed-off-by: Guo Xuenan <guoxuenan at huawei.com>
---
 lib/compressor.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/compressor.h |  2 ++
 2 files changed, 63 insertions(+)

diff --git a/lib/compressor.c b/lib/compressor.c
index 4333f26..6288297 100644
--- a/lib/compressor.c
+++ b/lib/compressor.c
@@ -26,6 +26,67 @@ static const struct erofs_compressor *compressors[] = {
 #endif
 };
 
+static struct erofs_supported_algorithm {
+	bool enable;
+	unsigned int algtype;
+	const char *name;
+	struct erofs_compress handle;
+} erofs_supported_algorithms[] = {
+	{ .algtype = Z_EROFS_COMPRESSION_LZ4, .name = "lz4" },
+	{ .algtype = Z_EROFS_COMPRESSION_LZ4, .name = "lz4hc" },
+	{ .algtype = Z_EROFS_COMPRESSION_LZMA, .name = "lzma" },
+	{ .algtype = Z_EROFS_COMPRESSION_DEFLATE, .name = "deflate" },
+	{ .algtype = Z_EROFS_COMPRESSION_DEFLATE, .name = "libdeflate" },
+};
+
+static void erofs_init_compressor(struct erofs_sb_info *sbi,
+		struct erofs_compress *handle, const struct erofs_compressor *alg)
+{
+
+	handle->alg = alg;
+
+	/* should be written in "minimum compression ratio * 100" */
+	handle->compress_threshold = 100;
+
+	/* optimize for 4k size page */
+	handle->destsize_alignsize = erofs_blksiz(sbi);
+	handle->destsize_redzone_begin = erofs_blksiz(sbi) - 16;
+	handle->destsize_redzone_end = EROFS_CONFIG_COMPR_DEF_BOUNDARY;
+
+	if (alg && alg->init)
+		alg->init(handle);
+}
+
+static void erofs_compressor_register(struct erofs_sb_info *sbi,
+		const char *name, const struct erofs_compressor *alg)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(erofs_supported_algorithms); i++) {
+		if (!strcmp(erofs_supported_algorithms[i].name, name)) {
+			erofs_init_compressor(sbi, &erofs_supported_algorithms[i].handle, alg);
+			return;
+		}
+	}
+}
+
+void erofs_register_compressors(struct erofs_sb_info *sbi)
+{
+#if LZ4_ENABLED
+	erofs_compressor_register(sbi, "lz4", &erofs_compressor_lz4);
+#if LZ4HC_ENABLED
+	erofs_compressor_register(sbi, "lz4hc", &erofs_compressor_lz4hc);
+#endif
+#endif
+#if HAVE_LIBLZMA
+	erofs_compressor_register(sbi, "lzma", &erofs_compressor_lzma);
+#endif
+	erofs_compressor_register(sbi, "deflate", &erofs_compressor_deflate);
+#if HAVE_LIBDEFLATE
+	erofs_compressor_register(sbi, "libdeflate", &erofs_compressor_libdeflate);
+#endif
+}
+
 int erofs_compress_destsize(const struct erofs_compress *c,
 			    const void *src, unsigned int *srcsize,
 			    void *dst, unsigned int dstsize, bool inblocks)
diff --git a/lib/compressor.h b/lib/compressor.h
index 08a3988..66ea933 100644
--- a/lib/compressor.h
+++ b/lib/compressor.h
@@ -8,6 +8,7 @@
 #define __EROFS_LIB_COMPRESSOR_H
 
 #include "erofs/defs.h"
+#include "erofs/config.h"
 
 struct erofs_compress;
 
@@ -56,5 +57,6 @@ int erofs_compressor_setlevel(struct erofs_compress *c, int compression_level);
 int erofs_compressor_init(struct erofs_sb_info *sbi,
 		struct erofs_compress *c, char *alg_name);
 int erofs_compressor_exit(struct erofs_compress *c);
+void erofs_register_compressors(struct erofs_sb_info *sbi);
 
 #endif
-- 
2.34.3



More information about the Linux-erofs mailing list