[PATCH 5/5] erofs-utils: lib: get rid of global sbi in lib/inode.c

Gao Xiang hsiangkao at linux.alibaba.com
Thu Jun 13 02:18:26 AEST 2024


In order to prepare for incremental builds.

Signed-off-by: Gao Xiang <hsiangkao at linux.alibaba.com>
---
 include/erofs/inode.h |  8 +++++---
 lib/fragments.c       |  2 +-
 lib/inode.c           | 47 +++++++++++++++++++++++--------------------
 lib/rebuild.c         |  4 ++--
 lib/tar.c             |  2 +-
 mkfs/main.c           | 37 +++++++++++++++++-----------------
 6 files changed, 53 insertions(+), 47 deletions(-)

diff --git a/include/erofs/inode.h b/include/erofs/inode.h
index 46d989c..3bdc2b4 100644
--- a/include/erofs/inode.h
+++ b/include/erofs/inode.h
@@ -38,9 +38,11 @@ int erofs_rebuild_dump_tree(struct erofs_inode *dir);
 int erofs_init_empty_dir(struct erofs_inode *dir);
 int __erofs_fill_inode(struct erofs_inode *inode, struct stat *st,
 		       const char *path);
-struct erofs_inode *erofs_new_inode(void);
-struct erofs_inode *erofs_mkfs_build_tree_from_path(const char *path);
-struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name);
+struct erofs_inode *erofs_new_inode(struct erofs_sb_info *sbi);
+struct erofs_inode *erofs_mkfs_build_tree_from_path(struct erofs_sb_info *sbi,
+						    const char *path);
+struct erofs_inode *erofs_mkfs_build_special_from_fd(struct erofs_sb_info *sbi,
+						     int fd, const char *name);
 
 #ifdef __cplusplus
 }
