btrfs: a bug fix and some cleanups in ctree.c
cover btrfs: a bug fix and some cleanups in ctree.c
From: Filipe Manana <[email protected]>
Trivial changes, details in the change logs.
Filipe Manana (8):
btrfs: fix leaf leak in an error path in btrfs_del_items()
btrfs: remove pointless return value update in btrfs_del_items()
btrfs: add unlikely to critical error in btrfs_extend_item()
btrfs: always use left leaf variable in __push_leaf_right()
btrfs: remove duplicated leaf dirty status clearing in __push_leaf_right()
btrfs: always use right leaf variable in __push_leaf_left()
btrfs: abort transaction on item count overflow in __push_leaf_left()
btrfs: update check_skip variable after unlocking current node
fs/btrfs/ctree.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)
Signature · 1 line
2.47.2
PATCH 1/8 btrfs: fix leaf leak in an error path in btrfs_del_items()
From: Filipe Manana <[email protected]>
If the call to btrfs_del_leaf() fails we return without decrementing the extra ref we took on the leaf, therefore leaking it. Fix this by ensuring we drop the ref count before returning the error.
--- fs/btrfs/ctree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/ctree.c +1 -1
@@ -4562,9 +4562,9 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,if (btrfs_header_nritems (leaf) ==0 ) { path->slots[1 ] = slot; ret =btrfs_del_leaf (trans, root, path, leaf); +free_extent_buffer (leaf);if (ret <0 )return ret; -free_extent_buffer (leaf); ret =0 ; }else {/* if we're still in the path, make sure
Signature · 1 line
2.47.2
PATCH 2/8 btrfs: remove pointless return value update in btrfs_del_items()
In reply to [email protected]From: Filipe Manana <[email protected]>
The call to btrfs_del_leaf() can only return an error (negative value) or zero (success). If we didn't get an error then 'ret' is zero, so it's pointless to set it to zero again.
--- fs/btrfs/ctree.c | 1 - 1 file changed, 1 deletion(-)
fs/btrfs/ctree.c +0 -1
@@ -4565,7 +4565,6 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,free_extent_buffer (leaf);if (ret <0 )return ret; - ret =0 ; }else {/* if we're still in the path, make sure * we're dirty. Otherwise, one of the
Signature · 1 line
2.47.2
PATCH 3/8 btrfs: add unlikely to critical error in btrfs_extend_item()
In reply to [email protected]From: Filipe Manana <[email protected]>
It's not expected to get a data size less than the leaf's free space, which would lead to a leaf dump and BUG(), so tag the if statement's expression as unlikely, hinting the compiler to potentially generate better code.
--- fs/btrfs/ctree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/ctree.c +1 -1
@@ -4106,7 +4106,7 @@ void btrfs_extend_item(struct btrfs_trans_handle *trans, nritems =btrfs_header_nritems (leaf); data_end =leaf_data_end (leaf); -if (btrfs_leaf_free_space (leaf) < data_size) { +if (unlikely (btrfs_leaf_free_space (leaf) < data_size)) {btrfs_print_leaf (leaf);BUG (); }
Signature · 1 line
2.47.2
PATCH 4/8 btrfs: always use left leaf variable in __push_leaf_right()
In reply to [email protected]From: Filipe Manana <[email protected]>
The 'left' variable points to path->nodes[0] and path->nodes[0] is never changed, but some places use 'left' while others refer to path->nodes[0]. Update all sites to use 'left' as not only it's shorter it's also easier to reason since it means the left leaf and avoids any confusion with the sibling right leaf.
--- fs/btrfs/ctree.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
fs/btrfs/ctree.c +4 -4
@@ -3214,10 +3214,10 @@ static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,/* then fixup the leaf pointer in the path */ if (path->slots[0 ] >= left_nritems) { path->slots[0 ] -= left_nritems; -if (btrfs_header_nritems (path->nodes[0 ]) ==0 ) -btrfs_clear_buffer_dirty (trans, path->nodes[0 ]); -btrfs_tree_unlock (path->nodes[0 ]); -free_extent_buffer (path->nodes[0 ]); +if (btrfs_header_nritems (left) ==0 ) +btrfs_clear_buffer_dirty (trans, left); +btrfs_tree_unlock (left); +free_extent_buffer (left); path->nodes[0 ] = right; path->slots[1 ] +=1 ; }else {
Signature · 1 line
2.47.2
PATCH 5/8 btrfs: remove duplicated leaf dirty status clearing in __push_leaf_right()
In reply to [email protected]From: Filipe Manana <[email protected]>
We have already called btrfs_clear_buffer_dirty() against the left leaf in the code above:
btrfs_set_header_nritems(left, left_nritems);
if (left_nritems)
btrfs_mark_buffer_dirty(trans, left);
else
btrfs_clear_buffer_dirty(trans, left);So remove the second check for a 0 number of items in the left leaf and calling again btrfs_clear_buffer_dirty() against the left leaf.
--- fs/btrfs/ctree.c | 2 -- 1 file changed, 2 deletions(-)
fs/btrfs/ctree.c +0 -2
@@ -3214,8 +3214,6 @@ static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,/* then fixup the leaf pointer in the path */ if (path->slots[0 ] >= left_nritems) { path->slots[0 ] -= left_nritems; -if (btrfs_header_nritems (left) ==0 ) -btrfs_clear_buffer_dirty (trans, left);btrfs_tree_unlock (left);free_extent_buffer (left); path->nodes[0 ] = right;
Signature · 1 line
2.47.2
PATCH 6/8 btrfs: always use right leaf variable in __push_leaf_left()
In reply to [email protected]From: Filipe Manana <[email protected]>
The 'right' variable points to path->nodes[0] and path->nodes[0] is never changed, but some places use 'right' while others refer to path->nodes[0]. Update all sites to use 'right' as not only it's shorter it's also easier to reason since it means the right leaf and avoids any confusion with the sibling left leaf.
--- fs/btrfs/ctree.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/ctree.c +2 -2
@@ -3428,8 +3428,8 @@ static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,/* then fixup the leaf pointer in the path */ if (path->slots[0 ] < push_items) { path->slots[0 ] += old_left_nritems; -btrfs_tree_unlock (path->nodes[0 ]); -free_extent_buffer (path->nodes[0 ]); +btrfs_tree_unlock (right); +free_extent_buffer (right); path->nodes[0 ] = left; path->slots[1 ] -=1 ; }else {
Signature · 1 line
2.47.2
PATCH 7/8 btrfs: abort transaction on item count overflow in __push_leaf_left()
In reply to [email protected]From: Filipe Manana <[email protected]>
If we try to push an item count from the right leaf that is greater than the number of items in the leaf, we just emit a warning. This should never happen but if it does we get an underflow in the new number of items in the right leaf and chaos follows from it. So replace the warning with proper error handling, by aborting the transaction and returning -EUCLEAN, and proper logging by using btrfs_crit() instead of WARN(), which gives us proper formatting and information about the filesystem.
--- fs/btrfs/ctree.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
fs/btrfs/ctree.c +7 -3
@@ -3393,9 +3393,13 @@ static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,btrfs_set_header_nritems (left, old_left_nritems + push_items);/* fixup right node */ -if (push_items > right_nritems) -WARN (1 ,KERN_CRIT "push items %d nr %u\n" , push_items, - right_nritems); +if (unlikely (push_items > right_nritems)) { + ret = -EUCLEAN ; +btrfs_abort_transaction (trans, ret); +btrfs_crit (fs_info,"push items (%d) > right leaf items (%u)" , + push_items, right_nritems); + goto out; + }if (push_items < right_nritems) { push_space = btrfs_item_offset(right, push_items - 1) -
Signature · 1 line
2.47.2
PATCH 8/8 btrfs: update check_skip variable after unlocking current node
In reply to [email protected]From: Filipe Manana <[email protected]>
There's no need to update the local variable 'check_skip' to false inside the critical section delimited by the lock of the current node, so do it after unlocking the node.
--- fs/btrfs/ctree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/ctree.c +1 -1
@@ -1435,8 +1435,8 @@ static noinline void unlock_up(struct btrfs_path *path, int level, }if (i >= lowest_unlock && i > skip_level) { - check_skip = false;btrfs_tree_unlock_rw (path->nodes[i], path->locks[i]); + check_skip = false; path->locks[i] =0 ; if (write_lock_level && i > min_write_lock_level &&
Signature · 1 line
2.47.2
Quoted 3 lines · 在 2025/11/14 03:26, [email protected] 写道:
From: Filipe Manana <[email protected]>
Trivial changes, details in the change logs.
Thanks,
Qu
Quoted 14 lines
Filipe Manana (8):
btrfs: fix leaf leak in an error path inbtrfs_del_items()
btrfs: remove pointless return value update inbtrfs_del_items()
btrfs: add unlikely to critical error inbtrfs_extend_item()
btrfs: always use left leaf variable in__push_leaf_right()
btrfs: remove duplicated leaf dirty status clearing in__push_leaf_right()
btrfs: always use right leaf variable in__push_leaf_left()
btrfs: abort transaction on item count overflow in__push_leaf_left()
btrfs: updatecheck_skipvariable after unlocking current nodefs/btrfs/ctree.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)