[RFC PATCH v0 1/4] erofs-utils: clevel set up as an individual function
Gao Xiang
hsiangkao at aol.com
Mon Jan 18 04:46:00 AEDT 2021
From: Gao Xiang <hsiangkao at aol.com>
Compression level passed in can be verified then. Also, in order for
preparation of upcoming LZMA fixed-sized output compression.
Signed-off-by: Gao Xiang <hsiangkao at aol.com>
---
lib/compress.c | 11 ++++++-----
lib/compressor.c | 22 +++++++++++++++-------
lib/compressor.h | 6 ++++--
lib/compressor_lz4.c | 1 -
lib/compressor_lz4hc.c | 21 +++++++++++++++------
5 files changed, 40 insertions(+), 21 deletions(-)
diff --git a/lib/compress.c b/lib/compress.c
index 86db940b6edd..2e8deaf81907 100644
--- a/lib/compress.c
+++ b/lib/compress.c
@@ -20,8 +20,6 @@
#include "compressor.h"
static struct erofs_compress compresshandle;
-static int compressionlevel;
-
static struct z_erofs_map_header mapheader;
struct z_erofs_vle_compress_ctx {
@@ -165,8 +163,7 @@ static int vle_compress_one(struct erofs_inode *inode,
}
count = len;
- ret = erofs_compress_destsize(h, compressionlevel,
- ctx->queue + ctx->head,
+ ret = erofs_compress_destsize(h, ctx->queue + ctx->head,
&count, dst, EROFS_BLKSIZ);
if (ret <= 0) {
if (ret != -EAGAIN) {
@@ -520,7 +517,11 @@ int z_erofs_compress_init(void)
if (!cfg.c_compr_alg_master)
return 0;
- compressionlevel = cfg.c_compr_level_master < 0 ?
+ ret = erofs_compressor_setlevel(&compresshandle, cfg.c_compr_level_master);
+ if (ret)
+ return ret;
+
+ compresshandle.compression_level = cfg.c_compr_level_master < 0 ?
compresshandle.alg->default_level :
cfg.c_compr_level_master;
diff --git a/lib/compressor.c b/lib/compressor.c
index b2434e0e5418..e28aa8f934c0 100644
--- a/lib/compressor.c
+++ b/lib/compressor.c
@@ -22,11 +22,8 @@ static struct erofs_compressor *compressors[] = {
};
int erofs_compress_destsize(struct erofs_compress *c,
- int compression_level,
- void *src,
- unsigned int *srcsize,
- void *dst,
- unsigned int dstsize)
+ void *src, unsigned int *srcsize,
+ void *dst, unsigned int dstsize)
{
int ret;
@@ -34,8 +31,7 @@ int erofs_compress_destsize(struct erofs_compress *c,
if (!c->alg->compress_destsize)
return -ENOTSUP;
- ret = c->alg->compress_destsize(c, compression_level,
- src, srcsize, dst, dstsize);
+ ret = c->alg->compress_destsize(c, src, srcsize, dst, dstsize);
if (ret < 0)
return ret;
@@ -50,6 +46,18 @@ const char *z_erofs_list_available_compressors(unsigned int i)
return i >= ARRAY_SIZE(compressors) ? NULL : compressors[i]->name;
}
+int erofs_compressor_setlevel(struct erofs_compress *c, int compression_level)
+{
+ DBG_BUGON(!c->alg);
+ if (c->alg->setlevel)
+ return c->alg->setlevel(c, compression_level);
+
+ if (compression_level >= 0)
+ return -EINVAL;
+ c->compression_level = 0;
+ return 0;
+}
+
int erofs_compressor_init(struct erofs_compress *c, char *alg_name)
{
int ret, i;
diff --git a/lib/compressor.h b/lib/compressor.h
index b2471c41ca4e..8c4e72cfa8b9 100644
--- a/lib/compressor.h
+++ b/lib/compressor.h
@@ -21,9 +21,9 @@ struct erofs_compressor {
int (*init)(struct erofs_compress *c);
int (*exit)(struct erofs_compress *c);
+ int (*setlevel)(struct erofs_compress *c, int compression_level);
int (*compress_destsize)(struct erofs_compress *c,
- int compress_level,
void *src, unsigned int *srcsize,
void *dst, unsigned int dstsize);
};
@@ -32,6 +32,7 @@ struct erofs_compress {
struct erofs_compressor *alg;
unsigned int compress_threshold;
+ unsigned int compression_level;
/* *_destsize specific */
unsigned int destsize_alignsize;
@@ -45,10 +46,11 @@ struct erofs_compress {
extern struct erofs_compressor erofs_compressor_lz4;
extern struct erofs_compressor erofs_compressor_lz4hc;
-int erofs_compress_destsize(struct erofs_compress *c, int compression_level,
+int erofs_compress_destsize(struct erofs_compress *c,
void *src, unsigned int *srcsize,
void *dst, unsigned int dstsize);
+int erofs_compressor_setlevel(struct erofs_compress *c, int compression_level);
int erofs_compressor_init(struct erofs_compress *c, char *alg_name);
int erofs_compressor_exit(struct erofs_compress *c);
diff --git a/lib/compressor_lz4.c b/lib/compressor_lz4.c
index 8540a0d01cbb..c2141f39e95b 100644
--- a/lib/compressor_lz4.c
+++ b/lib/compressor_lz4.c
@@ -11,7 +11,6 @@
#include "compressor.h"
static int lz4_compress_destsize(struct erofs_compress *c,
- int compression_level,
void *src, unsigned int *srcsize,
void *dst, unsigned int dstsize)
{
diff --git a/lib/compressor_lz4hc.c b/lib/compressor_lz4hc.c
index 6680563986c3..5188a7cf6ea4 100644
--- a/lib/compressor_lz4hc.c
+++ b/lib/compressor_lz4hc.c
@@ -12,16 +12,13 @@
#include "compressor.h"
static int lz4hc_compress_destsize(struct erofs_compress *c,
- int compression_level,
- void *src,
- unsigned int *srcsize,
- void *dst,
- unsigned int dstsize)
+ void *src, unsigned int *srcsize,
+ void *dst, unsigned int dstsize)
{
int srcSize = (int)*srcsize;
int rc = LZ4_compress_HC_destSize(c->private_data, src, dst,
&srcSize, (int)dstsize,
- compression_level);
+ c->compression_level);
if (!rc)
return -EFAULT;
*srcsize = srcSize;
@@ -47,12 +44,24 @@ static int compressor_lz4hc_init(struct erofs_compress *c)
return 0;
}
+static int compressor_lz4hc_setlevel(struct erofs_compress *c,
+ int compression_level)
+{
+ if (compression_level > LZ4HC_CLEVEL_MAX)
+ return -EINVAL;
+
+ c->compression_level = compression_level < 0 ?
+ LZ4HC_CLEVEL_DEFAULT : compression_level;
+ return 0;
+}
+
struct erofs_compressor erofs_compressor_lz4hc = {
.name = "lz4hc",
.default_level = LZ4HC_CLEVEL_DEFAULT,
.best_level = LZ4HC_CLEVEL_MAX,
.init = compressor_lz4hc_init,
.exit = compressor_lz4hc_exit,
+ .setlevel = compressor_lz4hc_setlevel,
.compress_destsize = lz4hc_compress_destsize,
};
--
2.24.0
More information about the Linux-erofs
mailing list