From 02fb308bdc2f2ab4f0f5389b854fa54f41cbe962 Mon Sep 17 00:00:00 2001 From: Josef Bacik Date: Mon, 7 Mar 2022 17:11:01 -0500 Subject: [PATCH] btrfs-progs: make btrfs_create_tree take a key for the root key We're going to start create global roots from mkfs, and we need to have a offset set for the root key. Make the btrfs_create_tree() take a key for the root_key instead of just the objectid so we can setup these new style roots properly. Signed-off-by: Josef Bacik Signed-off-by: David Sterba --- kernel-shared/disk-io.c | 21 ++++++++------------- kernel-shared/disk-io.h | 2 +- kernel-shared/free-space-tree.c | 7 +++++-- mkfs/main.c | 13 ++++++++++--- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/kernel-shared/disk-io.c b/kernel-shared/disk-io.c index f3ddf9e3..4964cd38 100644 --- a/kernel-shared/disk-io.c +++ b/kernel-shared/disk-io.c @@ -2426,25 +2426,22 @@ int btrfs_delete_and_free_root(struct btrfs_trans_handle *trans, struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, struct btrfs_fs_info *fs_info, - u64 objectid) + struct btrfs_key *key) { struct extent_buffer *leaf; struct btrfs_root *tree_root = fs_info->tree_root; struct btrfs_root *root; - struct btrfs_key key; int ret = 0; root = kzalloc(sizeof(*root), GFP_KERNEL); if (!root) return ERR_PTR(-ENOMEM); - btrfs_setup_root(root, fs_info, objectid); - root->root_key.objectid = objectid; - root->root_key.type = BTRFS_ROOT_ITEM_KEY; - root->root_key.offset = 0; + btrfs_setup_root(root, fs_info, key->objectid); + memcpy(&root->root_key, key, sizeof(struct btrfs_key)); - leaf = btrfs_alloc_free_block(trans, root, fs_info->nodesize, objectid, - NULL, 0, 0, 0); + leaf = btrfs_alloc_free_block(trans, root, fs_info->nodesize, + root->root_key.objectid, NULL, 0, 0, 0); if (IS_ERR(leaf)) { ret = PTR_ERR(leaf); leaf = NULL; @@ -2455,7 +2452,7 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, btrfs_set_header_bytenr(leaf, leaf->start); btrfs_set_header_generation(leaf, trans->transid); btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); - btrfs_set_header_owner(leaf, objectid); + btrfs_set_header_owner(leaf, root->root_key.objectid); root->node = leaf; write_extent_buffer(leaf, fs_info->fs_devices->metadata_uuid, btrfs_header_fsid(), BTRFS_FSID_SIZE); @@ -2480,10 +2477,8 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, memset(root->root_item.uuid, 0, BTRFS_UUID_SIZE); root->root_item.drop_level = 0; - key.objectid = objectid; - key.type = BTRFS_ROOT_ITEM_KEY; - key.offset = 0; - ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item); + ret = btrfs_insert_root(trans, tree_root, &root->root_key, + &root->root_item); if (ret) goto fail; diff --git a/kernel-shared/disk-io.h b/kernel-shared/disk-io.h index 1e97b9ac..e07141a9 100644 --- a/kernel-shared/disk-io.h +++ b/kernel-shared/disk-io.h @@ -220,7 +220,7 @@ int write_and_map_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb); 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_key *key); int btrfs_delete_and_free_root(struct btrfs_trans_handle *trans, struct btrfs_root *root); struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr); diff --git a/kernel-shared/free-space-tree.c b/kernel-shared/free-space-tree.c index 7ac75c20..03eb0ed2 100644 --- a/kernel-shared/free-space-tree.c +++ b/kernel-shared/free-space-tree.c @@ -1475,14 +1475,17 @@ int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info) struct btrfs_root *free_space_root; struct btrfs_block_group *block_group; u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE; + struct btrfs_key root_key = { + .objectid = BTRFS_FREE_SPACE_TREE_OBJECTID, + .type = BTRFS_ROOT_ITEM_KEY, + }; int ret; trans = btrfs_start_transaction(tree_root, 0); if (IS_ERR(trans)) return PTR_ERR(trans); - free_space_root = btrfs_create_tree(trans, fs_info, - BTRFS_FREE_SPACE_TREE_OBJECTID); + free_space_root = btrfs_create_tree(trans, fs_info, &root_key); if (IS_ERR(free_space_root)) { ret = PTR_ERR(free_space_root); goto abort; diff --git a/mkfs/main.c b/mkfs/main.c index 7f79ba1a..19535604 100644 --- a/mkfs/main.c +++ b/mkfs/main.c @@ -717,12 +717,15 @@ static int create_data_reloc_tree(struct btrfs_trans_handle *trans) struct btrfs_inode_item *inode; struct btrfs_root *root; struct btrfs_path path; - struct btrfs_key key; + struct btrfs_key key = { + .objectid = BTRFS_DATA_RELOC_TREE_OBJECTID, + .type = BTRFS_ROOT_ITEM_KEY, + }; u64 ino = BTRFS_FIRST_FREE_OBJECTID; char *name = ".."; int ret; - root = btrfs_create_tree(trans, fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID); + root = btrfs_create_tree(trans, fs_info, &key); if (IS_ERR(root)) { ret = PTR_ERR(root); goto out; @@ -782,10 +785,14 @@ static int create_uuid_tree(struct btrfs_trans_handle *trans) { struct btrfs_fs_info *fs_info = trans->fs_info; struct btrfs_root *root; + struct btrfs_key key = { + .objectid = BTRFS_UUID_TREE_OBJECTID, + .type = BTRFS_ROOT_ITEM_KEY, + }; int ret = 0; ASSERT(fs_info->uuid_root == NULL); - root = btrfs_create_tree(trans, fs_info, BTRFS_UUID_TREE_OBJECTID); + root = btrfs_create_tree(trans, fs_info, &key); if (IS_ERR(root)) { ret = PTR_ERR(root); goto out;