btrfs-progs: make a local copy of btrfs_next_sibling_block in print-tree.c

We use this in print-tree to do BFS tree printing, but there are no
other users and it doesn't exist upstream.  Copy the current code and
clean it up so it can exist in print-tree.c and use the local copy
there.  This will allow us to remove the function call when ctree.c is
synced.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Josef Bacik 2023-08-23 10:32:42 -04:00 committed by David Sterba
parent 3808db2b3e
commit 0c58bfd5b6
1 changed files with 52 additions and 1 deletions

View File

@ -1579,6 +1579,57 @@ out:
return ret;
}
/*
* Walk up the tree as far as necessary to find the next sibling tree block.
* More generic version of btrfs_next_leaf(), as it could find sibling nodes if
* @path->lowest_level is not 0.
*
* Returns 0 if it found something or 1 if there are no greater leaves.
* Returns < 0 on io errors.
*/
static int next_sibling_tree_block(struct btrfs_fs_info *fs_info,
struct btrfs_path *path)
{
int slot;
int level = path->lowest_level + 1;
struct extent_buffer *eb;
struct extent_buffer *next = NULL;
BUG_ON(path->lowest_level + 1 >= BTRFS_MAX_LEVEL);
do {
if (!path->nodes[level])
return 1;
slot = path->slots[level] + 1;
eb = path->nodes[level];
if (slot >= btrfs_header_nritems(eb)) {
level++;
if (level == BTRFS_MAX_LEVEL)
return 1;
continue;
}
next = btrfs_read_node_slot(eb, slot);
if (!extent_buffer_uptodate(next))
return -EIO;
break;
} while (level < BTRFS_MAX_LEVEL);
path->slots[level] = slot;
while(1) {
level--;
eb = path->nodes[level];
free_extent_buffer(eb);
path->nodes[level] = next;
path->slots[level] = 0;
if (level == path->lowest_level)
break;
next = btrfs_read_node_slot(next, 0);
if (!extent_buffer_uptodate(next))
return -EIO;
}
return 0;
}
static void bfs_print_children(struct extent_buffer *root_eb, unsigned int mode)
{
struct btrfs_fs_info *fs_info = root_eb->fs_info;
@ -1609,7 +1660,7 @@ static void bfs_print_children(struct extent_buffer *root_eb, unsigned int mode)
/* Print all sibling tree blocks */
while (1) {
btrfs_print_tree(path.nodes[cur_level], mode);
ret = btrfs_next_sibling_tree_block(fs_info, &path);
ret = next_sibling_tree_block(fs_info, &path);
if (ret < 0)
goto out;
if (ret > 0) {