diff mbox series

[v2,2/6] btrfs: update btrfs_add_block_group_cache() to use rb helper

Message ID e5a958037e7f03b124b66f392d20343482b65e61.1734108739.git.beckerlee3@gmail.com (mailing list archive)
State New
Headers show
Series reduce boilerplate code within btrfs | expand

Commit Message

Lee Beckermeyer Dec. 15, 2024, 3:26 p.m. UTC
update fs/btrfs/block-group.c to use rb_find_add_cached(),
also implements btrfs_bg_start_cmp() for use with
rb_find_add_cached().

Suggested-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Roger L. Beckermeyer III <beckerlee3@gmail.com>
---
 fs/btrfs/block-group.c | 41 ++++++++++++++++++-----------------------
 1 file changed, 18 insertions(+), 23 deletions(-)

Comments

Qu Wenruo Dec. 15, 2024, 10:23 p.m. UTC | #1
在 2024/12/16 01:56, Roger L. Beckermeyer III 写道:
> update fs/btrfs/block-group.c to use rb_find_add_cached(),
> also implements btrfs_bg_start_cmp() for use with
> rb_find_add_cached().
>
> Suggested-by: Josef Bacik <josef@toxicpanda.com>
> Signed-off-by: Roger L. Beckermeyer III <beckerlee3@gmail.com>
> ---
>   fs/btrfs/block-group.c | 41 ++++++++++++++++++-----------------------
>   1 file changed, 18 insertions(+), 23 deletions(-)
>
> diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
> index 5be029734cfa..a8d51023f7a4 100644
> --- a/fs/btrfs/block-group.c
> +++ b/fs/btrfs/block-group.c
> @@ -173,40 +173,35 @@ void btrfs_put_block_group(struct btrfs_block_group *cache)
>   	}
>   }
>
> +static int btrfs_bg_start_cmp(struct rb_node *new, const struct rb_node *exist)
> +{
> +	struct btrfs_block_group *cmp1 = rb_entry(new, struct btrfs_block_group, cache_node);
> +	const struct btrfs_block_group *cmp2 = rb_entry(exist, struct btrfs_block_group, cache_node);
> +
> +	if (cmp1->start < cmp2->start)
> +		return -1;
> +	if (cmp1->start > cmp2->start)
> +		return 1;
> +	return 0;
> +}
> +
>   /*
>    * This adds the block group to the fs_info rb tree for the block group cache
>    */
>   static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
>   				       struct btrfs_block_group *block_group)
>   {
> -	struct rb_node **p;
> -	struct rb_node *parent = NULL;
> -	struct btrfs_block_group *cache;
> -	bool leftmost = true;
> +	struct rb_node *exist;
>
>   	ASSERT(block_group->length != 0);
>
>   	write_lock(&info->block_group_cache_lock);
> -	p = &info->block_group_cache_tree.rb_root.rb_node;
> -
> -	while (*p) {
> -		parent = *p;
> -		cache = rb_entry(parent, struct btrfs_block_group, cache_node);
> -		if (block_group->start < cache->start) {
> -			p = &(*p)->rb_left;
> -		} else if (block_group->start > cache->start) {
> -			p = &(*p)->rb_right;
> -			leftmost = false;
> -		} else {
> -			write_unlock(&info->block_group_cache_lock);
> -			return -EEXIST;
> -		}
> -	}
> -
> -	rb_link_node(&block_group->cache_node, parent, p);
> -	rb_insert_color_cached(&block_group->cache_node,
> -			       &info->block_group_cache_tree, leftmost);
>
> +	exist = rb_find_add_cached(&block_group->cache_node,
> +			&info->block_group_cache_tree, btrfs_bg_start_cmp);
> +	if (exist)
> +		write_unlock(&info->block_group_cache_lock);
> +		return -EEXIST;

IIRC latest GCC would already warn about such indent.
This will cause the function to always return -EEXIST.

Thanks,
Qu

>   	write_unlock(&info->block_group_cache_lock);
>
>   	return 0;
Qu Wenruo Dec. 16, 2024, 8:38 p.m. UTC | #2
在 2024/12/17 06:51, Lee Beckermeyer 写道:
>
>
> On Sun, Dec 15, 2024 at 4:23 PM Qu Wenruo <quwenruo.btrfs@gmx.com
> <mailto:quwenruo.btrfs@gmx.com>> wrote:
>
>
>
>     在 2024/12/16 01:56, Roger L. Beckermeyer III 写道:
>      > update fs/btrfs/block-group.c to use rb_find_add_cached(),
>      > also implements btrfs_bg_start_cmp() for use with
>      > rb_find_add_cached().
>      >
>      > Suggested-by: Josef Bacik <josef@toxicpanda.com
>     <mailto:josef@toxicpanda.com>>
>      > Signed-off-by: Roger L. Beckermeyer III <beckerlee3@gmail.com
>     <mailto:beckerlee3@gmail.com>>
>      > ---
>      >   fs/btrfs/block-group.c | 41 +++++++++++++++++
>     +-----------------------
>      >   1 file changed, 18 insertions(+), 23 deletions(-)
>      >
>      > diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
>      > index 5be029734cfa..a8d51023f7a4 100644
>      > --- a/fs/btrfs/block-group.c
>      > +++ b/fs/btrfs/block-group.c
>      > @@ -173,40 +173,35 @@ void btrfs_put_block_group(struct
>     btrfs_block_group *cache)
>      >       }
>      >   }
>      >
>      > +static int btrfs_bg_start_cmp(struct rb_node *new, const struct
>     rb_node *exist)
>      > +{
>      > +     struct btrfs_block_group *cmp1 = rb_entry(new, struct
>     btrfs_block_group, cache_node);
>      > +     const struct btrfs_block_group *cmp2 = rb_entry(exist,
>     struct btrfs_block_group, cache_node);
>      > +
>      > +     if (cmp1->start < cmp2->start)
>      > +             return -1;
>      > +     if (cmp1->start > cmp2->start)
>      > +             return 1;
>      > +     return 0;
>      > +}
>      > +
>      >   /*
>      >    * This adds the block group to the fs_info rb tree for the
>     block group cache
>      >    */
>      >   static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
>      >                                      struct btrfs_block_group
>     *block_group)
>      >   {
>      > -     struct rb_node **p;
>      > -     struct rb_node *parent = NULL;
>      > -     struct btrfs_block_group *cache;
>      > -     bool leftmost = true;
>      > +     struct rb_node *exist;
>      >
>      >       ASSERT(block_group->length != 0);
>      >
>      >       write_lock(&info->block_group_cache_lock);
>      > -     p = &info->block_group_cache_tree.rb_root.rb_node;
>      > -
>      > -     while (*p) {
>      > -             parent = *p;
>      > -             cache = rb_entry(parent, struct btrfs_block_group,
>     cache_node);
>      > -             if (block_group->start < cache->start) {
>      > -                     p = &(*p)->rb_left;
>      > -             } else if (block_group->start > cache->start) {
>      > -                     p = &(*p)->rb_right;
>      > -                     leftmost = false;
>      > -             } else {
>      > -                     write_unlock(&info->block_group_cache_lock);
>      > -                     return -EEXIST;
>      > -             }
>      > -     }
>      > -
>      > -     rb_link_node(&block_group->cache_node, parent, p);
>      > -     rb_insert_color_cached(&block_group->cache_node,
>      > -                            &info->block_group_cache_tree,
>     leftmost);
>      >
>      > +     exist = rb_find_add_cached(&block_group->cache_node,
>      > +                     &info->block_group_cache_tree,
>     btrfs_bg_start_cmp);
>      > +     if (exist)
>      > +             write_unlock(&info->block_group_cache_lock);
>      > +             return -EEXIST;
>
>     IIRC latest GCC would already warn about such indent.
>     This will cause the function to always return -EEXIST.
>
>     Thanks,
>     Qu
>
> Oof, that's what I get for not compiling it again. reran all fstests
> again just to make sure, got similar results to the first time. Want me
> to just resend the whole email chain again?

I can fix this at merge time.

Thanks,
Qu
>
>
>      >       write_unlock(&info->block_group_cache_lock);
>      >
>      >       return 0;
>
diff mbox series

Patch

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 5be029734cfa..a8d51023f7a4 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -173,40 +173,35 @@  void btrfs_put_block_group(struct btrfs_block_group *cache)
 	}
 }
 
