Skip to contentSkip to conversation

erofs-utils: lib: use erofs_map_blocks() for all datalayouts

PATCH In review Gao Xiang 1 participant 5 messages Jan 17, 2025 – Jan 20, 2025 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/3 erofs-utils: lib: use erofs_map_blocks() for all datalayouts

It simplifies the codebase a bit.

---
 dump/main.c | 11 +----------
 fsck/main.c | 23 ++---------------------
 lib/data.c  | 12 ++++++++++--
 3 files changed, 13 insertions(+), 33 deletions(-)
dump/main.c +1 -10
index 860ee5a..44f65da 100644
@@ -359,14 +359,6 @@ static int erofsdump_readdir(struct erofs_dir_context *ctx)
 	return 0;
 }
 
-static int erofsdump_map_blocks(struct erofs_inode *inode,
-		struct erofs_map_blocks *map, int flags)
-{
-	if (erofs_inode_is_data_compressed(inode->datalayout))
-		return z_erofs_map_blocks_iter(inode, map, flags);
-	return erofs_map_blocks(inode, map, flags);
-}
-
 static void erofsdump_show_fileinfo(bool show_extent)
 {
 	const char *ext_fmt[] = {
@@ -461,8 +453,7 @@ static void erofsdump_show_fileinfo(bool show_extent)
 	while (map.m_la < inode.i_size) {
 		struct erofs_map_dev mdev;
 
-		err = erofsdump_map_blocks(&inode, &map,
-				EROFS_GET_BLOCKS_FIEMAP);
+		err = erofs_map_blocks(&inode, &map, EROFS_GET_BLOCKS_FIEMAP);
 		if (err) {
 			erofs_err("failed to get file blocks range");
 			return;
fsck/main.c +2 -21
index f20b767..f56a812 100644
@@ -527,31 +527,12 @@ static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)
 	erofs_dbg("verify data chunk of nid(%llu): type(%d)",
 		  inode->nid | 0ULL, inode->datalayout);
 
-	switch (inode->datalayout) {
-	case EROFS_INODE_FLAT_PLAIN:
-	case EROFS_INODE_FLAT_INLINE:
-	case EROFS_INODE_CHUNK_BASED:
-		compressed = false;
-		break;
-	case EROFS_INODE_COMPRESSED_FULL:
-	case EROFS_INODE_COMPRESSED_COMPACT:
-		compressed = true;
-		break;
-	default:
-		erofs_err("unknown datalayout");
-		return -EINVAL;
-	}
-
+	compressed = erofs_inode_is_data_compressed(inode->datalayout);
 	while (pos < inode->i_size) {
 		unsigned int alloc_rawsize;
 
 		map.m_la = pos;
-		if (compressed)
-			ret = z_erofs_map_blocks_iter(inode, &map,
-					EROFS_GET_BLOCKS_FIEMAP);
-		else
-			ret = erofs_map_blocks(inode, &map,
-					EROFS_GET_BLOCKS_FIEMAP);
+		ret = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_FIEMAP);
 		if (ret)
 			goto out;
 
lib/data.c +10 -2
index f37f8f0..8033208 100644
@@ -62,8 +62,8 @@ err_out:
 	return err;
 }
 
-int erofs_map_blocks(struct erofs_inode *inode,
-		struct erofs_map_blocks *map, int flags)
+int __erofs_map_blocks(struct erofs_inode *inode,
+		       struct erofs_map_blocks *map, int flags)
 {
 	struct erofs_inode *vi = inode;
 	struct erofs_sb_info *sbi = inode->sbi;
@@ -132,6 +132,14 @@ out:
 	return err;
 }
 
+int erofs_map_blocks(struct erofs_inode *inode,
+		     struct erofs_map_blocks *map, int flags)
+{
+	if (erofs_inode_is_data_compressed(inode->datalayout))
+		return z_erofs_map_blocks_iter(inode, map, flags);
+	return __erofs_map_blocks(inode, map, flags);
+}
+
 int erofs_map_dev(struct erofs_sb_info *sbi, struct erofs_map_dev *map)
 {
 	struct erofs_device_info *dif;
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 2/3 erofs-utils: lib: rework per-sb fragment generator

It's also used for the upcoming fragment cache.

---
 include/erofs/fragments.h |  14 ++--
 include/erofs/internal.h  |   3 +
 include/erofs/xattr.h     |   2 +-
 lib/fragments.c           | 150 +++++++++++++++++++++++---------------
 lib/xattr.c               |   3 +-
 mkfs/main.c               |  20 ++---
 6 files changed, 109 insertions(+), 83 deletions(-)
include/erofs/fragments.h +7 -7
index 65910f5..e92b7c7 100644
@@ -15,17 +15,17 @@ extern "C"
 extern const char *erofs_frags_packedname;
 #define EROFS_PACKED_INODE	erofs_frags_packedname
 
-FILE *erofs_packedfile_init(void);
-void erofs_packedfile_exit(void);
-int erofs_flush_packed_inode(struct erofs_sb_info *sbi);
-
 int z_erofs_fragments_dedupe(struct erofs_inode *inode, int fd, u32 *tofcrc);
+
+void z_erofs_fragments_commit(struct erofs_inode *inode);
 int z_erofs_pack_file_from_fd(struct erofs_inode *inode, int fd, u32 tofcrc);
 int z_erofs_pack_fragments(struct erofs_inode *inode, void *data,
 			   unsigned int len, u32 tofcrc);
-void z_erofs_fragments_commit(struct erofs_inode *inode);
-int z_erofs_fragments_init(void);
-void z_erofs_fragments_exit(void);
+int erofs_flush_packed_inode(struct erofs_sb_info *sbi);
+FILE *erofs_packedfile(struct erofs_sb_info *sbi);
+
+int erofs_packedfile_init(struct erofs_sb_info *sbi, bool fragments_mkfs);
+void erofs_packedfile_exit(struct erofs_sb_info *sbi);
 
 #ifdef __cplusplus
 }
include/erofs/internal.h +3 -0
index 5d7d022..eb665e2 100644
@@ -81,6 +81,8 @@ struct erofs_xattr_prefix_item {
 #define EROFS_PACKED_NID_UNALLOCATED	-1
 
 struct erofs_mkfs_dfops;
+struct erofs_packed_inode;
+
 struct erofs_sb_info {
 	struct erofs_sb_lz4_info lz4;
 	struct erofs_device_info *devs;
@@ -139,6 +141,7 @@ struct erofs_sb_info {
 	struct erofs_mkfs_dfops *mkfs_dfops;
 #endif
 	struct erofs_bufmgr *bmgr;
+	struct erofs_packed_inode *packedinode;
 	bool useqpl;
 };
 
include/erofs/xattr.h +1 -1
index 804f565..81604b6 100644
@@ -50,7 +50,7 @@ int erofs_build_shared_xattrs_from_path(struct erofs_sb_info *sbi, const char *p
 
 int erofs_xattr_insert_name_prefix(const char *prefix);
 void erofs_xattr_cleanup_name_prefixes(void);
-int erofs_xattr_write_name_prefixes(struct erofs_sb_info *sbi, FILE *f);
+int erofs_xattr_flush_name_prefixes(struct erofs_sb_info *sbi);
 void erofs_xattr_prefixes_cleanup(struct erofs_sb_info *sbi);
 int erofs_xattr_prefixes_init(struct erofs_sb_info *sbi);
 
lib/fragments.c +91 -59

262 lines not on this page · raw message

lib/xattr.c +2 -1
index c20c9a2..063b01a 100644
@@ -809,8 +809,9 @@ static int comp_shared_xattr_item(const void *a, const void *b)
 	return la > lb;
 }
 
-int erofs_xattr_write_name_prefixes(struct erofs_sb_info *sbi, FILE *f)
+int erofs_xattr_flush_name_prefixes(struct erofs_sb_info *sbi)
 {
+	FILE *f = erofs_packedfile(sbi);
 	struct ea_type_node *tnode;
 	off_t offset;
 
mkfs/main.c +5 -15
index 624b191..a0fce35 100644
@@ -1197,7 +1197,6 @@ int main(int argc, char **argv)
 	struct erofs_inode *root = NULL;
 	erofs_blk_t nblocks = 0;
 	struct timeval t;
-	FILE *packedfile = NULL;
 	FILE *blklst = NULL;
 	u32 crc;
 
@@ -1253,17 +1252,10 @@ int main(int argc, char **argv)
 		if (!cfg.c_mkfs_pclustersize_packed)
 			cfg.c_mkfs_pclustersize_packed = cfg.c_mkfs_pclustersize_def;
 
-		packedfile = erofs_packedfile_init();
-		if (IS_ERR(packedfile)) {
-			erofs_err("failed to initialize packedfile");
-			return 1;
-		}
-	}
-
-	if (cfg.c_fragments) {
-		err = z_erofs_fragments_init();
+		err = erofs_packedfile_init(&g_sbi, cfg.c_fragments);
 		if (err) {
-			erofs_err("failed to initialize fragments");
+			erofs_err("failed to initialize packedfile: %s",
+				  strerror(-err));
 			return 1;
 		}
 	}
@@ -1445,7 +1437,7 @@ int main(int argc, char **argv)
 		}
 
 		if (cfg.c_extra_ea_name_prefixes)
-			erofs_xattr_write_name_prefixes(&g_sbi, packedfile);
+			erofs_xattr_flush_name_prefixes(&g_sbi);
 
 		root = erofs_mkfs_build_tree_from_path(&g_sbi, cfg.c_src_path);
 		if (IS_ERR(root)) {
@@ -1510,9 +1502,7 @@ exit:
 	erofs_cleanup_exclude_rules();
 	if (cfg.c_chunkbits)
 		erofs_blob_exit();
-	if (cfg.c_fragments)
-		z_erofs_fragments_exit();
-	erofs_packedfile_exit();
+	erofs_packedfile_exit(&g_sbi);
 	erofs_xattr_cleanup_name_prefixes();
 	erofs_rebuild_cleanup();
 	erofs_diskbuf_exit();
Signature · 1 line
2.43.5
author
#2 lore raw

PATCH 3/3 erofs-utils: introduce fragment cache

In reply to Gao Xiang

Difference from the previous Yiyan's version [1], it just uses a tmpfile to keep all decompressed data for fragments.

Dataset: linux 5.4.140
mkfs.erofs command line:
mkfs.erofs -zlzma -C131072 -T0 -Eall-fragments,fragdedupe=inode foo.erofs <dir>
Test command line:

	hyperfine -p "echo 3 > /proc/sys/vm/drop_caches; sleep 1" "fsck/fsck.erofs --extract foo.erofs"

Vanilla:
Time (mean ± σ): 362.309 s ± 0.406 s [User: 360.298 s, System: 0.956 s]

After:
Time (mean ± σ): 20.880 s ± 0.026 s [User: 19.751 s, System: 1.058 s]

[1] https://lore.kernel.org/r/[email protected]

---
 fsck/main.c               |  12 ++-
 fuse/main.c               |  16 +++-
 include/erofs/fragments.h |   3 +
 lib/data.c                |  14 +--
 lib/fragments.c           | 192 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 222 insertions(+), 15 deletions(-)
fsck/main.c +11 -1
index f56a812..d375835 100644
@@ -16,6 +16,7 @@
 #include "erofs/dir.h"
 #include "erofs/xattr.h"
 #include "../lib/compressor.h"
+#include "erofs/fragments.h"
 
 static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid);
 
@@ -1079,10 +1080,17 @@ int main(int argc, char *argv[])
 		erofsfsck_hardlink_init();
 
 	if (erofs_sb_has_fragments(&g_sbi) && g_sbi.packed_nid > 0) {
+		err = erofs_packedfile_init(&g_sbi, false);
+		if (err) {
+			erofs_err("failed to initialize packedfile: %s",
+				  erofs_strerror(err));
+			goto exit_hardlink;
+		}
+
 		err = erofsfsck_check_inode(g_sbi.packed_nid, g_sbi.packed_nid);
 		if (err) {
 			erofs_err("failed to verify packed file");
-			goto exit_hardlink;
+			goto exit_packedinode;
 		}
 	}
 
@@ -1108,6 +1116,8 @@ int main(int argc, char *argv[])
 		}
 	}
 
+exit_packedinode:
+	erofs_packedfile_exit(&g_sbi);
 exit_hardlink:
 	if (fsckcfg.extract_path)
 		erofsfsck_hardlink_exit();
fuse/main.c +14 -2
index f6c04e8..cb2759e 100644
@@ -12,6 +12,7 @@
 #include "erofs/print.h"
 #include "erofs/dir.h"
 #include "erofs/inode.h"
+#include "erofs/fragments.h"
 
 #include <float.h>
 #include <fuse.h>
@@ -688,11 +689,20 @@ int main(int argc, char *argv[])
 		goto err_dev_close;
 	}
 
