Skip to contentSkip to conversation

erofs-utils: 48-bit layout support

PATCH New Gao Xiang 1 participant 10 messages Jul 4, 2025

Subsystems (2)
lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

v3 cover erofs-utils: 48-bit layout support

I think erofs-utils 1.8.x development should have been restrained.

I rebased this patchset for the formal erofs-utils 1.9 cycle and will be merged into -dev branch.

Changes since v2:
- Mainly random rebasing due to 1.8.x changes.

Gao Xiang (9):
erofs-utils: lib: simplify tail inline pcluster handling
erofs-utils: lib: clean up header parsing for ztailpacking and
fragments
erofs-utils: get rid of NULL_ADDR{,_UL}
erofs-utils: lib: sync up with the 48-bit kernel erofs_fs.h
erofs-utils: implement 48-bit block addressing for unencoded inodes
erofs-utils: mkfs: support 48-bit block addressing for unencoded
inodes
erofs-utils: support dot-omitted directories
erofs-utils: lib: implement encoded extent metadata
erofs-utils: support encoded extents

 dump/main.c                 |   3 +-
 fsck/main.c                 |   2 +-
 include/erofs/cache.h       |   4 +-
 include/erofs/config.h      |   1 +
 include/erofs/internal.h    |  36 +++---
 include/erofs_fs.h          | 194 ++++++++++++++---------------
 lib/blobchunk.c             |  33 +++--
 lib/block_list.c            |   8 +-
 lib/cache.c                 |  24 ++--
 lib/compress.c              | 236 ++++++++++++++++++++++++++++--------
 lib/compressor.c            |  11 ++
 lib/compressor.h            |   6 +
 lib/compressor_libdeflate.c |  18 +++
 lib/compressor_libzstd.c    |  18 +++
 lib/data.c                  | 147 +++++++++-------------
 lib/dir.c                   |   2 +
 lib/inode.c                 | 146 +++++++++++++++++-----
 lib/namei.c                 |  38 ++++--
 lib/rebuild.c               |   2 +-
 lib/super.c                 |  37 ++++--
 lib/zmap.c                  | 216 ++++++++++++++++++++++++++-------
 mkfs/main.c                 |  45 ++++++-
 22 files changed, 827 insertions(+), 400 deletions(-)
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH v3 1/9 erofs-utils: lib: simplify tail inline pcluster handling

Source kernel commit: b7710262d743aca112877d12abed61ce8a5d0d98

---
 include/erofs/internal.h |  4 ++--
 lib/zmap.c               | 20 ++++++++++----------
 2 files changed, 12 insertions(+), 12 deletions(-)
include/erofs/internal.h +2 -2
index 2a0e85e0..bc4ed0a9 100644
@@ -274,8 +274,8 @@ struct erofs_inode {
 				erofs_off_t fragment_size;
 			};
 			union {
-				unsigned int z_idataoff;
-				erofs_off_t fragmentoff;
+				erofs_off_t	fragmentoff;
+				erofs_off_t	z_fragmentoff;
 				void *fragment;
 			};
 #define z_idata_size	idata_size
