Ordered extent flushing refactor
v2 cover Ordered extent flushing refactor
Here is v2 of factoring out common code when flushing ordered extent. The main change in this version is the switch from inode to btrfs_inode for function interfaces as per David's feedback.
Nikolay Borisov (3):
btrfs: Implement btrfs_lock_and_flush_ordered_range
btrfs: Use newly introduced btrfs_lock_and_flush_ordered_range
btrfs: Always use a cached extent_state in
btrfs_lock_and_flush_ordered_range
fs/btrfs/extent_io.c | 29 ++++---------------------- fs/btrfs/file.c | 14 ++----------- fs/btrfs/inode.c | 17 ++-------------- fs/btrfs/ordered-data.c | 45 +++++++++++++++++++++++++++++++++++++++++ fs/btrfs/ordered-data.h | 4 ++++ 5 files changed, 57 insertions(+), 52 deletions(-)
Signature · 1 line
2.17.1
PATCH v2 1/3 btrfs: Implement btrfs_lock_and_flush_ordered_range
There is a certain idiom used in multiple places in btrfs' codebase, dealing with flushing an ordered range. Factor this in a separate function that can be reused. Future patches will replace the existing code with that function.
--- fs/btrfs/ordered-data.c | 33 +++++++++++++++++++++++++++++++++ fs/btrfs/ordered-data.h | 4 ++++ 2 files changed, 37 insertions(+)
fs/btrfs/ordered-data.c +33 -0
@@ -962,6 +962,39 @@ int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,return index; } +/* +* btrfs_flush_ordered_range - Lock the passed range and ensures all pending +* ordered extents in it are run to completion. +* +* @tree: IO tree used for locking out other users of the range +* @inode: Inode whose ordered tree is to be searched +* @start: Beginning of range to flush +* @end: Last byte of range to lock +* @cached_state: If passed, will return the extent state responsible for the +* locked range. It's the caller's responsibility to free the cached state. +* +* This function always returns with the given range locked, ensuring after it's +* called no order extent can be pending. +*/ +void btrfs_lock_and_flush_ordered_range (struct extent_io_tree *tree, +struct btrfs_inode *inode,u64 start, +u64 end, +struct extent_state **cached_state) +{ +struct btrfs_ordered_extent *ordered; + +while (1 ) { +lock_extent_bits (tree, start, end, cached_state); + ordered =btrfs_lookup_ordered_range (inode, start, + end - start +1 ); +if (!ordered) +break ; +unlock_extent_cached (tree, start, end, cached_state); +btrfs_start_ordered_extent (&inode->vfs_inode, ordered,1 ); +btrfs_put_ordered_extent (ordered); + } +} +int __init ordered_data_init (void ) { btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
fs/btrfs/ordered-data.h +4 -0
@@ -188,6 +188,10 @@ u64 btrfs_wait_ordered_extents(struct btrfs_root *root, u64 nr, const u64 range_start, const u64 range_len);u64 btrfs_wait_ordered_roots (struct btrfs_fs_info *fs_info,u64 nr,const u64 range_start,const u64 range_len); +void btrfs_lock_and_flush_ordered_range (struct extent_io_tree *tree, +struct btrfs_inode *inode,u64 start, +u64 end, +struct extent_state **cached_state);int __init ordered_data_init (void );void __cold ordered_data_exit (void );
Signature · 1 line
2.17.1
PATCH v2 2/3 btrfs: Use newly introduced btrfs_lock_and_flush_ordered_range
In reply to Nikolay BorisovThere several functions which open code btrfs_lock_and_flush_ordered_range, just replace them with a call to the function. No functional changes.
--- fs/btrfs/extent_io.c | 29 ++++------------------------- fs/btrfs/file.c | 14 ++------------ fs/btrfs/inode.c | 17 ++--------------- 3 files changed, 8 insertions(+), 52 deletions(-)
fs/btrfs/extent_io.c +4 -25
@@ -3206,21 +3206,10 @@ static inline void contiguous_readpages(struct extent_io_tree *tree,unsigned long *bio_flags, u64 *prev_em_start) { - struct inode *inode; -struct btrfs_ordered_extent *ordered; + struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host);int index; - inode = pages[0 ]->mapping->host; -while (1 ) { -lock_extent (tree, start, end); - ordered =btrfs_lookup_ordered_range (BTRFS_I (inode), start, - end - start +1 ); -if (!ordered) -break ; -unlock_extent (tree, start, end); -btrfs_start_ordered_extent (inode, ordered,1 ); -btrfs_put_ordered_extent (ordered); - } +btrfs_lock_and_flush_ordered_range (tree, inode, start, end,NULL );for (index =0 ; index < nr_pages; index++) { __do_readpage(tree, pages[index], btrfs_get_extent, em_cached,
@@ -3236,22 +3225,12 @@ static int __extent_read_full_page(struct extent_io_tree *tree,unsigned long *bio_flags, unsigned int read_flags) { - struct inode *inode = page->mapping->host; -struct btrfs_ordered_extent *ordered; + struct btrfs_inode *inode = BTRFS_I(page->mapping->host);u64 start =page_offset (page);u64 end = start +PAGE_SIZE -1 ;int ret; -while (1 ) { -lock_extent (tree, start, end); - ordered =btrfs_lookup_ordered_range (BTRFS_I (inode), start, -PAGE_SIZE ); -if (!ordered) -break ; -unlock_extent (tree, start, end); -btrfs_start_ordered_extent (inode, ordered,1 ); -btrfs_put_ordered_extent (ordered); - } +btrfs_lock_and_flush_ordered_range (tree, inode, start, end,NULL ); ret =__do_readpage (tree, page, get_extent,NULL , bio, mirror_num, bio_flags, read_flags,NULL );
fs/btrfs/file.c +2 -12
@@ -1550,7 +1550,6 @@ static noinline int check_can_nocow(struct btrfs_inode *inode, loff_t pos, {struct btrfs_fs_info *fs_info = inode->root->fs_info;struct btrfs_root *root = inode->root; -struct btrfs_ordered_extent *ordered;u64 lockstart, lockend;u64 num_bytes;int ret;
@@ -1563,17 +1562,8 @@ static noinline int check_can_nocow(struct btrfs_inode *inode, loff_t pos, lockend =round_up (pos + *write_bytes, fs_info->sectorsize) -1 ; -while (1 ) { -lock_extent (&inode->io_tree, lockstart, lockend); - ordered =btrfs_lookup_ordered_range (inode, lockstart, - lockend - lockstart +1 ); -if (!ordered) { -break ; - } -unlock_extent (&inode->io_tree, lockstart, lockend); -btrfs_start_ordered_extent (&inode->vfs_inode, ordered,1 ); -btrfs_put_ordered_extent (ordered); - } +btrfs_lock_and_flush_ordered_range (&inode->io_tree, inode, lockstart, + lockend,NULL ); num_bytes = lockend - lockstart +1 ; ret = can_nocow_extent(&inode->vfs_inode, lockstart, &num_bytes,
fs/btrfs/inode.c +2 -15
@@ -5008,21 +5008,8 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)if (size <= hole_start)return 0 ; -while (1 ) { -struct btrfs_ordered_extent *ordered; - -lock_extent_bits (io_tree, hole_start, block_end -1 , - &cached_state); - ordered =btrfs_lookup_ordered_range (BTRFS_I (inode), hole_start, - block_end - hole_start); -if (!ordered) -break ; -unlock_extent_cached (io_tree, hole_start, block_end -1 , - &cached_state); -btrfs_start_ordered_extent (inode, ordered,1 ); -btrfs_put_ordered_extent (ordered); - } - +btrfs_lock_and_flush_ordered_range (io_tree,BTRFS_I (inode), hole_start, + block_end -1 , &cached_state); cur_offset = hole_start;while (1 ) { em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Signature · 1 line
2.17.1
PATCH v2 3/3 btrfs: Always use a cached extent_state in btrfs_lock_and_flush_ordered_range
In reply to Nikolay BorisovIn case no cached_state argument is passed to btrfs_lock_and_flush_ordered_range use one locally in the function. This optimises the case when an ordered extent is found since the unlock function will be able to unlock that state directly without searching for it again.
--- fs/btrfs/ordered-data.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
fs/btrfs/ordered-data.c +15 -3
@@ -982,14 +982,26 @@ void btrfs_lock_and_flush_ordered_range(struct extent_io_tree *tree, struct extent_state **cached_state) {struct btrfs_ordered_extent *ordered; +struct extent_state *cachedp =NULL ; + +if (cached_state) + cachedp = *cached_state;while (1 ) { -lock_extent_bits (tree, start, end, cached_state); +lock_extent_bits (tree, start, end, &cachedp); ordered =btrfs_lookup_ordered_range (inode, start, end - start +1 ); -if (!ordered) +if (!ordered) { +/* +* If no external cached_state has been passed then +* decrement the extra ref taken for cachedp since we +* aren't exposing it outside of this function +*/ +if (!cached_state) +refcount_dec (&cachedp->refs);break ; -unlock_extent_cached (tree, start, end, cached_state); + } +unlock_extent_cached (tree, start, end, &cachedp);btrfs_start_ordered_extent (&inode->vfs_inode, ordered,1 );btrfs_put_ordered_extent (ordered); }
Signature · 1 line
2.17.1
Re: [PATCH v2 3/3] btrfs: Always use a cached extent_state in btrfs_lock_and_flush_ordered_range
Quoted 5 lines · Nikolay Borisov
In case no
cached_stateargument is passed tobtrfs_lock_and_flush_ordered_rangeuse one locally in the function. This optimises the case when an ordered extent is found since the unlock function will be able to unlock that state directly without searching for it again.
This will speed up all callers that previously did not cache the state, right? That can improve bring some improvement, I wonder if the caching can be used in more places, there are still many plain lock_extent calls.
check_can_nocow calls unlock_extent from 2 locations, so passing the cached pointer could help in case the ordered is found and thus unlock happens outside of btrfs_lock_and_flush_ordered_range. Elsewhere it's only the locking part so the cache would have to be passed along, but this might not make sense in all cases. Anyway, that's for another patch.
Quoted 10 lines · Nikolay Borisov
Here is v2 of factoring out common code when flushing ordered extent. The main change in this version is the switch from inode to
btrfs_inodefor function interfaces as per David's feedback.Nikolay Borisov (3):
btrfs: Implementbtrfs_lock_and_flush_ordered_range
btrfs: Use newly introducedbtrfs_lock_and_flush_ordered_range
btrfs: Always use a cachedextent_statein
btrfs_lock_and_flush_ordered_range
You can add
for the whole series. Thanks,
Josef
Quoted 9 lines · Nikolay Borisov
Here is v2 of factoring out common code when flushing ordered extent. The main change in this version is the switch from inode to
btrfs_inodefor function interfaces as per David's feedback.Nikolay Borisov (3):
btrfs: Implementbtrfs_lock_and_flush_ordered_range
btrfs: Use newly introducedbtrfs_lock_and_flush_ordered_range
btrfs: Always use a cachedextent_statein
btrfs_lock_and_flush_ordered_range
1-3 added to 5.3 queue, thanks.