+	if (erofs_sb_has_fragments(&g_sbi) && g_sbi.packed_nid > 0) {
+		ret = erofs_packedfile_init(&g_sbi, false);
+		if (ret) {
+			erofs_err("failed to initialize packedfile: %s",
+				  erofs_strerror(ret));
+			goto err_super_put;
+		}
+	}
+
 #if FUSE_MAJOR_VERSION >= 3
 	se = fuse_session_new(&args, &erofsfuse_lops, sizeof(erofsfuse_lops),
 			      NULL);
 	if (!se)
-		goto err_super_put;
+		goto err_packedinode;
 
 	if (fuse_session_mount(se, opts.mountpoint) >= 0) {
 		EROFSFUSE_MOUNT_MSG
@@ -722,7 +732,7 @@ int main(int argc, char *argv[])
 #else
 	ch = fuse_mount(opts.mountpoint, &args);
 	if (!ch)
-		goto err_super_put;
+		goto err_packedinode;
 	EROFSFUSE_MOUNT_MSG
 	se = fuse_lowlevel_new(&args, &erofsfuse_lops, sizeof(erofsfuse_lops),
 			       NULL);
@@ -743,6 +753,8 @@ int main(int argc, char *argv[])
 	fuse_unmount(opts.mountpoint, ch);
 #endif
 
+err_packedinode:
+	erofs_packedfile_exit(&g_sbi);
 err_super_put:
 	erofs_put_super(&g_sbi);
 err_dev_close:
include/erofs/fragments.h +3 -0
index e92b7c7..14a1b4a 100644
@@ -27,6 +27,9 @@ FILE *erofs_packedfile(struct erofs_sb_info *sbi);
 int erofs_packedfile_init(struct erofs_sb_info *sbi, bool fragments_mkfs);
 void erofs_packedfile_exit(struct erofs_sb_info *sbi);
 
+int erofs_packedfile_read(struct erofs_sb_info *sbi,
+			  void *buf, erofs_off_t len, erofs_off_t pos);
+
 #ifdef __cplusplus
 }
 #endif
