btrfs: move extent_io_tree code and cleanups
Subsystems (2)
cover btrfs: move extent_io_tree code and cleanups
Hello,
In working on extent tree v2 I got really bogged down in trying to sync work between btrfs-progs and the kernel. It basically takes me 3x as long, because there's a lot of different things missing in btrfs-progs, so each patchset has to be done from scratch and tested completely differently.
Additionally there's just a lot of tech-debt in these areas in general. So before tackling the rest of extent-tree-v2 I'm spending some time cleaning up things we all know are terrible. This is the first step in that direction, finishing the separation of the extent_io_tree code from extent_io.c. I started this a while ago, but got bogged down in other things.
This has 3 distinct parts
1. Cleanup the io_failure_record code. This has been tightly integrated into the extent_io_tree code forever without much reason for it. The first part of this series moves that handling into it's own tree, and uses our existing helpers to reduce the code quite a bit.
2. Move the code out of extent_io.c. This is easier than previous code moves because I did a lot of the prep work earlier. Unfortunately there is one big patch that copy and pastes all the core code, since it all depends on itself and would be more annoying to move. However the independent parts were moved piece by piece.
3. A wholesale cleanup of the extent_io_tree code. We have a ton of helpers here, that have all grown a ton of arguments over the years. I've trimmed down the arguments for our core helpers, and hidden the rest internally inside of extent-io-tree.c. Additionally I've cleaned up the lock/unlock extent bit helpers so we only have one lock_extent/unlock_extent variant that gets used everywhere.
I've tested this locally to make sure I didn't break anything. This isn't a simple code move so do please review most of it, the patches that start with "move X" are pure code move patches, but the rest do change things. Thanks,
Josef
Josef Bacik (31):
btrfs: cleanup clean_io_failure
btrfs: unexport internal failrec functions
btrfs: stop using extent_io_tree for io_failure_record's
btrfs: use find_first_extent_bit in btrfs_clean_io_failure
btrfs: separate out the extent state and extent buffer init code
btrfs: separate out the eb and extent state leak helpers
btrfs: temporarily export alloc_extent_state helpers
btrfs: move extent state init and alloc functions to their own file
btrfs: convert BUG_ON(EXTENT_BIT_LOCKED) checks to ASSERT's
btrfs: move simple extent bit helpers out of extent_io.c
btrfs: export wait_extent_bit
btrfs: move the core extent_io_tree code into extent-io-tree.c
btrfs: remove struct tree_entry
btrfs: use next_state instead of rb_next where we can
btrfs: make tree_search return struct extent_state
btrfs: make tree_search_for_insert return extent_state
btrfs: make tree_search_prev_next return extent_state's
btrfs: use next_state/prev_state in merge_state
btrfs: remove temporary exports for extent_state movement
btrfs: move irrelevant prototypes to their appropriate header
btrfs: drop exclusive_bits from set_extent_bit
btrfs: remove the wake argument from clear_extent_bits
btrfs: remove failed_start argument from set_extent_bit
btrfs: drop extent_changeset from set_extent_bit
btrfs: unify the lock/unlock extent variants
btrfs: get rid of track_uptodate
btrfs: get rid of ->dirty_bytes
btrfs: don't clear CTL bits when trying to release extent state
btrfs: replace delete argument with EXTENT_CLEAR_ALL_BITS
btrfs: don't init io tree with private data for non inodes
btrfs: remove is_data_inode() checks in extent-io-tree.c
fs/btrfs/Makefile | 2 +- fs/btrfs/btrfs_inode.h | 3 +- fs/btrfs/compression.c | 11 +- fs/btrfs/disk-io.c | 10 +- fs/btrfs/extent-io-tree.c | 1673 ++++++++++++++++++++++++ fs/btrfs/extent-io-tree.h | 119 +- fs/btrfs/extent_io.c | 2055 ++---------------------------- fs/btrfs/extent_io.h | 14 +- fs/btrfs/extent_map.c | 2 +- fs/btrfs/file-item.c | 2 +- fs/btrfs/file.c | 48 +- fs/btrfs/free-space-cache.c | 24 +- fs/btrfs/inode.c | 159 ++- fs/btrfs/ioctl.c | 24 +- fs/btrfs/misc.h | 35 + fs/btrfs/ordered-data.c | 4 +- fs/btrfs/reflink.c | 10 +- fs/btrfs/relocation.c | 18 +- fs/btrfs/super.c | 17 +- fs/btrfs/tests/extent-io-tests.c | 6 +- fs/btrfs/tests/inode-tests.c | 8 +- fs/btrfs/transaction.c | 4 +- fs/btrfs/tree-log.c | 8 +- include/trace/events/btrfs.h | 1 - 24 files changed, 2077 insertions(+), 2180 deletions(-) create mode 100644 fs/btrfs/extent-io-tree.c
Signature · 1 line
2.26.3
PATCH 1/31 btrfs: cleanup clean_io_failure
This is exported, so rename it to btrfs_clean_io_failure. Additionally we are passing in the io tree's and such from the inode, so instead of doing all that simply pass in the inode itself and get all the components we need directly inside of btrfs_clean_io_failure.
--- fs/btrfs/compression.c | 5 ++--- fs/btrfs/extent-io-tree.h | 6 ++---- fs/btrfs/extent_io.c | 17 +++++++---------- fs/btrfs/inode.c | 7 ++----- 4 files changed, 13 insertions(+), 22 deletions(-)
fs/btrfs/compression.c +2 -3
@@ -183,9 +183,8 @@ static void end_compressed_bio_read(struct btrfs_bio *bbio)if (!status && (!csum || !btrfs_check_data_csum (inode, bbio, offset, bv.bv_page, bv.bv_offset))) { -clean_io_failure (fs_info, &bi->io_failure_tree, - &bi->io_tree, start, bv.bv_page, -btrfs_ino (bi), bv.bv_offset); +btrfs_clean_io_failure (bi, start, bv.bv_page, + bv.bv_offset); }else {int ret;
fs/btrfs/extent-io-tree.h +2 -4
@@ -256,9 +256,7 @@ void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start,int free_io_failure (struct extent_io_tree *failure_tree,struct extent_io_tree *io_tree,struct io_failure_record *rec); -int clean_io_failure (struct btrfs_fs_info *fs_info, -struct extent_io_tree *failure_tree, -struct extent_io_tree *io_tree,u64 start, -struct page *page,u64 ino,unsigned int pg_offset); +int btrfs_clean_io_failure (struct btrfs_inode *inode,u64 start, +struct page *page,unsigned int pg_offset);#endif /* BTRFS_EXTENT_IO_TREE_H */
fs/btrfs/extent_io.c +7 -10
@@ -2431,11 +2431,13 @@ static int prev_mirror(const struct io_failure_record *failrec, int cur_mirror)* each time an IO finishes, we do a fast check in the IO failure tree * to see if we need to process or clean up an io_failure_record */ -int clean_io_failure (struct btrfs_fs_info *fs_info, -struct extent_io_tree *failure_tree, -struct extent_io_tree *io_tree,u64 start, -struct page *page,u64 ino,unsigned int pg_offset) +int btrfs_clean_io_failure (struct btrfs_inode *inode,u64 start, +struct page *page,unsigned int pg_offset) { +struct btrfs_fs_info *fs_info = inode->root->fs_info; +struct extent_io_tree *failure_tree = &inode->io_failure_tree; +struct extent_io_tree *io_tree = &inode->io_tree; +u64 ino =btrfs_ino (inode);u64 private;struct io_failure_record *failrec;struct extent_state *state;
@@ -2963,7 +2965,6 @@ static void end_bio_extent_readpage(struct btrfs_bio *bbio) {struct bio *bio = &bbio->bio;struct bio_vec *bvec; -struct extent_io_tree *tree, *failure_tree;struct processed_extent processed = {0 };/* * The offset to the beginning of a bio, since one bio can never be
@@ -2990,8 +2991,6 @@ static void end_bio_extent_readpage(struct btrfs_bio *bbio)"end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u" , bio->bi_iter.bi_sector, bio->bi_status, bbio->mirror_num); - tree = &BTRFS_I (inode)->io_tree; - failure_tree = &BTRFS_I (inode)->io_failure_tree;/* * We always issue full-sector reads, but if some block in a
@@ -3032,9 +3031,7 @@ static void end_bio_extent_readpage(struct btrfs_bio *bbio)loff_t i_size =i_size_read (inode);pgoff_t end_index = i_size >>PAGE_SHIFT ; -clean_io_failure (BTRFS_I (inode)->root->fs_info, - failure_tree, tree, start, page, -btrfs_ino (BTRFS_I (inode)),0 ); +btrfs_clean_io_failure (BTRFS_I (inode), start, page,0 );/* * Zero out the remaining part if this range straddles
fs/btrfs/inode.c +2 -5
@@ -7997,8 +7997,6 @@ static blk_status_t btrfs_check_read_dio_bio(struct btrfs_dio_private *dip, {struct inode *inode = dip->inode;struct btrfs_fs_info *fs_info =BTRFS_I (inode)->root->fs_info; -struct extent_io_tree *failure_tree = &BTRFS_I (inode)->io_failure_tree; -struct extent_io_tree *io_tree = &BTRFS_I (inode)->io_tree;const bool csum = !(BTRFS_I (inode)->flags &BTRFS_INODE_NODATASUM );blk_status_t err =BLK_STS_OK ;struct bvec_iter iter;
@@ -8011,9 +8009,8 @@ static blk_status_t btrfs_check_read_dio_bio(struct btrfs_dio_private *dip,if (uptodate && (!csum || !btrfs_check_data_csum (inode, bbio, offset, bv.bv_page, bv.bv_offset))) { -clean_io_failure (fs_info, failure_tree, io_tree, start, - bv.bv_page,btrfs_ino (BTRFS_I (inode)), - bv.bv_offset); +btrfs_clean_io_failure (BTRFS_I (inode), start, + bv.bv_page, bv.bv_offset); }else {int ret;
Signature · 1 line
2.26.3
PATCH 2/31 btrfs: unexport internal failrec functions
In reply to Josef BacikThese are internally used functions and are not used outside of extent_io.c.
--- fs/btrfs/extent-io-tree.h | 6 ------ fs/btrfs/extent_io.c | 13 +++++++------ 2 files changed, 7 insertions(+), 12 deletions(-)
fs/btrfs/extent-io-tree.h +0 -6
@@ -248,14 +248,8 @@ bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start, struct extent_state **cached_state);/* This should be reworked in the future and put elsewhere. */ -struct io_failure_record *get_state_failrec (struct extent_io_tree *tree,u64 start); -int set_state_failrec (struct extent_io_tree *tree,u64 start, -struct io_failure_record *failrec);void btrfs_free_io_failure_record (struct btrfs_inode *inode,u64 start,u64 end); -int free_io_failure (struct extent_io_tree *failure_tree, -struct extent_io_tree *io_tree, -struct io_failure_record *rec);int btrfs_clean_io_failure (struct btrfs_inode *inode,u64 start,struct page *page,unsigned int pg_offset);
fs/btrfs/extent_io.c +7 -6
@@ -2163,8 +2163,8 @@ u64 count_range_bits(struct extent_io_tree *tree,* set the private field for a given byte offset in the tree. If there isn't * an extent_state there already, this does nothing. */ -int set_state_failrec (struct extent_io_tree *tree,u64 start, -struct io_failure_record *failrec) +static int set_state_failrec (struct extent_io_tree *tree,u64 start, +struct io_failure_record *failrec) {struct rb_node *node;struct extent_state *state;
@@ -2191,7 +2191,8 @@ int set_state_failrec(struct extent_io_tree *tree, u64 start,return ret; } -struct io_failure_record *get_state_failrec (struct extent_io_tree *tree,u64 start) +static struct io_failure_record *get_state_failrec (struct extent_io_tree *tree, +u64 start) {struct rb_node *node;struct extent_state *state;
@@ -2275,9 +2276,9 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,return bitset; } -int free_io_failure (struct extent_io_tree *failure_tree, -struct extent_io_tree *io_tree, -struct io_failure_record *rec) +static int free_io_failure (struct extent_io_tree *failure_tree, +struct extent_io_tree *io_tree, +struct io_failure_record *rec) {int ret;int err =0 ;
Signature · 1 line
2.26.3
PATCH 3/31 btrfs: stop using extent_io_tree for io_failure_record's
In reply to Josef BacikWe still have this oddity of stashing the io_failure_record in the extent state for the io_failure_tree, which is leftover from when we used to stuff private pointers in extent_io_trees.
However this doesn't make a lot of sense for the io failure records, we can simply use a normal rb_tree for this. This will allow us to further simplify the extent_io_tree code by removing the io_failure_rec pointer from the extent state.
Convert the io_failure_tree to an rb tree + spinlock in the inode, and then use our rb tree simple helpers to insert and find failed records. This greatly cleans up this code and makes it easier to separate out the extent_io_tree code.
--- fs/btrfs/btrfs_inode.h | 3 +- fs/btrfs/extent-io-tree.h | 3 - fs/btrfs/extent_io.c | 164 +++++++++++------------------------ fs/btrfs/extent_io.h | 5 +- fs/btrfs/inode.c | 5 +- fs/btrfs/misc.h | 35 ++++++++ include/trace/events/btrfs.h | 1 - 7 files changed, 96 insertions(+), 120 deletions(-)
fs/btrfs/btrfs_inode.h +2 -1
@@ -94,7 +94,8 @@ struct btrfs_inode {/* special utility tree used to record which mirrors have already been * tried when checksums fail for a given block */ -struct extent_io_tree io_failure_tree; +struct rb_root io_failure_tree; +spinlock_t io_failure_lock;/* * Keep track of where the inode has extent items mapped in order to
fs/btrfs/extent-io-tree.h +0 -3
@@ -56,7 +56,6 @@ enum {IO_TREE_FS_EXCLUDED_EXTENTS ,IO_TREE_BTREE_INODE_IO ,IO_TREE_INODE_IO , -IO_TREE_INODE_IO_FAILURE ,IO_TREE_RELOC_BLOCKS ,IO_TREE_TRANS_DIRTY_PAGES , IO_TREE_ROOT_DIRTY_LOG_PAGES,
@@ -89,8 +88,6 @@ struct extent_state {refcount_t refs;u32 state; -struct io_failure_record *failrec; -#ifdef CONFIG_BTRFS_DEBUG struct list_head leak_list;#endif
fs/btrfs/extent_io.c +53 -111
278 lines not on this page · raw message
fs/btrfs/extent_io.h +4 -1
@@ -254,8 +254,11 @@ int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, int mirror_num);* bio end_io callback is called to indicate things have failed. */ struct io_failure_record { +struct { +struct rb_node rb_node; +u64 bytenr; + };/* Use rb_simple_node for search/insert */ struct page *page; -u64 start;u64 len;u64 logical;int this_mirror;
fs/btrfs/inode.c +2 -3
@@ -8899,6 +8899,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) ei->last_log_commit =0 ;spin_lock_init (&ei->lock); +spin_lock_init (&ei->io_failure_lock); ei->outstanding_extents =0 ;if (sb->s_magic !=BTRFS_TEST_MAGIC ) btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
@@ -8915,12 +8916,10 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) inode = &ei->vfs_inode;extent_map_tree_init (&ei->extent_tree);extent_io_tree_init (fs_info, &ei->io_tree,IO_TREE_INODE_IO , inode); -extent_io_tree_init (fs_info, &ei->io_failure_tree, -IO_TREE_INODE_IO_FAILURE , inode);extent_io_tree_init (fs_info, &ei->file_extent_tree,IO_TREE_INODE_FILE_EXTENT , inode); + ei->io_failure_tree =RB_ROOT ; ei->io_tree.track_uptodate = true; - ei->io_failure_tree.track_uptodate = true;atomic_set (&ei->sync_writers,0 );mutex_init (&ei->log_mutex);btrfs_ordered_inode_tree_init (&ei->ordered_tree);
fs/btrfs/misc.h +35 -0
@@ -88,6 +88,41 @@ static inline struct rb_node *rb_simple_search(struct rb_root *root, u64 bytenr)return NULL ; } +/** +* Search @root from an entry that starts or comes after @bytenr. +* +* @root: the root to search. +* @bytenr: bytenr to search from. +* +* Return the rb_node that start at or after @bytenr. If there is no entry at +* or after @bytner return NULL. +*/ +static inline struct rb_node *rb_simple_search_first (struct rb_root *root, +u64 bytenr) +{ +struct rb_node *node = root->rb_node, *ret =NULL ; +struct rb_simple_node *entry, *ret_entry =NULL ; + +while (node) { + entry = rb_entry(node, struct rb_simple_node, rb_node); + +if (bytenr < entry->bytenr) { +if (!ret || entry->bytenr < ret_entry->bytenr) { + ret = node; + ret_entry = entry; + } + + node = node->rb_left; + }else if (bytenr > entry->bytenr) { + node = node->rb_right; + }else { +return node; + } + } + +return ret; +} +static inline struct rb_node *rb_simple_insert (struct rb_root *root,u64 bytenr,struct rb_node *node) {
include/trace/events/btrfs.h +0 -1
@@ -84,7 +84,6 @@ struct raid56_bio_trace_info; EM( IO_TREE_FS_EXCLUDED_EXTENTS, "EXCLUDED_EXTENTS") \ EM( IO_TREE_BTREE_INODE_IO, "BTREE_INODE_IO") \ EM( IO_TREE_INODE_IO, "INODE_IO") \ - EM( IO_TREE_INODE_IO_FAILURE, "INODE_IO_FAILURE") \ EM( IO_TREE_RELOC_BLOCKS, "RELOC_BLOCKS") \ EM( IO_TREE_TRANS_DIRTY_PAGES, "TRANS_DIRTY_PAGES") \EM (IO_TREE_ROOT_DIRTY_LOG_PAGES ,"ROOT_DIRTY_LOG_PAGES" ) \
Signature · 1 line
2.26.3
PATCH 4/31 btrfs: use find_first_extent_bit in btrfs_clean_io_failure
In reply to Josef BacikCurrently we're using find_first_extent_bit_state to check if our state contains the given failrec range, however this is more of an internal extent_io_tree helper, and is technically unsafe to use because we're accessing the state outside of the extent_io_tree lock.
Instead use the normal helper find_first_extent_bit which returns the range of the extent state we find in find_first_extent_bit_state and use that to do our sanity checking.
--- fs/btrfs/extent_io.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
fs/btrfs/extent_io.c +6 -9
@@ -2395,9 +2395,10 @@ int btrfs_clean_io_failure(struct btrfs_inode *inode, u64 start,struct btrfs_fs_info *fs_info = inode->root->fs_info;struct extent_io_tree *io_tree = &inode->io_tree;u64 ino =btrfs_ino (inode); +u64 locked_start, locked_end;struct io_failure_record *failrec; -struct extent_state *state;int mirror; +int ret; failrec =get_failrec (inode, start);if (IS_ERR (failrec))
@@ -2408,14 +2409,10 @@ int btrfs_clean_io_failure(struct btrfs_inode *inode, u64 start,if (sb_rdonly (fs_info->sb)) goto out; -spin_lock (&io_tree->lock); - state =find_first_extent_bit_state (io_tree, - failrec->bytenr, -EXTENT_LOCKED ); -spin_unlock (&io_tree->lock); - -if (!state || state->start > failrec->bytenr || - state->end < failrec->bytenr + failrec->len -1 ) + ret =find_first_extent_bit (io_tree, failrec->bytenr, &locked_start, + &locked_end,EXTENT_LOCKED ,NULL ); +if (ret || locked_start > failrec->bytenr || + locked_end < failrec->bytenr + failrec->len -1 ) goto out; mirror = failrec->this_mirror;
Signature · 1 line
2.26.3
PATCH 5/31 btrfs: separate out the extent state and extent buffer init code
In reply to Josef BacikIn order to help separate the extent buffer from the extent io tree code we need to break up the init functions.
--- fs/btrfs/extent-io-tree.h | 4 ++-- fs/btrfs/extent_io.c | 21 ++++++++++++++------- fs/btrfs/extent_io.h | 3 +++ fs/btrfs/super.c | 17 ++++++++++++----- 4 files changed, 31 insertions(+), 14 deletions(-)
fs/btrfs/extent-io-tree.h +2 -2
@@ -108,8 +108,8 @@ static inline int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)int try_lock_extent (struct extent_io_tree *tree,u64 start,u64 end); -int __init extent_io_init (void ); -void __cold extent_io_exit (void ); +int __init extent_state_init_cachep (void ); +void __cold extent_state_free_cachep (void ); u64 count_range_bits(struct extent_io_tree *tree, u64 *start, u64 search_end,
fs/btrfs/extent_io.c +14 -7
@@ -230,7 +230,7 @@ static void submit_write_bio(struct extent_page_data *epd, int ret) } } -int __init extent_io_init (void ) +int __init extent_state_init_cachep (void ) { extent_state_cache = kmem_cache_create("btrfs_extent_state", sizeof(struct extent_state), 0,
@@ -238,18 +238,27 @@ int __init extent_io_init(void)if (!extent_state_cache)return -ENOMEM ; +return 0 ; +} + +int __init extent_buffer_init_cachep (void ) +{ extent_buffer_cache =kmem_cache_create ("btrfs_extent_buffer" ,sizeof (struct extent_buffer ),0 ,SLAB_MEM_SPREAD ,NULL ); -if (!extent_buffer_cache) { -kmem_cache_destroy (extent_state_cache); +if (!extent_buffer_cache)return -ENOMEM ; - }return 0 ; } -void __cold extent_io_exit (void ) +void __cold extent_state_free_cachep (void ) +{ +btrfs_extent_state_leak_debug_check (); +kmem_cache_destroy (extent_state_cache); +} + +void __cold extent_buffer_free_cachep (void ) {/* * Make sure all delayed rcu free are flushed before we
@@ -257,8 +266,6 @@ void __cold extent_io_exit(void)*/ rcu_barrier ();kmem_cache_destroy (extent_buffer_cache); -btrfs_extent_state_leak_debug_check (); -kmem_cache_destroy (extent_state_cache); }/*
fs/btrfs/extent_io.h +3 -0
@@ -64,6 +64,9 @@ struct btrfs_fs_info;struct io_failure_record ;struct extent_io_tree ; +int __init extent_buffer_init_cachep (void ); +void __cold extent_buffer_free_cachep (void ); +typedef void (submit_bio_hook_t )(struct inode *inode,struct bio *bio,int mirror_num,enum btrfs_compression_type compress_type);
fs/btrfs/super.c +12 -5
@@ -2736,13 +2736,17 @@ static int __init init_btrfs_fs(void)if (err) goto free_compress; - err =extent_io_init (); + err =extent_state_init_cachep ();if (err) goto free_cachep; + err =extent_buffer_init_cachep (); +if (err) + goto free_extent_cachep; + err =btrfs_bioset_init ();if (err) - goto free_extent_io; + goto free_eb_cachep; err =extent_map_init ();if (err)
@@ -2800,8 +2804,10 @@ static int __init init_btrfs_fs(void)extent_map_exit (); free_bioset:btrfs_bioset_exit (); -free_extent_io: -extent_io_exit (); +free_eb_cachep: +extent_buffer_free_cachep (); +free_extent_cachep: +extent_state_free_cachep (); free_cachep:btrfs_destroy_cachep (); free_compress:
@@ -2821,7 +2827,8 @@ static void __exit exit_btrfs_fs(void)ordered_data_exit ();extent_map_exit ();btrfs_bioset_exit (); -extent_io_exit (); +extent_state_free_cachep (); +extent_buffer_free_cachep ();btrfs_interface_exit ();unregister_filesystem (&btrfs_fs_type);btrfs_exit_sysfs ();
Signature · 1 line
2.26.3
PATCH 6/31 btrfs: separate out the eb and extent state leak helpers
In reply to Josef BacikCurrently we have the add/del functions generic so that we can use them for both extent buffers and extent states. We want to separate this code however, so separate these helpers into per-object helpers in anticipation of the split.
--- fs/btrfs/extent_io.c | 58 +++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 20 deletions(-)
fs/btrfs/extent_io.c +38 -20
@@ -43,25 +43,42 @@ static inline bool extent_state_in_tree(const struct extent_state *state)static LIST_HEAD (states);static DEFINE_SPINLOCK (leak_lock); -static inline void btrfs_leak_debug_add (spinlock_t *lock, -struct list_head *new, -struct list_head *head) +static inline void btrfs_leak_debug_add_eb (struct extent_buffer *eb) +{ +struct btrfs_fs_info *fs_info = eb->fs_info; +unsigned long flags; + +spin_lock_irqsave (&fs_info->eb_leak_lock, flags); +list_add (&eb->leak_list, &fs_info->allocated_ebs); +spin_unlock_irqrestore (&fs_info->eb_leak_lock, flags); +} + +static inline void btrfs_leak_debug_add_state (struct extent_state *state) {unsigned long flags; -spin_lock_irqsave (lock, flags); -list_add (new, head); -spin_unlock_irqrestore (lock, flags); +spin_lock_irqsave (&leak_lock, flags); +list_add (&state->leak_list, &states); +spin_unlock_irqrestore (&leak_lock, flags); +} + +static inline void btrfs_leak_debug_del_eb (struct extent_buffer *eb) +{ +struct btrfs_fs_info *fs_info = eb->fs_info; +unsigned long flags; + +spin_lock_irqsave (&fs_info->eb_leak_lock, flags); +list_del (&eb->leak_list); +spin_unlock_irqrestore (&fs_info->eb_leak_lock, flags); } -static inline void btrfs_leak_debug_del (spinlock_t *lock, -struct list_head *entry) +static inline void btrfs_leak_debug_del_state (struct extent_state *state) {unsigned long flags; -spin_lock_irqsave (lock, flags); -list_del (entry); -spin_unlock_irqrestore (lock, flags); +spin_lock_irqsave (&leak_lock, flags); +list_del (&state->leak_list); +spin_unlock_irqrestore (&leak_lock, flags); }void btrfs_extent_buffer_leak_debug_check (struct btrfs_fs_info *fs_info)
@@ -125,9 +142,11 @@ static inline void __btrfs_debug_check_extent_io_range(const char *caller, } }#else -#define btrfs_leak_debug_add (lock, new, head) do {} while (0) -#define btrfs_leak_debug_del (lock, entry) do {} while (0) -#define btrfs_extent_state_leak_debug_check () do {} while (0) +#define btrfs_leak_debug_add_eb (eb) do {} while (0) +#define btrfs_leak_debug_add_state (state) do {} while (0) +#define btrfs_leak_debug_del_eb (eb) do {} while (0) +#define btrfs_leak_debug_del_state (state) do {} while (0) +#define btrfs_extent_state_leak_debug_check () do {} while (0)#define btrfs_debug_check_extent_io_range (c, s, e) do {} while (0)#endif
@@ -334,7 +353,7 @@ static struct extent_state *alloc_extent_state(gfp_t mask)return state; state->state =0 ;RB_CLEAR_NODE (&state->rb_node); -btrfs_leak_debug_add (&leak_lock, &state->leak_list, &states); +btrfs_leak_debug_add_state (state);refcount_set (&state->refs,1 );init_waitqueue_head (&state->wq);trace_alloc_extent_state (state, mask, _RET_IP_);
@@ -347,7 +366,7 @@ void free_extent_state(struct extent_state *state)return ;if (refcount_dec_and_test (&state->refs)) {WARN_ON (extent_state_in_tree (state)); -btrfs_leak_debug_del (&leak_lock, &state->leak_list); +btrfs_leak_debug_del_state (state);trace_free_extent_state (state, _RET_IP_);kmem_cache_free (extent_state_cache, state); }
@@ -5713,7 +5732,7 @@ static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb)static inline void btrfs_release_extent_buffer (struct extent_buffer *eb) {btrfs_release_extent_buffer_pages (eb); -btrfs_leak_debug_del (&eb->fs_info->eb_leak_lock, &eb->leak_list); +btrfs_leak_debug_del_eb (eb);__free_extent_buffer (eb); }
@@ -5730,8 +5749,7 @@ __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, eb->bflags =0 ;init_rwsem (&eb->lock); -btrfs_leak_debug_add (&fs_info->eb_leak_lock, &eb->leak_list, - &fs_info->allocated_ebs); +btrfs_leak_debug_add_eb (eb);INIT_LIST_HEAD (&eb->release_list);spin_lock_init (&eb->refs_lock);
@@ -6199,7 +6217,7 @@ static int release_extent_buffer(struct extent_buffer *eb)spin_unlock (&eb->refs_lock); } -btrfs_leak_debug_del (&eb->fs_info->eb_leak_lock, &eb->leak_list); +btrfs_leak_debug_del_eb (eb);/* Should be safe to release our pages at this point */ btrfs_release_extent_buffer_pages (eb);#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Signature · 1 line
2.26.3
PATCH 7/31 btrfs: temporarily export alloc_extent_state helpers
In reply to Josef BacikWe're going to move this code in stages, but while we're doing that we need to export these helpers so we can more easily move the code into the new file.
--- fs/btrfs/extent-io-tree.h | 3 +++ fs/btrfs/extent_io.c | 5 ++--- 2 files changed, 5 insertions(+), 3 deletions(-)
fs/btrfs/extent-io-tree.h +3 -0
@@ -250,4 +250,7 @@ void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start,int btrfs_clean_io_failure (struct btrfs_inode *inode,u64 start,struct page *page,unsigned int pg_offset); +struct extent_state *alloc_extent_state_atomic (struct extent_state *prealloc); +struct extent_state *alloc_extent_state (gfp_t mask); +#endif /* BTRFS_EXTENT_IO_TREE_H */
fs/btrfs/extent_io.c +2 -3
@@ -339,7 +339,7 @@ void extent_io_tree_release(struct extent_io_tree *tree)spin_unlock (&tree->lock); } -static struct extent_state *alloc_extent_state (gfp_t mask) +struct extent_state *alloc_extent_state (gfp_t mask) {struct extent_state *state;
@@ -710,8 +710,7 @@ static struct extent_state *clear_state_bit(struct extent_io_tree *tree,return next; } -static struct extent_state * -alloc_extent_state_atomic (struct extent_state *prealloc) +struct extent_state *alloc_extent_state_atomic (struct extent_state *prealloc) {if (!prealloc) prealloc =alloc_extent_state (GFP_ATOMIC );
Signature · 1 line
2.26.3
PATCH 9/31 btrfs: convert BUG_ON(EXTENT_BIT_LOCKED) checks to ASSERT's
In reply to Josef BacikThese should be ASSERT()'s, not BUG_ON()'s.
--- fs/btrfs/extent_io.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/extent_io.c +2 -2
@@ -1293,7 +1293,7 @@ int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,* either fail with -EEXIST or changeset will record the whole * range. */ -BUG_ON (bits &EXTENT_LOCKED ); +ASSERT (!(bits &EXTENT_LOCKED ));return set_extent_bit (tree, start, end, bits,0 ,NULL ,NULL ,GFP_NOFS , changeset);
@@ -1321,7 +1321,7 @@ int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,* Don't support EXTENT_LOCKED case, same reason as * set_record_extent_bits(). */ -BUG_ON (bits &EXTENT_LOCKED ); +ASSERT (!(bits &EXTENT_LOCKED ));return __clear_extent_bit (tree, start, end, bits,0 ,0 ,NULL ,GFP_NOFS , changeset);
Signature · 1 line
2.26.3
PATCH 8/31 btrfs: move extent state init and alloc functions to their own file
In reply to Josef BacikStart cleaning up extent_io.c by moving the extent state code out of it. This patch starts with the extent state allocation code and the extent_io_tree init code.
--- fs/btrfs/Makefile | 2 +- fs/btrfs/extent-io-tree.c | 160 ++++++++++++++++++++++++++++++++++++++ fs/btrfs/extent-io-tree.h | 5 ++ fs/btrfs/extent_io.c | 155 ------------------------------------ 4 files changed, 166 insertions(+), 156 deletions(-) create mode 100644 fs/btrfs/extent-io-tree.c
fs/btrfs/Makefile +1 -1
@@ -31,7 +31,7 @@ btrfs-y += super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \ backref.o ulist.o qgroup.o send.o dev-replace.o raid56.o \ uuid-tree.o props.o free-space-tree.o tree-checker.o space-info.o \ block-rsv.o delalloc-space.o block-group.o discard.o reflink.o \ - subpage.o tree-mod-log.o + subpage.o tree-mod-log.o extent-io-tree.o btrfs-$(CONFIG_BTRFS_FS_POSIX_ACL) += acl.o btrfs-$(CONFIG_BTRFS_FS_CHECK_INTEGRITY) += check-integrity.o
fs/btrfs/extent-io-tree.c new file +160 -0
160 lines not on this page · raw message
fs/btrfs/extent-io-tree.h +5 -0
@@ -253,4 +253,9 @@ int btrfs_clean_io_failure(struct btrfs_inode *inode, u64 start,struct extent_state *alloc_extent_state_atomic (struct extent_state *prealloc);struct extent_state *alloc_extent_state (gfp_t mask); +static inline bool extent_state_in_tree (const struct extent_state *state) +{ +return !RB_EMPTY_NODE (&state->rb_node); +} +#endif /* BTRFS_EXTENT_IO_TREE_H */
fs/btrfs/extent_io.c +0 -155
213 lines not on this page · raw message
Signature · 1 line
2.26.3
PATCH 10/31 btrfs: move simple extent bit helpers out of extent_io.c
In reply to Josef BacikThese are just variants and wrappers around the actual work horses of the extent state. Extract these out of extent_io.c.
--- fs/btrfs/extent-io-tree.c | 45 +++++++++++++++++++++++++++++ fs/btrfs/extent-io-tree.h | 20 +++++++++---- fs/btrfs/extent_io.c | 60 --------------------------------------- 3 files changed, 60 insertions(+), 65 deletions(-)
fs/btrfs/extent-io-tree.c +45 -0
@@ -142,6 +142,51 @@ void free_extent_state(struct extent_state *state) } } +/* wrappers around set/clear extent bit */ +int set_record_extent_bits (struct extent_io_tree *tree,u64 start,u64 end, +u32 bits,struct extent_changeset *changeset) +{ +/* +* We don't support EXTENT_LOCKED yet, as current changeset will +* record any bits changed, so for EXTENT_LOCKED case, it will +* either fail with -EEXIST or changeset will record the whole +* range. +*/ +ASSERT (!(bits &EXTENT_LOCKED )); + +return set_extent_bit (tree, start, end, bits,0 ,NULL ,NULL ,GFP_NOFS , + changeset); +} + +int clear_record_extent_bits (struct extent_io_tree *tree,u64 start,u64 end, +u32 bits,struct extent_changeset *changeset) +{ +/* +* Don't support EXTENT_LOCKED case, same reason as +* set_record_extent_bits(). +*/ +ASSERT (!(bits &EXTENT_LOCKED )); + +return __clear_extent_bit (tree, start, end, bits,0 ,0 ,NULL ,GFP_NOFS , + changeset); +} + +int try_lock_extent (struct extent_io_tree *tree,u64 start,u64 end) +{ +int err; +u64 failed_start; + + err =set_extent_bit (tree, start, end,EXTENT_LOCKED ,EXTENT_LOCKED , + &failed_start,NULL ,GFP_NOFS ,NULL ); +if (err == -EEXIST ) { +if (failed_start > start) +clear_extent_bit (tree, start, failed_start -1 , +EXTENT_LOCKED ,1 ,0 ,NULL ); +return 0 ; + } +return 1 ; +} +void __cold extent_state_free_cachep (void ) {btrfs_extent_state_leak_debug_check ();
fs/btrfs/extent-io-tree.h +15 -5
@@ -120,14 +120,19 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits, int filled, struct extent_state *cached_state);int clear_record_extent_bits (struct extent_io_tree *tree,u64 start,u64 end,u32 bits,struct extent_changeset *changeset); -int clear_extent_bit (struct extent_io_tree *tree,u64 start,u64 end, -u32 bits,int wake,int delete, -struct extent_state **cached);int __clear_extent_bit (struct extent_io_tree *tree,u64 start,u64 end,u32 bits,int wake,int delete,struct extent_state **cached,gfp_t mask,struct extent_changeset *changeset); +static inline int clear_extent_bit (struct extent_io_tree *tree,u64 start, +u64 end,u32 bits,int wake,int delete, +struct extent_state **cached) +{ +return __clear_extent_bit (tree, start, end, bits, wake, delete, + cached,GFP_NOFS ,NULL ); +} +static inline int unlock_extent (struct extent_io_tree *tree,u64 start,u64 end) {return clear_extent_bit (tree, start, end,EXTENT_LOCKED ,1 ,0 ,NULL );
@@ -164,8 +169,13 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits, unsigned exclusive_bits, u64 *failed_start, struct extent_state **cached_state, gfp_t mask, struct extent_changeset *changeset); -int set_extent_bits_nowait (struct extent_io_tree *tree,u64 start,u64 end, -u32 bits); + +static inline int set_extent_bits_nowait (struct extent_io_tree *tree,u64 start, +u64 end,u32 bits) +{ +return set_extent_bit (tree, start, end, bits,0 ,NULL ,NULL , +GFP_NOWAIT ,NULL ); +}static inline int set_extent_bits (struct extent_io_tree *tree,u64 start,u64 end,u32 bits)
fs/btrfs/extent_io.c +0 -60
72 lines not on this page · raw message
Signature · 1 line
2.26.3
PATCH 11/31 btrfs: export wait_extent_bit
In reply to Josef BacikThis is used by the subpage code in addition to lock_extent_bits, so export it so we can move it out of extent_io.c
--- fs/btrfs/extent-io-tree.h | 1 + fs/btrfs/extent_io.c | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/extent-io-tree.h +1 -0
@@ -253,6 +253,7 @@ int extent_invalidate_folio(struct extent_io_tree *tree,bool btrfs_find_delalloc_range (struct extent_io_tree *tree,u64 *start,u64 *end,u64 max_bytes,struct extent_state **cached_state); +void wait_extent_bit (struct extent_io_tree *tree,u64 start,u64 end,u32 bits);/* This should be reworked in the future and put elsewhere. */ void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start,
fs/btrfs/extent_io.c +1 -2
@@ -753,8 +753,7 @@ static void wait_on_state(struct extent_io_tree *tree,* The range [start, end] is inclusive. * The tree lock is taken by this function */ -static void wait_extent_bit (struct extent_io_tree *tree,u64 start,u64 end, -u32 bits) +void wait_extent_bit (struct extent_io_tree *tree,u64 start,u64 end,u32 bits) {struct extent_state *state;struct rb_node *node;
Signature · 1 line
2.26.3
PATCH 12/31 btrfs: move the core extent_io_tree code into extent-io-tree.c
In reply to Josef BacikThis is a big patch unfortunately, all of this code is tightly coupled together. There is no code change at all, it is simply cut and paste from extent_io.c into extent-io-tree.c.
--- fs/btrfs/extent-io-tree.c | 1532 ++++++++++++++++++++++++++++++++++ fs/btrfs/extent_io.c | 1658 ++----------------------------------- 2 files changed, 1596 insertions(+), 1594 deletions(-)
fs/btrfs/extent-io-tree.c +1532 -0
@@ -4,6 +4,8 @@#include <trace/events/btrfs.h> #include "ctree.h" #include "extent-io-tree.h" +#include "btrfs_inode.h" +#include "misc.h" static struct kmem_cache *extent_state_cache;
@@ -43,12 +45,38 @@ static inline void btrfs_extent_state_leak_debug_check(void) kmem_cache_free(extent_state_cache, state); } } + +#define btrfs_debug_check_extent_io_range (tree, start, end) \ + __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end)) +static inline void __btrfs_debug_check_extent_io_range (const char *caller, +struct extent_io_tree *tree,u64 start,u64 end) +{ +struct inode *inode = tree->private_data; +u64 isize; + +if (!inode || !is_data_inode (inode)) +return ; + + isize =i_size_read (inode); +if (end >=PAGE_SIZE && (end %2 ) ==0 && end != isize -1 ) { +btrfs_debug_rl (BTRFS_I (inode)->root->fs_info, +"%s: ino %llu isize %llu odd range [%llu,%llu]" , + caller,btrfs_ino (BTRFS_I (inode)), isize, start, end); + } +}#else #define btrfs_leak_debug_add_state (state) do {} while (0)#define btrfs_leak_debug_del_state (state) do {} while (0)#define btrfs_extent_state_leak_debug_check () do {} while (0) +#define btrfs_debug_check_extent_io_range (c, s, e) do {} while (0)#endif +struct tree_entry { +u64 start; +u64 end; +struct rb_node rb_node; +}; +/* * For the file_extent_tree, we want to hold the inode lock when we lookup and * update the disk_i_size, but lockdep will complain because our io_tree we hold
@@ -142,6 +170,24 @@ void free_extent_state(struct extent_state *state) } } +static int add_extent_changeset (struct extent_state *state,u32 bits, +struct extent_changeset *changeset, +int set) +{ +int ret; + +if (!changeset) +return 0 ; +if (set && (state->state & bits) == bits) +return 0 ; +if (!set && (state->state & bits) ==0 ) +return 0 ; + changeset->bytes_changed += state->end - state->start +1 ; + ret =ulist_add (&changeset->range_changed, state->start, state->end, +GFP_ATOMIC ); +return ret; +} +/* wrappers around set/clear extent bit */ int set_record_extent_bits (struct extent_io_tree *tree,u64 start,u64 end,u32 bits,struct extent_changeset *changeset)
@@ -187,6 +233,1492 @@ int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)return 1 ; } +/** +* Search @tree for an entry that contains @offset. Such entry would have +* entry->start <= offset && entry->end >= offset. +* +* @tree: the tree to search +* @offset: offset that should fall within an entry in @tree +* @node_ret: pointer where new node should be anchored (used when inserting an +* entry in the tree) +* @parent_ret: points to entry which would have been the parent of the entry, +* containing @offset +* +* Return a pointer to the entry that contains @offset byte address and don't change +* @node_ret and @parent_ret. +* +* If no such entry exists, return pointer to entry that ends before @offset +* and fill parameters @node_ret and @parent_ret, ie. does not return NULL. +*/ +static inline struct rb_node *tree_search_for_insert (struct extent_io_tree *tree, +u64 offset, +struct rb_node ***node_ret, +struct rb_node **parent_ret) +{ +struct rb_root *root = &tree->state; +struct rb_node **node = &root->rb_node; +struct rb_node *prev =NULL ; +struct tree_entry *entry; + +while (*node) { + prev = *node; + entry = rb_entry(prev, struct tree_entry, rb_node); + +if (offset < entry->start) + node = &(*node)->rb_left; +else if (offset > entry->end) + node = &(*node)->rb_right; +else +return *node; + } + +if (node_ret) + *node_ret = node; +if (parent_ret) + *parent_ret = prev; + +/* Search neighbors until we find the first one past the end */ +while (prev && offset > entry->end) { + prev =rb_next (prev); + entry = rb_entry(prev, struct tree_entry, rb_node); + } + +return prev; +} + +/* +* Inexact rb-tree search, return the next entry if @offset is not found +*/ +static inline struct rb_node *tree_search (struct extent_io_tree *tree,u64 offset) +{ +return tree_search_for_insert (tree, offset,NULL ,NULL ); +} + +/** +* Search offset in the tree or fill neighbor rbtree node pointers. +* +* @tree: the tree to search +* @offset: offset that should fall within an entry in @tree +* @next_ret: pointer to the first entry whose range ends after @offset +* @prev_ret: pointer to the first entry whose range begins before @offset +* +* Return a pointer to the entry that contains @offset byte address. If no +* such entry exists, then return NULL and fill @prev_ret and @next_ret. +* Otherwise return the found entry and other pointers are left untouched. +*/ +static struct rb_node *tree_search_prev_next (struct extent_io_tree *tree, +u64 offset, +struct rb_node **prev_ret, +struct rb_node **next_ret) +{ +struct rb_root *root = &tree->state; +struct rb_node **node = &root->rb_node; +struct rb_node *prev =NULL ; +struct rb_node *orig_prev =NULL ; +struct tree_entry *entry; + +ASSERT (prev_ret); +ASSERT (next_ret); + +while (*node) { + prev = *node; + entry = rb_entry(prev, struct tree_entry, rb_node); + +if (offset < entry->start) + node = &(*node)->rb_left; +else if (offset > entry->end) + node = &(*node)->rb_right; +else +return *node; + } + + orig_prev = prev; +while (prev && offset > entry->end) { + prev =rb_next (prev); + entry = rb_entry(prev, struct tree_entry, rb_node); + } + *next_ret = prev; + prev = orig_prev; + + entry = rb_entry(prev, struct tree_entry, rb_node); +while (prev && offset < entry->start) { + prev =rb_prev (prev); + entry = rb_entry(prev, struct tree_entry, rb_node); + } + *prev_ret = prev; + +return NULL ; +} + +/* +* utility function to look for merge candidates inside a given range. +* Any extents with matching state are merged together into a single +* extent in the tree. Extents with EXTENT_IO in their state field +* are not merged because the end_io handlers need to be able to do +* operations on them without sleeping (or doing allocations/splits). +* +* This should be called with the tree lock held. +*/ +static void merge_state (struct extent_io_tree *tree, +struct extent_state *state) +{ +struct extent_state *other; +struct rb_node *other_node; + +if (state->state & (EXTENT_LOCKED |EXTENT_BOUNDARY )) +return ; + + other_node =rb_prev (&state->rb_node); +if (other_node) { + other = rb_entry(other_node, struct extent_state, rb_node); +if (other->end == state->start -1 && + other->state == state->state) { +if (tree->private_data && +is_data_inode (tree->private_data)) +btrfs_merge_delalloc_extent (tree->private_data, + state, other); + state->start = other->start; +rb_erase (&other->rb_node, &tree->state); +RB_CLEAR_NODE (&other->rb_node); +free_extent_state (other); + } + } + other_node =rb_next (&state->rb_node); +if (other_node) { + other = rb_entry(other_node, struct extent_state, rb_node); +if (other->start == state->end +1 && + other->state == state->state) { +if (tree->private_data && +is_data_inode (tree->private_data)) +btrfs_merge_delalloc_extent (tree->private_data, + state, other); + state->end = other->end; +rb_erase (&other->rb_node, &tree->state); +RB_CLEAR_NODE (&other->rb_node); +free_extent_state (other); + } + } +} + +static void set_state_bits (struct extent_io_tree *tree, +struct extent_state *state,u32 bits, +struct extent_changeset *changeset); + +/* +* insert an extent_state struct into the tree. 'bits' are set on the +* struct before it is inserted. +* +* This may return -EEXIST if the extent is already there, in which case the +* state struct is freed. +* +* The tree lock is not taken internally. This is a utility function and +* probably isn't what you want to call (see set/clear_extent_bit). +*/ +static int insert_state (struct extent_io_tree *tree, +struct extent_state *state, +u32 bits,struct extent_changeset *changeset) +{ +struct rb_node **node; +struct rb_node *parent; +const u64 end = state->end; + +set_state_bits (tree, state, bits, changeset); + + node = &tree->state.rb_node; +while (*node) { +struct tree_entry *entry; + + parent = *node; + entry = rb_entry(parent, struct tree_entry, rb_node); + +if (end < entry->start) { + node = &(*node)->rb_left; + }else if (end > entry->end) { + node = &(*node)->rb_right; + }else { +btrfs_err (tree->fs_info, +"found node %llu %llu on insert of %llu %llu" , + entry->start, entry->end, state->start, end); +return -EEXIST ; + } + } + +rb_link_node (&state->rb_node, parent, node); +rb_insert_color (&state->rb_node, &tree->state); + +merge_state (tree, state); +return 0 ; +} + +/* +* Insert state to @tree to the location given by @node and @parent. +*/ +static void insert_state_fast (struct extent_io_tree *tree, +struct extent_state *state,struct rb_node **node, +struct rb_node *parent,unsigned bits, +struct extent_changeset *changeset) +{ +set_state_bits (tree, state, bits, changeset); +rb_link_node (&state->rb_node, parent, node); +rb_insert_color (&state->rb_node, &tree->state); +merge_state (tree, state); +} + +/* +* split a given extent state struct in two, inserting the preallocated +* struct 'prealloc' as the newly created second half. 'split' indicates an +* offset inside 'orig' where it should be split. +* +* Before calling, +* the tree has 'orig' at [orig->start, orig->end]. After calling, there +* are two extent state structs in the tree: +* prealloc: [orig->start, split - 1] +* orig: [ split, orig->end ] +* +* The tree locks are not taken by this function. They need to be held +* by the caller. +*/ +static int split_state (struct extent_io_tree *tree,struct extent_state *orig, +struct extent_state *prealloc,u64 split) +{ +struct rb_node *parent =NULL ; +struct rb_node **node; + +if (tree->private_data &&is_data_inode (tree->private_data)) +btrfs_split_delalloc_extent (tree->private_data, orig, split); + + prealloc->start = orig->start; + prealloc->end = split -1 ; + prealloc->state = orig->state; + orig->start = split; + + parent = &orig->rb_node; + node = &parent; +while (*node) { +struct tree_entry *entry; + + parent = *node; + entry = rb_entry(parent, struct tree_entry, rb_node); + +if (prealloc->end < entry->start) { + node = &(*node)->rb_left; + }else if (prealloc->end > entry->end) { + node = &(*node)->rb_right; + }else { +free_extent_state (prealloc); +return -EEXIST ; + } + } + +rb_link_node (&prealloc->rb_node, parent, node); +rb_insert_color (&prealloc->rb_node, &tree->state); + +return 0 ; +} + +static struct extent_state *next_state (struct extent_state *state) +{ +struct rb_node *next =rb_next (&state->rb_node); +if (next) + return rb_entry(next, struct extent_state, rb_node); +else +return NULL ; +} + +/* +* utility function to clear some bits in an extent state struct. +* it will optionally wake up anyone waiting on this state (wake == 1). +* +* If no bits are set on the state struct after clearing things, the +* struct is freed and removed from the tree +*/ +static struct extent_state *clear_state_bit (struct extent_io_tree *tree, +struct extent_state *state, +u32 bits,int wake, +struct extent_changeset *changeset) +{ +struct extent_state *next; +u32 bits_to_clear = bits & ~EXTENT_CTLBITS ; +int ret; + +if ((bits_to_clear &EXTENT_DIRTY ) && (state->state &EXTENT_DIRTY )) { +u64 range = state->end - state->start +1 ; +WARN_ON (range > tree->dirty_bytes); + tree->dirty_bytes -= range; + } + +if (tree->private_data &&is_data_inode (tree->private_data)) +btrfs_clear_delalloc_extent (tree->private_data, state, bits); + + ret =add_extent_changeset (state, bits_to_clear, changeset,0 ); +BUG_ON (ret <0 ); + state->state &= ~bits_to_clear; +if (wake) +wake_up (&state->wq); +if (state->state ==0 ) { + next =next_state (state); +if (extent_state_in_tree (state)) { +rb_erase (&state->rb_node, &tree->state); +RB_CLEAR_NODE (&state->rb_node); +free_extent_state (state); + }else { +WARN_ON (1 ); + } + }else { +merge_state (tree, state); + next =next_state (state); + } +return next; +} + +static void extent_io_tree_panic (struct extent_io_tree *tree,int err) +{ +btrfs_panic (tree->fs_info, err, +"locking error: extent tree was modified by another thread while locked" ); +} + +/* +* clear some bits on a range in the tree. This may require splitting +* or inserting elements in the tree, so the gfp mask is used to +* indicate which allocations or sleeping are allowed. +* +* pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove +* the given range from the tree regardless of state (ie for truncate). +* +* the range [start, end] is inclusive. +* +* This takes the tree lock, and returns 0 on success and < 0 on error. +*/ +int __clear_extent_bit (struct extent_io_tree *tree,u64 start,u64 end, +u32 bits,int wake,int delete, +struct extent_state **cached_state, +gfp_t mask,struct extent_changeset *changeset) +{ +struct extent_state *state; +struct extent_state *cached; +struct extent_state *prealloc =NULL ; +struct rb_node *node; +u64 last_end; +int err; +int clear =0 ; + +btrfs_debug_check_extent_io_range (tree, start, end); +trace_btrfs_clear_extent_bit (tree, start, end - start +1 , bits); + +if (bits &EXTENT_DELALLOC ) + bits |=EXTENT_NORESERVE ; + +if (delete) + bits |= ~EXTENT_CTLBITS ; + +if (bits & (EXTENT_LOCKED |EXTENT_BOUNDARY )) + clear =1 ; +again: +if (!prealloc &&gfpflags_allow_blocking (mask)) { +/* +* Don't care for allocation failure here because we might end +* up not needing the pre-allocated extent state at all, which +* is the case if we only have in the tree extent states that +* cover our input range and don't cover too any other range. +* If we end up needing a new extent state we allocate it later. +*/ + prealloc =alloc_extent_state (mask); + } + +spin_lock (&tree->lock); +if (cached_state) { + cached = *cached_state; + +if (clear) { + *cached_state =NULL ; + cached_state =NULL ; + } + +if (cached &&extent_state_in_tree (cached) && + cached->start <= start && cached->end > start) { +if (clear) +refcount_dec (&cached->refs); + state = cached; + goto hit_next; + } +if (clear) +free_extent_state (cached); + } +/* +* this search will find the extents that end after +* our range starts +*/ + node =tree_search (tree, start); +if (!node) + goto out; + state = rb_entry(node, struct extent_state, rb_node); +hit_next: +if (state->start > end) + goto out; +WARN_ON (state->end < start); + last_end = state->end; + +/* the state doesn't have the wanted bits, go ahead */ +if (!(state->state & bits)) { + state =next_state (state); + goto next; + } + +/* +* | ---- desired range ---- | +* | state | or +* | ------------- state -------------- | +* +* We need to split the extent we found, and may flip +* bits on second half. +* +* If the extent we found extends past our range, we +* just split and search again. It'll get split again +* the next time though. +* +* If the extent we found is inside our range, we clear +* the desired bit on it. +*/ + +if (state->start < start) { + prealloc =alloc_extent_state_atomic (prealloc); +BUG_ON (!prealloc); + err =split_state (tree, state, prealloc, start); +if (err) +extent_io_tree_panic (tree, err); + + prealloc =NULL ; +if (err) + goto out; +if (state->end <= end) { + state =clear_state_bit (tree, state, bits, wake, changeset); + goto next; + } + goto search_again; + } +/* +* | ---- desired range ---- | +* | state | +* We need to split the extent, and clear the bit +* on the first half +*/ +if (state->start <= end && state->end > end) { + prealloc =alloc_extent_state_atomic (prealloc); +BUG_ON (!prealloc); + err =split_state (tree, state, prealloc, end +1 ); +if (err) +extent_io_tree_panic (tree, err); + +if (wake) +wake_up (&state->wq); + +clear_state_bit (tree, prealloc, bits, wake, changeset); + + prealloc =NULL ; + goto out; + } + + state =clear_state_bit (tree, state, bits, wake, changeset); +next: +if (last_end == (u64)-1 ) + goto out; + start = last_end +1 ; +if (start <= end && state && !need_resched ()) + goto hit_next; + +search_again: +if (start > end) + goto out; +spin_unlock (&tree->lock); +if (gfpflags_allow_blocking (mask)) +cond_resched (); + goto again; + +out: +spin_unlock (&tree->lock); +if (prealloc) +free_extent_state (prealloc); + +return 0 ; + +} + +static void wait_on_state (struct extent_io_tree *tree, +struct extent_state *state) + __releases(tree->lock) + __acquires(tree->lock) +{ +DEFINE_WAIT (wait); +prepare_to_wait (&state->wq, &wait,TASK_UNINTERRUPTIBLE ); +spin_unlock (&tree->lock); +schedule (); +spin_lock (&tree->lock); +finish_wait (&state->wq, &wait); +} + +/* +* waits for one or more bits to clear on a range in the state tree. +* The range [start, end] is inclusive. +* The tree lock is taken by this function +*/ +void wait_extent_bit (struct extent_io_tree *tree,u64 start,u64 end,u32 bits) +{ +struct extent_state *state; +struct rb_node *node; + +btrfs_debug_check_extent_io_range (tree, start, end); + +spin_lock (&tree->lock); +again: +while (1 ) { +/* +* this search will find all the extents that end after +* our range starts +*/ + node =tree_search (tree, start); +process_node: +if (!node) +break ; + + state = rb_entry(node, struct extent_state, rb_node); + +if (state->start > end) + goto out; + +if (state->state & bits) { + start = state->start; +refcount_inc (&state->refs); +wait_on_state (tree, state); +free_extent_state (state); + goto again; + } + start = state->end +1 ; + +if (start > end) +break ; + +if (!cond_resched_lock (&tree->lock)) { + node =rb_next (node); + goto process_node; + } + } +out: +spin_unlock (&tree->lock); +} + +static void set_state_bits (struct extent_io_tree *tree, +struct extent_state *state, +u32 bits,struct extent_changeset *changeset) +{ +u32 bits_to_set = bits & ~EXTENT_CTLBITS ; +int ret; + +if (tree->private_data &&is_data_inode (tree->private_data)) +btrfs_set_delalloc_extent (tree->private_data, state, bits); + +if ((bits_to_set &EXTENT_DIRTY ) && !(state->state &EXTENT_DIRTY )) { +u64 range = state->end - state->start +1 ; + tree->dirty_bytes += range; + } + ret =add_extent_changeset (state, bits_to_set, changeset,1 ); +BUG_ON (ret <0 ); + state->state |= bits_to_set; +} + +static void cache_state_if_flags (struct extent_state *state, +struct extent_state **cached_ptr, +unsigned flags) +{ +if (cached_ptr && !(*cached_ptr)) { +if (!flags || (state->state & flags)) { + *cached_ptr = state; +refcount_inc (&state->refs); + } + } +} + +static void cache_state (struct extent_state *state, +struct extent_state **cached_ptr) +{ +return cache_state_if_flags (state, cached_ptr, +EXTENT_LOCKED |EXTENT_BOUNDARY ); +} + +/* +* set some bits on a range in the tree. This may require allocations or +* sleeping, so the gfp mask is used to indicate what is allowed. +* +* If any of the exclusive bits are set, this will fail with -EEXIST if some +* part of the range already has the desired bits set. The start of the +* existing range is returned in failed_start in this case. +* +* [start, end] is inclusive This takes the tree lock. +*/ +int set_extent_bit (struct extent_io_tree *tree,u64 start,u64 end,u32 bits, +u32 exclusive_bits,u64 *failed_start, +struct extent_state **cached_state,gfp_t mask, +struct extent_changeset *changeset) +{ +struct extent_state *state; +struct extent_state *prealloc =NULL ; +struct rb_node *node; +struct rb_node **p; +struct rb_node *parent; +int err =0 ; +u64 last_start; +u64 last_end; + +btrfs_debug_check_extent_io_range (tree, start, end); +trace_btrfs_set_extent_bit (tree, start, end - start +1 , bits); + +if (exclusive_bits) +ASSERT (failed_start); +else +ASSERT (failed_start ==NULL ); +again: +if (!prealloc &&gfpflags_allow_blocking (mask)) { +/* +* Don't care for allocation failure here because we might end +* up not needing the pre-allocated extent state at all, which +* is the case if we only have in the tree extent states that +* cover our input range and don't cover too any other range. +* If we end up needing a new extent state we allocate it later. +*/ + prealloc =alloc_extent_state (mask); + } + +spin_lock (&tree->lock); +if (cached_state && *cached_state) { + state = *cached_state; +if (state->start <= start && state->end > start && +extent_state_in_tree (state)) { + node = &state->rb_node; + goto hit_next; + } + } +/* +* this search will find all the extents that end after +* our range starts. +*/ + node =tree_search_for_insert (tree, start, &p, &parent); +if (!node) { + prealloc =alloc_extent_state_atomic (prealloc); +BUG_ON (!prealloc); + prealloc->start = start; + prealloc->end = end; +insert_state_fast (tree, prealloc, p, parent, bits, changeset); +cache_state (prealloc, cached_state); + prealloc =NULL ; + goto out; + } + state = rb_entry(node, struct extent_state, rb_node); +hit_next: + last_start = state->start; + last_end = state->end; + +/* +* | ---- desired range ---- | +* | state | +* +* Just lock what we found and keep going +*/ +if (state->start == start && state->end <= end) { +if (state->state & exclusive_bits) { + *failed_start = state->start; + err = -EEXIST ; + goto out; + } + +set_state_bits (tree, state, bits, changeset); +cache_state (state, cached_state); +merge_state (tree, state); +if (last_end == (u64)-1 ) + goto out; + start = last_end +1 ; + state =next_state (state); +if (start < end && state && state->start == start && + !need_resched ()) + goto hit_next; + goto search_again; + } + +/* +* | ---- desired range ---- | +* | state | +* or +* | ------------- state -------------- | +* +* We need to split the extent we found, and may flip bits on +* second half. +* +* If the extent we found extends past our +* range, we just split and search again. It'll get split +* again the next time though. +* +* If the extent we found is inside our range, we set the +* desired bit on it. +*/ +if (state->start < start) { +if (state->state & exclusive_bits) { + *failed_start = start; + err = -EEXIST ; + goto out; + } + +/* +* If this extent already has all the bits we want set, then +* skip it, not necessary to split it or do anything with it. +*/ +if ((state->state & bits) == bits) { + start = state->end +1 ; +cache_state (state, cached_state); + goto search_again; + } + + prealloc =alloc_extent_state_atomic (prealloc); +BUG_ON (!prealloc); + err =split_state (tree, state, prealloc, start); +if (err) +extent_io_tree_panic (tree, err); + + prealloc =NULL ; +if (err) + goto out; +if (state->end <= end) { +set_state_bits (tree, state, bits, changeset); +cache_state (state, cached_state); +merge_state (tree, state); +if (last_end == (u64)-1 ) + goto out; + start = last_end +1 ; + state =next_state (state); +if (start < end && state && state->start == start && + !need_resched ()) + goto hit_next; + } + goto search_again; + } +/* +* | ---- desired range ---- | +* | state | or | state | +* +* There's a hole, we need to insert something in it and +* ignore the extent we found. +*/ +if (state->start > start) { +u64 this_end; +if (end < last_start) + this_end = end; +else + this_end = last_start -1 ; + + prealloc =alloc_extent_state_atomic (prealloc); +BUG_ON (!prealloc); + +/* +* Avoid to free 'prealloc' if it can be merged with +* the later extent. +*/ + prealloc->start = start; + prealloc->end = this_end; + err =insert_state (tree, prealloc, bits, changeset); +if (err) +extent_io_tree_panic (tree, err); + +cache_state (prealloc, cached_state); + prealloc =NULL ; + start = this_end +1 ; + goto search_again; + } +/* +* | ---- desired range ---- | +* | state | +* We need to split the extent, and set the bit +* on the first half +*/ +if (state->start <= end && state->end > end) { +if (state->state & exclusive_bits) { + *failed_start = start; + err = -EEXIST ; + goto out; + } + + prealloc =alloc_extent_state_atomic (prealloc); +BUG_ON (!prealloc); + err =split_state (tree, state, prealloc, end +1 ); +if (err) +extent_io_tree_panic (tree, err); + +set_state_bits (tree, prealloc, bits, changeset); +cache_state (prealloc, cached_state); +merge_state (tree, prealloc); + prealloc =NULL ; + goto out; + } + +search_again: +if (start > end) + goto out; +spin_unlock (&tree->lock); +if (gfpflags_allow_blocking (mask)) +cond_resched (); + goto again; + +out: +spin_unlock (&tree->lock); +if (prealloc) +free_extent_state (prealloc); + +return err; + +} + +/** +* convert_extent_bit - convert all bits in a given range from one bit to +* another +* @tree: the io tree to search +* @start: the start offset in bytes +* @end: the end offset in bytes (inclusive) +* @bits: the bits to set in this range +* @clear_bits: the bits to clear in this range +* @cached_state: state that we're going to cache +* +* This will go through and set bits for the given range. If any states exist +* already in this range they are set with the given bit and cleared of the +* clear_bits. This is only meant to be used by things that are mergeable, ie +* converting from say DELALLOC to DIRTY. This is not meant to be used with +* boundary bits like LOCK. +* +* All allocations are done with GFP_NOFS. +*/ +int convert_extent_bit (struct extent_io_tree *tree,u64 start,u64 end, +u32 bits,u32 clear_bits, +struct extent_state **cached_state) +{ +struct extent_state *state; +struct extent_state *prealloc =NULL ; +struct rb_node *node; +struct rb_node **p; +struct rb_node *parent; +int err =0 ; +u64 last_start; +u64 last_end; +bool first_iteration = true; + +btrfs_debug_check_extent_io_range (tree, start, end); +trace_btrfs_convert_extent_bit (tree, start, end - start +1 , bits, + clear_bits); + +again: +if (!prealloc) { +/* +* Best effort, don't worry if extent state allocation fails +* here for the first iteration. We might have a cached state +* that matches exactly the target range, in which case no +* extent state allocations are needed. We'll only know this +* after locking the tree. +*/ + prealloc =alloc_extent_state (GFP_NOFS ); +if (!prealloc && !first_iteration) +return -ENOMEM ; + } + +spin_lock (&tree->lock); +if (cached_state && *cached_state) { + state = *cached_state; +if (state->start <= start && state->end > start && +extent_state_in_tree (state)) { + node = &state->rb_node; + goto hit_next; + } + } + +/* +* this search will find all the extents that end after +* our range starts. +*/ + node =tree_search_for_insert (tree, start, &p, &parent); +if (!node) { + prealloc =alloc_extent_state_atomic (prealloc); +if (!prealloc) { + err = -ENOMEM ; + goto out; + } + prealloc->start = start; + prealloc->end = end; +insert_state_fast (tree, prealloc, p, parent, bits,NULL ); +cache_state (prealloc, cached_state); + prealloc =NULL ; + goto out; + } + state = rb_entry(node, struct extent_state, rb_node); +hit_next: + last_start = state->start; + last_end = state->end; + +/* +* | ---- desired range ---- | +* | state | +* +* Just lock what we found and keep going +*/ +if (state->start == start && state->end <= end) { +set_state_bits (tree, state, bits,NULL ); +cache_state (state, cached_state); + state =clear_state_bit (tree, state, clear_bits,0 ,NULL ); +if (last_end == (u64)-1 ) + goto out; + start = last_end +1 ; +if (start < end && state && state->start == start && + !need_resched ()) + goto hit_next; + goto search_again; + } + +/* +* | ---- desired range ---- | +* | state | +* or +* | ------------- state -------------- | +* +* We need to split the extent we found, and may flip bits on +* second half. +* +* If the extent we found extends past our +* range, we just split and search again. It'll get split +* again the next time though. +* +* If the extent we found is inside our range, we set the +* desired bit on it. +*/ +if (state->start < start) { + prealloc =alloc_extent_state_atomic (prealloc); +if (!prealloc) { + err = -ENOMEM ; + goto out; + } + err =split_state (tree, state, prealloc, start); +if (err) +extent_io_tree_panic (tree, err); + prealloc =NULL ; +if (err) + goto out; +if (state->end <= end) { +set_state_bits (tree, state, bits,NULL ); +cache_state (state, cached_state); + state =clear_state_bit (tree, state, clear_bits,0 ,NULL ); +if (last_end == (u64)-1 ) + goto out; + start = last_end +1 ; +if (start < end && state && state->start == start && + !need_resched ()) + goto hit_next; + } + goto search_again; + } +/* +* | ---- desired range ---- | +* | state | or | state | +* +* There's a hole, we need to insert something in it and +* ignore the extent we found. +*/ +if (state->start > start) { +u64 this_end; +if (end < last_start) + this_end = end; +else + this_end = last_start -1 ; + + prealloc =alloc_extent_state_atomic (prealloc); +if (!prealloc) { + err = -ENOMEM ; + goto out; + } + +/* +* Avoid to free 'prealloc' if it can be merged with +* the later extent. +*/ + prealloc->start = start; + prealloc->end = this_end; + err =insert_state (tree, prealloc, bits,NULL ); +if (err) +extent_io_tree_panic (tree, err); +cache_state (prealloc, cached_state); + prealloc =NULL ; + start = this_end +1 ; + goto search_again; + } +/* +* | ---- desired range ---- | +* | state | +* We need to split the extent, and set the bit +* on the first half +*/ +if (state->start <= end && state->end > end) { + prealloc =alloc_extent_state_atomic (prealloc); +if (!prealloc) { + err = -ENOMEM ; + goto out; + } + + err =split_state (tree, state, prealloc, end +1 ); +if (err) +extent_io_tree_panic (tree, err); + +set_state_bits (tree, prealloc, bits,NULL ); +cache_state (prealloc, cached_state); +clear_state_bit (tree, prealloc, clear_bits,0 ,NULL ); + prealloc =NULL ; + goto out; + } + +search_again: +if (start > end) + goto out; +spin_unlock (&tree->lock); +cond_resched (); + first_iteration = false; + goto again; + +out: +spin_unlock (&tree->lock); +if (prealloc) +free_extent_state (prealloc); + +return err; +} + +/* +* either insert or lock state struct between start and end use mask to tell +* us if waiting is desired. +*/ +int lock_extent_bits (struct extent_io_tree *tree,u64 start,u64 end, +struct extent_state **cached_state) +{ +int err; +u64 failed_start; + +while (1 ) { + err =set_extent_bit (tree, start, end,EXTENT_LOCKED , +EXTENT_LOCKED , &failed_start, + cached_state,GFP_NOFS ,NULL ); +if (err == -EEXIST ) { +wait_extent_bit (tree, failed_start, end,EXTENT_LOCKED ); + start = failed_start; + }else +break ; +WARN_ON (start > end); + } +return err; +} + +/* find the first state struct with 'bits' set after 'start', and +* return it. tree->lock must be held. NULL will returned if +* nothing was found after 'start' +*/ +static struct extent_state * +find_first_extent_bit_state (struct extent_io_tree *tree,u64 start,u32 bits) +{ +struct rb_node *node; +struct extent_state *state; + +/* +* this search will find all the extents that end after +* our range starts. +*/ + node =tree_search (tree, start); +if (!node) + goto out; + +while (1 ) { + state = rb_entry(node, struct extent_state, rb_node); +if (state->end >= start && (state->state & bits)) +return state; + + node =rb_next (node); +if (!node) +break ; + } +out: +return NULL ; +} + +/* +* Find the first offset in the io tree with one or more @bits set. +* +* Note: If there are multiple bits set in @bits, any of them will match. +* +* Return 0 if we find something, and update @start_ret and @end_ret. +* Return 1 if we found nothing. +*/ +int find_first_extent_bit (struct extent_io_tree *tree,u64 start, +u64 *start_ret,u64 *end_ret,u32 bits, +struct extent_state **cached_state) +{ +struct extent_state *state; +int ret =1 ; + +spin_lock (&tree->lock); +if (cached_state && *cached_state) { + state = *cached_state; +if (state->end == start -1 &&extent_state_in_tree (state)) { +while ((state =next_state (state)) !=NULL ) { +if (state->state & bits) + goto got_it; + } +free_extent_state (*cached_state); + *cached_state =NULL ; + goto out; + } +free_extent_state (*cached_state); + *cached_state =NULL ; + } + + state =find_first_extent_bit_state (tree, start, bits); +got_it: +if (state) { +cache_state_if_flags (state, cached_state,0 ); + *start_ret = state->start; + *end_ret = state->end; + ret =0 ; + } +out: +spin_unlock (&tree->lock); +return ret; +} + +/** +* Find a contiguous area of bits +* +* @tree: io tree to check +* @start: offset to start the search from +* @start_ret: the first offset we found with the bits set +* @end_ret: the final contiguous range of the bits that were set +* @bits: bits to look for +* +* set_extent_bit and clear_extent_bit can temporarily split contiguous ranges +* to set bits appropriately, and then merge them again. During this time it +* will drop the tree->lock, so use this helper if you want to find the actual +* contiguous area for given bits. We will search to the first bit we find, and +* then walk down the tree until we find a non-contiguous area. The area +* returned will be the full contiguous area with the bits set. +*/ +int find_contiguous_extent_bit (struct extent_io_tree *tree,u64 start, +u64 *start_ret,u64 *end_ret,u32 bits) +{ +struct extent_state *state; +int ret =1 ; + +spin_lock (&tree->lock); + state =find_first_extent_bit_state (tree, start, bits); +if (state) { + *start_ret = state->start; + *end_ret = state->end; +while ((state =next_state (state)) !=NULL ) { +if (state->start > (*end_ret +1 )) +break ; + *end_ret = state->end; + } + ret =0 ; + } +spin_unlock (&tree->lock); +return ret; +} + +/** +* Find the first range that has @bits not set. This range could start before +* @start. +* +* @tree: the tree to search +* @start: offset at/after which the found extent should start +* @start_ret: records the beginning of the range +* @end_ret: records the end of the range (inclusive) +* @bits: the set of bits which must be unset +* +* Since unallocated range is also considered one which doesn't have the bits +* set it's possible that @end_ret contains -1, this happens in case the range +* spans (last_range_end, end of device]. In this case it's up to the caller to +* trim @end_ret to the appropriate size. +*/ +void find_first_clear_extent_bit (struct extent_io_tree *tree,u64 start, +u64 *start_ret,u64 *end_ret,u32 bits) +{ +struct extent_state *state; +struct rb_node *node, *prev =NULL , *next; + +spin_lock (&tree->lock); + +/* Find first extent with bits cleared */ +while (1 ) { + node =tree_search_prev_next (tree, start, &prev, &next); +if (!node && !next && !prev) { +/* +* Tree is completely empty, send full range and let +* caller deal with it +*/ + *start_ret =0 ; + *end_ret =-1 ; + goto out; + }else if (!node && !next) { +/* +* We are past the last allocated chunk, set start at +* the end of the last extent. +*/ + state = rb_entry(prev, struct extent_state, rb_node); + *start_ret = state->end +1 ; + *end_ret =-1 ; + goto out; + }else if (!node) { + node = next; + } +/* +* At this point 'node' either contains 'start' or start is +* before 'node' +*/ + state = rb_entry(node, struct extent_state, rb_node); + +if (in_range (start, state->start, state->end - state->start +1 )) { +if (state->state & bits) { +/* +* |--range with bits sets--| +* | +* start +*/ + start = state->end +1 ; + }else { +/* +* 'start' falls within a range that doesn't +* have the bits set, so take its start as +* the beginning of the desired range +* +* |--range with bits cleared----| +* | +* start +*/ + *start_ret = state->start; +break ; + } + }else { +/* +* |---prev range---|---hole/unset---|---node range---| +* | +* start +* +* or +* +* |---hole/unset--||--first node--| +* 0 | +* start +*/ +if (prev) { + state = rb_entry(prev, struct extent_state, + rb_node); + *start_ret = state->end +1 ; + }else { + *start_ret =0 ; + } +break ; + } + } + +/* +* Find the longest stretch from start until an entry which has the +* bits set +*/ +while (1 ) { + state = rb_entry(node, struct extent_state, rb_node); +if (state->end >= start && !(state->state & bits)) { + *end_ret = state->end; + }else { + *end_ret = state->start -1 ; +break ; + } + + node =rb_next (node); +if (!node) +break ; + } +out: +spin_unlock (&tree->lock); +} + +/* +* find a contiguous range of bytes in the file marked as delalloc, not +* more than 'max_bytes'. start and end are used to return the range, +* +* true is returned if we find something, false if nothing was in the tree +*/ +bool btrfs_find_delalloc_range (struct extent_io_tree *tree,u64 *start, +u64 *end,u64 max_bytes, +struct extent_state **cached_state) +{ +struct rb_node *node; +struct extent_state *state; +u64 cur_start = *start; +bool found = false; +u64 total_bytes =0 ; + +spin_lock (&tree->lock); + +/* +* this search will find all the extents that end after +* our range starts. +*/ + node =tree_search (tree, cur_start); +if (!node) { + *end = (u64)-1 ; + goto out; + } + +while (1 ) { + state = rb_entry(node, struct extent_state, rb_node); +if (found && (state->start != cur_start || + (state->state &EXTENT_BOUNDARY ))) { + goto out; + } +if (!(state->state &EXTENT_DELALLOC )) { +if (!found) + *end = state->end; + goto out; + } +if (!found) { + *start = state->start; + *cached_state = state; +refcount_inc (&state->refs); + } + found = true; + *end = state->end; + cur_start = state->end +1 ; + node =rb_next (node); + total_bytes += state->end - state->start +1 ; +if (total_bytes >= max_bytes) +break ; +if (!node) +break ; + } +out: +spin_unlock (&tree->lock); +return found; +} + +/* +* count the number of bytes in the tree that have a given bit(s) +* set. This can be fairly slow, except for EXTENT_DIRTY which is +* cached. The total number found is returned. +*/ +u64 count_range_bits (struct extent_io_tree *tree, +u64 *start,u64 search_end,u64 max_bytes, +u32 bits,int contig) +{ +struct rb_node *node; +struct extent_state *state; +u64 cur_start = *start; +u64 total_bytes =0 ; +u64 last =0 ; +int found =0 ; + +if (WARN_ON (search_end <= cur_start)) +return 0 ; + +spin_lock (&tree->lock); +if (cur_start ==0 && bits ==EXTENT_DIRTY ) { + total_bytes = tree->dirty_bytes; + goto out; + } +/* +* this search will find all the extents that end after +* our range starts. +*/ + node =tree_search (tree, cur_start); +if (!node) + goto out; + +while (1 ) { + state = rb_entry(node, struct extent_state, rb_node); +if (state->start > search_end) +break ; +if (contig && found && state->start > last +1 ) +break ; +if (state->end >= cur_start && (state->state & bits) == bits) { + total_bytes +=min (search_end, state->end) +1 - +max (cur_start, state->start); +if (total_bytes >= max_bytes) +break ; +if (!found) { + *start =max (cur_start, state->start); + found =1 ; + } + last = state->end; + }else if (contig && found) { +break ; + } + node =rb_next (node); +if (!node) +break ; + } +out: +spin_unlock (&tree->lock); +return total_bytes; +} + +/* +* searches a range in the state tree for a given mask. +* If 'filled' == 1, this returns 1 only if every extent in the tree +* has the bits set. Otherwise, 1 is returned if any bit in the +* range is found set. +*/ +int test_range_bit (struct extent_io_tree *tree,u64 start,u64 end, +u32 bits,int filled,struct extent_state *cached) +{ +struct extent_state *state =NULL ; +struct rb_node *node; +int bitset =0 ; + +spin_lock (&tree->lock); +if (cached &&extent_state_in_tree (cached) && cached->start <= start && + cached->end > start) + node = &cached->rb_node; +else + node =tree_search (tree, start); +while (node && start <= end) { + state = rb_entry(node, struct extent_state, rb_node); + +if (filled && state->start > start) { + bitset =0 ; +break ; + } + +if (state->start > end) +break ; + +if (state->state & bits) { + bitset =1 ; +if (!filled) +break ; + }else if (filled) { + bitset =0 ; +break ; + } + +if (state->end == (u64)-1 ) +break ; + + start = state->end +1 ; +if (start > end) +break ; + node =rb_next (node); +if (!node) { +if (filled) + bitset =0 ; +break ; + } + } +spin_unlock (&tree->lock); +return bitset; +} +void __cold extent_state_free_cachep (void ) {btrfs_extent_state_leak_debug_check ();
fs/btrfs/extent_io.c +64 -1594
1,438 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.26.3
Part of the diff is not shown here. View the raw message.
PATCH 13/31 btrfs: remove struct tree_entry
In reply to Josef BacikThis existed when we overloaded the tree manipulation functions for both the extent_io_tree and the extent buffer tree. However the extent buffers are now stored in a radix tree, so we no longer need this abstraction. Remove struct tree_entry and use extent_state directly instead.
--- fs/btrfs/extent-io-tree.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-)
fs/btrfs/extent-io-tree.c +12 -18
@@ -71,12 +71,6 @@ static inline void __btrfs_debug_check_extent_io_range(const char *caller,#define btrfs_debug_check_extent_io_range (c, s, e) do {} while (0)#endif -struct tree_entry { -u64 start; -u64 end; -struct rb_node rb_node; -}; -/* * For the file_extent_tree, we want to hold the inode lock when we lookup and * update the disk_i_size, but lockdep will complain because our io_tree we hold
@@ -258,11 +252,11 @@ static inline struct rb_node *tree_search_for_insert(struct extent_io_tree *treestruct rb_root *root = &tree->state;struct rb_node **node = &root->rb_node;struct rb_node *prev =NULL ; -struct tree_entry *entry; +struct extent_state *entry;while (*node) { prev = *node; - entry = rb_entry(prev, struct tree_entry, rb_node); + entry = rb_entry(prev, struct extent_state, rb_node);if (offset < entry->start) node = &(*node)->rb_left;
@@ -280,7 +274,7 @@ static inline struct rb_node *tree_search_for_insert(struct extent_io_tree *tree/* Search neighbors until we find the first one past the end */ while (prev && offset > entry->end) { prev =rb_next (prev); - entry = rb_entry(prev, struct tree_entry, rb_node); + entry = rb_entry(prev, struct extent_state, rb_node); }return prev;
@@ -315,14 +309,14 @@ static struct rb_node *tree_search_prev_next(struct extent_io_tree *tree,struct rb_node **node = &root->rb_node;struct rb_node *prev =NULL ;struct rb_node *orig_prev =NULL ; -struct tree_entry *entry; +struct extent_state *entry;ASSERT (prev_ret);ASSERT (next_ret);while (*node) { prev = *node; - entry = rb_entry(prev, struct tree_entry, rb_node); + entry = rb_entry(prev, struct extent_state, rb_node);if (offset < entry->start) node = &(*node)->rb_left;
@@ -335,15 +329,15 @@ static struct rb_node *tree_search_prev_next(struct extent_io_tree *tree, orig_prev = prev;while (prev && offset > entry->end) { prev =rb_next (prev); - entry = rb_entry(prev, struct tree_entry, rb_node); + entry = rb_entry(prev, struct extent_state, rb_node); } *next_ret = prev; prev = orig_prev; - entry = rb_entry(prev, struct tree_entry, rb_node); + entry = rb_entry(prev, struct extent_state, rb_node);while (prev && offset < entry->start) { prev =rb_prev (prev); - entry = rb_entry(prev, struct tree_entry, rb_node); + entry = rb_entry(prev, struct extent_state, rb_node); } *prev_ret = prev;
@@ -426,10 +420,10 @@ static int insert_state(struct extent_io_tree *tree, node = &tree->state.rb_node;while (*node) { -struct tree_entry *entry; +struct extent_state *entry; parent = *node; - entry = rb_entry(parent, struct tree_entry, rb_node); + entry = rb_entry(parent, struct extent_state, rb_node);if (end < entry->start) { node = &(*node)->rb_left;
@@ -495,10 +489,10 @@ static int split_state(struct extent_io_tree *tree, struct extent_state *orig, parent = &orig->rb_node; node = &parent;while (*node) { -struct tree_entry *entry; +struct extent_state *entry; parent = *node; - entry = rb_entry(parent, struct tree_entry, rb_node); + entry = rb_entry(parent, struct extent_state, rb_node);if (prealloc->end < entry->start) { node = &(*node)->rb_left;
Signature · 1 line
2.26.3
PATCH 15/31 btrfs: make tree_search return struct extent_state
In reply to Josef BacikWe have a consistent pattern of
n = tree_search();
if (!n)
goto out;
state = rb_entry(n, struct extent_state, rb_node);
while (state) {
/* do something. */
}which is a bit redundant. If we make tree_search return the state we can simply have
state = tree_search();
while (state) {
/* do something. */
}which cleans up the code quite a bit.
--- fs/btrfs/extent-io-tree.c | 51 +++++++++++---------------------------- 1 file changed, 14 insertions(+), 37 deletions(-)
fs/btrfs/extent-io-tree.c +14 -37
@@ -283,9 +283,10 @@ static inline struct rb_node *tree_search_for_insert(struct extent_io_tree *tree/* * Inexact rb-tree search, return the next entry if @offset is not found */ -static inline struct rb_node *tree_search (struct extent_io_tree *tree,u64 offset) +static inline struct extent_state *tree_search (struct extent_io_tree *tree,u64 offset) { -return tree_search_for_insert (tree, offset,NULL ,NULL ); +struct rb_node *n =tree_search_for_insert (tree, offset,NULL ,NULL ); + return (n) ? rb_entry(n, struct extent_state, rb_node) : NULL; }/**
@@ -591,7 +592,6 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,struct extent_state *state;struct extent_state *cached;struct extent_state *prealloc =NULL ; -struct rb_node *node;u64 last_end;int err;int clear =0 ;
@@ -642,10 +642,9 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,* this search will find the extents that end after * our range starts */ - node =tree_search (tree, start); -if (!node) + state =tree_search (tree, start); +if (!state) goto out; - state = rb_entry(node, struct extent_state, rb_node); hit_next:if (state->start > end) goto out;
@@ -758,7 +757,6 @@ static void wait_on_state(struct extent_io_tree *tree,void wait_extent_bit (struct extent_io_tree *tree,u64 start,u64 end,u32 bits) {struct extent_state *state; -struct rb_node *node;btrfs_debug_check_extent_io_range (tree, start, end);
@@ -769,11 +767,10 @@ void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits)* this search will find all the extents that end after * our range starts */ - node =tree_search (tree, start); -if (!node) -break ; - state = rb_entry(node, struct extent_state, rb_node); + state =tree_search (tree, start); process_node: +if (!state) +break ;if (state->start > end) goto out;
@@ -1313,25 +1310,18 @@ int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,static struct extent_state *find_first_extent_bit_state (struct extent_io_tree *tree,u64 start,u32 bits) { -struct rb_node *node;struct extent_state *state;/* * this search will find all the extents that end after * our range starts. */ - node =tree_search (tree, start); -if (!node) - goto out; - state = rb_entry(node, struct extent_state, rb_node); - + state =tree_search (tree, start);while (state) { - state = rb_entry(node, struct extent_state, rb_node);if (state->end >= start && (state->state & bits))return state; state =next_state (state); } -out:return NULL ; }
@@ -1541,7 +1531,6 @@ bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start, u64 *end, u64 max_bytes, struct extent_state **cached_state) { - struct rb_node *node; struct extent_state *state;u64 cur_start = *start;bool found = false;
@@ -1553,13 +1542,12 @@ bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,* this search will find all the extents that end after * our range starts. */ - node =tree_search (tree, cur_start); -if (!node) { + state =tree_search (tree, cur_start); +if (!state) { *end = (u64)-1 ; goto out; } - state = rb_entry(node, struct extent_state, rb_node);while (state) {if (found && (state->start != cur_start || (state->state &EXTENT_BOUNDARY ))) {
@@ -1597,7 +1585,6 @@ u64 count_range_bits(struct extent_io_tree *tree, u64 *start, u64 search_end, u64 max_bytes, u32 bits, int contig) { - struct rb_node *node; struct extent_state *state;u64 cur_start = *start;u64 total_bytes =0 ;
@@ -1616,11 +1603,7 @@ u64 count_range_bits(struct extent_io_tree *tree,* this search will find all the extents that end after * our range starts. */ - node =tree_search (tree, cur_start); -if (!node) - goto out; - - state = rb_entry(node, struct extent_state, rb_node); + state =tree_search (tree, cur_start);while (state) {if (state->start > search_end)break ;
@@ -1656,19 +1639,14 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits, int filled, struct extent_state *cached) { struct extent_state *state = NULL; -struct rb_node *node;int bitset =0 ;spin_lock (&tree->lock);if (cached &&extent_state_in_tree (cached) && cached->start <= start && cached->end > start) - node = &cached->rb_node; + state = cached;else - node =tree_search (tree, start); -if (!node) - goto out; - - state = rb_entry(node, struct extent_state, rb_node); + state =tree_search (tree, start);while (state && start <= end) {if (filled && state->start > start) { bitset =0 ;
@@ -1699,7 +1677,6 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,/* We ran out of states and were still inside of our range. */ if (filled && !state) bitset =0 ; -out:spin_unlock (&tree->lock);return bitset; }
Signature · 1 line
2.26.3
PATCH 14/31 btrfs: use next_state instead of rb_next where we can
In reply to Josef BacikWe can simplify a lot of these functions where we have to cycle through extent_state's by simply using next_state() instead of rb_next(). In many spots this allows us to do things like
while (state) {
/* whatever */
state = next_state(state);
}instead of
while (1) {
state = rb_entry(n, struct extent_state, rb_node);
n = rb_next(n);
if (!n)
break;
}--- fs/btrfs/extent-io-tree.c | 57 +++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 33 deletions(-)
fs/btrfs/extent-io-tree.c +24 -33
@@ -770,12 +770,10 @@ void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits)* our range starts */ node =tree_search (tree, start); -process_node:if (!node)break ; - state = rb_entry(node, struct extent_state, rb_node); - +process_node:if (state->start > end) goto out;
@@ -792,7 +790,7 @@ void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits)break ;if (!cond_resched_lock (&tree->lock)) { - node =rb_next (node); + state =next_state (state); goto process_node; } }
@@ -1325,15 +1323,13 @@ find_first_extent_bit_state(struct extent_io_tree *tree, u64 start, u32 bits) node =tree_search (tree, start);if (!node) goto out; + state = rb_entry(node, struct extent_state, rb_node); -while (1 ) { +while (state) { state = rb_entry(node, struct extent_state, rb_node);if (state->end >= start && (state->state & bits))return state; - - node =rb_next (node); -if (!node) -break ; + state =next_state (state); } out:return NULL ;
@@ -1521,18 +1517,15 @@ void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,* Find the longest stretch from start until an entry which has the * bits set */ -while (1 ) { - state = rb_entry(node, struct extent_state, rb_node); + state = rb_entry(node, struct extent_state, rb_node); +while (state) {if (state->end >= start && !(state->state & bits)) { *end_ret = state->end; }else { *end_ret = state->start -1 ;break ; } - - node =rb_next (node); -if (!node) -break ; + state =next_state (state); } out:spin_unlock (&tree->lock);
@@ -1566,8 +1559,8 @@ bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start, goto out; } -while (1 ) { - state = rb_entry(node, struct extent_state, rb_node); + state = rb_entry(node, struct extent_state, rb_node); +while (state) {if (found && (state->start != cur_start || (state->state &EXTENT_BOUNDARY ))) { goto out;
@@ -1585,12 +1578,10 @@ bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start, found = true; *end = state->end; cur_start = state->end +1 ; - node =rb_next (node); total_bytes += state->end - state->start +1 ;if (total_bytes >= max_bytes)break ; -if (!node) -break ; + state =next_state (state); } out:spin_unlock (&tree->lock);
@@ -1629,8 +1620,8 @@ u64 count_range_bits(struct extent_io_tree *tree,if (!node) goto out; -while (1 ) { - state = rb_entry(node, struct extent_state, rb_node); + state = rb_entry(node, struct extent_state, rb_node); +while (state) {if (state->start > search_end)break ;if (contig && found && state->start > last +1 )
@@ -1648,9 +1639,7 @@ u64 count_range_bits(struct extent_io_tree *tree, }else if (contig && found) {break ; } - node =rb_next (node); -if (!node) -break ; + state =next_state (state); } out:spin_unlock (&tree->lock);
@@ -1676,9 +1665,11 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, node = &cached->rb_node; else node =tree_search (tree, start); -while (node && start <= end) { - state = rb_entry(node, struct extent_state, rb_node); +if (!node) + goto out; + state = rb_entry(node, struct extent_state, rb_node); +while (state && start <= end) {if (filled && state->start > start) { bitset =0 ;break ;
@@ -1702,13 +1693,13 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, start = state->end +1 ;if (start > end)break ; - node =rb_next (node); -if (!node) { -if (filled) - bitset =0 ; -break ; - } + state =next_state (state); } + +/* We ran out of states and were still inside of our range. */ +if (filled && !state) + bitset =0 ; +out:spin_unlock (&tree->lock);return bitset; }
Signature · 1 line
2.26.3
PATCH 17/31 btrfs: make tree_search_prev_next return extent_state's
In reply to Josef BacikInstead of doing the rb_entry again once we return from this function, simply return the actual states themselves, and then clean up the only user of this helper to handle states instead of nodes.
--- fs/btrfs/extent-io-tree.c | 73 ++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 39 deletions(-)
fs/btrfs/extent-io-tree.c +34 -39
@@ -236,6 +236,15 @@ static struct extent_state *next_state(struct extent_state *state)return NULL ; } +static struct extent_state *prev_state (struct extent_state *state) +{ +struct rb_node *next =rb_prev (&state->rb_node); +if (next) + return rb_entry(next, struct extent_state, rb_node); +else +return NULL ; +} +/** * Search @tree for an entry that contains @offset. Such entry would have * entry->start <= offset && entry->end >= offset.
@@ -307,46 +316,39 @@ static inline struct extent_state *tree_search(struct extent_io_tree *tree, u64* such entry exists, then return NULL and fill @prev_ret and @next_ret. * Otherwise return the found entry and other pointers are left untouched. */ -static struct rb_node *tree_search_prev_next (struct extent_io_tree *tree, -u64 offset, -struct rb_node **prev_ret, -struct rb_node **next_ret) +static struct extent_state *tree_search_prev_next (struct extent_io_tree *tree, +u64 offset, +struct extent_state **prev_ret, +struct extent_state **next_ret) {struct rb_root *root = &tree->state;struct rb_node **node = &root->rb_node; -struct rb_node *prev =NULL ; -struct rb_node *orig_prev =NULL ; -struct extent_state *entry; +struct extent_state *orig_prev; +struct extent_state *entry =NULL ;ASSERT (prev_ret);ASSERT (next_ret);while (*node) { - prev = *node; - entry = rb_entry(prev, struct extent_state, rb_node); + entry = rb_entry(*node, struct extent_state, rb_node);if (offset < entry->start) node = &(*node)->rb_left;else if (offset > entry->end) node = &(*node)->rb_right;else -return *node; +return entry; } - orig_prev = prev; -while (prev && offset > entry->end) { - prev =rb_next (prev); - entry = rb_entry(prev, struct extent_state, rb_node); - } - *next_ret = prev; - prev = orig_prev; + orig_prev = entry; +while (entry && offset > entry->end) + entry =next_state (entry); + *next_ret = entry; + entry = orig_prev; - entry = rb_entry(prev, struct extent_state, rb_node); -while (prev && offset < entry->start) { - prev =rb_prev (prev); - entry = rb_entry(prev, struct extent_state, rb_node); - } - *prev_ret = prev; +while (entry && offset < entry->start) + entry =prev_state (entry); + *prev_ret = entry;return NULL ; }
@@ -1415,14 +1417,14 @@ void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 *start_ret, u64 *end_ret, u32 bits) {struct extent_state *state; -struct rb_node *node, *prev =NULL , *next; +struct extent_state *prev =NULL , *next;spin_lock (&tree->lock);/* Find first extent with bits cleared */ while (1 ) { - node =tree_search_prev_next (tree, start, &prev, &next); -if (!node && !next && !prev) { + state =tree_search_prev_next (tree, start, &prev, &next); +if (!state && !next && !prev) {/* * Tree is completely empty, send full range and let * caller deal with it
@@ -1430,24 +1432,21 @@ void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start, *start_ret =0 ; *end_ret =-1 ; goto out; - }else if (!node && !next) { + }else if (!state && !next) {/* * We are past the last allocated chunk, set start at * the end of the last extent. */ - state = rb_entry(prev, struct extent_state, rb_node); - *start_ret = state->end +1 ; + *start_ret = prev->end +1 ; *end_ret =-1 ; goto out; - }else if (!node) { - node = next; + }else if (!state) { + state = next; }/* * At this point 'node' either contains 'start' or start is * before 'node' */ - state = rb_entry(node, struct extent_state, rb_node); -if (in_range (start, state->start, state->end - state->start +1 )) {if (state->state & bits) {/*
@@ -1481,13 +1480,10 @@ void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,* 0 | * start */ -if (prev) { - state = rb_entry(prev, struct extent_state, - rb_node); +if (prev) *start_ret = state->end +1 ; - }else { +else *start_ret =0 ; - }break ; } }
@@ -1496,7 +1492,6 @@ void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,* Find the longest stretch from start until an entry which has the * bits set */ - state = rb_entry(node, struct extent_state, rb_node);while (state) {if (state->end >= start && !(state->state & bits)) { *end_ret = state->end;
Signature · 1 line
2.26.3
PATCH 16/31 btrfs: make tree_search_for_insert return extent_state
In reply to Josef BacikWe use this to search for an extent state, or return the nodes we need to insert a new extent state. This means we have the following pattern
node = tree_search_for_insert();
if (!node) {
/* alloc and insert. */
goto again;
}state = rb_entry(node, struct extent_state, rb_node);
we don't use the node for anything else. Making tree_search_for_insert() return the extent_state means we can drop the rb_node and clean this up by eliminating the rb_entry.
--- fs/btrfs/extent-io-tree.c | 61 ++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 36 deletions(-)
fs/btrfs/extent-io-tree.c +25 -36
@@ -227,6 +227,15 @@ int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)return 1 ; } +static struct extent_state *next_state (struct extent_state *state) +{ +struct rb_node *next =rb_next (&state->rb_node); +if (next) + return rb_entry(next, struct extent_state, rb_node); +else +return NULL ; +} +/** * Search @tree for an entry that contains @offset. Such entry would have * entry->start <= offset && entry->end >= offset.
@@ -244,15 +253,15 @@ int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)* If no such entry exists, return pointer to entry that ends before @offset * and fill parameters @node_ret and @parent_ret, ie. does not return NULL. */ -static inline struct rb_node *tree_search_for_insert (struct extent_io_tree *tree, -u64 offset, -struct rb_node ***node_ret, -struct rb_node **parent_ret) +static inline struct extent_state *tree_search_for_insert (struct extent_io_tree *tree, +u64 offset, +struct rb_node ***node_ret, +struct rb_node **parent_ret) {struct rb_root *root = &tree->state;struct rb_node **node = &root->rb_node;struct rb_node *prev =NULL ; -struct extent_state *entry; +struct extent_state *entry =NULL ;while (*node) { prev = *node;
@@ -263,7 +272,7 @@ static inline struct rb_node *tree_search_for_insert(struct extent_io_tree *treeelse if (offset > entry->end) node = &(*node)->rb_right;else -return *node; +return entry; }if (node_ret)
@@ -272,12 +281,10 @@ static inline struct rb_node *tree_search_for_insert(struct extent_io_tree *tree *parent_ret = prev;/* Search neighbors until we find the first one past the end */ -while (prev && offset > entry->end) { - prev =rb_next (prev); - entry = rb_entry(prev, struct extent_state, rb_node); - } +while (entry && offset > entry->end) + entry =next_state (entry); -return prev; +return entry; }/*
@@ -285,8 +292,7 @@ static inline struct rb_node *tree_search_for_insert(struct extent_io_tree *tree*/ static inline struct extent_state *tree_search (struct extent_io_tree *tree,u64 offset) { -struct rb_node *n =tree_search_for_insert (tree, offset,NULL ,NULL ); - return (n) ? rb_entry(n, struct extent_state, rb_node) : NULL; +return tree_search_for_insert (tree, offset,NULL ,NULL ); }/**
@@ -511,15 +517,6 @@ static int split_state(struct extent_io_tree *tree, struct extent_state *orig,return 0 ; } -static struct extent_state *next_state (struct extent_state *state) -{ -struct rb_node *next =rb_next (&state->rb_node); -if (next) - return rb_entry(next, struct extent_state, rb_node); -else -return NULL ; -} -/* * utility function to clear some bits in an extent state struct. * it will optionally wake up anyone waiting on this state (wake == 1).
@@ -850,7 +847,6 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits, {struct extent_state *state;struct extent_state *prealloc =NULL ; -struct rb_node *node;struct rb_node **p;struct rb_node *parent;int err =0 ;
@@ -880,17 +876,15 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,if (cached_state && *cached_state) { state = *cached_state;if (state->start <= start && state->end > start && -extent_state_in_tree (state)) { - node = &state->rb_node; +extent_state_in_tree (state)) goto hit_next; - } }/* * this search will find all the extents that end after * our range starts. */ - node =tree_search_for_insert (tree, start, &p, &parent); -if (!node) { + state =tree_search_for_insert (tree, start, &p, &parent); +if (!state) { prealloc =alloc_extent_state_atomic (prealloc);BUG_ON (!prealloc); prealloc->start = start;
@@ -900,7 +894,6 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits, prealloc =NULL ; goto out; } - state = rb_entry(node, struct extent_state, rb_node); hit_next: last_start = state->start; last_end = state->end;
@@ -1086,7 +1079,6 @@ int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, {struct extent_state *state;struct extent_state *prealloc =NULL ; -struct rb_node *node;struct rb_node **p;struct rb_node *parent;int err =0 ;
@@ -1116,18 +1108,16 @@ int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,if (cached_state && *cached_state) { state = *cached_state;if (state->start <= start && state->end > start && -extent_state_in_tree (state)) { - node = &state->rb_node; +extent_state_in_tree (state)) goto hit_next; - } }/* * this search will find all the extents that end after * our range starts. */ - node =tree_search_for_insert (tree, start, &p, &parent); -if (!node) { + state =tree_search_for_insert (tree, start, &p, &parent); +if (!state) { prealloc =alloc_extent_state_atomic (prealloc);if (!prealloc) { err = -ENOMEM ;
@@ -1140,7 +1130,6 @@ int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, prealloc =NULL ; goto out; } - state = rb_entry(node, struct extent_state, rb_node); hit_next: last_start = state->start; last_end = state->end;
Signature · 1 line
2.26.3
PATCH 18/31 btrfs: use next_state/prev_state in merge_state
In reply to Josef BacikWe use rb_next/rb_prev and then get the entry for the adjacent items in an extent io tree. We have helpers for this, so convert merge_state to use next_state/prev_state and simplify the code.
--- fs/btrfs/extent-io-tree.c | 49 ++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 29 deletions(-)
fs/btrfs/extent-io-tree.c +20 -29
@@ -366,40 +366,31 @@ static void merge_state(struct extent_io_tree *tree, struct extent_state *state) {struct extent_state *other; -struct rb_node *other_node;if (state->state & (EXTENT_LOCKED |EXTENT_BOUNDARY ))return ; - other_node =rb_prev (&state->rb_node); -if (other_node) { - other = rb_entry(other_node, struct extent_state, rb_node); -if (other->end == state->start -1 && - other->state == state->state) { -if (tree->private_data && -is_data_inode (tree->private_data)) -btrfs_merge_delalloc_extent (tree->private_data, - state, other); - state->start = other->start; -rb_erase (&other->rb_node, &tree->state); -RB_CLEAR_NODE (&other->rb_node); -free_extent_state (other); - } + other =prev_state (state); +if (other && other->end == state->start -1 && + other->state == state->state) { +if (tree->private_data &&is_data_inode (tree->private_data)) +btrfs_merge_delalloc_extent (tree->private_data, + state, other); + state->start = other->start; +rb_erase (&other->rb_node, &tree->state); +RB_CLEAR_NODE (&other->rb_node); +free_extent_state (other); } - other_node =rb_next (&state->rb_node); -if (other_node) { - other = rb_entry(other_node, struct extent_state, rb_node); -if (other->start == state->end +1 && - other->state == state->state) { -if (tree->private_data && -is_data_inode (tree->private_data)) -btrfs_merge_delalloc_extent (tree->private_data, - state, other); - state->end = other->end; -rb_erase (&other->rb_node, &tree->state); -RB_CLEAR_NODE (&other->rb_node); -free_extent_state (other); - } + other =next_state (state); +if (other && other->start == state->end +1 && + other->state == state->state) { +if (tree->private_data &&is_data_inode (tree->private_data)) +btrfs_merge_delalloc_extent (tree->private_data, state, + other); + state->end = other->end; +rb_erase (&other->rb_node, &tree->state); +RB_CLEAR_NODE (&other->rb_node); +free_extent_state (other); } }
Signature · 1 line
2.26.3
PATCH 19/31 btrfs: remove temporary exports for extent_state movement
In reply to Josef BacikNow that the code has been moved we can remove these temporary exports.
--- fs/btrfs/extent-io-tree.c | 9 +++++++-- fs/btrfs/extent-io-tree.h | 9 --------- 2 files changed, 7 insertions(+), 11 deletions(-)
fs/btrfs/extent-io-tree.c +7 -2
@@ -9,6 +9,11 @@static struct kmem_cache *extent_state_cache; +static inline bool extent_state_in_tree (const struct extent_state *state) +{ +return !RB_EMPTY_NODE (&state->rb_node); +} +#ifdef CONFIG_BTRFS_DEBUG static LIST_HEAD (states);static DEFINE_SPINLOCK (leak_lock);
@@ -123,7 +128,7 @@ void extent_io_tree_release(struct extent_io_tree *tree)spin_unlock (&tree->lock); } -struct extent_state *alloc_extent_state (gfp_t mask) +static struct extent_state *alloc_extent_state (gfp_t mask) {struct extent_state *state;
@@ -144,7 +149,7 @@ struct extent_state *alloc_extent_state(gfp_t mask)return state; } -struct extent_state *alloc_extent_state_atomic (struct extent_state *prealloc) +static struct extent_state *alloc_extent_state_atomic (struct extent_state *prealloc) {if (!prealloc) prealloc =alloc_extent_state (GFP_ATOMIC );
fs/btrfs/extent-io-tree.h +0 -9
@@ -260,13 +260,4 @@ void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end);int btrfs_clean_io_failure (struct btrfs_inode *inode,u64 start,struct page *page,unsigned int pg_offset); - -struct extent_state *alloc_extent_state_atomic (struct extent_state *prealloc); -struct extent_state *alloc_extent_state (gfp_t mask); - -static inline bool extent_state_in_tree (const struct extent_state *state) -{ -return !RB_EMPTY_NODE (&state->rb_node); -} -#endif /* BTRFS_EXTENT_IO_TREE_H */
Signature · 1 line
2.26.3
PATCH 20/31 btrfs: move irrelevant prototypes to their appropriate header
In reply to Josef BacikThese prototypes have nothing to do with the extent_io_tree helpers, move them to their appropriate header.
--- fs/btrfs/extent-io-tree.h | 8 -------- fs/btrfs/extent_io.h | 6 ++++++ 2 files changed, 6 insertions(+), 8 deletions(-)
fs/btrfs/extent-io-tree.h +0 -8
@@ -248,16 +248,8 @@ void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 *start_ret, u64 *end_ret, u32 bits);int find_contiguous_extent_bit (struct extent_io_tree *tree,u64 start,u64 *start_ret,u64 *end_ret,u32 bits); -int extent_invalidate_folio (struct extent_io_tree *tree, -struct folio *folio,size_t offset);bool btrfs_find_delalloc_range (struct extent_io_tree *tree,u64 *start,u64 *end,u64 max_bytes,struct extent_state **cached_state);void wait_extent_bit (struct extent_io_tree *tree,u64 start,u64 end,u32 bits); - -/* This should be reworked in the future and put elsewhere. */ -void btrfs_free_io_failure_record (struct btrfs_inode *inode,u64 start, -u64 end); -int btrfs_clean_io_failure (struct btrfs_inode *inode,u64 start, -struct page *page,unsigned int pg_offset);#endif /* BTRFS_EXTENT_IO_TREE_H */
fs/btrfs/extent_io.h +6 -0
@@ -242,6 +242,8 @@ void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end);void extent_clear_unlock_delalloc (struct btrfs_inode *inode,u64 start,u64 end,struct page *locked_page,u32 bits_to_clear,unsigned long page_ops); +int extent_invalidate_folio (struct extent_io_tree *tree, +struct folio *folio,size_t offset);int btrfs_alloc_page_array (unsigned int nr_pages,struct page **page_array);
@@ -272,6 +274,10 @@ struct io_failure_record {int btrfs_repair_one_sector (struct inode *inode,struct btrfs_bio *failed_bbio,u32 bio_offset,struct page *page,unsigned int pgoff,submit_bio_hook_t *submit_bio_hook); +void btrfs_free_io_failure_record (struct btrfs_inode *inode,u64 start, +u64 end); +int btrfs_clean_io_failure (struct btrfs_inode *inode,u64 start, +struct page *page,unsigned int pg_offset);#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS bool find_lock_delalloc_range(struct inode *inode,
Signature · 1 line
2.26.3
PATCH 21/31 btrfs: drop exclusive_bits from set_extent_bit
In reply to Josef BacikThis is only ever set if we have EXTENT_LOCKED set, so simply push this into the function itself and remove the function argument.
--- fs/btrfs/extent-io-tree.c | 16 ++++++++-------- fs/btrfs/extent-io-tree.h | 20 ++++++++++---------- fs/btrfs/inode.c | 4 ++-- 3 files changed, 20 insertions(+), 20 deletions(-)
fs/btrfs/extent-io-tree.c +8 -8
@@ -199,7 +199,7 @@ int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,*/ ASSERT (!(bits &EXTENT_LOCKED )); -return set_extent_bit (tree, start, end, bits,0 ,NULL ,NULL ,GFP_NOFS , +return set_extent_bit (tree, start, end, bits,NULL ,NULL ,GFP_NOFS , changeset); }
@@ -221,8 +221,8 @@ int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)int err;u64 failed_start; - err =set_extent_bit (tree, start, end,EXTENT_LOCKED ,EXTENT_LOCKED , - &failed_start,NULL ,GFP_NOFS ,NULL ); + err =set_extent_bit (tree, start, end,EXTENT_LOCKED , &failed_start, +NULL ,GFP_NOFS ,NULL );if (err == -EEXIST ) {if (failed_start > start) clear_extent_bit(tree, start, failed_start - 1,
@@ -839,9 +839,8 @@ static void cache_state(struct extent_state *state,* [start, end] is inclusive This takes the tree lock. */ int set_extent_bit (struct extent_io_tree *tree,u64 start,u64 end,u32 bits, -u32 exclusive_bits,u64 *failed_start, -struct extent_state **cached_state,gfp_t mask, -struct extent_changeset *changeset) +u64 *failed_start,struct extent_state **cached_state, +gfp_t mask,struct extent_changeset *changeset) {struct extent_state *state;struct extent_state *prealloc =NULL ;
@@ -850,6 +849,7 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,int err =0 ;u64 last_start;u64 last_end; +u32 exclusive_bits = bits &EXTENT_LOCKED ;btrfs_debug_check_extent_io_range (tree, start, end);trace_btrfs_set_extent_bit (tree, start, end - start +1 , bits);
@@ -1278,8 +1278,8 @@ int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,while (1 ) { err =set_extent_bit (tree, start, end,EXTENT_LOCKED , -EXTENT_LOCKED , &failed_start, - cached_state,GFP_NOFS ,NULL ); + &failed_start, cached_state,GFP_NOFS , +NULL );if (err == -EEXIST ) {wait_extent_bit (tree, failed_start, end,EXTENT_LOCKED ); start = failed_start;
fs/btrfs/extent-io-tree.h +10 -10
@@ -166,21 +166,21 @@ static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start,int set_record_extent_bits (struct extent_io_tree *tree,u64 start,u64 end,u32 bits,struct extent_changeset *changeset);int set_extent_bit (struct extent_io_tree *tree,u64 start,u64 end, -u32 bits,unsigned exclusive_bits,u64 *failed_start, +u32 bits,u64 *failed_start,struct extent_state **cached_state,gfp_t mask,struct extent_changeset *changeset);static inline int set_extent_bits_nowait (struct extent_io_tree *tree,u64 start,u64 end,u32 bits) { -return set_extent_bit (tree, start, end, bits,0 ,NULL ,NULL , -GFP_NOWAIT ,NULL ); +return set_extent_bit (tree, start, end, bits,NULL ,NULL ,GFP_NOWAIT , +NULL ); }static inline int set_extent_bits (struct extent_io_tree *tree,u64 start,u64 end,u32 bits) { -return set_extent_bit (tree, start, end, bits,0 ,NULL ,NULL ,GFP_NOFS , +return set_extent_bit (tree, start, end, bits,NULL ,NULL ,GFP_NOFS ,NULL ); }
@@ -194,8 +194,8 @@ static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,static inline int set_extent_dirty (struct extent_io_tree *tree,u64 start,u64 end,gfp_t mask) { -return set_extent_bit (tree, start, end,EXTENT_DIRTY ,0 ,NULL ,NULL , - mask,NULL ); +return set_extent_bit (tree, start, end,EXTENT_DIRTY ,NULL ,NULL , mask, +NULL ); } static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start,
@@ -216,7 +216,7 @@ static inline int set_extent_delalloc(struct extent_io_tree *tree, u64 start, {return set_extent_bit (tree, start, end,EXTENT_DELALLOC | extra_bits, -0 ,NULL , cached_state,GFP_NOFS ,NULL ); +NULL , cached_state,GFP_NOFS ,NULL ); } static inline int set_extent_defrag(struct extent_io_tree *tree, u64 start,
@@ -224,20 +224,20 @@ static inline int set_extent_defrag(struct extent_io_tree *tree, u64 start, {return set_extent_bit (tree, start, end,EXTENT_DELALLOC |EXTENT_DEFRAG , -0 ,NULL , cached_state,GFP_NOFS ,NULL ); +NULL , cached_state,GFP_NOFS ,NULL ); }static inline int set_extent_new (struct extent_io_tree *tree,u64 start,u64 end) { -return set_extent_bit (tree, start, end,EXTENT_NEW ,0 ,NULL ,NULL , +return set_extent_bit (tree, start, end,EXTENT_NEW ,NULL ,NULL ,GFP_NOFS ,NULL ); }static inline int set_extent_uptodate (struct extent_io_tree *tree,u64 start,u64 end,struct extent_state **cached_state,gfp_t mask) { -return set_extent_bit (tree, start, end,EXTENT_UPTODATE ,0 ,NULL , +return set_extent_bit (tree, start, end,EXTENT_UPTODATE ,NULL , cached_state, mask,NULL ); }
fs/btrfs/inode.c +2 -2
@@ -2816,7 +2816,7 @@ static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, ret =set_extent_bit (&inode->io_tree, search_start, search_start + em_len -1 , -EXTENT_DELALLOC_NEW ,0 ,NULL , cached_state, +EXTENT_DELALLOC_NEW ,NULL , cached_state,GFP_NOFS ,NULL ); next: search_start =extent_map_end (em);
@@ -4964,7 +4964,7 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,if (only_release_metadata)set_extent_bit (&inode->io_tree, block_start, block_end, -EXTENT_NORESERVE ,0 ,NULL ,NULL ,GFP_NOFS ,NULL ); +EXTENT_NORESERVE ,NULL ,NULL ,GFP_NOFS ,NULL ); out_unlock:if (ret) {
Signature · 1 line
2.26.3
PATCH 22/31 btrfs: remove the wake argument from clear_extent_bits
In reply to Josef BacikThis is only used in the case that we are clearing EXTENT_LOCKED, so infer this value from the bits passed in instead of taking it as an argument.
--- fs/btrfs/extent-io-tree.c | 7 ++++--- fs/btrfs/extent-io-tree.h | 34 ++++++++++++++-------------------- fs/btrfs/extent_io.c | 4 ++-- fs/btrfs/extent_map.c | 2 +- fs/btrfs/file-item.c | 2 +- fs/btrfs/file.c | 2 +- fs/btrfs/free-space-cache.c | 6 +++--- fs/btrfs/inode.c | 17 ++++++++--------- fs/btrfs/ioctl.c | 2 +- fs/btrfs/reflink.c | 2 +- fs/btrfs/tests/inode-tests.c | 8 ++++---- fs/btrfs/transaction.c | 2 +- 12 files changed, 41 insertions(+), 47 deletions(-)
fs/btrfs/extent-io-tree.c +4 -3
@@ -212,7 +212,7 @@ int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,*/ ASSERT (!(bits &EXTENT_LOCKED )); -return __clear_extent_bit (tree, start, end, bits,0 ,0 ,NULL ,GFP_NOFS , +return __clear_extent_bit (tree, start, end, bits,0 ,NULL ,GFP_NOFS , changeset); }
@@ -226,7 +226,7 @@ int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)if (err == -EEXIST ) {if (failed_start > start)clear_extent_bit (tree, start, failed_start -1 , -EXTENT_LOCKED ,1 ,0 ,NULL ); +EXTENT_LOCKED ,0 ,NULL );return 0 ; }return 1 ;
@@ -580,7 +580,7 @@ static void extent_io_tree_panic(struct extent_io_tree *tree, int err)* This takes the tree lock, and returns 0 on success and < 0 on error. */ int __clear_extent_bit (struct extent_io_tree *tree,u64 start,u64 end, -u32 bits,int wake,int delete, +u32 bits,int delete,struct extent_state **cached_state,gfp_t mask,struct extent_changeset *changeset) {
@@ -590,6 +590,7 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,u64 last_end;int err;int clear =0 ; +int wake = (bits &EXTENT_LOCKED ) ?1 :0 ;btrfs_debug_check_extent_io_range (tree, start, end);trace_btrfs_clear_extent_bit (tree, start, end - start +1 , bits);
fs/btrfs/extent-io-tree.h +14 -20
@@ -121,46 +121,40 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,int clear_record_extent_bits (struct extent_io_tree *tree,u64 start,u64 end,u32 bits,struct extent_changeset *changeset);int __clear_extent_bit (struct extent_io_tree *tree,u64 start,u64 end, -u32 bits,int wake,int delete, -struct extent_state **cached,gfp_t mask, -struct extent_changeset *changeset); +u32 bits,int delete,struct extent_state **cached, +gfp_t mask,struct extent_changeset *changeset);static inline int clear_extent_bit (struct extent_io_tree *tree,u64 start, -u64 end,u32 bits,int wake,int delete, +u64 end,u32 bits,int delete,struct extent_state **cached) { -return __clear_extent_bit (tree, start, end, bits, wake, delete, - cached,GFP_NOFS ,NULL ); +return __clear_extent_bit (tree, start, end, bits, delete, cached, +GFP_NOFS ,NULL ); }static inline int unlock_extent (struct extent_io_tree *tree,u64 start,u64 end) { -return clear_extent_bit (tree, start, end,EXTENT_LOCKED ,1 ,0 ,NULL ); +return clear_extent_bit (tree, start, end,EXTENT_LOCKED ,0 ,NULL ); }static inline int unlock_extent_cached (struct extent_io_tree *tree,u64 start,u64 end,struct extent_state **cached) { -return __clear_extent_bit (tree, start, end,EXTENT_LOCKED ,1 ,0 , cached, -GFP_NOFS ,NULL ); +return __clear_extent_bit (tree, start, end,EXTENT_LOCKED ,0 , cached, +GFP_NOFS ,NULL ); }static inline int unlock_extent_cached_atomic (struct extent_io_tree *tree,u64 start,u64 end,struct extent_state **cached) { -return __clear_extent_bit (tree, start, end,EXTENT_LOCKED ,1 ,0 , cached, -GFP_ATOMIC ,NULL ); +return __clear_extent_bit (tree, start, end,EXTENT_LOCKED ,0 , cached, +GFP_ATOMIC ,NULL ); }static inline int clear_extent_bits (struct extent_io_tree *tree,u64 start,u64 end,u32 bits) { -int wake =0 ; - -if (bits &EXTENT_LOCKED ) - wake =1 ; - -return clear_extent_bit (tree, start, end, bits, wake,0 ,NULL ); +return clear_extent_bit (tree, start, end, bits,0 ,NULL ); } int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
@@ -187,8 +181,8 @@ static inline int set_extent_bits(struct extent_io_tree *tree, u64 start,static inline int clear_extent_uptodate (struct extent_io_tree *tree,u64 start,u64 end,struct extent_state **cached_state) { -return __clear_extent_bit (tree, start, end,EXTENT_UPTODATE ,0 ,0 , - cached_state,GFP_NOFS ,NULL ); +return __clear_extent_bit (tree, start, end,EXTENT_UPTODATE ,0 , + cached_state,GFP_NOFS ,NULL ); } static inline int set_extent_dirty(struct extent_io_tree *tree, u64 start,
@@ -203,7 +197,7 @@ static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start, {return clear_extent_bit (tree, start, end,EXTENT_DIRTY |EXTENT_DELALLOC | -EXTENT_DO_ACCOUNTING ,0 ,0 , cached); +EXTENT_DO_ACCOUNTING ,0 , cached); } int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
fs/btrfs/extent_io.c +2 -2
@@ -487,7 +487,7 @@ void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,struct page *locked_page, u32 clear_bits, unsigned long page_ops) { - clear_extent_bit(&inode->io_tree, start, end, clear_bits, 1, 0, NULL); + clear_extent_bit(&inode->io_tree, start, end, clear_bits, 0, NULL);__process_pages_contig (inode->vfs_inode.i_mapping, locked_page, start, end, page_ops,NULL );
@@ -3400,7 +3400,7 @@ static int try_release_extent_state(struct extent_io_tree *tree,*/ ret =__clear_extent_bit (tree, start, end, ~(EXTENT_LOCKED |EXTENT_NODATASUM |EXTENT_DELALLOC_NEW ), -0 ,0 ,NULL , mask,NULL ); +0 ,NULL , mask,NULL );/* if clear_extent_bit failed for enomem reasons, * we can't allow the release to continue.
fs/btrfs/extent_map.c +1 -1
@@ -382,7 +382,7 @@ static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)__clear_extent_bit (&device->alloc_state, stripe->physical, stripe->physical + stripe_size -1 , bits, -0 ,0 ,NULL ,GFP_NOWAIT ,NULL ); +0 ,NULL ,GFP_NOWAIT ,NULL ); } }
fs/btrfs/file-item.c +1 -1
@@ -118,7 +118,7 @@ int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start,if (btrfs_fs_incompat (inode->root->fs_info,NO_HOLES ))return 0 ;return clear_extent_bit (&inode->file_extent_tree, start, - start + len -1 ,EXTENT_DIRTY ,0 ,0 ,NULL ); + start + len -1 ,EXTENT_DIRTY ,0 ,NULL ); } static inline u32 max_ordered_sum_bytes(struct btrfs_fs_info *fs_info,
fs/btrfs/file.c +1 -1
@@ -473,7 +473,7 @@ int btrfs_dirty_pages(struct btrfs_inode *inode, struct page **pages,*/ clear_extent_bit (&inode->io_tree, start_pos, end_of_last_block,EXTENT_DELALLOC |EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG , -0 ,0 , cached); +0 , cached); err =btrfs_set_extent_delalloc (inode, start_pos, end_of_last_block, extra_bits, cached);
fs/btrfs/free-space-cache.c +3 -3
@@ -1163,7 +1163,7 @@ update_cache_item(struct btrfs_trans_handle *trans, ret =btrfs_search_slot (trans, root, &key, path,0 ,1 );if (ret <0 ) {clear_extent_bit (&BTRFS_I (inode)->io_tree,0 , inode->i_size -1 , -EXTENT_DELALLOC ,0 ,0 ,NULL ); +EXTENT_DELALLOC ,0 ,NULL ); goto fail; } leaf = path->nodes[0 ];
@@ -1176,7 +1176,7 @@ update_cache_item(struct btrfs_trans_handle *trans, found_key.offset != offset) {clear_extent_bit (&BTRFS_I (inode)->io_tree,0 , inode->i_size -1 ,EXTENT_DELALLOC ,0 , -0 ,NULL ); +NULL );btrfs_release_path (path); goto fail; }
@@ -1272,7 +1272,7 @@ static int flush_dirty_cache(struct inode *inode) ret =btrfs_wait_ordered_range (inode,0 , (u64)-1 );if (ret)clear_extent_bit (&BTRFS_I (inode)->io_tree,0 , inode->i_size -1 , -EXTENT_DELALLOC ,0 ,0 ,NULL ); +EXTENT_DELALLOC ,0 ,NULL );return ret; }
fs/btrfs/inode.c +8 -9
@@ -1748,7 +1748,7 @@ static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,if (count >0 )clear_extent_bit (io_tree, start, end,EXTENT_NORESERVE , -0 ,0 ,NULL ); +0 ,NULL ); } return cow_file_range(inode, locked_page, start, end, page_started,
@@ -3325,7 +3325,7 @@ int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))clear_extent_bit (&inode->io_tree, start, end,EXTENT_DELALLOC_NEW |EXTENT_ADD_INODE_BYTES , -0 ,0 , &cached_state); +0 , &cached_state);btrfs_inode_safe_disk_i_size_write (inode,0 ); ret =btrfs_update_inode_fallback (trans, root, inode);
@@ -3335,8 +3335,7 @@ int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) } ret =0 ; out: -clear_extent_bit (&inode->io_tree, start, end, clear_bits, - (clear_bits &EXTENT_LOCKED ) ?1 :0 ,0 , +clear_extent_bit (&inode->io_tree, start, end, clear_bits,0 , &cached_state);if (trans)
@@ -4937,7 +4936,7 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,clear_extent_bit (&inode->io_tree, block_start, block_end,EXTENT_DELALLOC |EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG , -0 ,0 , &cached_state); +0 , &cached_state); ret =btrfs_set_extent_delalloc (inode, block_start, block_end,0 , &cached_state);
@@ -5353,7 +5352,7 @@ static void evict_inode_truncate_pages(struct inode *inode)clear_extent_bit (io_tree, start, end,EXTENT_LOCKED |EXTENT_DELALLOC | -EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG ,1 ,1 , +EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG ,1 , &cached_state);cond_resched ();
@@ -8442,7 +8441,7 @@ static void btrfs_invalidate_folio(struct folio *folio, size_t offset,clear_extent_bit (tree, cur, range_end,EXTENT_DELALLOC |EXTENT_LOCKED |EXTENT_DO_ACCOUNTING | -EXTENT_DEFRAG ,1 ,0 , &cached_state); +EXTENT_DEFRAG ,0 , &cached_state);spin_lock_irq (&inode->ordered_tree.lock);set_bit (BTRFS_ORDERED_TRUNCATED , &ordered->flags);
@@ -8488,7 +8487,7 @@ static void btrfs_invalidate_folio(struct folio *folio, size_t offset,if (!inode_evicting) {clear_extent_bit (tree, cur, range_end,EXTENT_LOCKED |EXTENT_DELALLOC |EXTENT_UPTODATE | -EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG ,1 , +EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG , delete_states, &cached_state); } cur = range_end +1 ;
@@ -8624,7 +8623,7 @@ vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)*/ clear_extent_bit (&BTRFS_I (inode)->io_tree, page_start, end,EXTENT_DELALLOC |EXTENT_DO_ACCOUNTING | -EXTENT_DEFRAG ,0 ,0 , &cached_state); +EXTENT_DEFRAG ,0 , &cached_state); ret2 =btrfs_set_extent_delalloc (BTRFS_I (inode), page_start, end,0 , &cached_state);
fs/btrfs/ioctl.c +1 -1
@@ -1616,7 +1616,7 @@ static int defrag_one_locked_target(struct btrfs_inode *inode,return ret;clear_extent_bit (&inode->io_tree, start, start + len -1 ,EXTENT_DELALLOC |EXTENT_DO_ACCOUNTING | -EXTENT_DEFRAG ,0 ,0 , cached_state); +EXTENT_DEFRAG ,0 , cached_state);set_extent_defrag (&inode->io_tree, start, start + len -1 , cached_state);/* Update the page status */
fs/btrfs/reflink.c +1 -1
@@ -92,7 +92,7 @@ static int copy_inline_to_page(struct btrfs_inode *inode,clear_extent_bit (&inode->io_tree, file_offset, range_end,EXTENT_DELALLOC |EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG , -0 ,0 ,NULL ); +0 ,NULL ); ret =btrfs_set_extent_delalloc (inode, file_offset, range_end,0 ,NULL );if (ret) goto out_unlock;
fs/btrfs/tests/inode-tests.c +4 -4
@@ -975,7 +975,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_DELALLOC_NEW | - EXTENT_UPTODATE, 0, 0, NULL); + EXTENT_UPTODATE, 0, NULL);if (ret) {test_err ("clear_extent_bit returned %d" , ret); goto out;
@@ -1043,7 +1043,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)BTRFS_MAX_EXTENT_SIZE + sectorsize,BTRFS_MAX_EXTENT_SIZE +2 * sectorsize -1 ,EXTENT_DELALLOC |EXTENT_DELALLOC_NEW | - EXTENT_UPTODATE, 0, 0, NULL); + EXTENT_UPTODATE, 0, NULL);if (ret) {test_err ("clear_extent_bit returned %d" , ret); goto out;
@@ -1076,7 +1076,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)/* Empty */ ret =clear_extent_bit (&BTRFS_I (inode)->io_tree,0 , (u64)-1 ,EXTENT_DELALLOC |EXTENT_DELALLOC_NEW | -EXTENT_UPTODATE ,0 ,0 ,NULL ); +EXTENT_UPTODATE ,0 ,NULL );if (ret) {test_err ("clear_extent_bit returned %d" , ret); goto out;
@@ -1092,7 +1092,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)if (ret)clear_extent_bit (&BTRFS_I (inode)->io_tree,0 , (u64)-1 ,EXTENT_DELALLOC |EXTENT_DELALLOC_NEW | -EXTENT_UPTODATE ,0 ,0 ,NULL ); +EXTENT_UPTODATE ,0 ,NULL );iput (inode);btrfs_free_dummy_root (root);btrfs_free_dummy_fs_info (fs_info);
fs/btrfs/transaction.c +1 -1
@@ -1116,7 +1116,7 @@ static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info,* it's safe to do it (through extent_io_tree_release()). */ err =clear_extent_bit (dirty_pages, start, end, -EXTENT_NEED_WAIT ,0 ,0 , &cached_state); +EXTENT_NEED_WAIT ,0 , &cached_state);if (err == -ENOMEM ) err =0 ;if (!err)
Signature · 1 line
2.26.3
PATCH 23/31 btrfs: remove failed_start argument from set_extent_bit
In reply to Josef BacikThis is only used for internal locking related helpers, everybody else just passes in NULL. I've changed set_extent_bit to __set_extent_bit and made it static, removed failed_start from set_extent_bit and have it call __set_extent_bit with a NULL failed_start, and I've moved some code down below the now static __set_extent_bit.
--- fs/btrfs/extent-io-tree.c | 110 ++++++++++++++++++++------------------ fs/btrfs/extent-io-tree.h | 22 ++++---- fs/btrfs/inode.c | 4 +- 3 files changed, 70 insertions(+), 66 deletions(-)
fs/btrfs/extent-io-tree.c +59 -51
@@ -187,51 +187,6 @@ static int add_extent_changeset(struct extent_state *state, u32 bits,return ret; } -/* wrappers around set/clear extent bit */ -int set_record_extent_bits (struct extent_io_tree *tree,u64 start,u64 end, -u32 bits,struct extent_changeset *changeset) -{ -/* -* We don't support EXTENT_LOCKED yet, as current changeset will -* record any bits changed, so for EXTENT_LOCKED case, it will -* either fail with -EEXIST or changeset will record the whole -* range. -*/ -ASSERT (!(bits &EXTENT_LOCKED )); - -return set_extent_bit (tree, start, end, bits,NULL ,NULL ,GFP_NOFS , - changeset); -} - -int clear_record_extent_bits (struct extent_io_tree *tree,u64 start,u64 end, -u32 bits,struct extent_changeset *changeset) -{ -/* -* Don't support EXTENT_LOCKED case, same reason as -* set_record_extent_bits(). -*/ -ASSERT (!(bits &EXTENT_LOCKED )); - -return __clear_extent_bit (tree, start, end, bits,0 ,NULL ,GFP_NOFS , - changeset); -} - -int try_lock_extent (struct extent_io_tree *tree,u64 start,u64 end) -{ -int err; -u64 failed_start; - - err =set_extent_bit (tree, start, end,EXTENT_LOCKED , &failed_start, -NULL ,GFP_NOFS ,NULL ); -if (err == -EEXIST ) { -if (failed_start > start) -clear_extent_bit (tree, start, failed_start -1 , -EXTENT_LOCKED ,0 ,NULL ); -return 0 ; - } -return 1 ; -} -static struct extent_state *next_state (struct extent_state *state) {struct rb_node *next =rb_next (&state->rb_node);
@@ -839,9 +794,10 @@ static void cache_state(struct extent_state *state,* * [start, end] is inclusive This takes the tree lock. */ -int set_extent_bit (struct extent_io_tree *tree,u64 start,u64 end,u32 bits, -u64 *failed_start,struct extent_state **cached_state, -gfp_t mask,struct extent_changeset *changeset) +static int __set_extent_bit (struct extent_io_tree *tree,u64 start,u64 end, +u32 bits,u64 *failed_start, +struct extent_state **cached_state, +struct extent_changeset *changeset,gfp_t mask) {struct extent_state *state;struct extent_state *prealloc =NULL ;
@@ -1054,6 +1010,14 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits, } +int set_extent_bit (struct extent_io_tree *tree,u64 start,u64 end, +u32 bits,struct extent_state **cached_state,gfp_t mask, +struct extent_changeset *changeset) +{ +return __set_extent_bit (tree, start, end, bits,NULL , cached_state, + changeset, mask); +} +/** * convert_extent_bit - convert all bits in a given range from one bit to * another
@@ -1278,9 +1242,8 @@ int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,u64 failed_start;while (1 ) { - err =set_extent_bit (tree, start, end,EXTENT_LOCKED , - &failed_start, cached_state,GFP_NOFS , -NULL ); + err =__set_extent_bit (tree, start, end,EXTENT_LOCKED , + &failed_start, cached_state,NULL ,GFP_NOFS );if (err == -EEXIST ) {wait_extent_bit (tree, failed_start, end,EXTENT_LOCKED ); start = failed_start;
@@ -1662,6 +1625,51 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,return bitset; } +/* wrappers around set/clear extent bit */ +int set_record_extent_bits (struct extent_io_tree *tree,u64 start,u64 end, +u32 bits,struct extent_changeset *changeset) +{ +/* +* We don't support EXTENT_LOCKED yet, as current changeset will +* record any bits changed, so for EXTENT_LOCKED case, it will +* either fail with -EEXIST or changeset will record the whole +* range. +*/ +ASSERT (!(bits &EXTENT_LOCKED )); + +return __set_extent_bit (tree, start, end, bits,NULL ,NULL , changeset, +GFP_NOFS ); +} + +int clear_record_extent_bits (struct extent_io_tree *tree,u64 start,u64 end, +u32 bits,struct extent_changeset *changeset) +{ +/* +* Don't support EXTENT_LOCKED case, same reason as +* set_record_extent_bits(). +*/ +ASSERT (!(bits &EXTENT_LOCKED )); + +return __clear_extent_bit (tree, start, end, bits,0 ,NULL ,GFP_NOFS , + changeset); +} + +int try_lock_extent (struct extent_io_tree *tree,u64 start,u64 end) +{ +int err; +u64 failed_start; + + err =__set_extent_bit (tree, start, end,EXTENT_LOCKED , &failed_start, +NULL ,NULL ,GFP_NOFS ); +if (err == -EEXIST ) { +if (failed_start > start) +clear_extent_bit (tree, start, failed_start -1 , +EXTENT_LOCKED ,0 ,NULL ); +return 0 ; + } +return 1 ; +} +void __cold extent_state_free_cachep (void ) {btrfs_extent_state_leak_debug_check ();
fs/btrfs/extent-io-tree.h +9 -13
@@ -160,22 +160,19 @@ static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start,int set_record_extent_bits (struct extent_io_tree *tree,u64 start,u64 end,u32 bits,struct extent_changeset *changeset);int set_extent_bit (struct extent_io_tree *tree,u64 start,u64 end, -u32 bits,u64 *failed_start, -struct extent_state **cached_state,gfp_t mask, +u32 bits,struct extent_state **cached_state,gfp_t mask,struct extent_changeset *changeset);static inline int set_extent_bits_nowait (struct extent_io_tree *tree,u64 start,u64 end,u32 bits) { -return set_extent_bit (tree, start, end, bits,NULL ,NULL ,GFP_NOWAIT , -NULL ); +return set_extent_bit (tree, start, end, bits,NULL ,GFP_NOWAIT ,NULL ); }static inline int set_extent_bits (struct extent_io_tree *tree,u64 start,u64 end,u32 bits) { -return set_extent_bit (tree, start, end, bits,NULL ,NULL ,GFP_NOFS , -NULL ); +return set_extent_bit (tree, start, end, bits,NULL ,GFP_NOFS ,NULL ); } static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
@@ -188,8 +185,7 @@ static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,static inline int set_extent_dirty (struct extent_io_tree *tree,u64 start,u64 end,gfp_t mask) { -return set_extent_bit (tree, start, end,EXTENT_DIRTY ,NULL ,NULL , mask, -NULL ); +return set_extent_bit (tree, start, end,EXTENT_DIRTY ,NULL , mask,NULL ); } static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start,
@@ -210,7 +206,7 @@ static inline int set_extent_delalloc(struct extent_io_tree *tree, u64 start, {return set_extent_bit (tree, start, end,EXTENT_DELALLOC | extra_bits, -NULL , cached_state,GFP_NOFS ,NULL ); + cached_state,GFP_NOFS ,NULL ); } static inline int set_extent_defrag(struct extent_io_tree *tree, u64 start,
@@ -218,20 +214,20 @@ static inline int set_extent_defrag(struct extent_io_tree *tree, u64 start, {return set_extent_bit (tree, start, end,EXTENT_DELALLOC |EXTENT_DEFRAG , -NULL , cached_state,GFP_NOFS ,NULL ); + cached_state,GFP_NOFS ,NULL ); }static inline int set_extent_new (struct extent_io_tree *tree,u64 start,u64 end) { -return set_extent_bit (tree, start, end,EXTENT_NEW ,NULL ,NULL , -GFP_NOFS ,NULL ); +return set_extent_bit (tree, start, end,EXTENT_NEW ,NULL ,GFP_NOFS , +NULL ); }static inline int set_extent_uptodate (struct extent_io_tree *tree,u64 start,u64 end,struct extent_state **cached_state,gfp_t mask) { -return set_extent_bit (tree, start, end,EXTENT_UPTODATE ,NULL , +return set_extent_bit (tree, start, end,EXTENT_UPTODATE , cached_state, mask,NULL ); }
fs/btrfs/inode.c +2 -2
@@ -2816,7 +2816,7 @@ static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, ret =set_extent_bit (&inode->io_tree, search_start, search_start + em_len -1 , -EXTENT_DELALLOC_NEW ,NULL , cached_state, +EXTENT_DELALLOC_NEW , cached_state,GFP_NOFS ,NULL ); next: search_start =extent_map_end (em);
@@ -4963,7 +4963,7 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,if (only_release_metadata)set_extent_bit (&inode->io_tree, block_start, block_end, -EXTENT_NORESERVE ,NULL ,NULL ,GFP_NOFS ,NULL ); +EXTENT_NORESERVE ,NULL ,GFP_NOFS ,NULL ); out_unlock:if (ret) {
Signature · 1 line
2.26.3
PATCH 24/31 btrfs: drop extent_changeset from set_extent_bit
In reply to Josef BacikThe only places that set extent_changeset is set_record_extent_bits, everywhere else sets it to NULL. Drop this argument from set_extent_bit.
--- fs/btrfs/extent-io-tree.c | 5 ++--- fs/btrfs/extent-io-tree.h | 18 ++++++++---------- fs/btrfs/inode.c | 4 ++-- 3 files changed, 12 insertions(+), 15 deletions(-)
fs/btrfs/extent-io-tree.c +2 -3
@@ -1011,11 +1011,10 @@ static int __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, }int set_extent_bit (struct extent_io_tree *tree,u64 start,u64 end, -u32 bits,struct extent_state **cached_state,gfp_t mask, -struct extent_changeset *changeset) +u32 bits,struct extent_state **cached_state,gfp_t mask) {return __set_extent_bit (tree, start, end, bits,NULL , cached_state, - changeset, mask); +NULL , mask); }/**
fs/btrfs/extent-io-tree.h +8 -10
@@ -160,19 +160,18 @@ static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start,int set_record_extent_bits (struct extent_io_tree *tree,u64 start,u64 end,u32 bits,struct extent_changeset *changeset);int set_extent_bit (struct extent_io_tree *tree,u64 start,u64 end, -u32 bits,struct extent_state **cached_state,gfp_t mask, -struct extent_changeset *changeset); +u32 bits,struct extent_state **cached_state,gfp_t mask);static inline int set_extent_bits_nowait (struct extent_io_tree *tree,u64 start,u64 end,u32 bits) { -return set_extent_bit (tree, start, end, bits,NULL ,GFP_NOWAIT ,NULL ); +return set_extent_bit (tree, start, end, bits,NULL ,GFP_NOWAIT ); }static inline int set_extent_bits (struct extent_io_tree *tree,u64 start,u64 end,u32 bits) { -return set_extent_bit (tree, start, end, bits,NULL ,GFP_NOFS ,NULL ); +return set_extent_bit (tree, start, end, bits,NULL ,GFP_NOFS ); } static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
@@ -185,7 +184,7 @@ static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,static inline int set_extent_dirty (struct extent_io_tree *tree,u64 start,u64 end,gfp_t mask) { -return set_extent_bit (tree, start, end,EXTENT_DIRTY ,NULL , mask,NULL ); +return set_extent_bit (tree, start, end,EXTENT_DIRTY ,NULL , mask); } static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start,
@@ -206,7 +205,7 @@ static inline int set_extent_delalloc(struct extent_io_tree *tree, u64 start, {return set_extent_bit (tree, start, end,EXTENT_DELALLOC | extra_bits, - cached_state,GFP_NOFS ,NULL ); + cached_state,GFP_NOFS ); } static inline int set_extent_defrag(struct extent_io_tree *tree, u64 start,
@@ -214,21 +213,20 @@ static inline int set_extent_defrag(struct extent_io_tree *tree, u64 start, {return set_extent_bit (tree, start, end,EXTENT_DELALLOC |EXTENT_DEFRAG , - cached_state,GFP_NOFS ,NULL ); + cached_state,GFP_NOFS ); }static inline int set_extent_new (struct extent_io_tree *tree,u64 start,u64 end) { -return set_extent_bit (tree, start, end,EXTENT_NEW ,NULL ,GFP_NOFS , -NULL ); +return set_extent_bit (tree, start, end,EXTENT_NEW ,NULL ,GFP_NOFS ); }static inline int set_extent_uptodate (struct extent_io_tree *tree,u64 start,u64 end,struct extent_state **cached_state,gfp_t mask) {return set_extent_bit (tree, start, end,EXTENT_UPTODATE , - cached_state, mask,NULL ); + cached_state, mask); } int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
fs/btrfs/inode.c +2 -2
@@ -2817,7 +2817,7 @@ static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, ret =set_extent_bit (&inode->io_tree, search_start, search_start + em_len -1 ,EXTENT_DELALLOC_NEW , cached_state, -GFP_NOFS ,NULL ); +GFP_NOFS ); next: search_start =extent_map_end (em);free_extent_map (em);
@@ -4963,7 +4963,7 @@ int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,if (only_release_metadata)set_extent_bit (&inode->io_tree, block_start, block_end, -EXTENT_NORESERVE ,NULL ,GFP_NOFS ,NULL ); +EXTENT_NORESERVE ,NULL ,GFP_NOFS ); out_unlock:if (ret) {
Signature · 1 line
2.26.3
PATCH 26/31 btrfs: get rid of track_uptodate
In reply to Josef BacikThis is no longer used anymore, remove it from the extent_io_tree.
--- fs/btrfs/disk-io.c | 1 - fs/btrfs/extent-io-tree.h | 1 - fs/btrfs/inode.c | 1 - 3 files changed, 3 deletions(-)
fs/btrfs/disk-io.c +0 -1
@@ -2232,7 +2232,6 @@ static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)RB_CLEAR_NODE (&BTRFS_I (inode)->rb_node);extent_io_tree_init (fs_info, &BTRFS_I (inode)->io_tree,IO_TREE_BTREE_INODE_IO , inode); -BTRFS_I (inode)->io_tree.track_uptodate = false;extent_map_tree_init (&BTRFS_I (inode)->extent_tree);BTRFS_I (inode)->root =btrfs_grab_root (fs_info->tree_root);
fs/btrfs/extent-io-tree.h +0 -1
@@ -70,7 +70,6 @@ struct extent_io_tree {struct btrfs_fs_info *fs_info;void *private_data;u64 dirty_bytes; -bool track_uptodate;/* Who owns this io tree, should be one of IO_TREE_* */ u8 owner;
fs/btrfs/inode.c +0 -1
@@ -8914,7 +8914,6 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)extent_io_tree_init (fs_info, &ei->file_extent_tree,IO_TREE_INODE_FILE_EXTENT , inode); ei->io_failure_tree =RB_ROOT ; - ei->io_tree.track_uptodate = true;atomic_set (&ei->sync_writers,0 );mutex_init (&ei->log_mutex);btrfs_ordered_inode_tree_init (&ei->ordered_tree);
Signature · 1 line
2.26.3
PATCH 25/31 btrfs: unify the lock/unlock extent variants
In reply to Josef BacikWe have two variants of lock/unlock extent, one set that takes a cached state, another that does not. This is slightly annoying, and generally speaking there are only a few places where we don't have a cached state. Simplify this by making lock_extent/unlock_extent the only variant and make it take a cached state, then convert all the callers appropriately.
--- fs/btrfs/compression.c | 6 +- fs/btrfs/disk-io.c | 7 +-- fs/btrfs/extent-io-tree.c | 4 +- fs/btrfs/extent-io-tree.h | 22 ++----- fs/btrfs/extent_io.c | 40 ++++++------- fs/btrfs/file.c | 46 +++++++-------- fs/btrfs/free-space-cache.c | 16 +++--- fs/btrfs/inode.c | 98 +++++++++++++++----------------- fs/btrfs/ioctl.c | 22 +++---- fs/btrfs/ordered-data.c | 4 +- fs/btrfs/reflink.c | 8 +-- fs/btrfs/relocation.c | 18 +++--- fs/btrfs/tests/extent-io-tests.c | 6 +- fs/btrfs/tree-log.c | 8 +-- 14 files changed, 142 insertions(+), 163 deletions(-)
fs/btrfs/compression.c +3 -3
@@ -588,7 +588,7 @@ static noinline int add_ra_bio_pages(struct inode *inode, } page_end = (pg_index <<PAGE_SHIFT ) +PAGE_SIZE -1 ; -lock_extent (tree, cur, page_end); +lock_extent (tree, cur, page_end,NULL );read_lock (&em_tree->lock); em =lookup_extent_mapping (em_tree, cur, page_end +1 - cur);read_unlock (&em_tree->lock);
@@ -602,7 +602,7 @@ static noinline int add_ra_bio_pages(struct inode *inode, (cur + fs_info->sectorsize > extent_map_end(em)) || (em->block_start >> 9) != cb->orig_bio->bi_iter.bi_sector) {free_extent_map (em); -unlock_extent (tree, cur, page_end); +unlock_extent (tree, cur, page_end,NULL );unlock_page (page);put_page (page);break ;
@@ -622,7 +622,7 @@ static noinline int add_ra_bio_pages(struct inode *inode, add_size =min (em->start + em->len, page_end +1 ) - cur; ret =bio_add_page (cb->orig_bio, page, add_size,offset_in_page (cur));if (ret != add_size) { -unlock_extent (tree, cur, page_end); +unlock_extent (tree, cur, page_end,NULL );unlock_page (page);put_page (page);break ;
fs/btrfs/disk-io.c +3 -4
@@ -131,8 +131,7 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,if (atomic)return -EAGAIN ; -lock_extent_bits (io_tree, eb->start, eb->start + eb->len -1 , - &cached_state); +lock_extent (io_tree, eb->start, eb->start + eb->len -1 , &cached_state);if (extent_buffer_uptodate (eb) &&btrfs_header_generation (eb) == parent_transid) { ret =0 ;
@@ -145,8 +144,8 @@ static int verify_parent_transid(struct extent_io_tree *io_tree, ret =1 ;clear_extent_buffer_uptodate (eb); out: -unlock_extent_cached (io_tree, eb->start, eb->start + eb->len -1 , - &cached_state); +unlock_extent (io_tree, eb->start, eb->start + eb->len -1 , + &cached_state);return ret; }
fs/btrfs/extent-io-tree.c +2 -2
@@ -1234,8 +1234,8 @@ int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,* either insert or lock state struct between start and end use mask to tell * us if waiting is desired. */ -int lock_extent_bits (struct extent_io_tree *tree,u64 start,u64 end, -struct extent_state **cached_state) +int lock_extent (struct extent_io_tree *tree,u64 start,u64 end, +struct extent_state **cached_state) {int err;u64 failed_start;
fs/btrfs/extent-io-tree.h +6 -16
@@ -98,13 +98,8 @@ void extent_io_tree_init(struct btrfs_fs_info *fs_info, void *private_data);void extent_io_tree_release (struct extent_io_tree *tree); -int lock_extent_bits (struct extent_io_tree *tree,u64 start,u64 end, -struct extent_state **cached); - -static inline int lock_extent (struct extent_io_tree *tree,u64 start,u64 end) -{ -return lock_extent_bits (tree, start, end,NULL ); -} +int lock_extent (struct extent_io_tree *tree,u64 start,u64 end, +struct extent_state **cached);int try_lock_extent (struct extent_io_tree *tree,u64 start,u64 end);
@@ -132,20 +127,15 @@ static inline int clear_extent_bit(struct extent_io_tree *tree, u64 start, GFP_NOFS, NULL); } -static inline int unlock_extent (struct extent_io_tree *tree,u64 start,u64 end) -{ -return clear_extent_bit (tree, start, end,EXTENT_LOCKED ,0 ,NULL ); -} - -static inline int unlock_extent_cached (struct extent_io_tree *tree,u64 start, -u64 end,struct extent_state **cached) +static inline int unlock_extent (struct extent_io_tree *tree,u64 start,u64 end, +struct extent_state **cached) {return __clear_extent_bit (tree, start, end,EXTENT_LOCKED ,0 , cached,GFP_NOFS ,NULL ); } -static inline int unlock_extent_cached_atomic (struct extent_io_tree *tree, -u64 start,u64 end,struct extent_state **cached) +static inline int unlock_extent_atomic (struct extent_io_tree *tree,u64 start, +u64 end,struct extent_state **cached) {return __clear_extent_bit (tree, start, end,EXTENT_LOCKED ,0 , cached,GFP_ATOMIC ,NULL );
fs/btrfs/extent_io.c +18 -22
@@ -463,14 +463,14 @@ noinline_for_stack bool find_lock_delalloc_range(struct inode *inode, }/* step three, lock the state bits for the whole range */ -lock_extent_bits (tree, delalloc_start, delalloc_end, &cached_state); +lock_extent (tree, delalloc_start, delalloc_end, &cached_state);/* then test to make sure it is all still delalloc */ ret =test_range_bit (tree, delalloc_start, delalloc_end,EXTENT_DELALLOC ,1 , cached_state);if (!ret) { -unlock_extent_cached (tree, delalloc_start, delalloc_end, - &cached_state); +unlock_extent (tree, delalloc_start, delalloc_end, + &cached_state);__unlock_for_delalloc (inode, locked_page, delalloc_start, delalloc_end);cond_resched ();
@@ -914,8 +914,8 @@ static void end_sector_io(struct page *page, u64 offset, bool uptodate)if (uptodate)set_extent_uptodate (&inode->io_tree, offset, offset + sectorsize -1 , &cached,GFP_ATOMIC ); -unlock_extent_cached_atomic (&inode->io_tree, offset, - offset + sectorsize -1 , &cached); +unlock_extent_atomic (&inode->io_tree, offset, offset + sectorsize -1 , + &cached); } static void submit_data_read_repair(struct inode *inode,
@@ -1119,8 +1119,7 @@ static void endio_readpage_release_extent(struct processed_extent *processed,* Now we don't have range contiguous to the processed range, release * the processed range now. */ -unlock_extent_cached_atomic (tree, processed->start, processed->end, - &cached); +unlock_extent_atomic (tree, processed->start, processed->end, &cached); update:/* Update processed to current range */
@@ -1762,7 +1761,7 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached, ret =set_page_extent_mapped (page);if (ret <0 ) { -unlock_extent (tree, start, end); +unlock_extent (tree, start, end,NULL );btrfs_page_set_error (fs_info, page, start,PAGE_SIZE );unlock_page (page); goto out;
@@ -1790,15 +1789,14 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,memzero_page (page, pg_offset, iosize);set_extent_uptodate (tree, cur, cur + iosize -1 , &cached,GFP_NOFS ); -unlock_extent_cached (tree, cur, - cur + iosize -1 , &cached); +unlock_extent (tree, cur, cur + iosize -1 , &cached);end_page_read (page, true, cur, iosize);break ; } em =__get_extent_map (inode, page, pg_offset, cur, end - cur +1 , em_cached);if (IS_ERR (em)) { -unlock_extent (tree, cur, end); +unlock_extent (tree, cur, end,NULL );end_page_read (page, false, cur, end +1 - cur); ret =PTR_ERR (em);break ;
@@ -1873,8 +1871,7 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,set_extent_uptodate (tree, cur, cur + iosize -1 , &cached,GFP_NOFS ); -unlock_extent_cached (tree, cur, - cur + iosize -1 , &cached); +unlock_extent (tree, cur, cur + iosize -1 , &cached);end_page_read (page, true, cur, iosize); cur = cur + iosize; pg_offset += iosize;
@@ -1882,7 +1879,7 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached, }/* the get_extent function already copied into the page */ if (block_start ==EXTENT_MAP_INLINE ) { -unlock_extent (tree, cur, cur + iosize -1 ); +unlock_extent (tree, cur, cur + iosize -1 ,NULL );end_page_read (page, true, cur, iosize); cur = cur + iosize; pg_offset += iosize;
@@ -1898,7 +1895,7 @@ static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached,* We have to unlock the remaining range, or the page * will never be unlocked. */ -unlock_extent (tree, cur, end); +unlock_extent (tree, cur, end,NULL );end_page_read (page, false, cur, end +1 - cur); goto out; }
@@ -3365,7 +3362,7 @@ int extent_invalidate_folio(struct extent_io_tree *tree,if (start > end)return 0 ; -lock_extent_bits (tree, start, end, &cached_state); +lock_extent (tree, start, end, &cached_state);folio_wait_writeback (folio);/*
@@ -3373,7 +3370,7 @@ int extent_invalidate_folio(struct extent_io_tree *tree,* so here we only need to unlock the extent range to free any * existing extent state. */ -unlock_extent_cached (tree, start, end, &cached_state); +unlock_extent (tree, start, end, &cached_state);return 0 ; }
@@ -3738,7 +3735,7 @@ int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo, last_for_get_extent = isize; } -lock_extent_bits (&inode->io_tree, start, start + len -1 , +lock_extent (&inode->io_tree, start, start + len -1 , &cached_state); em =get_extent_skip_holes (inode, start, last_for_get_extent);
@@ -3851,8 +3848,7 @@ int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo, ret =emit_last_fiemap_cache (fieinfo, &cache);free_extent_map (em); out: -unlock_extent_cached (&inode->io_tree, start, start + len -1 , - &cached_state); +unlock_extent (&inode->io_tree, start, start + len -1 , &cached_state); out_free_ulist:btrfs_free_path (path);
@@ -4695,7 +4691,7 @@ static int read_extent_buffer_subpage(struct extent_buffer *eb, int wait,if (!try_lock_extent (io_tree, eb->start, eb->start + eb->len -1 ))return -EAGAIN ; }else { - ret =lock_extent (io_tree, eb->start, eb->start + eb->len -1 ); + ret =lock_extent (io_tree, eb->start, eb->start + eb->len -1 ,NULL );if (ret <0 )return ret; }
@@ -4705,7 +4701,7 @@ static int read_extent_buffer_subpage(struct extent_buffer *eb, int wait, PageUptodate(page) || btrfs_subpage_test_uptodate(fs_info, page, eb->start, eb->len)) {set_bit (EXTENT_BUFFER_UPTODATE , &eb->bflags); -unlock_extent (io_tree, eb->start, eb->start + eb->len -1 ); +unlock_extent (io_tree, eb->start, eb->start + eb->len -1 ,NULL );return ret; }
fs/btrfs/file.c +22 -24
123 lines not on this page · raw message
fs/btrfs/free-space-cache.c +8 -8
@@ -348,7 +348,7 @@ int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,btrfs_i_size_write (inode,0 );truncate_pagecache (vfs_inode,0 ); -lock_extent_bits (&inode->io_tree,0 , (u64)-1 , &cached_state); +lock_extent (&inode->io_tree,0 , (u64)-1 , &cached_state);btrfs_drop_extent_cache (inode,0 , (u64)-1 ,0 );/*
@@ -360,7 +360,7 @@ int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,inode_sub_bytes (&inode->vfs_inode, control.sub_bytes);btrfs_inode_safe_disk_i_size_write (inode, control.last_size); -unlock_extent_cached (&inode->io_tree,0 , (u64)-1 , &cached_state); +unlock_extent (&inode->io_tree,0 , (u64)-1 , &cached_state);if (ret) goto fail;
@@ -1292,8 +1292,8 @@ cleanup_write_cache_enospc(struct inode *inode, struct extent_state **cached_state) {io_ctl_drop_pages (io_ctl); -unlock_extent_cached (&BTRFS_I (inode)->io_tree,0 , -i_size_read (inode) -1 , cached_state); +unlock_extent (&BTRFS_I (inode)->io_tree,0 ,i_size_read (inode) -1 , + cached_state); } static int __btrfs_wait_cache_io(struct btrfs_root *root,
@@ -1418,8 +1418,8 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,if (ret) goto out_unlock; -lock_extent_bits (&BTRFS_I (inode)->io_tree,0 ,i_size_read (inode) -1 , - &cached_state); +lock_extent (&BTRFS_I (inode)->io_tree,0 ,i_size_read (inode) -1 , + &cached_state);io_ctl_set_generation (io_ctl, trans->transid);
@@ -1474,8 +1474,8 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,io_ctl_drop_pages (io_ctl);io_ctl_free (io_ctl); -unlock_extent_cached (&BTRFS_I (inode)->io_tree,0 , -i_size_read (inode) -1 , &cached_state); +unlock_extent (&BTRFS_I (inode)->io_tree,0 ,i_size_read (inode) -1 , + &cached_state);/* * at this point the pages are under IO and we're happy,
fs/btrfs/inode.c +47 -51
343 lines not on this page · raw message
fs/btrfs/ioctl.c +11 -11
@@ -1218,10 +1218,10 @@ static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start,/* get the big lock and read metadata off disk */ if (!locked) -lock_extent_bits (io_tree, start, end, &cached); +lock_extent (io_tree, start, end, &cached); em =defrag_get_extent (BTRFS_I (inode), start, newer_than);if (!locked) -unlock_extent_cached (io_tree, start, end, &cached); +unlock_extent (io_tree, start, end, &cached);if (IS_ERR (em))return NULL ;
@@ -1333,10 +1333,10 @@ static struct page *defrag_prepare_one_page(struct btrfs_inode *inode,while (1 ) {struct btrfs_ordered_extent *ordered; -lock_extent_bits (&inode->io_tree, page_start, page_end, &cached_state); +lock_extent (&inode->io_tree, page_start, page_end, &cached_state); ordered =btrfs_lookup_ordered_range (inode, page_start,PAGE_SIZE ); -unlock_extent_cached (&inode->io_tree, page_start, page_end, - &cached_state); +unlock_extent (&inode->io_tree, page_start, page_end, + &cached_state);if (!ordered)break ;
@@ -1666,9 +1666,9 @@ static int defrag_one_range(struct btrfs_inode *inode, u64 start, u32 len,wait_on_page_writeback (pages[i]);/* Lock the pages range */ -lock_extent_bits (&inode->io_tree, start_index <<PAGE_SHIFT , - (last_index <<PAGE_SHIFT ) +PAGE_SIZE -1 , - &cached_state); +lock_extent (&inode->io_tree, start_index <<PAGE_SHIFT , + (last_index <<PAGE_SHIFT ) +PAGE_SIZE -1 , + &cached_state);/* * Now we have a consistent view about the extent map, re-check * which range really needs to be defragged.
@@ -1694,9 +1694,9 @@ static int defrag_one_range(struct btrfs_inode *inode, u64 start, u32 len,kfree (entry); } unlock_extent: -unlock_extent_cached (&inode->io_tree, start_index <<PAGE_SHIFT , - (last_index <<PAGE_SHIFT ) +PAGE_SIZE -1 , - &cached_state); +unlock_extent (&inode->io_tree, start_index <<PAGE_SHIFT , + (last_index <<PAGE_SHIFT ) +PAGE_SIZE -1 , + &cached_state); free_pages:for (i =0 ; i < nr_pages; i++) {if (pages[i]) {
fs/btrfs/ordered-data.c +2 -2
@@ -1043,7 +1043,7 @@ void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start, cachedp = cached_state;while (1 ) { -lock_extent_bits (&inode->io_tree, start, end, cachedp); +lock_extent (&inode->io_tree, start, end, cachedp); ordered =btrfs_lookup_ordered_range (inode, start, end - start +1 );if (!ordered) {
@@ -1056,7 +1056,7 @@ void btrfs_lock_and_flush_ordered_range(struct btrfs_inode *inode, u64 start,refcount_dec (&cache->refs);break ; } -unlock_extent_cached (&inode->io_tree, start, end, cachedp); +unlock_extent (&inode->io_tree, start, end, cachedp);btrfs_start_ordered_extent (ordered,1 );btrfs_put_ordered_extent (ordered); }
fs/btrfs/reflink.c +4 -4
@@ -615,8 +615,8 @@ static int btrfs_clone(struct inode *src, struct inode *inode,static void btrfs_double_extent_unlock (struct inode *inode1,u64 loff1,struct inode *inode2,u64 loff2,u64 len) { -unlock_extent (&BTRFS_I (inode1)->io_tree, loff1, loff1 + len -1 ); -unlock_extent (&BTRFS_I (inode2)->io_tree, loff2, loff2 + len -1 ); +unlock_extent (&BTRFS_I (inode1)->io_tree, loff1, loff1 + len -1 ,NULL ); +unlock_extent (&BTRFS_I (inode2)->io_tree, loff2, loff2 + len -1 ,NULL ); } static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
@@ -634,8 +634,8 @@ static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,swap (range1_end, range2_end); } -lock_extent (&BTRFS_I (inode1)->io_tree, loff1, range1_end); -lock_extent (&BTRFS_I (inode2)->io_tree, loff2, range2_end); +lock_extent (&BTRFS_I (inode1)->io_tree, loff1, range1_end,NULL ); +lock_extent (&BTRFS_I (inode2)->io_tree, loff2, range2_end,NULL );btrfs_assert_inode_range_clean (BTRFS_I (inode1), loff1, range1_end);btrfs_assert_inode_range_clean (BTRFS_I (inode2), loff2, range2_end);
fs/btrfs/relocation.c +9 -9
@@ -1127,7 +1127,7 @@ int replace_file_extents(struct btrfs_trans_handle *trans,btrfs_drop_extent_cache (BTRFS_I (inode), key.offset, end,1 );unlock_extent (&BTRFS_I (inode)->io_tree, - key.offset, end); + key.offset, end,NULL ); } }
@@ -1566,9 +1566,9 @@ static int invalidate_extent_cache(struct btrfs_root *root, }/* the lock_extent waits for read_folio to complete */ -lock_extent (&BTRFS_I (inode)->io_tree, start, end); +lock_extent (&BTRFS_I (inode)->io_tree, start, end,NULL );btrfs_drop_extent_cache (BTRFS_I (inode), start, end,1 ); -unlock_extent (&BTRFS_I (inode)->io_tree, start, end); +unlock_extent (&BTRFS_I (inode)->io_tree, start, end,NULL ); }return 0 ; }
@@ -2869,13 +2869,13 @@ static noinline_for_stack int prealloc_file_extent_cluster(else end = cluster->end - offset; -lock_extent (&inode->io_tree, start, end); +lock_extent (&inode->io_tree, start, end,NULL ); num_bytes = end +1 - start; ret =btrfs_prealloc_file_range (&inode->vfs_inode,0 , start, num_bytes, num_bytes, end +1 , &alloc_hint); cur_offset = end +1 ; -unlock_extent (&inode->io_tree, start, end); +unlock_extent (&inode->io_tree, start, end,NULL );if (ret)break ; }
@@ -2904,7 +2904,7 @@ static noinline_for_stack int setup_relocation_extent_mapping(struct inode *inod em->block_start = block_start;set_bit (EXTENT_FLAG_PINNED , &em->flags); -lock_extent (&BTRFS_I (inode)->io_tree, start, end); +lock_extent (&BTRFS_I (inode)->io_tree, start, end,NULL );while (1 ) {write_lock (&em_tree->lock); ret =add_extent_mapping (em_tree, em,0 );
@@ -2915,7 +2915,7 @@ static noinline_for_stack int setup_relocation_extent_mapping(struct inode *inod }btrfs_drop_extent_cache (BTRFS_I (inode), start, end,0 ); } -unlock_extent (&BTRFS_I (inode)->io_tree, start, end); +unlock_extent (&BTRFS_I (inode)->io_tree, start, end,NULL );return ret; }
@@ -3006,7 +3006,7 @@ static int relocate_one_page(struct inode *inode, struct file_ra_state *ra, goto release_page;/* Mark the range delalloc and dirty for later writeback */ -lock_extent (&BTRFS_I (inode)->io_tree, clamped_start, clamped_end); +lock_extent (&BTRFS_I (inode)->io_tree, clamped_start, clamped_end,NULL ); ret =btrfs_set_extent_delalloc (BTRFS_I (inode), clamped_start, clamped_end,0 ,NULL );if (ret) {
@@ -3039,7 +3039,7 @@ static int relocate_one_page(struct inode *inode, struct file_ra_state *ra, boundary_start, boundary_end, EXTENT_BOUNDARY); } -unlock_extent (&BTRFS_I (inode)->io_tree, clamped_start, clamped_end); +unlock_extent (&BTRFS_I (inode)->io_tree, clamped_start, clamped_end,NULL );btrfs_delalloc_release_extents (BTRFS_I (inode), clamped_len); cur += clamped_len;
fs/btrfs/tests/extent-io-tests.c +3 -3
@@ -172,7 +172,7 @@ static int test_find_delalloc(u32 sectorsize) sectorsize - 1, start, end); goto out_bits; } -unlock_extent (tmp, start, end); +unlock_extent (tmp, start, end,NULL );unlock_page (locked_page);put_page (locked_page);
@@ -208,7 +208,7 @@ static int test_find_delalloc(u32 sectorsize)test_err ("there were unlocked pages in the range" ); goto out_bits; } -unlock_extent (tmp, start, end); +unlock_extent (tmp, start, end,NULL );/* locked_page was unlocked above */ put_page (locked_page);
@@ -263,7 +263,7 @@ static int test_find_delalloc(u32 sectorsize)test_err ("pages in range were not all locked" ); goto out_bits; } -unlock_extent (tmp, start, end); +unlock_extent (tmp, start, end,NULL );/* * Now to test where we run into a page that is no longer dirty in the
fs/btrfs/tree-log.c +4 -4
@@ -4271,8 +4271,8 @@ static int log_csums(struct btrfs_trans_handle *trans,* file which happens to refer to the same extent as well. Such races * can leave checksum items in the log with overlapping ranges. */ - ret =lock_extent_bits (&log_root->log_csum_range, sums->bytenr, - lock_end, &cached_state); + ret =lock_extent (&log_root->log_csum_range, sums->bytenr, lock_end, + &cached_state);if (ret)return ret;/*
@@ -4288,8 +4288,8 @@ static int log_csums(struct btrfs_trans_handle *trans,if (!ret) ret =btrfs_csum_file_blocks (trans, log_root, sums); -unlock_extent_cached (&log_root->log_csum_range, sums->bytenr, lock_end, - &cached_state); +unlock_extent (&log_root->log_csum_range, sums->bytenr, lock_end, + &cached_state);return ret; }
Signature · 1 line
2.26.3
PATCH 27/31 btrfs: get rid of ->dirty_bytes
In reply to Josef BacikThis was used as an optimization for count_range_bits(EXTENT_DIRTY), but we no longer do that, thus nothing actually uses the ->dirty_bytes counter in the extent_io_tree.
--- fs/btrfs/extent-io-tree.c | 17 +---------------- fs/btrfs/extent-io-tree.h | 1 - 2 files changed, 1 insertion(+), 17 deletions(-)
fs/btrfs/extent-io-tree.c +1 -16
@@ -91,7 +91,6 @@ void extent_io_tree_init(struct btrfs_fs_info *fs_info, { tree->fs_info = fs_info; tree->state =RB_ROOT ; - tree->dirty_bytes =0 ;spin_lock_init (&tree->lock); tree->private_data = private_data; tree->owner = owner;
@@ -486,12 +485,6 @@ static struct extent_state *clear_state_bit(struct extent_io_tree *tree,u32 bits_to_clear = bits & ~EXTENT_CTLBITS ;int ret; -if ((bits_to_clear &EXTENT_DIRTY ) && (state->state &EXTENT_DIRTY )) { -u64 range = state->end - state->start +1 ; -WARN_ON (range > tree->dirty_bytes); - tree->dirty_bytes -= range; - } -if (tree->private_data &&is_data_inode (tree->private_data))btrfs_clear_delalloc_extent (tree->private_data, state, bits);
@@ -756,10 +749,6 @@ static void set_state_bits(struct extent_io_tree *tree,if (tree->private_data &&is_data_inode (tree->private_data))btrfs_set_delalloc_extent (tree->private_data, state, bits); -if ((bits_to_set &EXTENT_DIRTY ) && !(state->state &EXTENT_DIRTY )) { -u64 range = state->end - state->start +1 ; - tree->dirty_bytes += range; - } ret =add_extent_changeset (state, bits_to_set, changeset,1 );BUG_ON (ret <0 ); state->state |= bits_to_set;
@@ -1538,10 +1527,7 @@ u64 count_range_bits(struct extent_io_tree *tree,return 0 ;spin_lock (&tree->lock); -if (cur_start ==0 && bits ==EXTENT_DIRTY ) { - total_bytes = tree->dirty_bytes; - goto out; - } +/* * this search will find all the extents that end after * our range starts.
@@ -1567,7 +1553,6 @@ u64 count_range_bits(struct extent_io_tree *tree, } state =next_state (state); } -out:spin_unlock (&tree->lock);return total_bytes; }
fs/btrfs/extent-io-tree.h +0 -1
@@ -69,7 +69,6 @@ struct extent_io_tree {struct rb_root state;struct btrfs_fs_info *fs_info;void *private_data; -u64 dirty_bytes;/* Who owns this io tree, should be one of IO_TREE_* */ u8 owner;
Signature · 1 line
2.26.3
PATCH 28/31 btrfs: don't clear CTL bits when trying to release extent state
In reply to Josef BacikWhen trying to release the extent states due to memory pressure we'll set all the bits except LOCKED, NODATASUM, and DELALLOC_NEW. This includes some of the CTL bits, which isn't really a problem but isn't correct either. Exclude the CTL bits from this clearing.
--- fs/btrfs/extent_io.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
fs/btrfs/extent_io.c +5 -3
@@ -3389,15 +3389,17 @@ static int try_release_extent_state(struct extent_io_tree *tree,if (test_range_bit (tree, start, end,EXTENT_LOCKED ,0 ,NULL )) { ret =0 ; }else { +u32 clear_bits = ~(EXTENT_LOCKED |EXTENT_NODATASUM | +EXTENT_DELALLOC_NEW |EXTENT_CTLBITS ); +/* * At this point we can safely clear everything except the * locked bit, the nodatasum bit and the delalloc new bit. * The delalloc new bit will be cleared by ordered extent * completion. */ - ret =__clear_extent_bit (tree, start, end, - ~(EXTENT_LOCKED |EXTENT_NODATASUM |EXTENT_DELALLOC_NEW ), -0 ,NULL , mask,NULL ); + ret =__clear_extent_bit (tree, start, end, clear_bits,0 ,NULL , + mask,NULL );/* if clear_extent_bit failed for enomem reasons, * we can't allow the release to continue.
Signature · 1 line
2.26.3
PATCH 29/31 btrfs: replace delete argument with EXTENT_CLEAR_ALL_BITS
In reply to Josef BacikInstead of taking up a whole argument to indicate we're clearing everything in a range, simply add another EXTENT bit to control this, and then update all the callers to drop this argument from the clear_extent_bit variants.
--- fs/btrfs/extent-io-tree.c | 17 ++++++++------- fs/btrfs/extent-io-tree.h | 28 +++++++++++++++--------- fs/btrfs/extent_io.c | 4 ++-- fs/btrfs/extent_map.c | 2 +- fs/btrfs/file-item.c | 2 +- fs/btrfs/file.c | 2 +- fs/btrfs/free-space-cache.c | 6 +++--- fs/btrfs/inode.c | 41 +++++++++++++++++------------------- fs/btrfs/ioctl.c | 2 +- fs/btrfs/reflink.c | 2 +- fs/btrfs/tests/inode-tests.c | 8 +++---- fs/btrfs/transaction.c | 2 +- 12 files changed, 61 insertions(+), 55 deletions(-)
fs/btrfs/extent-io-tree.c +9 -8
@@ -528,8 +528,7 @@ static void extent_io_tree_panic(struct extent_io_tree *tree, int err)* This takes the tree lock, and returns 0 on success and < 0 on error. */ int __clear_extent_bit (struct extent_io_tree *tree,u64 start,u64 end, -u32 bits,int delete, -struct extent_state **cached_state, +u32 bits,struct extent_state **cached_state,gfp_t mask,struct extent_changeset *changeset) {struct extent_state *state;
@@ -538,17 +537,19 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,u64 last_end;int err;int clear =0 ; -int wake = (bits &EXTENT_LOCKED ) ?1 :0 ; +int wake; +int delete = bits &EXTENT_CLEAR_ALL_BITS ;btrfs_debug_check_extent_io_range (tree, start, end);trace_btrfs_clear_extent_bit (tree, start, end - start +1 , bits); -if (bits &EXTENT_DELALLOC ) - bits |=EXTENT_NORESERVE ; -if (delete) bits |= ~EXTENT_CTLBITS ; +if (bits &EXTENT_DELALLOC ) + bits |=EXTENT_NORESERVE ; + + wake = (bits &EXTENT_LOCKED ) ?1 :0 ;if (bits & (EXTENT_LOCKED |EXTENT_BOUNDARY )) clear =1 ; again:
@@ -1634,7 +1635,7 @@ int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,*/ ASSERT (!(bits &EXTENT_LOCKED )); -return __clear_extent_bit (tree, start, end, bits,0 ,NULL ,GFP_NOFS , +return __clear_extent_bit (tree, start, end, bits,NULL ,GFP_NOFS , changeset); }
@@ -1648,7 +1649,7 @@ int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)if (err == -EEXIST ) {if (failed_start > start)clear_extent_bit (tree, start, failed_start -1 , -EXTENT_LOCKED ,0 ,NULL ); +EXTENT_LOCKED ,NULL );return 0 ; }return 1 ;
fs/btrfs/extent-io-tree.h +18 -10
@@ -35,10 +35,18 @@ struct io_failure_record;* delalloc bytes decremented, in an atomic way to prevent races with stat(2). */ #define EXTENT_ADD_INODE_BYTES (1U << 15) + +/* +* Set during truncate when we're clearing an entire range and we just want the +* extent states to go away. +*/ +#define EXTENT_CLEAR_ALL_BITS (1U << 16) +#define EXTENT_DO_ACCOUNTING (EXTENT_CLEAR_META_RESV | \ EXTENT_CLEAR_DATA_RESV)#define EXTENT_CTLBITS (EXTENT_DO_ACCOUNTING | \ - EXTENT_ADD_INODE_BYTES) + EXTENT_ADD_INODE_BYTES | \ + EXTENT_CLEAR_ALL_BITS)/* * Redefined bits above which are used only in the device allocation tree,
@@ -114,35 +122,35 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,int clear_record_extent_bits (struct extent_io_tree *tree,u64 start,u64 end,u32 bits,struct extent_changeset *changeset);int __clear_extent_bit (struct extent_io_tree *tree,u64 start,u64 end, -u32 bits,int delete,struct extent_state **cached, -gfp_t mask,struct extent_changeset *changeset); +u32 bits,struct extent_state **cached,gfp_t mask, +struct extent_changeset *changeset);static inline int clear_extent_bit (struct extent_io_tree *tree,u64 start, -u64 end,u32 bits,int delete, +u64 end,u32 bits,struct extent_state **cached) { -return __clear_extent_bit (tree, start, end, bits, delete, cached, +return __clear_extent_bit (tree, start, end, bits, cached,GFP_NOFS ,NULL ); }static inline int unlock_extent (struct extent_io_tree *tree,u64 start,u64 end,struct extent_state **cached) { -return __clear_extent_bit (tree, start, end,EXTENT_LOCKED ,0 , cached, +return __clear_extent_bit (tree, start, end,EXTENT_LOCKED , cached,GFP_NOFS ,NULL ); }static inline int unlock_extent_atomic (struct extent_io_tree *tree,u64 start,u64 end,struct extent_state **cached) { -return __clear_extent_bit (tree, start, end,EXTENT_LOCKED ,0 , cached, +return __clear_extent_bit (tree, start, end,EXTENT_LOCKED , cached,GFP_ATOMIC ,NULL ); }static inline int clear_extent_bits (struct extent_io_tree *tree,u64 start,u64 end,u32 bits) { -return clear_extent_bit (tree, start, end, bits,0 ,NULL ); +return clear_extent_bit (tree, start, end, bits,NULL ); } int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
@@ -165,7 +173,7 @@ static inline int set_extent_bits(struct extent_io_tree *tree, u64 start,static inline int clear_extent_uptodate (struct extent_io_tree *tree,u64 start,u64 end,struct extent_state **cached_state) { -return __clear_extent_bit (tree, start, end,EXTENT_UPTODATE ,0 , +return __clear_extent_bit (tree, start, end,EXTENT_UPTODATE , cached_state,GFP_NOFS ,NULL ); }
@@ -180,7 +188,7 @@ static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start, {return clear_extent_bit (tree, start, end,EXTENT_DIRTY |EXTENT_DELALLOC | -EXTENT_DO_ACCOUNTING ,0 , cached); +EXTENT_DO_ACCOUNTING , cached); } int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
fs/btrfs/extent_io.c +2 -2
@@ -487,7 +487,7 @@ void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,struct page *locked_page, u32 clear_bits, unsigned long page_ops) { - clear_extent_bit(&inode->io_tree, start, end, clear_bits, 0, NULL); + clear_extent_bit(&inode->io_tree, start, end, clear_bits, NULL);__process_pages_contig (inode->vfs_inode.i_mapping, locked_page, start, end, page_ops,NULL );
@@ -3398,7 +3398,7 @@ static int try_release_extent_state(struct extent_io_tree *tree,* The delalloc new bit will be cleared by ordered extent * completion. */ - ret =__clear_extent_bit (tree, start, end, clear_bits,0 ,NULL , + ret =__clear_extent_bit (tree, start, end, clear_bits,NULL , mask,NULL );/* if clear_extent_bit failed for enomem reasons,
fs/btrfs/extent_map.c +1 -1
@@ -382,7 +382,7 @@ static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)__clear_extent_bit (&device->alloc_state, stripe->physical, stripe->physical + stripe_size -1 , bits, -0 ,NULL ,GFP_NOWAIT ,NULL ); +NULL ,GFP_NOWAIT ,NULL ); } }
fs/btrfs/file-item.c +1 -1
@@ -118,7 +118,7 @@ int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start,if (btrfs_fs_incompat (inode->root->fs_info,NO_HOLES ))return 0 ;return clear_extent_bit (&inode->file_extent_tree, start, - start + len -1 ,EXTENT_DIRTY ,0 ,NULL ); + start + len -1 ,EXTENT_DIRTY ,NULL ); } static inline u32 max_ordered_sum_bytes(struct btrfs_fs_info *fs_info,
fs/btrfs/file.c +1 -1
@@ -473,7 +473,7 @@ int btrfs_dirty_pages(struct btrfs_inode *inode, struct page **pages,*/ clear_extent_bit (&inode->io_tree, start_pos, end_of_last_block,EXTENT_DELALLOC |EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG , -0 , cached); + cached); err =btrfs_set_extent_delalloc (inode, start_pos, end_of_last_block, extra_bits, cached);
fs/btrfs/free-space-cache.c +3 -3
@@ -1163,7 +1163,7 @@ update_cache_item(struct btrfs_trans_handle *trans, ret =btrfs_search_slot (trans, root, &key, path,0 ,1 );if (ret <0 ) {clear_extent_bit (&BTRFS_I (inode)->io_tree,0 , inode->i_size -1 , -EXTENT_DELALLOC ,0 ,NULL ); +EXTENT_DELALLOC ,NULL ); goto fail; } leaf = path->nodes[0 ];
@@ -1175,7 +1175,7 @@ update_cache_item(struct btrfs_trans_handle *trans,if (found_key.objectid !=BTRFS_FREE_SPACE_OBJECTID || found_key.offset != offset) {clear_extent_bit (&BTRFS_I (inode)->io_tree,0 , - inode->i_size -1 ,EXTENT_DELALLOC ,0 , + inode->i_size -1 ,EXTENT_DELALLOC ,NULL );btrfs_release_path (path); goto fail;
@@ -1272,7 +1272,7 @@ static int flush_dirty_cache(struct inode *inode) ret =btrfs_wait_ordered_range (inode,0 , (u64)-1 );if (ret)clear_extent_bit (&BTRFS_I (inode)->io_tree,0 , inode->i_size -1 , -EXTENT_DELALLOC ,0 ,NULL ); +EXTENT_DELALLOC ,NULL );return ret; }
fs/btrfs/inode.c +19 -22
127 lines not on this page · raw message
fs/btrfs/ioctl.c +1 -1
@@ -1616,7 +1616,7 @@ static int defrag_one_locked_target(struct btrfs_inode *inode,return ret;clear_extent_bit (&inode->io_tree, start, start + len -1 ,EXTENT_DELALLOC |EXTENT_DO_ACCOUNTING | -EXTENT_DEFRAG ,0 , cached_state); +EXTENT_DEFRAG , cached_state);set_extent_defrag (&inode->io_tree, start, start + len -1 , cached_state);/* Update the page status */
fs/btrfs/reflink.c +1 -1
@@ -92,7 +92,7 @@ static int copy_inline_to_page(struct btrfs_inode *inode,clear_extent_bit (&inode->io_tree, file_offset, range_end,EXTENT_DELALLOC |EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG , -0 ,NULL ); +NULL ); ret =btrfs_set_extent_delalloc (inode, file_offset, range_end,0 ,NULL );if (ret) goto out_unlock;
fs/btrfs/tests/inode-tests.c +4 -4
@@ -975,7 +975,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_DELALLOC_NEW | - EXTENT_UPTODATE, 0, NULL); + EXTENT_UPTODATE, NULL);if (ret) {test_err ("clear_extent_bit returned %d" , ret); goto out;
@@ -1043,7 +1043,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)BTRFS_MAX_EXTENT_SIZE + sectorsize,BTRFS_MAX_EXTENT_SIZE +2 * sectorsize -1 ,EXTENT_DELALLOC |EXTENT_DELALLOC_NEW | - EXTENT_UPTODATE, 0, NULL); + EXTENT_UPTODATE, NULL);if (ret) {test_err ("clear_extent_bit returned %d" , ret); goto out;
@@ -1076,7 +1076,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)/* Empty */ ret =clear_extent_bit (&BTRFS_I (inode)->io_tree,0 , (u64)-1 ,EXTENT_DELALLOC |EXTENT_DELALLOC_NEW | -EXTENT_UPTODATE ,0 ,NULL ); +EXTENT_UPTODATE ,NULL );if (ret) {test_err ("clear_extent_bit returned %d" , ret); goto out;
@@ -1092,7 +1092,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)if (ret)clear_extent_bit (&BTRFS_I (inode)->io_tree,0 , (u64)-1 ,EXTENT_DELALLOC |EXTENT_DELALLOC_NEW | -EXTENT_UPTODATE ,0 ,NULL ); +EXTENT_UPTODATE ,NULL );iput (inode);btrfs_free_dummy_root (root);btrfs_free_dummy_fs_info (fs_info);
fs/btrfs/transaction.c +1 -1
@@ -1116,7 +1116,7 @@ static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info,* it's safe to do it (through extent_io_tree_release()). */ err =clear_extent_bit (dirty_pages, start, end, -EXTENT_NEED_WAIT ,0 , &cached_state); +EXTENT_NEED_WAIT , &cached_state);if (err == -ENOMEM ) err =0 ;if (!err)
Signature · 1 line
2.26.3
PATCH 30/31 btrfs: don't init io tree with private data for non inodes
In reply to Josef BacikWe only use this for normal inodes, so don't set it if we're not a normal inode.
--- fs/btrfs/disk-io.c | 2 +- fs/btrfs/inode.c | 2 +- fs/btrfs/transaction.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/disk-io.c +1 -1
@@ -2231,7 +2231,7 @@ static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)RB_CLEAR_NODE (&BTRFS_I (inode)->rb_node);extent_io_tree_init (fs_info, &BTRFS_I (inode)->io_tree, -IO_TREE_BTREE_INODE_IO , inode); +IO_TREE_BTREE_INODE_IO ,NULL );extent_map_tree_init (&BTRFS_I (inode)->extent_tree);BTRFS_I (inode)->root =btrfs_grab_root (fs_info->tree_root);
fs/btrfs/inode.c +1 -1
@@ -8909,7 +8909,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)extent_map_tree_init (&ei->extent_tree);extent_io_tree_init (fs_info, &ei->io_tree,IO_TREE_INODE_IO , inode);extent_io_tree_init (fs_info, &ei->file_extent_tree, -IO_TREE_INODE_FILE_EXTENT , inode); +IO_TREE_INODE_FILE_EXTENT ,NULL ); ei->io_failure_tree =RB_ROOT ;atomic_set (&ei->sync_writers,0 );mutex_init (&ei->log_mutex);
fs/btrfs/transaction.c +1 -1
@@ -365,7 +365,7 @@ static noinline int join_transaction(struct btrfs_fs_info *fs_info,spin_lock_init (&cur_trans->releasing_ebs_lock);list_add_tail (&cur_trans->list, &fs_info->trans_list);extent_io_tree_init (fs_info, &cur_trans->dirty_pages, -IO_TREE_TRANS_DIRTY_PAGES , fs_info->btree_inode); +IO_TREE_TRANS_DIRTY_PAGES ,NULL );extent_io_tree_init (fs_info, &cur_trans->pinned_extents,IO_TREE_FS_PINNED_EXTENTS ,NULL ); fs_info->generation++;
Signature · 1 line
2.26.3
PATCH 31/31 btrfs: remove is_data_inode() checks in extent-io-tree.c
In reply to Josef BacikWe're only init'ing extent_io_tree's with a private data if we're a normal inode, so we don't need this extra check.
--- fs/btrfs/extent-io-tree.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
fs/btrfs/extent-io-tree.c +6 -6
@@ -59,7 +59,7 @@ static inline void __btrfs_debug_check_extent_io_range(const char *caller,struct inode *inode = tree->private_data;u64 isize; -if (!inode || !is_data_inode (inode)) +if (!inode)return ; isize =i_size_read (inode);
@@ -332,7 +332,7 @@ static void merge_state(struct extent_io_tree *tree, other =prev_state (state);if (other && other->end == state->start -1 && other->state == state->state) { -if (tree->private_data &&is_data_inode (tree->private_data)) +if (tree->private_data)btrfs_merge_delalloc_extent (tree->private_data, state, other); state->start = other->start;
@@ -343,7 +343,7 @@ static void merge_state(struct extent_io_tree *tree, other =next_state (state);if (other && other->start == state->end +1 && other->state == state->state) { -if (tree->private_data &&is_data_inode (tree->private_data)) +if (tree->private_data)btrfs_merge_delalloc_extent (tree->private_data, state, other); state->end = other->end;
@@ -437,7 +437,7 @@ static int split_state(struct extent_io_tree *tree, struct extent_state *orig,struct rb_node *parent =NULL ;struct rb_node **node; -if (tree->private_data &&is_data_inode (tree->private_data)) +if (tree->private_data)btrfs_split_delalloc_extent (tree->private_data, orig, split); prealloc->start = orig->start;
@@ -485,7 +485,7 @@ static struct extent_state *clear_state_bit(struct extent_io_tree *tree,u32 bits_to_clear = bits & ~EXTENT_CTLBITS ;int ret; -if (tree->private_data &&is_data_inode (tree->private_data)) +if (tree->private_data)btrfs_clear_delalloc_extent (tree->private_data, state, bits); ret =add_extent_changeset (state, bits_to_clear, changeset,0 );
@@ -747,7 +747,7 @@ static void set_state_bits(struct extent_io_tree *tree,u32 bits_to_set = bits & ~EXTENT_CTLBITS ;int ret; -if (tree->private_data &&is_data_inode (tree->private_data)) +if (tree->private_data)btrfs_set_delalloc_extent (tree->private_data, state, bits); ret =add_extent_changeset (state, bits_to_set, changeset,1 );
Signature · 1 line
2.26.3
kernel test robot
build botRe: [PATCH 12/31] btrfs: move the core extent_io_tree code into extent-io-tree.c
In reply to Josef BacikI love your patch! Perhaps something to improve:
Hi Josef,
I love your patch! Perhaps something to improve:
[auto build test WARNING on kdave/for-next]
[also build test WARNING on next-20220901]
[cannot apply to rostedt-trace/for-next linus/master v6.0-rc3] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Josef-Bacik/btrfs-move-extent_io_tree-code-and-cleanups/20220903-042359 base: https://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux.git for-next config: alpha-allyesconfig (https://download.01.org/0day-ci/archive/20220903/[email protected]/config) compiler: alpha-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/9fa98f420ce2ac5220d35bedf881d5e6bbd18e9d
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Josef-Bacik/btrfs-move-extent_io_tree-code-and-cleanups/20220903-042359 git checkout 9fa98f420ce2ac5220d35bedf881d5e6bbd18e9d
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=alpha SHELL=/bin/bash fs/btrfs/
If you fix the issue, kindly add following tag where applicable
All warnings (new ones prefixed by >>):
>> fs/btrfs/extent-io-tree.c:237: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst
* Search @tree for an entry that contains @offset. Such entry would have
fs/btrfs/extent-io-tree.c:298: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst
* Search offset in the tree or fill neighbor rbtree node pointers.
fs/btrfs/extent-io-tree.c:1393: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst
* Find a contiguous area of bits
fs/btrfs/extent-io-tree.c:1431: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst
* Find the first range that has @bits not set. This range could start before
vim +237 fs/btrfs/extent-io-tree.c
235
236 /**
> 237 * Search @tree for an entry that contains @offset. Such entry would have 238 * entry->start <= offset && entry->end >= offset. 239 * 240 * @tree: the tree to search 241 * @offset: offset that should fall within an entry in @tree 242 * @node_ret: pointer where new node should be anchored (used when inserting an
243 * entry in the tree)
244 * @parent_ret: points to entry which would have been the parent of the entry,
245 * containing @offset
246 *
247 * Return a pointer to the entry that contains @offset byte address and don't change
248 * @node_ret and @parent_ret.
249 *
250 * If no such entry exists, return pointer to entry that ends before @offset
251 * and fill parameters @node_ret and @parent_ret, ie. does not return NULL.
252 */
253 static inline struct rb_node *tree_search_for_insert(struct extent_io_tree *tree,
254 u64 offset,
255 struct rb_node ***node_ret,
256 struct rb_node **parent_ret)
257 {
258 struct rb_root *root = &tree->state;
259 struct rb_node **node = &root->rb_node;
260 struct rb_node *prev = NULL;
261 struct tree_entry *entry;
262
263 while (*node) {
264 prev = *node;
265 entry = rb_entry(prev, struct tree_entry, rb_node);
266
267 if (offset < entry->start)
268 node = &(*node)->rb_left;
269 else if (offset > entry->end)
270 node = &(*node)->rb_right;
271 else
272 return *node;
273 }
274
275 if (node_ret)
276 *node_ret = node;
277 if (parent_ret)
278 *parent_ret = prev;
279
280 /* Search neighbors until we find the first one past the end */
281 while (prev && offset > entry->end) {
282 prev = rb_next(prev);
283 entry = rb_entry(prev, struct tree_entry, rb_node);
284 }
285
286 return prev;
287 }
288
Signature · 2 lines
0-DAY CI Kernel Test Service https://01.org/lkp
Re: [PATCH 01/31] btrfs: cleanup clean_io_failure
In reply to Josef BacikQuoted 4 lines · Josef Bacik
This is exported, so rename it to
btrfs_clean_io_failure. Additionally we are passing in the io tree's and such from the inode, so instead of doing all that simply pass in the inode itself and get all the components we need directly inside ofbtrfs_clean_io_failure.
Just a heads up, Christoph's latest series is completely removing the io_failure_tree including clean_io_failure:
Quoted 31 lines · Josef Bacik
In working on extent tree v2 I got really bogged down in trying to sync work between btrfs-progs and the kernel. It basically takes me 3x as long, because there's a lot of different things missing in btrfs-progs, so each patchset has to be done from scratch and tested completely differently.
Additionally there's just a lot of tech-debt in these areas in general. So before tackling the rest of extent-tree-v2 I'm spending some time cleaning up things we all know are terrible. This is the first step in that direction, finishing the separation of the
extent_io_treecode fromextent_io.c. I started this a while ago, but got bogged down in other things.This has 3 distinct parts
1. Cleanup the
io_failure_recordcode. This has been tightly integrated into theextent_io_treecode forever without much reason for it. The first part of this series moves that handling into it's own tree, and uses our existing helpers to reduce the code quite a bit.2. Move the code out of
extent_io.c. This is easier than previous code moves because I did a lot of the prep work earlier. Unfortunately there is one big patch that copy and pastes all the core code, since it all depends on itself and would be more annoying to move. However the independent parts were moved piece by piece.3. A wholesale cleanup of the
extent_io_treecode. We have a ton of helpers here, that have all grown a ton of arguments over the years. I've trimmed down the arguments for our core helpers, and hidden the rest internally inside ofextent-io-tree.c. Additionally I've cleaned up the lock/unlock extent bit helpers so we only have one lock_extent/unlock_extent variant that gets used everywhere.
Quoted 3 lines
I've tested this locally to make sure I didn't break anything. This isn't a simple code move so do please review most of it, the patches that start with "move X" are pure code move patches, but the rest do change things. Thanks,
As this is mostly simple cleanups or straightfowrad changes I think it's safe to merge before code freeze. I'll send more comments to the patches, there are some minor issues or missing commit references to last code that used some structure when it's removed.
As others noted this patchset conflicts with the series from Christoph, namely the io_failure_tree removal, but that series is more intrusive and with pending reviews.
Quoted 24 lines · Josef Bacik
Josef Bacik (31):
btrfs: cleanupclean_io_failure
btrfs: unexport internal failrec functions
btrfs: stop usingextent_io_treefor io_failure_record's
btrfs: usefind_first_extent_bitinbtrfs_clean_io_failure
btrfs: separate out the extent state and extent buffer init code
btrfs: separate out the eb and extent state leak helpers
btrfs: temporarily exportalloc_extent_statehelpers
btrfs: move extent state init and alloc functions to their own file
btrfs: convertBUG_ON(EXTENT_BIT_LOCKED)checks to ASSERT's
btrfs: move simple extent bit helpers out ofextent_io.c
btrfs: exportwait_extent_bit
btrfs: move the coreextent_io_treecode intoextent-io-tree.c
btrfs: remove structtree_entry
btrfs: usenext_stateinstead ofrb_nextwhere we can
btrfs: maketree_searchreturn structextent_state
btrfs: maketree_search_for_insertreturnextent_state
btrfs: maketree_search_prev_nextreturn extent_state's
btrfs: use next_state/prev_state inmerge_state
btrfs: remove temporary exports forextent_statemovement
btrfs: move irrelevant prototypes to their appropriate header
btrfs: dropexclusive_bitsfromset_extent_bit
btrfs: remove the wake argument fromclear_extent_bits
btrfs: removefailed_startargument fromset_extent_bit
Quoted 8 lines
btrfs: drop
extent_changesetfromset_extent_bit
btrfs: unify the lock/unlock extent variants
btrfs: get rid oftrack_uptodate
btrfs: get rid of ->dirty_bytes
btrfs: don't clear CTL bits when trying to release extent state
btrfs: replace delete argument withEXTENT_CLEAR_ALL_BITS
btrfs: don't init io tree with private data for non inodes
btrfs: removeis_data_inode()checks inextent-io-tree.c
The self tests don't pass
[ 13.933867] Btrfs loaded, crc32c=crc32c-generic, debug=on, assert=on, integrity-checker=on, ref-verify=on, zoned=yes, fsverity=yes [ 13.936232] BTRFS: selftest: sectorsize: 4096 nodesize: 4096 [ 13.937186] BTRFS: selftest: running btrfs free space cache tests [ 13.938314] BTRFS: selftest: running extent only tests [ 13.939327] BTRFS: selftest: running bitmap only tests [ 13.940285] BTRFS: selftest: running bitmap and extent tests [ 13.941288] BTRFS: selftest: running space stealing from bitmap to extent tests [ 13.942627] BTRFS: selftest: running bytes index tests [ 13.943681] BTRFS: selftest: running extent buffer operation tests [ 13.944868] BTRFS: selftest: running btrfs_split_item tests [ 13.945641] BTRFS: selftest: running extent I/O tests [ 13.946662] BTRFS: selftest: running find delalloc tests [ 14.226329] BTRFS: selftest: running find_first_clear_extent_bit test [ 14.227517] BTRFS: selftest: fs/btrfs/tests/extent-io-tests.c:528 error finding trimmed range: start 67108864 end 33554431 [ 14.229160] BTRFS: selftest: io tree content: [ 14.229873] BTRFS: selftest: start=1048576 len=3145728 flags=DIRTY|DEFRAG [ 14.231208] BTRFS: selftest: start=33554432 len=33554432 flags=DIRTY|DEFRAG
Re: [PATCH 26/31] btrfs: get rid of track_uptodate
In reply to Josef BacikQuoted 1 line · Josef Bacik
This is no longer used anymore, remove it from the
extent_io_tree.
This should mention the last commit that removed the functionality, I think it's in this series so referenced by the the subject. Also if it's a struct member removal should say from which it's being removed, ie. extent_io_tree::track_uptodate.
Re: [PATCH 27/31] btrfs: get rid of ->dirty_bytes
In reply to Josef BacikQuoted 3 lines · Josef Bacik
This was used as an optimization for
count_range_bits(EXTENT_DIRTY), but we no longer do that, thus nothing actually uses the ->dirty_bytes counter in theextent_io_tree.
Same comment as for 26/31
Re: [PATCH 01/31] btrfs: cleanup clean_io_failure
In reply to Josef BacikQuoted 4 lines · Josef Bacik
This is exported, so rename it to
btrfs_clean_io_failure. Additionally we are passing in the io tree's and such from the inode, so instead of doing all that simply pass in the inode itself and get all the components we need directly inside ofbtrfs_clean_io_failure.
The 'cleanup function' is usually very unspecific as subject, but we have patches like that that do several cleanups inside the function. Something like "export and rename ...", or "simplify parameters of ..." when it's not purely exporting.
Re: [PATCH 03/31] btrfs: stop using extent_io_tree for io_failure_record's
In reply to Josef BacikQuoted 10 lines · Josef Bacik
We still have this oddity of stashing the
io_failure_recordin the extent state for theio_failure_tree, which is leftover from when we used to stuff private pointers inextent_io_trees.However this doesn't make a lot of sense for the io failure records, we can simply use a normal
rb_treefor this. This will allow us to further simplify theextent_io_treecode by removing theio_failure_recpointer from the extent state.Convert the
io_failure_treeto an rb tree + spinlock in the inode, and
That's the perfect subject "convert io_failure_tree to rb_tree", we still use the tree so we're not stopping using it.
Otherwise ok, it'll shave a few bytes from inode too.
Quoted 5 lines
then use our rb tree simple helpers to insert and find failed records. This greatly cleans up this code and makes it easier to separate out the
extent_io_treecode.Signed-off-by Josef Bacik <[email protected]>
Re: [PATCH 09/31] btrfs: convert BUG_ON(EXTENT_BIT_LOCKED) checks to ASSERT's
In reply to Josef BacikQuoted 1 line · Josef Bacik
These should be ASSERT()'s, not BUG_ON()'s.
This could explain why, the BUG_ON is a runtime check though not the best one, while ASSERT is good to make sure that the API is used correctly. It's from an old commit, I'm not sure if we have started using asserts back then.
Re: [PATCH 12/31] btrfs: move the core extent_io_tree code into extent-io-tree.c
In reply to Josef BacikQuoted 3 lines · Josef Bacik
This is a big patch unfortunately, all of this code is tightly coupled together. There is no code change at all, it is simply cut and paste from
extent_io.cintoextent-io-tree.c.
Doing it cleanly one by one would require temporary exports, move, unexport again. I understand doing it in one go but it's still 1600 lines of diff, hard to do review, but let's see.