Skip to contentSkip to conversation

mkfs.ubifs: Add support for lz4hc compressor in mkfs.ubifs

PATCH In review Elie De Brauwer 2 participants 8 messages Oct 5, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mkfs.ubifs: Add support for lz4hc compressor in mkfs.ubifs

Brent et all,

I was bored last night and while following the thread on the lz4hc compression for ubifs got my attention so I decided to give a go at implementing in. Below you can find a patch which does just that.

An lz4hc and favor_lz4hc compr type, they mimick lzo and favor_lzo but use lz4hc instead of lzo.

In order to use lz4hc I included 3 files from the lz4 project since lz4 is not yet available in a conventient modular form (suggestions to fix this are welcome).

In the meantime I saw however that Brent was also considering adding lz4hc support mkfs.ubifs so I'm making a v0 zero post either to serve as input for review comment (mainly on handling the lz4 dependency) and then I'll take care of cleaning up and testing it myself, or just to serve as some reference to the implementation of Brent, let me know of you want some polishing up on this version or are already in an advanced phase of a solution of yourself.

(Yes, it's called v0, it's still untested but I would like input on the lz4 dependency handling and the extension of the options, or do we see something feasible where we contain lzo/lz4hc and zlib on the fly, suggestions welcome).

my 2 cents
E.

---
 Makefile                 |    2 +-
 mkfs.ubifs/README        |    1 +
 mkfs.ubifs/compr.c       |   64 ++--
 mkfs.ubifs/compr.h       |    1 +
 mkfs.ubifs/lz4/lz4.h     |  205 ++++++++++++
 mkfs.ubifs/lz4/lz4hc.c   |  817 ++++++++++++++++++++++++++++++++++++++++++++++
 mkfs.ubifs/lz4/lz4hc.h   |  111 +++++++
 mkfs.ubifs/mkfs.ubifs.c  |   33 +-
 mkfs.ubifs/ubifs-media.h |    2 +
 mkfs.ubifs/ubifs.h       |    2 +
 10 files changed, 1203 insertions(+), 35 deletions(-)
 create mode 100644 mkfs.ubifs/lz4/lz4.h
 create mode 100644 mkfs.ubifs/lz4/lz4hc.c
 create mode 100644 mkfs.ubifs/lz4/lz4hc.h
Makefile +1 -1
index bf57a13..f79b08a 100644
@@ -102,7 +102,7 @@ $(call _mkdep,lib/,libmtd.a)
 #
 # Utils in mkfs.ubifs subdir
 #
-obj-mkfs.ubifs = crc16.o lpt.o compr.o devtable.o \
+obj-mkfs.ubifs = crc16.o lpt.o compr.o lz4/lz4hc.o devtable.o \
 	hashtable/hashtable.o hashtable/hashtable_itr.o
 LDLIBS_mkfs.ubifs = -lz -llzo2 -lm -luuid
 $(call mkdep,mkfs.ubifs/,mkfs.ubifs,,ubi-utils/libubi.a)
mkfs.ubifs/README +1 -0
index 4fbaa7d..249aaa6 100644
@@ -8,3 +8,4 @@ UBIFS File System - Make File System program
 * defs.h is a bunch of definitions to smooth things over.
 * lpt.c is a selection of functions copied from fs/ubifs/lpt.c from the linux kernel, and amended.
 * hashtable/* was downloaded from http://www.cl.cam.ac.uk/~cwc22/hashtable/
+* lz4/* is based upon https://code.google.com/p/lz4/ lz4-r106.tar.gz
\ No newline at end of file
mkfs.ubifs/compr.c +44 -20

109 lines not on this page · raw message

mkfs.ubifs/compr.h +1 -0
index e3dd95c..a20033c 100644
@@ -36,6 +36,7 @@ enum compression_type
 	MKFS_UBIFS_COMPR_NONE,
 	MKFS_UBIFS_COMPR_LZO,
 	MKFS_UBIFS_COMPR_ZLIB,
+	MKFS_UBIFS_COMPR_LZ4HC,
 };
 
 int compress_data(void *in_buf, size_t in_len, void *out_buf, size_t *out_len,
mkfs.ubifs/lz4/lz4.h new file +205 -0

205 lines not on this page · raw message

mkfs.ubifs/lz4/lz4hc.c new file +817 -0

817 lines not on this page · raw message

mkfs.ubifs/lz4/lz4hc.h new file +111 -0

111 lines not on this page · raw message

mkfs.ubifs/mkfs.ubifs.c +19 -14
index f9977bc..b68b0bd 100644
@@ -179,11 +179,11 @@ static const char *helptext =
 "-o, --output=FILE        output to FILE\n"
 "-j, --jrn-size=SIZE      journal size\n"
 "-R, --reserved=SIZE      how much space should be reserved for the super-user\n"
-"-x, --compr=TYPE         compression type - \"lzo\", \"favor_lzo\", \"zlib\" or\n"
-"                         \"none\" (default: \"lzo\")\n"
-"-X, --favor-percent      may only be used with favor LZO compression and defines\n"
-"                         how many percent better zlib should compress to make\n"
-"                         mkfs.ubifs use zlib instead of LZO (default 20%)\n"
+"-x, --compr=TYPE         compression type - \"lzo\", \"favor_lzo\", \"lz4hc\",\n"
+"                          \"favor_lz4hc\", \"zlib\" or \"none\" (default: \"lzo\")\n"
+"-X, --favor-percent      may only be used with favor LZO or LZ4HC compression and\n"
+"                         defines how many percent better zlib should compress to\n"
+"                         make mkfs.ubifs use zlib instead of LZO/LZ4HC (default 20%)\n"
 "-f, --fanout=NUM         fanout NUM (default: 8)\n"
 "-F, --space-fixup        file-system free space has to be fixed up on first mount\n"
 "                         (requires kernel version 3.0 or greater)\n"
@@ -201,14 +201,15 @@ static const char *helptext =
 "-h, --help               display this help text\n\n"
 "Note, SIZE is specified in bytes, but it may also be specified in Kilobytes,\n"
 "Megabytes, and Gigabytes if a KiB, MiB, or GiB suffix is used.\n\n"
-"If you specify \"lzo\" or \"zlib\" compressors, mkfs.ubifs will use this compressor\n"
-"for all data. The \"none\" disables any data compression. The \"favor_lzo\" is not\n"
-"really a separate compressor. It is just a method of combining \"lzo\" and \"zlib\"\n"
-"compressors. Namely, mkfs.ubifs tries to compress data with both \"lzo\" and \"zlib\"\n"
-"compressors, then it compares which compressor is better. If \"zlib\" compresses 20\n"
-"or more percent better than \"lzo\", mkfs.ubifs chooses \"lzo\", otherwise it chooses\n"
-"\"zlib\". The \"--favor-percent\" may specify arbitrary threshold instead of the\n"
-"default 20%.\n\n"
+"If you specify \"lz4hc\", \"lzo\" or \"zlib\" compressors, mkfs.ubifs will use this\n"
+"compressor for all data. The \"none\" disables any data compression. The \"favor_lzo\"\n"
+"or \"favor_lz4hc\" are not really separate compressors. They are just a method of\n"
+"combining \"lzo\" and \"zlib\" or \"lz4hc\" and \"zlib\" compressors.\n"
+"Namely, mkfs.ubifs tries to compress data with both compressors specified,\n"
+"then it compares which compressor is better. If \"zlib\" compresses 20\n"
+"or more percent better than the alternative, mkfs.ubifs chooses \"zlib\", otherwise\n"
+"the alternative is chosen. The \"--favor-percent\" may specify arbitrary threshold\n"
+"instead of the default 20%.\n\n"
 "The -F parameter is used to set the \"fix up free space\" flag in the superblock,\n"
 "which forces UBIFS to \"fixup\" all the free space which it is going to use. This\n"
 "option is useful to work-around the problem of double free space programming: if the\n"
@@ -596,8 +597,12 @@ static int get_options(int argc, char**argv)
 		case 'x':
 			if (strcmp(optarg, "favor_lzo") == 0)
 				c->favor_lzo = 1;
+			else if (strcmp(optarg, "favor_lz4hc") == 0)
+				c->favor_lz4hc = 1;
 			else if (strcmp(optarg, "zlib") == 0)
 				c->default_compr = UBIFS_COMPR_ZLIB;
+			else if (strcmp(optarg, "lz4hc") == 0)
+				c->default_compr = UBIFS_COMPR_LZ4HC;
 			else if (strcmp(optarg, "none") == 0)
 				c->default_compr = UBIFS_COMPR_NONE;
 			else if (strcmp(optarg, "lzo") != 0)
@@ -607,7 +612,7 @@ static int get_options(int argc, char**argv)
 			c->favor_percent = strtol(optarg, &endp, 0);
 			if (*endp != '\0' || endp == optarg ||
 			    c->favor_percent <= 0 || c->favor_percent >= 100)
-				return err_msg("bad favor LZO percent '%s'",
+				return err_msg("bad favor percent '%s'",
 					       optarg);
 			break;
 		case 'j':
mkfs.ubifs/ubifs-media.h +2 -0
index fe62d0e..96b8500 100644
@@ -296,6 +296,7 @@ enum {
  *
  * UBIFS_COMPR_NONE: no compression
  * UBIFS_COMPR_LZO: LZO compression
+ * UBIFS_COMPR_LZ4HC: LZ4HC compression
  * UBIFS_COMPR_ZLIB: ZLIB compression
  * UBIFS_COMPR_TYPES_CNT: count of supported compression types
  */
