Skip to contentSkip to conversation

erofs-utils: lib: use compressed offsets in `struct z_erofs_inmem_extent`

PATCH New Gao Xiang 1 participant 3 messages Apr 3, 2025 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/3 erofs-utils: lib: use compressed offsets in `struct z_erofs_inmem_extent`

... to prepare for the new on-disk encoded extents.

---
 include/erofs/dedupe.h |   4 +-
 lib/compress.c         | 164 +++++++++++++++++++++--------------------
 lib/dedupe.c           |  12 +--
 3 files changed, 91 insertions(+), 89 deletions(-)
include/erofs/dedupe.h +2 -2
index 4cbfb2c..1af08e3 100644
@@ -13,8 +13,8 @@ extern "C"
 #include "internal.h"
 
 struct z_erofs_inmem_extent {
-	erofs_blk_t blkaddr;
-	unsigned int compressedblks;
+	erofs_off_t pstart;
+	unsigned int plen;
 	unsigned int length;
 	bool raw, partial, inlined;
 };
lib/compress.c +83 -81

440 lines not on this page · raw message

lib/dedupe.c +6 -6
index 85ff3c9..074cae3 100644
@@ -72,8 +72,8 @@ struct z_erofs_dedupe_item {
 	u8		prefix_sha256[32];
 	u64		prefix_xxh64;
 
-	erofs_blk_t	compressed_blkaddr;
-	unsigned int	compressed_blks;
+	erofs_off_t	pstart;
+	unsigned int	plen;
 
 	int		original_length;
 	bool		partial, raw;
@@ -142,8 +142,8 @@ int z_erofs_dedupe_match(struct z_erofs_dedupe_ctx *ctx)
 			(window_size + extra < e->original_length);
 		ctx->e.raw = e->raw;
 		ctx->e.inlined = false;
-		ctx->e.blkaddr = e->compressed_blkaddr;
-		ctx->e.compressedblks = e->compressed_blks;
+		ctx->e.pstart = e->pstart;
+		ctx->e.plen = e->plen;
 		return 0;
 	}
 	return -ENOENT;
@@ -170,8 +170,8 @@ int z_erofs_dedupe_insert(struct z_erofs_inmem_extent *e,
 			window_size, true);
 	memcpy(di->extra_data, original_data + window_size,
 	       e->length - window_size);
-	di->compressed_blkaddr = e->blkaddr;
-	di->compressed_blks = e->compressedblks;
+	di->pstart = e->pstart;
+	di->plen = e->plen;
 	di->partial = e->partial;
 	di->raw = e->raw;
 
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 2/3 erofs-utils: lib: move buffer allocation into z_erofs_write_indexes()

Just used to prepare for extent-based metadata. No logic changes.

---
 lib/compress.c | 52 ++++++++++++++++++++++++++++----------------------
 1 file changed, 29 insertions(+), 23 deletions(-)
lib/compress.c +29 -23
index eaca4d3..50d155e 100644
@@ -121,7 +121,7 @@ static bool z_erofs_mt_enabled;
 
 #define Z_EROFS_LEGACY_MAP_HEADER_SIZE	Z_EROFS_FULL_INDEX_ALIGN(0)
 
-static void z_erofs_write_indexes_final(struct z_erofs_compress_ictx *ctx)
+static void z_erofs_fini_full_indexes(struct z_erofs_compress_ictx *ctx)
 {
 	const unsigned int type = Z_EROFS_LCLUSTER_TYPE_PLAIN;
 	struct z_erofs_lcluster_index di;
@@ -137,8 +137,8 @@ static void z_erofs_write_indexes_final(struct z_erofs_compress_ictx *ctx)
 	ctx->metacur += sizeof(di);
 }
 
