Skip to content

Commit 72c9865

Browse files
adam900710kdave
authored andcommitted
btrfs-progs: improve error handling in btrfs_split_item()
This involves the following error cases: - Unable to find the original item Return -EAGAIN and release the path (which is not done in the original code) - Error from split_leaf() Remove the BUG_ON() and handle the error. The most common error is ENOSPC. - Error from kmalloc() Just handle the error and return -ENOMEM. Issue: #312 Signed-off-by: Qu Wenruo <[email protected]>
1 parent 4141831 commit 72c9865

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

kernel-shared/ctree.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -2450,7 +2450,7 @@ int btrfs_split_item(struct btrfs_trans_handle *trans,
24502450
u32 nritems;
24512451
u32 orig_offset;
24522452
struct btrfs_disk_key disk_key;
2453-
char *buf;
2453+
char *buf = NULL;
24542454

24552455
leaf = path->nodes[0];
24562456
btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
@@ -2469,11 +2469,13 @@ int btrfs_split_item(struct btrfs_trans_handle *trans,
24692469
/* if our item isn't there or got smaller, return now */
24702470
if (ret != 0 || item_size != btrfs_item_size(path->nodes[0],
24712471
path->slots[0])) {
2472-
return -EAGAIN;
2472+
ret = -EAGAIN;
2473+
goto error;
24732474
}
24742475

24752476
ret = split_leaf(trans, root, &orig_key, path, 0, 0);
2476-
BUG_ON(ret);
2477+
if (ret < 0)
2478+
goto error;
24772479

24782480
BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
24792481
leaf = path->nodes[0];
@@ -2484,7 +2486,10 @@ int btrfs_split_item(struct btrfs_trans_handle *trans,
24842486

24852487

24862488
buf = kmalloc(item_size, GFP_NOFS);
2487-
BUG_ON(!buf);
2489+
if (!buf) {
2490+
ret = -ENOMEM;
2491+
goto error;
2492+
}
24882493
read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
24892494
path->slots[0]), item_size);
24902495
slot = path->slots[0] + 1;
@@ -2530,6 +2535,10 @@ int btrfs_split_item(struct btrfs_trans_handle *trans,
25302535
}
25312536
kfree(buf);
25322537
return ret;
2538+
error:
2539+
kfree(buf);
2540+
btrfs_release_path(path);
2541+
return ret;
25332542
}
25342543

25352544
void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)

0 commit comments

Comments
 (0)