Skip to content

Commit f94480b

Browse files
Josef Bacikkdave
authored andcommitted
Btrfs: abort transaction if fill_holes() fails
At this point we will have dropped extent entries from the file, so if we fail to insert the new hole entries then we are leaving the fs in a corrupt state (albeit an easily fixed one). Abort the transaciton if this happens so we can avoid corrupting the fs. Thanks, Signed-off-by: Josef Bacik <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 62fe51c commit f94480b

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

fs/btrfs/file.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,9 +2232,15 @@ static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
22322232
key.offset = offset;
22332233

22342234
ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2235-
if (ret < 0)
2235+
if (ret <= 0) {
2236+
/*
2237+
* We should have dropped this offset, so if we find it then
2238+
* something has gone horribly wrong.
2239+
*/
2240+
if (ret == 0)
2241+
ret = -EINVAL;
22362242
return ret;
2237-
BUG_ON(!ret);
2243+
}
22382244

22392245
leaf = path->nodes[0];
22402246
if (hole_mergeable(inode, leaf, path->slots[0]-1, offset, end)) {
@@ -2537,6 +2543,13 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
25372543
ret = fill_holes(trans, inode, path, cur_offset,
25382544
drop_end);
25392545
if (ret) {
2546+
/*
2547+
* If we failed then we didn't insert our hole
2548+
* entries for the area we dropped, so now the
2549+
* fs is corrupted, so we must abort the
2550+
* transaction.
2551+
*/
2552+
btrfs_abort_transaction(trans, ret);
25402553
err = ret;
25412554
break;
25422555
}
@@ -2601,6 +2614,8 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
26012614
if (cur_offset < ino_size && cur_offset < drop_end) {
26022615
ret = fill_holes(trans, inode, path, cur_offset, drop_end);
26032616
if (ret) {
2617+
/* Same comment as above. */
2618+
btrfs_abort_transaction(trans, ret);
26042619
err = ret;
26052620
goto out_trans;
26062621
}

0 commit comments

Comments
 (0)