diff --git a/lib/fragments.c b/lib/fragments.c
index f4c9bd7..4d5478f 100644
--- a/lib/fragments.c
+++ b/lib/fragments.c
@@ -330,7 +330,7 @@ struct erofs_inode *erofs_mkfs_build_packedfile(void)
 {
 	fflush(packedfile);
 
-	return erofs_mkfs_build_special_from_fd(fileno(packedfile),
+	return erofs_mkfs_build_special_from_fd(&sbi, fileno(packedfile),
 						EROFS_PACKED_INODE);
 }
 
diff --git a/lib/inode.c b/lib/inode.c
index 069484d..40ded5c 100644
--- a/lib/inode.c
+++ b/lib/inode.c
@@ -1011,7 +1011,7 @@ static int erofs_fill_inode(struct erofs_inode *inode, struct stat *st,
 	return 0;
 }
 
-struct erofs_inode *erofs_new_inode(void)
+struct erofs_inode *erofs_new_inode(struct erofs_sb_info *sbi)
 {
 	struct erofs_inode *inode;
 
@@ -1019,8 +1019,8 @@ struct erofs_inode *erofs_new_inode(void)
 	if (!inode)
 		return ERR_PTR(-ENOMEM);
 
-	inode->sbi = &sbi;
-	inode->i_ino[0] = sbi.inos++;	/* inode serial number */
+	inode->sbi = sbi;
+	inode->i_ino[0] = sbi->inos++;	/* inode serial number */
 	inode->i_count = 1;
 	inode->datalayout = EROFS_INODE_FLAT_PLAIN;
 
@@ -1030,17 +1030,14 @@ struct erofs_inode *erofs_new_inode(void)
 	return inode;
 }
 
-/* get the inode from the (source) path */
-static struct erofs_inode *erofs_iget_from_path(const char *path, bool is_src)
+/* get the inode from the source path */
+static struct erofs_inode *erofs_iget_from_srcpath(struct erofs_sb_info *sbi,
+						   const char *path)
 {
 	struct stat st;
 	struct erofs_inode *inode;
 	int ret;
 
-	/* currently, only source path is supported */
-	if (!is_src)
-		return ERR_PTR(-EINVAL);
-
 	ret = lstat(path, &st);
 	if (ret)
 		return ERR_PTR(-errno);
@@ -1057,7 +1054,7 @@ static struct erofs_inode *erofs_iget_from_path(const char *path, bool is_src)
 	}
 
 	/* cannot find in the inode cache */
-	inode = erofs_new_inode();
+	inode = erofs_new_inode(sbi);
 	if (IS_ERR(inode))
 		return inode;
 
@@ -1293,6 +1290,7 @@ static void erofs_mkfs_flushjobs(struct erofs_sb_info *sbi)
 
 static int erofs_mkfs_handle_directory(struct erofs_inode *dir)
 {
+	struct erofs_sb_info *sbi = dir->sbi;
 	DIR *_dir;
 	struct dirent *dp;
 	struct erofs_dentry *d;
@@ -1344,7 +1342,7 @@ static int erofs_mkfs_handle_directory(struct erofs_inode *dir)
 		if (ret < 0 || ret >= PATH_MAX)
 			goto err_closedir;
 
-		inode = erofs_iget_from_path(buf, true);
+		inode = erofs_iget_from_srcpath(sbi, buf);
 		if (IS_ERR(inode)) {
 			ret = PTR_ERR(inode);
 			goto err_closedir;
@@ -1375,7 +1373,7 @@ static int erofs_mkfs_handle_directory(struct erofs_inode *dir)
 	else
 		dir->i_nlink = i_nlink;
 
-	return erofs_mkfs_go(dir->sbi, EROFS_MKFS_JOB_DIR, &dir, sizeof(dir));
+	return erofs_mkfs_go(sbi, EROFS_MKFS_JOB_DIR, &dir, sizeof(dir));
 
 err_closedir:
 	closedir(_dir);
@@ -1597,11 +1595,11 @@ static int erofs_mkfs_dump_tree(struct erofs_inode *root, bool rebuild)
 }
 
 struct erofs_mkfs_buildtree_ctx {
+	struct erofs_sb_info *sbi;
 	union {
 		const char *path;
 		struct erofs_inode *root;
 	} u;
-	bool from_path;
 };
 #ifndef EROFS_MT_ENABLED
 #define __erofs_mkfs_build_tree erofs_mkfs_build_tree
@@ -1609,20 +1607,21 @@ struct erofs_mkfs_buildtree_ctx {
 
 static int __erofs_mkfs_build_tree(struct erofs_mkfs_buildtree_ctx *ctx)
 {
+	bool from_path = !!ctx->sbi;
 	struct erofs_inode *root;
 	int err;
 
-	if (ctx->from_path) {
-		root = erofs_iget_from_path(ctx->u.path, true);
+	if (from_path) {
+		root = erofs_iget_from_srcpath(ctx->sbi, ctx->u.path);
 		if (IS_ERR(root))
 			return PTR_ERR(root);
 	} else {
 		root = ctx->u.root;
 	}
 
-	err = erofs_mkfs_dump_tree(root, !ctx->from_path);
+	err = erofs_mkfs_dump_tree(root, !from_path);
 	if (err) {
-		if (ctx->from_path)
+		if (from_path)
 			erofs_iput(root);
 		return err;
 	}
@@ -1676,14 +1675,17 @@ fail:
 }
 #endif
 
-struct erofs_inode *erofs_mkfs_build_tree_from_path(const char *path)
+struct erofs_inode *erofs_mkfs_build_tree_from_path(struct erofs_sb_info *sbi,
+						    const char *path)
 {
 	struct erofs_mkfs_buildtree_ctx ctx = {
-		.from_path = true,
+		.sbi = sbi,
 		.u.path = path,
 	};
 	int err;
 
+	if (!sbi)
+		return ERR_PTR(-EINVAL);
 	err = erofs_mkfs_build_tree(&ctx);
 	if (err)
 		return ERR_PTR(err);
@@ -1693,12 +1695,13 @@ struct erofs_inode *erofs_mkfs_build_tree_from_path(const char *path)
 int erofs_rebuild_dump_tree(struct erofs_inode *root)
 {
 	return erofs_mkfs_build_tree(&((struct erofs_mkfs_buildtree_ctx) {
-		.from_path = false,
+		.sbi = NULL,
 		.u.root = root,
 	}));
 }
 
-struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name)
+struct erofs_inode *erofs_mkfs_build_special_from_fd(struct erofs_sb_info *sbi,
+						     int fd, const char *name)
 {
 	struct stat st;
 	struct erofs_inode *inode;
@@ -1713,7 +1716,7 @@ struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name)
 	if (ret)
 		return ERR_PTR(-errno);
 
-	inode = erofs_new_inode();
+	inode = erofs_new_inode(sbi);
 	if (IS_ERR(inode))
 		return inode;
 
diff --git a/lib/rebuild.c b/lib/rebuild.c
index c25d222..806d8b2 100644
--- a/lib/rebuild.c
+++ b/lib/rebuild.c
@@ -31,7 +31,7 @@ static struct erofs_dentry *erofs_rebuild_mkdir(struct erofs_inode *dir,
 	struct erofs_inode *inode;
 	struct erofs_dentry *d;
 
-	inode = erofs_new_inode();
+	inode = erofs_new_inode(dir->sbi);
 	if (IS_ERR(inode))
 		return ERR_CAST(inode);
 
@@ -296,7 +296,7 @@ static int erofs_rebuild_dirent_iter(struct erofs_dir_context *ctx)
 		u64 nid;
 
 		DBG_BUGON(parent != d->inode);
-		inode = erofs_new_inode();
+		inode = erofs_new_inode(dir->sbi);
 		if (IS_ERR(inode)) {
 			ret = PTR_ERR(inode);
 			goto out;
diff --git a/lib/tar.c b/lib/tar.c
index 6202d35..3a5d484 100644
--- a/lib/tar.c
+++ b/lib/tar.c
@@ -937,7 +937,7 @@ restart:
 		inode = d->inode;
 	} else {
 new_inode:
-		inode = erofs_new_inode();
+		inode = erofs_new_inode(sbi);
 		if (IS_ERR(inode)) {
 			ret = PTR_ERR(inode);
 			goto out;
diff --git a/mkfs/main.c b/mkfs/main.c
index 1e8ca3c..1b15bc5 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -1035,11 +1035,12 @@ void erofs_show_progs(int argc, char *argv[])
 	if (cfg.c_dbg_lvl >= EROFS_WARN)
 		printf("%s %s\n", basename(argv[0]), cfg.c_version);
 }
-static struct erofs_inode *erofs_alloc_root_inode(void)
+
+static struct erofs_inode *erofs_mkfs_alloc_root(struct erofs_sb_info *sbi)
 {
 	struct erofs_inode *root;
 
-	root = erofs_new_inode();
+	root = erofs_new_inode(sbi);
 	if (IS_ERR(root))
 		return root;
 	root->i_srcpath = strdup("/");
@@ -1135,7 +1136,7 @@ int main(int argc, char **argv)
 {
 	int err = 0;
 	struct erofs_buffer_head *sb_bh;
-	struct erofs_inode *root_inode, *packed_inode;
+	struct erofs_inode *root, *packed_inode;
 	erofs_blk_t nblocks;
 	struct timeval t;
 	FILE *packedfile = NULL;
@@ -1297,31 +1298,31 @@ int main(int argc, char **argv)
 	erofs_inode_manager_init();
 
 	if (tar_mode) {
-		root_inode = erofs_alloc_root_inode();
-		if (IS_ERR(root_inode)) {
-			err = PTR_ERR(root_inode);
+		root = erofs_mkfs_alloc_root(&sbi);
+		if (IS_ERR(root)) {
+			err = PTR_ERR(root);
 			goto exit;
 		}
 
-		while (!(err = tarerofs_parse_tar(root_inode, &erofstar)));
+		while (!(err = tarerofs_parse_tar(root, &erofstar)));
 
 		if (err < 0)
 			goto exit;
 
-		err = erofs_rebuild_dump_tree(root_inode);
+		err = erofs_rebuild_dump_tree(root);
 		if (err < 0)
 			goto exit;
 	} else if (rebuild_mode) {
-		root_inode = erofs_alloc_root_inode();
-		if (IS_ERR(root_inode)) {
-			err = PTR_ERR(root_inode);
+		root = erofs_mkfs_alloc_root(&sbi);
+		if (IS_ERR(root)) {
+			err = PTR_ERR(root);
 			goto exit;
 		}
 
-		err = erofs_rebuild_load_trees(root_inode);
+		err = erofs_rebuild_load_trees(root);
 		if (err)
 			goto exit;
-		err = erofs_rebuild_dump_tree(root_inode);
+		err = erofs_rebuild_dump_tree(root);
 		if (err)
 			goto exit;
 	} else {
@@ -1335,14 +1336,14 @@ int main(int argc, char **argv)
 		if (cfg.c_extra_ea_name_prefixes)
 			erofs_xattr_write_name_prefixes(&sbi, packedfile);
 
-		root_inode = erofs_mkfs_build_tree_from_path(cfg.c_src_path);
-		if (IS_ERR(root_inode)) {
-			err = PTR_ERR(root_inode);
+		root = erofs_mkfs_build_tree_from_path(&sbi, cfg.c_src_path);
+		if (IS_ERR(root)) {
+			err = PTR_ERR(root);
 			goto exit;
 		}
 	}
-	sbi.root_nid = erofs_lookupnid(root_inode);
-	erofs_iput(root_inode);
+	sbi.root_nid = erofs_lookupnid(root);
+	erofs_iput(root);
 
 	if (erofstar.index_mode && sbi.extra_devices && !erofstar.mapfile)
 		sbi.devs[0].blocks = BLK_ROUND_UP(&sbi, erofstar.offset);
-- 
2.39.3



More information about the Linux-erofs mailing list