diff mbox series

[1/2] exfat: fix referencing wrong parent directory information after renaming

Message ID HK2PR04MB3891BE0766FAF0AEC39FE2DC811A9@HK2PR04MB3891.apcprd04.prod.outlook.com (mailing list archive)
State New, archived
Headers show
Series fix referencing wrong parent dir info after renaming | expand

Commit Message

Yuezhang.Mo@sony.com March 25, 2022, 9:42 a.m. UTC
During renaming, the parent directory information maybe
updated. But the file/directory still references to the
old parent directory information.

This bug will cause 2 problems.

(1) The renamed file can not be written.

    [10768.175172] exFAT-fs (sda1): error, failed to bmap (inode : 7afd50e4 iblock : 0, err : -5)
    [10768.184285] exFAT-fs (sda1): Filesystem has been set read-only
    ash: write error: Input/output error

(2) Some dentries of the renamed file/directory are not set
    to deleted after removing the file/directory.

fixes: 5f2aa075070c ("exfat: add inode operations")

Signed-off-by: Yuezhang Mo <Yuezhang.Mo@sony.com>
Reviewed-by: Andy Wu <Andy.Wu@sony.com>
Reviewed-by: Aoyama Wataru <wataru.aoyama@sony.com>
Reviewed-by: Daniel Palmer <daniel.palmer@sony.com>
---
 fs/exfat/namei.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Namjae Jeon March 28, 2022, 1:43 a.m. UTC | #1
2022-03-25 18:42 GMT+09:00, Yuezhang.Mo@sony.com <Yuezhang.Mo@sony.com>:
Hi Yuezhang,

> During renaming, the parent directory information maybe
> updated. But the file/directory still references to the
> old parent directory information.
>
> This bug will cause 2 problems.
>
> (1) The renamed file can not be written.
>
>     [10768.175172] exFAT-fs (sda1): error, failed to bmap (inode : 7afd50e4
> iblock : 0, err : -5)
>     [10768.184285] exFAT-fs (sda1): Filesystem has been set read-only
>     ash: write error: Input/output error
Could you please elaborate how to reproduce it ?

