Skip to contentSkip to conversation

btrfs: Remove the duplicated @level parameter for btrfs_bin_search()

PATCH In review Qu Wenruo 5 participants 5 messages Apr 17, 2020 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/1 btrfs: Remove the duplicated @level parameter for btrfs_bin_search()

We can easily get the level from @eb parameter, thus the level is not needed.

This is inspired by the work of Marek in U-boot.

---
 fs/btrfs/ctree.c      | 14 +++++++-------
 fs/btrfs/ctree.h      |  2 +-
 fs/btrfs/relocation.c |  8 +++-----
 fs/btrfs/tree-log.c   |  3 +--
 4 files changed, 12 insertions(+), 15 deletions(-)
fs/btrfs/ctree.c +7 -7
index bfedbbe2311f..6c28efe5b14a 100644
@@ -1733,9 +1733,9 @@ static noinline int generic_bin_search(struct extent_buffer *eb,
  * leaves vs nodes
  */
 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
-		     int level, int *slot)
+		     int *slot)
 {
-	if (level == 0)
+	if (btrfs_header_level(eb) == 0)
 		return generic_bin_search(eb,
 					  offsetof(struct btrfs_leaf, items),
 					  sizeof(struct btrfs_item),
@@ -2502,10 +2502,10 @@ setup_nodes_for_search(struct btrfs_trans_handle *trans,
 }
 
 static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
-		      int level, int *prev_cmp, int *slot)
+		      int *prev_cmp, int *slot)
 {
 	if (*prev_cmp != 0) {
-		*prev_cmp = btrfs_bin_search(b, key, level, slot);
+		*prev_cmp = btrfs_bin_search(b, key, slot);
 		return *prev_cmp;
 	}
 
@@ -2783,7 +2783,7 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 			}
 		}
 
-		ret = key_search(b, key, level, &prev_cmp, &slot);
+		ret = key_search(b, key, &prev_cmp, &slot);
 		if (ret < 0)
 			goto done;
 
@@ -2947,7 +2947,7 @@ int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
 		 * time.
 		 */
 		prev_cmp = -1;
-		ret = key_search(b, key, level, &prev_cmp, &slot);
+		ret = key_search(b, key, &prev_cmp, &slot);
 		if (ret < 0)
 			goto done;
 
