1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STRATO AG 2013. All rights reserved.
6 #include <linux/uuid.h>
7 #include <asm/unaligned.h>
9 #include "transaction.h"
11 #include "print-tree.h"
14 static void btrfs_uuid_to_key(u8 *uuid, u8 type, struct btrfs_key *key)
17 key->objectid = get_unaligned_le64(uuid);
18 key->offset = get_unaligned_le64(uuid + sizeof(u64));
21 /* return -ENOENT for !found, < 0 for errors, or 0 if an item was found */
22 static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, u8 *uuid,
26 struct btrfs_path *path = NULL;
27 struct extent_buffer *eb;
33 if (WARN_ON_ONCE(!uuid_root)) {
38 path = btrfs_alloc_path();
44 btrfs_uuid_to_key(uuid, type, &key);
45 ret = btrfs_search_slot(NULL, uuid_root, &key, path, 0, 0);
54 slot = path->slots[0];
55 item_size = btrfs_item_size(eb, slot);
56 offset = btrfs_item_ptr_offset(eb, slot);
59 if (!IS_ALIGNED(item_size, sizeof(u64))) {
60 btrfs_warn(uuid_root->fs_info,
61 "uuid item with illegal size %lu!",
62 (unsigned long)item_size);
68 read_extent_buffer(eb, &data, offset, sizeof(data));
69 if (le64_to_cpu(data) == subid) {
73 offset += sizeof(data);
74 item_size -= sizeof(data);
78 btrfs_free_path(path);
82 int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
85 struct btrfs_fs_info *fs_info = trans->fs_info;
86 struct btrfs_root *uuid_root = fs_info->uuid_root;
88 struct btrfs_path *path = NULL;
90 struct extent_buffer *eb;
95 ret = btrfs_uuid_tree_lookup(uuid_root, uuid, type, subid_cpu);
99 if (WARN_ON_ONCE(!uuid_root)) {
104 btrfs_uuid_to_key(uuid, type, &key);
106 path = btrfs_alloc_path();
112 ret = btrfs_insert_empty_item(trans, uuid_root, path, &key,
115 /* Add an item for the type for the first time */
117 slot = path->slots[0];
118 offset = btrfs_item_ptr_offset(eb, slot);
119 } else if (ret == -EEXIST) {
121 * An item with that type already exists.
122 * Extend the item and store the new subid at the end.
124 btrfs_extend_item(path, sizeof(subid_le));
126 slot = path->slots[0];
127 offset = btrfs_item_ptr_offset(eb, slot);
128 offset += btrfs_item_size(eb, slot) - sizeof(subid_le);
131 "insert uuid item failed %d (0x%016llx, 0x%016llx) type %u!",
132 ret, key.objectid, key.offset, type);
137 subid_le = cpu_to_le64(subid_cpu);
138 write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));
139 btrfs_mark_buffer_dirty(eb);
142 btrfs_free_path(path);
146 int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
149 struct btrfs_fs_info *fs_info = trans->fs_info;
150 struct btrfs_root *uuid_root = fs_info->uuid_root;
152 struct btrfs_path *path = NULL;
153 struct btrfs_key key;
154 struct extent_buffer *eb;
156 unsigned long offset;
158 unsigned long move_dst;
159 unsigned long move_src;
160 unsigned long move_len;
162 if (WARN_ON_ONCE(!uuid_root)) {
167 btrfs_uuid_to_key(uuid, type, &key);
169 path = btrfs_alloc_path();
175 ret = btrfs_search_slot(trans, uuid_root, &key, path, -1, 1);
177 btrfs_warn(fs_info, "error %d while searching for uuid item!",
187 slot = path->slots[0];
188 offset = btrfs_item_ptr_offset(eb, slot);
189 item_size = btrfs_item_size(eb, slot);
190 if (!IS_ALIGNED(item_size, sizeof(u64))) {
191 btrfs_warn(fs_info, "uuid item with illegal size %lu!",
192 (unsigned long)item_size);
199 read_extent_buffer(eb, &read_subid, offset, sizeof(read_subid));
200 if (le64_to_cpu(read_subid) == subid)
202 offset += sizeof(read_subid);
203 item_size -= sizeof(read_subid);
211 item_size = btrfs_item_size(eb, slot);
212 if (item_size == sizeof(subid)) {
213 ret = btrfs_del_item(trans, uuid_root, path);
218 move_src = offset + sizeof(subid);
219 move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));
220 memmove_extent_buffer(eb, move_dst, move_src, move_len);
221 btrfs_truncate_item(path, item_size - sizeof(subid), 1);
224 btrfs_free_path(path);
228 static int btrfs_uuid_iter_rem(struct btrfs_root *uuid_root, u8 *uuid, u8 type,
231 struct btrfs_trans_handle *trans;
234 /* 1 - for the uuid item */
235 trans = btrfs_start_transaction(uuid_root, 1);
237 ret = PTR_ERR(trans);
241 ret = btrfs_uuid_tree_remove(trans, uuid, type, subid);
242 btrfs_end_transaction(trans);
249 * Check if there's an matching subvolume for given UUID
252 * 0 check succeeded, the entry is not outdated
253 * > 0 if the check failed, the caller should remove the entry
254 * < 0 if an error occurred
256 static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
257 u8 *uuid, u8 type, u64 subvolid)
260 struct btrfs_root *subvol_root;
262 if (type != BTRFS_UUID_KEY_SUBVOL &&
263 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
266 subvol_root = btrfs_get_fs_root(fs_info, subvolid, true);
267 if (IS_ERR(subvol_root)) {
268 ret = PTR_ERR(subvol_root);
275 case BTRFS_UUID_KEY_SUBVOL:
276 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
279 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
280 if (memcmp(uuid, subvol_root->root_item.received_uuid,
285 btrfs_put_root(subvol_root);
290 int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
292 struct btrfs_root *root = fs_info->uuid_root;
293 struct btrfs_key key;
294 struct btrfs_path *path;
296 struct extent_buffer *leaf;
299 unsigned long offset;
301 path = btrfs_alloc_path();
312 ret = btrfs_search_forward(root, &key, path, BTRFS_OLDEST_GENERATION);
320 if (btrfs_fs_closing(fs_info)) {
325 leaf = path->nodes[0];
326 slot = path->slots[0];
327 btrfs_item_key_to_cpu(leaf, &key, slot);
329 if (key.type != BTRFS_UUID_KEY_SUBVOL &&
330 key.type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
333 offset = btrfs_item_ptr_offset(leaf, slot);
334 item_size = btrfs_item_size(leaf, slot);
335 if (!IS_ALIGNED(item_size, sizeof(u64))) {
337 "uuid item with illegal size %lu!",
338 (unsigned long)item_size);
342 u8 uuid[BTRFS_UUID_SIZE];
346 put_unaligned_le64(key.objectid, uuid);
347 put_unaligned_le64(key.offset, uuid + sizeof(u64));
348 read_extent_buffer(leaf, &subid_le, offset,
350 subid_cpu = le64_to_cpu(subid_le);
351 ret = btrfs_check_uuid_tree_entry(fs_info, uuid,
352 key.type, subid_cpu);
356 btrfs_release_path(path);
357 ret = btrfs_uuid_iter_rem(root, uuid, key.type,
361 * this might look inefficient, but the
362 * justification is that it is an
363 * exception that check_func returns 1,
364 * and that in the regular case only one
365 * entry per UUID exists.
367 goto again_search_slot;
369 if (ret < 0 && ret != -ENOENT)
372 goto again_search_slot;
374 item_size -= sizeof(subid_le);
375 offset += sizeof(subid_le);
379 ret = btrfs_next_item(root, path);
388 btrfs_free_path(path);