lib/data.c +2 -12
index 8033208..fd9c21a 100644
@@ -8,6 +8,7 @@
 #include "erofs/internal.h"
 #include "erofs/trace.h"
 #include "erofs/decompress.h"
+#include "erofs/fragments.h"
 
 static int erofs_map_blocks_flatmode(struct erofs_inode *inode,
 				     struct erofs_map_blocks *map,
@@ -248,18 +249,7 @@ int z_erofs_read_one_data(struct erofs_inode *inode,
 	int ret = 0;
 
 	if (map->m_flags & EROFS_MAP_FRAGMENT) {
-		struct erofs_inode packed_inode = {
-			.sbi = sbi,
-			.nid = sbi->packed_nid,
-		};
-
-		ret = erofs_read_inode_from_disk(&packed_inode);
-		if (ret) {
-			erofs_err("failed to read packed inode from disk");
-			return ret;
-		}
-
-		return erofs_pread(&packed_inode, buffer, length - skip,
+		return erofs_packedfile_read(sbi, buffer, length - skip,
 				   inode->fragmentoff + skip);
 	}
 
lib/fragments.c +192 -0

219 lines not on this page · raw message

Signature · 1 line
2.43.5
author
#3 lore raw

Re: [PATCH 3/3] erofs-utils: introduce fragment cache

Quoted 7 lines · Gao Xiang

Difference from the previous Yiyan's version [1], it just uses a tmpfile to keep all decompressed data for fragments.

Dataset: linux 5.4.140
mkfs.erofs command line:
mkfs.erofs -zlzma -C131072 -T0 -Eall-fragments,fragdedupe=inode foo.erofs <dir>
Test command line:

Quoted 7 lines
	hyperfine -p "echo 3 > /proc/sys/vm/drop_caches; sleep 1" "fsck/fsck.erofs --extract foo.erofs"

Vanilla:

   Time (mean ± σ):     362.309 s ±  0.406 s   [User: 360.298 s, System: 0.956 s]

After:

   Time (mean ± σ):     20.880 s ±  0.026 s    [User: 19.751 s, System: 1.058 s]

An in-memory LRU cache could also be implemented later to meet different needs.

Quoted 4 lines

[1] https://lore.kernel.org/r/[email protected]

author
#4 lore raw

PATCH v2 3/3 erofs-utils: introduce fragment cache

In reply to Gao Xiang

Difference from the previous Yiyan's version [1], it just uses a tmpfile to keep all decompressed data for fragments.

Dataset: linux 5.4.140
mkfs.erofs command line:
mkfs.erofs -zlzma -C131072 -T0 -Eall-fragments,fragdedupe=inode foo.erofs <dir>
Test command line:

	hyperfine -p "echo 3 > /proc/sys/vm/drop_caches; sleep 1" "fsck/fsck.erofs --extract foo.erofs"

Vanilla:
Time (mean ± σ): 362.309 s ± 0.406 s [User: 360.298 s, System: 0.956 s]

After:
Time (mean ± σ): 20.880 s ± 0.026 s [User: 19.751 s, System: 1.058 s]

An in-memory LRU cache could also be implemented later to meet different needs.

[1] https://lore.kernel.org/r/[email protected]

---
change since v1:
- fix CI error: https://github.com/erofs/erofsnightly/actions/runs/12840095401/job/35808271964
fragments.c: In function ‘erofs_packedfile_preload’:
fragments.c:489:25: error: ignoring return value of ‘ftruncate’ declared with attribute ‘warn_unused_result’ [-Werror=unused-result]

  489 |                         ftruncate(fileno(epi->file), 0);
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors
but we just ftruncate the used space for ENOSPC as an attempt, so don't bother with the result really.

 fsck/main.c               |  12 ++-
 fuse/main.c               |  16 +++-
 include/erofs/fragments.h |   3 +
 lib/data.c                |  14 +--
 lib/fragments.c           | 192 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 222 insertions(+), 15 deletions(-)
fsck/main.c +11 -1
index f56a812..d375835 100644
@@ -16,6 +16,7 @@
 #include "erofs/dir.h"
 #include "erofs/xattr.h"
 #include "../lib/compressor.h"
+#include "erofs/fragments.h"
 
 static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid);
 
@@ -1079,10 +1080,17 @@ int main(int argc, char *argv[])
 		erofsfsck_hardlink_init();
 
 	if (erofs_sb_has_fragments(&g_sbi) && g_sbi.packed_nid > 0) {
+		err = erofs_packedfile_init(&g_sbi, false);
+		if (err) {
+			erofs_err("failed to initialize packedfile: %s",
+				  erofs_strerror(err));
+			goto exit_hardlink;
+		}
+
 		err = erofsfsck_check_inode(g_sbi.packed_nid, g_sbi.packed_nid);
 		if (err) {
 			erofs_err("failed to verify packed file");
-			goto exit_hardlink;
+			goto exit_packedinode;
 		}
 	}
 
@@ -1108,6 +1116,8 @@ int main(int argc, char *argv[])
 		}
 	}
 
