Skip to contentSkip to conversation

erofs-utils: mkfs: write lcluster index after compression

PATCH New Yifan Zhao 3 participants 7 messages Nov 6, 2023 – Dec 19, 2023 Library code

lore
Share & tools
lore

Yifan Zhao

author
#0 lore raw

PATCH 1/1 erofs-utils: mkfs: write lcluster index after compression

Currently mkfs writes the lcluster indexes(with on-disk format) in an extent by extent manner during the compression of a file, which is inflexible if we want to modify the indexes later in the multi-threaded compression scenario.

In order to support the multi-threaded compression feature of mkfs, this patch moves the writing of the lcluster indexes to take place after the file's compression is completed.

---
 include/erofs/dedupe.h |   4 +-
 lib/compress.c         | 366 ++++++++++++++++++++++++-----------------
 lib/dedupe.c           |   1 +
 3 files changed, 223 insertions(+), 148 deletions(-)
include/erofs/dedupe.h +3 -1
index 153bd4c..a3e365b 100644
@@ -16,7 +16,9 @@ struct z_erofs_inmem_extent {
 	erofs_blk_t blkaddr;
 	unsigned int compressedblks;
 	unsigned int length;
-	bool raw, partial;
+	bool raw, partial, reset_clusterofs;
+
+	struct list_head list;
 };
 
 struct z_erofs_dedupe_ctx {
lib/compress.c +219 -147

604 lines not on this page · raw message

lib/dedupe.c +1 -0
index 19a1c8d..993c7a3 100644
@@ -138,6 +138,7 @@ int z_erofs_dedupe_match(struct z_erofs_dedupe_ctx *ctx)
 		ctx->e.partial = e->partial ||
 			(window_size + extra < e->original_length);
 		ctx->e.raw = e->raw;
+		ctx->e.reset_clusterofs = false;
 		ctx->e.blkaddr = e->compressed_blkaddr;
 		ctx->e.compressedblks = e->compressed_blks;
 		return 0;
Signature · 1 line
2.42.1
author
#1 lore raw

PATCH v2 1/1 erofs-utils: mkfs: generate on-disk indexes after compression

Currently, mkfs generates the on-disk indexes of each compressed extent on the fly during compressing, which is inflexible if we'd like to merge sub-indexes of a file later for the multi-threaded compression scenarios.

Let's generate on-disk indexes after the compression for the file is completed.

---
Change since v1:
1. Polish commit message.
2. Iterate the extent list in a wrapper function to avoid massive change of indentation.
3. Remove the redundant reset_clusterofs field in struct z_erofs_inmem_extent. Instead, we check that if an uncompressed lcluster is aligned to 4K boundary and reset clusterofs to 0 during on-disk indexes generation. Note that this modification requires the ctx->head >= ctx->clusterofs being always true (lib/compress.c:328). Please reconfirm it as I may miss some corner cases.

 include/erofs/dedupe.h |   1 +
 lib/compress.c         | 253 ++++++++++++++++++++++++++---------------
 2 files changed, 163 insertions(+), 91 deletions(-)
include/erofs/dedupe.h +1 -0
index 153bd4c..3a413bf 100644
@@ -17,6 +17,7 @@ struct z_erofs_inmem_extent {
 	unsigned int compressedblks;
 	unsigned int length;
 	bool raw, partial;
+	struct list_head list;
 };
 
 struct z_erofs_dedupe_ctx {
lib/compress.c +162 -91

497 lines not on this page · raw message

Signature · 1 line
2.42.1
#2 lore raw

Re: [PATCH v2] erofs-utils: mkfs: generate on-disk indexes after compression

Hi Yifan,

Quoted 9 lines · Yifan Zhao

Currently, mkfs generates the on-disk indexes of each compressed extent on the fly during compressing, which is inflexible if we'd like to merge sub-indexes of a file later for the multi-threaded compression scenarios.

Let's generate on-disk indexes after the compression for the file is completed.

---

Sorry for the late reply. There are some issues on my side.
In order to make this work efficient. Let me to refine this commit as well.

Thanks,
Gao Xiang

#3 lore raw

PATCH v3 1/3 erofs-utils: lib: add z_erofs_need_refill()

In reply to Yifan Zhao

Let's remove redundant logic.

---
 lib/compress.c | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)
lib/compress.c +19 -20
index 61328ed..a5ef6e4 100644
@@ -166,6 +166,22 @@ static void z_erofs_write_indexes(struct z_erofs_vle_compress_ctx *ctx)
 	ctx->clusterofs = clusterofs + count;
 }
 
