btrfs: sector size < page size enhancement
cover btrfs: sector size < page size enhancement
This series contains several sector size < page size fixes and optimization:
- Pass generic/563 with 4k sector size and 16K/64K page size The last patch.
The test case is a special cgroup one, which requires the fs to avoid reading the whole folio as long as the buffered write range is btrfs sector aligned.
- Fix generic/750 failure with 4K sector size and 16K/64K page size It's a double ordered extent accounting for sector size < page size cases.
The first patch.
The remaining are all preparations for the above goals.
It's now rebased to the latest misc-next branch.
Qu Wenruo (6):
btrfs: fix double accounting of ordered extents during errors
btrfs: extract the inner loop of cow_file_range() to enhance the error
handling
btrfs: use FGP_STABLE to wait for folio writeback
btrfs: make btrfs_do_readpage() to do block-by-block read
btrfs: avoid deadlock when reading a partial uptodate folio
btrfs: allow buffered write to skip full page if it's sector aligned
fs/btrfs/defrag.c | 6 +- fs/btrfs/direct-io.c | 2 +- fs/btrfs/extent_io.c | 85 ++++++---- fs/btrfs/file.c | 13 +- fs/btrfs/inode.c | 347 ++++++++++++++++++++-------------------- fs/btrfs/ordered-data.c | 67 +++++++- fs/btrfs/ordered-data.h | 8 +- 7 files changed, 306 insertions(+), 222 deletions(-)
Signature · 1 line
2.47.0
PATCH 1/6 btrfs: fix double accounting of ordered extents during errors
[BUG]
Btrfs will fail generic/750 randomly if its sector size is smaller than page size.
One of the warning looks like this:
------------[ cut here ]------------ WARNING: CPU: 1 PID: 90263 at fs/btrfs/ordered-data.c:360 can_finish_ordered_extent+0x33c/0x390 [btrfs] CPU: 1 UID: 0 PID: 90263 Comm: kworker/u18:1 Tainted: G OE 6.12.0-rc3-custom+ #79 Workqueue: events_unbound btrfs_async_reclaim_metadata_space [btrfs] pc : can_finish_ordered_extent+0x33c/0x390 [btrfs] lr : can_finish_ordered_extent+0xdc/0x390 [btrfs] Call trace: can_finish_ordered_extent+0x33c/0x390 [btrfs] btrfs_mark_ordered_io_finished+0x130/0x2b8 [btrfs] extent_writepage+0xfc/0x338 [btrfs] extent_write_cache_pages+0x1d4/0x4b8 [btrfs] btrfs_writepages+0x94/0x158 [btrfs] do_writepages+0x74/0x190 filemap_fdatawrite_wbc+0x88/0xc8 start_delalloc_inodes+0x180/0x3b0 [btrfs] btrfs_start_delalloc_roots+0x17c/0x288 [btrfs] shrink_delalloc+0x11c/0x280 [btrfs] flush_space+0x27c/0x310 [btrfs] btrfs_async_reclaim_metadata_space+0xcc/0x208 [btrfs] process_one_work+0x228/0x670 worker_thread+0x1bc/0x360 kthread+0x100/0x118 ret_from_fork+0x10/0x20 irq event stamp: 9784200 hardirqs last enabled at (9784199): [<ffffd21ec54dc01c>] _raw_spin_unlock_irqrestore+0x74/0x80 hardirqs last disabled at (9784200): [<ffffd21ec54db374>] _raw_spin_lock_irqsave+0x8c/0xa0 softirqs last enabled at (9784148): [<ffffd21ec472ff44>] handle_softirqs+0x45c/0x4b0 softirqs last disabled at (9784141): [<ffffd21ec46d01e4>] __do_softirq+0x1c/0x28 ---[ end trace 0000000000000000 ]--- BTRFS critical (device dm-2): bad ordered extent accounting, root=5 ino=1492 OE offset=1654784 OE len=57344 to_dec=49152 left=0
[CAUSE]
The function btrfs_mark_ordered_io_finished() is called for marking all ordered extents in the page range as finished, for error handling.
But for sector size < page size cases, we can have multiple ordered extents in one page.
If extent_writepage_io() failed (the only possible case is submit_one_sector() failed to grab an extent map), then the call site inside extent_writepage() will call btrfs_mark_ordered_io_finished() to finish the created ordered extents.
However some range of the ordered extent may have been submitted already, then btrfs_mark_ordered_io_finished() is called on the same range, causing double accounting.
[FIX]
- Introduce a new member btrfs_bio_ctrl::last_submitted This will trace the last sector submitted through extent_writepage_io().
So for the above extent_writepage() case, we will know exactly which sectors are submitted and should not do the ordered extent accounting.
- Introduce a helper cleanup_ordered_extents()
This will do a sector-by-sector cleanup with btrfs_bio_ctrl::last_submitted and btrfs_bio_ctrl::submit_bitmap into consideartion.
Using @last_submitted is to avoid double accounting on the submitted ranges.
Meanwhile using @submit_bitmap is to avoid touching ranges going through compression.
--- fs/btrfs/extent_io.c | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-)
fs/btrfs/extent_io.c +37 -4
@@ -108,6 +108,14 @@ struct btrfs_bio_ctrl {* This is to avoid touching ranges covered by compression/inline. */ unsigned long submit_bitmap; + +/* +* The end (exclusive) of the last submitted range in the folio. +* +* This is for sector size < page size case where we may hit error +* half way. +*/ +u64 last_submitted; };static void submit_one_bio (struct btrfs_bio_ctrl *bio_ctrl)
@@ -1435,6 +1443,7 @@ static noinline_for_stack int extent_writepage_io(struct btrfs_inode *inode, ret =submit_one_sector (inode, folio, cur, bio_ctrl, i_size);if (ret <0 ) goto out; + bio_ctrl->last_submitted = cur + fs_info->sectorsize; submitted_io = true; } out:
@@ -1453,6 +1462,24 @@ static noinline_for_stack int extent_writepage_io(struct btrfs_inode *inode,return ret; } +static void cleanup_ordered_extents (struct btrfs_inode *inode, +struct folio *folio,u64 file_pos, +u64 num_bytes,unsigned long *bitmap) +{ +struct btrfs_fs_info *fs_info = inode->root->fs_info; +unsigned int cur_bit = (file_pos -folio_pos (folio)) >> fs_info->sectorsize_bits; + +for_each_set_bit_from (cur_bit, bitmap, fs_info->sectors_per_page) { +u64 cur_pos =folio_pos (folio) + (cur_bit << fs_info->sectorsize_bits); + +if (cur_pos >= file_pos + num_bytes) +break ; + +btrfs_mark_ordered_io_finished (inode, folio, cur_pos, + fs_info->sectorsize, false); + } +} +/* * the writepage semantics are similar to regular writepage. extent * records are inserted to lock ranges in the tree, and as dirty areas
@@ -1492,6 +1519,7 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl* The proper bitmap can only be initialized until writepage_delalloc(). */ bio_ctrl->submit_bitmap = (unsigned long )-1 ; + bio_ctrl->last_submitted = page_start; ret =set_folio_extent_mapped (folio);if (ret <0 ) goto done;
@@ -1511,8 +1539,10 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl done:if (ret) { -btrfs_mark_ordered_io_finished (BTRFS_I (inode), folio, - page_start,PAGE_SIZE , !ret); +cleanup_ordered_extents (BTRFS_I (inode), folio, + bio_ctrl->last_submitted, + page_start +PAGE_SIZE - bio_ctrl->last_submitted, + &bio_ctrl->submit_bitmap);mapping_set_error (folio->mapping, ret); }
@@ -2288,14 +2318,17 @@ void extent_write_locked_range(struct inode *inode, const struct folio *locked_f* extent_writepage_io() will do the truncation correctly. */ bio_ctrl.submit_bitmap = (unsigned long )-1 ; + bio_ctrl.last_submitted = cur; ret =extent_writepage_io (BTRFS_I (inode), folio, cur, cur_len, &bio_ctrl, i_size);if (ret ==1 ) goto next_page;if (ret) { -btrfs_mark_ordered_io_finished (BTRFS_I (inode), folio, - cur, cur_len, !ret); +cleanup_ordered_extents (BTRFS_I (inode), folio, + bio_ctrl.last_submitted, + cur_end +1 - bio_ctrl.last_submitted, + &bio_ctrl.submit_bitmap);mapping_set_error (mapping, ret); }btrfs_folio_end_lock (fs_info, folio, cur, cur_len);
Signature · 1 line
2.47.0
Re: [PATCH 1/6] btrfs: fix double accounting of ordered extents during errors
I know this is part of the subpage patches, but this is really a bug fix for the existing subpage handling.
Appreciate if anyone can give this a review.
Thanks,
Qu
Quoted 169 lines · Qu Wenruo
[BUG]
Btrfs will fail generic/750 randomly if its sector size is smaller than page size.One of the warning looks like this:
------------[ cut here ]------------ WARNING: CPU: 1 PID: 90263 at fs/btrfs/ordered-data.c:360 can_finish_ordered_extent+0x33c/0x390 [btrfs] CPU: 1 UID: 0 PID: 90263 Comm: kworker/u18:1 Tainted: G OE 6.12.0-rc3-custom+ #79 Workqueue: events_unbound btrfs_async_reclaim_metadata_space [btrfs] pc : can_finish_ordered_extent+0x33c/0x390 [btrfs] lr : can_finish_ordered_extent+0xdc/0x390 [btrfs] Call trace: can_finish_ordered_extent+0x33c/0x390 [btrfs] btrfs_mark_ordered_io_finished+0x130/0x2b8 [btrfs] extent_writepage+0xfc/0x338 [btrfs] extent_write_cache_pages+0x1d4/0x4b8 [btrfs] btrfs_writepages+0x94/0x158 [btrfs] do_writepages+0x74/0x190 filemap_fdatawrite_wbc+0x88/0xc8 start_delalloc_inodes+0x180/0x3b0 [btrfs] btrfs_start_delalloc_roots+0x17c/0x288 [btrfs] shrink_delalloc+0x11c/0x280 [btrfs] flush_space+0x27c/0x310 [btrfs] btrfs_async_reclaim_metadata_space+0xcc/0x208 [btrfs] process_one_work+0x228/0x670 worker_thread+0x1bc/0x360 kthread+0x100/0x118 ret_from_fork+0x10/0x20 irq event stamp: 9784200 hardirqs last enabled at (9784199): [<ffffd21ec54dc01c>] _raw_spin_unlock_irqrestore+0x74/0x80 hardirqs last disabled at (9784200): [<ffffd21ec54db374>] _raw_spin_lock_irqsave+0x8c/0xa0 softirqs last enabled at (9784148): [<ffffd21ec472ff44>] handle_softirqs+0x45c/0x4b0 softirqs last disabled at (9784141): [<ffffd21ec46d01e4>] __do_softirq+0x1c/0x28 ---[ end trace 0000000000000000 ]--- BTRFS critical (device dm-2): bad ordered extent accounting, root=5 ino=1492 OE offset=1654784 OE len=57344 to_dec=49152 left=0[CAUSE]
The functionbtrfs_mark_ordered_io_finished()is called for marking all ordered extents in the page range as finished, for error handling.But for sector size < page size cases, we can have multiple ordered extents in one page.
If
extent_writepage_io()failed (the only possible case issubmit_one_sector()failed to grab an extent map), then the call site insideextent_writepage()will callbtrfs_mark_ordered_io_finished()to finish the created ordered extents.However some range of the ordered extent may have been submitted already, then
btrfs_mark_ordered_io_finished()is called on the same range, causing double accounting.[FIX]
- Introduce a new memberbtrfs_bio_ctrl::last_submittedThis will trace the last sector submitted throughextent_writepage_io().So for the above
extent_writepage()case, we will know exactly which sectors are submitted and should not do the ordered extent accounting.- Introduce a helper
cleanup_ordered_extents()
This will do a sector-by-sector cleanup withbtrfs_bio_ctrl::last_submittedandbtrfs_bio_ctrl::submit_bitmapinto consideartion.Using @last_submitted is to avoid double accounting on the submitted ranges.
Meanwhile using @submit_bitmap is to avoid touching ranges going through compression.Signed-off-by Qu Wenruo <[email protected]>--- fs/btrfs/extent_io.c | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-)fs/btrfs/extent_io.c +37
-4@@ -108,6 +108,14 @@ struct btrfs_bio_ctrl {* This is to avoid touching ranges covered by compression/inline. */ unsigned long submit_bitmap; + +/* +* The end (exclusive) of the last submitted range in the folio. +* +* This is for sector size < page size case where we may hit error +* half way. +*/ +u64 last_submitted; };static void submit_one_bio (struct btrfs_bio_ctrl *bio_ctrl)@@ -1435,6 +1443,7 @@ static noinline_for_stack int extent_writepage_io(struct btrfs_inode *inode, ret =submit_one_sector (inode, folio, cur, bio_ctrl, i_size);if (ret <0 ) goto out; + bio_ctrl->last_submitted = cur + fs_info->sectorsize; submitted_io = true; } out:@@ -1453,6 +1462,24 @@ static noinline_for_stack int extent_writepage_io(struct btrfs_inode *inode,return ret; } +static void cleanup_ordered_extents (struct btrfs_inode *inode, +struct folio *folio,u64 file_pos, +u64 num_bytes,unsigned long *bitmap) +{ +struct btrfs_fs_info *fs_info = inode->root->fs_info; +unsigned int cur_bit = (file_pos -folio_pos (folio)) >> fs_info->sectorsize_bits; + +for_each_set_bit_from (cur_bit, bitmap, fs_info->sectors_per_page) { +u64 cur_pos =folio_pos (folio) + (cur_bit << fs_info->sectorsize_bits); + +if (cur_pos >= file_pos + num_bytes) +break ; + +btrfs_mark_ordered_io_finished (inode, folio, cur_pos, + fs_info->sectorsize, false); + } +} +/* * the writepage semantics are similar to regular writepage. extent * records are inserted to lock ranges in the tree, and as dirty areas @@ -1492,6 +1519,7 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl* The proper bitmap can only be initialized until writepage_delalloc(). */ bio_ctrl->submit_bitmap = (unsigned long )-1 ; + bio_ctrl->last_submitted = page_start; ret =set_folio_extent_mapped (folio);if (ret <0 ) goto done;@@ -1511,8 +1539,10 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl done:if (ret) { -btrfs_mark_ordered_io_finished (BTRFS_I (inode), folio, - page_start,PAGE_SIZE , !ret); +cleanup_ordered_extents (BTRFS_I (inode), folio, + bio_ctrl->last_submitted, + page_start +PAGE_SIZE - bio_ctrl->last_submitted, + &bio_ctrl->submit_bitmap);mapping_set_error (folio->mapping, ret); }@@ -2288,14 +2318,17 @@ void extent_write_locked_range(struct inode *inode, const struct folio *locked_f* extent_writepage_io() will do the truncation correctly. */ bio_ctrl.submit_bitmap = (unsigned long )-1 ; + bio_ctrl.last_submitted = cur; ret =extent_writepage_io (BTRFS_I (inode), folio, cur, cur_len, &bio_ctrl, i_size);if (ret ==1 ) goto next_page;if (ret) { -btrfs_mark_ordered_io_finished (BTRFS_I (inode), folio, - cur, cur_len, !ret); +cleanup_ordered_extents (BTRFS_I (inode), folio, + bio_ctrl.last_submitted, + cur_end +1 - bio_ctrl.last_submitted, + &bio_ctrl.submit_bitmap);mapping_set_error (mapping, ret); }btrfs_folio_end_lock (fs_info, folio, cur, cur_len);
Re: [PATCH 1/6] btrfs: fix double accounting of ordered extents during errors
Quoted 4 lines · Qu Wenruo
I know this is part of the subpage patches, but this is really a bug fix for the existing subpage handling.
Appreciate if anyone can give this a review.
Looks correct to me. One suggestion to clean up the parameters and to pass bio_ctrl and read the last_sibmitted and the bitmap directly, so something like that:
cleanup_ordered_extents(BTRFS_I(inode), folio, &bio_ctrl, cur_end + 1);
replacing the parameters with the values in the function. Though after another thought, the explicit expressions like
"page_start + PAGE_SIZE - bio_ctrl->last_submitted"
and "cur_end + 1 - bio_ctrl.last_submitted" make it a bit readable. Up to you.
Re: [PATCH 1/6] btrfs: fix double accounting of ordered extents during errors
Quoted 10 lines · David Sterba
Quoted 4 lines · Qu Wenruo
I know this is part of the subpage patches, but this is really a bug fix for the existing subpage handling.
Appreciate if anyone can give this a review.
Looks correct to me. One suggestion to clean up the parameters and to pass
bio_ctrland read thelast_sibmittedand the bitmap directly, so something like that:
Quoted 7 lines
cleanup_ordered_extents(BTRFS_I(inode), folio, &bio_ctrl, cur_end + 1);replacing the parameters with the values in the function. Though after another thought, the explicit expressions like
"page_start+PAGE_SIZE-bio_ctrl->last_submitted"
and "cur_end+ 1 - bio_ctrl.last_submitted" make it a bit readable. Up to you.
This one is replaced by this series:
https://lore.kernel.org/linux-btrfs/[email protected]/
However I'm still hitting hangs where some ordered extent never finishes. (At least better than crash, but still not ideal)
Thanks,
Qu
PATCH 2/6 btrfs: extract the inner loop of cow_file_range() to enhance the error handling
In reply to Qu Wenruo[PROBLEMS]
Currently cow_file_range() has a very complex error handling, splitting the handling into 3 different parts (with one extra hidden part)
|-------(1)----|---(2)---|-------------(3)----------|
`- orig_start `- start `- start + cur_alloc_size `- endRange (1) is the one we handled without any error. Range (2) is the current range we're allocating, has data extent reserved but failed to insert ordered extent.
Range (3) is the range we have not yet touched.
Furthermore there is a special case for range (1) that if a range is for data reloc tree, and btrfs_reloc_clone_csums() failed, such range will be counted as range (1), to avoid metadata space release.
Furthermore there is a very complex handling inside the while() loop.
[ENHANCEMENT]
- Extract the main part of the while() loop into run_one_cow_range() Which does the data extent reservation, extent map creation and ordered extent creation.
With its own error handling.
For the special case of range (1) where btrfs_reloc_clone_csums() failed, it will return error but still with ins->offset set, so that caller can skip the range for error handling.
- Shrink the error range handling to 2 types
With the proper error handling inside run_one_cow_range(), if a range failed run_one_cow_range(), there will be no ordered extent or extent map, thus the old range (2) and range (3) can be merged into one.
- Remove some unnecessary ALIGN()
The range [start, end] is already aligned to sector boundary. Just use ASSERT() to check the alignment.
--- fs/btrfs/inode.c | 337 ++++++++++++++++++++++++----------------------- 1 file changed, 173 insertions(+), 164 deletions(-)
fs/btrfs/inode.c +173 -164
@@ -1293,6 +1293,147 @@ u64 btrfs_get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,return alloc_hint; } +/* +* Run one cow range, which includes: +* +* - Reserve the data extent +* - Create the io extent map +* - Create the ordered extent +* +* @ins will be updated if the range should be skipped for error handling, no +* matter the return value. +* +* Return 0 if everything is fine. +* Return -EAGAIN if btrfs_reserve_extent() failed for zoned fs, caller needs +* some extra handling. +* Return <0 for other errors. +*/ +static int run_one_cow_range (struct btrfs_inode *inode, +struct folio *locked_folio, +struct btrfs_key *ins, +u64 start, +u64 end,u64 *alloc_hint,bool keep_locked) +{ +struct btrfs_root *root = inode->root; +struct btrfs_fs_info *fs_info = root->fs_info; +struct btrfs_ordered_extent *ordered; +struct btrfs_file_extent file_extent = {0 }; +struct extent_state *cached =NULL ; +struct extent_map *em =NULL ; +unsigned long page_ops; +const u64 len = end +1 - start; +u32 min_alloc_size; +int ret; + +ASSERT (IS_ALIGNED (start, fs_info->sectorsize)); +ASSERT (IS_ALIGNED (end +1 , fs_info->sectorsize)); + + ins->offset =0 ; + ins->objectid =0 ; + +/* +* Relocation relies on the relocated extents to have exactly the same +* size as the original extents. Normally writeback for relocation data +* extents follows a NOCOW path because relocation preallocates the +* extents. However, due to an operation such as scrub turning a block +* group to RO mode, it may fallback to COW mode, so we must make sure +* an extent allocated during COW has exactly the requested size and can +* not be split into smaller extents, otherwise relocation breaks and +* fails during the stage where it updates the bytenr of file extent +* items. +*/ +if (btrfs_is_data_reloc_root (root)) + min_alloc_size = len; +else + min_alloc_size = fs_info->sectorsize; + + ret =btrfs_reserve_extent (root, len, len, min_alloc_size,0 , + *alloc_hint, ins,1 ,1 ); +if (ret <0 ) +return ret; + + file_extent.disk_bytenr = ins->objectid; + file_extent.disk_num_bytes = ins->offset; + file_extent.num_bytes = ins->offset; + file_extent.ram_bytes = ins->offset; + file_extent.offset =0 ; + file_extent.compression =BTRFS_COMPRESS_NONE ; + +lock_extent (&inode->io_tree, start, start + ins->offset -1 , + &cached); + + em =btrfs_create_io_em (inode, start, &file_extent, +BTRFS_ORDERED_REGULAR ); +if (IS_ERR (em)) { +unlock_extent (&inode->io_tree, start, + start + ins->offset -1 , &cached); + ret =PTR_ERR (em); + goto out_free_reserved; + } +free_extent_map (em); + + ordered =btrfs_alloc_ordered_extent (inode, start, &file_extent, +1 <<BTRFS_ORDERED_REGULAR ); +if (IS_ERR (ordered)) { +unlock_extent (&inode->io_tree, start, + start + ins->offset -1 , &cached); + ret =PTR_ERR (ordered); + goto out_drop_em; + } + +if (btrfs_is_data_reloc_root (root)) { + ret =btrfs_reloc_clone_csums (ordered); + +/* +* Only drop cache here, and process as normal. +* +* We must not allow extent_clear_unlock_delalloc() +* at error handling to free meta of this ordered +* extent, as its meta should be freed by +* btrfs_finish_ordered_io(). +* +* So we must continue until @start is increased to +* skip current ordered extent. +*/ +if (ret <0 ) +btrfs_drop_extent_map_range (inode, start, + start + ins->offset -1 , + false); + } +btrfs_put_ordered_extent (ordered); + +btrfs_dec_block_group_reservations (fs_info, ins->objectid); + +/* +* We're not doing compressed IO, don't unlock the first page +* (which the caller expects to stay locked), don't clear any +* dirty bits and don't set any writeback bits +* +* Do set the Ordered (Private2) bit so we know this page was +* properly setup for writepage. +*/ + page_ops = (keep_locked ?0 :PAGE_UNLOCK ); + page_ops |=PAGE_SET_ORDERED ; + +extent_clear_unlock_delalloc (inode, start, start + ins->offset -1 , + locked_folio, &cached, +EXTENT_LOCKED |EXTENT_DELALLOC , + page_ops); + *alloc_hint = ins->objectid + ins->offset; +return ret; + +out_drop_em: +btrfs_drop_extent_map_range (inode, start, start + ins->offset -1 , false); +out_free_reserved: +btrfs_dec_block_group_reservations (fs_info, ins->objectid); +btrfs_free_reserved_extent (fs_info, ins->objectid, ins->offset,1 ); +/* This is reserved for btrfs_reserve_extent() error. */ +ASSERT (ret != -EAGAIN ); + ins->offset =0 ; + ins->objectid =0 ; +return ret; +} +/* * when extent_io.c finds a delayed allocation range in the file, * the call backs end up in this code. The basic idea is to
@@ -1328,15 +1469,10 @@ static noinline int cow_file_range(struct btrfs_inode *inode, {struct btrfs_root *root = inode->root;struct btrfs_fs_info *fs_info = root->fs_info; -struct extent_state *cached =NULL ; +const u64 num_bytes = end +1 - start;u64 alloc_hint =0 ;u64 orig_start = start; -u64 num_bytes; -u64 cur_alloc_size =0 ; -u64 min_alloc_size; -u64 blocksize = fs_info->sectorsize; -struct btrfs_key ins; -struct extent_map *em; +u64 cur = start;unsigned clear_bits;unsigned long page_ops;int ret =0 ;
@@ -1346,8 +1482,8 @@ static noinline int cow_file_range(struct btrfs_inode *inode, goto out_unlock; } - num_bytes =ALIGN (end - start +1 , blocksize); - num_bytes =max (blocksize, num_bytes); +ASSERT (IS_ALIGNED (start, fs_info->sectorsize)); +ASSERT (IS_ALIGNED (end +1 , fs_info->sectorsize));ASSERT (num_bytes <=btrfs_super_total_bytes (fs_info->super_copy));inode_should_defrag (inode, start, end, num_bytes,SZ_64K );
@@ -1372,29 +1508,16 @@ static noinline int cow_file_range(struct btrfs_inode *inode, alloc_hint =btrfs_get_extent_allocation_hint (inode, start, num_bytes); -/* -* Relocation relies on the relocated extents to have exactly the same -* size as the original extents. Normally writeback for relocation data -* extents follows a NOCOW path because relocation preallocates the -* extents. However, due to an operation such as scrub turning a block -* group to RO mode, it may fallback to COW mode, so we must make sure -* an extent allocated during COW has exactly the requested size and can -* not be split into smaller extents, otherwise relocation breaks and -* fails during the stage where it updates the bytenr of file extent -* items. -*/ -if (btrfs_is_data_reloc_root (root)) - min_alloc_size = num_bytes; -else - min_alloc_size = fs_info->sectorsize; +while (cur < end) { +struct btrfs_key ins = {0 }; -while (num_bytes >0 ) { -struct btrfs_ordered_extent *ordered; -struct btrfs_file_extent file_extent; - - ret =btrfs_reserve_extent (root, num_bytes, num_bytes, - min_alloc_size,0 , alloc_hint, - &ins,1 ,1 ); + ret =run_one_cow_range (inode, locked_folio, &ins, + cur, end, &alloc_hint, keep_locked); +/* +* @cur must be advanced before error handling. +* Special handling for possible btrfs_reloc_clone_csums() error. +*/ + cur += ins.offset;if (ret == -EAGAIN ) {/* * btrfs_reserve_extent only returns -EAGAIN for zoned
@@ -1408,145 +1531,51 @@ static noinline int cow_file_range(struct btrfs_inode *inode,* us, or return -ENOSPC if it can't handle retries. */ ASSERT (btrfs_is_zoned (fs_info)); -if (start == orig_start) { +if (cur == orig_start) {wait_on_bit_io (&inode->root->fs_info->flags,BTRFS_FS_NEED_ZONE_FINISH ,TASK_UNINTERRUPTIBLE );continue ; }if (done_offset) { - *done_offset = start -1 ; + *done_offset = cur -1 ;return 0 ; } ret = -ENOSPC ; }if (ret <0 ) goto out_unlock; - cur_alloc_size = ins.offset; - - file_extent.disk_bytenr = ins.objectid; - file_extent.disk_num_bytes = ins.offset; - file_extent.num_bytes = ins.offset; - file_extent.ram_bytes = ins.offset; - file_extent.offset =0 ; - file_extent.compression =BTRFS_COMPRESS_NONE ; - -lock_extent (&inode->io_tree, start, start + cur_alloc_size -1 , - &cached); - - em =btrfs_create_io_em (inode, start, &file_extent, -BTRFS_ORDERED_REGULAR ); -if (IS_ERR (em)) { -unlock_extent (&inode->io_tree, start, - start + cur_alloc_size -1 , &cached); - ret =PTR_ERR (em); - goto out_reserve; - } -free_extent_map (em); - - ordered =btrfs_alloc_ordered_extent (inode, start, &file_extent, -1 <<BTRFS_ORDERED_REGULAR ); -if (IS_ERR (ordered)) { -unlock_extent (&inode->io_tree, start, - start + cur_alloc_size -1 , &cached); - ret =PTR_ERR (ordered); - goto out_drop_extent_cache; - } - -if (btrfs_is_data_reloc_root (root)) { - ret =btrfs_reloc_clone_csums (ordered); - -/* -* Only drop cache here, and process as normal. -* -* We must not allow extent_clear_unlock_delalloc() -* at out_unlock label to free meta of this ordered -* extent, as its meta should be freed by -* btrfs_finish_ordered_io(). -* -* So we must continue until @start is increased to -* skip current ordered extent. -*/ -if (ret) -btrfs_drop_extent_map_range (inode, start, - start + cur_alloc_size -1 , - false); - } -btrfs_put_ordered_extent (ordered); - -btrfs_dec_block_group_reservations (fs_info, ins.objectid); - -/* -* We're not doing compressed IO, don't unlock the first page -* (which the caller expects to stay locked), don't clear any -* dirty bits and don't set any writeback bits -* -* Do set the Ordered (Private2) bit so we know this page was -* properly setup for writepage. -*/ - page_ops = (keep_locked ?0 :PAGE_UNLOCK ); - page_ops |=PAGE_SET_ORDERED ; - -extent_clear_unlock_delalloc (inode, start, start + cur_alloc_size -1 , - locked_folio, &cached, -EXTENT_LOCKED |EXTENT_DELALLOC , - page_ops); -if (num_bytes < cur_alloc_size) - num_bytes =0 ; -else - num_bytes -= cur_alloc_size; - alloc_hint = ins.objectid + ins.offset; - start += cur_alloc_size; - cur_alloc_size =0 ; - -/* -* btrfs_reloc_clone_csums() error, since start is increased -* extent_clear_unlock_delalloc() at out_unlock label won't -* free metadata of current ordered extent, we're OK to exit. -*/ -if (ret) - goto out_unlock; } done:if (done_offset) *done_offset = end;return ret; -out_drop_extent_cache: -btrfs_drop_extent_map_range (inode, start, start + cur_alloc_size -1 , false); -out_reserve: -btrfs_dec_block_group_reservations (fs_info, ins.objectid); -btrfs_free_reserved_extent (fs_info, ins.objectid, ins.offset,1 ); out_unlock:/* -* Now, we have three regions to clean up: +* Now we have two regions to clean up: * -* |-------(1)----|---(2)---|-------------(3)----------| -* `- orig_start `- start `- start + cur_alloc_size `- end +* |-----(1)-------|----(2)-----| +* `- start `- cur `- end * -* We process each region below. +* @cur is only updated when a successful extent reservation along +* with extent maps/ordered extents created. */ - clear_bits =EXTENT_LOCKED |EXTENT_DELALLOC |EXTENT_DELALLOC_NEW |EXTENT_DEFRAG |EXTENT_CLEAR_META_RESV ; page_ops =PAGE_UNLOCK |PAGE_START_WRITEBACK |PAGE_END_WRITEBACK ;/* -* For the range (1). We have already instantiated the ordered extents -* for this region. They are cleaned up by -* btrfs_cleanup_ordered_extents() in e.g, -* btrfs_run_delalloc_range(). EXTENT_LOCKED | EXTENT_DELALLOC are -* already cleared in the above loop. And, EXTENT_DELALLOC_NEW | -* EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV are handled by the cleanup -* function. +* For the range (1), we have already instantiated the ordered extents +* for the region. They will be cleaned up by btrfs_cleanup_ordered_extents(). * -* However, in case of @keep_locked, we still need to unlock the pages -* (except @locked_folio) to ensure all the pages are unlocked. +* Here we only need to unlock the pages, which will be done by +* the extent_clear_unlock_delalloc() with PAGE_UNLOCK. */ -if (keep_locked && orig_start < start) { +if (keep_locked && start < cur) {if (!locked_folio)mapping_set_error (inode->vfs_inode.i_mapping, ret); -extent_clear_unlock_delalloc (inode, orig_start, start -1 , +extent_clear_unlock_delalloc (inode, orig_start, cur -1 , locked_folio,NULL ,0 , page_ops); }
@@ -1555,39 +1584,19 @@ static noinline int cow_file_range(struct btrfs_inode *inode,* clearing these flags under the extent lock, so lock the rest of the * range and clear everything up. */ -lock_extent (&inode->io_tree, start, end,NULL ); +lock_extent (&inode->io_tree, cur, end,NULL );/* -* For the range (2). If we reserved an extent for our delalloc range -* (or a subrange) and failed to create the respective ordered extent, -* then it means that when we reserved the extent we decremented the -* extent's size from the data space_info's bytes_may_use counter and -* incremented the space_info's bytes_reserved counter by the same -* amount. We must make sure extent_clear_unlock_delalloc() does not try -* to decrement again the data space_info's bytes_may_use counter, -* therefore we do not pass it the flag EXTENT_CLEAR_DATA_RESV. -*/ -if (cur_alloc_size) { -extent_clear_unlock_delalloc (inode, start, - start + cur_alloc_size -1 , - locked_folio, &cached, clear_bits, - page_ops); -btrfs_qgroup_free_data (inode,NULL , start, cur_alloc_size,NULL ); - } - -/* -* For the range (3). We never touched the region. In addition to the +* For the range (2). We never touched the region. In addition to the * clear_bits above, we add EXTENT_CLEAR_DATA_RESV to release the data * space_info's bytes_may_use counter, reserved in * btrfs_check_data_free_space(). */ -if (start + cur_alloc_size < end) { +if (cur < end) { clear_bits |=EXTENT_CLEAR_DATA_RESV ; -extent_clear_unlock_delalloc (inode, start + cur_alloc_size, - end, locked_folio, - &cached, clear_bits, page_ops); -btrfs_qgroup_free_data (inode,NULL , start + cur_alloc_size, - end - start - cur_alloc_size +1 ,NULL ); +extent_clear_unlock_delalloc (inode, cur, end, locked_folio, +NULL , clear_bits, page_ops); +btrfs_qgroup_free_data (inode,NULL , cur, end +1 - cur,NULL ); }return ret; }
Signature · 1 line
2.47.0
PATCH 3/6 btrfs: use FGP_STABLE to wait for folio writeback
In reply to Qu Wenruo__filemap_get_folio() provides the flag FGP_STABLE to wait for writeback.
There are two call sites doing __filemap_get_folio() then folio_wait_writeback():
- btrfs_truncate_block()
- defrag_prepare_one_folio()
We can directly utilize that flag instead of manually calling folio_wait_writeback().
--- fs/btrfs/defrag.c | 4 +--- fs/btrfs/inode.c | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-)
fs/btrfs/defrag.c +1 -3
@@ -865,7 +865,7 @@ static struct folio *defrag_prepare_one_folio(struct btrfs_inode *inode, pgoff_t again: folio =__filemap_get_folio (mapping, index, -FGP_LOCK |FGP_ACCESSED |FGP_CREAT , mask); +FGP_LOCK |FGP_ACCESSED |FGP_CREAT |FGP_STABLE , mask);if (IS_ERR (folio))return folio;
@@ -1222,8 +1222,6 @@ static int defrag_one_range(struct btrfs_inode *inode, u64 start, u32 len, goto free_folios; } } -for (i =0 ; i < nr_pages; i++) -folio_wait_writeback (folios[i]);/* Lock the pages range */ lock_extent(&inode->io_tree, start_index << PAGE_SHIFT,
fs/btrfs/inode.c +1 -3
@@ -4775,7 +4775,7 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len, } again: folio =__filemap_get_folio (mapping, index, -FGP_LOCK |FGP_ACCESSED |FGP_CREAT , mask); +FGP_LOCK |FGP_ACCESSED |FGP_CREAT |FGP_STABLE , mask);if (IS_ERR (folio)) {btrfs_delalloc_release_space (inode, data_reserved, block_start, blocksize, true);
@@ -4808,8 +4808,6 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,if (ret <0 ) goto out_unlock; -folio_wait_writeback (folio); -lock_extent (io_tree, block_start, block_end, &cached_state); ordered =btrfs_lookup_ordered_extent (inode, block_start);
Signature · 1 line
2.47.0
PATCH 4/6 btrfs: make btrfs_do_readpage() to do block-by-block read
In reply to Qu WenruoCurrently if a btrfs has sector size < page size, btrfs_do_readpage() will handle the range extent by extent, this is good for performance as it doesn't need to re-lookup the same extent map again and again (although __get_extent_map() already does extra cached em check).
This is totally fine and is a valid optimization, but it has an assumption that, there is no partial uptodate range in the page.
Meanwhile there is an incoming feature, requiring btrfs to skip the full page read if a buffered write range covers a full sector but not a full page.
In that case, we can have a page that is partially uptodate, and the current per-extent lookup can not handle such case.
So here we change btrfs_do_readpage() to do block-by-block read, this simplifies the following things:
- Remove the need for @iosize variable Because we just use sectorsize as our increment.
- Remove @pg_offset, and calculate it inside the loop when needed It's just offset_in_folio().
- Use a for() loop instead of a while() loop
This will slightly reduce the read performance for
sector size < page size cases, but for the future where we can skip a full page read for a lot of cases, it should still be worthy.
For sector size == page size, this brings no performance change.
--- fs/btrfs/extent_io.c | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-)
fs/btrfs/extent_io.c +12 -25
@@ -961,9 +961,7 @@ static int btrfs_do_readpage(struct folio *folio, struct extent_map **em_cached,u64 block_start;struct extent_map *em;int ret =0 ; -size_t pg_offset =0 ; -size_t iosize; -size_t blocksize = fs_info->sectorsize; +const size_t blocksize = fs_info->sectorsize; ret =set_folio_extent_mapped (folio);if (ret <0 ) {
@@ -974,23 +972,22 @@ static int btrfs_do_readpage(struct folio *folio, struct extent_map **em_cached,if (folio->index == last_byte >>folio_shift (folio)) {size_t zero_offset =offset_in_folio (folio, last_byte); -if (zero_offset) { - iosize =folio_size (folio) - zero_offset; -folio_zero_range (folio, zero_offset, iosize); - } +if (zero_offset) +folio_zero_range (folio, zero_offset, +folio_size (folio) - zero_offset); } bio_ctrl->end_io_func = end_bbio_data_read;begin_folio_read (fs_info, folio); -while (cur <= end) { +for (cur = start; cur <= end; cur += blocksize) {enum btrfs_compression_type compress_type =BTRFS_COMPRESS_NONE ; +unsigned long pg_offset =offset_in_folio (folio, cur);bool force_bio_submit = false;u64 disk_bytenr;ASSERT (IS_ALIGNED (cur, fs_info->sectorsize));if (cur >= last_byte) { - iosize =folio_size (folio) - pg_offset; -folio_zero_range (folio, pg_offset, iosize); -end_folio_read (folio, true, cur, iosize); +folio_zero_range (folio, pg_offset, end - cur +1 ); +end_folio_read (folio, true, cur, end - cur +1 );break ; } em = __get_extent_map(inode, folio, cur, end - cur + 1,
@@ -1005,8 +1002,6 @@ static int btrfs_do_readpage(struct folio *folio, struct extent_map **em_cached, compress_type =extent_map_compression (em); - iosize =min (extent_map_end (em) - cur, end - cur +1 ); - iosize =ALIGN (iosize, blocksize);if (compress_type !=BTRFS_COMPRESS_NONE ) disk_bytenr = em->disk_bytenr;else
@@ -1062,18 +1057,13 @@ static int btrfs_do_readpage(struct folio *folio, struct extent_map **em_cached,/* we've found a hole, just zero and go on */ if (block_start ==EXTENT_MAP_HOLE ) { -folio_zero_range (folio, pg_offset, iosize); - -end_folio_read (folio, true, cur, iosize); - cur = cur + iosize; - pg_offset += iosize; +folio_zero_range (folio, pg_offset, blocksize); +end_folio_read (folio, true, cur, blocksize);continue ; }/* the get_extent function already copied into the folio */ if (block_start ==EXTENT_MAP_INLINE ) { -end_folio_read (folio, true, cur, iosize); - cur = cur + iosize; - pg_offset += iosize; +end_folio_read (folio, true, cur, blocksize);continue ; }
@@ -1084,12 +1074,9 @@ static int btrfs_do_readpage(struct folio *folio, struct extent_map **em_cached,if (force_bio_submit)submit_one_bio (bio_ctrl); -submit_extent_folio (bio_ctrl, disk_bytenr, folio, iosize, +submit_extent_folio (bio_ctrl, disk_bytenr, folio, blocksize, pg_offset); - cur = cur + iosize; - pg_offset += iosize; } -return 0 ; }
Signature · 1 line
2.47.0
PATCH 5/6 btrfs: avoid deadlock when reading a partial uptodate folio
In reply to Qu Wenruo[BUG]
This is for a deadlock only possible after the out-of-tree patch "btrfs: allow buffered write to skip full page if it's sector aligned".
For now it's impossible to hit the deadlock, the reason will be explained in [CAUSE] section.
If the sector size is smaller than page size, and we allow btrfs to avoid reading the full page because the buffered write range is sector aligned, we can hit a hang with generic/095 runs:
__switch_to+0xf8/0x168 __schedule+0x328/0x8a8 schedule+0x54/0x140 io_schedule+0x44/0x68 folio_wait_bit_common+0x198/0x3f8 __folio_lock+0x24/0x40 extent_write_cache_pages+0x2e0/0x4c0 [btrfs] btrfs_writepages+0x94/0x158 [btrfs] do_writepages+0x74/0x190 filemap_fdatawrite_wbc+0x88/0xc8 __filemap_fdatawrite_range+0x6c/0xa8 filemap_fdatawrite_range+0x1c/0x30 btrfs_start_ordered_extent+0x264/0x2e0 [btrfs] btrfs_lock_and_flush_ordered_range+0x8c/0x160 [btrfs] __get_extent_map+0xa0/0x220 [btrfs] btrfs_do_readpage+0x1bc/0x5d8 [btrfs] btrfs_read_folio+0x50/0xa0 [btrfs] filemap_read_folio+0x54/0x110 filemap_update_page+0x2e0/0x3b8 filemap_get_pages+0x228/0x4d8 filemap_read+0x11c/0x3b8 btrfs_file_read_iter+0x74/0x90 [btrfs] new_sync_read+0xd0/0x1d0 vfs_read+0x1a0/0x1f0
There is also the minimal fio reproducer extracted from that test case to reproduce the deadlock:
[global]
bs=8k
iodepth=1
randrepeat=1
size=256k
directory=$mnt
numjobs=1
[job1]
ioengine=sync
bs=512
direct=1
rw=randread
filename=file1
[job2]
ioengine=libaio
rw=randwrite
direct=1
filename=file1
[job3]
ioengine=posixaio
rw=randwrite
filename=file1
[CAUSE]
The above call trace shows that, during the folio read a writeback is triggered on the same folio.
And since during btrfs_do_readpage(), the folio is locked, the writeback will never be able to lock the folio, thus it is waiting on itself thus causing the deadlock.
The root cause is a little complex, the system is 64K page sized, with 4K sector size:
1) The folio has its range [48K, 64K) marked dirty by buffered write
0 16K 32K 48K 64K | |///////////|
\- sector Uptodate|Dirty
2) Writeback finished for [48K, 64K), but ordered extent not yet finished
0 16K 32K 48K 64K | |///////////|
\- sector Uptodate
extent map PINNED OE still here
3) The folio is released from page cache
This can be triggered by direct IO through the following call chain:
iomap_dio_rw()
\- kiocb_invalidate_pages()
\- filemap_invalidate_pages()
\- invalidate_inode_pages2_range()
\- invalidate_complete_folio2()
\- filemap_release_folio()
\- btrfs_release_folio()
\- __btrfs_release_folio()
\- try_release_extent_mapping() Since there is no extent state with EXTENT_LOCKED flag in the folio range, btrfs allows the folio to be released.
Now there is no folio->private to record which block is uptodate. But extent map and OE are still here.
0 16K 32K 48K 64K | |///////////|
\- extent map PINNED
OE still here
4) Buffered write dirtied range [0, 16K)
Since it's sector aligned, btrfs didn't read the full folio from disk.
0 16K 32K 48K 64K |//////////| |///////////|
\- sector Uptodate|Dirty \- extent map PINNED
OE still here
5) Read on the folio is triggered
For the range [0, 16K), since it's already uptodate, btrfs skips this range.
For the range [16K, 48K), btrfs submit the read from disk.
The problem comes to the range [48K, 64K), the following call chain happens:
btrfs_do_readpage()
\- __get_extent_map()
\- btrfs_lock_and_flush_ordered_range()
\- btrfs_start_ordered_extent()
\- filemap_fdatawrite_range()
\- btrfs_writepages()
\- extent_write_cache_pages()
\- folio_lock()Since the folio indeed has dirty sectors in range [0, 16K), the range will be written back.
But the folio is already locked by the folio read, the writeback will never be able to lock the folio, thus lead to the deadlock.
This sequence can only happen if all the following conditions are met:
- The sector size is smaller than page size.
Or we won't have mixed dirty blocks in the same folio we're reading.
- We allow the buffered write to skip the folio read if it's sector aligned.
This is done by the incoming patch
"btrfs: allow buffered write to skip full page if it's sector aligned".
The ultimate goal of that patch is to reduce unnecessary read for sector size < page size cases, and to pass generic/563.
Otherwise the folio will be read from the disk during buffered write, before marking it dirty.
Thus will not trigger the deadlock.
[FIX]
Break the step 5) of the above case.
By passing an optional @locked_folio into btrfs_start_ordered_extent() and btrfs_lock_and_flush_ordered_range().
If we got such locked folio skip the writeback for ranges of that folio.
Here we also do extra asserts to make sure the target
range is already not dirty, or the ordered extent we wait will never be able to finish, since part of the ordered extent is never submitted.
So far only the call site inside __get_extent_map() is passing the new parameter.
--- fs/btrfs/defrag.c | 2 +- fs/btrfs/direct-io.c | 2 +- fs/btrfs/extent_io.c | 3 +- fs/btrfs/file.c | 8 ++--- fs/btrfs/inode.c | 6 ++-- fs/btrfs/ordered-data.c | 67 ++++++++++++++++++++++++++++++++++++----- fs/btrfs/ordered-data.h | 8 +++-- 7 files changed, 75 insertions(+), 21 deletions(-)
fs/btrfs/defrag.c +1 -1
@@ -902,7 +902,7 @@ static struct folio *defrag_prepare_one_folio(struct btrfs_inode *inode, pgoff_tbreak ;folio_unlock (folio); -btrfs_start_ordered_extent (ordered); +btrfs_start_ordered_extent (ordered,NULL );btrfs_put_ordered_extent (ordered);folio_lock (folio);/*
fs/btrfs/direct-io.c +1 -1
@@ -103,7 +103,7 @@ static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,*/ if (writing ||test_bit (BTRFS_ORDERED_DIRECT , &ordered->flags)) -btrfs_start_ordered_extent (ordered); +btrfs_start_ordered_extent (ordered,NULL );else ret = nowait ? -EAGAIN : -ENOTBLK ;btrfs_put_ordered_extent (ordered);
fs/btrfs/extent_io.c +2 -1
@@ -930,7 +930,8 @@ static struct extent_map *__get_extent_map(struct inode *inode, *em_cached =NULL ; } -btrfs_lock_and_flush_ordered_range (BTRFS_I (inode), start, start + len -1 , &cached_state); +btrfs_lock_and_flush_ordered_range (BTRFS_I (inode), folio, start, + start + len -1 , &cached_state); em =btrfs_get_extent (BTRFS_I (inode), folio, start, len);if (!IS_ERR (em)) {BUG_ON (*em_cached);
fs/btrfs/file.c +4 -4
@@ -979,7 +979,7 @@ lock_and_cleanup_extent_if_need(struct btrfs_inode *inode, struct folio *folio, cached_state);folio_unlock (folio);folio_put (folio); -btrfs_start_ordered_extent (ordered); +btrfs_start_ordered_extent (ordered,NULL );btrfs_put_ordered_extent (ordered);return -EAGAIN ; }
@@ -1047,8 +1047,8 @@ int btrfs_check_nocow_lock(struct btrfs_inode *inode, loff_t pos,return -EAGAIN ; } }else { -btrfs_lock_and_flush_ordered_range (inode, lockstart, lockend, - &cached_state); +btrfs_lock_and_flush_ordered_range (inode,NULL , lockstart, + lockend, &cached_state); } ret =can_nocow_extent (&inode->vfs_inode, lockstart, &num_bytes,NULL , nowait, false);
@@ -1884,7 +1884,7 @@ static vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)unlock_extent (io_tree, page_start, page_end, &cached_state);folio_unlock (folio);up_read (&BTRFS_I (inode)->i_mmap_lock); -btrfs_start_ordered_extent (ordered); +btrfs_start_ordered_extent (ordered,NULL );btrfs_put_ordered_extent (ordered); goto again; }
fs/btrfs/inode.c +3 -3
@@ -2771,7 +2771,7 @@ static void btrfs_writepage_fixup_worker(struct btrfs_work *work)unlock_extent (&inode->io_tree, page_start, page_end, &cached_state);folio_unlock (folio); -btrfs_start_ordered_extent (ordered); +btrfs_start_ordered_extent (ordered,NULL );btrfs_put_ordered_extent (ordered); goto again; }
@@ -4815,7 +4815,7 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,unlock_extent (io_tree, block_start, block_end, &cached_state);folio_unlock (folio);folio_put (folio); -btrfs_start_ordered_extent (ordered); +btrfs_start_ordered_extent (ordered,NULL );btrfs_put_ordered_extent (ordered); goto again; }
@@ -4950,7 +4950,7 @@ int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)if (size <= hole_start)return 0 ; -btrfs_lock_and_flush_ordered_range (inode, hole_start, block_end -1 , +btrfs_lock_and_flush_ordered_range (inode,NULL , hole_start, block_end -1 , &cached_state); cur_offset = hole_start;while (1 ) {
fs/btrfs/ordered-data.c +59 -8
119 lines not on this page · raw message
fs/btrfs/ordered-data.h +5 -3
@@ -191,7 +191,8 @@ void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry, struct btrfs_ordered_sum *sum);struct btrfs_ordered_extent *btrfs_lookup_ordered_extent (struct btrfs_inode *inode,u64 file_offset); -void btrfs_start_ordered_extent (struct btrfs_ordered_extent *entry); +void btrfs_start_ordered_extent (struct btrfs_ordered_extent *entry, +struct folio *locked_folio);int btrfs_wait_ordered_range (struct btrfs_inode *inode,u64 start,u64 len);struct btrfs_ordered_extent *btrfs_lookup_first_ordered_extent (struct btrfs_inode *inode,u64 file_offset);
@@ -207,8 +208,9 @@ u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr, const struct btrfs_block_group *bg);void btrfs_wait_ordered_roots (struct btrfs_fs_info *fs_info,u64 nr,const struct btrfs_block_group *bg); -void btrfs_lock_and_flush_ordered_range (struct btrfs_inode *inode,u64 start, -u64 end, +void btrfs_lock_and_flush_ordered_range (struct btrfs_inode *inode, +struct folio *locked_folio, +u64 start,u64 end,struct extent_state **cached_state);bool btrfs_try_lock_ordered_range (struct btrfs_inode *inode,u64 start,u64 end,struct extent_state **cached_state);
Signature · 1 line
2.47.0
PATCH 6/6 btrfs: allow buffered write to skip full page if it's sector aligned
In reply to Qu Wenruo[BUG]
Since the support of sector size < page size for btrfs, test case generic/563 fails with 4K sector size and 64K page size:
--- tests/generic/563.out 2024-04-25 18:13:45.178550333 +0930
+++ /home/adam/xfstests-dev/results//generic/563.out.bad 2024-09-30 09:09:16.155312379 +0930
@@ -3,7 +3,8 @@
read is in range
write is in range
write -> read/write
-read is in range
+read has value of 8388608
+read is NOT in range -33792 .. 33792
write is in range
...[CAUSE]
The test case creates a 8MiB file, then buffered write into the 8MiB using 4K block size, to overwrite the whole file.
On 4K page sized systems, since the write range covers the full sector and page, btrfs will no bother reading the page, just like what XFS and EXT4 do.
But 64K page sized systems, although the write is sector aligned, it's not page aligned, thus btrfs still goes the full page alignment check, and read the full page out.
This causes extra data read, and fail the test case.
[FIX]
To skip the full page read, we need to do the following modification:
- Do not trigger full page read as long as the buffered write is sector aligned
This is pretty simple by modifying the check inside prepare_uptodate_page().
- Skip already uptodate sectors during full page read Or we can lead to the following data corruption:
0 32K 64K |///////| |
Where the file range [0, 32K) is dirtied by buffered write, the remaining range [32K, 64K) is not.
When reading the full page, since [0,32K) is only dirtied but not written back, there is no data extent map for it, but a hole covering [0, 64k).
If we continue reading the full page range [0, 64K), the dirtied range will be filled with 0 (since there is only a hole covering the whole range).
This causes the dirtied range to get lost.
--- fs/btrfs/extent_io.c | 4 ++++ fs/btrfs/file.c | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-)
fs/btrfs/extent_io.c +4 -0
@@ -991,6 +991,10 @@ static int btrfs_do_readpage(struct folio *folio, struct extent_map **em_cached,end_folio_read (folio, true, cur, end - cur +1 );break ; } +if (btrfs_folio_test_uptodate (fs_info, folio, cur, blocksize)) { +end_folio_read (folio, true, cur, blocksize); +continue ; + } em =__get_extent_map (inode, folio, cur, end - cur +1 , em_cached);if (IS_ERR (em)) {
fs/btrfs/file.c +3 -2
@@ -841,14 +841,15 @@ static int prepare_uptodate_folio(struct inode *inode, struct folio *folio, u64 {u64 clamp_start =max_t (u64, pos,folio_pos (folio));u64 clamp_end =min_t (u64, pos + len,folio_pos (folio) +folio_size (folio)); +const u32 sectorsize =inode_to_fs_info (inode)->sectorsize;int ret =0 ;if (folio_test_uptodate (folio))return 0 ;if (!force_uptodate && -IS_ALIGNED (clamp_start,PAGE_SIZE ) && -IS_ALIGNED (clamp_end,PAGE_SIZE )) +IS_ALIGNED (clamp_start, sectorsize) && +IS_ALIGNED (clamp_end, sectorsize))return 0 ; ret =btrfs_read_folio (NULL , folio);
Signature · 1 line
2.47.0