Commit 2ffd2a6a authored by Kemeng Shi's avatar Kemeng Shi Committed by Theodore Ts'o

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: default avatarKemeng Shi <shikemeng@huaweicloud.com>
Reviewed-by: default avatarJan Kara <jack@suse.cz>
Link: https://lore.kernel.org/r/20240105092102.496631-9-shikemeng@huaweicloud.comSigned-off-by: default avatarTheodore Ts'o <tytso@mit.edu>
parent 20427949
...@@ -2915,7 +2915,7 @@ extern int ext4_mb_init(struct super_block *); ...@@ -2915,7 +2915,7 @@ extern int ext4_mb_init(struct super_block *);
extern void ext4_mb_release(struct super_block *); extern void ext4_mb_release(struct super_block *);
extern ext4_fsblk_t ext4_mb_new_blocks(handle_t *, extern ext4_fsblk_t ext4_mb_new_blocks(handle_t *,
struct ext4_allocation_request *, int *); struct ext4_allocation_request *, int *);
extern void ext4_discard_preallocations(struct inode *, unsigned int); extern void ext4_discard_preallocations(struct inode *);
extern int __init ext4_init_mballoc(void); extern int __init ext4_init_mballoc(void);
extern void ext4_exit_mballoc(void); extern void ext4_exit_mballoc(void);
extern ext4_group_t ext4_mb_prefetch(struct super_block *sb, extern ext4_group_t ext4_mb_prefetch(struct super_block *sb,
......
...@@ -100,7 +100,7 @@ static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped) ...@@ -100,7 +100,7 @@ static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped)
* i_rwsem. So we can safely drop the i_data_sem here. * i_rwsem. So we can safely drop the i_data_sem here.
*/ */
BUG_ON(EXT4_JOURNAL(inode) == NULL); BUG_ON(EXT4_JOURNAL(inode) == NULL);
ext4_discard_preallocations(inode, 0); ext4_discard_preallocations(inode);
up_write(&EXT4_I(inode)->i_data_sem); up_write(&EXT4_I(inode)->i_data_sem);
*dropped = 1; *dropped = 1;
return 0; return 0;
...@@ -4313,7 +4313,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, ...@@ -4313,7 +4313,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
* not a good idea to call discard here directly, * not a good idea to call discard here directly,
* but otherwise we'd need to call it every free(). * but otherwise we'd need to call it every free().
*/ */
ext4_discard_preallocations(inode, 0); ext4_discard_preallocations(inode);
if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
fb_flags = EXT4_FREE_BLOCKS_NO_QUOT_UPDATE; fb_flags = EXT4_FREE_BLOCKS_NO_QUOT_UPDATE;
ext4_free_blocks(handle, inode, NULL, newblock, ext4_free_blocks(handle, inode, NULL, newblock,
...@@ -5357,7 +5357,7 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len) ...@@ -5357,7 +5357,7 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)
ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle); ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle);
down_write(&EXT4_I(inode)->i_data_sem); down_write(&EXT4_I(inode)->i_data_sem);
ext4_discard_preallocations(inode, 0); ext4_discard_preallocations(inode);
ext4_es_remove_extent(inode, punch_start, EXT_MAX_BLOCKS - punch_start); ext4_es_remove_extent(inode, punch_start, EXT_MAX_BLOCKS - punch_start);
ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1); ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
...@@ -5365,7 +5365,7 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len) ...@@ -5365,7 +5365,7 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)
up_write(&EXT4_I(inode)->i_data_sem); up_write(&EXT4_I(inode)->i_data_sem);
goto out_stop; goto out_stop;
} }
ext4_discard_preallocations(inode, 0); ext4_discard_preallocations(inode);
ret = ext4_ext_shift_extents(inode, handle, punch_stop, ret = ext4_ext_shift_extents(inode, handle, punch_stop,
punch_stop - punch_start, SHIFT_LEFT); punch_stop - punch_start, SHIFT_LEFT);
...@@ -5497,7 +5497,7 @@ static int ext4_insert_range(struct file *file, loff_t offset, loff_t len) ...@@ -5497,7 +5497,7 @@ static int ext4_insert_range(struct file *file, loff_t offset, loff_t len)
goto out_stop; goto out_stop;
down_write(&EXT4_I(inode)->i_data_sem); down_write(&EXT4_I(inode)->i_data_sem);
ext4_discard_preallocations(inode, 0); ext4_discard_preallocations(inode);
path = ext4_find_extent(inode, offset_lblk, NULL, 0); path = ext4_find_extent(inode, offset_lblk, NULL, 0);
if (IS_ERR(path)) { if (IS_ERR(path)) {
......
...@@ -174,7 +174,7 @@ static int ext4_release_file(struct inode *inode, struct file *filp) ...@@ -174,7 +174,7 @@ static int ext4_release_file(struct inode *inode, struct file *filp)
(atomic_read(&inode->i_writecount) == 1) && (atomic_read(&inode->i_writecount) == 1) &&
!EXT4_I(inode)->i_reserved_data_blocks) { !EXT4_I(inode)->i_reserved_data_blocks) {
down_write(&EXT4_I(inode)->i_data_sem); down_write(&EXT4_I(inode)->i_data_sem);
ext4_discard_preallocations(inode, 0); ext4_discard_preallocations(inode);
up_write(&EXT4_I(inode)->i_data_sem); up_write(&EXT4_I(inode)->i_data_sem);
} }
if (is_dx(inode) && filp->private_data) if (is_dx(inode) && filp->private_data)
......
...@@ -714,7 +714,7 @@ static int ext4_ind_trunc_restart_fn(handle_t *handle, struct inode *inode, ...@@ -714,7 +714,7 @@ static int ext4_ind_trunc_restart_fn(handle_t *handle, struct inode *inode,
* i_rwsem. So we can safely drop the i_data_sem here. * i_rwsem. So we can safely drop the i_data_sem here.
*/ */
BUG_ON(EXT4_JOURNAL(inode) == NULL); BUG_ON(EXT4_JOURNAL(inode) == NULL);
ext4_discard_preallocations(inode, 0); ext4_discard_preallocations(inode);
up_write(&EXT4_I(inode)->i_data_sem); up_write(&EXT4_I(inode)->i_data_sem);
*dropped = 1; *dropped = 1;
return 0; return 0;
......
...@@ -371,7 +371,7 @@ void ext4_da_update_reserve_space(struct inode *inode, ...@@ -371,7 +371,7 @@ void ext4_da_update_reserve_space(struct inode *inode,
*/ */
if ((ei->i_reserved_data_blocks == 0) && if ((ei->i_reserved_data_blocks == 0) &&
!inode_is_open_for_write(inode)) !inode_is_open_for_write(inode))
ext4_discard_preallocations(inode, 0); ext4_discard_preallocations(inode);
} }
static int __check_block_validity(struct inode *inode, const char *func, 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) ...@@ -4017,7 +4017,7 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
if (stop_block > first_block) { if (stop_block > first_block) {
down_write(&EXT4_I(inode)->i_data_sem); down_write(&EXT4_I(inode)->i_data_sem);
ext4_discard_preallocations(inode, 0); ext4_discard_preallocations(inode);
ext4_es_remove_extent(inode, first_block, ext4_es_remove_extent(inode, first_block,
stop_block - first_block); stop_block - first_block);
...@@ -4170,7 +4170,7 @@ int ext4_truncate(struct inode *inode) ...@@ -4170,7 +4170,7 @@ int ext4_truncate(struct inode *inode)
down_write(&EXT4_I(inode)->i_data_sem); down_write(&EXT4_I(inode)->i_data_sem);
ext4_discard_preallocations(inode, 0); ext4_discard_preallocations(inode);
if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
err = ext4_ext_truncate(handle, inode); err = ext4_ext_truncate(handle, inode);
......
...@@ -467,7 +467,7 @@ static long swap_inode_boot_loader(struct super_block *sb, ...@@ -467,7 +467,7 @@ static long swap_inode_boot_loader(struct super_block *sb,
ext4_reset_inode_seed(inode); ext4_reset_inode_seed(inode);
ext4_reset_inode_seed(inode_bl); ext4_reset_inode_seed(inode_bl);
ext4_discard_preallocations(inode, 0); ext4_discard_preallocations(inode);
err = ext4_mark_inode_dirty(handle, inode); err = ext4_mark_inode_dirty(handle, inode);
if (err < 0) { if (err < 0) {
......
...@@ -5498,7 +5498,7 @@ ext4_mb_discard_group_preallocations(struct super_block *sb, ...@@ -5498,7 +5498,7 @@ ext4_mb_discard_group_preallocations(struct super_block *sb,
* *
* FIXME!! Make sure it is valid at all the call sites * FIXME!! Make sure it is valid at all the call sites
*/ */
void ext4_discard_preallocations(struct inode *inode, unsigned int needed) void ext4_discard_preallocations(struct inode *inode)
{ {
struct ext4_inode_info *ei = EXT4_I(inode); struct ext4_inode_info *ei = EXT4_I(inode);
struct super_block *sb = inode->i_sb; struct super_block *sb = inode->i_sb;
...@@ -5520,15 +5520,12 @@ void ext4_discard_preallocations(struct inode *inode, unsigned int needed) ...@@ -5520,15 +5520,12 @@ void ext4_discard_preallocations(struct inode *inode, unsigned int needed)
mb_debug(sb, "discard preallocation for inode %lu\n", mb_debug(sb, "discard preallocation for inode %lu\n",
inode->i_ino); inode->i_ino);
trace_ext4_discard_preallocations(inode, trace_ext4_discard_preallocations(inode,
atomic_read(&ei->i_prealloc_active), needed); atomic_read(&ei->i_prealloc_active), 0);
if (needed == 0)
needed = UINT_MAX;
repeat: repeat:
/* first, collect all pa's in the inode */ /* first, collect all pa's in the inode */
write_lock(&ei->i_prealloc_lock); write_lock(&ei->i_prealloc_lock);
for (iter = rb_first(&ei->i_prealloc_node); iter && needed; for (iter = rb_first(&ei->i_prealloc_node); iter;
iter = rb_next(iter)) { iter = rb_next(iter)) {
pa = rb_entry(iter, struct ext4_prealloc_space, pa = rb_entry(iter, struct ext4_prealloc_space,
pa_node.inode_node); pa_node.inode_node);
...@@ -5552,7 +5549,6 @@ void ext4_discard_preallocations(struct inode *inode, unsigned int needed) ...@@ -5552,7 +5549,6 @@ void ext4_discard_preallocations(struct inode *inode, unsigned int needed)
spin_unlock(&pa->pa_lock); spin_unlock(&pa->pa_lock);
rb_erase(&pa->pa_node.inode_node, &ei->i_prealloc_node); rb_erase(&pa->pa_node.inode_node, &ei->i_prealloc_node);
list_add(&pa->u.pa_tmp_list, &list); list_add(&pa->u.pa_tmp_list, &list);
needed--;
continue; continue;
} }
......
...@@ -686,8 +686,8 @@ ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk, ...@@ -686,8 +686,8 @@ ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
out: out:
if (*moved_len) { if (*moved_len) {
ext4_discard_preallocations(orig_inode, 0); ext4_discard_preallocations(orig_inode);
ext4_discard_preallocations(donor_inode, 0); ext4_discard_preallocations(donor_inode);
} }
ext4_free_ext_path(path); ext4_free_ext_path(path);
......
...@@ -1525,7 +1525,7 @@ void ext4_clear_inode(struct inode *inode) ...@@ -1525,7 +1525,7 @@ void ext4_clear_inode(struct inode *inode)
ext4_fc_del(inode); ext4_fc_del(inode);
invalidate_inode_buffers(inode); invalidate_inode_buffers(inode);
clear_inode(inode); clear_inode(inode);
ext4_discard_preallocations(inode, 0); ext4_discard_preallocations(inode);
ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS); ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
dquot_drop(inode); dquot_drop(inode);
if (EXT4_I(inode)->jinode) { if (EXT4_I(inode)->jinode) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment