diff mbox series

btrfs: allocate dummy ordereded_sums objects for nocsum I/O on zoned file systems

Message ID 20230608121410.275766-2-hch@lst.de (mailing list archive)
State New, archived
Headers show
Series btrfs: allocate dummy ordereded_sums objects for nocsum I/O on zoned file systems | expand

Commit Message

Christoph Hellwig June 8, 2023, 12:14 p.m. UTC
Zoned file systems now need the ordereded_sums structure to record the
actual write location returned by zone append, so allocate dummy
structures without the csum array for them when the I/O doesn't use
checksums, and free them when completing the ordered_extent.

Fixes: 177b0eb2c180 ("btrfs: optimize the logical to physical mapping for zoned writes")
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 fs/btrfs/bio.c       |  4 ++++
 fs/btrfs/file-item.c | 16 ++++++++++++++++
 fs/btrfs/file-item.h |  1 +
 fs/btrfs/zoned.c     | 21 +++++++++++++++++++--
 4 files changed, 40 insertions(+), 2 deletions(-)

Comments

David Sterba June 8, 2023, 3:40 p.m. UTC | #1
On Thu, Jun 08, 2023 at 02:14:10PM +0200, Christoph Hellwig wrote:
> Zoned file systems now need the ordereded_sums structure to record the
> actual write location returned by zone append, so allocate dummy
> structures without the csum array for them when the I/O doesn't use
> checksums, and free them when completing the ordered_extent.
> 
> Fixes: 177b0eb2c180 ("btrfs: optimize the logical to physical mapping for zoned writes")

This patch is still in the devlopment queue so I don't want to do a
separate fix. Please send an incremental update that cleanly applies to
the patch.

There's a minor conflict in context of btrfs_finish_ordered_zoned in
zoned.c which only sets up the fs_info, so trivial to fix but the new
helper btrfs_alloc_dummy_sum() uses bbio->ordered which is not available
at this time and was added in a different series ("btrfs: add an
ordered_extent pointer to struct btrfs_bio").

Due to that there may be a cascading change needed in other patches in
misc-next but that should be fixable, the logic of adding bbio::ordered
is clear.

> --- a/fs/btrfs/file-item.c
> +++ b/fs/btrfs/file-item.c
> @@ -773,6 +773,22 @@ blk_status_t btrfs_csum_one_bio(struct btrfs_bio *bbio)
>  	return 0;
>  }
>  
> +/*
> + * Nodatasum I/O on zoned file systems still requires an btrfs_ordered_sum to
> + * record the updated logical address on Zone Append completion.
> + * Allocate just the structure with an empty sums array here for that case.
> + */
> +blk_status_t btrfs_alloc_dummy_sum(struct btrfs_bio *bbio)
> +{
> +	bbio->sums = kmalloc(sizeof(*bbio->sums), GFP_NOFS);
> +	if (!bbio->sums)
> +		return BLK_STS_RESOURCE;
> +	bbio->sums->len = bbio->bio.bi_iter.bi_size;
> +	bbio->sums->logical = bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT;
> +	btrfs_add_ordered_sum(bbio->ordered, bbio->sums);

bbio->ordered not available

> +	return 0;
> +}
> +
>  /*
>   * Remove one checksum overlapping a range.
>   *
> diff --git a/fs/btrfs/file-item.h b/fs/btrfs/file-item.h
> index 6be8725cd57474..4ec669b690080a 100644
> --- a/fs/btrfs/file-item.h
> +++ b/fs/btrfs/file-item.h
> @@ -50,6 +50,7 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
>  			   struct btrfs_root *root,
>  			   struct btrfs_ordered_sum *sums);
>  blk_status_t btrfs_csum_one_bio(struct btrfs_bio *bbio);
> +blk_status_t btrfs_alloc_dummy_sum(struct btrfs_bio *bbio);
>  int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
>  			     struct list_head *list, int search_commit,
>  			     bool nowait);
> diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
> index bbde4ddd475492..637b2a2f45c94e 100644
> --- a/fs/btrfs/zoned.c
> +++ b/fs/btrfs/zoned.c
> @@ -1702,7 +1702,8 @@ static bool btrfs_zoned_split_ordered(struct btrfs_ordered_extent *ordered,
>  
>  void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered)
>  {
> -	struct btrfs_fs_info *fs_info = btrfs_sb(ordered->inode->i_sb);
> +	struct btrfs_inode *inode = BTRFS_I(ordered->inode);
> +	struct btrfs_fs_info *fs_info = inode->root->fs_info;

Minor conflict here, function name is btrfs_rewrite_logical_zoned() and
inode/fs_info are obtained from extenta map tree.

>  	struct btrfs_ordered_sum *sum =
>  		list_first_entry(&ordered->list, typeof(*sum), list);
>  	u64 logical = sum->logical;
> @@ -1717,7 +1718,7 @@ void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered)
>  		if (!btrfs_zoned_split_ordered(ordered, logical, len)) {
>  			set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
>  			btrfs_err(fs_info, "failed to split ordered extent\n");
> -			return;
> +			goto out;
>  		}
>  		logical = sum->logical;
>  		len = sum->len;
> @@ -1725,6 +1726,22 @@ void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered)
>  
>  	if (ordered->disk_bytenr != logical)
>  		btrfs_rewrite_logical_zoned(ordered, logical);
> +
> +out:
> +	/*
> +	 * If we end up here for nodatasum I/O, the btrfs_ordered_sum structures
> +	 * were allocated by btrfs_alloc_dummy_sum only to record the logical
> +	 * addresses and don't contain actual checksums.  We thus must free them
> +	 * here so that we don't attempt to log the csums later.
> +	 */
> +	if ((inode->flags & BTRFS_INODE_NODATASUM) ||
> +	    test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state)) {
> +		while ((sum = list_first_entry_or_null(&ordered->list,
> +						       typeof(*sum), list))) {
> +			list_del(&sum->list);
> +			kfree(sum);
> +		}
> +	}
>  }
Christoph Hellwig June 9, 2023, 4:55 a.m. UTC | #2
On Thu, Jun 08, 2023 at 05:40:15PM +0200, David Sterba wrote:
> This patch is still in the devlopment queue so I don't want to do a
> separate fix. Please send an incremental update that cleanly applies to
> the patch.
> 
> There's a minor conflict in context of btrfs_finish_ordered_zoned in
> zoned.c which only sets up the fs_info, so trivial to fix but the new
> helper btrfs_alloc_dummy_sum() uses bbio->ordered which is not available
> at this time and was added in a different series ("btrfs: add an
> ordered_extent pointer to struct btrfs_bio").
> 
> Due to that there may be a cascading change needed in other patches in
> misc-next but that should be fixable, the logic of adding bbio::ordered
> is clear.

Ok.  Here is a stash of patches:

 1) incremental diff
 2) complete replacement for that commit with the incremental
    diff included

