diff mbox series

[06/11] btrfs: simplify btrfs_split_ordered_extent

Message ID 20230324023207.544800-7-hch@lst.de (mailing list archive)
State New, archived
Headers show
Series [01/11] btrfs: add function to create and return an ordered extent | expand

Commit Message

Christoph Hellwig March 24, 2023, 2:32 a.m. UTC
btrfs_split_ordered_extent is only ever asked to split out the beginning
of an ordered_extent.  Change it to only take a len to split out, and
switch it to allocate the new extent for the beginning, as that helps
with callers that want to keep a pointer to the ordered_extent that
it is stealing from.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 fs/btrfs/inode.c        |  8 +-------
 fs/btrfs/ordered-data.c | 28 ++++++++++++----------------
 fs/btrfs/ordered-data.h |  3 +--
 3 files changed, 14 insertions(+), 25 deletions(-)

Comments

Naohiro Aota March 24, 2023, 7:52 a.m. UTC | #1
On Fri, Mar 24, 2023 at 10:32:02AM +0800, Christoph Hellwig wrote:
> btrfs_split_ordered_extent is only ever asked to split out the beginning
> of an ordered_extent.  Change it to only take a len to split out, and
> switch it to allocate the new extent for the beginning, as that helps
> with callers that want to keep a pointer to the ordered_extent that
> it is stealing from.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
>  fs/btrfs/inode.c        |  8 +-------
>  fs/btrfs/ordered-data.c | 28 ++++++++++++----------------
>  fs/btrfs/ordered-data.h |  3 +--
>  3 files changed, 14 insertions(+), 25 deletions(-)
> 
> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index 2cbc6c316effc1..bff23bac85f2ef 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -2646,17 +2646,11 @@ blk_status_t btrfs_extract_ordered_extent(struct btrfs_bio *bbio)
>  		goto out;
>  	}
>  
> -	/* The bio must be entirely covered by the ordered extent */
> -	if (WARN_ON_ONCE(len > ordered_len)) {
> -		ret = -EINVAL;
> -		goto out;
> -	}
> -
>  	/* No need to split if the ordered extent covers the entire bio */
>  	if (ordered->disk_num_bytes == len)
>  		goto out;
>  
> -	ret = btrfs_split_ordered_extent(ordered, len, 0);
> +	ret = btrfs_split_ordered_extent(ordered, len);
>  	if (ret)
>  		goto out;
>  	ret = split_zoned_em(inode, bbio->file_offset, ordered_len, len, 0);
> diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
> index 4b46406c0c8af5..1d5971b6e68c66 100644
> --- a/fs/btrfs/ordered-data.c
> +++ b/fs/btrfs/ordered-data.c
> @@ -1138,17 +1138,19 @@ static int clone_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pos,
>  					ordered->compress_type);
>  }
>  
> -int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pre,
> -				u64 post)
> +/* split out a new ordered extent for this first @len bytes of @ordered */
> +int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 len)
>  {
>  	struct inode *inode = ordered->inode;
>  	struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
> -	struct rb_node *node;
>  	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
> -	int ret = 0;
> +	struct rb_node *node;
>  
>  	trace_btrfs_ordered_extent_split(BTRFS_I(inode), ordered);
>  
> +	/* The bio must be entirely covered by the ordered extent */
> +	if (WARN_ON_ONCE(len > ordered->num_bytes))
> +		return -EINVAL;

Can we also reject "len == ordered->num_bytes" case here? So, we will never
modify the ordered extent to have
ordered->{num_bytes,disk_num_bytes,bytes_left} == 0.

Either way,

Reviewed-by: Naohiro Aota <naohiro.aota@wdc.com>

>  	/* We cannot split once end_bio'd ordered extent */
>  	if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes))
>  		return -EINVAL;
> @@ -1167,11 +1169,11 @@ int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pre,
>  	if (tree->last == node)
>  		tree->last = NULL;
>  
> -	ordered->file_offset += pre;
> -	ordered->disk_bytenr += pre;
> -	ordered->num_bytes -= (pre + post);
> -	ordered->disk_num_bytes -= (pre + post);
> -	ordered->bytes_left -= (pre + post);
> +	ordered->file_offset += len;
> +	ordered->disk_bytenr += len;
> +	ordered->num_bytes -= len;
> +	ordered->disk_num_bytes -= len;
> +	ordered->bytes_left -= len;
>  
>  	/* Re-insert the node */
>  	node = tree_insert(&tree->tree, ordered->file_offset, &ordered->rb_node);
> @@ -1182,13 +1184,7 @@ int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pre,
>  
>  	spin_unlock_irq(&tree->lock);
>  
> -	if (pre)
> -		ret = clone_ordered_extent(ordered, 0, pre);
> -	if (ret == 0 && post)
> -		ret = clone_ordered_extent(ordered, pre + ordered->disk_num_bytes,
> -					   post);
> -
> -	return ret;
> +	return clone_ordered_extent(ordered, 0, len);
>  }
>  
>  int __init ordered_data_init(void)
> diff --git a/fs/btrfs/ordered-data.h b/fs/btrfs/ordered-data.h
> index 18007f9c00add8..f0f1138d23c331 100644
> --- a/fs/btrfs/ordered-data.h
> +++ b/fs/btrfs/ordered-data.h
> @@ -212,8 +212,7 @@ void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
>  					struct extent_state **cached_state);
>  bool btrfs_try_lock_ordered_range(struct btrfs_inode *inode, u64 start, u64 end,
>  				  struct extent_state **cached_state);
> -int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pre,
> -			       u64 post);
> +int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 len);
>  int __init ordered_data_init(void);
>  void __cold ordered_data_exit(void);
>  
> -- 
> 2.39.2
>
Christoph Hellwig March 25, 2023, 8:37 a.m. UTC | #2
On Fri, Mar 24, 2023 at 07:52:49AM +0000, Naohiro Aota wrote:
> > +	/* The bio must be entirely covered by the ordered extent */
> > +	if (WARN_ON_ONCE(len > ordered->num_bytes))
> > +		return -EINVAL;
> 
> Can we also reject "len == ordered->num_bytes" case here? So, we will never
> modify the ordered extent to have
> ordered->{num_bytes,disk_num_bytes,bytes_left} == 0.

Sure, I've replaced the > with a >= and expanded the comment.
diff mbox series

Patch

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 2cbc6c316effc1..bff23bac85f2ef 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -2646,17 +2646,11 @@  blk_status_t btrfs_extract_ordered_extent(struct btrfs_bio *bbio)
 		goto out;
 	}
 
-	/* The bio must be entirely covered by the ordered extent */
-	if (WARN_ON_ONCE(len > ordered_len)) {
-		ret = -EINVAL;
-		goto out;
-	}
-
 	/* No need to split if the ordered extent covers the entire bio */
 	if (ordered->disk_num_bytes == len)
 		goto out;
 
-	ret = btrfs_split_ordered_extent(ordered, len, 0);
+	ret = btrfs_split_ordered_extent(ordered, len);
 	if (ret)
 		goto out;
 	ret = split_zoned_em(inode, bbio->file_offset, ordered_len, len, 0);
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index 4b46406c0c8af5..1d5971b6e68c66 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -1138,17 +1138,19 @@  static int clone_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pos,
 					ordered->compress_type);
 }
 
-int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pre,
-				u64 post)
+/* split out a new ordered extent for this first @len bytes of @ordered */
+int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 len)
 {
 	struct inode *inode = ordered->inode;
 	struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
-	struct rb_node *node;
 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
-	int ret = 0;
+	struct rb_node *node;
 
 	trace_btrfs_ordered_extent_split(BTRFS_I(inode), ordered);
 
+	/* The bio must be entirely covered by the ordered extent */
+	if (WARN_ON_ONCE(len > ordered->num_bytes))
+		return -EINVAL;
 	/* We cannot split once end_bio'd ordered extent */
 	if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes))
 		return -EINVAL;
@@ -1167,11 +1169,11 @@  int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pre,
 	if (tree->last == node)
 		tree->last = NULL;
 
-	ordered->file_offset += pre;
-	ordered->disk_bytenr += pre;
-	ordered->num_bytes -= (pre + post);
-	ordered->disk_num_bytes -= (pre + post);
-	ordered->bytes_left -= (pre + post);
+	ordered->file_offset += len;
+	ordered->disk_bytenr += len;
+	ordered->num_bytes -= len;
+	ordered->disk_num_bytes -= len;
+	ordered->bytes_left -= len;
 
 	/* Re-insert the node */
 	node = tree_insert(&tree->tree, ordered->file_offset, &ordered->rb_node);
@@ -1182,13 +1184,7 @@  int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pre,
 
 	spin_unlock_irq(&tree->lock);
 
-	if (pre)
-		ret = clone_ordered_extent(ordered, 0, pre);
-	if (ret == 0 && post)
-		ret = clone_ordered_extent(ordered, pre + ordered->disk_num_bytes,
-					   post);
-
-	return ret;
+	return clone_ordered_extent(ordered, 0, len);
 }
 
 int __init ordered_data_init(void)
diff --git a/fs/btrfs/ordered-data.h b/fs/btrfs/ordered-data.h
index 18007f9c00add8..f0f1138d23c331 100644
--- a/fs/btrfs/ordered-data.h
+++ b/fs/btrfs/ordered-data.h
@@ -212,8 +212,7 @@  void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,
 					struct extent_state **cached_state);
 bool btrfs_try_lock_ordered_range(struct btrfs_inode *inode, u64 start, u64 end,
 				  struct extent_state **cached_state);
-int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 pre,
-			       u64 post);
+int btrfs_split_ordered_extent(struct btrfs_ordered_extent *ordered, u64 len);
 int __init ordered_data_init(void);
 void __cold ordered_data_exit(void);