2 * Copyright (C) STRATO AG 2013. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/uuid.h>
19 #include <asm/unaligned.h>
21 #include "transaction.h"
23 #include "print-tree.h"
26 static void btrfs_uuid_to_key(u8 *uuid, u8 type, struct btrfs_key *key)
29 key->objectid = get_unaligned_le64(uuid);
30 key->offset = get_unaligned_le64(uuid + sizeof(u64));
33 /* return -ENOENT for !found, < 0 for errors, or 0 if an item was found */
34 static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, u8 *uuid,
38 struct btrfs_path *path = NULL;
39 struct extent_buffer *eb;
45 if (WARN_ON_ONCE(!uuid_root)) {
50 path = btrfs_alloc_path();
56 btrfs_uuid_to_key(uuid, type, &key);
57 ret = btrfs_search_slot(NULL, uuid_root, &key, path, 0, 0);
66 slot = path->slots[0];
67 item_size = btrfs_item_size_nr(eb, slot);
68 offset = btrfs_item_ptr_offset(eb, slot);
71 if (!IS_ALIGNED(item_size, sizeof(u64))) {
72 btrfs_warn(uuid_root->fs_info,
73 "uuid item with illegal size %lu!",
74 (unsigned long)item_size);
80 read_extent_buffer(eb, &data, offset, sizeof(data));
81 if (le64_to_cpu(data) == subid) {
85 offset += sizeof(data);
86 item_size -= sizeof(data);
90 btrfs_free_path(path);
94 int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans,
95 struct btrfs_fs_info *fs_info, u8 *uuid, u8 type,
98 struct btrfs_root *uuid_root = fs_info->uuid_root;
100 struct btrfs_path *path = NULL;
101 struct btrfs_key key;
102 struct extent_buffer *eb;
104 unsigned long offset;
107 ret = btrfs_uuid_tree_lookup(uuid_root, uuid, type, subid_cpu);
111 if (WARN_ON_ONCE(!uuid_root)) {
116 btrfs_uuid_to_key(uuid, type, &key);
118 path = btrfs_alloc_path();
124 ret = btrfs_insert_empty_item(trans, uuid_root, path, &key,
127 /* Add an item for the type for the first time */
129 slot = path->slots[0];
130 offset = btrfs_item_ptr_offset(eb, slot);
131 } else if (ret == -EEXIST) {
133 * An item with that type already exists.
134 * Extend the item and store the new subid at the end.
136 btrfs_extend_item(fs_info, path, sizeof(subid_le));
138 slot = path->slots[0];
139 offset = btrfs_item_ptr_offset(eb, slot);
140 offset += btrfs_item_size_nr(eb, slot) - sizeof(subid_le);
141 } else if (ret < 0) {
143 "insert uuid item failed %d (0x%016llx, 0x%016llx) type %u!",
144 ret, (unsigned long long)key.objectid,
145 (unsigned long long)key.offset, type);
150 subid_le = cpu_to_le64(subid_cpu);
151 write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));
152 btrfs_mark_buffer_dirty(eb);
155 btrfs_free_path(path);
159 int btrfs_uuid_tree_rem(struct btrfs_trans_handle *trans,
160 struct btrfs_fs_info *fs_info, u8 *uuid, u8 type,
163 struct btrfs_root *uuid_root = fs_info->uuid_root;
165 struct btrfs_path *path = NULL;
166 struct btrfs_key key;
167 struct extent_buffer *eb;
169 unsigned long offset;
171 unsigned long move_dst;
172 unsigned long move_src;
173 unsigned long move_len;
175 if (WARN_ON_ONCE(!uuid_root)) {
180 btrfs_uuid_to_key(uuid, type, &key);
182 path = btrfs_alloc_path();
188 ret = btrfs_search_slot(trans, uuid_root, &key, path, -1, 1);
190 btrfs_warn(fs_info, "error %d while searching for uuid item!",
200 slot = path->slots[0];
201 offset = btrfs_item_ptr_offset(eb, slot);
202 item_size = btrfs_item_size_nr(eb, slot);
203 if (!IS_ALIGNED(item_size, sizeof(u64))) {
204 btrfs_warn(fs_info, "uuid item with illegal size %lu!",
205 (unsigned long)item_size);
212 read_extent_buffer(eb, &read_subid, offset, sizeof(read_subid));
213 if (le64_to_cpu(read_subid) == subid)
215 offset += sizeof(read_subid);
216 item_size -= sizeof(read_subid);
224 item_size = btrfs_item_size_nr(eb, slot);
225 if (item_size == sizeof(subid)) {
226 ret = btrfs_del_item(trans, uuid_root, path);
231 move_src = offset + sizeof(subid);
232 move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));
233 memmove_extent_buffer(eb, move_dst, move_src, move_len);
234 btrfs_truncate_item(fs_info, path, item_size - sizeof(subid), 1);
237 btrfs_free_path(path);
241 static int btrfs_uuid_iter_rem(struct btrfs_root *uuid_root, u8 *uuid, u8 type,
244 struct btrfs_trans_handle *trans;
247 /* 1 - for the uuid item */
248 trans = btrfs_start_transaction(uuid_root, 1);
250 ret = PTR_ERR(trans);
254 ret = btrfs_uuid_tree_rem(trans, uuid_root->fs_info, uuid, type, subid);
255 btrfs_end_transaction(trans);
261 int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info,
262 int (*check_func)(struct btrfs_fs_info *, u8 *, u8,
265 struct btrfs_root *root = fs_info->uuid_root;
266 struct btrfs_key key;
267 struct btrfs_path *path;
269 struct extent_buffer *leaf;
272 unsigned long offset;
274 path = btrfs_alloc_path();
285 ret = btrfs_search_forward(root, &key, path, 0);
294 leaf = path->nodes[0];
295 slot = path->slots[0];
296 btrfs_item_key_to_cpu(leaf, &key, slot);
298 if (key.type != BTRFS_UUID_KEY_SUBVOL &&
299 key.type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
302 offset = btrfs_item_ptr_offset(leaf, slot);
303 item_size = btrfs_item_size_nr(leaf, slot);
304 if (!IS_ALIGNED(item_size, sizeof(u64))) {
306 "uuid item with illegal size %lu!",
307 (unsigned long)item_size);
311 u8 uuid[BTRFS_UUID_SIZE];
315 put_unaligned_le64(key.objectid, uuid);
316 put_unaligned_le64(key.offset, uuid + sizeof(u64));
317 read_extent_buffer(leaf, &subid_le, offset,
319 subid_cpu = le64_to_cpu(subid_le);
320 ret = check_func(fs_info, uuid, key.type, subid_cpu);
324 btrfs_release_path(path);
325 ret = btrfs_uuid_iter_rem(root, uuid, key.type,
329 * this might look inefficient, but the
330 * justification is that it is an
331 * exception that check_func returns 1,
332 * and that in the regular case only one
333 * entry per UUID exists.
335 goto again_search_slot;
337 if (ret < 0 && ret != -ENOENT)
340 item_size -= sizeof(subid_le);
341 offset += sizeof(subid_le);
345 ret = btrfs_next_item(root, path);
354 btrfs_free_path(path);