Skip to contentSkip to conversation

erofs-utils: add a new compression framework

RFC PATCH New Gao Xiang 1 participant 4 messages Jan 16, 2019

Subsystems (2)
lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

RFC PATCH v2 1/4 erofs-utils: add a new compression framework

Signed-off-by Gao Xiang <gaoxiang25 at huawei.com>
---
 .gitignore                 |  2 +-
 Makefile.am                |  2 +-
 configure.ac               |  2 ++
 include/erofs/compressor.h | 52 +++++++++++++++++++++++++++++++++++
 include/erofs/config.h     | 12 ++++++++
 include/erofs/defs.h       | 42 ++++++++++++++++++++++++++++
 lib/Makefile.am            |  6 ++++
 lib/compressor.c           | 68 ++++++++++++++++++++++++++++++++++++++++++++++
 mkfs/erofs_config.h        |  6 ++--
 9 files changed, 188 insertions(+), 4 deletions(-)
 create mode 100644 include/erofs/compressor.h
 create mode 100644 include/erofs/config.h
 create mode 100644 lib/Makefile.am
 create mode 100644 lib/compressor.c
.gitignore +1 -1
index 3cebe03..6dca2b0 100644
@@ -28,7 +28,7 @@ TAGS
 .deps/
 Makefile.in
 aclocal.m4
-config.h
+/config.h
 config.h.in
 configure
 libtool
Makefile.am +1 -1
index 0eb37d6..d624708 100644
@@ -2,4 +2,4 @@
 
 ACLOCAL_AMFLAGS = -I m4
 
-SUBDIRS=mkfs
+SUBDIRS=lib mkfs
configure.ac +2 -0
index 74ad0c0..5249810 100644
@@ -10,6 +10,7 @@ AC_CONFIG_SRCDIR([mkfs/mkfs_main.c])
 AC_CONFIG_HEADERS([config.h])
 
 # Checks for programs.
+AM_PROG_AR
 AC_PROG_CC
 AC_PROG_INSTALL
 AC_PROG_LIBTOOL
@@ -110,5 +111,6 @@ if test "x${have_lz4}" = "x0" ; then
 fi
 
 AC_CONFIG_FILES([Makefile
+		 lib/Makefile
 		 mkfs/Makefile])
 AC_OUTPUT
include/erofs/compressor.h new file +52 -0

52 lines not on this page · raw message

include/erofs/config.h new file +12 -0
new file mode 100644
index 0000000..adaf964
@@ -0,0 +1,12 @@
+#ifndef __EROFS_CONFIG_H
+#define __EROFS_CONFIG_H
+
+#ifndef PAGE_SIZE
+#define PAGE_SIZE	4096
+#endif
+
+#define EROFS_CONFIG_COMPR_DEF_BOUNDARY     (128)
+#define EROFS_CONFIG_COMPR_RATIO_MAX_LIMIT  (100)
+
+#endif
+
include/erofs/defs.h +42 -0

53 lines not on this page · raw message

lib/Makefile.am new file +6 -0
new file mode 100644
index 0000000..3093d6a
@@ -0,0 +1,6 @@
+## Makefile.am
+
+noinst_LIBRARIES = liberofs.a
+liberofs_a_SOURCES = compressor.c
+liberofs_a_CPPFLAGS = -I$(top_srcdir)/include
+
lib/compressor.c new file +68 -0

68 lines not on this page · raw message

mkfs/erofs_config.h +4 -2
index 7e38f87..cac2d72 100644
@@ -9,12 +9,14 @@
 #ifndef __EROFS_MKFS_CONFIG_H
 #define __EROFS_MKFS_CONFIG_H
 
+/* this file(mkfs_config.h) will be deprecated later */
+
+#include "erofs/config.h"
+
 /* workaround of a lz4 native compression issue, which can crash the program */
 /* #define EROFS_CONFIG_COMPR_MAX_SZ        (1024 * 1024) */
 #define EROFS_CONFIG_COMPR_MAX_SZ           (900  * 1024)
 #define EROFS_CONFIG_COMPR_MIN_SZ           (32   * 1024)
-#define EROFS_CONFIG_COMPR_DEF_BOUNDARY     (128)
-#define EROFS_CONFIG_COMPR_RATIO_MAX_LIMIT  (100)
 
 struct erofs_compr_alg;
 
