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
, "uuid item with illegal size %lu!",
73 (unsigned long)item_size
);
79 read_extent_buffer(eb
, &data
, offset
, sizeof(data
));
80 if (le64_to_cpu(data
) == subid
) {
84 offset
+= sizeof(data
);
85 item_size
-= sizeof(data
);
89 btrfs_free_path(path
);
93 int btrfs_uuid_tree_add(struct btrfs_trans_handle
*trans
,
94 struct btrfs_root
*uuid_root
, u8
*uuid
, u8 type
,
98 struct btrfs_path
*path
= NULL
;
100 struct extent_buffer
*eb
;
102 unsigned long offset
;
105 ret
= btrfs_uuid_tree_lookup(uuid_root
, uuid
, type
, subid_cpu
);
109 if (WARN_ON_ONCE(!uuid_root
)) {
114 btrfs_uuid_to_key(uuid
, type
, &key
);
116 path
= btrfs_alloc_path();
122 ret
= btrfs_insert_empty_item(trans
, uuid_root
, path
, &key
,
125 /* Add an item for the type for the first time */
127 slot
= path
->slots
[0];
128 offset
= btrfs_item_ptr_offset(eb
, slot
);
129 } else if (ret
== -EEXIST
) {
131 * An item with that type already exists.
132 * Extend the item and store the new subid at the end.
134 btrfs_extend_item(uuid_root
, path
, sizeof(subid_le
));
136 slot
= path
->slots
[0];
137 offset
= btrfs_item_ptr_offset(eb
, slot
);
138 offset
+= btrfs_item_size_nr(eb
, slot
) - sizeof(subid_le
);
139 } else if (ret
< 0) {
140 btrfs_warn(uuid_root
->fs_info
, "insert uuid item failed %d "
141 "(0x%016llx, 0x%016llx) type %u!",
142 ret
, (unsigned long long)key
.objectid
,
143 (unsigned long long)key
.offset
, type
);
148 subid_le
= cpu_to_le64(subid_cpu
);
149 write_extent_buffer(eb
, &subid_le
, offset
, sizeof(subid_le
));
150 btrfs_mark_buffer_dirty(eb
);
153 btrfs_free_path(path
);
157 int btrfs_uuid_tree_rem(struct btrfs_trans_handle
*trans
,
158 struct btrfs_root
*uuid_root
, u8
*uuid
, u8 type
,
162 struct btrfs_path
*path
= NULL
;
163 struct btrfs_key key
;
164 struct extent_buffer
*eb
;
166 unsigned long offset
;
168 unsigned long move_dst
;
169 unsigned long move_src
;
170 unsigned long move_len
;
172 if (WARN_ON_ONCE(!uuid_root
)) {
177 btrfs_uuid_to_key(uuid
, type
, &key
);
179 path
= btrfs_alloc_path();
185 ret
= btrfs_search_slot(trans
, uuid_root
, &key
, path
, -1, 1);
187 btrfs_warn(uuid_root
->fs_info
, "error %d while searching for uuid item!",
197 slot
= path
->slots
[0];
198 offset
= btrfs_item_ptr_offset(eb
, slot
);
199 item_size
= btrfs_item_size_nr(eb
, slot
);
200 if (!IS_ALIGNED(item_size
, sizeof(u64
))) {
201 btrfs_warn(uuid_root
->fs_info
, "uuid item with illegal size %lu!",
202 (unsigned long)item_size
);
209 read_extent_buffer(eb
, &read_subid
, offset
, sizeof(read_subid
));
210 if (le64_to_cpu(read_subid
) == subid
)
212 offset
+= sizeof(read_subid
);
213 item_size
-= sizeof(read_subid
);
221 item_size
= btrfs_item_size_nr(eb
, slot
);
222 if (item_size
== sizeof(subid
)) {
223 ret
= btrfs_del_item(trans
, uuid_root
, path
);
228 move_src
= offset
+ sizeof(subid
);
229 move_len
= item_size
- (move_src
- btrfs_item_ptr_offset(eb
, slot
));
230 memmove_extent_buffer(eb
, move_dst
, move_src
, move_len
);
231 btrfs_truncate_item(uuid_root
, path
, item_size
- sizeof(subid
), 1);
234 btrfs_free_path(path
);
238 static int btrfs_uuid_iter_rem(struct btrfs_root
*uuid_root
, u8
*uuid
, u8 type
,
241 struct btrfs_trans_handle
*trans
;
244 /* 1 - for the uuid item */
245 trans
= btrfs_start_transaction(uuid_root
, 1);
247 ret
= PTR_ERR(trans
);
251 ret
= btrfs_uuid_tree_rem(trans
, uuid_root
, uuid
, type
, subid
);
252 btrfs_end_transaction(trans
, uuid_root
);
258 int btrfs_uuid_tree_iterate(struct btrfs_fs_info
*fs_info
,
259 int (*check_func
)(struct btrfs_fs_info
*, u8
*, u8
,
262 struct btrfs_root
*root
= fs_info
->uuid_root
;
263 struct btrfs_key key
;
264 struct btrfs_path
*path
;
266 struct extent_buffer
*leaf
;
269 unsigned long offset
;
271 path
= btrfs_alloc_path();
282 ret
= btrfs_search_forward(root
, &key
, path
, 0);
291 leaf
= path
->nodes
[0];
292 slot
= path
->slots
[0];
293 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
295 if (key
.type
!= BTRFS_UUID_KEY_SUBVOL
&&
296 key
.type
!= BTRFS_UUID_KEY_RECEIVED_SUBVOL
)
299 offset
= btrfs_item_ptr_offset(leaf
, slot
);
300 item_size
= btrfs_item_size_nr(leaf
, slot
);
301 if (!IS_ALIGNED(item_size
, sizeof(u64
))) {
302 btrfs_warn(fs_info
, "uuid item with illegal size %lu!",
303 (unsigned long)item_size
);
307 u8 uuid
[BTRFS_UUID_SIZE
];
311 put_unaligned_le64(key
.objectid
, uuid
);
312 put_unaligned_le64(key
.offset
, uuid
+ sizeof(u64
));
313 read_extent_buffer(leaf
, &subid_le
, offset
,
315 subid_cpu
= le64_to_cpu(subid_le
);
316 ret
= check_func(fs_info
, uuid
, key
.type
, subid_cpu
);
320 btrfs_release_path(path
);
321 ret
= btrfs_uuid_iter_rem(root
, uuid
, key
.type
,
325 * this might look inefficient, but the
326 * justification is that it is an
327 * exception that check_func returns 1,
328 * and that in the regular case only one
329 * entry per UUID exists.
331 goto again_search_slot
;
333 if (ret
< 0 && ret
!= -ENOENT
)
336 item_size
-= sizeof(subid_le
);
337 offset
+= sizeof(subid_le
);
341 ret
= btrfs_next_item(root
, path
);
350 btrfs_free_path(path
);
352 btrfs_warn(fs_info
, "btrfs_uuid_tree_iterate failed %d", ret
);