erofs-utils: lib: add erofs_{rebuild_make_root,enable_sb_chksum}
authorHongzhen Luo <hongzhen@linux.alibaba.com>
Tue, 25 Jun 2024 06:24:58 +0000 (14:24 +0800)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Tue, 25 Jun 2024 07:00:44 +0000 (15:00 +0800)
Move erofs_sb_csum_set() and erofs_mkfs_alloc_root() into liberofs
for external use.

Signed-off-by: Hongzhen Luo <hongzhen@linux.alibaba.com>
Link: https://lore.kernel.org/r/20240625062458.1514209-1-hongzhen@linux.alibaba.com
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
include/erofs/inode.h
include/erofs/internal.h
lib/inode.c
lib/super.c
mkfs/main.c

index 2af8e6cc3a729cf39d5f9aa6db0349cc1ffd1dda..604161c2a39af8326c4d8e914d027b2e6de4f2f8 100644 (file)
@@ -45,6 +45,7 @@ struct erofs_inode *erofs_mkfs_build_tree_from_path(struct erofs_sb_info *sbi,
 struct erofs_inode *erofs_mkfs_build_special_from_fd(struct erofs_sb_info *sbi,
                                                     int fd, const char *name);
 int erofs_fixup_root_inode(struct erofs_inode *root);
+struct erofs_inode *erofs_rebuild_make_root(struct erofs_sb_info *sbi);
 
 #ifdef __cplusplus
 }
index 61250d1cefbd550a95af2ce3c5827b5661080891..d5c5ce25551f0dde692c7de803c9240e99d45836 100644 (file)
@@ -403,6 +403,7 @@ void erofs_put_super(struct erofs_sb_info *sbi);
 int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh,
                  erofs_blk_t *blocks);
 struct erofs_buffer_head *erofs_reserve_sb(void);
+int erofs_enable_sb_chksum(struct erofs_sb_info *sbi, u32 *crc);
 
 /* namei.c */
 int erofs_read_inode_from_disk(struct erofs_inode *vi);
index 703dc63fc92b77152bffe9b3e3bbde96238f8967..3e82af74fa8966e817a3a72cd634e677b1bda687 100644 (file)
@@ -1978,3 +1978,19 @@ int erofs_fixup_root_inode(struct erofs_inode *root)
        free(ibuf);
        return err;
 }
+
+struct erofs_inode *erofs_rebuild_make_root(struct erofs_sb_info *sbi)
+{
+       struct erofs_inode *root;
+
+       root = erofs_new_inode(sbi);
+       if (IS_ERR(root))
+               return root;
+       root->i_srcpath = strdup("/");
+       root->i_mode = S_IFDIR | 0777;
+       root->i_parent = root;
+       root->i_mtime = root->sbi->build_time;
+       root->i_mtime_nsec = root->sbi->build_time_nsec;
+       erofs_init_empty_dir(root);
+       return root;
+}
index c8c33b63fdfbdc0928a9e69c1eb65343362d7790..3fbaf664d5b91329715a046d288faf2af03b79ec 100644 (file)
@@ -232,3 +232,50 @@ err_bdrop:
        erofs_bdrop(bh, true);
        return ERR_PTR(err);
 }
+
+int erofs_enable_sb_chksum(struct erofs_sb_info *sbi, u32 *crc)
+{
+       int ret;
+       u8 buf[EROFS_MAX_BLOCK_SIZE];
+       unsigned int len;
+       struct erofs_super_block *sb;
+
+       ret = erofs_blk_read(sbi, 0, buf, 0, erofs_blknr(sbi, EROFS_SUPER_END) + 1);
+       if (ret) {
+               erofs_err("failed to read superblock to set checksum: %s",
+                         erofs_strerror(ret));
+               return ret;
+       }
+
+       /*
+        * skip the first 1024 bytes, to allow for the installation
+        * of x86 boot sectors and other oddities.
+        */
+       sb = (struct erofs_super_block *)(buf + EROFS_SUPER_OFFSET);
+
+       if (le32_to_cpu(sb->magic) != EROFS_SUPER_MAGIC_V1) {
+               erofs_err("internal error: not an erofs valid image");
+               return -EFAULT;
+       }
+
+       /* turn on checksum feature */
+       sb->feature_compat = cpu_to_le32(le32_to_cpu(sb->feature_compat) |
+                                        EROFS_FEATURE_COMPAT_SB_CHKSUM);
+       if (erofs_blksiz(sbi) > EROFS_SUPER_OFFSET)
+               len = erofs_blksiz(sbi) - EROFS_SUPER_OFFSET;
+       else
+               len = erofs_blksiz(sbi);
+       *crc = erofs_crc32c(~0, (u8 *)sb, len);
+
+       /* set up checksum field to erofs_super_block */
+       sb->checksum = cpu_to_le32(*crc);
+
+       ret = erofs_blk_write(sbi, buf, 0, 1);
+       if (ret) {
+               erofs_err("failed to write checksummed superblock: %s",
+                         erofs_strerror(ret));
+               return ret;
+       }
+
+       return 0;
+}
index c1fe551f2e8a1236cdf8d262b97f7e43887e567e..321b8ace707d9146be9c5edf4a6b320334be6abb 100644 (file)
@@ -975,55 +975,6 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
        return 0;
 }
 
