Skip to content

Commit 43bbddc

Browse files
LiBaokun96tytso
authored andcommitted
ext4: add two helper functions extent_logical_end() and pa_logical_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, extent_logical_end() and pa_logical_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]> Reviewed-by: Ritesh Harjani (IBM) <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]>
1 parent 6eaae19 commit 43bbddc

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

fs/ext4/mballoc.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4432,7 +4432,7 @@ ext4_mb_normalize_request(struct ext4_allocation_context *ac,
44324432

44334433
/* first, let's learn actual file size
44344434
* given current request is allocated */
4435-
size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
4435+
size = extent_logical_end(sbi, &ac->ac_o_ex);
44364436
size = size << bsbits;
44374437
if (size < i_size_read(ac->ac_inode))
44384438
size = i_size_read(ac->ac_inode);
@@ -4766,7 +4766,6 @@ ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
47664766
struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
47674767
struct ext4_locality_group *lg;
47684768
struct ext4_prealloc_space *tmp_pa = NULL, *cpa = NULL;
4769-
loff_t tmp_pa_end;
47704769
struct rb_node *iter;
47714770
ext4_fsblk_t goal_block;
47724771

@@ -4862,9 +4861,7 @@ ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
48624861
* pa can possibly satisfy the request hence check if it overlaps
48634862
* original logical start and stop searching if it doesn't.
48644863
*/
4865-
tmp_pa_end = (loff_t)tmp_pa->pa_lstart + EXT4_C2B(sbi, tmp_pa->pa_len);
4866-
4867-
if (ac->ac_o_ex.fe_logical >= tmp_pa_end) {
4864+
if (ac->ac_o_ex.fe_logical >= pa_logical_end(sbi, tmp_pa)) {
48684865
spin_unlock(&tmp_pa->pa_lock);
48694866
goto try_group_pa;
48704867
}
@@ -5769,7 +5766,7 @@ static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
57695766

57705767
group_pa_eligible = sbi->s_mb_group_prealloc > 0;
57715768
inode_pa_eligible = true;
5772-
size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
5769+
size = extent_logical_end(sbi, &ac->ac_o_ex);
57735770
isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
57745771
>> bsbits;
57755772

fs/ext4/mballoc.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,20 @@ 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 extent_logical_end(struct ext4_sb_info *sbi,
237+
struct ext4_free_extent *fex)
238+
{
239+
/* Use loff_t to avoid end exceeding ext4_lblk_t max. */
240+
return (loff_t)fex->fe_logical + EXT4_C2B(sbi, fex->fe_len);
241+
}
242+
243+
static inline loff_t pa_logical_end(struct ext4_sb_info *sbi,
244+
struct ext4_prealloc_space *pa)
245+
{
246+
/* Use loff_t to avoid end exceeding ext4_lblk_t max. */
247+
return (loff_t)pa->pa_lstart + EXT4_C2B(sbi, pa->pa_len);
248+
}
249+
236250
typedef int (*ext4_mballoc_query_range_fn)(
237251
struct super_block *sb,
238252
ext4_group_t agno,

0 commit comments

Comments
 (0)