Skip to content

Commit f7d1331

Browse files
LiBaokun96tytso
authored andcommitted
ext4: get rid of ppath in ext4_ext_insert_extent()
The use of path and ppath is now very confusing, so to make the code more readable, pass path between functions uniformly, and get rid of ppath. To get rid of the ppath in ext4_ext_insert_extent(), the following is done here: * Free the extents path when an error is encountered. * Its caller needs to update ppath if it uses ppath. * Free path when npath is used, free npath when it is not used. * The got_allocated_blocks label in ext4_ext_map_blocks() does not update err now, so err is updated to 0 if the err returned by ext4_ext_search_right() is greater than 0 and is about to enter got_allocated_blocks. No functional changes. Signed-off-by: Baokun Li <[email protected]> Reviewed-by: Jan Kara <[email protected]> Reviewed-by: Ojaswin Mujoo <[email protected]> Tested-by: Ojaswin Mujoo <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Theodore Ts'o <[email protected]>
1 parent a000bc8 commit f7d1331

File tree

4 files changed

+61
-47
lines changed

4 files changed

+61
-47
lines changed

fs/ext4/ext4.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3710,9 +3710,10 @@ extern int ext4_map_blocks(handle_t *handle, struct inode *inode,
37103710
extern int ext4_ext_calc_credits_for_single_extent(struct inode *inode,
37113711
int num,
37123712
struct ext4_ext_path *path);
3713-
extern int ext4_ext_insert_extent(handle_t *, struct inode *,
3714-
struct ext4_ext_path **,
3715-
struct ext4_extent *, int);
3713+
extern struct ext4_ext_path *ext4_ext_insert_extent(
3714+
handle_t *handle, struct inode *inode,
3715+
struct ext4_ext_path *path,
3716+
struct ext4_extent *newext, int gb_flags);
37163717
extern struct ext4_ext_path *ext4_find_extent(struct inode *, ext4_lblk_t,
37173718
struct ext4_ext_path *,
37183719
int flags);

fs/ext4/extents.c

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,31 +1975,32 @@ static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
19751975
* inserts requested extent as new one into the tree,
19761976
* creating new leaf in the no-space case.
19771977
*/
1978-
int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1979-
struct ext4_ext_path **ppath,
1980-
struct ext4_extent *newext, int gb_flags)
1978+
struct ext4_ext_path *
1979+
ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1980+
struct ext4_ext_path *path,
1981+
struct ext4_extent *newext, int gb_flags)
19811982
{
1982-
struct ext4_ext_path *path = *ppath;
19831983
struct ext4_extent_header *eh;
19841984
struct ext4_extent *ex, *fex;
19851985
struct ext4_extent *nearex; /* nearest extent */
1986-
struct ext4_ext_path *npath = NULL;
1987-
int depth, len, err;
1986+
int depth, len, err = 0;
19881987
ext4_lblk_t next;
19891988
int mb_flags = 0, unwritten;
19901989

19911990
if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
19921991
mb_flags |= EXT4_MB_DELALLOC_RESERVED;
19931992
if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
19941993
EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1995-
return -EFSCORRUPTED;
1994+
err = -EFSCORRUPTED;
1995+
goto errout;
19961996
}
19971997
depth = ext_depth(inode);
19981998
ex = path[depth].p_ext;
19991999
eh = path[depth].p_hdr;
20002000
if (unlikely(path[depth].p_hdr == NULL)) {
20012001
EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2002-
return -EFSCORRUPTED;
2002+
err = -EFSCORRUPTED;
2003+
goto errout;
20032004
}
20042005

20052006
/* try to insert block into found extent and return */
@@ -2037,7 +2038,7 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
20372038
err = ext4_ext_get_access(handle, inode,
20382039
path + depth);
20392040
if (err)
2040-
return err;
2041+
goto errout;
20412042
unwritten = ext4_ext_is_unwritten(ex);
20422043
ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
20432044
+ ext4_ext_get_actual_len(newext));
@@ -2062,7 +2063,7 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
20622063
err = ext4_ext_get_access(handle, inode,
20632064
path + depth);
20642065
if (err)
2065-
return err;
2066+
goto errout;
20662067

