Skip to contentSkip to conversation

erofs-utils: support tail-packing inline compressed data

RFC PATCH Superseded Yue Hu 4 participants 6 messages Nov 17, 2021 – Dec 10, 2021 1 message not in the archive Library code

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Yue Hu

author
#1 lore raw

RFC PATCH v3 1/2 erofs-utils: support tail-packing inline compressed data

Currently, we only support tail-end inline data for uncompressed files, let's support it as well for compressed files.

The idea is from Xiang.

---
v3:
- based on commit 9fe440d0ac03 ("erofs-utils: mkfs: introduce --quiet option").
- move h_idata_size ahead of h_advise for compatibility.
- rename feature/update messages which i think they are more exact.

v2:
- add 2 bytes to record compressed size of tail-pcluster suggested from Xiang and update related code.

 include/erofs/internal.h |  1 +
 include/erofs_fs.h       | 10 ++++-
 lib/compress.c           | 83 ++++++++++++++++++++++++++++++----------
 lib/compressor.c         |  9 +++--
 lib/decompress.c         |  4 ++
 lib/inode.c              | 42 ++++++++++----------
 mkfs/main.c              |  7 ++++
 7 files changed, 108 insertions(+), 48 deletions(-)
include/erofs/internal.h +1 -0
index 8b154ed..54e5939 100644
@@ -110,6 +110,7 @@ EROFS_FEATURE_FUNCS(lz4_0padding, incompat, INCOMPAT_LZ4_0PADDING)
 EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
 EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
 EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
+EROFS_FEATURE_FUNCS(tail_packing, incompat, INCOMPAT_TAIL_PACKING)
 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
 
 #define EROFS_I_EA_INITED	(1 << 0)
include/erofs_fs.h +8 -2
index 66a68e3..0ebcd5b 100644
@@ -22,11 +22,13 @@
 #define EROFS_FEATURE_INCOMPAT_COMPR_CFGS	0x00000002
 #define EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER	0x00000002
 #define EROFS_FEATURE_INCOMPAT_CHUNKED_FILE	0x00000004
+#define EROFS_FEATURE_INCOMPAT_TAIL_PACKING	0x00000010
 #define EROFS_ALL_FEATURE_INCOMPAT		\
 	(EROFS_FEATURE_INCOMPAT_LZ4_0PADDING | \
 	 EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \
 	 EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER | \
-	 EROFS_FEATURE_INCOMPAT_CHUNKED_FILE)
+	 EROFS_FEATURE_INCOMPAT_CHUNKED_FILE | \
+	 EROFS_FEATURE_INCOMPAT_TAIL_PACKING)
 
 #define EROFS_SB_EXTSLOT_SIZE	16
 
@@ -266,13 +268,17 @@ struct z_erofs_lz4_cfgs {
  *                                  (4B) + 2B + (4B) if compacted 2B is on.
  * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
  * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
+ * bit 3 : tail-packing inline (un)compressed data
  */
 #define Z_EROFS_ADVISE_COMPACTED_2B		0x0001
 #define Z_EROFS_ADVISE_BIG_PCLUSTER_1		0x0002
 #define Z_EROFS_ADVISE_BIG_PCLUSTER_2		0x0004
+#define Z_EROFS_ADVISE_INLINE_DATA		0x0008
 
 struct z_erofs_map_header {
-	__le32	h_reserved1;
+	__le16	h_reserved1;
+	/* record the (un)compressed size of tail-packing pcluster */
+	__le16	h_idata_size;
 	__le16	h_advise;
 	/*
 	 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
lib/compress.c +62 -21

180 lines not on this page · raw message

lib/compressor.c +5 -4
index 89c1be1..9b9d2ff 100644
@@ -23,7 +23,7 @@ int erofs_compress_destsize(struct erofs_compress *c,
 			    void *src, unsigned int *srcsize,
 			    void *dst, unsigned int dstsize)
 {
-	unsigned int uncompressed_size;
+	unsigned int compressed_size;
 	int ret;
 
 	DBG_BUGON(!c->alg);
@@ -35,9 +35,10 @@ int erofs_compress_destsize(struct erofs_compress *c,
 		return ret;
 
 	/* check if there is enough gains to compress */
-	uncompressed_size = *srcsize;
-	if (roundup(ret, EROFS_BLKSIZ) >= uncompressed_size *
-	    c->compress_threshold / 100)
+	compressed_size = *srcsize <= EROFS_BLKSIZ ? ret :
+			  roundup(ret, EROFS_BLKSIZ);
+
+	if (*srcsize <= compressed_size * c->compress_threshold / 100)
 		return -EAGAIN;
 	return ret;
 }
lib/decompress.c +4 -0
index 2ee1439..6f4ecc2 100644
@@ -7,6 +7,7 @@
 
 #include "erofs/decompress.h"
 #include "erofs/err.h"
+#include "erofs/print.h"
 
 #ifdef LZ4_ENABLED
 #include <lz4.h>
@@ -48,6 +49,9 @@ static int z_erofs_decompress_lz4(struct z_erofs_decompress_req *rq)
 					  rq->decodedlength);
 
 	if (ret != (int)rq->decodedlength) {
+		erofs_err("failed to %s decompress %d in[%u, %u] out[%u]",
+			  rq->partial_decoding ? "partial" : "full",
+			  ret, rq->inputsize, inputmargin, rq->decodedlength);
 		ret = -EIO;
 		goto out;
 	}
lib/inode.c +21 -21

81 lines not on this page · raw message

