Skip to content

Commit 2ffd2a6

Browse files
Kemeng Shitytso
authored andcommitted
ext4: remove unnecessary parameter "needed" in ext4_discard_preallocations
The "needed" controls the number of ext4_prealloc_space to discard in ext4_discard_preallocations. Function ext4_discard_preallocations is supposed to discard all non-used preallocated blocks when "needed" is 0 and now ext4_discard_preallocations is always called with "needed" = 0. Remove unnecessary parameter "needed" and remove all non-used preallocated spaces in ext4_discard_preallocations to simplify the code. Note: If count of non-used preallocated spaces could be more than UINT_MAX, there was a memory leak as some non-used preallocated spaces are left ununsed and this commit will fix it. Otherwise, there is no behavior change. Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com> Reviewed-by: Jan Kara <jack@suse.cz> Link: https://lore.kernel.org/r/20240105092102.496631-9-shikemeng@huaweicloud.com Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 2042794 commit 2ffd2a6

File tree

9 files changed

+18
-22
lines changed

9 files changed

+18
-22
lines changed

fs/ext4/ext4.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2915,7 +2915,7 @@ extern int ext4_mb_init(struct super_block *);
29152915
extern void ext4_mb_release(struct super_block *);
29162916
extern ext4_fsblk_t ext4_mb_new_blocks(handle_t *,
29172917
struct ext4_allocation_request *, int *);
2918-
extern void ext4_discard_preallocations(struct inode *, unsigned int);
2918+
extern void ext4_discard_preallocations(struct inode *);
29192919
extern int __init ext4_init_mballoc(void);
29202920
extern void ext4_exit_mballoc(void);
29212921
extern ext4_group_t ext4_mb_prefetch(struct super_block *sb,

fs/ext4/extents.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped)
100100
* i_rwsem. So we can safely drop the i_data_sem here.
101101
*/
102102
BUG_ON(EXT4_JOURNAL(inode) == NULL);
103-
ext4_discard_preallocations(inode, 0);
103+
ext4_discard_preallocations(inode);
104104
up_write(&EXT4_I(inode)->i_data_sem);
105105
*dropped = 1;
106106
return 0;
@@ -4313,7 +4313,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
43134313
* not a good idea to call discard here directly,
43144314
* but otherwise we'd need to call it every free().
43154315
*/
4316-
ext4_discard_preallocations(inode, 0);
4316+
ext4_discard_preallocations(inode);
43174317
if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
43184318
fb_flags = EXT4_FREE_BLOCKS_NO_QUOT_UPDATE;
43194319
ext4_free_blocks(handle, inode, NULL, newblock,
@@ -5357,15 +5357,15 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)
53575357
ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle);
53585358

53595359
down_write(&EXT4_I(inode)->i_data_sem);
5360-
ext4_discard_preallocations(inode, 0);
5360+
ext4_discard_preallocations(inode);
53615361
ext4_es_remove_extent(inode, punch_start, EXT_MAX_BLOCKS - punch_start);
53625362

53635363
ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
53645364
if (ret) {
53655365
up_write(&EXT4_I(inode)->i_data_sem);
53665366
goto out_stop;
53675367
}
5368-
ext4_discard_preallocations(inode, 0);
5368+
ext4_discard_preallocations(inode);
53695369

53705370
ret = ext4_ext_shift_extents(inode, handle, punch_stop,
53715371
punch_stop - punch_start, SHIFT_LEFT);
@@ -5497,7 +5497,7 @@ static int ext4_insert_range(struct file *file, loff_t offset, loff_t len)
54975497
goto out_stop;
54985498

54995499
down_write(&EXT4_I(inode)->i_data_sem);
5500-
ext4_discard_preallocations(inode, 0);
5500+
ext4_discard_preallocations(inode);
55015501

55025502
path = ext4_find_extent(inode, offset_lblk, NULL, 0);
55035503
if (IS_ERR(path)) {

fs/ext4/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static int ext4_release_file(struct inode *inode, struct file *filp)
174174
(atomic_read(&inode->i_writecount) == 1) &&
175175
!EXT4_I(inode)->i_reserved_data_blocks) {
176176
down_write(&EXT4_I(inode)->i_data_sem);
177-
ext4_discard_preallocations(inode, 0);
177+
ext4_discard_preallocations(inode);
178178
up_write(&EXT4_I(inode)->i_data_sem);
179179
}
180180
if (is_dx(inode) && filp->private_data)