Signature · 1 line
2.14.4
author
#1 lore raw

RFC PATCH v2 2/4 erofs-utils: add lz4hc algorithm support

Signed-off-by Gao Xiang <gaoxiang25 at huawei.com>
---
 configure.ac               |  9 ++++++-
 include/erofs/compressor.h | 11 +++++++++
 lib/Makefile.am            |  5 ++++
 lib/compressor-lz4hc.c     | 60 ++++++++++++++++++++++++++++++++++++++++++++++
 lib/compressor.c           |  3 +++
 5 files changed, 87 insertions(+), 1 deletion(-)
 create mode 100644 lib/compressor-lz4hc.c
configure.ac +8 -1
index 5249810..0ed8a25 100644
@@ -104,11 +104,18 @@ AC_CHECK_FUNCS([ftruncate getcwd gettimeofday memset realpath strdup strerror st
 
 # Configure lz4.
 have_lz4="1"
+LIB_LZ4="-Wl,-Bstatic,-llz4,-Bdynamic"
 AC_CHECK_HEADERS([lz4.h], , [have_lz4="0"])
-AC_CHECK_LIB(lz4, LZ4_versionNumber, [LIBS="-Wl,-Bstatic,-llz4,-Bdynamic $LIBS"] , [have_lz4="0"])
+AC_CHECK_LIB(lz4, LZ4_versionNumber,, [have_lz4="0" LIB_LZ4=''])
 if test "x${have_lz4}" = "x0" ; then
   AC_MSG_ERROR([Cannot build without lz4])
 fi
+AM_CONDITIONAL(HasLZ4, test "$have_lz4" = '1')
+AC_SUBST([LIB_LZ4])
+
+have_lz4hc="1"
+AC_CHECK_HEADERS([lz4hc.h], , [have_lz4hc="0"])
+AM_CONDITIONAL(HasLZ4HC, test "$have_lz4hc" = '1')
 
 AC_CONFIG_FILES([Makefile
 		 lib/Makefile
include/erofs/compressor.h +11 -0
index 2a9a701..8e219d8 100644
@@ -39,8 +39,19 @@ struct erofs_compress {
 	unsigned int destsize_alignsize;
 	unsigned int destsize_redzone_begin;
 	unsigned int destsize_redzone_end;
+
+	union {
+#ifdef HAVE_LZ4HC_H
+		struct {
+			LZ4_streamHC_t *ctx;
+		} lz4hc;
+#endif
+	} u;
 };
 
+/* list of compression algorithms */
+extern struct erofs_compressor erofs_compressor_lz4hc;
+
 int erofs_compress_destsize(struct erofs_compress *c, int compression_level,
 			    void *src, unsigned int *srcsize,
 			    void *dst, unsigned int dstsize);
lib/Makefile.am +5 -0
index 3093d6a..33438a3 100644
@@ -2,5 +2,10 @@
 
 noinst_LIBRARIES = liberofs.a
 liberofs_a_SOURCES = compressor.c
+
+if HasLZ4HC
+liberofs_a_SOURCES += compressor-lz4hc.c
+endif
+
 liberofs_a_CPPFLAGS = -I$(top_srcdir)/include
 
lib/compressor-lz4hc.c new file +60 -0

60 lines not on this page · raw message

lib/compressor.c +3 -0
index a282be1..508203c 100644
@@ -36,6 +36,9 @@ int erofs_compressor_init(struct erofs_compress *c,
 			  char *alg_name)
 {
 	static struct erofs_compressor *compressors[] = {
+#ifdef HAVE_LZ4HC_H
+		&erofs_compressor_lz4hc,
+#endif
 	};
 
 	int ret, i;
Signature · 1 line
2.14.4
author
#2 lore raw

RFC PATCH v2 3/4 erofs-utils: add zlib algorithm support

In reply to Gao Xiang
Signed-off-by Gao Xiang <gaoxiang25 at huawei.com>
---
 configure.ac               |  12 ++
 include/erofs/compressor.h |   8 ++
 lib/Makefile.am            |   4 +
 lib/compressor-zlib.c      | 286 +++++++++++++++++++++++++++++++++++++++++++++
 lib/compressor.c           |   1 +
 5 files changed, 311 insertions(+)
 create mode 100644 lib/compressor-zlib.c
configure.ac +12 -0
index 0ed8a25..c82b900 100644
@@ -117,6 +117,18 @@ have_lz4hc="1"
 AC_CHECK_HEADERS([lz4hc.h], , [have_lz4hc="0"])
 AM_CONDITIONAL(HasLZ4HC, test "$have_lz4hc" = '1')
 
+# Configure zlib
+have_zlib="1"
+LIB_ZLIB=''
+AC_CHECK_HEADER(zlib.h, , [have_zlib='0'])
+AC_CHECK_LIB(z, deflate,, [have_zlib='0'])
+if test "x${have_lz4}" = "x1" ; then
+  AC_DEFINE(HAVE_ZLIB, 1, [Define to 1 if zlib is enabled])
+  LIB_ZLIB='-lz'
+fi
+AM_CONDITIONAL(HasZLIB, test "x$have_zlib" = 'x1')
+AC_SUBST([LIB_ZLIB])
+
 AC_CONFIG_FILES([Makefile
 		 lib/Makefile
 		 mkfs/Makefile])
include/erofs/compressor.h +8 -0
index 8e219d8..0355e18 100644
@@ -41,6 +41,13 @@ struct erofs_compress {
 	unsigned int destsize_redzone_end;
 
 	union {
+#ifdef HAVE_ZLIB
+		struct {
+			unsigned int destsize_excess;
+			unsigned int destsize_margin;
+		} zlib;
+#endif
+
 #ifdef HAVE_LZ4HC_H
 		struct {
 			LZ4_streamHC_t *ctx;
@@ -51,6 +58,7 @@ struct erofs_compress {
 
 /* list of compression algorithms */
 extern struct erofs_compressor erofs_compressor_lz4hc;
+extern struct erofs_compressor erofs_compressor_zlib;
 
 int erofs_compress_destsize(struct erofs_compress *c, int compression_level,
 			    void *src, unsigned int *srcsize,
lib/Makefile.am +4 -0
index 33438a3..f2de0e3 100644
@@ -7,5 +7,9 @@ if HasLZ4HC
 liberofs_a_SOURCES += compressor-lz4hc.c
 endif
 
+if HasZLIB
+liberofs_a_SOURCES += compressor-zlib.c
+endif
+
 liberofs_a_CPPFLAGS = -I$(top_srcdir)/include
 
lib/compressor-zlib.c new file +286 -0

286 lines not on this page · raw message

lib/compressor.c +1 -0
index 508203c..ed948f3 100644
@@ -39,6 +39,7 @@ int erofs_compressor_init(struct erofs_compress *c,
 #ifdef HAVE_LZ4HC_H
 		&erofs_compressor_lz4hc,
 #endif
+		&erofs_compressor_zlib,
 	};
 
 	int ret, i;
Signature · 1 line
2.14.4
author
#3 lore raw

RFC PATCH v2 4/4 erofs-utils: adapt new compression framework to mkfs

In reply to Gao Xiang
Signed-off-by Gao Xiang <gaoxiang25 at huawei.com>
---
 mkfs/Makefile.am        |   4 +-
 mkfs/erofs_compressor.c | 110 +++++++++++++++++++++++++++++++++---------------
 mkfs/erofs_compressor.h |   3 +-
 mkfs/erofs_lz4hc.c      |  52 -----------------------
 mkfs/erofs_lz4hc.h      |  21 ---------
 mkfs/mkfs_file.c        |  24 -----------
 6 files changed, 79 insertions(+), 135 deletions(-)
 delete mode 100644 mkfs/erofs_lz4hc.c
 delete mode 100644 mkfs/erofs_lz4hc.h
mkfs/Makefile.am +2 -2
index 0f57761..844cfa6 100644
@@ -3,7 +3,6 @@ AUTOMAKE_OPTIONS = foreign
 bin_PROGRAMS     = mkfs.erofs
 mkfs_erofs_SOURCES = erofs_compressor.c \
 		     mkfs_file.c \
-		     erofs_lz4hc.c \
 		     mkfs_main.c \
 		     erofs_cache.c \
 		     erofs_io.c \
@@ -13,7 +12,6 @@ mkfs_erofs_SOURCES = erofs_compressor.c \
 noinst_HEADERS = erofs_config.h  \
 		 mkfs_inode.h \
 		 erofs_error.h \
-		 erofs_lz4hc.h \
 		 erofs_cache.h \
 		 erofs_debug.h \
 		 mkfs_erofs.h \
@@ -22,6 +20,8 @@ noinst_HEADERS = erofs_config.h  \
 		 mkfs_file.h
 
 mkfs_erofs_CFLAGS = -Wall -Werror -I$(top_srcdir)/include
+# workaround since I don't know how to properly link LZ4 staticlly
+LIBS = -Wl,$(top_builddir)/lib/liberofs.a ${LIB_LZ4} ${LIB_ZLIB}
 
 if SUPPORT_LARG_FILE_AT_BIT32
     mkfs_erofs_CFLAGS += -D_FILE_OFFSET_BITS=64
mkfs/erofs_compressor.c +76 -34

146 lines not on this page · raw message

mkfs/erofs_compressor.h +1 -2
index b60f79d..2c8411b 100644
@@ -16,6 +16,7 @@
 enum erofs_compr_algs {
 	EROFS_COMPR_NONE,
 	EROFS_COMPR_LZ4HC,
+	EROFS_COMPR_ZLIB,
 	EROFS_COMPR_ALG_MAX,
 };
 
@@ -28,8 +29,6 @@ typedef void (*deinit_func)(void *cctx);
 struct erofs_compr_alg {
 	char *ca_name;
 	int ca_idx;
-	int ca_max_lvl;
-	int ca_min_lvl;
 	int ca_def_lvl;
 	compress_func ca_compress;
 	init_func ca_init;
mkfs/erofs_lz4hc.c deleted +0 -52

52 lines not on this page · raw message

mkfs/erofs_lz4hc.h deleted +0 -21
deleted file mode 100644
index d80340a..0000000
@@ -1,21 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0+ */
-/*
- * erofs_lz4hc.h
- *
- * Copyright (C) 2018 HUAWEI, Inc.
- *             http://www.huawei.com/
- * Created by Li Guifu <bluce.liguifu at huawei.com>
- */
-#ifndef __EROFS_LZ4HC_H__
-#define __EROFS_LZ4HC_H__
-
-#include <stdint.h>
-#include <lz4hc.h>
-
-#define EROFS_COMPR_LZ4HC_DEF_LVL (9)
-
-void *erofs_lz4hc_init(void);
-void erofs_lz4hc_deinit(void *ctx);
-int64_t erofs_lz4hc_compress(char *in, size_t insz, char *out, size_t outsz,
-			     size_t *inszptr, int level, void *);
-#endif
mkfs/mkfs_file.c +0 -24
index 382e9b6..262063c 100644
@@ -317,30 +317,6 @@ erofs_compr_idx_host_to_disk(struct erofs_compr_idx *hidx,
 static int erofs_compress_inline_file_data(struct erofs_compr_info *cinfo,
 					   struct erofs_compr_ctx *ctx)
 {
-	int64_t compr_count;
-	size_t comprsz = 0;
-
-	assert(ctx->cc_srclen <= EROFS_BLKSIZE);
-	assert(ctx->cc_buflen >= 2 * EROFS_BLKSIZE);
-
-	compr_count = erofs_compress(cinfo->ci_alg,
-				     ctx->cc_srcbuf,
-				     ctx->cc_srclen,
-				     ctx->cc_dstbuf,
-				     EROFS_BLKSIZE,
-				     &comprsz,
-				     cinfo->ci_lvl);
-
-	if (compr_count == 0 || compr_count == EROFS_COMPRESS_ERROR) {
-		erofs_err("Failed to compress data by %s",
-			  cinfo->ci_alg->ca_name);
-		return -EIO;
-	}
-
-	assert(comprsz == (size_t)ctx->cc_srclen);
-
-	ctx->cc_dstlen = (int)compr_count;
-	ctx->cc_nidxs  = EROFS_COMPR_CTX_INLINED_DATA;
 	return 0;
 }
 
Signature · 1 line
2.14.4