btrfs-progs: handle the per-block group global root id

We will now be using block_group->chunk_objectid to point at the global
root id for this particular block group.  For now we'll assign this
based on mod'ing the offset of the block group against the number of
global root id's and handle the block_group_item updating appropriately.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Josef Bacik 2022-03-07 17:10:58 -05:00 committed by David Sterba
parent 27eaa3b514
commit e33738306c
5 changed files with 50 additions and 4 deletions

View file

@ -1190,6 +1190,8 @@ struct btrfs_block_group {
*/ */
u64 alloc_offset; u64 alloc_offset;
u64 write_offset; u64 write_offset;
u64 global_root_id;
}; };
struct btrfs_device; struct btrfs_device;

View file

@ -806,13 +806,33 @@ struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info,
return NULL; return NULL;
} }
u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr)
{
struct btrfs_block_group *block_group;
u64 ret = 0;
if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
return ret;
/*
* We use this because we won't have this many global roots, and -1 is
* special, so we need something that'll not be found if we have any
* errors from here on.
*/
ret = BTRFS_LAST_FREE_OBJECTID;
block_group = btrfs_lookup_first_block_group(fs_info, bytenr);
if (block_group)
ret = block_group->global_root_id;
return ret;
}
struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info,
u64 bytenr) u64 bytenr)
{ {
struct btrfs_key key = { struct btrfs_key key = {
.objectid = BTRFS_CSUM_TREE_OBJECTID, .objectid = BTRFS_CSUM_TREE_OBJECTID,
.type = BTRFS_ROOT_ITEM_KEY, .type = BTRFS_ROOT_ITEM_KEY,
.offset = 0, .offset = btrfs_global_root_id(fs_info, bytenr),
}; };
return btrfs_global_root(fs_info, &key); return btrfs_global_root(fs_info, &key);
@ -824,7 +844,7 @@ struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info,
struct btrfs_key key = { struct btrfs_key key = {
.objectid = BTRFS_EXTENT_TREE_OBJECTID, .objectid = BTRFS_EXTENT_TREE_OBJECTID,
.type = BTRFS_ROOT_ITEM_KEY, .type = BTRFS_ROOT_ITEM_KEY,
.offset = 0, .offset = btrfs_global_root_id(fs_info, bytenr),
}; };
return btrfs_global_root(fs_info, &key); return btrfs_global_root(fs_info, &key);

View file

@ -225,6 +225,7 @@ struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr);
struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_inf, u64 bytenr); struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_inf, u64 bytenr);
struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info, struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info,
struct btrfs_key *key); struct btrfs_key *key);
u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr);
int btrfs_global_root_insert(struct btrfs_fs_info *fs_info, int btrfs_global_root_insert(struct btrfs_fs_info *fs_info,
struct btrfs_root *root); struct btrfs_root *root);

View file

@ -1561,7 +1561,7 @@ static int update_block_group_item(struct btrfs_trans_handle *trans,
btrfs_set_stack_block_group_used(&bgi, cache->used); btrfs_set_stack_block_group_used(&bgi, cache->used);
btrfs_set_stack_block_group_flags(&bgi, cache->flags); btrfs_set_stack_block_group_flags(&bgi, cache->flags);
btrfs_set_stack_block_group_chunk_objectid(&bgi, btrfs_set_stack_block_group_chunk_objectid(&bgi,
BTRFS_FIRST_CHUNK_TREE_OBJECTID); cache->global_root_id);
write_extent_buffer(leaf, &bgi, bi, sizeof(bgi)); write_extent_buffer(leaf, &bgi, bi, sizeof(bgi));
btrfs_mark_buffer_dirty(leaf); btrfs_mark_buffer_dirty(leaf);
fail: fail:
@ -2658,6 +2658,7 @@ static int read_block_group_item(struct btrfs_block_group *cache,
sizeof(bgi)); sizeof(bgi));
cache->used = btrfs_stack_block_group_used(&bgi); cache->used = btrfs_stack_block_group_used(&bgi);
cache->flags = btrfs_stack_block_group_flags(&bgi); cache->flags = btrfs_stack_block_group_flags(&bgi);
cache->global_root_id = btrfs_stack_block_group_chunk_objectid(&bgi);
return 0; return 0;
} }
@ -2765,6 +2766,24 @@ error:
return ret; return ret;
} }
/*
* For extent tree v2 we use the block_group_item->chunk_offset to point at our
* global root id. For v1 it's always set to BTRFS_FIRST_CHUNK_TREE_OBJECTID.
*/
static u64 calculate_global_root_id(struct btrfs_fs_info *fs_info, u64 offset)
{
u64 div = SZ_1G;
if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
return BTRFS_FIRST_CHUNK_TREE_OBJECTID;
/* If we have a smaller fs index based on 128m. */
if (btrfs_super_total_bytes(fs_info->super_copy) <= (SZ_1G * 10ULL))
div = SZ_128M;
return (div_u64(offset, div) % fs_info->nr_global_roots);
}
struct btrfs_block_group * struct btrfs_block_group *
btrfs_add_block_group(struct btrfs_fs_info *fs_info, u64 bytes_used, u64 type, btrfs_add_block_group(struct btrfs_fs_info *fs_info, u64 bytes_used, u64 type,
u64 chunk_offset, u64 size) u64 chunk_offset, u64 size)
@ -2776,6 +2795,7 @@ btrfs_add_block_group(struct btrfs_fs_info *fs_info, u64 bytes_used, u64 type,
BUG_ON(!cache); BUG_ON(!cache);
cache->start = chunk_offset; cache->start = chunk_offset;
cache->length = size; cache->length = size;
cache->global_root_id = calculate_global_root_id(fs_info, chunk_offset);
ret = btrfs_load_block_group_zone_info(fs_info, cache); ret = btrfs_load_block_group_zone_info(fs_info, cache);
BUG_ON(ret); BUG_ON(ret);
@ -2806,7 +2826,7 @@ static int insert_block_group_item(struct btrfs_trans_handle *trans,
btrfs_set_stack_block_group_used(&bgi, block_group->used); btrfs_set_stack_block_group_used(&bgi, block_group->used);
btrfs_set_stack_block_group_chunk_objectid(&bgi, btrfs_set_stack_block_group_chunk_objectid(&bgi,
BTRFS_FIRST_CHUNK_TREE_OBJECTID); block_group->global_root_id);
btrfs_set_stack_block_group_flags(&bgi, block_group->flags); btrfs_set_stack_block_group_flags(&bgi, block_group->flags);
key.objectid = block_group->start; key.objectid = block_group->start;
key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;

View file

@ -34,6 +34,9 @@ static struct btrfs_root *btrfs_free_space_root(struct btrfs_fs_info *fs_info,
.offset = 0, .offset = 0,
}; };
if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
key.offset = block_group->global_root_id;
return btrfs_global_root(fs_info, &key); return btrfs_global_root(fs_info, &key);
} }