jffs2: use fs_umode_to_dtype() for dirent type
PATCH 1/1 jffs2: use fs_umode_to_dtype() for dirent type
Use fs_umode_to_dtype() instead of direct bit shifting when setting directory entry type.
--- fs/jffs2/dir.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-)
fs/jffs2/dir.c +3 -8
@@ -261,8 +261,7 @@ static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct deif (d_is_dir (old_dentry))return -EPERM ; -/* XXX: This is ugly */ - type = (d_inode (old_dentry)->i_mode &S_IFMT ) >>12 ; + type =fs_umode_to_dtype (d_inode (old_dentry)->i_mode);if (!type) type =DT_REG ; now =JFFS2_NOW ();
@@ -730,10 +729,7 @@ static int jffs2_mknod (struct mnt_idmap *idmap, struct inode *dir_i, rd->ino =cpu_to_je32 (inode->i_ino); rd->mctime =cpu_to_je32 (JFFS2_NOW ()); rd->nsize = namelen; - -/* XXX: This is ugly. */ - rd->type = (mode &S_IFMT ) >>12 ; - + rd->type =fs_umode_to_dtype (mode); rd->node_crc =cpu_to_je32 (crc32 (0 , rd,sizeof (*rd)-8 )); rd->name_crc =cpu_to_je32 (crc32 (0 , dentry->d_name.name, namelen));
@@ -811,8 +807,7 @@ static int jffs2_rename (struct mnt_idmap *idmap,/* Make a hard link */ -/* XXX: This is ugly */ - type = (d_inode (old_dentry)->i_mode &S_IFMT ) >>12 ; + type =fs_umode_to_dtype (d_inode (old_dentry)->i_mode);if (!type) type =DT_REG ; now =JFFS2_NOW ();
Signature · 1 line
2.53.0
Quoted 13 lines · Adarsh Das
Use
fs_umode_to_dtype()instead of direct bit shifting when setting directory entry type.Signed-off-by Adarsh Das <[email protected]>--- fs/jffs2/dir.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-)fs/jffs2/dir.c +0
-0
fs/jffs2/dir.c static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct de · line 265
Quoted 7 lines
fs/jffs2/dir.c +1
-2@@ -261,8 +261,7 @@ static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct deif (d_is_dir (old_dentry))return -EPERM ; -/* XXX: This is ugly */ - type = (d_inode (old_dentry)->i_mode &S_IFMT ) >>12 ; + type =fs_umode_to_dtype (d_inode (old_dentry)->i_mode);if (!type) type =DT_REG ;
Hi Adarsh,
The convertion result is wrong for jffs2_do_link.
The i_mode of old_dentry is 'S_IFIFO', the jffs2 gets type DT_FIFO(1) and pass it to 'rd->type' by jffs2_do_link. However, after the patch applied, jffs2 gets type FT_FIFO(5), which will confuse other 'rd->type' accessing processes(eg. jffs2_scan_dirent_node[rd->type], jffs2_build_filesystem[fd->type]).
Quoted 25 lines
fs/jffs2/dir.c +2
-6now =JFFS2_NOW ();@@ -730,10 +729,7 @@ static int jffs2_mknod (struct mnt_idmap *idmap, struct inode *dir_i, rd->ino =cpu_to_je32 (inode->i_ino); rd->mctime =cpu_to_je32 (JFFS2_NOW ()); rd->nsize = namelen; - -/* XXX: This is ugly. */ - rd->type = (mode &S_IFMT ) >>12 ; - + rd->type =fs_umode_to_dtype (mode); rd->node_crc =cpu_to_je32 (crc32 (0 , rd,sizeof (*rd)-8 )); rd->name_crc =cpu_to_je32 (crc32 (0 , dentry->d_name.name, namelen));@@ -811,8 +807,7 @@ static int jffs2_rename (struct mnt_idmap *idmap,/* Make a hard link */ -/* XXX: This is ugly */ - type = (d_inode (old_dentry)->i_mode &S_IFMT ) >>12 ; + type =fs_umode_to_dtype (d_inode (old_dentry)->i_mode);if (!type) type =DT_REG ; now =JFFS2_NOW ();
Hi Zhihao,
Thank you for taking the time to review my patch!
I'm trying to understand the issue you've identified. I looked at the fs_umode_to_dtype() implementation in fs/fs_types.c:
unsigned char fs_umode_to_dtype(umode_t mode) {
return fs_ftype_to_dtype(fs_umode_to_ftype(mode));
}From what I can see, it seems like the function does produce FT_FIFO (5) as an intermediate value, but then converts it back before returning. Here's what I think happens for S_IFIFO (0010000):
Old bit-shift code:
type = (mode & S_IFMT) >> 12
= (0010000 & 00170000) >> 12
= 0010000 >> 12
= 1 (DT_FIFO)New fs_umode_to_dtype() code:
Step 1: fs_umode_to_ftype(0010000)
= fs_ftype_by_dtype[S_DT(0010000)]
= fs_ftype_by_dtype[1]
= FT_FIFO = 5
Step 2: fs_ftype_to_dtype(5)
= fs_dtype_by_ftype[5]
= DT_FIFO = 1I'm not sure how FT_FIFO (5) would remain after the function returns, since it seems to get converted back to DT_FIFO (1) in step 2.
If there's an issue with this approach, I can edit my patch and keep changes in only jffs2_mknod.
Quoted 22 lines · Adarsh Das
Hi Zhihao,
Thank you for taking the time to review my patch!
I'm trying to understand the issue you've identified. I looked at the
fs_umode_to_dtype()implementation infs/fs_types.c:unsigned char fs_umode_to_dtype(umode_t mode) { return fs_ftype_to_dtype(fs_umode_to_ftype(mode)); }Quoted 1 line
From what I can see, it seems like the function does produce
FT_FIFO(5)as an intermediate value, but then converts it back before returning. Here's what I think happens for
S_IFIFO(0010000):Old bit-shift code:
type = (mode & S_IFMT) >> 12 = (0010000 & 00170000) >> 12 = 0010000 >> 12 = 1 (DT_FIFO)New
fs_umode_to_dtype()code:
Quoted 9 lines
Step 1: fs_umode_to_ftype(0010000)
= fs_ftype_by_dtype[S_DT(0010000)]
= fs_ftype_by_dtype[1]
= FT_FIFO = 5
Step 2: fs_ftype_to_dtype(5)
= fs_dtype_by_ftype[5]
= DT_FIFO = 1Oh, that's fs_umode_to_dtype, sorry for the mistake, I saw it as 'fs_umode_to_ftype'.
Quoted 6 lines
I'm not sure how
FT_FIFO(5) would remain after the function returns, since it seems to get converted back toDT_FIFO(1) in step 2.If there's an issue with this approach, I can edit my patch and keep changes in only
jffs2_mknod. .
Quoted 8 lines · Adarsh Das
Use
fs_umode_to_dtype()instead of direct bit shifting when setting directory entry type.Signed-off-by Adarsh Das <[email protected]>--- fs/jffs2/dir.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-)
Quoted 37 lines
fs/jffs2/dir.c +3
-8@@ -261,8 +261,7 @@ static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct deif (d_is_dir (old_dentry))return -EPERM ; -/* XXX: This is ugly */ - type = (d_inode (old_dentry)->i_mode &S_IFMT ) >>12 ; + type =fs_umode_to_dtype (d_inode (old_dentry)->i_mode);if (!type) type =DT_REG ; now =JFFS2_NOW ();@@ -730,10 +729,7 @@ static int jffs2_mknod (struct mnt_idmap *idmap, struct inode *dir_i, rd->ino =cpu_to_je32 (inode->i_ino); rd->mctime =cpu_to_je32 (JFFS2_NOW ()); rd->nsize = namelen; - -/* XXX: This is ugly. */ - rd->type = (mode &S_IFMT ) >>12 ; - + rd->type =fs_umode_to_dtype (mode); rd->node_crc =cpu_to_je32 (crc32 (0 , rd,sizeof (*rd)-8 )); rd->name_crc =cpu_to_je32 (crc32 (0 , dentry->d_name.name, namelen));@@ -811,8 +807,7 @@ static int jffs2_rename (struct mnt_idmap *idmap,/* Make a hard link */ -/* XXX: This is ugly */ - type = (d_inode (old_dentry)->i_mode &S_IFMT ) >>12 ; + type =fs_umode_to_dtype (d_inode (old_dentry)->i_mode);if (!type) type =DT_REG ; now =JFFS2_NOW ();