Remaining queue
Subsystems (2)
cover Remaining queue
Here's the updated batch of the remaining queue of patches from me. I've addressed all of the outstanding review feedback for everything and they've been pretty thoroughly tested. Most of the changes are around changelogs and adding comments, as well as switching to lockdep_assert_held from whatever crap I was using before. Thanks,
Josef
PATCH 1/8 Btrfs: rework outstanding_extents
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_delalloc_reserve_metadata - outstanding_extents = 1
btrfs_set_extent_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_extent_delalloc
btrfs_set_bit_hook - outstanding_extents = 3
btrfs_merge_extent_hook - outstanding_extents = 2btrfs_delalloc_release_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_delalloc_reserve_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.
--- fs/btrfs/btrfs_inode.h | 18 ++++++ fs/btrfs/ctree.h | 2 + fs/btrfs/extent-tree.c | 139 ++++++++++++++++++++++++++++--------------- 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 ++---- 10 files changed, 186 insertions(+), 156 deletions(-)
fs/btrfs/btrfs_inode.h +18 -0
@@ -267,6 +267,24 @@ 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) +{ +lockdep_assert_held (&inode->lock); + inode->outstanding_extents += mod; +if (btrfs_is_free_space_inode (inode)) +return ; +} + +static inline void btrfs_mod_reserved_extents (struct btrfs_inode *inode, +int mod) +{ +lockdep_assert_held (&inode->lock); + inode->reserved_extents += mod; +if (btrfs_is_free_space_inode (inode)) +return ; +} +static inline int btrfs_inode_in_log (struct btrfs_inode *inode,u64 generation) {int ret =0 ;
fs/btrfs/ctree.h +2 -0
@@ -2747,6 +2747,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 +92 -47
228 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) {
@@ -1678,8 +1679,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 ; + } extents_locked = lock_and_cleanup_extent_if_need( BTRFS_I(inode), pages,
@@ -1688,6 +1692,8 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,if (extents_locked <0 ) {if (extents_locked == -EAGAIN ) goto again; +btrfs_delalloc_release_extents (BTRFS_I (inode), + reserve_bytes); ret = extents_locked;break ; }
@@ -1716,23 +1722,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);
@@ -1758,6 +1751,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
@@ -3246,6 +3246,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; }
@@ -3275,6 +3277,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);
Signature · 1 line
2.7.5
PATCH 2/8 btrfs: add tracepoints for outstanding extents mods
In reply to Josef BacikThis is handy for tracing problems with modifying the outstanding extents counters.
--- fs/btrfs/btrfs_inode.h | 2 ++ include/trace/events/btrfs.h | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+)
fs/btrfs/btrfs_inode.h +2 -0
@@ -274,6 +274,8 @@ static inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode, 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,
include/trace/events/btrfs.h +21 -0
@@ -1695,6 +1695,27 @@ DEFINE_EVENT(btrfs__prelim_ref, btrfs_prelim_ref_insert,TP_ARGS (fs_info, oldref, newref, tree_size) ); +TRACE_EVENT (btrfs_inode_mod_outstanding_extents, + TP_PROTO(struct btrfs_root *root, u64 ino, int mod), + +TP_ARGS (root, ino, mod), + +TP_STRUCT__entry_btrfs ( + __field( u64, root_objectid ) + __field( u64, ino ) +__field ( int, mod ) + ), + +TP_fast_assign_btrfs (root->fs_info, + __entry->root_objectid = root->objectid; + __entry->ino = ino; + __entry->mod = mod; + ), + +TP_printk_btrfs ("root = %llu(%s) ino = %llu mod = %d" , +show_root_type (__entry->root_objectid), + (unsigned long long )__entry->ino, __entry->mod) +);#endif /* _TRACE_BTRFS_H */ /* This part must be outside protection */
Signature · 1 line
2.7.5
PATCH 3/8 btrfs: make the delalloc block rsv per inode
In reply to Josef BacikThe way we handle delalloc metadata reservations has gotten progressively more complicated over the years. There is so much cruft and weirdness around keeping the reserved count and outstanding counters consistent and handling the error cases that it's impossible to understand.
Fix this by making the delalloc block rsv per-inode. This way we can calculate the actual size of the outstanding metadata reservations every time we make a change, and then reserve the delta based on that amount. This greatly simplifies the code everywhere, and makes the error handling in btrfs_delalloc_reserve_metadata far less terrifying.
--- fs/btrfs/btrfs_inode.h | 27 ++-- fs/btrfs/ctree.h | 5 +- fs/btrfs/delayed-inode.c | 46 +------ fs/btrfs/disk-io.c | 18 ++- fs/btrfs/extent-tree.c | 320 ++++++++++++++++------------------------------- fs/btrfs/inode.c | 18 +-- 6 files changed, 141 insertions(+), 293 deletions(-)
fs/btrfs/btrfs_inode.h +9 -18
@@ -36,14 +36,13 @@#define BTRFS_INODE_ORPHAN_META_RESERVED 1#define BTRFS_INODE_DUMMY 2#define BTRFS_INODE_IN_DEFRAG 3 -#define BTRFS_INODE_DELALLOC_META_RESERVED 4 -#define BTRFS_INODE_HAS_ORPHAN_ITEM 5 -#define BTRFS_INODE_HAS_ASYNC_EXTENT 6 -#define BTRFS_INODE_NEEDS_FULL_SYNC 7 -#define BTRFS_INODE_COPY_EVERYTHING 8 -#define BTRFS_INODE_IN_DELALLOC_LIST 9 -#define BTRFS_INODE_READDIO_NEED_LOCK 10 -#define BTRFS_INODE_HAS_PROPS 11 +#define BTRFS_INODE_HAS_ORPHAN_ITEM 4 +#define BTRFS_INODE_HAS_ASYNC_EXTENT 5 +#define BTRFS_INODE_NEEDS_FULL_SYNC 6 +#define BTRFS_INODE_COPY_EVERYTHING 7 +#define BTRFS_INODE_IN_DELALLOC_LIST 8 +#define BTRFS_INODE_READDIO_NEED_LOCK 9 +#define BTRFS_INODE_HAS_PROPS 10/* in memory btrfs inode */ struct btrfs_inode {
@@ -176,7 +175,8 @@ struct btrfs_inode {* of extent items we've reserved metadata for. */ unsigned outstanding_extents; -unsigned reserved_extents; + +struct btrfs_block_rsv block_rsv;/* * Cached values of inode properties
@@ -278,15 +278,6 @@ static inline void btrfs_mod_outstanding_extents(struct btrfs_inode *inode, mod); } -static inline void btrfs_mod_reserved_extents (struct btrfs_inode *inode, -int mod) -{ -lockdep_assert_held (&inode->lock); - inode->reserved_extents += mod; -if (btrfs_is_free_space_inode (inode)) -return ; -} -static inline int btrfs_inode_in_log (struct btrfs_inode *inode,u64 generation) {int ret =0 ;
fs/btrfs/ctree.h +3 -2
@@ -763,8 +763,6 @@ struct btrfs_fs_info {* delayed dir index item */ struct btrfs_block_rsv global_block_rsv; -/* block reservation for delay allocation */ -struct btrfs_block_rsv delalloc_block_rsv;/* block reservation for metadata operations */ struct btrfs_block_rsv trans_block_rsv;/* block reservation for chunk tree */
@@ -2756,6 +2754,9 @@ int btrfs_delalloc_reserve_space(struct inode *inode,void btrfs_init_block_rsv (struct btrfs_block_rsv *rsv,unsigned short type);struct btrfs_block_rsv *btrfs_alloc_block_rsv (struct btrfs_fs_info *fs_info,unsigned short type); +void btrfs_init_metadata_block_rsv (struct btrfs_fs_info *fs_info, +struct btrfs_block_rsv *rsv, +unsigned short type);void btrfs_free_block_rsv (struct btrfs_fs_info *fs_info,struct btrfs_block_rsv *rsv);void __btrfs_free_block_rsv (struct btrfs_block_rsv *rsv);
fs/btrfs/delayed-inode.c +1 -45
76 lines not on this page · raw message
fs/btrfs/disk-io.c +8 -10
@@ -2447,14 +2447,6 @@ int open_ctree(struct super_block *sb, goto fail_delalloc_bytes; } - fs_info->btree_inode =new_inode (sb); -if (!fs_info->btree_inode) { - err = -ENOMEM ; - goto fail_bio_counter; - } - -mapping_set_gfp_mask (fs_info->btree_inode->i_mapping,GFP_NOFS ); -INIT_RADIX_TREE (&fs_info->fs_roots_radix,GFP_ATOMIC );INIT_RADIX_TREE (&fs_info->buffer_radix,GFP_ATOMIC );INIT_LIST_HEAD (&fs_info->trans_list);
@@ -2487,8 +2479,6 @@ int open_ctree(struct super_block *sb,btrfs_mapping_init (&fs_info->mapping_tree);btrfs_init_block_rsv (&fs_info->global_block_rsv,BTRFS_BLOCK_RSV_GLOBAL ); -btrfs_init_block_rsv (&fs_info->delalloc_block_rsv, -BTRFS_BLOCK_RSV_DELALLOC );btrfs_init_block_rsv (&fs_info->trans_block_rsv,BTRFS_BLOCK_RSV_TRANS );btrfs_init_block_rsv (&fs_info->chunk_block_rsv,BTRFS_BLOCK_RSV_CHUNK );btrfs_init_block_rsv (&fs_info->empty_block_rsv,BTRFS_BLOCK_RSV_EMPTY );
@@ -2517,6 +2507,14 @@ int open_ctree(struct super_block *sb,INIT_LIST_HEAD (&fs_info->ordered_roots);spin_lock_init (&fs_info->ordered_root_lock); + + fs_info->btree_inode =new_inode (sb); +if (!fs_info->btree_inode) { + err = -ENOMEM ; + goto fail_bio_counter; + } +mapping_set_gfp_mask (fs_info->btree_inode->i_mapping,GFP_NOFS ); + fs_info->delayed_root =kmalloc (sizeof (struct btrfs_delayed_root ),GFP_KERNEL );if (!fs_info->delayed_root) {
fs/btrfs/extent-tree.c +109 -211
467 lines not on this page · raw message
fs/btrfs/inode.c +11 -7
@@ -42,6 +42,7 @@#include <linux/blkdev.h> #include <linux/posix_acl_xattr.h> #include <linux/uio.h> +#include <linux/magic.h> #include "ctree.h" #include "disk-io.h" #include "transaction.h"
@@ -315,7 +316,7 @@ static noinline int cow_file_range_inline(struct btrfs_root *root,btrfs_free_path (path);return PTR_ERR (trans); } - trans->block_rsv = &fs_info->delalloc_block_rsv; + trans->block_rsv = &BTRFS_I (inode)->block_rsv;if (compressed_size && compressed_pages) extent_item_size = btrfs_file_extent_calc_inline_size(
@@ -2951,7 +2952,7 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) trans =NULL ; goto out; } - trans->block_rsv = &fs_info->delalloc_block_rsv; + trans->block_rsv = &BTRFS_I (inode)->block_rsv; ret =btrfs_update_inode_fallback (trans, root, inode);if (ret)/* -ENOMEM or corruption */ btrfs_abort_transaction (trans, ret);
@@ -2987,7 +2988,7 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) goto out; } - trans->block_rsv = &fs_info->delalloc_block_rsv; + trans->block_rsv = &BTRFS_I (inode)->block_rsv;if (test_bit (BTRFS_ORDERED_COMPRESSED , &ordered_extent->flags)) compress_type = ordered_extent->compress_type;
@@ -8842,7 +8843,6 @@ static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)if (iov_iter_rw (iter) ==WRITE ) {up_read (&BTRFS_I (inode)->dio_sem); current->journal_info =NULL ; -btrfs_delalloc_release_extents (BTRFS_I (inode), count);if (ret <0 && ret != -EIOCBQUEUED ) {if (dio_data.reserve) btrfs_delalloc_release_space(inode, data_reserved,
@@ -8863,6 +8863,7 @@ static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) }else if (ret >=0 && (size_t )ret < count)btrfs_delalloc_release_space (inode, data_reserved, offset, count - (size_t )ret); +btrfs_delalloc_release_extents (BTRFS_I (inode), count); } out:if (wakeup)
@@ -9427,6 +9428,7 @@ int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,struct inode *btrfs_alloc_inode (struct super_block *sb) { +struct btrfs_fs_info *fs_info =btrfs_sb (sb);struct btrfs_inode *ei;struct inode *inode;
@@ -9453,8 +9455,9 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)spin_lock_init (&ei->lock); ei->outstanding_extents =0 ; - ei->reserved_extents =0 ; - +if (sb->s_magic !=BTRFS_TEST_MAGIC ) +btrfs_init_metadata_block_rsv (fs_info, &ei->block_rsv, +BTRFS_BLOCK_RSV_DELALLOC ); ei->runtime_flags =0 ; ei->prop_compress =BTRFS_COMPRESS_NONE ; ei->defrag_compress =BTRFS_COMPRESS_NONE ;
@@ -9504,8 +9507,9 @@ void btrfs_destroy_inode(struct inode *inode)WARN_ON (!hlist_empty (&inode->i_dentry));WARN_ON (inode->i_data.nrpages); +WARN_ON (BTRFS_I (inode)->block_rsv.reserved); +WARN_ON (BTRFS_I (inode)->block_rsv.size);WARN_ON (BTRFS_I (inode)->outstanding_extents); -WARN_ON (BTRFS_I (inode)->reserved_extents);WARN_ON (BTRFS_I (inode)->delalloc_bytes);WARN_ON (BTRFS_I (inode)->new_delalloc_bytes);WARN_ON (BTRFS_I (inode)->csum_bytes);
Signature · 1 line
2.7.5
PATCH 4/8 btrfs: switch args for comp_*_refs
In reply to Josef BacikMake it more consistent, we want the inserted ref to be compared against what's already in there. This will make the order go from lowest seq -> highest seq, which will make us more likely to make forward progress if there's a seqlock currently held.
--- fs/btrfs/delayed-ref.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
fs/btrfs/delayed-ref.c +4 -4
@@ -40,8 +40,8 @@ struct kmem_cache *btrfs_delayed_extent_op_cachep;/* * compare two delayed tree backrefs with same bytenr and type */ -static int comp_tree_refs (struct btrfs_delayed_tree_ref *ref2, -struct btrfs_delayed_tree_ref *ref1) +static int comp_tree_refs (struct btrfs_delayed_tree_ref *ref1, +struct btrfs_delayed_tree_ref *ref2) {if (ref1->node.type ==BTRFS_TREE_BLOCK_REF_KEY ) {if (ref1->root < ref2->root)
@@ -60,8 +60,8 @@ static int comp_tree_refs(struct btrfs_delayed_tree_ref *ref2,/* * compare two delayed data backrefs with same bytenr and type */ -static int comp_data_refs (struct btrfs_delayed_data_ref *ref2, -struct btrfs_delayed_data_ref *ref1) +static int comp_data_refs (struct btrfs_delayed_data_ref *ref1, +struct btrfs_delayed_data_ref *ref2) {if (ref1->node.type ==BTRFS_EXTENT_DATA_REF_KEY ) {if (ref1->root < ref2->root)
Signature · 1 line
2.7.5
PATCH 5/8 btrfs: add a comp_refs() helper
In reply to Josef BacikInstead of open-coding the delayed ref comparisons, add a helper to do the comparisons generically and use that everywhere. We compare sequence numbers last for following patches.
--- fs/btrfs/delayed-ref.c | 54 ++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 24 deletions(-)
fs/btrfs/delayed-ref.c +30 -24
@@ -85,6 +85,34 @@ static int comp_data_refs(struct btrfs_delayed_data_ref *ref1,return 0 ; } +static int comp_refs (struct btrfs_delayed_ref_node *ref1, +struct btrfs_delayed_ref_node *ref2, +bool check_seq) +{ +int ret =0 ; +if (ref1->type < ref2->type) +return -1 ; +if (ref1->type > ref2->type) +return 1 ; +if (ref1->type ==BTRFS_TREE_BLOCK_REF_KEY || + ref1->type ==BTRFS_SHARED_BLOCK_REF_KEY ) + ret =comp_tree_refs (btrfs_delayed_node_to_tree_ref (ref1), +btrfs_delayed_node_to_tree_ref (ref2)); +else + ret =comp_data_refs (btrfs_delayed_node_to_data_ref (ref1), +btrfs_delayed_node_to_data_ref (ref2)); +if (ret) +return ret; +if (check_seq) { +if (ref1->seq < ref2->seq) +return -1 ; +if (ref1->seq > ref2->seq) +return 1 ; + } +return 0 ; +} + +/* insert a new ref to head ref rbtree */ static struct btrfs_delayed_ref_head *htree_insert (struct rb_root *root,struct rb_node *node)
@@ -217,18 +245,7 @@ static bool merge_ref(struct btrfs_trans_handle *trans,if (seq && next->seq >= seq) goto next; -if (next->type != ref->type) - goto next; - -if ((ref->type ==BTRFS_TREE_BLOCK_REF_KEY || - ref->type ==BTRFS_SHARED_BLOCK_REF_KEY ) && -comp_tree_refs (btrfs_delayed_node_to_tree_ref (ref), -btrfs_delayed_node_to_tree_ref (next))) - goto next; -if ((ref->type ==BTRFS_EXTENT_DATA_REF_KEY || - ref->type ==BTRFS_SHARED_DATA_REF_KEY ) && -comp_data_refs (btrfs_delayed_node_to_data_ref (ref), -btrfs_delayed_node_to_data_ref (next))) +if (comp_refs (ref, next, false)) goto next;if (ref->action == next->action) {
@@ -402,18 +419,7 @@ add_delayed_ref_tail_merge(struct btrfs_trans_handle *trans, exist = list_entry(href->ref_list.prev, struct btrfs_delayed_ref_node, list);/* No need to compare bytenr nor is_head */ -if (exist->type != ref->type || exist->seq != ref->seq) - goto add_tail; - -if ((exist->type ==BTRFS_TREE_BLOCK_REF_KEY || - exist->type ==BTRFS_SHARED_BLOCK_REF_KEY ) && -comp_tree_refs (btrfs_delayed_node_to_tree_ref (exist), -btrfs_delayed_node_to_tree_ref (ref))) - goto add_tail; -if ((exist->type ==BTRFS_EXTENT_DATA_REF_KEY || - exist->type ==BTRFS_SHARED_DATA_REF_KEY ) && -comp_data_refs (btrfs_delayed_node_to_data_ref (exist), -btrfs_delayed_node_to_data_ref (ref))) +if (comp_refs (exist, ref, true)) goto add_tail;/* Now we are sure we can merge */
Signature · 1 line
2.7.5
PATCH 6/8 btrfs: track refs in a rb_tree instead of a list
In reply to Josef BacikIf we get a significant amount of delayed refs for a single block (think modifying multiple snapshots) we can end up spending an ungodly amount of time looping through all of the entries trying to see if they can be merged. This is because we only add them to a list, so we have O(2n) for every ref head. This doesn't make any sense as we likely have refs for different roots, and so they cannot be merged. Tracking in a tree will allow us to break as soon as we hit an entry that doesn't match, making our worst case O(n).
With this we can also merge entries more easily. Before we had to hope that matching refs were on the ends of our list, but with the tree we can search down to exact matches and merge them at insert time.
--- fs/btrfs/backref.c | 5 ++- fs/btrfs/delayed-ref.c | 107 +++++++++++++++++++++++++------------------------ fs/btrfs/delayed-ref.h | 5 +-- fs/btrfs/disk-io.c | 10 +++-- fs/btrfs/extent-tree.c | 21 ++++++---- 5 files changed, 81 insertions(+), 67 deletions(-)
fs/btrfs/backref.c +4 -1
@@ -769,6 +769,7 @@ static int add_delayed_refs(const struct btrfs_fs_info *fs_info,struct btrfs_key key;struct btrfs_key tmp_op_key;struct btrfs_key *op_key =NULL ; +struct rb_node *n;int count;int ret =0 ;
@@ -778,7 +779,9 @@ static int add_delayed_refs(const struct btrfs_fs_info *fs_info, }spin_lock (&head->lock); -list_for_each_entry (node, &head->ref_list, list) { +for (n =rb_first (&head->ref_tree); n; n =rb_next (n)) { + node = rb_entry(n, struct btrfs_delayed_ref_node, + ref_node);if (node->seq > seq)continue ;
fs/btrfs/delayed-ref.c +55 -52
203 lines not on this page · raw message
fs/btrfs/delayed-ref.h +2 -3
@@ -27,8 +27,7 @@#define BTRFS_UPDATE_DELAYED_HEAD 4/* not changing ref count on head ref */ struct btrfs_delayed_ref_node { -/*data/tree ref use list, stored in ref_head->ref_list. */ -struct list_head list; +struct rb_node ref_node;/* * If action is BTRFS_ADD_DELAYED_REF, also link this node to * ref_head->ref_add_list, then we do not need to iterate the
@@ -92,7 +91,7 @@ struct btrfs_delayed_ref_head {struct mutex mutex;spinlock_t lock; -struct list_head ref_list; +struct rb_root ref_tree;/* accumulate add BTRFS_ADD_DELAYED_REF nodes to this ref_add_list. */ struct list_head ref_add_list;
fs/btrfs/disk-io.c +6 -4
@@ -4113,7 +4113,7 @@ static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,while ((node =rb_first (&delayed_refs->href_root)) !=NULL ) {struct btrfs_delayed_ref_head *head; -struct btrfs_delayed_ref_node *tmp; +struct rb_node *n;bool pin_bytes = false; head = rb_entry(node, struct btrfs_delayed_ref_head,
@@ -4129,10 +4129,12 @@ static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,continue ; }spin_lock (&head->lock); -list_for_each_entry_safe_reverse (ref, tmp, &head->ref_list, - list) { +while ((n =rb_first (&head->ref_tree)) !=NULL ) { + ref = rb_entry(n, struct btrfs_delayed_ref_node, + ref_node); ref->in_tree =0 ; -list_del (&ref->list); +rb_erase (&ref->ref_node, &head->ref_tree); +RB_CLEAR_NODE (&ref->ref_node);if (!list_empty (&ref->add_list))list_del (&ref->add_list);atomic_dec (&delayed_refs->num_entries);
fs/btrfs/extent-tree.c +14 -7
@@ -2519,7 +2519,7 @@ select_delayed_ref(struct btrfs_delayed_ref_head *head) {struct btrfs_delayed_ref_node *ref; -if (list_empty (&head->ref_list)) +if (RB_EMPTY_ROOT (&head->ref_tree))return NULL ;/*
@@ -2532,8 +2532,8 @@ select_delayed_ref(struct btrfs_delayed_ref_head *head)return list_first_entry (&head->ref_add_list, struct btrfs_delayed_ref_node, add_list); - ref = list_first_entry(&head->ref_list, struct btrfs_delayed_ref_node, - list); + ref =rb_entry (rb_first (&head->ref_tree), + struct btrfs_delayed_ref_node, ref_node);ASSERT (list_empty (&ref->add_list));return ref; }
@@ -2593,7 +2593,7 @@ static int cleanup_ref_head(struct btrfs_trans_handle *trans,spin_unlock (&head->lock);spin_lock (&delayed_refs->lock);spin_lock (&head->lock); -if (!list_empty (&head->ref_list) || head->extent_op) { +if (!RB_EMPTY_ROOT (&head->ref_tree) || head->extent_op) {spin_unlock (&head->lock);spin_unlock (&delayed_refs->lock);return 1 ;
@@ -2740,7 +2740,8 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, actual_count++; ref->in_tree =0 ; -list_del (&ref->list); +rb_erase (&ref->ref_node, &locked_ref->ref_tree); +RB_CLEAR_NODE (&ref->ref_node);if (!list_empty (&ref->add_list))list_del (&ref->add_list);/*
@@ -3138,6 +3139,7 @@ static noinline int check_delayed_ref(struct btrfs_root *root,struct btrfs_delayed_data_ref *data_ref;struct btrfs_delayed_ref_root *delayed_refs;struct btrfs_transaction *cur_trans; +struct rb_node *node;int ret =0 ; cur_trans = root->fs_info->running_transaction;
@@ -3170,7 +3172,12 @@ static noinline int check_delayed_ref(struct btrfs_root *root,spin_unlock (&delayed_refs->lock);spin_lock (&head->lock); -list_for_each_entry (ref, &head->ref_list, list) { +/* +* XXX: We should replace this with a proper search function in the +* future. +*/ +for (node =rb_first (&head->ref_tree); node; node =rb_next (node)) { + ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);/* If it's a shared ref we know a cross reference exists */ if (ref->type !=BTRFS_EXTENT_DATA_REF_KEY ) { ret =1 ;
@@ -7141,7 +7148,7 @@ static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, goto out_delayed_unlock;spin_lock (&head->lock); -if (!list_empty (&head->ref_list)) +if (!RB_EMPTY_ROOT (&head->ref_tree)) goto out;if (head->extent_op) {
Signature · 1 line
2.7.5
PATCH 7/8 btrfs: don't call btrfs_start_delalloc_roots in flushoncommit
In reply to Josef BacikWe're holding the sb_start_intwrite lock at this point, and doing async filemap_flush of the inodes will result in a deadlock if we freeze the fs during this operation. This is because we could do a btrfs_join_transaction() in the thread we are waiting on which would block at sb_start_intwrite, and thus deadlock. Using writeback_inodes_sb() side steps the problem by not introducing all of these extra locking dependencies.
--- fs/btrfs/transaction.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
fs/btrfs/transaction.c +10 -1
@@ -1916,8 +1916,17 @@ static void cleanup_transaction(struct btrfs_trans_handle *trans,static inline int btrfs_start_delalloc_flush (struct btrfs_fs_info *fs_info) { +/* +* We use writeback_inodes_sb here because if we used +* btrfs_start_delalloc_roots we would deadlock with fs freeze. +* Currently are holding the fs freeze lock, if we do an async flush +* we'll do btrfs_join_transaction() and deadlock because we need to +* wait for the fs freeze lock. Using the direct flushing we benefit +* from already being in a transaction and our join_transaction doesn't +* have to re-take the fs freeze lock. +*/ if (btrfs_test_opt (fs_info,FLUSHONCOMMIT )) -return btrfs_start_delalloc_roots (fs_info,1 ,-1 ); +writeback_inodes_sb (fs_info->sb,WB_REASON_SYNC );return 0 ; }
Signature · 1 line
2.7.5
PATCH 8/8 btrfs: move btrfs_truncate_block out of trans handle
In reply to Josef BacikSince we do a delalloc reserve in btrfs_truncate_block we can deadlock with freeze. If somebody else is trying to allocate metadata for this inode and it gets stuck in start_delalloc_inodes because of freeze we will deadlock. Be safe and move this outside of a trans handle. This also has a side-effect of making sure that we're not leaving stale data behind in the other_encoding or encryption case. Not an issue now since nobody uses it, but it would be a problem in the future.
--- fs/btrfs/inode.c | 119 ++++++++++++++++++++----------------------------------- 1 file changed, 44 insertions(+), 75 deletions(-)
fs/btrfs/inode.c +44 -75
@@ -4357,47 +4357,11 @@ static int truncate_space_check(struct btrfs_trans_handle *trans, } -static int truncate_inline_extent (struct inode *inode, -struct btrfs_path *path, -struct btrfs_key *found_key, -const u64 item_end, -const u64 new_size) -{ -struct extent_buffer *leaf = path->nodes[0 ]; -int slot = path->slots[0 ]; -struct btrfs_file_extent_item *fi; -u32 size = (u32)(new_size - found_key->offset); -struct btrfs_root *root =BTRFS_I (inode)->root; - - fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); - -if (btrfs_file_extent_compression (leaf, fi) !=BTRFS_COMPRESS_NONE ) { -loff_t offset = new_size; -loff_t page_end =ALIGN (offset,PAGE_SIZE ); - -/* -* Zero out the remaining of the last page of our inline extent, -* instead of directly truncating our inline extent here - that -* would be much more complex (decompressing all the data, then -* compressing the truncated data, which might be bigger than -* the size of the inline extent, resize the extent, etc). -* We release the path because to get the page we might need to -* read the extent item from disk (data not in the page cache). -*/ -btrfs_release_path (path); -return btrfs_truncate_block (inode, offset, page_end - offset, -0 ); - } - -btrfs_set_file_extent_ram_bytes (leaf, fi, size); - size =btrfs_file_extent_calc_inline_size (size); -btrfs_truncate_item (root->fs_info, path, size,1 ); - -if (test_bit (BTRFS_ROOT_REF_COWS , &root->state)) -inode_sub_bytes (inode, item_end +1 - new_size); - -return 0 ; -} +/* +* Return this if we need to call truncate_block for the last bit of the +* truncate. +*/ +#define NEED_TRUNCATE_BLOCK 1/* * this can truncate away extent items, csum items and directory items.
@@ -4558,11 +4522,6 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,if (found_type !=BTRFS_EXTENT_DATA_KEY ) goto delete; -if (del_item) - last_size = found_key.offset; -else - last_size = new_size; -if (extent_type !=BTRFS_FILE_EXTENT_INLINE ) {u64 num_dec; extent_start =btrfs_file_extent_disk_bytenr (leaf, fi);
@@ -4604,40 +4563,29 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,*/ if (!del_item &&btrfs_file_extent_encryption (leaf, fi) ==0 && -btrfs_file_extent_other_encoding (leaf, fi) ==0 ) { - +btrfs_file_extent_other_encoding (leaf, fi) ==0 && +btrfs_file_extent_compression (leaf, fi) ==0 ) { +u32 size = (u32)(new_size - found_key.offset); +btrfs_set_file_extent_ram_bytes (leaf, fi, size); + size =btrfs_file_extent_calc_inline_size (size); +btrfs_truncate_item (root->fs_info, path, size,1 ); + }else if (!del_item) {/* -* Need to release path in order to truncate a -* compressed extent. So delete any accumulated -* extent items so far. +* We have to bail so the last_size is set to +* just before this extent. */ -if (btrfs_file_extent_compression (leaf, fi) != -BTRFS_COMPRESS_NONE && pending_del_nr) { - err =btrfs_del_items (trans, root, path, - pending_del_slot, - pending_del_nr); -if (err) { -btrfs_abort_transaction (trans, - err); - goto error; - } - pending_del_nr =0 ; - } + err =NEED_TRUNCATE_BLOCK ; +break ; + } - err =truncate_inline_extent (inode, path, - &found_key, - item_end, - new_size); -if (err) { -btrfs_abort_transaction (trans, err); - goto error; - } - }else if (test_bit (BTRFS_ROOT_REF_COWS , - &root->state)) { +if (test_bit (BTRFS_ROOT_REF_COWS , &root->state))inode_sub_bytes (inode, item_end +1 - new_size); - } } delete: +if (del_item) + last_size = found_key.offset; +else + last_size = new_size;if (del_item) {if (!pending_del_nr) {/* no pending yet, add ourselves */
@@ -9335,12 +9283,12 @@ static int btrfs_truncate(struct inode *inode) ret =btrfs_truncate_inode_items (trans, root, inode, inode->i_size,BTRFS_EXTENT_DATA_KEY ); + trans->block_rsv = &fs_info->trans_block_rsv;if (ret != -ENOSPC && ret != -EAGAIN ) { err = ret;break ; } - trans->block_rsv = &fs_info->trans_block_rsv; ret =btrfs_update_inode (trans, root, inode);if (ret) { err = ret;
@@ -9364,6 +9312,27 @@ static int btrfs_truncate(struct inode *inode) trans->block_rsv = rsv; } +/* +* We can't call btrfs_truncate_block inside a trans handle as we could +* deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know +* we've truncated everything except the last little bit, and can do +* btrfs_truncate_block and then update the disk_i_size. +*/ +if (ret ==NEED_TRUNCATE_BLOCK ) { +btrfs_end_transaction (trans); +btrfs_btree_balance_dirty (fs_info); + + ret =btrfs_truncate_block (inode, inode->i_size,0 ,0 ); +if (ret) + goto out; + trans =btrfs_start_transaction (root,1 ); +if (IS_ERR (trans)) { + ret =PTR_ERR (trans); + goto out; + } +btrfs_ordered_update_i_size (inode, inode->i_size,NULL ); + } +if (ret ==0 && inode->i_nlink >0 ) { trans->block_rsv = root->orphan_block_rsv; ret =btrfs_orphan_del (trans,BTRFS_I (inode));
Signature · 1 line
2.7.5
Quoted 5 lines · Josef Bacik
Here's the updated batch of the remaining queue of patches from me. I've addressed all of the outstanding review feedback for everything and they've been pretty thoroughly tested. Most of the changes are around changelogs and adding comments, as well as switching to
lockdep_assert_heldfrom whatever crap I was using before. Thanks,
Thanks. I've replaced the patches and moved them to the main 4.15 pile. If anybody wants to do a final review and have the tag added, there's still time.