erofs-utils: lib: add erofs_rebuild_load_tree() helper
authorJingbo Xu <jefflexu@linux.alibaba.com>
Wed, 13 Sep 2023 12:03:01 +0000 (20:03 +0800)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Thu, 14 Sep 2023 09:45:11 +0000 (17:45 +0800)
Add erofs_rebuild_load_tree() helper to load the inode tree from a
given erofs image, and make it merged acting as an overlayfs-like
model.

Since the content of the symlink file needs to be read when loading
tree, let's add a dependency on zlib_LIBS for mkfs.erofs.

Also rename tarerofs_dump_tree() to erofs_rebuild_dump_tree() since
it is also called in the rebuild mode.

Signed-off-by: Jingbo Xu <jefflexu@linux.alibaba.com>
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Link: https://lore.kernel.org/r/20230913120304.15741-7-jefflexu@linux.alibaba.com
include/erofs/inode.h
include/erofs/internal.h
include/erofs/rebuild.h
lib/inode.c
lib/rebuild.c
mkfs/main.c

index 1c602a8db33902cb9e6641efe9ffc364d33cbad1..fe9dda272fc1ab72b79f1cb6f705bd9735cf1c45 100644 (file)
@@ -32,7 +32,7 @@ unsigned int erofs_iput(struct erofs_inode *inode);
 erofs_nid_t erofs_lookupnid(struct erofs_inode *inode);
 struct erofs_dentry *erofs_d_alloc(struct erofs_inode *parent,
                                   const char *name);
-int tarerofs_dump_tree(struct erofs_inode *dir);
+int erofs_rebuild_dump_tree(struct erofs_inode *dir);
 int erofs_init_empty_dir(struct erofs_inode *dir);
 struct erofs_inode *erofs_new_inode(void);
 struct erofs_inode *erofs_mkfs_build_tree_from_path(const char *path);
index 3b866c9397191b6ec3fd2eb23e995136ddd89b0f..19b912bb8299c45e4e6f6fba5a950e905637058e 100644 (file)
@@ -109,6 +109,7 @@ struct erofs_sb_info {
 
        int devfd;
        u64 devsz;
+       dev_t dev;
        unsigned int nblobs;
        unsigned int blobfd[256];
 
@@ -155,8 +156,6 @@ struct erofs_inode {
        union {
                /* (erofsfuse) runtime flags */
                unsigned int flags;
-               /* (mkfs.erofs) device ID containing source file */
-               u32 dev;
                /* (mkfs.erofs) queued sub-directories blocking dump */
                u32 subdirs_queued;
        };
@@ -164,6 +163,9 @@ struct erofs_inode {
        struct erofs_sb_info *sbi;
        struct erofs_inode *i_parent;
 
+       /* (mkfs.erofs) device ID containing source file */
+       u32 dev;
+
        umode_t i_mode;
        erofs_off_t i_size;
 
index 92873c97aa1d2453f8f77f20db1040fa0d7bcfe4..3ac074ce1969b615cb8576797ad4d0e36f975358 100644 (file)
@@ -12,6 +12,8 @@ extern "C"
 struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
                char *path, bool aufs, bool *whout, bool *opq);
 
+int erofs_rebuild_load_tree(struct erofs_inode *root, struct erofs_sb_info *sbi);
+
 #ifdef __cplusplus
 }
 #endif
index 79e17954e9aeab720100ebc8039b6e8bd03df332..93e6b232e1fb8fe1f6178f83564639d4709ddb1c 100644 (file)
@@ -1324,7 +1324,7 @@ struct erofs_inode *erofs_mkfs_build_special_from_fd(int fd, const char *name)
        return inode;
 }
 
