f2fs: fix to do cast in F2FS_{BLK_TO_BYTES, BTYES_TO_BLK} to avoid overflow
Subsystems (2)
Chao Yu
maintainerPATCH 1/4 f2fs: fix to do cast in F2FS_{BLK_TO_BYTES, BTYES_TO_BLK} to avoid overflow
It missed to cast variable to unsigned long long type before bit shift, which will cause overflow, fix it.
--- include/linux/f2fs_fs.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/f2fs_fs.h +3 -3
@@ -24,10 +24,10 @@#define NEW_ADDR ((block_t)-1)/* used as block_t addresses */ #define COMPRESS_ADDR ((block_t)-2)/* used as compressed data flag */ -#define F2FS_BYTES_TO_BLK (bytes) ((bytes) >> F2FS_BLKSIZE_BITS) -#define F2FS_BLK_TO_BYTES (blk) ((blk) << F2FS_BLKSIZE_BITS) +#define F2FS_BYTES_TO_BLK (bytes) ((unsigned long long)(bytes) >> F2FS_BLKSIZE_BITS) +#define F2FS_BLK_TO_BYTES (blk) ((unsigned long long)(blk) << F2FS_BLKSIZE_BITS)#define F2FS_BLK_END_BYTES (blk) (F2FS_BLK_TO_BYTES(blk + 1) - 1) -#define F2FS_BLK_ALIGN (x) (F2FS_BYTES_TO_BLK((x) + F2FS_BLKSIZE - 1)) +#define F2FS_BLK_ALIGN (x) (F2FS_BYTES_TO_BLK((x) + F2FS_BLKSIZE - 1))/* 0, 1(node nid), 2(meta nid) are reserved node id */ #define F2FS_RESERVED_NODE_NUM 3
Signature · 1 line
2.40.1
Chao Yu
maintainerPATCH 2/4 f2fs: clean up w/ F2FS_{BLK_TO_BYTES,BTYES_TO_BLK}
f2fs doesn't support different blksize in one instance, so bytes_to_blks() and blks_to_bytes() are equal to F2FS_BYTES_TO_BLK and F2FS_BLK_TO_BYTES, let's use F2FS_BYTES_TO_BLK/F2FS_BLK_TO_BYTES instead for cleanup.
--- fs/f2fs/data.c | 68 +++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 39 deletions(-)
fs/f2fs/data.c +29 -39
@@ -1819,16 +1819,6 @@ bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)return true; } -static inline u64 bytes_to_blks (struct inode *inode,u64 bytes) -{ -return (bytes >> inode->i_blkbits); -} - -static inline u64 blks_to_bytes (struct inode *inode,u64 blks) -{ -return (blks << inode->i_blkbits); -} -static int f2fs_xattr_fiemap (struct inode *inode,struct fiemap_extent_info *fieinfo) {
@@ -1854,7 +1844,7 @@ static int f2fs_xattr_fiemap(struct inode *inode,return err; } - phys =blks_to_bytes (inode, ni.blk_addr); + phys =F2FS_BLK_TO_BYTES (ni.blk_addr); offset = offsetof(struct f2fs_inode , i_addr) +sizeof (__le32) * (DEF_ADDRS_PER_INODE -get_inline_xattr_addrs (inode));
@@ -1886,7 +1876,7 @@ static int f2fs_xattr_fiemap(struct inode *inode,return err; } - phys =blks_to_bytes (inode, ni.blk_addr); + phys =F2FS_BLK_TO_BYTES (ni.blk_addr); len = inode->i_sb->s_blocksize;f2fs_put_page (page,1 );
@@ -1948,16 +1938,16 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, goto out; } -if (bytes_to_blks (inode, len) ==0 ) - len =blks_to_bytes (inode,1 ); +if (F2FS_BYTES_TO_BLK (len) ==0 ) + len =F2FS_BLKSIZE ; - start_blk =bytes_to_blks (inode, start); - last_blk =bytes_to_blks (inode, start + len -1 ); + start_blk =F2FS_BYTES_TO_BLK (start); + last_blk =F2FS_BYTES_TO_BLK (start + len -1 ); next:memset (&map,0 ,sizeof (map)); map.m_lblk = start_blk; - map.m_len =bytes_to_blks (inode, len); + map.m_len =F2FS_BYTES_TO_BLK (len); map.m_next_pgofs = &next_pgofs; map.m_seg_type =NO_CHECK_TYPE ;
@@ -1974,7 +1964,7 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,if (!compr_cluster && !(map.m_flags &F2FS_MAP_FLAGS )) { start_blk = next_pgofs; -if (blks_to_bytes (inode, start_blk) < maxbytes) +if (F2FS_BLK_TO_BYTES (start_blk) < maxbytes) goto prep_next; flags |=FIEMAP_EXTENT_LAST ;
@@ -2011,14 +2001,14 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, }else if (compr_appended) {unsigned int appended_blks = cluster_size - count_in_cluster +1 ; - size +=blks_to_bytes (inode, appended_blks); + size +=F2FS_BLK_TO_BYTES (appended_blks); start_blk += appended_blks; compr_cluster = false; }else { - logical =blks_to_bytes (inode, start_blk); + logical =F2FS_BLK_TO_BYTES (start_blk); phys =__is_valid_data_blkaddr (map.m_pblk) ? -blks_to_bytes (inode, map.m_pblk) :0 ; - size =blks_to_bytes (inode, map.m_len); +F2FS_BLK_TO_BYTES (map.m_pblk) :0 ; + size =F2FS_BLK_TO_BYTES (map.m_len); flags =0 ;if (compr_cluster) {
@@ -2026,13 +2016,13 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, count_in_cluster += map.m_len;if (count_in_cluster == cluster_size) { compr_cluster = false; - size +=blks_to_bytes (inode,1 ); + size +=F2FS_BLKSIZE ; } }else if (map.m_flags &F2FS_MAP_DELALLOC ) { flags =FIEMAP_EXTENT_UNWRITTEN ; } - start_blk +=bytes_to_blks (inode, size); + start_blk +=F2FS_BYTES_TO_BLK (size); } prep_next:
@@ -2070,7 +2060,7 @@ static int f2fs_read_single_page(struct inode *inode, struct folio *folio, struct readahead_control *rac) {struct bio *bio = *bio_ret; -const unsigned blocksize =blks_to_bytes (inode,1 ); +const unsigned int blocksize =F2FS_BLKSIZE ;sector_t block_in_file;sector_t last_block;sector_t last_block_in_file;
@@ -2080,8 +2070,8 @@ static int f2fs_read_single_page(struct inode *inode, struct folio *folio, block_in_file = (sector_t )index; last_block = block_in_file + nr_pages; - last_block_in_file =bytes_to_blks (inode, -f2fs_readpage_limit (inode) + blocksize -1 ); + last_block_in_file =F2FS_BYTES_TO_BLK (f2fs_readpage_limit (inode) + + blocksize -1 );if (last_block > last_block_in_file) last_block = last_block_in_file;
@@ -2181,7 +2171,7 @@ int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,struct bio *bio = *bio_ret;unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;sector_t last_block_in_file; -const unsigned blocksize =blks_to_bytes (inode,1 ); +const unsigned int blocksize =F2FS_BLKSIZE ;struct decompress_io_ctx *dic =NULL ;struct extent_info ei = {};bool from_dnode = true;
@@ -2190,8 +2180,8 @@ int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,f2fs_bug_on (sbi,f2fs_cluster_is_empty (cc)); - last_block_in_file =bytes_to_blks (inode, -f2fs_readpage_limit (inode) + blocksize -1 ); + last_block_in_file =F2FS_BYTES_TO_BLK (f2fs_readpage_limit (inode) + + blocksize -1 );/* get rid of pages beyond EOF */ for (i =0 ; i < cc->cluster_size; i++) {
@@ -3957,7 +3947,7 @@ static int check_swap_activate(struct swap_info_struct *sis,* to be very smart. */ cur_lblock =0 ; - last_lblock =bytes_to_blks (inode,i_size_read (inode)); + last_lblock =F2FS_BYTES_TO_BLK (i_size_read (inode));while (cur_lblock < last_lblock && cur_lblock < sis->max) {struct f2fs_map_blocks map;
@@ -4200,8 +4190,8 @@ static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,pgoff_t next_pgofs =0 ;int err; - map.m_lblk =bytes_to_blks (inode, offset); - map.m_len =bytes_to_blks (inode, offset + length -1 ) - map.m_lblk +1 ; + map.m_lblk =F2FS_BYTES_TO_BLK (offset); + map.m_len =F2FS_BYTES_TO_BLK (offset + length -1 ) - map.m_lblk +1 ; map.m_next_pgofs = &next_pgofs; map.m_seg_type =f2fs_rw_hint_to_seg_type (F2FS_I_SB (inode), inode->i_write_hint);
@@ -4212,7 +4202,7 @@ static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,if (err)return err; - iomap->offset =blks_to_bytes (inode, map.m_lblk); + iomap->offset =F2FS_BLK_TO_BYTES (map.m_lblk);/* * When inline encryption is enabled, sometimes I/O to an encrypted file
@@ -4232,21 +4222,21 @@ static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,if (WARN_ON_ONCE (map.m_pblk ==NEW_ADDR ))return -EINVAL ; - iomap->length =blks_to_bytes (inode, map.m_len); + iomap->length =F2FS_BLK_TO_BYTES (map.m_len); iomap->type =IOMAP_MAPPED ; iomap->flags |=IOMAP_F_MERGED ; iomap->bdev = map.m_bdev; - iomap->addr =blks_to_bytes (inode, map.m_pblk); + iomap->addr =F2FS_BLK_TO_BYTES (map.m_pblk); }else {if (flags &IOMAP_WRITE )return -ENOTBLK ;if (map.m_pblk ==NULL_ADDR ) { - iomap->length =blks_to_bytes (inode, next_pgofs) - - iomap->offset; + iomap->length =F2FS_BLK_TO_BYTES (next_pgofs) - + iomap->offset; iomap->type =IOMAP_HOLE ; }else if (map.m_pblk ==NEW_ADDR ) { - iomap->length =blks_to_bytes (inode, map.m_len); + iomap->length =F2FS_BLK_TO_BYTES (map.m_len); iomap->type =IOMAP_UNWRITTEN ; }else {f2fs_bug_on (F2FS_I_SB (inode),1 );
Signature · 1 line
2.40.1
Chao Yu
maintainerPATCH 3/4 f2fs: fix to adjust appropriate length for fiemap
In reply to Chao YuFrom: Zhiguo Niu <[email protected]>
If user give a file size as "length" parameter for fiemap operations, but if this size is non-block size aligned, it will show 2 segments fiemap results even this whole file is contiguous on disk, such as the following results:
./f2fs_io fiemap 0 19034 ylog/analyzer.py
Fiemap: offset = 0 len = 19034
logical addr. physical addr. length flags
0 0000000000000000 0000000020baa000 0000000000004000 00001000
1 0000000000004000 0000000020bae000 0000000000001000 00001001
after this patch:
./f2fs_io fiemap 0 19034 ylog/analyzer.py
Fiemap: offset = 0 len = 19034
logical addr. physical addr. length flags
0 0000000000000000 00000000315f3000 0000000000005000 00001001
--- fs/f2fs/data.c | 6 +++--- include/linux/f2fs_fs.h | 1 + 2 files changed, 4 insertions(+), 3 deletions(-)
fs/f2fs/data.c +3 -3
@@ -1938,12 +1938,12 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, goto out; } -if (F2FS_BYTES_TO_BLK (len) ==0 ) - len =F2FS_BLKSIZE ; - start_blk =F2FS_BYTES_TO_BLK (start); last_blk =F2FS_BYTES_TO_BLK (start + len -1 ); +if (len &F2FS_BLKSIZE_MASK ) + len =round_up (len,F2FS_BLKSIZE ); + next:memset (&map,0 ,sizeof (map)); map.m_lblk = start_blk;
include/linux/f2fs_fs.h +1 -0
@@ -24,6 +24,7 @@#define NEW_ADDR ((block_t)-1)/* used as block_t addresses */ #define COMPRESS_ADDR ((block_t)-2)/* used as compressed data flag */ +#define F2FS_BLKSIZE_MASK (F2FS_BLKSIZE - 1)#define F2FS_BYTES_TO_BLK (bytes) ((unsigned long long)(bytes) >> F2FS_BLKSIZE_BITS)#define F2FS_BLK_TO_BYTES (blk) ((unsigned long long)(blk) << F2FS_BLKSIZE_BITS)#define F2FS_BLK_END_BYTES (blk) (F2FS_BLK_TO_BYTES(blk + 1) - 1)
Signature · 1 line
2.40.1
Chao Yu
maintainerPATCH 4/4 f2fs: fix to requery extent which cross boundary of inquiry
In reply to Chao Yudd if=/dev/zero of=file bs=4k count=5xfs_io file -c "fiemap -v 2 16384"
file:
EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS
0: [0..31]: 139272..139303 32 0x1000
1: [32..39]: 139304..139311 8 0x1001
xfs_io file -c "fiemap -v 0 16384"
file:
EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS
0: [0..31]: 139272..139303 32 0x1000
xfs_io file -c "fiemap -v 0 16385"
file:
EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS
0: [0..39]: 139272..139311 40 0x1001There are two problems:
- continuous extent is split to two
- FIEMAP_EXTENT_LAST is missing in last extent
The root cause is: if upper boundary of inquiry crosses extent, f2fs_map_blocks() will truncate length of returned extent to F2FS_BYTES_TO_BLK(len), and also, it will stop to query latter extent or hole to make sure current extent is last or not.
In order to fix this issue, once we found an extent locates in the end of inquiry range by f2fs_map_blocks(), we need to expand inquiry range to requiry.
--- fs/f2fs/data.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
fs/f2fs/data.c +15 -5
@@ -1896,7 +1896,7 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, u64 start, u64 len) {struct f2fs_map_blocks map; -sector_t start_blk, last_blk; +sector_t start_blk, last_blk, blk_len, max_len;pgoff_t next_pgofs;u64 logical =0 , phys =0 , size =0 ;u32 flags =0 ;
@@ -1940,14 +1940,13 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, start_blk =F2FS_BYTES_TO_BLK (start); last_blk =F2FS_BYTES_TO_BLK (start + len -1 ); - -if (len &F2FS_BLKSIZE_MASK ) - len =round_up (len,F2FS_BLKSIZE ); + blk_len = last_blk - start_blk +1 ; + max_len =F2FS_BYTES_TO_BLK (maxbytes) - start_blk; next:memset (&map,0 ,sizeof (map)); map.m_lblk = start_blk; - map.m_len =F2FS_BYTES_TO_BLK (len); + map.m_len = blk_len; map.m_next_pgofs = &next_pgofs; map.m_seg_type =NO_CHECK_TYPE ;
@@ -1970,6 +1969,17 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, flags |=FIEMAP_EXTENT_LAST ; } +/* +* current extent may cross boundary of inquiry, increase len to +* requery. +*/ +if (!compr_cluster && (map.m_flags &F2FS_MAP_MAPPED ) && + map.m_lblk + map.m_len -1 == last_blk && + blk_len != max_len) { + blk_len = max_len; + goto next; + } + compr_appended = false;/* In a case of compressed cluster, append this to the last extent */ if (compr_cluster && ((map.m_flags & F2FS_MAP_DELALLOC) ||
Signature · 1 line
2.40.1
Jaegeuk Kim
maintainerI applied this with the below change to avoid build failure.
fs/f2fs/super.c +1 -1
@@ -3336,7 +3336,7 @@ loff_t max_file_blocks(struct inode *inode)* fit within U32_MAX + 1 data units. */ - result =min (result,F2FS_BYTES_TO_BLK (((loff_t )U32_MAX +1 ) *4096 )); + result =umin (result,F2FS_BYTES_TO_BLK (((loff_t )U32_MAX +1 ) *4096 ));return result; }
Quoted 29 lines · Chao Yu
It missed to cast variable to unsigned long long type before bit shift, which will cause overflow, fix it.
Fixes f7ef9b83b583 ("f2fs: introduce macros to convert bytes and blocks in f2fs")Signed-off-by Chao Yu <[email protected]>--- include/linux/f2fs_fs.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)include/linux/f2fs_fs.h +3
-3@@ -24,10 +24,10 @@#define NEW_ADDR ((block_t)-1)/* used as block_t addresses */ #define COMPRESS_ADDR ((block_t)-2)/* used as compressed data flag */ -#define F2FS_BYTES_TO_BLK (bytes) ((bytes) >> F2FS_BLKSIZE_BITS) -#define F2FS_BLK_TO_BYTES (blk) ((blk) << F2FS_BLKSIZE_BITS) +#define F2FS_BYTES_TO_BLK (bytes) ((unsigned long long)(bytes) >> F2FS_BLKSIZE_BITS) +#define F2FS_BLK_TO_BYTES (blk) ((unsigned long long)(blk) << F2FS_BLKSIZE_BITS)#define F2FS_BLK_END_BYTES (blk) (F2FS_BLK_TO_BYTES(blk + 1) - 1) -#define F2FS_BLK_ALIGN (x) (F2FS_BYTES_TO_BLK((x) + F2FS_BLKSIZE - 1)) +#define F2FS_BLK_ALIGN (x) (F2FS_BYTES_TO_BLK((x) + F2FS_BLKSIZE - 1))/* 0, 1(node nid), 2(meta nid) are reserved node id */ #define F2FS_RESERVED_NODE_NUM 3Signature · 1 line
2.40.1
Chao Yu
maintainerQuoted 1 line · Jaegeuk Kim
I applied this with the below change to avoid build failure.
Thank you!
Thanks,
Quoted 43 lines
fs/f2fs/super.c +1
-1@@ -3336,7 +3336,7 @@ loff_t max_file_blocks(struct inode *inode)* fit within U32_MAX + 1 data units. */ - result =min (result,F2FS_BYTES_TO_BLK (((loff_t )U32_MAX +1 ) *4096 )); + result =umin (result,F2FS_BYTES_TO_BLK (((loff_t )U32_MAX +1 ) *4096 ));return result; }Quoted 29 lines · Chao Yu
It missed to cast variable to unsigned long long type before bit shift, which will cause overflow, fix it.
Fixes f7ef9b83b583 ("f2fs: introduce macros to convert bytes and blocks in f2fs")Signed-off-by Chao Yu <[email protected]>--- include/linux/f2fs_fs.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)include/linux/f2fs_fs.h +3
-3@@ -24,10 +24,10 @@#define NEW_ADDR ((block_t)-1)/* used as block_t addresses */ #define COMPRESS_ADDR ((block_t)-2)/* used as compressed data flag */ -#define F2FS_BYTES_TO_BLK (bytes) ((bytes) >> F2FS_BLKSIZE_BITS) -#define F2FS_BLK_TO_BYTES (blk) ((blk) << F2FS_BLKSIZE_BITS) +#define F2FS_BYTES_TO_BLK (bytes) ((unsigned long long)(bytes) >> F2FS_BLKSIZE_BITS) +#define F2FS_BLK_TO_BYTES (blk) ((unsigned long long)(blk) << F2FS_BLKSIZE_BITS)#define F2FS_BLK_END_BYTES (blk) (F2FS_BLK_TO_BYTES(blk + 1) - 1) -#define F2FS_BLK_ALIGN (x) (F2FS_BYTES_TO_BLK((x) + F2FS_BLKSIZE - 1)) +#define F2FS_BLK_ALIGN (x) (F2FS_BYTES_TO_BLK((x) + F2FS_BLKSIZE - 1))/* 0, 1(node nid), 2(meta nid) are reserved node id */ #define F2FS_RESERVED_NODE_NUM 3Signature · 1 line
2.40.1
This series was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:
Hello:
This series was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:
Quoted 8 lines · On Fri, 8 Nov 2024 09:25:54 +0800 you wrote:
It missed to cast variable to unsigned long long type before bit shift, which will cause overflow, fix it.
Fixes f7ef9b83b583 ("f2fs: introduce macros to convert bytes and blocks in f2fs")Signed-off-by Chao Yu <[email protected]>--- include/linux/f2fs_fs.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Here is the summary with links:
- [f2fs-dev,1/4] f2fs: fix to do cast in F2FS_{BLK_TO_BYTES, BTYES_TO_BLK} to avoid overflow (no matching commit)
- [f2fs-dev,2/4] f2fs: clean up w/ F2FS_{BLK_TO_BYTES, BTYES_TO_BLK}
https://git.kernel.org/jaegeuk/f2fs/c/7461f3709418 - [f2fs-dev,3/4] f2fs: fix to adjust appropriate length for fiemap https://git.kernel.org/jaegeuk/f2fs/c/77569f785c86 - [f2fs-dev,4/4] f2fs: fix to requery extent which cross boundary of inquiry https://git.kernel.org/jaegeuk/f2fs/c/6787a8224585
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html