btrfs: minor cleanup related to bitmap operations
cover btrfs: minor cleanup related to bitmap operations
This is the safer cleanup part extracted from the previous attempt:
https://lore.kernel.org/linux-btrfs/[email protected]/
The first patch is to concentrate the error handling of submit_one_sector() so we do not have two separate error handling.
The second patch is the convert for_each_set_bit() to a more efficient for_each_set_bitmap() so that we can benefit from functions which accept a range other than a single block.
Qu Wenruo (2):
btrfs: integrate the error handling of submit_one_sector()
btrfs: replace for_each_set_bit() with for_each_set_bitmap()
fs/btrfs/extent_io.c | 55 +++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 24 deletions(-)
Signature · 1 line
2.52.0
PATCH 1/2 btrfs: integrate the error handling of submit_one_sector()
Currently submit_one_sector() has only one failure pattern from btrfs_get_extent().
However the error handling is split into two parts, one inside submit_one_sector(), which clears the dirty flag and finishes the writeback for the fs block.
The other part is to submit any remaining bio inside bio_ctrl and mark the ordered extent finished for the fs block.
There is no special reason that we must split the error handling, let's just concentrate all the error handling into submit_one_sector().
--- fs/btrfs/extent_io.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-)
fs/btrfs/extent_io.c +15 -14
@@ -1598,7 +1598,7 @@ static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode,/* * Return 0 if we have submitted or queued the sector for submission. -* Return <0 for critical errors, and the sector will have its dirty flag cleared. +* Return <0 for critical errors, and the involved sector will be cleaned up. * * Caller should make sure filepos < i_size and handle filepos >= i_size case. */
@@ -1622,6 +1622,13 @@ static int submit_one_sector(struct btrfs_inode *inode, em =btrfs_get_extent (inode,NULL , filepos, sectorsize);if (IS_ERR (em)) { +/* +* bio_ctrl may contain a bio crossing several folios. +* Submit it immediately so that the bio has a chance +* to finish normally, other than marked as error. +*/ +submit_one_bio (bio_ctrl); +/* * When submission failed, we should still clear the folio dirty. * Or the folio will be written back again but without any
@@ -1630,6 +1637,13 @@ static int submit_one_sector(struct btrfs_inode *inode,btrfs_folio_clear_dirty (fs_info, folio, filepos, sectorsize);btrfs_folio_set_writeback (fs_info, folio, filepos, sectorsize);btrfs_folio_clear_writeback (fs_info, folio, filepos, sectorsize); + +/* +* Since there is no bio submitted to finish the ordered +* extent, we have to manually finish this sector. +*/ +btrfs_mark_ordered_io_finished (inode, folio, filepos, + fs_info->sectorsize, false);return PTR_ERR (em); }
@@ -1756,19 +1770,6 @@ static noinline_for_stack int extent_writepage_io(struct btrfs_inode *inode, } ret =submit_one_sector (inode, folio, cur, bio_ctrl, i_size);if (unlikely (ret <0 )) { -/* -* bio_ctrl may contain a bio crossing several folios. -* Submit it immediately so that the bio has a chance -* to finish normally, other than marked as error. -*/ -submit_one_bio (bio_ctrl); -/* -* Failed to grab the extent map which should be very rare. -* Since there is no bio submitted to finish the ordered -* extent, we have to manually finish this sector. -*/ -btrfs_mark_ordered_io_finished (inode, folio, cur, - fs_info->sectorsize, false);if (!found_error) found_error = ret;continue ;
Signature · 1 line
2.52.0
PATCH 2/2 btrfs: replace for_each_set_bit() with for_each_set_bitmap()
In reply to Qu WenruoInside extent_io.c, there are several simple call sites doing things like:
for_each_set_bit(bit, bitmap, bitmap_size) {
/* handle one fs block */
}The workload includes:
- set_bit()
Inside extent_writepage_io().
This can be replaced with a bitmap_set().
- btrfs_folio_set_lock()
- btrfs_mark_ordered_io_finished()
Inside writepage_delalloc().
Instead of calling it multiple times, we can pass a range into the function with one call.
--- fs/btrfs/extent_io.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)
fs/btrfs/extent_io.c +16 -10
@@ -1426,8 +1426,9 @@ static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode,u64 delalloc_start = page_start;u64 delalloc_end = page_end;u64 delalloc_to_write =0 ; +unsigned int start_bit; +unsigned int end_bit;int ret =0 ; -int bit;/* Save the dirty bitmap as our submission bitmap will be a subset of it. */ if (btrfs_is_subpage (fs_info, folio)) {
@@ -1437,10 +1438,12 @@ static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode, bio_ctrl->submit_bitmap =1 ; } -for_each_set_bit (bit, &bio_ctrl->submit_bitmap, blocks_per_folio) { -u64 start = page_start + (bit << fs_info->sectorsize_bits); +for_each_set_bitrange (start_bit, end_bit, &bio_ctrl->submit_bitmap, + blocks_per_folio) { +u64 start = page_start + (start_bit << fs_info->sectorsize_bits); +u32 len = (end_bit - start_bit) << fs_info->sectorsize_bits; -btrfs_folio_set_lock (fs_info, folio, start, fs_info->sectorsize); +btrfs_folio_set_lock (fs_info, folio, start, len); }/* Lock all (subpage) delalloc ranges inside the folio first. */
@@ -1557,10 +1560,13 @@ static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode, fs_info->sectorsize_bits, blocks_per_folio); -for_each_set_bit (bit, &bio_ctrl->submit_bitmap, bitmap_size) -btrfs_mark_ordered_io_finished (inode, folio, - page_start + (bit << fs_info->sectorsize_bits), - fs_info->sectorsize, false); +for_each_set_bitrange (start_bit, end_bit, &bio_ctrl->submit_bitmap, + bitmap_size) { +u64 start = page_start + (start_bit << fs_info->sectorsize_bits); +u32 len = (end_bit - start_bit) << fs_info->sectorsize_bits; + +btrfs_mark_ordered_io_finished (inode, folio, start, len, false); + }return ret; } out:
@@ -1728,8 +1734,8 @@ static noinline_for_stack int extent_writepage_io(struct btrfs_inode *inode,return ret; } -for (cur = start; cur < end; cur += fs_info->sectorsize) -set_bit ((cur - folio_start) >> fs_info->sectorsize_bits, &range_bitmap); +bitmap_set (&range_bitmap, (start -folio_pos (folio)) >> fs_info->sectorsize_bits, + len >> fs_info->sectorsize_bits);bitmap_and (&bio_ctrl->submit_bitmap, &bio_ctrl->submit_bitmap, &range_bitmap, blocks_per_folio);
Signature · 1 line
2.52.0
Quoted 6 lines · Qu Wenruo
This is the safer cleanup part extracted from the previous attempt:
https://lore.kernel.org/linux-btrfs/[email protected]/The first patch is to concentrate the error handling of
submit_one_sector()so we do not have two separate error handling.
Quoted 7 lines
The second patch is the convert
for_each_set_bit()to a more efficientfor_each_set_bitmap()so that we can benefit from functions which accept a range other than a single block.Qu Wenruo (2):
btrfs: integrate the error handling ofsubmit_one_sector()
btrfs: replacefor_each_set_bit()withfor_each_set_bitmap()