-int tarerofs_dump_tree(struct erofs_inode *dir)
+int erofs_rebuild_dump_tree(struct erofs_inode *dir)
 {
        struct erofs_dentry *d;
        unsigned int nr_subdirs;
@@ -1395,7 +1395,7 @@ int tarerofs_dump_tree(struct erofs_inode *dir)
                        continue;
 
                inode = erofs_igrab(d->inode);
-               ret = tarerofs_dump_tree(inode);
+               ret = erofs_rebuild_dump_tree(inode);
                dir->i_nlink += (erofs_mode_to_ftype(inode->i_mode) == EROFS_FT_DIR);
                erofs_iput(inode);
                if (ret)
index 2398ca623bc420fe62f9cc9b74e1b45c9affe288..27a1df45eb94178ce9d1ad6842c92301f1e0fc8f 100644 (file)
@@ -3,9 +3,18 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <config.h>
+#if defined(HAVE_SYS_SYSMACROS_H)
+#include <sys/sysmacros.h>
+#endif
 #include "erofs/print.h"
 #include "erofs/inode.h"
 #include "erofs/rebuild.h"
+#include "erofs/io.h"
+#include "erofs/dir.h"
+#include "erofs/xattr.h"
+#include "erofs/blobchunk.h"
 #include "erofs/internal.h"
 
 #ifdef HAVE_LINUX_AUFS_TYPE_H
@@ -114,3 +123,282 @@ struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
        }
        return d;
 }
