erofs-utils: lib: get rid of global sbi in lib/inode.c
authorGao Xiang <hsiangkao@linux.alibaba.com>
Wed, 12 Jun 2024 16:18:26 +0000 (00:18 +0800)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Thu, 13 Jun 2024 02:44:24 +0000 (10:44 +0800)
In order to prepare for incremental builds.

Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Link: https://lore.kernel.org/r/20240612161826.711279-5-hsiangkao@linux.alibaba.com
include/erofs/inode.h
lib/fragments.c
lib/inode.c
lib/rebuild.c
lib/tar.c
mkfs/main.c

index 46d989ce24a87beaf236f269c5b63dd0e3cea96a..3bdc2b42b6398a1c7f25b987d72e04dbabab8128 100644 (file)
@@ -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
 }
index f4c9bd7eb05dd08e4ef73f0299210871b15e19c4..4d5478ff2afac84d317ff67d841f55c4aea52fd1 100644 (file)
@@ -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);
 }
 
index cb314d438650f30b4f3829a246e324786ca34b00..1ec73fe37db58b126210d3221072e98ea8d28a75 100644 (file)
@@ -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);
@@ -1598,11 +1596,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
@@ -1610,20 +1608,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;
        }
@@ -1677,14 +1676,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);
@@ -1694,12 +1696,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;
@@ -1714,7 +1717,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;
 
index c25d22295cd35c93e65bbb92a295643ec31c3ec4..806d8b2be81da977ab22cfe358e4eb9a61da3cc7 100644 (file)
@@ -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;
index 6202d35f56dafbfb5583ce60bb0910ff56163b95..3a5d484c0c7709f120043f1b5a89f812f7a097ad 100644 (file)
--- 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;
index 1e8ca3cba9c7a2a856b6df6b31b27f235bc14820..1b15bc5c7c1737cefb1d9bf800d6cdd48017608f 100644 (file)
@@ -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);