Change data reservations to use the ticketing infra
v4 cover Change data reservations to use the ticketing infra
v3->v4:
- Rebased onto a recent misc-next, slight fixup because of commenting around the flush states.
v2->v3:
- Rebased onto a recent misc-next
v1->v2:
- Adjusted a comment in may_commit_transaction.
- Fixed one of the intermediate patches to properly update ->reclaim_size.
We've had two different things in place to reserve data and metadata space, because generally speaking data is much simpler. However the data reservations still suffered from the same issues that plagued metadata reservations, you could get multiple tasks racing in to get reservations. This causes problems with cases like write/delete loops where we should be able to fill up the fs, delete everything, and go again. You would sometimes race with a flusher that's trying to unpin the free space, take it's reservations, and then it would fail.
Fix this by moving the data reservations under the metadata ticketing infrastructure. This gets rid of that problem, and simplifies our enospc code more by consolidating it into a single path. Thanks,
Josef
PATCH v4 1/23 btrfs: change nr to u64 in btrfs_start_delalloc_roots
We have btrfs_wait_ordered_roots() which takes a u64 for nr, but btrfs_start_delalloc_roots() that takes an int for nr, which makes using them in conjunction, especially for something like (u64)-1, annoying and inconsistent. Fix btrfs_start_delalloc_roots() to take a u64 for nr and adjust start_delalloc_inodes() and it's callers appropriately.
This means we've adjusted start_delalloc_inodes() to take a pointer of nr since we want to preserve the ability for start-delalloc_inodes() to return an error, so simply make it do the nr adjusting as necessary.
Part of adjusting the callers to this means changing btrfs_writeback_inodes_sb_nr() to take a u64 for items. This may be confusing because it seems unrelated, but the caller of btrfs_writeback_inodes_sb_nr() already passes in a u64, it's just the function variable that needs to be changed.
--- fs/btrfs/ctree.h | 2 +- fs/btrfs/dev-replace.c | 2 +- fs/btrfs/inode.c | 27 +++++++++++---------------- fs/btrfs/ioctl.c | 2 +- fs/btrfs/space-info.c | 2 +- 5 files changed, 15 insertions(+), 20 deletions(-)
fs/btrfs/ctree.h +1 -1
@@ -2956,7 +2956,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, u32 min_type);int btrfs_start_delalloc_snapshot (struct btrfs_root *root); -int btrfs_start_delalloc_roots (struct btrfs_fs_info *fs_info,int nr); +int btrfs_start_delalloc_roots (struct btrfs_fs_info *fs_info,u64 nr);int btrfs_set_extent_delalloc (struct btrfs_inode *inode,u64 start,u64 end,unsigned int extra_bits,struct extent_state **cached_state);
fs/btrfs/dev-replace.c +1 -1
@@ -630,7 +630,7 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,* flush all outstanding I/O and inode extent mappings before the * copy operation is declared as being finished */ - ret =btrfs_start_delalloc_roots (fs_info,-1 ); + ret =btrfs_start_delalloc_roots (fs_info,U64_MAX );if (ret) {mutex_unlock (&dev_replace->lock_finishing_cancel_unmount);return ret;
fs/btrfs/inode.c +11 -16
@@ -9382,7 +9382,8 @@ static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode* some fairly slow code that needs optimization. This walks the list * of all the inodes with pending delalloc and forces them to disk. */ -static int start_delalloc_inodes (struct btrfs_root *root,int nr,bool snapshot) +static int start_delalloc_inodes (struct btrfs_root *root,u64 *nr, +bool snapshot) {struct btrfs_inode *binode;struct inode *inode;
@@ -9422,9 +9423,11 @@ static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)list_add_tail (&work->list, &works);btrfs_queue_work (root->fs_info->flush_workers, &work->work); - ret++; -if (nr !=-1 && ret >= nr) - goto out; +if (*nr !=U64_MAX ) { + (*nr)--; +if (*nr ==0 ) + goto out; + }cond_resched ();spin_lock (&root->delalloc_lock); }
@@ -9449,18 +9452,15 @@ static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)int btrfs_start_delalloc_snapshot (struct btrfs_root *root) {struct btrfs_fs_info *fs_info = root->fs_info; -int ret; +u64 nr =U64_MAX ;if (test_bit (BTRFS_FS_STATE_ERROR , &fs_info->fs_state))return -EROFS ; - ret =start_delalloc_inodes (root,-1 , true); -if (ret >0 ) - ret =0 ; -return ret; +return start_delalloc_inodes (root, &nr, true); } -int btrfs_start_delalloc_roots (struct btrfs_fs_info *fs_info,int nr) +int btrfs_start_delalloc_roots (struct btrfs_fs_info *fs_info,u64 nr) {struct btrfs_root *root;struct list_head splice;
@@ -9483,15 +9483,10 @@ int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr) &fs_info->delalloc_roots);spin_unlock (&fs_info->delalloc_root_lock); - ret =start_delalloc_inodes (root, nr, false); + ret =start_delalloc_inodes (root, &nr, false);btrfs_put_root (root);if (ret <0 ) goto out; - -if (nr !=-1 ) { - nr -= ret; -WARN_ON (nr <0 ); - }spin_lock (&fs_info->delalloc_root_lock); }spin_unlock (&fs_info->delalloc_root_lock);
fs/btrfs/ioctl.c +1 -1
@@ -4883,7 +4883,7 @@ long btrfs_ioctl(struct file *file, unsigned intcase BTRFS_IOC_SYNC : {int ret; - ret =btrfs_start_delalloc_roots (fs_info,-1 ); + ret =btrfs_start_delalloc_roots (fs_info,U64_MAX );if (ret)return ret; ret =btrfs_sync_fs (inode->i_sb,1 );
fs/btrfs/space-info.c +1 -1
@@ -477,7 +477,7 @@ void btrfs_dump_space_info(struct btrfs_fs_info *fs_info, }static void btrfs_writeback_inodes_sb_nr (struct btrfs_fs_info *fs_info, -unsigned long nr_pages,int nr_items) +unsigned long nr_pages,u64 nr_items) {struct super_block *sb = fs_info->sb;
Signature · 1 line
2.24.1
PATCH v4 2/23 btrfs: remove orig from shrink_delalloc
In reply to Josef BacikWe don't use this anywhere inside of shrink_delalloc, remove it.
--- fs/btrfs/space-info.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/space-info.c +2 -2
@@ -517,7 +517,7 @@ static inline u64 calc_reclaim_items_nr(struct btrfs_fs_info *fs_info,* shrink metadata reservation for delalloc */ static void shrink_delalloc (struct btrfs_fs_info *fs_info,u64 to_reclaim, -u64 orig,bool wait_ordered) +bool wait_ordered) {struct btrfs_space_info *space_info;struct btrfs_trans_handle *trans;
@@ -742,7 +742,7 @@ static void flush_space(struct btrfs_fs_info *fs_info,break ;case FLUSH_DELALLOC :case FLUSH_DELALLOC_WAIT : -shrink_delalloc (fs_info, num_bytes *2 , num_bytes, +shrink_delalloc (fs_info, num_bytes *2 , state ==FLUSH_DELALLOC_WAIT );break ;case FLUSH_DELAYED_REFS_NR :
Signature · 1 line
2.24.1
PATCH v4 3/23 btrfs: handle U64_MAX for shrink_delalloc
In reply to Josef BacikData allocations are going to want to pass in U64_MAX for flushing space, adjust shrink_delalloc to handle this properly.
--- fs/btrfs/space-info.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
fs/btrfs/space-info.c +14 -3
@@ -530,8 +530,19 @@ static void shrink_delalloc(struct btrfs_fs_info *fs_info, u64 to_reclaim,int loops;/* Calc the number of the pages we need flush for space reservation */ - items =calc_reclaim_items_nr (fs_info, to_reclaim); - to_reclaim = items *EXTENT_SIZE_PER_ITEM ; +if (to_reclaim ==U64_MAX ) { + items =U64_MAX ; + }else { +/* +* to_reclaim is set to however much metadata we need to +* reclaim, but reclaiming that much data doesn't really track +* exactly, so increase the amount to reclaim by 2x in order to +* make sure we're flushing enough delalloc to hopefully reclaim +* some metadata reservations. +*/ + items =calc_reclaim_items_nr (fs_info, to_reclaim) *2 ; + to_reclaim = items *EXTENT_SIZE_PER_ITEM ; + } trans = (struct btrfs_trans_handle *)current->journal_info; space_info =btrfs_find_space_info (fs_info,BTRFS_BLOCK_GROUP_METADATA );
@@ -742,7 +753,7 @@ static void flush_space(struct btrfs_fs_info *fs_info,break ;case FLUSH_DELALLOC :case FLUSH_DELALLOC_WAIT : -shrink_delalloc (fs_info, num_bytes *2 , +shrink_delalloc (fs_info, num_bytes, state ==FLUSH_DELALLOC_WAIT );break ;case FLUSH_DELAYED_REFS_NR :
Signature · 1 line
2.24.1
PATCH v4 4/23 btrfs: make shrink_delalloc take space_info as an arg
In reply to Josef BacikCurrently shrink_delalloc just looks up the metadata space info, but this won't work if we're trying to reclaim space for data chunks. We get the right space_info we want passed into flush_space, so simply pass that along to shrink_delalloc.
--- fs/btrfs/space-info.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
fs/btrfs/space-info.c +4 -5
@@ -516,10 +516,10 @@ static inline u64 calc_reclaim_items_nr(struct btrfs_fs_info *fs_info,/* * shrink metadata reservation for delalloc */ -static void shrink_delalloc (struct btrfs_fs_info *fs_info,u64 to_reclaim, -bool wait_ordered) +static void shrink_delalloc (struct btrfs_fs_info *fs_info, +struct btrfs_space_info *space_info, +u64 to_reclaim,bool wait_ordered) { -struct btrfs_space_info *space_info;struct btrfs_trans_handle *trans;u64 delalloc_bytes;u64 dio_bytes;
@@ -545,7 +545,6 @@ static void shrink_delalloc(struct btrfs_fs_info *fs_info, u64 to_reclaim, } trans = (struct btrfs_trans_handle *)current->journal_info; - space_info =btrfs_find_space_info (fs_info,BTRFS_BLOCK_GROUP_METADATA ); delalloc_bytes =percpu_counter_sum_positive ( &fs_info->delalloc_bytes);
@@ -753,7 +752,7 @@ static void flush_space(struct btrfs_fs_info *fs_info,break ;case FLUSH_DELALLOC :case FLUSH_DELALLOC_WAIT : -shrink_delalloc (fs_info, num_bytes, +shrink_delalloc (fs_info, space_info, num_bytes, state ==FLUSH_DELALLOC_WAIT );break ;case FLUSH_DELAYED_REFS_NR :
Signature · 1 line
2.24.1
PATCH v4 5/23 btrfs: make ALLOC_CHUNK use the space info flags
In reply to Josef BacikWe have traditionally used flush_space() to flush metadata space, so we've been unconditionally using btrfs_metadata_alloc_profile() for our profile to allocate a chunk. However if we're going to use this for data we need to use btrfs_get_alloc_profile() on the space_info we pass in.
--- fs/btrfs/space-info.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
fs/btrfs/space-info.c +2 -1
@@ -777,7 +777,8 @@ static void flush_space(struct btrfs_fs_info *fs_info,break ; } ret =btrfs_chunk_alloc (trans, -btrfs_metadata_alloc_profile (fs_info), +btrfs_get_alloc_profile (fs_info, + space_info->flags), (state ==ALLOC_CHUNK ) ?CHUNK_ALLOC_NO_FORCE :CHUNK_ALLOC_FORCE );btrfs_end_transaction (trans);
Signature · 1 line
2.24.1
PATCH v4 6/23 btrfs: call btrfs_try_granting_tickets when freeing reserved bytes
In reply to Josef BacikWe were missing a call to btrfs_try_granting_tickets in btrfs_free_reserved_bytes, so add it to handle the case where we're able to satisfy an allocation because we've freed a pending reservation.
--- fs/btrfs/block-group.c | 2 ++ 1 file changed, 2 insertions(+)
fs/btrfs/block-group.c +2 -0
@@ -3043,6 +3043,8 @@ void btrfs_free_reserved_bytes(struct btrfs_block_group *cache,if (delalloc) cache->delalloc_bytes -= num_bytes;spin_unlock (&cache->lock); + +btrfs_try_granting_tickets (cache->fs_info, space_info);spin_unlock (&space_info->lock); }
Signature · 1 line
2.24.1
PATCH v4 7/23 btrfs: call btrfs_try_granting_tickets when unpinning anything
In reply to Josef BacikWhen unpinning we were only calling btrfs_try_granting_tickets() if global_rsv->space_info == space_info, which is problematic because we use ticketing for SYSTEM chunks, and want to use it for DATA as well. Fix this by moving this call outside of that if statement.
--- fs/btrfs/extent-tree.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
fs/btrfs/extent-tree.c +3 -4
@@ -2844,11 +2844,10 @@ static int unpin_extent_range(struct btrfs_fs_info *fs_info, len -= to_add; }spin_unlock (&global_rsv->lock); -/* Add to any tickets we may have */ -if (len) -btrfs_try_granting_tickets (fs_info, - space_info); } +/* Add to any tickets we may have */ +if (!readonly && return_free_space && len) +btrfs_try_granting_tickets (fs_info, space_info);spin_unlock (&space_info->lock); }
Signature · 1 line
2.24.1
PATCH v4 8/23 btrfs: call btrfs_try_granting_tickets when reserving space
In reply to Josef BacikIf we have compression on we could free up more space than we reserved, and thus be able to make a space reservation. Add the call for this scenario.
--- fs/btrfs/block-group.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/btrfs/block-group.c +7 -0
@@ -3010,6 +3010,13 @@ int btrfs_add_reserved_bytes(struct btrfs_block_group *cache, space_info, -ram_bytes);if (delalloc) cache->delalloc_bytes += num_bytes; + +/* +* Compression can use less space than we reserved, so wake +* tickets if that happens. +*/ +if (num_bytes < ram_bytes) +btrfs_try_granting_tickets (cache->fs_info, space_info); }spin_unlock (&cache->lock);spin_unlock (&space_info->lock);
Signature · 1 line
2.24.1
PATCH v4 9/23 btrfs: use the btrfs_space_info_free_bytes_may_use helper for delalloc
In reply to Josef BacikWe are going to use the ticket infrastructure for data, so use the btrfs_space_info_free_bytes_may_use() helper in btrfs_free_reserved_data_space_noquota() so we get the try_granting_tickets call when we free our reservation.
--- fs/btrfs/delalloc-space.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
fs/btrfs/delalloc-space.c +1 -3
@@ -277,9 +277,7 @@ void btrfs_free_reserved_data_space_noquota(struct btrfs_fs_info *fs_info,ASSERT (IS_ALIGNED (len, fs_info->sectorsize)); data_sinfo = fs_info->data_sinfo; -spin_lock (&data_sinfo->lock); -btrfs_space_info_update_bytes_may_use (fs_info, data_sinfo, -len); -spin_unlock (&data_sinfo->lock); +btrfs_space_info_free_bytes_may_use (fs_info, data_sinfo, len); }/*
Signature · 1 line
2.24.1
PATCH v4 10/23 btrfs: use btrfs_start_delalloc_roots in shrink_delalloc
In reply to Josef BacikThe original iteration of flushing had us flushing delalloc and then checking to see if we could make our reservation, thus we were very careful about how many pages we would flush at once.
But now that everything is async and we satisfy tickets as the space becomes available we don't have to keep track of any of this, simply try and flush the number of dirty inodes we may have in order to reclaim space to make our reservation. This cleans up our delalloc flushing significantly.
The async_pages stuff is dropped because btrfs_start_delalloc_roots() handles the case that we generate async extents for us, so we no longer require this extra logic.
--- fs/btrfs/space-info.c | 55 +------------------------------------------ 1 file changed, 1 insertion(+), 54 deletions(-)
fs/btrfs/space-info.c +1 -54
@@ -476,28 +476,6 @@ void btrfs_dump_space_info(struct btrfs_fs_info *fs_info,up_read (&info->groups_sem); } -static void btrfs_writeback_inodes_sb_nr (struct btrfs_fs_info *fs_info, -unsigned long nr_pages,u64 nr_items) -{ -struct super_block *sb = fs_info->sb; - -if (down_read_trylock (&sb->s_umount)) { -writeback_inodes_sb_nr (sb, nr_pages,WB_REASON_FS_FREE_SPACE ); -up_read (&sb->s_umount); - }else { -/* -* We needn't worry the filesystem going from r/w to r/o though -* we don't acquire ->s_umount mutex, because the filesystem -* should guarantee the delalloc inodes list be empty after -* the filesystem is readonly(all dirty pages are written to -* the disk). -*/ -btrfs_start_delalloc_roots (fs_info, nr_items); -if (!current->journal_info) -btrfs_wait_ordered_roots (fs_info, nr_items,0 , (u64)-1 ); - } -} -static inline u64 calc_reclaim_items_nr (struct btrfs_fs_info *fs_info,u64 to_reclaim) {
@@ -523,10 +501,8 @@ static void shrink_delalloc(struct btrfs_fs_info *fs_info,struct btrfs_trans_handle *trans;u64 delalloc_bytes;u64 dio_bytes; -u64 async_pages;u64 items;long time_left; -unsigned long nr_pages;int loops;/* Calc the number of the pages we need flush for space reservation */
@@ -567,37 +543,8 @@ static void shrink_delalloc(struct btrfs_fs_info *fs_info, loops =0 ;while ((delalloc_bytes || dio_bytes) && loops <3 ) { - nr_pages =min (delalloc_bytes, to_reclaim) >>PAGE_SHIFT ; - -/* -* Triggers inode writeback for up to nr_pages. This will invoke -* ->writepages callback and trigger delalloc filling -* (btrfs_run_delalloc_range()). -*/ -btrfs_writeback_inodes_sb_nr (fs_info, nr_pages, items); - -/* -* We need to wait for the compressed pages to start before -* we continue. -*/ - async_pages =atomic_read (&fs_info->async_delalloc_pages); -if (!async_pages) - goto skip_async; - -/* -* Calculate how many compressed pages we want to be written -* before we continue. I.e if there are more async pages than we -* require wait_event will wait until nr_pages are written. -*/ -if (async_pages <= nr_pages) - async_pages =0 ; -else - async_pages -= nr_pages; +btrfs_start_delalloc_roots (fs_info, items); -wait_event (fs_info->async_submit_wait, -atomic_read (&fs_info->async_delalloc_pages) <= - (int )async_pages); -skip_async:spin_lock (&space_info->lock);if (list_empty (&space_info->tickets) &&list_empty (&space_info->priority_tickets)) {
Signature · 1 line
2.24.1
PATCH v4 11/23 btrfs: check tickets after waiting on ordered extents
In reply to Josef BacikRight now if the space is free'd up after the ordered extents complete (which is likely since the reservations are held until they complete), we would do extra delalloc flushing before we'd notice that we didn't have any more tickets. Fix this by moving the tickets check after our wait_ordered_extents check.
--- fs/btrfs/space-info.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
fs/btrfs/space-info.c +9 -8
@@ -545,14 +545,6 @@ static void shrink_delalloc(struct btrfs_fs_info *fs_info,while ((delalloc_bytes || dio_bytes) && loops <3 ) {btrfs_start_delalloc_roots (fs_info, items); -spin_lock (&space_info->lock); -if (list_empty (&space_info->tickets) && -list_empty (&space_info->priority_tickets)) { -spin_unlock (&space_info->lock); -break ; - } -spin_unlock (&space_info->lock); - loops++;if (wait_ordered && !trans) {btrfs_wait_ordered_roots (fs_info, items,0 , (u64)-1 );
@@ -561,6 +553,15 @@ static void shrink_delalloc(struct btrfs_fs_info *fs_info,if (time_left)break ; } + +spin_lock (&space_info->lock); +if (list_empty (&space_info->tickets) && +list_empty (&space_info->priority_tickets)) { +spin_unlock (&space_info->lock); +break ; + } +spin_unlock (&space_info->lock); + delalloc_bytes =percpu_counter_sum_positive ( &fs_info->delalloc_bytes); dio_bytes =percpu_counter_sum_positive (&fs_info->dio_bytes);
Signature · 1 line
2.24.1
PATCH v4 12/23 btrfs: add flushing states for handling data reservations
In reply to Josef BacikCurrently the way we do data reservations is by seeing if we have enough space in our space_info. If we do not and we're a normal inode we'll
1) Attempt to force a chunk allocation until we can't anymore.
2) If that fails we'll flush delalloc, then commit the transaction, then run the delayed iputs.
If we are a free space inode we're only allowed to force a chunk allocation. In order to use the normal flushing mechanism we need to encode this into a flush state array for normal inodes. Since both will start with allocating chunks until the space info is full there is no need to add this as a flush state, this will be handled specially.
--- fs/btrfs/ctree.h | 2 ++ fs/btrfs/space-info.c | 6 ++++++ 2 files changed, 8 insertions(+)
fs/btrfs/ctree.h +2 -0
@@ -2592,6 +2592,8 @@ enum btrfs_reserve_flush_enum {* * Can be interruped by fatal signal. */ +BTRFS_RESERVE_FLUSH_DATA , +BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE , BTRFS_RESERVE_FLUSH_ALL,/*
fs/btrfs/space-info.c +6 -0
@@ -1018,6 +1018,12 @@ static const enum btrfs_flush_state evict_flush_states[] = { COMMIT_TRANS, }; +static const enum btrfs_flush_state data_flush_states[] = { +FLUSH_DELALLOC_WAIT , +COMMIT_TRANS , +RUN_DELAYED_IPUTS , +}; + static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info, struct btrfs_space_info *space_info, struct reserve_ticket *ticket,
Signature · 1 line
2.24.1
Re: [PATCH 12/23] btrfs: add flushing states for handling data reservations
Quoted 26 lines · Josef Bacik
Currently the way we do data reservations is by seeing if we have enough space in our
space_info. If we do not and we're a normal inode we'll1) Attempt to force a chunk allocation until we can't anymore.
2) If that fails we'll flush delalloc, then commit the transaction, then run the delayed iputs.If we are a free space inode we're only allowed to force a chunk allocation. In order to use the normal flushing mechanism we need to encode this into a flush state array for normal inodes. Since both will start with allocating chunks until the space info is full there is no need to add this as a flush state, this will be handled specially.
Reviewed-by Nikolay Borisov <[email protected]>Tested-by Nikolay Borisov <[email protected]>Signed-off-by Josef Bacik <[email protected]>--- fs/btrfs/ctree.h | 2 ++ fs/btrfs/space-info.c | 6 ++++++ 2 files changed, 8 insertions(+)fs/btrfs/ctree.h +0
-0
fs/btrfs/ctree.h enum btrfs_reserve_flush_enum { · line 2596
Quoted 5 lines
fs/btrfs/ctree.h +2
-0@@ -2592,6 +2592,8 @@ enum btrfs_reserve_flush_enum {* * Can be interruped by fatal signal. */ +BTRFS_RESERVE_FLUSH_DATA , + BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE,
The new states are inserted between a comment that is there for the FLUSH_ALL state. I'm not sure it's still valid or needs separate comments and can't tell from looking at other patches in the series.
Quoted 1 line
fs/btrfs/ctree.h +0
-0BTRFS_RESERVE_FLUSH_ALL,
PATCH v4 13/23 btrfs: add the data transaction commit logic into may_commit_transaction
In reply to Josef BacikData space flushing currently unconditionally commits the transaction twice in a row, and the last time it checks if there's enough pinned extents to satisfy it's reservation before deciding to commit the transaction for the 3rd and final time.
Encode this logic into may_commit_transaction(). In the next patch we will pass in U64_MAX for bytes_needed the first two times, and the final time we will pass in the actual bytes we need so the normal logic will apply.
This patch exists soley to make the logical changes I will make to the flushing state machine separate to make it easier to bisect any performance related regressions.
--- fs/btrfs/space-info.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
fs/btrfs/space-info.c +22 -6
@@ -579,21 +579,33 @@ static void shrink_delalloc(struct btrfs_fs_info *fs_info,* will return -ENOSPC. */ static int may_commit_transaction (struct btrfs_fs_info *fs_info, -struct btrfs_space_info *space_info) +struct btrfs_space_info *space_info, +u64 bytes_needed) {struct reserve_ticket *ticket =NULL ;struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv;struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;struct btrfs_block_rsv *trans_rsv = &fs_info->trans_block_rsv;struct btrfs_trans_handle *trans; -u64 bytes_needed;u64 reclaim_bytes =0 ;u64 cur_free_bytes =0 ; +bool do_commit = false; trans = (struct btrfs_trans_handle *)current->journal_info;if (trans)return -EAGAIN ; +/* +* If we are data and have passed in U64_MAX we just want to +* unconditionally commit the transaction to match the previous data +* flushing behavior. +*/ +if ((space_info->flags &BTRFS_BLOCK_GROUP_DATA ) && + bytes_needed ==U64_MAX ) { + do_commit = true; + goto check_pinned; + } +spin_lock (&space_info->lock); cur_free_bytes =btrfs_space_info_used (space_info, true);if (cur_free_bytes < space_info->total_bytes)
@@ -607,7 +619,7 @@ static int may_commit_transaction(struct btrfs_fs_info *fs_info,else if (!list_empty (&space_info->tickets)) ticket =list_first_entry (&space_info->tickets, struct reserve_ticket, list); - bytes_needed = (ticket) ? ticket->bytes :0 ; + bytes_needed = (ticket) ? ticket->bytes : bytes_needed;if (bytes_needed > cur_free_bytes) bytes_needed -= cur_free_bytes;
@@ -618,6 +630,7 @@ static int may_commit_transaction(struct btrfs_fs_info *fs_info,if (!bytes_needed)return 0 ; +check_pinned: trans =btrfs_join_transaction (fs_info->extent_root);if (IS_ERR (trans))return PTR_ERR (trans);
@@ -627,7 +640,8 @@ static int may_commit_transaction(struct btrfs_fs_info *fs_info,* we have block groups that are going to be freed, allowing us to * possibly do a chunk allocation the next loop through. */ -if (test_bit (BTRFS_TRANS_HAVE_FREE_BGS , &trans->transaction->flags) || +if (do_commit || +test_bit (BTRFS_TRANS_HAVE_FREE_BGS , &trans->transaction->flags) ||__percpu_counter_compare (&space_info->total_bytes_pinned, bytes_needed,BTRFS_TOTAL_BYTES_PINNED_BATCH ) >=0 )
@@ -635,7 +649,9 @@ static int may_commit_transaction(struct btrfs_fs_info *fs_info,/* * See if there is some space in the delayed insertion reservation for -* this reservation. +* this reservation. If the space_info's don't match (like for DATA or +* SYSTEM) then just enospc, reclaiming this space won't recover any +* space to satisfy those reservations. */ if (space_info != delayed_rsv->space_info) goto enospc;
@@ -743,7 +759,7 @@ static void flush_space(struct btrfs_fs_info *fs_info,btrfs_wait_on_delayed_iputs (fs_info);break ;case COMMIT_TRANS : - ret =may_commit_transaction (fs_info, space_info); + ret =may_commit_transaction (fs_info, space_info, num_bytes);break ;default : ret = -ENOSPC ;
Signature · 1 line
2.24.1
Re: [PATCH 13/23] btrfs: add the data transaction commit logic into may_commit_transaction
fs/btrfs/space-info.c static int may_commit_transaction(struct btrfs_fs_info *fs_info, · line 622
Quoted 2 lines · Josef Bacik
fs/btrfs/space-info.c +1
-1- bytes_needed = (ticket) ? ticket->bytes :0 ; + bytes_needed = (ticket) ? ticket->bytes : bytes_needed;
I'd rather write that as
if (ticket) bytes_needed = ticket->bytes;
PATCH v4 14/23 btrfs: add btrfs_reserve_data_bytes and use it
In reply to Josef BacikCreate a new function btrfs_reserve_data_bytes() in order to handle data reservations. This uses the new flush types and flush states to handle making data reservations.
This patch specifically does not change any functionality, and is purposefully not cleaned up in order to make bisection easier for the future patches. The new helper is identical to the old helper in how it handles data reservations. We first try to force a chunk allocation, and then we run through the flush states all at once and in the same order that they were done with the old helper.
Subsequent patches will clean this up and change the behavior of the flushing, and it is important to keep those changes separate so we can easily bisect down to the patch that caused the regression, rather than the patch that made us start using the new infrastructure.
--- fs/btrfs/delalloc-space.c | 119 ++------------------------------------ fs/btrfs/space-info.c | 92 +++++++++++++++++++++++++++++ fs/btrfs/space-info.h | 2 + 3 files changed, 98 insertions(+), 115 deletions(-)
fs/btrfs/delalloc-space.c +4 -115
@@ -115,126 +115,15 @@ int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes) {struct btrfs_root *root = inode->root;struct btrfs_fs_info *fs_info = root->fs_info; -struct btrfs_space_info *data_sinfo = fs_info->data_sinfo; -u64 used; -int ret =0 ; -int need_commit =2 ; -int have_pinned_space; +enum btrfs_reserve_flush_enum flush =BTRFS_RESERVE_FLUSH_DATA ;/* Make sure bytes are sectorsize aligned */ bytes =ALIGN (bytes, fs_info->sectorsize); -if (btrfs_is_free_space_inode (inode)) { - need_commit =0 ; -ASSERT (current->journal_info); - } - -again: -/* Make sure we have enough space to handle the data first */ -spin_lock (&data_sinfo->lock); - used =btrfs_space_info_used (data_sinfo, true); - -if (used + bytes > data_sinfo->total_bytes) { -struct btrfs_trans_handle *trans; - -/* -* If we don't have enough free bytes in this space then we need -* to alloc a new chunk. -*/ -if (!data_sinfo->full) { -u64 alloc_target; - - data_sinfo->force_alloc =CHUNK_ALLOC_FORCE ; -spin_unlock (&data_sinfo->lock); - - alloc_target =btrfs_data_alloc_profile (fs_info); -/* -* It is ugly that we don't call nolock join -* transaction for the free space inode case here. -* But it is safe because we only do the data space -* reservation for the free space cache in the -* transaction context, the common join transaction -* just increase the counter of the current transaction -* handler, doesn't try to acquire the trans_lock of -* the fs. -*/ - trans =btrfs_join_transaction (root); -if (IS_ERR (trans)) -return PTR_ERR (trans); - - ret =btrfs_chunk_alloc (trans, alloc_target, -CHUNK_ALLOC_NO_FORCE ); -btrfs_end_transaction (trans); -if (ret <0 ) { -if (ret != -ENOSPC ) -return ret; -else { - have_pinned_space =1 ; - goto commit_trans; - } - } - - goto again; - } - -/* -* If we don't have enough pinned space to deal with this -* allocation, and no removed chunk in current transaction, -* don't bother committing the transaction. -*/ - have_pinned_space =__percpu_counter_compare ( - &data_sinfo->total_bytes_pinned, - used + bytes - data_sinfo->total_bytes, -BTRFS_TOTAL_BYTES_PINNED_BATCH ); -spin_unlock (&data_sinfo->lock); - -/* Commit the current transaction and try again */ -commit_trans: -if (need_commit) { - need_commit--; - -if (need_commit >0 ) { -btrfs_start_delalloc_roots (fs_info,-1 ); -btrfs_wait_ordered_roots (fs_info,U64_MAX ,0 , - (u64)-1 ); - } - - trans =btrfs_join_transaction (root); -if (IS_ERR (trans)) -return PTR_ERR (trans); -if (have_pinned_space >=0 || -test_bit (BTRFS_TRANS_HAVE_FREE_BGS , - &trans->transaction->flags) || - need_commit >0 ) { - ret =btrfs_commit_transaction (trans); -if (ret) -return ret; -/* -* The cleaner kthread might still be doing iput -* operations. Wait for it to finish so that -* more space is released. We don't need to -* explicitly run the delayed iputs here because -* the commit_transaction would have woken up -* the cleaner. -*/ - ret =btrfs_wait_on_delayed_iputs (fs_info); -if (ret) -return ret; - goto again; - }else { -btrfs_end_transaction (trans); - } - } - -trace_btrfs_space_reservation (fs_info, -"space_info:enospc" , - data_sinfo->flags, bytes,1 ); -return -ENOSPC ; - } -btrfs_space_info_update_bytes_may_use (fs_info, data_sinfo, bytes); -spin_unlock (&data_sinfo->lock); +if (btrfs_is_free_space_inode (inode)) + flush =BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE ; -return 0 ; +return btrfs_reserve_data_bytes (fs_info, bytes, flush); } int btrfs_check_data_free_space(struct btrfs_inode *inode,
fs/btrfs/space-info.c +92 -0
95 lines not on this page · raw message
fs/btrfs/space-info.h +2 -0
@@ -149,5 +149,7 @@ static inline void btrfs_space_info_free_bytes_may_use(btrfs_try_granting_tickets (fs_info, space_info); spin_unlock(&space_info->lock); } +int btrfs_reserve_data_bytes (struct btrfs_fs_info *fs_info,u64 bytes, +enum btrfs_reserve_flush_enum flush);#endif /* BTRFS_SPACE_INFO_H */
Signature · 1 line
2.24.1
PATCH v4 15/23 btrfs: use ticketing for data space reservations
In reply to Josef BacikNow that we have all the infrastructure in place, use the ticketing infrastructure to make data allocations. This still maintains the exact same flushing behavior, but now we're using tickets to get our reservations satisfied.
--- fs/btrfs/space-info.c | 126 +++++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 58 deletions(-)
fs/btrfs/space-info.c +68 -58
@@ -1070,6 +1070,54 @@ static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info, }while (flush_state < states_nr); } +static void priority_reclaim_data_space (struct btrfs_fs_info *fs_info, +struct btrfs_space_info *space_info, +struct reserve_ticket *ticket, +const enum btrfs_flush_state *states, +int states_nr) +{ +int flush_state =0 ; +int commit_cycles =2 ; + +while (!space_info->full) { +flush_space (fs_info, space_info,U64_MAX ,ALLOC_CHUNK_FORCE ); +spin_lock (&space_info->lock); +if (ticket->bytes ==0 ) { +spin_unlock (&space_info->lock); +return ; + } +spin_unlock (&space_info->lock); + } +again: +while (flush_state < states_nr) { +u64 flush_bytes =U64_MAX ; + +if (!commit_cycles) { +if (states[flush_state] ==FLUSH_DELALLOC_WAIT ) { + flush_state++; +continue ; + } +if (states[flush_state] ==COMMIT_TRANS ) + flush_bytes = ticket->bytes; + } + +flush_space (fs_info, space_info, flush_bytes, + states[flush_state]); +spin_lock (&space_info->lock); +if (ticket->bytes ==0 ) { +spin_unlock (&space_info->lock); +return ; + } +spin_unlock (&space_info->lock); + flush_state++; + } +if (commit_cycles) { + commit_cycles--; + flush_state =0 ; + goto again; + } +} +static void wait_reserve_ticket (struct btrfs_fs_info *fs_info,struct btrfs_space_info *space_info,struct reserve_ticket *ticket)
@@ -1136,6 +1184,15 @@ static int handle_reserve_ticket(struct btrfs_fs_info *fs_info, evict_flush_states, ARRAY_SIZE(evict_flush_states));break ; +case BTRFS_RESERVE_FLUSH_DATA : +priority_reclaim_data_space (fs_info, space_info, ticket, + data_flush_states, +ARRAY_SIZE (data_flush_states)); +break ; +case BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE : +priority_reclaim_data_space (fs_info, space_info, ticket, +NULL ,0 ); +break ;default :ASSERT (0 );break ;
@@ -1343,78 +1400,31 @@ int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes, enum btrfs_reserve_flush_enum flush) {struct btrfs_space_info *data_sinfo = fs_info->data_sinfo; -const enum btrfs_flush_state *states =NULL ;u64 used; -int states_nr =0 ; -int commit_cycles =2 ;int ret = -ENOSPC ;ASSERT (!current->journal_info || flush !=BTRFS_RESERVE_FLUSH_DATA ); -if (flush ==BTRFS_RESERVE_FLUSH_DATA ) { - states = data_flush_states; - states_nr =ARRAY_SIZE (data_flush_states); - } -spin_lock (&data_sinfo->lock); -again: used =btrfs_space_info_used (data_sinfo, true);if (used + bytes > data_sinfo->total_bytes) { -u64 prev_total_bytes = data_sinfo->total_bytes; -int flush_state =0 ; +struct reserve_ticket ticket; +init_waitqueue_head (&ticket.wait); + ticket.bytes = bytes; + ticket.error =0 ; +list_add_tail (&ticket.list, &data_sinfo->priority_tickets); + data_sinfo->reclaim_size += bytes;spin_unlock (&data_sinfo->lock); -/* -* Everybody can force chunk allocation, so try this first to -* see if we can just bail here and make our reservation. -*/ -flush_space (fs_info, data_sinfo, bytes,ALLOC_CHUNK_FORCE ); -spin_lock (&data_sinfo->lock); -if (prev_total_bytes < data_sinfo->total_bytes) - goto again; + ret =handle_reserve_ticket (fs_info, data_sinfo, &ticket, + flush); + }else { +btrfs_space_info_update_bytes_may_use (fs_info, data_sinfo, bytes); + ret =0 ;spin_unlock (&data_sinfo->lock); - -/* -* Cycle through the rest of the flushing options for our flush -* type, then try again. -*/ -while (flush_state < states_nr) { -u64 flush_bytes =U64_MAX ; - -/* -* Previously we unconditionally committed the -* transaction twice before finally checking against -* pinned space before committing the final time. We -* also skipped flushing delalloc the final pass -* through. -*/ -if (!commit_cycles) { -if (states[flush_state] ==FLUSH_DELALLOC_WAIT ) { - flush_state++; -continue ; - } -if (states[flush_state] ==COMMIT_TRANS ) - flush_bytes = bytes; - } - -flush_space (fs_info, data_sinfo, flush_bytes, - states[flush_state]); - flush_state++; - } - -if (!commit_cycles) - goto out; - - commit_cycles--; -spin_lock (&data_sinfo->lock); - goto again; } -btrfs_space_info_update_bytes_may_use (fs_info, data_sinfo, bytes); - ret =0 ; -spin_unlock (&data_sinfo->lock); -out:if (ret) trace_btrfs_space_reservation(fs_info, "space_info:enospc",
Signature · 1 line
2.24.1
Re: [PATCH 15/23] btrfs: use ticketing for data space reservations
Quoted 8 lines · Josef Bacik
fs/btrfs/space-info.c +8
-0+static void priority_reclaim_data_space (struct btrfs_fs_info *fs_info, +struct btrfs_space_info *space_info, +struct reserve_ticket *ticket, +const enum btrfs_flush_state *states, +int states_nr) +{ +int flush_state =0 ; +int commit_cycles =2 ;
fs/btrfs/space-info.c static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info, · line 1090
Quoted 10 lines
fs/btrfs/space-info.c +10
-0+ +while (!space_info->full) { +flush_space (fs_info, space_info,U64_MAX ,ALLOC_CHUNK_FORCE ); +spin_lock (&space_info->lock); +if (ticket->bytes ==0 ) { +spin_unlock (&space_info->lock); +return ; + } +spin_unlock (&space_info->lock); + }
->full is checked unlocked, IIRC this is not a problem as it's sticky once it's 1 then it won't change until the flushing is done and everything else locks the space infos. So a comment would be good here.
Re: [PATCH 15/23] btrfs: use ticketing for data space reservations
In reply to Josef BacikQuoted 31 lines · Josef Bacik
fs/btrfs/space-info.c +31
-0+static void priority_reclaim_data_space (struct btrfs_fs_info *fs_info, +struct btrfs_space_info *space_info, +struct reserve_ticket *ticket, +const enum btrfs_flush_state *states, +int states_nr) +{ +int flush_state =0 ; +int commit_cycles =2 ; + +while (!space_info->full) { +flush_space (fs_info, space_info,U64_MAX ,ALLOC_CHUNK_FORCE ); +spin_lock (&space_info->lock); +if (ticket->bytes ==0 ) { +spin_unlock (&space_info->lock); +return ; + } +spin_unlock (&space_info->lock); + } +again: +while (flush_state < states_nr) { +u64 flush_bytes =U64_MAX ; + +if (!commit_cycles) { +if (states[flush_state] ==FLUSH_DELALLOC_WAIT ) { + flush_state++; +continue ; + } +if (states[flush_state] ==COMMIT_TRANS ) + flush_bytes = ticket->bytes; + } +
fs/btrfs/space-info.c static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info, · line 1113
Quoted 10 lines
fs/btrfs/space-info.c +10
-0+flush_space (fs_info, space_info, flush_bytes, + states[flush_state]); +spin_lock (&space_info->lock); +if (ticket->bytes ==0 ) { +spin_unlock (&space_info->lock); +return ; + } +spin_unlock (&space_info->lock); + flush_state++; + }
Actually, the whole logic in this loop could use some human readable description. There are 2 overlapping loops, one is over the states and the other are commit cycles, with some state-conditional changes.
Quoted 6 lines
fs/btrfs/space-info.c +6
-0+if (commit_cycles) { + commit_cycles--; + flush_state =0 ; + goto again; + } +}
Re: [PATCH 15/23] btrfs: use ticketing for data space reservations
Quoted 38 lines · David Sterba
Quoted 37 lines · Josef Bacik
fs/btrfs/space-info.c +37
-0+static void priority_reclaim_data_space (struct btrfs_fs_info *fs_info, +struct btrfs_space_info *space_info, +struct reserve_ticket *ticket, +const enum btrfs_flush_state *states, +int states_nr) +{ +int flush_state =0 ; +int commit_cycles =2 ; + +while (!space_info->full) { +flush_space (fs_info, space_info,U64_MAX ,ALLOC_CHUNK_FORCE ); +spin_lock (&space_info->lock); +if (ticket->bytes ==0 ) { +spin_unlock (&space_info->lock); +return ; + } +spin_unlock (&space_info->lock); + } +again: +while (flush_state < states_nr) { +u64 flush_bytes =U64_MAX ; + +if (!commit_cycles) { +if (states[flush_state] ==FLUSH_DELALLOC_WAIT ) { + flush_state++; +continue ; + } +if (states[flush_state] ==COMMIT_TRANS ) + flush_bytes = ticket->bytes; + } + +flush_space (fs_info, space_info, flush_bytes, + states[flush_state]); +spin_lock (&space_info->lock); +if (ticket->bytes ==0 ) { +spin_unlock (&space_info->lock); +return ;
Quoted 8 lines
fs/btrfs/space-info.c static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info, · line 1099
Quoted 4 lines
fs/btrfs/space-info.c +4
-0+ } +spin_unlock (&space_info->lock); + flush_state++; + }Actually, the whole logic in this loop could use some human readable description. There are 2 overlapping loops, one is over the states and the other are commit cycles, with some state-conditional changes.
Never mind, the code gets removed in a later patch.
PATCH v4 16/23 btrfs: serialize data reservations if we are flushing
In reply to Josef BacikNikolay reported a problem where generic/371 would fail sometimes with a slow drive. The gist of the test is that we fallocate a file in parallel with a pwrite of a different file. These two files combined are smaller than the file system, but sometimes the pwrite would ENOSPC.
A fair bit of investigation uncovered the fact that the fallocate workload was racing in and grabbing the free space that the pwrite workload was trying to free up so it could make its own reservation. After a few loops of this eventually the pwrite workload would error out with an ENOSPC.
We've had the same problem with metadata as well, and we serialized all metadata allocations to satisfy this problem. This wasn't usually a problem with data because data reservations are more straightforward, but obviously could still happen.
Fix this by not allowing reservations to occur if there are any pending tickets waiting to be satisfied on the space info.
--- fs/btrfs/space-info.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
fs/btrfs/space-info.c +5 -1
@@ -1402,13 +1402,17 @@ int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;u64 used;int ret = -ENOSPC ; +bool pending_tickets;ASSERT (!current->journal_info || flush !=BTRFS_RESERVE_FLUSH_DATA );spin_lock (&data_sinfo->lock); used =btrfs_space_info_used (data_sinfo, true); + pending_tickets = !list_empty (&data_sinfo->tickets) || + !list_empty (&data_sinfo->priority_tickets); -if (used + bytes > data_sinfo->total_bytes) { +if (pending_tickets || + used + bytes > data_sinfo->total_bytes) {struct reserve_ticket ticket;init_waitqueue_head (&ticket.wait);
Signature · 1 line
2.24.1
PATCH v4 17/23 btrfs: use the same helper for data and metadata reservations
In reply to Josef BacikNow that data reservations follow the same pattern as metadata reservations we can simply rename __reserve_metadata_bytes to __reserve_bytes and use that helper for data reservations.
Things to keep in mind, btrfs_can_overcommit() returns 0 for data, because we can never overcommit. We also will never pass in FLUSH_ALL for data, so we'll simply be added to the priority list and go straight into handle_reserve_ticket.
--- fs/btrfs/space-info.c | 49 +++++++++++++------------------------------ 1 file changed, 14 insertions(+), 35 deletions(-)
fs/btrfs/space-info.c +14 -35
@@ -1252,10 +1252,9 @@ static inline bool is_normal_flushing(enum btrfs_reserve_flush_enum flush)* regain reservations will be made and this will fail if there is not enough * space already. */ -static int __reserve_metadata_bytes (struct btrfs_fs_info *fs_info, -struct btrfs_space_info *space_info, -u64 orig_bytes, -enum btrfs_reserve_flush_enum flush) +static int __reserve_bytes (struct btrfs_fs_info *fs_info, +struct btrfs_space_info *space_info,u64 orig_bytes, +enum btrfs_reserve_flush_enum flush) {struct reserve_ticket ticket;u64 used;
@@ -1367,8 +1366,8 @@ int btrfs_reserve_metadata_bytes(struct btrfs_root *root,struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;int ret; - ret =__reserve_metadata_bytes (fs_info, block_rsv->space_info, - orig_bytes, flush); + ret =__reserve_bytes (fs_info, block_rsv->space_info, orig_bytes, + flush);if (ret == -ENOSPC &&unlikely (root->orphan_cleanup_state ==ORPHAN_CLEANUP_STARTED )) { if (block_rsv != global_rsv &&
@@ -1400,38 +1399,18 @@ int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes, enum btrfs_reserve_flush_enum flush) {struct btrfs_space_info *data_sinfo = fs_info->data_sinfo; -u64 used; -int ret = -ENOSPC ; -bool pending_tickets; +int ret; +ASSERT (flush ==BTRFS_RESERVE_FLUSH_DATA || + flush ==BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE );ASSERT (!current->journal_info || flush !=BTRFS_RESERVE_FLUSH_DATA ); -spin_lock (&data_sinfo->lock); - used =btrfs_space_info_used (data_sinfo, true); - pending_tickets = !list_empty (&data_sinfo->tickets) || - !list_empty (&data_sinfo->priority_tickets); - -if (pending_tickets || - used + bytes > data_sinfo->total_bytes) { -struct reserve_ticket ticket; - -init_waitqueue_head (&ticket.wait); - ticket.bytes = bytes; - ticket.error =0 ; -list_add_tail (&ticket.list, &data_sinfo->priority_tickets); - data_sinfo->reclaim_size += bytes; -spin_unlock (&data_sinfo->lock); - - ret =handle_reserve_ticket (fs_info, data_sinfo, &ticket, - flush); - }else { -btrfs_space_info_update_bytes_may_use (fs_info, data_sinfo, bytes); - ret =0 ; -spin_unlock (&data_sinfo->lock); - } -if (ret) -trace_btrfs_space_reservation (fs_info, -"space_info:enospc" , + ret =__reserve_bytes (fs_info, data_sinfo, bytes, flush); +if (ret == -ENOSPC ) { +trace_btrfs_space_reservation (fs_info,"space_info:enospc" , data_sinfo->flags, bytes,1 ); +if (btrfs_test_opt (fs_info,ENOSPC_DEBUG )) +btrfs_dump_space_info (fs_info, data_sinfo, bytes,0 ); + }return ret; }
Signature · 1 line
2.24.1
PATCH v4 18/23 btrfs: drop the commit_cycles stuff for data reservations
In reply to Josef BacikThis was an old wart left over from how we previously did data reservations. Before we could have people race in and take a reservation while we were flushing space, so we needed to make sure we looped a few times before giving up. Now that we're using the ticketing infrastructure we don't have to worry about this and can drop the logic altogether.
--- fs/btrfs/space-info.c | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-)
fs/btrfs/space-info.c +2 -20
@@ -1077,7 +1077,6 @@ static void priority_reclaim_data_space(struct btrfs_fs_info *fs_info, int states_nr) {int flush_state =0 ; -int commit_cycles =2 ;while (!space_info->full) {flush_space (fs_info, space_info,U64_MAX ,ALLOC_CHUNK_FORCE );
@@ -1088,21 +1087,9 @@ static void priority_reclaim_data_space(struct btrfs_fs_info *fs_info, }spin_unlock (&space_info->lock); } -again: -while (flush_state < states_nr) { -u64 flush_bytes =U64_MAX ; - -if (!commit_cycles) { -if (states[flush_state] ==FLUSH_DELALLOC_WAIT ) { - flush_state++; -continue ; - } -if (states[flush_state] ==COMMIT_TRANS ) - flush_bytes = ticket->bytes; - } -flush_space (fs_info, space_info, flush_bytes, - states[flush_state]); +while (flush_state < states_nr) { +flush_space (fs_info, space_info,U64_MAX , states[flush_state]);spin_lock (&space_info->lock);if (ticket->bytes ==0 ) {spin_unlock (&space_info->lock);
@@ -1111,11 +1098,6 @@ static void priority_reclaim_data_space(struct btrfs_fs_info *fs_info,spin_unlock (&space_info->lock); flush_state++; } -if (commit_cycles) { - commit_cycles--; - flush_state =0 ; - goto again; - } } static void wait_reserve_ticket(struct btrfs_fs_info *fs_info,
Signature · 1 line
2.24.1
PATCH v4 19/23 btrfs: don't force commit if we are data
In reply to Josef BacikWe used to unconditionally commit the transaction at least 2 times and then on the 3rd try check against pinned space to make sure committing the transaction was worth the effort. This is overkill, we know nobody is going to steal our reservation, and if we can't make our reservation with the pinned amount simply bail out.
This also cleans up the passing of bytes_needed to may_commit_transaction, as that was the thing we added into place in order to accomplish this behavior. We no longer need it so remove that mess.
--- fs/btrfs/space-info.c | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-)
fs/btrfs/space-info.c +5 -19
@@ -579,8 +579,7 @@ static void shrink_delalloc(struct btrfs_fs_info *fs_info,* will return -ENOSPC. */ static int may_commit_transaction (struct btrfs_fs_info *fs_info, -struct btrfs_space_info *space_info, -u64 bytes_needed) +struct btrfs_space_info *space_info) {struct reserve_ticket *ticket =NULL ;struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv;
@@ -588,24 +587,13 @@ static int may_commit_transaction(struct btrfs_fs_info *fs_info,struct btrfs_block_rsv *trans_rsv = &fs_info->trans_block_rsv;struct btrfs_trans_handle *trans;u64 reclaim_bytes =0 ; +u64 bytes_needed;u64 cur_free_bytes =0 ; -bool do_commit = false; trans = (struct btrfs_trans_handle *)current->journal_info;if (trans)return -EAGAIN ; -/* -* If we are data and have passed in U64_MAX we just want to -* unconditionally commit the transaction to match the previous data -* flushing behavior. -*/ -if ((space_info->flags &BTRFS_BLOCK_GROUP_DATA ) && - bytes_needed ==U64_MAX ) { - do_commit = true; - goto check_pinned; - } -spin_lock (&space_info->lock); cur_free_bytes =btrfs_space_info_used (space_info, true);if (cur_free_bytes < space_info->total_bytes)
@@ -619,7 +607,7 @@ static int may_commit_transaction(struct btrfs_fs_info *fs_info,else if (!list_empty (&space_info->tickets)) ticket =list_first_entry (&space_info->tickets, struct reserve_ticket, list); - bytes_needed = (ticket) ? ticket->bytes : bytes_needed; + bytes_needed = (ticket) ? ticket->bytes :0 ;if (bytes_needed > cur_free_bytes) bytes_needed -= cur_free_bytes;
@@ -630,7 +618,6 @@ static int may_commit_transaction(struct btrfs_fs_info *fs_info,if (!bytes_needed)return 0 ; -check_pinned: trans =btrfs_join_transaction (fs_info->extent_root);if (IS_ERR (trans))return PTR_ERR (trans);
@@ -640,8 +627,7 @@ static int may_commit_transaction(struct btrfs_fs_info *fs_info,* we have block groups that are going to be freed, allowing us to * possibly do a chunk allocation the next loop through. */ -if (do_commit || -test_bit (BTRFS_TRANS_HAVE_FREE_BGS , &trans->transaction->flags) || +if (test_bit (BTRFS_TRANS_HAVE_FREE_BGS , &trans->transaction->flags) ||__percpu_counter_compare (&space_info->total_bytes_pinned, bytes_needed,BTRFS_TOTAL_BYTES_PINNED_BATCH ) >=0 )
@@ -759,7 +745,7 @@ static void flush_space(struct btrfs_fs_info *fs_info,btrfs_wait_on_delayed_iputs (fs_info);break ;case COMMIT_TRANS : - ret =may_commit_transaction (fs_info, space_info, num_bytes); + ret =may_commit_transaction (fs_info, space_info);break ;default : ret = -ENOSPC ;
Signature · 1 line
2.24.1
PATCH v4 20/23 btrfs: run delayed iputs before committing the transaction for data
In reply to Josef BacikBefore we were waiting on iputs after we committed the transaction, but this doesn't really make much sense. We want to reclaim any space we may have in order to be more likely to commit the transaction, due to pinned space being added by running the delayed iputs. Fix this by making delayed iputs run before committing the transaction.
--- fs/btrfs/space-info.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/space-info.c +1 -1
@@ -1022,8 +1022,8 @@ static const enum btrfs_flush_state evict_flush_states[] = {static const enum btrfs_flush_state data_flush_states[] = {FLUSH_DELALLOC_WAIT , -COMMIT_TRANS ,RUN_DELAYED_IPUTS , +COMMIT_TRANS , }; static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,
Signature · 1 line
2.24.1
PATCH v4 21/23 btrfs: flush delayed refs when trying to reserve data space
In reply to Josef BacikWe can end up with free'd extents in the delayed refs, and thus may_commit_transaction() may not think we have enough pinned space to commit the transaction and we'll ENOSPC early. Handle this by running the delayed refs in order to make sure pinned is uptodate before we try to commit the transaction.
--- fs/btrfs/space-info.c | 1 + 1 file changed, 1 insertion(+)
fs/btrfs/space-info.c +1 -0
@@ -1023,6 +1023,7 @@ static const enum btrfs_flush_state evict_flush_states[] = {static const enum btrfs_flush_state data_flush_states[] = {FLUSH_DELALLOC_WAIT ,RUN_DELAYED_IPUTS , +FLUSH_DELAYED_REFS ,COMMIT_TRANS , };
Signature · 1 line
2.24.1
PATCH v4 22/23 btrfs: do async reclaim for data reservations
In reply to Josef BacikNow that we have the data ticketing stuff in place, move normal data reservations to use an async reclaim helper to satisfy tickets. Before we could have multiple tasks race in and both allocate chunks, resulting in more data chunks than we would necessarily need. Serializing these allocations and making a single thread responsible for flushing will only allocate chunks as needed, as well as cut down on transaction commits and other flush related activities.
Priority reservations will still work as they have before, simply trying to allocate a chunk until they can make their reservation.
--- fs/btrfs/ctree.h | 3 +- fs/btrfs/disk-io.c | 3 +- fs/btrfs/space-info.c | 123 ++++++++++++++++++++++++++++++------------ fs/btrfs/super.c | 1 + 4 files changed, 93 insertions(+), 37 deletions(-)
fs/btrfs/ctree.h +2 -1
@@ -494,7 +494,7 @@ enum btrfs_orphan_cleanup_state { ORPHAN_CLEANUP_DONE = 2, }; -void btrfs_init_async_reclaim_work (struct work_struct *work); +void btrfs_init_async_reclaim_work (struct btrfs_fs_info *fs_info);/* fs_info */ struct reloc_control ;
@@ -912,6 +912,7 @@ struct btrfs_fs_info {/* Used to reclaim the metadata space in the background. */ struct work_struct async_reclaim_work; +struct work_struct async_data_reclaim_work;spinlock_t unused_bgs_lock;struct list_head unused_bgs;
fs/btrfs/disk-io.c +2 -1
@@ -2775,7 +2775,7 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) fs_info->check_integrity_print_mask =0 ;#endif btrfs_init_balance (fs_info); -btrfs_init_async_reclaim_work (&fs_info->async_reclaim_work); +btrfs_init_async_reclaim_work (fs_info);spin_lock_init (&fs_info->block_group_cache_lock); fs_info->block_group_cache_tree =RB_ROOT ;
@@ -4078,6 +4078,7 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info)btrfs_cleanup_defrag_inodes (fs_info);cancel_work_sync (&fs_info->async_reclaim_work); +cancel_work_sync (&fs_info->async_data_reclaim_work);/* Cancel or finish ongoing discard work */ btrfs_discard_cleanup (fs_info);
fs/btrfs/space-info.c +88 -35
186 lines not on this page · raw message
fs/btrfs/super.c +1 -0
@@ -1852,6 +1852,7 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)* the filesystem is busy. */ cancel_work_sync (&fs_info->async_reclaim_work); +cancel_work_sync (&fs_info->async_data_reclaim_work);btrfs_discard_cleanup (fs_info);
Signature · 1 line
2.24.1
PATCH v4 23/23 btrfs: add a comment explaining the data flush steps
In reply to Josef BacikThe data flushing steps are not obvious to people other than myself and Chris. Write a giant comment explaining the reasoning behind each flush step for data as well as why it is in that particular order.
--- fs/btrfs/space-info.c | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)
fs/btrfs/space-info.c +47 -0
@@ -998,6 +998,53 @@ static void btrfs_async_reclaim_metadata_space(struct work_struct *work) }while (flush_state <=COMMIT_TRANS ); } +/* +* FLUSH_DELALLOC_WAIT: +* Space is free'd from flushing delalloc in one of two ways. +* +* 1) compression is on and we allocate less space than we reserved. +* 2) We are overwriting existing space. +* +* For #1 that extra space is reclaimed as soon as the delalloc pages are +* cow'ed, by way of btrfs_add_reserved_bytes() which adds the actual extent +* length to ->bytes_reserved, and subtracts the reserved space from +* ->bytes_may_use. +* +* For #2 this is trickier. Once the ordered extent runs we will drop the +* extent in the range we are overwriting, which creates a delayed ref for +* that freed extent. This however is not reclaimed until the transaction +* commits, thus the next stages. +* +* RUN_DELAYED_IPUTS +* If we are freeing inodes, we want to make sure all delayed iputs have +* completed, because they could have been on an inode with i_nlink == 0, and +* thus have been trunated and free'd up space. But again this space is not +* immediately re-usable, it comes in the form of a delayed ref, which must be +* run and then the transaction must be committed. +* +* FLUSH_DELAYED_REFS +* The above two cases generate delayed refs that will affect +* ->total_bytes_pinned. However this counter can be inconsistent with +* reality if there are outstanding delayed refs. This is because we adjust +* the counter based soley on the current set of delayed refs and disregard +* any on-disk state which might include more refs. So for example, if we +* have an extent with 2 references, but we only drop 1, we'll see that there +* is a negative delayed ref count for the extent and assume that the space +* will be free'd, and thus increase ->total_bytes_pinned. +* +* Running the delayed refs gives us the actual real view of what will be +* freed at the transaction commit time. This stage will not actually free +* space for us, it just makes sure that may_commit_transaction() has all of +* the information it needs to make the right decision. +* +* COMMIT_TRANS +* This is where we reclaim all of the pinned space generated by the previous +* two stages. We will not commit the transaction if we don't think we're +* likely to satisfy our request, which means if our current free space + +* total_bytes_pinned < reservation we will not commit. This is why the +* previous states are actually important, to make sure we know for sure +* whether committing the transaction will allow us to make progress. +*/ static const enum btrfs_flush_state data_flush_states[] = {FLUSH_DELALLOC_WAIT ,RUN_DELAYED_IPUTS ,
Signature · 1 line
2.24.1
Quoted 19 lines · Josef Bacik
v3->v4:
- Rebased onto a recent misc-next, slight fixup because of commenting around the flush states.
v2->v3:
- Rebased onto a recent misc-next
v1->v2:
- Adjusted a comment inmay_commit_transaction.
- Fixed one of the intermediate patches to properly update ->reclaim_size.We've had two different things in place to reserve data and metadata space, because generally speaking data is much simpler. However the data reservations still suffered from the same issues that plagued metadata reservations, you could get multiple tasks racing in to get reservations. This causes problems with cases like write/delete loops where we should be able to fill up the fs, delete everything, and go again. You would sometimes race with a flusher that's trying to unpin the free space, take it's reservations, and then it would fail.
Quoted 6 lines
Fix this by moving the data reservations under the metadata ticketing infrastructure. This gets rid of that problem, and simplifies our enospc code more by consolidating it into a single path. Thanks,
Josef
I've gone through the series again and it looks good. However, btrfs_alloc_data_chunk_ondemand no longer allocates a data chunk but simply tries to reserve the requested data space. This means this series needs another patch giving it a more becoming name, something like btrfs_(alloc|reserve)_data_space or some such ?
Quoted 26 lines · Nikolay Borisov
Quoted 23 lines · Josef Bacik
v3->v4:
- Rebased onto a recent misc-next, slight fixup because of commenting around the flush states.
v2->v3:
- Rebased onto a recent misc-next
v1->v2:
- Adjusted a comment inmay_commit_transaction.
- Fixed one of the intermediate patches to properly update ->reclaim_size.We've had two different things in place to reserve data and metadata space, because generally speaking data is much simpler. However the data reservations still suffered from the same issues that plagued metadata reservations, you could get multiple tasks racing in to get reservations. This causes problems with cases like write/delete loops where we should be able to fill up the fs, delete everything, and go again. You would sometimes race with a flusher that's trying to unpin the free space, take it's reservations, and then it would fail.
Fix this by moving the data reservations under the metadata ticketing infrastructure. This gets rid of that problem, and simplifies our enospc code more by consolidating it into a single path. Thanks,
Quoted 8 lines
Quoted 2 lines
Josef
I've gone through the series again and it looks good. However,
btrfs_alloc_data_chunk_ondemandno longer allocates a data chunk but simply tries to reserve the requested data space. This means this series needs another patch giving it a more becoming name, something like btrfs_(alloc|reserve)_data_space or some such ?
Yes please, this could be confunsing as allocation and reservation are different things.
Quoted 19 lines · Josef Bacik
v3->v4:
- Rebased onto a recent misc-next, slight fixup because of commenting around the flush states.
v2->v3:
- Rebased onto a recent misc-next
v1->v2:
- Adjusted a comment inmay_commit_transaction.
- Fixed one of the intermediate patches to properly update ->reclaim_size.We've had two different things in place to reserve data and metadata space, because generally speaking data is much simpler. However the data reservations still suffered from the same issues that plagued metadata reservations, you could get multiple tasks racing in to get reservations. This causes problems with cases like write/delete loops where we should be able to fill up the fs, delete everything, and go again. You would sometimes race with a flusher that's trying to unpin the free space, take it's reservations, and then it would fail.
Quoted 3 lines
Fix this by moving the data reservations under the metadata ticketing infrastructure. This gets rid of that problem, and simplifies our enospc code more by consolidating it into a single path. Thanks,
V4 was in for-next so it got some testing coverage, though I haven't done enospc targeted tests. I'm going to go through this series and put it to misc-next once rc1 is out.
Quoted 20 lines · David Sterba
Quoted 19 lines · Josef Bacik
v3->v4:
- Rebased onto a recent misc-next, slight fixup because of commenting around the flush states.
v2->v3:
- Rebased onto a recent misc-next
v1->v2:
- Adjusted a comment inmay_commit_transaction.
- Fixed one of the intermediate patches to properly update ->reclaim_size.We've had two different things in place to reserve data and metadata space, because generally speaking data is much simpler. However the data reservations still suffered from the same issues that plagued metadata reservations, you could get multiple tasks racing in to get reservations. This causes problems with cases like write/delete loops where we should be able to fill up the fs, delete everything, and go again. You would sometimes race with a flusher that's trying to unpin the free space, take it's reservations, and then it would fail.
Quoted 7 lines
Quoted 3 lines
Fix this by moving the data reservations under the metadata ticketing infrastructure. This gets rid of that problem, and simplifies our enospc code more by consolidating it into a single path. Thanks,
V4 was in for-next so it got some testing coverage, though I haven't done enospc targeted tests. I'm going to go through this series and put it to misc-next once rc1 is out.
It's in misc-next now (5.9-rc1 based).