btrfs-progs: stop accessing ->csum_root directly

With extent tree v2 we will have per-block group checksums, so add a
helper to access the csum root and rename the fs_info csum_root to
_csum_root to catch all the places that are accessing it directly.
Convert everybody to use the helper except for internal things.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Josef Bacik 2021-11-10 15:07:59 -05:00 committed by David Sterba
parent 8742a38d5f
commit 639b1fc2e7
9 changed files with 45 additions and 35 deletions

View File

@ -1031,7 +1031,7 @@ static int delete_csum(struct btrfs_root *root, u64 bytenr, u64 bytes)
struct btrfs_trans_handle *trans;
int ret;
root = root->fs_info->csum_root;
root = btrfs_csum_root(root->fs_info, bytenr);
trans = btrfs_start_transaction(root, 1);
if (IS_ERR(trans)) {
fprintf(stderr, "Couldn't start transaction %ld\n",

View File

@ -5992,7 +5992,7 @@ static int check_csums(struct btrfs_root *root)
max_entries = ((BTRFS_LEAF_DATA_SIZE(gfs_info) -
(sizeof(struct btrfs_item) * 2)) / csum_size) - 1;
root = gfs_info->csum_root;
root = btrfs_csum_root(gfs_info, 0);
if (!extent_buffer_uptodate(root->node)) {
fprintf(stderr, "No valid csum tree found\n");
return -ENOENT;
@ -9509,7 +9509,7 @@ static int populate_csum(struct btrfs_trans_handle *trans,
static int fill_csum_tree_from_one_fs_root(struct btrfs_trans_handle *trans,
struct btrfs_root *cur_root)
{
struct btrfs_root *csum_root = gfs_info->csum_root;
struct btrfs_root *csum_root;
struct btrfs_path path;
struct btrfs_key key;
struct extent_buffer *node;
@ -9545,6 +9545,7 @@ static int fill_csum_tree_from_one_fs_root(struct btrfs_trans_handle *trans,
start = btrfs_file_extent_disk_bytenr(node, fi);
len = btrfs_file_extent_disk_num_bytes(node, fi);
csum_root = btrfs_csum_root(gfs_info, start);
ret = populate_csum(trans, csum_root, buf, start, len);
if (ret == -EEXIST)
ret = 0;
@ -9631,7 +9632,7 @@ out:
static int fill_csum_tree_from_extent(struct btrfs_trans_handle *trans)
{
struct btrfs_root *extent_root = gfs_info->extent_root;
struct btrfs_root *csum_root = gfs_info->csum_root;
struct btrfs_root *csum_root;
struct btrfs_path path;
struct btrfs_extent_item *ei;
struct extent_buffer *leaf;
@ -9681,6 +9682,7 @@ static int fill_csum_tree_from_extent(struct btrfs_trans_handle *trans)
continue;
}
csum_root = btrfs_csum_root(gfs_info, key.objectid);
ret = populate_csum(trans, csum_root, buf, key.objectid,
key.offset);
if (ret)
@ -10701,7 +10703,8 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
if (init_csum_tree) {
printf("Reinitialize checksum tree\n");
ret = btrfs_fsck_reinit_root(trans, gfs_info->csum_root);
ret = btrfs_fsck_reinit_root(trans,
btrfs_csum_root(gfs_info, 0));
if (ret) {
error("checksum tree initialization failed: %d",
ret);
@ -10732,7 +10735,9 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
err |= !!ret;
goto close_out;
}
if (!extent_buffer_uptodate(gfs_info->csum_root->node)) {
root = btrfs_csum_root(gfs_info, 0);
if (!extent_buffer_uptodate(root->node)) {
error("critical: csum_root, unable to check the filesystem");
ret = -EIO;
err |= !!ret;

View File

@ -287,6 +287,7 @@ out:
*/
int count_csum_range(u64 start, u64 len, u64 *found)
{
struct btrfs_root *csum_root = btrfs_csum_root(gfs_info, start);
struct btrfs_key key;
struct btrfs_path path;
struct extent_buffer *leaf;
@ -302,8 +303,7 @@ int count_csum_range(u64 start, u64 len, u64 *found)
key.offset = start;
key.type = BTRFS_EXTENT_CSUM_KEY;
ret = btrfs_search_slot(NULL, gfs_info->csum_root,
&key, &path, 0, 0);
ret = btrfs_search_slot(NULL, csum_root, &key, &path, 0, 0);
if (ret < 0)
goto out;
if (ret > 0 && path.slots[0] > 0) {
@ -317,7 +317,7 @@ int count_csum_range(u64 start, u64 len, u64 *found)
while (len > 0) {
leaf = path.nodes[0];
if (path.slots[0] >= btrfs_header_nritems(leaf)) {
ret = btrfs_next_leaf(gfs_info->csum_root, &path);
ret = btrfs_next_leaf(csum_root, &path);
if (ret > 0)
break;
else if (ret < 0)

View File

@ -1799,7 +1799,7 @@ static int btrfs_rebuild_chunk_stripes(struct recover_control *rc,
return ret;
}
static int next_csum(struct btrfs_root *root,
static int next_csum(struct btrfs_root *csum_root,
struct extent_buffer **leaf,
struct btrfs_path *path,
int *slot,
@ -1809,10 +1809,9 @@ static int next_csum(struct btrfs_root *root,
struct btrfs_key *key)
{
int ret = 0;
struct btrfs_root *csum_root = root->fs_info->csum_root;
struct btrfs_csum_item *csum_item;
u32 blocksize = root->fs_info->sectorsize;
u16 csum_size = root->fs_info->csum_size;
u32 blocksize = csum_root->fs_info->sectorsize;
u16 csum_size = csum_root->fs_info->csum_size;
int csums_in_item = btrfs_item_size_nr(*leaf, *slot) / csum_size;
if (*csum_offset >= csums_in_item) {
@ -1992,7 +1991,6 @@ static int rebuild_raid_data_chunk_stripes(struct recover_control *rc,
LIST_HEAD(unordered);
LIST_HEAD(candidates);
csum_root = root->fs_info->csum_root;
btrfs_init_path(&path);
list_splice_init(&chunk->dextents, &candidates);
again:
@ -2003,6 +2001,7 @@ again:
key.type = BTRFS_EXTENT_CSUM_KEY;
key.offset = start;
csum_root = btrfs_csum_root(root->fs_info, start);
ret = btrfs_search_slot(NULL, csum_root, &key, &path, 0, 0);
if (ret < 0) {
fprintf(stderr, "Search csum failed(%d)\n", ret);
@ -2020,8 +2019,8 @@ again:
} else if (ret > 0) {
slot = btrfs_header_nritems(leaf) - 1;
btrfs_item_key_to_cpu(leaf, &key, slot);
if (item_end_offset(root, &key, leaf, slot)
> start) {
if (item_end_offset(csum_root, &key,
leaf, slot) > start) {
csum_offset = start - key.offset;
csum_offset /= blocksize;
goto next_csum;
@ -2059,8 +2058,8 @@ again:
goto out;
}
next_csum:
ret = next_csum(root, &leaf, &path, &slot, &csum_offset, &tree_csum,
end, &key);
ret = next_csum(csum_root, &leaf, &path, &slot, &csum_offset,
&tree_csum, end, &key);
if (ret < 0) {
fprintf(stderr, "Fetch csum failed\n");
goto fail_out;

View File

@ -1167,7 +1167,7 @@ struct btrfs_fs_info {
struct btrfs_root *tree_root;
struct btrfs_root *chunk_root;
struct btrfs_root *dev_root;
struct btrfs_root *csum_root;
struct btrfs_root *_csum_root;
struct btrfs_root *quota_root;
struct btrfs_root *free_space_root;
struct btrfs_root *uuid_root;

View File

@ -755,6 +755,12 @@ int btrfs_fs_roots_compare_roots(struct rb_node *node1, struct rb_node *node2)
return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
}
struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info,
u64 bytenr)
{
return fs_info->_csum_root;
}
struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
struct btrfs_key *location)
{
@ -772,7 +778,7 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
return fs_info->dev_root;
if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
return fs_info->csum_root;
return btrfs_csum_root(fs_info, location->offset);
if (location->objectid == BTRFS_UUID_TREE_OBJECTID)
return fs_info->uuid_root ? fs_info->uuid_root : ERR_PTR(-ENOENT);
if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
@ -808,7 +814,7 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
free(fs_info->extent_root);
free(fs_info->chunk_root);
free(fs_info->dev_root);
free(fs_info->csum_root);
free(fs_info->_csum_root);
free(fs_info->free_space_root);
free(fs_info->uuid_root);
free(fs_info->super_copy);
@ -828,7 +834,7 @@ struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
fs_info->extent_root = calloc(1, sizeof(struct btrfs_root));
fs_info->chunk_root = calloc(1, sizeof(struct btrfs_root));
fs_info->dev_root = calloc(1, sizeof(struct btrfs_root));
fs_info->csum_root = calloc(1, sizeof(struct btrfs_root));
fs_info->_csum_root = calloc(1, sizeof(struct btrfs_root));
fs_info->quota_root = calloc(1, sizeof(struct btrfs_root));
fs_info->free_space_root = calloc(1, sizeof(struct btrfs_root));
fs_info->uuid_root = calloc(1, sizeof(struct btrfs_root));
@ -836,7 +842,7 @@ struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
if (!fs_info->tree_root || !fs_info->extent_root ||
!fs_info->chunk_root || !fs_info->dev_root ||
!fs_info->csum_root || !fs_info->quota_root ||
!fs_info->_csum_root || !fs_info->quota_root ||
!fs_info->free_space_root || !fs_info->uuid_root ||
!fs_info->super_copy)
goto free_all;
@ -1008,11 +1014,11 @@ int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
}
fs_info->dev_root->track_dirty = 1;
ret = setup_root_or_create_block(fs_info, flags, fs_info->csum_root,
ret = setup_root_or_create_block(fs_info, flags, fs_info->_csum_root,
BTRFS_CSUM_TREE_OBJECTID, "csum");
if (ret)
return ret;
fs_info->csum_root->track_dirty = 1;
fs_info->_csum_root->track_dirty = 1;
ret = find_and_setup_root(root, fs_info, BTRFS_UUID_TREE_OBJECTID,
fs_info->uuid_root);
@ -1087,8 +1093,8 @@ void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
free_extent_buffer(fs_info->free_space_root->node);
if (fs_info->quota_root)
free_extent_buffer(fs_info->quota_root->node);
if (fs_info->csum_root)
free_extent_buffer(fs_info->csum_root->node);
if (fs_info->_csum_root)
free_extent_buffer(fs_info->_csum_root->node);
if (fs_info->dev_root)
free_extent_buffer(fs_info->dev_root->node);
if (fs_info->extent_root)
@ -1870,11 +1876,11 @@ static void backup_super_roots(struct btrfs_fs_info *info)
btrfs_set_backup_dev_root_level(root_backup,
btrfs_header_level(info->dev_root->node));
btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start);
btrfs_set_backup_csum_root(root_backup, info->_csum_root->node->start);
btrfs_set_backup_csum_root_gen(root_backup,
btrfs_header_generation(info->csum_root->node));
btrfs_header_generation(info->_csum_root->node));
btrfs_set_backup_csum_root_level(root_backup,
btrfs_header_level(info->csum_root->node));
btrfs_header_level(info->_csum_root->node));
btrfs_set_backup_total_bytes(root_backup,
btrfs_super_total_bytes(info->super_copy));

View File

@ -214,5 +214,5 @@ int btrfs_fs_roots_compare_roots(struct rb_node *node1, struct rb_node *node2);
struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
struct btrfs_fs_info *fs_info,
u64 objectid);
struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr);
#endif

View File

@ -184,7 +184,7 @@ fail:
int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
u64 alloc_end, u64 bytenr, char *data, size_t len)
{
struct btrfs_root *root = trans->fs_info->csum_root;
struct btrfs_root *root = btrfs_csum_root(trans->fs_info, bytenr);
int ret = 0;
struct btrfs_key file_key;
struct btrfs_key found_key;
@ -396,8 +396,7 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans, u64 bytenr, u64 len)
int ret;
u16 csum_size = trans->fs_info->csum_size;
int blocksize = trans->fs_info->sectorsize;
struct btrfs_root *csum_root = trans->fs_info->csum_root;
struct btrfs_root *csum_root = btrfs_csum_root(trans->fs_info, bytenr);
path = btrfs_alloc_path();
if (!path)

View File

@ -268,6 +268,7 @@ static int recow_roots(struct btrfs_trans_handle *trans,
struct btrfs_root *root)
{
struct btrfs_fs_info *info = root->fs_info;
struct btrfs_root *csum_root = btrfs_csum_root(info, 0);
int ret;
ret = __recow_root(trans, info->fs_root);
@ -285,7 +286,7 @@ static int recow_roots(struct btrfs_trans_handle *trans,
ret = __recow_root(trans, info->dev_root);
if (ret)
return ret;
ret = __recow_root(trans, info->csum_root);
ret = __recow_root(trans, csum_root);
if (ret)
return ret;
if (info->free_space_root) {