mkfs/main.c +7 -0
index 055d077..7505ff6 100644
@@ -47,6 +47,7 @@ static struct option long_options[] = {
 	{"compress-hints", required_argument, NULL, 10},
 	{"chunksize", required_argument, NULL, 11},
 	{"quiet", no_argument, 0, 12},
+	{"inline-data", no_argument, NULL, 13},
 #ifdef WITH_ANDROID
 	{"mount-point", required_argument, NULL, 512},
 	{"product-out", required_argument, NULL, 513},
@@ -95,6 +96,7 @@ static void usage(void)
 	      " --help                display this help and exit\n"
 	      " --max-extent-bytes=#  set maximum decompressed extent size # in bytes\n"
 	      " --quiet               quiet execution (do not write anything to standard output.)\n"
+	      " --inline-data         tail-packing inline compressed data\n"
 #ifndef NDEBUG
 	      " --random-pclusterblks randomize pclusterblks for big pcluster (debugging only)\n"
 #endif
@@ -349,6 +351,9 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 		case 12:
 			quiet = true;
 			break;
+		case 13:
+			erofs_sb_set_tail_packing();
+			break;
 		case 1:
 			usage();
 			exit(0);
@@ -607,6 +612,8 @@ int main(int argc, char **argv)
 	erofs_show_config();
 	if (erofs_sb_has_chunked_file())
 		erofs_warn("EXPERIMENTAL chunked file feature in use. Use at your own risk!");
+	if (erofs_sb_has_tail_packing())
+		erofs_warn("EXPERIMENTAL compression inline data feature in use. Use at your own risk!");
 	erofs_set_fs_root(cfg.c_src_path);
 #ifndef NDEBUG
 	if (cfg.c_random_pclusterblks)
Signature · 1 line
2.17.1
#2 lore raw

RFC PATCH v3 2/2 erofs-utils: fuse: support tail-packing inline compressed data

Add tail-packing inline compressed data support for erofsfuse.

---
v3:
- remove z_idata_addr, add z_idata_headlcn instead of m_taillcn.
- add bug_on for legacy if enable inline and disable big pcluster.
- extract z_erofs_do_map_blocks() instead of added z_erofs_map_tail_data_blocks() with similar logic.

v2:
- add tail-packing information to inode and get it on first read.
- update tail-packing checking logic.

 include/erofs/internal.h |   2 +
 lib/decompress.c         |   3 -
 lib/zmap.c               | 136 +++++++++++++++++++++++++++++++++------
 3 files changed, 120 insertions(+), 21 deletions(-)
include/erofs/internal.h +2 -0
index 54e5939..5d1a44c 100644
@@ -172,6 +172,8 @@ struct erofs_inode {
 			uint8_t  z_algorithmtype[2];
 			uint8_t  z_logical_clusterbits;
 			uint8_t  z_physical_clusterblks;
+			uint16_t z_idata_size;
+			uint32_t z_idata_headlcn;
 		};
 	};
 #ifdef WITH_ANDROID
lib/decompress.c +0 -3
index 6f4ecc2..806ac91 100644
@@ -71,9 +71,6 @@ out:
 int z_erofs_decompress(struct z_erofs_decompress_req *rq)
 {
 	if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED) {
-		if (rq->inputsize != EROFS_BLKSIZ)
-			return -EFSCORRUPTED;
-
 		DBG_BUGON(rq->decodedlength > EROFS_BLKSIZ);
 		DBG_BUGON(rq->decodedlength < rq->decodedskip);
 
lib/zmap.c +118 -18

206 lines not on this page · raw message

Signature · 1 line
2.17.1
#3 lore raw

Re: [RFC PATCH v3 2/2] erofs-utils: fuse: support tail-packing inline compressed data

Hi Yue,

Quoted 3 lines · Yue Hu via Linux-erofs

Add tail-packing inline compressed data support for erofsfuse.

Sorry about long delay, I'm very busy in container use cases now. I've checked your patch, some inlined comments below.

Quoted 34 lines

---
v3:
- remove z_idata_addr, add z_idata_headlcn instead of m_taillcn.
- add bug_on for legacy if enable inline and disable big pcluster.
- extract z_erofs_do_map_blocks() instead of added z_erofs_map_tail_data_blocks() with similar logic.

v2:
- add tail-packing information to inode and get it on first read.
- update tail-packing checking logic.

 include/erofs/internal.h |   2 +
 lib/decompress.c         |   3 -
 lib/zmap.c               | 136 +++++++++++++++++++++++++++++++++------
 3 files changed, 120 insertions(+), 21 deletions(-)
include/erofs/internal.h +2 -0
index 54e5939..5d1a44c 100644
@@ -172,6 +172,8 @@ struct erofs_inode {
 			uint8_t  z_algorithmtype[2];
 			uint8_t  z_logical_clusterbits;
 			uint8_t  z_physical_clusterblks;
+			uint16_t z_idata_size;
+			uint32_t z_idata_headlcn;
 		};
 	};
 #ifdef WITH_ANDROID
lib/decompress.c +0 -0
index 6f4ecc2..806ac91 100644

lib/decompress.c out: · line 76

Quoted 6 lines
lib/decompress.c +0 -3
@@ -71,9 +71,6 @@ out:
 int z_erofs_decompress(struct z_erofs_decompress_req *rq)
 {
 	if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED) {
-		if (rq->inputsize != EROFS_BLKSIZ)
-			return -EFSCORRUPTED;
-
		if (rq->inputsize > EROFS_BLKSIZ)
			return -EFSCORRUPTED;
Quoted 73 lines
lib/decompress.c +0 -0
 		DBG_BUGON(rq->decodedlength > EROFS_BLKSIZ);
 		DBG_BUGON(rq->decodedlength < rq->decodedskip);
 
lib/zmap.c +49 -0
index 458030b..42783e5 100644
@@ -10,6 +10,9 @@
 #include "erofs/io.h"
 #include "erofs/print.h"
 
+static int z_erofs_do_map_blocks(struct erofs_inode *vi,
+				 struct erofs_map_blocks *map);
+
 int z_erofs_fill_inode(struct erofs_inode *vi)
 {
 	if (!erofs_sb_has_big_pcluster() &&
@@ -18,12 +21,69 @@ int z_erofs_fill_inode(struct erofs_inode *vi)
 		vi->z_algorithmtype[0] = 0;
 		vi->z_algorithmtype[1] = 0;
 		vi->z_logical_clusterbits = LOG_BLOCK_SIZE;
+		vi->z_idata_size = 0;
 
 		vi->flags |= EROFS_I_Z_INITED;
+		DBG_BUGON(erofs_sb_has_tail_packing());
 	}
 	return 0;
 }
 
+static erofs_off_t compacted_inline_data_addr(struct erofs_inode *vi,
+					      unsigned int totalidx)
+{
+	const erofs_off_t ebase = round_up(iloc(vi->nid) + vi->inode_isize +
+				  vi->xattr_isize, 8) +
+				  sizeof(struct z_erofs_map_header);
+	unsigned int compacted_4b_initial, compacted_4b_end;
+	unsigned int compacted_2b;
+	erofs_off_t addr;
+
+	compacted_4b_initial = (32 - ebase % 32) / 4;
+	if (compacted_4b_initial == 32 / 4)
+		compacted_4b_initial = 0;
+
+	if (compacted_4b_initial > totalidx) {
+		compacted_4b_initial = 0;
+		compacted_2b = 0;
+	} else if (vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B) {
+		compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
+	} else
+		compacted_2b = 0;
+
+	compacted_4b_end = totalidx - compacted_4b_initial - compacted_2b;
+
+	addr = ebase + compacted_4b_initial * 4 + compacted_2b * 2;
+	if (compacted_4b_end > 1)
+		addr += (compacted_4b_end/2) * 8;
+	if (compacted_4b_end % 2)
+		addr += 8;
+
+	return addr;
+}
+
+static erofs_off_t legacy_inline_data_addr(struct erofs_inode *vi,
+					   unsigned int totalidx)
+{
+	return Z_EROFS_VLE_LEGACY_INDEX_ALIGN(iloc(vi->nid) + vi->inode_isize +
+					      vi->xattr_isize) +
+		totalidx * sizeof(struct z_erofs_vle_decompressed_index);
+}
+
+static erofs_off_t z_erofs_inline_data_addr(struct erofs_inode *vi)
+{
+	const unsigned int datamode = vi->datalayout;

lib/zmap.c int z_erofs_fill_inode(struct erofs_inode *vi) · line 85

Quoted 10 lines
lib/zmap.c +10 -0
+	const unsigned int totalidx = DIV_ROUND_UP(vi->i_size, EROFS_BLKSIZ);
+
+	if (datamode == EROFS_INODE_FLAT_COMPRESSION)
+		return compacted_inline_data_addr(vi, totalidx);
+
+	if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY)
+		return legacy_inline_data_addr(vi, totalidx);
+
+	return -EINVAL;
+}

no need these three new functions if introducing
EROFS_GET_BLOCKS_FINDTAIL, see below..

Quoted 13 lines
lib/zmap.c +2 -0
+
 static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 {
 	int ret;
@@ -44,6 +104,7 @@ static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 
 	h = (struct z_erofs_map_header *)buf;
 	vi->z_advise = le16_to_cpu(h->h_advise);
+	vi->z_idata_size = le16_to_cpu(h->h_idata_size);
 	vi->z_algorithmtype[0] = h->h_algorithmtype & 15;
 	vi->z_algorithmtype[1] = h->h_algorithmtype >> 4;
 

lib/zmap.c static int z_erofs_fill_inode_lazy(struct erofs_inode *vi) · line 131

Quoted 10 lines
lib/zmap.c +7 -0
@@ -61,6 +122,16 @@ static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 			  vi->nid * 1ULL);
 		return -EFSCORRUPTED;
 	}
+
+	if (vi->z_idata_size) {
+		struct erofs_map_blocks map = { .m_la = vi->i_size - 1 };
+
+		ret = z_erofs_do_map_blocks(vi, &map);
+		if (ret)
+			return ret;

How about introducing another mode called
EROFS_GET_BLOCKS_FINDTAIL

which implys .m_la = vi->i_size - 1 internally, so we won't need to handle .m_la here.

lib/zmap.c static int z_erofs_fill_inode_lazy(struct erofs_inode *vi) · line 132

Quoted 1 line
lib/zmap.c +1 -0
+		vi->z_idata_headlcn = map.m_la >> vi->z_logical_clusterbits;

Also updaing vi->z_idata_headlcn when EROFS_GET_BLOCKS_FINDTAIL.

Quoted 60 lines
lib/zmap.c +13 -15
+	}
+
 	vi->flags |= EROFS_I_Z_INITED;
 	return 0;
 }
@@ -374,7 +445,8 @@ static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
 }
 
 static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
-					    unsigned int initial_lcn)
+					    unsigned int initial_lcn,
+					    bool idatamap)
 {
 	struct erofs_inode *const vi = m->inode;
 	struct erofs_map_blocks *const map = m->map;
@@ -384,6 +456,12 @@ static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
 
 	DBG_BUGON(m->type != Z_EROFS_VLE_CLUSTER_TYPE_PLAIN &&
 		  m->type != Z_EROFS_VLE_CLUSTER_TYPE_HEAD);
+
+	if (idatamap) {
+		map->m_plen = vi->z_idata_size;
+		return 0;
+	}
+
 	if (!(map->m_flags & EROFS_MAP_ZIPPED) ||
 	    !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1)) {
 		map->m_plen = 1 << lclusterbits;
@@ -440,8 +518,8 @@ err_bonus_cblkcnt:
 	return -EFSCORRUPTED;
 }
 