Thanks.
>
> (2) Some dentries of the renamed file/directory are not set
>     to deleted after removing the file/directory.
>
> fixes: 5f2aa075070c ("exfat: add inode operations")
>
> Signed-off-by: Yuezhang Mo <Yuezhang.Mo@sony.com>
> Reviewed-by: Andy Wu <Andy.Wu@sony.com>
> Reviewed-by: Aoyama Wataru <wataru.aoyama@sony.com>
> Reviewed-by: Daniel Palmer <daniel.palmer@sony.com>
> ---
>  fs/exfat/namei.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/fs/exfat/namei.c b/fs/exfat/namei.c
> index a02a04a993bf..e7adb6bfd9d5 100644
> --- a/fs/exfat/namei.c
> +++ b/fs/exfat/namei.c
> @@ -1080,6 +1080,7 @@ static int exfat_rename_file(struct inode *inode,
> struct exfat_chain *p_dir,
>
>  		exfat_remove_entries(inode, p_dir, oldentry, 0,
>  			num_old_entries);
> +		ei->dir = *p_dir;
>  		ei->entry = newentry;
>  	} else {
>  		if (exfat_get_entry_type(epold) == TYPE_FILE) {
> --
> 2.25.1
>
Yuezhang.Mo@sony.com March 28, 2022, 6:18 a.m. UTC | #2
Hi Namjae Jeon,

The issue can be reproduced by

Step 1: create and rename the file as:

dir=${mount_point}/dir

rm -fr ${mount_point}/*
mkdir ${dir}
dirsize=$(du -b ${dir} | awk '{print $1}')

for ((i = 1; i <= dirsize / (32 * 3); i++)); do
        touch ${dir}/file-$i
done

mkdir ${mount_point}/dir2

mv ${dir}/file-1 ${dir}/long-file-name-1234567890-1234567890

> > (1) The renamed file can not be written.
> >
> >     [10768.175172] exFAT-fs (sda1): error, failed to bmap (inode :
> > 7afd50e4 iblock : 0, err : -5)
> >     [10768.184285] exFAT-fs (sda1): Filesystem has been set read-only
> >     ash: write error: Input/output error
> Could you please elaborate how to reproduce it ?

Step 2: Write data to the renamed file, such as:

echo xxx > ${dir}/file-1 ${dir}/long-file-name-1234567890-1234567890

> > (2) Some dentries of the renamed file/directory are not set
> >     to deleted after removing the file/directory.

After applying the debug patch,
```diff
--- a/fs/exfat/namei.c
+++ b/fs/exfat/namei.c
@@ -824,6 +824,11 @@ static int exfat_unlink(struct inode *dir, struct dentry *dentry)
        num_entries++;
        brelse(bh);
 
+       if (num_entries != ep->dentry.file.num_ext + 1) {
+               pr_err("file has %d entries\n", ep->dentry.file.num_ext + 1);
+               pr_err("But only set %d entries to deleted\n", num_entries);
+       }
+
        exfat_set_volume_dirty(sb);
        /* update the directory entry */
        if (exfat_remove_entries(dir, &cdir, entry, 0, num_entries)) {
```

We can find that 4 entries are not set to delete. 

[  388.140802] file has 5 entries
[  388.144200] But only set 1 entries to deleted
Sungjong Seo April 1, 2022, 10:32 a.m. UTC | #3
> During renaming, the parent directory information maybe updated. But the
> file/directory still references to the old parent directory information.
> 
> This bug will cause 2 problems.
> 
> (1) The renamed file can not be written.
> 
>     [10768.175172] exFAT-fs (sda1): error, failed to bmap (inode : 7afd50e4
> iblock : 0, err : -5)
>     [10768.184285] exFAT-fs (sda1): Filesystem has been set read-only
>     ash: write error: Input/output error
> 
> (2) Some dentries of the renamed file/directory are not set
>     to deleted after removing the file/directory.
> 
> fixes: 5f2aa075070c ("exfat: add inode operations")
> 
> Signed-off-by: Yuezhang Mo <Yuezhang.Mo@sony.com>
> Reviewed-by: Andy Wu <Andy.Wu@sony.com>
> Reviewed-by: Aoyama Wataru <wataru.aoyama@sony.com>
> Reviewed-by: Daniel Palmer <daniel.palmer@sony.com>

Looks good!
Thanks for your patch!
Reviewed-by: Sungjong Seo <sj1557.seo@samsung.com>

> ---
>  fs/exfat/namei.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/fs/exfat/namei.c b/fs/exfat/namei.c index
> a02a04a993bf..e7adb6bfd9d5 100644
> --- a/fs/exfat/namei.c
> +++ b/fs/exfat/namei.c
> @@ -1080,6 +1080,7 @@ static int exfat_rename_file(struct inode *inode,
> struct exfat_chain *p_dir,
> 
>  		exfat_remove_entries(inode, p_dir, oldentry, 0,
>  			num_old_entries);
> +		ei->dir = *p_dir;
>  		ei->entry = newentry;
>  	} else {
>  		if (exfat_get_entry_type(epold) == TYPE_FILE) {
> --
> 2.25.1
diff mbox series

Patch

diff --git a/fs/exfat/namei.c b/fs/exfat/namei.c
index a02a04a993bf..e7adb6bfd9d5 100644
--- a/fs/exfat/namei.c
+++ b/fs/exfat/namei.c
@@ -1080,6 +1080,7 @@  static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir,
 
 		exfat_remove_entries(inode, p_dir, oldentry, 0,
 			num_old_entries);
+		ei->dir = *p_dir;
 		ei->entry = newentry;
 	} else {
 		if (exfat_get_entry_type(epold) == TYPE_FILE) {