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_root *uuid_root, u8 *uuid, u8 type,
99 struct btrfs_path *path = NULL;
100 struct btrfs_key key;
101 struct extent_buffer *eb;
103 unsigned long offset;
106 ret = btrfs_uuid_tree_lookup(uuid_root, uuid, type, subid_cpu);
110 if (WARN_ON_ONCE(!uuid_root)) {
115 btrfs_uuid_to_key(uuid, type, &key);
117 path = btrfs_alloc_path();
123 ret = btrfs_insert_empty_item(trans, uuid_root, path, &key,
126 /* Add an item for the type for the first time */
128 slot = path->slots[0];
129 offset = btrfs_item_ptr_offset(eb, slot);
130 } else if (ret == -EEXIST) {
132 * An item with that type already exists.
133 * Extend the item and store the new subid at the end.
135 btrfs_extend_item(uuid_root, path, sizeof(subid_le));
137 slot = path->slots[0];
138 offset = btrfs_item_ptr_offset(eb, slot);
139 offset += btrfs_item_size_nr(eb, slot) - sizeof(subid_le);
140 } else if (ret < 0) {
141 btrfs_warn(uuid_root->fs_info,
142 "insert uuid item failed %d (0x%016llx, 0x%016llx) type %u!",
143 ret, (unsigned long long)key.objectid,
144 (unsigned long long)key.offset, type);
149 subid_le = cpu_to_le64(subid_cpu);
150 write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));
151 btrfs_mark_buffer_dirty(eb);
154 btrfs_free_path(path);
158 int btrfs_uuid_tree_rem(struct btrfs_trans_handle *trans,
159 struct btrfs_root *uuid_root, u8 *uuid, u8 type,
163 struct btrfs_path *path = NULL;
164 struct btrfs_key key;
165 struct extent_buffer *eb;
167 unsigned long offset;
169 unsigned long move_dst;
170 unsigned long move_src;
171 unsigned long move_len;
173 if (WARN_ON_ONCE(!uuid_root)) {
178 btrfs_uuid_to_key(uuid, type, &key);
180 path = btrfs_alloc_path();
186 ret = btrfs_search_slot(trans, uuid_root, &key, path, -1, 1);
188 btrfs_warn(uuid_root->fs_info,
189 "error %d while searching for uuid item!", ret);
198 slot = path->slots[0];
199 offset = btrfs_item_ptr_offset(eb, slot);
200 item_size = btrfs_item_size_nr(eb, slot);
201 if (!IS_ALIGNED(item_size, sizeof(u64))) {
202 btrfs_warn(uuid_root->fs_info,
203 "uuid item with illegal size %lu!",
204 (unsigned long)item_size);
211 read_extent_buffer(eb, &read_subid, offset, sizeof(read_subid));
212 if (le64_to_cpu(read_subid) == subid)
214 offset += sizeof(read_subid);
215 item_size -= sizeof(read_subid);
223 item_size = btrfs_item_size_nr(eb, slot);
224 if (item_size == sizeof(subid)) {
225 ret = btrfs_del_item(trans, uuid_root, path);
230 move_src = offset + sizeof(subid);
231 move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));
232 memmove_extent_buffer(eb, move_dst, move_src, move_len);
233 btrfs_truncate_item(uuid_root, path, item_size - sizeof(subid), 1);
236 btrfs_free_path(path);
240 static int btrfs_uuid_iter_rem(struct btrfs_root *uuid_root, u8 *uuid, u8 type,
243 struct btrfs_trans_handle *trans;
246 /* 1 - for the uuid item */
247 trans = btrfs_start_transaction(uuid_root, 1);
249 ret = PTR_ERR(trans);
253 ret = btrfs_uuid_tree_rem(trans, uuid_root, uuid, type, subid);
254 btrfs_end_transaction(trans, uuid_root);
260 int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info,
261 int (*check_func)(struct btrfs_fs_info *, u8 *, u8,
264 struct btrfs_root *root = fs_info->uuid_root;
265 struct btrfs_key key;
266 struct btrfs_path *path;
268 struct extent_buffer *leaf;
271 unsigned long offset;
273 path = btrfs_alloc_path();
284 ret = btrfs_search_forward(root, &key, path, 0);
293 leaf = path->nodes[0];
294 slot = path->slots[0];
295 btrfs_item_key_to_cpu(leaf, &key, slot);
297 if (key.type != BTRFS_UUID_KEY_SUBVOL &&
298 key.type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
301 offset = btrfs_item_ptr_offset(leaf, slot);
302 item_size = btrfs_item_size_nr(leaf, slot);
303 if (!IS_ALIGNED(item_size, sizeof(u64))) {
305 "uuid item with illegal size %lu!",
306 (unsigned long)item_size);
310 u8 uuid[BTRFS_UUID_SIZE];
314 put_unaligned_le64(key.objectid, uuid);
315 put_unaligned_le64(key.offset, uuid + sizeof(u64));
316 read_extent_buffer(leaf, &subid_le, offset,
318 subid_cpu = le64_to_cpu(subid_le);
319 ret = check_func(fs_info, uuid, key.type, subid_cpu);
323 btrfs_release_path(path);
324 ret = btrfs_uuid_iter_rem(root, uuid, key.type,
328 * this might look inefficient, but the
329 * justification is that it is an
330 * exception that check_func returns 1,
331 * and that in the regular case only one
332 * entry per UUID exists.
334 goto again_search_slot;
336 if (ret < 0 && ret != -ENOENT)
339 goto again_search_slot;
341 item_size -= sizeof(subid_le);
342 offset += sizeof(subid_le);
346 ret = btrfs_next_item(root, path);
355 btrfs_free_path(path);