fs/ext4/indirect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ static int ext4_ind_trunc_restart_fn(handle_t *handle, struct inode *inode,
714714
* i_rwsem. So we can safely drop the i_data_sem here.
715715
*/
716716
BUG_ON(EXT4_JOURNAL(inode) == NULL);
717-
ext4_discard_preallocations(inode, 0);
717+
ext4_discard_preallocations(inode);
718718
up_write(&EXT4_I(inode)->i_data_sem);
719719
*dropped = 1;
720720
return 0;

fs/ext4/inode.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ void ext4_da_update_reserve_space(struct inode *inode,
371371
*/
372372
if ((ei->i_reserved_data_blocks == 0) &&
373373
!inode_is_open_for_write(inode))
374-
ext4_discard_preallocations(inode, 0);
374+
ext4_discard_preallocations(inode);
375375
}
376376

377377
static int __check_block_validity(struct inode *inode, const char *func,
@@ -4017,7 +4017,7 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
40174017
if (stop_block > first_block) {
40184018

40194019
down_write(&EXT4_I(inode)->i_data_sem);
4020-
ext4_discard_preallocations(inode, 0);
4020+
ext4_discard_preallocations(inode);
40214021

40224022
ext4_es_remove_extent(inode, first_block,
40234023
stop_block - first_block);
@@ -4170,7 +4170,7 @@ int ext4_truncate(struct inode *inode)
41704170

41714171
down_write(&EXT4_I(inode)->i_data_sem);
41724172

4173-
ext4_discard_preallocations(inode, 0);
4173+
ext4_discard_preallocations(inode);
41744174

41754175
if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
41764176
err = ext4_ext_truncate(handle, inode);

fs/ext4/ioctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ static long swap_inode_boot_loader(struct super_block *sb,
467467
ext4_reset_inode_seed(inode);
468468
ext4_reset_inode_seed(inode_bl);
469469

470-
ext4_discard_preallocations(inode, 0);
470+
ext4_discard_preallocations(inode);
471471

472472
err = ext4_mark_inode_dirty(handle, inode);
473473
if (err < 0) {

fs/ext4/mballoc.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5498,7 +5498,7 @@ ext4_mb_discard_group_preallocations(struct super_block *sb,
54985498
*
54995499
* FIXME!! Make sure it is valid at all the call sites
55005500
*/
5501-
void ext4_discard_preallocations(struct inode *inode, unsigned int needed)
5501+
void ext4_discard_preallocations(struct inode *inode)
55025502
{
55035503
struct ext4_inode_info *ei = EXT4_I(inode);
55045504
struct super_block *sb = inode->i_sb;
@@ -5520,15 +5520,12 @@ void ext4_discard_preallocations(struct inode *inode, unsigned int needed)
55205520
mb_debug(sb, "discard preallocation for inode %lu\n",
55215521
inode->i_ino);
55225522
trace_ext4_discard_preallocations(inode,
5523-
atomic_read(&ei->i_prealloc_active), needed);
5524-
5525-
if (needed == 0)
5526-
needed = UINT_MAX;
5523+
atomic_read(&ei->i_prealloc_active), 0);
55275524

55285525
repeat:
55295526
/* first, collect all pa's in the inode */
55305527
write_lock(&ei->i_prealloc_lock);
5531-
for (iter = rb_first(&ei->i_prealloc_node); iter && needed;
5528+
for (iter = rb_first(&ei->i_prealloc_node); iter;
55325529
iter = rb_next(iter)) {
55335530
pa = rb_entry(iter, struct ext4_prealloc_space,
55345531
pa_node.inode_node);
@@ -5552,7 +5549,6 @@ void ext4_discard_preallocations(struct inode *inode, unsigned int needed)
55525549
spin_unlock(&pa->pa_lock);
55535550
rb_erase(&pa->pa_node.inode_node, &ei->i_prealloc_node);
55545551
list_add(&pa->u.pa_tmp_list, &list);
5555-
needed--;
55565552
continue;
55575553
}
55585554

fs/ext4/move_extent.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,8 +686,8 @@ ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
686686

687687
out:
688688
if (*moved_len) {
689-
ext4_discard_preallocations(orig_inode, 0);
690-
ext4_discard_preallocations(donor_inode, 0);
689+
ext4_discard_preallocations(orig_inode);
690+
ext4_discard_preallocations(donor_inode);
691691
}
692692

693693
ext4_free_ext_path(path);

fs/ext4/super.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ void ext4_clear_inode(struct inode *inode)
15251525
ext4_fc_del(inode);
15261526
invalidate_inode_buffers(inode);
15271527
clear_inode(inode);
1528-
ext4_discard_preallocations(inode, 0);
1528+
ext4_discard_preallocations(inode);
15291529
ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
15301530
dquot_drop(inode);
15311531
if (EXT4_I(inode)->jinode) {

0 commit comments

Comments
 (0)