Commit 64403612 authored by Josef Bacik's avatar Josef Bacik Committed by David Sterba

btrfs: rework btrfs_check_space_for_delayed_refs

Now with the delayed_refs_rsv we can now know exactly how much pending
delayed refs space we need.  This means we can drastically simplify
btrfs_check_space_for_delayed_refs by simply checking how much space we
have reserved for the global rsv (which acts as a spill over buffer) and
the delayed refs rsv.  If our total size is beyond that amount then we
know it's time to commit the transaction and stop any more delayed refs
from being generated.

With the introduction of dealyed_refs_rsv infrastructure, namely
btrfs_update_delayed_refs_rsv we now know exactly how much pending
delayed refs space is required.
Reviewed-by: default avatarNikolay Borisov <nborisov@suse.com>
Signed-off-by: default avatarJosef Bacik <josef@toxicpanda.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 413df725
...@@ -2651,7 +2651,7 @@ static inline u64 btrfs_calc_trunc_metadata_size(struct btrfs_fs_info *fs_info, ...@@ -2651,7 +2651,7 @@ static inline u64 btrfs_calc_trunc_metadata_size(struct btrfs_fs_info *fs_info,
} }
int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans); int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans);
int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans); bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info);
void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info, void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
const u64 start); const u64 start);
void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg); void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg);
......
...@@ -2839,40 +2839,28 @@ u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes) ...@@ -2839,40 +2839,28 @@ u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
return num_csums; return num_csums;
} }
int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans) bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info)
{ {
struct btrfs_fs_info *fs_info = trans->fs_info; struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
struct btrfs_block_rsv *global_rsv; struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
u64 num_heads = trans->transaction->delayed_refs.num_heads_ready; bool ret = false;
u64 csum_bytes = trans->transaction->delayed_refs.pending_csums; u64 reserved;
unsigned int num_dirty_bgs = trans->transaction->num_dirty_bgs;
u64 num_bytes, num_dirty_bgs_bytes;
int ret = 0;
num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); spin_lock(&global_rsv->lock);
num_heads = heads_to_leaves(fs_info, num_heads); reserved = global_rsv->reserved;
if (num_heads > 1) spin_unlock(&global_rsv->lock);
num_bytes += (num_heads - 1) * fs_info->nodesize;
num_bytes <<= 1;
num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) *
fs_info->nodesize;
num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info,
num_dirty_bgs);
global_rsv = &fs_info->global_block_rsv;
/* /*
* If we can't allocate any more chunks lets make sure we have _lots_ of * Since the global reserve is just kind of magic we don't really want
* wiggle room since running delayed refs can create more delayed refs. * to rely on it to save our bacon, so if our size is more than the
* delayed_refs_rsv and the global rsv then it's time to think about
* bailing.
*/ */
if (global_rsv->space_info->full) { spin_lock(&delayed_refs_rsv->lock);
num_dirty_bgs_bytes <<= 1; reserved += delayed_refs_rsv->reserved;
num_bytes <<= 1; if (delayed_refs_rsv->size >= reserved)
} ret = true;
spin_unlock(&delayed_refs_rsv->lock);
spin_lock(&global_rsv->lock);
if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
ret = 1;
spin_unlock(&global_rsv->lock);
return ret; return ret;
} }
...@@ -2891,7 +2879,7 @@ int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans) ...@@ -2891,7 +2879,7 @@ int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans)
if (val >= NSEC_PER_SEC / 2) if (val >= NSEC_PER_SEC / 2)
return 2; return 2;
return btrfs_check_space_for_delayed_refs(trans); return btrfs_check_space_for_delayed_refs(trans->fs_info);
} }
struct async_delayed_refs { struct async_delayed_refs {
......
...@@ -5331,8 +5331,8 @@ static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root, ...@@ -5331,8 +5331,8 @@ static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
* Try to steal from the global reserve if there is space for * Try to steal from the global reserve if there is space for
* it. * it.
*/ */
if (!btrfs_check_space_for_delayed_refs(trans) && if (!btrfs_check_space_for_delayed_refs(fs_info) &&
!btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, false)) !btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0))
return trans; return trans;
/* If not, commit and try again. */ /* If not, commit and try again. */
......
...@@ -789,7 +789,7 @@ static int should_end_transaction(struct btrfs_trans_handle *trans) ...@@ -789,7 +789,7 @@ static int should_end_transaction(struct btrfs_trans_handle *trans)
{ {
struct btrfs_fs_info *fs_info = trans->fs_info; struct btrfs_fs_info *fs_info = trans->fs_info;
if (btrfs_check_space_for_delayed_refs(trans)) if (btrfs_check_space_for_delayed_refs(fs_info))
return 1; return 1;
return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5); return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment