Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) STRATO AG 2013.  All rights reserved.
  4 */
  5
  6#include <linux/uuid.h>
  7#include <asm/unaligned.h>
  8#include "messages.h"
  9#include "ctree.h"
 10#include "transaction.h"
 11#include "disk-io.h"
 12#include "print-tree.h"
 13#include "fs.h"
 14#include "accessors.h"
 15#include "uuid-tree.h"
 16
 17static void btrfs_uuid_to_key(u8 *uuid, u8 type, struct btrfs_key *key)
 18{
 19	key->type = type;
 20	key->objectid = get_unaligned_le64(uuid);
 21	key->offset = get_unaligned_le64(uuid + sizeof(u64));
 22}
 23
 24/* return -ENOENT for !found, < 0 for errors, or 0 if an item was found */
 25static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, u8 *uuid,
 26				  u8 type, u64 subid)
 27{
 28	int ret;
 29	struct btrfs_path *path = NULL;
 30	struct extent_buffer *eb;
 31	int slot;
 32	u32 item_size;
 33	unsigned long offset;
 34	struct btrfs_key key;
 35
 36	if (WARN_ON_ONCE(!uuid_root)) {
 37		ret = -ENOENT;
 38		goto out;
 39	}
 40
 41	path = btrfs_alloc_path();
 42	if (!path) {
 43		ret = -ENOMEM;
 44		goto out;
 45	}
 46
 47	btrfs_uuid_to_key(uuid, type, &key);
 48	ret = btrfs_search_slot(NULL, uuid_root, &key, path, 0, 0);
 49	if (ret < 0) {
 50		goto out;
 51	} else if (ret > 0) {
 52		ret = -ENOENT;
 53		goto out;
 54	}
 55
 56	eb = path->nodes[0];
 57	slot = path->slots[0];
 58	item_size = btrfs_item_size(eb, slot);
 59	offset = btrfs_item_ptr_offset(eb, slot);
 60	ret = -ENOENT;
 61
 62	if (!IS_ALIGNED(item_size, sizeof(u64))) {
 63		btrfs_warn(uuid_root->fs_info,
 64			   "uuid item with illegal size %lu!",
 65			   (unsigned long)item_size);
 66		goto out;
 67	}
 68	while (item_size) {
 69		__le64 data;
 70
 71		read_extent_buffer(eb, &data, offset, sizeof(data));
 72		if (le64_to_cpu(data) == subid) {
 73			ret = 0;
 74			break;
 75		}
 76		offset += sizeof(data);
 77		item_size -= sizeof(data);
 78	}
 79
 80out:
 81	btrfs_free_path(path);
 82	return ret;
 83}
 84
 85int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
 86			u64 subid_cpu)
 87{
 88	struct btrfs_fs_info *fs_info = trans->fs_info;
 89	struct btrfs_root *uuid_root = fs_info->uuid_root;
 90	int ret;
 91	struct btrfs_path *path = NULL;
 92	struct btrfs_key key;
 93	struct extent_buffer *eb;
 94	int slot;
 95	unsigned long offset;
 96	__le64 subid_le;
 97
 98	ret = btrfs_uuid_tree_lookup(uuid_root, uuid, type, subid_cpu);
 99	if (ret != -ENOENT)
100		return ret;
101
102	if (WARN_ON_ONCE(!uuid_root)) {
103		ret = -EINVAL;
104		goto out;
105	}
106
107	btrfs_uuid_to_key(uuid, type, &key);
108
109	path = btrfs_alloc_path();
110	if (!path) {
111		ret = -ENOMEM;
112		goto out;
113	}
114
115	ret = btrfs_insert_empty_item(trans, uuid_root, path, &key,
116				      sizeof(subid_le));
117	if (ret >= 0) {
118		/* Add an item for the type for the first time */
119		eb = path->nodes[0];
120		slot = path->slots[0];
121		offset = btrfs_item_ptr_offset(eb, slot);
122	} else if (ret == -EEXIST) {
123		/*
124		 * An item with that type already exists.
125		 * Extend the item and store the new subid at the end.
126		 */
127		btrfs_extend_item(path, sizeof(subid_le));
128		eb = path->nodes[0];
129		slot = path->slots[0];
130		offset = btrfs_item_ptr_offset(eb, slot);
131		offset += btrfs_item_size(eb, slot) - sizeof(subid_le);
132	} else {
133		btrfs_warn(fs_info,
134			   "insert uuid item failed %d (0x%016llx, 0x%016llx) type %u!",
135			   ret, key.objectid, key.offset, type);
136		goto out;
137	}
138
139	ret = 0;
140	subid_le = cpu_to_le64(subid_cpu);
141	write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));
142	btrfs_mark_buffer_dirty(eb);
143
144out:
145	btrfs_free_path(path);
146	return ret;
147}
148
149int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
150			u64 subid)
151{
152	struct btrfs_fs_info *fs_info = trans->fs_info;
153	struct btrfs_root *uuid_root = fs_info->uuid_root;
154	int ret;
155	struct btrfs_path *path = NULL;
156	struct btrfs_key key;
157	struct extent_buffer *eb;
158	int slot;
159	unsigned long offset;
160	u32 item_size;
161	unsigned long move_dst;
162	unsigned long move_src;
163	unsigned long move_len;
164
165	if (WARN_ON_ONCE(!uuid_root)) {
166		ret = -EINVAL;
167		goto out;
168	}
169
170	btrfs_uuid_to_key(uuid, type, &key);
171
172	path = btrfs_alloc_path();
173	if (!path) {
174		ret = -ENOMEM;
175		goto out;
176	}
177
178	ret = btrfs_search_slot(trans, uuid_root, &key, path, -1, 1);
179	if (ret < 0) {
180		btrfs_warn(fs_info, "error %d while searching for uuid item!",
181			   ret);
182		goto out;
183	}
184	if (ret > 0) {
185		ret = -ENOENT;
186		goto out;
187	}
188
189	eb = path->nodes[0];
190	slot = path->slots[0];
191	offset = btrfs_item_ptr_offset(eb, slot);
192	item_size = btrfs_item_size(eb, slot);
193	if (!IS_ALIGNED(item_size, sizeof(u64))) {
194		btrfs_warn(fs_info, "uuid item with illegal size %lu!",
195			   (unsigned long)item_size);
196		ret = -ENOENT;
197		goto out;
198	}
199	while (item_size) {
200		__le64 read_subid;
201
202		read_extent_buffer(eb, &read_subid, offset, sizeof(read_subid));
203		if (le64_to_cpu(read_subid) == subid)
204			break;
205		offset += sizeof(read_subid);
206		item_size -= sizeof(read_subid);
207	}
208
209	if (!item_size) {
210		ret = -ENOENT;
211		goto out;
212	}
213
214	item_size = btrfs_item_size(eb, slot);
215	if (item_size == sizeof(subid)) {
216		ret = btrfs_del_item(trans, uuid_root, path);
217		goto out;
218	}
219
220	move_dst = offset;
221	move_src = offset + sizeof(subid);
222	move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));
223	memmove_extent_buffer(eb, move_dst, move_src, move_len);
224	btrfs_truncate_item(path, item_size - sizeof(subid), 1);
225
226out:
227	btrfs_free_path(path);
228	return ret;
229}
230
231static int btrfs_uuid_iter_rem(struct btrfs_root *uuid_root, u8 *uuid, u8 type,
232			       u64 subid)
233{
234	struct btrfs_trans_handle *trans;
235	int ret;
236
237	/* 1 - for the uuid item */
238	trans = btrfs_start_transaction(uuid_root, 1);
239	if (IS_ERR(trans)) {
240		ret = PTR_ERR(trans);
241		goto out;
242	}
243
244	ret = btrfs_uuid_tree_remove(trans, uuid, type, subid);
245	btrfs_end_transaction(trans);
246
247out:
248	return ret;
249}
250
251/*
252 * Check if there's an matching subvolume for given UUID
253 *
254 * Return:
255 * 0	check succeeded, the entry is not outdated
256 * > 0	if the check failed, the caller should remove the entry
257 * < 0	if an error occurred
258 */
259static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
260				       u8 *uuid, u8 type, u64 subvolid)
261{
262	int ret = 0;
263	struct btrfs_root *subvol_root;
264
265	if (type != BTRFS_UUID_KEY_SUBVOL &&
266	    type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
267		goto out;
268
269	subvol_root = btrfs_get_fs_root(fs_info, subvolid, true);
270	if (IS_ERR(subvol_root)) {
271		ret = PTR_ERR(subvol_root);
272		if (ret == -ENOENT)
273			ret = 1;
274		goto out;
275	}
276
277	switch (type) {
278	case BTRFS_UUID_KEY_SUBVOL:
279		if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
280			ret = 1;
281		break;
282	case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
283		if (memcmp(uuid, subvol_root->root_item.received_uuid,
284			   BTRFS_UUID_SIZE))
285			ret = 1;
286		break;
287	}
288	btrfs_put_root(subvol_root);
289out:
290	return ret;
291}
292
293int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
294{
295	struct btrfs_root *root = fs_info->uuid_root;
296	struct btrfs_key key;
297	struct btrfs_path *path;
298	int ret = 0;
299	struct extent_buffer *leaf;
300	int slot;
301	u32 item_size;
302	unsigned long offset;
303
304	path = btrfs_alloc_path();
305	if (!path) {
306		ret = -ENOMEM;
307		goto out;
308	}
309
310	key.objectid = 0;
311	key.type = 0;
312	key.offset = 0;
313
314again_search_slot:
315	ret = btrfs_search_forward(root, &key, path, BTRFS_OLDEST_GENERATION);
316	if (ret) {
317		if (ret > 0)
318			ret = 0;
319		goto out;
320	}
321
322	while (1) {
323		if (btrfs_fs_closing(fs_info)) {
324			ret = -EINTR;
325			goto out;
326		}
327		cond_resched();
328		leaf = path->nodes[0];
329		slot = path->slots[0];
330		btrfs_item_key_to_cpu(leaf, &key, slot);
331
332		if (key.type != BTRFS_UUID_KEY_SUBVOL &&
333		    key.type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
334			goto skip;
335
336		offset = btrfs_item_ptr_offset(leaf, slot);
337		item_size = btrfs_item_size(leaf, slot);
338		if (!IS_ALIGNED(item_size, sizeof(u64))) {
339			btrfs_warn(fs_info,
340				   "uuid item with illegal size %lu!",
341				   (unsigned long)item_size);
342			goto skip;
343		}
344		while (item_size) {
345			u8 uuid[BTRFS_UUID_SIZE];
346			__le64 subid_le;
347			u64 subid_cpu;
348
349			put_unaligned_le64(key.objectid, uuid);
350			put_unaligned_le64(key.offset, uuid + sizeof(u64));
351			read_extent_buffer(leaf, &subid_le, offset,
352					   sizeof(subid_le));
353			subid_cpu = le64_to_cpu(subid_le);
354			ret = btrfs_check_uuid_tree_entry(fs_info, uuid,
355							  key.type, subid_cpu);
356			if (ret < 0)
357				goto out;
358			if (ret > 0) {
359				btrfs_release_path(path);
360				ret = btrfs_uuid_iter_rem(root, uuid, key.type,
361							  subid_cpu);
362				if (ret == 0) {
363					/*
364					 * this might look inefficient, but the
365					 * justification is that it is an
366					 * exception that check_func returns 1,
367					 * and that in the regular case only one
368					 * entry per UUID exists.
369					 */
370					goto again_search_slot;
371				}
372				if (ret < 0 && ret != -ENOENT)
373					goto out;
374				key.offset++;
375				goto again_search_slot;
376			}
377			item_size -= sizeof(subid_le);
378			offset += sizeof(subid_le);
379		}
380
381skip:
382		ret = btrfs_next_item(root, path);
383		if (ret == 0)
384			continue;
385		else if (ret > 0)
386			ret = 0;
387		break;
388	}
389
390out:
391	btrfs_free_path(path);
392	return ret;
393}
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) STRATO AG 2013.  All rights reserved.
  4 */
  5
  6#include <linux/uuid.h>
  7#include <asm/unaligned.h>
  8#include "messages.h"
  9#include "ctree.h"
 10#include "transaction.h"
 11#include "disk-io.h"
 
 12#include "fs.h"
 13#include "accessors.h"
 14#include "uuid-tree.h"
 15
 16static void btrfs_uuid_to_key(u8 *uuid, u8 type, struct btrfs_key *key)
 17{
 18	key->type = type;
 19	key->objectid = get_unaligned_le64(uuid);
 20	key->offset = get_unaligned_le64(uuid + sizeof(u64));
 21}
 22
 23/* return -ENOENT for !found, < 0 for errors, or 0 if an item was found */
 24static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, u8 *uuid,
 25				  u8 type, u64 subid)
 26{
 27	int ret;
 28	struct btrfs_path *path = NULL;
 29	struct extent_buffer *eb;
 30	int slot;
 31	u32 item_size;
 32	unsigned long offset;
 33	struct btrfs_key key;
 34
 35	if (WARN_ON_ONCE(!uuid_root)) {
 36		ret = -ENOENT;
 37		goto out;
 38	}
 39
 40	path = btrfs_alloc_path();
 41	if (!path) {
 42		ret = -ENOMEM;
 43		goto out;
 44	}
 45
 46	btrfs_uuid_to_key(uuid, type, &key);
 47	ret = btrfs_search_slot(NULL, uuid_root, &key, path, 0, 0);
 48	if (ret < 0) {
 49		goto out;
 50	} else if (ret > 0) {
 51		ret = -ENOENT;
 52		goto out;
 53	}
 54
 55	eb = path->nodes[0];
 56	slot = path->slots[0];
 57	item_size = btrfs_item_size(eb, slot);
 58	offset = btrfs_item_ptr_offset(eb, slot);
 59	ret = -ENOENT;
 60
 61	if (!IS_ALIGNED(item_size, sizeof(u64))) {
 62		btrfs_warn(uuid_root->fs_info,
 63			   "uuid item with illegal size %lu!",
 64			   (unsigned long)item_size);
 65		goto out;
 66	}
 67	while (item_size) {
 68		__le64 data;
 69
 70		read_extent_buffer(eb, &data, offset, sizeof(data));
 71		if (le64_to_cpu(data) == subid) {
 72			ret = 0;
 73			break;
 74		}
 75		offset += sizeof(data);
 76		item_size -= sizeof(data);
 77	}
 78
 79out:
 80	btrfs_free_path(path);
 81	return ret;
 82}
 83
 84int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
 85			u64 subid_cpu)
 86{
 87	struct btrfs_fs_info *fs_info = trans->fs_info;
 88	struct btrfs_root *uuid_root = fs_info->uuid_root;
 89	int ret;
 90	struct btrfs_path *path = NULL;
 91	struct btrfs_key key;
 92	struct extent_buffer *eb;
 93	int slot;
 94	unsigned long offset;
 95	__le64 subid_le;
 96
 97	ret = btrfs_uuid_tree_lookup(uuid_root, uuid, type, subid_cpu);
 98	if (ret != -ENOENT)
 99		return ret;
100
101	if (WARN_ON_ONCE(!uuid_root)) {
102		ret = -EINVAL;
103		goto out;
104	}
105
106	btrfs_uuid_to_key(uuid, type, &key);
107
108	path = btrfs_alloc_path();
109	if (!path) {
110		ret = -ENOMEM;
111		goto out;
112	}
113
114	ret = btrfs_insert_empty_item(trans, uuid_root, path, &key,
115				      sizeof(subid_le));
116	if (ret == 0) {
117		/* Add an item for the type for the first time */
118		eb = path->nodes[0];
119		slot = path->slots[0];
120		offset = btrfs_item_ptr_offset(eb, slot);
121	} else if (ret == -EEXIST) {
122		/*
123		 * An item with that type already exists.
124		 * Extend the item and store the new subid at the end.
125		 */
126		btrfs_extend_item(trans, path, sizeof(subid_le));
127		eb = path->nodes[0];
128		slot = path->slots[0];
129		offset = btrfs_item_ptr_offset(eb, slot);
130		offset += btrfs_item_size(eb, slot) - sizeof(subid_le);
131	} else {
132		btrfs_warn(fs_info,
133			   "insert uuid item failed %d (0x%016llx, 0x%016llx) type %u!",
134			   ret, key.objectid, key.offset, type);
135		goto out;
136	}
137
138	ret = 0;
139	subid_le = cpu_to_le64(subid_cpu);
140	write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));
141	btrfs_mark_buffer_dirty(trans, eb);
142
143out:
144	btrfs_free_path(path);
145	return ret;
146}
147
148int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
149			u64 subid)
150{
151	struct btrfs_fs_info *fs_info = trans->fs_info;
152	struct btrfs_root *uuid_root = fs_info->uuid_root;
153	int ret;
154	struct btrfs_path *path = NULL;
155	struct btrfs_key key;
156	struct extent_buffer *eb;
157	int slot;
158	unsigned long offset;
159	u32 item_size;
160	unsigned long move_dst;
161	unsigned long move_src;
162	unsigned long move_len;
163
164	if (WARN_ON_ONCE(!uuid_root)) {
165		ret = -EINVAL;
166		goto out;
167	}
168
169	btrfs_uuid_to_key(uuid, type, &key);
170
171	path = btrfs_alloc_path();
172	if (!path) {
173		ret = -ENOMEM;
174		goto out;
175	}
176
177	ret = btrfs_search_slot(trans, uuid_root, &key, path, -1, 1);
178	if (ret < 0) {
179		btrfs_warn(fs_info, "error %d while searching for uuid item!",
180			   ret);
181		goto out;
182	}
183	if (ret > 0) {
184		ret = -ENOENT;
185		goto out;
186	}
187
188	eb = path->nodes[0];
189	slot = path->slots[0];
190	offset = btrfs_item_ptr_offset(eb, slot);
191	item_size = btrfs_item_size(eb, slot);
192	if (!IS_ALIGNED(item_size, sizeof(u64))) {
193		btrfs_warn(fs_info, "uuid item with illegal size %lu!",
194			   (unsigned long)item_size);
195		ret = -ENOENT;
196		goto out;
197	}
198	while (item_size) {
199		__le64 read_subid;
200
201		read_extent_buffer(eb, &read_subid, offset, sizeof(read_subid));
202		if (le64_to_cpu(read_subid) == subid)
203			break;
204		offset += sizeof(read_subid);
205		item_size -= sizeof(read_subid);
206	}
207
208	if (!item_size) {
209		ret = -ENOENT;
210		goto out;
211	}
212
213	item_size = btrfs_item_size(eb, slot);
214	if (item_size == sizeof(subid)) {
215		ret = btrfs_del_item(trans, uuid_root, path);
216		goto out;
217	}
218
219	move_dst = offset;
220	move_src = offset + sizeof(subid);
221	move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));
222	memmove_extent_buffer(eb, move_dst, move_src, move_len);
223	btrfs_truncate_item(trans, path, item_size - sizeof(subid), 1);
224
225out:
226	btrfs_free_path(path);
227	return ret;
228}
229
230static int btrfs_uuid_iter_rem(struct btrfs_root *uuid_root, u8 *uuid, u8 type,
231			       u64 subid)
232{
233	struct btrfs_trans_handle *trans;
234	int ret;
235
236	/* 1 - for the uuid item */
237	trans = btrfs_start_transaction(uuid_root, 1);
238	if (IS_ERR(trans)) {
239		ret = PTR_ERR(trans);
240		goto out;
241	}
242
243	ret = btrfs_uuid_tree_remove(trans, uuid, type, subid);
244	btrfs_end_transaction(trans);
245
246out:
247	return ret;
248}
249
250/*
251 * Check if there's an matching subvolume for given UUID
252 *
253 * Return:
254 * 0	check succeeded, the entry is not outdated
255 * > 0	if the check failed, the caller should remove the entry
256 * < 0	if an error occurred
257 */
258static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
259				       u8 *uuid, u8 type, u64 subvolid)
260{
261	int ret = 0;
262	struct btrfs_root *subvol_root;
263
264	if (type != BTRFS_UUID_KEY_SUBVOL &&
265	    type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
266		goto out;
267
268	subvol_root = btrfs_get_fs_root(fs_info, subvolid, true);
269	if (IS_ERR(subvol_root)) {
270		ret = PTR_ERR(subvol_root);
271		if (ret == -ENOENT)
272			ret = 1;
273		goto out;
274	}
275
276	switch (type) {
277	case BTRFS_UUID_KEY_SUBVOL:
278		if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
279			ret = 1;
280		break;
281	case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
282		if (memcmp(uuid, subvol_root->root_item.received_uuid,
283			   BTRFS_UUID_SIZE))
284			ret = 1;
285		break;
286	}
287	btrfs_put_root(subvol_root);
288out:
289	return ret;
290}
291
292int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
293{
294	struct btrfs_root *root = fs_info->uuid_root;
295	struct btrfs_key key;
296	struct btrfs_path *path;
297	int ret = 0;
298	struct extent_buffer *leaf;
299	int slot;
300	u32 item_size;
301	unsigned long offset;
302
303	path = btrfs_alloc_path();
304	if (!path) {
305		ret = -ENOMEM;
306		goto out;
307	}
308
309	key.objectid = 0;
310	key.type = 0;
311	key.offset = 0;
312
313again_search_slot:
314	ret = btrfs_search_forward(root, &key, path, BTRFS_OLDEST_GENERATION);
315	if (ret) {
316		if (ret > 0)
317			ret = 0;
318		goto out;
319	}
320
321	while (1) {
322		if (btrfs_fs_closing(fs_info)) {
323			ret = -EINTR;
324			goto out;
325		}
326		cond_resched();
327		leaf = path->nodes[0];
328		slot = path->slots[0];
329		btrfs_item_key_to_cpu(leaf, &key, slot);
330
331		if (key.type != BTRFS_UUID_KEY_SUBVOL &&
332		    key.type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
333			goto skip;
334
335		offset = btrfs_item_ptr_offset(leaf, slot);
336		item_size = btrfs_item_size(leaf, slot);
337		if (!IS_ALIGNED(item_size, sizeof(u64))) {
338			btrfs_warn(fs_info,
339				   "uuid item with illegal size %lu!",
340				   (unsigned long)item_size);
341			goto skip;
342		}
343		while (item_size) {
344			u8 uuid[BTRFS_UUID_SIZE];
345			__le64 subid_le;
346			u64 subid_cpu;
347
348			put_unaligned_le64(key.objectid, uuid);
349			put_unaligned_le64(key.offset, uuid + sizeof(u64));
350			read_extent_buffer(leaf, &subid_le, offset,
351					   sizeof(subid_le));
352			subid_cpu = le64_to_cpu(subid_le);
353			ret = btrfs_check_uuid_tree_entry(fs_info, uuid,
354							  key.type, subid_cpu);
355			if (ret < 0)
356				goto out;
357			if (ret > 0) {
358				btrfs_release_path(path);
359				ret = btrfs_uuid_iter_rem(root, uuid, key.type,
360							  subid_cpu);
361				if (ret == 0) {
362					/*
363					 * this might look inefficient, but the
364					 * justification is that it is an
365					 * exception that check_func returns 1,
366					 * and that in the regular case only one
367					 * entry per UUID exists.
368					 */
369					goto again_search_slot;
370				}
371				if (ret < 0 && ret != -ENOENT)
372					goto out;
373				key.offset++;
374				goto again_search_slot;
375			}
376			item_size -= sizeof(subid_le);
377			offset += sizeof(subid_le);
378		}
379
380skip:
381		ret = btrfs_next_item(root, path);
382		if (ret == 0)
383			continue;
384		else if (ret > 0)
385			ret = 0;
386		break;
387	}
388
389out:
390	btrfs_free_path(path);
391	return ret;
392}