And then new version of the two later patches affected by the squashing:

 3) btrfs: defer splitting of ordered extents until I/O completion
 4) btrfs: add an ordered_extent pointer to struct btrfs_bio

A git tree with all this is also available here:

    git://git.infradead.org/users/hch/misc.git btrfs-zoned-fixes

Gitweb:

    http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/btrfs-zoned-fixes

Note that this is based of misc-next as of last night CET, but misc-next
has been rebased since then.
David Sterba June 9, 2023, 7:18 p.m. UTC | #3
On Fri, Jun 09, 2023 at 06:55:16AM +0200, Christoph Hellwig wrote:
> On Thu, Jun 08, 2023 at 05:40:15PM +0200, David Sterba wrote:
> > This patch is still in the devlopment queue so I don't want to do a
> > separate fix. Please send an incremental update that cleanly applies to
> > the patch.
> > 
> > There's a minor conflict in context of btrfs_finish_ordered_zoned in
> > zoned.c which only sets up the fs_info, so trivial to fix but the new
> > helper btrfs_alloc_dummy_sum() uses bbio->ordered which is not available
> > at this time and was added in a different series ("btrfs: add an
> > ordered_extent pointer to struct btrfs_bio").
> > 
> > Due to that there may be a cascading change needed in other patches in
> > misc-next but that should be fixable, the logic of adding bbio::ordered
> > is clear.
> 
> Ok.  Here is a stash of patches:
> 
>  1) incremental diff
>  2) complete replacement for that commit with the incremental
>     diff included
> 
> And then new version of the two later patches affected by the squashing:
> 
>  3) btrfs: defer splitting of ordered extents until I/O completion
>  4) btrfs: add an ordered_extent pointer to struct btrfs_bio
> 
> A git tree with all this is also available here:
> 
>     git://git.infradead.org/users/hch/misc.git btrfs-zoned-fixes