+static bool z_erofs_need_refill(struct z_erofs_vle_compress_ctx *ctx)
+{
+	const bool final = !ctx->remaining;
+	unsigned int qh_aligned, qh_after;
+
+	if (final || ctx->head < EROFS_CONFIG_COMPR_MAX_SZ)
+		return false;
+
+	qh_aligned = round_down(ctx->head, erofs_blksiz(ctx->inode->sbi));
+	qh_after = ctx->head - qh_aligned;
+	memmove(ctx->queue, ctx->queue + qh_aligned, ctx->tail - qh_aligned);
+	ctx->tail -= qh_aligned;
+	ctx->head = qh_after;
+	return true;
+}
+
 static int z_erofs_compress_dedupe(struct z_erofs_vle_compress_ctx *ctx,
 				   unsigned int *len)
 {
@@ -243,15 +259,7 @@ static int z_erofs_compress_dedupe(struct z_erofs_vle_compress_ctx *ctx,
 		DBG_BUGON(*len < dctx.e.length - delta);
 		*len -= dctx.e.length - delta;
 
-		if (ctx->head >= EROFS_CONFIG_COMPR_MAX_SZ) {
-			const unsigned int qh_aligned =
-				round_down(ctx->head, erofs_blksiz(sbi));
-			const unsigned int qh_after = ctx->head - qh_aligned;
-
-			memmove(ctx->queue, ctx->queue + qh_aligned,
-				*len + qh_after);
-			ctx->head = qh_after;
-			ctx->tail = qh_after + *len;
+		if (z_erofs_need_refill(ctx)) {
 			ret = -EAGAIN;
 			break;
 		}
@@ -413,7 +421,7 @@ static int vle_compress_one(struct z_erofs_vle_compress_ctx *ctx)
 		bool fix_dedupedfrag = ctx->fix_dedupedfrag;
 		unsigned int compressedsize;
 
-		if (z_erofs_compress_dedupe(ctx, &len) && !final)
+		if (z_erofs_compress_dedupe(ctx, &len))
 			break;
 
 		if (len <= ctx->pclustersize) {
@@ -568,17 +576,8 @@ frag_packing:
 		    z_erofs_fixup_deduped_fragment(ctx, len))
 			break;
 
-		if (!final && ctx->head >= EROFS_CONFIG_COMPR_MAX_SZ) {
-			const unsigned int qh_aligned =
-				round_down(ctx->head, blksz);
-			const unsigned int qh_after = ctx->head - qh_aligned;
-
-			memmove(ctx->queue, ctx->queue + qh_aligned,
-				len + qh_after);
-			ctx->head = qh_after;
-			ctx->tail = qh_after + len;
+		if (z_erofs_need_refill(ctx))
 			break;
-		}
 	}
 	return 0;
 
Signature · 1 line
2.39.3
#4 lore raw

PATCH v3 2/3 erofs-utils: lib: split vle_compress_one()

Split compression for each extent into a new helper for later reworking.

---
 lib/compress.c | 297 ++++++++++++++++++++++++-------------------------
 1 file changed, 147 insertions(+), 150 deletions(-)
lib/compress.c +147 -150
index a5ef6e4..eafbad1 100644
@@ -400,7 +400,8 @@ static bool z_erofs_fixup_deduped_fragment(struct z_erofs_vle_compress_ctx *ctx,
 	return true;
 }
 
-static int vle_compress_one(struct z_erofs_vle_compress_ctx *ctx)
+static int __z_erofs_compress_one(struct z_erofs_vle_compress_ctx *ctx,
+				  struct z_erofs_inmem_extent *e)
 {
 	static char dstbuf[EROFS_CONFIG_COMPR_MAX_SZ + EROFS_MAX_BLOCK_SIZE];
 	struct erofs_inode *inode = ctx->inode;
@@ -411,181 +412,177 @@ static int vle_compress_one(struct z_erofs_vle_compress_ctx *ctx)
 	unsigned int len = ctx->tail - ctx->head;
 	bool is_packed_inode = erofs_is_packed_inode(inode);
 	bool final = !ctx->remaining;
+	bool may_packing = (cfg.c_fragments && final && !is_packed_inode);
+	bool may_inline = (cfg.c_ztailpacking && final && !may_packing);
+	unsigned int compressedsize;
 	int ret;
 
-	while (len) {
-		bool may_packing = (cfg.c_fragments && final &&
-				   !is_packed_inode);
-		bool may_inline = (cfg.c_ztailpacking && final &&
-				  !may_packing);
-		bool fix_dedupedfrag = ctx->fix_dedupedfrag;
-		unsigned int compressedsize;
-
-		if (z_erofs_compress_dedupe(ctx, &len))
-			break;
-
-		if (len <= ctx->pclustersize) {
-			if (!final || !len)
-				break;
-			if (may_packing) {
-				if (inode->fragment_size && !fix_dedupedfrag) {
-					ctx->pclustersize = roundup(len, blksz);
-					goto fix_dedupedfrag;
-				}
-				ctx->e.length = len;
-				goto frag_packing;
+	if (len <= ctx->pclustersize) {
+		if (!final || !len)
+			return 1;
+		if (may_packing) {
+			if (inode->fragment_size && !ctx->fix_dedupedfrag) {
+				ctx->pclustersize = roundup(len, blksz);
+				goto fix_dedupedfrag;
 			}
-			if (!may_inline && len <= blksz)
-				goto nocompression;
+			e->length = len;
+			goto frag_packing;
 		}
+		if (!may_inline && len <= blksz)
+			goto nocompression;
+	}
 
-		ctx->e.length = min(len,
-				cfg.c_max_decompressed_extent_bytes);
+	e->length = min(len, cfg.c_max_decompressed_extent_bytes);
+	ret = erofs_compress_destsize(h, ctx->queue + ctx->head,
+				      &e->length, dst, ctx->pclustersize);
+	if (ret <= 0) {
+		erofs_err("failed to compress %s: %s", inode->i_srcpath,
+			  erofs_strerror(ret));
+		return ret;
+	}
 
-		ret = erofs_compress_destsize(h, ctx->queue + ctx->head,
-				&ctx->e.length, dst, ctx->pclustersize);
-		if (ret <= 0) {
-			erofs_err("failed to compress %s: %s", inode->i_srcpath,
-				  erofs_strerror(ret));
-			return ret;
-		}
+	compressedsize = ret;
+	/* even compressed size is smaller, there is no real gain */
+	if (!(may_inline && e->length == len && ret < blksz))
+		ret = roundup(ret, blksz);
 
-		compressedsize = ret;
-		/* even compressed size is smaller, there is no real gain */
-		if (!(may_inline && ctx->e.length == len && ret < blksz))
-			ret = roundup(ret, blksz);
-
-		/* check if there is enough gain to keep the compressed data */
-		if (ret * h->compress_threshold / 100 >= ctx->e.length) {
-			if (may_inline && len < blksz) {
-				ret = z_erofs_fill_inline_data(inode,
-						ctx->queue + ctx->head,
-						len, true);
-			} else {
-				may_inline = false;
-				may_packing = false;
+	/* check if there is enough gain to keep the compressed data */
+	if (ret * h->compress_threshold / 100 >= e->length) {
+		if (may_inline && len < blksz) {
+			ret = z_erofs_fill_inline_data(inode,
+					ctx->queue + ctx->head, len, true);
+		} else {
+			may_inline = false;
+			may_packing = false;
 nocompression:
-				ret = write_uncompressed_extent(ctx, &len, dst);
-			}
+			ret = write_uncompressed_extent(ctx, &len, dst);
+		}
 
-			if (ret < 0)
-				return ret;
-			ctx->e.length = ret;
+		if (ret < 0)
+			return ret;
+		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->e.compressedblks = 1;
-			ctx->e.raw = true;
-		} else if (may_packing && len == ctx->e.length &&
-			   compressedsize < ctx->pclustersize &&
-			   (!inode->fragment_size || fix_dedupedfrag)) {
+		/*
+		 * XXX: For now, we have to leave `ctx->compressedblk = 1'
+		 * since there is no way to generate compressed indexes after
+		 * the time that ztailpacking is decided.
+		 */
+		e->compressedblks = 1;
+		e->raw = true;
+	} else if (may_packing && len == e->length &&
+		   compressedsize < ctx->pclustersize &&
+		   (!inode->fragment_size || ctx->fix_dedupedfrag)) {
 frag_packing:
-			ret = z_erofs_pack_fragments(inode,
-						     ctx->queue + ctx->head,
-						     len, ctx->tof_chksum);
-			if (ret < 0)
-				return ret;
-			ctx->e.compressedblks = 0; /* indicate a fragment */
-			ctx->e.raw = false;
-			ctx->fragemitted = true;
-			fix_dedupedfrag = false;
-		/* tailpcluster should be less than 1 block */
-		} else if (may_inline && len == ctx->e.length &&
-			   compressedsize < blksz) {
-			if (ctx->clusterofs + len <= blksz) {
-				inode->eof_tailraw = malloc(len);
-				if (!inode->eof_tailraw)
-					return -ENOMEM;
-
-				memcpy(inode->eof_tailraw,
-				       ctx->queue + ctx->head, len);
-				inode->eof_tailrawsize = len;
-			}
-
-			ret = z_erofs_fill_inline_data(inode, dst,
-					compressedsize, false);
-			if (ret < 0)
-				return ret;
-			ctx->e.compressedblks = 1;
-			ctx->e.raw = false;
-		} else {
-			unsigned int tailused, padding;
+		ret = z_erofs_pack_fragments(inode, ctx->queue + ctx->head,
+					     len, ctx->tof_chksum);
+		if (ret < 0)
+			return ret;
+		e->compressedblks = 0; /* indicate a fragment */
+		e->raw = false;
+		ctx->fragemitted = true;
+	/* tailpcluster should be less than 1 block */
+	} else if (may_inline && len == e->length && compressedsize < blksz) {
+		if (ctx->clusterofs + len <= blksz) {
+			inode->eof_tailraw = malloc(len);
+			if (!inode->eof_tailraw)
+				return -ENOMEM;
+
+			memcpy(inode->eof_tailraw, ctx->queue + ctx->head, len);
+			inode->eof_tailrawsize = len;
+		}
 
-			/*
-			 * If there's space left for the last round when
-			 * deduping fragments, try to read the fragment and
-			 * recompress a little more to check whether it can be
-			 * filled up. Fix up the fragment if succeeds.
-			 * Otherwise, just drop it and go to packing.
-			 */
-			if (may_packing && len == ctx->e.length &&
-			    (compressedsize & (blksz - 1)) &&
-			    ctx->tail < sizeof(ctx->queue)) {
-				ctx->pclustersize =
-					roundup(compressedsize, blksz);
-				goto fix_dedupedfrag;
-			}
+		ret = z_erofs_fill_inline_data(inode, dst,
+				compressedsize, false);
+		if (ret < 0)
+			return ret;
+		e->compressedblks = 1;
+		e->raw = false;
+	} else {
+		unsigned int tailused, padding;
 
-			if (may_inline && len == ctx->e.length)
-				tryrecompress_trailing(ctx, h,
-						ctx->queue + ctx->head,
-						&ctx->e.length, dst,
-						&compressedsize);
+		/*
+		 * If there's space left for the last round when deduping
+		 * fragments, try to read the fragment and recompress a little
+		 * more to check whether it can be filled up.  Fix the fragment
+		 * if succeeds.  Otherwise, just drop it and go on packing.
+		 */
+		if (may_packing && len == e->length &&
+		    (compressedsize & (blksz - 1)) &&
+		    ctx->tail < sizeof(ctx->queue)) {
+			ctx->pclustersize = roundup(compressedsize, blksz);
+			goto fix_dedupedfrag;
+		}
 
-			ctx->e.compressedblks = BLK_ROUND_UP(sbi, compressedsize);
-			DBG_BUGON(ctx->e.compressedblks * blksz >=
-				  ctx->e.length);
+		if (may_inline && len == e->length)
+			tryrecompress_trailing(ctx, h, ctx->queue + ctx->head,
+					&e->length, dst, &compressedsize);
 
-			padding = 0;
-			tailused = compressedsize & (blksz - 1);
-			if (tailused)
-				padding = blksz - tailused;
-
-			/* zero out garbage trailing data for non-0padding */
-			if (!erofs_sb_has_lz4_0padding(sbi)) {
-				memset(dst + compressedsize, 0, padding);
-				padding = 0;
-			}
+		e->compressedblks = BLK_ROUND_UP(sbi, compressedsize);
+		DBG_BUGON(e->compressedblks * blksz >= e->length);
 
-			/* write compressed data */
-			erofs_dbg("Writing %u compressed data to %u of %u blocks",
-				  ctx->e.length, ctx->blkaddr,
-				  ctx->e.compressedblks);
+		padding = 0;
+		tailused = compressedsize & (blksz - 1);
+		if (tailused)
+			padding = blksz - tailused;
 
-			ret = blk_write(sbi, dst - padding, ctx->blkaddr,
-					ctx->e.compressedblks);
-			if (ret)
-				return ret;
-			ctx->e.raw = false;
-			may_inline = false;
-			may_packing = false;
+		/* zero out garbage trailing data for non-0padding */
+		if (!erofs_sb_has_lz4_0padding(sbi)) {
+			memset(dst + compressedsize, 0, padding);
+			padding = 0;
 		}
-		ctx->e.partial = false;
-		ctx->e.blkaddr = ctx->blkaddr;
-		if (!may_inline && !may_packing && !is_packed_inode)
-			(void)z_erofs_dedupe_insert(&ctx->e,
-						    ctx->queue + ctx->head);
-		ctx->blkaddr += ctx->e.compressedblks;
-		ctx->head += ctx->e.length;
-		len -= ctx->e.length;
 
-		if (fix_dedupedfrag &&
-		    z_erofs_fixup_deduped_fragment(ctx, len))
-			break;
+		/* write compressed data */
+		erofs_dbg("Writing %u compressed data to %u of %u blocks",
+			  e->length, ctx->blkaddr, e->compressedblks);
 
-		if (z_erofs_need_refill(ctx))
-			break;
+		ret = blk_write(sbi, dst - padding, ctx->blkaddr,
+				e->compressedblks);
+		if (ret)
+			return ret;
+		e->raw = false;
+		may_inline = false;
+		may_packing = false;
 	}
+	e->partial = false;
+	e->blkaddr = ctx->blkaddr;
+	if (!may_inline && !may_packing && !is_packed_inode)
+		(void)z_erofs_dedupe_insert(e, ctx->queue + ctx->head);
+	ctx->blkaddr += e->compressedblks;
+	ctx->head += e->length;
 	return 0;
 
 fix_dedupedfrag:
 	DBG_BUGON(!inode->fragment_size);
 	ctx->remaining += inode->fragment_size;
-	ctx->e.length = 0;
+	e->length = 0;
 	ctx->fix_dedupedfrag = true;
+	return 1;
+}
+
+static int z_erofs_compress_one(struct z_erofs_vle_compress_ctx *ctx)
+{
+	unsigned int len = ctx->tail - ctx->head;
+	int ret;
+
+	while (len) {
+		if (z_erofs_compress_dedupe(ctx, &len))
+			break;
+
+		ret = __z_erofs_compress_one(ctx, &ctx->e);
+		if (ret) {
+			if (ret > 0)
+				break;		/* need more data */
+			return ret;
+		}
+
+		len -= ctx->e.length;
+		if (ctx->fix_dedupedfrag && !ctx->fragemitted &&
+		    z_erofs_fixup_deduped_fragment(ctx, len))
+			break;
+
+		if (z_erofs_need_refill(ctx))
+			break;
+	}
 	return 0;
 }
 
@@ -964,7 +961,7 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd)
 			ctx.remaining -= rx;
 			ctx.tail += rx;
 
-			ret = vle_compress_one(&ctx);
+			ret = z_erofs_compress_one(&ctx);
 			if (ret)
 				goto err_free_idata;
 		}
Signature · 1 line
2.39.3
#6 lore raw

PATCH v3 3/3 erofs-utils: lib: generate compression indexes in memory first

In reply to Gao Xiang

From: Yifan Zhao <[email protected]>

Currently, mkfs generates the on-disk indexes of each compressed extent on the fly during compressing, which is inflexible if we'd like to merge sub-indexes of a file later for the multi-threaded scenarios.

Let's generate on-disk indexes after the compression is completed.

---
 lib/compress.c | 185 ++++++++++++++++++++++++++++++++-----------------
 1 file changed, 121 insertions(+), 64 deletions(-)
lib/compress.c +121 -64
index eafbad1..8f61f92 100644
@@ -28,9 +28,15 @@ struct erofs_compress_cfg {
 	bool enable;
 } erofs_ccfg[EROFS_MAX_COMPR_CFGS];
 
+struct z_erofs_extent_item {
+	struct list_head list;
+	struct z_erofs_inmem_extent e;
+};
+
 struct z_erofs_vle_compress_ctx {
 	u8 queue[EROFS_CONFIG_COMPR_MAX_SZ * 2];
-	struct z_erofs_inmem_extent e;	/* (lookahead) extent */
+	struct list_head extents;
+	struct z_erofs_extent_item *pivot;
 
 	struct erofs_inode *inode;
 	struct erofs_compress_cfg *ccfg;
@@ -65,20 +71,18 @@ static void z_erofs_write_indexes_final(struct z_erofs_vle_compress_ctx *ctx)
 	ctx->metacur += sizeof(di);
 }
 
-static void z_erofs_write_indexes(struct z_erofs_vle_compress_ctx *ctx)
+static void z_erofs_write_extent(struct z_erofs_vle_compress_ctx *ctx,
+				 struct z_erofs_inmem_extent *e)
 {
 	struct erofs_inode *inode = ctx->inode;
 	struct erofs_sb_info *sbi = inode->sbi;
 	unsigned int clusterofs = ctx->clusterofs;
-	unsigned int count = ctx->e.length;
+	unsigned int count = e->length;
 	unsigned int d0 = 0, d1 = (clusterofs + count) / erofs_blksiz(sbi);
 	struct z_erofs_lcluster_index di;
 	unsigned int type, advise;
 
-	if (!count)
-		return;
-
-	ctx->e.length = 0;	/* mark as written first */
+	DBG_BUGON(!count);
 	di.di_clusterofs = cpu_to_le16(ctx->clusterofs);
 
 	/* whether the tail-end (un)compressed block or not */
@@ -87,18 +91,18 @@ static void z_erofs_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(!ctx->e.raw && !cfg.c_ztailpacking && !cfg.c_fragments);
-		DBG_BUGON(ctx->e.partial);
-		type = ctx->e.raw ? Z_EROFS_LCLUSTER_TYPE_PLAIN :
+		DBG_BUGON(!e->raw && !cfg.c_ztailpacking && !cfg.c_fragments);
+		DBG_BUGON(e->partial);
+		type = e->raw ? Z_EROFS_LCLUSTER_TYPE_PLAIN :
 			Z_EROFS_LCLUSTER_TYPE_HEAD1;
 		advise = type << Z_EROFS_LI_LCLUSTER_TYPE_BIT;
 		di.di_advise = cpu_to_le16(advise);
 
 		if (inode->datalayout == EROFS_INODE_COMPRESSED_FULL &&
-		    !ctx->e.compressedblks)
+		    !e->compressedblks)
 			di.di_u.blkaddr = cpu_to_le32(inode->fragmentoff >> 32);
 		else
-			di.di_u.blkaddr = cpu_to_le32(ctx->e.blkaddr);
+			di.di_u.blkaddr = cpu_to_le32(e->blkaddr);
 		memcpy(ctx->metacur, &di, sizeof(di));
 		ctx->metacur += sizeof(di);
 
@@ -112,7 +116,7 @@ static void z_erofs_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(sbi)) {
 			type = Z_EROFS_LCLUSTER_TYPE_NONHEAD;
-			di.di_u.delta[0] = cpu_to_le16(ctx->e.compressedblks |
+			di.di_u.delta[0] = cpu_to_le16(e->compressedblks |
 						       Z_EROFS_LI_D0_CBLKCNT);
 			di.di_u.delta[1] = cpu_to_le16(d1);
 		} else if (d0) {
@@ -136,17 +140,17 @@ static void z_erofs_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 = ctx->e.raw ? Z_EROFS_LCLUSTER_TYPE_PLAIN :
+			type = e->raw ? Z_EROFS_LCLUSTER_TYPE_PLAIN :
 				Z_EROFS_LCLUSTER_TYPE_HEAD1;
 
 			if (inode->datalayout == EROFS_INODE_COMPRESSED_FULL &&
-			    !ctx->e.compressedblks)
+			    !e->compressedblks)
 				di.di_u.blkaddr = cpu_to_le32(inode->fragmentoff >> 32);
 			else
-				di.di_u.blkaddr = cpu_to_le32(ctx->e.blkaddr);
+				di.di_u.blkaddr = cpu_to_le32(e->blkaddr);
 
-			if (ctx->e.partial) {
-				DBG_BUGON(ctx->e.raw);
+			if (e->partial) {
+				DBG_BUGON(e->raw);
 				advise |= Z_EROFS_LI_PARTIAL_REF;
 			}
 		}
@@ -166,6 +170,20 @@ static void z_erofs_write_indexes(struct z_erofs_vle_compress_ctx *ctx)
 	ctx->clusterofs = clusterofs + count;
 }
 
+static void z_erofs_write_indexes(struct z_erofs_vle_compress_ctx *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_vle_compress_ctx *ctx)
 {
 	const bool final = !ctx->remaining;
@@ -182,13 +200,25 @@ static bool z_erofs_need_refill(struct z_erofs_vle_compress_ctx *ctx)
 	return true;
 }
 
+static void z_erofs_commit_extent(struct z_erofs_vle_compress_ctx *ctx,
+				  struct z_erofs_extent_item *ei)
+{
+	list_add_tail(&ei->list, &ctx->extents);
+	ctx->clusterofs = (ctx->clusterofs + ei->e.length) &
+			(erofs_blksiz(ctx->inode->sbi) - 1);
+
+}
+
 static int z_erofs_compress_dedupe(struct z_erofs_vle_compress_ctx *ctx,
 				   unsigned int *len)
 {
 	struct erofs_inode *inode = ctx->inode;
 	const unsigned int lclustermask = (1 << inode->z_logical_clusterbits) - 1;
 	struct erofs_sb_info *sbi = inode->sbi;
-	int ret = 0;
+	struct z_erofs_extent_item *ei = ctx->pivot;
+
+	if (!ei)
+		return 0;
 
 	/*
 	 * No need dedupe for packed inode since it is composed of
@@ -200,12 +230,12 @@ static int z_erofs_compress_dedupe(struct z_erofs_vle_compress_ctx *ctx,
 	do {
 		struct z_erofs_dedupe_ctx dctx = {
 			.start = ctx->queue + ctx->head - ({ int rc;
-				if (ctx->e.length <= erofs_blksiz(sbi))
+				if (ei->e.length <= erofs_blksiz(sbi))
 					rc = 0;
-				else if (ctx->e.length - erofs_blksiz(sbi) >= ctx->head)
+				else if (ei->e.length - erofs_blksiz(sbi) >= ctx->head)
 					rc = ctx->head;
 				else
-					rc = ctx->e.length - erofs_blksiz(sbi);
+					rc = ei->e.length - erofs_blksiz(sbi);
 				rc; }),
 			.end = ctx->queue + ctx->head + *len,
 			.cur = ctx->queue + ctx->head,
@@ -222,25 +252,31 @@ static int z_erofs_compress_dedupe(struct z_erofs_vle_compress_ctx *ctx,
 		 * decompresssion could be done as another try in practice.
 		 */
 		if (dctx.e.compressedblks > 1 &&
-		    ((ctx->clusterofs + ctx->e.length - delta) & lclustermask) +
+		    ((ctx->clusterofs + ei->e.length - delta) & lclustermask) +
 			dctx.e.length < 2 * (lclustermask + 1))
 			break;
 
+		ctx->pivot = malloc(sizeof(struct z_erofs_extent_item));
+		if (!ctx->pivot) {
+			z_erofs_commit_extent(ctx, ei);
+			return -ENOMEM;
+		}
+
 		if (delta) {
 			DBG_BUGON(delta < 0);
-			DBG_BUGON(!ctx->e.length);
+			DBG_BUGON(!ei->e.length);
 
 			/*
 			 * For big pcluster dedupe, if we decide to shorten the
 			 * previous big pcluster, make sure that the previous
 			 * CBLKCNT is still kept.
 			 */
-			if (ctx->e.compressedblks > 1 &&
-			    (ctx->clusterofs & lclustermask) + ctx->e.length
+			if (ei->e.compressedblks > 1 &&
+			    (ctx->clusterofs & lclustermask) + ei->e.length
 				- delta < 2 * (lclustermask + 1))
 				break;
-			ctx->e.partial = true;
-			ctx->e.length -= delta;
+			ei->e.partial = true;
+			ei->e.length -= delta;
 		}
 
 		/* fall back to noncompact indexes for deduplication */
@@ -253,39 +289,32 @@ static int z_erofs_compress_dedupe(struct z_erofs_vle_compress_ctx *ctx,
 		erofs_dbg("Dedupe %u %scompressed data (delta %d) to %u of %u blocks",
 			  dctx.e.length, dctx.e.raw ? "un" : "",
 			  delta, dctx.e.blkaddr, dctx.e.compressedblks);
-		z_erofs_write_indexes(ctx);
-		ctx->e = dctx.e;
+
+		z_erofs_commit_extent(ctx, ei);
+		ei = ctx->pivot;
+		init_list_head(&ei->list);
+		ei->e = dctx.e;
+
 		ctx->head += dctx.e.length - delta;
 		DBG_BUGON(*len < dctx.e.length - delta);
 		*len -= dctx.e.length - delta;
 
-		if (z_erofs_need_refill(ctx)) {
-			ret = -EAGAIN;
-			break;
-		}
+		if (z_erofs_need_refill(ctx))
+			return 1;
 	} while (*len);
-
 out:
-	z_erofs_write_indexes(ctx);
-	return ret;
+	z_erofs_commit_extent(ctx, ei);
+	ctx->pivot = NULL;
+	return 0;
 }
 
 static int write_uncompressed_extent(struct z_erofs_vle_compress_ctx *ctx,
-				     unsigned int *len, char *dst)
+				     unsigned int len, char *dst)
 {
-	int ret;
 	struct erofs_sb_info *sbi = ctx->inode->sbi;
-	unsigned int count, interlaced_offset, rightpart;
-
-	/* reset clusterofs to 0 if permitted */
-	if (!erofs_sb_has_lz4_0padding(sbi) && ctx->clusterofs &&
-	    ctx->head >= ctx->clusterofs) {
-		ctx->head -= ctx->clusterofs;
-		*len += ctx->clusterofs;
-		ctx->clusterofs = 0;
-	}
-
-	count = min(erofs_blksiz(sbi), *len);
+	unsigned int count = min(erofs_blksiz(sbi), len);
+	unsigned int interlaced_offset, rightpart;
+	int ret;
 
 	/* write interlaced uncompressed data if needed */
 	if (ctx->inode->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER)
@@ -455,7 +484,8 @@ static int __z_erofs_compress_one(struct z_erofs_vle_compress_ctx *ctx,
 			may_inline = false;
 			may_packing = false;
 nocompression:
-			ret = write_uncompressed_extent(ctx, &len, dst);
+			/* TODO: reset clusterofs to 0 if permitted */
+			ret = write_uncompressed_extent(ctx, len, dst);
 		}
 
 		if (ret < 0)
@@ -554,7 +584,6 @@ frag_packing:
 fix_dedupedfrag:
 	DBG_BUGON(!inode->fragment_size);
 	ctx->remaining += inode->fragment_size;
-	e->length = 0;
 	ctx->fix_dedupedfrag = true;
 	return 1;
 }
@@ -562,20 +591,32 @@ fix_dedupedfrag:
 static int z_erofs_compress_one(struct z_erofs_vle_compress_ctx *ctx)
 {
 	unsigned int len = ctx->tail - ctx->head;
-	int ret;
+	struct z_erofs_extent_item *ei;
 
 	while (len) {
-		if (z_erofs_compress_dedupe(ctx, &len))
+		int ret = z_erofs_compress_dedupe(ctx, &len);
+
+		if (ret > 0)
 			break;
+		else if (ret < 0)
+			return ret;
 
-		ret = __z_erofs_compress_one(ctx, &ctx->e);
+		DBG_BUGON(ctx->pivot);
+		ei = malloc(sizeof(*ei));
+		if (!ei)
+			return -ENOMEM;
+
+		init_list_head(&ei->list);
+		ret = __z_erofs_compress_one(ctx, &ei->e);
 		if (ret) {
+			free(ei);
 			if (ret > 0)
 				break;		/* need more data */
 			return ret;
 		}
 
-		len -= ctx->e.length;
+		len -= ei->e.length;
+		ctx->pivot = ei;
 		if (ctx->fix_dedupedfrag && !ctx->fragemitted &&
 		    z_erofs_fixup_deduped_fragment(ctx, len))
 			break;
@@ -939,7 +980,8 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd)
 	ctx.metacur = compressmeta + Z_EROFS_LEGACY_MAP_HEADER_SIZE;
 	ctx.head = ctx.tail = 0;
 	ctx.clusterofs = 0;
-	ctx.e.length = 0;
+	ctx.pivot = NULL;
+	init_list_head(&ctx.extents);
 	ctx.remaining = inode->i_size - inode->fragment_size;
 	ctx.fix_dedupedfrag = false;
 	ctx.fragemitted = false;
@@ -973,19 +1015,34 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd)
 	DBG_BUGON(compressed_blocks < !!inode->idata_size);
 	compressed_blocks -= !!inode->idata_size;
 
+	if (ctx.pivot) {
+		z_erofs_commit_extent(&ctx, ctx.pivot);
+		ctx.pivot = NULL;
+	}
+
 	/* generate an extent for the deduplicated fragment */
 	if (inode->fragment_size && !ctx.fragemitted) {
-		z_erofs_write_indexes(&ctx);
-		ctx.e.length = inode->fragment_size;
-		ctx.e.compressedblks = 0;
-		ctx.e.raw = false;
-		ctx.e.partial = false;
-		ctx.e.blkaddr = ctx.blkaddr;
+		struct z_erofs_extent_item *ei;
+
+		ei = malloc(sizeof(*ei));
+		if (!ei) {
+			ret = -ENOMEM;
+			goto err_free_idata;
+		}
+
+		ei->e = (struct z_erofs_inmem_extent) {
+			.length = inode->fragment_size,
+			.compressedblks = 0,
+			.raw = false,
+			.partial = false,
+			.blkaddr = ctx.blkaddr,
+		};
+		init_list_head(&ei->list);
+		z_erofs_commit_extent(&ctx, ei);
 	}
 	z_erofs_fragments_commit(inode);
 
 	z_erofs_write_indexes(&ctx);
-	z_erofs_write_indexes_final(&ctx);
 	legacymetasize = ctx.metacur - compressmeta;
 	/* estimate if data compression saves space or not */
 	if (!inode->fragment_size &&
Signature · 1 line
2.39.3