+
+static int erofs_rebuild_fixup_inode_index(struct erofs_inode *inode)
+{
+       int ret;
+       unsigned int count, unit, chunkbits, i;
+       struct erofs_inode_chunk_index *idx;
+       erofs_off_t chunksize;
+       erofs_blk_t blkaddr;
+
+       /* TODO: fill data map in other layouts */
+       if (inode->datalayout != EROFS_INODE_CHUNK_BASED &&
+           inode->datalayout != EROFS_INODE_FLAT_PLAIN) {
+               erofs_err("%s: unsupported datalayout %d", inode->i_srcpath, inode->datalayout);
+               return -EOPNOTSUPP;
+       }
+
+       if (inode->sbi->extra_devices) {
+               chunkbits = inode->u.chunkbits;
+               if (chunkbits < sbi.blkszbits) {
+                       erofs_err("%s: chunk size %u is too small to fit the target block size %u",
+                                 inode->i_srcpath, 1U << chunkbits, 1U << sbi.blkszbits);
+                       return -EINVAL;
+               }
+       } else {
+               chunkbits = ilog2(inode->i_size - 1) + 1;
+               if (chunkbits < sbi.blkszbits)
+                       chunkbits = sbi.blkszbits;
+               if (chunkbits - sbi.blkszbits > EROFS_CHUNK_FORMAT_BLKBITS_MASK)
+                       chunkbits = EROFS_CHUNK_FORMAT_BLKBITS_MASK + sbi.blkszbits;
+       }
+       chunksize = 1ULL << chunkbits;
+       count = DIV_ROUND_UP(inode->i_size, chunksize);
+
+       unit = sizeof(struct erofs_inode_chunk_index);
+       inode->extent_isize = count * unit;
+       idx = malloc(max(sizeof(*idx), sizeof(void *)));
+       if (!idx)
+               return -ENOMEM;
+       inode->chunkindexes = idx;
+
+       for (i = 0; i < count; i++) {
+               struct erofs_blobchunk *chunk;
+               struct erofs_map_blocks map = {
+                       .index = UINT_MAX,
+               };
+
+               map.m_la = i << chunkbits;
+               ret = erofs_map_blocks(inode, &map, 0);
+               if (ret)
+                       goto err;
+
+               blkaddr = erofs_blknr(&sbi, map.m_pa);
+               chunk = erofs_get_unhashed_chunk(inode->dev, blkaddr, 0);
+               if (IS_ERR(chunk)) {
+                       ret = PTR_ERR(chunk);
+                       goto err;
+               }
+               *(void **)idx++ = chunk;
+
+       }
+       inode->datalayout = EROFS_INODE_CHUNK_BASED;
+       inode->u.chunkformat = EROFS_CHUNK_FORMAT_INDEXES;
+       inode->u.chunkformat |= chunkbits - sbi.blkszbits;
+       return 0;
+err:
+       free(idx);
+       inode->chunkindexes = NULL;
+       return ret;
+}
+
+static int erofs_rebuild_fill_inode(struct erofs_inode *inode)
+{
+       switch (inode->i_mode & S_IFMT) {
+       case S_IFCHR:
+               if (erofs_inode_is_whiteout(inode))
+                       inode->i_parent->whiteouts = true;
+               /* fallthrough */
+       case S_IFBLK:
+       case S_IFIFO:
+       case S_IFSOCK:
+               inode->i_size = 0;
+               erofs_dbg("\tdev: %d %d", major(inode->u.i_rdev),
+                         minor(inode->u.i_rdev));
+               inode->u.i_rdev = erofs_new_encode_dev(inode->u.i_rdev);
+               return 0;
+       case S_IFDIR:
+               return erofs_init_empty_dir(inode);
+       case S_IFLNK: {
+               int ret;
+
+               inode->i_link = malloc(inode->i_size + 1);
+               if (!inode->i_link)
+                       return -ENOMEM;
+               ret = erofs_pread(inode, inode->i_link, inode->i_size, 0);
+               erofs_dbg("\tsymlink: %s -> %s", inode->i_srcpath, inode->i_link);
+               return ret;
+       }
+       case S_IFREG:
+               if (inode->i_size)
+                       return erofs_rebuild_fixup_inode_index(inode);
+               return 0;
+       default:
+               break;
+       }
+       return -EINVAL;
+}
+
+/*
+ * @parent:  parent directory in inode tree
+ * @ctx.dir: parent directory when itering erofs_iterate_dir()
+ */
+struct erofs_rebuild_dir_context {
+       struct erofs_dir_context ctx;
+       struct erofs_inode *parent;
+};
+
+static int erofs_rebuild_dirent_iter(struct erofs_dir_context *ctx)
+{
+       struct erofs_rebuild_dir_context *rctx = (void *)ctx;
+       struct erofs_inode *parent = rctx->parent;
+       struct erofs_inode *dir = ctx->dir;
+       struct erofs_inode *inode, *candidate;
+       struct erofs_inode src;
+       struct erofs_dentry *d;
+       char *path, *dname;
+       bool dumb;
+       int ret;
+
+       if (ctx->dot_dotdot)
+               return 0;
+
+       ret = asprintf(&path, "%s/%.*s", rctx->parent->i_srcpath,
+                      ctx->de_namelen, ctx->dname);
+       if (ret < 0)
+               return ret;
+
+       erofs_dbg("parsing %s", path);
+       dname = path + strlen(parent->i_srcpath) + 1;
+
+       d = erofs_rebuild_get_dentry(parent, dname, false, &dumb, &dumb);
+       if (IS_ERR(d)) {
+               ret = PTR_ERR(d);
+               goto out;
+       }
+
+       ret = 0;
+       if (d->type != EROFS_FT_UNKNOWN) {
+               /*
+                * bail out if the file exists in the upper layers.  (Note that
+                * extended attributes won't be merged too even for dirs.)
+                */
+               if (!S_ISDIR(d->inode->i_mode) || d->inode->opaque)
+                       goto out;
+
+               /* merge directory entries */
+               src = (struct erofs_inode) {
+                       .sbi = dir->sbi,
+                       .nid = ctx->de_nid
+               };
+               ret = erofs_read_inode_from_disk(&src);
+               if (ret || !S_ISDIR(src.i_mode))
+                       goto out;
+               parent = d->inode;
+               inode = dir = &src;
+       } else {
+               u64 nid;
+
+               DBG_BUGON(parent != d->inode);
+               inode = erofs_new_inode();
+               if (IS_ERR(inode)) {
+                       ret = PTR_ERR(inode);
+                       goto out;
+               }
+
+               /* reuse i_ino[0] to read nid in source fs */
+               nid = inode->i_ino[0];
+               inode->sbi = dir->sbi;
+               inode->nid = ctx->de_nid;
+               ret = erofs_read_inode_from_disk(inode);
+               if (ret)
+                       goto out;
+
+               /* restore nid in new generated fs */
+               inode->i_ino[1] = inode->i_ino[0];
+               inode->i_ino[0] = nid;
+               inode->dev = inode->sbi->dev;
+
+               if (S_ISREG(inode->i_mode) && inode->i_nlink > 1 &&
+                   (candidate = erofs_iget(inode->dev, ctx->de_nid))) {
+                       /* hardlink file */
+                       erofs_iput(inode);
+                       inode = candidate;
+                       if (S_ISDIR(inode->i_mode)) {
+                               erofs_err("hardlink directory not supported");
+                               ret = -EISDIR;
+                               goto out;
+                       }
+                       inode->i_nlink++;
+                       erofs_dbg("\thardlink: %s -> %s", path, inode->i_srcpath);
+               } else {
+                       ret = erofs_read_xattrs_from_disk(inode);
+                       if (ret) {
+                               erofs_iput(inode);
+                               goto out;
+                       }
+
+                       inode->i_parent = d->inode;
+                       inode->i_srcpath = path;
+                       path = NULL;
+                       inode->i_ino[1] = inode->nid;
+                       inode->i_nlink = 1;
+
+                       ret = erofs_rebuild_fill_inode(inode);
+                       if (ret) {
+                               erofs_iput(inode);
+                               goto out;
+                       }
+
+                       erofs_insert_ihash(inode, inode->dev, inode->i_ino[1]);
+                       parent = dir = inode;
+               }
+
+               d->inode = inode;
+               d->type = erofs_mode_to_ftype(inode->i_mode);
+       }
+
+       if (S_ISDIR(inode->i_mode)) {
+               struct erofs_rebuild_dir_context nctx = *rctx;
+
+               nctx.parent = parent;
+               nctx.ctx.dir = dir;
+               ret = erofs_iterate_dir(&nctx.ctx, false);
+               if (ret)
+                       goto out;
+       }
+
+       /* reset sbi, nid after subdirs are all loaded for the final dump */
+       inode->sbi = &sbi;
+       inode->nid = 0;
+out:
+       free(path);
+       return ret;
+}
+
+int erofs_rebuild_load_tree(struct erofs_inode *root, struct erofs_sb_info *sbi)
+{
+       struct erofs_inode inode = {};
+       struct erofs_rebuild_dir_context ctx;
+       int ret;
+
+       if (!sbi->devname) {
+               erofs_err("failed to find a device for rebuilding");
+               return -EINVAL;
+       }
+
+       ret = erofs_read_superblock(sbi);
+       if (ret) {
+               erofs_err("failed to read superblock of %s", sbi->devname);
+               return ret;
+       }
+
+       inode.nid = sbi->root_nid;
+       inode.sbi = sbi;
+       ret = erofs_read_inode_from_disk(&inode);
+       if (ret) {
+               erofs_err("failed to read root inode of %s", sbi->devname);
+               return ret;
+       }
+       inode.i_srcpath = strdup("/");
+
+       ctx = (struct erofs_rebuild_dir_context) {
+               .ctx.dir = &inode,
+               .ctx.cb = erofs_rebuild_dirent_iter,
+               .parent = root,
+       };
+       ret = erofs_iterate_dir(&ctx.ctx, false);
+       free(inode.i_srcpath);
+       return ret;
+}
index c7f404751015dbe48a1b121efc51cb290c85d055..49b91c55ab3d3fd4a0ab910cc7b120be4cdd857d 100644 (file)
@@ -965,7 +965,7 @@ int main(int argc, char **argv)
                if (err < 0)
                        goto exit;
 
-               err = tarerofs_dump_tree(root_inode);
+               err = erofs_rebuild_dump_tree(root_inode);
                if (err < 0)
                        goto exit;
        }