+static int btrfs_bg_start_cmp(struct rb_node *new, const struct rb_node *exist)
+{
+	struct btrfs_block_group *cmp1 = rb_entry(new, struct btrfs_block_group, cache_node);
+	const struct btrfs_block_group *cmp2 = rb_entry(exist, struct btrfs_block_group, cache_node);
+
+	if (cmp1->start < cmp2->start)
+		return -1;
+	if (cmp1->start > cmp2->start)
+		return 1;
+	return 0;
+}
+
 /*
  * This adds the block group to the fs_info rb tree for the block group cache
  */
 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
 				       struct btrfs_block_group *block_group)
 {
-	struct rb_node **p;
-	struct rb_node *parent = NULL;
-	struct btrfs_block_group *cache;
-	bool leftmost = true;
+	struct rb_node *exist;
 
 	ASSERT(block_group->length != 0);
 
 	write_lock(&info->block_group_cache_lock);
-	p = &info->block_group_cache_tree.rb_root.rb_node;
-
-	while (*p) {
-		parent = *p;
-		cache = rb_entry(parent, struct btrfs_block_group, cache_node);
-		if (block_group->start < cache->start) {
-			p = &(*p)->rb_left;
-		} else if (block_group->start > cache->start) {
-			p = &(*p)->rb_right;
-			leftmost = false;
-		} else {
-			write_unlock(&info->block_group_cache_lock);
-			return -EEXIST;
-		}
-	}
-
-	rb_link_node(&block_group->cache_node, parent, p);
-	rb_insert_color_cached(&block_group->cache_node,
-			       &info->block_group_cache_tree, leftmost);
 
+	exist = rb_find_add_cached(&block_group->cache_node,
+			&info->block_group_cache_tree, btrfs_bg_start_cmp);
+	if (exist)
+		write_unlock(&info->block_group_cache_lock);
+		return -EEXIST;
 	write_unlock(&info->block_group_cache_lock);
 
 	return 0;