btrfs-progs: move btrfs_uuid_tree_add into mkfs/main.c

This function is only used in mkfs, and doesn't exist in the kernel in
ctree.c.  Additionally we have a uuid lookup function to see if the uuid
exists in the tree, which for mkfs it won't because we just created the
tree.  Move btrfs_uuid_tree_add into mkfs, and remove the lookup
function as it's not needed.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Josef Bacik 2023-08-23 10:32:45 -04:00 committed by David Sterba
parent 0c58bfd5b6
commit 0fa89a9da7
3 changed files with 58 additions and 135 deletions

View File

@ -3194,136 +3194,3 @@ int btrfs_previous_extent_item(struct btrfs_root *root,
}
return 1;
}
/*
* Search uuid tree - unmounted
*
* return -ENOENT for !found, < 0 for errors, or 0 if an item was found
*/
static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, u8 *uuid,
u8 type, u64 subid)
{
int ret;
struct btrfs_path *path = NULL;
struct extent_buffer *eb;
int slot;
u32 item_size;
unsigned long offset;
struct btrfs_key key;
if (!uuid_root) {
ret = -ENOENT;
goto out;
}
path = btrfs_alloc_path();
if (!path) {
ret = -ENOMEM;
goto out;
}
btrfs_uuid_to_key(uuid, &key);
key.type = type;
ret = btrfs_search_slot(NULL, uuid_root, &key, path, 0, 0);
if (ret < 0) {
goto out;
} else if (ret > 0) {
ret = -ENOENT;
goto out;
}
eb = path->nodes[0];
slot = path->slots[0];
item_size = btrfs_item_size(eb, slot);
offset = btrfs_item_ptr_offset(eb, slot);
ret = -ENOENT;
if (!IS_ALIGNED(item_size, sizeof(u64))) {
warning("uuid item with invalid size %lu!",
(unsigned long)item_size);
goto out;
}
while (item_size) {
__le64 data;
read_extent_buffer(eb, &data, offset, sizeof(data));
if (le64_to_cpu(data) == subid) {
ret = 0;
break;
}
offset += sizeof(data);
item_size -= sizeof(data);
}
out:
btrfs_free_path(path);
return ret;
}
int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
u64 subvol_id_cpu)
{
struct btrfs_fs_info *fs_info = trans->fs_info;
struct btrfs_root *uuid_root = fs_info->uuid_root;
int ret;
struct btrfs_path *path = NULL;
struct btrfs_key key;
struct extent_buffer *eb;
int slot;
unsigned long offset;
__le64 subvol_id_le;
if (!uuid_root) {
warning("%s: uuid root is not initialized", __func__);
return -EINVAL;
}
ret = btrfs_uuid_tree_lookup(uuid_root, uuid, type, subvol_id_cpu);
if (ret != -ENOENT)
return ret;
key.type = type;
btrfs_uuid_to_key(uuid, &key);
path = btrfs_alloc_path();
if (!path) {
ret = -ENOMEM;
goto out;
}
ret = btrfs_insert_empty_item(trans, uuid_root, path, &key,
sizeof(subvol_id_le));
if (ret < 0 && ret != -EEXIST) {
warning(
"inserting uuid item failed (0x%016llx, 0x%016llx) type %u: %d",
(unsigned long long)key.objectid,
(unsigned long long)key.offset, type, ret);
goto out;
}
if (ret >= 0) {
/* Add an item for the type for the first time */
eb = path->nodes[0];
slot = path->slots[0];
offset = btrfs_item_ptr_offset(eb, slot);
} else {
/*
* ret == -EEXIST case, An item with that type already exists.
* Extend the item and store the new subvol_id at the end.
*/
btrfs_extend_item(path, sizeof(subvol_id_le));
eb = path->nodes[0];
slot = path->slots[0];
offset = btrfs_item_ptr_offset(eb, slot);
offset += btrfs_item_size(eb, slot) - sizeof(subvol_id_le);
}
ret = 0;
subvol_id_le = cpu_to_le64(subvol_id_cpu);
write_extent_buffer(eb, &subvol_id_le, offset, sizeof(subvol_id_le));
btrfs_mark_buffer_dirty(eb);
out:
btrfs_free_path(path);
return ret;
}

View File

@ -1142,8 +1142,6 @@ int btrfs_lookup_uuid_received_subvol_item(int fd, const u8 *uuid,
u64 *subvol_id);
/* uuid-tree.c, interface for unmounte filesystem */
int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
u64 subvol_id_cpu);
int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
u64 subid);

View File

@ -771,6 +771,64 @@ out:
return ret;
}
static int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid,
u8 type, u64 subvol_id_cpu)
{
struct btrfs_fs_info *fs_info = trans->fs_info;
struct btrfs_root *uuid_root = fs_info->uuid_root;
int ret;
struct btrfs_path *path = NULL;
struct btrfs_key key;
struct extent_buffer *eb;
int slot;
unsigned long offset;
__le64 subvol_id_le;
key.type = type;
btrfs_uuid_to_key(uuid, &key);
path = btrfs_alloc_path();
if (!path) {
ret = -ENOMEM;
goto out;
}
ret = btrfs_insert_empty_item(trans, uuid_root, path, &key, sizeof(subvol_id_le));
if (ret < 0 && ret != -EEXIST) {
warning(
"inserting uuid item failed (0x%016llx, 0x%016llx) type %u: %d",
(unsigned long long)key.objectid,
(unsigned long long)key.offset, type, ret);
goto out;
}
if (ret >= 0) {
/* Add an item for the type for the first time. */
eb = path->nodes[0];
slot = path->slots[0];
offset = btrfs_item_ptr_offset(eb, slot);
} else {
/*
* ret == -EEXIST case, an item with that type already exists.
* Extend the item and store the new subvol_id at the end.
*/
btrfs_extend_item(path, sizeof(subvol_id_le));
eb = path->nodes[0];
slot = path->slots[0];
offset = btrfs_item_ptr_offset(eb, slot);
offset += btrfs_item_size(eb, slot) - sizeof(subvol_id_le);
}
ret = 0;
subvol_id_le = cpu_to_le64(subvol_id_cpu);
write_extent_buffer(eb, &subvol_id_le, offset, sizeof(subvol_id_le));
btrfs_mark_buffer_dirty(eb);
out:
btrfs_free_path(path);
return ret;
}
static int create_uuid_tree(struct btrfs_trans_handle *trans)
{
struct btrfs_fs_info *fs_info = trans->fs_info;