[PATCH v3] erofs-utils: lib: correctly set {u,g}id in erofs_make_empty_root_inode()

Yifan Zhao zhaoyifan28 at huawei.com
Wed Jan 14 19:35:37 AEDT 2026


In rebuild mode, the {u,g}id of the root inode is currently defaulted
to 0 and is not controlled by --force_{u,g}id. This behavior also causes
the {u,g}id of intermediate dir inodes created by
`erofs_rebuild_mkdir()` to be set to 0.

This patch fixes the behavior by explicitly setting permissions for the
root inode:

- If --force-{u,g}id is not specified, it now defaults to the current
   user's {u,g}id.
- If --force-{u,g}id is specified, it correctly updates the ownership
   for all files and directories.

Signed-off-by: Yifan Zhao <zhaoyifan28 at huawei.com>
---
 include/erofs/inode.h | 3 ++-
 lib/inode.c           | 8 +++++++-
 mkfs/main.c           | 2 +-
 3 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/include/erofs/inode.h b/include/erofs/inode.h
index 8b91771..89bd16a 100644
--- a/include/erofs/inode.h
+++ b/include/erofs/inode.h
@@ -48,7 +48,8 @@ int erofs_importer_load_tree(struct erofs_importer *im, bool rebuild,
 struct erofs_inode *erofs_mkfs_build_special_from_fd(struct erofs_importer *im,
 						     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);
+struct erofs_inode *erofs_make_empty_root_inode(struct erofs_importer *im,
+						struct erofs_sb_info *sbi);
 
 #ifdef __cplusplus
 }
diff --git a/lib/inode.c b/lib/inode.c
index 26fefa2..e44e03c 100644
--- a/lib/inode.c
+++ b/lib/inode.c
@@ -2375,8 +2375,10 @@ int erofs_fixup_root_inode(struct erofs_inode *root)
 	return err;
 }
 
-struct erofs_inode *erofs_rebuild_make_root(struct erofs_sb_info *sbi)
+struct erofs_inode *erofs_make_empty_root_inode(struct erofs_importer *im,
+						struct erofs_sb_info *sbi)
 {
+	struct erofs_importer_params *params = im ? im->params : NULL;
 	struct erofs_inode *root;
 
 	root = erofs_new_inode(sbi);
@@ -2384,6 +2386,10 @@ struct erofs_inode *erofs_rebuild_make_root(struct erofs_sb_info *sbi)
 		return root;
 	root->i_srcpath = strdup("/");
 	root->i_mode = S_IFDIR | 0777;
+	root->i_uid = (!params || params->fixed_uid == -1) ? getuid() :
+							     params->fixed_uid;
+	root->i_gid = (!params || params->fixed_gid == -1) ? getgid() :
+							     params->fixed_gid;
 	root->i_parent = root;
 	root->i_mtime = root->sbi->epoch + root->sbi->build_time;
 	root->i_mtime_nsec = root->sbi->fixed_nsec;
diff --git a/mkfs/main.c b/mkfs/main.c
index f709190..620b1ed 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -1910,7 +1910,7 @@ int main(int argc, char **argv)
 			goto exit;
 		}
 
-		root = erofs_rebuild_make_root(&g_sbi);
+		root = erofs_make_empty_root_inode(&importer, &g_sbi);
 		if (IS_ERR(root)) {
 			err = PTR_ERR(root);
 			goto exit;
-- 
2.47.3



More information about the Linux-erofs mailing list