From f6f518a08153e22ca7b17058c4c103897c91c575 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Wed, 23 Jan 2019 12:16:46 +0100 Subject: [PATCH] btrfs-progs: build: fix libbtrfs build Commit 75b5eabb61de ("btrfs-progs: uuid: Port btrfs_uuid_tree_add() function") brings code from kernel and introduces an unsatisfied build dependency for libbtrfs. This fails build for external library users like snapper. As a hotfix, shuffle the new UUID functions so the build works. Simply adding uuid-tree.o to libbtrfs_objects does not work due to other missing symbols. [CC] uuid-tree.o [LD] libbtrfs.so.0.1 [LN] libbtrfs.so [TEST PREP] library-test ld: .../libbtrfs.so: undefined reference to `btrfs_alloc_path' ld: .../libbtrfs.so: undefined reference to `btrfs_extend_item' ld: .../libbtrfs.so: undefined reference to `btrfs_insert_empty_items' ld: .../libbtrfs.so: undefined reference to `btrfs_free_path' ld: .../libbtrfs.so: undefined reference to `read_extent_buffer' ld: .../libbtrfs.so: undefined reference to `btrfs_mark_buffer_dirty' ld: .../libbtrfs.so: undefined reference to `write_extent_buffer' ld: .../libbtrfs.so: undefined reference to `btrfs_search_slot' LinK: https://bugs.gentoo.org/675974 Link: https://github.com/openSUSE/snapper/issues/473 Fixes: 75b5eabb61de ("btrfs-progs: uuid: Port btrfs_uuid_tree_add() function") Signed-off-by: David Sterba --- ctree.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++ ctree.h | 2 + uuid-tree.c | 135 +--------------------------------------------------- 3 files changed, 134 insertions(+), 134 deletions(-) diff --git a/ctree.c b/ctree.c index d3101e5b07..7cb3f84515 100644 --- a/ctree.c +++ b/ctree.c @@ -3107,3 +3107,134 @@ int btrfs_next_extent_item(struct btrfs_root *root, return 0; } } + +/* + * 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.objectid, &key.offset); + 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_nr(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.objectid, &key.offset); + + 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) { + /* 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 if (ret == -EEXIST) { + /* + * An item with that type already exists. + * Extend the item and store the new subvol_id at the end. + */ + btrfs_extend_item(uuid_root, path, sizeof(subvol_id_le)); + eb = path->nodes[0]; + slot = path->slots[0]; + offset = btrfs_item_ptr_offset(eb, slot); + offset += btrfs_item_size_nr(eb, slot) - sizeof(subvol_id_le); + } else if (ret < 0) { + 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; + } + + 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; +} diff --git a/ctree.h b/ctree.h index f644cbdcab..abc20e283f 100644 --- a/ctree.h +++ b/ctree.h @@ -2798,6 +2798,8 @@ static inline int is_fstree(u64 rootid) return 0; } +void btrfs_uuid_to_key(const u8 *uuid, u64 *key_objectid, u64 *key_offset); + /* inode.c */ int check_dir_conflict(struct btrfs_root *root, char *name, int namelen, u64 dir, u64 index); diff --git a/uuid-tree.c b/uuid-tree.c index 43fdeba753..422260fa01 100644 --- a/uuid-tree.c +++ b/uuid-tree.c @@ -25,9 +25,7 @@ #include "print-tree.h" #include "utils.h" - -static void btrfs_uuid_to_key(const u8 *uuid, u64 *key_objectid, - u64 *key_offset) +void btrfs_uuid_to_key(const u8 *uuid, u64 *key_objectid, u64 *key_offset) { *key_objectid = get_unaligned_le64(uuid); *key_offset = get_unaligned_le64(uuid + sizeof(u64)); @@ -107,134 +105,3 @@ int btrfs_lookup_uuid_received_subvol_item(int fd, const u8 *uuid, BTRFS_UUID_KEY_RECEIVED_SUBVOL, subvol_id); } - -/* - * 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.objectid, &key.offset); - 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_nr(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.objectid, &key.offset); - - 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) { - /* 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 if (ret == -EEXIST) { - /* - * An item with that type already exists. - * Extend the item and store the new subvol_id at the end. - */ - btrfs_extend_item(uuid_root, path, sizeof(subvol_id_le)); - eb = path->nodes[0]; - slot = path->slots[0]; - offset = btrfs_item_ptr_offset(eb, slot); - offset += btrfs_item_size_nr(eb, slot) - sizeof(subvol_id_le); - } else if (ret < 0) { - 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; - } - - 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; -}