Commit 7ff5fdda authored by Ye Bin's avatar Ye Bin Committed by Theodore Ts'o

ext4: factor out ext4_free_ext_path()

Factor out ext4_free_ext_path() to free extent path. As after previous patch
'ext4_ext_drop_refs()' is only used in 'extents.c', so make it static.
Signed-off-by: default avatarYe Bin <yebin10@huawei.com>
Reviewed-by: default avatarJan Kara <jack@suse.cz>
Link: https://lore.kernel.org/r/20220924021211.3831551-3-yebin10@huawei.comSigned-off-by: default avatarTheodore Ts'o <tytso@mit.edu>
parent b6a750c0
...@@ -3709,7 +3709,7 @@ extern int ext4_ext_insert_extent(handle_t *, struct inode *, ...@@ -3709,7 +3709,7 @@ extern int ext4_ext_insert_extent(handle_t *, struct inode *,
extern struct ext4_ext_path *ext4_find_extent(struct inode *, ext4_lblk_t, extern struct ext4_ext_path *ext4_find_extent(struct inode *, ext4_lblk_t,
struct ext4_ext_path **, struct ext4_ext_path **,
int flags); int flags);
extern void ext4_ext_drop_refs(struct ext4_ext_path *); extern void ext4_free_ext_path(struct ext4_ext_path *);
extern int ext4_ext_check_inode(struct inode *inode); extern int ext4_ext_check_inode(struct inode *inode);
extern ext4_lblk_t ext4_ext_next_allocated_block(struct ext4_ext_path *path); extern ext4_lblk_t ext4_ext_next_allocated_block(struct ext4_ext_path *path);
extern int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, extern int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
......
...@@ -106,6 +106,25 @@ static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped) ...@@ -106,6 +106,25 @@ static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped)
return 0; return 0;
} }
static void ext4_ext_drop_refs(struct ext4_ext_path *path)
{
int depth, i;
if (!path)
return;
depth = path->p_depth;
for (i = 0; i <= depth; i++, path++) {
brelse(path->p_bh);
path->p_bh = NULL;
}
}
void ext4_free_ext_path(struct ext4_ext_path *path)
{
ext4_ext_drop_refs(path);
kfree(path);
}
/* /*
* Make sure 'handle' has at least 'check_cred' credits. If not, restart * Make sure 'handle' has at least 'check_cred' credits. If not, restart
* transaction with 'restart_cred' credits. The function drops i_data_sem * transaction with 'restart_cred' credits. The function drops i_data_sem
...@@ -636,8 +655,7 @@ int ext4_ext_precache(struct inode *inode) ...@@ -636,8 +655,7 @@ int ext4_ext_precache(struct inode *inode)
ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED); ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
out: out:
up_read(&ei->i_data_sem); up_read(&ei->i_data_sem);
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
return ret; return ret;
} }
...@@ -724,19 +742,6 @@ static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path, ...@@ -724,19 +742,6 @@ static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
#define ext4_ext_show_move(inode, path, newblock, level) #define ext4_ext_show_move(inode, path, newblock, level)
#endif #endif
void ext4_ext_drop_refs(struct ext4_ext_path *path)
{
int depth, i;
if (!path)
return;
depth = path->p_depth;
for (i = 0; i <= depth; i++, path++) {
brelse(path->p_bh);
path->p_bh = NULL;
}
}
/* /*
* ext4_ext_binsearch_idx: * ext4_ext_binsearch_idx:
* binary search for the closest index of the given block * binary search for the closest index of the given block
...@@ -955,8 +960,7 @@ ext4_find_extent(struct inode *inode, ext4_lblk_t block, ...@@ -955,8 +960,7 @@ ext4_find_extent(struct inode *inode, ext4_lblk_t block,
return path; return path;
err: err:
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
if (orig_path) if (orig_path)
*orig_path = NULL; *orig_path = NULL;
return ERR_PTR(ret); return ERR_PTR(ret);
...@@ -2174,8 +2178,7 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, ...@@ -2174,8 +2178,7 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
err = ext4_ext_dirty(handle, inode, path + path->p_depth); err = ext4_ext_dirty(handle, inode, path + path->p_depth);
cleanup: cleanup:
ext4_ext_drop_refs(npath); ext4_free_ext_path(npath);
kfree(npath);
return err; return err;
} }
...@@ -3061,8 +3064,7 @@ int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start, ...@@ -3061,8 +3064,7 @@ int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
} }
} }
out: out:
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
path = NULL; path = NULL;
if (err == -EAGAIN) if (err == -EAGAIN)
goto again; goto again;
...@@ -4375,8 +4377,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, ...@@ -4375,8 +4377,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
allocated = map->m_len; allocated = map->m_len;
ext4_ext_show_leaf(inode, path); ext4_ext_show_leaf(inode, path);
out: out:
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
trace_ext4_ext_map_blocks_exit(inode, flags, map, trace_ext4_ext_map_blocks_exit(inode, flags, map,
err ? err : allocated); err ? err : allocated);
...@@ -5245,8 +5246,7 @@ ext4_ext_shift_extents(struct inode *inode, handle_t *handle, ...@@ -5245,8 +5246,7 @@ ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
break; break;
} }
out: out:
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
return ret; return ret;
} }
...@@ -5538,15 +5538,13 @@ static int ext4_insert_range(struct file *file, loff_t offset, loff_t len) ...@@ -5538,15 +5538,13 @@ static int ext4_insert_range(struct file *file, loff_t offset, loff_t len)
EXT4_GET_BLOCKS_METADATA_NOFAIL); EXT4_GET_BLOCKS_METADATA_NOFAIL);
} }
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
if (ret < 0) { if (ret < 0) {
up_write(&EXT4_I(inode)->i_data_sem); up_write(&EXT4_I(inode)->i_data_sem);
goto out_stop; goto out_stop;
} }
} else { } else {
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
} }
ret = ext4_es_remove_extent(inode, offset_lblk, ret = ext4_es_remove_extent(inode, offset_lblk,
...@@ -5766,10 +5764,8 @@ ext4_swap_extents(handle_t *handle, struct inode *inode1, ...@@ -5766,10 +5764,8 @@ ext4_swap_extents(handle_t *handle, struct inode *inode1,
count -= len; count -= len;
repeat: repeat:
ext4_ext_drop_refs(path1); ext4_free_ext_path(path1);
kfree(path1); ext4_free_ext_path(path2);
ext4_ext_drop_refs(path2);
kfree(path2);
path1 = path2 = NULL; path1 = path2 = NULL;
} }
return replaced_count; return replaced_count;
...@@ -5848,8 +5844,7 @@ int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu) ...@@ -5848,8 +5844,7 @@ int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu)
} }
out: out:
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
return err ? err : mapped; return err ? err : mapped;
} }
...@@ -5916,8 +5911,7 @@ int ext4_ext_replay_update_ex(struct inode *inode, ext4_lblk_t start, ...@@ -5916,8 +5911,7 @@ int ext4_ext_replay_update_ex(struct inode *inode, ext4_lblk_t start,
ret = ext4_ext_dirty(NULL, inode, &path[path->p_depth]); ret = ext4_ext_dirty(NULL, inode, &path[path->p_depth]);
up_write(&EXT4_I(inode)->i_data_sem); up_write(&EXT4_I(inode)->i_data_sem);
out: out:
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
ext4_mark_inode_dirty(NULL, inode); ext4_mark_inode_dirty(NULL, inode);
return ret; return ret;
} }
...@@ -5935,8 +5929,7 @@ void ext4_ext_replay_shrink_inode(struct inode *inode, ext4_lblk_t end) ...@@ -5935,8 +5929,7 @@ void ext4_ext_replay_shrink_inode(struct inode *inode, ext4_lblk_t end)
return; return;
ex = path[path->p_depth].p_ext; ex = path[path->p_depth].p_ext;
if (!ex) { if (!ex) {
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
ext4_mark_inode_dirty(NULL, inode); ext4_mark_inode_dirty(NULL, inode);
return; return;
} }
...@@ -5949,8 +5942,7 @@ void ext4_ext_replay_shrink_inode(struct inode *inode, ext4_lblk_t end) ...@@ -5949,8 +5942,7 @@ void ext4_ext_replay_shrink_inode(struct inode *inode, ext4_lblk_t end)
ext4_ext_dirty(NULL, inode, &path[path->p_depth]); ext4_ext_dirty(NULL, inode, &path[path->p_depth]);
up_write(&EXT4_I(inode)->i_data_sem); up_write(&EXT4_I(inode)->i_data_sem);
ext4_mark_inode_dirty(NULL, inode); ext4_mark_inode_dirty(NULL, inode);
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
} }
} }
...@@ -5989,13 +5981,11 @@ int ext4_ext_replay_set_iblocks(struct inode *inode) ...@@ -5989,13 +5981,11 @@ int ext4_ext_replay_set_iblocks(struct inode *inode)
return PTR_ERR(path); return PTR_ERR(path);
ex = path[path->p_depth].p_ext; ex = path[path->p_depth].p_ext;
if (!ex) { if (!ex) {
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
goto out; goto out;
} }
end = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex); end = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
/* Count the number of data blocks */ /* Count the number of data blocks */
cur = 0; cur = 0;
...@@ -6025,30 +6015,26 @@ int ext4_ext_replay_set_iblocks(struct inode *inode) ...@@ -6025,30 +6015,26 @@ int ext4_ext_replay_set_iblocks(struct inode *inode)
if (IS_ERR(path)) if (IS_ERR(path))
goto out; goto out;
numblks += path->p_depth; numblks += path->p_depth;
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
while (cur < end) { while (cur < end) {
path = ext4_find_extent(inode, cur, NULL, 0); path = ext4_find_extent(inode, cur, NULL, 0);
if (IS_ERR(path)) if (IS_ERR(path))
break; break;
ex = path[path->p_depth].p_ext; ex = path[path->p_depth].p_ext;
if (!ex) { if (!ex) {
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
return 0; return 0;
} }
cur = max(cur + 1, le32_to_cpu(ex->ee_block) + cur = max(cur + 1, le32_to_cpu(ex->ee_block) +
ext4_ext_get_actual_len(ex)); ext4_ext_get_actual_len(ex));
ret = skip_hole(inode, &cur); ret = skip_hole(inode, &cur);
if (ret < 0) { if (ret < 0) {
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
break; break;
} }
path2 = ext4_find_extent(inode, cur, NULL, 0); path2 = ext4_find_extent(inode, cur, NULL, 0);
if (IS_ERR(path2)) { if (IS_ERR(path2)) {
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
break; break;
} }
for (i = 0; i <= max(path->p_depth, path2->p_depth); i++) { for (i = 0; i <= max(path->p_depth, path2->p_depth); i++) {
...@@ -6062,10 +6048,8 @@ int ext4_ext_replay_set_iblocks(struct inode *inode) ...@@ -6062,10 +6048,8 @@ int ext4_ext_replay_set_iblocks(struct inode *inode)
if (cmp1 != cmp2 && cmp2 != 0) if (cmp1 != cmp2 && cmp2 != 0)
numblks++; numblks++;
} }
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
ext4_ext_drop_refs(path2); ext4_free_ext_path(path2);
kfree(path);
kfree(path2);
} }
out: out:
...@@ -6092,13 +6076,11 @@ int ext4_ext_clear_bb(struct inode *inode) ...@@ -6092,13 +6076,11 @@ int ext4_ext_clear_bb(struct inode *inode)
return PTR_ERR(path); return PTR_ERR(path);
ex = path[path->p_depth].p_ext; ex = path[path->p_depth].p_ext;
if (!ex) { if (!ex) {
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
return 0; return 0;
} }
end = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex); end = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
cur = 0; cur = 0;
while (cur < end) { while (cur < end) {
...@@ -6117,8 +6099,7 @@ int ext4_ext_clear_bb(struct inode *inode) ...@@ -6117,8 +6099,7 @@ int ext4_ext_clear_bb(struct inode *inode)
ext4_fc_record_regions(inode->i_sb, inode->i_ino, ext4_fc_record_regions(inode->i_sb, inode->i_ino,
0, path[j].p_block, 1, 1); 0, path[j].p_block, 1, 1);
} }
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
} }
ext4_mb_mark_bb(inode->i_sb, map.m_pblk, map.m_len, 0); ext4_mb_mark_bb(inode->i_sb, map.m_pblk, map.m_len, 0);
ext4_fc_record_regions(inode->i_sb, inode->i_ino, ext4_fc_record_regions(inode->i_sb, inode->i_ino,
......
...@@ -667,8 +667,7 @@ static void ext4_es_insert_extent_ext_check(struct inode *inode, ...@@ -667,8 +667,7 @@ static void ext4_es_insert_extent_ext_check(struct inode *inode,
} }
} }
out: out:
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
} }
static void ext4_es_insert_extent_ind_check(struct inode *inode, static void ext4_es_insert_extent_ind_check(struct inode *inode,
......
...@@ -1770,8 +1770,7 @@ static int ext4_fc_replay_add_range(struct super_block *sb, ...@@ -1770,8 +1770,7 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
ret = ext4_ext_insert_extent( ret = ext4_ext_insert_extent(
NULL, inode, &path, &newex, 0); NULL, inode, &path, &newex, 0);
up_write((&EXT4_I(inode)->i_data_sem)); up_write((&EXT4_I(inode)->i_data_sem));
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
if (ret) if (ret)
goto out; goto out;
goto next; goto next;
...@@ -1926,8 +1925,7 @@ static void ext4_fc_set_bitmaps_and_counters(struct super_block *sb) ...@@ -1926,8 +1925,7 @@ static void ext4_fc_set_bitmaps_and_counters(struct super_block *sb)
for (j = 0; j < path->p_depth; j++) for (j = 0; j < path->p_depth; j++)
ext4_mb_mark_bb(inode->i_sb, ext4_mb_mark_bb(inode->i_sb,
path[j].p_block, 1, 1); path[j].p_block, 1, 1);
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
} }
cur += ret; cur += ret;
ext4_mb_mark_bb(inode->i_sb, map.m_pblk, ext4_mb_mark_bb(inode->i_sb, map.m_pblk,
......
...@@ -56,8 +56,7 @@ static int finish_range(handle_t *handle, struct inode *inode, ...@@ -56,8 +56,7 @@ static int finish_range(handle_t *handle, struct inode *inode,
retval = ext4_ext_insert_extent(handle, inode, &path, &newext, 0); retval = ext4_ext_insert_extent(handle, inode, &path, &newext, 0);
err_out: err_out:
up_write((&EXT4_I(inode)->i_data_sem)); up_write((&EXT4_I(inode)->i_data_sem));
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
lb->first_pblock = 0; lb->first_pblock = 0;
return retval; return retval;
} }
......
...@@ -32,8 +32,7 @@ get_ext_path(struct inode *inode, ext4_lblk_t lblock, ...@@ -32,8 +32,7 @@ get_ext_path(struct inode *inode, ext4_lblk_t lblock,
if (IS_ERR(path)) if (IS_ERR(path))
return PTR_ERR(path); return PTR_ERR(path);
if (path[ext_depth(inode)].p_ext == NULL) { if (path[ext_depth(inode)].p_ext == NULL) {
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
*ppath = NULL; *ppath = NULL;
return -ENODATA; return -ENODATA;
} }
...@@ -106,8 +105,7 @@ mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count, ...@@ -106,8 +105,7 @@ mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count,
} }
ret = 1; ret = 1;
out: out:
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
return ret; return ret;
} }
...@@ -691,8 +689,7 @@ ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk, ...@@ -691,8 +689,7 @@ ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
ext4_discard_preallocations(donor_inode, 0); ext4_discard_preallocations(donor_inode, 0);
} }
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
ext4_double_up_write_data_sem(orig_inode, donor_inode); ext4_double_up_write_data_sem(orig_inode, donor_inode);
unlock_two_nondirectories(orig_inode, donor_inode); unlock_two_nondirectories(orig_inode, donor_inode);
......
...@@ -298,16 +298,14 @@ static int ext4_get_verity_descriptor_location(struct inode *inode, ...@@ -298,16 +298,14 @@ static int ext4_get_verity_descriptor_location(struct inode *inode,
last_extent = path[path->p_depth].p_ext; last_extent = path[path->p_depth].p_ext;
if (!last_extent) { if (!last_extent) {
EXT4_ERROR_INODE(inode, "verity file has no extents"); EXT4_ERROR_INODE(inode, "verity file has no extents");
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
return -EFSCORRUPTED; return -EFSCORRUPTED;
} }
end_lblk = le32_to_cpu(last_extent->ee_block) + end_lblk = le32_to_cpu(last_extent->ee_block) +
ext4_ext_get_actual_len(last_extent); ext4_ext_get_actual_len(last_extent);
desc_size_pos = (u64)end_lblk << inode->i_blkbits; desc_size_pos = (u64)end_lblk << inode->i_blkbits;
ext4_ext_drop_refs(path); ext4_free_ext_path(path);
kfree(path);
if (desc_size_pos < sizeof(desc_size_disk)) if (desc_size_pos < sizeof(desc_size_disk))
goto bad; goto bad;
......
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