@@ -303,6 +304,7 @@ enum {
 	UBIFS_COMPR_NONE,
 	UBIFS_COMPR_LZO,
 	UBIFS_COMPR_ZLIB,
+	UBIFS_COMPR_LZ4HC,
 	UBIFS_COMPR_TYPES_CNT,
 };
 
mkfs.ubifs/ubifs.h +2 -0
index 434b651..87f944a 100644
@@ -276,6 +276,7 @@ struct ubifs_znode
  * @main_first: first LEB of the main area
  * @default_compr: default compression type
  * @favor_lzo: favor LZO compression method
+ * @favor_lz4hc: favor LZ4HC compression method
  * @favor_percent: lzo vs. zlib threshold used in case favor LZO
  *
  * @key_hash_type: type of the key hash
@@ -354,6 +355,7 @@ struct ubifs_info
 	int main_first;
 	int default_compr;
 	int favor_lzo;
+	int favor_lz4hc;
 	int favor_percent;
 
 	uint8_t key_hash_type;
Signature · 1 line
1.7.10.4
#1 lore raw

05.10.2013, 10:53, "Elie De Brauwer" <[email protected]>:

Quoted 21 lines

Brent et all,

I was bored last night and while following the thread on the lz4hc compression for ubifs got my attention so I decided to give a go at implementing in. Below you can find a patch which does just that.

An lz4hc and favor_lz4hc compr type, they mimick lzo and favor_lzo but use lz4hc instead of lzo.

In order to use lz4hc I included 3 files from the lz4 project since lz4 is not yet available in a conventient modular form (suggestions to fix this are welcome).

In the meantime I saw however that Brent was also considering adding lz4hc support mkfs.ubifs so I'm making a v0 zero post either to serve as input for review comment (mainly on handling the lz4 dependency) and then I'll take care of cleaning up and testing it myself, or just to serve as some reference to the implementation of Brent, let me know of you want some polishing up on this version or are already in an advanced phase of a solution of yourself.

Quoted 7 lines

(Yes, it's called v0, it's still untested but I would like input on the lz4 dependency handling and the extension of the options, or do we see something feasible where we contain lzo/lz4hc and zlib on the fly, suggestions welcome).

my 2 cents
E.

Thank you very much!

Unfortunately, your patch does not apply cleanly. I've tried mtd-utils master and v1.5.0, and got rejects in both cases.

Signature · 2 lines
Regards,
Konstantin
#2 lore raw
Quoted 29 lines · Konstantin Tokarev

05.10.2013, 10:53, "Elie De Brauwer" <[email protected]>:

Quoted 26 lines

Brent et all,

I was bored last night and while following the thread on the lz4hc compression for ubifs got my attention so I decided to give a go at implementing in. Below you can find a patch which does just that.

An lz4hc and favor_lz4hc compr type, they mimick lzo and favor_lzo but use lz4hc instead of lzo.

In order to use lz4hc I included 3 files from the lz4 project since lz4 is not yet available in a conventient modular form (suggestions to fix this are welcome).

In the meantime I saw however that Brent was also considering adding lz4hc support mkfs.ubifs so I'm making a v0 zero post either to serve as input for review comment (mainly on handling the lz4 dependency) and then I'll take care of cleaning up and testing it myself, or just to serve as some reference to the implementation of Brent, let me know of you want some polishing up on this version or are already in an advanced phase of a solution of yourself.

(Yes, it's called v0, it's still untested but I would like input on the lz4 dependency handling and the extension of the options, or do we see something feasible where we contain lzo/lz4hc and zlib on the fly, suggestions welcome).

Quoted 8 lines
Quoted 2 lines

my 2 cents
E.

Thank you very much!

Unfortunately, your patch does not apply cleanly. I've tried mtd-utils master and v1.5.0, and got rejects in both cases.

That's odd, yesterday I started by cloning mtd-utils.git and when I try to repeat my own steps and apply my patch it does seem to work.