-static void z_erofs_write_extent(struct z_erofs_compress_ictx *ctx,
-				 struct z_erofs_inmem_extent *e)
+static void z_erofs_write_full_indexes(struct z_erofs_compress_ictx *ctx,
+				       struct z_erofs_inmem_extent *e)
 {
 	struct erofs_inode *inode = ctx->inode;
 	struct erofs_sb_info *sbi = inode->sbi;
@@ -236,20 +236,6 @@ static void z_erofs_write_extent(struct z_erofs_compress_ictx *ctx,
 	ctx->clusterofs = clusterofs + count;
 }
 
-static void z_erofs_write_indexes(struct z_erofs_compress_ictx *ctx)
-{
-	struct z_erofs_extent_item *ei, *n;
-
-	ctx->clusterofs = 0;
-	list_for_each_entry_safe(ei, n, &ctx->extents, list) {
-		z_erofs_write_extent(ctx, &ei->e);
-
-		list_del(&ei->list);
-		free(ei);
-	}
-	z_erofs_write_indexes_final(ctx);
-}
-
 static bool z_erofs_need_refill(struct z_erofs_compress_sctx *ctx)
 {
 	const bool final = !ctx->remaining;
@@ -1012,6 +998,31 @@ static void z_erofs_write_mapheader(struct erofs_inode *inode,
 	memcpy(compressmeta, &h, sizeof(struct z_erofs_map_header));
 }
 
+static void *z_erofs_write_indexes(struct z_erofs_compress_ictx *ctx)
+{
+	struct erofs_inode *inode = ctx->inode;
+	struct z_erofs_extent_item *ei, *n;
+	void *metabuf;
+
+	metabuf = malloc(BLK_ROUND_UP(inode->sbi, inode->i_size) *
+			 sizeof(struct z_erofs_lcluster_index) +
+			 Z_EROFS_LEGACY_MAP_HEADER_SIZE);
+	if (!metabuf)
+		return ERR_PTR(-ENOMEM);
+
+	ctx->metacur = metabuf + Z_EROFS_LEGACY_MAP_HEADER_SIZE;
+	ctx->clusterofs = 0;
+	list_for_each_entry_safe(ei, n, &ctx->extents, list) {
+		z_erofs_write_full_indexes(ctx, &ei->e);
+
+		list_del(&ei->list);
+		free(ei);
+	}
+	z_erofs_fini_full_indexes(ctx);
+	z_erofs_write_mapheader(inode, metabuf);
+	return metabuf;
+}
+
 void z_erofs_drop_inline_pcluster(struct erofs_inode *inode)
 {
 	struct erofs_sb_info *sbi = inode->sbi;
@@ -1145,15 +1156,11 @@ int erofs_commit_compressed_file(struct z_erofs_compress_ictx *ictx,
 	DBG_BUGON(pstart < (!!inode->idata_size) << bbits);
 	ptotal -= (!!inode->idata_size) << bbits;
 
-	compressmeta = malloc(BLK_ROUND_UP(sbi, inode->i_size) *
-			      sizeof(struct z_erofs_lcluster_index) +
-			      Z_EROFS_LEGACY_MAP_HEADER_SIZE);
+	compressmeta = z_erofs_write_indexes(ictx);
 	if (!compressmeta) {
 		ret = -ENOMEM;
 		goto err_free_idata;
 	}
-	ictx->metacur = compressmeta + Z_EROFS_LEGACY_MAP_HEADER_SIZE;
-	z_erofs_write_indexes(ictx);
 
 	legacymetasize = ictx->metacur - compressmeta;
 	/* estimate if data compression saves space or not */
@@ -1164,7 +1171,6 @@ int erofs_commit_compressed_file(struct z_erofs_compress_ictx *ictx,
 		goto err_free_meta;
 	}
 	z_erofs_dedupe_commit(false);
-	z_erofs_write_mapheader(inode, compressmeta);
 
 	if (!ictx->fragemitted)
 		sbi->saved_by_deduplication += inode->fragment_size;
Signature · 1 line
2.43.5
author
#2 lore raw

PATCH 3/3 erofs-utils: mkfs: implement extent-based deduplication

In reply to Gao Xiang

Currently, only rolling-hash deduplication could be used for compressed data, and it is still single-threaded for now.

Before applying multi-threaded compression to that, let's allow extent-based compressed data deduplication if -Efragments is on.

This feature will only work if multi-threaded compression is active.

---
 include/erofs/dedupe.h |   8 ++++
 lib/Makefile.am        |   2 +-
 lib/compress.c         |  63 +++++++++++++++++---------
 lib/dedupe_ext.c       | 100 +++++++++++++++++++++++++++++++++++++++++
 mkfs/main.c            |  10 +++++
 5 files changed, 162 insertions(+), 21 deletions(-)
 create mode 100644 lib/dedupe_ext.c
include/erofs/dedupe.h +8 -0
index 1af08e3..ffb00a5 100644
@@ -32,6 +32,14 @@ void z_erofs_dedupe_commit(bool drop);
 int z_erofs_dedupe_init(unsigned int wsiz);
 void z_erofs_dedupe_exit(void);
 
+int z_erofs_dedupe_ext_insert(struct z_erofs_inmem_extent *e,
+			      u64 hash);
+erofs_blk_t z_erofs_dedupe_ext_match(struct erofs_sb_info *sbi,
+			u8 *encoded, unsigned int size, bool raw, u64 *hash);
+void z_erofs_dedupe_ext_commit(bool drop);
+int z_erofs_dedupe_ext_init(void);
+void z_erofs_dedupe_ext_exit(void);
+
 #ifdef __cplusplus
 }
 #endif
lib/Makefile.am +1 -1
index 9cddc92..bdc74ad 100644
@@ -35,7 +35,7 @@ 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 \
 		      fragments.c dedupe.c uuid_unparse.c uuid.c tar.c \
-		      block_list.c rebuild.c diskbuf.c bitops.c
+		      block_list.c rebuild.c diskbuf.c bitops.c dedupe_ext.c
 
 liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include
 if ENABLE_LZ4
lib/compress.c +43 -20

120 lines not on this page · raw message

lib/dedupe_ext.c new file +100 -0

100 lines not on this page · raw message

mkfs/main.c +10 -0
index b2396d0..00bd21a 100644
@@ -1374,6 +1374,15 @@ int main(int argc, char **argv)
 		}
 	}
 
+	if (cfg.c_fragments) {
+		err = z_erofs_dedupe_ext_init();
+		if (err) {
+			erofs_err("failed to initialize extent deduplication: %s",
+				  erofs_strerror(err));
+			goto exit;
+		}
+	}
+
 	if (cfg.c_chunkbits) {
 		err = erofs_blob_init(cfg.c_blobdev_path, 1 << cfg.c_chunkbits);
 		if (err)
@@ -1486,6 +1495,7 @@ exit:
 		erofs_iput(root);
 	z_erofs_compress_exit();
 	z_erofs_dedupe_exit();
+	z_erofs_dedupe_ext_exit();
 	blklst = erofs_blocklist_close();
 	if (blklst)
 		fclose(blklst);
Signature · 1 line
2.43.5