Btrfs: rework outstanding_extents
Subsystems (2)
PATCH v2 1/1 Btrfs: rework outstanding_extents
From: Josef Bacik <[email protected]>
Right now we do a lot of weird hoops around outstanding_extents in order to keep the extent count consistent. This is because we logically transfer the outstanding_extent count from the initial reservation through the set_delalloc_bits. This makes it pretty difficult to get a handle on how and when we need to mess with outstanding_extents.
Fix this by revamping the rules of how we deal with outstanding_extents. Now instead everybody that is holding on to a delalloc extent is required to increase the outstanding extents count for itself. This means we'll have something like this
btrfs_dealloc_reserve_metadata - outstanding_extents = 1
btrfs_set_delalloc - outstanding_extents = 2btrfs_release_delalloc_extents - outstanding_extents = 1
for an initial file write. Now take the append write where we extend an existing delalloc range but still under the maximum extent size
btrfs_delalloc_reserve_metadata - outstanding_extents = 2
btrfs_set_delalloc
btrfs_set_bit_hook - outstanding_extents = 3
btrfs_merge_bit_hook - outstanding_extents = 2btrfs_release_delalloc_extents - outstanding_extnets = 1
In order to make the ordered extent transition we of course must now make ordered extents carry their own outstanding_extent reservation, so for cow_file_range we end up with
btrfs_add_ordered_extent - outstanding_extents = 2clear_extent_bit - outstanding_extents = 1btrfs_remove_ordered_extent - outstanding_extents = 0
This makes all manipulations of outstanding_extents much more explicit. Every successful call to btrfs_reserve_delalloc_metadata _must_ now be combined with btrfs_release_delalloc_extents, even in the error case, as that is the only function that actually modifies the outstanding_extents counter.
The drawback to this is now we are much more likely to have transient cases where outstanding_extents is much larger than it actually should be. This could happen before as we manipulated the delalloc bits, but now it happens basically at every write. This may put more pressure on the ENOSPC flushing code, but I think making this code simpler is worth the cost. I have another change coming to mitigate this side-effect somewhat.
I also added trace points for the counter manipulation. These were used by a bpf script I wrote to help track down leak issues.
---v1->v2:
- make btrfs_block_rsv_release_bytes return the bytes released instead of using a pointer to store it.
- use lockdep_assert_held instead of ASSERT().
fs/btrfs/btrfs_inode.h | 22 +++++++ fs/btrfs/ctree.h | 2 + fs/btrfs/extent-tree.c | 141 ++++++++++++++++++++++++++++--------------- fs/btrfs/file.c | 22 +++---- fs/btrfs/inode-map.c | 3 +- fs/btrfs/inode.c | 114 +++++++++++----------------------- fs/btrfs/ioctl.c | 2 + fs/btrfs/ordered-data.c | 21 ++++++- fs/btrfs/relocation.c | 3 + fs/btrfs/tests/inode-tests.c | 18 ++---- include/trace/events/btrfs.h | 44 ++++++++++++++ 11 files changed, 236 insertions(+), 156 deletions(-)
fs/btrfs/btrfs_inode.h +22 -0
@@ -267,6 +267,28 @@ static inline bool btrfs_is_free_space_inode(struct btrfs_inode *inode)return false; } +static inline void btrfs_mod_outstanding_extents (struct btrfs_inode *inode, +int mod) +{ +ASSERT (spin_is_locked (&inode->lock)); + inode->outstanding_extents += mod; +if (btrfs_is_free_space_inode (inode)) +return ; +trace_btrfs_inode_mod_outstanding_extents (inode->root,btrfs_ino (inode), + mod); +} + +static inline void btrfs_mod_reserved_extents (struct btrfs_inode *inode, +int mod) +{ +ASSERT (spin_is_locked (&inode->lock)); + inode->reserved_extents += mod; +if (btrfs_is_free_space_inode (inode)) +return ; +trace_btrfs_inode_mod_reserved_extents (inode->root,btrfs_ino (inode), + mod); +} +static inline int btrfs_inode_in_log (struct btrfs_inode *inode,u64 generation) {int ret =0 ;
fs/btrfs/ctree.h +2 -0
@@ -2735,6 +2735,8 @@ int btrfs_subvolume_reserve_metadata(struct btrfs_root *root, u64 *qgroup_reserved, bool use_global_rsv);void btrfs_subvolume_release_metadata (struct btrfs_fs_info *fs_info,struct btrfs_block_rsv *rsv); +void btrfs_delalloc_release_extents (struct btrfs_inode *inode,u64 num_bytes); +int btrfs_delalloc_reserve_metadata (struct btrfs_inode *inode,u64 num_bytes);void btrfs_delalloc_release_metadata (struct btrfs_inode *inode,u64 num_bytes); int btrfs_delalloc_reserve_space(struct inode *inode,
fs/btrfs/extent-tree.c +94 -47
230 lines not on this page · raw message
fs/btrfs/file.c +8 -14
@@ -1656,6 +1656,7 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file, } } +WARN_ON (reserve_bytes ==0 ); ret =btrfs_delalloc_reserve_metadata (BTRFS_I (inode), reserve_bytes);if (ret) {
@@ -1679,8 +1680,11 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file, ret =prepare_pages (inode, pages, num_pages, pos, write_bytes, force_page_uptodate); -if (ret) +if (ret) { +btrfs_delalloc_release_extents (BTRFS_I (inode), + reserve_bytes);break ; + } ret = lock_and_cleanup_extent_if_need(BTRFS_I(inode), pages, num_pages, pos, write_bytes, &lockstart,
@@ -1688,6 +1692,8 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,if (ret <0 ) {if (ret == -EAGAIN ) goto again; +btrfs_delalloc_release_extents (BTRFS_I (inode), + reserve_bytes);break ; }else if (ret >0 ) { need_unlock = true;
@@ -1718,23 +1724,10 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file, PAGE_SIZE); } -/* -* If we had a short copy we need to release the excess delaloc -* bytes we reserved. We need to increment outstanding_extents -* because btrfs_delalloc_release_space and -* btrfs_delalloc_release_metadata will decrement it, but -* we still have an outstanding extent for the chunk we actually -* managed to copy. -*/ if (num_sectors > dirty_sectors) {/* release everything except the sectors we dirtied */ release_bytes -= dirty_sectors << fs_info->sb->s_blocksize_bits; -if (copied >0 ) { -spin_lock (&BTRFS_I (inode)->lock); -BTRFS_I (inode)->outstanding_extents++; -spin_unlock (&BTRFS_I (inode)->lock); - }if (only_release_metadata) {btrfs_delalloc_release_metadata (BTRFS_I (inode), release_bytes);
@@ -1760,6 +1753,7 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,unlock_extent_cached (&BTRFS_I (inode)->io_tree, lockstart, lockend, &cached_state,GFP_NOFS ); +btrfs_delalloc_release_extents (BTRFS_I (inode), reserve_bytes);if (ret) {btrfs_drop_pages (pages, num_pages);break ;
fs/btrfs/inode-map.c +2 -1
@@ -500,11 +500,12 @@ int btrfs_save_ino_cache(struct btrfs_root *root, ret =btrfs_prealloc_file_range_trans (inode, trans,0 ,0 , prealloc, prealloc, prealloc, &alloc_hint);if (ret) { -btrfs_delalloc_release_metadata (BTRFS_I (inode), prealloc); +btrfs_delalloc_release_extents (BTRFS_I (inode), prealloc); goto out_put; } ret =btrfs_write_out_ino_cache (root, trans, path, inode); +btrfs_delalloc_release_extents (BTRFS_I (inode), prealloc); out_put:iput (inode); out_release:
fs/btrfs/inode.c +36 -78
272 lines not on this page · raw message
fs/btrfs/ioctl.c +2 -0
@@ -1217,6 +1217,7 @@ static int cluster_pages_for_defrag(struct inode *inode,unlock_page (pages[i]);put_page (pages[i]); } +btrfs_delalloc_release_extents (BTRFS_I (inode), page_cnt <<PAGE_SHIFT );extent_changeset_free (data_reserved);return i_done; out:
@@ -1227,6 +1228,7 @@ static int cluster_pages_for_defrag(struct inode *inode,btrfs_delalloc_release_space (inode, data_reserved, start_index <<PAGE_SHIFT , page_cnt <<PAGE_SHIFT ); +btrfs_delalloc_release_extents (BTRFS_I (inode), page_cnt <<PAGE_SHIFT );extent_changeset_free (data_reserved);return ret;
fs/btrfs/ordered-data.c +19 -2
@@ -242,6 +242,15 @@ static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset, }spin_unlock (&root->ordered_extent_lock); +/* +* We don't need the count_max_extents here, we can assume that all of +* that work has been done at higher layers, so this is truly the +* smallest the extent is going to get. +*/ +spin_lock (&BTRFS_I (inode)->lock); +btrfs_mod_outstanding_extents (BTRFS_I (inode),1 ); +spin_unlock (&BTRFS_I (inode)->lock); +return 0 ; }
@@ -591,11 +600,19 @@ void btrfs_remove_ordered_extent(struct inode *inode, {struct btrfs_fs_info *fs_info =btrfs_sb (inode->i_sb);struct btrfs_ordered_inode_tree *tree; -struct btrfs_root *root =BTRFS_I (inode)->root; +struct btrfs_inode *btrfs_inode =BTRFS_I (inode); +struct btrfs_root *root = btrfs_inode->root;struct rb_node *node;bool dec_pending_ordered = false; - tree = &BTRFS_I (inode)->ordered_tree; +/* This is paired with btrfs_add_ordered_extent. */ +spin_lock (&btrfs_inode->lock); +btrfs_mod_outstanding_extents (btrfs_inode,-1 ); +spin_unlock (&btrfs_inode->lock); +if (root != fs_info->tree_root) +btrfs_delalloc_release_metadata (btrfs_inode, entry->len); + + tree = &btrfs_inode->ordered_tree;spin_lock_irq (&tree->lock); node = &entry->rb_node;rb_erase (node, &tree->tree);
fs/btrfs/relocation.c +3 -0
@@ -3237,6 +3237,8 @@ static int relocate_file_extent_cluster(struct inode *inode,put_page (page);btrfs_delalloc_release_metadata (BTRFS_I (inode),PAGE_SIZE ); +btrfs_delalloc_release_extents (BTRFS_I (inode), +PAGE_SIZE ); ret = -EIO ; goto out; }
@@ -3266,6 +3268,7 @@ static int relocate_file_extent_cluster(struct inode *inode,put_page (page); index++; +btrfs_delalloc_release_extents (BTRFS_I (inode),PAGE_SIZE );balance_dirty_pages_ratelimited (inode->i_mapping);btrfs_throttle (fs_info); }
fs/btrfs/tests/inode-tests.c +4 -14
@@ -968,7 +968,6 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)btrfs_test_inode_set_ops (inode);/* [BTRFS_MAX_EXTENT_SIZE] */ -BTRFS_I (inode)->outstanding_extents++; ret =btrfs_set_extent_delalloc (inode,0 ,BTRFS_MAX_EXTENT_SIZE -1 ,NULL ,0 );if (ret) {
@@ -983,7 +982,6 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize) }/* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */ -BTRFS_I (inode)->outstanding_extents++; ret =btrfs_set_extent_delalloc (inode,BTRFS_MAX_EXTENT_SIZE ,BTRFS_MAX_EXTENT_SIZE + sectorsize -1 ,NULL ,0 );
@@ -1003,7 +1001,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)BTRFS_MAX_EXTENT_SIZE >>1 , (BTRFS_MAX_EXTENT_SIZE >>1 ) + sectorsize -1 ,EXTENT_DELALLOC |EXTENT_DIRTY | -EXTENT_UPTODATE |EXTENT_DO_ACCOUNTING ,0 ,0 , +EXTENT_UPTODATE ,0 ,0 , NULL, GFP_KERNEL);if (ret) {test_msg ("clear_extent_bit returned %d\n" , ret);
@@ -1017,7 +1015,6 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize) }/* [BTRFS_MAX_EXTENT_SIZE][sectorsize] */ -BTRFS_I (inode)->outstanding_extents++; ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE >> 1, (BTRFS_MAX_EXTENT_SIZE >> 1) + sectorsize - 1,
@@ -1035,12 +1032,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)/* * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize HOLE][BTRFS_MAX_EXTENT_SIZE+sectorsize] -* -* I'm artificially adding 2 to outstanding_extents because in the -* buffered IO case we'd add things up as we go, but I don't feel like -* doing that here, this isn't the interesting case we want to test. */ -BTRFS_I (inode)->outstanding_extents +=2 ; ret = btrfs_set_extent_delalloc(inode, BTRFS_MAX_EXTENT_SIZE + 2 * sectorsize, (BTRFS_MAX_EXTENT_SIZE << 1) + 3 * sectorsize - 1,
@@ -1059,7 +1051,6 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)/* * [BTRFS_MAX_EXTENT_SIZE+sectorsize][sectorsize][BTRFS_MAX_EXTENT_SIZE+sectorsize] */ -BTRFS_I (inode)->outstanding_extents++; ret =btrfs_set_extent_delalloc (inode,BTRFS_MAX_EXTENT_SIZE + sectorsize,BTRFS_MAX_EXTENT_SIZE +2 * sectorsize -1 ,NULL ,0 );
@@ -1079,7 +1070,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)BTRFS_MAX_EXTENT_SIZE + sectorsize,BTRFS_MAX_EXTENT_SIZE +2 * sectorsize -1 ,EXTENT_DIRTY |EXTENT_DELALLOC | -EXTENT_DO_ACCOUNTING |EXTENT_UPTODATE ,0 ,0 , +EXTENT_UPTODATE ,0 ,0 , NULL, GFP_KERNEL);if (ret) {test_msg ("clear_extent_bit returned %d\n" , ret);
@@ -1096,7 +1087,6 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)* Refill the hole again just for good measure, because I thought it * might fail and I'd rather satisfy my paranoia at this point. */ -BTRFS_I (inode)->outstanding_extents++; ret =btrfs_set_extent_delalloc (inode,BTRFS_MAX_EXTENT_SIZE + sectorsize,BTRFS_MAX_EXTENT_SIZE +2 * sectorsize -1 ,NULL ,0 );
@@ -1114,7 +1104,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)/* Empty */ ret =clear_extent_bit (&BTRFS_I (inode)->io_tree,0 , (u64)-1 ,EXTENT_DIRTY |EXTENT_DELALLOC | -EXTENT_DO_ACCOUNTING |EXTENT_UPTODATE ,0 ,0 , +EXTENT_UPTODATE ,0 ,0 ,NULL ,GFP_KERNEL );if (ret) {test_msg ("clear_extent_bit returned %d\n" , ret);
@@ -1131,7 +1121,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)if (ret)clear_extent_bit (&BTRFS_I (inode)->io_tree,0 , (u64)-1 ,EXTENT_DIRTY |EXTENT_DELALLOC | -EXTENT_DO_ACCOUNTING |EXTENT_UPTODATE ,0 ,0 , +EXTENT_UPTODATE ,0 ,0 ,NULL ,GFP_KERNEL );iput (inode);btrfs_free_dummy_root (root);
include/trace/events/btrfs.h +44 -0
50 lines not on this page · raw message
Signature · 1 line
2.7.4
Ignore this, I git format-patch'ed the wrong patch.
Josef
Quoted 1 line · Josef Bacik
Ignore this, I git format-patch'ed the wrong patch.
So if you're going to send another version, please split the tracepoints to another patch and update the format strings to the preferred format
https://btrfs.wiki.kernel.org/index.php/Development_notes#Tracepoints