Skip to content

Commit 19008f6

Browse files
committed
ext4: fix ext4_swap_extents() error handling
If ext4_ext_find_extent() returns an error, we have to clear path1 or path2 or else we would end up trying to free an ERR_PTR, which would be bad. Also eliminate some redundant code and mark the error paths as unlikely() Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent fcf6b1b commit 19008f6

1 file changed

Lines changed: 29 additions & 33 deletions

File tree

‎fs/ext4/extents.c‎

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
869869
if (!path) {
870870
path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
871871
GFP_NOFS);
872-
if (!path)
872+
if (unlikely(!path))
873873
return ERR_PTR(-ENOMEM);
874874
alloc = 1;
875875
}
@@ -889,7 +889,7 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
889889

890890
bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
891891
flags);
892-
if (IS_ERR(bh)) {
892+
if (unlikely(IS_ERR(bh))) {
893893
ret = PTR_ERR(bh);
894894
goto err;
895895
}
@@ -5545,10 +5545,10 @@ ext4_swap_extents(handle_t *handle, struct inode *inode1,
55455545
BUG_ON(!mutex_is_locked(&inode1->i_mutex));
55465546

55475547
*erp = ext4_es_remove_extent(inode1, lblk1, count);
5548-
if (*erp)
5548+
if (unlikely(*erp))
55495549
return 0;
55505550
*erp = ext4_es_remove_extent(inode2, lblk2, count);
5551-
if (*erp)
5551+
if (unlikely(*erp))
55525552
return 0;
55535553

55545554
while (count) {
@@ -5558,20 +5558,24 @@ ext4_swap_extents(handle_t *handle, struct inode *inode1,
55585558
int split = 0;
55595559

55605560
path1 = ext4_ext_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
5561-
if (IS_ERR(path1)) {
5561+
if (unlikely(IS_ERR(path1))) {
55625562
*erp = PTR_ERR(path1);
5563-
break;
5563+
path1 = NULL;
5564+
finish:
5565+
count = 0;
5566+
goto repeat;
55645567
}
55655568
path2 = ext4_ext_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
5566-
if (IS_ERR(path2)) {
5569+
if (unlikely(IS_ERR(path2))) {
55675570
*erp = PTR_ERR(path2);
5568-
break;
5571+
path2 = NULL;
5572+
goto finish;
55695573
}
55705574
ex1 = path1[path1->p_depth].p_ext;
55715575
ex2 = path2[path2->p_depth].p_ext;
55725576
/* Do we have somthing to swap ? */
55735577
if (unlikely(!ex2 || !ex1))
5574-
break;
5578+
goto finish;
55755579

55765580
e1_blk = le32_to_cpu(ex1->ee_block);
55775581
e2_blk = le32_to_cpu(ex2->ee_block);
@@ -5593,7 +5597,7 @@ ext4_swap_extents(handle_t *handle, struct inode *inode1,
55935597
next2 = e1_blk;
55945598
/* Do we have something to swap */
55955599
if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
5596-
break;
5600+
goto finish;
55975601
/* Move to the rightest boundary */
55985602
len = next1 - lblk1;
55995603
if (len < next2 - lblk2)
@@ -5611,15 +5615,15 @@ ext4_swap_extents(handle_t *handle, struct inode *inode1,
56115615
split = 1;
56125616
*erp = ext4_force_split_extent_at(handle, inode1,
56135617
path1, lblk1, 0);
5614-
if (*erp)
5615-
break;
5618+
if (unlikely(*erp))
5619+
goto finish;
56165620
}
56175621
if (e2_blk < lblk2) {
56185622
split = 1;
56195623
*erp = ext4_force_split_extent_at(handle, inode2,
56205624
path2, lblk2, 0);
5621-
if (*erp)
5622-
break;
5625+
if (unlikely(*erp))
5626+
goto finish;
56235627
}
56245628
/* ext4_split_extent_at() may retult in leaf extent split,
56255629
* path must to be revalidated. */
@@ -5637,15 +5641,15 @@ ext4_swap_extents(handle_t *handle, struct inode *inode1,
56375641
split = 1;
56385642
*erp = ext4_force_split_extent_at(handle, inode1,
56395643
path1, lblk1 + len, 0);
5640-
if (*erp)
5641-
break;
5644+
if (unlikely(*erp))
5645+
goto finish;
56425646
}
56435647
if (len != e2_len) {
56445648
split = 1;
56455649
*erp = ext4_force_split_extent_at(handle, inode2,
56465650
path2, lblk2 + len, 0);
56475651
if (*erp)
5648-
break;
5652+
goto finish;
56495653
}
56505654
/* ext4_split_extent_at() may retult in leaf extent split,
56515655
* path must to be revalidated. */
@@ -5654,11 +5658,11 @@ ext4_swap_extents(handle_t *handle, struct inode *inode1,
56545658

56555659
BUG_ON(e2_len != e1_len);
56565660
*erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
5657-
if (*erp)
5658-
break;
5661+
if (unlikely(*erp))
5662+
goto finish;
56595663
*erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
5660-
if (*erp)
5661-
break;
5664+
if (unlikely(*erp))
5665+
goto finish;
56625666

56635667
/* Both extents are fully inside boundaries. Swap it now */
56645668
tmp_ex = *ex1;
@@ -5675,8 +5679,8 @@ ext4_swap_extents(handle_t *handle, struct inode *inode1,
56755679
ext4_ext_try_to_merge(handle, inode1, path1, ex1);
56765680
*erp = ext4_ext_dirty(handle, inode2, path2 +
56775681
path2->p_depth);
5678-
if (*erp)
5679-
break;
5682+
if (unlikely(*erp))
5683+
goto finish;
56805684
*erp = ext4_ext_dirty(handle, inode1, path1 +
56815685
path1->p_depth);
56825686
/*
@@ -5685,8 +5689,8 @@ ext4_swap_extents(handle_t *handle, struct inode *inode1,
56855689
* only due to journal error, so full transaction will be
56865690
* aborted anyway.
56875691
*/
5688-
if (*erp)
5689-
break;
5692+
if (unlikely(*erp))
5693+
goto finish;
56905694
lblk1 += len;
56915695
lblk2 += len;
56925696
replaced_count += len;
@@ -5704,13 +5708,5 @@ ext4_swap_extents(handle_t *handle, struct inode *inode1,
57045708
path2 = NULL;
57055709
}
57065710
}
5707-
if (path1) {
5708-
ext4_ext_drop_refs(path1);
5709-
kfree(path1);
5710-
}
5711-
if (path2) {
5712-
ext4_ext_drop_refs(path2);
5713-
kfree(path2);
5714-
}
57155711
return replaced_count;
57165712
}

0 commit comments

Comments
 (0)