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

Yifan Zhao zhaoyifan28 at huawei.com
Wed Jan 14 18:38:06 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 directories 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           | 5 ++++-
 mkfs/main.c           | 2 +-
 3 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/include/erofs/inode.h b/include/erofs/inode.h
index 8b91771..2a7af31 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_rebuild_make_root(struct erofs_importer *im,
+					    struct erofs_sb_info *sbi);
 
 #ifdef __cplusplus
 }
diff --git a/lib/inode.c b/lib/inode.c
index 26fefa2..4f87757 100644
--- a/lib/inode.c
+++ b/lib/inode.c
@@ -2375,7 +2375,8 @@ 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_rebuild_make_root(struct erofs_importer *im,
+					    struct erofs_sb_info *sbi)
 {
 	struct erofs_inode *root;
 
@@ -2384,6 +2385,8 @@ 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 = im->params->fixed_uid == -1 ? getuid() : im->params->fixed_uid;
+	root->i_gid = im->params->fixed_gid == -1 ? getgid() : im->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..e9ae29a 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_rebuild_make_root(&importer, &g_sbi);
 		if (IS_ERR(root)) {
 			err = PTR_ERR(root);
 			goto exit;
-- 
2.47.3



More information about the Linux-erofs mailing list