if (sb->s_flags & MS_RDONLY)
return 0;
+ if (btrfs_test_opt(tree_root, FREE_SPACE_TREE) &&
+ !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
+ pr_info("BTRFS: creating free space tree\n");
+ ret = btrfs_create_free_space_tree(fs_info);
+ if (ret) {
+ pr_warn("BTRFS: failed to create free space tree %d\n",
+ ret);
+ close_ctree(tree_root);
+ return ret;
+ }
+ }
+
down_read(&fs_info->cleanup_work_sem);
if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) ||
(ret = btrfs_orphan_cleanup(fs_info->tree_root))) {
}
}
- if (btrfs_test_opt(tree_root, FREE_SPACE_TREE) &&
- !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
- pr_info("BTRFS: creating free space tree\n");
- ret = btrfs_create_free_space_tree(fs_info);
- if (ret) {
- pr_warn("BTRFS: failed to create free space tree %d\n",
- ret);
- close_ctree(tree_root);
- return ret;
- }
- }
-
if (!fs_info->uuid_root) {
pr_info("BTRFS: creating UUID tree\n");
ret = btrfs_create_uuid_tree(fs_info);
if (ret)
goto out;
+ mutex_lock(&block_group->free_space_lock);
+
/*
* Iterate through all of the extent and metadata items in this block
* group, adding the free space between them and the free space at the
ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0);
if (ret < 0)
- goto out;
+ goto out_locked;
ASSERT(ret == 0);
start = block_group->key.objectid;
key.objectid -
start);
if (ret)
- goto out;
+ goto out_locked;
}
start = key.objectid;
if (key.type == BTRFS_METADATA_ITEM_KEY)
ret = btrfs_next_item(extent_root, path);
if (ret < 0)
- goto out;
+ goto out_locked;
if (ret)
break;
}
ret = __add_to_free_space_tree(trans, fs_info, block_group,
path2, start, end - start);
if (ret)
- goto out;
+ goto out_locked;
}
ret = 0;
+out_locked:
+ mutex_unlock(&block_group->free_space_lock);
out:
btrfs_free_path(path2);
btrfs_free_path(path);
if (IS_ERR(trans))
return PTR_ERR(trans);
+ fs_info->creating_free_space_tree = 1;
free_space_root = btrfs_create_tree(trans, fs_info,
BTRFS_FREE_SPACE_TREE_OBJECTID);
if (IS_ERR(free_space_root)) {
}
btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE);
+ fs_info->creating_free_space_tree = 0;
ret = btrfs_commit_transaction(trans, tree_root);
if (ret)
return 0;
abort:
+ fs_info->creating_free_space_tree = 0;
btrfs_abort_transaction(trans, tree_root, ret);
btrfs_end_transaction(trans, tree_root);
return ret;