Skip to content

Commit 62fe51c

Browse files
Josef Bacikkdave
authored andcommitted
Btrfs: fix file extent corruption
In order to do hole punching we have a block reserve to hold the reservation we need to drop the extents in our range. Since we could end up dropping a lot of extents we set rsv->failfast so we can just loop around again and drop the remaining of the range. Unfortunately we unconditionally fill the hole extents in and start from the last extent we encountered, which we may or may not have dropped. So this can result in overlapping file extent entries, which can be tripped over in a variety of ways, either by hitting BUG_ON(!ret) in fill_holes() after the search, or in btrfs_set_item_key_safe() in btrfs_drop_extent() at a later time by an unrelated task. Fix this by only setting drop_end to the last extent we did actually drop. This way our holes are filled in properly for the range that we did drop, and the rest of the range that remains to be dropped is actually dropped. Thanks, Signed-off-by: Josef Bacik <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent d2fbb2b commit 62fe51c

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

fs/btrfs/file.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
705705
u64 num_bytes = 0;
706706
u64 extent_offset = 0;
707707
u64 extent_end = 0;
708+
u64 last_end = start;
708709
int del_nr = 0;
709710
int del_slot = 0;
710711
int extent_type;
@@ -796,8 +797,10 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
796797
* extent item in the call to setup_items_for_insert() later
797798
* in this function.
798799
*/
799-
if (extent_end == key.offset && extent_end >= search_start)
800+
if (extent_end == key.offset && extent_end >= search_start) {
801+
last_end = extent_end;
800802
goto delete_extent_item;
803+
}
801804

802805
if (extent_end <= search_start) {
803806
path->slots[0]++;
@@ -859,6 +862,12 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
859862
}
860863
key.offset = start;
861864
}
865+
/*
866+
* From here on out we will have actually dropped something, so
867+
* last_end can be updated.
868+
*/
869+
last_end = extent_end;
870+
862871
/*
863872
* | ---- range to drop ----- |
864873
* | -------- extent -------- |
@@ -1009,7 +1018,7 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
10091018
if (!replace_extent || !(*key_inserted))
10101019
btrfs_release_path(path);
10111020
if (drop_end)
1012-
*drop_end = found ? min(end, extent_end) : end;
1021+
*drop_end = found ? min(end, last_end) : end;
10131022
return ret;
10141023
}
10151024

@@ -2524,7 +2533,7 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
25242533

25252534
trans->block_rsv = &root->fs_info->trans_block_rsv;
25262535

2527-
if (cur_offset < ino_size) {
2536+
if (cur_offset < drop_end && cur_offset < ino_size) {
25282537
ret = fill_holes(trans, inode, path, cur_offset,
25292538
drop_end);
25302539
if (ret) {

0 commit comments

Comments
 (0)