btrfs: use true/false and simplify boolean parameters in btrfs_{inc,dec}_ref
v2 cover btrfs: use true/false and simplify boolean parameters in btrfs_{inc,dec}_ref
This tiny series removes the last 0/1 integer literals passed to btrfs_inc_ref() / btrfs_dec_ref() and replaces the open-coded if/else blocks with concise boolean expressions.
Patch 1 switches the callsites to the self-documenting true/false constants, eliminating the implicit bool <-> int mixing.
Patch 2 folds the remaining if/else ladders into a single line using the condition directly, which shrinks the code and makes the intent obvious.
No functional change.
[Changelog]
v2:
- Introduce local variable for the conditions in patch 2, suggested by Boris
- Merge the non-zero ret handling blocks in btrfs_copy_root()
Sun YangKai (2):
btrfs: use true/false for boolean parameters in btrfs_{inc,dec}_ref
btrfs: simplify boolean argument for btrfs_{inc,dec}_ref
fs/btrfs/ctree.c | 46 +++++++++++++++--------------------------- fs/btrfs/extent-tree.c | 21 +++++++------------ 2 files changed, 23 insertions(+), 44 deletions(-)
Signature · 1 line
2.51.2
PATCH v2 1/2 btrfs: use true/false for boolean parameters in btrfs_{inc,dec}_ref
Replace integer literals 0/1 with true/false when calling btrfs_inc_ref() and btrfs_dec_ref() to make the code self-documenting and avoid mixing bool/integer types.
No functional change intended.
--- fs/btrfs/ctree.c | 20 ++++++++++---------- fs/btrfs/extent-tree.c | 8 ++++---- 2 files changed, 14 insertions(+), 14 deletions(-)
fs/btrfs/ctree.c +10 -10
@@ -292,11 +292,11 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans, }if (new_root_objectid ==BTRFS_TREE_RELOC_OBJECTID ) { - ret =btrfs_inc_ref (trans, root, cow,1 ); + ret =btrfs_inc_ref (trans, root, cow, true);if (unlikely (ret))btrfs_abort_transaction (trans, ret); }else { - ret =btrfs_inc_ref (trans, root, cow,0 ); + ret =btrfs_inc_ref (trans, root, cow, false);if (unlikely (ret))btrfs_abort_transaction (trans, ret); }
@@ -420,15 +420,15 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,if ((owner ==btrfs_root_id (root) ||btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID ) && !(flags &BTRFS_BLOCK_FLAG_FULL_BACKREF )) { - ret =btrfs_inc_ref (trans, root, buf,1 ); + ret =btrfs_inc_ref (trans, root, buf, true);if (ret)return ret;if (btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID ) { - ret =btrfs_dec_ref (trans, root, buf,0 ); + ret =btrfs_dec_ref (trans, root, buf, false);if (ret)return ret; - ret =btrfs_inc_ref (trans, root, cow,1 ); + ret =btrfs_inc_ref (trans, root, cow, true);if (ret)return ret; }
@@ -439,21 +439,21 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, }else {if (btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID ) - ret =btrfs_inc_ref (trans, root, cow,1 ); + ret =btrfs_inc_ref (trans, root, cow, true);else - ret =btrfs_inc_ref (trans, root, cow,0 ); + ret =btrfs_inc_ref (trans, root, cow, false);if (ret)return ret; } }else {if (flags &BTRFS_BLOCK_FLAG_FULL_BACKREF ) {if (btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID ) - ret =btrfs_inc_ref (trans, root, cow,1 ); + ret =btrfs_inc_ref (trans, root, cow, true);else - ret =btrfs_inc_ref (trans, root, cow,0 ); + ret =btrfs_inc_ref (trans, root, cow, false);if (ret)return ret; - ret =btrfs_dec_ref (trans, root, buf,1 ); + ret =btrfs_dec_ref (trans, root, buf, true);if (ret)return ret; }
fs/btrfs/extent-tree.c +4 -4
@@ -5469,12 +5469,12 @@ static noinline int walk_down_proc(struct btrfs_trans_handle *trans,/* wc->stage == UPDATE_BACKREF */ if (!(wc->flags[level] & flag)) {ASSERT (path->locks[level]); - ret =btrfs_inc_ref (trans, root, eb,1 ); + ret =btrfs_inc_ref (trans, root, eb, true);if (unlikely (ret)) {btrfs_abort_transaction (trans, ret);return ret; } - ret =btrfs_dec_ref (trans, root, eb,0 ); + ret =btrfs_dec_ref (trans, root, eb, false);if (unlikely (ret)) {btrfs_abort_transaction (trans, ret);return ret;
@@ -5876,13 +5876,13 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,if (wc->refs[level] ==1 ) {if (level ==0 ) {if (wc->flags[level] &BTRFS_BLOCK_FLAG_FULL_BACKREF ) { - ret =btrfs_dec_ref (trans, root, eb,1 ); + ret =btrfs_dec_ref (trans, root, eb, true);if (ret) {btrfs_abort_transaction (trans, ret);return ret; } }else { - ret =btrfs_dec_ref (trans, root, eb,0 ); + ret =btrfs_dec_ref (trans, root, eb, false);if (unlikely (ret)) {btrfs_abort_transaction (trans, ret);return ret;
Signature · 1 line
2.51.2
PATCH v2 2/2 btrfs: simplify boolean argument for btrfs_{inc,dec}_ref
In reply to Sun YangKaiReplace open-coded if/else blocks with the boolean directly and introduce local const bool variables, making the code shorter and easier to read.
No functional change.
--- fs/btrfs/ctree.c | 38 ++++++++++++-------------------------- fs/btrfs/extent-tree.c | 17 +++++------------ 2 files changed, 17 insertions(+), 38 deletions(-)
fs/btrfs/ctree.c +12 -26
@@ -249,6 +249,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,int ret =0 ;int level;struct btrfs_disk_key disk_key; +const bool is_reloc_root = (new_root_objectid ==BTRFS_TREE_RELOC_OBJECTID );u64 reloc_src_root =0 ; WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
@@ -262,7 +263,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,else btrfs_node_key (buf, &disk_key,0 ); -if (new_root_objectid ==BTRFS_TREE_RELOC_OBJECTID ) +if (is_reloc_root) reloc_src_root =btrfs_header_owner (buf); cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid, &disk_key, level, buf->start, 0,
@@ -276,7 +277,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,btrfs_set_header_backref_rev (cow,BTRFS_MIXED_BACKREF_REV );btrfs_clear_header_flag (cow,BTRFS_HEADER_FLAG_WRITTEN |BTRFS_HEADER_FLAG_RELOC ); -if (new_root_objectid ==BTRFS_TREE_RELOC_OBJECTID ) +if (is_reloc_root)btrfs_set_header_flag (cow,BTRFS_HEADER_FLAG_RELOC );else btrfs_set_header_owner (cow, new_root_objectid);
@@ -291,16 +292,9 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,return ret; } -if (new_root_objectid ==BTRFS_TREE_RELOC_OBJECTID ) { - ret =btrfs_inc_ref (trans, root, cow, true); -if (unlikely (ret)) -btrfs_abort_transaction (trans, ret); - }else { - ret =btrfs_inc_ref (trans, root, cow, false); -if (unlikely (ret)) -btrfs_abort_transaction (trans, ret); - } -if (ret) { + ret =btrfs_inc_ref (trans, root, cow, is_reloc_root); +if (unlikely (ret)) { +btrfs_abort_transaction (trans, ret);btrfs_tree_unlock (cow);free_extent_buffer (cow);return ret;
@@ -362,6 +356,7 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,u64 owner;u64 flags;int ret; +const bool is_reloc_root = (btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID );/* * Backrefs update rules:
@@ -397,8 +392,7 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, } }else { refs =1 ; -if (btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID || -btrfs_header_backref_rev (buf) <BTRFS_MIXED_BACKREF_REV ) +if (is_reloc_root ||btrfs_header_backref_rev (buf) <BTRFS_MIXED_BACKREF_REV ) flags =BTRFS_BLOCK_FLAG_FULL_BACKREF ;else flags =0 ;
@@ -417,14 +411,13 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, }if (refs >1 ) { -if ((owner ==btrfs_root_id (root) || -btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID ) && +if ((owner ==btrfs_root_id (root) || is_reloc_root) && !(flags &BTRFS_BLOCK_FLAG_FULL_BACKREF )) { ret =btrfs_inc_ref (trans, root, buf, true);if (ret)return ret; -if (btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID ) { +if (is_reloc_root) { ret =btrfs_dec_ref (trans, root, buf, false);if (ret)return ret;
@@ -437,20 +430,13 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,if (ret)return ret; }else { - -if (btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID ) - ret =btrfs_inc_ref (trans, root, cow, true); -else - ret =btrfs_inc_ref (trans, root, cow, false); + ret =btrfs_inc_ref (trans, root, cow, is_reloc_root);if (ret)return ret; } }else {if (flags &BTRFS_BLOCK_FLAG_FULL_BACKREF ) { -if (btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID ) - ret =btrfs_inc_ref (trans, root, cow, true); -else - ret =btrfs_inc_ref (trans, root, cow, false); + ret =btrfs_inc_ref (trans, root, cow, is_reloc_root);if (ret)return ret; ret =btrfs_dec_ref (trans, root, buf, true);
fs/btrfs/extent-tree.c +5 -12
@@ -5875,18 +5875,11 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,if (wc->refs[level] ==1 ) {if (level ==0 ) { -if (wc->flags[level] &BTRFS_BLOCK_FLAG_FULL_BACKREF ) { - ret =btrfs_dec_ref (trans, root, eb, true); -if (ret) { -btrfs_abort_transaction (trans, ret); -return ret; - } - }else { - ret =btrfs_dec_ref (trans, root, eb, false); -if (unlikely (ret)) { -btrfs_abort_transaction (trans, ret); -return ret; - } +const bool full_backref = (wc->flags[level] &BTRFS_BLOCK_FLAG_FULL_BACKREF ); + ret =btrfs_dec_ref (trans, root, eb, full_backref); +if (unlikely (ret)) { +btrfs_abort_transaction (trans, ret); +return ret; }if (btrfs_is_fstree (btrfs_root_id (root))) { ret =btrfs_qgroup_trace_leaf_items (trans, eb);
Signature · 1 line
2.51.2
Re: [PATCH v2 2/2] btrfs: simplify boolean argument for btrfs_{inc,dec}_ref
Quoted 46 lines · Sun YangKai
Replace open-coded if/else blocks with the boolean directly and introduce local const bool variables, making the code shorter and easier to read.
No functional change.
Signed-off-by Sun YangKai <[email protected]>--- fs/btrfs/ctree.c | 38 ++++++++++++-------------------------- fs/btrfs/extent-tree.c | 17 +++++------------ 2 files changed, 17 insertions(+), 38 deletions(-)fs/btrfs/ctree.c +3
-3@@ -249,6 +249,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,int ret =0 ;int level;struct btrfs_disk_key disk_key; +const bool is_reloc_root = (new_root_objectid ==BTRFS_TREE_RELOC_OBJECTID );u64 reloc_src_root =0 ; WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&@@ -262,7 +263,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,else btrfs_node_key (buf, &disk_key,0 ); -if (new_root_objectid ==BTRFS_TREE_RELOC_OBJECTID ) +if (is_reloc_root) reloc_src_root =btrfs_header_owner (buf); cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid, &disk_key, level, buf->start, 0,@@ -276,7 +277,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,btrfs_set_header_backref_rev (cow,BTRFS_MIXED_BACKREF_REV );btrfs_clear_header_flag (cow,BTRFS_HEADER_FLAG_WRITTEN |BTRFS_HEADER_FLAG_RELOC ); -if (new_root_objectid ==BTRFS_TREE_RELOC_OBJECTID ) +if (is_reloc_root)btrfs_set_header_flag (cow,BTRFS_HEADER_FLAG_RELOC );else btrfs_set_header_owner (cow, new_root_objectid);@@ -291,16 +292,9 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,return ret; } -if (new_root_objectid ==BTRFS_TREE_RELOC_OBJECTID ) {
fs/btrfs/ctree.c int btrfs_copy_root(struct btrfs_trans_handle *trans, · line 295
Quoted 10 lines
fs/btrfs/ctree.c +1
-9- ret =btrfs_inc_ref (trans, root, cow, true); -if (unlikely (ret)) -btrfs_abort_transaction (trans, ret); - }else { - ret =btrfs_inc_ref (trans, root, cow, false); -if (unlikely (ret)) -btrfs_abort_transaction (trans, ret); - } -if (ret) { + ret =btrfs_inc_ref (trans, root, cow, is_reloc_root);
Now we have something like this:
ret = btrfs_inc_ref(trans, root, cow, is_reloc_root);
As suggested by Daniel, it would be helpful to add a comment explaining why we pass true to @full_backref when dealing with a relocation tree (is_reloc_root). However, I currently lack the knowledge to provide a clear explanation.
Quoted 91 lines
fs/btrfs/ctree.c +8
-14+if (unlikely (ret)) { +btrfs_abort_transaction (trans, ret);btrfs_tree_unlock (cow);free_extent_buffer (cow);return ret;@@ -362,6 +356,7 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,u64 owner;u64 flags;int ret; +const bool is_reloc_root = (btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID );/* * Backrefs update rules: @@ -397,8 +392,7 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, } }else { refs =1 ; -if (btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID || -btrfs_header_backref_rev (buf) <BTRFS_MIXED_BACKREF_REV ) +if (is_reloc_root ||btrfs_header_backref_rev (buf) <BTRFS_MIXED_BACKREF_REV ) flags =BTRFS_BLOCK_FLAG_FULL_BACKREF ;else flags =0 ;@@ -417,14 +411,13 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, }if (refs >1 ) { -if ((owner ==btrfs_root_id (root) || -btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID ) && +if ((owner ==btrfs_root_id (root) || is_reloc_root) && !(flags &BTRFS_BLOCK_FLAG_FULL_BACKREF )) { ret =btrfs_inc_ref (trans, root, buf, true);if (ret)return ret; -if (btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID ) { +if (is_reloc_root) { ret =btrfs_dec_ref (trans, root, buf, false);if (ret)return ret;@@ -437,20 +430,13 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,if (ret)return ret; }else { - -if (btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID ) - ret =btrfs_inc_ref (trans, root, cow, true); -else - ret =btrfs_inc_ref (trans, root, cow, false); + ret =btrfs_inc_ref (trans, root, cow, is_reloc_root);if (ret)return ret; } }else {if (flags &BTRFS_BLOCK_FLAG_FULL_BACKREF ) { -if (btrfs_root_id (root) ==BTRFS_TREE_RELOC_OBJECTID ) - ret =btrfs_inc_ref (trans, root, cow, true); -else - ret =btrfs_inc_ref (trans, root, cow, false); + ret =btrfs_inc_ref (trans, root, cow, is_reloc_root);if (ret)return ret; ret =btrfs_dec_ref (trans, root, buf, true);fs/btrfs/extent-tree.c +5
-12@@ -5875,18 +5875,11 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,if (wc->refs[level] ==1 ) {if (level ==0 ) { -if (wc->flags[level] &BTRFS_BLOCK_FLAG_FULL_BACKREF ) { - ret =btrfs_dec_ref (trans, root, eb, true); -if (ret) { -btrfs_abort_transaction (trans, ret); -return ret; - } - }else { - ret =btrfs_dec_ref (trans, root, eb, false); -if (unlikely (ret)) { -btrfs_abort_transaction (trans, ret); -return ret; - } +const bool full_backref = (wc->flags[level] &BTRFS_BLOCK_FLAG_FULL_BACKREF ); + ret =btrfs_dec_ref (trans, root, eb, full_backref); +if (unlikely (ret)) { +btrfs_abort_transaction (trans, ret); +return ret; }if (btrfs_is_fstree (btrfs_root_id (root))) { ret =btrfs_qgroup_trace_leaf_items (trans, eb);
Re: [PATCH v2 2/2] btrfs: simplify boolean argument for btrfs_{inc,dec}_ref
Quoted 14 lines · Sun Yangkai
fs/btrfs/ctree.c int btrfs_copy_root(struct btrfs_trans_handle *trans, · line 295
Quoted 11 lines
fs/btrfs/ctree.c +1
-10-if (new_root_objectid ==BTRFS_TREE_RELOC_OBJECTID ) { - ret =btrfs_inc_ref (trans, root, cow, true); -if (unlikely (ret)) -btrfs_abort_transaction (trans, ret); - }else { - ret =btrfs_inc_ref (trans, root, cow, false); -if (unlikely (ret)) -btrfs_abort_transaction (trans, ret); - } -if (ret) { + ret =btrfs_inc_ref (trans, root, cow, is_reloc_root);Now we have something like this:
Quoted 4 lines
ret =
btrfs_inc_ref(trans, root, cow, is_reloc_root);As suggested by Daniel, it would be helpful to add a comment explaining why we pass true to @full_backref when dealing with a relocation tree (
is_reloc_root).
The relocation tree is special in many ways, there was no comment in the old code and I'm not sure how much explanation we'd need here because the code dealing with relocation tree is scattered everywhere.
Quoted 17 lines · Sun YangKai
This tiny series removes the last 0/1 integer literals passed to
btrfs_inc_ref()/btrfs_dec_ref()and replaces the open-coded if/else blocks with concise boolean expressions.Patch 1 switches the callsites to the self-documenting true/false constants, eliminating the implicit bool <-> int mixing.
Patch 2 folds the remaining if/else ladders into a single line using the condition directly, which shrinks the code and makes the intent obvious.
No functional change.
[Changelog]
v2:
Quoted 7 lines
- Introduce local variable for the conditions in patch 2, suggested by Boris
- Merge the non-zero ret handling blocks in
btrfs_copy_root()Sun YangKai (2):
btrfs: use true/false for boolean parameters in btrfs_{inc,dec}_ref
btrfs: simplify boolean argument for btrfs_{inc,dec}_ref
Added to for-next, thanks.
Re: [PATCH v2 1/2] btrfs: use true/false for boolean parameters in btrfs_{inc,dec}_ref
In reply to Sun YangKaiQuoted 5 lines · Sun YangKai
Replace integer literals 0/1 with true/false when calling
btrfs_inc_ref()andbtrfs_dec_ref()to make the code self-documenting and avoid mixing bool/integer types.No functional change intended.
You don't need to write this sentence to the changelog, in this case it's obvious it's a simple change, and complicated changes would be explained in full.
Re: [PATCH v2 1/2] btrfs: use true/false for boolean parameters in btrfs_{inc,dec}_ref
Got it. Thanks.
Quoted 10 lines · David Sterba
Quoted 5 lines · Sun YangKai
Replace integer literals 0/1 with true/false when calling
btrfs_inc_ref()andbtrfs_dec_ref()to make the code self-documenting and avoid mixing bool/integer types.No functional change intended.
You don't need to write this sentence to the changelog, in this case it's obvious it's a simple change, and complicated changes would be explained in full.