-static int erofs_mkfs_superblock_csum_set(void)
-{
-       int ret;
-       u8 buf[EROFS_MAX_BLOCK_SIZE];
-       u32 crc;
-       unsigned int len;
-       struct erofs_super_block *sb;
-
-       ret = erofs_blk_read(&sbi, 0, buf, 0, erofs_blknr(&sbi, EROFS_SUPER_END) + 1);
-       if (ret) {
-               erofs_err("failed to read superblock to set checksum: %s",
-                         erofs_strerror(ret));
-               return ret;
-       }
-
-       /*
-        * skip the first 1024 bytes, to allow for the installation
-        * of x86 boot sectors and other oddities.
-        */
-       sb = (struct erofs_super_block *)(buf + EROFS_SUPER_OFFSET);
-
-       if (le32_to_cpu(sb->magic) != EROFS_SUPER_MAGIC_V1) {
-               erofs_err("internal error: not an erofs valid image");
-               return -EFAULT;
-       }
-
-       /* turn on checksum feature */
-       sb->feature_compat = cpu_to_le32(le32_to_cpu(sb->feature_compat) |
-                                        EROFS_FEATURE_COMPAT_SB_CHKSUM);
-       if (erofs_blksiz(&sbi) > EROFS_SUPER_OFFSET)
-               len = erofs_blksiz(&sbi) - EROFS_SUPER_OFFSET;
-       else
-               len = erofs_blksiz(&sbi);
-       crc = erofs_crc32c(~0, (u8 *)sb, len);
-
-       /* set up checksum field to erofs_super_block */
-       sb->checksum = cpu_to_le32(crc);
-
-       ret = erofs_blk_write(&sbi, buf, 0, 1);
-       if (ret) {
-               erofs_err("failed to write checksummed superblock: %s",
-                         erofs_strerror(ret));
-               return ret;
-       }
-
-       erofs_info("superblock checksum 0x%08x written", crc);
-       return 0;
-}
-
 static void erofs_mkfs_default_options(void)
 {
        cfg.c_showprogress = true;
@@ -1075,22 +1026,6 @@ void erofs_show_progs(int argc, char *argv[])
                printf("%s %s\n", basename(argv[0]), cfg.c_version);
 }
 
-static struct erofs_inode *erofs_mkfs_alloc_root(struct erofs_sb_info *sbi)
-{
-       struct erofs_inode *root;
-
-       root = erofs_new_inode(sbi);
-       if (IS_ERR(root))
-               return root;
-       root->i_srcpath = strdup("/");
-       root->i_mode = S_IFDIR | 0777;
-       root->i_parent = root;
-       root->i_mtime = root->sbi->build_time;
-       root->i_mtime_nsec = root->sbi->build_time_nsec;
-       erofs_init_empty_dir(root);
-       return root;
-}
-
 static int erofs_mkfs_rebuild_load_trees(struct erofs_inode *root)
 {
        struct erofs_sb_info *src;
@@ -1198,6 +1133,7 @@ int main(int argc, char **argv)
        erofs_blk_t nblocks;
        struct timeval t;
        FILE *packedfile = NULL;
+       u32 crc;
 
        erofs_init_configure();
        erofs_mkfs_default_options();
@@ -1390,7 +1326,7 @@ int main(int argc, char **argv)
        erofs_inode_manager_init();
 
        if (tar_mode) {
-               root = erofs_mkfs_alloc_root(&sbi);
+               root = erofs_rebuild_make_root(&sbi);
                if (IS_ERR(root)) {
                        err = PTR_ERR(root);
                        goto exit;
@@ -1405,7 +1341,7 @@ int main(int argc, char **argv)
                if (err < 0)
                        goto exit;
        } else if (rebuild_mode) {
-               root = erofs_mkfs_alloc_root(&sbi);
+               root = erofs_rebuild_make_root(&sbi);
                if (IS_ERR(root)) {
                        err = PTR_ERR(root);
                        goto exit;
@@ -1471,8 +1407,11 @@ int main(int argc, char **argv)
 
        err = erofs_dev_resize(&sbi, nblocks);
 
-       if (!err && erofs_sb_has_sb_chksum(&sbi))
-               err = erofs_mkfs_superblock_csum_set();
+       if (!err && erofs_sb_has_sb_chksum(&sbi)) {
+               err = erofs_enable_sb_chksum(&sbi, &crc);
+               if (!err)
+                       erofs_info("superblock checksum 0x%08x written", crc);
+       }
 exit:
        if (root)
                erofs_iput(root);