Btrfs: release delalloc reservations on extent item insertion
PATCH 1/1 Btrfs: release delalloc reservations on extent item insertion
This patch fixes an issue with the delalloc metadata space reservation code. The problem is we used to free the reservation as soon as we allocated the delalloc region. The problem with this is if we are not inserting an inline extent, we don't actually insert the extent item until after the ordered extent is written out. This patch does 3 things,
1) It moves the reservation clearing stuff into the ordered code, so when we remove the ordered extent we remove the reservation.
2) It adds a EXTENT_DO_ACCOUNTING flag that gets passed when we clear delalloc bits in the cases where we want to clear the metadata reservation when we clear the delalloc extent, in the case that we do an inline extent or we invalidate the page.
3) It adds another waitqueue to the space info so that when we start a fs wide delalloc flush, anybody else who also hits that area will simply wait for the flush to finish and then try to make their allocation.
This has been tested thoroughly to make sure we did not regress on performance.
--- fs/btrfs/btrfs_inode.h | 12 ++++++---- fs/btrfs/ctree.h | 3 ++ fs/btrfs/extent-tree.c | 54 ++++++++++++++++++++++++++++++++++++--------- fs/btrfs/extent_io.c | 15 +++++++++--- fs/btrfs/extent_io.h | 2 + fs/btrfs/file.c | 3 +- fs/btrfs/inode.c | 56 ++++++++++++++++++++++++++++++---------------- fs/btrfs/ordered-data.c | 7 ++++++ 8 files changed, 111 insertions(+), 41 deletions(-)
fs/btrfs/btrfs_inode.h +7 -5
@@ -128,12 +128,14 @@ struct btrfs_inode {u64 last_unlink_trans;/* -* These two counters are for delalloc metadata reservations. We keep -* track of how many extents we've accounted for vs how many extents we -* have. +* Counters to keep track of the number of extent item's we may use due +* to delalloc and such. outstanding_extents is the number of extent +* items we think we'll end up using, and reserved_extents is the number +* of extent items we've reserved metadata for. */ -int delalloc_reserved_extents; -int delalloc_extents; +spinlock_t accounting_lock; +int reserved_extents; +int outstanding_extents;/* * ordered_data_close is set by truncate when a file that used
fs/btrfs/ctree.h +3 -0
@@ -695,6 +695,9 @@ struct btrfs_space_info {int allocating_chunk;wait_queue_head_t wait; + +int flushing; +wait_queue_head_t flush_wait; };/*
fs/btrfs/extent-tree.c +43 -11
92 lines not on this page · raw message
fs/btrfs/extent_io.c +11 -4
@@ -460,7 +460,8 @@ static int clear_state_bit(struct extent_io_tree *tree, struct extent_state *state, int bits, int wake, int delete) { - int ret = state->state & bits; + int bits_to_clear = bits & ~EXTENT_DO_ACCOUNTING; +int ret = state->state & bits_to_clear;if ((bits &EXTENT_DIRTY ) && (state->state &EXTENT_DIRTY )) {u64 range = state->end - state->start +1 ;
@@ -468,7 +469,7 @@ static int clear_state_bit(struct extent_io_tree *tree, tree->dirty_bytes -= range; }clear_state_cb (tree, state, bits); - state->state &= ~bits; + state->state &= ~bits_to_clear;if (wake)wake_up (&state->wq);if (delete || state->state ==0 ) {
@@ -955,7 +956,8 @@ int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask) {return clear_extent_bit (tree, start, end, -EXTENT_DIRTY |EXTENT_DELALLOC ,0 ,0 , +EXTENT_DIRTY |EXTENT_DELALLOC | +EXTENT_DO_ACCOUNTING ,0 ,0 ,NULL , mask); }
@@ -1403,6 +1405,7 @@ int extent_clear_unlock_delalloc(struct inode *inode,int unlock_pages, int clear_unlock, int clear_delalloc, int clear_dirty, + int clear_accounting, int set_writeback, int end_writeback, int set_private2)
@@ -1423,6 +1426,9 @@ int extent_clear_unlock_delalloc(struct inode *inode,if (clear_delalloc) clear_bits |=EXTENT_DELALLOC ; +if (clear_accounting) + clear_bits |=EXTENT_DO_ACCOUNTING ; +clear_extent_bit (tree, start, end, clear_bits,1 ,0 ,NULL ,GFP_NOFS );if (!(unlock_pages || clear_dirty || set_writeback || end_writeback || set_private2))
@@ -2693,7 +2699,8 @@ int extent_invalidatepage(struct extent_io_tree *tree,lock_extent (tree, start, end,GFP_NOFS );wait_on_page_writeback (page);clear_extent_bit (tree, start, end, -EXTENT_LOCKED |EXTENT_DIRTY |EXTENT_DELALLOC , +EXTENT_LOCKED |EXTENT_DIRTY |EXTENT_DELALLOC | +EXTENT_DO_ACCOUNTING ,1 ,1 ,NULL ,GFP_NOFS );return 0 ; }
fs/btrfs/extent_io.h +2 -0
@@ -15,6 +15,7 @@#define EXTENT_BUFFER_FILLED (1 << 8)#define EXTENT_BOUNDARY (1 << 9)#define EXTENT_NODATASUM (1 << 10) +#define EXTENT_DO_ACCOUNTING (1 << 11)#define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)/* flags for bio submission */
@@ -291,6 +292,7 @@ int extent_clear_unlock_delalloc(struct inode *inode,int unlock_page, int clear_unlock, int clear_delalloc, int clear_dirty, + int clear_accounting, int set_writeback, int end_writeback, int set_private2);
fs/btrfs/file.c +2 -1
@@ -878,7 +878,8 @@ again:btrfs_put_ordered_extent (ordered);clear_extent_bits (&BTRFS_I (inode)->io_tree, start_pos, - last_pos -1 ,EXTENT_DIRTY |EXTENT_DELALLOC , + last_pos -1 ,EXTENT_DIRTY |EXTENT_DELALLOC | +EXTENT_DO_ACCOUNTING ,GFP_NOFS );unlock_extent (&BTRFS_I (inode)->io_tree, start_pos, last_pos -1 ,GFP_NOFS );
fs/btrfs/inode.c +36 -20
152 lines not on this page · raw message
fs/btrfs/ordered-data.c +7 -0
@@ -305,6 +305,13 @@ int btrfs_remove_ordered_extent(struct inode *inode,rb_erase (node, &tree->tree); tree->last =NULL ;set_bit (BTRFS_ORDERED_COMPLETE , &entry->flags); +if (!test_bit (BTRFS_ORDERED_COMPRESSED , &entry->flags)) { +spin_lock (&BTRFS_I (inode)->accounting_lock); +BTRFS_I (inode)->outstanding_extents--; +spin_unlock (&BTRFS_I (inode)->accounting_lock); +btrfs_unreserve_metadata_for_delalloc (BTRFS_I (inode)->root, + inode,1 ); + }spin_lock (&BTRFS_I (inode)->root->fs_info->ordered_extent_lock);list_del_init (&entry->root_extent_list);
Signature · 1 line
1.5.4.3