Skip to content

Commit 705912c

Browse files
committed
ext4: teach ext4_ext_find_extent() to free path on error
Right now, there are a places where it is all to easy to leak memory on an error path, via a usage like this: struct ext4_ext_path *path = NULL while (...) { ... path = ext4_ext_find_extent(inode, block, path, 0); if (IS_ERR(path)) { /* oops, if path was non-NULL before the call to ext4_ext_find_extent, we've leaked it! :-( */ ... return PTR_ERR(path); } ... } Unfortunately, there some code paths where we are doing the following instead: path = ext4_ext_find_extent(inode, block, orig_path, 0); and where it's important that we _not_ free orig_path in the case where ext4_ext_find_extent() returns an error. So change the function signature of ext4_ext_find_extent() so that it takes a struct ext4_ext_path ** for its third argument, and by default, on an error, it will free the struct ext4_ext_path, and then zero out the struct ext4_ext_path * pointer. In order to avoid causing problems, we add a flag EXT4_EX_NOFREE_ON_ERR which causes ext4_ext_find_extent() to use the original behavior of forcing the caller to deal with freeing the original path pointer on the error case. The goal is to get rid of EXT4_EX_NOFREE_ON_ERR entirely, but this allows for a gentle transition and makes the patches easier to verify. Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent bd30d70 commit 705912c

3 files changed

Lines changed: 21 additions & 12 deletions

File tree

‎fs/ext4/ext4.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ enum {
582582
*/
583583
#define EXT4_EX_NOCACHE 0x0800
584584
#define EXT4_EX_FORCE_CACHE 0x1000
585+
#define EXT4_EX_NOFREE_ON_ERR 0x2000
585586

586587
/*
587588
* Flags used by ext4_free_blocks
@@ -2733,7 +2734,7 @@ extern int ext4_ext_insert_extent(handle_t *, struct inode *,
27332734
struct ext4_ext_path *,
27342735
struct ext4_extent *, int);
27352736
extern struct ext4_ext_path *ext4_ext_find_extent(struct inode *, ext4_lblk_t,
2736-
struct ext4_ext_path *,
2737+
struct ext4_ext_path **,
27372738
int flags);
27382739
extern void ext4_ext_drop_refs(struct ext4_ext_path *);
27392740
extern int ext4_ext_check_inode(struct inode *inode);

‎fs/ext4/extents.c‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -855,11 +855,13 @@ int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
855855

856856
struct ext4_ext_path *
857857
ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
858-
struct ext4_ext_path *path, int flags)
858+
struct ext4_ext_path **orig_path, int flags)
859859
{
860860
struct ext4_extent_header *eh;
861861
struct buffer_head *bh;
862-
short int depth, i, ppos = 0, alloc = 0;
862+
struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
863+
short int depth, i, ppos = 0;
864+
short free_on_err = (flags & EXT4_EX_NOFREE_ON_ERR) == 0;
863865
int ret;
864866

865867
eh = ext_inode_hdr(inode);
@@ -871,7 +873,7 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
871873
GFP_NOFS);
872874
if (unlikely(!path))
873875
return ERR_PTR(-ENOMEM);
874-
alloc = 1;
876+
free_on_err = 1;
875877
}
876878
path[0].p_hdr = eh;
877879
path[0].p_bh = NULL;
@@ -923,8 +925,11 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
923925

924926
err:
925927
ext4_ext_drop_refs(path);
926-
if (alloc)
928+
if (free_on_err) {
927929
kfree(path);
930+
if (orig_path)
931+
*orig_path = NULL;
932+
}
928933
return ERR_PTR(ret);
929934
}
930935

@@ -1356,7 +1361,7 @@ static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
13561361
ext4_ext_drop_refs(path);
13571362
path = ext4_ext_find_extent(inode,
13581363
(ext4_lblk_t)le32_to_cpu(newext->ee_block),
1359-
path, gb_flags);
1364+
&path, gb_flags | EXT4_EX_NOFREE_ON_ERR);
13601365
if (IS_ERR(path))
13611366
err = PTR_ERR(path);
13621367
} else {
@@ -1369,7 +1374,7 @@ static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
13691374
ext4_ext_drop_refs(path);
13701375
path = ext4_ext_find_extent(inode,
13711376
(ext4_lblk_t)le32_to_cpu(newext->ee_block),
1372-
path, gb_flags);
1377+
&path, gb_flags | EXT4_EX_NOFREE_ON_ERR);
13731378
if (IS_ERR(path)) {
13741379
err = PTR_ERR(path);
13751380
goto out;
@@ -2152,7 +2157,7 @@ static int ext4_fill_fiemap_extents(struct inode *inode,
21522157
path = NULL;
21532158
}
21542159

2155-
path = ext4_ext_find_extent(inode, block, path, 0);
2160+
path = ext4_ext_find_extent(inode, block, &path, 0);
21562161
if (IS_ERR(path)) {
21572162
up_read(&EXT4_I(inode)->i_data_sem);
21582163
err = PTR_ERR(path);
@@ -3313,7 +3318,8 @@ static int ext4_split_extent(handle_t *handle,
33133318
* result in split of original leaf or extent zeroout.
33143319
*/
33153320
ext4_ext_drop_refs(path);
3316-
path = ext4_ext_find_extent(inode, map->m_lblk, path, 0);
3321+
path = ext4_ext_find_extent(inode, map->m_lblk, &path,
3322+
EXT4_EX_NOFREE_ON_ERR);
33173323
if (IS_ERR(path))
33183324
return PTR_ERR(path);
33193325
depth = ext_depth(inode);
@@ -3697,7 +3703,8 @@ static int ext4_convert_initialized_extents(handle_t *handle,
36973703
if (err < 0)
36983704
goto out;
36993705
ext4_ext_drop_refs(path);
3700-
path = ext4_ext_find_extent(inode, map->m_lblk, path, 0);
3706+
path = ext4_ext_find_extent(inode, map->m_lblk, &path,
3707+
EXT4_EX_NOFREE_ON_ERR);
37013708
if (IS_ERR(path)) {
37023709
err = PTR_ERR(path);
37033710
goto out;
@@ -3769,7 +3776,8 @@ static int ext4_convert_unwritten_extents_endio(handle_t *handle,
37693776
if (err < 0)
37703777
goto out;
37713778
ext4_ext_drop_refs(path);
3772-
path = ext4_ext_find_extent(inode, map->m_lblk, path, 0);
3779+
path = ext4_ext_find_extent(inode, map->m_lblk, &path,
3780+
EXT4_EX_NOFREE_ON_ERR);
37733781
if (IS_ERR(path)) {
37743782
err = PTR_ERR(path);
37753783
goto out;

‎fs/ext4/move_extent.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ get_ext_path(struct inode *inode, ext4_lblk_t lblock,
3737
int ret = 0;
3838
struct ext4_ext_path *path;
3939

40-
path = ext4_ext_find_extent(inode, lblock, *orig_path, EXT4_EX_NOCACHE);
40+
path = ext4_ext_find_extent(inode, lblock, orig_path, EXT4_EX_NOCACHE);
4141
if (IS_ERR(path))
4242
ret = PTR_ERR(path);
4343
else if (path[ext_depth(inode)].p_ext == NULL)

0 commit comments

Comments
 (0)