erofs-utils: introduce z_erofs_inmem_extent
PATCH v2 1/4 erofs-utils: introduce z_erofs_inmem_extent
From: Gao Xiang <[email protected]>
In order to introduce deduplicatation for compressed pclusters. A lookahead extent is recorded so that the following deduplication process can adjust the previous extent on demand.
Also, in the future, it can be used for parallel compression as well.
--- lib/compress.c | 87 ++++++++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 39 deletions(-)
lib/compress.c +48 -39
@@ -22,12 +22,19 @@static struct erofs_compress compresshandle;static unsigned int algorithmtype[2 ]; -struct z_erofs_vle_compress_ctx { -u8 *metacur; +struct z_erofs_inmem_extent { +erofs_blk_t blkaddr; +unsigned int compressedblks; +unsigned int length; +bool raw; +}; +struct z_erofs_vle_compress_ctx {u8 queue[EROFS_CONFIG_COMPR_MAX_SZ *2 ]; +struct z_erofs_inmem_extent e;/* (lookahead) extent */ + +u8 *metacur;unsigned int head, tail; -unsigned int compressedblks;erofs_blk_t blkaddr;/* pointing to the next blkaddr */ u16 clusterofs; };
@@ -43,7 +50,7 @@ static unsigned int vle_compressmeta_capacity(erofs_off_t filesize)return Z_EROFS_LEGACY_MAP_HEADER_SIZE + indexsize; } -static void vle_write_indexes_final (struct z_erofs_vle_compress_ctx *ctx) +static void z_erofs_write_indexes_final (struct z_erofs_vle_compress_ctx *ctx) {const unsigned int type =Z_EROFS_VLE_CLUSTER_TYPE_PLAIN ;struct z_erofs_vle_decompressed_index di;
@@ -59,10 +66,10 @@ static void vle_write_indexes_final(struct z_erofs_vle_compress_ctx *ctx) ctx->metacur +=sizeof (di); } -static void vle_write_indexes (struct z_erofs_vle_compress_ctx *ctx, -unsigned int count,bool raw) +static void z_erofs_write_indexes (struct z_erofs_vle_compress_ctx *ctx) {unsigned int clusterofs = ctx->clusterofs; +unsigned int count = ctx->e.length;unsigned int d0 =0 , d1 = (clusterofs + count) /EROFS_BLKSIZ ;struct z_erofs_vle_decompressed_index di;unsigned int type;
@@ -76,13 +83,13 @@ static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx,* A lcluster cannot have three parts with the middle one which * is well-compressed for !ztailpacking cases. */ -DBG_BUGON (!raw && !cfg.c_ztailpacking); - type = raw ?Z_EROFS_VLE_CLUSTER_TYPE_PLAIN : +DBG_BUGON (!ctx->e.raw && !cfg.c_ztailpacking); + type = ctx->e.raw ?Z_EROFS_VLE_CLUSTER_TYPE_PLAIN :Z_EROFS_VLE_CLUSTER_TYPE_HEAD ; advise =cpu_to_le16 (type <<Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT ); di.di_advise = advise; - di.di_u.blkaddr =cpu_to_le32 (ctx->blkaddr); + di.di_u.blkaddr =cpu_to_le32 (ctx->e.blkaddr);memcpy (ctx->metacur, &di,sizeof (di)); ctx->metacur +=sizeof (di);
@@ -95,7 +102,7 @@ static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx,/* XXX: big pcluster feature should be per-inode */ if (d0 ==1 &&erofs_sb_has_big_pcluster ()) { type =Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD ; - di.di_u.delta[0 ] =cpu_to_le16 (ctx->compressedblks | + di.di_u.delta[0 ] =cpu_to_le16 (ctx->e.compressedblks |Z_EROFS_VLE_DI_D0_CBLKCNT ); di.di_u.delta[1 ] =cpu_to_le16 (d1); }else if (d0) {
@@ -119,9 +126,9 @@ static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx, di.di_u.delta[0 ] =cpu_to_le16 (d0); di.di_u.delta[1 ] =cpu_to_le16 (d1); }else { - type = raw ?Z_EROFS_VLE_CLUSTER_TYPE_PLAIN : + type = ctx->e.raw ?Z_EROFS_VLE_CLUSTER_TYPE_PLAIN :Z_EROFS_VLE_CLUSTER_TYPE_HEAD ; - di.di_u.blkaddr =cpu_to_le32 (ctx->blkaddr); + di.di_u.blkaddr =cpu_to_le32 (ctx->e.blkaddr); } advise =cpu_to_le16 (type <<Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT ); di.di_advise = advise;
@@ -226,18 +233,16 @@ static int vle_compress_one(struct erofs_inode *inode, struct z_erofs_vle_compress_ctx *ctx, bool final) { +static char dstbuf[EROFS_CONFIG_COMPR_MAX_SZ +EROFS_BLKSIZ ]; +char *const dst = dstbuf +EROFS_BLKSIZ ;struct erofs_compress *const h = &compresshandle;unsigned int len = ctx->tail - ctx->head; -unsigned int count;int ret; -static char dstbuf[EROFS_CONFIG_COMPR_MAX_SZ +EROFS_BLKSIZ ]; -char *const dst = dstbuf +EROFS_BLKSIZ ;while (len) {unsigned int pclustersize =z_erofs_get_max_pclusterblks (inode) *EROFS_BLKSIZ ;bool may_inline = (cfg.c_ztailpacking && final); -bool raw;if (len <= pclustersize) {if (!final)
@@ -246,10 +251,11 @@ static int vle_compress_one(struct erofs_inode *inode, goto nocompression; } - count =min (len, cfg.c_max_decompressed_extent_bytes); + ctx->e.length =min (len, + cfg.c_max_decompressed_extent_bytes); ret =erofs_compress_destsize (h, ctx->queue + ctx->head, - &count, dst, pclustersize, - !(final && len == count)); + &ctx->e.length, dst, pclustersize, + !(final && len == ctx->e.length));if (ret <=0 ) {if (ret != -EAGAIN ) { erofs_err("failed to compress %s: %s",
@@ -267,17 +273,17 @@ nocompression:if (ret <0 )return ret; - count = ret; + ctx->e.length = ret;/* * XXX: For now, we have to leave `ctx->compressedblks * = 1' since there is no way to generate compressed * indexes after the time that ztailpacking is decided. */ - ctx->compressedblks =1 ; - raw = true; + ctx->e.compressedblks =1 ; + ctx->e.raw = true;/* tailpcluster should be less than 1 block */ - }else if (may_inline && len == count && + }else if (may_inline && len == ctx->e.length && ret <EROFS_BLKSIZ ) {if (ctx->clusterofs + len <=EROFS_BLKSIZ ) { inode->eof_tailraw =malloc (len);
@@ -292,19 +298,20 @@ nocompression: ret =z_erofs_fill_inline_data (inode, dst, ret, false);if (ret <0 )return ret; - ctx->compressedblks =1 ; - raw = false; + ctx->e.compressedblks =1 ; + ctx->e.raw = false; }else {unsigned int tailused, padding; -if (may_inline && len == count) +if (may_inline && len == ctx->e.length)tryrecompress_trailing (ctx->queue + ctx->head, - &count, dst, &ret); + &ctx->e.length, dst, &ret); tailused = ret & (EROFS_BLKSIZ -1 ); padding =0 ; - ctx->compressedblks =BLK_ROUND_UP (ret); -DBG_BUGON (ctx->compressedblks *EROFS_BLKSIZ >= count); + ctx->e.compressedblks =BLK_ROUND_UP (ret); +DBG_BUGON (ctx->e.compressedblks *EROFS_BLKSIZ >= + ctx->e.length);/* zero out garbage trailing data for non-0padding */ if (!erofs_sb_has_lz4_0padding ())
@@ -315,21 +322,22 @@ nocompression:/* write compressed data */ erofs_dbg ("Writing %u compressed data to %u of %u blocks" , - count, ctx->blkaddr, ctx->compressedblks); + ctx->e.length, ctx->blkaddr, + ctx->e.compressedblks); ret =blk_write (dst - padding, ctx->blkaddr, - ctx->compressedblks); + ctx->e.compressedblks);if (ret)return ret; - raw = false; + ctx->e.raw = false; } +/* write indexes for this pcluster */ + ctx->e.blkaddr = ctx->blkaddr; +z_erofs_write_indexes (ctx); - ctx->head += count; -/* write compression indexes for this pcluster */ -vle_write_indexes (ctx, count, raw); - - ctx->blkaddr += ctx->compressedblks; - len -= count; + ctx->blkaddr += ctx->e.compressedblks; + ctx->head += ctx->e.length; + len -= ctx->e.length;if (!final && ctx->head >=EROFS_CONFIG_COMPR_MAX_SZ ) { const unsigned int qh_aligned =
@@ -654,6 +662,7 @@ int erofs_write_compressed_file(struct erofs_inode *inode) ctx.metacur = compressmeta +Z_EROFS_LEGACY_MAP_HEADER_SIZE ; ctx.head = ctx.tail =0 ; ctx.clusterofs =0 ; + ctx.e.length =0 ; remaining = inode->i_size;while (remaining) {
@@ -679,7 +688,7 @@ int erofs_write_compressed_file(struct erofs_inode *inode)DBG_BUGON (compressed_blocks < !!inode->idata_size); compressed_blocks -= !!inode->idata_size; -vle_write_indexes_final (&ctx); +z_erofs_write_indexes_final (&ctx); legacymetasize = ctx.metacur - compressmeta;/* estimate if data compression saves space or not */ if (compressed_blocks * EROFS_BLKSIZ + inode->idata_size +
Signature · 1 line
2.27.0
PATCH v2 2/4 erofs-utils: lib: add rb-tree implementation
From: Ziyang Zhang <[email protected]>
Introduce a simple rb-tree implementation in order to store the hash map for deduplication.
--- lib/Makefile.am | 2 +- lib/rb_tree.c | 512 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/rb_tree.h | 104 ++++++++++ 3 files changed, 617 insertions(+), 1 deletion(-) create mode 100644 lib/rb_tree.c create mode 100644 lib/rb_tree.h
lib/Makefile.am +1 -1
@@ -27,7 +27,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \ noinst_HEADERS += compressor.h liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \ namei.c data.c compress.c compressor.c zmap.c decompress.c \ - compress_hints.c hashmap.c sha256.c blobchunk.c dir.c + compress_hints.c hashmap.c sha256.c blobchunk.c dir.c rb_tree.c liberofs_la_CFLAGS = -Wall -I$(top_srcdir)/include if ENABLE_LZ4 liberofs_la_CFLAGS += ${LZ4_CFLAGS}
lib/rb_tree.c new file +512 -0
512 lines not on this page · raw message
lib/rb_tree.h new file +104 -0
104 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v2 3/4 erofs-utils: fuse: introduce partial-referenced pclusters
In reply to ZiyangZhangFrom: Gao Xiang <[email protected]>
Due to deduplication for compressed data, pclusters can be partially referenced with their prefixes.
Decompression algorithms should know that in advance, otherwise they will fail out unexpectedly.
--- include/erofs/internal.h | 3 +++ include/erofs_fs.h | 3 +++ lib/data.c | 3 ++- lib/zmap.c | 6 ++++++ 4 files changed, 14 insertions(+), 1 deletion(-)
include/erofs/internal.h +3 -0
@@ -279,6 +279,7 @@ enum { BH_Mapped, BH_Encoded, BH_FullMapped, + BH_Partialref, };/* Has a disk mapping */
@@ -289,6 +290,8 @@ enum {#define EROFS_MAP_ENCODED (1 << BH_Encoded)/* The length of extent is full */ #define EROFS_MAP_FULL_MAPPED (1 << BH_FullMapped) +/* The extent refers to partial compressed data */ +#define EROFS_MAP_PARTIAL_REF (1 << BH_Partialref)struct erofs_map_blocks {char mpage[EROFS_BLKSIZ ];
include/erofs_fs.h +3 -0
@@ -355,6 +355,9 @@ enum {#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS 2#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT 0 +/* (noncompact, HEAD) This pcluster refers to compressed data partially */ +#define Z_EROFS_VLE_DI_PARTIAL_REF (1 << 15) +/* * D0_CBLKCNT will be marked _only_ at the 1st non-head lcluster to store the * compressed block count of a compressed extent (in logical clusters, aka.
lib/data.c +2 -1
@@ -258,7 +258,8 @@ static int z_erofs_read_data(struct erofs_inode *inode, char *buffer, }else {DBG_BUGON (end != map.m_la + map.m_llen); length = map.m_llen; - partial = !(map.m_flags &EROFS_MAP_FULL_MAPPED ); + partial = !(map.m_flags &EROFS_MAP_FULL_MAPPED ) || + (map.m_flags &EROFS_MAP_PARTIAL_REF ); }if (map.m_la < offset) {
lib/zmap.c +6 -0
@@ -99,6 +99,7 @@ struct z_erofs_maprecorder {u16 delta[2 ];erofs_blk_t pblk, compressedlcs;erofs_off_t nextpackoff; +bool partialref; }; static int z_erofs_reload_indexes(struct z_erofs_maprecorder *m,
@@ -161,6 +162,9 @@ static int legacy_load_cluster_from_disk(struct z_erofs_maprecorder *m,break ;case Z_EROFS_VLE_CLUSTER_TYPE_PLAIN :case Z_EROFS_VLE_CLUSTER_TYPE_HEAD : +if (advise &Z_EROFS_VLE_DI_PARTIAL_REF ) + m->partialref = true; + m->clusterofs =le16_to_cpu (di->di_clusterofs); m->pblk =le32_to_cpu (di->di_u.blkaddr);break ;
@@ -602,6 +606,8 @@ static int z_erofs_do_map_blocks(struct erofs_inode *vi, goto out; } +if (m.partialref) + map->m_flags |=EROFS_MAP_PARTIAL_REF ; map->m_llen = end - map->m_la;if (flags &EROFS_GET_BLOCKS_FINDTAIL ) vi->z_tailextent_headlcn = m.lcn;
Signature · 1 line
2.27.0
PATCH v2 4/4 erofs-utils: mkfs: introduce global compressed data deduplication
In reply to ZiyangZhangThis patch introduces global compressed data deduplication to reuse potential prefixes for each pcluster.
It also uses rolling hashing and tries to shorten the previous compressed extent in order to explore more possibilities for data deduplication.
--- include/erofs/config.h | 1 + include/erofs/dedupe.h | 39 +++++++++ lib/Makefile.am | 4 +- lib/compress.c | 117 ++++++++++++++++++++++----- lib/dedupe.c | 176 +++++++++++++++++++++++++++++++++++++++++ lib/rolling_hash.h | 60 ++++++++++++++ mkfs/main.c | 19 +++++ 7 files changed, 394 insertions(+), 22 deletions(-) create mode 100644 include/erofs/dedupe.h create mode 100644 lib/dedupe.c create mode 100644 lib/rolling_hash.h
include/erofs/config.h +1 -0
@@ -44,6 +44,7 @@ struct erofs_configure {char c_chunkbits;bool c_noinline_data;bool c_ztailpacking; +bool c_dedupe;bool c_ignore_mtime;bool c_showprogress;
include/erofs/dedupe.h new file +39 -0
@@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2022 Alibaba Cloud +*/ +#ifndef __EROFS_DEDUPE_H +#define __EROFS_DEDUPE_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "internal.h" + +struct z_erofs_inmem_extent { +erofs_blk_t blkaddr; +unsigned int compressedblks; +unsigned int length; +bool raw, partial; +}; + +struct z_erofs_dedupe_ctx { +u8 *start, *end; +u8 *cur; +struct z_erofs_inmem_extent e; +}; + +int z_erofs_dedupe_match (struct z_erofs_dedupe_ctx *ctx); +int z_erofs_dedupe_insert (struct z_erofs_inmem_extent *e, +void *original_data); +void z_erofs_dedupe_commit (bool drop); +int z_erofs_dedupe_init (unsigned int wsiz); +void z_erofs_dedupe_exit (void ); + +#ifdef __cplusplus +} +#endif + +#endif
lib/Makefile.am +3 -1
@@ -27,7 +27,9 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \ noinst_HEADERS += compressor.h liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \ namei.c data.c compress.c compressor.c zmap.c decompress.c \ - compress_hints.c hashmap.c sha256.c blobchunk.c dir.c rb_tree.c + compress_hints.c hashmap.c sha256.c blobchunk.c dir.c rb_tree.c \ + dedupe.c + liberofs_la_CFLAGS = -Wall -I$(top_srcdir)/include if ENABLE_LZ4 liberofs_la_CFLAGS += ${LZ4_CFLAGS}
lib/compress.c +96 -21
210 lines not on this page · raw message
lib/dedupe.c new file +176 -0
176 lines not on this page · raw message
lib/rolling_hash.h new file +60 -0
60 lines not on this page · raw message
mkfs/main.c +19 -0
@@ -18,6 +18,7 @@#include "erofs/inode.h" #include "erofs/io.h" #include "erofs/compress.h" +#include "erofs/dedupe.h" #include "erofs/xattr.h" #include "erofs/exclude.h" #include "erofs/block_list.h"
@@ -202,6 +203,12 @@ static int parse_extended_opts(const char *opts)return -EINVAL ; cfg.c_ztailpacking = true; } + +if (MATCH_EXTENTED_OPT ("dedupe" , token, keylen)) { +if (vallen) +return -EINVAL ; + cfg.c_dedupe = true; + } }return 0 ; }
@@ -668,6 +675,8 @@ int main(int argc, char **argv)erofs_show_config ();if (cfg.c_ztailpacking)erofs_warn ("EXPERIMENTAL compressed inline data feature in use. Use at your own risk!" ); +if (cfg.c_dedupe) +erofs_warn ("EXPERIMENTAL data deduplication feature in use. Use at your own risk!" );erofs_set_fs_root (cfg.c_src_path);#ifndef NDEBUG if (cfg.c_random_pclusterblks)
@@ -694,6 +703,15 @@ int main(int argc, char **argv) goto exit; } +if (cfg.c_dedupe) { + err =z_erofs_dedupe_init (EROFS_BLKSIZ ); +if (err) { +erofs_err ("failed to initialize deduplication: %s" , +erofs_strerror (err)); + goto exit; + } + } + err =z_erofs_compress_init (sb_bh);if (err) { erofs_err("failed to initialize compressor: %s",
@@ -751,6 +769,7 @@ int main(int argc, char **argv) err =erofs_mkfs_superblock_csum_set (); exit:z_erofs_compress_exit (); +z_erofs_dedupe_exit ();#ifdef WITH_ANDROID erofs_droid_blocklist_fclose ();#endif
Signature · 1 line
2.27.0