Thanks. I used the patches in the mail, I can't pull from infradead
(it.infradead.org[0: ...]: errno=Connection refused) so I at least
compared the committed versions against the patches. Misc-next updated
and pushed but I haven't tested it yet.
diff mbox series

Patch

diff --git a/fs/btrfs/bio.c b/fs/btrfs/bio.c
index 2ca2d1fcdf2b9a..12b12443efaabb 100644
--- a/fs/btrfs/bio.c
+++ b/fs/btrfs/bio.c
@@ -705,6 +705,10 @@  static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num)
 			ret = btrfs_bio_csum(bbio);
 			if (ret)
 				goto fail_put_bio;
+		} else if (use_append) {
+			ret = btrfs_alloc_dummy_sum(bbio);
+			if (ret)
+				goto fail_put_bio;
 		}
 	}
 
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index 2db90c3bfd95a9..696bf695d8eb00 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -773,6 +773,22 @@  blk_status_t btrfs_csum_one_bio(struct btrfs_bio *bbio)
 	return 0;
 }
 
+/*
+ * Nodatasum I/O on zoned file systems still requires an btrfs_ordered_sum to
+ * record the updated logical address on Zone Append completion.
+ * Allocate just the structure with an empty sums array here for that case.
+ */
+blk_status_t btrfs_alloc_dummy_sum(struct btrfs_bio *bbio)
+{
+	bbio->sums = kmalloc(sizeof(*bbio->sums), GFP_NOFS);
+	if (!bbio->sums)
+		return BLK_STS_RESOURCE;
+	bbio->sums->len = bbio->bio.bi_iter.bi_size;
+	bbio->sums->logical = bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT;
+	btrfs_add_ordered_sum(bbio->ordered, bbio->sums);
+	return 0;
+}
+
 /*
  * Remove one checksum overlapping a range.
  *
diff --git a/fs/btrfs/file-item.h b/fs/btrfs/file-item.h
index 6be8725cd57474..4ec669b690080a 100644
--- a/fs/btrfs/file-item.h
+++ b/fs/btrfs/file-item.h
@@ -50,6 +50,7 @@  int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
 			   struct btrfs_root *root,
 			   struct btrfs_ordered_sum *sums);
 blk_status_t btrfs_csum_one_bio(struct btrfs_bio *bbio);
+blk_status_t btrfs_alloc_dummy_sum(struct btrfs_bio *bbio);
 int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
 			     struct list_head *list, int search_commit,
 			     bool nowait);
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index bbde4ddd475492..637b2a2f45c94e 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -1702,7 +1702,8 @@  static bool btrfs_zoned_split_ordered(struct btrfs_ordered_extent *ordered,
 
 void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered)
 {
-	struct btrfs_fs_info *fs_info = btrfs_sb(ordered->inode->i_sb);
+	struct btrfs_inode *inode = BTRFS_I(ordered->inode);
+	struct btrfs_fs_info *fs_info = inode->root->fs_info;
 	struct btrfs_ordered_sum *sum =
 		list_first_entry(&ordered->list, typeof(*sum), list);
 	u64 logical = sum->logical;
@@ -1717,7 +1718,7 @@  void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered)
 		if (!btrfs_zoned_split_ordered(ordered, logical, len)) {
 			set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
 			btrfs_err(fs_info, "failed to split ordered extent\n");
-			return;
+			goto out;
 		}
 		logical = sum->logical;
 		len = sum->len;
@@ -1725,6 +1726,22 @@  void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered)
 
 	if (ordered->disk_bytenr != logical)
 		btrfs_rewrite_logical_zoned(ordered, logical);
+
+out:
+	/*
+	 * If we end up here for nodatasum I/O, the btrfs_ordered_sum structures
+	 * were allocated by btrfs_alloc_dummy_sum only to record the logical
+	 * addresses and don't contain actual checksums.  We thus must free them
+	 * here so that we don't attempt to log the csums later.
+	 */
+	if ((inode->flags & BTRFS_INODE_NODATASUM) ||
+	    test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state)) {
+		while ((sum = list_first_entry_or_null(&ordered->list,
+						       typeof(*sum), list))) {
+			list_del(&sum->list);
+			kfree(sum);
+		}
+	}
 }
 
 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,