@@ -5103,7 +5103,7 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
 	while (1) {
 		nritems = btrfs_header_nritems(cur);
 		level = btrfs_header_level(cur);
-		sret = btrfs_bin_search(cur, min_key, level, &slot);
+		sret = btrfs_bin_search(cur, min_key, &slot);
 		if (sret < 0) {
 			ret = sret;
 			goto out;
fs/btrfs/ctree.h +1 -1
index 4d787d749315..dfbcf794d895 100644
@@ -2305,7 +2305,7 @@ void btrfs_wait_for_snapshot_creation(struct btrfs_root *root);
 
 /* ctree.c */
 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
-		     int level, int *slot);
+		     int *slot);
 int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
 int btrfs_previous_item(struct btrfs_root *root,
 			struct btrfs_path *path, u64 min_objectid,
fs/btrfs/relocation.c +3 -5
index f65595602aa8..6f2dd203a1c6 100644
@@ -1925,7 +1925,7 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,
 		level = btrfs_header_level(parent);
 		BUG_ON(level < lowest_level);
 
-		ret = btrfs_bin_search(parent, &key, level, &slot);
+		ret = btrfs_bin_search(parent, &key, &slot);
 		if (ret < 0)
 			break;
 		if (ret && slot > 0)
@@ -2914,8 +2914,7 @@ static int do_relocation(struct btrfs_trans_handle *trans,
 
 		if (upper->eb && !upper->locked) {
 			if (!lowest) {
-				ret = btrfs_bin_search(upper->eb, key,
-						       upper->level, &slot);
+				ret = btrfs_bin_search(upper->eb, key, &slot);
 				if (ret < 0) {
 					err = ret;
 					goto next;
@@ -2953,8 +2952,7 @@ static int do_relocation(struct btrfs_trans_handle *trans,
 			slot = path->slots[upper->level];
 			btrfs_release_path(path);
 		} else {
-			ret = btrfs_bin_search(upper->eb, key, upper->level,
-					       &slot);
+			ret = btrfs_bin_search(upper->eb, key, &slot);
 			if (ret < 0) {
 				err = ret;
 				goto next;
fs/btrfs/tree-log.c +1 -2
index 58c111474ba5..981a3df9c7ff 100644
@@ -3816,8 +3816,7 @@ static int drop_objectid_items(struct btrfs_trans_handle *trans,
 
 		found_key.offset = 0;
 		found_key.type = 0;
-		ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
-				       &start_slot);
+		ret = btrfs_bin_search(path->nodes[0], &found_key, &start_slot);
 		if (ret < 0)
 			break;
 
Signature · 1 line
2.26.0
Reviewed-by on v1 1/1
#2 lore raw
In reply to Qu Wenruo
Quoted 7 lines · Qu Wenruo

We can easily get the level from @eb parameter, thus the level is not needed.

This is inspired by the work of Marek in U-boot.

<snip>

fs/btrfs/relocation.c static int do_relocation(struct btrfs_trans_handle *trans, · line 2955

Quoted 7 lines
fs/btrfs/relocation.c +1 -2
@@ -2953,8 +2952,7 @@ static int do_relocation(struct btrfs_trans_handle *trans,
 			slot = path->slots[upper->level];
 			btrfs_release_path(path);
 		} else {
-			ret = btrfs_bin_search(upper->eb, key, upper->level,
-					       &slot);
+			ret = btrfs_bin_search(upper->eb, key, &slot);

nit: By the same token why does btrfs_backref_node need an explicit level member if its level is always equal to that of eb->level ?

<snip>

Quoted 1 line
#3 lore raw
Quoted 15 lines · Nikolay Borisov
Quoted 7 lines · Qu Wenruo

We can easily get the level from @eb parameter, thus the level is not needed.

This is inspired by the work of Marek in U-boot.

<snip>

Quoted 10 lines

fs/btrfs/relocation.c static int do_relocation(struct btrfs_trans_handle *trans, · line 2955

Quoted 7 lines
fs/btrfs/relocation.c +1 -2
@@ -2953,8 +2952,7 @@ static int do_relocation(struct btrfs_trans_handle *trans,
 			slot = path->slots[upper->level];
 			btrfs_release_path(path);
 		} else {
-			ret = btrfs_bin_search(upper->eb, key, upper->level,
-					       &slot);
+			ret = btrfs_bin_search(upper->eb, key, &slot);

nit: By the same token why does btrfs_backref_node need an explicit level member if its level is always equal to that of eb->level ?

I guess because btrfs_backref_node::eb is not always ensured to exist.

Thanks,
Qu

Quoted 3 lines

<snip>

Quoted 1 line
#4 lore raw
In reply to Qu Wenruo
Quoted 7 lines · Qu Wenruo

We can easily get the level from @eb parameter, thus the level is not needed.

This is inspired by the work of Marek in U-boot.

Added to misc-next, thanks. Please don't use the @ formatting in the subject, this is for the docs.

Quoted 125 lines
---
 fs/btrfs/ctree.c      | 14 +++++++-------
 fs/btrfs/ctree.h      |  2 +-
 fs/btrfs/relocation.c |  8 +++-----
 fs/btrfs/tree-log.c   |  3 +--
 4 files changed, 12 insertions(+), 15 deletions(-)
fs/btrfs/ctree.c +7 -7
index bfedbbe2311f..6c28efe5b14a 100644
@@ -1733,9 +1733,9 @@ static noinline int generic_bin_search(struct extent_buffer *eb,
  * leaves vs nodes
  */
 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
-		     int level, int *slot)
+		     int *slot)
 {
-	if (level == 0)
+	if (btrfs_header_level(eb) == 0)
 		return generic_bin_search(eb,
 					  offsetof(struct btrfs_leaf, items),
 					  sizeof(struct btrfs_item),
@@ -2502,10 +2502,10 @@ setup_nodes_for_search(struct btrfs_trans_handle *trans,
 }
 
 static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
-		      int level, int *prev_cmp, int *slot)
+		      int *prev_cmp, int *slot)
 {
 	if (*prev_cmp != 0) {
-		*prev_cmp = btrfs_bin_search(b, key, level, slot);
+		*prev_cmp = btrfs_bin_search(b, key, slot);
 		return *prev_cmp;
 	}
 
@@ -2783,7 +2783,7 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 			}
 		}
 
-		ret = key_search(b, key, level, &prev_cmp, &slot);
+		ret = key_search(b, key, &prev_cmp, &slot);
 		if (ret < 0)
 			goto done;
 
@@ -2947,7 +2947,7 @@ int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
 		 * time.
 		 */
 		prev_cmp = -1;
-		ret = key_search(b, key, level, &prev_cmp, &slot);
+		ret = key_search(b, key, &prev_cmp, &slot);
 		if (ret < 0)
 			goto done;
 
@@ -5103,7 +5103,7 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
 	while (1) {
 		nritems = btrfs_header_nritems(cur);
 		level = btrfs_header_level(cur);
-		sret = btrfs_bin_search(cur, min_key, level, &slot);
+		sret = btrfs_bin_search(cur, min_key, &slot);
 		if (sret < 0) {
 			ret = sret;
 			goto out;
fs/btrfs/ctree.h +1 -1
index 4d787d749315..dfbcf794d895 100644
@@ -2305,7 +2305,7 @@ void btrfs_wait_for_snapshot_creation(struct btrfs_root *root);
 
 /* ctree.c */
 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
-		     int level, int *slot);
+		     int *slot);
 int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
 int btrfs_previous_item(struct btrfs_root *root,
 			struct btrfs_path *path, u64 min_objectid,
fs/btrfs/relocation.c +3 -5
index f65595602aa8..6f2dd203a1c6 100644
@@ -1925,7 +1925,7 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,
 		level = btrfs_header_level(parent);
 		BUG_ON(level < lowest_level);
 
-		ret = btrfs_bin_search(parent, &key, level, &slot);
+		ret = btrfs_bin_search(parent, &key, &slot);
 		if (ret < 0)
 			break;
 		if (ret && slot > 0)
@@ -2914,8 +2914,7 @@ static int do_relocation(struct btrfs_trans_handle *trans,
 
 		if (upper->eb && !upper->locked) {
 			if (!lowest) {
-				ret = btrfs_bin_search(upper->eb, key,
-						       upper->level, &slot);
+				ret = btrfs_bin_search(upper->eb, key, &slot);
 				if (ret < 0) {
 					err = ret;
 					goto next;
@@ -2953,8 +2952,7 @@ static int do_relocation(struct btrfs_trans_handle *trans,
 			slot = path->slots[upper->level];
 			btrfs_release_path(path);
 		} else {
-			ret = btrfs_bin_search(upper->eb, key, upper->level,
-					       &slot);
+			ret = btrfs_bin_search(upper->eb, key, &slot);
 			if (ret < 0) {
 				err = ret;
 				goto next;
fs/btrfs/tree-log.c +1 -2
index 58c111474ba5..981a3df9c7ff 100644
@@ -3816,8 +3816,7 @@ static int drop_objectid_items(struct btrfs_trans_handle *trans,
 
 		found_key.offset = 0;
 		found_key.type = 0;
-		ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
-				       &start_slot);
+		ret = btrfs_bin_search(path->nodes[0], &found_key, &start_slot);
 		if (ret < 0)
 			break;
 
Signature · 1 line
2.26.0