edb@lapelidb:/tmp$ mkdir l
edb@lapelidb:/tmp$ cd l/
edb@lapelidb:/tmp/l$ git clone -q git://git.infradead.org/mtd-utils.git
edb@lapelidb:/tmp/l$ cd mtd-utils/
edb@lapelidb:/tmp/l/mtd-utils$ patch -p1 <
/tmp/0001-mkfs.ubifs-Add-support-for-lz4hc-compressor-in-mkfs..patch
patching file Makefile
patching file mkfs.ubifs/README
patching file mkfs.ubifs/compr.c
patching file mkfs.ubifs/compr.h
patching file mkfs.ubifs/lz4/lz4.h
patching file mkfs.ubifs/lz4/lz4hc.c
patching file mkfs.ubifs/lz4/lz4hc.h
patching file mkfs.ubifs/mkfs.ubifs.c
patching file mkfs.ubifs/ubifs-media.h
patching file mkfs.ubifs/ubifs.h
edb@lapelidb:/tmp/l/mtd-utils$

The only thing I can imagine is that I started from a wrong repository ... any pointers ?

gr
E.

Signature · 1 line
Elie De Brauwer
#3 lore raw

05.10.2013, 16:56, "Elie De Brauwer" <[email protected]>:

Quoted 2 lines

That's odd, yesterday I started by cloning mtd-utils.git and when I try to repeat my own steps and apply my patch it does seem to work.

I've figured it out: you've included patch into the mail instead of attaching it as a file, so I copied it, and tab characters got lost. patch -p1 -l works successfully.

Signature · 2 lines
Regards,
Konstantin
#4 lore raw

05.10.2013, 17:25, "Konstantin Tokarev" <[email protected]>:

Quoted 8 lines

05.10.2013, 16:56, "Elie De Brauwer" <[email protected]>:

Quoted 2 lines

 That's odd, yesterday I started by cloning mtd-utils.git and when I try to repeat my own steps and apply my patch it does seem to work.

I've figured it out: you've included patch into the mail instead of attaching it as a file, so I copied it, and tab characters got lost. patch -p1 -l works successfully.

When trying to compress my rootfs, I got message "32866 compression errors occurred". Resulting image is much larger than LZO one (133M for lz4hc, 86M for lzo, 79M for zlib)

Signature · 2 lines
Regards,
Konstantin
#5 lore raw

05.10.2013, 17:52, "Konstantin Tokarev" <[email protected]>:

Quoted 11 lines

05.10.2013, 17:25, "Konstantin Tokarev" <[email protected]>:

Quoted 6 lines

 05.10.2013, 16:56, "Elie De Brauwer" <[email protected]>:

Quoted 2 lines

  That's odd, yesterday I started by cloning mtd-utils.git and when I try to repeat my own steps and apply my patch it does seem to work.

 I've figured it out: you've included patch into the mail instead of attaching it as a file, so I copied it, and tab characters got lost. patch -p1 -l works successfully.

When trying to compress my rootfs, I got message "32866 compression errors occurred". Resulting image is much larger than LZO one (133M for lz4hc, 86M for lzo, 79M for zlib)

The next patch over your patch fixes the issue:

