Skip to content

Commit

Permalink
Btrfs: prepare block group cache before writing
Browse files Browse the repository at this point in the history
Writing the block group cache will modify the extent tree quite a bit because it
truncates the old space cache and pre-allocates new stuff.  To try and cut down
on the churn lets do the setup dance first, then later on hopefully we can avoid
looping with newly dirtied roots.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
  • Loading branch information
Josef Bacik committed Mar 17, 2015
1 parent ea526d1 commit dcdf7f6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions fs/btrfs/ctree.h
Expand Up @@ -3386,6 +3386,8 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,

int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
struct btrfs_root *root);
int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
struct btrfs_root *root);
int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr);
int btrfs_free_block_groups(struct btrfs_fs_info *info);
int btrfs_read_block_groups(struct btrfs_root *root);
Expand Down
26 changes: 26 additions & 0 deletions fs/btrfs/extent-tree.c
Expand Up @@ -3325,6 +3325,32 @@ static int cache_save_setup(struct btrfs_block_group_cache *block_group,
return ret;
}

int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
struct btrfs_root *root)
{
struct btrfs_block_group_cache *cache, *tmp;
struct btrfs_transaction *cur_trans = trans->transaction;
struct btrfs_path *path;

if (list_empty(&cur_trans->dirty_bgs) ||
!btrfs_test_opt(root, SPACE_CACHE))
return 0;

path = btrfs_alloc_path();
if (!path)
return -ENOMEM;

/* Could add new block groups, use _safe just in case */
list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
dirty_list) {
if (cache->disk_cache_state == BTRFS_DC_CLEAR)
cache_save_setup(cache, trans, path);
}

btrfs_free_path(path);
return 0;
}

int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
struct btrfs_root *root)
{
Expand Down
5 changes: 4 additions & 1 deletion fs/btrfs/transaction.c
Expand Up @@ -1025,7 +1025,6 @@ static int update_cowonly_root(struct btrfs_trans_handle *trans,
struct btrfs_root *tree_root = root->fs_info->tree_root;

old_root_used = btrfs_root_used(&root->root_item);
btrfs_write_dirty_block_groups(trans, root);

while (1) {
old_root_bytenr = btrfs_root_bytenr(&root->root_item);
Expand Down Expand Up @@ -1085,6 +1084,10 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
if (ret)
return ret;

ret = btrfs_setup_space_cache(trans, root);
if (ret)
return ret;

/* run_qgroups might have added some more refs */
ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
if (ret)
Expand Down

0 comments on commit dcdf7f6

Please sign in to comment.