lib/zmap.c +10 -10
index 30bb7e38..d47ed6b8 100644
@@ -414,8 +414,8 @@ static int z_erofs_do_map_blocks(struct erofs_inode *vi,
 				 int flags)
 {
 	struct erofs_sb_info *sbi = vi->sbi;
-	bool ztailpacking = vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER;
 	bool fragment = vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER;
+	bool ztailpacking = vi->z_idata_size;
 	struct z_erofs_maprecorder m = {
 		.inode = vi,
 		.map = map,
@@ -435,9 +435,8 @@ static int z_erofs_do_map_blocks(struct erofs_inode *vi,
 	if (err)
 		goto out;
 
-	if (ztailpacking && (flags & EROFS_GET_BLOCKS_FINDTAIL))
-		vi->z_idataoff = m.nextpackoff;
-
+	if ((flags & EROFS_GET_BLOCKS_FINDTAIL) && ztailpacking)
+		vi->z_fragmentoff = m.nextpackoff;
 	map->m_flags = EROFS_MAP_MAPPED | EROFS_MAP_ENCODED;
 	end = (m.lcn + 1ULL) << lclusterbits;
 
@@ -492,7 +491,7 @@ static int z_erofs_do_map_blocks(struct erofs_inode *vi,
 	}
 	if (ztailpacking && m.lcn == vi->z_tailextent_headlcn) {
 		map->m_flags |= EROFS_MAP_META;
-		map->m_pa = vi->z_idataoff;
+		map->m_pa = vi->z_fragmentoff;
 		map->m_plen = vi->z_idata_size;
 	} else if (fragment && m.lcn == vi->z_tailextent_headlcn) {
 		map->m_flags |= EROFS_MAP_FRAGMENT;
@@ -568,6 +567,10 @@ static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 	vi->z_advise = le16_to_cpu(h->h_advise);
 	vi->z_algorithmtype[0] = h->h_algorithmtype & 15;
 	vi->z_algorithmtype[1] = h->h_algorithmtype >> 4;
+	if (vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER)
+		vi->z_fragmentoff = le32_to_cpu(h->h_fragmentoff);
+	else if (vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER)
+		vi->z_idata_size = le16_to_cpu(h->h_idata_size);
 
 	headnr = 0;
 	if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX ||
@@ -586,14 +589,12 @@ static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 		return -EFSCORRUPTED;
 	}
 
-	if (vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER) {
+	if (vi->z_idata_size) {
 		struct erofs_map_blocks map = { .index = UINT_MAX };
 
-		vi->idata_size = le16_to_cpu(h->h_idata_size);
 		err = z_erofs_do_map_blocks(vi, &map,
 					    EROFS_GET_BLOCKS_FINDTAIL);
-		if (!map.m_plen ||
-		    erofs_blkoff(sbi, map.m_pa) + map.m_plen > erofs_blksiz(sbi)) {
+		if (erofs_blkoff(sbi, map.m_pa) + map.m_plen > erofs_blksiz(sbi)) {
 			erofs_err("invalid tail-packing pclustersize %llu",
 				  map.m_plen | 0ULL);
 			return -EFSCORRUPTED;
@@ -605,7 +606,6 @@ static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 	    !(h->h_clusterbits >> Z_EROFS_FRAGMENT_INODE_BIT)) {
 		struct erofs_map_blocks map = { .index = UINT_MAX };
 
-		vi->fragmentoff = le32_to_cpu(h->h_fragmentoff);
 		err = z_erofs_do_map_blocks(vi, &map,
 					    EROFS_GET_BLOCKS_FINDTAIL);
 		if (err < 0)
Signature · 1 line
2.43.5
author
#2 lore raw

PATCH v3 2/9 erofs-utils: lib: clean up header parsing for ztailpacking and fragments

In reply to Gao Xiang

Source kernel commit: 540787d38b10dbc16a7d2bc2845752ab1605403a

---
 lib/zmap.c | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)
lib/zmap.c +7 -15
index d47ed6b8..07c6a83c 100644
@@ -493,6 +493,11 @@ static int z_erofs_do_map_blocks(struct erofs_inode *vi,
 		map->m_flags |= EROFS_MAP_META;
 		map->m_pa = vi->z_fragmentoff;
 		map->m_plen = vi->z_idata_size;
+		if (erofs_blkoff(sbi, map->m_pa) + map->m_plen > erofs_blksiz(sbi)) {
+			erofs_err("invalid tail-packing pclustersize %llu",
+				  map->m_plen | 0ULL);
+			goto out;
+		}
 	} else if (fragment && m.lcn == vi->z_tailextent_headlcn) {
 		map->m_flags |= EROFS_MAP_FRAGMENT;
 	} else {
@@ -589,21 +594,8 @@ static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 		return -EFSCORRUPTED;
 	}
 
-	if (vi->z_idata_size) {
-		struct erofs_map_blocks map = { .index = UINT_MAX };
-
-		err = z_erofs_do_map_blocks(vi, &map,
-					    EROFS_GET_BLOCKS_FINDTAIL);
-		if (erofs_blkoff(sbi, map.m_pa) + map.m_plen > erofs_blksiz(sbi)) {
-			erofs_err("invalid tail-packing pclustersize %llu",
-				  map.m_plen | 0ULL);
-			return -EFSCORRUPTED;
-		}
-		if (err < 0)
-			return err;
-	}
-	if (vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER &&
-	    !(h->h_clusterbits >> Z_EROFS_FRAGMENT_INODE_BIT)) {
+	if (vi->z_idata_size ||
+	    (vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER)) {
 		struct erofs_map_blocks map = { .index = UINT_MAX };
 
 		err = z_erofs_do_map_blocks(vi, &map,
Signature · 1 line
2.43.5
author
#3 lore raw

PATCH v3 3/9 erofs-utils: get rid of NULL_ADDR{,_UL}

In reply to Gao Xiang

.. And replace them with EROFS_NULL_ADDR.

---
 include/erofs/cache.h    |  4 ++--
 include/erofs/internal.h |  3 ---
 lib/cache.c              | 24 ++++++++++++------------
 lib/inode.c              |  2 +-
 lib/rebuild.c            |  2 +-
 lib/super.c              |  2 +-
 6 files changed, 17 insertions(+), 20 deletions(-)
include/erofs/cache.h +2 -2
index 87d743a3..0c665e8f 100644
@@ -107,8 +107,8 @@ static inline erofs_off_t erofs_btell(struct erofs_buffer_head *bh, bool end)
 	struct erofs_bufmgr *bmgr =
 			(struct erofs_bufmgr *)bb->buffers.fsprivate;
 
-	if (bb->blkaddr == NULL_ADDR)
-		return NULL_ADDR_UL;
+	if (bb->blkaddr == EROFS_NULL_ADDR)
+		return EROFS_NULL_ADDR;
 
 	return erofs_pos(bmgr->sbi, bb->blkaddr) +
 		(end ? list_next_entry(bh, list)->off : bh->off);
include/erofs/internal.h +0 -3
index bc4ed0a9..200146bd 100644
@@ -44,9 +44,6 @@ typedef u64 erofs_nid_t;
 /* data type for filesystem-wide blocks number */
 typedef u32 erofs_blk_t;
 
-#define NULL_ADDR	((unsigned int)-1)
-#define NULL_ADDR_UL	((unsigned long)-1)
-
 /* global sbi */
 extern struct erofs_sb_info g_sbi;
 
lib/cache.c +12 -12
index eddf21f4..b91a2887 100644
@@ -49,7 +49,7 @@ struct erofs_bufmgr *erofs_buffer_init(struct erofs_sb_info *sbi,
 		}
 	}
 	init_list_head(&bmgr->blkh.list);
-	bmgr->blkh.blkaddr = NULL_ADDR;
+	bmgr->blkh.blkaddr = EROFS_NULL_ADDR;
 	bmgr->tail_blkaddr = startblk;
 	bmgr->last_mapped_block = &bmgr->blkh;
 	bmgr->dsunit = 0;
@@ -79,7 +79,7 @@ static void erofs_update_bwatermeter(struct erofs_buffer_block *bb, bool free)
 	struct erofs_bufmgr *bmgr = bb->buffers.fsprivate;
 	struct erofs_sb_info *sbi = bmgr->sbi;
 	unsigned int blksiz = erofs_blksiz(sbi);
-	bool mapped = bb->blkaddr != NULL_ADDR;
+	bool mapped = bb->blkaddr != EROFS_NULL_ADDR;
 	struct list_head *h = bmgr->watermeter[bb->type][mapped];
 	unsigned int nr;
 
@@ -127,12 +127,12 @@ static int __erofs_battach(struct erofs_buffer_block *bb,
 	oob = cmpsgn(alignedoffset + incr + inline_ext,
 		     bb->buffers.nblocks << sbi->blkszbits);
 	if (oob >= 0) {
-		/* the next buffer block should be NULL_ADDR all the time */
-		if (oob && list_next_entry(bb, list)->blkaddr != NULL_ADDR)
+		/* the next buffer block should be EROFS_NULL_ADDR all the time */
+		if (oob && list_next_entry(bb, list)->blkaddr != EROFS_NULL_ADDR)
 			return -EINVAL;
 
 		blkaddr = bb->blkaddr;
-		if (blkaddr != NULL_ADDR) {
+		if (blkaddr != EROFS_NULL_ADDR) {
 			tailupdate = (bmgr->tail_blkaddr == blkaddr +
 				      bb->buffers.nblocks);
 			if (oob && !tailupdate)
@@ -244,7 +244,7 @@ static int erofs_bfind_for_attach(struct erofs_bufmgr *bmgr,
 
 			/* skip the last mapped block */
 			if (mapped &&
-			    list_next_entry(cur, list)->blkaddr == NULL_ADDR) {
+			    list_next_entry(cur, list)->blkaddr == EROFS_NULL_ADDR) {
 				DBG_BUGON(cur != bmgr->last_mapped_block);
 				cur = list_next_entry(cur, sibling);
 				if (&cur->sibling == bt)
@@ -252,7 +252,7 @@ static int erofs_bfind_for_attach(struct erofs_bufmgr *bmgr,
 			}
 
 			DBG_BUGON(cur->type != type);
-			DBG_BUGON((cur->blkaddr != NULL_ADDR) ^ mapped);
+			DBG_BUGON((cur->blkaddr != EROFS_NULL_ADDR) ^ mapped);
 			DBG_BUGON(used != (cur->buffers.off & blkmask));
 
 			ret = __erofs_battach(cur, NULL, size, alignsize,
@@ -327,7 +327,7 @@ struct erofs_buffer_head *erofs_balloc(struct erofs_bufmgr *bmgr,
 			return ERR_PTR(-ENOMEM);
 
 		bb->type = type;
-		bb->blkaddr = NULL_ADDR;
+		bb->blkaddr = EROFS_NULL_ADDR;
 		bb->buffers.off = 0;
 		bb->buffers.nblocks = 0;
 		bb->buffers.fsprivate = bmgr;
@@ -388,7 +388,7 @@ static void __erofs_mapbh(struct erofs_buffer_block *bb)
 	struct erofs_bufmgr *bmgr = bb->buffers.fsprivate;
 	erofs_blk_t blkaddr = bmgr->tail_blkaddr;
 
-	if (bb->blkaddr == NULL_ADDR) {
+	if (bb->blkaddr == EROFS_NULL_ADDR) {
 		bb->blkaddr = blkaddr;
 		if (__erofs_unlikely(bmgr->dsunit > 1) && bb->type == DATA) {
 			struct erofs_buffer_block *pb = list_prev_entry(bb, list);
@@ -418,14 +418,14 @@ erofs_blk_t erofs_mapbh(struct erofs_bufmgr *bmgr,
 		bmgr = bb->buffers.fsprivate;
 	t = bmgr->last_mapped_block;
 
-	if (bb && bb->blkaddr != NULL_ADDR)
+	if (bb && bb->blkaddr != EROFS_NULL_ADDR)
 		return bb->blkaddr;
 	do {
 		t = list_next_entry(t, list);
 		if (t == &bmgr->blkh)
 			break;
 
-		DBG_BUGON(t->blkaddr != NULL_ADDR);
+		DBG_BUGON(t->blkaddr != EROFS_NULL_ADDR);
 		__erofs_mapbh(t);
 	} while (t != bb);
 	return bmgr->tail_blkaddr;
@@ -501,7 +501,7 @@ void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke)
 	bool rollback = false;
 
 	/* tail_blkaddr could be rolled back after revoking all bhs */
-	if (tryrevoke && blkaddr != NULL_ADDR &&
+	if (tryrevoke && blkaddr != EROFS_NULL_ADDR &&
 	    bmgr->tail_blkaddr == blkaddr + bb->buffers.nblocks)
 		rollback = true;
 
lib/inode.c +1 -1
index a2ee5228..a1fe4ae9 100644
@@ -189,7 +189,7 @@ int erofs_allocate_inode_bh_data(struct erofs_inode *inode, erofs_blk_t nblocks)
 
 	if (!nblocks) {
 		/* it has only tail-end data */
-		inode->u.i_blkaddr = NULL_ADDR;
+		inode->u.i_blkaddr = EROFS_NULL_ADDR;
 		return 0;
 	}
 
lib/rebuild.c +1 -1
index 3c9f7446..576d9d05 100644
@@ -252,7 +252,7 @@ static int erofs_rebuild_update_inode(struct erofs_sb_info *dst_sb,
 		break;
 	case S_IFREG:
 		if (!inode->i_size) {
-			inode->u.i_blkaddr = NULL_ADDR;
+			inode->u.i_blkaddr = EROFS_NULL_ADDR;
 			break;
 		}
 		if (datamode == EROFS_REBUILD_DATA_BLOB_INDEX)
lib/super.c +1 -1
index 69746671..cc0ccb46 100644
@@ -351,7 +351,7 @@ int erofs_write_device_table(struct erofs_sb_info *sbi)
 		return -EINVAL;
 
 	pos = erofs_btell(bh, false);
-	if (pos == NULL_ADDR_UL) {
+	if (pos == EROFS_NULL_ADDR) {
 		DBG_BUGON(1);
 		return -EINVAL;
 	}
Signature · 1 line
2.43.5
author
#4 lore raw

PATCH v3 4/9 erofs-utils: lib: sync up with the 48-bit kernel erofs_fs.h

In reply to Gao Xiang

Keep in sync with the related kernel commits:
commit 61ba89b57905 ("erofs: add 48-bit block addressing on-disk support") commit efb2aef569b3 ("erofs: add encoded extent on-disk definition")

---
 include/erofs/internal.h |   4 +-
 include/erofs_fs.h       | 191 ++++++++++++++++++---------------------
 lib/blobchunk.c          |  16 ++--
 lib/compress.c           |   2 +-
 lib/data.c               |   8 +-
 lib/inode.c              |   6 +-
 lib/namei.c              |   4 +-
 lib/super.c              |  24 ++---
 lib/zmap.c               |   2 +-
 9 files changed, 121 insertions(+), 136 deletions(-)
include/erofs/internal.h +2 -2
index 200146bd..a294561f 100644
@@ -60,8 +60,8 @@ struct erofs_bufmgr;
 struct erofs_device_info {
 	char *src_path;
 	u8 tag[64];
-	u32 blocks;
-	u32 mapped_blkaddr;
+	erofs_blk_t blocks;
+	erofs_blk_t uniaddr;
 };
 
 /* all filesystem-wide lz4 configurations */
include/erofs_fs.h +88 -103

310 lines not on this page · raw message

lib/blobchunk.c +8 -8
index 8c55277f..18eafdce 100644
@@ -157,32 +157,32 @@ int erofs_blob_write_chunk_indexes(struct erofs_inode *inode,
 		chunk = *(void **)(inode->chunkindexes + src);
 
 		if (chunk->blkaddr == EROFS_NULL_ADDR) {
-			idx.blkaddr = EROFS_NULL_ADDR;
+			idx.startblk_lo = EROFS_NULL_ADDR;
 		} else if (chunk->device_id) {
 			DBG_BUGON(!(inode->u.chunkformat & EROFS_CHUNK_FORMAT_INDEXES));
-			idx.blkaddr = chunk->blkaddr;
+			idx.startblk_lo = chunk->blkaddr;
 			extent_start = EROFS_NULL_ADDR;
 		} else {
-			idx.blkaddr = remapped_base + chunk->blkaddr;
+			idx.startblk_lo = remapped_base + chunk->blkaddr;
 		}
 
 		if (extent_start == EROFS_NULL_ADDR ||
-		    idx.blkaddr != extent_end) {
+		    idx.startblk_lo != extent_end) {
 			if (extent_start != EROFS_NULL_ADDR) {
 				remaining_blks -= extent_end - extent_start;
 				tarerofs_blocklist_write(extent_start,
 						extent_end - extent_start,
 						source_offset, 0);
 			}
-			extent_start = idx.blkaddr;
+			extent_start = idx.startblk_lo;
 			source_offset = chunk->sourceoffset;
 		}
-		extent_end = idx.blkaddr + chunkblks;
+		extent_end = idx.startblk_lo + chunkblks;
 		idx.device_id = cpu_to_le16(chunk->device_id);
-		idx.blkaddr = cpu_to_le32(idx.blkaddr);
+		idx.startblk_lo = cpu_to_le32(idx.startblk_lo);
 
 		if (unit == EROFS_BLOCK_MAP_ENTRY_SIZE)
-			memcpy(inode->chunkindexes + dst, &idx.blkaddr, unit);
+			memcpy(inode->chunkindexes + dst, &idx.startblk_lo, unit);
 		else
 			memcpy(inode->chunkindexes + dst, &idx, sizeof(idx));
 	}
lib/compress.c +1 -1
index 6f65993c..6e7b5241 100644
@@ -137,7 +137,7 @@ struct z_erofs_mgr {
 
 static bool z_erofs_mt_enabled;
 
-#define Z_EROFS_LEGACY_MAP_HEADER_SIZE	Z_EROFS_FULL_INDEX_ALIGN(0)
+#define Z_EROFS_LEGACY_MAP_HEADER_SIZE	Z_EROFS_FULL_INDEX_START(0)
 
 static void z_erofs_fini_full_indexes(struct z_erofs_compress_ictx *ctx)
 {
lib/data.c +4 -4
index 0f79f789..2547615f 100644
@@ -117,14 +117,14 @@ int __erofs_map_blocks(struct erofs_inode *inode,
 	}
 	/* parse chunk indexes */
 	idx = (void *)buf + erofs_blkoff(sbi, pos);
-	switch (le32_to_cpu(idx->blkaddr)) {
+	switch (le32_to_cpu(idx->startblk_lo)) {
 	case EROFS_NULL_ADDR:
 		map->m_flags = 0;
 		break;
 	default:
 		map->m_deviceid = le16_to_cpu(idx->device_id) &
 			sbi->device_id_mask;
-		map->m_pa = erofs_pos(sbi, le32_to_cpu(idx->blkaddr));
+		map->m_pa = erofs_pos(sbi, le32_to_cpu(idx->startblk_lo));
 		map->m_flags = EROFS_MAP_MAPPED;
 		break;
 	}
@@ -154,9 +154,9 @@ int erofs_map_dev(struct erofs_sb_info *sbi, struct erofs_map_dev *map)
 			erofs_off_t startoff, length;
 
 			dif = sbi->devs + id;
-			if (!dif->mapped_blkaddr)
+			if (!dif->uniaddr)
 				continue;
-			startoff = erofs_pos(sbi, dif->mapped_blkaddr);
+			startoff = erofs_pos(sbi, dif->uniaddr);
 			length = erofs_pos(sbi, dif->blocks);
 
 			if (map->m_pa >= startoff &&
lib/inode.c +3 -3
index a1fe4ae9..5ccffc09 100644
@@ -603,18 +603,18 @@ int erofs_iflush(struct erofs_inode *inode)
 	    S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode))
 		u1.rdev = cpu_to_le32(inode->u.i_rdev);
 	else if (is_inode_layout_compression(inode))
-		u1.compressed_blocks = cpu_to_le32(inode->u.i_blocks);
+		u1.blocks_lo = cpu_to_le32(inode->u.i_blocks);
 	else if (inode->datalayout == EROFS_INODE_CHUNK_BASED)
 		u1.c.format = cpu_to_le16(inode->u.chunkformat);
 	else
-		u1.raw_blkaddr = cpu_to_le32(inode->u.i_blkaddr);
+		u1.startblk_lo = cpu_to_le32(inode->u.i_blkaddr);
 
 	switch (inode->inode_isize) {
 	case sizeof(struct erofs_inode_compact):
 		u.dic.i_format = cpu_to_le16(0 | (inode->datalayout << 1));
 		u.dic.i_xattr_icount = cpu_to_le16(icount);
 		u.dic.i_mode = cpu_to_le16(inode->i_mode);
-		u.dic.i_nlink = cpu_to_le16(inode->i_nlink);
+		u.dic.i_nb.nlink = cpu_to_le16(inode->i_nlink);
 		u.dic.i_size = cpu_to_le32((u32)inode->i_size);
 
 		u.dic.i_ino = cpu_to_le32(inode->i_ino[0]);
lib/namei.c +2 -2
index b40f092c..66b8eef4 100644
@@ -77,7 +77,7 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 		iu = dic->i_u;
 		vi->i_uid = le16_to_cpu(dic->i_uid);
 		vi->i_gid = le16_to_cpu(dic->i_gid);
-		vi->i_nlink = le16_to_cpu(dic->i_nlink);
+		vi->i_nlink = le16_to_cpu(dic->i_nb.nlink);
 
 		vi->i_mtime = sbi->build_time;
 		vi->i_mtime_nsec = sbi->build_time_nsec;
@@ -94,7 +94,7 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 	case S_IFREG:
 	case S_IFDIR:
 	case S_IFLNK:
-		vi->u.i_blkaddr = le32_to_cpu(iu.raw_blkaddr);
+		vi->u.i_blkaddr = le32_to_cpu(iu.startblk_lo);
 		break;
 	case S_IFCHR:
 	case S_IFBLK:
lib/super.c +12 -12
index cc0ccb46..6a59a236 100644
@@ -63,8 +63,8 @@ static int erofs_init_devices(struct erofs_sb_info *sbi,
 			return ret;
 		}
 
-		sbi->devs[i].mapped_blkaddr = le32_to_cpu(dis.mapped_blkaddr);
-		sbi->devs[i].blocks = le32_to_cpu(dis.blocks);
+		sbi->devs[i].uniaddr = le32_to_cpu(dis.uniaddr_lo);
+		sbi->devs[i].blocks = le32_to_cpu(dis.blocks_lo);
 		memcpy(sbi->devs[i].tag, dis.tag, sizeof(dis.tag));
 		sbi->total_blocks += sbi->devs[i].blocks;
 		pos += EROFS_DEVT_SLOT_SIZE;
@@ -109,19 +109,19 @@ int erofs_read_superblock(struct erofs_sb_info *sbi)
 		erofs_err("invalid sb_extslots %u", dsb->sb_extslots);
 		return -EINVAL;
 	}
-	sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks);
+	sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks_lo);
 	sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
 	sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
 	sbi->xattr_prefix_start = le32_to_cpu(dsb->xattr_prefix_start);
 	sbi->xattr_prefix_count = dsb->xattr_prefix_count;
 	sbi->islotbits = EROFS_ISLOTBITS;
-	sbi->root_nid = le16_to_cpu(dsb->root_nid);
+	sbi->root_nid = le16_to_cpu(dsb->rb.rootnid_2b);
 	sbi->packed_nid = le64_to_cpu(dsb->packed_nid);
 	sbi->inos = le64_to_cpu(dsb->inos);
 	sbi->checksum = le32_to_cpu(dsb->checksum);
 
-	sbi->build_time = le64_to_cpu(dsb->build_time);
-	sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
+	sbi->build_time = le64_to_cpu(dsb->epoch);
+	sbi->build_time_nsec = le32_to_cpu(dsb->fixed_nsec);
 
 	memcpy(&sbi->uuid, dsb->uuid, sizeof(dsb->uuid));
 
@@ -164,10 +164,10 @@ int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh)
 	struct erofs_super_block sb = {
 		.magic     = cpu_to_le32(EROFS_SUPER_MAGIC_V1),
 		.blkszbits = sbi->blkszbits,
-		.root_nid  = cpu_to_le16(sbi->root_nid),
+		.rb.rootnid_2b  = cpu_to_le16(sbi->root_nid),
 		.inos      = cpu_to_le64(sbi->inos),
-		.build_time = cpu_to_le64(sbi->build_time),
-		.build_time_nsec = cpu_to_le32(sbi->build_time_nsec),
+		.epoch     = cpu_to_le64(sbi->build_time),
+		.fixed_nsec = cpu_to_le32(sbi->build_time_nsec),
 		.meta_blkaddr  = cpu_to_le32(sbi->meta_blkaddr),
 		.xattr_blkaddr = cpu_to_le32(sbi->xattr_blkaddr),
 		.xattr_prefix_count = sbi->xattr_prefix_count,
@@ -183,7 +183,7 @@ int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh)
 	char *buf;
 	int ret;
 
-	sb.blocks       = cpu_to_le32(sbi->primarydevice_blocks);
+	sb.blocks_lo	= cpu_to_le32(sbi->primarydevice_blocks);
 	memcpy(sb.uuid, sbi->uuid, sizeof(sb.uuid));
 	memcpy(sb.volume_name, sbi->volume_name, sizeof(sb.volume_name));
 
@@ -359,8 +359,8 @@ int erofs_write_device_table(struct erofs_sb_info *sbi)
 	i = 0;
 	do {
 		struct erofs_deviceslot dis = {
-			.mapped_blkaddr = cpu_to_le32(nblocks),
-			.blocks = cpu_to_le32(sbi->devs[i].blocks),
+			.uniaddr_lo = cpu_to_le32(nblocks),
+			.blocks_lo = cpu_to_le32(sbi->devs[i].blocks),
 		};
 
 		memcpy(dis.tag, sbi->devs[i].tag, sizeof(dis.tag));
lib/zmap.c +1 -1
index 07c6a83c..83833859 100644
@@ -30,7 +30,7 @@ static int z_erofs_load_full_lcluster(struct z_erofs_maprecorder *m,
 {
 	struct erofs_inode *const vi = m->inode;
 	struct erofs_sb_info *sbi = vi->sbi;
-	const erofs_off_t pos = Z_EROFS_FULL_INDEX_ALIGN(erofs_iloc(vi) +
+	const erofs_off_t pos = Z_EROFS_FULL_INDEX_START(erofs_iloc(vi) +
 			vi->inode_isize + vi->xattr_isize) +
 			lcn * sizeof(struct z_erofs_lcluster_index);
 	erofs_blk_t eblk = erofs_blknr(sbi, pos);
Signature · 1 line
2.43.5
author
#5 lore raw

PATCH v3 5/9 erofs-utils: implement 48-bit block addressing for unencoded inodes

In reply to Gao Xiang

.. Also port the same kernel logic to erofsfuse and dump.erofs.

---
 dump/main.c              |   3 +-
 include/erofs/internal.h |  21 +++---
 include/erofs_fs.h       |   2 +-
 lib/blobchunk.c          |   2 +-
 lib/block_list.c         |   8 +--
 lib/data.c               | 143 +++++++++++++++------------------------
 lib/inode.c              |  18 ++---
 lib/namei.c              |  35 +++++++---
 lib/super.c              |  18 +++--
 mkfs/main.c              |   8 +--
 10 files changed, 124 insertions(+), 134 deletions(-)
dump/main.c +2 -1
index 44f65dab..993d9386 100644
@@ -104,6 +104,7 @@ static struct erofsdump_feature feature_lists[] = {
 	{ false, EROFS_FEATURE_INCOMPAT_FRAGMENTS, "fragments" },
 	{ false, EROFS_FEATURE_INCOMPAT_DEDUPE, "dedupe" },
 	{ false, EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES, "xattr_prefixes" },
+	{ false, EROFS_FEATURE_INCOMPAT_48BIT, "48bit" },
 };
 
 static int erofsdump_readdir(struct erofs_dir_context *ctx);
@@ -623,7 +624,7 @@ static void erofsdump_print_supported_compressors(FILE *f, unsigned int mask)
 
 static void erofsdump_show_superblock(void)
 {
-	time_t time = g_sbi.build_time;
+	time_t time = g_sbi.epoch + g_sbi.build_time;
 	char uuid_str[37];
 	int i = 0;
 
include/erofs/internal.h +11 -10
index a294561f..51def099 100644
@@ -41,15 +41,14 @@ typedef unsigned short umode_t;
 
 typedef u64 erofs_off_t;
 typedef u64 erofs_nid_t;
-/* data type for filesystem-wide blocks number */
-typedef u32 erofs_blk_t;
+typedef u64 erofs_blk_t;
 
 /* global sbi */
 extern struct erofs_sb_info g_sbi;
 
 #define erofs_blksiz(sbi)	(1u << (sbi)->blkszbits)
-#define erofs_blknr(sbi, addr)  ((addr) >> (sbi)->blkszbits)
-#define erofs_blkoff(sbi, addr) ((addr) & (erofs_blksiz(sbi) - 1))
+#define erofs_blknr(sbi, pos)	((pos) >> (sbi)->blkszbits)
+#define erofs_blkoff(sbi, pos)	((pos) & (erofs_blksiz(sbi) - 1))
 #define erofs_pos(sbi, nr)      ((erofs_off_t)(nr) << (sbi)->blkszbits)
 #define BLK_ROUND_UP(sbi, addr)	\
 	(roundup(addr, erofs_blksiz(sbi)) >> (sbi)->blkszbits)
@@ -90,8 +89,8 @@ struct erofs_sb_info {
 	u64 total_blocks;
 	u64 primarydevice_blocks;
 
-	erofs_blk_t meta_blkaddr;
-	erofs_blk_t xattr_blkaddr;
+	u32 meta_blkaddr;
+	u32 xattr_blkaddr;
 
 	u32 feature_compat;
 	u32 feature_incompat;
@@ -100,8 +99,9 @@ struct erofs_sb_info {
 	unsigned char blkszbits;
 
 	u32 sb_size;			/* total superblock size */
-	u32 build_time_nsec;
-	u64 build_time;
+	u32 build_time;
+	u32 fixed_nsec;
+	u64 epoch;
 
 	/* what we really care is nid, rather than ino.. */
 	erofs_nid_t root_nid;
@@ -174,6 +174,7 @@ EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING)
 EROFS_FEATURE_FUNCS(fragments, incompat, INCOMPAT_FRAGMENTS)
 EROFS_FEATURE_FUNCS(dedupe, incompat, INCOMPAT_DEDUPE)
 EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES)
+EROFS_FEATURE_FUNCS(48bit, incompat, INCOMPAT_48BIT)
 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
 EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER)
 
@@ -218,8 +219,8 @@ struct erofs_inode {
 	u32 i_nlink;
 
 	union {
-		u32 i_blkaddr;
-		u32 i_blocks;
+		erofs_blk_t i_blkaddr;
+		erofs_blk_t i_blocks;
 		u32 i_rdev;
 		struct {
 			unsigned short	chunkformat;
include/erofs_fs.h +1 -1
index ce319d79..41a398cb 100644
@@ -256,7 +256,7 @@ static inline unsigned int erofs_xattr_entry_size(struct erofs_xattr_entry *e)
 }
 
 /* represent a zeroed chunk (hole) */
-#define EROFS_NULL_ADDR			-1
+#define EROFS_NULL_ADDR			-1ULL
 
 /* 4-byte block address array */
 #define EROFS_BLOCK_MAP_ENTRY_SIZE	sizeof(__le32)
lib/blobchunk.c +1 -1
index 18eafdce..38c44229 100644
@@ -157,7 +157,7 @@ int erofs_blob_write_chunk_indexes(struct erofs_inode *inode,
 		chunk = *(void **)(inode->chunkindexes + src);
 
 		if (chunk->blkaddr == EROFS_NULL_ADDR) {
-			idx.startblk_lo = EROFS_NULL_ADDR;
+			idx.startblk_lo = (u32)EROFS_NULL_ADDR;
 		} else if (chunk->device_id) {
 			DBG_BUGON(!(inode->u.chunkformat & EROFS_CHUNK_FORMAT_INDEXES));
 			idx.startblk_lo = chunk->blkaddr;
lib/block_list.c +4 -4
index 4a6466de..f8dc9138 100644
@@ -38,9 +38,9 @@ void tarerofs_blocklist_write(erofs_blk_t blkaddr, erofs_blk_t nblocks,
 		return;
 
 	if (zeroedlen)
-		fprintf(block_list_fp, "%08x %8x %08" PRIx64 " %08u\n",
-			blkaddr, nblocks, srcoff, zeroedlen);
+		fprintf(block_list_fp, "%08llx %8llx %08" PRIx64 " %08u\n",
+			blkaddr | 0ULL, nblocks | 0ULL, srcoff, zeroedlen);
 	else
-		fprintf(block_list_fp, "%08x %8x %08" PRIx64 "\n",
-			blkaddr, nblocks, srcoff);
+		fprintf(block_list_fp, "%08llx %8llx %08" PRIx64 "\n",
+			blkaddr | 0ULL, nblocks | 0ULL, srcoff);
 }
lib/data.c +55 -88

173 lines not on this page · raw message

lib/inode.c +9 -9
index 5ccffc09..026a71b4 100644
@@ -923,8 +923,8 @@ static bool erofs_should_use_inode_extended(struct erofs_inode *inode,
 	if (inode->i_nlink > USHRT_MAX)
 		return true;
 	if (path != EROFS_PACKED_INODE &&
-	    (inode->i_mtime != inode->sbi->build_time ||
-	     inode->i_mtime_nsec != inode->sbi->build_time_nsec) &&
+	    (inode->i_mtime != inode->sbi->epoch ||
+	     inode->i_mtime_nsec != inode->sbi->fixed_nsec) &&
 	    !cfg.c_ignore_mtime)
 		return true;
 	return false;
@@ -1016,8 +1016,8 @@ int __erofs_fill_inode(struct erofs_inode *inode, struct stat *st,
 	inode->i_gid += cfg.c_gid_offset;
 
 	if (path == EROFS_PACKED_INODE) {
-		inode->i_mtime = sbi->build_time;
-		inode->i_mtime_nsec = sbi->build_time_nsec;
+		inode->i_mtime = sbi->epoch + sbi->build_time;
+		inode->i_mtime_nsec = sbi->fixed_nsec;
 		return 0;
 	}
 	inode->i_mtime = st->st_mtime;
@@ -1025,11 +1025,11 @@ int __erofs_fill_inode(struct erofs_inode *inode, struct stat *st,
 
 	switch (cfg.c_timeinherit) {
 	case TIMESTAMP_CLAMPING:
-		if (inode->i_mtime < sbi->build_time)
+		if (inode->i_mtime < sbi->epoch + sbi->build_time)
 			break;
 	case TIMESTAMP_FIXED:
-		inode->i_mtime = sbi->build_time;
-		inode->i_mtime_nsec = sbi->build_time_nsec;
+		inode->i_mtime = sbi->epoch + sbi->build_time;
+		inode->i_mtime_nsec = sbi->fixed_nsec;
 	default:
 		break;
 	}
@@ -2048,8 +2048,8 @@ struct erofs_inode *erofs_rebuild_make_root(struct erofs_sb_info *sbi)
 	root->i_srcpath = strdup("/");
 	root->i_mode = S_IFDIR | 0777;
 	root->i_parent = root;
-	root->i_mtime = root->sbi->build_time;
-	root->i_mtime_nsec = root->sbi->build_time_nsec;
+	root->i_mtime = root->sbi->epoch + root->sbi->build_time;
+	root->i_mtime_nsec = root->sbi->fixed_nsec;
 	erofs_init_empty_dir(root);
 	return root;
 }
lib/namei.c +24 -11
index 66b8eef4..dec544c9 100644
@@ -28,9 +28,9 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 	char buf[sizeof(struct erofs_inode_extended)];
 	erofs_off_t inode_loc = erofs_iloc(vi);
 	struct erofs_sb_info *sbi = vi->sbi;
+	erofs_blk_t addrmask = BIT_ULL(48) - 1;
+	struct erofs_inode_extended *die, copied;
 	struct erofs_inode_compact *dic;
-	struct erofs_inode_extended *die;
-	union erofs_inode_i_u iu;
 
 	DBG_BUGON(!sbi);
 	ret = erofs_dev_read(sbi, 0, buf, inode_loc, sizeof(*dic));
@@ -60,7 +60,8 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 		vi->xattr_isize = erofs_xattr_ibody_size(die->i_xattr_icount);
 		vi->i_mode = le16_to_cpu(die->i_mode);
 		vi->i_ino[0] = le32_to_cpu(die->i_ino);
-		iu = die->i_u;
+		copied.i_u = die->i_u;
+		copied.i_nb = die->i_nb;
 		vi->i_uid = le32_to_cpu(die->i_uid);
 		vi->i_gid = le32_to_cpu(die->i_gid);
 		vi->i_nlink = le32_to_cpu(die->i_nlink);
@@ -74,13 +75,21 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 		vi->xattr_isize = erofs_xattr_ibody_size(dic->i_xattr_icount);
 		vi->i_mode = le16_to_cpu(dic->i_mode);
 		vi->i_ino[0] = le32_to_cpu(dic->i_ino);
-		iu = dic->i_u;
+		copied.i_u = dic->i_u;
+		copied.i_nb = dic->i_nb;
 		vi->i_uid = le16_to_cpu(dic->i_uid);
 		vi->i_gid = le16_to_cpu(dic->i_gid);
-		vi->i_nlink = le16_to_cpu(dic->i_nb.nlink);
-
-		vi->i_mtime = sbi->build_time;
-		vi->i_mtime_nsec = sbi->build_time_nsec;
+		if (!S_ISDIR(vi->i_mode) &&
+		    ((ifmt >> EROFS_I_NLINK_1_BIT) & 1)) {
+			vi->i_nlink = 1;
+			copied.i_nb = dic->i_nb;
+		} else {
+			vi->i_nlink = le16_to_cpu(dic->i_nb.nlink);
+			copied.i_nb.startblk_hi = 0;
+			addrmask = BIT_ULL(32) - 1;
+		}
+		vi->i_mtime = sbi->epoch + le32_to_cpu(dic->i_mtime);
+		vi->i_mtime_nsec = sbi->fixed_nsec;
 
 		vi->i_size = le32_to_cpu(dic->i_size);
 		break;
@@ -94,11 +103,15 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 	case S_IFREG:
 	case S_IFDIR:
 	case S_IFLNK:
-		vi->u.i_blkaddr = le32_to_cpu(iu.startblk_lo);
+		vi->u.i_blkaddr = le32_to_cpu(copied.i_u.startblk_lo) |
+			((u64)le16_to_cpu(copied.i_nb.startblk_hi) << 32);
+		if (vi->datalayout == EROFS_INODE_FLAT_PLAIN &&
+		    !((vi->u.i_blkaddr ^ EROFS_NULL_ADDR) & addrmask))
+			vi->u.i_blkaddr = EROFS_NULL_ADDR;
 		break;
 	case S_IFCHR:
 	case S_IFBLK:
-		vi->u.i_rdev = erofs_new_decode_dev(le32_to_cpu(iu.rdev));
+		vi->u.i_rdev = erofs_new_decode_dev(le32_to_cpu(copied.i_u.rdev));
 		break;
 	case S_IFIFO:
 	case S_IFSOCK:
@@ -113,7 +126,7 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 	vi->flags = 0;
 	if (vi->datalayout == EROFS_INODE_CHUNK_BASED) {
 		/* fill chunked inode summary info */
-		vi->u.chunkformat = le16_to_cpu(iu.c.format);
+		vi->u.chunkformat = le16_to_cpu(copied.i_u.c.format);
 		if (vi->u.chunkformat & ~EROFS_CHUNK_FORMAT_ALL) {
 			erofs_err("unsupported chunk format %x of nid %llu",
 				  vi->u.chunkformat, vi->nid | 0ULL);
lib/super.c +13 -5
index 6a59a236..e4696f87 100644
@@ -115,13 +115,20 @@ int erofs_read_superblock(struct erofs_sb_info *sbi)
 	sbi->xattr_prefix_start = le32_to_cpu(dsb->xattr_prefix_start);
 	sbi->xattr_prefix_count = dsb->xattr_prefix_count;
 	sbi->islotbits = EROFS_ISLOTBITS;
-	sbi->root_nid = le16_to_cpu(dsb->rb.rootnid_2b);
+	if (erofs_sb_has_48bit(sbi) && dsb->rootnid_8b) {
+		sbi->root_nid = le64_to_cpu(dsb->rootnid_8b);
+		sbi->primarydevice_blocks = (sbi->primarydevice_blocks << 32) |
+				le16_to_cpu(dsb->rb.blocks_hi);
+	} else {
+		sbi->root_nid = le16_to_cpu(dsb->rb.rootnid_2b);
+	}
 	sbi->packed_nid = le64_to_cpu(dsb->packed_nid);
 	sbi->inos = le64_to_cpu(dsb->inos);
 	sbi->checksum = le32_to_cpu(dsb->checksum);
 
-	sbi->build_time = le64_to_cpu(dsb->epoch);
-	sbi->build_time_nsec = le32_to_cpu(dsb->fixed_nsec);
+	sbi->epoch = (s64)le64_to_cpu(dsb->epoch);
+	sbi->fixed_nsec = le32_to_cpu(dsb->fixed_nsec);
+	sbi->build_time = le32_to_cpu(dsb->build_time);
 
 	memcpy(&sbi->uuid, dsb->uuid, sizeof(dsb->uuid));
 
@@ -166,8 +173,9 @@ int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh)
 		.blkszbits = sbi->blkszbits,
 		.rb.rootnid_2b  = cpu_to_le16(sbi->root_nid),
 		.inos      = cpu_to_le64(sbi->inos),
-		.epoch     = cpu_to_le64(sbi->build_time),
-		.fixed_nsec = cpu_to_le32(sbi->build_time_nsec),
+		.epoch     = cpu_to_le64(sbi->epoch),
+		.build_time = cpu_to_le64(sbi->build_time),
+		.fixed_nsec = cpu_to_le32(sbi->fixed_nsec),
 		.meta_blkaddr  = cpu_to_le32(sbi->meta_blkaddr),
 		.xattr_blkaddr = cpu_to_le32(sbi->xattr_blkaddr),
 		.xattr_prefix_count = sbi->xattr_prefix_count,
mkfs/main.c +4 -4
index c266f617..579b90fe 100644
@@ -1231,11 +1231,11 @@ static void erofs_mkfs_showsummaries(void)
 	fprintf(stdout, "------\nFilesystem UUID: %s\n"
 		"Filesystem total blocks: %llu (of %u-byte blocks)\n"
 		"Filesystem total inodes: %llu\n"
-		"Filesystem %s metadata blocks: %u\n"
+		"Filesystem %s metadata blocks: %llu\n"
 		"Filesystem %s deduplicated bytes (of source files): %llu\n",
 		uuid_str, g_sbi.total_blocks | 0ULL, 1U << g_sbi.blkszbits,
 		g_sbi.inos | 0ULL,
-		incr, erofs_total_metablocks(g_sbi.bmgr),
+		incr, erofs_total_metablocks(g_sbi.bmgr) | 0ULL,
 		incr, g_sbi.saved_by_deduplication | 0ULL);
 }
 
@@ -1268,10 +1268,10 @@ int main(int argc, char **argv)
 
 	if (cfg.c_unix_timestamp != -1) {
 		g_sbi.build_time      = cfg.c_unix_timestamp;
-		g_sbi.build_time_nsec = 0;
+		g_sbi.fixed_nsec      = 0;
 	} else if (!gettimeofday(&t, NULL)) {
 		g_sbi.build_time      = t.tv_sec;
-		g_sbi.build_time_nsec = t.tv_usec;
+		g_sbi.fixed_nsec      = t.tv_usec;
 	}
 
 	err = erofs_dev_open(&g_sbi, cfg.c_img_path, O_RDWR |
Signature · 1 line
2.43.5
author
#6 lore raw

PATCH v3 6/9 erofs-utils: mkfs: support 48-bit block addressing for unencoded inodes

In reply to Gao Xiang

Add an extended option -E48bit to indicate 48-bit block addressing is used. Then, 32-byte inodes is preferred since they have per-inode timestamps too.

---
 lib/blobchunk.c | 31 +++++++++++------
 lib/inode.c     | 91 +++++++++++++++++++++++++++++++++++++++++++------
 lib/super.c     |  5 +++
 mkfs/main.c     | 27 ++++++++++++---
 4 files changed, 129 insertions(+), 25 deletions(-)
lib/blobchunk.c +21 -10
index 38c44229..ccc77b0b 100644
@@ -139,10 +139,11 @@ int erofs_blob_write_chunk_indexes(struct erofs_inode *inode,
 	struct erofs_sb_info *sbi = inode->sbi;
 	erofs_blk_t remaining_blks = BLK_ROUND_UP(sbi, inode->i_size);
 	struct erofs_inode_chunk_index idx = {0};
+	erofs_blk_t extent_end = EROFS_NULL_ADDR, chunkblks, addrmask;
 	erofs_blk_t extent_start = EROFS_NULL_ADDR;
-	erofs_blk_t extent_end, chunkblks;
 	erofs_off_t source_offset;
 	unsigned int dst, src, unit, zeroedlen;
+	bool _48bit;
 
 	if (inode->u.chunkformat & EROFS_CHUNK_FORMAT_INDEXES)
 		unit = sizeof(struct erofs_inode_chunk_index);
@@ -150,36 +151,42 @@ int erofs_blob_write_chunk_indexes(struct erofs_inode *inode,
 		unit = EROFS_BLOCK_MAP_ENTRY_SIZE;
 
 	chunkblks = 1U << (inode->u.chunkformat & EROFS_CHUNK_FORMAT_BLKBITS_MASK);
+	_48bit = inode->u.chunkformat & EROFS_CHUNK_FORMAT_48BIT;
 	for (dst = src = 0; dst < inode->extent_isize;
 	     src += sizeof(void *), dst += unit) {
 		struct erofs_blobchunk *chunk;
+		erofs_blk_t startblk;
 
 		chunk = *(void **)(inode->chunkindexes + src);
 
 		if (chunk->blkaddr == EROFS_NULL_ADDR) {
-			idx.startblk_lo = (u32)EROFS_NULL_ADDR;
+			startblk = EROFS_NULL_ADDR;
 		} else if (chunk->device_id) {
 			DBG_BUGON(!(inode->u.chunkformat & EROFS_CHUNK_FORMAT_INDEXES));
-			idx.startblk_lo = chunk->blkaddr;
+			startblk = chunk->blkaddr;
 			extent_start = EROFS_NULL_ADDR;
 		} else {
-			idx.startblk_lo = remapped_base + chunk->blkaddr;
+			startblk = remapped_base + chunk->blkaddr;
 		}
 
-		if (extent_start == EROFS_NULL_ADDR ||
-		    idx.startblk_lo != extent_end) {
+		if (extent_start == EROFS_NULL_ADDR || startblk != extent_end) {
 			if (extent_start != EROFS_NULL_ADDR) {
 				remaining_blks -= extent_end - extent_start;
 				tarerofs_blocklist_write(extent_start,
 						extent_end - extent_start,
 						source_offset, 0);
 			}
-			extent_start = idx.startblk_lo;
+			extent_start = startblk;
 			source_offset = chunk->sourceoffset;
 		}
-		extent_end = idx.startblk_lo + chunkblks;
+		extent_end = startblk + chunkblks;
+
+		addrmask = _48bit ? BIT_ULL(48) - 1 : BIT_ULL(32) - 1;
+		startblk &= addrmask;
 		idx.device_id = cpu_to_le16(chunk->device_id);
-		idx.startblk_lo = cpu_to_le32(idx.startblk_lo);
+		idx.startblk_lo = cpu_to_le32(startblk);
+		idx.startblk_hi = cpu_to_le32(startblk >> 32);
+		DBG_BUGON(!_48bit && idx.startblk_hi);
 
 		if (unit == EROFS_BLOCK_MAP_ENTRY_SIZE)
 			memcpy(inode->chunkindexes + dst, &idx.startblk_lo, unit);
@@ -187,8 +194,8 @@ int erofs_blob_write_chunk_indexes(struct erofs_inode *inode,
 			memcpy(inode->chunkindexes + dst, &idx, sizeof(idx));
 	}
 	off = roundup(off, unit);
-	extent_end = min(extent_end, extent_start + remaining_blks);
 	if (extent_start != EROFS_NULL_ADDR) {
+		extent_end = min(extent_end, extent_start + remaining_blks);
 		zeroedlen = inode->i_size & (erofs_blksiz(sbi) - 1);
 		if (zeroedlen)
 			zeroedlen = erofs_blksiz(sbi) - zeroedlen;
@@ -355,6 +362,10 @@ int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
 			goto err;
 		}
 
+		/* FIXME! `chunk->blkaddr` is not the final blkaddr here */
+		if (chunk->blkaddr != EROFS_NULL_ADDR &&
+		    chunk->blkaddr >= UINT32_MAX)
+			inode->u.chunkformat |= EROFS_CHUNK_FORMAT_48BIT;
 		if (!erofs_blob_can_merge(sbi, lastch, chunk)) {
 			erofs_update_minextblks(sbi, interval_start, pos,
 						&minextblks);
lib/inode.c +80 -11

171 lines not on this page · raw message

lib/super.c +5 -0
index e4696f87..29c36527 100644
@@ -192,6 +192,11 @@ int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh)
 	int ret;
 
 	sb.blocks_lo	= cpu_to_le32(sbi->primarydevice_blocks);
+	if (sbi->primarydevice_blocks > UINT32_MAX ||
+	    sbi->root_nid > UINT16_MAX) {
+		sb.rb.blocks_hi = sb.rb.blocks_hi;
+		sb.rootnid_8b = cpu_to_le64(sbi->root_nid);
+	}
 	memcpy(sb.uuid, sbi->uuid, sizeof(sb.uuid));
 	memcpy(sb.volume_name, sbi->volume_name, sizeof(sb.volume_name));
 
mkfs/main.c +23 -4
index 579b90fe..93b4ff2d 100644
@@ -333,6 +333,18 @@ static int erofs_mkfs_feat_set_fragdedupe(bool en, const char *val,
 	return 0;
 }
 
+static int erofs_mkfs_feat_set_48bit(bool en, const char *val,
+				     unsigned int vallen)
+{
+	if (vallen)
+		return -EINVAL;
+	if (en)
+		erofs_sb_set_48bit(&g_sbi);
+	else
+		erofs_sb_clear_48bit(&g_sbi);
+	return 0;
+}
+
 static struct {
 	char *feat;
 	int (*set)(bool en, const char *val, unsigned int len);
@@ -343,6 +355,7 @@ static struct {
 	{"all-fragments", erofs_mkfs_feat_set_all_fragments},
 	{"dedupe", erofs_mkfs_feat_set_dedupe},
 	{"fragdedupe", erofs_mkfs_feat_set_fragdedupe},
+	{"48bit", erofs_mkfs_feat_set_48bit},
 	{NULL, NULL},
 };
 
@@ -1246,6 +1259,7 @@ int main(int argc, char **argv)
 	bool tar_index_512b = false;
 	struct timeval t;
 	FILE *blklst = NULL;
+	s64 mkfs_time = 0;
 	int err = 0;
 	u32 crc;
 
@@ -1266,12 +1280,17 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
+	g_sbi.fixed_nsec = 0;
 	if (cfg.c_unix_timestamp != -1) {
-		g_sbi.build_time      = cfg.c_unix_timestamp;
-		g_sbi.fixed_nsec      = 0;
+		mkfs_time = cfg.c_unix_timestamp;
 	} else if (!gettimeofday(&t, NULL)) {
-		g_sbi.build_time      = t.tv_sec;
-		g_sbi.fixed_nsec      = t.tv_usec;
+		mkfs_time = t.tv_sec;
+	}
+	if (erofs_sb_has_48bit(&g_sbi)) {
+		g_sbi.epoch = max_t(s64, 0, mkfs_time - UINT32_MAX);
+		g_sbi.build_time = mkfs_time - g_sbi.epoch;
+	} else {
+		g_sbi.epoch = mkfs_time;
 	}
 
 	err = erofs_dev_open(&g_sbi, cfg.c_img_path, O_RDWR |
Signature · 1 line
2.43.5
author
#7 lore raw

PATCH v3 7/9 erofs-utils: support dot-omitted directories

In reply to Gao Xiang

Omit the dot dirent by default if 48bit layout is set.

---
 include/erofs/config.h   |  1 +
 include/erofs/internal.h |  1 +
 lib/dir.c                |  2 ++
 lib/inode.c              | 35 +++++++++++++++++++++++++----------
 lib/namei.c              |  3 ++-
 mkfs/main.c              | 14 ++++++++++++++
 6 files changed, 45 insertions(+), 11 deletions(-)
include/erofs/config.h +1 -0
index 9b42dbf8..e4d2bb32 100644
@@ -66,6 +66,7 @@ struct erofs_configure {
 	bool c_xattr_name_filter;
 	bool c_ovlfs_strip;
 	bool c_hard_dereference;
+	bool c_dot_omitted;
 
 #ifdef HAVE_LIBSELINUX
 	struct selabel_handle *sehnd;
include/erofs/internal.h +1 -0
index 51def099..a35b9e79 100644
@@ -243,6 +243,7 @@ struct erofs_inode {
 	bool opaque;
 	/* OVL: non-merge dir that may contain whiteout entries */
 	bool whiteouts;
+	bool dot_omitted;
 
 	unsigned int xattr_isize;
 	unsigned int extent_isize;
lib/dir.c +2 -0
index d786a5b9..3405844c 100644
@@ -150,6 +150,8 @@ int erofs_iterate_dir(struct erofs_dir_context *ctx, bool fsck)
 		return -ENOTDIR;
 
 	ctx->flags &= ~EROFS_READDIR_ALL_SPECIAL_FOUND;
+	if (dir->dot_omitted)
+		ctx->flags |= EROFS_READDIR_DOT_FOUND;
 	pos = 0;
 	while (pos < dir->i_size) {
 		erofs_blk_t lblk = erofs_blknr(sbi, pos);
lib/inode.c +25 -10
index a97425f5..4f6715af 100644
@@ -253,21 +253,29 @@ static int erofs_prepare_dir_file(struct erofs_inode *dir,
 {
 	struct erofs_sb_info *sbi = dir->sbi;
 	struct erofs_dentry *d, *n, **sorted_d;
+	bool dot_omitted = cfg.c_dot_omitted;
 	unsigned int i;
 	unsigned int d_size = 0;
 
 	sorted_d = malloc(nr_subdirs * sizeof(d));
 	if (!sorted_d)
 		return -ENOMEM;
+
+	dir->dot_omitted = dot_omitted;
 	i = 0;
 	list_for_each_entry_safe(d, n, &dir->i_subdirs, d_child) {
 		list_del(&d->d_child);
+		if (dot_omitted && !strcmp(d->name, ".")) {
+			erofs_iput(d->inode);
+			free(d);
+			continue;
+		}
 		sorted_d[i++] = d;
 	}
-	DBG_BUGON(i != nr_subdirs);
-	qsort(sorted_d, nr_subdirs, sizeof(d), comp_subdir);
-	for (i = 0; i < nr_subdirs; i++)
-		list_add_tail(&sorted_d[i]->d_child, &dir->i_subdirs);
+	DBG_BUGON(i + dot_omitted != nr_subdirs);
+	qsort(sorted_d, i, sizeof(d), comp_subdir);
+	while (i)
+		list_add(&sorted_d[--i]->d_child, &dir->i_subdirs);
 	free(sorted_d);
 
 	/* let's calculate dir size */
@@ -644,10 +652,12 @@ int erofs_iflush(struct erofs_inode *inode)
 		nlink_1 = false;
 		nb = (union erofs_inode_i_nb){};
 	}
+	fmt = S_ISDIR(inode->i_mode) && inode->dot_omitted ?
+		1 << EROFS_I_DOT_OMITTED_BIT : 0;
 
 	switch (inode->inode_isize) {
 	case sizeof(struct erofs_inode_compact):
-		fmt = 0 | (inode->datalayout << 1);
+		fmt |= 0 | (inode->datalayout << 1);
 		u.dic.i_xattr_icount = cpu_to_le16(icount);
 		u.dic.i_mode = cpu_to_le16(inode->i_mode);
 		u.dic.i_nb.nlink = cpu_to_le16(inode->i_nlink);
@@ -672,7 +682,8 @@ int erofs_iflush(struct erofs_inode *inode)
 		u.dic.i_format = cpu_to_le16(fmt);
 		break;
 	case sizeof(struct erofs_inode_extended):
-		u.die.i_format = cpu_to_le16(1 | (inode->datalayout << 1));
+		fmt |= 1 | (inode->datalayout << 1);
+		u.die.i_format = cpu_to_le16(fmt);
 		u.die.i_xattr_icount = cpu_to_le16(icount);
 		u.die.i_mode = cpu_to_le16(inode->i_mode);
 		u.die.i_nlink = cpu_to_le32(inode->i_nlink);
@@ -1563,13 +1574,17 @@ static int erofs_mkfs_handle_directory(struct erofs_inode *dir)
 
 	/*
 	 * if there're too many subdirs as compact form, set nlink=1
-	 * rather than upgrade to use extented form instead.
+	 * rather than upgrade to use extented form instead if possible.
 	 */
 	if (i_nlink > USHRT_MAX &&
-	    dir->inode_isize == sizeof(struct erofs_inode_compact))
-		dir->i_nlink = 1;
-	else
+	    dir->inode_isize == sizeof(struct erofs_inode_compact)) {
+		if (dir->dot_omitted)
+			dir->inode_isize = sizeof(struct erofs_inode_extended);
+		else
+			dir->i_nlink = 1;
+	} else {
 		dir->i_nlink = i_nlink;
+	}
 
 	return erofs_mkfs_go(sbi, EROFS_MKFS_JOB_DIR, &dir, sizeof(dir));
 
lib/namei.c +2 -1
index dec544c9..5da8ed98 100644
@@ -100,8 +100,9 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 	}
 
 	switch (vi->i_mode & S_IFMT) {
-	case S_IFREG:
 	case S_IFDIR:
+		vi->dot_omitted = (ifmt >> EROFS_I_DOT_OMITTED_BIT) & 1;
+	case S_IFREG:
 	case S_IFLNK:
 		vi->u.i_blkaddr = le32_to_cpu(copied.i_u.startblk_lo) |
 			((u64)le16_to_cpu(copied.i_nb.startblk_hi) << 32);
mkfs/main.c +14 -0
index 93b4ff2d..fce26336 100644
@@ -345,6 +345,16 @@ static int erofs_mkfs_feat_set_48bit(bool en, const char *val,
 	return 0;
 }
 
+static int erofs_mkfs_feat_set_dot_omitted(bool en, const char *val,
+					   unsigned int vallen)
+{
+	if (vallen)
+		return -EINVAL;
+
+	cfg.c_dot_omitted = en;
+	return 0;
+}
+
 static struct {
 	char *feat;
 	int (*set)(bool en, const char *val, unsigned int len);
@@ -356,6 +366,7 @@ static struct {
 	{"dedupe", erofs_mkfs_feat_set_dedupe},
 	{"fragdedupe", erofs_mkfs_feat_set_fragdedupe},
 	{"48bit", erofs_mkfs_feat_set_48bit},
+	{"dot-omitted", erofs_mkfs_feat_set_dot_omitted},
 	{NULL, NULL},
 };
 
@@ -1293,6 +1304,9 @@ int main(int argc, char **argv)
 		g_sbi.epoch = mkfs_time;
 	}
 
+	if (cfg.c_dot_omitted)
+		erofs_sb_set_48bit(&g_sbi);
+
 	err = erofs_dev_open(&g_sbi, cfg.c_img_path, O_RDWR |
 				(incremental_mode ? 0 : O_TRUNC));
 	if (err) {
Signature · 1 line
2.43.5
author
#8 lore raw

PATCH v3 8/9 erofs-utils: lib: implement encoded extent metadata

In reply to Gao Xiang

Source kernel commit: efb2aef569b35b415c232c4e9fdecd0e540e1f60 Source kernel commit: 1d191b4ca51d73699cb127386b95ac152af2b930 Source kernel commit: 7869738b6908eec7755818aaf6f3aa068b2f0e1b Source kernel commit: 417b8af2e30d7f131682a893ad79c506fd39c624

---
 fsck/main.c              |   2 +-
 include/erofs/internal.h |   3 +-
 lib/compress.c           |  14 ++--
 lib/zmap.c               | 176 ++++++++++++++++++++++++++++++++++-----
 4 files changed, 164 insertions(+), 31 deletions(-)
fsck/main.c +1 -1
index 8f619a55..91c193f8 100644
@@ -549,7 +549,7 @@ static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)
 		}
 
 		if (map.m_plen > Z_EROFS_PCLUSTER_MAX_SIZE) {
-			if (compressed) {
+			if (compressed && !(map.m_flags & EROFS_MAP_FRAGMENT)) {
 				erofs_err("invalid pcluster size %" PRIu64 " @ offset %" PRIu64 " of nid %" PRIu64,
 					  map.m_plen, map.m_la,
 					  inode->nid | 0ULL);
include/erofs/internal.h +2 -1
index a35b9e79..14a8e30d 100644
@@ -266,11 +266,12 @@ struct erofs_inode {
 		struct {
 			uint16_t z_advise;
 			uint8_t  z_algorithmtype[2];
-			uint8_t  z_logical_clusterbits;
+			uint8_t  z_lclusterbits;
 			uint8_t  z_physical_clusterblks;
 			union {
 				uint64_t z_tailextent_headlcn;
 				erofs_off_t fragment_size;
+				u64		z_extents;
 			};
 			union {
 				erofs_off_t	fragmentoff;
lib/compress.c +7 -7
index 6e7b5241..5d4d47f9 100644
@@ -289,7 +289,7 @@ static void z_erofs_commit_extent(struct z_erofs_compress_sctx *ctx,
 static int z_erofs_compress_dedupe(struct z_erofs_compress_sctx *ctx)
 {
 	struct erofs_inode *inode = ctx->ictx->inode;
-	const unsigned int lclustermask = (1 << inode->z_logical_clusterbits) - 1;
+	const unsigned int lclustermask = (1 << inode->z_lclusterbits) - 1;
 	struct erofs_sb_info *sbi = inode->sbi;
 	struct z_erofs_extent_item *ei = ctx->pivot;
 
@@ -422,7 +422,7 @@ static int write_uncompressed_extents(struct z_erofs_compress_sctx *ctx,
 				      char *dst)
 {
 	struct erofs_inode *inode = ctx->ictx->inode;
-	unsigned int lclustersize = 1 << inode->z_logical_clusterbits;
+	unsigned int lclustersize = 1 << inode->z_lclusterbits;
 	struct z_erofs_extent_item *ei;
 	int count;
 
@@ -889,7 +889,7 @@ int z_erofs_convert_to_compacted_format(struct erofs_inode *inode,
 	const unsigned int totalidx = (legacymetasize -
 			Z_EROFS_LEGACY_MAP_HEADER_SIZE) /
 				sizeof(struct z_erofs_lcluster_index);
-	const unsigned int logical_clusterbits = inode->z_logical_clusterbits;
+	const unsigned int logical_clusterbits = inode->z_lclusterbits;
 	u8 *out, *in;
 	struct z_erofs_compressindex_vec cv[16];
 	struct erofs_sb_info *sbi = inode->sbi;
@@ -998,7 +998,7 @@ static void z_erofs_write_mapheader(struct erofs_inode *inode,
 		.h_algorithmtype = inode->z_algorithmtype[1] << 4 |
 				   inode->z_algorithmtype[0],
 		/* lclustersize */
-		.h_clusterbits = inode->z_logical_clusterbits - sbi->blkszbits,
+		.h_clusterbits = inode->z_lclusterbits - sbi->blkszbits,
 	};
 
 	if (inode->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER)
@@ -1025,8 +1025,8 @@ static void *z_erofs_write_indexes(struct z_erofs_compress_ictx *ctx)
 	if (inode->fragment_size && inode->fragmentoff >> 32) {
 		inode->datalayout = EROFS_INODE_COMPRESSED_FULL;
 	} else if (!cfg.c_legacy_compress && !ctx->dedupe &&
-	    inode->z_logical_clusterbits <= 14) {
-		if (inode->z_logical_clusterbits <= 12)
+	    inode->z_lclusterbits <= 14) {
+		if (inode->z_lclusterbits <= 12)
 			inode->z_advise |= Z_EROFS_ADVISE_COMPACTED_2B;
 		inode->datalayout = EROFS_INODE_COMPRESSED_COMPACT;
 	} else {
@@ -1631,7 +1631,7 @@ void *erofs_begin_compressed_file(struct erofs_inode *inode, int fd, u64 fpos)
 
 	/* initialize per-file compression setting */
 	inode->z_advise = 0;
-	inode->z_logical_clusterbits = sbi->blkszbits;
+	inode->z_lclusterbits = sbi->blkszbits;
 	if (cfg.c_fragments && !cfg.c_dedupe)
 		inode->z_advise |= Z_EROFS_ADVISE_INTERLACED_PCLUSTER;
 
lib/zmap.c +154 -22

271 lines not on this page · raw message

Signature · 1 line
2.43.5
author
#9 lore raw

PATCH v3 9/9 erofs-utils: support encoded extents

In reply to Gao Xiang

Use encoded extents if 48bit is set and metadata is smaller for big pclusters.

For Zstd and libdeflate, since they don't natively support fixed-size output compression, switch to use fixed-size input compression if --max-extent-bytes= is specified and no more than -C. Later we will introduce a simpilified option for users too.

---
 include/erofs/internal.h    |   2 +-
 include/erofs_fs.h          |   3 +-
 lib/compress.c              | 224 +++++++++++++++++++++++++++++-------
 lib/compressor.c            |  11 ++
 lib/compressor.h            |   6 +
 lib/compressor_libdeflate.c |  18 +++
 lib/compressor_libzstd.c    |  18 +++
 7 files changed, 237 insertions(+), 45 deletions(-)
include/erofs/internal.h +1 -1
index 14a8e30d..d380c45e 100644
@@ -271,13 +271,13 @@ struct erofs_inode {
 			union {
 				uint64_t z_tailextent_headlcn;
 				erofs_off_t fragment_size;
-				u64		z_extents;
 			};
 			union {
 				erofs_off_t	fragmentoff;
 				erofs_off_t	z_fragmentoff;
 				void *fragment;
 			};
+			u64	z_extents;
 #define z_idata_size	idata_size
 		};
 	};
include/erofs_fs.h +2 -1
index 41a398cb..e180c5d4 100644
@@ -413,8 +413,9 @@ struct z_erofs_lcluster_index {
 	} di_u;
 };
 
+#define Z_EROFS_MAP_HEADER_START(end)	round_up(end, 8)
 #define Z_EROFS_MAP_HEADER_END(end)	\
-	(round_up(end, 8) + sizeof(struct z_erofs_map_header))
+	(Z_EROFS_MAP_HEADER_START(end) + sizeof(struct z_erofs_map_header))
 #define Z_EROFS_FULL_INDEX_START(end)	(Z_EROFS_MAP_HEADER_END(end) + 8)
 
 #define Z_EROFS_EXTENT_PLEN_PARTIAL	BIT(27)
lib/compress.c +181 -43

356 lines not on this page · raw message

lib/compressor.c +11 -0
index 41f49fff..6d8c1c23 100644
@@ -85,6 +85,17 @@ int erofs_compress_destsize(const struct erofs_compress *c,
 	return c->alg->c->compress_destsize(c, src, srcsize, dst, dstsize);
 }
 
+int erofs_compress(const struct erofs_compress *c,
+		   const void *src, unsigned int srcsize,
+		   void *dst, unsigned int dstcapacity)
+{
+	DBG_BUGON(!c->alg);
+	if (!c->alg->c->compress)
+		return -EOPNOTSUPP;
+
+	return c->alg->c->compress(c, src, srcsize, dst, dstcapacity);
+}
+
 int erofs_compressor_init(struct erofs_sb_info *sbi, struct erofs_compress *c,
 			  char *alg_name, int compression_level, u32 dict_size)
 {
lib/compressor.h +6 -0
index 8d322d59..5f86f155 100644
@@ -26,6 +26,9 @@ struct erofs_compressor {
 	int (*compress_destsize)(const struct erofs_compress *c,
 				 const void *src, unsigned int *srcsize,
 				 void *dst, unsigned int dstsize);
+	int (*compress)(const struct erofs_compress *c,
+			const void *src, unsigned int srcsize,
+			void *dst, unsigned int dstcapacity);
 };
 
 struct erofs_algorithm {
@@ -60,6 +63,9 @@ int z_erofs_get_compress_algorithm_id(const struct erofs_compress *c);
 int erofs_compress_destsize(const struct erofs_compress *c,
 			    const void *src, unsigned int *srcsize,
 			    void *dst, unsigned int dstsize);
+int erofs_compress(const struct erofs_compress *c,
+		   const void *src, unsigned int srcsize,
+		   void *dst, unsigned int dstcapacity);
 
 int erofs_compressor_init(struct erofs_sb_info *sbi, struct erofs_compress *c,
 			  char *alg_name, int compression_level, u32 dict_size);
lib/compressor_libdeflate.c +18 -0
index 65531de8..186da87c 100644
@@ -14,6 +14,23 @@ struct erofs_libdeflate_context {
 	size_t last_uncompressed_size;
 };
 
+static int libdeflate_compress(const struct erofs_compress *c,
+			       const void *src, unsigned int srcsize,
+			       void *dst, unsigned int dstcapacity)
+{
+	struct erofs_libdeflate_context *ctx = c->private_data;
+	size_t csize;
+
+	csize = libdeflate_deflate_compress(ctx->strm, src, srcsize,
+					    dst, dstcapacity);
+	if (!csize)
+		return -ENOSPC;
+	/* See the comment in libdeflate_compress_destsize() */
+	if (!((u8 *)dst)[0])
+		((u8 *)dst)[0] = 1 << (2 + 1);
+	return csize;
+}
+
 static int libdeflate_compress_destsize(const struct erofs_compress *c,
 				        const void *src, unsigned int *srcsize,
 				        void *dst, unsigned int dstsize)
@@ -149,5 +166,6 @@ const struct erofs_compressor erofs_compressor_libdeflate = {
 	.exit = compressor_libdeflate_exit,
 	.reset = compressor_libdeflate_reset,
 	.setlevel = erofs_compressor_libdeflate_setlevel,
+	.compress = libdeflate_compress,
 	.compress_destsize = libdeflate_compress_destsize,
 };
lib/compressor_libzstd.c +18 -0
index de04c58d..3233d723 100644
@@ -14,6 +14,23 @@ struct erofs_libzstd_context {
 	unsigned int fitblk_bufsiz;
 };
 
+static int libzstd_compress(const struct erofs_compress *c,
+			    const void *src, unsigned int srcsize,
+			    void *dst, unsigned int dstcapacity)
+{
+	struct erofs_libzstd_context *ctx = c->private_data;
+	size_t csize;
+
+	csize = ZSTD_compress2(ctx->cctx, dst, dstcapacity, src, srcsize);
+	if (ZSTD_isError(csize)) {
+		if (ZSTD_getErrorCode(csize) == ZSTD_error_dstSize_tooSmall)
+			return -ENOSPC;
+		erofs_err("Zstd compress failed: %s", ZSTD_getErrorName(csize));
+		return -EFAULT;
+	}
+	return csize;
+}
+
 static int libzstd_compress_destsize(const struct erofs_compress *c,
 				     const void *src, unsigned int *srcsize,
 				     void *dst, unsigned int dstsize)
@@ -176,5 +193,6 @@ const struct erofs_compressor erofs_compressor_libzstd = {
 	.exit = compressor_libzstd_exit,
 	.setlevel = erofs_compressor_libzstd_setlevel,
 	.setdictsize = erofs_compressor_libzstd_setdictsize,
+	.compress = libzstd_compress,
 	.compress_destsize = libzstd_compress_destsize,
 };
Signature · 1 line
2.43.5