Skip to contentSkip to conversation

erofs-utils: lib: introduce erofs_importer_flush_all()

PATCH New Gao Xiang 1 participant 3 messages Aug 20, 2025 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/3 erofs-utils: lib: introduce erofs_importer_flush_all()

As a new formal API for external users, it flushes all dirty metadata except the superblock.

---
 include/erofs/cache.h    |  1 +
 include/erofs/importer.h |  2 ++
 lib/importer.c           | 39 +++++++++++++++++++++++++++++++++++++++
 mkfs/main.c              | 29 ++---------------------------
 4 files changed, 44 insertions(+), 27 deletions(-)
include/erofs/cache.h +1 -0
index d5618c0..c248b73 100644
@@ -13,6 +13,7 @@ extern "C"
 {
 #endif
 
+#include <stdlib.h>
 #include "internal.h"
 
 struct erofs_buffer_head;
include/erofs/importer.h +2 -0
index e83c3e3..28d29bd 100644
@@ -19,6 +19,7 @@ struct erofs_importer_params {
 	u32 fixed_gid;
 	u32 uid_offset;
 	u32 gid_offset;
+	u32 fsalignblks;
 	bool no_datainline;
 	bool hard_dereference;
 	bool ovlfs_strip;
@@ -33,6 +34,7 @@ struct erofs_importer {
 
 void erofs_importer_preset(struct erofs_importer_params *params);
 int erofs_importer_init(struct erofs_importer *im);
+int erofs_importer_flush_all(struct erofs_importer *im);
 void erofs_importer_exit(struct erofs_importer *im);
 
 #ifdef __cplusplus
lib/importer.c +39 -0
index 8e86993..a65fa39 100644
@@ -4,6 +4,7 @@
  */
 #include "erofs/fragments.h"
 #include "erofs/importer.h"
+#include "erofs/cache.h"
 #include "erofs/config.h"
 #include "erofs/dedupe.h"
 #include "erofs/inode.h"
@@ -19,6 +20,7 @@ void erofs_importer_preset(struct erofs_importer_params *params)
 	*params = (struct erofs_importer_params) {
 		.fixed_uid = -1,
 		.fixed_gid = -1,
+		.fsalignblks = 1,
 	};
 }
 
@@ -72,6 +74,43 @@ out_err:
 	return err;
 }
 
+int erofs_importer_flush_all(struct erofs_importer *im)
+{
+	struct erofs_sb_info *sbi = im->sbi;
+	unsigned int fsalignblks;
+	int err;
+
+	if (erofs_sb_has_metabox(sbi)) {
+		erofs_update_progressinfo("Handling metabox ...");
+		err = erofs_metabox_iflush(im);
+		if (err)
+			return err;
+	}
+
+	if ((cfg.c_fragments || cfg.c_extra_ea_name_prefixes) &&
+	    erofs_sb_has_fragments(sbi)) {
+		erofs_update_progressinfo("Handling packed data ...");
+		err = erofs_flush_packed_inode(im);
+		if (err)
+			return err;
+	}
+
+	fsalignblks = im->params->fsalignblks ?
+		roundup_pow_of_two(im->params->fsalignblks) : 1;
+	sbi->primarydevice_blocks = roundup(erofs_mapbh(sbi->bmgr, NULL),
+					    fsalignblks);
+	err = erofs_write_device_table(sbi);
+	if (err)
+		return err;
+
+	/* flush all buffers except for the superblock */
+	err = erofs_bflush(sbi->bmgr, NULL);
+	if (err)
+		return err;
+
+	return erofs_fixup_root_inode(im->root);
+}
+
 void erofs_importer_exit(struct erofs_importer *im)
 {
 	struct erofs_sb_info *sbi = im->sbi;
mkfs/main.c +2 -27
index a11134e..0a8f477 100644
@@ -281,7 +281,6 @@ static LIST_HEAD(rebuild_src_list);
 static u8 fixeduuid[16];
 static bool valid_fixeduuid;
 static unsigned int dsunit;
-static unsigned int fsalignblks = 1;
 static int tarerofs_decoder;
 static FILE *vmdk_dcf;
 
@@ -1203,7 +1202,7 @@ static int mkfs_parse_options_cfg(struct erofs_importer_params *params,
 			break;
 #endif
 		case 531:
-			fsalignblks = strtoul(optarg, &endptr, 0);
+			params->fsalignblks = strtoul(optarg, &endptr, 0);
 			if (*endptr != '\0') {
 				erofs_err("invalid fsalignblks %s", optarg);
 				return -EINVAL;
@@ -1760,39 +1759,15 @@ int main(int argc, char **argv)
 		}
 	}
 
-	if (erofs_sb_has_metabox(&g_sbi)) {
-		erofs_update_progressinfo("Handling metabox ...");
-		erofs_metabox_iflush(&importer);
-		if (err)
-			goto exit;
-	}
-
-	if ((cfg.c_fragments || cfg.c_extra_ea_name_prefixes) &&
-	    erofs_sb_has_fragments(&g_sbi)) {
-		erofs_update_progressinfo("Handling packed data ...");
-		err = erofs_flush_packed_inode(&importer);
-		if (err)
-			goto exit;
-	}
-
 	if (erofstar.index_mode || cfg.c_chunkbits || g_sbi.extra_devices) {
 		err = erofs_mkfs_dump_blobs(&g_sbi);
 		if (err)
 			goto exit;
 	}
 
-	g_sbi.primarydevice_blocks =
-		roundup(erofs_mapbh(g_sbi.bmgr, NULL), fsalignblks);
-	err = erofs_write_device_table(&g_sbi);
-	if (err)
-		goto exit;
-
-	/* flush all buffers except for the superblock */
-	err = erofs_bflush(g_sbi.bmgr, NULL);
+	err = erofs_importer_flush_all(&importer);
 	if (err)
 		goto exit;
-
-	erofs_fixup_root_inode(root);
 	erofs_iput(root);
 	root = NULL;
 
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 2/3 erofs-utils: lib: introduce API helpers to prepare mkfs context

- erofs_mkfs_format_fs Create a new filesystem

- erofs_mkfs_load_fs Load an existing filesystem, especially for

                         incremental builds;

Unlike importer APIs (designed for multiple data sources), this can only be executed once.

---
 include/erofs/compress.h |  3 +-
 include/erofs/internal.h |  6 ++-
 lib/cache.c              |  1 +
 lib/compress.c           |  5 +-
 lib/importer.c           |  4 ++
 lib/super.c              | 55 +++++++++++++++++++++-
 mkfs/main.c              | 99 ++++++++++++----------------------------
 7 files changed, 98 insertions(+), 75 deletions(-)
include/erofs/compress.h +1 -2
index d5b2519..00e7715 100644
@@ -23,8 +23,7 @@ void z_erofs_drop_inline_pcluster(struct erofs_inode *inode);
 void *erofs_begin_compressed_file(struct erofs_inode *inode, int fd, u64 fpos);
 int erofs_write_compressed_file(struct z_erofs_compress_ictx *ictx);
 
-int z_erofs_compress_init(struct erofs_sb_info *sbi,
-			  struct erofs_buffer_head *bh);
+int z_erofs_compress_init(struct erofs_sb_info *sbi);
 int z_erofs_compress_exit(struct erofs_sb_info *sbi);
 
 const char *z_erofs_list_supported_algorithms(int i, unsigned int *mask);
include/erofs/internal.h +5 -1
index 92e83fd..a609fbd 100644
@@ -150,6 +150,7 @@ struct erofs_sb_info {
 	struct z_erofs_mgr *zmgr;
 	struct erofs_metaboxmgr *m2gr;
 	struct erofs_packed_inode *packedinode;
+	struct erofs_buffer_head *bh_sb;
 	struct erofs_buffer_head *bh_devt;
 	bool useqpl;
 };
@@ -432,12 +433,15 @@ void liberofs_global_exit(void);
 /* super.c */
 int erofs_read_superblock(struct erofs_sb_info *sbi);
 void erofs_put_super(struct erofs_sb_info *sbi);
-int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh);
+int erofs_writesb(struct erofs_sb_info *sbi);
 struct erofs_buffer_head *erofs_reserve_sb(struct erofs_bufmgr *bmgr);
 int erofs_mkfs_init_devices(struct erofs_sb_info *sbi, unsigned int devices);
 int erofs_write_device_table(struct erofs_sb_info *sbi);
 int erofs_enable_sb_chksum(struct erofs_sb_info *sbi, u32 *crc);
 int erofs_superblock_csum_verify(struct erofs_sb_info *sbi);
+int erofs_mkfs_format_fs(struct erofs_sb_info *sbi,
+			 unsigned int blkszbits, unsigned int dsunit);
+int erofs_mkfs_load_fs(struct erofs_sb_info *sbi, unsigned int dsunit);
 
 /* namei.c */
 int erofs_read_inode_from_disk(struct erofs_inode *vi);
lib/cache.c +1 -0
index 079465e..cd11737 100644
@@ -533,5 +533,6 @@ erofs_blk_t erofs_total_metablocks(struct erofs_bufmgr *bmgr)
 
 void erofs_buffer_exit(struct erofs_bufmgr *bmgr)
 {
+	DBG_BUGON(!list_empty(&bmgr->blkh.list));
 	free(bmgr);
 }
lib/compress.c +3 -2
index 0bfad3f..0049199 100644
@@ -2035,11 +2035,12 @@ static int z_erofs_build_compr_cfgs(struct erofs_sb_info *sbi,
 	return ret;
 }
 
-int z_erofs_compress_init(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh)
+int z_erofs_compress_init(struct erofs_sb_info *sbi)
 {
-	int i, ret, id;
+	struct erofs_buffer_head *sb_bh = sbi->bh_sb;
 	u32 max_dict_size[Z_EROFS_COMPRESSION_MAX] = {};
 	u32 available_compr_algs = 0;
+	int i, ret, id;
 
 	if (!sbi->zmgr) {
 		sbi->zmgr = calloc(1, sizeof(*sbi->zmgr));
lib/importer.c +4 -0
index a65fa39..95f006d 100644
@@ -39,6 +39,7 @@ void erofs_importer_global_init(void)
 int erofs_importer_init(struct erofs_importer *im)
 {
 	struct erofs_sb_info *sbi = im->sbi;
+	struct erofs_importer_params *params = im->params;
 	const char *subsys = NULL;
 	int err;
 
@@ -67,6 +68,9 @@ int erofs_importer_init(struct erofs_importer *im)
 		if (err)
 			goto out_err;
 	}
+
+	if (params->dot_omitted)
+		erofs_sb_set_48bit(sbi);
 	return 0;
 
 out_err:
lib/super.c +54 -1

65 lines not on this page · raw message

mkfs/main.c +30 -69

197 lines not on this page · raw message

Signature · 1 line
2.43.5
author
#2 lore raw

PATCH 3/3 erofs-utils: lib: unexport `erofs/cache.h`

In reply to Gao Xiang

It shouldn't be used by external users.

---
 include/erofs/internal.h                      |  3 +++
 lib/Makefile.am                               |  2 +-
 lib/blobchunk.c                               |  2 +-
 lib/cache.c                                   |  2 +-
 lib/compress.c                                |  2 +-
 lib/importer.c                                |  2 +-
 lib/inode.c                                   |  2 +-
 include/erofs/cache.h => lib/liberofs_cache.h | 12 +++++-------
 lib/metabox.c                                 |  2 +-
 lib/super.c                                   |  2 +-
 lib/tar.c                                     |  2 +-
 lib/xattr.c                                   |  2 +-
 mkfs/main.c                                   |  1 -
 13 files changed, 18 insertions(+), 18 deletions(-)
 rename include/erofs/cache.h => lib/liberofs_cache.h (92%)
include/erofs/internal.h +3 -0
index a609fbd..ea58584 100644
@@ -537,6 +537,9 @@ static inline int erofs_blk_read(struct erofs_sb_info *sbi, int device_id,
 /* vmdk.c */
 int erofs_dump_vmdk_desc(FILE *f, struct erofs_sb_info *sbi);
 
+/* XXX: will find a better way later */
+erofs_blk_t erofs_total_metablocks(struct erofs_bufmgr *bmgr);
+
 #ifdef EUCLEAN
 #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
 #else
lib/Makefile.am +1 -1
index f485440..955495d 100644
@@ -4,7 +4,6 @@ noinst_LTLIBRARIES = liberofs.la
 noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
       $(top_srcdir)/include/erofs/blobchunk.h \
       $(top_srcdir)/include/erofs/block_list.h \
-      $(top_srcdir)/include/erofs/cache.h \
       $(top_srcdir)/include/erofs/compress.h \
       $(top_srcdir)/include/erofs/config.h \
       $(top_srcdir)/include/erofs/decompress.h \
@@ -28,6 +27,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
       $(top_srcdir)/include/erofs/fragments.h \
       $(top_srcdir)/include/erofs/rebuild.h \
       $(top_srcdir)/include/erofs/importer.h \
+      $(top_srcdir)/lib/liberofs_cache.h \
       $(top_srcdir)/lib/liberofs_private.h \
       $(top_srcdir)/lib/liberofs_xxhash.h \
       $(top_srcdir)/lib/liberofs_metabox.h \
lib/blobchunk.c +1 -1
index 157b9a9..af6ddd7 100644
@@ -8,7 +8,7 @@
 #include "erofs/hashmap.h"
 #include "erofs/blobchunk.h"
 #include "erofs/block_list.h"
-#include "erofs/cache.h"
+#include "liberofs_cache.h"
 #include "liberofs_private.h"
 #include "sha256.h"
 #include <unistd.h>
lib/cache.c +1 -1
index cd11737..24449f2 100644
@@ -6,9 +6,9 @@
  * with heavy changes by Gao Xiang <[email protected]>
  */
 #include <stdlib.h>
-#include <erofs/cache.h>
 #include <erofs/bitops.h>
 #include "erofs/print.h"
+#include "liberofs_cache.h"
 
 static int erofs_bh_flush_drop_directly(struct erofs_buffer_head *bh)
 {
lib/compress.c +1 -1
index 0049199..622a205 100644
@@ -15,7 +15,6 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include "erofs/print.h"
-#include "erofs/cache.h"
 #include "erofs/compress.h"
 #include "erofs/dedupe.h"
 #include "compressor.h"
@@ -25,6 +24,7 @@
 #ifdef EROFS_MT_ENABLED
 #include "erofs/workqueue.h"
 #endif
+#include "liberofs_cache.h"
 #include "liberofs_metabox.h"
 
 #define Z_EROFS_DESTBUF_SZ	(Z_EROFS_PCLUSTER_MAX_SIZE + EROFS_MAX_BLOCK_SIZE * 2)
lib/importer.c +1 -1
index 95f006d..9c57c07 100644
@@ -4,12 +4,12 @@
  */
 #include "erofs/fragments.h"
 #include "erofs/importer.h"
-#include "erofs/cache.h"
 #include "erofs/config.h"
 #include "erofs/dedupe.h"
 #include "erofs/inode.h"
 #include "erofs/print.h"
 #include "erofs/lock.h"
+#include "liberofs_cache.h"
 #include "liberofs_metabox.h"
 
 static EROFS_DEFINE_MUTEX(erofs_importer_global_mutex);
lib/inode.c +1 -1
index 302492c..7ee6b3d 100644
@@ -19,7 +19,6 @@
 #include "erofs/lock.h"
 #include "erofs/diskbuf.h"
 #include "erofs/inode.h"
-#include "erofs/cache.h"
 #include "erofs/compress.h"
 #include "erofs/xattr.h"
 #include "erofs/exclude.h"
@@ -28,6 +27,7 @@
 #include "erofs/blobchunk.h"
 #include "erofs/fragments.h"
 #include "erofs/importer.h"
+#include "liberofs_cache.h"
 #include "liberofs_private.h"
 #include "liberofs_metabox.h"
 
lib/liberofs_cache.h renamed from include/erofs/cache.h +5 -7
similarity index 92%
rename from include/erofs/cache.h
rename to lib/liberofs_cache.h
index c248b73..215b320 100644
@@ -1,12 +1,11 @@
 /* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
 /*
  * Copyright (C) 2018 HUAWEI, Inc.
- *             http://www.huawei.com/
- * Created by Miao Xie <[email protected]>
- * with heavy changes by Gao Xiang <[email protected]>
+ *             http://www.huawei.com
+ * Copyright (C) 2025 Alibaba Cloud
  */
-#ifndef __EROFS_CACHE_H
-#define __EROFS_CACHE_H
+#ifndef __EROFS_LIB_LIBEROFS_CACHE_H
+#define __EROFS_LIB_LIBEROFS_CACHE_H
 
 #ifdef __cplusplus
 extern "C"
@@ -14,7 +13,7 @@ extern "C"
 #endif
 
 #include <stdlib.h>
-#include "internal.h"
+#include "erofs/internal.h"
 
 struct erofs_buffer_head;
 struct erofs_buffer_block;
@@ -139,7 +138,6 @@ int erofs_bflush(struct erofs_bufmgr *bmgr,
 		 struct erofs_buffer_block *bb);
 
 void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke);
-erofs_blk_t erofs_total_metablocks(struct erofs_bufmgr *bmgr);
 void erofs_buffer_exit(struct erofs_bufmgr *bmgr);
 
 #ifdef __cplusplus
lib/metabox.c +1 -1
index c9db9ac..fdc46eb 100644
@@ -1,8 +1,8 @@
 // SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0
 #include <stdlib.h>
-#include "erofs/cache.h"
 #include "erofs/inode.h"
 #include "erofs/importer.h"
+#include "liberofs_cache.h"
 #include "liberofs_private.h"
 #include "liberofs_metabox.h"
 
lib/super.c +1 -1
index 97d955f..a9ec3aa 100644
@@ -6,7 +6,7 @@
 #include <stdlib.h>
 #include "erofs/print.h"
 #include "erofs/xattr.h"
-#include "erofs/cache.h"
+#include "liberofs_cache.h"
 
 static bool check_layout_compatibility(struct erofs_sb_info *sbi,
 				       struct erofs_super_block *dsb)
lib/tar.c +1 -1
index 95129b3..fe801e2 100644
@@ -4,7 +4,6 @@
 #include <string.h>
 #include <sys/stat.h>
 #include "erofs/print.h"
-#include "erofs/cache.h"
 #include "erofs/diskbuf.h"
 #include "erofs/inode.h"
 #include "erofs/list.h"
@@ -16,6 +15,7 @@
 #if defined(HAVE_ZLIB)
 #include <zlib.h>
 #endif
+#include "liberofs_cache.h"
 
 /* This file is a tape/volume header.  Ignore it on extraction.  */
 #define GNUTYPE_VOLHDR 'V'
lib/xattr.c +1 -1
index 1f6a83f..114e2bb 100644
@@ -15,8 +15,8 @@
 #include "erofs/print.h"
 #include "erofs/hashtable.h"
 #include "erofs/xattr.h"
-#include "erofs/cache.h"
 #include "erofs/fragments.h"
+#include "liberofs_cache.h"
 #include "liberofs_xxhash.h"
 #include "liberofs_private.h"
 
mkfs/main.c +0 -1
index d2950b7..e0ba55d 100644
@@ -15,7 +15,6 @@
 #include <getopt.h>
 #include "erofs/config.h"
 #include "erofs/print.h"
-#include "erofs/cache.h"
 #include "erofs/importer.h"
 #include "erofs/diskbuf.h"
 #include "erofs/inode.h"
Signature · 1 line
2.43.5