exfat: fix i_blocks for files truncated over 4 GiB
Subsystems (2)
cover exfat: fix i_blocks for files truncated over 4 GiB
From: Christophe Vu-Brugier <[email protected]>
The following patch fixes an issue in the exFAT driver. The number of allocated blocks becomes wrong if a file is truncated over 4 GiB.
A similar issue was fixed last month by Sungjong Seo:
commit 0c336d6e33f4 ("exfat: fix incorrect loading of i_blocks for
large files")
Below is a test case for the issue. A 7 GiB file is truncated to 5 GiB but stat() st_blocks shows only 1 GiB being used.
$ dd if=/dev/urandom of=file.bin bs=1024k count=7168
$ /sbin/xfs_io -c "stat" file.bin fd.path = "file.bin" fd.flags = non-sync,non-direct,read-write stat.ino = 11 stat.type = regular file stat.size = 7516192768 stat.blocks = 14680064
$ /sbin/xfs_io -c "truncate 5368709120" file.bin
$ /sbin/xfs_io -c "stat" file.bin fd.path = "file.bin" fd.flags = non-sync,non-direct,read-write stat.ino = 11 stat.type = regular file stat.size = 5368709120 stat.blocks = 2097152
Christophe Vu-Brugier (1):
exfat: fix i_blocks for files truncated over 4 GiB
fs/exfat/file.c | 2 +- fs/exfat/super.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
Signature · 1 line
2.33.0
PATCH 1/1 exfat: fix i_blocks for files truncated over 4 GiB
From: Christophe Vu-Brugier <[email protected]>
In exfat_truncate(), the computation of inode->i_blocks is wrong if the file is larger than 4 GiB because a 32-bit variable is used as a mask. This is fixed by casting the variable to loff_t which is 64-bit.
Also fix the same computation in exfat_read_root().
This commit is similar to Sungjong Seo's fix in exfat_fill_inode() last month:
commit 0c336d6e33f4 ("exfat: fix incorrect loading of i_blocks for
large files")
--- fs/exfat/file.c | 2 +- fs/exfat/super.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
fs/exfat/file.c +1 -1
@@ -252,7 +252,7 @@ void exfat_truncate(struct inode *inode, loff_t size)mark_inode_dirty (inode); inode->i_blocks = ((i_size_read (inode) + (sbi->cluster_size -1 )) & - ~(sbi->cluster_size -1 )) >> inode->i_blkbits; + ~((loff_t )sbi->cluster_size -1 )) >> inode->i_blkbits; write_size: aligned_size =i_size_read (inode);if (aligned_size & (blocksize -1 )) {
fs/exfat/super.c +2 -2
@@ -364,8 +364,8 @@ static int exfat_read_root(struct inode *inode) inode->i_op = &exfat_dir_inode_operations; inode->i_fop = &exfat_dir_operations; - inode->i_blocks = ((i_size_read (inode) + (sbi->cluster_size -1 )) - & ~(sbi->cluster_size -1 )) >> inode->i_blkbits; + inode->i_blocks = ((i_size_read (inode) + (sbi->cluster_size -1 )) & + ~((loff_t )sbi->cluster_size -1 )) >> inode->i_blkbits;EXFAT_I (inode)->i_pos = ((loff_t )sbi->root_dir <<32 ) |0xffffffff ;EXFAT_I (inode)->i_size_aligned =i_size_read (inode);EXFAT_I (inode)->i_size_ondisk =i_size_read (inode);
Signature · 1 line
2.33.0
fs/exfat/file.c void exfat_truncate(struct inode *inode, loff_t size) · line 255
Quoted 3 lines · Christophe Vu-Brugier
fs/exfat/file.c +1
-1inode->i_blocks = ((i_size_read (inode) + (sbi->cluster_size -1 )) & - ~(sbi->cluster_size -1 )) >> inode->i_blkbits; + ~((loff_t )sbi->cluster_size -1 )) >> inode->i_blkbits;
Isn't this a convoluted way to write:
inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> inode->i_blkbits;
?
Hi Matthew,
Le 18/11/2021 à 22:50, Matthew Wilcox a écrit :
Quoted 10 lines · Christophe Vu-Brugier
fs/exfat/file.c void exfat_truncate(struct inode *inode, loff_t size) · line 255
Quoted 3 lines
inode->i_blocks= ((i_size_read(inode)+ (sbi->cluster_size- 1)) &fs/exfat/file.c +1
-1- ~(sbi->cluster_size - 1)) >> inode->i_blkbits; + ~((loff_t)sbi->cluster_size - 1)) >> inode->i_blkbits;Isn't this a convoluted way to write:
inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> inode->i_blkbits;?
Yes, it is. And we have evidence that it is more error prone.
I will update my patch with what you suggest.
Thanks!
Signature · 1 line
Christophe Vu-Brugier
PATCH v2 1/1 exfat: fix i_blocks for files truncated over 4 GiB
In reply to Matthew WilcoxFrom: Christophe Vu-Brugier <[email protected]>
In exfat_truncate(), the computation of inode->i_blocks is wrong if the file is larger than 4 GiB because a 32-bit variable is used as a mask. This is fixed and simplified by using round_up().
Also fix the same buggy computation in exfat_read_root() and another (correct) one in exfat_fill_inode(). The latter was fixed another way last month but can be simplified by using round_up() as well. See:
commit 0c336d6e33f4 ("exfat: fix incorrect loading of i_blocks for
large files")
--- fs/exfat/file.c | 4 ++-- fs/exfat/inode.c | 4 ++-- fs/exfat/super.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)
fs/exfat/file.c +2 -2
@@ -251,8 +251,8 @@ void exfat_truncate(struct inode *inode, loff_t size)else mark_inode_dirty (inode); - inode->i_blocks = ((i_size_read (inode) + (sbi->cluster_size -1 )) & - ~(sbi->cluster_size -1 )) >> inode->i_blkbits; + inode->i_blocks =round_up (i_size_read (inode), sbi->cluster_size) >> + inode->i_blkbits; write_size: aligned_size =i_size_read (inode);if (aligned_size & (blocksize -1 )) {
fs/exfat/inode.c +2 -2
@@ -603,8 +603,8 @@ static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)exfat_save_attr (inode, info->attr); - inode->i_blocks = ((i_size_read (inode) + (sbi->cluster_size -1 )) & - ~((loff_t )sbi->cluster_size -1 )) >> inode->i_blkbits; + inode->i_blocks =round_up (i_size_read (inode), sbi->cluster_size) >> + inode->i_blkbits; inode->i_mtime = info->mtime; inode->i_ctime = info->mtime; ei->i_crtime = info->crtime;
fs/exfat/super.c +2 -2
@@ -364,8 +364,8 @@ static int exfat_read_root(struct inode *inode) inode->i_op = &exfat_dir_inode_operations; inode->i_fop = &exfat_dir_operations; - inode->i_blocks = ((i_size_read (inode) + (sbi->cluster_size -1 )) - & ~(sbi->cluster_size -1 )) >> inode->i_blkbits; + inode->i_blocks =round_up (i_size_read (inode), sbi->cluster_size) >> + inode->i_blkbits;EXFAT_I (inode)->i_pos = ((loff_t )sbi->root_dir <<32 ) |0xffffffff ;EXFAT_I (inode)->i_size_aligned =i_size_read (inode);EXFAT_I (inode)->i_size_ondisk =i_size_read (inode);
Signature · 1 line
2.33.0
Quoted 10 lines
From: Christophe Vu-Brugier <[email protected]>
In
exfat_truncate(), the computation ofinode->i_blocksis wrong if the file is larger than 4 GiB because a 32-bit variable is used as a mask. This is fixed and simplified by usinground_up().Also fix the same buggy computation in
exfat_read_root()and another (correct) one inexfat_fill_inode(). The latter was fixed another way last month but can be simplified by usinground_up()as well. See:
Quoted 5 lines
commit
0c336d6e33f4("exfat: fix incorrect loading ofi_blocksfor
large files")Signed-off-by Christophe Vu-Brugier <[email protected]>Suggested-by Matthew Wilcox <[email protected]>
Thanks for your patch!
Please update your patch again with below tags.
Namjae Jeon
maintainer2021-11-22 11:10 GMT+09:00, Sungjong Seo <[email protected]>:
Quoted 11 lines
Quoted 11 lines
From: Christophe Vu-Brugier <[email protected]>
In
exfat_truncate(), the computation ofinode->i_blocksis wrong if the file is larger than 4 GiB because a 32-bit variable is used as a mask. This is fixed and simplified by usinground_up().Also fix the same buggy computation in
exfat_read_root()and another (correct) one inexfat_fill_inode(). The latter was fixed another way last
month but can be simplified by usinground_up()as well. See:
Quoted 8 lines
Quoted 5 lines
commit
0c336d6e33f4("exfat: fix incorrect loading ofi_blocksfor
large files")Signed-off-by Christophe Vu-Brugier <[email protected]>Suggested-by Matthew Wilcox <[email protected]>Thanks for your patch!
Please update your patch again with below tags.
There is no need to send a patch again.
I will directly update and apply it.
Thanks!
Quoted 8 lines
Fixes 719c1e1829166 ("exfat: add super block operations")Fixes 98d917047e8b7 ("exfat: add file operations")Cc [email protected] # v5.7+Reviewed-by Sungjong Seo <[email protected]>