erofs-utils: mkfs: use scandir for stable output
PATCH v2 1/1 erofs-utils: mkfs: use scandir for stable output
The iteration order of opendir/readdir depends on filesystem implementation. Hence, even with the same contents, the filesystem of the input directory affects the output.
In this change, opendir/readdir is replaced with scandir for stable order of iteration. This produces the same output regardless of the filesystem of the input directory.
Test: mkfs.erofs ... inputdir(ext3)
Test: mkfs.erofs ... inputdir(tmpfs)
# should generate the same output Signed-off-by: Jooyung Han <[email protected]> ---
v1: https://lore.kernel.org/linux-erofs/[email protected]/
change since v1:
- modify commit msg (no change-id/bug/typo)
- rename the label to err_cleanup
lib/inode.c | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-)
lib/inode.c +14 -25
@@ -1422,37 +1422,25 @@ 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 dirent *dp, **dent; +int i, num_entries;struct erofs_dentry *d;unsigned int nr_subdirs, i_nlink;int ret; - _dir =opendir (dir->i_srcpath); -if (!_dir) { -erofs_err ("failed to opendir at %s: %s" , + num_entries =scandir (dir->i_srcpath, &dent,NULL , alphasort); +if (num_entries ==-1 ) { +erofs_err ("failed to scandir at %s: %s" , dir->i_srcpath,erofs_strerror (-errno));return -errno; } nr_subdirs =0 ; i_nlink =0 ; -while (1 ) { +for (i =0 ; i < num_entries;free (dent[i]), i++) {char buf[PATH_MAX ];struct erofs_inode *inode; - -/* -* set errno to 0 before calling readdir() in order to -* distinguish end of stream and from an error. -*/ - errno =0 ; - dp =readdir (_dir); -if (!dp) { -if (!errno) -break ; - ret = -errno; - goto err_closedir; - } + dp = dent[i];if (is_dot_dotdot (dp->d_name)) { ++i_nlink;
@@ -1466,17 +1454,17 @@ static int erofs_mkfs_handle_directory(struct erofs_inode *dir) d =erofs_d_alloc (dir, dp->d_name);if (IS_ERR (d)) { ret =PTR_ERR (d); - goto err_closedir; + goto err_cleanup; } ret =snprintf (buf,PATH_MAX ,"%s/%s" , dir->i_srcpath, d->name);if (ret <0 || ret >=PATH_MAX ) - goto err_closedir; + goto err_cleanup; inode =erofs_iget_from_srcpath (sbi, buf);if (IS_ERR (inode)) { ret =PTR_ERR (inode); - goto err_closedir; + goto err_cleanup; } d->inode = inode; d->type =erofs_mode_to_ftype (inode->i_mode);
@@ -1484,7 +1472,7 @@ static int erofs_mkfs_handle_directory(struct erofs_inode *dir)erofs_dbg ("file %s added (type %u)" , buf, d->type); nr_subdirs++; } -closedir (_dir); +free (dent); ret =erofs_init_empty_dir (dir);if (ret)
@@ -1506,8 +1494,9 @@ static int erofs_mkfs_handle_directory(struct erofs_inode *dir)return erofs_mkfs_go (sbi,EROFS_MKFS_JOB_DIR , &dir,sizeof (dir)); -err_closedir: -closedir (_dir); +err_cleanup: +for (; i < num_entries;free (dent[i]), i++); +free (dent);return ret; }
Signature · 1 line
2.47.0.338.g60cca15819-goog
Hi Jooyung,
Quoted 4 lines · Jooyung Han
The iteration order of opendir/readdir depends on filesystem implementation. Hence, even with the same contents, the filesystem of the input directory affects the output.
Quoted 8 lines
In this change, opendir/readdir is replaced with scandir for stable order of iteration. This produces the same output regardless of the filesystem of the input directory.
Test: mkfs.erofs ... inputdir(ext3)
Test: mkfs.erofs ... inputdir(tmpfs)# should generate the same output Signed-off-by: Jooyung Han <[email protected]>
Thanks for the patch. I haven't tested the current behavior (1.8.2), but EROFS will sort all dirents in a directory in erofs_prepare_dir_file(). And then dump all subdirs in erofs_mkfs_dump_tree().
It seems both dirents and inodes are sorted in the alphabetical order, could you give more hints about this?
Thanks,
Gao Xiang
Quoted 101 lines
---
v1: https://lore.kernel.org/linux-erofs/[email protected]/
change since v1:
- modify commit msg (no change-id/bug/typo)
- rename the label toerr_cleanuplib/inode.c | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-)lib/inode.c +14
-25@@ -1422,37 +1422,25 @@ 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 dirent *dp, **dent; +int i, num_entries;struct erofs_dentry *d;unsigned int nr_subdirs, i_nlink;int ret; - _dir =opendir (dir->i_srcpath); -if (!_dir) { -erofs_err ("failed to opendir at %s: %s" , + num_entries =scandir (dir->i_srcpath, &dent,NULL , alphasort); +if (num_entries ==-1 ) { +erofs_err ("failed to scandir at %s: %s" , dir->i_srcpath,erofs_strerror (-errno));return -errno; } nr_subdirs =0 ; i_nlink =0 ; -while (1 ) { +for (i =0 ; i < num_entries;free (dent[i]), i++) {char buf[PATH_MAX ];struct erofs_inode *inode; - -/* -* set errno to 0 before calling readdir() in order to -* distinguish end of stream and from an error. -*/ - errno =0 ; - dp =readdir (_dir); -if (!dp) { -if (!errno) -break ; - ret = -errno; - goto err_closedir; - } + dp = dent[i];if (is_dot_dotdot (dp->d_name)) { ++i_nlink;@@ -1466,17 +1454,17 @@ static int erofs_mkfs_handle_directory(struct erofs_inode *dir) d =erofs_d_alloc (dir, dp->d_name);if (IS_ERR (d)) { ret =PTR_ERR (d); - goto err_closedir; + goto err_cleanup; } ret =snprintf (buf,PATH_MAX ,"%s/%s" , dir->i_srcpath, d->name);if (ret <0 || ret >=PATH_MAX ) - goto err_closedir; + goto err_cleanup; inode =erofs_iget_from_srcpath (sbi, buf);if (IS_ERR (inode)) { ret =PTR_ERR (inode); - goto err_closedir; + goto err_cleanup; } d->inode = inode; d->type =erofs_mode_to_ftype (inode->i_mode);@@ -1484,7 +1472,7 @@ static int erofs_mkfs_handle_directory(struct erofs_inode *dir)erofs_dbg ("file %s added (type %u)" , buf, d->type); nr_subdirs++; } -closedir (_dir); +free (dent); ret =erofs_init_empty_dir (dir);if (ret)@@ -1506,8 +1494,9 @@ static int erofs_mkfs_handle_directory(struct erofs_inode *dir)return erofs_mkfs_go (sbi,EROFS_MKFS_JOB_DIR , &dir,sizeof (dir)); -err_closedir: -closedir (_dir); +err_cleanup: +for (; i < num_entries;free (dent[i]), i++); +free (dent);return ret; }
Hi Gao,
I found that in the loop erofs_iget_from_srcpath() is called in different order due to readdir and erofs_iget_from_srcpath() calls erofs_new_inode() which fills i_ino[0] for newly created inode. I think this i_ino[0] having different values caused the difference in the output.
Quoted 130 lines · Gao Xiang
Hi Jooyung,
Quoted 4 lines · Jooyung Han
The iteration order of opendir/readdir depends on filesystem implementation. Hence, even with the same contents, the filesystem of the input directory affects the output.
Quoted 8 lines
In this change, opendir/readdir is replaced with scandir for stable order of iteration. This produces the same output regardless of the filesystem of the input directory.
Test: mkfs.erofs ... inputdir(ext3)
Test: mkfs.erofs ... inputdir(tmpfs)# should generate the same output Signed-off-by: Jooyung Han <[email protected]>Thanks for the patch. I haven't tested the current behavior (1.8.2), but EROFS will sort all dirents in a directory in
erofs_prepare_dir_file(). And then dump all subdirs inerofs_mkfs_dump_tree().It seems both dirents and inodes are sorted in the alphabetical order, could you give more hints about this?
Thanks,
Gao XiangQuoted 101 lines
---
v1: https://lore.kernel.org/linux-erofs/[email protected]/
change since v1:
- modify commit msg (no change-id/bug/typo)
- rename the label toerr_cleanuplib/inode.c | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-)lib/inode.c +14
-25@@ -1422,37 +1422,25 @@ 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 dirent *dp, **dent; +int i, num_entries;struct erofs_dentry *d;unsigned int nr_subdirs, i_nlink;int ret; - _dir =opendir (dir->i_srcpath); -if (!_dir) { -erofs_err ("failed to opendir at %s: %s" , + num_entries =scandir (dir->i_srcpath, &dent,NULL , alphasort); +if (num_entries ==-1 ) { +erofs_err ("failed to scandir at %s: %s" , dir->i_srcpath,erofs_strerror (-errno));return -errno; } nr_subdirs =0 ; i_nlink =0 ; -while (1 ) { +for (i =0 ; i < num_entries;free (dent[i]), i++) {char buf[PATH_MAX ];struct erofs_inode *inode; - -/* -* set errno to 0 before calling readdir() in order to -* distinguish end of stream and from an error. -*/ - errno =0 ; - dp =readdir (_dir); -if (!dp) { -if (!errno) -break ; - ret = -errno; - goto err_closedir; - } + dp = dent[i];if (is_dot_dotdot (dp->d_name)) { ++i_nlink;@@ -1466,17 +1454,17 @@ static int erofs_mkfs_handle_directory(struct erofs_inode *dir) d =erofs_d_alloc (dir, dp->d_name);if (IS_ERR (d)) { ret =PTR_ERR (d); - goto err_closedir; + goto err_cleanup; } ret =snprintf (buf,PATH_MAX ,"%s/%s" , dir->i_srcpath, d->name);if (ret <0 || ret >=PATH_MAX ) - goto err_closedir; + goto err_cleanup; inode =erofs_iget_from_srcpath (sbi, buf);if (IS_ERR (inode)) { ret =PTR_ERR (inode); - goto err_closedir; + goto err_cleanup; } d->inode = inode; d->type =erofs_mode_to_ftype (inode->i_mode);@@ -1484,7 +1472,7 @@ static int erofs_mkfs_handle_directory(struct erofs_inode *dir)erofs_dbg ("file %s added (type %u)" , buf, d->type); nr_subdirs++; } -closedir (_dir); +free (dent); ret =erofs_init_empty_dir (dir);if (ret)@@ -1506,8 +1494,9 @@ static int erofs_mkfs_handle_directory(struct erofs_inode *dir)return erofs_mkfs_go (sbi,EROFS_MKFS_JOB_DIR , &dir,sizeof (dir)); -err_closedir: -closedir (_dir); +err_cleanup: +for (; i < num_entries;free (dent[i]), i++); +free (dent);return ret; }
Quoted 7 lines · Jooyung Han
Hi Gao,
I found that in the loop
erofs_iget_from_srcpath()is called in different order due to readdir anderofs_iget_from_srcpath()callserofs_new_inode()which fills i_ino[0] for newly created inode. I think this i_ino[0] having different values caused the difference in the output.
Oh, okay, that makes sense, I think we'd better move
inode->i_ino[0] = sbi->inos++; /* inode serial number */
to erofs_mkfs_dump_tree() (since we'd better to leave i_ino[0] stable even without dumping from localdir later.) and even clean up a bit.
If you don't have more time to clean up this, let's just commit a patch to fix this directly.
Thanks,
Gao Xiang
Quoted 13 lines · Gao Xiang
Quoted 7 lines · Jooyung Han
Hi Gao,
I found that in the loop
erofs_iget_from_srcpath()is called in different order due to readdir anderofs_iget_from_srcpath()callserofs_new_inode()which fills i_ino[0] for newly created inode. I think this i_ino[0] having different values caused the difference in the output.Oh, okay, that makes sense, I think we'd better move
Quoted 8 lines
inode->i_ino[0] = sbi->inos++; /* inode serial number */
to erofs_mkfs_dump_tree() (since we'd better to leave i_ino[0] stable even without dumping from localdir later.) and even clean up a bit.
If you don't have more time to clean up this, let's just commit a patch to fix this directly.
Actually I think erofs_prepare_inode_buffer() is a good place to fill i_ino[0], since it will stablize the exact physical place of an on-disk inode.
Thanks,
Gao Xiang
Quoted 3 lines
Thanks,
Gao Xiang
(I forgot to reply all)
Quoted 14 lines · Gao Xiang
Quoted 7 lines · Jooyung Han
Hi Gao,
I found that in the loop
erofs_iget_from_srcpath()is called in different order due to readdir anderofs_iget_from_srcpath()callserofs_new_inode()which fills i_ino[0] for newly created inode. I think this i_ino[0] having different values caused the difference in the output.Oh, okay, that makes sense, I think we'd better move
Quoted 8 lines
inode->i_ino[0] = sbi->inos++; /* inode serial number */
to
erofs_mkfs_dump_tree()(since we'd better to leave i_ino[0] stable even without dumping from localdir later.) and even clean up a bit.If you don't have more time to clean up this, let's just commit a patch to fix this directly.
Sounds good to me ;-)
To be honest, I don't know the stuff well enough for this cleanup.
Quoted 3 lines
Thanks,
Gao Xiang
Thanks,
Jooyung
Quoted 1 line · Jooyung Han
(I forgot to reply all)
Yeah, I received that.
Quoted 22 lines · Gao Xiang
Quoted 20 lines · Jooyung Han
Quoted 7 lines
Hi Gao,
I found that in the loop
erofs_iget_from_srcpath()is called in different order due to readdir anderofs_iget_from_srcpath()callserofs_new_inode()which fills i_ino[0] for newly created inode. I think this i_ino[0] having different values caused the difference in the output.Oh, okay, that makes sense, I think we'd better move
inode->i_ino[0] = sbi->inos++; /* inode serial number */
to
erofs_mkfs_dump_tree()(since we'd better to leave i_ino[0] stable even without dumping from localdir later.) and even clean up a bit.
Quoted 5 lines
Quoted 2 lines
If you don't have more time to clean up this, let's just commit a patch to fix this directly.
Sounds good to me ;-)
To be honest, I don't know the stuff well enough for this cleanup.
Ok, anyway if you could submit a patch that moves inode->i_ino[0] = sbi->inos++;
from erofs_new_inode() to erofs_prepare_inode_buffer()
I will apply directly.
Also I'd like to update it as
inode->i_ino[0] = ++sbi->inos;
Since i_ino == 0 is invalid...
Thanks,
Gao Xiang
Quoted 7 lines
Quoted 4 lines
Thanks,
Gao XiangThanks,
Jooyung
Hi Gao,
Since the new patch is quite different from the last, I sent it as a new one, instead of up-revving.
Quoted 58 lines · Gao Xiang
Quoted 1 line · Jooyung Han
(I forgot to reply all)
Yeah, I received that.
Quoted 22 lines · Gao Xiang
Quoted 20 lines · Jooyung Han
Quoted 7 lines
Hi Gao,
I found that in the loop
erofs_iget_from_srcpath()is called in different order due to readdir anderofs_iget_from_srcpath()callserofs_new_inode()which fills i_ino[0] for newly created inode. I think this i_ino[0] having different values caused the difference in the output.Oh, okay, that makes sense, I think we'd better move
inode->i_ino[0] = sbi->inos++; /* inode serial number */
to
erofs_mkfs_dump_tree()(since we'd better to leave i_ino[0] stable even without dumping from localdir later.) and even clean up a bit.Quoted 5 lines
Quoted 2 lines
If you don't have more time to clean up this, let's just commit a patch to fix this directly.
Sounds good to me ;-)
To be honest, I don't know the stuff well enough for this cleanup.Ok, anyway if you could submit a patch that moves inode->i_ino[0] = sbi->inos++;
fromerofs_new_inode()toerofs_prepare_inode_buffer()I will apply directly.
Also I'd like to update it as
inode->i_ino[0] = ++sbi->inos;Since
i_ino == 0is invalid...Thanks,
Gao XiangQuoted 7 lines
Quoted 4 lines
Thanks,
Gao XiangThanks,
Jooyung