-int z_erofs_map_blocks_iter(struct erofs_inode *vi,
-			    struct erofs_map_blocks *map)
+static int z_erofs_do_map_blocks(struct erofs_inode *vi,
+				 struct erofs_map_blocks *map)
 {
 	struct z_erofs_maprecorder m = {
 		.inode = vi,
@@ -452,18 +530,7 @@ int z_erofs_map_blocks_iter(struct erofs_inode *vi,
 	unsigned int lclusterbits, endoff;
 	unsigned long initial_lcn;
 	unsigned long long ofs, end;
-
-	/* when trying to read beyond EOF, leave it unmapped */
-	if (map->m_la >= vi->i_size) {
-		map->m_llen = map->m_la + 1 - vi->i_size;
-		map->m_la = vi->i_size;
-		map->m_flags = 0;
-		goto out;
-	}
-
-	err = z_erofs_fill_inode_lazy(vi);
-	if (err)
-		goto out;
+	bool idatamap;
 
 	lclusterbits = vi->z_logical_clusterbits;
 	ofs = map->m_la;

lib/zmap.c int z_erofs_map_blocks_iter(struct erofs_inode *vi, · line 582

Quoted 6 lines
lib/zmap.c +3 -0
@@ -510,19 +577,52 @@ int z_erofs_map_blocks_iter(struct erofs_inode *vi,
 		goto out;
 	}
 
+	/* check if mapping tail-packing data */
+	idatamap = vi->z_idata_size && (ofs == vi->i_size - 1 ||
+		   m.lcn == vi->z_idata_headlcn);

better namin as `is_idata'...

I think no need to handle ofs == vi->i_size - 1 specially here if EROFS_GET_BLOCKS_FINDTAIL is introduced...

lib/zmap.c int z_erofs_map_blocks_iter(struct erofs_inode *vi, · line 587

Quoted 6 lines
lib/zmap.c +4 -1
+
+	/* need to trim tail-packing data if beyond file size */
 	map->m_llen = end - map->m_la;
-	map->m_pa = blknr_to_addr(m.pblk);
+	if (idatamap && end > vi->i_size)
+		map->m_llen -= end - vi->i_size;

No need as well?

lib/zmap.c int z_erofs_map_blocks_iter(struct erofs_inode *vi, · line 590

Quoted 4 lines
lib/zmap.c +2 -1
-	err = z_erofs_get_extent_compressedlen(&m, initial_lcn);
+	if (idatamap && (vi->z_advise & Z_EROFS_ADVISE_INLINE_DATA)) {
+		map->m_pa = z_erofs_inline_data_addr(vi);

How about setting another u32 z_idataoff when EROFS_GET_BLOCKS_FINDTAIL so we won't need to introduce another calculate methods instead?

Thanks,
Gao Xiang

#4 lore raw

Re: [RFC PATCH v3 2/2] erofs-utils: fuse: support tail-packing inline compressed data

Hi Xiang,

Quoted 132 lines · On Mon, 6 Dec 2021 08:39:04 +0800 Gao Xiang <[email protected]> wrote:

Hi Yue,

Quoted 3 lines · Yue Hu via Linux-erofs

Add tail-packing inline compressed data support for erofsfuse.

Sorry about long delay, I'm very busy in container use cases now. I've checked your patch, some inlined comments below.

Quoted 34 lines

---
v3:
- remove z_idata_addr, add z_idata_headlcn instead of m_taillcn.
- add bug_on for legacy if enable inline and disable big pcluster.
- extract z_erofs_do_map_blocks() instead of added z_erofs_map_tail_data_blocks() with similar logic.

v2:
- add tail-packing information to inode and get it on first read.
- update tail-packing checking logic.

 include/erofs/internal.h |   2 +
 lib/decompress.c         |   3 -
 lib/zmap.c               | 136 +++++++++++++++++++++++++++++++++------
 3 files changed, 120 insertions(+), 21 deletions(-)
include/erofs/internal.h +2 -0
index 54e5939..5d1a44c 100644
@@ -172,6 +172,8 @@ struct erofs_inode {
 			uint8_t  z_algorithmtype[2];
 			uint8_t  z_logical_clusterbits;
 			uint8_t  z_physical_clusterblks;
+			uint16_t z_idata_size;
+			uint32_t z_idata_headlcn;
 		};
 	};
 #ifdef WITH_ANDROID
lib/decompress.c +0 -0
index 6f4ecc2..806ac91 100644

lib/decompress.c out: · line 76

Quoted 6 lines
lib/decompress.c +0 -3
@@ -71,9 +71,6 @@ out:
 int z_erofs_decompress(struct z_erofs_decompress_req *rq)
 {
 	if (rq->alg == Z_EROFS_COMPRESSION_SHIFTED) {
-		if (rq->inputsize != EROFS_BLKSIZ)
-			return -EFSCORRUPTED;
-  
		if (rq->inputsize > EROFS_BLKSIZ)
			return -EFSCORRUPTED;
Quoted 78 lines
lib/decompress.c +0 -0
 		DBG_BUGON(rq->decodedlength > EROFS_BLKSIZ);
 		DBG_BUGON(rq->decodedlength < rq->decodedskip);
 
lib/zmap.c +54 -0
index 458030b..42783e5 100644
@@ -10,6 +10,9 @@
 #include "erofs/io.h"
 #include "erofs/print.h"
 
+static int z_erofs_do_map_blocks(struct erofs_inode *vi,
+				 struct erofs_map_blocks *map);
+
 int z_erofs_fill_inode(struct erofs_inode *vi)
 {
 	if (!erofs_sb_has_big_pcluster() &&
@@ -18,12 +21,69 @@ int z_erofs_fill_inode(struct erofs_inode *vi)
 		vi->z_algorithmtype[0] = 0;
 		vi->z_algorithmtype[1] = 0;
 		vi->z_logical_clusterbits = LOG_BLOCK_SIZE;
+		vi->z_idata_size = 0;
 
 		vi->flags |= EROFS_I_Z_INITED;
+		DBG_BUGON(erofs_sb_has_tail_packing());
 	}
 	return 0;
 }
 
+static erofs_off_t compacted_inline_data_addr(struct erofs_inode *vi,
+					      unsigned int totalidx)
+{
+	const erofs_off_t ebase = round_up(iloc(vi->nid) + vi->inode_isize +
+				  vi->xattr_isize, 8) +
+				  sizeof(struct z_erofs_map_header);
+	unsigned int compacted_4b_initial, compacted_4b_end;
+	unsigned int compacted_2b;
+	erofs_off_t addr;
+
+	compacted_4b_initial = (32 - ebase % 32) / 4;
+	if (compacted_4b_initial == 32 / 4)
+		compacted_4b_initial = 0;
+
+	if (compacted_4b_initial > totalidx) {
+		compacted_4b_initial = 0;
+		compacted_2b = 0;
+	} else if (vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B) {
+		compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
+	} else
+		compacted_2b = 0;
+
+	compacted_4b_end = totalidx - compacted_4b_initial - compacted_2b;
+
+	addr = ebase + compacted_4b_initial * 4 + compacted_2b * 2;
+	if (compacted_4b_end > 1)
+		addr += (compacted_4b_end/2) * 8;
+	if (compacted_4b_end % 2)
+		addr += 8;
+
+	return addr;
+}
+
+static erofs_off_t legacy_inline_data_addr(struct erofs_inode *vi,
+					   unsigned int totalidx)
+{
+	return Z_EROFS_VLE_LEGACY_INDEX_ALIGN(iloc(vi->nid) + vi->inode_isize +
+					      vi->xattr_isize) +
+		totalidx * sizeof(struct z_erofs_vle_decompressed_index);
+}
+
+static erofs_off_t z_erofs_inline_data_addr(struct erofs_inode *vi)
+{
+	const unsigned int datamode = vi->datalayout;
+	const unsigned int totalidx = DIV_ROUND_UP(vi->i_size, EROFS_BLKSIZ);
+
+	if (datamode == EROFS_INODE_FLAT_COMPRESSION)
+		return compacted_inline_data_addr(vi, totalidx);
+
Quoted 8 lines

lib/zmap.c int z_erofs_fill_inode(struct erofs_inode *vi) · line 85

Quoted 5 lines
lib/zmap.c +5 -0
+	if (datamode == EROFS_INODE_FLAT_COMPRESSION_LEGACY)
+		return legacy_inline_data_addr(vi, totalidx);
+
+	return -EINVAL;
+}  

no need these three new functions if introducing
EROFS_GET_BLOCKS_FINDTAIL, see below..

Let me optimize the logic.

Quoted 13 lines
Quoted 13 lines
lib/zmap.c +2 -0
+
 static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 {
 	int ret;
@@ -44,6 +104,7 @@ static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 
 	h = (struct z_erofs_map_header *)buf;
 	vi->z_advise = le16_to_cpu(h->h_advise);
+	vi->z_idata_size = le16_to_cpu(h->h_idata_size);
 	vi->z_algorithmtype[0] = h->h_algorithmtype & 15;
 	vi->z_algorithmtype[1] = h->h_algorithmtype >> 4;
 
Quoted 14 lines

lib/zmap.c static int z_erofs_fill_inode_lazy(struct erofs_inode *vi) · line 131

Quoted 11 lines
lib/zmap.c +7 -0
@@ -61,6 +122,16 @@ static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 			  vi->nid * 1ULL);
 		return -EFSCORRUPTED;
 	}
+
+	if (vi->z_idata_size) {
+		struct erofs_map_blocks map = { .m_la = vi->i_size - 1 };
+
+		ret = z_erofs_do_map_blocks(vi, &map);
+		if (ret)
+			return ret;  

How about introducing another mode called
EROFS_GET_BLOCKS_FINDTAIL

Aha, nice name. That flag have leaped into my mind :)

Quoted 7 lines

which implys .m_la = vi->i_size - 1 internally, so we won't need to handle .m_la here.

lib/zmap.c static int z_erofs_fill_inode_lazy(struct erofs_inode *vi) · line 132

Quoted 1 line
lib/zmap.c +1 -0
+		vi->z_idata_headlcn = map.m_la >> vi->z_logical_clusterbits;  

Also updaing vi->z_idata_headlcn when EROFS_GET_BLOCKS_FINDTAIL.

Right.

Quoted 64 lines
Quoted 64 lines
lib/zmap.c +13 -15
+	}
+
 	vi->flags |= EROFS_I_Z_INITED;
 	return 0;
 }
@@ -374,7 +445,8 @@ static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
 }
 
 static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
-					    unsigned int initial_lcn)
+					    unsigned int initial_lcn,
+					    bool idatamap)
 {
 	struct erofs_inode *const vi = m->inode;
 	struct erofs_map_blocks *const map = m->map;
@@ -384,6 +456,12 @@ static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
 
 	DBG_BUGON(m->type != Z_EROFS_VLE_CLUSTER_TYPE_PLAIN &&
 		  m->type != Z_EROFS_VLE_CLUSTER_TYPE_HEAD);
+
+	if (idatamap) {
+		map->m_plen = vi->z_idata_size;
+		return 0;
+	}
+
 	if (!(map->m_flags & EROFS_MAP_ZIPPED) ||
 	    !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1)) {
 		map->m_plen = 1 << lclusterbits;
@@ -440,8 +518,8 @@ err_bonus_cblkcnt:
 	return -EFSCORRUPTED;
 }
 
-int z_erofs_map_blocks_iter(struct erofs_inode *vi,
-			    struct erofs_map_blocks *map)
+static int z_erofs_do_map_blocks(struct erofs_inode *vi,
+				 struct erofs_map_blocks *map)
 {
 	struct z_erofs_maprecorder m = {
 		.inode = vi,
@@ -452,18 +530,7 @@ int z_erofs_map_blocks_iter(struct erofs_inode *vi,
 	unsigned int lclusterbits, endoff;
 	unsigned long initial_lcn;
 	unsigned long long ofs, end;
-
-	/* when trying to read beyond EOF, leave it unmapped */
-	if (map->m_la >= vi->i_size) {
-		map->m_llen = map->m_la + 1 - vi->i_size;
-		map->m_la = vi->i_size;
-		map->m_flags = 0;
-		goto out;
-	}
-
-	err = z_erofs_fill_inode_lazy(vi);
-	if (err)
-		goto out;
+	bool idatamap;
 
 	lclusterbits = vi->z_logical_clusterbits;
 	ofs = map->m_la;
@@ -510,19 +577,52 @@ int z_erofs_map_blocks_iter(struct erofs_inode *vi,
 		goto out;
 	}
 
Quoted 5 lines

lib/zmap.c int z_erofs_map_blocks_iter(struct erofs_inode *vi, · line 582

Quoted 3 lines
lib/zmap.c +3 -0
+	/* check if mapping tail-packing data */
+	idatamap = vi->z_idata_size && (ofs == vi->i_size - 1 ||
+		   m.lcn == vi->z_idata_headlcn);  

better namin as `is_idata'...

Okay.

Quoted 3 lines

I think no need to handle ofs == vi->i_size - 1 specially here if EROFS_GET_BLOCKS_FINDTAIL is introduced...

Right.

Quoted 9 lines

lib/zmap.c int z_erofs_map_blocks_iter(struct erofs_inode *vi, · line 587

Quoted 7 lines
lib/zmap.c +4 -1
+
+	/* need to trim tail-packing data if beyond file size */
 	map->m_llen = end - map->m_la;
-	map->m_pa = blknr_to_addr(m.pblk);
+	if (idatamap && end > vi->i_size)
+		map->m_llen -= end - vi->i_size;  

No need as well?

Let me confirm.

Quoted 8 lines

lib/zmap.c int z_erofs_map_blocks_iter(struct erofs_inode *vi, · line 590

Quoted 5 lines
lib/zmap.c +2 -1
-	err = z_erofs_get_extent_compressedlen(&m, initial_lcn);
+	if (idatamap && (vi->z_advise & Z_EROFS_ADVISE_INLINE_DATA)) {
+		map->m_pa = z_erofs_inline_data_addr(vi);  

How about setting another u32 z_idataoff when EROFS_GET_BLOCKS_FINDTAIL so we won't need to introduce another calculate methods instead?

Agree, need to improve it.

Quoted 3 lines

Thanks,
Gao Xiang

#5 lore raw
In reply to Yue Hu

Hi Yue,

Quoted 6 lines · Yue Hu

Currently, we only support tail-end inline data for uncompressed files, let's support it as well for compressed files.

The idea is from Xiang.

Sorry about delay. Finally I seeked some time to look into this...

Quoted 24 lines

---
v3:
- based on commit 9fe440d0ac03 ("erofs-utils: mkfs: introduce --quiet option").
- move h_idata_size ahead of h_advise for compatibility.
- rename feature/update messages which i think they are more exact.

v2:
- add 2 bytes to record compressed size of tail-pcluster suggested from Xiang and update related code.

 include/erofs/internal.h |  1 +
 include/erofs_fs.h       | 10 ++++-
 lib/compress.c           | 83 ++++++++++++++++++++++++++++++----------
 lib/compressor.c         |  9 +++--
 lib/decompress.c         |  4 ++
 lib/inode.c              | 42 ++++++++++----------
 mkfs/main.c              |  7 ++++
 7 files changed, 108 insertions(+), 48 deletions(-)
include/erofs/internal.h +0 -0
index 8b154ed..54e5939 100644

include/erofs/internal.h EROFS_FEATURE_FUNCS(lz4_0padding, incompat, INCOMPAT_LZ4_0PADDING) · line 113

Quoted 4 lines
include/erofs/internal.h +1 -0
@@ -110,6 +110,7 @@ EROFS_FEATURE_FUNCS(lz4_0padding, incompat, INCOMPAT_LZ4_0PADDING)
 EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
 EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
 EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
+EROFS_FEATURE_FUNCS(tail_packing, incompat, INCOMPAT_TAIL_PACKING)

How about moving fuse support as [PATCH 1/2] and introducing these?

Also the on-disk feature name is somewhat confusing, maybe INCOMPAT_ZTAILPACKING

is better?

Quoted 91 lines
include/erofs/internal.h +0 -0
 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
 
 #define EROFS_I_EA_INITED	(1 << 0)
include/erofs_fs.h +8 -2
index 66a68e3..0ebcd5b 100644
@@ -22,11 +22,13 @@
 #define EROFS_FEATURE_INCOMPAT_COMPR_CFGS	0x00000002
 #define EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER	0x00000002
 #define EROFS_FEATURE_INCOMPAT_CHUNKED_FILE	0x00000004
+#define EROFS_FEATURE_INCOMPAT_TAIL_PACKING	0x00000010
 #define EROFS_ALL_FEATURE_INCOMPAT		\
 	(EROFS_FEATURE_INCOMPAT_LZ4_0PADDING | \
 	 EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \
 	 EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER | \
-	 EROFS_FEATURE_INCOMPAT_CHUNKED_FILE)
+	 EROFS_FEATURE_INCOMPAT_CHUNKED_FILE | \
+	 EROFS_FEATURE_INCOMPAT_TAIL_PACKING)
 
 #define EROFS_SB_EXTSLOT_SIZE	16
 
@@ -266,13 +268,17 @@ struct z_erofs_lz4_cfgs {
  *                                  (4B) + 2B + (4B) if compacted 2B is on.
  * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
  * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
+ * bit 3 : tail-packing inline (un)compressed data
  */
 #define Z_EROFS_ADVISE_COMPACTED_2B		0x0001
 #define Z_EROFS_ADVISE_BIG_PCLUSTER_1		0x0002
 #define Z_EROFS_ADVISE_BIG_PCLUSTER_2		0x0004
+#define Z_EROFS_ADVISE_INLINE_DATA		0x0008
 
 struct z_erofs_map_header {
-	__le32	h_reserved1;
+	__le16	h_reserved1;
+	/* record the (un)compressed size of tail-packing pcluster */
+	__le16	h_idata_size;
 	__le16	h_advise;
 	/*
 	 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
lib/compress.c +16 -6
index 6ca5bed..d7d60b9 100644
@@ -70,11 +70,10 @@ static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx,
 
 	di.di_clusterofs = cpu_to_le16(ctx->clusterofs);
 
-	/* whether the tail-end uncompressed block or not */
+	/* whether the tail-end (un)compressed block or not */
 	if (!d1) {
-		/* TODO: tail-packing inline compressed data */
-		DBG_BUGON(!raw);
-		type = Z_EROFS_VLE_CLUSTER_TYPE_PLAIN;
+		type = 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;
@@ -162,6 +161,17 @@ static unsigned int z_erofs_get_max_pclusterblks(struct erofs_inode *inode)
 	return cfg.c_pclusterblks_def;
 }
 
+static int z_erofs_fill_inline_data(struct erofs_inode *inode, char *data,
+				    unsigned int len)
+{
+	inode->idata_size = len;
+	inode->idata = malloc(inode->idata_size);
+	if (!inode->idata)
+		return -ENOMEM;
+	memcpy(inode->idata, data, inode->idata_size);
+	return 0;
+}
+
 static int vle_compress_one(struct erofs_inode *inode,
 			    struct z_erofs_vle_compress_ctx *ctx,
 			    bool final)
@@ -172,15 +182,20 @@ static int vle_compress_one(struct erofs_inode *inode,
 	int ret;
 	static char dstbuf[EROFS_CONFIG_COMPR_MAX_SZ + EROFS_BLKSIZ];
 	char *const dst = dstbuf + EROFS_BLKSIZ;
+	bool tail_pcluster = false;
 
 	while (len) {
-		const unsigned int pclustersize =
+		unsigned int pclustersize =
 			z_erofs_get_max_pclusterblks(inode) * EROFS_BLKSIZ;
 		bool raw;
 
-		if (len <= pclustersize) {

lib/compress.c static int vle_compress_one(struct erofs_inode *inode, · line 200

Quoted 10 lines
lib/compress.c +6 -1
+		if (!tail_pcluster && len <= pclustersize) {
 			if (final) {
-				if (len <= EROFS_BLKSIZ)
+				/* TODO: compress with 2 pclusters */
+				if (erofs_sb_has_tail_packing()) {
+					tail_pcluster = true;
+					pclustersize = EROFS_BLKSIZ;
+				} else if (len <= EROFS_BLKSIZ)
 					goto nocompression;
 			} else {

It seems somewhat messy...
Just a rough thought, how about the following code (not even tested...)

lib/compress.c +19 -13
@@ -182,21 +182,22 @@ static int vle_compress_one(struct erofs_inode *inode,
        int ret;
        static char dstbuf[EROFS_CONFIG_COMPR_MAX_SZ + EROFS_BLKSIZ];
        char *const dst = dstbuf + EROFS_BLKSIZ;
-       bool tail_pcluster = false;
+       bool trailing = false;
 
        while (len) {
                unsigned int pclustersize =
                        z_erofs_get_max_pclusterblks(inode) * EROFS_BLKSIZ;
                bool raw;
 
-               if (!tail_pcluster && len <= pclustersize) {
+               if (len <= pclustersize) {
                        if (final) {
                                /* TODO: compress with 2 pclusters */
                                if (erofs_sb_has_tail_packing()) {
-                                       tail_pcluster = true;
+                                       trailing = true;
                                        pclustersize = EROFS_BLKSIZ;
-                               } else if (len <= EROFS_BLKSIZ)
+                               } else if (len <= EROFS_BLKSIZ) {
                                        goto nocompression;
+                               }
                        } else {
                                break;
                        }
@@ -211,23 +212,29 @@ static int vle_compress_one(struct erofs_inode *inode,
                                          inode->i_srcpath,
                                          erofs_strerror(ret));
                        }
-                       if (tail_pcluster && len < EROFS_BLKSIZ) {
+                       if (trailing && len < EROFS_BLKSIZ) {
                                ret = z_erofs_fill_inline_data(inode,
                                        (char *)(ctx->queue + ctx->head), len);
                                if (ret)
                                        return ret;
                                count = len;
                                raw = true;
+                               ctx->compressedblks = 0;
+                       } else {
+nocompression:
+                               ret = write_uncompressed_extent(ctx, &len, dst);
+                               if (ret < 0)
+                                       return ret;
+                               count = ret;
                                ctx->compressedblks = 1;
-                               goto add_head;
+                               raw = true;
                        }
-nocompression:
-                       ret = write_uncompressed_extent(ctx, &len, dst);
-                       if (ret < 0)
+               } else if (trailing && ret < EROFS_BLKSIZ && len == count) {
+                       ret = z_erofs_fill_inline_data(inode, dst, ret);
+                       if (ret)
                                return ret;
-                       count = ret;
-                       ctx->compressedblks = 1;
-                       raw = true;
+                       raw = false;
+                       ctx->compressedblks = 0;
                } else {
                        if (tail_pcluster && ret < EROFS_BLKSIZ &&
                            !(len - count)) {
@@ -262,7 +269,6 @@ nocompression:
                        raw = false;
                }
 
-add_head:
                ctx->head += count;
                /* write compression indexes for this pcluster */
                vle_write_indexes(ctx, count, raw);

Thanks,
Gao Xiang

#6 lore raw

Hi Xiang,

Quoted 43 lines · On Fri, 10 Dec 2021 11:52:08 +0800 Gao Xiang <[email protected]>…

Hi Yue,

Quoted 6 lines · Yue Hu

Currently, we only support tail-end inline data for uncompressed files, let's support it as well for compressed files.

The idea is from Xiang.

Sorry about delay. Finally I seeked some time to look into this...

Quoted 24 lines

---
v3:
- based on commit 9fe440d0ac03 ("erofs-utils: mkfs: introduce --quiet option").
- move h_idata_size ahead of h_advise for compatibility.
- rename feature/update messages which i think they are more exact.

v2:
- add 2 bytes to record compressed size of tail-pcluster suggested from Xiang and update related code.

 include/erofs/internal.h |  1 +
 include/erofs_fs.h       | 10 ++++-
 lib/compress.c           | 83 ++++++++++++++++++++++++++++++----------
 lib/compressor.c         |  9 +++--
 lib/decompress.c         |  4 ++
 lib/inode.c              | 42 ++++++++++----------
 mkfs/main.c              |  7 ++++
 7 files changed, 108 insertions(+), 48 deletions(-)
include/erofs/internal.h +0 -0
index 8b154ed..54e5939 100644

include/erofs/internal.h EROFS_FEATURE_FUNCS(lz4_0padding, incompat, INCOMPAT_LZ4_0PADDING) · line 113

Quoted 4 lines
include/erofs/internal.h +1 -0
@@ -110,6 +110,7 @@ EROFS_FEATURE_FUNCS(lz4_0padding, incompat, INCOMPAT_LZ4_0PADDING)
 EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
 EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
 EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
+EROFS_FEATURE_FUNCS(tail_packing, incompat, INCOMPAT_TAIL_PACKING)  

How about moving fuse support as [PATCH 1/2] and introducing these?

Quoted 4 lines

Also the on-disk feature name is somewhat confusing, maybe INCOMPAT_ZTAILPACKING

is better?

ok, i will do that.

Quoted 182 lines
Quoted 92 lines
include/erofs/internal.h +0 -0
 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
 
 #define EROFS_I_EA_INITED	(1 << 0)
include/erofs_fs.h +8 -2
index 66a68e3..0ebcd5b 100644
@@ -22,11 +22,13 @@
 #define EROFS_FEATURE_INCOMPAT_COMPR_CFGS	0x00000002
 #define EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER	0x00000002
 #define EROFS_FEATURE_INCOMPAT_CHUNKED_FILE	0x00000004
+#define EROFS_FEATURE_INCOMPAT_TAIL_PACKING	0x00000010
 #define EROFS_ALL_FEATURE_INCOMPAT		\
 	(EROFS_FEATURE_INCOMPAT_LZ4_0PADDING | \
 	 EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \
 	 EROFS_FEATURE_INCOMPAT_BIG_PCLUSTER | \
-	 EROFS_FEATURE_INCOMPAT_CHUNKED_FILE)
+	 EROFS_FEATURE_INCOMPAT_CHUNKED_FILE | \
+	 EROFS_FEATURE_INCOMPAT_TAIL_PACKING)
 
 #define EROFS_SB_EXTSLOT_SIZE	16
 
@@ -266,13 +268,17 @@ struct z_erofs_lz4_cfgs {
  *                                  (4B) + 2B + (4B) if compacted 2B is on.
  * bit 1 : HEAD1 big pcluster (0 - off; 1 - on)
  * bit 2 : HEAD2 big pcluster (0 - off; 1 - on)
+ * bit 3 : tail-packing inline (un)compressed data
  */
 #define Z_EROFS_ADVISE_COMPACTED_2B		0x0001
 #define Z_EROFS_ADVISE_BIG_PCLUSTER_1		0x0002
 #define Z_EROFS_ADVISE_BIG_PCLUSTER_2		0x0004
+#define Z_EROFS_ADVISE_INLINE_DATA		0x0008
 
 struct z_erofs_map_header {
-	__le32	h_reserved1;
+	__le16	h_reserved1;
+	/* record the (un)compressed size of tail-packing pcluster */
+	__le16	h_idata_size;
 	__le16	h_advise;
 	/*
 	 * bit 0-3 : algorithm type of head 1 (logical cluster type 01);
lib/compress.c +16 -6
index 6ca5bed..d7d60b9 100644
@@ -70,11 +70,10 @@ static void vle_write_indexes(struct z_erofs_vle_compress_ctx *ctx,
 
 	di.di_clusterofs = cpu_to_le16(ctx->clusterofs);
 
-	/* whether the tail-end uncompressed block or not */
+	/* whether the tail-end (un)compressed block or not */
 	if (!d1) {
-		/* TODO: tail-packing inline compressed data */
-		DBG_BUGON(!raw);
-		type = Z_EROFS_VLE_CLUSTER_TYPE_PLAIN;
+		type = 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;
@@ -162,6 +161,17 @@ static unsigned int z_erofs_get_max_pclusterblks(struct erofs_inode *inode)
 	return cfg.c_pclusterblks_def;
 }
 
+static int z_erofs_fill_inline_data(struct erofs_inode *inode, char *data,
+				    unsigned int len)
+{
+	inode->idata_size = len;
+	inode->idata = malloc(inode->idata_size);
+	if (!inode->idata)
+		return -ENOMEM;
+	memcpy(inode->idata, data, inode->idata_size);
+	return 0;
+}
+
 static int vle_compress_one(struct erofs_inode *inode,
 			    struct z_erofs_vle_compress_ctx *ctx,
 			    bool final)
@@ -172,15 +182,20 @@ static int vle_compress_one(struct erofs_inode *inode,
 	int ret;
 	static char dstbuf[EROFS_CONFIG_COMPR_MAX_SZ + EROFS_BLKSIZ];
 	char *const dst = dstbuf + EROFS_BLKSIZ;
+	bool tail_pcluster = false;
 
 	while (len) {
-		const unsigned int pclustersize =
+		unsigned int pclustersize =
 			z_erofs_get_max_pclusterblks(inode) * EROFS_BLKSIZ;
 		bool raw;
 
-		if (len <= pclustersize) {

lib/compress.c static int vle_compress_one(struct erofs_inode *inode, · line 200

Quoted 10 lines
lib/compress.c +6 -1
+		if (!tail_pcluster && len <= pclustersize) {
 			if (final) {
-				if (len <= EROFS_BLKSIZ)
+				/* TODO: compress with 2 pclusters */
+				if (erofs_sb_has_tail_packing()) {
+					tail_pcluster = true;
+					pclustersize = EROFS_BLKSIZ;
+				} else if (len <= EROFS_BLKSIZ)
 					goto nocompression;
 			} else {  

It seems somewhat messy...
Just a rough thought, how about the following code (not even tested...)

lib/compress.c +19 -13
@@ -182,21 +182,22 @@ static int vle_compress_one(struct erofs_inode *inode,
        int ret;
        static char dstbuf[EROFS_CONFIG_COMPR_MAX_SZ + EROFS_BLKSIZ];
        char *const dst = dstbuf + EROFS_BLKSIZ;
-       bool tail_pcluster = false;
+       bool trailing = false;
 
        while (len) {
                unsigned int pclustersize =
                        z_erofs_get_max_pclusterblks(inode) * EROFS_BLKSIZ;
                bool raw;
 
-               if (!tail_pcluster && len <= pclustersize) {
+               if (len <= pclustersize) {
                        if (final) {
                                /* TODO: compress with 2 pclusters */
                                if (erofs_sb_has_tail_packing()) {
-                                       tail_pcluster = true;
+                                       trailing = true;
                                        pclustersize = EROFS_BLKSIZ;
-                               } else if (len <= EROFS_BLKSIZ)
+                               } else if (len <= EROFS_BLKSIZ) {
                                        goto nocompression;
+                               }
                        } else {
                                break;
                        }
@@ -211,23 +212,29 @@ static int vle_compress_one(struct erofs_inode *inode,
                                          inode->i_srcpath,
                                          erofs_strerror(ret));
                        }
-                       if (tail_pcluster && len < EROFS_BLKSIZ) {
+                       if (trailing && len < EROFS_BLKSIZ) {
                                ret = z_erofs_fill_inline_data(inode,
                                        (char *)(ctx->queue + ctx->head), len);
                                if (ret)
                                        return ret;
                                count = len;
                                raw = true;
+                               ctx->compressedblks = 0;
+                       } else {
+nocompression:
+                               ret = write_uncompressed_extent(ctx, &len, dst);
+                               if (ret < 0)
+                                       return ret;
+                               count = ret;
                                ctx->compressedblks = 1;
-                               goto add_head;
+                               raw = true;
                        }
-nocompression:
-                       ret = write_uncompressed_extent(ctx, &len, dst);
-                       if (ret < 0)
+               } else if (trailing && ret < EROFS_BLKSIZ && len == count) {
+                       ret = z_erofs_fill_inline_data(inode, dst, ret);
+                       if (ret)
                                return ret;
-                       count = ret;
-                       ctx->compressedblks = 1;
-                       raw = true;
+                       raw = false;
+                       ctx->compressedblks = 0;
                } else {
                        if (tail_pcluster && ret < EROFS_BLKSIZ &&
                            !(len - count)) {
@@ -262,7 +269,6 @@ nocompression:
                        raw = false;
                }
 
-add_head:
                ctx->head += count;
                /* write compression indexes for this pcluster */
                vle_write_indexes(ctx, count, raw);
Quoted 2 lines

Thanks,
Gao Xiang

Let me think above.

Thanks.