Skip to content

Commit 7569141

Browse files
fdmananakdave
authored andcommitted
btrfs: replace BUG_ON() at split_item() with proper error handling
There's no need to BUG_ON() at split_item() if the leaf does not have enough free space for the new item, we can just return -ENOSPC since the caller can deal with errors from split_item(). Also, as this is a very unlikely condition to happen, because the caller has invoked setup_leaf_for_split() before calling split_item(), surround the condition with a WARN_ON() which makes it easier to notice this unexpected condition and tags the if branch with 'unlikely' as well. I've actually once hit this BUG_ON() with some incorrect code changes I had, which was very inconvenient as it required rebooting the VM. Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 751a276 commit 7569141

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

fs/btrfs/ctree.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4000,7 +4000,12 @@ static noinline int split_item(struct btrfs_path *path,
40004000
struct btrfs_disk_key disk_key;
40014001

40024002
leaf = path->nodes[0];
4003-
BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
4003+
/*
4004+
* Shouldn't happen because the caller must have previously called
4005+
* setup_leaf_for_split() to make room for the new item in the leaf.
4006+
*/
4007+
if (WARN_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item)))
4008+
return -ENOSPC;
40044009

40054010
orig_slot = path->slots[0];
40064011
orig_offset = btrfs_item_offset(leaf, path->slots[0]);

0 commit comments

Comments
 (0)