Skip to content

Commit 245190c

Browse files
LiBaokun96riteshharjani
authored andcommitted
ext4: add two helper functions fex_end() and pa_end()
When we use lstart + len to calculate the end of free extent or prealloc space, it may exceed the maximum value of 4294967295(0xffffffff) supported by ext4_lblk_t and cause overflow, which may lead to various problems. Therefore, we add two helper functions, fex_end() and pa_end(), to limit the type of end to loff_t, and also convert lstart to loff_t for calculation to avoid overflow. Signed-off-by: Baokun Li <[email protected]>
1 parent ccff6d1 commit 245190c

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

fs/ext4/mballoc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4428,7 +4428,7 @@ ext4_mb_normalize_request(struct ext4_allocation_context *ac,
44284428

44294429
/* first, let's learn actual file size
44304430
* given current request is allocated */
4431-
size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
4431+
size = fex_end(sbi, &ac->ac_o_ex, NULL);
44324432
size = size << bsbits;
44334433
if (size < i_size_read(ac->ac_inode))
44344434
size = i_size_read(ac->ac_inode);
@@ -5661,7 +5661,7 @@ static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
56615661

56625662
group_pa_eligible = sbi->s_mb_group_prealloc > 0;
56635663
inode_pa_eligible = true;
5664-
size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
5664+
size = fex_end(sbi, &ac->ac_o_ex, NULL);
56655665
isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
56665666
>> bsbits;
56675667

fs/ext4/mballoc.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,19 @@ static inline ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
233233
(fex->fe_start << EXT4_SB(sb)->s_cluster_bits);
234234
}
235235

236+
static inline loff_t fex_end(struct ext4_sb_info *sbi,
237+
struct ext4_free_extent *fex, ext4_grpblk_t *fe_len)
238+
{
239+
return (loff_t)fex->fe_logical +
240+
EXT4_C2B(sbi, fe_len ? *fe_len : fex->fe_len);
241+
}
242+
243+
static inline loff_t pa_end(struct ext4_sb_info *sbi,
244+
struct ext4_prealloc_space *pa)
245+
{
246+
return (loff_t)pa->pa_lstart + EXT4_C2B(sbi, pa->pa_len);
247+
}
248+
236249
typedef int (*ext4_mballoc_query_range_fn)(
237250
struct super_block *sb,
238251
ext4_group_t agno,

0 commit comments

Comments
 (0)