20672068
unwritten = ext4_ext_is_unwritten(ex);
20682069
ex->ee_block = newext->ee_block;
@@ -2087,21 +2088,26 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
20872088
if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
20882089
next = ext4_ext_next_leaf_block(path);
20892090
if (next != EXT_MAX_BLOCKS) {
2091+
struct ext4_ext_path *npath;
2092+
20902093
ext_debug(inode, "next leaf block - %u\n", next);
2091-
BUG_ON(npath != NULL);
20922094
npath = ext4_find_extent(inode, next, NULL, gb_flags);
2093-
if (IS_ERR(npath))
2094-
return PTR_ERR(npath);
2095+
if (IS_ERR(npath)) {
2096+
err = PTR_ERR(npath);
2097+
goto errout;
2098+
}
20952099
BUG_ON(npath->p_depth != path->p_depth);
20962100
eh = npath[depth].p_hdr;
20972101
if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
20982102
ext_debug(inode, "next leaf isn't full(%d)\n",
20992103
le16_to_cpu(eh->eh_entries));
2104+
ext4_free_ext_path(path);
21002105
path = npath;
21012106
goto has_space;
21022107
}
21032108
ext_debug(inode, "next leaf has no free space(%d,%d)\n",
21042109
le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2110+
ext4_free_ext_path(npath);
21052111
}
21062112

21072113
/*
@@ -2112,12 +2118,8 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
21122118
mb_flags |= EXT4_MB_USE_RESERVED;
21132119
path = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
21142120
path, newext);
2115-
if (IS_ERR(path)) {
2116-
*ppath = NULL;
2117-
err = PTR_ERR(path);
2118-
goto cleanup;
2119-
}
2120-
*ppath = path;
2121+
if (IS_ERR(path))
2122+
return path;
21212123
depth = ext_depth(inode);
21222124
eh = path[depth].p_hdr;
21232125

@@ -2126,7 +2128,7 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
21262128

21272129
err = ext4_ext_get_access(handle, inode, path + depth);
21282130
if (err)
2129-
goto cleanup;
2131+
goto errout;
21302132

21312133
if (!nearex) {
21322134
/* there is no extent in this leaf, create first one */
@@ -2184,17 +2186,20 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
21842186
if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
21852187
ext4_ext_try_to_merge(handle, inode, path, nearex);
21862188

2187-
21882189
/* time to correct all indexes above */
21892190
err = ext4_ext_correct_indexes(handle, inode, path);
21902191
if (err)
2191-
goto cleanup;
2192+
goto errout;
21922193

21932194
err = ext4_ext_dirty(handle, inode, path + path->p_depth);
2195+
if (err)
2196+
goto errout;
21942197

2195-
cleanup:
2196-
ext4_free_ext_path(npath);
2197-
return err;
2198+
return path;
2199+
2200+
errout:
2201+
ext4_free_ext_path(path);
2202+
return ERR_PTR(err);
21982203
}
21992204