+exit_packedinode:
+	erofs_packedfile_exit(&g_sbi);
 exit_hardlink:
 	if (fsckcfg.extract_path)
 		erofsfsck_hardlink_exit();
fuse/main.c +14 -2
index f6c04e8..cb2759e 100644
@@ -12,6 +12,7 @@
 #include "erofs/print.h"
 #include "erofs/dir.h"
 #include "erofs/inode.h"
+#include "erofs/fragments.h"
 
 #include <float.h>
 #include <fuse.h>
@@ -688,11 +689,20 @@ int main(int argc, char *argv[])
 		goto err_dev_close;
 	}
 
+	if (erofs_sb_has_fragments(&g_sbi) && g_sbi.packed_nid > 0) {
+		ret = erofs_packedfile_init(&g_sbi, false);
+		if (ret) {
+			erofs_err("failed to initialize packedfile: %s",
+				  erofs_strerror(ret));
+			goto err_super_put;
+		}
+	}
+
 #if FUSE_MAJOR_VERSION >= 3
 	se = fuse_session_new(&args, &erofsfuse_lops, sizeof(erofsfuse_lops),
 			      NULL);
 	if (!se)
-		goto err_super_put;
+		goto err_packedinode;
 
 	if (fuse_session_mount(se, opts.mountpoint) >= 0) {
 		EROFSFUSE_MOUNT_MSG
@@ -722,7 +732,7 @@ int main(int argc, char *argv[])
 #else
 	ch = fuse_mount(opts.mountpoint, &args);
 	if (!ch)
-		goto err_super_put;
+		goto err_packedinode;
 	EROFSFUSE_MOUNT_MSG
 	se = fuse_lowlevel_new(&args, &erofsfuse_lops, sizeof(erofsfuse_lops),
 			       NULL);
@@ -743,6 +753,8 @@ int main(int argc, char *argv[])
 	fuse_unmount(opts.mountpoint, ch);
 #endif
 
+err_packedinode:
+	erofs_packedfile_exit(&g_sbi);
 err_super_put:
 	erofs_put_super(&g_sbi);
 err_dev_close:
include/erofs/fragments.h +3 -0
index e92b7c7..14a1b4a 100644
@@ -27,6 +27,9 @@ FILE *erofs_packedfile(struct erofs_sb_info *sbi);
 int erofs_packedfile_init(struct erofs_sb_info *sbi, bool fragments_mkfs);
 void erofs_packedfile_exit(struct erofs_sb_info *sbi);
 
+int erofs_packedfile_read(struct erofs_sb_info *sbi,
+			  void *buf, erofs_off_t len, erofs_off_t pos);
+
 #ifdef __cplusplus
 }
 #endif
lib/data.c +2 -12
index 8033208..fd9c21a 100644
@@ -8,6 +8,7 @@
 #include "erofs/internal.h"
 #include "erofs/trace.h"
 #include "erofs/decompress.h"
+#include "erofs/fragments.h"
 
 static int erofs_map_blocks_flatmode(struct erofs_inode *inode,
 				     struct erofs_map_blocks *map,
@@ -248,18 +249,7 @@ int z_erofs_read_one_data(struct erofs_inode *inode,
 	int ret = 0;
 
 	if (map->m_flags & EROFS_MAP_FRAGMENT) {
-		struct erofs_inode packed_inode = {
-			.sbi = sbi,
-			.nid = sbi->packed_nid,
-		};
-
-		ret = erofs_read_inode_from_disk(&packed_inode);
-		if (ret) {
-			erofs_err("failed to read packed inode from disk");
-			return ret;
-		}
-
-		return erofs_pread(&packed_inode, buffer, length - skip,
+		return erofs_packedfile_read(sbi, buffer, length - skip,
 				   inode->fragmentoff + skip);
 	}
 
lib/fragments.c +192 -0

219 lines not on this page · raw message

Signature · 1 line
2.43.5