diff mbox series

[19/42] btrfs: set max_extent_size properly

Message ID 20181011195431.3441-20-josef@toxicpanda.com (mailing list archive)
State New, archived
Headers show
Series My current patch queue | expand

Commit Message

Josef Bacik Oct. 11, 2018, 7:54 p.m. UTC
From: Josef Bacik <jbacik@fb.com>

We can't use entry->bytes if our entry is a bitmap entry, we need to use
entry->max_extent_size in that case.  Fix up all the logic to make this
consistent.

Signed-off-by: Josef Bacik <jbacik@fb.com>
---
 fs/btrfs/free-space-cache.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)

Comments

Filipe Manana Oct. 12, 2018, 8:50 a.m. UTC | #1
On Thu, Oct 11, 2018 at 8:57 PM Josef Bacik <josef@toxicpanda.com> wrote:
>
> From: Josef Bacik <jbacik@fb.com>
>
> We can't use entry->bytes if our entry is a bitmap entry, we need to use
> entry->max_extent_size in that case.  Fix up all the logic to make this
> consistent.
>
> Signed-off-by: Josef Bacik <jbacik@fb.com>
Reviewed-by: Filipe Manana <fdmanana@suse.com>

> ---
>  fs/btrfs/free-space-cache.c | 29 +++++++++++++++++++----------
>  1 file changed, 19 insertions(+), 10 deletions(-)
>
> diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
> index e077ad3b4549..2e96ee7da3ec 100644
> --- a/fs/btrfs/free-space-cache.c
> +++ b/fs/btrfs/free-space-cache.c
> @@ -1770,6 +1770,18 @@ static int search_bitmap(struct btrfs_free_space_ctl *ctl,
>         return -1;
>  }
>
> +static void set_max_extent_size(struct btrfs_free_space *entry,
> +                               u64 *max_extent_size)
> +{
> +       if (entry->bitmap) {
> +               if (entry->max_extent_size > *max_extent_size)
> +                       *max_extent_size = entry->max_extent_size;
> +       } else {
> +               if (entry->bytes > *max_extent_size)
> +                       *max_extent_size = entry->bytes;
> +       }
> +}
> +
>  /* Cache the size of the max extent in bytes */
>  static struct btrfs_free_space *
>  find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
> @@ -1791,8 +1803,7 @@ find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
>         for (node = &entry->offset_index; node; node = rb_next(node)) {
>                 entry = rb_entry(node, struct btrfs_free_space, offset_index);
>                 if (entry->bytes < *bytes) {
> -                       if (entry->bytes > *max_extent_size)
> -                               *max_extent_size = entry->bytes;
> +                       set_max_extent_size(entry, max_extent_size);
>                         continue;
>                 }
>
> @@ -1810,8 +1821,7 @@ find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
>                 }
>
>                 if (entry->bytes < *bytes + align_off) {
> -                       if (entry->bytes > *max_extent_size)
> -                               *max_extent_size = entry->bytes;
> +                       set_max_extent_size(entry, max_extent_size);
>                         continue;
>                 }
>
> @@ -1823,8 +1833,8 @@ find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
>                                 *offset = tmp;
>                                 *bytes = size;
>                                 return entry;
> -                       } else if (size > *max_extent_size) {
> -                               *max_extent_size = size;
> +                       } else {
> +                               set_max_extent_size(entry, max_extent_size);
>                         }
>                         continue;
>                 }
> @@ -2684,8 +2694,7 @@ static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
>
>         err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
>         if (err) {
> -               if (search_bytes > *max_extent_size)
> -                       *max_extent_size = search_bytes;
> +               set_max_extent_size(entry, max_extent_size);
>                 return 0;
>         }
>
> @@ -2722,8 +2731,8 @@ u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
>
>         entry = rb_entry(node, struct btrfs_free_space, offset_index);
>         while (1) {
> -               if (entry->bytes < bytes && entry->bytes > *max_extent_size)
> -                       *max_extent_size = entry->bytes;
> +               if (entry->bytes < bytes)
> +                       set_max_extent_size(entry, max_extent_size);
>
>                 if (entry->bytes < bytes ||
>                     (!entry->bitmap && entry->offset < min_start)) {
> --
> 2.14.3
>
David Sterba Oct. 12, 2018, 5:30 p.m. UTC | #2
On Thu, Oct 11, 2018 at 03:54:08PM -0400, Josef Bacik wrote:
> From: Josef Bacik <jbacik@fb.com>
> 
> We can't use entry->bytes if our entry is a bitmap entry, we need to use
> entry->max_extent_size in that case.  Fix up all the logic to make this
> consistent.
> 
> Signed-off-by: Josef Bacik <jbacik@fb.com>
> ---
>  fs/btrfs/free-space-cache.c | 29 +++++++++++++++++++----------
>  1 file changed, 19 insertions(+), 10 deletions(-)
> 
> diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
> index e077ad3b4549..2e96ee7da3ec 100644
> --- a/fs/btrfs/free-space-cache.c
> +++ b/fs/btrfs/free-space-cache.c
> @@ -1770,6 +1770,18 @@ static int search_bitmap(struct btrfs_free_space_ctl *ctl,
>  	return -1;
>  }
>  
> +static void set_max_extent_size(struct btrfs_free_space *entry,

I find this a bit confusing, in the code it's like

	set_max_extent_size(entry, max_extent_size)

so it reads like 'set max extent size of the entry to value', while the
function does the opposite. That the 2nd parameter is a pointer does not
help either.  A function comment explaining that would not help as it's
confusing at the callsites.

	*max_extent_size = get_max_extent_size(entry, *max_extent_size)

This is more obvious what it does but can't be called an improvement
regarding readability. Other ideas?
diff mbox series

Patch

diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index e077ad3b4549..2e96ee7da3ec 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -1770,6 +1770,18 @@  static int search_bitmap(struct btrfs_free_space_ctl *ctl,
 	return -1;
 }
 
+static void set_max_extent_size(struct btrfs_free_space *entry,
+				u64 *max_extent_size)
+{
+	if (entry->bitmap) {
+		if (entry->max_extent_size > *max_extent_size)
+			*max_extent_size = entry->max_extent_size;
+	} else {
+		if (entry->bytes > *max_extent_size)
+			*max_extent_size = entry->bytes;
+	}
+}
+
 /* Cache the size of the max extent in bytes */
 static struct btrfs_free_space *
 find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
@@ -1791,8 +1803,7 @@  find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
 	for (node = &entry->offset_index; node; node = rb_next(node)) {
 		entry = rb_entry(node, struct btrfs_free_space, offset_index);
 		if (entry->bytes < *bytes) {
-			if (entry->bytes > *max_extent_size)
-				*max_extent_size = entry->bytes;
+			set_max_extent_size(entry, max_extent_size);
 			continue;
 		}
 
@@ -1810,8 +1821,7 @@  find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
 		}
 
 		if (entry->bytes < *bytes + align_off) {
-			if (entry->bytes > *max_extent_size)
-				*max_extent_size = entry->bytes;
+			set_max_extent_size(entry, max_extent_size);
 			continue;
 		}
 
@@ -1823,8 +1833,8 @@  find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
 				*offset = tmp;
 				*bytes = size;
 				return entry;
-			} else if (size > *max_extent_size) {
-				*max_extent_size = size;
+			} else {
+				set_max_extent_size(entry, max_extent_size);
 			}
 			continue;
 		}
@@ -2684,8 +2694,7 @@  static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
 
 	err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
 	if (err) {
-		if (search_bytes > *max_extent_size)
-			*max_extent_size = search_bytes;
+		set_max_extent_size(entry, max_extent_size);
 		return 0;
 	}
 
@@ -2722,8 +2731,8 @@  u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
 
 	entry = rb_entry(node, struct btrfs_free_space, offset_index);
 	while (1) {
-		if (entry->bytes < bytes && entry->bytes > *max_extent_size)
-			*max_extent_size = entry->bytes;
+		if (entry->bytes < bytes)
+			set_max_extent_size(entry, max_extent_size);
 
 		if (entry->bytes < bytes ||
 		    (!entry->bitmap && entry->offset < min_start)) {