btrfs: random code cleanup
cover btrfs: random code cleanup
These patches are not intented to change any behavior of current code. Just trying to make the codes easier to read, and, maybe, perform better. I'm working on btrfs_search_forward(), trying to improve it and fix some misbehaviors. And I'll send some patches that will change the behavior of the code later.
I'm new to the maillist, trying to read the implements, and improve it from my perspective.
If you have any feedback or questions, please let me know :)
Sun YangKai (3):
btrfs: improve readability in search_ioctl()
btrfs: remove unnecessary 'found_key' local variable in
btrfs_search_forward()
btrfs: avoid redundant slot assignment in btrfs_search_forward()
fs/btrfs/ctree.c | 12 +++++------- fs/btrfs/ioctl.c | 15 +++++++-------- 2 files changed, 12 insertions(+), 15 deletions(-)
Signature · 1 line
2.48.1
PATCH 1/3 btrfs: improve readability in search_ioctl()
This commit addresses two code issues in the search_ioctl() function:
1. Move the assignment of ret = -EFAULT to within the error condition check in fault_in_subpage_writeable(). The previous placement outside the condition could lead to the error value being overwritten by subsequent assignments, cause unnecessary assignments.
2. Simplify loop exit logic by removing redundant goto.
The original code used goto err to bypass post-loop processing after handling errors from btrfs_search_forward(). However, the loop's termination naturally falls through to the post-loop section, which already handles ret values. Replacing goto err with break eliminates redundant control flow, consolidates error handling, and makes the loop's exit conditions explicit.
The changes ensure proper error propagation and make the loop's exit conditions clearer while maintaining functional equivalence.
--- fs/btrfs/ioctl.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
fs/btrfs/ioctl.c +7 -8
@@ -1642,21 +1642,20 @@ static noinline int search_ioctl(struct inode *inode, key.offset = sk->min_offset;while (1 ) { - ret = -EFAULT ;/* * Ensure that the whole user buffer is faulted in at sub-page * granularity, otherwise the loop may live-lock. */ if (fault_in_subpage_writeable (ubuf + sk_offset, - *buf_size - sk_offset)) + *buf_size - sk_offset)) { + ret = -EFAULT ;break ; + } ret =btrfs_search_forward (root, &key, path, sk->min_transid); -if (ret !=0 ) { -if (ret >0 ) - ret =0 ; - goto err; - } +if (ret) +break ; + ret =copy_to_sk (path, &key, sk, buf_size, ubuf, &sk_offset, &num_found);btrfs_release_path (path);
@@ -1666,7 +1665,7 @@ static noinline int search_ioctl(struct inode *inode, }if (ret >0 ) ret =0 ; -err: + sk->nr_items = num_found;btrfs_put_root (root);btrfs_free_path (path);
Signature · 1 line
2.48.1
Re: [PATCH 1/3] btrfs: improve readability in search_ioctl()
Quoted 1 line · Sun YangKai
This commit addresses two code issues in the
search_ioctl()function:
Please avoid the phrases "This commit" or "This patch". Also the readability is a general goal, in this case it can be more specific, like simplifying the return values.
Quoted 65 lines
1. Move the assignment of ret =
-EFAULTto within the error condition check infault_in_subpage_writeable(). The previous placement outside the condition could lead to the error value being overwritten by subsequent assignments, cause unnecessary assignments.2. Simplify loop exit logic by removing redundant
goto.
The original code usedgoto errto bypass post-loop processing after handling errors frombtrfs_search_forward(). However, the loop's termination naturally falls through to the post-loop section, which already handlesretvalues. Replacinggoto errwithbreakeliminates redundant control flow, consolidates error handling, and makes the loop's exit conditions explicit.The changes ensure proper error propagation and make the loop's exit conditions clearer while maintaining functional equivalence.
Signed-off-by Sun YangKai <[email protected]>--- fs/btrfs/ioctl.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)fs/btrfs/ioctl.c +7
-8@@ -1642,21 +1642,20 @@ static noinline int search_ioctl(struct inode *inode, key.offset = sk->min_offset;while (1 ) { - ret = -EFAULT ;/* * Ensure that the whole user buffer is faulted in at sub-page * granularity, otherwise the loop may live-lock. */ if (fault_in_subpage_writeable (ubuf + sk_offset, - *buf_size - sk_offset)) + *buf_size - sk_offset)) { + ret = -EFAULT ;break ; + } ret =btrfs_search_forward (root, &key, path, sk->min_transid); -if (ret !=0 ) { -if (ret >0 ) - ret =0 ; - goto err; - } +if (ret) +break ; + ret =copy_to_sk (path, &key, sk, buf_size, ubuf, &sk_offset, &num_found);btrfs_release_path (path);@@ -1666,7 +1665,7 @@ static noinline int search_ioctl(struct inode *inode, }if (ret >0 ) ret =0 ; -err: + sk->nr_items = num_found;btrfs_put_root (root);btrfs_free_path (path);Signature · 1 line
2.48.1
PATCH 2/3 btrfs: remove unnecessary 'found_key' local variable in btrfs_search_forward()
In reply to Sun YangKaiThe 'found_key' variable was only used to temporarily store a key value before copying it to 'min_key' at the end of the function when returning success (ret=0).
This commit optimizes the code by:
1. Eliminating the intermediate 'found_key' variable
2. Directly populating 'min_key' at the exact loop exit points where ret=0 is set
3. Removing the final memcpy operation in the return path
This change improves code clarity by:
- Removing redundant variable usage
- Simplifying the success path logic
- Reducing memory operations
The found key value is now stored directly into the destination 'min_key' structure at the point of discovery, maintaining identical functionality while:
- Eliminating an unnecessary memory copy
- Reducing code complexity
--- fs/btrfs/ctree.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
fs/btrfs/ctree.c +4 -5
@@ -4608,7 +4608,6 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, u64 min_trans) {struct extent_buffer *cur; -struct btrfs_key found_key;int slot;int sret;u32 nritems;
@@ -4644,7 +4643,8 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, goto find_next_key; ret =0 ; path->slots[level] = slot; -btrfs_item_key_to_cpu (cur, &found_key, slot); +/* save our key for returning back */ +btrfs_item_key_to_cpu (cur, min_key, slot); goto out; }if (sret && slot >0 )
@@ -4679,11 +4679,11 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, goto out; } } -/* save our key for returning back */ -btrfs_node_key_to_cpu (cur, &found_key, slot); path->slots[level] = slot;if (level == path->lowest_level) { ret =0 ; +/* save our key for returning back */ +btrfs_node_key_to_cpu (cur, min_key, slot); goto out; } cur =btrfs_read_node_slot (cur, slot);
@@ -4702,7 +4702,6 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, path->keep_locks = keep_locks;if (ret ==0 ) {btrfs_unlock_up_safe (path, path->lowest_level +1 ); -memcpy (min_key, &found_key,sizeof (found_key)); }return ret; }
Signature · 1 line
2.48.1
Re: [PATCH 2/3] btrfs: remove unnecessary 'found_key' local variable in btrfs_search_forward()
Quoted 1 line · Sun YangKai
The '
found_key' variable was only used to temporarily store a key value
In the changelog text it's ok to use quoting but not in the subject, due to the special characters.
Quoted 14 lines
before copying it to '
min_key' at the end of the function when returning success (ret=0).This commit optimizes the code by:
1. Eliminating the intermediate 'found_key' variable
2. Directly populating 'min_key' at the exact loop exit points where ret=0 is set
3. Removing the final memcpy operation in the return pathThis change improves code clarity by:
- Removing redundant variable usage
- Simplifying the success path logic
- Reducing memory operations
Quoted 5 lines
The found key value is now stored directly into the destination '
min_key' structure at the point of discovery, maintaining identical functionality while:
- Eliminating an unnecessary memory copy
- Reducing code complexity
I think the text is overexplaining, you can keep it to the point what the cleanup does and that it does not change the logic but does not need to mention general statements.
Quoted 18 lines
Signed-off-by Sun YangKai <[email protected]>--- fs/btrfs/ctree.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)fs/btrfs/ctree.c +0
-1@@ -4608,7 +4608,6 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, u64 min_trans) {struct extent_buffer *cur; -struct btrfs_key found_key;int slot;int sret;u32 nritems;
fs/btrfs/ctree.c int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, · line 4646
Quoted 5 lines
fs/btrfs/ctree.c +1
-1@@ -4644,7 +4643,8 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, goto find_next_key; ret =0 ; path->slots[level] = slot; -btrfs_item_key_to_cpu (cur, &found_key, slot); +/* save our key for returning back */
If you're moving a comment please also fix it so it conforms to the preferred style, it's a sentence, so capital first letter and ended by a period. For old code like that's it's a rare opportunity to do it so it ought to be done.
Quoted 19 lines
fs/btrfs/ctree.c +3
-2+btrfs_item_key_to_cpu (cur, min_key, slot); goto out; }if (sret && slot >0 )@@ -4679,11 +4679,11 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, goto out; } } -/* save our key for returning back */ -btrfs_node_key_to_cpu (cur, &found_key, slot); path->slots[level] = slot;if (level == path->lowest_level) { ret =0 ; +/* save our key for returning back */ +btrfs_node_key_to_cpu (cur, min_key, slot); goto out; } cur =btrfs_read_node_slot (cur, slot);
fs/btrfs/ctree.c int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, · line 4705
Quoted 5 lines
fs/btrfs/ctree.c +0
-1@@ -4702,7 +4702,6 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, path->keep_locks = keep_locks;if (ret ==0 ) {btrfs_unlock_up_safe (path, path->lowest_level +1 ); -memcpy (min_key, &found_key,sizeof (found_key)); }
Single statement 'if' should not enclose the statements in { }
Quoted 5 lines
fs/btrfs/ctree.c +0
-0return ret; }Signature · 1 line
2.48.1
PATCH 3/3 btrfs: avoid redundant slot assignment in btrfs_search_forward()
In reply to Sun YangKaiThis patch moves path->slots[level] = slot before the condition check to prevent duplicate assignment. Previously, the slot was set both inside and after the slot >= nritems block with no change in its value, which is unnecessary.
--- fs/btrfs/ctree.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/ctree.c +1 -2
@@ -4668,8 +4668,8 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,* we didn't find a candidate key in this node, walk forward * and find another one */ + path->slots[level] = slot;if (slot >= nritems) { - path->slots[level] = slot; sret =btrfs_find_next_key (root, path, min_key, level, min_trans);if (sret ==0 ) {
@@ -4679,7 +4679,6 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, goto out; } } - path->slots[level] = slot;if (level == path->lowest_level) { ret =0 ;/* save our key for returning back */
Signature · 1 line
2.48.1
Quoted 10 lines · Sun YangKai
These patches are not intented to change any behavior of current code. Just trying to make the codes easier to read, and, maybe, perform better. I'm working on
btrfs_search_forward(), trying to improve it and fix some misbehaviors. And I'll send some patches that will change the behavior of the code later.I'm new to the maillist, trying to read the implements, and improve it from my perspective.
If you have any feedback or questions, please let me know :)
Quoted 5 lines
Sun YangKai (3):
btrfs: improve readability insearch_ioctl()
btrfs: remove unnecessary 'found_key' local variable in
btrfs_search_forward()
btrfs: avoid redundant slot assignment inbtrfs_search_forward()
The cleanups look good to me, thanks. There are so minor style issues, commented under the patches.