Factor out common ordered extent flushing code
cover Factor out common ordered extent flushing code
There are a couple of places in the code which need to ensure that a particular range is locked and doesn't have pending ordered extents. Currently this logic is open-coded in said places. This patchset refactors the code such that we now have btrfs_lock_and_flush_ordered_range which encompasses this logic.
Patch 1 introduces a new function which contains the duplicated logic.
Patch 2 replaces occurences of given code pattern in callers
Patch 3 Introduces a micro-op so that unlocks in the function can happen without looking up in the ordered tree. This change is careful to not leak extra extent_state references.
Patches have undergone multiple successful xfstest runs.
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 | 27 +++---------------------- fs/btrfs/file.c | 14 ++----------- fs/btrfs/inode.c | 17 ++-------------- fs/btrfs/ordered-data.c | 44 +++++++++++++++++++++++++++++++++++++++++ fs/btrfs/ordered-data.h | 3 +++ 5 files changed, 54 insertions(+), 51 deletions(-)
Signature · 1 line
2.17.1
PATCH 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 | 32 ++++++++++++++++++++++++++++++++ fs/btrfs/ordered-data.h | 3 +++ 2 files changed, 35 insertions(+)
fs/btrfs/ordered-data.c +32 -0
@@ -954,6 +954,38 @@ 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 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 (BTRFS_I (inode), start, + end - start +1 ); +if (!ordered) +break ; +unlock_extent_cached (tree, start, end, cached_state); +btrfs_start_ordered_extent (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 +3 -0
@@ -188,6 +188,9 @@ 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 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
Re: [PATCH 1/3] btrfs: Implement btrfs_lock_and_flush_ordered_range
Quoted 27 lines · Nikolay Borisov
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.
Signed-off-by Nikolay Borisov <[email protected]>--- fs/btrfs/ordered-data.c | 32 ++++++++++++++++++++++++++++++++ fs/btrfs/ordered-data.h | 3 +++ 2 files changed, 35 insertions(+)fs/btrfs/ordered-data.c +8
-0@@ -954,6 +954,38 @@ 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
fs/btrfs/ordered-data.c int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr, · line 974
Quoted 10 lines
fs/btrfs/ordered-data.c +10
-0+* @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 inode *inode,u64 start,u64 end, +struct extent_state **cached_state) +{
Please use btrfs_inode instead of inode for interfaces that are internal to btrfs. This is not consistent but the plan is to switch everything to btrfs_inode so new code should try to follow that.
PATCH 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 | 27 +++------------------------ fs/btrfs/file.c | 14 ++------------ fs/btrfs/inode.c | 17 ++--------------- 3 files changed, 7 insertions(+), 51 deletions(-)
fs/btrfs/extent_io.c +3 -24
@@ -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 inode *inode = 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,
@@ -3237,21 +3226,11 @@ static int __extent_read_full_page(struct extent_io_tree *tree, unsigned int read_flags) {struct inode *inode = page->mapping->host; -struct btrfs_ordered_extent *ordered;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
@@ -1541,7 +1541,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;
@@ -1554,17 +1553,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->vfs_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
@@ -4985,21 +4985,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, 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 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
@@ -973,14 +973,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 (BTRFS_I (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, ordered,1 );btrfs_put_ordered_extent (ordered); }
Signature · 1 line
2.17.1