mkfs.ubifs/compr.c +3 -2
index 2030042..65c7dbe 100644
@@ -109,9 +109,10 @@ static int lz4hc_compress(void *in_buf, size_t in_len, void *out_buf,
            size_t *out_len)
 {
    int ret;
-   ret = LZ4_compressHC_limitedOutput(in_buf, out_buf, in_len, *out_len);
+   ret = LZ4_compressHC(in_buf, out_buf, in_len);
+   *out_len = ret;
 
-   if (ret != 0) {
+   if (ret == 0) {
        errcnt += 1;
        return -1;
    }

I've replaced LZ4_compressHC_limitedOutput with LZ4_compressHC to match lzo_compress logic which changes out_len instead of limiting it. No errors are reported now. However, result is still larger than LZO (93M).

Signature · 2 lines
Regards,
Konstantin
#7 lore raw
In reply to Konstantin Tokarev
Quoted 8 lines · Konstantin Tokarev

05.10.2013, 17:25, "Konstantin Tokarev" <[email protected]>:

Quoted 5 lines

05.10.2013, 16:56, "Elie De Brauwer" <[email protected]>:

Quoted 2 lines

That's odd, yesterday I started by cloning mtd-utils.git and when I try to repeat my own steps and apply my patch it does seem to work.

Quoted 7 lines
Quoted 3 lines

I've figured it out: you've included patch into the mail instead of attaching it as a file, so I copied it, and tab characters got lost. patch -p1 -l works successfully.

When trying to compress my rootfs, I got message "32866 compression errors occurred". Resulting image is much larger than LZO one (133M for lz4hc, 86M for lzo, 79M for zlib)

That only proofs I wasn't lying when I wrote this morning that my code was fully untested. I had some time to do some testing and if you apply the following, (cummulative to my last patch) it should also work.

mkfs.ubifs/compr.c +3 -2
index d9886b4..0900653 100644
@@ -110,11 +110,12 @@ static int lz4hc_compress(void *in_buf, size_t in_len, voi
 {
        int ret;
        ret = LZ4_compressHC_limitedOutput(in_buf, out_buf, in_len, *out_len);
-
-       if (ret != 0) {
+       if (ret == 0) {
                errcnt += 1;
                return -1;
        }
+       else
+               *out_len = ret;
 
        return 0;
 }

I get the following (preliminary) results:

edb@lapelidb:/tmp$ ls -hal imag*
-rw-r--r-- 1 edb  edb  28M Oct  5 16:12 imag.lz4hc
-rw-r--r-- 1 root root 25M Oct  5 16:09 imag.lzo
-rw-r--r-- 1 root root 43M Oct  5 16:11 imag.none
-rw-r--r-- 1 root root 22M Oct  5 16:11 imag.zlib

gr
E.

Signature · 1 line
Elie De Brauwer
#6 lore raw
In reply to Konstantin Tokarev
Quoted 36 lines · Konstantin Tokarev

05.10.2013, 17:52, "Konstantin Tokarev" <[email protected]>:

Quoted 11 lines

05.10.2013, 17:25, "Konstantin Tokarev" <[email protected]>:

Quoted 6 lines

05.10.2013, 16:56, "Elie De Brauwer" <[email protected]>:

Quoted 2 lines

That's odd, yesterday I started by cloning mtd-utils.git and when I try to repeat my own steps and apply my patch it does seem to work.

I've figured it out: you've included patch into the mail instead of attaching it as a file, so I copied it, and tab characters got lost. patch -p1 -l works successfully.

When trying to compress my rootfs, I got message "32866 compression errors occurred". Resulting image is much larger than LZO one (133M for lz4hc, 86M for lzo, 79M for zlib)

The next patch over your patch fixes the issue:

mkfs.ubifs/compr.c +3 -2
index 2030042..65c7dbe 100644
@@ -109,9 +109,10 @@ static int lz4hc_compress(void *in_buf, size_t in_len, void *out_buf,
            size_t *out_len)
 {
    int ret;
-   ret = LZ4_compressHC_limitedOutput(in_buf, out_buf, in_len, *out_len);
+   ret = LZ4_compressHC(in_buf, out_buf, in_len);
+   *out_len = ret;
 
-   if (ret != 0) {
+   if (ret == 0) {
        errcnt += 1;
        return -1;
    }
Quoted 3 lines

I've replaced LZ4_compressHC_limitedOutput with LZ4_compressHC to match lzo_compress logic which changes out_len instead of limiting it. No errors are reported now. However, result is still larger than LZO (93M).

Oops, guessed our e-mails crossed each-other, the reason why I initially choose limitedOutput is because compressHC will assume the output buffer is large enough (which means it could potentially outgrow the input buffer in size, it seemed safer to use the limitedOutput option now rather than to introduce a potential buffer overflow).

But apparently our findings are the same, lz4hc also produces a slightly larger resulting binary, but it is the fastet one (on my x86 laptop):

edb@lapelidb:/tmp/l/mtd-utils$ time sudo mkfs.ubifs/mkfs.ubifs  -x
none -m 2048 -e 126976 -c 744 -d /tmp/rootfs/ /tmp/imag.zlib

real 0m0.452s
user 0m0.108s
sys 0m0.044s

edb@lapelidb:/tmp/l/mtd-utils$ time sudo mkfs.ubifs/mkfs.ubifs  -x
lz4hc -m 2048 -e 126976 -c 744 -d /tmp/rootfs/ /tmp/imag.lz4hc

real 0m0.805s
user 0m0.768s
sys 0m0.032s

edb@lapelidb:/tmp/l/mtd-utils$ time sudo mkfs.ubifs/mkfs.ubifs  -x
zlib -m 2048 -e 126976 -c 744 -d /tmp/rootfs/ /tmp/imag.zlib

real 0m1.834s
user 0m1.792s
sys 0m0.036s

edb@lapelidb:/tmp/l/mtd-utils$ time sudo mkfs.ubifs/mkfs.ubifs  -x lzo
-m 2048 -e 126976 -c 744 -d /tmp/rootfs/ /tmp/imag.lzo

real 0m3.341s
user 0m3.296s
sys 0m0.044s

But it's at the decompression side that the real benefit probably lies (as in flash performance vs decompression speed).

my 2 cents
E.

Signature · 1 line
Elie De Brauwer