btrfs: discard before adding to the free space cache
diff mbox series

Message ID 20191209193846.18162-1-dennis@kernel.org
State New
Headers show
Series
  • btrfs: discard before adding to the free space cache
Related show

Commit Message

Dennis Zhou Dec. 9, 2019, 7:38 p.m. UTC
Returning free space to the free space cache lets it immediately be
reused. So discard before returning the free space otherwise we can race
here.

Fixes: 55e734b728c0 ("btrfs: Don't discard unwritten extents")
Cc: Nikolay Borisov <nborisov@suse.com>
Cc: Omar Sandoval <osandov@osandov.com>
Signed-off-by: Dennis Zhou <dennis@kernel.org>
---
 fs/btrfs/inode.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Nikolay Borisov Dec. 9, 2019, 8:01 p.m. UTC | #1
On 9.12.19 г. 21:38 ч., Dennis Zhou wrote:
> Returning free space to the free space cache lets it immediately be
> reused. So discard before returning the free space otherwise we can race
> here.
> 
> Fixes: 55e734b728c0 ("btrfs: Don't discard unwritten extents")
> Cc: Nikolay Borisov <nborisov@suse.com>
> Cc: Omar Sandoval <osandov@osandov.com>
> Signed-off-by: Dennis Zhou <dennis@kernel.org>

Fair point.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>


> ---
>  fs/btrfs/inode.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index 625439875299..1ab13943cdf0 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -3250,13 +3250,14 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
>  		    clear_reserved_extent &&
>  		    !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
>  		    !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
> -			btrfs_free_reserved_extent(fs_info,
> -						   ordered_extent->start,
> -						   ordered_extent->disk_len, 1);
>  			if (ret && btrfs_test_opt(fs_info, DISCARD))
>  				btrfs_discard_extent(fs_info,
>  						ordered_extent->start,
>  						ordered_extent->disk_len, NULL);
> +
> +			btrfs_free_reserved_extent(fs_info,
> +						   ordered_extent->start,
> +						   ordered_extent->disk_len, 1);
>  		}
>  	}
>  
>
Johannes Thumshirn Dec. 11, 2019, 8:58 a.m. UTC | #2
Looks good,
Reviewed-by: Johannes Thumshirn <jthumshirn@suse.de>
David Sterba Jan. 2, 2020, 2:44 p.m. UTC | #3
On Mon, Dec 09, 2019 at 11:38:46AM -0800, Dennis Zhou wrote:
> Returning free space to the free space cache lets it immediately be
> reused. So discard before returning the free space otherwise we can race
> here.
> 
> Fixes: 55e734b728c0 ("btrfs: Don't discard unwritten extents")

Unstable commit id as the patch is still in misc-next, so I'm going to
fold the fix to the original patch. Thanks.

Patch
diff mbox series

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 625439875299..1ab13943cdf0 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -3250,13 +3250,14 @@  static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
 		    clear_reserved_extent &&
 		    !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
 		    !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
-			btrfs_free_reserved_extent(fs_info,
-						   ordered_extent->start,
-						   ordered_extent->disk_len, 1);
 			if (ret && btrfs_test_opt(fs_info, DISCARD))
 				btrfs_discard_extent(fs_info,
 						ordered_extent->start,
 						ordered_extent->disk_len, NULL);
+
+			btrfs_free_reserved_extent(fs_info,
+						   ordered_extent->start,
+						   ordered_extent->disk_len, 1);
 		}
 	}