Skip to contentSkip to conversation

erofs-utils: mkfs: don't generate encoded extents for ztailpacking

PATCH New Gao Xiang 1 participant 11 messages Jul 18, 2025 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/11 erofs-utils: mkfs: don't generate encoded extents for ztailpacking

.. Need more library work for ztailpacking.

Fixes f978245e9da8 ("erofs-utils: support encoded extents")
---
 lib/compress.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/compress.c +4 -1
index a57bb6a3..22fb5d6d 100644
@@ -1142,7 +1142,8 @@ static void *z_erofs_write_indexes(struct z_erofs_compress_ictx *ctx)
 	struct z_erofs_extent_item *ei, *n;
 	void *metabuf;
 
-	if (erofs_sb_has_48bit(sbi)) {
+	/* TODO: support writing encoded extents for ztailpacking later. */
+	if (erofs_sb_has_48bit(sbi) && !inode->idata_size) {
 		metabuf = z_erofs_write_extents(ctx);
 		if (metabuf != ERR_PTR(-EAGAIN)) {
 			if (IS_ERR(metabuf))
@@ -1181,6 +1182,8 @@ static void *z_erofs_write_indexes(struct z_erofs_compress_ictx *ctx)
 	ctx->metacur = metabuf + Z_EROFS_LEGACY_MAP_HEADER_SIZE;
 	ctx->clusterofs = 0;
 	list_for_each_entry_safe(ei, n, &ctx->extents, list) {
+		DBG_BUGON(ei->list.next != &ctx->extents &&
+			  ctx->clusterofs + ei->e.length < erofs_blksiz(sbi));
 		z_erofs_write_full_indexes(ctx, &ei->e);
 
 		list_del(&ei->list);
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 2/11 erofs-utils: lib: consolidate erofs_iflush()

.. Avoid erofs_dev_write() and use the bdev virtual file instead.

---
 include/erofs/internal.h |  3 +-
 lib/blobchunk.c          |  6 ++--
 lib/inode.c              | 67 +++++++++++++++++++---------------------
 lib/super.c              |  1 -
 4 files changed, 35 insertions(+), 42 deletions(-)
include/erofs/internal.h +1 -2
index b5df8a48..65d40840 100644
@@ -95,7 +95,6 @@ struct erofs_sb_info {
 	u32 feature_compat;
 	u32 feature_incompat;
 
-	unsigned char islotbits;
 	unsigned char blkszbits;
 
 	u32 sb_size;			/* total superblock size */
@@ -293,7 +292,7 @@ static inline erofs_off_t erofs_iloc(struct erofs_inode *inode)
 	struct erofs_sb_info *sbi = inode->sbi;
 
 	return erofs_pos(sbi, sbi->meta_blkaddr) +
-			(inode->nid << sbi->islotbits);
+			(inode->nid << EROFS_ISLOTBITS);
 }
 
 static inline bool is_inode_layout_compression(struct erofs_inode *inode)
lib/blobchunk.c +3 -3
index 3388ce0d..bbc69cfb 100644
@@ -193,7 +193,6 @@ int erofs_blob_write_chunk_indexes(struct erofs_inode *inode,
 		else
 			memcpy(inode->chunkindexes + dst, &idx, sizeof(idx));
 	}
-	off = roundup(off, unit);
 	if (extent_start != EROFS_NULL_ADDR) {
 		extent_end = min(extent_end, extent_start + remaining_blks);
 		zeroedlen = inode->i_size & (erofs_blksiz(sbi) - 1);
@@ -202,8 +201,9 @@ int erofs_blob_write_chunk_indexes(struct erofs_inode *inode,
 		tarerofs_blocklist_write(extent_start, extent_end - extent_start,
 					 source_offset, zeroedlen);
 	}
-	return erofs_dev_write(inode->sbi, inode->chunkindexes, off,
-			       inode->extent_isize);
+	off = roundup(off, unit);
+	return erofs_io_pwrite(&sbi->bdev, inode->chunkindexes,
+			       off, inode->extent_isize);
 }
 
 int erofs_blob_mergechunks(struct erofs_inode *inode, unsigned int chunkbits,
lib/inode.c +31 -36

109 lines not on this page · raw message

lib/super.c +0 -1
index 0533a7c8..e5364d27 100644
@@ -114,7 +114,6 @@ int erofs_read_superblock(struct erofs_sb_info *sbi)
 	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;
 	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) |
Signature · 1 line
2.43.5
author
#2 lore raw

PATCH 3/11 erofs-utils: use virtual file interface for the buffer manager

In reply to Gao Xiang

In order to prepare for the upcoming metadata compression.

---
 include/erofs/cache.h    |  9 +++++----
 include/erofs/internal.h |  6 ------
 include/erofs/io.h       |  2 +-
 lib/cache.c              | 12 +++++++++---
 lib/io.c                 |  6 +++---
 mkfs/main.c              |  4 ++--
 6 files changed, 20 insertions(+), 19 deletions(-)
include/erofs/cache.h +5 -4
index 0c665e8f..d5618c00 100644
@@ -57,18 +57,18 @@ struct erofs_buffer_block {
 };
 
 struct erofs_bufmgr {
-	struct erofs_sb_info *sbi;
-
 	/* buckets for all buffer blocks to boost up allocation */
 	struct list_head watermeter[META + 1][2][EROFS_MAX_BLOCK_SIZE];
 	unsigned long bktmap[META + 1][2][EROFS_MAX_BLOCK_SIZE / BITS_PER_LONG];
 
 	struct erofs_buffer_block blkh;
-	erofs_blk_t tail_blkaddr, metablkcnt;
+	struct erofs_sb_info *sbi;
+	struct erofs_vfile *vf;
 
 	/* last mapped buffer block to accelerate erofs_mapbh() */
 	struct erofs_buffer_block *last_mapped_block;
 
+	erofs_blk_t tail_blkaddr, metablkcnt;
 	/* align data block addresses to multiples of `dsunit` */
 	unsigned int dsunit;
 };
@@ -122,7 +122,8 @@ static inline int erofs_bh_flush_generic_end(struct erofs_buffer_head *bh)
 }
 
 struct erofs_bufmgr *erofs_buffer_init(struct erofs_sb_info *sbi,
-				       erofs_blk_t startblk);
+				       erofs_blk_t startblk,
+				       struct erofs_vfile *vf);
 int erofs_bh_balloon(struct erofs_buffer_head *bh, erofs_off_t incr);
 
 struct erofs_buffer_head *erofs_balloc(struct erofs_bufmgr *bmgr,
include/erofs/internal.h +0 -6
index 65d40840..1431c16d 100644
@@ -487,12 +487,6 @@ static inline int erofs_dev_write(struct erofs_sb_info *sbi, const void *buf,
 	return 0;
 }
 
-static inline int erofs_dev_fillzero(struct erofs_sb_info *sbi, u64 offset,
-				     size_t len, bool pad)
-{
-	return erofs_io_fallocate(&sbi->bdev, offset, len, pad);
-}
-
 static inline int erofs_dev_resize(struct erofs_sb_info *sbi,
 				   erofs_blk_t blocks)
 {
include/erofs/io.h +1 -1
index 101a5ba4..14d6e45f 100644
@@ -59,7 +59,7 @@ ssize_t erofs_io_pwrite(struct erofs_vfile *vf, const void *buf, u64 pos, size_t
 ssize_t erofs_io_pwritev(struct erofs_vfile *vf, const struct iovec *iov,
 			 int iovcnt, u64 pos);
 int erofs_io_fsync(struct erofs_vfile *vf);
-ssize_t erofs_io_fallocate(struct erofs_vfile *vf, u64 offset, size_t len, bool pad);
+int erofs_io_fallocate(struct erofs_vfile *vf, u64 offset, size_t len, bool pad);
 int erofs_io_ftruncate(struct erofs_vfile *vf, u64 length);
 ssize_t erofs_io_pread(struct erofs_vfile *vf, void *buf, u64 offset, size_t len);
 ssize_t erofs_io_read(struct erofs_vfile *vf, void *buf, size_t len);
lib/cache.c +9 -3
index b3cf1c48..079465ed 100644
@@ -29,7 +29,8 @@ const struct erofs_bhops erofs_skip_write_bhops = {
 };
 
 struct erofs_bufmgr *erofs_buffer_init(struct erofs_sb_info *sbi,
-				       erofs_blk_t startblk)
+				       erofs_blk_t startblk,
+				       struct erofs_vfile *vf)
 {
 	unsigned int blksiz = erofs_blksiz(sbi);
 	struct erofs_bufmgr *bmgr;
@@ -54,6 +55,7 @@ struct erofs_bufmgr *erofs_buffer_init(struct erofs_sb_info *sbi,
 	bmgr->last_mapped_block = &bmgr->blkh;
 	bmgr->metablkcnt = 0;
 	bmgr->dsunit = 0;
+	bmgr->vf = vf ?: &sbi->bdev;
 	return bmgr;
 }
 
@@ -482,9 +484,13 @@ int erofs_bflush(struct erofs_bufmgr *bmgr,
 			continue;
 
 		padding = blksiz - (p->buffers.off & (blksiz - 1));
-		if (padding != blksiz)
-			erofs_dev_fillzero(sbi, erofs_pos(sbi, blkaddr) - padding,
+		if (padding != blksiz) {
+			ret = erofs_io_fallocate(bmgr->vf,
+					   erofs_pos(sbi, blkaddr) - padding,
 					   padding, true);
+			if (ret < 0)
+				return ret;
+		}
 
 		if (p->type != DATA)
 			bmgr->metablkcnt += p->buffers.nblocks;
lib/io.c +3 -3
index aa043cae..983d9bf9 100644
@@ -147,8 +147,8 @@ int erofs_io_fsync(struct erofs_vfile *vf)
 	return 0;
 }
 
-ssize_t erofs_io_fallocate(struct erofs_vfile *vf, u64 offset,
-			   size_t len, bool zeroout)
+int erofs_io_fallocate(struct erofs_vfile *vf, u64 offset,
+		       size_t len, bool zeroout)
 {
 	static const char zero[EROFS_MAX_BLOCK_SIZE] = {0};
 	ssize_t ret;
@@ -167,7 +167,7 @@ ssize_t erofs_io_fallocate(struct erofs_vfile *vf, u64 offset,
 	while (len > EROFS_MAX_BLOCK_SIZE) {
 		ret = erofs_io_pwrite(vf, zero, offset, EROFS_MAX_BLOCK_SIZE);
 		if (ret < 0)
-			return ret;
+			return (int)ret;
 		len -= ret;
 		offset += ret;
 	}
mkfs/main.c +2 -2
index fce26336..30804d10 100644
@@ -1377,7 +1377,7 @@ int main(int argc, char **argv)
 	}
 
 	if (!incremental_mode) {
-		g_sbi.bmgr = erofs_buffer_init(&g_sbi, 0);
+		g_sbi.bmgr = erofs_buffer_init(&g_sbi, 0, NULL);
 		if (!g_sbi.bmgr) {
 			err = -ENOMEM;
 			goto exit;
@@ -1405,7 +1405,7 @@ int main(int argc, char **argv)
 			u.startblk = DIV_ROUND_UP(u.st.st_size, erofs_blksiz(&g_sbi));
 		else
 			u.startblk = g_sbi.primarydevice_blocks;
-		g_sbi.bmgr = erofs_buffer_init(&g_sbi, u.startblk);
+		g_sbi.bmgr = erofs_buffer_init(&g_sbi, u.startblk, NULL);
 		if (!g_sbi.bmgr) {
 			err = -ENOMEM;
 			goto exit;
Signature · 1 line
2.43.5
author
#3 lore raw

PATCH 4/11 erofs-utils: lib: change argument order of erofs_io_pread()

In reply to Gao Xiang

To match pread(), swap the order of len and pos.

---
 include/erofs/io.h | 4 ++--
 lib/io.c           | 8 ++++----
 lib/super.c        | 2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)
include/erofs/io.h +2 -2
index 14d6e45f..01a7ff44 100644
@@ -26,7 +26,7 @@ extern "C"
 struct erofs_vfile;
 
 struct erofs_vfops {
-	ssize_t (*pread)(struct erofs_vfile *vf, void *buf, u64 offset, size_t len);
+	ssize_t (*pread)(struct erofs_vfile *vf, void *buf, size_t len, u64 offset);
 	ssize_t (*pwrite)(struct erofs_vfile *vf, const void *buf, u64 offset, size_t len);
 	ssize_t (*pwritev)(struct erofs_vfile *vf, const struct iovec *iov,
 			   int iovcnt, u64 pos);
@@ -61,7 +61,7 @@ ssize_t erofs_io_pwritev(struct erofs_vfile *vf, const struct iovec *iov,
 int erofs_io_fsync(struct erofs_vfile *vf);
 int erofs_io_fallocate(struct erofs_vfile *vf, u64 offset, size_t len, bool pad);
 int erofs_io_ftruncate(struct erofs_vfile *vf, u64 length);
-ssize_t erofs_io_pread(struct erofs_vfile *vf, void *buf, u64 offset, size_t len);
+ssize_t erofs_io_pread(struct erofs_vfile *vf, void *buf, size_t len, u64 offset);
 ssize_t erofs_io_read(struct erofs_vfile *vf, void *buf, size_t len);
 off_t erofs_io_lseek(struct erofs_vfile *vf, u64 offset, int whence);
 
lib/io.c +4 -4
index 983d9bf9..b91c93cf 100644
@@ -196,7 +196,7 @@ int erofs_io_ftruncate(struct erofs_vfile *vf, u64 length)
 	return ftruncate(vf->fd, length);
 }
 
-ssize_t erofs_io_pread(struct erofs_vfile *vf, void *buf, u64 pos, size_t len)
+ssize_t erofs_io_pread(struct erofs_vfile *vf, void *buf, size_t len, u64 pos)
 {
 	ssize_t ret, read = 0;
 
@@ -204,7 +204,7 @@ ssize_t erofs_io_pread(struct erofs_vfile *vf, void *buf, u64 pos, size_t len)
 		return 0;
 
 	if (vf->ops)
-		return vf->ops->pread(vf, buf, pos, len);
+		return vf->ops->pread(vf, buf, len, pos);
 
 	pos += vf->offset;
 	do {
@@ -403,9 +403,9 @@ ssize_t erofs_dev_read(struct erofs_sb_info *sbi, int device_id,
 		}
 		read = erofs_io_pread(&((struct erofs_vfile) {
 				.fd = sbi->blobfd[device_id - 1],
-			}), buf, offset, len);
+			}), buf, len, offset);
 	} else {
-		read = erofs_io_pread(&sbi->bdev, buf, offset, len);
+		read = erofs_io_pread(&sbi->bdev, buf, len, offset);
 	}
 
 	if (read < 0)
lib/super.c +1 -1
index e5364d27..8c0abafd 100644
@@ -78,7 +78,7 @@ int erofs_read_superblock(struct erofs_sb_info *sbi)
 	struct erofs_super_block *dsb;
 	int read, ret;
 
-	read = erofs_io_pread(&sbi->bdev, data, 0, EROFS_MAX_BLOCK_SIZE);
+	read = erofs_io_pread(&sbi->bdev, data, EROFS_MAX_BLOCK_SIZE, 0);
 	if (read < EROFS_SUPER_END) {
 		ret = read < 0 ? read : -EIO;
 		erofs_err("cannot read erofs superblock: %d", ret);
Signature · 1 line
2.43.5
author
#4 lore raw

PATCH 5/11 erofs-utils: lib: introduce meta buffer operations

In reply to Gao Xiang

Source kernel commit: fdf80a4793021c2f27953b3075f401a497519ba4 Source kernel commit: 469ad583c1293f5d9f45183050b3beeb4a8c3475 Source kernel commit: e09815446d6944fc5590a6e5f15dd51697202441

---
 include/erofs/internal.h | 14 ++++++++++++++
 lib/data.c               | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
include/erofs/internal.h +14 -0
index 1431c16d..b61fe716 100644
@@ -46,6 +46,14 @@ typedef u64 erofs_blk_t;
 /* global sbi */
 extern struct erofs_sb_info g_sbi;
 
+struct erofs_buf {
+	struct erofs_sb_info *sbi;
+	struct erofs_vfile *vf;
+	erofs_blk_t blocknr;
+	u8 base[EROFS_MAX_BLOCK_SIZE];
+};
+#define __EROFS_BUF_INITIALIZER ((struct erofs_buf){.blocknr = ~0ULL})
+
 #define erofs_blksiz(sbi)	(1u << (sbi)->blkszbits)
 #define erofs_blknr(sbi, pos)	((pos) >> (sbi)->blkszbits)
 #define erofs_blkoff(sbi, pos)	((pos) & (erofs_blksiz(sbi) - 1))
@@ -427,6 +435,12 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi);
 int erofs_ilookup(const char *path, struct erofs_inode *vi);
 
 /* data.c */
+static inline void erofs_unmap_metabuf(struct erofs_buf *buf) {}
+static inline void erofs_put_metabuf(struct erofs_buf *buf) {}
+void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap);
+void erofs_init_metabuf(struct erofs_buf *buf, struct erofs_sb_info *sbi);
+void *erofs_read_metabuf(struct erofs_buf *buf, struct erofs_sb_info *sbi,
+			 erofs_off_t offset);
 int erofs_pread(struct erofs_inode *inode, char *buf,
 		erofs_off_t count, erofs_off_t offset);
 int erofs_map_blocks(struct erofs_inode *inode,
lib/data.c +36 -0
index e2d9b5eb..d2558254 100644
@@ -10,6 +10,42 @@
 #include "erofs/decompress.h"
 #include "erofs/fragments.h"
 
+void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap)
+{
+	struct erofs_sb_info *sbi = buf->sbi;
+	u32 blksiz = erofs_blksiz(sbi);
+	erofs_blk_t blknr;
+	int err;
+
+	if (!need_kmap)
+		return NULL;
+	blknr = erofs_blknr(sbi, offset);
+	if (blknr != buf->blocknr) {
+		buf->blocknr = ~0ULL;
+		err = erofs_io_pread(buf->vf, buf->base, blksiz,
+				     round_down(offset, blksiz));
+		if (err < 0)
+			return ERR_PTR(err);
+		if (err != blksiz)
+			return ERR_PTR(-EIO);
+		buf->blocknr = blknr;
+	}
+	return buf->base + erofs_blkoff(sbi, offset);
+}
+
+void erofs_init_metabuf(struct erofs_buf *buf, struct erofs_sb_info *sbi)
+{
+	buf->sbi = sbi;
+	buf->vf = &sbi->bdev;
+}
+
+void *erofs_read_metabuf(struct erofs_buf *buf, struct erofs_sb_info *sbi,
+			 erofs_off_t offset)
+{
+	erofs_init_metabuf(buf, sbi);
+	return erofs_bread(buf, offset, true);
+}
+
 int __erofs_map_blocks(struct erofs_inode *inode,
 		       struct erofs_map_blocks *map, int flags)
 {
Signature · 1 line
2.43.5
author
#5 lore raw

PATCH 6/11 erofs-utils: lib: use meta buffers for inode operations

In reply to Gao Xiang

Source kernel commit: c521e3ad6cc980df6f3bdd2616808ecb973af880

---
 lib/namei.c | 78 ++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 56 insertions(+), 22 deletions(-)
lib/namei.c +56 -22
index d6013e5c..c3ddd590 100644
@@ -24,39 +24,67 @@ static dev_t erofs_new_decode_dev(u32 dev)
 
 int erofs_read_inode_from_disk(struct erofs_inode *vi)
 {
-	int ret, ifmt;
-	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 blkaddr = erofs_blknr(sbi, erofs_iloc(vi));
+	unsigned int ofs = erofs_blkoff(sbi, erofs_iloc(vi));
+	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
 	erofs_blk_t addrmask = BIT_ULL(48) - 1;
 	struct erofs_inode_extended *die, copied;
 	struct erofs_inode_compact *dic;
+	unsigned int ifmt;
+	void *ptr;
+	int err = 0;
+
+	ptr = erofs_read_metabuf(&buf, sbi, erofs_pos(sbi, blkaddr));
+	if (IS_ERR(ptr)) {
+		err = PTR_ERR(ptr);
+		erofs_err("failed to get inode (nid: %llu) page, err %d",
+			  vi->nid, err);
+		goto err_out;
+	}
 
-	DBG_BUGON(!sbi);
-	ret = erofs_dev_read(sbi, 0, buf, inode_loc, sizeof(*dic));
-	if (ret < 0)
-		return -EIO;
-
-	dic = (struct erofs_inode_compact *)buf;
+	dic = ptr + ofs;
 	ifmt = le16_to_cpu(dic->i_format);
+	if (ifmt & ~EROFS_I_ALL) {
+		erofs_err("unsupported i_format %u of nid %llu",
+			  ifmt, vi->nid);
+		err = -EOPNOTSUPP;
+		goto err_out;
+	}
 
 	vi->datalayout = erofs_inode_datalayout(ifmt);
 	if (vi->datalayout >= EROFS_INODE_DATALAYOUT_MAX) {
 		erofs_err("unsupported datalayout %u of nid %llu",
 			  vi->datalayout, vi->nid | 0ULL);
-		return -EOPNOTSUPP;
+		err = -EOPNOTSUPP;
+		goto err_out;
 	}
+
 	switch (erofs_inode_version(ifmt)) {
 	case EROFS_INODE_LAYOUT_EXTENDED:
 		vi->inode_isize = sizeof(struct erofs_inode_extended);
-
-		ret = erofs_dev_read(sbi, 0, buf + sizeof(*dic),
-			       inode_loc + sizeof(*dic),
-			       sizeof(*die) - sizeof(*dic));
-		if (ret < 0)
-			return -EIO;
-
-		die = (struct erofs_inode_extended *)buf;
+		/* check if the extended inode acrosses block boundary */
+		if (ofs + vi->inode_isize <= erofs_blksiz(sbi)) {
+			ofs += vi->inode_isize;
+			die = (struct erofs_inode_extended *)dic;
+			copied.i_u = die->i_u;
+			copied.i_nb = die->i_nb;
+		} else {
+			const unsigned int gotten = erofs_blksiz(sbi) - ofs;
+
+			memcpy(&copied, dic, gotten);
+			ptr = erofs_read_metabuf(&buf, sbi,
+					erofs_pos(sbi, blkaddr + 1));
+			if (IS_ERR(ptr)) {
+				err = PTR_ERR(ptr);
+				erofs_err("failed to get inode payload block (nid: %llu), err %d",
+					  vi->nid, err);
+				goto err_out;
+			}
+			ofs = vi->inode_isize - gotten;
+			memcpy((u8 *)&copied + gotten, ptr, ofs);
+			die = &copied;
+		}
 		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);
@@ -72,6 +100,7 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 		break;
 	case EROFS_INODE_LAYOUT_COMPACT:
 		vi->inode_isize = sizeof(struct erofs_inode_compact);
+		ofs += vi->inode_isize;
 		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);
@@ -96,7 +125,8 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 	default:
 		erofs_err("unsupported on-disk inode version %u of nid %llu",
 			  erofs_inode_version(ifmt), vi->nid | 0ULL);
-		return -EOPNOTSUPP;
+		err = -EOPNOTSUPP;
+		goto err_out;
 	}
 
 	switch (vi->i_mode & S_IFMT) {
@@ -122,7 +152,8 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 	default:
 		erofs_err("bogus i_mode (%o) @ nid %llu", vi->i_mode,
 			  vi->nid | 0ULL);
-		return -EFSCORRUPTED;
+		err = -EFSCORRUPTED;
+		goto err_out;
 	}
 
 	vi->flags = 0;
@@ -132,12 +163,15 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 		if (vi->u.chunkformat & ~EROFS_CHUNK_FORMAT_ALL) {
 			erofs_err("unsupported chunk format %x of nid %llu",
 				  vi->u.chunkformat, vi->nid | 0ULL);
-			return -EOPNOTSUPP;
+			err = -EOPNOTSUPP;
+			goto err_out;
 		}
 		vi->u.chunkbits = sbi->blkszbits +
 			(vi->u.chunkformat & EROFS_CHUNK_FORMAT_BLKBITS_MASK);
 	}
-	return 0;
+err_out:
+	erofs_put_metabuf(&buf);
+	return err;
 }
 
 struct erofs_dirent *find_target_dirent(erofs_nid_t pnid,
Signature · 1 line
2.43.5
author
#6 lore raw

PATCH 7/11 erofs-utils: lib: use meta buffers for zmap operations

In reply to Gao Xiang

Source kernel commit: 09c543798c3cde19aae575a0f76d5fc7c130ff18

---
 dump/main.c              |  2 +-
 fsck/main.c              |  2 +-
 include/erofs/internal.h |  3 +-
 lib/data.c               |  4 +-
 lib/fragments.c          |  4 +-
 lib/rebuild.c            |  2 +-
 lib/zmap.c               | 98 ++++++++++++++--------------------------
 7 files changed, 42 insertions(+), 73 deletions(-)
dump/main.c +1 -1
index 4e68b0ad..632075a2 100644
@@ -375,7 +375,7 @@ static void erofsdump_show_fileinfo(bool show_extent)
 	char timebuf[128] = {0};
 	unsigned int extent_count = 0;
 	struct erofs_map_blocks map = {
-		.index = UINT_MAX,
+		.buf = __EROFS_BUF_INITIALIZER,
 		.m_la = 0,
 	};
 
fsck/main.c +1 -1
index 04370f46..96096a91 100644
@@ -499,7 +499,7 @@ out:
 static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)
 {
 	struct erofs_map_blocks map = {
-		.index = UINT_MAX,
+		.buf = __EROFS_BUF_INITIALIZER,
 	};
 	bool needdecode = fsckcfg.check_decomp && !erofs_is_packed_inode(inode);
 	int ret = 0;
include/erofs/internal.h +1 -2
index b61fe716..0a49394d 100644
@@ -390,7 +390,7 @@ enum {
 #define EROFS_MAP_FRAGMENT	(EROFS_MAP_MAPPED | __EROFS_MAP_FRAGMENT)
 
 struct erofs_map_blocks {
-	char mpage[EROFS_MAX_BLOCK_SIZE];
+	struct erofs_buf buf;
 
 	erofs_off_t m_pa, m_la;
 	u64 m_plen, m_llen;
@@ -398,7 +398,6 @@ struct erofs_map_blocks {
 	unsigned short m_deviceid;
 	char m_algorithmformat;
 	unsigned int m_flags;
-	erofs_blk_t index;
 };
 
 /*
lib/data.c +2 -2
index d2558254..83cc5d5d 100644
@@ -197,7 +197,7 @@ static int erofs_read_raw_data(struct erofs_inode *inode, char *buffer,
 			       erofs_off_t size, erofs_off_t offset)
 {
 	struct erofs_map_blocks map = {
-		.index = UINT_MAX,
+		.buf = __EROFS_BUF_INITIALIZER,
 	};
 	int ret;
 	erofs_off_t ptr = offset;
@@ -300,7 +300,7 @@ static int z_erofs_read_data(struct erofs_inode *inode, char *buffer,
 {
 	erofs_off_t end, length, skip;
 	struct erofs_map_blocks map = {
-		.index = UINT_MAX,
+		.buf = __EROFS_BUF_INITIALIZER,
 	};
 	bool trimmed;
 	unsigned int bufsize = 0;
lib/fragments.c +1 -3
index 3278f473..887c2530 100644
@@ -569,9 +569,7 @@ int erofs_packedfile_read(struct erofs_sb_info *sbi,
 	struct erofs_inode pi = {
 		.sbi = sbi,
 	};
-	struct erofs_map_blocks map = {
-		.index = UINT_MAX,
-	};
+	struct erofs_map_blocks map = { .buf = __EROFS_BUF_INITIALIZER };
 	unsigned int bsz = erofs_blksiz(sbi);
 	erofs_off_t end = pos + len;
 	char *buffer = NULL;
lib/rebuild.c +1 -1
index c580f81f..33857fd6 100644
@@ -194,7 +194,7 @@ static int erofs_rebuild_write_blob_index(struct erofs_sb_info *dst_sb,
 	for (i = 0; i < count; i++) {
 		struct erofs_blobchunk *chunk;
 		struct erofs_map_blocks map = {
-			.index = UINT_MAX,
+			.buf = __EROFS_BUF_INITIALIZER,
 		};
 
 		map.m_la = i << chunkbits;
lib/zmap.c +35 -63

203 lines not on this page · raw message

Signature · 1 line
2.43.5
author
#7 lore raw

PATCH 8/11 erofs-utils: lib: use meta buffers for xattr operations

In reply to Gao Xiang

Source kernel commit: bb88e8da00253bea0e7f0f4cdfd7910572d7799f

---
 lib/xattr.c | 92 +++++++++++++++++++++++++----------------------------
 1 file changed, 44 insertions(+), 48 deletions(-)
lib/xattr.c +44 -48
index 6711dcc1..a2ef8d20 100644
@@ -1033,13 +1033,12 @@ char *erofs_export_xattr_ibody(struct erofs_inode *inode)
 }
 
 struct xattr_iter {
-	char page[EROFS_MAX_BLOCK_SIZE];
-
+	struct erofs_sb_info *sbi;
+	struct erofs_buf buf;
 	void *kaddr;
 
 	erofs_blk_t blkaddr;
 	unsigned int ofs;
-	struct erofs_sb_info *sbi;
 };
 
 static int init_inode_xattrs(struct erofs_inode *vi)
@@ -1075,20 +1074,23 @@ static int init_inode_xattrs(struct erofs_inode *vi)
 		return -ENOATTR;
 	}
 
+	it.buf = __EROFS_BUF_INITIALIZER;
 	it.blkaddr = erofs_blknr(sbi, erofs_iloc(vi) + vi->inode_isize);
 	it.ofs = erofs_blkoff(sbi, erofs_iloc(vi) + vi->inode_isize);
 
-	ret = erofs_blk_read(sbi, 0, it.page, it.blkaddr, 1);
-	if (ret < 0)
-		return -EIO;
+	/* read in shared xattr array (non-atomic, see kmalloc below) */
+	it.kaddr = erofs_read_metabuf(&it.buf, sbi, erofs_pos(sbi, it.blkaddr));
+	if (IS_ERR(it.kaddr))
+		return PTR_ERR(it.kaddr);
 
-	it.kaddr = it.page;
 	ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
 
 	vi->xattr_shared_count = ih->h_shared_count;
 	vi->xattr_shared_xattrs = malloc(vi->xattr_shared_count * sizeof(uint));
-	if (!vi->xattr_shared_xattrs)
+	if (!vi->xattr_shared_xattrs) {
+		erofs_put_metabuf(&it.buf);
 		return -ENOMEM;
+	}
 
 	/* let's skip ibody header */
 	it.ofs += sizeof(struct erofs_xattr_ibody_header);
@@ -1098,20 +1100,20 @@ static int init_inode_xattrs(struct erofs_inode *vi)
 			/* cannot be unaligned */
 			DBG_BUGON(it.ofs != erofs_blksiz(sbi));
 
-			ret = erofs_blk_read(sbi, 0, it.page, ++it.blkaddr, 1);
-			if (ret < 0) {
+			it.kaddr = erofs_read_metabuf(&it.buf, sbi,
+					erofs_pos(sbi, ++it.blkaddr));
+			if (IS_ERR(it.kaddr)) {
 				free(vi->xattr_shared_xattrs);
 				vi->xattr_shared_xattrs = NULL;
-				return -EIO;
+				return PTR_ERR(it.kaddr);
 			}
-
-			it.kaddr = it.page;
 			it.ofs = 0;
 		}
 		vi->xattr_shared_xattrs[i] =
 			le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs));
 		it.ofs += sizeof(__le32);
 	}
+	erofs_put_metabuf(&it.buf);
 	erofs_atomic_set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
 	return ret;
 }
@@ -1135,28 +1137,24 @@ struct xattr_iter_handlers {
 static inline int xattr_iter_fixup(struct xattr_iter *it)
 {
 	struct erofs_sb_info *sbi = it->sbi;
-	int ret;
 
 	if (it->ofs < erofs_blksiz(sbi))
 		return 0;
 
 	it->blkaddr += erofs_blknr(sbi, it->ofs);
-
-	ret = erofs_blk_read(sbi, 0, it->page, it->blkaddr, 1);
-	if (ret < 0)
-		return -EIO;
-
-	it->kaddr = it->page;
+	it->kaddr = erofs_read_metabuf(&it->buf, sbi,
+				       erofs_pos(sbi, it->blkaddr));
+	if (IS_ERR(it->kaddr))
+		return PTR_ERR(it->kaddr);
 	it->ofs = erofs_blkoff(sbi, it->ofs);
 	return 0;
 }
 
-static int inline_xattr_iter_pre(struct xattr_iter *it,
+static int inline_xattr_iter_begin(struct xattr_iter *it,
 				   struct erofs_inode *vi)
 {
 	struct erofs_sb_info *sbi = vi->sbi;
 	unsigned int xattr_header_sz, inline_xattr_ofs;
-	int ret;
 
 	xattr_header_sz = inlinexattr_header_size(vi);
 	if (xattr_header_sz >= vi->xattr_isize) {
@@ -1169,11 +1167,10 @@ static int inline_xattr_iter_pre(struct xattr_iter *it,
 	it->blkaddr = erofs_blknr(sbi, erofs_iloc(vi) + inline_xattr_ofs);
 	it->ofs = erofs_blkoff(sbi, erofs_iloc(vi) + inline_xattr_ofs);
 
-	ret = erofs_blk_read(sbi, 0, it->page, it->blkaddr, 1);
-	if (ret < 0)
-		return -EIO;
-
-	it->kaddr = it->page;
+	it->kaddr = erofs_read_metabuf(&it->buf, sbi,
+				       erofs_pos(sbi, it->blkaddr));
+	if (IS_ERR(it->kaddr))
+		return PTR_ERR(it->kaddr);
 	return vi->xattr_isize - xattr_header_sz;
 }
 
@@ -1370,7 +1367,7 @@ static int inline_getxattr(struct erofs_inode *vi, struct getxattr_iter *it)
 	int ret;
 	unsigned int remaining;
 
-	ret = inline_xattr_iter_pre(&it->it, vi);
+	ret = inline_xattr_iter_begin(&it->it, vi);
 	if (ret < 0)
 		return ret;
 
@@ -1386,6 +1383,7 @@ static int inline_getxattr(struct erofs_inode *vi, struct getxattr_iter *it)
 
 static int shared_getxattr(struct erofs_inode *vi, struct getxattr_iter *it)
 {
+	struct erofs_sb_info *sbi = vi->sbi;
 	unsigned int i;
 	int ret = -ENOATTR;
 
@@ -1394,15 +1392,11 @@ static int shared_getxattr(struct erofs_inode *vi, struct getxattr_iter *it)
 			xattrblock_addr(vi, vi->xattr_shared_xattrs[i]);
 
 		it->it.ofs = xattrblock_offset(vi, vi->xattr_shared_xattrs[i]);
-
-		if (!i || blkaddr != it->it.blkaddr) {
-			ret = erofs_blk_read(vi->sbi, 0, it->it.page, blkaddr, 1);
-			if (ret < 0)
-				return -EIO;
-
-			it->it.kaddr = it->it.page;
-			it->it.blkaddr = blkaddr;
-		}
+		it->it.kaddr = erofs_read_metabuf(&it->it.buf, sbi,
+						  erofs_pos(sbi, blkaddr));
+		if (IS_ERR(it->it.kaddr))
+			return PTR_ERR(it->it.kaddr);
+		it->it.blkaddr = blkaddr;
 
 		ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL);
 		if (ret != -ENOATTR)
@@ -1435,12 +1429,14 @@ int erofs_getxattr(struct erofs_inode *vi, const char *name, char *buffer,
 	if (it.len > EROFS_NAME_LEN)
 		return -ERANGE;
 
+	it.it.buf = __EROFS_BUF_INITIALIZER;
 	it.buffer = buffer;
 	it.buffer_size = buffer_size;
 
 	ret = inline_getxattr(vi, &it);
 	if (ret == -ENOATTR)
 		ret = shared_getxattr(vi, &it);
+	erofs_put_metabuf(&it.it.buf);
 	return ret;
 }
 
@@ -1526,7 +1522,7 @@ static int inline_listxattr(struct erofs_inode *vi, struct listxattr_iter *it)
 	int ret;
 	unsigned int remaining;
 
-	ret = inline_xattr_iter_pre(&it->it, vi);
+	ret = inline_xattr_iter_begin(&it->it, vi);
 	if (ret < 0)
 		return ret;
 
@@ -1542,6 +1538,7 @@ static int inline_listxattr(struct erofs_inode *vi, struct listxattr_iter *it)
 
 static int shared_listxattr(struct erofs_inode *vi, struct listxattr_iter *it)
 {
+	struct erofs_sb_info *sbi = vi->sbi;
 	unsigned int i;
 	int ret = 0;
 
@@ -1550,14 +1547,11 @@ static int shared_listxattr(struct erofs_inode *vi, struct listxattr_iter *it)
 			xattrblock_addr(vi, vi->xattr_shared_xattrs[i]);
 
 		it->it.ofs = xattrblock_offset(vi, vi->xattr_shared_xattrs[i]);
-		if (!i || blkaddr != it->it.blkaddr) {
-			ret = erofs_blk_read(vi->sbi, 0, it->it.page, blkaddr, 1);
-			if (ret < 0)
-				return -EIO;
-
-			it->it.kaddr = it->it.page;
-			it->it.blkaddr = blkaddr;
-		}
+		it->it.kaddr = erofs_read_metabuf(&it->it.buf, sbi,
+						  erofs_pos(sbi, blkaddr));
+		if (IS_ERR(it->it.kaddr))
+			return PTR_ERR(it->it.kaddr);
+		it->it.blkaddr = blkaddr;
 
 		ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL);
 		if (ret)
@@ -1579,14 +1573,16 @@ int erofs_listxattr(struct erofs_inode *vi, char *buffer, size_t buffer_size)
 		return ret;
 
 	it.it.sbi = vi->sbi;
+	it.it.buf = __EROFS_BUF_INITIALIZER;
 	it.buffer = buffer;
 	it.buffer_size = buffer_size;
 	it.buffer_ofs = 0;
 
 	ret = inline_listxattr(vi, &it);
 	if (ret < 0 && ret != -ENOATTR)
-		return ret;
-	return shared_listxattr(vi, &it);
+		ret = shared_listxattr(vi, &it);
+	erofs_put_metabuf(&it.it.buf);
+	return ret;
 }
 
 int erofs_xattr_insert_name_prefix(const char *prefix)
Signature · 1 line
2.43.5
author
#8 lore raw

PATCH 9/11 erofs-utils: formalize erofs_pread()

In reply to Gao Xiang

Now erofs_pread() is simply a wrapper around erofs_io_pread(), and erofs_iopen() is used to obtain a virtual file for erofs_pread().

---
 dump/main.c              | 10 +++++++--
 fsck/main.c              |  7 +++++-
 fuse/main.c              | 23 ++++++++++++++-----
 include/erofs/internal.h |  3 +--
 include/erofs/io.h       | 11 +++++++++
 lib/data.c               | 48 ++++++++++++++++++++++------------------
 lib/dir.c                | 12 ++++++----
 lib/fragments.c          | 20 +++++++++++++----
 lib/inode.c              |  7 +++++-
 lib/namei.c              |  7 +++++-
 lib/rebuild.c            | 10 +++++++--
 lib/super.c              |  3 ++-
 12 files changed, 116 insertions(+), 45 deletions(-)
dump/main.c +8 -2
index 632075a2..f0dab02e 100644
@@ -679,6 +679,7 @@ static void erofsdump_show_file_content(void)
 	erofs_off_t pending_size;
 	erofs_off_t read_offset;
 	erofs_off_t read_size;
+	struct erofs_vfile vf;
 
 	if (dumpcfg.inode_path) {
 		err = erofs_ilookup(dumpcfg.inode_path, &inode);
@@ -694,6 +695,10 @@ static void erofsdump_show_file_content(void)
 		}
 	}
 
+	err = erofs_iopen(&vf, &inode);
+	if (err)
+		return;
+
 	buffer_size = erofs_blksiz(inode.sbi);
 	buffer_ptr = malloc(buffer_size);
 	if (!buffer_ptr) {
@@ -704,8 +709,9 @@ static void erofsdump_show_file_content(void)
 	pending_size = inode.i_size;
 	read_offset = 0;
 	while (pending_size > 0) {
-		read_size = pending_size > buffer_size? buffer_size: pending_size;
-		err = erofs_pread(&inode, buffer_ptr, read_size, read_offset);
+		read_size = pending_size > buffer_size ?
+			buffer_size : pending_size;
+		err = erofs_pread(&vf, buffer_ptr, read_size, read_offset);
 		if (err) {
 			erofs_err("read file failed @ nid %llu", inode.nid | 0ULL);
 			goto out;
fsck/main.c +6 -1
index 96096a91..44719b95 100644
@@ -775,6 +775,7 @@ again:
 
 static inline int erofs_extract_symlink(struct erofs_inode *inode)
 {
+	struct erofs_vfile vf;
 	bool tryagain = true;
 	int ret;
 	char *buf = NULL;
@@ -792,7 +793,11 @@ static inline int erofs_extract_symlink(struct erofs_inode *inode)
 		goto out;
 	}
 
-	ret = erofs_pread(inode, buf, inode->i_size, 0);
+	ret = erofs_iopen(&vf, inode);
+	if (ret)
+		goto out;
+
+	ret = erofs_pread(&vf, buf, inode->i_size, 0);
 	if (ret) {
 		erofs_err("I/O error occurred when reading symlink @ nid %llu: %d",
 			  inode->nid | 0ULL, ret);
fuse/main.c +17 -6
index db4f3236..001d1fde 100644
@@ -366,11 +366,15 @@ out:
 static void erofsfuse_read(fuse_req_t req, fuse_ino_t ino, size_t size,
 			   off_t off, struct fuse_file_info *fi)
 {
-	int ret;
-	char *buf = NULL;
 	struct erofs_inode *vi = (struct erofs_inode *)fi->fh;
+	struct erofs_vfile vf;
+	char *buf = NULL;
+	int ret;
 
 	erofs_dbg("read(%llu): size = %zu, off = %lu", ino | 0ULL, size, off);
+	ret = erofs_iopen(&vf, vi);
+	if (ret)
+		return ret;
 
 	buf = malloc(size);
 	if (!buf) {
@@ -378,7 +382,7 @@ static void erofsfuse_read(fuse_req_t req, fuse_ino_t ino, size_t size,
 		return;
 	}
 
-	ret = erofs_pread(vi, buf, size, off);
+	ret = erofs_pread(&vf, buf, size, off);
 	if (ret) {
 		fuse_reply_err(req, -ret);
 		goto out;
@@ -398,9 +402,10 @@ out:
 
 static void erofsfuse_readlink(fuse_req_t req, fuse_ino_t ino)
 {
-	int ret;
-	char *buf = NULL;
 	struct erofs_inode vi = { .sbi = &g_sbi, .nid = erofsfuse_to_nid(ino) };
+	struct erofs_vfile vf;
+	char *buf = NULL;
+	int ret;
 
 	ret = erofs_read_inode_from_disk(&vi);
 	if (ret < 0) {
@@ -408,13 +413,19 @@ static void erofsfuse_readlink(fuse_req_t req, fuse_ino_t ino)
 		return;
 	}
 
+	ret = erofs_iopen(&vf, &vi);
+	if (ret) {
+		fuse_reply_err(req, -ret);
+		return;
+	}
+
 	buf = malloc(vi.i_size + 1);
 	if (!buf) {
 		fuse_reply_err(req, ENOMEM);
 		return;
 	}
 
-	ret = erofs_pread(&vi, buf, vi.i_size, 0);
+	ret = erofs_pread(&vf, buf, vi.i_size, 0);
 	if (ret < 0) {
 		fuse_reply_err(req, -ret);
 		goto out;
include/erofs/internal.h +1 -2
index 0a49394d..3439a183 100644
@@ -440,8 +440,7 @@ void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap);
 void erofs_init_metabuf(struct erofs_buf *buf, struct erofs_sb_info *sbi);
 void *erofs_read_metabuf(struct erofs_buf *buf, struct erofs_sb_info *sbi,
 			 erofs_off_t offset);
-int erofs_pread(struct erofs_inode *inode, char *buf,
-		erofs_off_t count, erofs_off_t offset);
+int erofs_iopen(struct erofs_vfile *vf, struct erofs_inode *inode);
 int erofs_map_blocks(struct erofs_inode *inode,
 		struct erofs_map_blocks *map, int flags);
 int erofs_map_dev(struct erofs_sb_info *sbi, struct erofs_map_dev *map);
include/erofs/io.h +11 -0
index 01a7ff44..cc7a3cd2 100644
@@ -70,6 +70,17 @@ ssize_t erofs_copy_file_range(int fd_in, u64 *off_in, int fd_out, u64 *off_out,
 int erofs_io_xcopy(struct erofs_vfile *vout, off_t pos,
 		   struct erofs_vfile *vin, unsigned int len, bool noseek);
 
+static inline int erofs_pread(struct erofs_vfile *vf, void *buf,
+			      size_t len, u64 offset)
+{
+	ssize_t read;
+
+	read = erofs_io_pread(vf, buf, len, offset);
+	if (read < 0)
+		return read;
+	return read != len ? -EIO : 0;
+}
+
 #ifdef __cplusplus
 }
 #endif
lib/data.c +27 -21

81 lines not on this page · raw message

lib/dir.c +8 -4
index 9c6849d8..98edb8e1 100644
@@ -142,9 +142,10 @@ int erofs_iterate_dir(struct erofs_dir_context *ctx, bool fsck)
 {
 	struct erofs_inode *dir = ctx->dir;
 	struct erofs_sb_info *sbi = dir->sbi;
-	int err = 0;
+	struct erofs_vfile vf;
 	erofs_off_t pos;
 	char buf[EROFS_MAX_BLOCK_SIZE];
+	int err = 0;
 
 	if (!S_ISDIR(dir->i_mode))
 		return -ENOTDIR;
@@ -152,15 +153,18 @@ int erofs_iterate_dir(struct erofs_dir_context *ctx, bool fsck)
 	ctx->flags &= ~EROFS_READDIR_ALL_SPECIAL_FOUND;
 	if (dir->dot_omitted)
 		ctx->flags |= EROFS_READDIR_DOT_FOUND;
-	pos = 0;
-	while (pos < dir->i_size) {
+	err = erofs_iopen(&vf, dir);
+	if (err)
+		return err;
+
+	for (pos = 0; pos < dir->i_size; ) {
 		erofs_blk_t lblk = erofs_blknr(sbi, pos);
 		erofs_off_t maxsize = min_t(erofs_off_t,
 					dir->i_size - pos, erofs_blksiz(sbi));
 		const struct erofs_dirent *de = (const void *)buf;
 		unsigned int nameoff;
 
-		err = erofs_pread(dir, buf, maxsize, pos);
+		err = erofs_pread(&vf, buf, maxsize, pos);
 		if (err) {
 			erofs_err("I/O error when reading dirents @ nid %llu, lblk %llu: %s",
 				  dir->nid | 0ULL, lblk | 0ULL,
lib/fragments.c +16 -4
index 887c2530..0221a538 100644
@@ -510,6 +510,7 @@ static void *erofs_packedfile_preload(struct erofs_inode *pi,
 	struct erofs_sb_info *sbi = pi->sbi;
 	struct erofs_packed_inode *epi = sbi->packedinode;
 	unsigned int bsz = erofs_blksiz(sbi);
+	struct erofs_vfile vf;
 	char *buffer;
 	erofs_off_t pos, end;
 	ssize_t err;
@@ -529,13 +530,17 @@ static void *erofs_packedfile_preload(struct erofs_inode *pi,
 	else
 		DBG_BUGON(map->m_la > pos);
 
+	err = erofs_iopen(&vf, pi);
+	if (err)
+		return ERR_PTR(err);
+
 	map->m_llen = end - map->m_la;
 	DBG_BUGON(!map->m_llen);
 	buffer = malloc(map->m_llen);
 	if (!buffer)
 		return ERR_PTR(-ENOMEM);
 
-	err = erofs_pread(pi, buffer, map->m_llen, map->m_la);
+	err = erofs_pread(&vf, buffer, map->m_llen, map->m_la);
 	if (err)
 		goto err_out;
 
@@ -572,13 +577,17 @@ int erofs_packedfile_read(struct erofs_sb_info *sbi,
 	struct erofs_map_blocks map = { .buf = __EROFS_BUF_INITIALIZER };
 	unsigned int bsz = erofs_blksiz(sbi);
 	erofs_off_t end = pos + len;
+	struct erofs_vfile vf;
 	char *buffer = NULL;
 	int err;
 
 	if (!epi) {
 		err = erofs_load_packedinode_from_disk(&pi);
-		if (!err)
-			err = erofs_pread(&pi, buf, len, pos);
+		if (!err) {
+			err = erofs_iopen(&vf, &pi);
+			if (!err)
+				err = erofs_pread(&vf, buf, len, pos);
+		}
 		return err;
 	}
 
@@ -632,8 +641,11 @@ int erofs_packedfile_read(struct erofs_sb_info *sbi,
 			} else {
 fallback:
 				err = erofs_load_packedinode_from_disk(&pi);
+				if (err)
+					break;
+				err = erofs_iopen(&vf, &pi);
 				if (!err)
-					err = erofs_pread(&pi, buf, len, pos);
+					err = erofs_pread(&vf, buf, len, pos);
 				if (err)
 					break;
 			}
lib/inode.c +6 -1
index f7c6b87f..5f50c09f 100644
@@ -371,6 +371,7 @@ static int erofs_rebuild_inode_fix_pnid(struct erofs_inode *parent,
 	};
 	unsigned int bsz = erofs_blksiz(dir.sbi);
 	unsigned int err, isz;
+	struct erofs_vfile vf;
 	erofs_off_t boff, off;
 	erofs_nid_t pnid;
 	bool fixed = false;
@@ -386,6 +387,10 @@ static int erofs_rebuild_inode_fix_pnid(struct erofs_inode *parent,
 	    dir.datalayout != EROFS_INODE_FLAT_PLAIN)
 		return -EOPNOTSUPP;
 
+	err = erofs_iopen(&vf, &dir);
+	if (err)
+		return err;
+
 	pnid = erofs_lookupnid(parent);
 	isz = dir.inode_isize + dir.xattr_isize;
 	boff = erofs_pos(dir.sbi, dir.u.i_blkaddr);
@@ -395,7 +400,7 @@ static int erofs_rebuild_inode_fix_pnid(struct erofs_inode *parent,
 		unsigned int nameoff, count, de_nameoff;
 
 		count = min_t(erofs_off_t, bsz, dir.i_size - off);
-		err = erofs_pread(&dir, buf, count, off);
+		err = erofs_pread(&vf, buf, count, off);
 		if (err)
 			return err;
 
lib/namei.c +6 -1
index c3ddd590..8de0a908 100644
@@ -224,12 +224,17 @@ int erofs_namei(struct nameidata *nd, const char *name, unsigned int len)
 	char buf[EROFS_MAX_BLOCK_SIZE];
 	struct erofs_sb_info *sbi = nd->sbi;
 	struct erofs_inode vi = { .sbi = sbi, .nid = nid };
+	struct erofs_vfile vf;
 	erofs_off_t offset;
 
 	ret = erofs_read_inode_from_disk(&vi);
 	if (ret)
 		return ret;
 
+	ret = erofs_iopen(&vf, &vi);
+	if (ret)
+		return ret;
+
 	offset = 0;
 	while (offset < vi.i_size) {
 		erofs_off_t maxsize = min_t(erofs_off_t,
@@ -237,7 +242,7 @@ int erofs_namei(struct nameidata *nd, const char *name, unsigned int len)
 		struct erofs_dirent *de = (void *)buf;
 		unsigned int nameoff;
 
-		ret = erofs_pread(&vi, buf, maxsize, offset);
+		ret = erofs_pread(&vf, buf, maxsize, offset);
 		if (ret)
 			return ret;
 
lib/rebuild.c +8 -2
index 33857fd6..7ad0658a 100644
@@ -243,13 +243,19 @@ static int erofs_rebuild_update_inode(struct erofs_sb_info *dst_sb,
 	case S_IFDIR:
 		err = erofs_init_empty_dir(inode);
 		break;
-	case S_IFLNK:
+	case S_IFLNK: {
+		struct erofs_vfile vf;
+
 		inode->i_link = malloc(inode->i_size + 1);
 		if (!inode->i_link)
 			return -ENOMEM;
-		err = erofs_pread(inode, inode->i_link, inode->i_size, 0);
+		err = erofs_iopen(&vf, inode);
+		if (err)
+			return err;
+		err = erofs_pread(&vf, inode->i_link, inode->i_size, 0);
 		erofs_dbg("\tsymlink: %s -> %s", inode->i_srcpath, inode->i_link);
 		break;
+	}
 	case S_IFREG:
 		if (!inode->i_size) {
 			inode->u.i_blkaddr = EROFS_NULL_ADDR;
lib/super.c +2 -1
index 8c0abafd..1d13e6e3 100644
@@ -81,7 +81,8 @@ int erofs_read_superblock(struct erofs_sb_info *sbi)
 	read = erofs_io_pread(&sbi->bdev, data, EROFS_MAX_BLOCK_SIZE, 0);
 	if (read < EROFS_SUPER_END) {
 		ret = read < 0 ? read : -EIO;
-		erofs_err("cannot read erofs superblock: %d", ret);
+		erofs_err("cannot read erofs superblock: %s",
+			  erofs_strerror(ret));
 		return ret;
 	}
 	dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
Signature · 1 line
2.43.5
author
#9 lore raw

PATCH 10/11 erofs-utils: fsck, dump: support metadata compression

In reply to Gao Xiang

Source kernel commit: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Source kernel commit: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

---
 dump/main.c              | 10 +++++++++-
 fsck/main.c              | 25 ++++++++++++++---------
 include/erofs/internal.h | 19 ++++++++++++------
 include/erofs_fs.h       | 12 ++++++++---
 lib/data.c               | 43 ++++++++++++++++++++++++++++++++++------
 lib/namei.c              |  5 +++--
 lib/super.c              | 29 +++++++++++++++++++++------
 lib/xattr.c              | 12 +++++------
 lib/zmap.c               | 15 ++++++++------
 9 files changed, 125 insertions(+), 45 deletions(-)
dump/main.c +9 -1
index f0dab02e..38cc8a28 100644
@@ -105,6 +105,7 @@ static struct erofsdump_feature feature_lists[] = {
 	{ false, EROFS_FEATURE_INCOMPAT_DEDUPE, "dedupe" },
 	{ false, EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES, "xattr_prefixes" },
 	{ false, EROFS_FEATURE_INCOMPAT_48BIT, "48bit" },
+	{ false, EROFS_FEATURE_INCOMPAT_METABOX, "metabox" },
 };
 
 static int erofsdump_readdir(struct erofs_dir_context *ctx);
@@ -163,7 +164,11 @@ static int erofsdump_parse_options_cfg(int argc, char **argv)
 			exit(0);
 		case 2:
 			dumpcfg.show_inode = true;
-			dumpcfg.nid = (erofs_nid_t)atoll(optarg);
+			dumpcfg.nid = strtoull(optarg, &endptr, 0);
+			if (*endptr != '\0') {
+				erofs_err("invalid NID %s", optarg);
+				return -EINVAL;
+			}
 			++dumpcfg.totalshow;
 			break;
 		case 'h':
@@ -643,6 +648,9 @@ static void erofsdump_show_superblock(void)
 	if (erofs_sb_has_fragments(&g_sbi) && g_sbi.packed_nid > 0)
 		fprintf(stdout, "Filesystem packed nid:                        %llu\n",
 			g_sbi.packed_nid | 0ULL);
+	if (erofs_sb_has_metabox(&g_sbi))
+		fprintf(stdout, "Filesystem metabox nid:                       %llu\n",
+			g_sbi.metabox_nid | 0ULL);
 	if (erofs_sb_has_compr_cfgs(&g_sbi)) {
 		fprintf(stdout, "Filesystem compr_algs:                        ");
 		erofsdump_print_supported_compressors(stdout,
fsck/main.c +16 -9
index 44719b95..d4f0fea7 100644
@@ -333,12 +333,14 @@ static int erofs_verify_xattr(struct erofs_inode *inode)
 	struct erofs_sb_info *sbi = inode->sbi;
 	unsigned int xattr_hdr_size = sizeof(struct erofs_xattr_ibody_header);
 	unsigned int xattr_entry_size = sizeof(struct erofs_xattr_entry);
-	erofs_off_t addr;
+	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
 	unsigned int ofs, xattr_shared_count;
 	struct erofs_xattr_ibody_header *ih;
 	struct erofs_xattr_entry *entry;
-	int i, remaining = inode->xattr_isize, ret = 0;
-	char buf[EROFS_MAX_BLOCK_SIZE];
+	int remaining = inode->xattr_isize, ret = 0;
+	erofs_off_t addr;
+	char *ptr;
+	int i;
 
 	if (inode->xattr_isize == xattr_hdr_size) {
 		erofs_err("xattr_isize %d of nid %llu is not supported yet",
@@ -355,13 +357,15 @@ static int erofs_verify_xattr(struct erofs_inode *inode)
 	}
 
 	addr = erofs_iloc(inode) + inode->inode_isize;
-	ret = erofs_dev_read(sbi, 0, buf, addr, xattr_hdr_size);
-	if (ret < 0) {
+	ptr = erofs_read_metabuf(&buf, sbi, addr,
+				 erofs_inode_in_metabox(inode));
+	if (IS_ERR(ptr)) {
+		ret = PTR_ERR(ptr);
 		erofs_err("failed to read xattr header @ nid %llu: %d",
 			  inode->nid | 0ULL, ret);
 		goto out;
 	}
-	ih = (struct erofs_xattr_ibody_header *)buf;
+	ih = (struct erofs_xattr_ibody_header *)ptr;
 	xattr_shared_count = ih->h_shared_count;
 
 	ofs = erofs_blkoff(sbi, addr) + xattr_hdr_size;
@@ -385,14 +389,16 @@ static int erofs_verify_xattr(struct erofs_inode *inode)
 	while (remaining > 0) {
 		unsigned int entry_sz;
 
-		ret = erofs_dev_read(sbi, 0, buf, addr, xattr_entry_size);
-		if (ret) {
+		ptr = erofs_read_metabuf(&buf, sbi, addr,
+				 erofs_inode_in_metabox(inode));
+		if (IS_ERR(ptr)) {
+			ret = PTR_ERR(ptr);
 			erofs_err("failed to read xattr entry @ nid %llu: %d",
 				  inode->nid | 0ULL, ret);
 			goto out;
 		}
 
-		entry = (struct erofs_xattr_entry *)buf;
+		entry = (struct erofs_xattr_entry *)ptr;
 		entry_sz = erofs_xattr_entry_size(entry);
 		if (remaining < entry_sz) {
 			erofs_err("xattr on-disk corruption: xattr entry beyond xattr_isize @ nid %llu",
@@ -404,6 +410,7 @@ static int erofs_verify_xattr(struct erofs_inode *inode)
 		remaining -= entry_sz;
 	}
 out:
+	erofs_put_metabuf(&buf);
 	return ret;
 }
 
include/erofs/internal.h +13 -6
index 3439a183..de6f4eae 100644
@@ -126,6 +126,7 @@ struct erofs_sb_info {
 		u16 device_id_mask;		/* used for others */
 	};
 	erofs_nid_t packed_nid;
+	erofs_nid_t metabox_nid;
 
 	u32 xattr_prefix_start;
 	u8 xattr_prefix_count;
@@ -153,8 +154,6 @@ struct erofs_sb_info {
 	bool useqpl;
 };
 
-#define EROFS_SUPER_END (EROFS_SUPER_OFFSET + sizeof(struct erofs_super_block))
-
 /* make sure that any user of the erofs headers has atleast 64bit off_t type */
 extern int erofs_assert_largefile[sizeof(off_t)-8];
 
@@ -182,6 +181,7 @@ 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(metabox, incompat, INCOMPAT_METABOX)
 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
 EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER)
 
@@ -295,12 +295,18 @@ struct erofs_inode {
 #endif
 };
 
+static inline bool erofs_inode_in_metabox(struct erofs_inode *inode)
+{
+	return inode->nid >> EROFS_DIRENT_NID_METABOX_BIT;
+}
+
 static inline erofs_off_t erofs_iloc(struct erofs_inode *inode)
 {
 	struct erofs_sb_info *sbi = inode->sbi;
+	erofs_off_t base = erofs_inode_in_metabox(inode) ? 0 :
+			erofs_pos(sbi, sbi->meta_blkaddr);
 
-	return erofs_pos(sbi, sbi->meta_blkaddr) +
-			(inode->nid << EROFS_ISLOTBITS);
+	return base + ((inode->nid & EROFS_DIRENT_NID_MASK) << EROFS_ISLOTBITS);
 }
 
 static inline bool is_inode_layout_compression(struct erofs_inode *inode)
@@ -437,9 +443,10 @@ int erofs_ilookup(const char *path, struct erofs_inode *vi);
 static inline void erofs_unmap_metabuf(struct erofs_buf *buf) {}
 static inline void erofs_put_metabuf(struct erofs_buf *buf) {}
 void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap);
-void erofs_init_metabuf(struct erofs_buf *buf, struct erofs_sb_info *sbi);
+void erofs_init_metabuf(struct erofs_buf *buf, struct erofs_sb_info *sbi,
+			bool in_mbox);
 void *erofs_read_metabuf(struct erofs_buf *buf, struct erofs_sb_info *sbi,
-			 erofs_off_t offset);
+			 erofs_off_t offset, bool in_mbox);
 int erofs_iopen(struct erofs_vfile *vf, struct erofs_inode *inode);
 int erofs_map_blocks(struct erofs_inode *inode,
 		struct erofs_map_blocks *map, int flags);
include/erofs_fs.h +9 -3
index e180c5d4..56e268e5 100644
@@ -32,8 +32,9 @@
 #define EROFS_FEATURE_INCOMPAT_DEDUPE		0x00000020
 #define EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES	0x00000040
 #define EROFS_FEATURE_INCOMPAT_48BIT		0x00000080
+#define EROFS_FEATURE_INCOMPAT_METABOX		0x00000100
 #define EROFS_ALL_FEATURE_INCOMPAT		\
-	((EROFS_FEATURE_INCOMPAT_48BIT << 1) - 1)
+	((EROFS_FEATURE_INCOMPAT_METABOX << 1) - 1)
 
 #define EROFS_SB_EXTSLOT_SIZE	16
 
@@ -83,7 +84,9 @@ struct erofs_super_block {
 	__u8 reserved[3];
 	__le32 build_time;	/* seconds added to epoch for mkfs time */
 	__le64 rootnid_8b;	/* (48BIT on) nid of root directory */
-	__u8 reserved2[8];
+	__le64 reserved2;
+	__le64 metabox_nid;	/* (METABOX on) nid of the metabox inode */
+	__le64 reserved3;
 };
 
 /*
@@ -268,6 +271,9 @@ struct erofs_inode_chunk_index {
 	__le32 startblk_lo;	/* starting block number of this chunk */
 };
 
+#define EROFS_DIRENT_NID_METABOX_BIT	63
+#define EROFS_DIRENT_NID_MASK		(BIT(EROFS_DIRENT_NID_METABOX_BIT) - 1)
+
 /* dirent sorts in alphabet order, thus we can do binary search */
 struct erofs_dirent {
 	__le64 nid;     /* node number */
@@ -448,7 +454,7 @@ static inline void erofs_check_ondisk_layout_definitions(void)
 	};
 #endif
 
-	BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
+	BUILD_BUG_ON(sizeof(struct erofs_super_block) != 144);
 	BUILD_BUG_ON(sizeof(struct erofs_inode_compact) != 32);
 	BUILD_BUG_ON(sizeof(struct erofs_inode_extended) != 64);
 	BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
lib/data.c +37 -6

73 lines not on this page · raw message

lib/namei.c +3 -2
index 8de0a908..e0a60853 100644
@@ -27,6 +27,7 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 	struct erofs_sb_info *sbi = vi->sbi;
 	erofs_blk_t blkaddr = erofs_blknr(sbi, erofs_iloc(vi));
 	unsigned int ofs = erofs_blkoff(sbi, erofs_iloc(vi));
+	bool in_mbox = erofs_inode_in_metabox(vi);
 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
 	erofs_blk_t addrmask = BIT_ULL(48) - 1;
 	struct erofs_inode_extended *die, copied;
@@ -35,7 +36,7 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 	void *ptr;
 	int err = 0;
 
-	ptr = erofs_read_metabuf(&buf, sbi, erofs_pos(sbi, blkaddr));
+	ptr = erofs_read_metabuf(&buf, sbi, erofs_pos(sbi, blkaddr), in_mbox);
 	if (IS_ERR(ptr)) {
 		err = PTR_ERR(ptr);
 		erofs_err("failed to get inode (nid: %llu) page, err %d",
@@ -74,7 +75,7 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 
 			memcpy(&copied, dic, gotten);
 			ptr = erofs_read_metabuf(&buf, sbi,
-					erofs_pos(sbi, blkaddr + 1));
+					erofs_pos(sbi, blkaddr + 1), in_mbox);
 			if (IS_ERR(ptr)) {
 				err = PTR_ERR(ptr);
 				erofs_err("failed to get inode payload block (nid: %llu), err %d",
lib/super.c +23 -6
index 1d13e6e3..57849fbf 100644
@@ -79,7 +79,7 @@ int erofs_read_superblock(struct erofs_sb_info *sbi)
 	int read, ret;
 
 	read = erofs_io_pread(&sbi->bdev, data, EROFS_MAX_BLOCK_SIZE, 0);
-	if (read < EROFS_SUPER_END) {
+	if (read < EROFS_SUPER_OFFSET + sizeof(*dsb)) {
 		ret = read < 0 ? read : -EIO;
 		erofs_err("cannot read erofs superblock: %s",
 			  erofs_strerror(ret));
@@ -123,6 +123,12 @@ int erofs_read_superblock(struct erofs_sb_info *sbi)
 		sbi->root_nid = le16_to_cpu(dsb->rb.rootnid_2b);
 	}
 	sbi->packed_nid = le64_to_cpu(dsb->packed_nid);
+	if (erofs_sb_has_metabox(sbi)) {
+		if (sbi->sb_size <= offsetof(struct erofs_super_block,
+					     metabox_nid))
+			return -EFSCORRUPTED;
+		sbi->metabox_nid = le64_to_cpu(dsb->metabox_nid);
+	}
 	sbi->inos = le64_to_cpu(dsb->inos);
 	sbi->checksum = le32_to_cpu(dsb->checksum);
 
@@ -187,7 +193,6 @@ int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh)
 		.devt_slotoff = cpu_to_le16(sbi->devt_slotoff),
 		.packed_nid = cpu_to_le64(sbi->packed_nid),
 	};
-	const u32 sb_blksize = round_up(EROFS_SUPER_END, erofs_blksiz(sbi));
 	char *buf;
 	int ret;
 
@@ -205,16 +210,21 @@ int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh)
 	else
 		sb.u1.lz4_max_distance = cpu_to_le16(sbi->lz4.max_distance);
 
-	buf = calloc(sb_blksize, 1);
+	if (erofs_sb_has_metabox(sbi))
+		sb.metabox_nid = cpu_to_le64(sbi->metabox_nid);
+	sb.sb_extslots = (sbi->sb_size - 128) >> 4;
+
+	buf = calloc(round_up(EROFS_SUPER_OFFSET + sbi->sb_size,
+			      erofs_blksiz(sbi)), 1);
 	if (!buf) {
 		erofs_err("failed to allocate memory for sb: %s",
 			  erofs_strerror(-errno));
 		return -ENOMEM;
 	}
-	memcpy(buf + EROFS_SUPER_OFFSET, &sb, sizeof(sb));
+	memcpy(buf + EROFS_SUPER_OFFSET, &sb, sbi->sb_size);
 
 	ret = erofs_dev_write(sbi, buf, sb_bh ? erofs_btell(sb_bh, false) : 0,
-			      EROFS_SUPER_END);
+			      EROFS_SUPER_OFFSET + sbi->sb_size);
 	free(buf);
 	if (sb_bh)
 		erofs_bdrop(sb_bh, false);
@@ -223,9 +233,16 @@ int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh)
 
 struct erofs_buffer_head *erofs_reserve_sb(struct erofs_bufmgr *bmgr)
 {
+	struct erofs_sb_info *sbi = bmgr->sbi;
 	struct erofs_buffer_head *bh;
+	unsigned int sb_size = 128;
 	int err;
 
+	if (erofs_sb_has_metabox(sbi) &&
+	    sb_size <= offsetof(struct erofs_super_block, metabox_nid))
+		sb_size = offsetof(struct erofs_super_block, metabox_nid) + 8;
+	sbi->sb_size = round_up(sb_size, 16);
+
 	bh = erofs_balloc(bmgr, META, 0, 0);
 	if (IS_ERR(bh)) {
 		erofs_err("failed to allocate super: %s",
@@ -233,7 +250,7 @@ struct erofs_buffer_head *erofs_reserve_sb(struct erofs_bufmgr *bmgr)
 		return bh;
 	}
 	bh->op = &erofs_skip_write_bhops;
-	err = erofs_bh_balloon(bh, EROFS_SUPER_END);
+	err = erofs_bh_balloon(bh, EROFS_SUPER_OFFSET + sbi->sb_size);
 	if (err < 0) {
 		erofs_err("failed to balloon super: %s", erofs_strerror(err));
 		goto err_bdrop;
lib/xattr.c +6 -6
index a2ef8d20..1f6a83f6 100644
@@ -1079,7 +1079,7 @@ static int init_inode_xattrs(struct erofs_inode *vi)
 	it.ofs = erofs_blkoff(sbi, erofs_iloc(vi) + vi->inode_isize);
 
 	/* read in shared xattr array (non-atomic, see kmalloc below) */
-	it.kaddr = erofs_read_metabuf(&it.buf, sbi, erofs_pos(sbi, it.blkaddr));
+	it.kaddr = erofs_read_metabuf(&it.buf, sbi, erofs_pos(sbi, it.blkaddr), false);
 	if (IS_ERR(it.kaddr))
 		return PTR_ERR(it.kaddr);
 
@@ -1101,7 +1101,7 @@ static int init_inode_xattrs(struct erofs_inode *vi)
 			DBG_BUGON(it.ofs != erofs_blksiz(sbi));
 
 			it.kaddr = erofs_read_metabuf(&it.buf, sbi,
-					erofs_pos(sbi, ++it.blkaddr));
+					erofs_pos(sbi, ++it.blkaddr), false);
 			if (IS_ERR(it.kaddr)) {
 				free(vi->xattr_shared_xattrs);
 				vi->xattr_shared_xattrs = NULL;
@@ -1143,7 +1143,7 @@ static inline int xattr_iter_fixup(struct xattr_iter *it)
 
 	it->blkaddr += erofs_blknr(sbi, it->ofs);
 	it->kaddr = erofs_read_metabuf(&it->buf, sbi,
-				       erofs_pos(sbi, it->blkaddr));
+				       erofs_pos(sbi, it->blkaddr), false);
 	if (IS_ERR(it->kaddr))
 		return PTR_ERR(it->kaddr);
 	it->ofs = erofs_blkoff(sbi, it->ofs);
@@ -1168,7 +1168,7 @@ static int inline_xattr_iter_begin(struct xattr_iter *it,
 	it->ofs = erofs_blkoff(sbi, erofs_iloc(vi) + inline_xattr_ofs);
 
 	it->kaddr = erofs_read_metabuf(&it->buf, sbi,
-				       erofs_pos(sbi, it->blkaddr));
+				       erofs_pos(sbi, it->blkaddr), false);
 	if (IS_ERR(it->kaddr))
 		return PTR_ERR(it->kaddr);
 	return vi->xattr_isize - xattr_header_sz;
@@ -1393,7 +1393,7 @@ static int shared_getxattr(struct erofs_inode *vi, struct getxattr_iter *it)
 
 		it->it.ofs = xattrblock_offset(vi, vi->xattr_shared_xattrs[i]);
 		it->it.kaddr = erofs_read_metabuf(&it->it.buf, sbi,
-						  erofs_pos(sbi, blkaddr));
+						  erofs_pos(sbi, blkaddr), false);
 		if (IS_ERR(it->it.kaddr))
 			return PTR_ERR(it->it.kaddr);
 		it->it.blkaddr = blkaddr;
@@ -1548,7 +1548,7 @@ static int shared_listxattr(struct erofs_inode *vi, struct listxattr_iter *it)
 
 		it->it.ofs = xattrblock_offset(vi, vi->xattr_shared_xattrs[i]);
 		it->it.kaddr = erofs_read_metabuf(&it->it.buf, sbi,
-						  erofs_pos(sbi, blkaddr));
+						  erofs_pos(sbi, blkaddr), false);
 		if (IS_ERR(it->it.kaddr))
 			return PTR_ERR(it->it.kaddr);
 		it->it.blkaddr = blkaddr;
lib/zmap.c +9 -6
index db8561be..916b0d25 100644
@@ -35,7 +35,8 @@ static int z_erofs_load_full_lcluster(struct z_erofs_maprecorder *m,
 	struct z_erofs_lcluster_index *di;
 	unsigned int advise;
 
-	di = erofs_read_metabuf(&m->map->buf, sbi, pos);
+	di = erofs_read_metabuf(&m->map->buf, sbi, pos,
+				erofs_inode_in_metabox(vi));
 	if (IS_ERR(di))
 		return PTR_ERR(di);
 	m->lcn = lcn;
@@ -150,7 +151,8 @@ static int z_erofs_load_compact_lcluster(struct z_erofs_maprecorder *m,
 	else
 		return -EOPNOTSUPP;
 
-	in = erofs_read_metabuf(&m->map->buf, sbi, pos);
+	in = erofs_read_metabuf(&m->map->buf, sbi, pos,
+				erofs_inode_in_metabox(vi));
 	if (IS_ERR(in))
 		return PTR_ERR(in);
 
@@ -541,6 +543,7 @@ static int z_erofs_map_blocks_ext(struct erofs_inode *vi,
 	unsigned int recsz = z_erofs_extent_recsize(vi->z_advise);
 	erofs_off_t pos = round_up(Z_EROFS_MAP_HEADER_END(erofs_iloc(vi) +
 				   vi->inode_isize + vi->xattr_isize), recsz);
+	bool in_mbox = erofs_inode_in_metabox(vi);
 	erofs_off_t lend = vi->i_size;
 	erofs_off_t l, r, mid, pa, la, lstart;
 	struct z_erofs_extent *ext;
@@ -550,7 +553,7 @@ static int z_erofs_map_blocks_ext(struct erofs_inode *vi,
 	map->m_flags = 0;
 	if (recsz <= offsetof(struct z_erofs_extent, pstart_hi)) {
 		if (recsz <= offsetof(struct z_erofs_extent, pstart_lo)) {
-			ext = erofs_read_metabuf(&map->buf, sbi, pos);
+			ext = erofs_read_metabuf(&map->buf, sbi, pos, in_mbox);
 			if (IS_ERR(ext))
 				return PTR_ERR(ext);
 			pa = le64_to_cpu(*(__le64 *)ext);
@@ -562,7 +565,7 @@ static int z_erofs_map_blocks_ext(struct erofs_inode *vi,
 		}
 
 		for (; lstart <= map->m_la; lstart += 1 << vi->z_lclusterbits) {
-			ext = erofs_read_metabuf(&map->buf, sbi, pos);
+			ext = erofs_read_metabuf(&map->buf, sbi, pos, in_mbox);
 			if (IS_ERR(ext))
 				return PTR_ERR(ext);
 			map->m_plen = le32_to_cpu(ext->plen);
@@ -582,7 +585,7 @@ static int z_erofs_map_blocks_ext(struct erofs_inode *vi,
 		for (l = 0, r = vi->z_extents; l < r; ) {
 			mid = l + (r - l) / 2;
 			ext = erofs_read_metabuf(&map->buf, sbi,
-						 pos + mid * recsz);
+						 pos + mid * recsz, in_mbox);
 			if (IS_ERR(ext))
 				return PTR_ERR(ext);
 
@@ -651,7 +654,7 @@ static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 		return 0;
 
 	pos = round_up(erofs_iloc(vi) + vi->inode_isize + vi->xattr_isize, 8);
-	h = erofs_read_metabuf(&buf, sbi, pos);
+	h = erofs_read_metabuf(&buf, sbi, pos, erofs_inode_in_metabox(vi));
 	if (IS_ERR(h))
 		return PTR_ERR(h);
 
Signature · 1 line
2.43.5
author
#10 lore raw

PATCH 11/11 erofs-utils: introduce metadata compression [metabox]

In reply to Gao Xiang

This implements metadata compression for mkfs.erofs.

It introduces a new command-line option -m <size>[:algorithm]. If specified, all inode metadata will be kept in a special inode called the metabox inode.

The metabox inode will be compressed using the specified physical cluster size <size>. For example:

  $ mkfs.erofs -zlzma -C1048576 -m 4096 foo.erofs foo

will compress metadata in 4096-byte pclusters.

EROFS supports multiple algorithms within a filesystem, so metadata can be compressed with a different algorithm. For instance, LZMA may be too slow for metadata, but users may still want to save metadata space:

  $ mkfs.erofs -zlzma -C1048576 -m4096:lz4hc,12 foo.erofs foo
Or
  $ mkfs.erofs -zlzma:lz4hc,12 -C1048576 -m4096:1 foo.erofs foo

will compress metadata in 4096-byte pclusters using LZ4HC level 12.

---
 include/erofs/blobchunk.h |   3 +-
 include/erofs/compress.h  |  11 ----
 include/erofs/config.h    |   2 +
 include/erofs/fragments.h |   5 ++
 include/erofs/internal.h  |   5 +-
 lib/Makefile.am           |   5 +-
 lib/blobchunk.c           |   6 +-
 lib/compress.c            |  26 ++++++--
 lib/config.c              |   1 +
 lib/inode.c               | 131 +++++++++++++++++++++++++-------------
 lib/liberofs_metabox.h    |  19 ++++++
 lib/metabox.c             |  66 +++++++++++++++++++
 mkfs/main.c               |  62 ++++++++++++++++--
 13 files changed, 268 insertions(+), 74 deletions(-)
 create mode 100644 lib/liberofs_metabox.h
 create mode 100644 lib/metabox.c
include/erofs/blobchunk.h +2 -1
index 619155f7..ef067732 100644
@@ -16,7 +16,8 @@ extern "C"
 
 struct erofs_blobchunk *erofs_get_unhashed_chunk(unsigned int device_id,
 		erofs_blk_t blkaddr, erofs_off_t sourceoffset);
-int erofs_blob_write_chunk_indexes(struct erofs_inode *inode, erofs_off_t off);
+int erofs_write_chunk_indexes(struct erofs_inode *inode, struct erofs_vfile *vf,
+			      erofs_off_t off);
 int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
 				  erofs_off_t startoff);
 int erofs_write_zero_inode(struct erofs_inode *inode);
include/erofs/compress.h +0 -11
index 4731a8b4..d5b25197 100644
@@ -30,17 +30,6 @@ int z_erofs_compress_exit(struct erofs_sb_info *sbi);
 const char *z_erofs_list_supported_algorithms(int i, unsigned int *mask);
 const struct erofs_algorithm *z_erofs_list_available_compressors(int *i);
 
-static inline bool erofs_is_packed_inode(struct erofs_inode *inode)
-{
-	erofs_nid_t packed_nid = inode->sbi->packed_nid;
-
-	if (inode->nid == EROFS_PACKED_NID_UNALLOCATED) {
-		DBG_BUGON(packed_nid != EROFS_PACKED_NID_UNALLOCATED);
-		return true;
-	}
-	return (packed_nid > 0 && inode->nid == packed_nid);
-}
-
 #ifdef __cplusplus
 }
 #endif
include/erofs/config.h +2 -0
index e4d2bb32..8c40fd1b 100644
@@ -79,11 +79,13 @@ struct erofs_configure {
 	struct erofs_compr_opts c_compr_opts[EROFS_MAX_COMPR_CFGS];
 	char c_force_inodeversion;
 	char c_force_chunkformat;
+	u8 c_mkfs_metabox_algid;
 	/* < 0, xattr disabled and INT_MAX, always use inline xattrs */
 	int c_inline_xattr_tolerance;
 	u32 c_mkfs_pclustersize_max;
 	u32 c_mkfs_pclustersize_def;
 	u32 c_mkfs_pclustersize_packed;
+	s32 c_mkfs_pclustersize_metabox;
 	u32 c_max_decompressed_extent_bytes;
 	u64 c_unix_timestamp;
 	u32 c_uid, c_gid;
include/erofs/fragments.h +5 -0
index 112f002e..7c7acf46 100644
@@ -15,6 +15,11 @@ extern "C"
 extern const char *erofs_frags_packedname;
 #define EROFS_PACKED_INODE	erofs_frags_packedname
 
+static inline bool erofs_is_packed_inode(struct erofs_inode *inode)
+{
+	return inode->i_srcpath == EROFS_PACKED_INODE;
+}
+
 u32 z_erofs_fragments_tofh(struct erofs_inode *inode, int fd, erofs_off_t fpos);
 int erofs_fragment_findmatch(struct erofs_inode *inode, int fd, u32 tofh);
 
include/erofs/internal.h +3 -2
index de6f4eae..e9b90995 100644
@@ -83,11 +83,10 @@ struct erofs_xattr_prefix_item {
 	u8 infix_len;
 };
 
-#define EROFS_PACKED_NID_UNALLOCATED	-1
-
 struct erofs_mkfs_dfops;
 struct erofs_packed_inode;
 struct z_erofs_mgr;
+struct erofs_metaboxmgr;
 
 struct erofs_sb_info {
 	struct erofs_sb_lz4_info lz4;
@@ -149,6 +148,7 @@ struct erofs_sb_info {
 #endif
 	struct erofs_bufmgr *bmgr;
 	struct z_erofs_mgr *zmgr;
+	struct erofs_metaboxmgr *m2gr;
 	struct erofs_packed_inode *packedinode;
 	struct erofs_buffer_head *bh_devt;
 	bool useqpl;
@@ -245,6 +245,7 @@ struct erofs_inode {
 	/* inline tail-end packing size */
 	unsigned short idata_size;
 	char datasource;
+	bool in_metabox;
 	bool compressed_idata;
 	bool lazy_tailblock;
 	bool opaque;
lib/Makefile.am +3 -2
index 688403b7..0db81df7 100644
@@ -28,7 +28,8 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
       $(top_srcdir)/include/erofs/fragments.h \
       $(top_srcdir)/include/erofs/rebuild.h \
       $(top_srcdir)/lib/liberofs_private.h \
-      $(top_srcdir)/lib/liberofs_xxhash.h
+      $(top_srcdir)/lib/liberofs_xxhash.h \
+      $(top_srcdir)/lib/liberofs_metabox.h
 
 noinst_HEADERS += compressor.h
 liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \
@@ -36,7 +37,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \
 		      compress_hints.c hashmap.c sha256.c blobchunk.c dir.c \
 		      fragments.c dedupe.c uuid_unparse.c uuid.c tar.c \
 		      block_list.c rebuild.c diskbuf.c bitops.c dedupe_ext.c \
-		      vmdk.c
+		      vmdk.c metabox.c
 
 liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include
 if ENABLE_LZ4
lib/blobchunk.c +3 -3
index bbc69cfb..157b9a9c 100644
@@ -133,8 +133,8 @@ static int erofs_blob_hashmap_cmp(const void *a, const void *b,
 		      sizeof(ec1->sha256));
 }
 
-int erofs_blob_write_chunk_indexes(struct erofs_inode *inode,
-				   erofs_off_t off)
+int erofs_write_chunk_indexes(struct erofs_inode *inode, struct erofs_vfile *vf,
+			      erofs_off_t off)
 {
 	struct erofs_sb_info *sbi = inode->sbi;
 	erofs_blk_t remaining_blks = BLK_ROUND_UP(sbi, inode->i_size);
@@ -202,7 +202,7 @@ int erofs_blob_write_chunk_indexes(struct erofs_inode *inode,
 					 source_offset, zeroedlen);
 	}
 	off = roundup(off, unit);
-	return erofs_io_pwrite(&sbi->bdev, inode->chunkindexes,
+	return erofs_io_pwrite(vf, inode->chunkindexes,
 			       off, inode->extent_isize);
 }
 
lib/compress.c +20 -6
index 22fb5d6d..0bfad3f5 100644
@@ -25,6 +25,7 @@
 #ifdef EROFS_MT_ENABLED
 #include "erofs/workqueue.h"
 #endif
+#include "liberofs_metabox.h"
 
 #define Z_EROFS_DESTBUF_SZ	(Z_EROFS_PCLUSTER_MAX_SIZE + EROFS_MAX_BLOCK_SIZE * 2)
 
@@ -459,6 +460,8 @@ static unsigned int z_erofs_get_max_pclustersize(struct erofs_inode *inode)
 {
 	if (erofs_is_packed_inode(inode)) {
 		return cfg.c_mkfs_pclustersize_packed;
+	} else if (erofs_is_metabox_inode(inode)) {
+		return cfg.c_mkfs_pclustersize_metabox;
 #ifndef NDEBUG
 	} else if (cfg.c_random_pclusterblks) {
 		unsigned int pclusterblks =
@@ -567,7 +570,8 @@ static int __z_erofs_compress_one(struct z_erofs_compress_sctx *ctx,
 	unsigned int len = ctx->tail - ctx->head;
 	bool is_packed_inode = erofs_is_packed_inode(inode);
 	bool tsg = (ctx->seg_idx + 1 >= ictx->seg_num), final = !ctx->remaining;
-	bool may_packing = (cfg.c_fragments && tsg && final && !is_packed_inode);
+	bool may_packing = (cfg.c_fragments && tsg && final && !is_packed_inode &&
+			    !erofs_is_metabox_inode(inode));
 	bool data_unaligned = ictx->data_unaligned;
 	bool may_inline = (cfg.c_ztailpacking && !data_unaligned && tsg &&
 			   final && !may_packing);
@@ -1247,6 +1251,7 @@ int z_erofs_compress_segment(struct z_erofs_compress_sctx *ctx,
 	struct z_erofs_compress_ictx *ictx = ctx->ictx;
 	struct erofs_inode *inode = ictx->inode;
 	bool frag = cfg.c_fragments && !erofs_is_packed_inode(inode) &&
+		!erofs_is_metabox_inode(inode) &&
 		ctx->seg_idx >= ictx->seg_num - 1;
 	int fd = ictx->fd;
 	int ret;
@@ -1764,8 +1769,9 @@ void *erofs_begin_compressed_file(struct erofs_inode *inode, int fd, u64 fpos)
 {
 	struct erofs_sb_info *sbi = inode->sbi;
 	struct z_erofs_compress_ictx *ictx;
-	bool all_fragments = cfg.c_all_fragments &&
-					!erofs_is_packed_inode(inode);
+	bool frag = cfg.c_fragments && !erofs_is_packed_inode(inode) &&
+		!erofs_is_metabox_inode(inode);
+	bool all_fragments = cfg.c_all_fragments && frag;
 	int ret;
 
 	/* initialize per-file compression setting */
@@ -1799,8 +1805,10 @@ void *erofs_begin_compressed_file(struct erofs_inode *inode, int fd, u64 fpos)
 			return ERR_PTR(-ENOMEM);
 	}
 	ictx->fd = fd;
-
-	ictx->ccfg = &sbi->zmgr->ccfg[inode->z_algorithmtype[0]];
+	if (erofs_is_metabox_inode(inode))
+		ictx->ccfg = &sbi->zmgr->ccfg[cfg.c_mkfs_metabox_algid];
+	else
+		ictx->ccfg = &sbi->zmgr->ccfg[inode->z_algorithmtype[0]];
 	inode->z_algorithmtype[0] = ictx->ccfg->algorithmtype;
 	inode->z_algorithmtype[1] = 0;
 	ictx->data_unaligned = erofs_sb_has_48bit(sbi) &&
@@ -1809,7 +1817,7 @@ void *erofs_begin_compressed_file(struct erofs_inode *inode, int fd, u64 fpos)
 	if (cfg.c_fragments && !cfg.c_dedupe && !ictx->data_unaligned)
 		inode->z_advise |= Z_EROFS_ADVISE_INTERLACED_PCLUSTER;
 
-	if (cfg.c_fragments && !erofs_is_packed_inode(inode)) {
+	if (frag) {
 		ictx->tofh = z_erofs_fragments_tofh(inode, fd, fpos);
 		if (ictx == &g_ictx && cfg.c_fragdedupe != FRAGDEDUPE_OFF) {
 			/*
@@ -2107,6 +2115,12 @@ int z_erofs_compress_init(struct erofs_sb_info *sbi, struct erofs_buffer_head *s
 		return -EINVAL;
 	}
 
+	if (cfg.c_mkfs_pclustersize_metabox > (s32)cfg.c_mkfs_pclustersize_max) {
+		erofs_err("invalid pclustersize for the metabox file %u",
+			  cfg.c_mkfs_pclustersize_metabox);
+		return -EINVAL;
+	}
+
 	if (sb_bh && erofs_sb_has_compr_cfgs(sbi)) {
 		ret = z_erofs_build_compr_cfgs(sbi, sb_bh, max_dict_size);
 		if (ret)
lib/config.c +1 -0
index 848bc590..f7c6fbad 100644
@@ -36,6 +36,7 @@ void erofs_init_configure(void)
 	cfg.c_uid = -1;
 	cfg.c_gid = -1;
 	cfg.c_max_decompressed_extent_bytes = -1;
+	cfg.c_mkfs_pclustersize_metabox = -1;
 	erofs_stdout_tty = isatty(STDOUT_FILENO);
 }
 
lib/inode.c +88 -43

285 lines not on this page · raw message

lib/liberofs_metabox.h new file +19 -0
new file mode 100644
index 00000000..75b1e69e
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+#ifndef __EROFS_LIB_LIBEROFS_METABOX_H
+#define __EROFS_LIB_LIBEROFS_METABOX_H
+
+#include "erofs/internal.h"
+
+extern const char *erofs_metabox_identifier;
+#define EROFS_METABOX_INODE	erofs_metabox_identifier
+
+static inline bool erofs_is_metabox_inode(struct erofs_inode *inode)
+{
+	return inode->i_srcpath == EROFS_METABOX_INODE;
+}
+
+int erofs_metabox_init(struct erofs_sb_info *sbi);
+struct erofs_bufmgr *erofs_metabox_bmgr(struct erofs_sb_info *sbi);
+int erofs_metabox_iflush(struct erofs_sb_info *sbi);
+
+#endif
lib/metabox.c new file +66 -0

66 lines not on this page · raw message

mkfs/main.c +56 -6

135 lines not on this page · raw message

Signature · 1 line
2.43.5