22002205
static int ext4_fill_es_cache_info(struct inode *inode,
@@ -3249,24 +3254,29 @@ static int ext4_split_extent_at(handle_t *handle,
32493254
if (split_flag & EXT4_EXT_MARK_UNWRIT2)
32503255
ext4_ext_mark_unwritten(ex2);
32513256

3252-
err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags);
3253-
if (err != -ENOSPC && err != -EDQUOT && err != -ENOMEM)
3257+
path = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
3258+
if (!IS_ERR(path)) {
3259+
*ppath = path;
32543260
goto out;
3261+
}
3262+
*ppath = NULL;
3263+
err = PTR_ERR(path);
3264+
if (err != -ENOSPC && err != -EDQUOT && err != -ENOMEM)
3265+
return err;
32553266

32563267
/*
3257-
* Update path is required because previous ext4_ext_insert_extent()
3258-
* may have freed or reallocated the path. Using EXT4_EX_NOFAIL
3259-
* guarantees that ext4_find_extent() will not return -ENOMEM,
3260-
* otherwise -ENOMEM will cause a retry in do_writepages(), and a
3261-
* WARN_ON may be triggered in ext4_da_update_reserve_space() due to
3262-
* an incorrect ee_len causing the i_reserved_data_blocks exception.
3268+
* Get a new path to try to zeroout or fix the extent length.
3269+
* Using EXT4_EX_NOFAIL guarantees that ext4_find_extent()
3270+
* will not return -ENOMEM, otherwise -ENOMEM will cause a
3271+
* retry in do_writepages(), and a WARN_ON may be triggered
3272+
* in ext4_da_update_reserve_space() due to an incorrect
3273+
* ee_len causing the i_reserved_data_blocks exception.
32633274
*/
3264-
path = ext4_find_extent(inode, ee_block, *ppath,
3275+
path = ext4_find_extent(inode, ee_block, NULL,
32653276
flags | EXT4_EX_NOFAIL);
32663277
if (IS_ERR(path)) {
32673278
EXT4_ERROR_INODE(inode, "Failed split extent on %u, err %ld",
32683279
split, PTR_ERR(path));
3269-
*ppath = NULL;
32703280
return PTR_ERR(path);
32713281
}
32723282
depth = ext_depth(inode);
@@ -3325,7 +3335,7 @@ static int ext4_split_extent_at(handle_t *handle,
33253335
ext4_ext_dirty(handle, inode, path + path->p_depth);
33263336
return err;
33273337
out:
3328-
ext4_ext_show_leaf(inode, *ppath);
3338+
ext4_ext_show_leaf(inode, path);
33293339
return err;
33303340
}
33313341

@@ -4315,6 +4325,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
43154325
get_implied_cluster_alloc(inode->i_sb, map, &ex2, path)) {
43164326
ar.len = allocated = map->m_len;
43174327
newblock = map->m_pblk;
4328+
err = 0;
43184329
goto got_allocated_blocks;
43194330
}
43204331

@@ -4387,8 +4398,9 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
43874398
map->m_flags |= EXT4_MAP_UNWRITTEN;
43884399
}
43894400

4390-
err = ext4_ext_insert_extent(handle, inode, &path, &newex, flags);
4391-
if (err) {
4401+
path = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
4402+
if (IS_ERR(path)) {
4403+
err = PTR_ERR(path);
43924404
if (allocated_clusters) {
43934405
int fb_flags = 0;
43944406

fs/ext4/fast_commit.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,12 +1803,12 @@ static int ext4_fc_replay_add_range(struct super_block *sb,
18031803
if (ext4_ext_is_unwritten(ex))
18041804
ext4_ext_mark_unwritten(&newex);
18051805
down_write(&EXT4_I(inode)->i_data_sem);
1806-
ret = ext4_ext_insert_extent(
1807-
NULL, inode, &path, &newex, 0);
1806+
path = ext4_ext_insert_extent(NULL, inode,
1807+
path, &newex, 0);
18081808
up_write((&EXT4_I(inode)->i_data_sem));
1809-
ext4_free_ext_path(path);
1810-
if (ret)
1809+
if (IS_ERR(path))
18111810
goto out;
1811+
ext4_free_ext_path(path);
18121812
goto next;
18131813
}
18141814

fs/ext4/migrate.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ static int finish_range(handle_t *handle, struct inode *inode,
3737
path = ext4_find_extent(inode, lb->first_block, NULL, 0);
3838
if (IS_ERR(path)) {
3939
retval = PTR_ERR(path);
40-
path = NULL;
4140
goto err_out;
4241
}
4342

@@ -53,7 +52,9 @@ static int finish_range(handle_t *handle, struct inode *inode,
5352
retval = ext4_datasem_ensure_credits(handle, inode, needed, needed, 0);
5453
if (retval < 0)
5554
goto err_out;
56-
retval = ext4_ext_insert_extent(handle, inode, &path, &newext, 0);
55+
path = ext4_ext_insert_extent(handle, inode, path, &newext, 0);
56+
if (IS_ERR(path))
57+
retval = PTR_ERR(path);
5758
err_out:
5859
up_write((&EXT4_I(inode)->i_data_sem));
5960
ext4_free_ext_path(path);

0 commit comments

Comments
 (0)