fix nodatasum I/O for zone devices v2
Hi all,
my recent series to optimize the ordered_extent splitting for zoned devices broke nodatasum I/O - this was hidden by the fact that a normal xfstests run appears to only exercise nodatasum in combination with dev-replace, which was also broken..
Changes since v1:
- drop the btrfs_inode_is_nodatasum helper
- add a new helper to just allocate a dummy ordered_sum structure
- more comments
PATCH 1/1 btrfs: allocate dummy ordereded_sums objects for nocsum I/O on zoned file systems
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.
--- 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(-)
fs/btrfs/bio.c +4 -0
@@ -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; } }
fs/btrfs/file-item.c +16 -0
@@ -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. *
fs/btrfs/file-item.h +1 -0
@@ -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);
fs/btrfs/zoned.c +19 -2
@@ -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,
Signature · 1 line
2.39.2
Re: [PATCH] btrfs: allocate dummy ordereded_sums objects for nocsum I/O on zoned file systems
Quoted 6 lines · Christoph Hellwig
Zoned file systems now need the
ordereded_sumsstructure 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 theordered_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.
Quoted 9 lines
fs/btrfs/file-item.c +3
-0@@ -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.
fs/btrfs/file-item.c blk_status_t btrfs_csum_one_bio(struct btrfs_bio *bbio) · line 788
Quoted 10 lines
fs/btrfs/file-item.c +10
-0+* 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
Quoted 23 lines
fs/btrfs/file-item.c +3
-0+return 0 ; +} +/* * Remove one checksum overlapping a range. * fs/btrfs/file-item.h +1
-0@@ -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);fs/btrfs/zoned.c +0
-0
fs/btrfs/zoned.c static bool btrfs_zoned_split_ordered(struct btrfs_ordered_extent *ordered, · line 1706
Quoted 6 lines
fs/btrfs/zoned.c +2
-1@@ -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.
Quoted 33 lines
fs/btrfs/zoned.c +17
-1struct 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
Reviewed-by Johannes Thumshirn Reviewed-by David Sterba Reviewed-by Johannes Thumshirn Reviewed-by Johannes Thumshirn Reviewed-by Josef Bacik Reviewed-by David SterbaRe: [PATCH] btrfs: allocate dummy ordereded_sums objects for nocsum I/O on zoned file systems
Quoted 10 lines · David Sterba
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_zonedinzoned.cwhich only sets up thefs_info, so trivial to fix but the new helperbtrfs_alloc_dummy_sum()usesbbio->orderedwhich is not available at this time and was added in a different series ("btrfs: add anordered_extentpointer to structbtrfs_bio").
Quoted 3 lines
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::orderedis 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.
fs/btrfs/bio.c +4 -0
@@ -686,6 +686,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; } }
fs/btrfs/file-item.c +23 -0
@@ -830,6 +830,29 @@ 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) +{ +struct btrfs_ordered_extent *ordered = +btrfs_lookup_ordered_extent (bbio->inode, bbio->file_offset); + +if (WARN_ON_ONCE (!ordered)) +return BLK_STS_IOERR ; + + 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 (ordered, bbio->sums); +btrfs_put_ordered_extent (ordered); +return 0 ; +} +/* * Remove one checksum overlapping a range. *
fs/btrfs/file-item.h +1 -0
@@ -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);
fs/btrfs/zoned.c +16 -2
@@ -1667,14 +1667,15 @@ void btrfs_record_physical_zoned(struct btrfs_bio *bbio)void btrfs_rewrite_logical_zoned (struct btrfs_ordered_extent *ordered) { -struct extent_map_tree *em_tree = &BTRFS_I (ordered->inode)->extent_tree; +struct btrfs_inode *inode =BTRFS_I (ordered->inode); +struct extent_map_tree *em_tree = &inode->extent_tree;struct extent_map *em;struct btrfs_ordered_sum *sum =list_first_entry (&ordered->list,typeof (*sum), list);u64 logical = sum->logical;if (ordered->disk_bytenr == logical) -return ; + goto out; ordered->disk_bytenr = logical;
@@ -1684,6 +1685,19 @@ void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered) em->block_start = logical;free_extent_map (em);write_unlock (&em_tree->lock); + +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 , &inode->root->fs_info->fs_state)) { +list_del (&sum->list); +kfree (sum); + } } bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
From 9457d75e22b7020846bfbd15f805892cee99f027 Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Wed, 24 May 2023 17:03:08 +0200
Subject: btrfs: optimize the logical to physical mapping for zoned writes
The current code to store the final logical to physical mapping for a zone append write in the extent tree is rather inefficient. It first has to split the ordered extent so that there is one ordered extent per bio, so that it can look up the ordered extent on I/O completion in btrfs_record_physical_zoned and store the physical LBA returned by the block driver in the ordered extent.
btrfs_rewrite_logical_zoned then has to do a lookup in the chunk tree to see what physical address the logical address for this bio / ordered extent is mapped to, and then rewrite it in the extent tree.
To optimize this process, we can store the physical address assigned in the chunk tree to the original logical address and a pointer to btrfs_ordered_sum structure the in the btrfs_bio structure, and then use this information to rewrite the logical address in the btrfs_ordered_sum structure directly at I/O completion time in btrfs_record_physical_zoned. btrfs_rewrite_logical_zoned then simply updates the logical address in the extent tree and the ordered_extent itself.
The code in btrfs_rewrite_logical_zoned now runs for all data I/O completions in zoned file systems, which is fine as there is no remapping to do for non-append writes to conventional zones or for relocation, and the overhead for quickly breaking out of the loop is very low.
Because zoned file systems now need the ordereded_sums structure to record the actual write location returned by zone append, 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.
Note that the btrfs_bio doesn't grow as the new field are places into a union that is so far not used for data writes and has plenty of space left in it.
--- fs/btrfs/bio.c | 5 ++++ fs/btrfs/bio.h | 17 ++++++++++-- fs/btrfs/file-item.c | 30 ++++++++++++++++++++ fs/btrfs/file-item.h | 1 + fs/btrfs/inode.c | 6 +--- fs/btrfs/ordered-data.c | 1 - fs/btrfs/ordered-data.h | 6 ---- fs/btrfs/zoned.c | 61 +++++++++++++++-------------------------- 8 files changed, 73 insertions(+), 54 deletions(-)
fs/btrfs/bio.c +5 -0
@@ -431,6 +431,7 @@ static void btrfs_submit_dev_bio(struct btrfs_device *dev, struct bio *bio)u64 zone_start =round_down (physical, dev->fs_info->zone_size);ASSERT (btrfs_dev_is_sequential (dev, physical)); +btrfs_bio (bio)->orig_physical = physical; bio->bi_iter.bi_sector = zone_start >>SECTOR_SHIFT ; } btrfs_debug_in_rcu(dev->fs_info,
@@ -685,6 +686,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; } }
fs/btrfs/bio.h +14 -3
@@ -39,8 +39,8 @@ struct btrfs_bio {union {/* -* Data checksumming and original I/O information for internal -* use in the btrfs_submit_bio machinery. +* For data reads: checksumming and original I/O information. +* (for internal use in the btrfs_submit_bio machinery only) */ struct {u8 *csum;
@@ -48,7 +48,18 @@ struct btrfs_bio {struct bvec_iter saved_iter; }; -/* For metadata parentness verification. */ +/* +* For data writes: +* - pointer to the checksums for this bio +* - original physical address from the allocator +* (for zone append only) +*/ +struct { +struct btrfs_ordered_sum *sums; +u64 orig_physical; + }; + +/* For metadata reads: parentness verification. */ struct btrfs_tree_parent_check parent_check; };
fs/btrfs/file-item.c +30 -0
@@ -818,11 +818,41 @@ blk_status_t btrfs_csum_one_bio(struct btrfs_bio *bbio) } this_sum_bytes =0 ; + +/* +* The ->sums assignment is for zoned writes, where a bio never spans +* ordered extents and is only done unconditionally because that's cheaper +* than a branch. +*/ + bbio->sums = sums;btrfs_add_ordered_sum (ordered, sums);btrfs_put_ordered_extent (ordered);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) +{ +struct btrfs_ordered_extent *ordered = +btrfs_lookup_ordered_extent (bbio->inode, bbio->file_offset); + +if (WARN_ON_ONCE (!ordered)) +return BLK_STS_IOERR ; + + 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 (ordered, bbio->sums); +btrfs_put_ordered_extent (ordered); +return 0 ; +} +/* * Remove one checksum overlapping a range. *
fs/btrfs/file-item.h +1 -0
@@ -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);
fs/btrfs/inode.c +1 -5
@@ -3301,14 +3301,10 @@ int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) goto out; } -/* A valid ->physical implies a write on a sequential zone. */ -if (ordered_extent->physical != (u64)-1 ) { +if (btrfs_is_zoned (fs_info)) {btrfs_rewrite_logical_zoned (ordered_extent);btrfs_zone_finish_endio (fs_info, ordered_extent->disk_bytenr, ordered_extent->disk_num_bytes); - }else if (btrfs_is_data_reloc_root (inode->root)) { -btrfs_zone_finish_endio (fs_info, ordered_extent->disk_bytenr, - ordered_extent->disk_num_bytes); }if (test_bit (BTRFS_ORDERED_TRUNCATED , &ordered_extent->flags)) {
fs/btrfs/ordered-data.c +0 -1
@@ -209,7 +209,6 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent( entry->compress_type = compress_type; entry->truncated_len = (u64)-1 ; entry->qgroup_rsv = ret; - entry->physical = (u64)-1 ;ASSERT ((flags & ~BTRFS_ORDERED_TYPE_FLAGS ) ==0 ); entry->flags = flags;
fs/btrfs/ordered-data.h +0 -6
@@ -151,12 +151,6 @@ struct btrfs_ordered_extent {struct completion completion;struct btrfs_work flush_work;struct list_head work_list; - -/* -* Used to reverse-map physical address returned from ZONE_APPEND write -* command in a workqueue context -*/ -u64 physical; }; static inline void
fs/btrfs/zoned.c +22 -39
84 lines not on this page · raw message
Signature · 1 line
2.39.2
From a6df812af6f23df7d8e2cfe12e3b85a280362a02 Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Thu, 8 Jun 2023 19:46:50 +0200
Subject: btrfs: defer splitting of ordered extents until I/O completion
The btrfs zoned completion code currently needs an ordered_extent and extent_map per bio so that it can account for the non-predictable write location from Zone Append. To archive that it currently splits the ordered_extent and extent_map at I/O submission time, and then records the actual physical address in the ->physical field of the ordered_extent.
This patch instead switches to record the "original" physical address that the btrfs allocator assigned in spare space in the btrfs_bio, and then rewrites the logical address in the btrfs_ordered_sum structure at I/O completion time. This allows the ordered extent completion handler to simply walk the list of ordered csums and split the ordered extent as needed. This removes an extra ordered extent and extent_map lookup and manipulation during the I/O submission path, and instead batches it in the I/O completion path where we need to touch these anyway.
--- fs/btrfs/bio.c | 17 ---------- fs/btrfs/btrfs_inode.h | 2 -- fs/btrfs/inode.c | 18 +++++++---- fs/btrfs/ordered-data.h | 1 + fs/btrfs/zoned.c | 70 ++++++++++++++++++++++++++++++++++------- fs/btrfs/zoned.h | 6 ++-- 6 files changed, 73 insertions(+), 41 deletions(-)
fs/btrfs/bio.c +0 -17
@@ -61,20 +61,6 @@ struct btrfs_bio *btrfs_bio_alloc(unsigned int nr_vecs, blk_opf_t opf,return bbio; } -static blk_status_t btrfs_bio_extract_ordered_extent (struct btrfs_bio *bbio) -{ -struct btrfs_ordered_extent *ordered; -int ret; - - ordered =btrfs_lookup_ordered_extent (bbio->inode, bbio->file_offset); -if (WARN_ON_ONCE (!ordered)) -return BLK_STS_IOERR ; - ret =btrfs_extract_ordered_extent (bbio, ordered); -btrfs_put_ordered_extent (ordered); - -return errno_to_blk_status (ret); -} -static struct btrfs_bio *btrfs_split_bio (struct btrfs_fs_info *fs_info,struct btrfs_bio *orig_bbio,u64 map_length,bool use_append)
@@ -667,9 +653,6 @@ static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num)if (use_append) { bio->bi_opf &= ~REQ_OP_WRITE ; bio->bi_opf |=REQ_OP_ZONE_APPEND ; - ret =btrfs_bio_extract_ordered_extent (bbio); -if (ret) - goto fail_put_bio; }/*
fs/btrfs/btrfs_inode.h +0 -2
@@ -410,8 +410,6 @@ static inline bool btrfs_inode_can_compress(const struct btrfs_inode *inode)int btrfs_check_sector_csum (struct btrfs_fs_info *fs_info,struct page *page,u32 pgoff,u8 *csum,const u8 *const csum_expected); -int btrfs_extract_ordered_extent (struct btrfs_bio *bbio, -struct btrfs_ordered_extent *ordered);bool btrfs_data_csum_ok (struct btrfs_bio *bbio,struct btrfs_device *dev,u32 bio_offset,struct bio_vec *bv); noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
fs/btrfs/inode.c +12 -6
@@ -2714,8 +2714,8 @@ void btrfs_clear_delalloc_extent(struct btrfs_inode *inode, } } -int btrfs_extract_ordered_extent (struct btrfs_bio *bbio, -struct btrfs_ordered_extent *ordered) +static int btrfs_extract_ordered_extent (struct btrfs_bio *bbio, +struct btrfs_ordered_extent *ordered) {u64 start = (u64 )bbio->bio.bi_iter.bi_sector <<SECTOR_SHIFT ;u64 len = bbio->bio.bi_iter.bi_size;
@@ -3180,7 +3180,7 @@ static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,* an ordered extent if the range of bytes in the file it covers are * fully written. */ -int btrfs_finish_ordered_io (struct btrfs_ordered_extent *ordered_extent) +int btrfs_finish_one_ordered (struct btrfs_ordered_extent *ordered_extent) {struct btrfs_inode *inode =BTRFS_I (ordered_extent->inode);struct btrfs_root *root = inode->root;
@@ -3215,11 +3215,9 @@ int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) goto out; } -if (btrfs_is_zoned (fs_info)) { -btrfs_rewrite_logical_zoned (ordered_extent); +if (btrfs_is_zoned (fs_info))btrfs_zone_finish_endio (fs_info, ordered_extent->disk_bytenr, ordered_extent->disk_num_bytes); - }if (test_bit (BTRFS_ORDERED_TRUNCATED , &ordered_extent->flags)) { truncated = true;
@@ -3387,6 +3385,14 @@ int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)return ret; } +int btrfs_finish_ordered_io (struct btrfs_ordered_extent *ordered) +{ +if (btrfs_is_zoned (btrfs_sb (ordered->inode->i_sb)) && + !test_bit (BTRFS_ORDERED_IOERR , &ordered->flags)) +btrfs_finish_ordered_zoned (ordered); +return btrfs_finish_one_ordered (ordered); +} +void btrfs_writepage_endio_finish_ordered (struct btrfs_inode *inode,struct page *page,u64 start,u64 end,bool uptodate)
fs/btrfs/ordered-data.h +1 -0
@@ -161,6 +161,7 @@ btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t) t->last =NULL ; } +int btrfs_finish_one_ordered (struct btrfs_ordered_extent *ordered_extent);int btrfs_finish_ordered_io (struct btrfs_ordered_extent *ordered_extent);void btrfs_put_ordered_extent (struct btrfs_ordered_extent *entry);
fs/btrfs/zoned.c +58 -12
96 lines not on this page · raw message
fs/btrfs/zoned.h +2 -4
@@ -30,6 +30,8 @@ struct btrfs_zoned_device_info {struct blk_zone sb_zones[2 *BTRFS_SUPER_MIRROR_MAX ]; }; +void btrfs_finish_ordered_zoned (struct btrfs_ordered_extent *ordered); +#ifdef CONFIG_BLK_DEV_ZONED int btrfs_get_dev_zone (struct btrfs_device *device,u64 pos,struct blk_zone *zone);
@@ -56,7 +58,6 @@ void btrfs_redirty_list_add(struct btrfs_transaction *trans, struct extent_buffer *eb);bool btrfs_use_zone_append (struct btrfs_bio *bbio);void btrfs_record_physical_zoned (struct btrfs_bio *bbio); -void btrfs_rewrite_logical_zoned (struct btrfs_ordered_extent *ordered);bool btrfs_check_meta_write_pointer (struct btrfs_fs_info *fs_info,struct extent_buffer *eb,struct btrfs_block_group **cache_ret);
@@ -188,9 +189,6 @@ static inline void btrfs_record_physical_zoned(struct btrfs_bio *bbio) { } -static inline void btrfs_rewrite_logical_zoned ( -struct btrfs_ordered_extent *ordered) { } -static inline bool btrfs_check_meta_write_pointer (struct btrfs_fs_info *fs_info,struct extent_buffer *eb,struct btrfs_block_group **cache_ret)
Signature · 1 line
2.39.2
From 8807a2e457fc7deba266d191681960d4666d9e8e Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Wed, 31 May 2023 09:54:02 +0200
Subject: btrfs: add an ordered_extent pointer to struct btrfs_bio
Add a pointer to the ordered_extent to the existing union in struct btrfs_bio, so all code dealing with data write bios can just use a pointer dereference to retrieve the ordered_extent instead of doing multiple rbtree lookups per I/O.
The reference to this ordered_extent is dropped at end I/O time, which implies that an extra one must be acquired when the bio is split. This also requires moving the btrfs_extract_ordered_extent call into btrfs_split_bio so that the invariant of always having a valid ordered_extent reference for the btrfs_bio is kept.
--- fs/btrfs/bio.c | 42 ++++++++++++++++++++++++++++++++++++++---- fs/btrfs/bio.h | 9 +++------ fs/btrfs/compression.c | 2 +- fs/btrfs/extent_io.c | 2 +- fs/btrfs/file-item.c | 9 +-------- fs/btrfs/inode.c | 8 +++++--- 6 files changed, 49 insertions(+), 23 deletions(-)
fs/btrfs/bio.c +38 -4
74 lines not on this page · raw message
fs/btrfs/bio.h +3 -6
@@ -50,11 +50,13 @@ struct btrfs_bio {/* * For data writes: +* - ordered extent covering the bio * - pointer to the checksums for this bio * - original physical address from the allocator * (for zone append only) */ struct { +struct btrfs_ordered_extent *ordered;struct btrfs_ordered_sum *sums;u64 orig_physical; };
@@ -95,12 +97,7 @@ void btrfs_bio_init(struct btrfs_bio *bbio, struct btrfs_fs_info *fs_info,struct btrfs_bio *btrfs_bio_alloc (unsigned int nr_vecs,blk_opf_t opf,struct btrfs_fs_info *fs_info,btrfs_bio_end_io_t end_io,void *private); - -static inline void btrfs_bio_end_io (struct btrfs_bio *bbio,blk_status_t status) -{ - bbio->bio.bi_status = status; - bbio->end_io (bbio); -} +void btrfs_bio_end_io (struct btrfs_bio *bbio,blk_status_t status);/* Submit using blkcg_punt_bio_submit. */ #define REQ_BTRFS_CGROUP_PUNT REQ_FS_PRIVATE
fs/btrfs/compression.c +1 -1
@@ -303,10 +303,10 @@ void btrfs_submit_compressed_write(struct btrfs_ordered_extent *ordered,INIT_WORK (&cb->write_end_work, btrfs_finish_compressed_write_work); cb->nr_pages = nr_pages; cb->bbio.bio.bi_iter.bi_sector = ordered->disk_bytenr >>SECTOR_SHIFT ; + cb->bbio.ordered = ordered;btrfs_add_compressed_bio_pages (cb);btrfs_submit_bio (&cb->bbio,0 ); -btrfs_put_ordered_extent (ordered); }/*
fs/btrfs/extent_io.c +1 -1
@@ -835,7 +835,7 @@ static void alloc_new_bio(struct btrfs_inode *inode, bio_ctrl->len_to_oe_boundary =min_t (u32,U32_MAX , ordered->file_offset + ordered->disk_num_bytes - file_offset); -btrfs_put_ordered_extent (ordered); + bbio->ordered = ordered; }/*
fs/btrfs/file-item.c +1 -8
@@ -799,19 +799,12 @@ blk_status_t btrfs_csum_one_bio(struct btrfs_bio *bbio)*/ blk_status_t btrfs_alloc_dummy_sum (struct btrfs_bio *bbio) { -struct btrfs_ordered_extent *ordered = -btrfs_lookup_ordered_extent (bbio->inode, bbio->file_offset); - -if (WARN_ON_ONCE (!ordered)) -return BLK_STS_IOERR ; - 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 (ordered, bbio->sums); -btrfs_put_ordered_extent (ordered); +btrfs_add_ordered_sum (bbio->ordered, bbio->sums);return 0 ; }
fs/btrfs/inode.c +5 -3
@@ -2716,8 +2716,11 @@ static int btrfs_extract_ordered_extent(struct btrfs_bio *bbio,return -EINVAL ;/* No need to split if the ordered extent covers the entire bio. */ -if (ordered->disk_num_bytes == len) +if (ordered->disk_num_bytes == len) { +refcount_inc (&ordered->refs); + bbio->ordered = ordered;return 0 ; + }/* * Don't split the extent_map for NOCOW extents, as we're writing into
@@ -2734,8 +2737,7 @@ static int btrfs_extract_ordered_extent(struct btrfs_bio *bbio, new =btrfs_split_ordered_extent (ordered, len);if (IS_ERR (new))return PTR_ERR (new); -btrfs_put_ordered_extent (new); - + bbio->ordered = new;return 0 ; }
Signature · 1 line
2.39.2
Re: [PATCH] btrfs: allocate dummy ordereded_sums objects for nocsum I/O on zoned file systems
Quoted 21 lines · Christoph Hellwig
Quoted 10 lines · David Sterba
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_zonedinzoned.cwhich only sets up thefs_info, so trivial to fix but the new helperbtrfs_alloc_dummy_sum()usesbbio->orderedwhich is not available at this time and was added in a different series ("btrfs: add anordered_extentpointer to structbtrfs_bio").Quoted 3 lines
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::orderedis clear.Ok. Here is a stash of patches:
1) incremental diff
2) complete replacement for that commit with the incremental diff included
Quoted 8 lines
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 anordered_extentpointer to structbtrfs_bioA git tree with all this is also available here:
git://git.infradead.org/users/hch/misc.gitbtrfs-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.